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