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@506 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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@83 | 22 | from micropython.common import * |
paul@83 | 23 | from micropython.data import * |
paul@555 | 24 | from micropython.errors import * |
paul@17 | 25 | from micropython.rsvp import * |
paul@196 | 26 | from micropython.trans import Helper |
paul@438 | 27 | from micropython.code import Assembler |
paul@17 | 28 | import compiler.ast |
paul@17 | 29 | |
paul@17 | 30 | # Program visitors. |
paul@17 | 31 | |
paul@438 | 32 | class Translation(ASTVisitor, Assembler, Helper): |
paul@17 | 33 | |
paul@438 | 34 | "A module translator." |
paul@17 | 35 | |
paul@122 | 36 | # Attribute access instructions, for use with the appropriate handlers. |
paul@122 | 37 | |
paul@194 | 38 | attribute_load_instructions = ( |
paul@194 | 39 | LoadAddress, LoadAddressContext, LoadAddressContextCond, |
paul@226 | 40 | LoadAttr, LoadAttrIndex, LoadAttrIndexContextCond |
paul@194 | 41 | ) |
paul@405 | 42 | # attribute_store_instructions are defined by the optimiser |
paul@122 | 43 | |
paul@122 | 44 | # Name access instructions, for use with the appropriate handlers. |
paul@122 | 45 | |
paul@223 | 46 | name_load_instructions = (LoadName, LoadAddress, None) |
paul@223 | 47 | name_store_instructions = (StoreName, StoreAddress, StoreAddressContext) |
paul@98 | 48 | |
paul@175 | 49 | def __init__(self, module, program): |
paul@18 | 50 | |
paul@21 | 51 | """ |
paul@175 | 52 | Initialise the translation with an inspected 'module' and the 'program' |
paul@175 | 53 | container. |
paul@21 | 54 | """ |
paul@18 | 55 | |
paul@479 | 56 | Assembler.__init__(self, program) |
paul@17 | 57 | self.visitor = self |
paul@17 | 58 | self.module = module |
paul@72 | 59 | |
paul@72 | 60 | # Global program dependencies. |
paul@72 | 61 | |
paul@479 | 62 | self.native = program.native |
paul@175 | 63 | self.objtable = self.program.get_object_table() |
paul@175 | 64 | self.paramtable = self.program.get_parameter_table() |
paul@175 | 65 | self.importer = self.program.get_importer() |
paul@72 | 66 | self.builtins = self.importer.modules.get("__builtins__") |
paul@61 | 67 | |
paul@438 | 68 | # Status flags. |
paul@17 | 69 | |
paul@235 | 70 | self.in_exception_handler = 0 |
paul@374 | 71 | self.in_assignment = 0 # for slicing and subscript |
paul@41 | 72 | |
paul@438 | 73 | # Reset the assembler. |
paul@438 | 74 | |
paul@156 | 75 | self.reset() |
paul@19 | 76 | |
paul@95 | 77 | def __repr__(self): |
paul@95 | 78 | return "Translation(%r)" % self.module |
paul@72 | 79 | |
paul@242 | 80 | def get_module_code(self): |
paul@139 | 81 | |
paul@139 | 82 | """ |
paul@242 | 83 | Return the top-level module code. |
paul@139 | 84 | """ |
paul@17 | 85 | |
paul@17 | 86 | self.unit = self.module |
paul@156 | 87 | self.reset() |
paul@398 | 88 | self.add_exception_unit() |
paul@73 | 89 | |
paul@163 | 90 | block = self.new_block() |
paul@163 | 91 | self.set_block(block) |
paul@163 | 92 | |
paul@375 | 93 | # Handle exceptions for the program. |
paul@375 | 94 | |
paul@375 | 95 | if self.module.name == "__main__": |
paul@375 | 96 | handler_block = self.new_block() |
paul@375 | 97 | self.new_op(PushHandler(handler_block)) |
paul@375 | 98 | |
paul@375 | 99 | # Generate code for the module. |
paul@375 | 100 | |
paul@343 | 101 | if self.module.astnode is not None: |
paul@343 | 102 | self.dispatch(self.module.astnode) |
paul@73 | 103 | |
paul@139 | 104 | # Finish off the translated program if appropriate. |
paul@139 | 105 | |
paul@242 | 106 | if self.module.name == "__main__": |
paul@375 | 107 | self.set_block(handler_block) |
paul@398 | 108 | self.new_op(PopHandler(1)) |
paul@562 | 109 | else: |
paul@562 | 110 | self.new_op(DropFrame()) |
paul@562 | 111 | |
paul@562 | 112 | # All modules return if invoked. |
paul@562 | 113 | |
paul@562 | 114 | self.new_op(Return()) |
paul@139 | 115 | |
paul@398 | 116 | self.drop_exception_unit() |
paul@116 | 117 | self.unit.temp_usage = self.max_temp_position + 1 |
paul@173 | 118 | self.unit.blocks = self.blocks |
paul@163 | 119 | return self.blocks |
paul@17 | 120 | |
paul@17 | 121 | def get_code(self, unit): |
paul@17 | 122 | |
paul@17 | 123 | "Return the code for the given 'unit'." |
paul@17 | 124 | |
paul@17 | 125 | self.unit = unit |
paul@156 | 126 | self.reset() |
paul@398 | 127 | self.add_exception_unit() |
paul@73 | 128 | |
paul@163 | 129 | block = self.new_block() |
paul@163 | 130 | self.set_block(block) |
paul@163 | 131 | |
paul@91 | 132 | if unit.astnode is not None: |
paul@91 | 133 | self.dispatch(unit.astnode) |
paul@73 | 134 | |
paul@398 | 135 | self.drop_exception_unit() |
paul@208 | 136 | self.unit.temp_usage = self.max_temp_position + 2 # include space for instantiators to expand backwards |
paul@173 | 137 | self.unit.blocks = self.blocks |
paul@163 | 138 | return self.blocks |
paul@17 | 139 | |
paul@137 | 140 | def get_instantiator_code(self, cls): |
paul@137 | 141 | |
paul@137 | 142 | "Return the code for the given class 'cls'." |
paul@137 | 143 | |
paul@408 | 144 | # Obtain the function object to be populated. |
paul@408 | 145 | |
paul@137 | 146 | self.unit = cls.get_instantiator() |
paul@156 | 147 | self.reset() |
paul@137 | 148 | |
paul@163 | 149 | block = self.new_block() |
paul@163 | 150 | self.set_block(block) |
paul@163 | 151 | |
paul@137 | 152 | init_method = cls.get_init_method() |
paul@137 | 153 | |
paul@230 | 154 | # Make an object and store it in the unused first slot. |
paul@137 | 155 | |
paul@233 | 156 | self.make_instance(cls, len(cls.instance_attributes())) |
paul@230 | 157 | self.new_op(StoreTemp(0)) |
paul@137 | 158 | |
paul@137 | 159 | # Invoke the appropriate initialiser. |
paul@137 | 160 | |
paul@223 | 161 | self.new_op(LoadFunction(init_method)) |
paul@137 | 162 | self.new_op(LoadCallable()) |
paul@562 | 163 | self.new_op(JumpWithFrame()) |
paul@137 | 164 | |
paul@137 | 165 | # Store the object as the result. |
paul@137 | 166 | |
paul@455 | 167 | self.new_op(LoadTemp(0)) # load the context from the locals |
paul@137 | 168 | self.new_op(Return()) |
paul@137 | 169 | |
paul@173 | 170 | self.unit.blocks = self.blocks |
paul@163 | 171 | return self.blocks |
paul@137 | 172 | |
paul@103 | 173 | # Visitor methods. |
paul@103 | 174 | |
paul@103 | 175 | def default(self, node, *args): |
paul@339 | 176 | raise TranslateError("Node class %r is not supported." % node.__class__) |
paul@103 | 177 | |
paul@103 | 178 | # Concrete visitor methods. |
paul@103 | 179 | |
paul@160 | 180 | # Binary operators. |
paul@160 | 181 | |
paul@552 | 182 | visitBitand = \ |
paul@552 | 183 | visitBitor = \ |
paul@259 | 184 | visitBitxor = Helper._visitBinaryBit |
paul@552 | 185 | |
paul@552 | 186 | visitAdd = \ |
paul@552 | 187 | visitDiv = \ |
paul@552 | 188 | visitFloorDiv = \ |
paul@552 | 189 | visitLeftShift = \ |
paul@552 | 190 | visitMod = \ |
paul@552 | 191 | visitMul = \ |
paul@552 | 192 | visitPower = \ |
paul@552 | 193 | visitRightShift = \ |
paul@196 | 194 | visitSub = Helper._visitBinary |
paul@160 | 195 | |
paul@160 | 196 | # Unary operators. |
paul@160 | 197 | |
paul@552 | 198 | visitInvert = \ |
paul@552 | 199 | visitUnaryAdd = \ |
paul@196 | 200 | visitUnarySub = Helper._visitUnary |
paul@160 | 201 | |
paul@160 | 202 | # Logical operators. |
paul@160 | 203 | |
paul@101 | 204 | def visitAnd(self, node): |
paul@163 | 205 | end_block = self.new_block() |
paul@130 | 206 | temp_pos = self.reserve_temp() |
paul@101 | 207 | |
paul@101 | 208 | for n in node.nodes[:-1]: |
paul@101 | 209 | self.dispatch(n) |
paul@130 | 210 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 211 | |
paul@450 | 212 | self._generateTestBoolean(n, LoadTemp(temp_pos)) |
paul@450 | 213 | self.new_op(JumpIfFalse(end_block, working="status")) |
paul@101 | 214 | |
paul@101 | 215 | self.dispatch(node.nodes[-1]) |
paul@130 | 216 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 217 | |
paul@163 | 218 | self.set_block(end_block) |
paul@130 | 219 | |
paul@450 | 220 | temp = LoadTemp(temp_pos) |
paul@130 | 221 | self.new_op(temp) |
paul@130 | 222 | self.discard_temp(temp) |
paul@56 | 223 | |
paul@160 | 224 | def visitNot(self, node): |
paul@160 | 225 | self.dispatch(node.expr) |
paul@160 | 226 | |
paul@160 | 227 | temp = self.optimiser.optimise_temp_storage() |
paul@455 | 228 | self.new_op(temp) |
paul@160 | 229 | self._generateTestBoolean(node.expr, temp) |
paul@160 | 230 | self.discard_temp(temp) |
paul@160 | 231 | |
paul@450 | 232 | self.new_op(InvertBoolean(source="status", target="status")) |
paul@160 | 233 | self._generateLoadBoolean(node) |
paul@160 | 234 | |
paul@160 | 235 | def visitOr(self, node): |
paul@163 | 236 | end_block = self.new_block() |
paul@160 | 237 | temp_pos = self.reserve_temp() |
paul@160 | 238 | |
paul@160 | 239 | for n in node.nodes[:-1]: |
paul@160 | 240 | self.dispatch(n) |
paul@160 | 241 | self.new_op(StoreTemp(temp_pos)) |
paul@160 | 242 | |
paul@450 | 243 | self._generateTestBoolean(n, LoadTemp(temp_pos)) |
paul@450 | 244 | self.new_op(JumpIfTrue(end_block, working="status")) |
paul@160 | 245 | |
paul@160 | 246 | self.dispatch(node.nodes[-1]) |
paul@160 | 247 | self.new_op(StoreTemp(temp_pos)) |
paul@160 | 248 | |
paul@163 | 249 | self.set_block(end_block) |
paul@160 | 250 | |
paul@450 | 251 | temp = LoadTemp(temp_pos) |
paul@160 | 252 | self.new_op(temp) |
paul@160 | 253 | self.discard_temp(temp) |
paul@160 | 254 | |
paul@160 | 255 | # Comparisons. |
paul@160 | 256 | |
paul@160 | 257 | def visitCompare(self, node): |
paul@160 | 258 | |
paul@160 | 259 | """ |
paul@160 | 260 | _t1 = node.expr |
paul@160 | 261 | _t1 op1 _t2 and _t2 op2 _t3 and ... |
paul@160 | 262 | """ |
paul@160 | 263 | |
paul@163 | 264 | end_block = self.new_block() |
paul@160 | 265 | |
paul@160 | 266 | self.dispatch(node.expr) |
paul@160 | 267 | temp2 = self.optimiser.optimise_temp_storage() |
paul@160 | 268 | |
paul@172 | 269 | # NOTE: Replicated by some code in micropython.inspect.visitCompare. |
paul@172 | 270 | |
paul@160 | 271 | last_op = node.ops[-1] |
paul@160 | 272 | |
paul@160 | 273 | for op in node.ops: |
paul@160 | 274 | op_name, next_node = op |
paul@354 | 275 | operator_fn = operator_functions.get(op_name) |
paul@160 | 276 | |
paul@161 | 277 | # Propagate the arguments as we traverse the construct. |
paul@161 | 278 | |
paul@160 | 279 | temp1 = temp2 |
paul@160 | 280 | self.dispatch(next_node) |
paul@160 | 281 | temp2 = self.optimiser.optimise_temp_storage() |
paul@160 | 282 | |
paul@160 | 283 | # Use the appropriate mechanism, setting the boolean status for the |
paul@160 | 284 | # comparison. |
paul@160 | 285 | |
paul@354 | 286 | if operator_fn is not None: |
paul@354 | 287 | |
paul@354 | 288 | # Generate function call using evaluated argument and next node. |
paul@160 | 289 | |
paul@354 | 290 | temp_fn = self._generateOperatorFunction(op_name) |
paul@354 | 291 | self._generateInvocation(temp_fn, (temp1, temp2)) |
paul@354 | 292 | self.discard_temp(temp_fn) |
paul@160 | 293 | |
paul@354 | 294 | temp_result = self.optimiser.optimise_temp_storage() |
paul@160 | 295 | self._generateTestBoolean(node, temp_result) |
paul@160 | 296 | self.discard_temp(temp_result) |
paul@160 | 297 | |
paul@160 | 298 | else: |
paul@160 | 299 | # Deal with the special operators. |
paul@160 | 300 | |
paul@160 | 301 | if op_name.startswith("is"): |
paul@160 | 302 | self.new_op(temp1) |
paul@160 | 303 | self.record_value() |
paul@469 | 304 | self.start_target() |
paul@160 | 305 | self.new_op(temp2) |
paul@450 | 306 | self.new_op(TestIdentity(target="status")) |
paul@451 | 307 | self.assign_value() |
paul@160 | 308 | self.discard_value() |
paul@160 | 309 | |
paul@160 | 310 | elif op_name.endswith("in"): |
paul@160 | 311 | self.new_op(temp2) |
paul@160 | 312 | |
paul@160 | 313 | # Get method on temp2. |
paul@160 | 314 | |
paul@160 | 315 | self._generateAttr(node, "__contains__", self.attribute_load_instructions) |
paul@160 | 316 | temp_method = self.optimiser.optimise_temp_storage() |
paul@160 | 317 | |
paul@160 | 318 | # Add arguments. |
paul@160 | 319 | # NOTE: No support for defaults. |
paul@160 | 320 | |
paul@354 | 321 | self._generateInvocation(temp_method, (temp2, temp1)) |
paul@160 | 322 | |
paul@160 | 323 | temp_result = self.get_temp() |
paul@160 | 324 | self._generateTestBoolean(node, temp_result) |
paul@160 | 325 | self.discard_temp(temp_result) |
paul@160 | 326 | |
paul@160 | 327 | if op_name.find("not") != -1: |
paul@450 | 328 | self.new_op(InvertBoolean(source="status", target="status")) |
paul@160 | 329 | |
paul@163 | 330 | # Test the result and jump to the end block if false. |
paul@160 | 331 | |
paul@160 | 332 | if op is not last_op: |
paul@450 | 333 | self.new_op(JumpIfFalse(end_block, working="status")) |
paul@160 | 334 | |
paul@160 | 335 | # Compilation duties... |
paul@160 | 336 | |
paul@160 | 337 | self.discard_temp(temp1) |
paul@160 | 338 | |
paul@160 | 339 | self.discard_temp(temp2) |
paul@161 | 340 | |
paul@161 | 341 | # With the status set above, produce a boolean result. |
paul@161 | 342 | |
paul@163 | 343 | self.set_block(end_block) |
paul@161 | 344 | |
paul@160 | 345 | # Yield the appropriate value. |
paul@160 | 346 | |
paul@160 | 347 | self._generateLoadBoolean(node) |
paul@160 | 348 | |
paul@160 | 349 | # Expressions. |
paul@160 | 350 | |
paul@339 | 351 | def visitBackquote(self, node): raise TranslationNotImplementedError("Backquote") |
paul@160 | 352 | |
paul@160 | 353 | def visitCallFunc(self, node): |
paul@160 | 354 | |
paul@160 | 355 | """ |
paul@160 | 356 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@160 | 357 | the correct location, then invoke the function. |
paul@160 | 358 | """ |
paul@160 | 359 | |
paul@160 | 360 | # Mark the frame, evaluate the target, generate the call. |
paul@160 | 361 | |
paul@160 | 362 | self._startCallFunc() |
paul@160 | 363 | self.dispatch(node.node) |
paul@234 | 364 | temp_target, target, temp_context = self._generateCallFunc(node.args, node) |
paul@234 | 365 | self._doCallFunc(temp_target, target) |
paul@369 | 366 | self._endCallFunc(temp_target, temp_context) |
paul@160 | 367 | |
paul@160 | 368 | def visitConst(self, node): |
paul@168 | 369 | const = self.importer.get_constant(node.value) |
paul@160 | 370 | self.new_op(LoadConst(const)) |
paul@160 | 371 | |
paul@339 | 372 | def visitDict(self, node): raise TranslationNotImplementedError("Dict") |
paul@160 | 373 | |
paul@339 | 374 | def visitEllipsis(self, node): raise TranslationNotImplementedError("Ellipsis") |
paul@160 | 375 | |
paul@339 | 376 | def visitExec(self, node): raise TranslationNotImplementedError("Exec") |
paul@160 | 377 | |
paul@339 | 378 | def visitExpression(self, node): raise TranslationNotImplementedError("Expression") |
paul@160 | 379 | |
paul@339 | 380 | def visitGenExpr(self, node): raise TranslationNotImplementedError("GenExpr") |
paul@160 | 381 | |
paul@339 | 382 | def visitGenExprFor(self, node): raise TranslationNotImplementedError("GenExprFor") |
paul@160 | 383 | |
paul@339 | 384 | def visitGenExprIf(self, node): raise TranslationNotImplementedError("GenExprIf") |
paul@160 | 385 | |
paul@339 | 386 | def visitGenExprInner(self, node): raise TranslationNotImplementedError("GenExprInner") |
paul@160 | 387 | |
paul@160 | 388 | def visitGetattr(self, node): |
paul@160 | 389 | self._visitAttr(node, self.attribute_load_instructions) |
paul@160 | 390 | |
paul@160 | 391 | def visitList(self, node): |
paul@506 | 392 | self._generateList(node, node.nodes) |
paul@506 | 393 | |
paul@506 | 394 | def visitListComp(self, node): |
paul@246 | 395 | self._generateList(node) |
paul@506 | 396 | temp_list = self.optimiser.optimise_temp_storage() |
paul@506 | 397 | |
paul@506 | 398 | # Get the list's append method. |
paul@506 | 399 | |
paul@506 | 400 | self._generateAttr(node, "append", self.attribute_load_instructions) |
paul@506 | 401 | temp_method = self.optimiser.optimise_temp_storage() |
paul@506 | 402 | |
paul@506 | 403 | self.visitListCompFor(node.quals[0], node.quals[1:], node.expr, temp_list, temp_method) |
paul@506 | 404 | |
paul@506 | 405 | # Generate a reference to the list. |
paul@506 | 406 | |
paul@506 | 407 | self.new_op(temp_list) |
paul@160 | 408 | |
paul@506 | 409 | self.discard_temp(temp_method) |
paul@506 | 410 | self.discard_temp(temp_list) |
paul@506 | 411 | |
paul@506 | 412 | def visitListCompFor(self, node, following_quals, expr, temp_list, temp_method): |
paul@506 | 413 | temp_iterator, next_block, exit_block, else_block = self._startFor(node) |
paul@506 | 414 | |
paul@506 | 415 | # Explicit dispatch to tests, following loops, expression. |
paul@506 | 416 | |
paul@506 | 417 | if node.ifs: |
paul@506 | 418 | self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr, temp_list, temp_method) |
paul@506 | 419 | |
paul@506 | 420 | # Explicit dispatch to following loops, expression. |
paul@506 | 421 | |
paul@506 | 422 | elif following_quals: |
paul@506 | 423 | self.visitListCompFor(following_quals[0], following_quals[1:], expr, temp_list, temp_method) |
paul@506 | 424 | |
paul@506 | 425 | # Explicit dispatch to the expression. |
paul@506 | 426 | |
paul@506 | 427 | else: |
paul@506 | 428 | self.dispatch(expr) |
paul@160 | 429 | |
paul@506 | 430 | # Append the value to the list. |
paul@506 | 431 | |
paul@506 | 432 | temp_value = self.optimiser.optimise_temp_storage() |
paul@506 | 433 | self._generateInvocation(temp_method, (temp_list, temp_value,)) |
paul@506 | 434 | |
paul@506 | 435 | self._endFor(node, temp_iterator, next_block, exit_block, else_block) |
paul@506 | 436 | |
paul@506 | 437 | def visitListCompIf(self, node, following_tests, following_quals, expr, temp_list, temp_method): |
paul@506 | 438 | exit_block = self.new_block() |
paul@506 | 439 | |
paul@506 | 440 | # Evaluate the test and jump beyond the body if it is not satisfied. |
paul@506 | 441 | |
paul@506 | 442 | self.dispatch(node.test) |
paul@506 | 443 | |
paul@506 | 444 | temp = self.optimiser.optimise_temp_storage() |
paul@506 | 445 | self.new_op(temp) |
paul@506 | 446 | self._generateTestBoolean(node, temp) |
paul@506 | 447 | self.new_op(JumpIfFalse(exit_block, working="status")) |
paul@160 | 448 | |
paul@506 | 449 | # Explicit dispatch to tests, following loops, expression. |
paul@506 | 450 | |
paul@506 | 451 | if following_tests: |
paul@506 | 452 | self.visitListCompIf(following_tests[0], following_tests[1:], following_quals, expr, temp_list, temp_method) |
paul@506 | 453 | |
paul@506 | 454 | # Explicit dispatch to following loops, expression. |
paul@506 | 455 | |
paul@506 | 456 | elif following_quals: |
paul@506 | 457 | self.visitListCompFor(following_quals[0], following_quals[1:], expr, temp_list, temp_method) |
paul@506 | 458 | |
paul@506 | 459 | # Explicit dispatch to the expression. |
paul@506 | 460 | |
paul@506 | 461 | else: |
paul@506 | 462 | self.dispatch(expr) |
paul@506 | 463 | |
paul@506 | 464 | # Append the value to the list. |
paul@506 | 465 | |
paul@506 | 466 | temp_value = self.optimiser.optimise_temp_storage() |
paul@506 | 467 | self._generateInvocation(temp_method, (temp_list, temp_value,)) |
paul@506 | 468 | |
paul@506 | 469 | self.set_block(exit_block) |
paul@160 | 470 | |
paul@160 | 471 | def visitName(self, node): |
paul@343 | 472 | self._visitName(node, self.name_load_instructions) |
paul@160 | 473 | |
paul@342 | 474 | def visitSlice(self, node): |
paul@374 | 475 | args = [node.expr] |
paul@374 | 476 | |
paul@342 | 477 | if node.lower is None: |
paul@374 | 478 | args.append(compiler.ast.Name("None")) |
paul@342 | 479 | if node.upper is None: |
paul@374 | 480 | args.append(compiler.ast.Name("None")) |
paul@342 | 481 | else: |
paul@374 | 482 | args.append(node.upper) |
paul@342 | 483 | else: |
paul@374 | 484 | args.append(node.lower) |
paul@373 | 485 | if node.upper is None: |
paul@373 | 486 | args.append(compiler.ast.Name("None")) |
paul@373 | 487 | else: |
paul@373 | 488 | args.append(node.upper) |
paul@342 | 489 | |
paul@374 | 490 | temp_fn = self._getOperatorFunction(node, self.in_assignment and "AssSlice" or "Slice") |
paul@374 | 491 | self._visitCall(node, temp_fn, args) |
paul@374 | 492 | self.discard_temp(temp_fn) |
paul@160 | 493 | |
paul@216 | 494 | def visitSubscript(self, node): |
paul@374 | 495 | temp_fn = self._getOperatorFunction(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@374 | 496 | self._visitCall(node, temp_fn, [node.expr] + node.subs) |
paul@374 | 497 | self.discard_temp(temp_fn) |
paul@160 | 498 | |
paul@160 | 499 | def visitTuple(self, node): |
paul@246 | 500 | self._generateTuple(node) |
paul@160 | 501 | |
paul@160 | 502 | # Definitions. |
paul@56 | 503 | |
paul@56 | 504 | def visitAssign(self, node): |
paul@159 | 505 | |
paul@159 | 506 | """ |
paul@159 | 507 | Evaluate the expression from the given 'node' and assign it to the |
paul@159 | 508 | associated recipients. |
paul@159 | 509 | """ |
paul@159 | 510 | |
paul@56 | 511 | self.dispatch(node.expr) |
paul@146 | 512 | |
paul@146 | 513 | # Record the value and then dispatch to the assignment targets. |
paul@146 | 514 | |
paul@159 | 515 | self.record_value(self.has_immediate_usage(node.nodes)) |
paul@101 | 516 | |
paul@374 | 517 | self.in_assignment = 1 |
paul@374 | 518 | |
paul@56 | 519 | for n in node.nodes: |
paul@143 | 520 | self.dispatch(n) |
paul@56 | 521 | |
paul@374 | 522 | self.in_assignment = 0 |
paul@104 | 523 | self.discard_value() |
paul@101 | 524 | |
paul@143 | 525 | def visitAssAttr(self, node): |
paul@159 | 526 | |
paul@159 | 527 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 528 | |
paul@469 | 529 | self.start_target() |
paul@405 | 530 | self._visitAttr(node, self.optimiser.get_attribute_store_instructions()) |
paul@451 | 531 | self.assign_value() |
paul@56 | 532 | |
paul@143 | 533 | def visitAssList(self, node): |
paul@159 | 534 | |
paul@159 | 535 | """ |
paul@159 | 536 | Assign items from the assignment expression to each of the recipients |
paul@159 | 537 | found within the given 'node'. |
paul@159 | 538 | """ |
paul@159 | 539 | |
paul@368 | 540 | self.new_op(self.expr_temp[-1]) |
paul@368 | 541 | self._generateAttr(node, "__getitem__", self.attribute_load_instructions) |
paul@368 | 542 | temp_getitem = self.optimiser.optimise_temp_storage() |
paul@368 | 543 | |
paul@139 | 544 | for i, n in enumerate(node.nodes): |
paul@139 | 545 | self._startCallFunc() |
paul@368 | 546 | self.new_op(temp_getitem) |
paul@234 | 547 | temp_target, target, temp_context = self._generateCallFunc([compiler.ast.Const(i)], node) |
paul@234 | 548 | self._doCallFunc(temp_target, target) |
paul@464 | 549 | self._endCallFunc(temp_context=temp_context) |
paul@139 | 550 | |
paul@139 | 551 | # Provide a different source value. |
paul@159 | 552 | # NOTE: Permitting immediate usage given that neither name nor |
paul@159 | 553 | # NOTE: attribute accesses should involve a function call |
paul@159 | 554 | # NOTE: overwriting the above result. |
paul@159 | 555 | |
paul@159 | 556 | self.record_value(self.is_immediate_user(n)) |
paul@143 | 557 | self.dispatch(n) |
paul@139 | 558 | self.discard_value() |
paul@139 | 559 | |
paul@368 | 560 | self.discard_temp(temp_getitem) |
paul@368 | 561 | |
paul@143 | 562 | def visitAssName(self, node): |
paul@159 | 563 | |
paul@159 | 564 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 565 | |
paul@289 | 566 | if hasattr(node, "flags") and node.flags == "OP_DELETE": |
paul@339 | 567 | raise TranslationNotImplementedError("AssName(OP_DELETE)") |
paul@271 | 568 | |
paul@469 | 569 | self.start_target() |
paul@106 | 570 | self._visitName(node, self.name_store_instructions) |
paul@451 | 571 | self.assign_value() |
paul@56 | 572 | |
paul@279 | 573 | # Add any attribute usage guards. |
paul@279 | 574 | |
paul@381 | 575 | self._generateGuards(node) |
paul@279 | 576 | |
paul@56 | 577 | visitAssTuple = visitAssList |
paul@56 | 578 | |
paul@151 | 579 | def visitAugAssign(self, node): |
paul@151 | 580 | |
paul@354 | 581 | # Find the augmented assignment function and attempt to use it. |
paul@151 | 582 | |
paul@354 | 583 | temp_fn = self._getOperatorAugAssignFunction(node) |
paul@374 | 584 | self._visitCall(node, temp_fn, (node.node, node.expr)) |
paul@354 | 585 | self.discard_temp(temp_fn) |
paul@151 | 586 | |
paul@151 | 587 | # Assign the result to the name. |
paul@151 | 588 | |
paul@151 | 589 | self.record_value(1) |
paul@151 | 590 | |
paul@151 | 591 | if isinstance(node.node, compiler.ast.Name): |
paul@151 | 592 | self.visitAssName(node.node) |
paul@151 | 593 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@151 | 594 | self.visitAssAttr(node.node) |
paul@151 | 595 | else: |
paul@339 | 596 | raise TranslationNotImplementedError("AugAssign(Slice or Subscript)") |
paul@151 | 597 | |
paul@151 | 598 | self.discard_value() |
paul@151 | 599 | |
paul@38 | 600 | def visitClass(self, node): |
paul@353 | 601 | if not used_by_unit(node): |
paul@167 | 602 | return |
paul@90 | 603 | |
paul@90 | 604 | # Store the name. |
paul@90 | 605 | |
paul@237 | 606 | self.new_op(LoadClass(node.unit)) |
paul@104 | 607 | self.record_value() |
paul@469 | 608 | self.start_target() |
paul@106 | 609 | self._visitName(node, self.name_store_instructions) |
paul@451 | 610 | self.assign_value() |
paul@104 | 611 | self.discard_value() |
paul@90 | 612 | |
paul@90 | 613 | # Visit the code. |
paul@90 | 614 | |
paul@38 | 615 | unit = self.unit |
paul@39 | 616 | self.unit = node.unit |
paul@38 | 617 | self.dispatch(node.code) |
paul@38 | 618 | self.unit = unit |
paul@17 | 619 | |
paul@339 | 620 | def visitDecorators(self, node): raise TranslationNotImplementedError("Decorators") |
paul@160 | 621 | |
paul@562 | 622 | def visitFrom(self, node): |
paul@562 | 623 | |
paul@562 | 624 | """ |
paul@562 | 625 | Although imported code already resides in any generated image, the |
paul@562 | 626 | code must be invoked. |
paul@562 | 627 | """ |
paul@562 | 628 | |
paul@562 | 629 | modules = self.importer.importers.get(node) |
paul@562 | 630 | if modules: |
paul@562 | 631 | for module in modules: |
paul@562 | 632 | self.new_op(MakeFrame(0)) |
paul@562 | 633 | self.new_op(LoadConst(module)) |
paul@562 | 634 | self.new_op(LoadCallable()) |
paul@562 | 635 | self.new_op(JumpWithFrame()) |
paul@160 | 636 | |
paul@160 | 637 | def visitFunction(self, node): |
paul@353 | 638 | if not used_by_unit(node): |
paul@167 | 639 | return |
paul@160 | 640 | |
paul@160 | 641 | # Only store the name when visiting this node from outside. |
paul@160 | 642 | |
paul@160 | 643 | if self.unit is not node.unit: |
paul@330 | 644 | self._visitFunctionDeclaration(node) |
paul@330 | 645 | |
paul@330 | 646 | # Record the declared function. |
paul@330 | 647 | |
paul@222 | 648 | self.record_value() |
paul@160 | 649 | |
paul@469 | 650 | self.start_target() |
paul@160 | 651 | self._visitName(node, self.name_store_instructions) # AssName equivalent |
paul@451 | 652 | self.assign_value() |
paul@160 | 653 | self.discard_value() |
paul@160 | 654 | |
paul@165 | 655 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 656 | |
paul@160 | 657 | else: |
paul@330 | 658 | self._visitFunctionDefinition(node) |
paul@160 | 659 | |
paul@160 | 660 | def visitGlobal(self, node): pass |
paul@160 | 661 | |
paul@562 | 662 | visitImport = visitFrom |
paul@160 | 663 | |
paul@160 | 664 | def visitKeyword(self, node): pass |
paul@160 | 665 | |
paul@160 | 666 | def visitLambda(self, node): |
paul@21 | 667 | |
paul@21 | 668 | """ |
paul@160 | 669 | Lambda functions can be represented as globally defined functions |
paul@160 | 670 | provided they do not define any default parameter values, since these |
paul@160 | 671 | may defined in a non-global scope. |
paul@160 | 672 | |
paul@160 | 673 | Where defaults are defined, an object must be created and its content |
paul@160 | 674 | defined: the callable member of the object's structure must be set to |
paul@160 | 675 | the lambda function definition; each default must be attached to the |
paul@160 | 676 | object as an attribute, as is the case with normal functions and |
paul@160 | 677 | methods. |
paul@112 | 678 | """ |
paul@112 | 679 | |
paul@160 | 680 | # Produce the reference to this function when visiting this node from |
paul@160 | 681 | # outside. |
paul@160 | 682 | |
paul@160 | 683 | if self.unit is not node.unit: |
paul@160 | 684 | |
paul@330 | 685 | # Provide the declared function. |
paul@160 | 686 | |
paul@330 | 687 | self._visitFunctionDeclaration(node) |
paul@160 | 688 | |
paul@165 | 689 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 690 | |
paul@160 | 691 | else: |
paul@330 | 692 | self._visitFunctionDefinition(node) |
paul@215 | 693 | |
paul@160 | 694 | def visitModule(self, node): |
paul@160 | 695 | extend = ExtendFrame() |
paul@160 | 696 | self.new_op(extend) |
paul@160 | 697 | self.dispatch(node.node) |
paul@160 | 698 | self.set_frame_usage(node, extend) |
paul@160 | 699 | |
paul@160 | 700 | # Statements. |
paul@160 | 701 | |
paul@160 | 702 | def visitStmt(self, node): |
paul@160 | 703 | |
paul@160 | 704 | "Process the collection of statements provided by 'node'." |
paul@160 | 705 | |
paul@160 | 706 | for n in node.nodes: |
paul@160 | 707 | |
paul@160 | 708 | # Process the statement. |
paul@160 | 709 | |
paul@160 | 710 | self.dispatch(n) |
paul@160 | 711 | |
paul@160 | 712 | # Prevent incorrect optimisation by resetting the optimiser after |
paul@160 | 713 | # each statement. |
paul@160 | 714 | |
paul@160 | 715 | self.optimiser.reset() |
paul@160 | 716 | |
paul@339 | 717 | def visitAssert(self, node): raise TranslationNotImplementedError("Assert") |
paul@160 | 718 | |
paul@160 | 719 | def visitBreak(self, node): |
paul@163 | 720 | next_block, exit_block = self.get_loop_blocks() |
paul@163 | 721 | self.new_op(Jump(exit_block)) |
paul@17 | 722 | |
paul@41 | 723 | def visitContinue(self, node): |
paul@163 | 724 | next_block, exit_block = self.get_loop_blocks() |
paul@163 | 725 | self.new_op(Jump(next_block)) |
paul@17 | 726 | |
paul@18 | 727 | def visitDiscard(self, node): |
paul@19 | 728 | self.dispatch(node.expr) |
paul@140 | 729 | self.optimiser.optimise_unused_results() |
paul@17 | 730 | |
paul@44 | 731 | def visitFor(self, node): |
paul@506 | 732 | temp_iterator, next_block, exit_block, else_block = self._startFor(node, node.else_) |
paul@44 | 733 | self.dispatch(node.body) |
paul@506 | 734 | self._endFor(node, temp_iterator, next_block, exit_block, else_block, node.else_) |
paul@98 | 735 | |
paul@17 | 736 | def visitIf(self, node): |
paul@19 | 737 | first = 1 |
paul@232 | 738 | next_block = None |
paul@163 | 739 | exit_block = self.new_block() |
paul@17 | 740 | |
paul@108 | 741 | clauses = node.tests + [(None, node.else_)] |
paul@108 | 742 | |
paul@108 | 743 | for clause in clauses: |
paul@108 | 744 | test, body = clause |
paul@19 | 745 | if body is None: |
paul@19 | 746 | break |
paul@232 | 747 | |
paul@19 | 748 | if not first: |
paul@232 | 749 | self.new_op(Jump(exit_block)) # finish last body |
paul@232 | 750 | self.set_block(next_block) # start next test |
paul@232 | 751 | next_block = None |
paul@232 | 752 | |
paul@19 | 753 | if test is not None: |
paul@19 | 754 | self.dispatch(test) |
paul@234 | 755 | |
paul@234 | 756 | temp = self.optimiser.optimise_temp_storage() |
paul@455 | 757 | self.new_op(temp) |
paul@234 | 758 | self._generateTestBoolean(node, temp) |
paul@234 | 759 | |
paul@163 | 760 | next_block = self.new_block() |
paul@450 | 761 | self.new_op(JumpIfFalse(next_block, working="status")) |
paul@232 | 762 | |
paul@19 | 763 | self.dispatch(body) |
paul@232 | 764 | first = 0 |
paul@160 | 765 | |
paul@232 | 766 | if next_block is not None: |
paul@232 | 767 | self.set_block(next_block) |
paul@17 | 768 | |
paul@163 | 769 | self.set_block(exit_block) |
paul@17 | 770 | |
paul@462 | 771 | def visitIfExp(self, node): raise TranslationNotImplementedError("IfExp") |
paul@462 | 772 | |
paul@19 | 773 | def visitPass(self, node): pass |
paul@17 | 774 | |
paul@393 | 775 | def visitPrint(self, node): |
paul@393 | 776 | self._visitPrint(node, "_print") |
paul@17 | 777 | |
paul@393 | 778 | def visitPrintnl(self, node): |
paul@393 | 779 | self._visitPrint(node, "_printnl") |
paul@17 | 780 | |
paul@97 | 781 | def visitRaise(self, node): |
paul@411 | 782 | |
paul@411 | 783 | if node.expr1 is not None: |
paul@411 | 784 | |
paul@411 | 785 | # NOTE: expr1 only => instance provided |
paul@97 | 786 | |
paul@411 | 787 | self.dispatch(node.expr1) |
paul@411 | 788 | |
paul@411 | 789 | if node.expr2 is not None: |
paul@411 | 790 | temp = self.optimiser.optimise_temp_storage() |
paul@102 | 791 | |
paul@411 | 792 | self.dispatch(node.expr2) |
paul@411 | 793 | temp_arg = self.optimiser.optimise_temp_storage() |
paul@411 | 794 | |
paul@411 | 795 | self._generateInvocation(temp, (temp_arg,)) |
paul@102 | 796 | |
paul@411 | 797 | self.discard_temp(temp_arg) |
paul@102 | 798 | |
paul@455 | 799 | self.new_op(Transfer(source="working", target="exception")) |
paul@97 | 800 | |
paul@97 | 801 | self.new_op(RaiseException()) |
paul@17 | 802 | |
paul@21 | 803 | def visitReturn(self, node): |
paul@21 | 804 | if node.value is not None: |
paul@21 | 805 | self.dispatch(node.value) |
paul@68 | 806 | else: |
paul@68 | 807 | self.dispatch(compiler.ast.Name("None")) |
paul@104 | 808 | |
paul@235 | 809 | if self.in_exception_handler: |
paul@450 | 810 | self.new_op(ClearException(target="exception")) |
paul@235 | 811 | |
paul@398 | 812 | # NOTE: Support finally blocks here. |
paul@398 | 813 | |
paul@398 | 814 | if self.exception_blocks[-1]: |
paul@398 | 815 | self.new_op(PopHandler(len(self.exception_blocks[-1]))) |
paul@398 | 816 | |
paul@21 | 817 | self.new_op(Return()) |
paul@17 | 818 | |
paul@57 | 819 | def visitTryExcept(self, node): |
paul@163 | 820 | exit_block = self.new_block() |
paul@163 | 821 | else_block = self.new_block() |
paul@163 | 822 | handler_block = self.new_block() |
paul@163 | 823 | |
paul@163 | 824 | self.add_exception_blocks(handler_block, exit_block) |
paul@57 | 825 | |
paul@68 | 826 | # Try... |
paul@68 | 827 | # Produce the code, then jump to the exit. |
paul@68 | 828 | |
paul@163 | 829 | self.new_op(PushHandler(handler_block)) |
paul@57 | 830 | self.dispatch(node.body) |
paul@398 | 831 | self.new_op(PopHandler(1)) |
paul@160 | 832 | |
paul@160 | 833 | if node.else_ is not None: |
paul@163 | 834 | self.new_op(Jump(else_block)) |
paul@160 | 835 | else: |
paul@163 | 836 | self.new_op(Jump(exit_block)) |
paul@57 | 837 | |
paul@68 | 838 | # Start of handlers. |
paul@68 | 839 | |
paul@163 | 840 | self.set_block(handler_block) |
paul@398 | 841 | self.new_op(PopHandler(1)) |
paul@398 | 842 | |
paul@398 | 843 | # Disable the handlers. |
paul@398 | 844 | |
paul@398 | 845 | self.drop_exception_blocks() |
paul@102 | 846 | |
paul@57 | 847 | for name, assignment, handler in node.handlers: |
paul@163 | 848 | next_block = self.new_block() |
paul@57 | 849 | |
paul@68 | 850 | # Test the given exception against the current exception. |
paul@68 | 851 | |
paul@57 | 852 | if name is not None: |
paul@57 | 853 | self.dispatch(name) |
paul@145 | 854 | |
paul@450 | 855 | self.new_op(CheckException(target="status")) |
paul@450 | 856 | self.new_op(JumpIfFalse(next_block, working="status")) |
paul@57 | 857 | |
paul@145 | 858 | # Handle assignment to exception variable. |
paul@145 | 859 | |
paul@145 | 860 | if assignment is not None: |
paul@450 | 861 | self.new_op(Transfer(source="working", target="exception")) |
paul@145 | 862 | |
paul@145 | 863 | # Record the value to be assigned. |
paul@145 | 864 | |
paul@145 | 865 | self.record_value() |
paul@145 | 866 | self.dispatch(assignment) |
paul@145 | 867 | self.discard_value() |
paul@57 | 868 | |
paul@68 | 869 | # Produce the handler code, then jump to the exit. |
paul@68 | 870 | |
paul@235 | 871 | self.in_exception_handler = 1 |
paul@57 | 872 | self.dispatch(handler) |
paul@235 | 873 | self.in_exception_handler = 0 |
paul@235 | 874 | |
paul@163 | 875 | self.new_op(Jump(exit_block)) |
paul@163 | 876 | |
paul@163 | 877 | self.set_block(next_block) |
paul@160 | 878 | |
paul@57 | 879 | # Unhandled exceptions. |
paul@57 | 880 | |
paul@57 | 881 | self.new_op(RaiseException()) |
paul@57 | 882 | |
paul@57 | 883 | # Optional else clause. |
paul@57 | 884 | |
paul@57 | 885 | if node.else_ is not None: |
paul@163 | 886 | self.set_block(else_block) |
paul@57 | 887 | self.dispatch(node.else_) |
paul@57 | 888 | |
paul@232 | 889 | # Clear the exception. |
paul@232 | 890 | |
paul@163 | 891 | self.set_block(exit_block) |
paul@450 | 892 | self.new_op(ClearException(target="exception")) |
paul@17 | 893 | |
paul@262 | 894 | def visitTryFinally(self, node): |
paul@262 | 895 | |
paul@262 | 896 | """ |
paul@262 | 897 | Add finally handler, potentially as an exception handler. |
paul@262 | 898 | Generate body, potentially changing return statements so that they do |
paul@262 | 899 | not return immediately. |
paul@262 | 900 | Generate handler, removing the handler from the active handler list, |
paul@262 | 901 | adding instructions which raise active exceptions. |
paul@262 | 902 | """ |
paul@262 | 903 | |
paul@339 | 904 | raise TranslationNotImplementedError("TryFinally") |
paul@17 | 905 | |
paul@41 | 906 | def visitWhile(self, node): |
paul@163 | 907 | exit_block = self.new_block() |
paul@163 | 908 | next_block = self.new_block() |
paul@163 | 909 | else_block = self.new_block() |
paul@163 | 910 | |
paul@163 | 911 | self.set_block(next_block) |
paul@44 | 912 | self.dispatch(node.test) |
paul@310 | 913 | |
paul@310 | 914 | temp = self.optimiser.optimise_temp_storage() |
paul@455 | 915 | self.new_op(temp) |
paul@310 | 916 | self._generateTestBoolean(node, temp) |
paul@310 | 917 | |
paul@44 | 918 | if node.else_ is not None: |
paul@450 | 919 | self.new_op(JumpIfFalse(else_block, working="status")) |
paul@44 | 920 | else: |
paul@450 | 921 | self.new_op(JumpIfFalse(exit_block, working="status")) |
paul@163 | 922 | |
paul@163 | 923 | self.add_loop_blocks(next_block, exit_block) |
paul@41 | 924 | |
paul@41 | 925 | self.dispatch(node.body) |
paul@163 | 926 | self.new_op(Jump(next_block)) |
paul@41 | 927 | |
paul@41 | 928 | if node.else_ is not None: |
paul@163 | 929 | self.set_block(else_block) |
paul@160 | 930 | |
paul@41 | 931 | self.dispatch(node.else_) |
paul@41 | 932 | |
paul@163 | 933 | self.set_block(exit_block) |
paul@163 | 934 | |
paul@163 | 935 | self.drop_loop_blocks() |
paul@17 | 936 | |
paul@339 | 937 | def visitWith(self, node): raise TranslationNotImplementedError("With") |
paul@17 | 938 | |
paul@339 | 939 | def visitYield(self, node): raise TranslationNotImplementedError("Yield") |
paul@17 | 940 | |
paul@17 | 941 | # vim: tabstop=4 expandtab shiftwidth=4 |