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