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