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