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