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