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