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