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