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