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