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@591 | 70 | self.in_exception_handler = False |
paul@591 | 71 | self.in_assignment = False # 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@623 | 80 | def get_unit(self): |
paul@623 | 81 | return self.unit |
paul@623 | 82 | |
paul@242 | 83 | def get_module_code(self): |
paul@139 | 84 | |
paul@139 | 85 | """ |
paul@242 | 86 | Return the top-level module code. |
paul@139 | 87 | """ |
paul@17 | 88 | |
paul@17 | 89 | self.unit = self.module |
paul@156 | 90 | self.reset() |
paul@398 | 91 | self.add_exception_unit() |
paul@73 | 92 | |
paul@163 | 93 | block = self.new_block() |
paul@163 | 94 | self.set_block(block) |
paul@163 | 95 | |
paul@375 | 96 | # Handle exceptions for the program. |
paul@375 | 97 | |
paul@375 | 98 | if self.module.name == "__main__": |
paul@375 | 99 | handler_block = self.new_block() |
paul@375 | 100 | self.new_op(PushHandler(handler_block)) |
paul@375 | 101 | |
paul@375 | 102 | # Generate code for the module. |
paul@375 | 103 | |
paul@343 | 104 | if self.module.astnode is not None: |
paul@343 | 105 | self.dispatch(self.module.astnode) |
paul@73 | 106 | |
paul@139 | 107 | # Finish off the translated program if appropriate. |
paul@139 | 108 | |
paul@242 | 109 | if self.module.name == "__main__": |
paul@375 | 110 | self.set_block(handler_block) |
paul@398 | 111 | self.new_op(PopHandler(1)) |
paul@562 | 112 | |
paul@562 | 113 | # All modules return if invoked. |
paul@562 | 114 | |
paul@562 | 115 | self.new_op(Return()) |
paul@139 | 116 | |
paul@398 | 117 | self.drop_exception_unit() |
paul@116 | 118 | self.unit.temp_usage = self.max_temp_position + 1 |
paul@173 | 119 | self.unit.blocks = self.blocks |
paul@163 | 120 | return self.blocks |
paul@17 | 121 | |
paul@17 | 122 | def get_code(self, unit): |
paul@17 | 123 | |
paul@17 | 124 | "Return the code for the given 'unit'." |
paul@17 | 125 | |
paul@17 | 126 | self.unit = unit |
paul@156 | 127 | self.reset() |
paul@398 | 128 | self.add_exception_unit() |
paul@73 | 129 | |
paul@163 | 130 | block = self.new_block() |
paul@163 | 131 | self.set_block(block) |
paul@163 | 132 | |
paul@91 | 133 | if unit.astnode is not None: |
paul@91 | 134 | self.dispatch(unit.astnode) |
paul@73 | 135 | |
paul@398 | 136 | self.drop_exception_unit() |
paul@208 | 137 | self.unit.temp_usage = self.max_temp_position + 2 # include space for instantiators to expand backwards |
paul@173 | 138 | self.unit.blocks = self.blocks |
paul@163 | 139 | return self.blocks |
paul@17 | 140 | |
paul@137 | 141 | def get_instantiator_code(self, cls): |
paul@137 | 142 | |
paul@137 | 143 | "Return the code for the given class 'cls'." |
paul@137 | 144 | |
paul@408 | 145 | # Obtain the function object to be populated. |
paul@408 | 146 | |
paul@137 | 147 | self.unit = cls.get_instantiator() |
paul@156 | 148 | self.reset() |
paul@137 | 149 | |
paul@163 | 150 | block = self.new_block() |
paul@163 | 151 | self.set_block(block) |
paul@163 | 152 | |
paul@137 | 153 | init_method = cls.get_init_method() |
paul@137 | 154 | |
paul@230 | 155 | # Make an object and store it in the unused first slot. |
paul@137 | 156 | |
paul@233 | 157 | self.make_instance(cls, len(cls.instance_attributes())) |
paul@230 | 158 | self.new_op(StoreTemp(0)) |
paul@137 | 159 | |
paul@137 | 160 | # Invoke the appropriate initialiser. |
paul@137 | 161 | |
paul@223 | 162 | self.new_op(LoadFunction(init_method)) |
paul@137 | 163 | self.new_op(LoadCallable()) |
paul@564 | 164 | self.new_op(JumpInFrame()) |
paul@137 | 165 | |
paul@137 | 166 | # Store the object as the result. |
paul@137 | 167 | |
paul@455 | 168 | self.new_op(LoadTemp(0)) # load the context from the locals |
paul@137 | 169 | self.new_op(Return()) |
paul@137 | 170 | |
paul@173 | 171 | self.unit.blocks = self.blocks |
paul@163 | 172 | return self.blocks |
paul@137 | 173 | |
paul@103 | 174 | # Visitor methods. |
paul@103 | 175 | |
paul@103 | 176 | def default(self, node, *args): |
paul@339 | 177 | raise TranslateError("Node class %r is not supported." % node.__class__) |
paul@103 | 178 | |
paul@103 | 179 | # Concrete visitor methods. |
paul@103 | 180 | |
paul@160 | 181 | # Binary operators. |
paul@160 | 182 | |
paul@552 | 183 | visitBitand = \ |
paul@552 | 184 | visitBitor = \ |
paul@259 | 185 | visitBitxor = Helper._visitBinaryBit |
paul@552 | 186 | |
paul@552 | 187 | visitAdd = \ |
paul@552 | 188 | visitDiv = \ |
paul@552 | 189 | visitFloorDiv = \ |
paul@552 | 190 | visitLeftShift = \ |
paul@552 | 191 | visitMod = \ |
paul@552 | 192 | visitMul = \ |
paul@552 | 193 | visitPower = \ |
paul@552 | 194 | visitRightShift = \ |
paul@196 | 195 | visitSub = Helper._visitBinary |
paul@160 | 196 | |
paul@160 | 197 | # Unary operators. |
paul@160 | 198 | |
paul@552 | 199 | visitInvert = \ |
paul@552 | 200 | visitUnaryAdd = \ |
paul@196 | 201 | visitUnarySub = Helper._visitUnary |
paul@160 | 202 | |
paul@160 | 203 | # Logical operators. |
paul@160 | 204 | |
paul@101 | 205 | def visitAnd(self, node): |
paul@163 | 206 | end_block = self.new_block() |
paul@130 | 207 | temp_pos = self.reserve_temp() |
paul@101 | 208 | |
paul@101 | 209 | for n in node.nodes[:-1]: |
paul@101 | 210 | self.dispatch(n) |
paul@130 | 211 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 212 | |
paul@450 | 213 | self._generateTestBoolean(n, LoadTemp(temp_pos)) |
paul@450 | 214 | self.new_op(JumpIfFalse(end_block, working="status")) |
paul@101 | 215 | |
paul@101 | 216 | self.dispatch(node.nodes[-1]) |
paul@130 | 217 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 218 | |
paul@163 | 219 | self.set_block(end_block) |
paul@130 | 220 | |
paul@450 | 221 | temp = LoadTemp(temp_pos) |
paul@130 | 222 | self.new_op(temp) |
paul@130 | 223 | self.discard_temp(temp) |
paul@56 | 224 | |
paul@160 | 225 | def visitNot(self, node): |
paul@160 | 226 | self.dispatch(node.expr) |
paul@160 | 227 | |
paul@160 | 228 | temp = self.optimiser.optimise_temp_storage() |
paul@455 | 229 | self.new_op(temp) |
paul@160 | 230 | self._generateTestBoolean(node.expr, temp) |
paul@160 | 231 | self.discard_temp(temp) |
paul@160 | 232 | |
paul@450 | 233 | self.new_op(InvertBoolean(source="status", target="status")) |
paul@160 | 234 | self._generateLoadBoolean(node) |
paul@160 | 235 | |
paul@160 | 236 | def visitOr(self, node): |
paul@163 | 237 | end_block = self.new_block() |
paul@160 | 238 | temp_pos = self.reserve_temp() |
paul@160 | 239 | |
paul@160 | 240 | for n in node.nodes[:-1]: |
paul@160 | 241 | self.dispatch(n) |
paul@160 | 242 | self.new_op(StoreTemp(temp_pos)) |
paul@160 | 243 | |
paul@450 | 244 | self._generateTestBoolean(n, LoadTemp(temp_pos)) |
paul@450 | 245 | self.new_op(JumpIfTrue(end_block, working="status")) |
paul@160 | 246 | |
paul@160 | 247 | self.dispatch(node.nodes[-1]) |
paul@160 | 248 | self.new_op(StoreTemp(temp_pos)) |
paul@160 | 249 | |
paul@163 | 250 | self.set_block(end_block) |
paul@160 | 251 | |
paul@450 | 252 | temp = LoadTemp(temp_pos) |
paul@160 | 253 | self.new_op(temp) |
paul@160 | 254 | self.discard_temp(temp) |
paul@160 | 255 | |
paul@160 | 256 | # Comparisons. |
paul@160 | 257 | |
paul@160 | 258 | def visitCompare(self, node): |
paul@160 | 259 | |
paul@160 | 260 | """ |
paul@160 | 261 | _t1 = node.expr |
paul@160 | 262 | _t1 op1 _t2 and _t2 op2 _t3 and ... |
paul@160 | 263 | """ |
paul@160 | 264 | |
paul@163 | 265 | end_block = self.new_block() |
paul@160 | 266 | |
paul@160 | 267 | self.dispatch(node.expr) |
paul@160 | 268 | temp2 = self.optimiser.optimise_temp_storage() |
paul@160 | 269 | |
paul@172 | 270 | # NOTE: Replicated by some code in micropython.inspect.visitCompare. |
paul@172 | 271 | |
paul@160 | 272 | last_op = node.ops[-1] |
paul@160 | 273 | |
paul@160 | 274 | for op in node.ops: |
paul@160 | 275 | op_name, next_node = op |
paul@354 | 276 | operator_fn = operator_functions.get(op_name) |
paul@160 | 277 | |
paul@161 | 278 | # Propagate the arguments as we traverse the construct. |
paul@161 | 279 | |
paul@160 | 280 | temp1 = temp2 |
paul@160 | 281 | self.dispatch(next_node) |
paul@160 | 282 | temp2 = self.optimiser.optimise_temp_storage() |
paul@160 | 283 | |
paul@160 | 284 | # Use the appropriate mechanism, setting the boolean status for the |
paul@160 | 285 | # comparison. |
paul@160 | 286 | |
paul@354 | 287 | if operator_fn is not None: |
paul@354 | 288 | |
paul@354 | 289 | # Generate function call using evaluated argument and next node. |
paul@160 | 290 | |
paul@354 | 291 | temp_fn = self._generateOperatorFunction(op_name) |
paul@354 | 292 | self._generateInvocation(temp_fn, (temp1, temp2)) |
paul@354 | 293 | self.discard_temp(temp_fn) |
paul@160 | 294 | |
paul@354 | 295 | temp_result = self.optimiser.optimise_temp_storage() |
paul@160 | 296 | self._generateTestBoolean(node, temp_result) |
paul@160 | 297 | self.discard_temp(temp_result) |
paul@160 | 298 | |
paul@160 | 299 | else: |
paul@160 | 300 | # Deal with the special operators. |
paul@160 | 301 | |
paul@160 | 302 | if op_name.startswith("is"): |
paul@160 | 303 | self.new_op(temp1) |
paul@160 | 304 | self.record_value() |
paul@469 | 305 | self.start_target() |
paul@160 | 306 | self.new_op(temp2) |
paul@450 | 307 | self.new_op(TestIdentity(target="status")) |
paul@451 | 308 | self.assign_value() |
paul@160 | 309 | self.discard_value() |
paul@160 | 310 | |
paul@160 | 311 | elif op_name.endswith("in"): |
paul@160 | 312 | self.new_op(temp2) |
paul@160 | 313 | |
paul@160 | 314 | # Get method on temp2. |
paul@160 | 315 | |
paul@160 | 316 | self._generateAttr(node, "__contains__", self.attribute_load_instructions) |
paul@160 | 317 | temp_method = self.optimiser.optimise_temp_storage() |
paul@160 | 318 | |
paul@160 | 319 | # Add arguments. |
paul@160 | 320 | # NOTE: No support for defaults. |
paul@160 | 321 | |
paul@354 | 322 | self._generateInvocation(temp_method, (temp2, temp1)) |
paul@160 | 323 | |
paul@160 | 324 | temp_result = self.get_temp() |
paul@160 | 325 | self._generateTestBoolean(node, temp_result) |
paul@160 | 326 | self.discard_temp(temp_result) |
paul@160 | 327 | |
paul@160 | 328 | if op_name.find("not") != -1: |
paul@450 | 329 | self.new_op(InvertBoolean(source="status", target="status")) |
paul@160 | 330 | |
paul@163 | 331 | # Test the result and jump to the end block if false. |
paul@160 | 332 | |
paul@160 | 333 | if op is not last_op: |
paul@450 | 334 | self.new_op(JumpIfFalse(end_block, working="status")) |
paul@160 | 335 | |
paul@160 | 336 | # Compilation duties... |
paul@160 | 337 | |
paul@160 | 338 | self.discard_temp(temp1) |
paul@160 | 339 | |
paul@160 | 340 | self.discard_temp(temp2) |
paul@161 | 341 | |
paul@161 | 342 | # With the status set above, produce a boolean result. |
paul@161 | 343 | |
paul@163 | 344 | self.set_block(end_block) |
paul@161 | 345 | |
paul@160 | 346 | # Yield the appropriate value. |
paul@160 | 347 | |
paul@160 | 348 | self._generateLoadBoolean(node) |
paul@160 | 349 | |
paul@160 | 350 | # Expressions. |
paul@160 | 351 | |
paul@339 | 352 | def visitBackquote(self, node): raise TranslationNotImplementedError("Backquote") |
paul@160 | 353 | |
paul@160 | 354 | def visitCallFunc(self, node): |
paul@160 | 355 | |
paul@160 | 356 | """ |
paul@160 | 357 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@160 | 358 | the correct location, then invoke the function. |
paul@160 | 359 | """ |
paul@160 | 360 | |
paul@160 | 361 | # Mark the frame, evaluate the target, generate the call. |
paul@160 | 362 | |
paul@160 | 363 | self._startCallFunc() |
paul@160 | 364 | self.dispatch(node.node) |
paul@234 | 365 | temp_target, target, temp_context = self._generateCallFunc(node.args, node) |
paul@234 | 366 | self._doCallFunc(temp_target, target) |
paul@369 | 367 | self._endCallFunc(temp_target, temp_context) |
paul@160 | 368 | |
paul@160 | 369 | def visitConst(self, node): |
paul@168 | 370 | const = self.importer.get_constant(node.value) |
paul@160 | 371 | self.new_op(LoadConst(const)) |
paul@160 | 372 | |
paul@339 | 373 | def visitDict(self, node): raise TranslationNotImplementedError("Dict") |
paul@160 | 374 | |
paul@339 | 375 | def visitEllipsis(self, node): raise TranslationNotImplementedError("Ellipsis") |
paul@160 | 376 | |
paul@339 | 377 | def visitExec(self, node): raise TranslationNotImplementedError("Exec") |
paul@160 | 378 | |
paul@339 | 379 | def visitExpression(self, node): raise TranslationNotImplementedError("Expression") |
paul@160 | 380 | |
paul@339 | 381 | def visitGenExpr(self, node): raise TranslationNotImplementedError("GenExpr") |
paul@160 | 382 | |
paul@339 | 383 | def visitGenExprFor(self, node): raise TranslationNotImplementedError("GenExprFor") |
paul@160 | 384 | |
paul@339 | 385 | def visitGenExprIf(self, node): raise TranslationNotImplementedError("GenExprIf") |
paul@160 | 386 | |
paul@339 | 387 | def visitGenExprInner(self, node): raise TranslationNotImplementedError("GenExprInner") |
paul@160 | 388 | |
paul@160 | 389 | def visitGetattr(self, node): |
paul@160 | 390 | self._visitAttr(node, self.attribute_load_instructions) |
paul@160 | 391 | |
paul@160 | 392 | def visitList(self, node): |
paul@506 | 393 | self._generateList(node, node.nodes) |
paul@506 | 394 | |
paul@506 | 395 | def visitListComp(self, node): |
paul@246 | 396 | self._generateList(node) |
paul@506 | 397 | temp_list = self.optimiser.optimise_temp_storage() |
paul@506 | 398 | |
paul@506 | 399 | # Get the list's append method. |
paul@506 | 400 | |
paul@506 | 401 | self._generateAttr(node, "append", self.attribute_load_instructions) |
paul@506 | 402 | temp_method = self.optimiser.optimise_temp_storage() |
paul@506 | 403 | |
paul@506 | 404 | self.visitListCompFor(node.quals[0], node.quals[1:], node.expr, temp_list, temp_method) |
paul@506 | 405 | |
paul@506 | 406 | # Generate a reference to the list. |
paul@506 | 407 | |
paul@506 | 408 | self.new_op(temp_list) |
paul@160 | 409 | |
paul@506 | 410 | self.discard_temp(temp_method) |
paul@506 | 411 | self.discard_temp(temp_list) |
paul@506 | 412 | |
paul@506 | 413 | def visitListCompFor(self, node, following_quals, expr, temp_list, temp_method): |
paul@506 | 414 | temp_iterator, next_block, exit_block, else_block = self._startFor(node) |
paul@506 | 415 | |
paul@506 | 416 | # Explicit dispatch to tests, following loops, expression. |
paul@506 | 417 | |
paul@506 | 418 | if node.ifs: |
paul@506 | 419 | self.visitListCompIf(node.ifs[0], node.ifs[1:], following_quals, expr, temp_list, temp_method) |
paul@506 | 420 | |
paul@506 | 421 | # Explicit dispatch to following loops, expression. |
paul@506 | 422 | |
paul@506 | 423 | elif following_quals: |
paul@506 | 424 | self.visitListCompFor(following_quals[0], following_quals[1:], expr, temp_list, temp_method) |
paul@506 | 425 | |
paul@506 | 426 | # Explicit dispatch to the expression. |
paul@506 | 427 | |
paul@506 | 428 | else: |
paul@506 | 429 | self.dispatch(expr) |
paul@160 | 430 | |
paul@506 | 431 | # Append the value to the list. |
paul@506 | 432 | |
paul@506 | 433 | temp_value = self.optimiser.optimise_temp_storage() |
paul@506 | 434 | self._generateInvocation(temp_method, (temp_list, temp_value,)) |
paul@506 | 435 | |
paul@506 | 436 | self._endFor(node, temp_iterator, next_block, exit_block, else_block) |
paul@506 | 437 | |
paul@506 | 438 | def visitListCompIf(self, node, following_tests, following_quals, expr, temp_list, temp_method): |
paul@506 | 439 | exit_block = self.new_block() |
paul@506 | 440 | |
paul@506 | 441 | # Evaluate the test and jump beyond the body if it is not satisfied. |
paul@506 | 442 | |
paul@506 | 443 | self.dispatch(node.test) |
paul@506 | 444 | |
paul@506 | 445 | temp = self.optimiser.optimise_temp_storage() |
paul@506 | 446 | self.new_op(temp) |
paul@506 | 447 | self._generateTestBoolean(node, temp) |
paul@506 | 448 | self.new_op(JumpIfFalse(exit_block, working="status")) |
paul@160 | 449 | |
paul@506 | 450 | # Explicit dispatch to tests, following loops, expression. |
paul@506 | 451 | |
paul@506 | 452 | if following_tests: |
paul@506 | 453 | self.visitListCompIf(following_tests[0], following_tests[1:], following_quals, expr, temp_list, temp_method) |
paul@506 | 454 | |
paul@506 | 455 | # Explicit dispatch to following loops, expression. |
paul@506 | 456 | |
paul@506 | 457 | elif following_quals: |
paul@506 | 458 | self.visitListCompFor(following_quals[0], following_quals[1:], expr, temp_list, temp_method) |
paul@506 | 459 | |
paul@506 | 460 | # Explicit dispatch to the expression. |
paul@506 | 461 | |
paul@506 | 462 | else: |
paul@506 | 463 | self.dispatch(expr) |
paul@506 | 464 | |
paul@506 | 465 | # Append the value to the list. |
paul@506 | 466 | |
paul@506 | 467 | temp_value = self.optimiser.optimise_temp_storage() |
paul@506 | 468 | self._generateInvocation(temp_method, (temp_list, temp_value,)) |
paul@506 | 469 | |
paul@506 | 470 | self.set_block(exit_block) |
paul@160 | 471 | |
paul@160 | 472 | def visitName(self, node): |
paul@343 | 473 | self._visitName(node, self.name_load_instructions) |
paul@160 | 474 | |
paul@342 | 475 | def visitSlice(self, node): |
paul@374 | 476 | args = [node.expr] |
paul@374 | 477 | |
paul@342 | 478 | if node.lower is None: |
paul@374 | 479 | args.append(compiler.ast.Name("None")) |
paul@342 | 480 | if node.upper is None: |
paul@374 | 481 | args.append(compiler.ast.Name("None")) |
paul@342 | 482 | else: |
paul@374 | 483 | args.append(node.upper) |
paul@342 | 484 | else: |
paul@374 | 485 | args.append(node.lower) |
paul@373 | 486 | if node.upper is None: |
paul@373 | 487 | args.append(compiler.ast.Name("None")) |
paul@373 | 488 | else: |
paul@373 | 489 | args.append(node.upper) |
paul@342 | 490 | |
paul@374 | 491 | temp_fn = self._getOperatorFunction(node, self.in_assignment and "AssSlice" or "Slice") |
paul@374 | 492 | self._visitCall(node, temp_fn, args) |
paul@374 | 493 | self.discard_temp(temp_fn) |
paul@160 | 494 | |
paul@216 | 495 | def visitSubscript(self, node): |
paul@374 | 496 | temp_fn = self._getOperatorFunction(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@374 | 497 | self._visitCall(node, temp_fn, [node.expr] + node.subs) |
paul@374 | 498 | self.discard_temp(temp_fn) |
paul@160 | 499 | |
paul@160 | 500 | def visitTuple(self, node): |
paul@246 | 501 | self._generateTuple(node) |
paul@160 | 502 | |
paul@160 | 503 | # Definitions. |
paul@56 | 504 | |
paul@56 | 505 | def visitAssign(self, node): |
paul@159 | 506 | |
paul@159 | 507 | """ |
paul@159 | 508 | Evaluate the expression from the given 'node' and assign it to the |
paul@159 | 509 | associated recipients. |
paul@159 | 510 | """ |
paul@159 | 511 | |
paul@56 | 512 | self.dispatch(node.expr) |
paul@146 | 513 | |
paul@146 | 514 | # Record the value and then dispatch to the assignment targets. |
paul@146 | 515 | |
paul@159 | 516 | self.record_value(self.has_immediate_usage(node.nodes)) |
paul@101 | 517 | |
paul@591 | 518 | self.in_assignment = True |
paul@374 | 519 | |
paul@56 | 520 | for n in node.nodes: |
paul@143 | 521 | self.dispatch(n) |
paul@56 | 522 | |
paul@591 | 523 | self.in_assignment = False |
paul@104 | 524 | self.discard_value() |
paul@101 | 525 | |
paul@143 | 526 | def visitAssAttr(self, node): |
paul@159 | 527 | |
paul@159 | 528 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 529 | |
paul@469 | 530 | self.start_target() |
paul@405 | 531 | self._visitAttr(node, self.optimiser.get_attribute_store_instructions()) |
paul@451 | 532 | self.assign_value() |
paul@56 | 533 | |
paul@143 | 534 | def visitAssList(self, node): |
paul@159 | 535 | |
paul@159 | 536 | """ |
paul@159 | 537 | Assign items from the assignment expression to each of the recipients |
paul@159 | 538 | found within the given 'node'. |
paul@159 | 539 | """ |
paul@159 | 540 | |
paul@368 | 541 | self.new_op(self.expr_temp[-1]) |
paul@368 | 542 | self._generateAttr(node, "__getitem__", self.attribute_load_instructions) |
paul@368 | 543 | temp_getitem = self.optimiser.optimise_temp_storage() |
paul@368 | 544 | |
paul@139 | 545 | for i, n in enumerate(node.nodes): |
paul@139 | 546 | self._startCallFunc() |
paul@368 | 547 | self.new_op(temp_getitem) |
paul@234 | 548 | temp_target, target, temp_context = self._generateCallFunc([compiler.ast.Const(i)], node) |
paul@234 | 549 | self._doCallFunc(temp_target, target) |
paul@464 | 550 | self._endCallFunc(temp_context=temp_context) |
paul@139 | 551 | |
paul@139 | 552 | # Provide a different source value. |
paul@159 | 553 | # NOTE: Permitting immediate usage given that neither name nor |
paul@159 | 554 | # NOTE: attribute accesses should involve a function call |
paul@159 | 555 | # NOTE: overwriting the above result. |
paul@159 | 556 | |
paul@159 | 557 | self.record_value(self.is_immediate_user(n)) |
paul@143 | 558 | self.dispatch(n) |
paul@139 | 559 | self.discard_value() |
paul@139 | 560 | |
paul@368 | 561 | self.discard_temp(temp_getitem) |
paul@368 | 562 | |
paul@143 | 563 | def visitAssName(self, node): |
paul@159 | 564 | |
paul@159 | 565 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 566 | |
paul@599 | 567 | if node.flags == "OP_DELETE": |
paul@339 | 568 | raise TranslationNotImplementedError("AssName(OP_DELETE)") |
paul@599 | 569 | self._visitAssName(node) |
paul@271 | 570 | |
paul@599 | 571 | def _visitAssName(self, node): |
paul@469 | 572 | self.start_target() |
paul@106 | 573 | self._visitName(node, self.name_store_instructions) |
paul@451 | 574 | self.assign_value() |
paul@56 | 575 | |
paul@279 | 576 | # Add any attribute usage guards. |
paul@279 | 577 | |
paul@381 | 578 | self._generateGuards(node) |
paul@279 | 579 | |
paul@56 | 580 | visitAssTuple = visitAssList |
paul@56 | 581 | |
paul@151 | 582 | def visitAugAssign(self, node): |
paul@151 | 583 | |
paul@354 | 584 | # Find the augmented assignment function and attempt to use it. |
paul@151 | 585 | |
paul@354 | 586 | temp_fn = self._getOperatorAugAssignFunction(node) |
paul@374 | 587 | self._visitCall(node, temp_fn, (node.node, node.expr)) |
paul@354 | 588 | self.discard_temp(temp_fn) |
paul@151 | 589 | |
paul@151 | 590 | # Assign the result to the name. |
paul@151 | 591 | |
paul@151 | 592 | self.record_value(1) |
paul@151 | 593 | |
paul@151 | 594 | if isinstance(node.node, compiler.ast.Name): |
paul@599 | 595 | self._visitAssName(node.node) |
paul@151 | 596 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@151 | 597 | self.visitAssAttr(node.node) |
paul@151 | 598 | else: |
paul@339 | 599 | raise TranslationNotImplementedError("AugAssign(Slice or Subscript)") |
paul@151 | 600 | |
paul@151 | 601 | self.discard_value() |
paul@151 | 602 | |
paul@38 | 603 | def visitClass(self, node): |
paul@353 | 604 | if not used_by_unit(node): |
paul@167 | 605 | return |
paul@90 | 606 | |
paul@90 | 607 | # Store the name. |
paul@90 | 608 | |
paul@237 | 609 | self.new_op(LoadClass(node.unit)) |
paul@563 | 610 | self._storeName(node) |
paul@90 | 611 | |
paul@90 | 612 | # Visit the code. |
paul@90 | 613 | |
paul@38 | 614 | unit = self.unit |
paul@39 | 615 | self.unit = node.unit |
paul@38 | 616 | self.dispatch(node.code) |
paul@38 | 617 | self.unit = unit |
paul@17 | 618 | |
paul@339 | 619 | def visitDecorators(self, node): raise TranslationNotImplementedError("Decorators") |
paul@160 | 620 | |
paul@562 | 621 | def visitFrom(self, node): |
paul@563 | 622 | self._visitImport(node) |
paul@562 | 623 | |
paul@563 | 624 | # Store each imported name if its reference is not set as a constant |
paul@563 | 625 | # module global. |
paul@563 | 626 | |
paul@563 | 627 | module = self.importer.get_module(node.modname) |
paul@562 | 628 | |
paul@563 | 629 | for name, alias in node.names: |
paul@563 | 630 | if name != "*": |
paul@563 | 631 | self._importName(module, name, alias, node) |
paul@563 | 632 | else: |
paul@563 | 633 | for attrname in module.module_attributes().keys(): |
paul@563 | 634 | self._importName(module, attrname, None, node) |
paul@160 | 635 | |
paul@160 | 636 | def visitFunction(self, node): |
paul@353 | 637 | if not used_by_unit(node): |
paul@167 | 638 | return |
paul@160 | 639 | |
paul@160 | 640 | # Only store the name when visiting this node from outside. |
paul@160 | 641 | |
paul@160 | 642 | if self.unit is not node.unit: |
paul@330 | 643 | self._visitFunctionDeclaration(node) |
paul@330 | 644 | |
paul@330 | 645 | # Record the declared function. |
paul@330 | 646 | |
paul@563 | 647 | self._storeName(node) |
paul@160 | 648 | |
paul@165 | 649 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 650 | |
paul@160 | 651 | else: |
paul@330 | 652 | self._visitFunctionDefinition(node) |
paul@160 | 653 | |
paul@160 | 654 | def visitGlobal(self, node): pass |
paul@160 | 655 | |
paul@563 | 656 | def visitImport(self, node): |
paul@563 | 657 | self._visitImport(node) |
paul@563 | 658 | |
paul@563 | 659 | for name, alias in node.names: |
paul@563 | 660 | module = self.importer.get_module(name) |
paul@563 | 661 | self.new_op(LoadConst(module)) |
paul@563 | 662 | self._storeName(node, alias or name) |
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@591 | 737 | first = True |
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@591 | 764 | first = False |
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@591 | 871 | self.in_exception_handler = True |
paul@57 | 872 | self.dispatch(handler) |
paul@591 | 873 | self.in_exception_handler = False |
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 |