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