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