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