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