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