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