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