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