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