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