paul@196 | 1 | #!/usr/bin/env python |
paul@196 | 2 | |
paul@196 | 3 | """ |
paul@196 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@196 | 5 | |
paul@196 | 6 | Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> |
paul@196 | 7 | |
paul@196 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@196 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@196 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@196 | 11 | version. |
paul@196 | 12 | |
paul@196 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@196 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@196 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@196 | 16 | details. |
paul@196 | 17 | |
paul@196 | 18 | You should have received a copy of the GNU General Public License along with |
paul@196 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@196 | 20 | """ |
paul@196 | 21 | |
paul@196 | 22 | from micropython.common import * |
paul@196 | 23 | from micropython.data import * |
paul@196 | 24 | from micropython.rsvp import * |
paul@196 | 25 | import compiler.ast |
paul@196 | 26 | |
paul@196 | 27 | class Helper: |
paul@196 | 28 | |
paul@196 | 29 | "Internal helper methods for AST visitors." |
paul@196 | 30 | |
paul@196 | 31 | # Allocation-related methods. |
paul@196 | 32 | |
paul@196 | 33 | def make_object(self, cls, n): |
paul@196 | 34 | |
paul@196 | 35 | """ |
paul@196 | 36 | Request a new object with the given class 'cls' and with 'n' attributes. |
paul@196 | 37 | """ |
paul@196 | 38 | |
paul@196 | 39 | # Load the class in order to locate the instance template. |
paul@196 | 40 | |
paul@196 | 41 | self.new_op(LoadConst(cls)) |
paul@196 | 42 | |
paul@196 | 43 | # NOTE: Object headers are one location. |
paul@196 | 44 | |
paul@196 | 45 | self.new_op(MakeObject(n + 1)) |
paul@196 | 46 | |
paul@196 | 47 | # Name-related methods. |
paul@196 | 48 | |
paul@196 | 49 | def get_scope(self, name): |
paul@196 | 50 | |
paul@196 | 51 | "Return the scope for the given 'name'." |
paul@196 | 52 | |
paul@196 | 53 | if self.unit.has_key(name): |
paul@196 | 54 | return "local" |
paul@196 | 55 | elif self.module.has_key(name): |
paul@196 | 56 | return "global" |
paul@196 | 57 | else: |
paul@196 | 58 | return "builtins" |
paul@196 | 59 | |
paul@196 | 60 | def load_builtin(self, name, node): |
paul@196 | 61 | |
paul@196 | 62 | "Generate an instruction loading 'name' for the given 'node'." |
paul@196 | 63 | |
paul@196 | 64 | self.new_op(LoadAddress(self.get_builtin(name, node))) |
paul@196 | 65 | |
paul@196 | 66 | def get_builtin_class(self, name, node): |
paul@196 | 67 | |
paul@196 | 68 | "Return the built-in class with the given 'name' for the given 'node'." |
paul@196 | 69 | |
paul@196 | 70 | return self.get_builtin(name, node).get_value() |
paul@196 | 71 | |
paul@196 | 72 | def get_builtin(self, name, node): |
paul@196 | 73 | |
paul@196 | 74 | """ |
paul@196 | 75 | Return the built-in module definition for the given 'name', used by the |
paul@196 | 76 | given 'node'. |
paul@196 | 77 | """ |
paul@196 | 78 | |
paul@196 | 79 | if self.builtins is not None: |
paul@196 | 80 | try: |
paul@196 | 81 | return self.builtins[name] |
paul@196 | 82 | except KeyError: |
paul@196 | 83 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@196 | 84 | else: |
paul@196 | 85 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@196 | 86 | |
paul@196 | 87 | # Code feature methods. |
paul@196 | 88 | |
paul@196 | 89 | def new_block(self): |
paul@196 | 90 | |
paul@196 | 91 | "Return a new code block." |
paul@196 | 92 | |
paul@196 | 93 | return Block() |
paul@196 | 94 | |
paul@196 | 95 | def set_block(self, block): |
paul@196 | 96 | |
paul@196 | 97 | "Add the given 'block' to the unit's list of blocks." |
paul@196 | 98 | |
paul@196 | 99 | self.optimiser.reset() |
paul@196 | 100 | self.blocks.append(block) |
paul@196 | 101 | |
paul@196 | 102 | def get_loop_blocks(self): |
paul@196 | 103 | return self.loop_blocks[-1] |
paul@196 | 104 | |
paul@196 | 105 | def add_loop_blocks(self, next_block, exit_block): |
paul@196 | 106 | self.loop_blocks.append((next_block, exit_block)) |
paul@196 | 107 | |
paul@196 | 108 | def drop_loop_blocks(self): |
paul@196 | 109 | self.loop_blocks.pop() |
paul@196 | 110 | |
paul@196 | 111 | def get_exception_blocks(self): |
paul@196 | 112 | return self.exception_blocks[-1] |
paul@196 | 113 | |
paul@196 | 114 | def add_exception_blocks(self, handler_block, exit_block): |
paul@196 | 115 | self.exception_blocks.append((handler_block, exit_block)) |
paul@196 | 116 | |
paul@196 | 117 | def drop_exception_blocks(self): |
paul@196 | 118 | self.exception_blocks.pop() |
paul@196 | 119 | |
paul@196 | 120 | # Assignment expression values. |
paul@196 | 121 | |
paul@196 | 122 | def record_value(self, immediate=1): |
paul@196 | 123 | |
paul@196 | 124 | """ |
paul@196 | 125 | Record the current active value for an assignment. If the optional |
paul@196 | 126 | 'immediate' parameter if set to a false value always allocates new |
paul@196 | 127 | temporary storage to hold the recorded value; otherwise, the |
paul@196 | 128 | value-providing instruction may be replicated in order to provide the |
paul@196 | 129 | active value later on. |
paul@196 | 130 | """ |
paul@196 | 131 | |
paul@196 | 132 | if immediate: |
paul@196 | 133 | temp = self.optimiser.optimise_temp_storage() |
paul@196 | 134 | else: |
paul@196 | 135 | temp = self.get_temp() |
paul@196 | 136 | self.expr_temp.append(temp) |
paul@196 | 137 | |
paul@196 | 138 | def discard_value(self): |
paul@196 | 139 | |
paul@196 | 140 | "Discard any temporary storage in use for the current assignment value." |
paul@196 | 141 | |
paul@196 | 142 | self.discard_temp(self.expr_temp.pop()) |
paul@196 | 143 | |
paul@196 | 144 | def set_source(self): |
paul@196 | 145 | |
paul@196 | 146 | "Set the source of an assignment using the current assignment value." |
paul@196 | 147 | |
paul@196 | 148 | self.optimiser.set_source(self.expr_temp[-1]) |
paul@196 | 149 | |
paul@196 | 150 | # Optimise away constant storage if appropriate. |
paul@196 | 151 | |
paul@196 | 152 | if self.optimiser.optimise_constant_storage(): |
paul@196 | 153 | self.remove_op() |
paul@196 | 154 | |
paul@196 | 155 | def is_immediate_user(self, node): |
paul@196 | 156 | |
paul@196 | 157 | """ |
paul@196 | 158 | Return whether 'node' is an immediate user of an assignment expression. |
paul@196 | 159 | """ |
paul@196 | 160 | |
paul@196 | 161 | return isinstance(node, (compiler.ast.AssName, compiler.ast.AssAttr)) |
paul@196 | 162 | |
paul@196 | 163 | def has_immediate_usage(self, nodes): |
paul@196 | 164 | |
paul@196 | 165 | """ |
paul@196 | 166 | Return whether 'nodes' are all immediate users of an assignment expression. |
paul@196 | 167 | """ |
paul@196 | 168 | |
paul@196 | 169 | for n in nodes: |
paul@196 | 170 | if not self.is_immediate_user(n): |
paul@196 | 171 | return 0 |
paul@196 | 172 | return 1 |
paul@196 | 173 | |
paul@196 | 174 | # Temporary storage administration. |
paul@196 | 175 | |
paul@196 | 176 | def get_temp(self): |
paul@196 | 177 | |
paul@196 | 178 | """ |
paul@196 | 179 | Add a temporary storage instruction for the current value and return a |
paul@196 | 180 | sequence of access instructions. |
paul@196 | 181 | """ |
paul@196 | 182 | |
paul@196 | 183 | position_in_frame = self.reserve_temp() |
paul@196 | 184 | self.new_op(StoreTemp(position_in_frame)) |
paul@196 | 185 | return LoadTemp(position_in_frame) |
paul@196 | 186 | |
paul@196 | 187 | def reserve_temp(self, temp_position=None): |
paul@196 | 188 | |
paul@196 | 189 | """ |
paul@196 | 190 | Reserve a new temporary storage position, or if the optional |
paul@196 | 191 | 'temp_position' is specified, ensure that this particular position is |
paul@196 | 192 | reserved. |
paul@196 | 193 | """ |
paul@196 | 194 | |
paul@196 | 195 | if temp_position is not None: |
paul@196 | 196 | pass |
paul@196 | 197 | elif not self.temp_positions: |
paul@196 | 198 | temp_position = 0 |
paul@196 | 199 | else: |
paul@196 | 200 | temp_position = max(self.temp_positions) + 1 |
paul@196 | 201 | self.temp_positions.add(temp_position) |
paul@196 | 202 | self.max_temp_position = max(self.max_temp_position, temp_position) |
paul@196 | 203 | return self.unit.all_local_usage + temp_position # position in frame |
paul@196 | 204 | |
paul@196 | 205 | def ensure_temp(self, instruction=None): |
paul@196 | 206 | |
paul@196 | 207 | """ |
paul@196 | 208 | Ensure that the 'instruction' is using a reserved temporary storage |
paul@196 | 209 | position. |
paul@196 | 210 | """ |
paul@196 | 211 | |
paul@196 | 212 | if isinstance(instruction, LoadTemp): |
paul@196 | 213 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@196 | 214 | self.reserve_temp(temp_position) |
paul@196 | 215 | |
paul@196 | 216 | def discard_temp(self, instruction=None): |
paul@196 | 217 | |
paul@196 | 218 | "Discard any temporary storage position used by 'instruction'." |
paul@196 | 219 | |
paul@196 | 220 | if isinstance(instruction, LoadTemp): |
paul@196 | 221 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@196 | 222 | self.free_temp(temp_position) |
paul@196 | 223 | |
paul@196 | 224 | def free_temp(self, temp_position): |
paul@196 | 225 | |
paul@196 | 226 | "Free the temporary storage position specified by 'temp_position'." |
paul@196 | 227 | |
paul@196 | 228 | if temp_position in self.temp_positions: |
paul@196 | 229 | self.temp_positions.remove(temp_position) |
paul@196 | 230 | |
paul@196 | 231 | def set_frame_usage(self, node, extend): |
paul@196 | 232 | |
paul@196 | 233 | """ |
paul@196 | 234 | Ensure that the frame usage for the unit associated with 'node' is set |
paul@196 | 235 | on the 'extend' instruction. |
paul@196 | 236 | """ |
paul@196 | 237 | |
paul@208 | 238 | ntemp = self.max_temp_position + 2 # include space for instantiators to expand backwards |
paul@196 | 239 | extend.attr = ntemp + node.unit.local_usage # NOTE: See get_code for similar code. |
paul@196 | 240 | |
paul@196 | 241 | # Code writing methods. |
paul@196 | 242 | |
paul@196 | 243 | def new_op(self, op): |
paul@196 | 244 | |
paul@196 | 245 | "Add 'op' to the generated code." |
paul@196 | 246 | |
paul@196 | 247 | # Optimise load operations employed by this instruction. |
paul@196 | 248 | |
paul@196 | 249 | self.optimiser.optimise_load_operations(op) |
paul@196 | 250 | if self.optimiser.optimise_away_no_operations(op): |
paul@196 | 251 | return |
paul@196 | 252 | |
paul@196 | 253 | # Add the operation to the current block. |
paul@196 | 254 | |
paul@196 | 255 | self.blocks[-1].code.append(op) |
paul@196 | 256 | self.optimiser.set_new(op) |
paul@196 | 257 | |
paul@196 | 258 | def remove_op(self): |
paul@196 | 259 | |
paul@196 | 260 | "Remove the last instruction." |
paul@196 | 261 | |
paul@196 | 262 | op = self.blocks[-1].code.pop() |
paul@196 | 263 | self.optimiser.clear_active() |
paul@196 | 264 | |
paul@196 | 265 | def replace_op(self, op): |
paul@196 | 266 | |
paul@196 | 267 | "Replace the last added instruction with 'op'." |
paul@196 | 268 | |
paul@196 | 269 | self.remove_op() |
paul@196 | 270 | self.new_op(op) |
paul@196 | 271 | |
paul@196 | 272 | def replace_active_value(self, op): |
paul@196 | 273 | |
paul@196 | 274 | """ |
paul@196 | 275 | Replace the value-providing active instruction with 'op' if appropriate. |
paul@196 | 276 | """ |
paul@196 | 277 | |
paul@196 | 278 | self.optimiser.remove_active_value() |
paul@196 | 279 | self.new_op(op) |
paul@196 | 280 | |
paul@196 | 281 | def last_op(self): |
paul@196 | 282 | |
paul@196 | 283 | "Return the last added instruction." |
paul@196 | 284 | |
paul@196 | 285 | try: |
paul@196 | 286 | return self.blocks[-1].code[-1] |
paul@196 | 287 | except IndexError: |
paul@196 | 288 | return None |
paul@196 | 289 | |
paul@196 | 290 | # Common methods. |
paul@196 | 291 | |
paul@196 | 292 | def _visitAttr(self, node, classes): |
paul@196 | 293 | |
paul@196 | 294 | """ |
paul@196 | 295 | Visit the attribute-related 'node', generating instructions based on the |
paul@196 | 296 | given 'classes'. |
paul@196 | 297 | """ |
paul@196 | 298 | |
paul@196 | 299 | self.dispatch(node.expr) |
paul@196 | 300 | self._generateAttr(node, node.attrname, classes) |
paul@196 | 301 | |
paul@196 | 302 | def _generateAttr(self, node, attrname, classes): |
paul@196 | 303 | |
paul@196 | 304 | """ |
paul@196 | 305 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@196 | 306 | """ |
paul@196 | 307 | |
paul@196 | 308 | AddressInstruction, AddressContextInstruction, AddressContextCondInstruction, \ |
paul@196 | 309 | AttrInstruction, AttrIndexInstruction, AttrIndexContextInstruction, \ |
paul@196 | 310 | AttrIndexContextCondInstruction = classes |
paul@196 | 311 | |
paul@196 | 312 | # Where the last operation (defining the attribute owner) yields a |
paul@196 | 313 | # constant... |
paul@196 | 314 | |
paul@196 | 315 | target_name = self.optimiser.optimise_constant_accessor() |
paul@196 | 316 | |
paul@196 | 317 | # Only try and discover the position if the target can be resolved. |
paul@196 | 318 | # Since instances cannot be constants, this involves classes and |
paul@196 | 319 | # modules. |
paul@196 | 320 | |
paul@196 | 321 | if target_name is not None: |
paul@196 | 322 | |
paul@196 | 323 | # Access the object table to get the attribute position. |
paul@196 | 324 | |
paul@196 | 325 | try: |
paul@196 | 326 | table_entry = self.objtable.table[target_name] |
paul@196 | 327 | except KeyError: |
paul@196 | 328 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 329 | "No object entry exists for target %r." % target_name) |
paul@196 | 330 | |
paul@196 | 331 | try: |
paul@196 | 332 | pos = table_entry[attrname] |
paul@196 | 333 | except KeyError: |
paul@196 | 334 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 335 | "No attribute entry exists for name %r in target %r." % (attrname, target_name)) |
paul@196 | 336 | |
paul@196 | 337 | # Produce a suitable instruction. |
paul@196 | 338 | |
paul@196 | 339 | if AddressInstruction is not None: |
paul@196 | 340 | self.replace_active_value(AddressInstruction(pos)) |
paul@196 | 341 | else: |
paul@196 | 342 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 343 | "Storing of class or module attribute %r via an object is not permitted." % attrname) |
paul@196 | 344 | |
paul@196 | 345 | return |
paul@196 | 346 | |
paul@196 | 347 | # Where the last operation involves the special 'self' name, check to |
paul@196 | 348 | # see if the attribute is acceptably positioned and produce a direct |
paul@196 | 349 | # access to the attribute. |
paul@196 | 350 | |
paul@196 | 351 | # This is the only reliable way of detecting instance accesses at |
paul@196 | 352 | # compile-time since in general, objects could be classes or modules, |
paul@196 | 353 | # but 'self' should only refer to instances. |
paul@196 | 354 | |
paul@196 | 355 | elif self.optimiser.optimise_self_access(self.unit, attrname): |
paul@196 | 356 | |
paul@196 | 357 | # Either generate an instruction operating on an instance attribute. |
paul@196 | 358 | |
paul@196 | 359 | try: |
paul@196 | 360 | attr = self.unit.parent.instance_attributes()[attrname] |
paul@196 | 361 | self.new_op(AttrInstruction(attr)) |
paul@196 | 362 | return |
paul@196 | 363 | |
paul@196 | 364 | # Or generate an instruction operating on a class attribute. |
paul@196 | 365 | # NOTE: Any simple instruction providing self is not removed. |
paul@196 | 366 | |
paul@196 | 367 | except KeyError: |
paul@196 | 368 | |
paul@196 | 369 | try: |
paul@196 | 370 | attr = self.unit.parent.all_attributes()[attrname] |
paul@196 | 371 | |
paul@196 | 372 | # Switch the context if the class attribute is compatible with |
paul@196 | 373 | # the instance. |
paul@196 | 374 | |
paul@196 | 375 | if attr.defined_within_hierarchy(): |
paul@196 | 376 | |
paul@196 | 377 | # Only permit loading (not storing) of class attributes via self. |
paul@196 | 378 | |
paul@196 | 379 | if AddressContextInstruction is not None: |
paul@196 | 380 | self.new_op(AddressContextInstruction(attr)) |
paul@196 | 381 | else: |
paul@196 | 382 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 383 | "Storing of class attribute %r via self not permitted." % attrname) |
paul@196 | 384 | |
paul@196 | 385 | # Preserve the context if the class attribute comes from an |
paul@196 | 386 | # incompatible class. |
paul@196 | 387 | |
paul@196 | 388 | elif attr.defined_outside_hierarchy(): |
paul@196 | 389 | |
paul@196 | 390 | # Only permit loading (not storing) of class attributes via self. |
paul@196 | 391 | |
paul@196 | 392 | if AddressInstruction is not None: |
paul@196 | 393 | self.new_op(AddressInstruction(attr)) |
paul@196 | 394 | else: |
paul@196 | 395 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 396 | "Storing of class attribute %r via self not permitted." % attrname) |
paul@196 | 397 | |
paul@196 | 398 | # Otherwise, test for a suitable context at run-time. |
paul@196 | 399 | |
paul@196 | 400 | else: |
paul@196 | 401 | |
paul@196 | 402 | # Only permit loading (not storing) of class attributes via self. |
paul@196 | 403 | |
paul@196 | 404 | if AddressContextCondInstruction is not None: |
paul@196 | 405 | self.new_op(AddressContextCondInstruction(attr)) |
paul@196 | 406 | else: |
paul@196 | 407 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 408 | "Storing of class attribute %r via self not permitted." % attrname) |
paul@196 | 409 | |
paul@196 | 410 | return |
paul@196 | 411 | |
paul@196 | 412 | # Or delegate the attribute access to a general instruction |
paul@196 | 413 | # since the kind of attribute cannot be deduced. |
paul@196 | 414 | |
paul@196 | 415 | except KeyError: |
paul@196 | 416 | pass |
paul@196 | 417 | |
paul@196 | 418 | # Otherwise, perform a normal operation. |
paul@196 | 419 | |
paul@196 | 420 | try: |
paul@196 | 421 | index = self.objtable.get_index(attrname) |
paul@196 | 422 | |
paul@196 | 423 | except self.objtable.TableError: |
paul@196 | 424 | |
paul@196 | 425 | # If this error arises on generated code, check the names_used |
paul@196 | 426 | # attribute on the Importer. |
paul@196 | 427 | |
paul@196 | 428 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 429 | "No attribute entry exists for name %r." % attrname) |
paul@196 | 430 | |
paul@196 | 431 | # NOTE: Test for class vs. instance attributes, generating |
paul@196 | 432 | # NOTE: context-related instructions. |
paul@196 | 433 | |
paul@196 | 434 | if AttrIndexContextCondInstruction is not None: |
paul@196 | 435 | self.new_op(AttrIndexContextCondInstruction(index)) |
paul@196 | 436 | |
paul@196 | 437 | # Store instructions do not need to consider context modifications. |
paul@196 | 438 | |
paul@196 | 439 | else: |
paul@196 | 440 | self.new_op(AttrIndexInstruction(index)) |
paul@196 | 441 | |
paul@196 | 442 | # Invocations involve the following: |
paul@196 | 443 | # |
paul@196 | 444 | # 1. Reservation of a frame for the arguments |
paul@196 | 445 | # 2. Identification of the target which is then held in temporary storage |
paul@196 | 446 | # 3. Optional inclusion of a context (important for methods) |
paul@196 | 447 | # 4. Preparation of the argument frame |
paul@196 | 448 | # 5. Invocation of the target |
paul@196 | 449 | # 6. Discarding of the frame |
paul@196 | 450 | # |
paul@196 | 451 | # In order to support nested invocations - such as a(b(c)) - use of the |
paul@196 | 452 | # temporary storage is essential. |
paul@196 | 453 | |
paul@196 | 454 | def _startCallFunc(self): |
paul@196 | 455 | |
paul@196 | 456 | "Record the location of the invocation." |
paul@196 | 457 | |
paul@196 | 458 | op = MakeFrame() |
paul@196 | 459 | self.new_op(op) # records the start of the frame |
paul@196 | 460 | self.frame_makers.append(op) |
paul@196 | 461 | |
paul@196 | 462 | def _generateCallFunc(self, args, node): |
paul@196 | 463 | |
paul@196 | 464 | """ |
paul@196 | 465 | Support a generic function invocation using the given 'args', occurring |
paul@196 | 466 | on the given 'node', where the expression providing the invocation |
paul@196 | 467 | target has just been generated. |
paul@196 | 468 | |
paul@196 | 469 | In other situations, the invocation is much simpler and does not need to |
paul@196 | 470 | handle the full flexibility of a typical Python invocation. Internal |
paul@196 | 471 | invocations, such as those employed by operators and certain |
paul@196 | 472 | control-flow mechanisms, use predetermined arguments and arguably do not |
paul@196 | 473 | need to support the same things as the more general invocations. |
paul@196 | 474 | """ |
paul@196 | 475 | |
paul@196 | 476 | target, context, temp = self._generateCallFuncContext() |
paul@196 | 477 | self._generateCallFuncArgs(target, context, temp, args, node) |
paul@196 | 478 | return temp, target |
paul@196 | 479 | |
paul@196 | 480 | def _generateCallFuncContext(self): |
paul@196 | 481 | |
paul@196 | 482 | """ |
paul@196 | 483 | Produce code which loads and checks the context of the current |
paul@196 | 484 | invocation, the instructions for whose target have already been |
paul@196 | 485 | produced, returning a list of instructions which reference the |
paul@196 | 486 | invocation target. |
paul@196 | 487 | """ |
paul@196 | 488 | |
paul@196 | 489 | t = self.optimiser.optimise_known_target() |
paul@196 | 490 | if t: |
paul@196 | 491 | target, context = t |
paul@196 | 492 | if isinstance(target, Instance): # lambda object |
paul@196 | 493 | target, context = None, None |
paul@196 | 494 | else: |
paul@196 | 495 | target, context = None, None |
paul@196 | 496 | |
paul@196 | 497 | # Store the target in temporary storage for subsequent referencing. |
paul@196 | 498 | # NOTE: This may not be appropriate for class invocations |
paul@196 | 499 | # NOTE: (instantiation). |
paul@196 | 500 | |
paul@196 | 501 | temp = self.optimiser.optimise_temp_storage() |
paul@196 | 502 | |
paul@196 | 503 | # Where a target or context are not known or where an instance is known |
paul@196 | 504 | # to be the context, load the context. |
paul@196 | 505 | |
paul@196 | 506 | if target is None or isinstance(context, Instance): |
paul@196 | 507 | self.new_op(temp) |
paul@196 | 508 | self.new_op(LoadContext()) |
paul@196 | 509 | self.new_op(StoreFrame(0)) |
paul@196 | 510 | |
paul@196 | 511 | # Otherwise omit the context. |
paul@196 | 512 | |
paul@196 | 513 | else: |
paul@196 | 514 | pass # NOTE: Class methods should be supported. |
paul@196 | 515 | |
paul@196 | 516 | return target, context, temp |
paul@196 | 517 | |
paul@196 | 518 | def _generateCallFuncArgs(self, target, context, temp, args, node): |
paul@196 | 519 | |
paul@196 | 520 | """ |
paul@196 | 521 | Given invocation 'target' and 'context' information, the 'temp' |
paul@196 | 522 | reference to the target, a list of nodes representing the 'args' |
paul@196 | 523 | (arguments), generate instructions which load the arguments for the |
paul@196 | 524 | invocation defined by the given 'node'. |
paul@196 | 525 | """ |
paul@196 | 526 | |
paul@196 | 527 | # Evaluate the arguments. |
paul@196 | 528 | |
paul@196 | 529 | employed_positions = set() |
paul@196 | 530 | employed_keywords = set() |
paul@196 | 531 | extra_keywords = [] |
paul@196 | 532 | |
paul@196 | 533 | # Find keyword arguments in advance in order to help resolve targets. |
paul@196 | 534 | |
paul@196 | 535 | for arg in args: |
paul@196 | 536 | if isinstance(arg, compiler.ast.Keyword): |
paul@196 | 537 | employed_keywords.add(arg.name) |
paul@196 | 538 | |
paul@196 | 539 | possible_targets = self.paramtable.all_possible_objects(employed_keywords) |
paul@196 | 540 | |
paul@196 | 541 | # Note the presence of the context in the frame where appropriate. |
paul@196 | 542 | |
paul@196 | 543 | if target is None or isinstance(context, Instance): |
paul@196 | 544 | ncontext = 1 |
paul@196 | 545 | expect_context = 0 |
paul@196 | 546 | |
paul@196 | 547 | # Handle calls to classes. |
paul@203 | 548 | # The resulting target must match that used in the actual invocation. |
paul@196 | 549 | |
paul@196 | 550 | elif isinstance(target, Class): |
paul@203 | 551 | ncontext = 0 |
paul@196 | 552 | expect_context = 0 |
paul@203 | 553 | target = target.get_instantiator() |
paul@196 | 554 | |
paul@196 | 555 | # Method calls via classes. |
paul@196 | 556 | |
paul@196 | 557 | elif isinstance(context, Class): |
paul@196 | 558 | ncontext = 0 |
paul@196 | 559 | expect_context = 1 |
paul@196 | 560 | |
paul@196 | 561 | # Function calls. |
paul@196 | 562 | |
paul@196 | 563 | else: |
paul@196 | 564 | ncontext = 0 |
paul@196 | 565 | expect_context = 0 |
paul@196 | 566 | |
paul@196 | 567 | first = 1 |
paul@196 | 568 | frame_pos = ncontext |
paul@196 | 569 | max_keyword_pos = -1 |
paul@196 | 570 | |
paul@196 | 571 | for arg in args: |
paul@196 | 572 | |
paul@196 | 573 | # Handle positional and keyword arguments separately. |
paul@196 | 574 | |
paul@196 | 575 | if isinstance(arg, compiler.ast.Keyword): |
paul@196 | 576 | |
paul@196 | 577 | # Optimise where the target is known now. |
paul@196 | 578 | |
paul@196 | 579 | if target is not None: |
paul@196 | 580 | |
paul@196 | 581 | # Find the parameter table entry for the target. |
paul@196 | 582 | |
paul@196 | 583 | target_name = target.full_name() |
paul@196 | 584 | |
paul@196 | 585 | # Look for a callable with the precise target name. |
paul@196 | 586 | |
paul@196 | 587 | table_entry = self.paramtable.table[target_name] |
paul@196 | 588 | |
paul@196 | 589 | # Look the name up in the parameter table entry. |
paul@196 | 590 | |
paul@196 | 591 | try: |
paul@196 | 592 | pos = table_entry[arg.name] |
paul@196 | 593 | |
paul@196 | 594 | # Where no position is found, this could be an extra keyword |
paul@196 | 595 | # argument. |
paul@196 | 596 | |
paul@196 | 597 | except KeyError: |
paul@196 | 598 | extra_keywords.append(arg) |
paul@196 | 599 | continue |
paul@196 | 600 | |
paul@196 | 601 | # Test for illegal conditions. |
paul@196 | 602 | |
paul@196 | 603 | if pos in employed_positions: |
paul@196 | 604 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 605 | "Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@196 | 606 | |
paul@196 | 607 | employed_positions.add(pos) |
paul@196 | 608 | |
paul@196 | 609 | # Generate code for the keyword and the positioning |
paul@196 | 610 | # operation. |
paul@196 | 611 | |
paul@196 | 612 | self.dispatch(arg.expr) |
paul@196 | 613 | self.new_op(StoreFrame(pos)) |
paul@196 | 614 | |
paul@196 | 615 | # Otherwise, generate the code needed to obtain the details of |
paul@196 | 616 | # the parameter location. |
paul@196 | 617 | |
paul@196 | 618 | else: |
paul@196 | 619 | |
paul@196 | 620 | # Combine the target details with the name to get the location. |
paul@196 | 621 | # See the access method on the List class. |
paul@196 | 622 | |
paul@196 | 623 | try: |
paul@196 | 624 | paramindex = self.paramtable.get_index(arg.name) |
paul@196 | 625 | |
paul@196 | 626 | # Where no position is found, this could be an extra keyword |
paul@196 | 627 | # argument. |
paul@196 | 628 | |
paul@196 | 629 | except self.paramtable.TableError: |
paul@196 | 630 | extra_keywords.append(arg) |
paul@196 | 631 | continue |
paul@196 | 632 | |
paul@196 | 633 | # Generate code for the keyword and the positioning |
paul@196 | 634 | # operation. Get the value as the source of the assignment. |
paul@196 | 635 | |
paul@196 | 636 | self.dispatch(arg.expr) |
paul@196 | 637 | self.record_value() |
paul@196 | 638 | |
paul@196 | 639 | # Store the source value using the callable's parameter |
paul@196 | 640 | # table information. |
paul@196 | 641 | |
paul@196 | 642 | self.new_op(temp) |
paul@196 | 643 | self.new_op(StoreFrameIndex(paramindex)) |
paul@196 | 644 | |
paul@196 | 645 | self.set_source() |
paul@196 | 646 | self.discard_value() |
paul@196 | 647 | |
paul@196 | 648 | # Record the highest possible frame position for this argument. |
paul@196 | 649 | |
paul@196 | 650 | max_keyword_pos = max(max_keyword_pos, max(self.paramtable.all_attribute_positions(arg.name))) |
paul@196 | 651 | |
paul@196 | 652 | else: |
paul@196 | 653 | self.dispatch(arg) |
paul@196 | 654 | self.new_op(StoreFrame(frame_pos)) |
paul@196 | 655 | |
paul@196 | 656 | employed_positions.add(frame_pos) |
paul@196 | 657 | |
paul@196 | 658 | # Check to see if the first argument is appropriate (compatible with |
paul@196 | 659 | # the target where methods are being invoked via classes). |
paul@196 | 660 | |
paul@196 | 661 | if first and expect_context: |
paul@196 | 662 | |
paul@196 | 663 | # Drop any test if the target and the context are known. |
paul@196 | 664 | |
paul@196 | 665 | if not self.optimiser.have_correct_self_for_target(context, self.unit): |
paul@196 | 666 | |
paul@196 | 667 | continue_block = self.new_block() |
paul@196 | 668 | |
paul@196 | 669 | self.new_op(CheckSelf()) |
paul@196 | 670 | self.optimiser.set_source(temp) |
paul@196 | 671 | self.new_op(JumpIfTrue(continue_block)) |
paul@196 | 672 | |
paul@196 | 673 | # Where the context is inappropriate, drop the incomplete frame and |
paul@196 | 674 | # raise an exception. |
paul@196 | 675 | |
paul@196 | 676 | self.new_op(DropFrame()) |
paul@196 | 677 | self.new_op(LoadResult()) |
paul@196 | 678 | |
paul@196 | 679 | self.load_builtin("TypeError", node) |
paul@196 | 680 | self.new_op(StoreException()) |
paul@196 | 681 | self.new_op(RaiseException()) |
paul@196 | 682 | |
paul@196 | 683 | self.set_block(continue_block) |
paul@196 | 684 | |
paul@196 | 685 | first = 0 |
paul@196 | 686 | frame_pos += 1 |
paul@196 | 687 | |
paul@196 | 688 | # NOTE: Extra keywords are not supported. |
paul@196 | 689 | # NOTE: Somehow, the above needs to be combined with * arguments. |
paul@196 | 690 | |
paul@196 | 691 | if extra_keywords: |
paul@196 | 692 | print "Warning: extra keyword argument(s) %s not handled." % ", ".join([arg.name for arg in extra_keywords]) |
paul@196 | 693 | |
paul@196 | 694 | # Either test for a complete set of arguments. |
paul@196 | 695 | |
paul@196 | 696 | if target is not None: |
paul@196 | 697 | |
paul@196 | 698 | # Make sure that enough arguments have been given. |
paul@196 | 699 | |
paul@196 | 700 | nargs_max = len(target.positional_names) |
paul@196 | 701 | ndefaults = len(target.defaults) |
paul@196 | 702 | nargs_min = nargs_max - ndefaults |
paul@196 | 703 | |
paul@213 | 704 | # Visit each argument position and look for a supplied argument. |
paul@213 | 705 | |
paul@196 | 706 | for i in range(ncontext, nargs_min): |
paul@196 | 707 | if i not in employed_positions: |
paul@196 | 708 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 709 | "Argument %r not supplied for %r: need at least %d argument(s)." % (i+1, target.name, nargs_min)) |
paul@196 | 710 | |
paul@196 | 711 | nargs = frame_pos |
paul@196 | 712 | |
paul@213 | 713 | # Determine whether too many arguments have been given and how big |
paul@213 | 714 | # the frame should be. |
paul@213 | 715 | |
paul@213 | 716 | # For parameter lists with * or ** parameters, accept as many |
paul@213 | 717 | # arguments as are allowed or as many as we have. |
paul@213 | 718 | |
paul@213 | 719 | if target.has_star or target.has_dstar: |
paul@213 | 720 | frame_size = max(nargs, nargs_max) |
paul@213 | 721 | |
paul@213 | 722 | # For other parameter lists, only accept as many arguments as we are |
paul@213 | 723 | # allowed. |
paul@213 | 724 | |
paul@213 | 725 | elif nargs > nargs_max: |
paul@196 | 726 | raise TranslateError(self.module.full_name(), node, |
paul@196 | 727 | "Too many arguments for %r: need at most %d argument(s)." % (target.name, nargs_max)) |
paul@196 | 728 | |
paul@213 | 729 | else: |
paul@213 | 730 | frame_size = nargs_max |
paul@213 | 731 | |
paul@196 | 732 | # Where defaults are involved, put them into the frame. |
paul@196 | 733 | |
paul@196 | 734 | self._generateCallFuncDefaultArgs(target, temp, nargs_min, nargs_max, employed_positions) |
paul@196 | 735 | |
paul@196 | 736 | # Set the frame size. |
paul@196 | 737 | |
paul@213 | 738 | self._endCallFuncArgs(frame_size) |
paul@196 | 739 | |
paul@196 | 740 | # Or generate instructions to do this at run-time. |
paul@196 | 741 | |
paul@196 | 742 | else: |
paul@196 | 743 | max_pos = max(max(employed_positions or [-1]), max_keyword_pos, frame_pos - 1) |
paul@196 | 744 | |
paul@196 | 745 | # Only check non-empty frames (using the callable's details). |
paul@196 | 746 | |
paul@196 | 747 | if employed_positions or max_pos >= 0: |
paul@196 | 748 | self.new_op(temp) |
paul@196 | 749 | self.new_op(CheckFrame(max_pos + 1)) |
paul@196 | 750 | |
paul@196 | 751 | # Set the frame size. |
paul@196 | 752 | |
paul@196 | 753 | self._endCallFuncArgs(max_pos + 1) |
paul@196 | 754 | |
paul@196 | 755 | def _generateCallFuncDefaultArgs(self, target, temp, nargs_min, nargs_max, employed_positions): |
paul@196 | 756 | |
paul@196 | 757 | """ |
paul@196 | 758 | For the given 'target' and 'temp' reference to the target, generate |
paul@196 | 759 | default arguments for those positions in the range 'nargs_min'... |
paul@196 | 760 | 'nargs_max' which are not present in the 'employed_positions' |
paul@196 | 761 | collection. |
paul@196 | 762 | """ |
paul@196 | 763 | |
paul@196 | 764 | # Where a lambda is involved, construct a dynamic object to hold the |
paul@196 | 765 | # defaults. |
paul@196 | 766 | |
paul@196 | 767 | dynamic = target.name is None |
paul@196 | 768 | |
paul@196 | 769 | # Here, we use negative index values to visit the right hand end of |
paul@196 | 770 | # the defaults list. |
paul@196 | 771 | |
paul@196 | 772 | for pos in range(nargs_min, nargs_max): |
paul@196 | 773 | if pos not in employed_positions: |
paul@196 | 774 | if dynamic: |
paul@196 | 775 | self.new_op(temp) |
paul@196 | 776 | self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@196 | 777 | else: |
paul@196 | 778 | self.new_op(LoadAddress(target.default_attrs[pos - nargs_min])) |
paul@196 | 779 | self.new_op(StoreFrame(pos)) |
paul@196 | 780 | |
paul@196 | 781 | def _doCallFunc(self, instruction, target=None): |
paul@196 | 782 | |
paul@196 | 783 | "Make the invocation." |
paul@196 | 784 | |
paul@203 | 785 | # For classes, the target itself is used, since the instantiator will be |
paul@203 | 786 | # obtained via the class. |
paul@203 | 787 | |
paul@196 | 788 | if isinstance(target, Class): |
paul@203 | 789 | self.new_op(LoadConst(target)) |
paul@196 | 790 | else: |
paul@196 | 791 | self.new_op(instruction) |
paul@203 | 792 | |
paul@196 | 793 | self.new_op(LoadCallable()) |
paul@196 | 794 | self.new_op(JumpWithFrame()) |
paul@196 | 795 | |
paul@196 | 796 | def _endCallFuncArgs(self, nargs): |
paul@196 | 797 | |
paul@196 | 798 | "Set the frame size." |
paul@196 | 799 | |
paul@196 | 800 | self.frame_makers[-1].attr = nargs |
paul@196 | 801 | self.frame_makers.pop() |
paul@196 | 802 | |
paul@196 | 803 | def _endCallFunc(self, instruction=None, target=None, load_result=1): |
paul@196 | 804 | |
paul@196 | 805 | "Finish the invocation and tidy up afterwards." |
paul@196 | 806 | |
paul@196 | 807 | self.new_op(DropFrame()) |
paul@196 | 808 | if load_result: |
paul@196 | 809 | self.new_op(LoadResult()) |
paul@196 | 810 | |
paul@196 | 811 | # Discard any temporary storage instructions. |
paul@196 | 812 | |
paul@196 | 813 | if instruction is not None: |
paul@196 | 814 | self.discard_temp(instruction) |
paul@196 | 815 | |
paul@196 | 816 | def _generateFunctionDefaults(self, function): |
paul@196 | 817 | |
paul@196 | 818 | """ |
paul@196 | 819 | Generate the default initialisation code for 'function', returning |
paul@196 | 820 | a temporary storage reference if a dynamic object was created for the |
paul@196 | 821 | function. |
paul@196 | 822 | """ |
paul@196 | 823 | |
paul@196 | 824 | attr_to_default = zip(function.default_attrs, function.defaults) |
paul@196 | 825 | if not attr_to_default: |
paul@196 | 826 | return None |
paul@196 | 827 | |
paul@196 | 828 | # Where a lambda is involved, construct a dynamic object to hold the |
paul@196 | 829 | # defaults. |
paul@196 | 830 | |
paul@196 | 831 | dynamic = function.name is None |
paul@196 | 832 | |
paul@196 | 833 | if dynamic: |
paul@196 | 834 | self.make_object(self.get_builtin_class("function", function), len(attr_to_default)) |
paul@196 | 835 | temp = self.get_temp() |
paul@196 | 836 | |
paul@196 | 837 | for attr, default in attr_to_default: |
paul@196 | 838 | self.dispatch(default) |
paul@196 | 839 | |
paul@196 | 840 | self.record_value() |
paul@196 | 841 | if dynamic: |
paul@196 | 842 | self.new_op(temp) |
paul@196 | 843 | self.new_op(StoreAttr(attr)) |
paul@196 | 844 | else: |
paul@196 | 845 | self.new_op(StoreAddress(attr)) |
paul@196 | 846 | self.set_source() |
paul@196 | 847 | self.discard_value() |
paul@196 | 848 | |
paul@196 | 849 | if dynamic: |
paul@196 | 850 | return temp |
paul@196 | 851 | else: |
paul@196 | 852 | return None |
paul@196 | 853 | |
paul@196 | 854 | def _visitName(self, node, classes): |
paul@196 | 855 | |
paul@196 | 856 | """ |
paul@196 | 857 | Visit the name-related 'node', generating instructions based on the |
paul@196 | 858 | given 'classes'. |
paul@196 | 859 | """ |
paul@196 | 860 | |
paul@196 | 861 | name = node.name |
paul@196 | 862 | scope = self.get_scope(name) |
paul@196 | 863 | #print self.module.name, node.lineno, name, scope |
paul@196 | 864 | self._generateName(name, scope, classes, node) |
paul@196 | 865 | |
paul@196 | 866 | def _generateName(self, name, scope, classes, node): |
paul@196 | 867 | |
paul@196 | 868 | """ |
paul@196 | 869 | Generate code for the access to 'name' in 'scope' using the given |
paul@196 | 870 | 'classes', and using the given 'node' as the source of the access. |
paul@196 | 871 | """ |
paul@196 | 872 | |
paul@196 | 873 | NameInstruction, AddressInstruction = classes |
paul@196 | 874 | |
paul@196 | 875 | if scope == "local": |
paul@196 | 876 | unit = self.unit |
paul@196 | 877 | if isinstance(unit, Function): |
paul@196 | 878 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@196 | 879 | elif isinstance(unit, Class): |
paul@196 | 880 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@196 | 881 | elif isinstance(unit, Module): |
paul@196 | 882 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@196 | 883 | else: |
paul@196 | 884 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@196 | 885 | |
paul@196 | 886 | elif scope == "global": |
paul@196 | 887 | globals = self.module.module_attributes() |
paul@196 | 888 | if globals.has_key(name): |
paul@196 | 889 | self.new_op(AddressInstruction(globals[name])) |
paul@196 | 890 | else: |
paul@196 | 891 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@196 | 892 | |
paul@196 | 893 | else: |
paul@196 | 894 | self.new_op(AddressInstruction(self.get_builtin(name, node))) |
paul@196 | 895 | |
paul@196 | 896 | def _visitUnary(self, node): |
paul@196 | 897 | |
paul@196 | 898 | """ |
paul@196 | 899 | _t = node.expr |
paul@196 | 900 | try: |
paul@196 | 901 | _result = _t.__pos__() |
paul@196 | 902 | except AttributeError: |
paul@196 | 903 | raise TypeError |
paul@196 | 904 | """ |
paul@196 | 905 | |
paul@196 | 906 | method = unary_methods[node.__class__.__name__] |
paul@196 | 907 | |
paul@196 | 908 | type_error_block = self.new_block() |
paul@196 | 909 | end_block = self.new_block() |
paul@196 | 910 | |
paul@196 | 911 | # Evaluate and store the operand in temporary storage. |
paul@196 | 912 | |
paul@196 | 913 | self.dispatch(node.expr) |
paul@196 | 914 | temp = self.optimiser.optimise_temp_storage() |
paul@196 | 915 | |
paul@196 | 916 | self.new_op(temp) |
paul@196 | 917 | |
paul@196 | 918 | # Get the method on temp. |
paul@196 | 919 | |
paul@196 | 920 | self._generateAttr(node, method, self.attribute_load_instructions) |
paul@196 | 921 | temp_method = self.optimiser.optimise_temp_storage() |
paul@196 | 922 | |
paul@196 | 923 | self._handleAttributeError(node, type_error_block) |
paul@196 | 924 | |
paul@196 | 925 | # Add arguments. |
paul@196 | 926 | # NOTE: No support for defaults. |
paul@196 | 927 | |
paul@196 | 928 | self._startCallFunc() |
paul@196 | 929 | self.new_op(temp) # Explicit context as first argument. |
paul@196 | 930 | self.new_op(StoreFrame(0)) |
paul@196 | 931 | self._endCallFuncArgs(1) |
paul@196 | 932 | self._doCallFunc(temp_method) |
paul@196 | 933 | self._endCallFunc(temp_method) |
paul@196 | 934 | self.new_op(Jump(end_block)) |
paul@196 | 935 | |
paul@196 | 936 | # Store the result. |
paul@196 | 937 | |
paul@196 | 938 | temp_out = self.get_temp() |
paul@196 | 939 | |
paul@196 | 940 | # Raise a TypeError. |
paul@196 | 941 | |
paul@196 | 942 | self.set_block(type_error_block) |
paul@196 | 943 | self.load_builtin("TypeError", node) |
paul@196 | 944 | self.new_op(StoreException()) |
paul@196 | 945 | self.new_op(RaiseException()) |
paul@196 | 946 | |
paul@196 | 947 | self.set_block(end_block) |
paul@196 | 948 | |
paul@196 | 949 | # Produce the result. |
paul@196 | 950 | |
paul@196 | 951 | self.new_op(temp_out) |
paul@196 | 952 | |
paul@196 | 953 | # Compilation duties... |
paul@196 | 954 | |
paul@196 | 955 | self.discard_temp(temp) |
paul@196 | 956 | self.discard_temp(temp_out) |
paul@196 | 957 | |
paul@196 | 958 | def _visitBinary(self, node): |
paul@196 | 959 | |
paul@196 | 960 | """ |
paul@196 | 961 | _t1 = node.left |
paul@196 | 962 | _t2 = node.right |
paul@196 | 963 | try: |
paul@196 | 964 | _result = _t1.__add__(_t2) |
paul@196 | 965 | if _result is NotImplemented: |
paul@196 | 966 | raise AttributeError |
paul@196 | 967 | except AttributeError: |
paul@196 | 968 | try: |
paul@196 | 969 | _result = _t2.__radd__(_t1) |
paul@196 | 970 | if _result is NotImplemented: |
paul@196 | 971 | raise AttributeError |
paul@196 | 972 | except AttributeError: |
paul@196 | 973 | raise TypeError |
paul@196 | 974 | """ |
paul@196 | 975 | |
paul@196 | 976 | left_method, right_method = binary_methods[node.__class__.__name__] |
paul@196 | 977 | |
paul@196 | 978 | # Evaluate and store the left operand in temporary storage. |
paul@196 | 979 | |
paul@196 | 980 | self.dispatch(node.left) |
paul@196 | 981 | temp1 = self.optimiser.optimise_temp_storage() |
paul@196 | 982 | |
paul@196 | 983 | # Evaluate and store the right operand in temporary storage. |
paul@196 | 984 | |
paul@196 | 985 | self.dispatch(node.right) |
paul@196 | 986 | temp2 = self.optimiser.optimise_temp_storage() |
paul@196 | 987 | |
paul@196 | 988 | temp_out = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@196 | 989 | |
paul@196 | 990 | # Produce the result. |
paul@196 | 991 | |
paul@196 | 992 | self.new_op(temp_out) |
paul@196 | 993 | |
paul@196 | 994 | # Compilation duties... |
paul@196 | 995 | |
paul@196 | 996 | self.discard_temp(temp1) |
paul@196 | 997 | self.discard_temp(temp2) |
paul@196 | 998 | self.discard_temp(temp_out) |
paul@196 | 999 | |
paul@196 | 1000 | def _generateBinary(self, node, temp1, temp2, left_method, right_method): |
paul@196 | 1001 | |
paul@196 | 1002 | """ |
paul@196 | 1003 | For the given 'node', generate the binary operator pattern for the |
paul@196 | 1004 | operands 'temp1' and 'temp2', employing 'left_method' and 'right_method' |
paul@196 | 1005 | as defined for binary operators, but also used in comparisons (for which |
paul@196 | 1006 | this method is provided). |
paul@196 | 1007 | |
paul@196 | 1008 | A temporary storage reference is returned from this method. |
paul@196 | 1009 | """ |
paul@196 | 1010 | |
paul@196 | 1011 | right_block = self.new_block() |
paul@196 | 1012 | type_error_block = self.new_block() |
paul@196 | 1013 | end_block = self.new_block() |
paul@196 | 1014 | |
paul@196 | 1015 | # Left method. |
paul@196 | 1016 | |
paul@196 | 1017 | temp_out = self._generateOpMethod(node, temp1, temp2, left_method, right_block, end_block) |
paul@196 | 1018 | self.discard_temp(temp_out) # NOTE: Will re-use the same storage. |
paul@196 | 1019 | |
paul@196 | 1020 | # Right method. |
paul@196 | 1021 | |
paul@196 | 1022 | self.set_block(right_block) |
paul@196 | 1023 | temp_out = self._generateOpMethod(node, temp2, temp1, right_method, type_error_block, end_block) |
paul@196 | 1024 | |
paul@196 | 1025 | # Raise a TypeError. |
paul@196 | 1026 | |
paul@196 | 1027 | self.set_block(type_error_block) |
paul@196 | 1028 | self.load_builtin("TypeError", node) |
paul@196 | 1029 | self.new_op(StoreException()) |
paul@196 | 1030 | self.new_op(RaiseException()) |
paul@196 | 1031 | |
paul@196 | 1032 | self.set_block(end_block) |
paul@196 | 1033 | return temp_out |
paul@196 | 1034 | |
paul@196 | 1035 | def _generateOpMethod(self, node, temp1, temp2, method_name, next_method_block, end_block): |
paul@196 | 1036 | |
paul@196 | 1037 | """ |
paul@196 | 1038 | For the given 'node', generate the operator method invocation using the |
paul@196 | 1039 | operands 'temp1' and 'temp2', employing the given 'method_name', and |
paul@196 | 1040 | jumping appropriately to 'next_method_block' where a NotImplemented |
paul@196 | 1041 | result is returned, or to 'end_block' if the method call was successful. |
paul@196 | 1042 | |
paul@196 | 1043 | A temporary storage reference is returned from this method. |
paul@196 | 1044 | """ |
paul@196 | 1045 | |
paul@196 | 1046 | end_attempt_block = self.new_block() |
paul@196 | 1047 | |
paul@196 | 1048 | self.new_op(temp1) |
paul@196 | 1049 | |
paul@196 | 1050 | # Get method on temp1. |
paul@196 | 1051 | |
paul@196 | 1052 | self._generateAttr(node, method_name, self.attribute_load_instructions) |
paul@196 | 1053 | temp_method = self.optimiser.optimise_temp_storage() |
paul@196 | 1054 | |
paul@196 | 1055 | self._handleAttributeError(node, end_attempt_block) |
paul@196 | 1056 | |
paul@196 | 1057 | # Add arguments. |
paul@196 | 1058 | # NOTE: No support for defaults. |
paul@196 | 1059 | |
paul@196 | 1060 | self._startCallFunc() |
paul@196 | 1061 | self.new_op(temp1) |
paul@196 | 1062 | self.new_op(StoreFrame(0)) |
paul@196 | 1063 | self.new_op(temp2) |
paul@196 | 1064 | self.new_op(StoreFrame(1)) |
paul@196 | 1065 | self._endCallFuncArgs(2) |
paul@196 | 1066 | self._doCallFunc(temp_method) |
paul@196 | 1067 | self._endCallFunc(temp_method) |
paul@196 | 1068 | |
paul@196 | 1069 | # Store the result. |
paul@196 | 1070 | |
paul@196 | 1071 | temp_out = self.get_temp() |
paul@196 | 1072 | |
paul@196 | 1073 | # Test for NotImplemented. |
paul@196 | 1074 | # Don't actually raise an exception. |
paul@196 | 1075 | |
paul@196 | 1076 | self.new_op(TestIdentityAddress(self.importer.get_predefined_constant("NotImplemented"))) |
paul@196 | 1077 | self.new_op(JumpIfTrue(next_method_block)) |
paul@196 | 1078 | self.new_op(Jump(end_block)) |
paul@196 | 1079 | |
paul@196 | 1080 | # End method attempt. |
paul@196 | 1081 | |
paul@196 | 1082 | self.set_block(end_attempt_block) |
paul@196 | 1083 | return temp_out |
paul@196 | 1084 | |
paul@196 | 1085 | def _handleAttributeError(self, node, end_call_block): |
paul@196 | 1086 | |
paul@196 | 1087 | """ |
paul@196 | 1088 | Add exception handling to the method acquisition instructions where the |
paul@196 | 1089 | attribute access cannot be resolved at compile-time. |
paul@196 | 1090 | """ |
paul@196 | 1091 | |
paul@196 | 1092 | if not self.optimiser.optimise_known_target(): |
paul@196 | 1093 | self.load_builtin("AttributeError", node) |
paul@196 | 1094 | self.new_op(CheckException()) |
paul@196 | 1095 | self.new_op(JumpIfTrue(end_call_block)) |
paul@196 | 1096 | |
paul@196 | 1097 | def _generateSequence(self, sequence_type, node): |
paul@196 | 1098 | |
paul@196 | 1099 | "Make a sequence of 'sequence_type' for the given program 'node'." |
paul@196 | 1100 | |
paul@196 | 1101 | self.make_object(self.get_builtin_class(sequence_type, node), len(node.nodes)) |
paul@196 | 1102 | temp = self.get_temp() |
paul@196 | 1103 | |
paul@196 | 1104 | for i, n in enumerate(node.nodes): |
paul@196 | 1105 | self.dispatch(n) |
paul@196 | 1106 | self.record_value() |
paul@196 | 1107 | self.new_op(temp) |
paul@196 | 1108 | self.new_op(StoreAttr(Attr(i, None, None))) |
paul@196 | 1109 | self.set_source() |
paul@196 | 1110 | self.discard_value() |
paul@196 | 1111 | |
paul@196 | 1112 | self.new_op(temp) |
paul@196 | 1113 | self.discard_temp(temp) |
paul@196 | 1114 | |
paul@196 | 1115 | def _generateTestBoolean(self, node, temp): |
paul@196 | 1116 | |
paul@196 | 1117 | """ |
paul@196 | 1118 | Generate a test of the boolean status of the current value for the given |
paul@196 | 1119 | program 'node'. |
paul@196 | 1120 | """ |
paul@196 | 1121 | |
paul@196 | 1122 | # Get method on temp. |
paul@196 | 1123 | # NOTE: Using __bool__ instead of __nonzero__. |
paul@196 | 1124 | |
paul@196 | 1125 | self._generateAttr(node, "__bool__", self.attribute_load_instructions) |
paul@196 | 1126 | temp_method = self.optimiser.optimise_temp_storage() |
paul@196 | 1127 | |
paul@196 | 1128 | self._startCallFunc() |
paul@196 | 1129 | self.new_op(temp) |
paul@196 | 1130 | self.new_op(StoreFrame(0)) |
paul@196 | 1131 | self._endCallFuncArgs(1) |
paul@196 | 1132 | self._doCallFunc(temp_method) |
paul@196 | 1133 | self._endCallFunc(temp_method) |
paul@196 | 1134 | |
paul@196 | 1135 | self.discard_temp(temp_method) |
paul@196 | 1136 | |
paul@196 | 1137 | # Convert result to boolean (a StoreBoolean operation). |
paul@196 | 1138 | |
paul@196 | 1139 | self.new_op(TestIdentityAddress(self.importer.get_predefined_constant("True"))) |
paul@196 | 1140 | |
paul@196 | 1141 | def _generateLoadBoolean(self, node): |
paul@196 | 1142 | |
paul@196 | 1143 | """ |
paul@196 | 1144 | Generate instructions to load the appropriate value given the current |
paul@196 | 1145 | boolean status. |
paul@196 | 1146 | """ |
paul@196 | 1147 | |
paul@196 | 1148 | true_block = self.new_block() |
paul@196 | 1149 | end_block = self.new_block() |
paul@196 | 1150 | |
paul@196 | 1151 | self.new_op(JumpIfTrue(true_block)) |
paul@197 | 1152 | self.new_op(LoadConst(self.importer.get_predefined_constant("False"))) |
paul@196 | 1153 | self.new_op(Jump(end_block)) |
paul@196 | 1154 | |
paul@196 | 1155 | self.set_block(true_block) |
paul@197 | 1156 | self.new_op(LoadConst(self.importer.get_predefined_constant("True"))) |
paul@196 | 1157 | |
paul@196 | 1158 | self.set_block(end_block) |
paul@196 | 1159 | |
paul@196 | 1160 | # vim: tabstop=4 expandtab shiftwidth=4 |