paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@17 | 5 | |
paul@39 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@17 | 7 | |
paul@17 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@17 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@17 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@17 | 11 | version. |
paul@17 | 12 | |
paul@17 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@17 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@17 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@17 | 16 | details. |
paul@17 | 17 | |
paul@17 | 18 | You should have received a copy of the GNU General Public License along with |
paul@17 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@17 | 20 | """ |
paul@17 | 21 | |
paul@17 | 22 | import micropython.inspect |
paul@17 | 23 | from micropython.rsvp import * |
paul@39 | 24 | from micropython.common import * |
paul@17 | 25 | import compiler.ast |
paul@17 | 26 | from compiler.visitor import ASTVisitor |
paul@17 | 27 | try: |
paul@17 | 28 | set |
paul@17 | 29 | except NameError: |
paul@17 | 30 | from sets import Set as set |
paul@17 | 31 | |
paul@63 | 32 | class TranslateError(NodeProcessingError): pass |
paul@17 | 33 | |
paul@19 | 34 | class Label: |
paul@19 | 35 | |
paul@19 | 36 | "A reference to a location." |
paul@19 | 37 | |
paul@19 | 38 | def __init__(self, number, location=None): |
paul@19 | 39 | self.number = number |
paul@19 | 40 | self.location = location |
paul@19 | 41 | |
paul@19 | 42 | def __repr__(self): |
paul@19 | 43 | return "Label(%r, location=%r)" % (self.number, self.location) |
paul@19 | 44 | |
paul@17 | 45 | # Program visitors. |
paul@17 | 46 | |
paul@17 | 47 | class Translation(ASTVisitor): |
paul@17 | 48 | |
paul@17 | 49 | "A translated module." |
paul@17 | 50 | |
paul@65 | 51 | supported_optimisations = ["constant_storage", "known_target", "self_access"] |
paul@62 | 52 | |
paul@62 | 53 | def __init__(self, module, objtable, paramtable, builtins=None, optimisations=None): |
paul@18 | 54 | |
paul@21 | 55 | """ |
paul@21 | 56 | Initialise the translation with an inspected 'module' and an attribute |
paul@21 | 57 | table 'objtable' and parameter table 'paramtable'. |
paul@21 | 58 | """ |
paul@18 | 59 | |
paul@17 | 60 | ASTVisitor.__init__(self) |
paul@17 | 61 | self.visitor = self |
paul@17 | 62 | self.module = module |
paul@21 | 63 | self.objtable = objtable |
paul@21 | 64 | self.paramtable = paramtable |
paul@61 | 65 | self.builtins = builtins |
paul@61 | 66 | |
paul@62 | 67 | # Desired optimisations. |
paul@62 | 68 | |
paul@62 | 69 | self.optimisations = set(optimisations or []) |
paul@62 | 70 | |
paul@61 | 71 | # The current unit being translated. |
paul@61 | 72 | |
paul@17 | 73 | self.unit = None |
paul@17 | 74 | |
paul@19 | 75 | # Wiring within the code. |
paul@19 | 76 | |
paul@19 | 77 | self.labels = {} |
paul@19 | 78 | self.label_number = 0 |
paul@41 | 79 | self.loop_labels = [] |
paul@57 | 80 | self.exception_labels = [] |
paul@41 | 81 | |
paul@41 | 82 | # The code itself. |
paul@41 | 83 | |
paul@19 | 84 | self.code = None |
paul@19 | 85 | |
paul@17 | 86 | def get_module_code(self): |
paul@17 | 87 | |
paul@17 | 88 | "Return the top-level module code." |
paul@17 | 89 | |
paul@17 | 90 | self.unit = self.module |
paul@19 | 91 | self.code = [] |
paul@44 | 92 | if self.module.module is not None: |
paul@44 | 93 | self.dispatch(self.module.module) |
paul@19 | 94 | return self.code |
paul@17 | 95 | |
paul@17 | 96 | def get_code(self, unit): |
paul@17 | 97 | |
paul@17 | 98 | "Return the code for the given 'unit'." |
paul@17 | 99 | |
paul@17 | 100 | self.unit = unit |
paul@19 | 101 | self.code = [] |
paul@25 | 102 | if unit.node is not None: |
paul@25 | 103 | self.dispatch(unit.node) |
paul@19 | 104 | return self.code |
paul@17 | 105 | |
paul@66 | 106 | def get_default_code(self, unit): |
paul@66 | 107 | |
paul@66 | 108 | "Return the code for the defaults in the given 'unit'." |
paul@66 | 109 | |
paul@66 | 110 | self.code = [] |
paul@66 | 111 | for attr, default in zip(unit.default_attrs, unit.defaults): |
paul@66 | 112 | self.dispatch(default) |
paul@66 | 113 | self.new_op(LoadConst(unit)) |
paul@66 | 114 | self.new_op(StoreAttr(attr)) |
paul@66 | 115 | return self.code |
paul@66 | 116 | |
paul@17 | 117 | def __repr__(self): |
paul@17 | 118 | return "Translation(%r)" % self.module |
paul@17 | 119 | |
paul@17 | 120 | def get_scope(self, name): |
paul@17 | 121 | if self.unit.has_key(name): |
paul@17 | 122 | return "local" |
paul@17 | 123 | elif self.module.has_key(name): |
paul@17 | 124 | return "global" |
paul@17 | 125 | else: |
paul@17 | 126 | return "builtins" |
paul@17 | 127 | |
paul@19 | 128 | # Code writing methods. |
paul@19 | 129 | |
paul@19 | 130 | def new_label(self): |
paul@44 | 131 | |
paul@44 | 132 | "Return a new label object for use with set_label." |
paul@44 | 133 | |
paul@19 | 134 | number = self.label_number |
paul@19 | 135 | label = Label(number) |
paul@19 | 136 | self.labels[label] = label |
paul@19 | 137 | self.label_number += 1 |
paul@19 | 138 | return label |
paul@19 | 139 | |
paul@19 | 140 | def set_label(self, label): |
paul@19 | 141 | |
paul@19 | 142 | """ |
paul@19 | 143 | Set the location of 'label' to that within the entire image: the |
paul@19 | 144 | location within the code combined with location of the code unit. |
paul@19 | 145 | """ |
paul@19 | 146 | |
paul@19 | 147 | label.location = len(self.code) + self.unit.code_location |
paul@19 | 148 | |
paul@57 | 149 | def get_loop_labels(self): |
paul@57 | 150 | return self.loop_labels[-1] |
paul@57 | 151 | |
paul@57 | 152 | def add_loop_labels(self, next_label, exit_label): |
paul@57 | 153 | self.loop_labels.append((next_label, exit_label)) |
paul@57 | 154 | |
paul@57 | 155 | def drop_loop_labels(self): |
paul@57 | 156 | self.loop_labels.pop() |
paul@57 | 157 | |
paul@57 | 158 | def get_exception_labels(self): |
paul@57 | 159 | return self.exception_labels[-1] |
paul@57 | 160 | |
paul@57 | 161 | def add_exception_labels(self, handler_label, exit_label): |
paul@57 | 162 | self.exception_labels.append((handler_label, exit_label)) |
paul@57 | 163 | |
paul@57 | 164 | def drop_exception_labels(self): |
paul@57 | 165 | self.exception_labels.pop() |
paul@57 | 166 | |
paul@19 | 167 | def new_op(self, op): |
paul@44 | 168 | |
paul@44 | 169 | "Add 'op' to the generated code." |
paul@44 | 170 | |
paul@19 | 171 | self.code.append(op) |
paul@19 | 172 | |
paul@43 | 173 | def replace_op(self, op): |
paul@44 | 174 | |
paul@44 | 175 | "Replace the last added instruction with 'op'." |
paul@44 | 176 | |
paul@43 | 177 | self.code[-1] = op |
paul@43 | 178 | |
paul@56 | 179 | def remove_ops(self, n): |
paul@56 | 180 | |
paul@56 | 181 | "Remove the last 'n' instructions." |
paul@56 | 182 | |
paul@56 | 183 | del self.code[-n:] |
paul@56 | 184 | |
paul@56 | 185 | def last_ops(self, n): |
paul@56 | 186 | |
paul@56 | 187 | "Return the last 'n' added instructions in reverse chronological order." |
paul@56 | 188 | |
paul@56 | 189 | ops = self.code[-n:] |
paul@56 | 190 | ops.reverse() |
paul@56 | 191 | return ops |
paul@56 | 192 | |
paul@26 | 193 | def last_op(self): |
paul@44 | 194 | |
paul@44 | 195 | "Return the last added instruction." |
paul@44 | 196 | |
paul@26 | 197 | return self.code[-1] |
paul@26 | 198 | |
paul@56 | 199 | # Internal helper methods. |
paul@17 | 200 | |
paul@43 | 201 | def _visitAttr(self, node, classes): |
paul@44 | 202 | |
paul@44 | 203 | """ |
paul@44 | 204 | Visit the attribute-related 'node', generating instructions based on the |
paul@44 | 205 | given 'classes'. |
paul@44 | 206 | """ |
paul@44 | 207 | |
paul@43 | 208 | self.dispatch(node.expr) |
paul@63 | 209 | self._generateAttr(node, node.attrname, classes) |
paul@43 | 210 | |
paul@63 | 211 | def _generateAttr(self, node, attrname, classes): |
paul@44 | 212 | |
paul@44 | 213 | """ |
paul@44 | 214 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@44 | 215 | """ |
paul@44 | 216 | |
paul@44 | 217 | AttrInstruction, AttrIndexInstruction = classes |
paul@43 | 218 | # NOTE: Only simple cases are used for optimisations. |
paul@43 | 219 | |
paul@43 | 220 | last = self.last_op() |
paul@54 | 221 | |
paul@56 | 222 | # Where the last operation (defining the attribute owner) yields a |
paul@56 | 223 | # constant... |
paul@54 | 224 | |
paul@62 | 225 | if self._have_constant_input(0): |
paul@62 | 226 | |
paul@62 | 227 | # Optimise away the constant storage if appropriate. |
paul@43 | 228 | |
paul@56 | 229 | if self._optimise_constant_storage(AttrInstruction, 1): |
paul@56 | 230 | return |
paul@56 | 231 | |
paul@43 | 232 | # Get the details of the access. |
paul@43 | 233 | |
paul@43 | 234 | target = last.attr.value |
paul@43 | 235 | target_name = target.full_name() |
paul@63 | 236 | |
paul@63 | 237 | try: |
paul@63 | 238 | table_entry = self.objtable.table[target_name] |
paul@63 | 239 | except KeyError: |
paul@63 | 240 | raise TranslateError(self.module.full_name(), node, |
paul@63 | 241 | "No object entry exists for target %r." % target_name) |
paul@63 | 242 | |
paul@63 | 243 | try: |
paul@63 | 244 | pos = table_entry[attrname] |
paul@63 | 245 | except KeyError: |
paul@63 | 246 | raise TranslateError(self.module.full_name(), node, |
paul@63 | 247 | "No attribute entry exists for name %r in target %r." % (attrname, target_name)) |
paul@63 | 248 | |
paul@55 | 249 | self.replace_op(AttrInstruction(pos)) |
paul@54 | 250 | |
paul@54 | 251 | # Where the last operation involves the special 'self' name, check to |
paul@65 | 252 | # see if the attribute is acceptably positioned and produce a direct |
paul@65 | 253 | # access to the attribute. |
paul@54 | 254 | |
paul@65 | 255 | elif self._optimise_self_access(attrname, AttrInstruction): |
paul@65 | 256 | pass |
paul@54 | 257 | |
paul@54 | 258 | # Otherwise, perform a normal operation. |
paul@43 | 259 | |
paul@43 | 260 | else: |
paul@63 | 261 | try: |
paul@63 | 262 | index = self.objtable.get_index(attrname) |
paul@63 | 263 | except self.objtable.TableError: |
paul@63 | 264 | raise TranslateError(self.module.full_name(), node, |
paul@63 | 265 | "No attribute entry exists for name %r." % attrname) |
paul@63 | 266 | |
paul@43 | 267 | self.new_op(AttrIndexInstruction(index)) |
paul@43 | 268 | |
paul@45 | 269 | def _startCallFunc(self): |
paul@45 | 270 | |
paul@45 | 271 | "Record the location of the invocation." |
paul@21 | 272 | |
paul@21 | 273 | self.new_op(MakeFrame()) # records the start of the frame |
paul@21 | 274 | |
paul@44 | 275 | def _generateCallFunc(self, args, node): |
paul@44 | 276 | |
paul@26 | 277 | # NOTE: Only simple cases are used for optimisations. |
paul@26 | 278 | |
paul@59 | 279 | target, context = self._optimise_known_target() |
paul@45 | 280 | |
paul@45 | 281 | # Where a target is known and has a known context, avoid generating any |
paul@59 | 282 | # first argument. Instance methods do not have a known target since they |
paul@59 | 283 | # are accessed via an instance whose identity cannot generally be known |
paul@59 | 284 | # at compile-time. |
paul@45 | 285 | |
paul@59 | 286 | if context is None: |
paul@45 | 287 | continue_label = self.new_label() |
paul@45 | 288 | self.new_op(LoadContext()) |
paul@45 | 289 | self.new_op(CheckContext()) |
paul@45 | 290 | self.new_op(JumpIfTrue(continue_label)) |
paul@57 | 291 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@45 | 292 | self.new_op(RaiseException()) |
paul@45 | 293 | self.set_label(continue_label) |
paul@59 | 294 | else: |
paul@59 | 295 | pass # NOTE: Class methods should be supported. |
paul@26 | 296 | |
paul@21 | 297 | # Evaluate the arguments. |
paul@21 | 298 | |
paul@67 | 299 | employed_positions = set() |
paul@59 | 300 | extra_keywords = [] |
paul@22 | 301 | |
paul@66 | 302 | for frame_pos, arg in enumerate(args): |
paul@66 | 303 | |
paul@66 | 304 | # Handle positional and keyword arguments separately. |
paul@66 | 305 | |
paul@21 | 306 | if isinstance(arg, compiler.ast.Keyword): |
paul@66 | 307 | |
paul@26 | 308 | # Optimise where the target is known now. |
paul@26 | 309 | |
paul@26 | 310 | if target is not None: |
paul@39 | 311 | |
paul@39 | 312 | # Find the parameter table entry for the target. |
paul@39 | 313 | |
paul@39 | 314 | target_name = target.full_name() |
paul@39 | 315 | |
paul@39 | 316 | # Look for a callable with the precise target name. |
paul@39 | 317 | |
paul@59 | 318 | table_entry = self.paramtable.table[target_name] |
paul@59 | 319 | |
paul@59 | 320 | # Look the name up in the parameter table entry. |
paul@39 | 321 | |
paul@59 | 322 | try: |
paul@59 | 323 | pos = table_entry[arg.name] |
paul@59 | 324 | |
paul@59 | 325 | # Where no position is found, this could be an extra keyword |
paul@59 | 326 | # argument. |
paul@39 | 327 | |
paul@39 | 328 | except KeyError: |
paul@59 | 329 | extra_keywords.append(arg) |
paul@59 | 330 | continue |
paul@58 | 331 | |
paul@58 | 332 | # Test for illegal conditions. |
paul@58 | 333 | |
paul@67 | 334 | if pos in employed_positions: |
paul@58 | 335 | raise TranslateError(self.module.full_name(), node, |
paul@58 | 336 | "Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@67 | 337 | |
paul@67 | 338 | employed_positions.add(pos) |
paul@58 | 339 | |
paul@67 | 340 | # Add space for arguments appearing before this one. |
paul@67 | 341 | |
paul@67 | 342 | if frame_pos < pos: |
paul@67 | 343 | self.new_op(ReserveFrame(pos - frame_pos) |
paul@59 | 344 | |
paul@59 | 345 | # Generate code for the keyword and the positioning |
paul@59 | 346 | # operation. |
paul@59 | 347 | |
paul@59 | 348 | self.dispatch(arg.expr) |
paul@66 | 349 | |
paul@66 | 350 | # If the position corresponds to the current frame element, |
paul@67 | 351 | # skip generating the store instruction. |
paul@66 | 352 | |
paul@67 | 353 | if frame_pos > pos: |
paul@66 | 354 | self.new_op(StoreFrame(pos)) |
paul@26 | 355 | |
paul@26 | 356 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 357 | # the parameter location. |
paul@26 | 358 | |
paul@26 | 359 | else: |
paul@21 | 360 | |
paul@26 | 361 | # Combine the target details with the name to get the location. |
paul@26 | 362 | # See the access method on the List class. |
paul@26 | 363 | |
paul@39 | 364 | try: |
paul@39 | 365 | paramindex = self.paramtable.get_index(arg.name) |
paul@59 | 366 | |
paul@59 | 367 | # Where no position is found, this could be an extra keyword |
paul@59 | 368 | # argument. |
paul@59 | 369 | |
paul@39 | 370 | except ValueError: |
paul@59 | 371 | extra_keywords.append(arg) |
paul@59 | 372 | continue |
paul@21 | 373 | |
paul@59 | 374 | # Generate code for the keyword and the positioning |
paul@59 | 375 | # operation. |
paul@59 | 376 | |
paul@59 | 377 | self.dispatch(arg.expr) |
paul@26 | 378 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 379 | |
paul@26 | 380 | # use (callable+0)+paramindex+table |
paul@26 | 381 | # checks embedded offset against (callable+0) |
paul@26 | 382 | # moves the top of stack to frame+position |
paul@21 | 383 | |
paul@21 | 384 | else: |
paul@21 | 385 | self.dispatch(arg) |
paul@67 | 386 | employed_positions.add(frame_pos) |
paul@21 | 387 | |
paul@66 | 388 | frame_pos = len(args) |
paul@66 | 389 | |
paul@67 | 390 | # NOTE: If any extra keywords were identified, generate them now. |
paul@67 | 391 | # NOTE: This needs to populate a ** argument dictionary. |
paul@67 | 392 | # |
paul@67 | 393 | # for arg in extra_keywords: |
paul@67 | 394 | # const = self.module.constant_values[arg.name] |
paul@67 | 395 | # self.new_op(LoadConst(const)) |
paul@67 | 396 | # self.dispatch(arg.expr) |
paul@67 | 397 | # frame_pos += 1 |
paul@59 | 398 | |
paul@59 | 399 | # NOTE: Somehow, the above needs to be combined with * and ** arguments. |
paul@59 | 400 | |
paul@59 | 401 | # Either test for a complete set of arguments. |
paul@59 | 402 | |
paul@59 | 403 | if target is not None: |
paul@66 | 404 | |
paul@66 | 405 | # Make sure that enough arguments have been given. |
paul@66 | 406 | |
paul@67 | 407 | nargs_max = len(target.positional_names) |
paul@66 | 408 | ndefaults = len(target.defaults) |
paul@66 | 409 | nargs_min = nargs_max - ndefaults |
paul@67 | 410 | |
paul@67 | 411 | for i in range(0, nargs_min): |
paul@67 | 412 | if i not in employed_positions: |
paul@67 | 413 | raise TranslateError(self.module.full_name(), node, |
paul@67 | 414 | "Argument %r not supplied for %r: need at least %d arguments." % (i+1, target.name, nargs_min)) |
paul@67 | 415 | |
paul@66 | 416 | nargs = len(args) |
paul@66 | 417 | |
paul@67 | 418 | if nargs > nargs_max and not target.has_star and not target.has_dstar: |
paul@59 | 419 | raise TranslateError(self.module.full_name(), node, |
paul@66 | 420 | "Too many arguments for %r: need at most %d arguments." % (target.name, nargs_max)) |
paul@66 | 421 | |
paul@66 | 422 | # Where defaults are involved, put them into the frame. |
paul@66 | 423 | # Here, we use negative index values to visit the right hand end of |
paul@66 | 424 | # the defaults list. |
paul@66 | 425 | |
paul@67 | 426 | for pos in range(nargs_min, nargs_max): |
paul@67 | 427 | if pos not in employed_positions: |
paul@67 | 428 | self.new_op(LoadConst(target)) |
paul@67 | 429 | self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@66 | 430 | |
paul@66 | 431 | # If the position corresponds to the current frame element, |
paul@66 | 432 | # skip generating the instruction. |
paul@66 | 433 | |
paul@66 | 434 | if frame_pos != pos: |
paul@66 | 435 | self.new_op(StoreFrame(pos)) |
paul@66 | 436 | |
paul@66 | 437 | frame_pos += 1 |
paul@59 | 438 | |
paul@59 | 439 | # Or generate instructions to do this at run-time. |
paul@66 | 440 | # NOTE: CheckFrame has to check the number of arguments and to fill in |
paul@66 | 441 | # NOTE: defaults. |
paul@59 | 442 | |
paul@59 | 443 | else: |
paul@59 | 444 | self.new_op(CheckFrame()) |
paul@59 | 445 | |
paul@57 | 446 | def _endCallFunc(self): |
paul@57 | 447 | |
paul@57 | 448 | "Make the invocation and tidy up afterwards." |
paul@57 | 449 | |
paul@21 | 450 | self.new_op(LoadCallable()) # uses the start of the frame to get the callable |
paul@68 | 451 | self.new_op(JumpWithFrame()) |
paul@21 | 452 | |
paul@21 | 453 | # NOTE: Exception handling required. |
paul@21 | 454 | |
paul@21 | 455 | self.new_op(DropFrame()) |
paul@17 | 456 | |
paul@56 | 457 | def _visitName(self, node, classes): |
paul@56 | 458 | |
paul@56 | 459 | """ |
paul@56 | 460 | Visit the name-related 'node', generating instructions based on the |
paul@56 | 461 | given 'classes'. |
paul@56 | 462 | """ |
paul@56 | 463 | |
paul@56 | 464 | name = node.name |
paul@56 | 465 | scope = self.get_scope(name) |
paul@56 | 466 | #print self.module.name, node.lineno, name, scope |
paul@56 | 467 | self._generateName(name, scope, classes, node) |
paul@56 | 468 | |
paul@56 | 469 | def _generateName(self, name, scope, classes, node): |
paul@56 | 470 | |
paul@56 | 471 | """ |
paul@56 | 472 | Generate code for the access to 'name' in 'scope' using the given |
paul@56 | 473 | 'classes', and using the given 'node' as the source of the access. |
paul@56 | 474 | """ |
paul@56 | 475 | |
paul@56 | 476 | NameInstruction, AttrInstruction = classes |
paul@56 | 477 | |
paul@56 | 478 | if self._optimise_constant_storage(NameInstruction, 0): |
paul@56 | 479 | return |
paul@56 | 480 | |
paul@56 | 481 | if scope == "local": |
paul@56 | 482 | unit = self.unit |
paul@56 | 483 | if isinstance(unit, micropython.inspect.Function): |
paul@56 | 484 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@56 | 485 | elif isinstance(unit, micropython.inspect.Class): |
paul@56 | 486 | self.new_op(AttrInstruction(unit.all_class_attributes()[name])) |
paul@56 | 487 | elif isinstance(unit, micropython.inspect.Module): |
paul@56 | 488 | self.new_op(AttrInstruction(unit.module_attributes()[name])) |
paul@56 | 489 | else: |
paul@61 | 490 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@56 | 491 | |
paul@56 | 492 | elif scope == "global": |
paul@56 | 493 | globals = self.module.module_attributes() |
paul@56 | 494 | if globals.has_key(name): |
paul@56 | 495 | self.new_op(AttrInstruction(globals[name])) |
paul@56 | 496 | else: |
paul@61 | 497 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@56 | 498 | |
paul@56 | 499 | else: |
paul@61 | 500 | self.new_op(AttrInstruction(self._get_builtin(name, node))) |
paul@56 | 501 | |
paul@61 | 502 | def _get_builtin(self, name, node): |
paul@61 | 503 | if self.builtins is not None: |
paul@63 | 504 | try: |
paul@63 | 505 | return self.builtins[name] |
paul@63 | 506 | except KeyError: |
paul@63 | 507 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@61 | 508 | else: |
paul@61 | 509 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@56 | 510 | |
paul@62 | 511 | # Optimisation tests. |
paul@62 | 512 | |
paul@62 | 513 | def _should_optimise_constant_storage(self): |
paul@62 | 514 | return "constant_storage" in self.optimisations |
paul@62 | 515 | |
paul@62 | 516 | def _should_optimise_known_target(self): |
paul@62 | 517 | return "known_target" in self.optimisations |
paul@62 | 518 | |
paul@65 | 519 | def _should_optimise_self_access(self): |
paul@65 | 520 | return "self_access" in self.optimisations |
paul@65 | 521 | |
paul@62 | 522 | def _have_constant_input(self, n): |
paul@62 | 523 | last = self.last_ops(n+1) |
paul@62 | 524 | return len(last) > n and (isinstance(last[n], LoadAttr) and last[n].attr.assignments == 1 or |
paul@62 | 525 | isinstance(last[n], LoadConst)) |
paul@62 | 526 | |
paul@62 | 527 | def _have_known_target(self): |
paul@62 | 528 | return self._have_constant_input(0) |
paul@62 | 529 | |
paul@65 | 530 | def _have_self_input(self): |
paul@65 | 531 | last = self.last_op() |
paul@65 | 532 | return isinstance(self.unit, micropython.inspect.Function) and \ |
paul@65 | 533 | self.unit.is_method() and isinstance(last, LoadName) and \ |
paul@65 | 534 | last.attr.name == "self" |
paul@65 | 535 | |
paul@62 | 536 | # Optimisation methods. See the supported_optimisations class attribute. |
paul@56 | 537 | |
paul@65 | 538 | def _optimise_constant_storage(self, instruction, n): |
paul@56 | 539 | |
paul@56 | 540 | """ |
paul@56 | 541 | Where this operation should store a constant into a target which is |
paul@56 | 542 | also constant, optimise away both operations. |
paul@56 | 543 | """ |
paul@56 | 544 | |
paul@65 | 545 | if self._should_optimise_constant_storage() and \ |
paul@65 | 546 | instruction in (StoreAttr, StoreName) and \ |
paul@65 | 547 | self._have_constant_input(n) and \ |
paul@65 | 548 | (n == 0 or self._have_constant_input(n-1)): |
paul@65 | 549 | |
paul@56 | 550 | self.remove_ops(n+1) |
paul@56 | 551 | return 1 |
paul@56 | 552 | else: |
paul@56 | 553 | return 0 |
paul@56 | 554 | |
paul@59 | 555 | def _optimise_known_target(self): |
paul@59 | 556 | |
paul@59 | 557 | """ |
paul@59 | 558 | Where the target of an invocation is known, provide information about it |
paul@59 | 559 | and its context. If a class is being invoked and the conditions are |
paul@59 | 560 | appropriate, get information about the specific initialiser. |
paul@59 | 561 | """ |
paul@59 | 562 | |
paul@62 | 563 | if self._should_optimise_known_target() and self._have_known_target(): |
paul@62 | 564 | last = self.last_op() |
paul@59 | 565 | target = last.attr.value |
paul@59 | 566 | context = last.attr.parent |
paul@59 | 567 | |
paul@59 | 568 | # Handle calls to classes. |
paul@59 | 569 | |
paul@59 | 570 | if isinstance(target, micropython.inspect.Class): |
paul@61 | 571 | target = target.get_instantiator() |
paul@59 | 572 | context = micropython.inspect.Instance() |
paul@59 | 573 | |
paul@59 | 574 | # A special context is chosen to avoid generating unnecessary |
paul@59 | 575 | # context loading and checking instructions. |
paul@59 | 576 | |
paul@59 | 577 | else: |
paul@59 | 578 | target = None |
paul@59 | 579 | context = None |
paul@59 | 580 | |
paul@59 | 581 | return target, context |
paul@59 | 582 | |
paul@65 | 583 | def _optimise_self_access(self, attrname, instruction): |
paul@65 | 584 | |
paul@65 | 585 | """ |
paul@65 | 586 | Where the provided 'attrname' accesses an attribute which occupies the |
paul@65 | 587 | same position in all possible objects which can be accessed, generate an |
paul@65 | 588 | 'instruction' accessing the attribute directly. |
paul@65 | 589 | """ |
paul@65 | 590 | |
paul@65 | 591 | if self._should_optimise_self_access() and self._have_self_input() and \ |
paul@65 | 592 | not self.unit.is_relocated(attrname): |
paul@65 | 593 | |
paul@65 | 594 | attr = self.unit.parent.all_attributes()[attrname] |
paul@65 | 595 | self.new_op(instruction(attr)) |
paul@65 | 596 | return 1 |
paul@65 | 597 | else: |
paul@65 | 598 | return 0 |
paul@65 | 599 | |
paul@56 | 600 | # Visitor methods. |
paul@56 | 601 | |
paul@56 | 602 | def default(self, node, *args): |
paul@56 | 603 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@56 | 604 | |
paul@56 | 605 | def dispatch(self, node, *args): |
paul@56 | 606 | return ASTVisitor.dispatch(self, node, *args) |
paul@56 | 607 | |
paul@57 | 608 | def _visitBinary(self, node, left_method, right_method): |
paul@56 | 609 | |
paul@56 | 610 | """ |
paul@56 | 611 | _t1 = node.left |
paul@56 | 612 | _t2 = node.right |
paul@56 | 613 | try: |
paul@56 | 614 | _t1.__add__(_t2) |
paul@56 | 615 | except AttributeError: |
paul@56 | 616 | _t2.__radd__(_t1) |
paul@56 | 617 | """ |
paul@56 | 618 | |
paul@57 | 619 | right_label = self.new_label() |
paul@57 | 620 | end_label = self.new_label() |
paul@57 | 621 | |
paul@57 | 622 | # NOTE: Decide on temporary storage access. |
paul@57 | 623 | |
paul@57 | 624 | self.dispatch(node.left) |
paul@57 | 625 | self.new_op(StoreTemp(1)) |
paul@57 | 626 | self.dispatch(node.right) |
paul@57 | 627 | self.new_op(StoreTemp(2)) |
paul@57 | 628 | |
paul@57 | 629 | # Left method. |
paul@57 | 630 | |
paul@57 | 631 | self._startCallFunc() |
paul@57 | 632 | self.new_op(LoadTemp(1)) |
paul@63 | 633 | self._generateAttr(node, left_method, (LoadAttr, LoadAttrIndex)) |
paul@57 | 634 | self.new_op(LoadTemp(1)) # Explicit context as first argument. |
paul@57 | 635 | self.new_op(LoadTemp(2)) |
paul@57 | 636 | self._endCallFunc() |
paul@57 | 637 | |
paul@57 | 638 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@57 | 639 | self.new_op(CheckException()) |
paul@57 | 640 | self.new_op(JumpIfFalse(end_label)) |
paul@57 | 641 | |
paul@57 | 642 | # Right method. |
paul@57 | 643 | |
paul@57 | 644 | self.set_label(right_label) |
paul@57 | 645 | self._startCallFunc() |
paul@57 | 646 | self.new_op(LoadTemp(2)) |
paul@63 | 647 | self._generateAttr(node, right_method, (LoadAttr, LoadAttrIndex)) |
paul@57 | 648 | self.new_op(LoadTemp(2)) # Explicit context as first argument. |
paul@57 | 649 | self.new_op(LoadTemp(1)) |
paul@57 | 650 | self._endCallFunc() |
paul@57 | 651 | |
paul@57 | 652 | self.set_label(end_label) |
paul@57 | 653 | |
paul@57 | 654 | def visitAdd(self, node): |
paul@57 | 655 | self._visitBinary(node, "__add__", "__radd__") |
paul@57 | 656 | |
paul@56 | 657 | def visitAnd(self, node): pass |
paul@56 | 658 | |
paul@56 | 659 | def visitAssert(self, node): pass |
paul@56 | 660 | |
paul@56 | 661 | def visitAssign(self, node): |
paul@56 | 662 | self.dispatch(node.expr) |
paul@56 | 663 | for n in node.nodes: |
paul@56 | 664 | self.dispatch(n) |
paul@56 | 665 | |
paul@56 | 666 | def visitAssAttr(self, node): |
paul@56 | 667 | self._visitAttr(node, (StoreAttr, StoreAttrIndex)) |
paul@56 | 668 | |
paul@56 | 669 | def visitAssList(self, node): pass |
paul@56 | 670 | |
paul@56 | 671 | def visitAssName(self, node): |
paul@56 | 672 | self._visitName(node, (StoreName, StoreAttr)) |
paul@56 | 673 | |
paul@56 | 674 | visitAssTuple = visitAssList |
paul@56 | 675 | |
paul@56 | 676 | def visitAugAssign(self, node): pass |
paul@56 | 677 | |
paul@56 | 678 | def visitBackquote(self, node): pass |
paul@56 | 679 | |
paul@56 | 680 | def visitBitand(self, node): pass |
paul@56 | 681 | |
paul@56 | 682 | def visitBitor(self, node): pass |
paul@56 | 683 | |
paul@56 | 684 | def visitBitxor(self, node): pass |
paul@56 | 685 | |
paul@56 | 686 | def visitBreak(self, node): |
paul@57 | 687 | next_label, exit_label = self.get_loop_labels() |
paul@56 | 688 | self.new_op(Jump(exit_label)) |
paul@56 | 689 | |
paul@56 | 690 | def visitCallFunc(self, node): |
paul@56 | 691 | |
paul@56 | 692 | """ |
paul@56 | 693 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@56 | 694 | the correct location, then invoke the function. |
paul@56 | 695 | """ |
paul@56 | 696 | |
paul@56 | 697 | # Mark the frame, evaluate the target, generate the call. |
paul@56 | 698 | |
paul@56 | 699 | self._startCallFunc() |
paul@56 | 700 | self.dispatch(node.node) |
paul@56 | 701 | self._generateCallFunc(node.args, node) |
paul@57 | 702 | self._endCallFunc() |
paul@56 | 703 | |
paul@38 | 704 | def visitClass(self, node): |
paul@38 | 705 | unit = self.unit |
paul@39 | 706 | self.unit = node.unit |
paul@44 | 707 | self.unit.code_location = self.module.code_location # class body code is not independently addressable |
paul@38 | 708 | self.dispatch(node.code) |
paul@38 | 709 | self.unit = unit |
paul@17 | 710 | |
paul@21 | 711 | def visitCompare(self, node): |
paul@21 | 712 | |
paul@21 | 713 | """ |
paul@21 | 714 | self.dispatch(node.expr) |
paul@21 | 715 | for op_name, next_node in compare.ops: |
paul@21 | 716 | methods = self.comparison_methods[op_name] |
paul@21 | 717 | if methods is not None: |
paul@21 | 718 | # Generate method call using evaluated argument and next node. |
paul@21 | 719 | else: |
paul@21 | 720 | # Deal with the special operators. |
paul@21 | 721 | # Provide short-circuiting. |
paul@21 | 722 | """ |
paul@17 | 723 | |
paul@22 | 724 | def visitConst(self, node): |
paul@22 | 725 | const = self.module.constant_values[node.value] |
paul@22 | 726 | self.new_op(LoadConst(const)) |
paul@17 | 727 | |
paul@41 | 728 | def visitContinue(self, node): |
paul@57 | 729 | next_label, exit_label = self.get_loop_labels() |
paul@41 | 730 | self.new_op(Jump(next_label)) |
paul@17 | 731 | |
paul@19 | 732 | def visitDecorators(self, node): pass |
paul@17 | 733 | |
paul@19 | 734 | def visitDict(self, node): pass |
paul@17 | 735 | |
paul@18 | 736 | def visitDiscard(self, node): |
paul@19 | 737 | self.dispatch(node.expr) |
paul@17 | 738 | |
paul@57 | 739 | def visitDiv(self, node): |
paul@57 | 740 | self._visitBinary(node, "__div__", "__rdiv__") |
paul@17 | 741 | |
paul@19 | 742 | def visitEllipsis(self, node): pass |
paul@17 | 743 | |
paul@19 | 744 | def visitExec(self, node): pass |
paul@17 | 745 | |
paul@19 | 746 | def visitExpression(self, node): pass |
paul@17 | 747 | |
paul@57 | 748 | def visitFloorDiv(self, node): |
paul@57 | 749 | self._visitBinary(node, "__floordiv__", "__rfloordiv__") |
paul@17 | 750 | |
paul@44 | 751 | def visitFor(self, node): |
paul@44 | 752 | exit_label = self.new_label() |
paul@44 | 753 | next_label = self.new_label() |
paul@44 | 754 | else_label = self.new_label() |
paul@44 | 755 | |
paul@44 | 756 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 757 | |
paul@45 | 758 | self._startCallFunc() |
paul@44 | 759 | self.dispatch(node.list) |
paul@63 | 760 | self._generateAttr(node, "__iter__", (LoadAttr, LoadAttrIndex)) |
paul@44 | 761 | self._generateCallFunc([], node) |
paul@57 | 762 | self._endCallFunc() |
paul@57 | 763 | |
paul@44 | 764 | # Iterator on stack. |
paul@44 | 765 | |
paul@44 | 766 | # In the loop... |
paul@44 | 767 | |
paul@44 | 768 | self.set_label(next_label) |
paul@44 | 769 | |
paul@44 | 770 | # Use the iterator to get the next value. |
paul@44 | 771 | |
paul@45 | 772 | self._startCallFunc() |
paul@44 | 773 | self.new_op(Duplicate()) |
paul@63 | 774 | self._generateAttr(node, "next", (LoadAttr, LoadAttrIndex)) |
paul@44 | 775 | self._generateCallFunc([], node) |
paul@57 | 776 | self._endCallFunc() |
paul@44 | 777 | |
paul@44 | 778 | # Test for StopIteration. |
paul@44 | 779 | |
paul@57 | 780 | self.dispatch(compiler.ast.Name("StopIteration")) |
paul@57 | 781 | self.new_op(CheckException()) |
paul@44 | 782 | if node.else_ is not None: |
paul@44 | 783 | self.new_op(JumpIfTrue(else_label)) |
paul@44 | 784 | else: |
paul@44 | 785 | self.new_op(JumpIfTrue(exit_label)) |
paul@44 | 786 | |
paul@44 | 787 | # Assign to the target. |
paul@44 | 788 | |
paul@44 | 789 | self.dispatch(node.assign) |
paul@44 | 790 | |
paul@44 | 791 | # Process the body with the current next and exit points. |
paul@44 | 792 | |
paul@57 | 793 | self.add_loop_labels(next_label, exit_label) |
paul@44 | 794 | self.dispatch(node.body) |
paul@57 | 795 | self.drop_loop_labels() |
paul@44 | 796 | |
paul@44 | 797 | # Repeat the loop. |
paul@44 | 798 | |
paul@44 | 799 | self.new_op(Jump(next_label)) |
paul@44 | 800 | |
paul@44 | 801 | # Produce the "else" section. |
paul@44 | 802 | |
paul@44 | 803 | if node.else_ is not None: |
paul@44 | 804 | self.set_label(exit_label) |
paul@44 | 805 | self.dispatch(node.else_) |
paul@44 | 806 | |
paul@44 | 807 | # Pop the iterator. |
paul@44 | 808 | |
paul@44 | 809 | self.set_label(exit_label) |
paul@44 | 810 | self.new_op(Pop()) |
paul@17 | 811 | |
paul@19 | 812 | def visitFrom(self, node): pass |
paul@17 | 813 | |
paul@17 | 814 | def visitFunction(self, node): |
paul@17 | 815 | |
paul@17 | 816 | # Only store the name when visiting this node from outside. |
paul@17 | 817 | |
paul@39 | 818 | if self.unit is not node.unit: |
paul@55 | 819 | self.new_op(LoadConst(node.unit)) |
paul@19 | 820 | self._visitName(node, (StoreName, StoreAttr)) |
paul@22 | 821 | |
paul@22 | 822 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 823 | |
paul@17 | 824 | else: |
paul@19 | 825 | self.dispatch(node.code) |
paul@68 | 826 | if not isinstance(self.last_op(), Return): |
paul@68 | 827 | self.dispatch(compiler.ast.Name("None")) |
paul@68 | 828 | self.new_op(Return()) |
paul@17 | 829 | |
paul@19 | 830 | def visitGenExpr(self, node): pass |
paul@17 | 831 | |
paul@19 | 832 | def visitGenExprFor(self, node): pass |
paul@17 | 833 | |
paul@19 | 834 | def visitGenExprIf(self, node): pass |
paul@17 | 835 | |
paul@19 | 836 | def visitGenExprInner(self, node): pass |
paul@17 | 837 | |
paul@43 | 838 | def visitGetattr(self, node): |
paul@43 | 839 | self._visitAttr(node, (LoadAttr, LoadAttrIndex)) |
paul@17 | 840 | |
paul@19 | 841 | def visitGlobal(self, node): pass |
paul@17 | 842 | |
paul@17 | 843 | def visitIf(self, node): |
paul@19 | 844 | first = 1 |
paul@19 | 845 | exit_label = self.new_label() |
paul@17 | 846 | |
paul@19 | 847 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 848 | if body is None: |
paul@19 | 849 | break |
paul@19 | 850 | if not first: |
paul@19 | 851 | self.set_label(next_label) |
paul@19 | 852 | if test is not None: |
paul@19 | 853 | self.dispatch(test) |
paul@19 | 854 | next_label = self.new_label() |
paul@41 | 855 | self.new_op(JumpIfFalse(next_label)) |
paul@19 | 856 | self.dispatch(body) |
paul@19 | 857 | self.new_op(Jump(exit_label)) |
paul@19 | 858 | first = 0 |
paul@17 | 859 | |
paul@19 | 860 | self.set_label(exit_label) |
paul@17 | 861 | |
paul@19 | 862 | def visitImport(self, node): pass |
paul@19 | 863 | |
paul@19 | 864 | def visitInvert(self, node): pass |
paul@17 | 865 | |
paul@19 | 866 | def visitKeyword(self, node): pass |
paul@17 | 867 | |
paul@19 | 868 | def visitLambda(self, node): pass |
paul@19 | 869 | |
paul@19 | 870 | def visitLeftShift(self, node): pass |
paul@17 | 871 | |
paul@19 | 872 | def visitList(self, node): pass |
paul@17 | 873 | |
paul@19 | 874 | def visitListComp(self, node): pass |
paul@17 | 875 | |
paul@19 | 876 | def visitListCompFor(self, node): pass |
paul@17 | 877 | |
paul@19 | 878 | def visitListCompIf(self, node): pass |
paul@19 | 879 | |
paul@57 | 880 | def visitMod(self, node): |
paul@57 | 881 | self._visitBinary(node, "__mod__", "__rmod__") |
paul@17 | 882 | |
paul@17 | 883 | def visitModule(self, node): |
paul@19 | 884 | self.dispatch(node.node) |
paul@17 | 885 | |
paul@57 | 886 | def visitMul(self, node): |
paul@57 | 887 | self._visitBinary(node, "__mul__", "__rmul__") |
paul@17 | 888 | |
paul@17 | 889 | def visitName(self, node): |
paul@19 | 890 | self._visitName(node, (LoadName, LoadAttr)) |
paul@17 | 891 | |
paul@19 | 892 | def visitNot(self, node): pass |
paul@17 | 893 | |
paul@19 | 894 | def visitOr(self, node): pass |
paul@17 | 895 | |
paul@19 | 896 | def visitPass(self, node): pass |
paul@17 | 897 | |
paul@19 | 898 | def visitPower(self, node): pass |
paul@17 | 899 | |
paul@19 | 900 | def visitPrint(self, node): pass |
paul@17 | 901 | |
paul@19 | 902 | def visitPrintnl(self, node): pass |
paul@17 | 903 | |
paul@19 | 904 | def visitRaise(self, node): pass |
paul@17 | 905 | |
paul@21 | 906 | def visitReturn(self, node): |
paul@21 | 907 | if node.value is not None: |
paul@21 | 908 | self.dispatch(node.value) |
paul@68 | 909 | else: |
paul@68 | 910 | self.dispatch(compiler.ast.Name("None")) |
paul@21 | 911 | self.new_op(Return()) |
paul@17 | 912 | |
paul@19 | 913 | def visitRightShift(self, node): pass |
paul@17 | 914 | |
paul@19 | 915 | def visitSlice(self, node): pass |
paul@17 | 916 | |
paul@17 | 917 | def visitStmt(self, node): |
paul@17 | 918 | for n in node.nodes: |
paul@19 | 919 | self.dispatch(n) |
paul@19 | 920 | |
paul@57 | 921 | def visitSub(self, node): |
paul@57 | 922 | self._visitBinary(node, "__sub__", "__rsub__") |
paul@17 | 923 | |
paul@19 | 924 | def visitSubscript(self, node): pass |
paul@17 | 925 | |
paul@57 | 926 | def visitTryExcept(self, node): |
paul@57 | 927 | |
paul@57 | 928 | """ |
paul@57 | 929 | Enter try block. |
paul@57 | 930 | Dispatch to code. |
paul@57 | 931 | |
paul@57 | 932 | """ |
paul@57 | 933 | |
paul@57 | 934 | exit_label = self.new_label() |
paul@57 | 935 | handler_label = self.new_label() |
paul@57 | 936 | |
paul@57 | 937 | self.add_exception_labels(handler_label, exit_label) |
paul@57 | 938 | |
paul@68 | 939 | # Try... |
paul@68 | 940 | # Produce the code, then jump to the exit. |
paul@68 | 941 | |
paul@57 | 942 | self.dispatch(node.body) |
paul@57 | 943 | self.new_op(Jump(exit_label)) |
paul@57 | 944 | |
paul@68 | 945 | # Start of handlers. |
paul@68 | 946 | |
paul@57 | 947 | self.set_label(handler_label) |
paul@57 | 948 | for name, assignment, handler in node.handlers: |
paul@57 | 949 | next_label = self.new_label() |
paul@57 | 950 | |
paul@68 | 951 | # Test the given exception against the current exception. |
paul@68 | 952 | |
paul@57 | 953 | if name is not None: |
paul@57 | 954 | self.dispatch(name) |
paul@57 | 955 | self.new_op(CheckException()) |
paul@57 | 956 | self.new_op(JumpIfFalse(next_label)) |
paul@57 | 957 | |
paul@68 | 958 | # Handle assignment to exception variable. |
paul@68 | 959 | |
paul@57 | 960 | if assignment is not None: |
paul@57 | 961 | self.dispatch(assignment) |
paul@57 | 962 | |
paul@68 | 963 | # Produce the handler code, then jump to the exit. |
paul@68 | 964 | |
paul@57 | 965 | self.dispatch(handler) |
paul@57 | 966 | self.new_op(Jump(exit_label)) |
paul@57 | 967 | |
paul@57 | 968 | self.set_label(next_label) |
paul@57 | 969 | |
paul@57 | 970 | # Unhandled exceptions. |
paul@57 | 971 | |
paul@57 | 972 | self.new_op(RaiseException()) |
paul@57 | 973 | |
paul@57 | 974 | # After exception |
paul@57 | 975 | |
paul@57 | 976 | self.set_label(exit_label) |
paul@57 | 977 | |
paul@57 | 978 | # Optional else clause. |
paul@57 | 979 | |
paul@57 | 980 | if node.else_ is not None: |
paul@57 | 981 | self.dispatch(node.else_) |
paul@57 | 982 | |
paul@57 | 983 | self.drop_exception_labels() |
paul@17 | 984 | |
paul@19 | 985 | def visitTryFinally(self, node): pass |
paul@17 | 986 | |
paul@19 | 987 | def visitTuple(self, node): pass |
paul@17 | 988 | |
paul@19 | 989 | def visitUnaryAdd(self, node): pass |
paul@17 | 990 | |
paul@19 | 991 | def visitUnarySub(self, node): pass |
paul@17 | 992 | |
paul@41 | 993 | def visitWhile(self, node): |
paul@41 | 994 | exit_label = self.new_label() |
paul@44 | 995 | next_label = self.new_label() |
paul@44 | 996 | else_label = self.new_label() |
paul@41 | 997 | |
paul@41 | 998 | self.set_label(next_label) |
paul@44 | 999 | self.dispatch(node.test) |
paul@44 | 1000 | if node.else_ is not None: |
paul@44 | 1001 | self.new_op(JumpIfFalse(else_label)) |
paul@44 | 1002 | else: |
paul@44 | 1003 | self.new_op(JumpIfFalse(exit_label)) |
paul@44 | 1004 | |
paul@57 | 1005 | self.add_loop_labels(next_label, exit_label) |
paul@41 | 1006 | |
paul@41 | 1007 | self.dispatch(node.body) |
paul@41 | 1008 | self.new_op(Jump(next_label)) |
paul@41 | 1009 | |
paul@41 | 1010 | if node.else_ is not None: |
paul@44 | 1011 | self.set_label(else_label) |
paul@41 | 1012 | self.dispatch(node.else_) |
paul@41 | 1013 | |
paul@41 | 1014 | self.set_label(exit_label) |
paul@57 | 1015 | self.drop_loop_labels() |
paul@17 | 1016 | |
paul@19 | 1017 | def visitWith(self, node): pass |
paul@17 | 1018 | |
paul@19 | 1019 | def visitYield(self, node): pass |
paul@17 | 1020 | |
paul@21 | 1021 | # Useful data. |
paul@21 | 1022 | |
paul@21 | 1023 | comparison_methods = { |
paul@21 | 1024 | "==" : ("__eq__", "__ne__"), |
paul@21 | 1025 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 1026 | "<" : ("__lt__", "__gt__"), |
paul@21 | 1027 | "<=" : ("__le__", "__ge__"), |
paul@21 | 1028 | ">=" : ("__ge__", "__le__"), |
paul@21 | 1029 | ">" : ("__gt__", "__lt__"), |
paul@21 | 1030 | "is" : None, |
paul@21 | 1031 | "is not" : None, |
paul@21 | 1032 | "in" : None, |
paul@21 | 1033 | "not in" : None |
paul@21 | 1034 | } |
paul@21 | 1035 | |
paul@17 | 1036 | # vim: tabstop=4 expandtab shiftwidth=4 |