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@398 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011 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@246 | 385 | self._generateList(node) |
paul@160 | 386 | |
paul@339 | 387 | def visitListComp(self, node): raise TranslationNotImplementedError("ListComp") |
paul@160 | 388 | |
paul@339 | 389 | def visitListCompFor(self, node): raise TranslationNotImplementedError("ListCompFor") |
paul@160 | 390 | |
paul@339 | 391 | def visitListCompIf(self, node): raise TranslationNotImplementedError("ListCompIf") |
paul@160 | 392 | |
paul@160 | 393 | def visitName(self, node): |
paul@343 | 394 | self._visitName(node, self.name_load_instructions) |
paul@160 | 395 | |
paul@342 | 396 | def visitSlice(self, node): |
paul@374 | 397 | args = [node.expr] |
paul@374 | 398 | |
paul@342 | 399 | if node.lower is None: |
paul@374 | 400 | args.append(compiler.ast.Name("None")) |
paul@342 | 401 | if node.upper is None: |
paul@374 | 402 | args.append(compiler.ast.Name("None")) |
paul@342 | 403 | else: |
paul@374 | 404 | args.append(node.upper) |
paul@342 | 405 | else: |
paul@374 | 406 | args.append(node.lower) |
paul@373 | 407 | if node.upper is None: |
paul@373 | 408 | args.append(compiler.ast.Name("None")) |
paul@373 | 409 | else: |
paul@373 | 410 | args.append(node.upper) |
paul@342 | 411 | |
paul@374 | 412 | temp_fn = self._getOperatorFunction(node, self.in_assignment and "AssSlice" or "Slice") |
paul@374 | 413 | self._visitCall(node, temp_fn, args) |
paul@374 | 414 | self.discard_temp(temp_fn) |
paul@160 | 415 | |
paul@216 | 416 | def visitSubscript(self, node): |
paul@374 | 417 | temp_fn = self._getOperatorFunction(node, self.in_assignment and "AssSubscript" or "Subscript") |
paul@374 | 418 | self._visitCall(node, temp_fn, [node.expr] + node.subs) |
paul@374 | 419 | self.discard_temp(temp_fn) |
paul@160 | 420 | |
paul@160 | 421 | def visitTuple(self, node): |
paul@246 | 422 | self._generateTuple(node) |
paul@160 | 423 | |
paul@160 | 424 | # Definitions. |
paul@56 | 425 | |
paul@56 | 426 | def visitAssign(self, node): |
paul@159 | 427 | |
paul@159 | 428 | """ |
paul@159 | 429 | Evaluate the expression from the given 'node' and assign it to the |
paul@159 | 430 | associated recipients. |
paul@159 | 431 | """ |
paul@159 | 432 | |
paul@56 | 433 | self.dispatch(node.expr) |
paul@146 | 434 | |
paul@146 | 435 | # Record the value and then dispatch to the assignment targets. |
paul@146 | 436 | |
paul@159 | 437 | self.record_value(self.has_immediate_usage(node.nodes)) |
paul@101 | 438 | |
paul@374 | 439 | self.in_assignment = 1 |
paul@374 | 440 | |
paul@56 | 441 | for n in node.nodes: |
paul@143 | 442 | self.dispatch(n) |
paul@56 | 443 | |
paul@374 | 444 | self.in_assignment = 0 |
paul@104 | 445 | self.discard_value() |
paul@101 | 446 | |
paul@143 | 447 | def visitAssAttr(self, node): |
paul@159 | 448 | |
paul@159 | 449 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 450 | |
paul@469 | 451 | self.start_target() |
paul@405 | 452 | self._visitAttr(node, self.optimiser.get_attribute_store_instructions()) |
paul@451 | 453 | self.assign_value() |
paul@56 | 454 | |
paul@143 | 455 | def visitAssList(self, node): |
paul@159 | 456 | |
paul@159 | 457 | """ |
paul@159 | 458 | Assign items from the assignment expression to each of the recipients |
paul@159 | 459 | found within the given 'node'. |
paul@159 | 460 | """ |
paul@159 | 461 | |
paul@368 | 462 | self.new_op(self.expr_temp[-1]) |
paul@368 | 463 | self._generateAttr(node, "__getitem__", self.attribute_load_instructions) |
paul@368 | 464 | temp_getitem = self.optimiser.optimise_temp_storage() |
paul@368 | 465 | |
paul@139 | 466 | for i, n in enumerate(node.nodes): |
paul@139 | 467 | self._startCallFunc() |
paul@368 | 468 | self.new_op(temp_getitem) |
paul@234 | 469 | temp_target, target, temp_context = self._generateCallFunc([compiler.ast.Const(i)], node) |
paul@234 | 470 | self._doCallFunc(temp_target, target) |
paul@464 | 471 | self._endCallFunc(temp_context=temp_context) |
paul@139 | 472 | |
paul@139 | 473 | # Provide a different source value. |
paul@159 | 474 | # NOTE: Permitting immediate usage given that neither name nor |
paul@159 | 475 | # NOTE: attribute accesses should involve a function call |
paul@159 | 476 | # NOTE: overwriting the above result. |
paul@159 | 477 | |
paul@159 | 478 | self.record_value(self.is_immediate_user(n)) |
paul@143 | 479 | self.dispatch(n) |
paul@139 | 480 | self.discard_value() |
paul@139 | 481 | |
paul@368 | 482 | self.discard_temp(temp_getitem) |
paul@368 | 483 | |
paul@143 | 484 | def visitAssName(self, node): |
paul@159 | 485 | |
paul@159 | 486 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 487 | |
paul@289 | 488 | if hasattr(node, "flags") and node.flags == "OP_DELETE": |
paul@339 | 489 | raise TranslationNotImplementedError("AssName(OP_DELETE)") |
paul@271 | 490 | |
paul@469 | 491 | self.start_target() |
paul@106 | 492 | self._visitName(node, self.name_store_instructions) |
paul@451 | 493 | self.assign_value() |
paul@56 | 494 | |
paul@279 | 495 | # Add any attribute usage guards. |
paul@279 | 496 | |
paul@381 | 497 | self._generateGuards(node) |
paul@279 | 498 | |
paul@56 | 499 | visitAssTuple = visitAssList |
paul@56 | 500 | |
paul@151 | 501 | def visitAugAssign(self, node): |
paul@151 | 502 | |
paul@354 | 503 | # Find the augmented assignment function and attempt to use it. |
paul@151 | 504 | |
paul@354 | 505 | temp_fn = self._getOperatorAugAssignFunction(node) |
paul@374 | 506 | self._visitCall(node, temp_fn, (node.node, node.expr)) |
paul@354 | 507 | self.discard_temp(temp_fn) |
paul@151 | 508 | |
paul@151 | 509 | # Assign the result to the name. |
paul@151 | 510 | |
paul@151 | 511 | self.record_value(1) |
paul@151 | 512 | |
paul@151 | 513 | if isinstance(node.node, compiler.ast.Name): |
paul@151 | 514 | self.visitAssName(node.node) |
paul@151 | 515 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@151 | 516 | self.visitAssAttr(node.node) |
paul@151 | 517 | else: |
paul@339 | 518 | raise TranslationNotImplementedError("AugAssign(Slice or Subscript)") |
paul@151 | 519 | |
paul@151 | 520 | self.discard_value() |
paul@151 | 521 | |
paul@38 | 522 | def visitClass(self, node): |
paul@353 | 523 | if not used_by_unit(node): |
paul@167 | 524 | return |
paul@90 | 525 | |
paul@90 | 526 | # Store the name. |
paul@90 | 527 | |
paul@237 | 528 | self.new_op(LoadClass(node.unit)) |
paul@104 | 529 | self.record_value() |
paul@469 | 530 | self.start_target() |
paul@106 | 531 | self._visitName(node, self.name_store_instructions) |
paul@451 | 532 | self.assign_value() |
paul@104 | 533 | self.discard_value() |
paul@90 | 534 | |
paul@90 | 535 | # Visit the code. |
paul@90 | 536 | |
paul@38 | 537 | unit = self.unit |
paul@39 | 538 | self.unit = node.unit |
paul@38 | 539 | self.dispatch(node.code) |
paul@38 | 540 | self.unit = unit |
paul@17 | 541 | |
paul@339 | 542 | def visitDecorators(self, node): raise TranslationNotImplementedError("Decorators") |
paul@160 | 543 | |
paul@160 | 544 | def visitFrom(self, node): pass |
paul@160 | 545 | |
paul@160 | 546 | def visitFunction(self, node): |
paul@353 | 547 | if not used_by_unit(node): |
paul@167 | 548 | return |
paul@160 | 549 | |
paul@160 | 550 | # Only store the name when visiting this node from outside. |
paul@160 | 551 | |
paul@160 | 552 | if self.unit is not node.unit: |
paul@330 | 553 | self._visitFunctionDeclaration(node) |
paul@330 | 554 | |
paul@330 | 555 | # Record the declared function. |
paul@330 | 556 | |
paul@222 | 557 | self.record_value() |
paul@160 | 558 | |
paul@469 | 559 | self.start_target() |
paul@160 | 560 | self._visitName(node, self.name_store_instructions) # AssName equivalent |
paul@451 | 561 | self.assign_value() |
paul@160 | 562 | self.discard_value() |
paul@160 | 563 | |
paul@165 | 564 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 565 | |
paul@160 | 566 | else: |
paul@330 | 567 | self._visitFunctionDefinition(node) |
paul@160 | 568 | |
paul@160 | 569 | def visitGlobal(self, node): pass |
paul@160 | 570 | |
paul@160 | 571 | def visitImport(self, node): pass |
paul@160 | 572 | |
paul@160 | 573 | def visitKeyword(self, node): pass |
paul@160 | 574 | |
paul@160 | 575 | def visitLambda(self, node): |
paul@21 | 576 | |
paul@21 | 577 | """ |
paul@160 | 578 | Lambda functions can be represented as globally defined functions |
paul@160 | 579 | provided they do not define any default parameter values, since these |
paul@160 | 580 | may defined in a non-global scope. |
paul@160 | 581 | |
paul@160 | 582 | Where defaults are defined, an object must be created and its content |
paul@160 | 583 | defined: the callable member of the object's structure must be set to |
paul@160 | 584 | the lambda function definition; each default must be attached to the |
paul@160 | 585 | object as an attribute, as is the case with normal functions and |
paul@160 | 586 | methods. |
paul@112 | 587 | """ |
paul@112 | 588 | |
paul@160 | 589 | # Produce the reference to this function when visiting this node from |
paul@160 | 590 | # outside. |
paul@160 | 591 | |
paul@160 | 592 | if self.unit is not node.unit: |
paul@160 | 593 | |
paul@330 | 594 | # Provide the declared function. |
paul@160 | 595 | |
paul@330 | 596 | self._visitFunctionDeclaration(node) |
paul@160 | 597 | |
paul@165 | 598 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 599 | |
paul@160 | 600 | else: |
paul@330 | 601 | self._visitFunctionDefinition(node) |
paul@215 | 602 | |
paul@160 | 603 | def visitModule(self, node): |
paul@160 | 604 | extend = ExtendFrame() |
paul@160 | 605 | self.new_op(extend) |
paul@160 | 606 | self.dispatch(node.node) |
paul@160 | 607 | self.set_frame_usage(node, extend) |
paul@160 | 608 | |
paul@160 | 609 | # Statements. |
paul@160 | 610 | |
paul@160 | 611 | def visitStmt(self, node): |
paul@160 | 612 | |
paul@160 | 613 | "Process the collection of statements provided by 'node'." |
paul@160 | 614 | |
paul@160 | 615 | for n in node.nodes: |
paul@160 | 616 | |
paul@160 | 617 | # Process the statement. |
paul@160 | 618 | |
paul@160 | 619 | self.dispatch(n) |
paul@160 | 620 | |
paul@160 | 621 | # Prevent incorrect optimisation by resetting the optimiser after |
paul@160 | 622 | # each statement. |
paul@160 | 623 | |
paul@160 | 624 | self.optimiser.reset() |
paul@160 | 625 | |
paul@339 | 626 | def visitAssert(self, node): raise TranslationNotImplementedError("Assert") |
paul@160 | 627 | |
paul@160 | 628 | def visitBreak(self, node): |
paul@163 | 629 | next_block, exit_block = self.get_loop_blocks() |
paul@163 | 630 | self.new_op(Jump(exit_block)) |
paul@17 | 631 | |
paul@41 | 632 | def visitContinue(self, node): |
paul@163 | 633 | next_block, exit_block = self.get_loop_blocks() |
paul@163 | 634 | self.new_op(Jump(next_block)) |
paul@17 | 635 | |
paul@18 | 636 | def visitDiscard(self, node): |
paul@19 | 637 | self.dispatch(node.expr) |
paul@140 | 638 | self.optimiser.optimise_unused_results() |
paul@17 | 639 | |
paul@44 | 640 | def visitFor(self, node): |
paul@163 | 641 | next_handler_block = self.new_block() |
paul@163 | 642 | end_handler_block = self.new_block() |
paul@163 | 643 | exit_block = self.new_block() |
paul@163 | 644 | next_block = self.new_block() |
paul@163 | 645 | else_block = self.new_block() |
paul@44 | 646 | |
paul@44 | 647 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 648 | |
paul@45 | 649 | self._startCallFunc() |
paul@44 | 650 | self.dispatch(node.list) |
paul@104 | 651 | self._generateAttr(node, "__iter__", self.attribute_load_instructions) |
paul@234 | 652 | temp_target, target, temp_context = self._generateCallFunc([], node) |
paul@234 | 653 | self._doCallFunc(temp_target, target) |
paul@369 | 654 | self._endCallFunc(temp_target, temp_context) |
paul@57 | 655 | |
paul@240 | 656 | # Use a long-lasting temporary storage slot, since any result from the |
paul@240 | 657 | # __iter__ method will not remain around for long. |
paul@240 | 658 | |
paul@240 | 659 | temp_iterator = self.get_temp() |
paul@98 | 660 | |
paul@44 | 661 | # In the loop... |
paul@44 | 662 | |
paul@163 | 663 | self.set_block(next_block) |
paul@44 | 664 | |
paul@146 | 665 | # Handle exceptions when calling "next"... |
paul@146 | 666 | |
paul@398 | 667 | self.add_exception_blocks(next_handler_block, end_handler_block) |
paul@163 | 668 | self.new_op(PushHandler(next_handler_block)) |
paul@146 | 669 | |
paul@44 | 670 | # Use the iterator to get the next value. |
paul@44 | 671 | |
paul@45 | 672 | self._startCallFunc() |
paul@102 | 673 | self.new_op(temp_iterator) |
paul@104 | 674 | self._generateAttr(node, "next", self.attribute_load_instructions) |
paul@234 | 675 | temp_target, target, temp_context = self._generateCallFunc([], node) |
paul@234 | 676 | self._doCallFunc(temp_target, target) |
paul@369 | 677 | self._endCallFunc(temp_target, temp_context) |
paul@44 | 678 | |
paul@139 | 679 | # Record the value to be assigned. |
paul@139 | 680 | |
paul@139 | 681 | self.record_value() |
paul@139 | 682 | |
paul@146 | 683 | # Skip the handler where the call was successful. |
paul@146 | 684 | |
paul@398 | 685 | self.new_op(PopHandler(1)) |
paul@163 | 686 | self.new_op(Jump(end_handler_block)) |
paul@146 | 687 | |
paul@146 | 688 | # Enter the exception handler. |
paul@146 | 689 | |
paul@163 | 690 | self.set_block(next_handler_block) |
paul@398 | 691 | self.new_op(PopHandler(1)) |
paul@398 | 692 | |
paul@398 | 693 | # Disable the handlers. |
paul@398 | 694 | |
paul@398 | 695 | self.drop_exception_blocks() |
paul@146 | 696 | |
paul@44 | 697 | # Test for StopIteration. |
paul@44 | 698 | |
paul@101 | 699 | self.load_builtin("StopIteration", node) |
paul@450 | 700 | self.new_op(CheckException(target="status")) |
paul@44 | 701 | if node.else_ is not None: |
paul@450 | 702 | self.new_op(JumpIfTrue(else_block, working="status")) |
paul@44 | 703 | else: |
paul@450 | 704 | self.new_op(JumpIfTrue(exit_block, working="status")) |
paul@44 | 705 | |
paul@146 | 706 | # Re-raise the exception otherwise. |
paul@146 | 707 | |
paul@146 | 708 | self.new_op(RaiseException()) |
paul@146 | 709 | |
paul@232 | 710 | # After the handler, clear the exception. |
paul@146 | 711 | |
paul@163 | 712 | self.set_block(end_handler_block) |
paul@146 | 713 | |
paul@44 | 714 | # Assign to the target. |
paul@44 | 715 | |
paul@44 | 716 | self.dispatch(node.assign) |
paul@139 | 717 | self.discard_value() |
paul@44 | 718 | |
paul@44 | 719 | # Process the body with the current next and exit points. |
paul@44 | 720 | |
paul@163 | 721 | self.add_loop_blocks(next_block, exit_block) |
paul@44 | 722 | self.dispatch(node.body) |
paul@163 | 723 | self.drop_loop_blocks() |
paul@44 | 724 | |
paul@44 | 725 | # Repeat the loop. |
paul@44 | 726 | |
paul@163 | 727 | self.new_op(Jump(next_block)) |
paul@44 | 728 | |
paul@44 | 729 | # Produce the "else" section. |
paul@44 | 730 | |
paul@44 | 731 | if node.else_ is not None: |
paul@240 | 732 | self.set_block(else_block) |
paul@450 | 733 | self.new_op(ClearException(target="exception")) |
paul@44 | 734 | self.dispatch(node.else_) |
paul@44 | 735 | |
paul@240 | 736 | # After the loop... |
paul@240 | 737 | |
paul@240 | 738 | self.set_block(exit_block) |
paul@44 | 739 | |
paul@240 | 740 | else: |
paul@240 | 741 | # After the loop... |
paul@240 | 742 | |
paul@240 | 743 | self.set_block(exit_block) |
paul@450 | 744 | self.new_op(ClearException(target="exception")) |
paul@17 | 745 | |
paul@98 | 746 | # Compilation duties... |
paul@98 | 747 | |
paul@98 | 748 | self.discard_temp(temp_iterator) |
paul@98 | 749 | |
paul@17 | 750 | def visitIf(self, node): |
paul@19 | 751 | first = 1 |
paul@232 | 752 | next_block = None |
paul@163 | 753 | exit_block = self.new_block() |
paul@17 | 754 | |
paul@108 | 755 | clauses = node.tests + [(None, node.else_)] |
paul@108 | 756 | |
paul@108 | 757 | for clause in clauses: |
paul@108 | 758 | test, body = clause |
paul@19 | 759 | if body is None: |
paul@19 | 760 | break |
paul@232 | 761 | |
paul@19 | 762 | if not first: |
paul@232 | 763 | self.new_op(Jump(exit_block)) # finish last body |
paul@232 | 764 | self.set_block(next_block) # start next test |
paul@232 | 765 | next_block = None |
paul@232 | 766 | |
paul@19 | 767 | if test is not None: |
paul@19 | 768 | self.dispatch(test) |
paul@234 | 769 | |
paul@234 | 770 | temp = self.optimiser.optimise_temp_storage() |
paul@455 | 771 | self.new_op(temp) |
paul@234 | 772 | self._generateTestBoolean(node, temp) |
paul@234 | 773 | |
paul@163 | 774 | next_block = self.new_block() |
paul@450 | 775 | self.new_op(JumpIfFalse(next_block, working="status")) |
paul@232 | 776 | |
paul@19 | 777 | self.dispatch(body) |
paul@232 | 778 | first = 0 |
paul@160 | 779 | |
paul@232 | 780 | if next_block is not None: |
paul@232 | 781 | self.set_block(next_block) |
paul@17 | 782 | |
paul@163 | 783 | self.set_block(exit_block) |
paul@17 | 784 | |
paul@462 | 785 | def visitIfExp(self, node): raise TranslationNotImplementedError("IfExp") |
paul@462 | 786 | |
paul@19 | 787 | def visitPass(self, node): pass |
paul@17 | 788 | |
paul@393 | 789 | def visitPrint(self, node): |
paul@393 | 790 | self._visitPrint(node, "_print") |
paul@17 | 791 | |
paul@393 | 792 | def visitPrintnl(self, node): |
paul@393 | 793 | self._visitPrint(node, "_printnl") |
paul@17 | 794 | |
paul@97 | 795 | def visitRaise(self, node): |
paul@411 | 796 | |
paul@411 | 797 | if node.expr1 is not None: |
paul@411 | 798 | |
paul@411 | 799 | # NOTE: expr1 only => instance provided |
paul@97 | 800 | |
paul@411 | 801 | self.dispatch(node.expr1) |
paul@411 | 802 | |
paul@411 | 803 | if node.expr2 is not None: |
paul@411 | 804 | temp = self.optimiser.optimise_temp_storage() |
paul@102 | 805 | |
paul@411 | 806 | self.dispatch(node.expr2) |
paul@411 | 807 | temp_arg = self.optimiser.optimise_temp_storage() |
paul@411 | 808 | |
paul@411 | 809 | self._generateInvocation(temp, (temp_arg,)) |
paul@102 | 810 | |
paul@411 | 811 | self.discard_temp(temp_arg) |
paul@102 | 812 | |
paul@455 | 813 | self.new_op(Transfer(source="working", target="exception")) |
paul@97 | 814 | |
paul@97 | 815 | self.new_op(RaiseException()) |
paul@17 | 816 | |
paul@21 | 817 | def visitReturn(self, node): |
paul@21 | 818 | if node.value is not None: |
paul@21 | 819 | self.dispatch(node.value) |
paul@68 | 820 | else: |
paul@68 | 821 | self.dispatch(compiler.ast.Name("None")) |
paul@104 | 822 | |
paul@235 | 823 | if self.in_exception_handler: |
paul@450 | 824 | self.new_op(ClearException(target="exception")) |
paul@235 | 825 | |
paul@398 | 826 | # NOTE: Support finally blocks here. |
paul@398 | 827 | |
paul@398 | 828 | if self.exception_blocks[-1]: |
paul@398 | 829 | self.new_op(PopHandler(len(self.exception_blocks[-1]))) |
paul@398 | 830 | |
paul@21 | 831 | self.new_op(Return()) |
paul@17 | 832 | |
paul@57 | 833 | def visitTryExcept(self, node): |
paul@163 | 834 | exit_block = self.new_block() |
paul@163 | 835 | else_block = self.new_block() |
paul@163 | 836 | handler_block = self.new_block() |
paul@163 | 837 | |
paul@163 | 838 | self.add_exception_blocks(handler_block, exit_block) |
paul@57 | 839 | |
paul@68 | 840 | # Try... |
paul@68 | 841 | # Produce the code, then jump to the exit. |
paul@68 | 842 | |
paul@163 | 843 | self.new_op(PushHandler(handler_block)) |
paul@57 | 844 | self.dispatch(node.body) |
paul@398 | 845 | self.new_op(PopHandler(1)) |
paul@160 | 846 | |
paul@160 | 847 | if node.else_ is not None: |
paul@163 | 848 | self.new_op(Jump(else_block)) |
paul@160 | 849 | else: |
paul@163 | 850 | self.new_op(Jump(exit_block)) |
paul@57 | 851 | |
paul@68 | 852 | # Start of handlers. |
paul@68 | 853 | |
paul@163 | 854 | self.set_block(handler_block) |
paul@398 | 855 | self.new_op(PopHandler(1)) |
paul@398 | 856 | |
paul@398 | 857 | # Disable the handlers. |
paul@398 | 858 | |
paul@398 | 859 | self.drop_exception_blocks() |
paul@102 | 860 | |
paul@57 | 861 | for name, assignment, handler in node.handlers: |
paul@163 | 862 | next_block = self.new_block() |
paul@57 | 863 | |
paul@68 | 864 | # Test the given exception against the current exception. |
paul@68 | 865 | |
paul@57 | 866 | if name is not None: |
paul@57 | 867 | self.dispatch(name) |
paul@145 | 868 | |
paul@450 | 869 | self.new_op(CheckException(target="status")) |
paul@450 | 870 | self.new_op(JumpIfFalse(next_block, working="status")) |
paul@57 | 871 | |
paul@145 | 872 | # Handle assignment to exception variable. |
paul@145 | 873 | |
paul@145 | 874 | if assignment is not None: |
paul@450 | 875 | self.new_op(Transfer(source="working", target="exception")) |
paul@145 | 876 | |
paul@145 | 877 | # Record the value to be assigned. |
paul@145 | 878 | |
paul@145 | 879 | self.record_value() |
paul@145 | 880 | self.dispatch(assignment) |
paul@145 | 881 | self.discard_value() |
paul@57 | 882 | |
paul@68 | 883 | # Produce the handler code, then jump to the exit. |
paul@68 | 884 | |
paul@235 | 885 | self.in_exception_handler = 1 |
paul@57 | 886 | self.dispatch(handler) |
paul@235 | 887 | self.in_exception_handler = 0 |
paul@235 | 888 | |
paul@163 | 889 | self.new_op(Jump(exit_block)) |
paul@163 | 890 | |
paul@163 | 891 | self.set_block(next_block) |
paul@160 | 892 | |
paul@57 | 893 | # Unhandled exceptions. |
paul@57 | 894 | |
paul@57 | 895 | self.new_op(RaiseException()) |
paul@57 | 896 | |
paul@57 | 897 | # Optional else clause. |
paul@57 | 898 | |
paul@57 | 899 | if node.else_ is not None: |
paul@163 | 900 | self.set_block(else_block) |
paul@57 | 901 | self.dispatch(node.else_) |
paul@57 | 902 | |
paul@232 | 903 | # Clear the exception. |
paul@232 | 904 | |
paul@163 | 905 | self.set_block(exit_block) |
paul@450 | 906 | self.new_op(ClearException(target="exception")) |
paul@17 | 907 | |
paul@262 | 908 | def visitTryFinally(self, node): |
paul@262 | 909 | |
paul@262 | 910 | """ |
paul@262 | 911 | Add finally handler, potentially as an exception handler. |
paul@262 | 912 | Generate body, potentially changing return statements so that they do |
paul@262 | 913 | not return immediately. |
paul@262 | 914 | Generate handler, removing the handler from the active handler list, |
paul@262 | 915 | adding instructions which raise active exceptions. |
paul@262 | 916 | """ |
paul@262 | 917 | |
paul@339 | 918 | raise TranslationNotImplementedError("TryFinally") |
paul@17 | 919 | |
paul@41 | 920 | def visitWhile(self, node): |
paul@163 | 921 | exit_block = self.new_block() |
paul@163 | 922 | next_block = self.new_block() |
paul@163 | 923 | else_block = self.new_block() |
paul@163 | 924 | |
paul@163 | 925 | self.set_block(next_block) |
paul@44 | 926 | self.dispatch(node.test) |
paul@310 | 927 | |
paul@310 | 928 | temp = self.optimiser.optimise_temp_storage() |
paul@455 | 929 | self.new_op(temp) |
paul@310 | 930 | self._generateTestBoolean(node, temp) |
paul@310 | 931 | |
paul@44 | 932 | if node.else_ is not None: |
paul@450 | 933 | self.new_op(JumpIfFalse(else_block, working="status")) |
paul@44 | 934 | else: |
paul@450 | 935 | self.new_op(JumpIfFalse(exit_block, working="status")) |
paul@163 | 936 | |
paul@163 | 937 | self.add_loop_blocks(next_block, exit_block) |
paul@41 | 938 | |
paul@41 | 939 | self.dispatch(node.body) |
paul@163 | 940 | self.new_op(Jump(next_block)) |
paul@41 | 941 | |
paul@41 | 942 | if node.else_ is not None: |
paul@163 | 943 | self.set_block(else_block) |
paul@160 | 944 | |
paul@41 | 945 | self.dispatch(node.else_) |
paul@41 | 946 | |
paul@163 | 947 | self.set_block(exit_block) |
paul@163 | 948 | |
paul@163 | 949 | self.drop_loop_blocks() |
paul@17 | 950 | |
paul@339 | 951 | def visitWith(self, node): raise TranslationNotImplementedError("With") |
paul@17 | 952 | |
paul@339 | 953 | def visitYield(self, node): raise TranslationNotImplementedError("Yield") |
paul@17 | 954 | |
paul@17 | 955 | # vim: tabstop=4 expandtab shiftwidth=4 |