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