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