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@39 | 6 | Copyright (C) 2007, 2008 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@17 | 26 | import compiler.ast |
paul@17 | 27 | from compiler.visitor import ASTVisitor |
paul@17 | 28 | |
paul@17 | 29 | # Program visitors. |
paul@17 | 30 | |
paul@17 | 31 | class Translation(ASTVisitor): |
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@98 | 37 | attribute_load_instructions = (LoadAddress, LoadAddressContext, LoadAttr, LoadAttrIndex) |
paul@106 | 38 | attribute_store_instructions = (None, None, StoreAttr, StoreAttrIndex) |
paul@122 | 39 | |
paul@122 | 40 | # Name access instructions, for use with the appropriate handlers. |
paul@122 | 41 | |
paul@106 | 42 | name_load_instructions = (LoadName, LoadAddress) |
paul@106 | 43 | name_store_instructions = (StoreName, StoreAddress) |
paul@98 | 44 | |
paul@167 | 45 | def __init__(self, module, importer): |
paul@18 | 46 | |
paul@21 | 47 | """ |
paul@167 | 48 | Initialise the translation with an inspected 'module' and the module |
paul@167 | 49 | 'importer'. |
paul@21 | 50 | """ |
paul@18 | 51 | |
paul@17 | 52 | ASTVisitor.__init__(self) |
paul@17 | 53 | self.visitor = self |
paul@17 | 54 | self.module = module |
paul@72 | 55 | |
paul@72 | 56 | # Global program dependencies. |
paul@72 | 57 | |
paul@72 | 58 | self.importer = importer |
paul@72 | 59 | self.objtable = self.importer.get_object_table() |
paul@72 | 60 | self.paramtable = self.importer.get_parameter_table() |
paul@72 | 61 | self.builtins = self.importer.modules.get("__builtins__") |
paul@61 | 62 | |
paul@140 | 63 | # Optimisation. |
paul@140 | 64 | |
paul@167 | 65 | self.optimiser = Optimiser(self, importer.optimisations) |
paul@62 | 66 | |
paul@61 | 67 | # The current unit being translated. |
paul@61 | 68 | |
paul@17 | 69 | self.unit = None |
paul@17 | 70 | |
paul@103 | 71 | # The temporary storage used by the current assignment expression. |
paul@103 | 72 | |
paul@139 | 73 | self.expr_temp = [] |
paul@103 | 74 | |
paul@19 | 75 | # Wiring within the code. |
paul@19 | 76 | |
paul@19 | 77 | self.labels = {} |
paul@19 | 78 | self.label_number = 0 |
paul@163 | 79 | self.loop_blocks = [] |
paul@163 | 80 | self.exception_blocks = [] |
paul@41 | 81 | |
paul@156 | 82 | self.reset() |
paul@19 | 83 | |
paul@95 | 84 | def __repr__(self): |
paul@95 | 85 | return "Translation(%r)" % self.module |
paul@72 | 86 | |
paul@156 | 87 | def reset(self): |
paul@156 | 88 | |
paul@156 | 89 | "Reset the state of the translator." |
paul@156 | 90 | |
paul@156 | 91 | # The code itself. This is limited to the code for a particular block |
paul@156 | 92 | # being processed. |
paul@156 | 93 | |
paul@163 | 94 | self.blocks = [] |
paul@156 | 95 | |
paul@156 | 96 | # Information about temporary values. |
paul@156 | 97 | |
paul@156 | 98 | self.temp_positions = set() |
paul@156 | 99 | self.max_temp_position = -1 |
paul@156 | 100 | |
paul@156 | 101 | # Information about instructions which construct frames. |
paul@156 | 102 | |
paul@156 | 103 | self.frame_makers = [] |
paul@156 | 104 | |
paul@156 | 105 | # Optimiser state must be reset for each unit. |
paul@156 | 106 | |
paul@156 | 107 | self.optimiser.reset() |
paul@156 | 108 | |
paul@139 | 109 | def get_module_code(self, final=0): |
paul@139 | 110 | |
paul@139 | 111 | """ |
paul@139 | 112 | Return the top-level module code including finalising code if 'final' is |
paul@139 | 113 | set to a true value. |
paul@139 | 114 | """ |
paul@17 | 115 | |
paul@17 | 116 | self.unit = self.module |
paul@156 | 117 | self.reset() |
paul@73 | 118 | |
paul@163 | 119 | block = self.new_block() |
paul@163 | 120 | self.set_block(block) |
paul@163 | 121 | |
paul@44 | 122 | if self.module.module is not None: |
paul@44 | 123 | self.dispatch(self.module.module) |
paul@73 | 124 | |
paul@139 | 125 | # Finish off the translated program if appropriate. |
paul@139 | 126 | |
paul@139 | 127 | if final: |
paul@139 | 128 | self.new_op(Return()) |
paul@139 | 129 | |
paul@116 | 130 | self.unit.temp_usage = self.max_temp_position + 1 |
paul@173 | 131 | self.unit.blocks = self.blocks |
paul@163 | 132 | return self.blocks |
paul@17 | 133 | |
paul@17 | 134 | def get_code(self, unit): |
paul@17 | 135 | |
paul@17 | 136 | "Return the code for the given 'unit'." |
paul@17 | 137 | |
paul@17 | 138 | self.unit = unit |
paul@156 | 139 | self.reset() |
paul@73 | 140 | |
paul@163 | 141 | block = self.new_block() |
paul@163 | 142 | self.set_block(block) |
paul@163 | 143 | |
paul@91 | 144 | if unit.astnode is not None: |
paul@91 | 145 | self.dispatch(unit.astnode) |
paul@73 | 146 | |
paul@116 | 147 | self.unit.temp_usage = self.max_temp_position + 1 |
paul@173 | 148 | self.unit.blocks = self.blocks |
paul@163 | 149 | return self.blocks |
paul@17 | 150 | |
paul@137 | 151 | def get_instantiator_code(self, cls): |
paul@137 | 152 | |
paul@137 | 153 | "Return the code for the given class 'cls'." |
paul@137 | 154 | |
paul@137 | 155 | self.unit = cls.get_instantiator() |
paul@156 | 156 | self.reset() |
paul@137 | 157 | |
paul@163 | 158 | block = self.new_block() |
paul@163 | 159 | self.set_block(block) |
paul@163 | 160 | |
paul@137 | 161 | init_method = cls.get_init_method() |
paul@137 | 162 | |
paul@137 | 163 | # Convert this frame back to being an invocation frame. |
paul@137 | 164 | |
paul@137 | 165 | self.new_op(RecoverFrame()) |
paul@137 | 166 | |
paul@137 | 167 | # Fix the current frame to include a new storage slot at the beginning. |
paul@137 | 168 | |
paul@137 | 169 | self.new_op(AdjustFrame(-1)) |
paul@137 | 170 | |
paul@137 | 171 | # Make an object. |
paul@137 | 172 | |
paul@138 | 173 | self.make_object(cls, len(cls.instance_attributes())) |
paul@137 | 174 | self.new_op(StoreFrame(0)) |
paul@137 | 175 | |
paul@137 | 176 | # Invoke the appropriate initialiser. |
paul@137 | 177 | |
paul@138 | 178 | self.new_op(LoadConst(init_method)) |
paul@137 | 179 | self.new_op(LoadCallable()) |
paul@137 | 180 | self.new_op(JumpWithFrame()) |
paul@137 | 181 | |
paul@137 | 182 | # Store the object as the result. |
paul@137 | 183 | |
paul@137 | 184 | self.new_op(LoadName(init_method.all_locals()["self"])) # load the context in the invocation frame |
paul@137 | 185 | self.new_op(StoreResult()) |
paul@137 | 186 | self.new_op(Return()) |
paul@137 | 187 | |
paul@173 | 188 | self.unit.blocks = self.blocks |
paul@163 | 189 | return self.blocks |
paul@137 | 190 | |
paul@138 | 191 | # Allocation-related methods. |
paul@138 | 192 | |
paul@138 | 193 | def make_object(self, cls, n): |
paul@138 | 194 | |
paul@138 | 195 | """ |
paul@138 | 196 | Request a new object with the given class 'cls' and with 'n' attributes. |
paul@138 | 197 | """ |
paul@138 | 198 | |
paul@138 | 199 | # NOTE: Object headers are one location. |
paul@138 | 200 | |
paul@138 | 201 | self.new_op(LoadConst(cls)) |
paul@138 | 202 | self.new_op(MakeObject(n + 1)) |
paul@138 | 203 | |
paul@101 | 204 | # Name-related methods. |
paul@101 | 205 | |
paul@17 | 206 | def get_scope(self, name): |
paul@99 | 207 | |
paul@99 | 208 | "Return the scope for the given 'name'." |
paul@99 | 209 | |
paul@17 | 210 | if self.unit.has_key(name): |
paul@17 | 211 | return "local" |
paul@17 | 212 | elif self.module.has_key(name): |
paul@17 | 213 | return "global" |
paul@17 | 214 | else: |
paul@17 | 215 | return "builtins" |
paul@17 | 216 | |
paul@101 | 217 | def load_builtin(self, name, node): |
paul@95 | 218 | |
paul@101 | 219 | "Generate an instruction loading 'name' for the given 'node'." |
paul@95 | 220 | |
paul@101 | 221 | self.new_op(LoadAddress(self.get_builtin(name, node))) |
paul@95 | 222 | |
paul@138 | 223 | def get_builtin_class(self, name, node): |
paul@156 | 224 | |
paul@156 | 225 | "Return the built-in class with the given 'name' for the given 'node'." |
paul@156 | 226 | |
paul@138 | 227 | return self.get_builtin(name, node).value |
paul@138 | 228 | |
paul@101 | 229 | def get_builtin(self, name, node): |
paul@95 | 230 | |
paul@101 | 231 | """ |
paul@101 | 232 | Return the built-in module definition for the given 'name', used by the |
paul@101 | 233 | given 'node'. |
paul@101 | 234 | """ |
paul@95 | 235 | |
paul@101 | 236 | if self.builtins is not None: |
paul@101 | 237 | try: |
paul@101 | 238 | return self.builtins[name] |
paul@101 | 239 | except KeyError: |
paul@101 | 240 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@101 | 241 | else: |
paul@101 | 242 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@97 | 243 | |
paul@101 | 244 | # Code feature methods. |
paul@97 | 245 | |
paul@163 | 246 | def new_block(self): |
paul@163 | 247 | |
paul@163 | 248 | "Return a new code block." |
paul@163 | 249 | |
paul@163 | 250 | return Block() |
paul@163 | 251 | |
paul@163 | 252 | def set_block(self, block): |
paul@163 | 253 | |
paul@163 | 254 | "Add the given 'block' to the unit's list of blocks." |
paul@163 | 255 | |
paul@163 | 256 | self.optimiser.reset() |
paul@163 | 257 | self.blocks.append(block) |
paul@163 | 258 | |
paul@163 | 259 | def get_loop_blocks(self): |
paul@163 | 260 | return self.loop_blocks[-1] |
paul@163 | 261 | |
paul@163 | 262 | def add_loop_blocks(self, next_block, exit_block): |
paul@163 | 263 | self.loop_blocks.append((next_block, exit_block)) |
paul@163 | 264 | |
paul@163 | 265 | def drop_loop_blocks(self): |
paul@163 | 266 | self.loop_blocks.pop() |
paul@163 | 267 | |
paul@163 | 268 | def get_exception_blocks(self): |
paul@163 | 269 | return self.exception_blocks[-1] |
paul@163 | 270 | |
paul@163 | 271 | def add_exception_blocks(self, handler_block, exit_block): |
paul@163 | 272 | self.exception_blocks.append((handler_block, exit_block)) |
paul@163 | 273 | |
paul@163 | 274 | def drop_exception_blocks(self): |
paul@163 | 275 | self.exception_blocks.pop() |
paul@163 | 276 | |
paul@104 | 277 | # Assignment expression values. |
paul@104 | 278 | |
paul@143 | 279 | def record_value(self, immediate=1): |
paul@156 | 280 | |
paul@156 | 281 | """ |
paul@156 | 282 | Record the current active value for an assignment. If the optional |
paul@156 | 283 | 'immediate' parameter if set to a false value always allocates new |
paul@156 | 284 | temporary storage to hold the recorded value; otherwise, the |
paul@156 | 285 | value-providing instruction may be replicated in order to provide the |
paul@156 | 286 | active value later on. |
paul@156 | 287 | """ |
paul@156 | 288 | |
paul@143 | 289 | if immediate: |
paul@143 | 290 | temp = self.optimiser.optimise_temp_storage() |
paul@143 | 291 | else: |
paul@143 | 292 | temp = self.get_temp() |
paul@143 | 293 | self.expr_temp.append(temp) |
paul@104 | 294 | |
paul@104 | 295 | def discard_value(self): |
paul@156 | 296 | |
paul@156 | 297 | "Discard any temporary storage in use for the current assignment value." |
paul@156 | 298 | |
paul@139 | 299 | self.discard_temp(self.expr_temp.pop()) |
paul@104 | 300 | |
paul@104 | 301 | def set_source(self): |
paul@156 | 302 | |
paul@156 | 303 | "Set the source of an assignment using the current assignment value." |
paul@156 | 304 | |
paul@154 | 305 | self.optimiser.set_source(self.expr_temp[-1]) |
paul@104 | 306 | |
paul@104 | 307 | # Optimise away constant storage if appropriate. |
paul@104 | 308 | |
paul@156 | 309 | if self.optimiser.optimise_constant_storage(): |
paul@156 | 310 | self.remove_op() |
paul@104 | 311 | |
paul@159 | 312 | def is_immediate_user(self, node): |
paul@159 | 313 | |
paul@159 | 314 | """ |
paul@159 | 315 | Return whether 'node' is an immediate user of an assignment expression. |
paul@159 | 316 | """ |
paul@159 | 317 | |
paul@159 | 318 | return isinstance(node, (compiler.ast.AssName, compiler.ast.AssAttr)) |
paul@159 | 319 | |
paul@159 | 320 | def has_immediate_usage(self, nodes): |
paul@159 | 321 | |
paul@159 | 322 | """ |
paul@159 | 323 | Return whether 'nodes' are all immediate users of an assignment expression. |
paul@159 | 324 | """ |
paul@159 | 325 | |
paul@159 | 326 | for n in nodes: |
paul@159 | 327 | if not self.is_immediate_user(n): |
paul@159 | 328 | return 0 |
paul@159 | 329 | return 1 |
paul@159 | 330 | |
paul@104 | 331 | # Temporary storage administration. |
paul@104 | 332 | |
paul@101 | 333 | def get_temp(self): |
paul@101 | 334 | |
paul@101 | 335 | """ |
paul@101 | 336 | Add a temporary storage instruction for the current value and return a |
paul@101 | 337 | sequence of access instructions. |
paul@101 | 338 | """ |
paul@101 | 339 | |
paul@130 | 340 | position_in_frame = self.reserve_temp() |
paul@117 | 341 | self.new_op(StoreTemp(position_in_frame)) |
paul@117 | 342 | return LoadTemp(position_in_frame) |
paul@101 | 343 | |
paul@152 | 344 | def reserve_temp(self, temp_position=None): |
paul@159 | 345 | |
paul@159 | 346 | """ |
paul@159 | 347 | Reserve a new temporary storage position, or if the optional |
paul@159 | 348 | 'temp_position' is specified, ensure that this particular position is |
paul@159 | 349 | reserved. |
paul@159 | 350 | """ |
paul@159 | 351 | |
paul@152 | 352 | if temp_position is not None: |
paul@152 | 353 | pass |
paul@152 | 354 | elif not self.temp_positions: |
paul@114 | 355 | temp_position = 0 |
paul@114 | 356 | else: |
paul@139 | 357 | temp_position = max(self.temp_positions) + 1 |
paul@114 | 358 | self.temp_positions.add(temp_position) |
paul@116 | 359 | self.max_temp_position = max(self.max_temp_position, temp_position) |
paul@133 | 360 | return self.unit.all_local_usage + temp_position # position in frame |
paul@73 | 361 | |
paul@152 | 362 | def ensure_temp(self, instruction=None): |
paul@159 | 363 | |
paul@159 | 364 | """ |
paul@159 | 365 | Ensure that the 'instruction' is using a reserved temporary storage |
paul@159 | 366 | position. |
paul@159 | 367 | """ |
paul@159 | 368 | |
paul@152 | 369 | if isinstance(instruction, LoadTemp): |
paul@152 | 370 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@152 | 371 | self.reserve_temp(temp_position) |
paul@152 | 372 | |
paul@114 | 373 | def discard_temp(self, instruction=None): |
paul@159 | 374 | |
paul@159 | 375 | "Discard any temporary storage position used by 'instruction'." |
paul@159 | 376 | |
paul@102 | 377 | if isinstance(instruction, LoadTemp): |
paul@133 | 378 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@130 | 379 | self.free_temp(temp_position) |
paul@130 | 380 | |
paul@130 | 381 | def free_temp(self, temp_position): |
paul@159 | 382 | |
paul@159 | 383 | "Free the temporary storage position specified by 'temp_position'." |
paul@159 | 384 | |
paul@130 | 385 | if temp_position in self.temp_positions: |
paul@130 | 386 | self.temp_positions.remove(temp_position) |
paul@73 | 387 | |
paul@137 | 388 | def set_frame_usage(self, node, extend): |
paul@159 | 389 | |
paul@159 | 390 | """ |
paul@159 | 391 | Ensure that the frame usage for the unit associated with 'node' is set |
paul@159 | 392 | on the 'extend' instruction. |
paul@159 | 393 | """ |
paul@159 | 394 | |
paul@144 | 395 | ntemp = self.max_temp_position + 1 |
paul@165 | 396 | extend.attr = ntemp + node.unit.local_usage # NOTE: See get_code for similar code. |
paul@137 | 397 | |
paul@95 | 398 | # Code writing methods. |
paul@95 | 399 | |
paul@19 | 400 | def new_op(self, op): |
paul@44 | 401 | |
paul@44 | 402 | "Add 'op' to the generated code." |
paul@44 | 403 | |
paul@103 | 404 | # Optimise load operations employed by this instruction. |
paul@103 | 405 | |
paul@140 | 406 | self.optimiser.optimise_load_operations(op) |
paul@140 | 407 | if self.optimiser.optimise_away_no_operations(op): |
paul@113 | 408 | return |
paul@103 | 409 | |
paul@163 | 410 | # Add the operation to the current block. |
paul@163 | 411 | |
paul@163 | 412 | self.blocks[-1].code.append(op) |
paul@154 | 413 | self.optimiser.set_new(op) |
paul@122 | 414 | |
paul@101 | 415 | def remove_op(self): |
paul@95 | 416 | |
paul@101 | 417 | "Remove the last instruction." |
paul@101 | 418 | |
paul@163 | 419 | op = self.blocks[-1].code.pop() |
paul@154 | 420 | self.optimiser.clear_active() |
paul@123 | 421 | |
paul@43 | 422 | def replace_op(self, op): |
paul@44 | 423 | |
paul@44 | 424 | "Replace the last added instruction with 'op'." |
paul@44 | 425 | |
paul@101 | 426 | self.remove_op() |
paul@93 | 427 | self.new_op(op) |
paul@93 | 428 | |
paul@129 | 429 | def replace_active_value(self, op): |
paul@129 | 430 | |
paul@129 | 431 | """ |
paul@129 | 432 | Replace the value-providing active instruction with 'op' if appropriate. |
paul@129 | 433 | """ |
paul@129 | 434 | |
paul@154 | 435 | self.optimiser.remove_active_value() |
paul@129 | 436 | self.new_op(op) |
paul@129 | 437 | |
paul@81 | 438 | def last_op(self): |
paul@44 | 439 | |
paul@44 | 440 | "Return the last added instruction." |
paul@44 | 441 | |
paul@70 | 442 | try: |
paul@163 | 443 | return self.blocks[-1].code[-1] |
paul@70 | 444 | except IndexError: |
paul@70 | 445 | return None |
paul@26 | 446 | |
paul@103 | 447 | # Visitor methods. |
paul@103 | 448 | |
paul@103 | 449 | def default(self, node, *args): |
paul@103 | 450 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@103 | 451 | |
paul@103 | 452 | def dispatch(self, node, *args): |
paul@103 | 453 | return ASTVisitor.dispatch(self, node, *args) |
paul@103 | 454 | |
paul@56 | 455 | # Internal helper methods. |
paul@17 | 456 | |
paul@43 | 457 | def _visitAttr(self, node, classes): |
paul@44 | 458 | |
paul@44 | 459 | """ |
paul@44 | 460 | Visit the attribute-related 'node', generating instructions based on the |
paul@44 | 461 | given 'classes'. |
paul@44 | 462 | """ |
paul@44 | 463 | |
paul@43 | 464 | self.dispatch(node.expr) |
paul@63 | 465 | self._generateAttr(node, node.attrname, classes) |
paul@43 | 466 | |
paul@63 | 467 | def _generateAttr(self, node, attrname, classes): |
paul@44 | 468 | |
paul@44 | 469 | """ |
paul@44 | 470 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@44 | 471 | """ |
paul@44 | 472 | |
paul@98 | 473 | AddressInstruction, AddressContextInstruction, AttrInstruction, AttrIndexInstruction = classes |
paul@43 | 474 | |
paul@56 | 475 | # Where the last operation (defining the attribute owner) yields a |
paul@56 | 476 | # constant... |
paul@54 | 477 | |
paul@155 | 478 | target_name = self.optimiser.optimise_constant_accessor() |
paul@155 | 479 | |
paul@155 | 480 | # Only try and discover the position if the target can be resolved. |
paul@155 | 481 | |
paul@155 | 482 | if target_name is not None: |
paul@155 | 483 | |
paul@155 | 484 | # Access the object table to get the attribute position. |
paul@155 | 485 | |
paul@155 | 486 | try: |
paul@155 | 487 | table_entry = self.objtable.table[target_name] |
paul@155 | 488 | except KeyError: |
paul@155 | 489 | raise TranslateError(self.module.full_name(), node, |
paul@155 | 490 | "No object entry exists for target %r." % target_name) |
paul@155 | 491 | |
paul@155 | 492 | try: |
paul@155 | 493 | pos = table_entry[attrname] |
paul@155 | 494 | except KeyError: |
paul@155 | 495 | raise TranslateError(self.module.full_name(), node, |
paul@155 | 496 | "No attribute entry exists for name %r in target %r." % (attrname, target_name)) |
paul@155 | 497 | |
paul@155 | 498 | # Produce a suitable instruction. |
paul@155 | 499 | |
paul@155 | 500 | if AddressInstruction is not None: |
paul@155 | 501 | self.replace_active_value(AddressInstruction(pos)) |
paul@82 | 502 | else: |
paul@155 | 503 | raise TranslateError(self.module.full_name(), node, |
paul@155 | 504 | "Storing of class or module attribute %r via an object is not permitted." % attrname) |
paul@155 | 505 | |
paul@155 | 506 | return |
paul@54 | 507 | |
paul@54 | 508 | # Where the last operation involves the special 'self' name, check to |
paul@65 | 509 | # see if the attribute is acceptably positioned and produce a direct |
paul@65 | 510 | # access to the attribute. |
paul@54 | 511 | |
paul@156 | 512 | elif self.optimiser.optimise_self_access(self.unit, attrname): |
paul@156 | 513 | |
paul@156 | 514 | # Either generate an instruction operating on an instance attribute. |
paul@156 | 515 | |
paul@156 | 516 | try: |
paul@156 | 517 | attr = self.unit.parent.instance_attributes()[attrname] |
paul@156 | 518 | self.new_op(AttrInstruction(attr)) |
paul@156 | 519 | |
paul@156 | 520 | # Or generate an instruction operating on a class attribute. |
paul@156 | 521 | |
paul@156 | 522 | except KeyError: |
paul@156 | 523 | attr = self.unit.parent.all_attributes()[attrname] |
paul@156 | 524 | |
paul@156 | 525 | # Switch the context if the class attribute is compatible with |
paul@156 | 526 | # the instance. |
paul@156 | 527 | |
paul@156 | 528 | if attr.defined_within_hierarchy(): |
paul@156 | 529 | |
paul@156 | 530 | # Only permit loading (not storing) of class attributes via self. |
paul@156 | 531 | |
paul@156 | 532 | if AddressContextInstruction is not None: |
paul@156 | 533 | self.new_op(AddressContextInstruction(attr)) |
paul@156 | 534 | else: |
paul@156 | 535 | raise TranslateError(self.module.full_name(), node, |
paul@156 | 536 | "Storing of class attribute %r via self not permitted." % attrname) |
paul@156 | 537 | |
paul@156 | 538 | # Preserve the context if the class attribute comes from an |
paul@156 | 539 | # incompatible class. |
paul@156 | 540 | |
paul@156 | 541 | else: |
paul@156 | 542 | if AddressInstruction is not None: |
paul@156 | 543 | self.new_op(AddressInstruction(attr)) |
paul@156 | 544 | else: |
paul@156 | 545 | raise TranslateError(self.module.full_name(), node, |
paul@156 | 546 | "Storing of class attribute %r via self not permitted." % attrname) |
paul@156 | 547 | |
paul@86 | 548 | return |
paul@54 | 549 | |
paul@54 | 550 | # Otherwise, perform a normal operation. |
paul@43 | 551 | |
paul@86 | 552 | try: |
paul@86 | 553 | index = self.objtable.get_index(attrname) |
paul@156 | 554 | |
paul@86 | 555 | except self.objtable.TableError: |
paul@86 | 556 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 557 | "No attribute entry exists for name %r." % attrname) |
paul@63 | 558 | |
paul@86 | 559 | self.new_op(AttrIndexInstruction(index)) |
paul@43 | 560 | |
paul@101 | 561 | # Invocations involve the following: |
paul@101 | 562 | # |
paul@101 | 563 | # 1. Reservation of a frame for the arguments |
paul@101 | 564 | # 2. Identification of the target which is then held in temporary storage |
paul@101 | 565 | # 3. Optional inclusion of a context (important for methods) |
paul@101 | 566 | # 4. Preparation of the argument frame |
paul@101 | 567 | # 5. Invocation of the target |
paul@101 | 568 | # 6. Discarding of the frame |
paul@101 | 569 | # |
paul@101 | 570 | # In order to support nested invocations - eg. a(b(c)) - use of the |
paul@101 | 571 | # temporary storage is essential. |
paul@101 | 572 | |
paul@45 | 573 | def _startCallFunc(self): |
paul@45 | 574 | |
paul@45 | 575 | "Record the location of the invocation." |
paul@21 | 576 | |
paul@117 | 577 | op = MakeFrame() |
paul@117 | 578 | self.new_op(op) # records the start of the frame |
paul@117 | 579 | self.frame_makers.append(op) |
paul@21 | 580 | |
paul@44 | 581 | def _generateCallFunc(self, args, node): |
paul@44 | 582 | |
paul@86 | 583 | """ |
paul@86 | 584 | Support a generic function invocation using the given 'args', occurring |
paul@86 | 585 | on the given 'node', where the expression providing the invocation |
paul@86 | 586 | target has just been generated. |
paul@86 | 587 | |
paul@86 | 588 | In other situations, the invocation is much simpler and does not need to |
paul@86 | 589 | handle the full flexibility of a typical Python invocation. Internal |
paul@86 | 590 | invocations, such as those employed by operators and certain |
paul@86 | 591 | control-flow mechanisms, use predetermined arguments and arguably do not |
paul@86 | 592 | need to support the same things as the more general invocations. |
paul@86 | 593 | """ |
paul@86 | 594 | |
paul@86 | 595 | target, context, temp = self._generateCallFuncContext() |
paul@109 | 596 | self._generateCallFuncArgs(target, context, temp, args, node) |
paul@137 | 597 | return temp, target |
paul@86 | 598 | |
paul@86 | 599 | def _generateCallFuncContext(self): |
paul@86 | 600 | |
paul@86 | 601 | """ |
paul@86 | 602 | Produce code which loads and checks the context of the current |
paul@86 | 603 | invocation, the instructions for whose target have already been |
paul@86 | 604 | produced, returning a list of instructions which reference the |
paul@86 | 605 | invocation target. |
paul@86 | 606 | """ |
paul@26 | 607 | |
paul@140 | 608 | t = self.optimiser.optimise_known_target() |
paul@77 | 609 | if t: |
paul@77 | 610 | target, context = t |
paul@167 | 611 | if isinstance(target, Instance): # lambda object |
paul@167 | 612 | target, context = None, None |
paul@77 | 613 | else: |
paul@77 | 614 | target, context = None, None |
paul@45 | 615 | |
paul@78 | 616 | # Store the target in temporary storage for subsequent referencing. |
paul@137 | 617 | # NOTE: This may not be appropriate for class invocations |
paul@137 | 618 | # NOTE: (instantiation). |
paul@78 | 619 | |
paul@140 | 620 | temp = self.optimiser.optimise_temp_storage() |
paul@78 | 621 | |
paul@98 | 622 | # Where a target or context are not known or where an instance is known |
paul@98 | 623 | # to be the context, load the context. |
paul@45 | 624 | |
paul@132 | 625 | if target is None or isinstance(context, Instance): |
paul@102 | 626 | self.new_op(temp) |
paul@45 | 627 | self.new_op(LoadContext()) |
paul@98 | 628 | self.new_op(StoreFrame(0)) |
paul@87 | 629 | |
paul@137 | 630 | # For known instantiations, provide a new object as the first argument |
paul@137 | 631 | # to the __init__ method. |
paul@137 | 632 | |
paul@137 | 633 | elif isinstance(target, Class): |
paul@138 | 634 | self.make_object(target, len(target.instance_attributes())) |
paul@137 | 635 | self.new_op(StoreFrame(0)) |
paul@137 | 636 | |
paul@87 | 637 | # Otherwise omit the context. |
paul@87 | 638 | |
paul@59 | 639 | else: |
paul@59 | 640 | pass # NOTE: Class methods should be supported. |
paul@26 | 641 | |
paul@86 | 642 | return target, context, temp |
paul@86 | 643 | |
paul@109 | 644 | def _generateCallFuncArgs(self, target, context, temp, args, node): |
paul@86 | 645 | |
paul@86 | 646 | """ |
paul@109 | 647 | Given invocation 'target' and 'context' information, the 'temp' |
paul@109 | 648 | reference to the target, a list of nodes representing the 'args' |
paul@109 | 649 | (arguments), generate instructions which load the arguments for the |
paul@109 | 650 | invocation defined by the given 'node'. |
paul@86 | 651 | """ |
paul@86 | 652 | |
paul@21 | 653 | # Evaluate the arguments. |
paul@21 | 654 | |
paul@67 | 655 | employed_positions = set() |
paul@132 | 656 | employed_keywords = set() |
paul@59 | 657 | extra_keywords = [] |
paul@22 | 658 | |
paul@132 | 659 | # Find keyword arguments in advance in order to help resolve targets. |
paul@132 | 660 | |
paul@132 | 661 | for arg in args: |
paul@132 | 662 | if isinstance(arg, compiler.ast.Keyword): |
paul@132 | 663 | employed_keywords.add(arg.name) |
paul@132 | 664 | |
paul@132 | 665 | possible_targets = self.paramtable.all_possible_objects(employed_keywords) |
paul@132 | 666 | |
paul@98 | 667 | # Note the presence of the context in the frame where appropriate. |
paul@86 | 668 | |
paul@132 | 669 | if target is None or isinstance(context, Instance): |
paul@86 | 670 | ncontext = 1 |
paul@98 | 671 | expect_context = 0 |
paul@137 | 672 | |
paul@137 | 673 | # Handle calls to classes. |
paul@137 | 674 | |
paul@137 | 675 | elif isinstance(target, Class): |
paul@137 | 676 | ncontext = 1 |
paul@137 | 677 | expect_context = 0 |
paul@137 | 678 | target = target.get_init_method() |
paul@137 | 679 | |
paul@137 | 680 | # Method calls via classes. |
paul@137 | 681 | |
paul@98 | 682 | elif isinstance(context, Class): |
paul@98 | 683 | ncontext = 0 |
paul@98 | 684 | expect_context = 1 |
paul@137 | 685 | |
paul@137 | 686 | # Function calls. |
paul@137 | 687 | |
paul@86 | 688 | else: |
paul@86 | 689 | ncontext = 0 |
paul@98 | 690 | expect_context = 0 |
paul@86 | 691 | |
paul@92 | 692 | first = 1 |
paul@92 | 693 | frame_pos = ncontext |
paul@112 | 694 | max_keyword_pos = -1 |
paul@92 | 695 | |
paul@92 | 696 | for arg in args: |
paul@66 | 697 | |
paul@66 | 698 | # Handle positional and keyword arguments separately. |
paul@66 | 699 | |
paul@21 | 700 | if isinstance(arg, compiler.ast.Keyword): |
paul@66 | 701 | |
paul@26 | 702 | # Optimise where the target is known now. |
paul@26 | 703 | |
paul@26 | 704 | if target is not None: |
paul@39 | 705 | |
paul@39 | 706 | # Find the parameter table entry for the target. |
paul@39 | 707 | |
paul@39 | 708 | target_name = target.full_name() |
paul@39 | 709 | |
paul@39 | 710 | # Look for a callable with the precise target name. |
paul@39 | 711 | |
paul@59 | 712 | table_entry = self.paramtable.table[target_name] |
paul@59 | 713 | |
paul@59 | 714 | # Look the name up in the parameter table entry. |
paul@39 | 715 | |
paul@59 | 716 | try: |
paul@59 | 717 | pos = table_entry[arg.name] |
paul@59 | 718 | |
paul@59 | 719 | # Where no position is found, this could be an extra keyword |
paul@59 | 720 | # argument. |
paul@39 | 721 | |
paul@39 | 722 | except KeyError: |
paul@59 | 723 | extra_keywords.append(arg) |
paul@59 | 724 | continue |
paul@58 | 725 | |
paul@58 | 726 | # Test for illegal conditions. |
paul@58 | 727 | |
paul@67 | 728 | if pos in employed_positions: |
paul@58 | 729 | raise TranslateError(self.module.full_name(), node, |
paul@58 | 730 | "Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@67 | 731 | |
paul@67 | 732 | employed_positions.add(pos) |
paul@58 | 733 | |
paul@59 | 734 | # Generate code for the keyword and the positioning |
paul@59 | 735 | # operation. |
paul@59 | 736 | |
paul@59 | 737 | self.dispatch(arg.expr) |
paul@98 | 738 | self.new_op(StoreFrame(pos)) |
paul@26 | 739 | |
paul@26 | 740 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 741 | # the parameter location. |
paul@26 | 742 | |
paul@26 | 743 | else: |
paul@21 | 744 | |
paul@26 | 745 | # Combine the target details with the name to get the location. |
paul@26 | 746 | # See the access method on the List class. |
paul@26 | 747 | |
paul@39 | 748 | try: |
paul@39 | 749 | paramindex = self.paramtable.get_index(arg.name) |
paul@59 | 750 | |
paul@59 | 751 | # Where no position is found, this could be an extra keyword |
paul@59 | 752 | # argument. |
paul@59 | 753 | |
paul@70 | 754 | except self.paramtable.TableError: |
paul@59 | 755 | extra_keywords.append(arg) |
paul@59 | 756 | continue |
paul@21 | 757 | |
paul@59 | 758 | # Generate code for the keyword and the positioning |
paul@59 | 759 | # operation. |
paul@59 | 760 | |
paul@59 | 761 | self.dispatch(arg.expr) |
paul@26 | 762 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 763 | |
paul@26 | 764 | # use (callable+0)+paramindex+table |
paul@26 | 765 | # checks embedded offset against (callable+0) |
paul@101 | 766 | # moves the current value to frame+position |
paul@21 | 767 | |
paul@111 | 768 | # Record the highest possible frame position for this argument. |
paul@111 | 769 | |
paul@111 | 770 | max_keyword_pos = max(max_keyword_pos, max(self.paramtable.all_attribute_positions(arg.name))) |
paul@111 | 771 | |
paul@21 | 772 | else: |
paul@21 | 773 | self.dispatch(arg) |
paul@98 | 774 | self.new_op(StoreFrame(frame_pos)) |
paul@104 | 775 | |
paul@98 | 776 | employed_positions.add(frame_pos) |
paul@21 | 777 | |
paul@92 | 778 | # Check to see if the first argument is appropriate (compatible with |
paul@98 | 779 | # the target where methods are being invoked via classes). |
paul@92 | 780 | |
paul@98 | 781 | if first and expect_context: |
paul@122 | 782 | |
paul@122 | 783 | # Drop any test if the target and the context are known. |
paul@122 | 784 | |
paul@158 | 785 | if not self.optimiser.have_correct_self_for_target(context, self.unit): |
paul@122 | 786 | |
paul@163 | 787 | continue_block = self.new_block() |
paul@163 | 788 | |
paul@122 | 789 | self.new_op(CheckSelf()) |
paul@154 | 790 | self.optimiser.set_source(temp) |
paul@163 | 791 | self.new_op(JumpIfTrue(continue_block)) |
paul@122 | 792 | |
paul@122 | 793 | # Where the context is inappropriate, drop the incomplete frame and |
paul@122 | 794 | # raise an exception. |
paul@122 | 795 | |
paul@122 | 796 | self.new_op(DropFrame()) |
paul@122 | 797 | self.new_op(LoadResult()) |
paul@122 | 798 | |
paul@122 | 799 | self.load_builtin("TypeError", node) |
paul@122 | 800 | self.new_op(StoreException()) |
paul@122 | 801 | self.new_op(RaiseException()) |
paul@163 | 802 | |
paul@163 | 803 | self.set_block(continue_block) |
paul@92 | 804 | |
paul@95 | 805 | first = 0 |
paul@92 | 806 | frame_pos += 1 |
paul@66 | 807 | |
paul@75 | 808 | # NOTE: Extra keywords are not supported. |
paul@75 | 809 | # NOTE: Somehow, the above needs to be combined with * arguments. |
paul@59 | 810 | |
paul@112 | 811 | if extra_keywords: |
paul@112 | 812 | print "Warning: extra keyword argument(s) %s not handled." % ", ".join([arg.name for arg in extra_keywords]) |
paul@112 | 813 | |
paul@59 | 814 | # Either test for a complete set of arguments. |
paul@59 | 815 | |
paul@59 | 816 | if target is not None: |
paul@66 | 817 | |
paul@66 | 818 | # Make sure that enough arguments have been given. |
paul@66 | 819 | |
paul@67 | 820 | nargs_max = len(target.positional_names) |
paul@66 | 821 | ndefaults = len(target.defaults) |
paul@66 | 822 | nargs_min = nargs_max - ndefaults |
paul@67 | 823 | |
paul@86 | 824 | for i in range(ncontext, nargs_min): |
paul@67 | 825 | if i not in employed_positions: |
paul@67 | 826 | raise TranslateError(self.module.full_name(), node, |
paul@110 | 827 | "Argument %r not supplied for %r: need at least %d argument(s)." % (i+1, target.name, nargs_min)) |
paul@67 | 828 | |
paul@137 | 829 | nargs = frame_pos |
paul@66 | 830 | |
paul@67 | 831 | if nargs > nargs_max and not target.has_star and not target.has_dstar: |
paul@59 | 832 | raise TranslateError(self.module.full_name(), node, |
paul@110 | 833 | "Too many arguments for %r: need at most %d argument(s)." % (target.name, nargs_max)) |
paul@66 | 834 | |
paul@66 | 835 | # Where defaults are involved, put them into the frame. |
paul@66 | 836 | |
paul@109 | 837 | self._generateCallFuncDefaultArgs(target, temp, nargs_min, nargs_max, employed_positions) |
paul@59 | 838 | |
paul@117 | 839 | # Set the frame size. |
paul@117 | 840 | |
paul@129 | 841 | self._endCallFuncArgs(nargs_max) |
paul@117 | 842 | |
paul@59 | 843 | # Or generate instructions to do this at run-time. |
paul@66 | 844 | # NOTE: CheckFrame has to check the number of arguments and to fill in |
paul@98 | 845 | # NOTE: defaults; it also has to shift the invocation frame according to |
paul@98 | 846 | # NOTE: the context in use. |
paul@59 | 847 | |
paul@59 | 848 | else: |
paul@117 | 849 | max_pos = max(max(employed_positions or [-1]), max_keyword_pos, frame_pos - 1) |
paul@112 | 850 | |
paul@132 | 851 | # Only check non-empty frames (using the callable's details). |
paul@112 | 852 | |
paul@117 | 853 | if employed_positions or max_pos >= 0: |
paul@132 | 854 | self.new_op(temp) |
paul@112 | 855 | self.new_op(CheckFrame(max_pos + 1)) |
paul@59 | 856 | |
paul@117 | 857 | # Set the frame size. |
paul@117 | 858 | |
paul@129 | 859 | self._endCallFuncArgs(max_pos + 1) |
paul@117 | 860 | |
paul@109 | 861 | def _generateCallFuncDefaultArgs(self, target, temp, nargs_min, nargs_max, employed_positions): |
paul@109 | 862 | |
paul@109 | 863 | """ |
paul@109 | 864 | For the given 'target' and 'temp' reference to the target, generate |
paul@109 | 865 | default arguments for those positions in the range 'nargs_min'... |
paul@109 | 866 | 'nargs_max' which are not present in the 'employed_positions' |
paul@109 | 867 | collection. |
paul@109 | 868 | """ |
paul@109 | 869 | |
paul@109 | 870 | # Where a lambda is involved, construct a dynamic object to hold the |
paul@109 | 871 | # defaults. |
paul@109 | 872 | |
paul@109 | 873 | dynamic = target.name is None |
paul@109 | 874 | |
paul@109 | 875 | # Here, we use negative index values to visit the right hand end of |
paul@109 | 876 | # the defaults list. |
paul@109 | 877 | |
paul@109 | 878 | for pos in range(nargs_min, nargs_max): |
paul@109 | 879 | if pos not in employed_positions: |
paul@109 | 880 | if dynamic: |
paul@109 | 881 | self.new_op(temp) |
paul@109 | 882 | self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@109 | 883 | else: |
paul@109 | 884 | self.new_op(LoadAddress(target.default_attrs[pos - nargs_min])) |
paul@109 | 885 | self.new_op(StoreFrame(pos)) |
paul@109 | 886 | |
paul@137 | 887 | def _doCallFunc(self, instruction, target=None): |
paul@97 | 888 | |
paul@97 | 889 | "Make the invocation." |
paul@57 | 890 | |
paul@137 | 891 | if isinstance(target, Class): |
paul@138 | 892 | self.new_op(LoadConst(target.get_init_method())) |
paul@137 | 893 | else: |
paul@137 | 894 | self.new_op(instruction) |
paul@137 | 895 | self.new_op(LoadCallable()) |
paul@97 | 896 | self.new_op(JumpWithFrame()) |
paul@57 | 897 | |
paul@129 | 898 | def _endCallFuncArgs(self, nargs): |
paul@129 | 899 | |
paul@129 | 900 | "Set the frame size." |
paul@129 | 901 | |
paul@129 | 902 | self.frame_makers[-1].attr = nargs |
paul@129 | 903 | self.frame_makers.pop() |
paul@129 | 904 | |
paul@137 | 905 | def _endCallFunc(self, instruction=None, target=None, load_result=1): |
paul@97 | 906 | |
paul@97 | 907 | "Finish the invocation and tidy up afterwards." |
paul@21 | 908 | |
paul@137 | 909 | if isinstance(target, Class): |
paul@137 | 910 | self.new_op(LoadName(target.get_init_method().all_locals()["self"])) # load the context in the invocation frame |
paul@137 | 911 | self.new_op(StoreResult()) |
paul@21 | 912 | self.new_op(DropFrame()) |
paul@113 | 913 | if load_result: |
paul@113 | 914 | self.new_op(LoadResult()) |
paul@97 | 915 | |
paul@97 | 916 | # Discard any temporary storage instructions. |
paul@97 | 917 | |
paul@102 | 918 | if instruction is not None: |
paul@102 | 919 | self.discard_temp(instruction) |
paul@17 | 920 | |
paul@109 | 921 | def _generateFunctionDefaults(self, function): |
paul@109 | 922 | |
paul@109 | 923 | """ |
paul@109 | 924 | Generate the default initialisation code for 'function', returning |
paul@109 | 925 | a temporary storage reference if a dynamic object was created for the |
paul@109 | 926 | function. |
paul@109 | 927 | """ |
paul@109 | 928 | |
paul@109 | 929 | attr_to_default = zip(function.default_attrs, function.defaults) |
paul@109 | 930 | if not attr_to_default: |
paul@109 | 931 | return None |
paul@109 | 932 | |
paul@109 | 933 | # Where a lambda is involved, construct a dynamic object to hold the |
paul@109 | 934 | # defaults. |
paul@109 | 935 | |
paul@109 | 936 | dynamic = function.name is None |
paul@109 | 937 | |
paul@109 | 938 | if dynamic: |
paul@138 | 939 | self.make_object(self.get_builtin_class("function", function), len(attr_to_default)) |
paul@109 | 940 | temp = self.get_temp() |
paul@109 | 941 | |
paul@109 | 942 | for attr, default in attr_to_default: |
paul@109 | 943 | self.dispatch(default) |
paul@109 | 944 | |
paul@109 | 945 | self.record_value() |
paul@109 | 946 | if dynamic: |
paul@109 | 947 | self.new_op(temp) |
paul@109 | 948 | self.new_op(StoreAttr(attr)) |
paul@109 | 949 | else: |
paul@109 | 950 | self.new_op(StoreAddress(attr)) |
paul@109 | 951 | self.set_source() |
paul@109 | 952 | self.discard_value() |
paul@109 | 953 | |
paul@109 | 954 | if dynamic: |
paul@109 | 955 | return temp |
paul@109 | 956 | else: |
paul@109 | 957 | return None |
paul@109 | 958 | |
paul@56 | 959 | def _visitName(self, node, classes): |
paul@56 | 960 | |
paul@56 | 961 | """ |
paul@56 | 962 | Visit the name-related 'node', generating instructions based on the |
paul@56 | 963 | given 'classes'. |
paul@56 | 964 | """ |
paul@56 | 965 | |
paul@56 | 966 | name = node.name |
paul@56 | 967 | scope = self.get_scope(name) |
paul@56 | 968 | #print self.module.name, node.lineno, name, scope |
paul@56 | 969 | self._generateName(name, scope, classes, node) |
paul@56 | 970 | |
paul@56 | 971 | def _generateName(self, name, scope, classes, node): |
paul@56 | 972 | |
paul@56 | 973 | """ |
paul@56 | 974 | Generate code for the access to 'name' in 'scope' using the given |
paul@56 | 975 | 'classes', and using the given 'node' as the source of the access. |
paul@56 | 976 | """ |
paul@56 | 977 | |
paul@72 | 978 | NameInstruction, AddressInstruction = classes |
paul@56 | 979 | |
paul@56 | 980 | if scope == "local": |
paul@56 | 981 | unit = self.unit |
paul@83 | 982 | if isinstance(unit, Function): |
paul@56 | 983 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@83 | 984 | elif isinstance(unit, Class): |
paul@72 | 985 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@83 | 986 | elif isinstance(unit, Module): |
paul@72 | 987 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@56 | 988 | else: |
paul@61 | 989 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@56 | 990 | |
paul@56 | 991 | elif scope == "global": |
paul@56 | 992 | globals = self.module.module_attributes() |
paul@56 | 993 | if globals.has_key(name): |
paul@72 | 994 | self.new_op(AddressInstruction(globals[name])) |
paul@56 | 995 | else: |
paul@61 | 996 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@56 | 997 | |
paul@56 | 998 | else: |
paul@101 | 999 | self.new_op(AddressInstruction(self.get_builtin(name, node))) |
paul@56 | 1000 | |
paul@172 | 1001 | def _visitUnary(self, node): |
paul@85 | 1002 | |
paul@85 | 1003 | """ |
paul@85 | 1004 | _t = node.expr |
paul@85 | 1005 | try: |
paul@85 | 1006 | _result = _t.__pos__() |
paul@85 | 1007 | except AttributeError: |
paul@85 | 1008 | raise TypeError |
paul@85 | 1009 | """ |
paul@85 | 1010 | |
paul@172 | 1011 | method = unary_methods[node.__class__.__name__] |
paul@172 | 1012 | |
paul@163 | 1013 | type_error_block = self.new_block() |
paul@163 | 1014 | end_block = self.new_block() |
paul@85 | 1015 | |
paul@85 | 1016 | # Evaluate and store the operand in temporary storage. |
paul@85 | 1017 | |
paul@85 | 1018 | self.dispatch(node.expr) |
paul@140 | 1019 | temp = self.optimiser.optimise_temp_storage() |
paul@85 | 1020 | |
paul@102 | 1021 | self.new_op(temp) |
paul@85 | 1022 | |
paul@85 | 1023 | # Get the method on temp. |
paul@85 | 1024 | |
paul@104 | 1025 | self._generateAttr(node, method, self.attribute_load_instructions) |
paul@140 | 1026 | temp_method = self.optimiser.optimise_temp_storage() |
paul@85 | 1027 | |
paul@163 | 1028 | self._handleAttributeError(node, type_error_block) |
paul@114 | 1029 | |
paul@85 | 1030 | # Add arguments. |
paul@86 | 1031 | # NOTE: No support for defaults. |
paul@85 | 1032 | |
paul@161 | 1033 | self._startCallFunc() |
paul@102 | 1034 | self.new_op(temp) # Explicit context as first argument. |
paul@113 | 1035 | self.new_op(StoreFrame(0)) |
paul@129 | 1036 | self._endCallFuncArgs(1) |
paul@97 | 1037 | self._doCallFunc(temp_method) |
paul@101 | 1038 | self._endCallFunc(temp_method) |
paul@163 | 1039 | self.new_op(Jump(end_block)) |
paul@85 | 1040 | |
paul@151 | 1041 | # Store the result. |
paul@151 | 1042 | |
paul@151 | 1043 | temp_out = self.get_temp() |
paul@85 | 1044 | |
paul@85 | 1045 | # Raise a TypeError. |
paul@85 | 1046 | |
paul@163 | 1047 | self.set_block(type_error_block) |
paul@101 | 1048 | self.load_builtin("TypeError", node) |
paul@105 | 1049 | self.new_op(StoreException()) |
paul@85 | 1050 | self.new_op(RaiseException()) |
paul@85 | 1051 | |
paul@163 | 1052 | self.set_block(end_block) |
paul@85 | 1053 | |
paul@151 | 1054 | # Produce the result. |
paul@151 | 1055 | |
paul@151 | 1056 | self.new_op(temp_out) |
paul@151 | 1057 | |
paul@85 | 1058 | # Compilation duties... |
paul@85 | 1059 | |
paul@85 | 1060 | self.discard_temp(temp) |
paul@152 | 1061 | self.discard_temp(temp_out) |
paul@85 | 1062 | |
paul@172 | 1063 | def _visitBinary(self, node): |
paul@56 | 1064 | |
paul@56 | 1065 | """ |
paul@56 | 1066 | _t1 = node.left |
paul@56 | 1067 | _t2 = node.right |
paul@56 | 1068 | try: |
paul@76 | 1069 | _result = _t1.__add__(_t2) |
paul@76 | 1070 | if _result is NotImplemented: |
paul@76 | 1071 | raise AttributeError |
paul@56 | 1072 | except AttributeError: |
paul@76 | 1073 | try: |
paul@76 | 1074 | _result = _t2.__radd__(_t1) |
paul@76 | 1075 | if _result is NotImplemented: |
paul@76 | 1076 | raise AttributeError |
paul@76 | 1077 | except AttributeError: |
paul@76 | 1078 | raise TypeError |
paul@56 | 1079 | """ |
paul@56 | 1080 | |
paul@172 | 1081 | left_method, right_method = binary_methods[node.__class__.__name__] |
paul@172 | 1082 | |
paul@78 | 1083 | # Evaluate and store the left operand in temporary storage. |
paul@73 | 1084 | |
paul@57 | 1085 | self.dispatch(node.left) |
paul@140 | 1086 | temp1 = self.optimiser.optimise_temp_storage() |
paul@74 | 1087 | |
paul@78 | 1088 | # Evaluate and store the right operand in temporary storage. |
paul@74 | 1089 | |
paul@57 | 1090 | self.dispatch(node.right) |
paul@140 | 1091 | temp2 = self.optimiser.optimise_temp_storage() |
paul@57 | 1092 | |
paul@151 | 1093 | temp_out = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@151 | 1094 | |
paul@151 | 1095 | # Produce the result. |
paul@151 | 1096 | |
paul@151 | 1097 | self.new_op(temp_out) |
paul@113 | 1098 | |
paul@112 | 1099 | # Compilation duties... |
paul@112 | 1100 | |
paul@112 | 1101 | self.discard_temp(temp1) |
paul@112 | 1102 | self.discard_temp(temp2) |
paul@152 | 1103 | self.discard_temp(temp_out) |
paul@112 | 1104 | |
paul@113 | 1105 | def _generateBinary(self, node, temp1, temp2, left_method, right_method): |
paul@112 | 1106 | |
paul@114 | 1107 | """ |
paul@114 | 1108 | For the given 'node', generate the binary operator pattern for the |
paul@114 | 1109 | operands 'temp1' and 'temp2', employing 'left_method' and 'right_method' |
paul@114 | 1110 | as defined for binary operators, but also used in comparisons (for which |
paul@114 | 1111 | this method is provided). |
paul@161 | 1112 | |
paul@161 | 1113 | A temporary storage reference is returned from this method. |
paul@114 | 1114 | """ |
paul@114 | 1115 | |
paul@163 | 1116 | right_block = self.new_block() |
paul@163 | 1117 | type_error_block = self.new_block() |
paul@163 | 1118 | end_block = self.new_block() |
paul@112 | 1119 | |
paul@57 | 1120 | # Left method. |
paul@57 | 1121 | |
paul@163 | 1122 | temp_out = self._generateOpMethod(node, temp1, temp2, left_method, right_block, end_block) |
paul@151 | 1123 | self.discard_temp(temp_out) # NOTE: Will re-use the same storage. |
paul@112 | 1124 | |
paul@112 | 1125 | # Right method. |
paul@112 | 1126 | |
paul@163 | 1127 | self.set_block(right_block) |
paul@163 | 1128 | temp_out = self._generateOpMethod(node, temp2, temp1, right_method, type_error_block, end_block) |
paul@112 | 1129 | |
paul@112 | 1130 | # Raise a TypeError. |
paul@112 | 1131 | |
paul@163 | 1132 | self.set_block(type_error_block) |
paul@112 | 1133 | self.load_builtin("TypeError", node) |
paul@112 | 1134 | self.new_op(StoreException()) |
paul@112 | 1135 | self.new_op(RaiseException()) |
paul@112 | 1136 | |
paul@163 | 1137 | self.set_block(end_block) |
paul@151 | 1138 | return temp_out |
paul@112 | 1139 | |
paul@163 | 1140 | def _generateOpMethod(self, node, temp1, temp2, method_name, next_method_block, end_block): |
paul@112 | 1141 | |
paul@114 | 1142 | """ |
paul@114 | 1143 | For the given 'node', generate the operator method invocation using the |
paul@114 | 1144 | operands 'temp1' and 'temp2', employing the given 'method_name', and |
paul@163 | 1145 | jumping appropriately to 'next_method_block' where a NotImplemented |
paul@163 | 1146 | result is returned, or to 'end_block' if the method call was successful. |
paul@161 | 1147 | |
paul@161 | 1148 | A temporary storage reference is returned from this method. |
paul@114 | 1149 | """ |
paul@114 | 1150 | |
paul@163 | 1151 | end_attempt_block = self.new_block() |
paul@112 | 1152 | |
paul@102 | 1153 | self.new_op(temp1) |
paul@76 | 1154 | |
paul@112 | 1155 | # Get method on temp1. |
paul@76 | 1156 | |
paul@112 | 1157 | self._generateAttr(node, method_name, self.attribute_load_instructions) |
paul@140 | 1158 | temp_method = self.optimiser.optimise_temp_storage() |
paul@76 | 1159 | |
paul@163 | 1160 | self._handleAttributeError(node, end_attempt_block) |
paul@114 | 1161 | |
paul@76 | 1162 | # Add arguments. |
paul@86 | 1163 | # NOTE: No support for defaults. |
paul@76 | 1164 | |
paul@131 | 1165 | self._startCallFunc() |
paul@114 | 1166 | self.new_op(temp1) |
paul@113 | 1167 | self.new_op(StoreFrame(0)) |
paul@102 | 1168 | self.new_op(temp2) |
paul@113 | 1169 | self.new_op(StoreFrame(1)) |
paul@129 | 1170 | self._endCallFuncArgs(2) |
paul@97 | 1171 | self._doCallFunc(temp_method) |
paul@101 | 1172 | self._endCallFunc(temp_method) |
paul@57 | 1173 | |
paul@151 | 1174 | # Store the result. |
paul@151 | 1175 | |
paul@151 | 1176 | temp_out = self.get_temp() |
paul@151 | 1177 | |
paul@76 | 1178 | # Test for NotImplemented. |
paul@76 | 1179 | # Don't actually raise an exception. |
paul@76 | 1180 | |
paul@168 | 1181 | self.new_op(TestIdentityAddress(self.importer.get_predefined_constant("NotImplemented"))) |
paul@163 | 1182 | self.new_op(JumpIfTrue(next_method_block)) |
paul@163 | 1183 | self.new_op(Jump(end_block)) |
paul@76 | 1184 | |
paul@112 | 1185 | # End method attempt. |
paul@76 | 1186 | |
paul@163 | 1187 | self.set_block(end_attempt_block) |
paul@151 | 1188 | return temp_out |
paul@57 | 1189 | |
paul@163 | 1190 | def _handleAttributeError(self, node, end_call_block): |
paul@114 | 1191 | |
paul@114 | 1192 | """ |
paul@114 | 1193 | Add exception handling to the method acquisition instructions where the |
paul@114 | 1194 | attribute access cannot be resolved at compile-time. |
paul@114 | 1195 | """ |
paul@114 | 1196 | |
paul@140 | 1197 | if not self.optimiser.optimise_known_target(): |
paul@114 | 1198 | self.load_builtin("AttributeError", node) |
paul@114 | 1199 | self.new_op(CheckException()) |
paul@163 | 1200 | self.new_op(JumpIfTrue(end_call_block)) |
paul@114 | 1201 | |
paul@114 | 1202 | def _generateSequence(self, sequence_type, node): |
paul@114 | 1203 | |
paul@114 | 1204 | "Make a sequence of 'sequence_type' for the given program 'node'." |
paul@114 | 1205 | |
paul@143 | 1206 | self.make_object(self.get_builtin_class(sequence_type, node), len(node.nodes)) |
paul@112 | 1207 | temp = self.get_temp() |
paul@76 | 1208 | |
paul@112 | 1209 | for i, n in enumerate(node.nodes): |
paul@112 | 1210 | self.dispatch(n) |
paul@112 | 1211 | self.record_value() |
paul@112 | 1212 | self.new_op(temp) |
paul@112 | 1213 | self.new_op(StoreAttr(Attr(i, None, None, None))) |
paul@112 | 1214 | self.set_source() |
paul@112 | 1215 | self.discard_value() |
paul@85 | 1216 | |
paul@112 | 1217 | self.new_op(temp) |
paul@112 | 1218 | self.discard_temp(temp) |
paul@73 | 1219 | |
paul@130 | 1220 | def _generateTestBoolean(self, node, temp): |
paul@129 | 1221 | |
paul@129 | 1222 | """ |
paul@129 | 1223 | Generate a test of the boolean status of the current value for the given |
paul@129 | 1224 | program 'node'. |
paul@129 | 1225 | """ |
paul@129 | 1226 | |
paul@129 | 1227 | # Get method on temp. |
paul@129 | 1228 | # NOTE: Using __bool__ instead of __nonzero__. |
paul@129 | 1229 | |
paul@129 | 1230 | self._generateAttr(node, "__bool__", self.attribute_load_instructions) |
paul@140 | 1231 | temp_method = self.optimiser.optimise_temp_storage() |
paul@129 | 1232 | |
paul@129 | 1233 | self._startCallFunc() |
paul@129 | 1234 | self.new_op(temp) |
paul@129 | 1235 | self.new_op(StoreFrame(0)) |
paul@129 | 1236 | self._endCallFuncArgs(1) |
paul@129 | 1237 | self._doCallFunc(temp_method) |
paul@129 | 1238 | self._endCallFunc(temp_method) |
paul@129 | 1239 | |
paul@130 | 1240 | self.discard_temp(temp_method) |
paul@130 | 1241 | |
paul@129 | 1242 | # Convert result to boolean (a StoreBoolean operation). |
paul@129 | 1243 | |
paul@129 | 1244 | self.new_op(TestIdentityAddress(self.get_builtin("True", node))) |
paul@129 | 1245 | |
paul@129 | 1246 | def _generateLoadBoolean(self, node): |
paul@129 | 1247 | |
paul@129 | 1248 | """ |
paul@129 | 1249 | Generate instructions to load the appropriate value given the current |
paul@129 | 1250 | boolean status. |
paul@129 | 1251 | """ |
paul@129 | 1252 | |
paul@163 | 1253 | true_block = self.new_block() |
paul@163 | 1254 | end_block = self.new_block() |
paul@163 | 1255 | |
paul@163 | 1256 | self.new_op(JumpIfTrue(true_block)) |
paul@129 | 1257 | self.load_builtin("False", node) |
paul@163 | 1258 | self.new_op(Jump(end_block)) |
paul@163 | 1259 | |
paul@163 | 1260 | self.set_block(true_block) |
paul@129 | 1261 | self.load_builtin("True", node) |
paul@129 | 1262 | |
paul@163 | 1263 | self.set_block(end_block) |
paul@129 | 1264 | |
paul@103 | 1265 | # Concrete visitor methods. |
paul@103 | 1266 | |
paul@160 | 1267 | # Binary operators. |
paul@160 | 1268 | |
paul@172 | 1269 | visitAdd = _visitBinary |
paul@172 | 1270 | visitBitand = _visitBinary |
paul@172 | 1271 | visitBitor = _visitBinary |
paul@172 | 1272 | visitBitxor = _visitBinary |
paul@172 | 1273 | visitDiv = _visitBinary |
paul@172 | 1274 | visitFloorDiv = _visitBinary |
paul@172 | 1275 | visitLeftShift = _visitBinary |
paul@172 | 1276 | visitMod = _visitBinary |
paul@172 | 1277 | visitMul = _visitBinary |
paul@172 | 1278 | visitPower = _visitBinary |
paul@172 | 1279 | visitRightShift = _visitBinary |
paul@172 | 1280 | visitSub = _visitBinary |
paul@160 | 1281 | |
paul@160 | 1282 | # Unary operators. |
paul@160 | 1283 | |
paul@172 | 1284 | visitInvert = _visitUnary |
paul@172 | 1285 | visitUnaryAdd = _visitUnary |
paul@172 | 1286 | visitUnarySub = _visitUnary |
paul@160 | 1287 | |
paul@160 | 1288 | # Logical operators. |
paul@160 | 1289 | |
paul@101 | 1290 | def visitAnd(self, node): |
paul@163 | 1291 | end_block = self.new_block() |
paul@130 | 1292 | temp_pos = self.reserve_temp() |
paul@130 | 1293 | temp = LoadTemp(temp_pos) |
paul@101 | 1294 | |
paul@101 | 1295 | for n in node.nodes[:-1]: |
paul@101 | 1296 | self.dispatch(n) |
paul@130 | 1297 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 1298 | |
paul@130 | 1299 | self._generateTestBoolean(n, temp) |
paul@163 | 1300 | self.new_op(JumpIfFalse(end_block)) |
paul@101 | 1301 | |
paul@101 | 1302 | self.dispatch(node.nodes[-1]) |
paul@130 | 1303 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 1304 | |
paul@163 | 1305 | self.set_block(end_block) |
paul@130 | 1306 | |
paul@130 | 1307 | self.new_op(temp) |
paul@130 | 1308 | self.discard_temp(temp) |
paul@56 | 1309 | |
paul@160 | 1310 | def visitNot(self, node): |
paul@160 | 1311 | self.dispatch(node.expr) |
paul@160 | 1312 | |
paul@160 | 1313 | temp = self.optimiser.optimise_temp_storage() |
paul@160 | 1314 | self._generateTestBoolean(node.expr, temp) |
paul@160 | 1315 | self.discard_temp(temp) |
paul@160 | 1316 | |
paul@160 | 1317 | self.new_op(InvertBoolean()) |
paul@160 | 1318 | self._generateLoadBoolean(node) |
paul@160 | 1319 | |
paul@160 | 1320 | def visitOr(self, node): |
paul@163 | 1321 | end_block = self.new_block() |
paul@160 | 1322 | temp_pos = self.reserve_temp() |
paul@160 | 1323 | temp = LoadTemp(temp_pos) |
paul@160 | 1324 | |
paul@160 | 1325 | for n in node.nodes[:-1]: |
paul@160 | 1326 | self.dispatch(n) |
paul@160 | 1327 | self.new_op(StoreTemp(temp_pos)) |
paul@160 | 1328 | |
paul@160 | 1329 | self._generateTestBoolean(n, temp) |
paul@163 | 1330 | self.new_op(JumpIfTrue(end_block)) |
paul@160 | 1331 | |
paul@160 | 1332 | self.dispatch(node.nodes[-1]) |
paul@160 | 1333 | self.new_op(StoreTemp(temp_pos)) |
paul@160 | 1334 | |
paul@163 | 1335 | self.set_block(end_block) |
paul@160 | 1336 | |
paul@160 | 1337 | self.new_op(temp) |
paul@160 | 1338 | self.discard_temp(temp) |
paul@160 | 1339 | |
paul@160 | 1340 | # Comparisons. |
paul@160 | 1341 | |
paul@160 | 1342 | def visitCompare(self, node): |
paul@160 | 1343 | |
paul@160 | 1344 | """ |
paul@160 | 1345 | _t1 = node.expr |
paul@160 | 1346 | _t1 op1 _t2 and _t2 op2 _t3 and ... |
paul@160 | 1347 | """ |
paul@160 | 1348 | |
paul@163 | 1349 | end_block = self.new_block() |
paul@160 | 1350 | |
paul@160 | 1351 | self.dispatch(node.expr) |
paul@160 | 1352 | temp2 = self.optimiser.optimise_temp_storage() |
paul@160 | 1353 | |
paul@172 | 1354 | # NOTE: Replicated by some code in micropython.inspect.visitCompare. |
paul@172 | 1355 | |
paul@160 | 1356 | last_op = node.ops[-1] |
paul@160 | 1357 | |
paul@160 | 1358 | for op in node.ops: |
paul@160 | 1359 | op_name, next_node = op |
paul@172 | 1360 | methods = comparison_methods[op_name] |
paul@160 | 1361 | |
paul@161 | 1362 | # Propagate the arguments as we traverse the construct. |
paul@161 | 1363 | |
paul@160 | 1364 | temp1 = temp2 |
paul@160 | 1365 | self.dispatch(next_node) |
paul@160 | 1366 | temp2 = self.optimiser.optimise_temp_storage() |
paul@160 | 1367 | |
paul@160 | 1368 | # Use the appropriate mechanism, setting the boolean status for the |
paul@160 | 1369 | # comparison. |
paul@160 | 1370 | |
paul@160 | 1371 | if methods is not None: |
paul@160 | 1372 | left_method, right_method = methods |
paul@160 | 1373 | |
paul@160 | 1374 | # Generate method call using evaluated argument and next node. |
paul@160 | 1375 | |
paul@160 | 1376 | temp_result = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@160 | 1377 | self.new_op(temp_result) |
paul@160 | 1378 | self._generateTestBoolean(node, temp_result) |
paul@160 | 1379 | self.discard_temp(temp_result) |
paul@160 | 1380 | |
paul@160 | 1381 | else: |
paul@160 | 1382 | # Deal with the special operators. |
paul@160 | 1383 | |
paul@160 | 1384 | if op_name.startswith("is"): |
paul@160 | 1385 | self.new_op(temp1) |
paul@160 | 1386 | self.record_value() |
paul@160 | 1387 | self.new_op(temp2) |
paul@160 | 1388 | self.new_op(TestIdentity()) |
paul@160 | 1389 | self.set_source() |
paul@160 | 1390 | self.discard_value() |
paul@160 | 1391 | |
paul@160 | 1392 | elif op_name.endswith("in"): |
paul@160 | 1393 | self.new_op(temp2) |
paul@160 | 1394 | |
paul@160 | 1395 | # Get method on temp2. |
paul@160 | 1396 | |
paul@160 | 1397 | self._generateAttr(node, "__contains__", self.attribute_load_instructions) |
paul@160 | 1398 | temp_method = self.optimiser.optimise_temp_storage() |
paul@160 | 1399 | |
paul@160 | 1400 | # Add arguments. |
paul@160 | 1401 | # NOTE: No support for defaults. |
paul@160 | 1402 | |
paul@161 | 1403 | self._startCallFunc() |
paul@160 | 1404 | self.new_op(temp2) |
paul@160 | 1405 | self.new_op(StoreFrame(0)) |
paul@160 | 1406 | self.new_op(temp1) |
paul@160 | 1407 | self.new_op(StoreFrame(1)) |
paul@160 | 1408 | self._endCallFuncArgs(2) |
paul@160 | 1409 | self._doCallFunc(temp_method) |
paul@160 | 1410 | self._endCallFunc(temp_method) |
paul@160 | 1411 | |
paul@160 | 1412 | temp_result = self.get_temp() |
paul@160 | 1413 | self._generateTestBoolean(node, temp_result) |
paul@160 | 1414 | self.discard_temp(temp_result) |
paul@160 | 1415 | |
paul@160 | 1416 | if op_name.find("not") != -1: |
paul@160 | 1417 | self.new_op(InvertBoolean()) |
paul@160 | 1418 | |
paul@163 | 1419 | # Test the result and jump to the end block if false. |
paul@160 | 1420 | |
paul@160 | 1421 | if op is not last_op: |
paul@163 | 1422 | self.new_op(JumpIfFalse(end_block)) |
paul@160 | 1423 | |
paul@160 | 1424 | # Compilation duties... |
paul@160 | 1425 | |
paul@160 | 1426 | self.discard_temp(temp1) |
paul@160 | 1427 | |
paul@160 | 1428 | self.discard_temp(temp2) |
paul@161 | 1429 | |
paul@161 | 1430 | # With the status set above, produce a boolean result. |
paul@161 | 1431 | |
paul@163 | 1432 | self.set_block(end_block) |
paul@161 | 1433 | |
paul@160 | 1434 | # Yield the appropriate value. |
paul@160 | 1435 | |
paul@160 | 1436 | self._generateLoadBoolean(node) |
paul@160 | 1437 | |
paul@160 | 1438 | # Expressions. |
paul@160 | 1439 | |
paul@160 | 1440 | def visitBackquote(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Backquote") |
paul@160 | 1441 | |
paul@160 | 1442 | def visitCallFunc(self, node): |
paul@160 | 1443 | |
paul@160 | 1444 | """ |
paul@160 | 1445 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@160 | 1446 | the correct location, then invoke the function. |
paul@160 | 1447 | """ |
paul@160 | 1448 | |
paul@160 | 1449 | # Mark the frame, evaluate the target, generate the call. |
paul@160 | 1450 | |
paul@160 | 1451 | self._startCallFunc() |
paul@160 | 1452 | self.dispatch(node.node) |
paul@160 | 1453 | temp, target = self._generateCallFunc(node.args, node) |
paul@160 | 1454 | self._doCallFunc(temp, target) |
paul@160 | 1455 | self._endCallFunc(temp, target) |
paul@160 | 1456 | |
paul@160 | 1457 | def visitConst(self, node): |
paul@168 | 1458 | const = self.importer.get_constant(node.value) |
paul@160 | 1459 | self.new_op(LoadConst(const)) |
paul@160 | 1460 | |
paul@160 | 1461 | def visitDict(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Dict") |
paul@160 | 1462 | |
paul@160 | 1463 | def visitEllipsis(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Ellipsis") |
paul@160 | 1464 | |
paul@160 | 1465 | def visitExec(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Exec") |
paul@160 | 1466 | |
paul@160 | 1467 | def visitExpression(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Expression") |
paul@160 | 1468 | |
paul@160 | 1469 | def visitGenExpr(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExpr") |
paul@160 | 1470 | |
paul@160 | 1471 | def visitGenExprFor(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExprFor") |
paul@160 | 1472 | |
paul@160 | 1473 | def visitGenExprIf(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExprIf") |
paul@160 | 1474 | |
paul@160 | 1475 | def visitGenExprInner(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExprInner") |
paul@160 | 1476 | |
paul@160 | 1477 | def visitGetattr(self, node): |
paul@160 | 1478 | self._visitAttr(node, self.attribute_load_instructions) |
paul@160 | 1479 | |
paul@160 | 1480 | def visitList(self, node): |
paul@160 | 1481 | self._generateSequence("list", node) |
paul@160 | 1482 | |
paul@160 | 1483 | def visitListComp(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "ListComp") |
paul@160 | 1484 | |
paul@160 | 1485 | def visitListCompFor(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "ListCompFor") |
paul@160 | 1486 | |
paul@160 | 1487 | def visitListCompIf(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "ListCompIf") |
paul@160 | 1488 | |
paul@160 | 1489 | def visitName(self, node): |
paul@168 | 1490 | if self.importer.predefined_constants.has_key(node.name): |
paul@168 | 1491 | const = self.importer.get_predefined_constant(node.name) |
paul@160 | 1492 | self.new_op(LoadConst(const)) |
paul@160 | 1493 | else: |
paul@160 | 1494 | self._visitName(node, self.name_load_instructions) |
paul@160 | 1495 | |
paul@160 | 1496 | def visitSlice(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Slice") |
paul@160 | 1497 | |
paul@160 | 1498 | def visitSubscript(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Subscript") |
paul@160 | 1499 | |
paul@160 | 1500 | def visitTuple(self, node): |
paul@160 | 1501 | self._generateSequence("tuple", node) |
paul@160 | 1502 | |
paul@160 | 1503 | # Definitions. |
paul@56 | 1504 | |
paul@56 | 1505 | def visitAssign(self, node): |
paul@159 | 1506 | |
paul@159 | 1507 | """ |
paul@159 | 1508 | Evaluate the expression from the given 'node' and assign it to the |
paul@159 | 1509 | associated recipients. |
paul@159 | 1510 | """ |
paul@159 | 1511 | |
paul@56 | 1512 | self.dispatch(node.expr) |
paul@146 | 1513 | |
paul@146 | 1514 | # Record the value and then dispatch to the assignment targets. |
paul@146 | 1515 | |
paul@159 | 1516 | self.record_value(self.has_immediate_usage(node.nodes)) |
paul@101 | 1517 | |
paul@56 | 1518 | for n in node.nodes: |
paul@143 | 1519 | self.dispatch(n) |
paul@56 | 1520 | |
paul@104 | 1521 | self.discard_value() |
paul@101 | 1522 | |
paul@143 | 1523 | def visitAssAttr(self, node): |
paul@159 | 1524 | |
paul@159 | 1525 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 1526 | |
paul@98 | 1527 | self._visitAttr(node, self.attribute_store_instructions) |
paul@104 | 1528 | self.set_source() |
paul@56 | 1529 | |
paul@143 | 1530 | def visitAssList(self, node): |
paul@159 | 1531 | |
paul@159 | 1532 | """ |
paul@159 | 1533 | Assign items from the assignment expression to each of the recipients |
paul@159 | 1534 | found within the given 'node'. |
paul@159 | 1535 | """ |
paul@159 | 1536 | |
paul@139 | 1537 | for i, n in enumerate(node.nodes): |
paul@139 | 1538 | self._startCallFunc() |
paul@139 | 1539 | self.new_op(self.expr_temp[-1]) |
paul@139 | 1540 | self._generateAttr(node, "__getitem__", self.attribute_load_instructions) |
paul@139 | 1541 | temp, target = self._generateCallFunc([compiler.ast.Const(i)], node) |
paul@139 | 1542 | self._doCallFunc(temp, target) |
paul@139 | 1543 | self._endCallFunc(temp, target) |
paul@139 | 1544 | |
paul@139 | 1545 | # Provide a different source value. |
paul@159 | 1546 | # NOTE: Permitting immediate usage given that neither name nor |
paul@159 | 1547 | # NOTE: attribute accesses should involve a function call |
paul@159 | 1548 | # NOTE: overwriting the above result. |
paul@159 | 1549 | |
paul@159 | 1550 | self.record_value(self.is_immediate_user(n)) |
paul@143 | 1551 | self.dispatch(n) |
paul@139 | 1552 | self.discard_value() |
paul@139 | 1553 | |
paul@143 | 1554 | def visitAssName(self, node): |
paul@159 | 1555 | |
paul@159 | 1556 | "Assign the assignment expression to the recipient 'node'." |
paul@159 | 1557 | |
paul@106 | 1558 | self._visitName(node, self.name_store_instructions) |
paul@143 | 1559 | self.set_source() |
paul@56 | 1560 | |
paul@56 | 1561 | visitAssTuple = visitAssList |
paul@56 | 1562 | |
paul@151 | 1563 | def visitAugAssign(self, node): |
paul@163 | 1564 | use_binary_block = self.new_block() |
paul@163 | 1565 | end_block = self.new_block() |
paul@151 | 1566 | |
paul@151 | 1567 | # Evaluate the expression. |
paul@151 | 1568 | |
paul@151 | 1569 | self.dispatch(node.expr) |
paul@151 | 1570 | temp2 = self.optimiser.optimise_temp_storage() |
paul@151 | 1571 | |
paul@151 | 1572 | # Evaluate the target. |
paul@151 | 1573 | |
paul@151 | 1574 | self.dispatch(node.node) |
paul@151 | 1575 | temp1 = self.optimiser.optimise_temp_storage() |
paul@151 | 1576 | |
paul@151 | 1577 | # Find the augmented assignment method and attempt to use it. |
paul@151 | 1578 | |
paul@172 | 1579 | aug_method, (left_method, right_method) = augassign_methods[node.op] |
paul@163 | 1580 | temp_out = self._generateOpMethod(node, temp1, temp2, aug_method, use_binary_block, end_block) |
paul@151 | 1581 | self.discard_temp(temp_out) # NOTE: Will re-use the same storage. |
paul@151 | 1582 | |
paul@151 | 1583 | # Where no such method exists, use the binary operator methods. |
paul@151 | 1584 | |
paul@163 | 1585 | self.set_block(use_binary_block) |
paul@151 | 1586 | temp_out = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@151 | 1587 | |
paul@151 | 1588 | # Assign the result to the name. |
paul@151 | 1589 | |
paul@163 | 1590 | self.set_block(end_block) |
paul@151 | 1591 | self.new_op(temp_out) |
paul@151 | 1592 | self.record_value(1) |
paul@151 | 1593 | |
paul@151 | 1594 | if isinstance(node.node, compiler.ast.Name): |
paul@151 | 1595 | self.visitAssName(node.node) |
paul@151 | 1596 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@151 | 1597 | self.visitAssAttr(node.node) |
paul@151 | 1598 | else: |
paul@151 | 1599 | raise TranslationNotImplementedError(self.module.full_name(), node, "AugAssign(Slice or Subscript)") |
paul@151 | 1600 | |
paul@151 | 1601 | self.discard_value() |
paul@151 | 1602 | |
paul@151 | 1603 | # Compilation duties... |
paul@151 | 1604 | |
paul@151 | 1605 | self.discard_temp(temp1) |
paul@151 | 1606 | self.discard_temp(temp2) |
paul@56 | 1607 | |
paul@38 | 1608 | def visitClass(self, node): |
paul@167 | 1609 | if not node.unit.referenced: |
paul@167 | 1610 | return |
paul@90 | 1611 | |
paul@90 | 1612 | # Store the name. |
paul@90 | 1613 | |
paul@90 | 1614 | self.new_op(LoadConst(node.unit)) |
paul@104 | 1615 | self.record_value() |
paul@106 | 1616 | self._visitName(node, self.name_store_instructions) |
paul@104 | 1617 | self.set_source() |
paul@104 | 1618 | self.discard_value() |
paul@90 | 1619 | |
paul@90 | 1620 | # Visit the code. |
paul@90 | 1621 | |
paul@38 | 1622 | unit = self.unit |
paul@39 | 1623 | self.unit = node.unit |
paul@38 | 1624 | self.dispatch(node.code) |
paul@38 | 1625 | self.unit = unit |
paul@17 | 1626 | |
paul@160 | 1627 | def visitDecorators(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Decorators") |
paul@160 | 1628 | |
paul@160 | 1629 | def visitFrom(self, node): pass |
paul@160 | 1630 | |
paul@160 | 1631 | def visitFunction(self, node): |
paul@171 | 1632 | if not node.unit.referenced and (not node.unit.is_method() or not node.unit.parent.has_key(node.unit.name)): |
paul@167 | 1633 | return |
paul@160 | 1634 | |
paul@160 | 1635 | # Only store the name when visiting this node from outside. |
paul@160 | 1636 | |
paul@160 | 1637 | if self.unit is not node.unit: |
paul@160 | 1638 | self.new_op(LoadConst(node.unit)) |
paul@160 | 1639 | |
paul@160 | 1640 | self.record_value() |
paul@160 | 1641 | self._visitName(node, self.name_store_instructions) # AssName equivalent |
paul@160 | 1642 | self.set_source() |
paul@160 | 1643 | self.discard_value() |
paul@160 | 1644 | |
paul@160 | 1645 | self._generateFunctionDefaults(node.unit) |
paul@160 | 1646 | |
paul@165 | 1647 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 1648 | |
paul@160 | 1649 | else: |
paul@160 | 1650 | extend = ExtendFrame() |
paul@160 | 1651 | self.new_op(extend) |
paul@160 | 1652 | |
paul@160 | 1653 | self.dispatch(node.code) |
paul@160 | 1654 | if not isinstance(self.last_op(), Return): |
paul@160 | 1655 | self.dispatch(compiler.ast.Name("None")) |
paul@160 | 1656 | self.new_op(StoreResult()) |
paul@160 | 1657 | |
paul@160 | 1658 | self.new_op(Return()) |
paul@160 | 1659 | |
paul@160 | 1660 | self.set_frame_usage(node, extend) |
paul@160 | 1661 | |
paul@160 | 1662 | def visitGlobal(self, node): pass |
paul@160 | 1663 | |
paul@160 | 1664 | def visitImport(self, node): pass |
paul@160 | 1665 | |
paul@160 | 1666 | def visitKeyword(self, node): pass |
paul@160 | 1667 | |
paul@160 | 1668 | def visitLambda(self, node): |
paul@21 | 1669 | |
paul@21 | 1670 | """ |
paul@160 | 1671 | Lambda functions can be represented as globally defined functions |
paul@160 | 1672 | provided they do not define any default parameter values, since these |
paul@160 | 1673 | may defined in a non-global scope. |
paul@160 | 1674 | |
paul@160 | 1675 | Where defaults are defined, an object must be created and its content |
paul@160 | 1676 | defined: the callable member of the object's structure must be set to |
paul@160 | 1677 | the lambda function definition; each default must be attached to the |
paul@160 | 1678 | object as an attribute, as is the case with normal functions and |
paul@160 | 1679 | methods. |
paul@112 | 1680 | """ |
paul@112 | 1681 | |
paul@160 | 1682 | # Produce the reference to this function when visiting this node from |
paul@160 | 1683 | # outside. |
paul@160 | 1684 | |
paul@160 | 1685 | if self.unit is not node.unit: |
paul@160 | 1686 | temp = self._generateFunctionDefaults(node.unit) |
paul@160 | 1687 | self.new_op(LoadConst(node.unit)) |
paul@160 | 1688 | |
paul@160 | 1689 | # Populate the new object required for the function. |
paul@160 | 1690 | |
paul@160 | 1691 | if temp is not None: |
paul@160 | 1692 | self.new_op(LoadCallable()) |
paul@160 | 1693 | self.new_op(temp) |
paul@160 | 1694 | self.new_op(StoreCallable()) |
paul@160 | 1695 | |
paul@160 | 1696 | self.new_op(temp) |
paul@160 | 1697 | #self.discard_temp(temp) |
paul@160 | 1698 | |
paul@165 | 1699 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@160 | 1700 | |
paul@160 | 1701 | else: |
paul@160 | 1702 | self.dispatch(node.code) |
paul@160 | 1703 | self.new_op(StoreResult()) |
paul@160 | 1704 | self.new_op(Return()) |
paul@160 | 1705 | |
paul@160 | 1706 | def visitModule(self, node): |
paul@160 | 1707 | extend = ExtendFrame() |
paul@160 | 1708 | self.new_op(extend) |
paul@160 | 1709 | self.dispatch(node.node) |
paul@160 | 1710 | self.set_frame_usage(node, extend) |
paul@160 | 1711 | |
paul@160 | 1712 | # Statements. |
paul@160 | 1713 | |
paul@160 | 1714 | def visitStmt(self, node): |
paul@160 | 1715 | |
paul@160 | 1716 | "Process the collection of statements provided by 'node'." |
paul@160 | 1717 | |
paul@160 | 1718 | for n in node.nodes: |
paul@160 | 1719 | |
paul@160 | 1720 | # Process the statement. |
paul@160 | 1721 | |
paul@160 | 1722 | self.dispatch(n) |
paul@160 | 1723 | |
paul@160 | 1724 | # Discard temporary storage. |
paul@160 | 1725 | |
paul@160 | 1726 | if self.temp_positions: |
paul@160 | 1727 | #print "Had temp", self.temp_positions |
paul@160 | 1728 | self.temp_positions = set() |
paul@160 | 1729 | |
paul@160 | 1730 | # Prevent incorrect optimisation by resetting the optimiser after |
paul@160 | 1731 | # each statement. |
paul@160 | 1732 | |
paul@160 | 1733 | self.optimiser.reset() |
paul@160 | 1734 | |
paul@160 | 1735 | def visitAssert(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Assert") |
paul@160 | 1736 | |
paul@160 | 1737 | def visitBreak(self, node): |
paul@163 | 1738 | next_block, exit_block = self.get_loop_blocks() |
paul@163 | 1739 | self.new_op(Jump(exit_block)) |
paul@17 | 1740 | |
paul@41 | 1741 | def visitContinue(self, node): |
paul@163 | 1742 | next_block, exit_block = self.get_loop_blocks() |
paul@163 | 1743 | self.new_op(Jump(next_block)) |
paul@17 | 1744 | |
paul@18 | 1745 | def visitDiscard(self, node): |
paul@19 | 1746 | self.dispatch(node.expr) |
paul@140 | 1747 | self.optimiser.optimise_unused_results() |
paul@17 | 1748 | |
paul@44 | 1749 | def visitFor(self, node): |
paul@163 | 1750 | next_handler_block = self.new_block() |
paul@163 | 1751 | end_handler_block = self.new_block() |
paul@163 | 1752 | exit_block = self.new_block() |
paul@163 | 1753 | next_block = self.new_block() |
paul@163 | 1754 | else_block = self.new_block() |
paul@44 | 1755 | |
paul@44 | 1756 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 1757 | |
paul@45 | 1758 | self._startCallFunc() |
paul@44 | 1759 | self.dispatch(node.list) |
paul@104 | 1760 | self._generateAttr(node, "__iter__", self.attribute_load_instructions) |
paul@137 | 1761 | temp, target = self._generateCallFunc([], node) |
paul@137 | 1762 | self._doCallFunc(temp, target) |
paul@137 | 1763 | self._endCallFunc(temp, target) |
paul@57 | 1764 | |
paul@140 | 1765 | temp_iterator = self.optimiser.optimise_temp_storage() |
paul@98 | 1766 | |
paul@44 | 1767 | # In the loop... |
paul@44 | 1768 | |
paul@163 | 1769 | self.set_block(next_block) |
paul@44 | 1770 | |
paul@146 | 1771 | # Handle exceptions when calling "next"... |
paul@146 | 1772 | |
paul@163 | 1773 | self.new_op(PushHandler(next_handler_block)) |
paul@146 | 1774 | |
paul@44 | 1775 | # Use the iterator to get the next value. |
paul@44 | 1776 | |
paul@45 | 1777 | self._startCallFunc() |
paul@102 | 1778 | self.new_op(temp_iterator) |
paul@104 | 1779 | self._generateAttr(node, "next", self.attribute_load_instructions) |
paul@137 | 1780 | temp, target = self._generateCallFunc([], node) |
paul@137 | 1781 | self._doCallFunc(temp, target) |
paul@137 | 1782 | self._endCallFunc(temp, target) |
paul@44 | 1783 | |
paul@139 | 1784 | # Record the value to be assigned. |
paul@139 | 1785 | |
paul@139 | 1786 | self.record_value() |
paul@139 | 1787 | |
paul@146 | 1788 | # Skip the handler where the call was successful. |
paul@146 | 1789 | |
paul@163 | 1790 | self.new_op(Jump(end_handler_block)) |
paul@146 | 1791 | |
paul@146 | 1792 | # Enter the exception handler. |
paul@146 | 1793 | |
paul@163 | 1794 | self.set_block(next_handler_block) |
paul@146 | 1795 | self.new_op(PopHandler()) |
paul@146 | 1796 | |
paul@44 | 1797 | # Test for StopIteration. |
paul@44 | 1798 | |
paul@101 | 1799 | self.load_builtin("StopIteration", node) |
paul@57 | 1800 | self.new_op(CheckException()) |
paul@44 | 1801 | if node.else_ is not None: |
paul@163 | 1802 | self.new_op(JumpIfTrue(else_block)) |
paul@44 | 1803 | else: |
paul@163 | 1804 | self.new_op(JumpIfTrue(exit_block)) |
paul@44 | 1805 | |
paul@146 | 1806 | # Re-raise the exception otherwise. |
paul@146 | 1807 | |
paul@146 | 1808 | self.new_op(RaiseException()) |
paul@146 | 1809 | |
paul@146 | 1810 | # After the handler. |
paul@146 | 1811 | |
paul@163 | 1812 | self.set_block(end_handler_block) |
paul@146 | 1813 | |
paul@44 | 1814 | # Assign to the target. |
paul@44 | 1815 | |
paul@44 | 1816 | self.dispatch(node.assign) |
paul@139 | 1817 | self.discard_value() |
paul@44 | 1818 | |
paul@44 | 1819 | # Process the body with the current next and exit points. |
paul@44 | 1820 | |
paul@163 | 1821 | self.add_loop_blocks(next_block, exit_block) |
paul@44 | 1822 | self.dispatch(node.body) |
paul@163 | 1823 | self.drop_loop_blocks() |
paul@44 | 1824 | |
paul@44 | 1825 | # Repeat the loop. |
paul@44 | 1826 | |
paul@163 | 1827 | self.new_op(Jump(next_block)) |
paul@44 | 1828 | |
paul@44 | 1829 | # Produce the "else" section. |
paul@44 | 1830 | |
paul@44 | 1831 | if node.else_ is not None: |
paul@163 | 1832 | self.set_block(exit_block) |
paul@160 | 1833 | |
paul@44 | 1834 | self.dispatch(node.else_) |
paul@44 | 1835 | |
paul@98 | 1836 | # After the loop... |
paul@44 | 1837 | |
paul@163 | 1838 | self.set_block(exit_block) |
paul@17 | 1839 | |
paul@98 | 1840 | # Compilation duties... |
paul@98 | 1841 | |
paul@98 | 1842 | self.discard_temp(temp_iterator) |
paul@98 | 1843 | |
paul@17 | 1844 | def visitIf(self, node): |
paul@19 | 1845 | first = 1 |
paul@163 | 1846 | exit_block = self.new_block() |
paul@17 | 1847 | |
paul@108 | 1848 | clauses = node.tests + [(None, node.else_)] |
paul@108 | 1849 | last_clause = clauses[-1] |
paul@108 | 1850 | |
paul@108 | 1851 | for clause in clauses: |
paul@108 | 1852 | test, body = clause |
paul@19 | 1853 | if body is None: |
paul@19 | 1854 | break |
paul@19 | 1855 | if not first: |
paul@163 | 1856 | self.set_block(next_block) |
paul@19 | 1857 | if test is not None: |
paul@19 | 1858 | self.dispatch(test) |
paul@163 | 1859 | next_block = self.new_block() |
paul@163 | 1860 | self.new_op(JumpIfFalse(next_block)) |
paul@19 | 1861 | self.dispatch(body) |
paul@108 | 1862 | if clause is not last_clause: |
paul@163 | 1863 | self.new_op(Jump(exit_block)) |
paul@160 | 1864 | |
paul@19 | 1865 | first = 0 |
paul@17 | 1866 | |
paul@163 | 1867 | self.set_block(exit_block) |
paul@17 | 1868 | |
paul@19 | 1869 | def visitPass(self, node): pass |
paul@17 | 1870 | |
paul@100 | 1871 | def visitPrint(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Print") |
paul@17 | 1872 | |
paul@100 | 1873 | def visitPrintnl(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Printnl") |
paul@17 | 1874 | |
paul@97 | 1875 | def visitRaise(self, node): |
paul@97 | 1876 | # NOTE: expr1 only => instance provided |
paul@97 | 1877 | self.dispatch(node.expr1) |
paul@97 | 1878 | |
paul@97 | 1879 | if node.expr2 is not None: |
paul@140 | 1880 | temp = self.optimiser.optimise_temp_storage() |
paul@102 | 1881 | |
paul@97 | 1882 | self.dispatch(node.expr2) |
paul@140 | 1883 | temp_arg = self.optimiser.optimise_temp_storage() |
paul@102 | 1884 | |
paul@102 | 1885 | self._startCallFunc() |
paul@102 | 1886 | self.new_op(temp_arg) |
paul@102 | 1887 | self.new_op(StoreFrame(0)) |
paul@129 | 1888 | self._endCallFuncArgs(1) |
paul@102 | 1889 | self._doCallFunc(temp) |
paul@102 | 1890 | self._endCallFunc(temp) |
paul@102 | 1891 | |
paul@102 | 1892 | self.discard_temp(temp_arg) |
paul@97 | 1893 | |
paul@105 | 1894 | self.new_op(StoreException()) |
paul@97 | 1895 | self.new_op(RaiseException()) |
paul@17 | 1896 | |
paul@21 | 1897 | def visitReturn(self, node): |
paul@21 | 1898 | if node.value is not None: |
paul@21 | 1899 | self.dispatch(node.value) |
paul@68 | 1900 | else: |
paul@68 | 1901 | self.dispatch(compiler.ast.Name("None")) |
paul@104 | 1902 | |
paul@104 | 1903 | self.new_op(StoreResult()) |
paul@21 | 1904 | self.new_op(Return()) |
paul@17 | 1905 | |
paul@57 | 1906 | def visitTryExcept(self, node): |
paul@163 | 1907 | exit_block = self.new_block() |
paul@163 | 1908 | else_block = self.new_block() |
paul@163 | 1909 | handler_block = self.new_block() |
paul@163 | 1910 | |
paul@163 | 1911 | self.add_exception_blocks(handler_block, exit_block) |
paul@57 | 1912 | |
paul@68 | 1913 | # Try... |
paul@68 | 1914 | # Produce the code, then jump to the exit. |
paul@68 | 1915 | |
paul@163 | 1916 | self.new_op(PushHandler(handler_block)) |
paul@57 | 1917 | self.dispatch(node.body) |
paul@103 | 1918 | self.new_op(PopHandler()) |
paul@160 | 1919 | |
paul@160 | 1920 | if node.else_ is not None: |
paul@163 | 1921 | self.new_op(Jump(else_block)) |
paul@160 | 1922 | else: |
paul@163 | 1923 | self.new_op(Jump(exit_block)) |
paul@57 | 1924 | |
paul@68 | 1925 | # Start of handlers. |
paul@68 | 1926 | |
paul@163 | 1927 | self.set_block(handler_block) |
paul@160 | 1928 | |
paul@103 | 1929 | self.new_op(PopHandler()) |
paul@102 | 1930 | |
paul@57 | 1931 | for name, assignment, handler in node.handlers: |
paul@163 | 1932 | next_block = self.new_block() |
paul@57 | 1933 | |
paul@68 | 1934 | # Test the given exception against the current exception. |
paul@68 | 1935 | |
paul@57 | 1936 | if name is not None: |
paul@57 | 1937 | self.dispatch(name) |
paul@145 | 1938 | |
paul@57 | 1939 | self.new_op(CheckException()) |
paul@163 | 1940 | self.new_op(JumpIfFalse(next_block)) |
paul@57 | 1941 | |
paul@145 | 1942 | # Handle assignment to exception variable. |
paul@145 | 1943 | |
paul@145 | 1944 | if assignment is not None: |
paul@145 | 1945 | self.new_op(LoadException()) |
paul@145 | 1946 | |
paul@145 | 1947 | # Record the value to be assigned. |
paul@145 | 1948 | |
paul@145 | 1949 | self.record_value() |
paul@145 | 1950 | self.dispatch(assignment) |
paul@145 | 1951 | self.discard_value() |
paul@57 | 1952 | |
paul@68 | 1953 | # Produce the handler code, then jump to the exit. |
paul@68 | 1954 | |
paul@57 | 1955 | self.dispatch(handler) |
paul@163 | 1956 | self.new_op(Jump(exit_block)) |
paul@163 | 1957 | |
paul@163 | 1958 | self.set_block(next_block) |
paul@160 | 1959 | |
paul@57 | 1960 | # Unhandled exceptions. |
paul@57 | 1961 | |
paul@57 | 1962 | self.new_op(RaiseException()) |
paul@57 | 1963 | |
paul@57 | 1964 | # Optional else clause. |
paul@57 | 1965 | |
paul@57 | 1966 | if node.else_ is not None: |
paul@163 | 1967 | self.set_block(else_block) |
paul@160 | 1968 | |
paul@57 | 1969 | self.dispatch(node.else_) |
paul@57 | 1970 | |
paul@163 | 1971 | self.set_block(exit_block) |
paul@163 | 1972 | self.drop_exception_blocks() |
paul@17 | 1973 | |
paul@100 | 1974 | def visitTryFinally(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "TryFinally") |
paul@17 | 1975 | |
paul@41 | 1976 | def visitWhile(self, node): |
paul@163 | 1977 | exit_block = self.new_block() |
paul@163 | 1978 | next_block = self.new_block() |
paul@163 | 1979 | else_block = self.new_block() |
paul@163 | 1980 | |
paul@163 | 1981 | self.set_block(next_block) |
paul@44 | 1982 | self.dispatch(node.test) |
paul@44 | 1983 | if node.else_ is not None: |
paul@163 | 1984 | self.new_op(JumpIfFalse(else_block)) |
paul@44 | 1985 | else: |
paul@163 | 1986 | self.new_op(JumpIfFalse(exit_block)) |
paul@163 | 1987 | |
paul@163 | 1988 | self.add_loop_blocks(next_block, exit_block) |
paul@41 | 1989 | |
paul@41 | 1990 | self.dispatch(node.body) |
paul@163 | 1991 | self.new_op(Jump(next_block)) |
paul@41 | 1992 | |
paul@41 | 1993 | if node.else_ is not None: |
paul@163 | 1994 | self.set_block(else_block) |
paul@160 | 1995 | |
paul@41 | 1996 | self.dispatch(node.else_) |
paul@41 | 1997 | |
paul@163 | 1998 | self.set_block(exit_block) |
paul@163 | 1999 | |
paul@163 | 2000 | self.drop_loop_blocks() |
paul@17 | 2001 | |
paul@100 | 2002 | def visitWith(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "With") |
paul@17 | 2003 | |
paul@100 | 2004 | def visitYield(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Yield") |
paul@17 | 2005 | |
paul@17 | 2006 | # vim: tabstop=4 expandtab shiftwidth=4 |