1 #!/usr/bin/env python 2 3 """ 4 Simplify AST structures for easier type propagation and analysis. The code in 5 this module processes AST trees originating from the compiler module and 6 produces a result tree consisting of instruction-oriented program nodes. 7 8 Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> 9 10 This software is free software; you can redistribute it and/or 11 modify it under the terms of the GNU General Public License as 12 published by the Free Software Foundation; either version 2 of 13 the License, or (at your option) any later version. 14 15 This software is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public 21 License along with this library; see the file LICENCE.txt 22 If not, write to the Free Software Foundation, Inc., 23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 24 25 -------- 26 27 To use this module, the easiest approach is to use the simplify function: 28 29 simplify(filename) 30 31 The more complicated approach involves first instantiating a Simplifier object: 32 33 simplifier = Simplifier() 34 35 Then, applying the simplifier to an AST tree: 36 37 module = compiler.parseFile(filename) 38 simplifier.process(module) 39 """ 40 41 from simplified import * 42 import compiler.ast 43 import os 44 45 class Simplifier(Visitor): 46 47 """ 48 A simplifying visitor for AST nodes. 49 50 Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, 51 Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, 52 Div, FloorDiv, For, From, Function, Getattr, Global, If, Import, 53 Invert, Keyword, Lambda, List, Mod, Module, Mul, Name, Not, Or, 54 Pass, Power, Print, Printnl, Raise, Return, Slice, Stmt, Sub, 55 Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub. 56 57 Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, 58 Exec, LeftShift, ListComp, ListCompFor, ListCompIf, RightShift, 59 Sliceobj, Yield. 60 """ 61 62 def __init__(self, builtins=0): 63 64 """ 65 Initialise the simplifier with the optional 'builtins' parameter 66 indicating whether the module contains the built-in classes and 67 functions. 68 """ 69 70 Visitor.__init__(self) 71 self.subprograms = [] # Subprograms outside the tree. 72 self.structures = [] # Structures/classes. 73 self.constants = {} # Constants. 74 self.current_subprograms = [] # Current subprograms being processed. 75 self.current_structures = [] # Current structures being processed. 76 self.within_class = 0 # Whether a class is being defined. 77 self.builtins = builtins # Whether the builtins are being processed. 78 79 # Convenience attributes. 80 81 self.subnames = {} 82 83 # For compiler package mechanisms. 84 85 self.visitor = self 86 87 def process(self, node, name): 88 result = self.dispatch(node, name) 89 result.simplifier = self 90 return result 91 92 def dispatch_or_none(self, node, *args): 93 if node is not None: 94 return self.dispatch(node, *args) 95 else: 96 return LoadName(node, name="None") 97 98 # Top-level transformation. 99 100 def visitModule(self, module, name=None): 101 102 """ 103 Process the given 'module', producing a Module object which contains the 104 resulting program nodes. If the optional 'name' is provided, the 'name' 105 attribute is set on the Module object using a value other than None. 106 """ 107 108 result = self.module = Module(module, 1, name=name) 109 module_code = self.dispatch(module.node) 110 111 # NOTE: Constant initialisation necessary for annotation but perhaps 112 # NOTE: redundant in the program. 113 114 init_code = [] 115 for value, constant in self.constants.items(): 116 init_code.append( 117 StoreAttr( 118 lvalue=LoadRef(ref=constant), 119 name="__class__", 120 expr=LoadName(name=constant.typename) 121 ) 122 ) 123 124 # NOTE: Hack to ensure correct initialisation of constants. 125 126 if self.builtins: 127 result.code = module_code + init_code 128 else: 129 result.code = init_code + module_code 130 return result 131 132 # Node transformations. 133 134 def visitAdd(self, add): 135 return self._visitBinary(add, "__add__", "__radd__") 136 137 def visitAnd(self, and_): 138 139 """ 140 Make a subprogram for the 'and_' node and record its contents inside the 141 subprogram. Convert... 142 143 And (test) 144 (test) 145 ... 146 147 ...to: 148 149 Subprogram -> Conditional (test) -> ReturnFromBlock ... 150 (else) -> Conditional (test) -> ReturnFromBlock ... 151 (else) -> ... 152 """ 153 154 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 155 self.current_subprograms.append(subprogram) 156 157 # In the subprogram, make instructions which store each operand, test 158 # for each operand's truth status, and if appropriate return from the 159 # subprogram with the value of the operand. 160 161 last = and_.nodes[-1] 162 results = nodes = [] 163 164 for node in and_.nodes: 165 expr = self.dispatch(node) 166 167 # Return from the subprogram where the test is not satisfied. 168 169 if node is not last: 170 nodes += [ 171 StoreTemp(expr=expr), 172 Conditional( 173 test=self._visitNot(LoadTemp()), 174 body=[ 175 ReturnFromBlock( 176 expr=LoadTemp() 177 ) 178 ], 179 else_=[ 180 ReleaseTemp() 181 # Subsequent operations go here! 182 ] 183 ) 184 ] 185 186 # Put subsequent operations in the else section of this conditional. 187 188 nodes = nodes[-1].else_ 189 190 # For the last operation, return the result. 191 192 else: 193 nodes.append(ReturnFromBlock(expr=expr)) 194 195 # Finish the subprogram definition. 196 197 subprogram.code = results 198 199 self.current_subprograms.pop() 200 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 201 202 # Make an invocation of the subprogram. 203 204 result = InvokeBlock(and_, 1, produces_result=1) 205 result.expr = LoadRef(ref=subprogram) 206 return result 207 208 # Assignments. 209 210 def visitAssAttr(self, assattr, in_sequence=0): 211 expr = self._visitAssNameOrAttr(assattr, in_sequence) 212 lvalue = self.dispatch(assattr.expr) 213 result = StoreAttr(assattr, 1, name=assattr.attrname, lvalue=lvalue, expr=expr) 214 return result 215 216 def visitAssign(self, assign): 217 result = Assign(assign, 1) 218 store = StoreTemp(expr=self.dispatch(assign.expr)) 219 release = ReleaseTemp() 220 result.code = [store] + self.dispatches(assign.nodes, 0) + [release] 221 return result 222 223 def visitAssList(self, asslist, in_sequence=0): 224 if not in_sequence: 225 expr = LoadTemp() 226 else: 227 expr = InvokeFunction(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) 228 result = Assign(asslist, 1) 229 store = StoreTemp(expr=InvokeFunction(asslist, expr=LoadAttr(name="__iter__", expr=expr))) 230 release = ReleaseTemp() 231 result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] 232 return result 233 234 visitAssTuple = visitAssList 235 236 def _visitAssNameOrAttr(self, node, in_sequence): 237 if not in_sequence: 238 return LoadTemp() 239 else: 240 return InvokeFunction(node, expr=LoadAttr(expr=LoadTemp(), name="next")) 241 242 def visitAssName(self, assname, in_sequence=0): 243 expr = self._visitAssNameOrAttr(assname, in_sequence) 244 result = StoreName(assname, 1, name=assname.name, expr=expr) 245 return result 246 247 augassign_methods = { 248 "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", 249 "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", 250 "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" 251 } 252 253 def visitAugAssign(self, augassign): 254 255 """ 256 Convert the augmented assignment... 257 258 AugAssign (node) -> Name | Getattr | Slice | Subscript 259 (op) 260 (expr) 261 262 ...to: 263 264 Assign (code) -> StoreTemp (expr) -> InvokeFunction (expr) -> LoadAttr (expr) -> <name> 265 (name) -> <op> 266 StoreName (name) -> <name> 267 (expr) -> LoadTemp 268 ReleaseTemp 269 """ 270 271 result = Assign(augassign, 1) 272 expr = self.dispatch(augassign.expr) 273 274 # Simple augmented assignment: name += expr 275 276 if isinstance(augassign.node, compiler.ast.Name): 277 result.code = [ 278 StoreTemp( 279 expr=InvokeFunction( # referenced below 280 augassign, 281 args=[expr], 282 star=None, 283 dstar=None, 284 expr=LoadAttr( 285 expr=self.dispatch(augassign.node), 286 name=self.augassign_methods[augassign.op] 287 ) 288 ) 289 ), 290 StoreName( 291 expr=LoadTemp(), 292 name=augassign.node.name), 293 ReleaseTemp() 294 ] 295 296 # Make nice annotations for the viewer. 297 298 augassign._op_call = result.code[0].expr 299 300 # Complicated augmented assignment: lvalue.attr += expr 301 302 elif isinstance(augassign.node, compiler.ast.Getattr): 303 304 # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) 305 306 result.code = [ 307 StoreTemp( 308 index="expr", 309 expr=self.dispatch(augassign.node.expr) 310 ), 311 StoreTemp( 312 expr=InvokeFunction( # referenced below 313 augassign, 314 args=[expr], star=None, dstar=None, 315 expr=LoadAttr( 316 expr=LoadAttr(augassign.node, 1, 317 expr=LoadTemp(index="expr"), 318 name=augassign.node.attrname 319 ), 320 name=self.augassign_methods[augassign.op] 321 ) 322 ) 323 ), 324 StoreAttr( 325 expr=LoadTemp(), 326 lvalue=LoadTemp(index="expr"), 327 name=augassign.node.attrname 328 ), 329 ReleaseTemp(index="expr"), 330 ReleaseTemp() 331 ] 332 333 # Make nice annotations for the viewer. 334 335 augassign._op_call = result.code[1].expr 336 337 # Complicated augassign using slices: lvalue[lower:upper] += expr 338 339 elif isinstance(augassign.node, compiler.ast.Slice): 340 341 # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) 342 343 result.code = [ 344 StoreTemp( 345 index="expr", 346 expr=self.dispatch(augassign.node.expr) 347 ), 348 StoreTemp( 349 index="lower", 350 expr=self.dispatch_or_none(augassign.node.lower) 351 ), 352 StoreTemp( 353 index="upper", 354 expr=self.dispatch_or_none(augassign.node.upper) 355 ), 356 StoreTemp( 357 expr=InvokeFunction( # referenced below 358 augassign, 359 args=[expr], star=None, dstar=None, 360 expr=LoadAttr( 361 expr=self._visitSlice( 362 augassign.node, 363 LoadTemp(index="expr"), 364 LoadTemp(index="lower"), 365 LoadTemp(index="upper"), 366 "OP_APPLY"), 367 name=self.augassign_methods[augassign.op] 368 ) 369 ) 370 ), 371 self._visitSlice( 372 augassign.node, 373 LoadTemp(index="expr"), 374 LoadTemp(index="lower"), 375 LoadTemp(index="upper"), 376 "OP_ASSIGN", 377 LoadTemp() 378 ), 379 ReleaseTemp(index="expr"), 380 ReleaseTemp(index="lower"), 381 ReleaseTemp(index="upper"), 382 ReleaseTemp() 383 ] 384 385 # Make nice annotations for the viewer. 386 387 augassign._op_call = result.code[3].expr 388 389 # Complicated augassign using subscripts: lvalue[subs] += expr 390 391 elif isinstance(augassign.node, compiler.ast.Subscript): 392 393 # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) 394 395 result.code = [ 396 StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), 397 StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), 398 StoreTemp( 399 expr=InvokeFunction( # referenced below 400 augassign, 401 args=[expr], star=None, dstar=None, 402 expr=LoadAttr( 403 expr=self._visitSubscript( 404 augassign.node, 405 LoadTemp(index="expr"), 406 LoadTemp(index="subs"), 407 "OP_APPLY" 408 ), 409 name=self.augassign_methods[augassign.op] 410 ) 411 ) 412 ), 413 self._visitSubscript( 414 augassign.node, 415 LoadTemp(index="expr"), 416 LoadTemp(index="subs"), 417 "OP_ASSIGN", 418 LoadTemp() 419 ), 420 ReleaseTemp(index="expr"), 421 ReleaseTemp(index="subs"), 422 ReleaseTemp() 423 ] 424 425 # Make nice annotations for the viewer. 426 427 augassign._op_call = result.code[2].expr 428 429 else: 430 raise NotImplementedError, augassign.node.__class__ 431 432 return result 433 434 def visitBreak(self, break_): 435 result = ReturnFromBlock(break_, 1) 436 return result 437 438 def visitCallFunc(self, callfunc): 439 result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) 440 if callfunc.star_args is not None: 441 result.star = self.dispatch(callfunc.star_args) 442 if callfunc.dstar_args is not None: 443 result.dstar = self.dispatch(callfunc.dstar_args) 444 result.expr = self.dispatch(callfunc.node) 445 return result 446 447 def visitClass(self, class_): 448 449 # Add "object" if the class is not "object" and has an empty bases list. 450 451 if class_.name != "object" and not class_.bases: 452 bases = [compiler.ast.Name("object")] 453 else: 454 bases = class_.bases 455 456 structure = Class(name=class_.name, module=self.module, bases=self.dispatches(bases)) 457 self.structures.append(structure) 458 within_class = self.within_class 459 self.within_class = 1 460 461 # Make a subprogram which initialises the class structure. 462 463 subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) 464 self.current_subprograms.append(subprogram) 465 self.current_structures.append(structure) # mostly for name construction 466 467 # The class is initialised using the code found inside. 468 469 subprogram.code = self.dispatch(class_.code) + [ReturnFromBlock()] 470 471 self.within_class = within_class 472 self.current_structures.pop() 473 self.current_subprograms.pop() 474 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 475 476 # Make a definition of the class associating it with a name. 477 478 result = Assign( 479 code=[ 480 StoreName(class_, 1, # defines the class 481 name=class_.name, 482 expr=LoadRef(ref=structure) 483 ), 484 InvokeBlock( 485 class_, 486 share_locals=0, # override the local sharing usually in InvokeBlock 487 expr=LoadRef(ref=subprogram) 488 ) 489 ] 490 ) 491 return result 492 493 comparison_methods = { 494 "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__", 495 ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None 496 } 497 498 def visitCompare(self, compare): 499 500 """ 501 Make a subprogram for the 'compare' node and record its contents inside 502 the subprogram. Convert... 503 504 Compare (expr) 505 (name/node) 506 ... 507 508 ...to: 509 510 InvokeBlock -> Subprogram -> Conditional (test) -> (body) 511 (else) -> Conditional (test) -> (body) 512 (else) -> ... 513 """ 514 515 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 516 self.current_subprograms.append(subprogram) 517 518 # In the subprogram, make instructions which invoke a method on the 519 # first operand of each operand pair and, if appropriate, return with 520 # the value from that method. 521 522 last = compare.ops[-1] 523 previous = self.dispatch(compare.expr) 524 results = nodes = [] 525 526 # For viewing purposes, record invocations on the AST node. 527 528 compare._ops = [] 529 530 for op in compare.ops: 531 op_name, node = op 532 expr = self.dispatch(node) 533 534 # Identify the operation and produce the appropriate method call. 535 536 method_name = self.comparison_methods[op_name] 537 if method_name: 538 invocation = InvokeFunction( 539 compare, 540 expr=LoadAttr( 541 expr=previous, 542 name=method_name), 543 args=[expr], 544 star=None, 545 dstar=None) 546 547 elif op_name == "is": 548 invocation = InvokeFunction( 549 compare, 550 expr=LoadName(name="__is__"), 551 args=[previous, expr], 552 star=None, 553 dstar=None) 554 555 elif op_name == "is not": 556 invocation = Not( 557 expr=InvokeFunction( 558 compare, 559 expr=LoadName(name="__is__"), 560 args=[previous, expr], 561 star=None, 562 dstar=None) 563 ) 564 else: 565 raise NotImplementedError, op_name 566 567 nodes.append(StoreTemp(expr=invocation)) 568 compare._ops.append(invocation) 569 570 # Return from the subprogram where the test is not satisfied. 571 572 if op is not last: 573 nodes.append( 574 Conditional( 575 test=self._visitNot(LoadTemp()), 576 body=[ 577 ReturnFromBlock(expr=LoadTemp()) 578 ], 579 else_=[ 580 ReleaseTemp() 581 # Subsequent operations go here! 582 ] 583 ) 584 ) 585 586 # Put subsequent operations in the else section of this conditional. 587 588 nodes = nodes[-1].else_ 589 590 # For the last operation, return the result. 591 592 else: 593 nodes.append( 594 ReturnFromBlock(expr=LoadTemp(release=1)) 595 ) 596 597 previous = expr 598 599 # Finish the subprogram definition. 600 601 subprogram.code = results 602 603 self.current_subprograms.pop() 604 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 605 606 # Make an invocation of the subprogram. 607 608 result = InvokeBlock(compare, 1, produces_result=1) 609 result.expr = LoadRef(ref=subprogram) 610 return result 611 612 def visitConst(self, const): 613 key = "%s-%s" % (const.value.__class__.__name__, const.value) 614 if not self.constants.has_key(key): 615 self.constants[key] = Constant(name=repr(const.value), value=const.value) 616 result = LoadRef(const, 1, ref=self.constants[key]) 617 return result 618 619 def visitContinue(self, continue_): 620 result = InvokeBlock(continue_, 1, 621 expr=LoadRef(ref=self.current_subprograms[-1]) 622 ) 623 return result 624 625 def visitDict(self, dict): 626 result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) 627 args = [] 628 for key, value in dict.items: 629 tuple = InvokeFunction(dict, expr=LoadName(name="tuple"), star=None, dstar=None) 630 tuple.set_args([self.dispatch(key), self.dispatch(value)]) 631 args.append(tuple) 632 result.set_args(args) 633 return result 634 635 def visitDiscard(self, discard): 636 return self.dispatch(discard.expr) 637 638 def visitDiv(self, div): 639 return self._visitBinary(div, "__div__", "__rdiv__") 640 641 def visitFloorDiv(self, floordiv): 642 return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") 643 644 def visitFor(self, for_): 645 646 """ 647 Make a subprogram for the 'for_' node and record its contents inside the 648 subprogram. Convert... 649 650 For (assign) 651 (body) 652 (else) 653 654 ...to: 655 656 Assign (assign #1) 657 Invoke -> Subprogram -> Try (body) -> (assign #2) 658 (body) 659 Invoke subprogram 660 (handler) -> ... 661 (else) -> ... 662 """ 663 664 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) 665 self.current_subprograms.append(subprogram) 666 667 # Always return from conditional sections/subprograms. 668 669 if for_.else_ is not None: 670 else_stmt = self.dispatch(for_.else_) + [ReturnFromBlock()] 671 else: 672 else_stmt = [ReturnFromBlock()] 673 674 # Wrap the assignment in a try...except statement. 675 # Inside the body, add a recursive invocation to the subprogram. 676 677 subprogram.code = [ 678 Try( 679 body=[ 680 Assign( 681 code=[ 682 StoreTemp(expr=InvokeFunction(for_, expr=LoadAttr(expr=LoadTemp(), name="next"))), 683 self.dispatch(for_.assign), 684 ReleaseTemp() 685 ]) 686 ] + self.dispatch(for_.body) + [ 687 InvokeBlock( 688 for_, 689 expr=LoadRef(ref=subprogram) 690 ) 691 ], 692 handler=[ 693 Conditional( 694 test=InvokeFunction( 695 for_, 696 expr=LoadName(name="isinstance"), 697 args=[LoadExc(), LoadName(name="StopIteration")], 698 star=None, 699 dstar=None), 700 body=else_stmt, 701 else_=[Raise(expr=LoadExc())] 702 ) 703 ], 704 else_=[], 705 finally_=[] 706 ), 707 ReturnFromBlock() 708 ] 709 710 # Finish the subprogram definition. 711 712 self.current_subprograms.pop() 713 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 714 715 # Obtain an iterator for the sequence involved. 716 # Then, make an invocation of the subprogram. 717 718 result = Assign(for_, 1, 719 code=[ 720 StoreTemp( 721 expr=InvokeFunction( 722 for_, 723 expr=LoadAttr( 724 name="__iter__", 725 expr=self.dispatch(for_.list) 726 ) 727 ) 728 ), 729 InvokeBlock(for_, expr=LoadRef(ref=subprogram)), 730 ReleaseTemp() 731 ] 732 ) 733 734 # Make nice annotations for the viewer. 735 736 for_._iter_call = result.code[0].expr 737 for_._next_call = subprogram.code[0].body[0].code[0].expr 738 739 return result 740 741 def visitFrom(self, from_): 742 result = Assign(from_, 1) 743 code = [] 744 _names = [] 745 code.append( 746 StoreTemp( 747 expr=Import(name=from_.modname, alias=1) 748 ) 749 ) 750 from_._modname = code[-1].expr 751 for name, alias in from_.names: 752 code.append( 753 StoreName( 754 expr=LoadAttr( 755 expr=LoadTemp(), 756 name=name), 757 name=(alias or name) 758 ) 759 ) 760 _names.append(code[-1].expr) 761 code.append(ReleaseTemp()) 762 result.code = code 763 from_._names = _names 764 return result 765 766 def _visitFunction(self, function, subprogram): 767 768 """ 769 A common function generator which transforms the given 'function' node 770 and initialises the given 'subprogram' appropriately. 771 """ 772 773 # Discover star and dstar parameters. 774 775 if function.flags & 4 != 0: 776 has_star = 1 777 else: 778 has_star = 0 779 if function.flags & 8 != 0: 780 has_dstar = 1 781 else: 782 has_dstar = 0 783 784 # Discover the number of defaults and positional parameters. 785 786 ndefaults = len(function.defaults) 787 npositional = len(function.argnames) - has_star - has_dstar 788 789 # Produce star and dstar parameters with appropriate defaults. 790 791 if has_star: 792 star = ( 793 function.argnames[npositional], 794 self.dispatch(compiler.ast.List([])) 795 ) 796 else: 797 star = None 798 if has_dstar: 799 dstar = ( 800 function.argnames[npositional + has_star], 801 self.dispatch(compiler.ast.Dict([])) 802 ) 803 else: 804 dstar = None 805 806 params = [] 807 for i in range(0, npositional - ndefaults): 808 params.append((function.argnames[i], None)) 809 810 # Process defaults. 811 812 for i in range(0, ndefaults): 813 default = function.defaults[i] 814 if default is not None: 815 params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) 816 else: 817 params.append((function.argnames[npositional - ndefaults + i], None)) 818 819 subprogram.params = params 820 subprogram.star = star 821 subprogram.dstar = dstar 822 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 823 824 def visitFunction(self, function): 825 826 """ 827 Make a subprogram for the 'function' and record it outside the main 828 tree. Produce something like the following: 829 830 StoreName (name) 831 (expr) -> LoadRef (ref) -> Subprogram (params) 832 (star) 833 (dstar) 834 """ 835 836 # NOTE: Making the actual subprogram defining, too. 837 838 subprogram = Subprogram(function, 1, name=function.name, module=self.module, structures=self.current_structures[:], 839 internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) 840 841 self.current_subprograms.append(subprogram) 842 within_class = self.within_class 843 self.within_class = 0 844 845 subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] 846 847 self.within_class = within_class 848 self.current_subprograms.pop() 849 self._visitFunction(function, subprogram) 850 851 # Make a definition of the function associating it with a name. 852 853 result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) 854 return result 855 856 def visitGetattr(self, getattr): 857 result = LoadAttr(getattr, 1, 858 name=getattr.attrname, 859 expr=self.dispatch(getattr.expr) 860 ) 861 return result 862 863 def visitGlobal(self, global_): 864 result = Global(global_, 1, 865 names=global_.names 866 ) 867 return result 868 869 def visitIf(self, if_): 870 871 """ 872 Make conditionals for each test from an 'if_' AST node, adding the body 873 and putting each subsequent test as part of the conditional's else 874 section. 875 876 Convert... 877 878 If (test/body) 879 (test/body) 880 ... 881 (else/body) 882 883 ...to: 884 885 Conditional (test) -> (body) 886 (else) -> Conditional (test) -> (body) 887 (else) -> ... 888 """ 889 890 891 results = nodes = [] 892 893 # Produce something like... 894 # expr.__bool__() ? body 895 896 first = 1 897 for compare, stmt in if_.tests: 898 899 # Set the first as the defining node. 900 901 test = Conditional(if_, first, 902 test=InvokeFunction( 903 if_, 904 expr=LoadAttr( 905 expr=self.dispatch(compare), 906 name="__bool__" 907 ), 908 ) 909 ) 910 test.body = self.dispatch(stmt) 911 nodes.append(test) 912 nodes = test.else_ = [] 913 first = 0 914 915 # Add the compound statement from any else clause to the end. 916 917 if if_.else_ is not None: 918 nodes += self.dispatch(if_.else_) 919 920 result = results[0] 921 return result 922 923 def visitImport(self, import_): 924 result = Assign(import_, 1) 925 code = [] 926 _names = [] 927 for path, alias in import_.names: 928 importer = Import(name=path, alias=alias) 929 top = alias or path.split(".")[0] 930 code.append(StoreName(expr=importer, name=top)) 931 _names.append(code[-1].expr) 932 result.code = code 933 import_._names = _names 934 return result 935 936 def visitInvert(self, invert): 937 return self._visitUnary(invert, "__invert__") 938 939 def visitKeyword(self, keyword): 940 result = Keyword(keyword, 1, 941 name=keyword.name, 942 expr=self.dispatch(keyword.expr) 943 ) 944 return result 945 946 def visitLambda(self, lambda_): 947 948 # Make a subprogram for the function and record it outside the main 949 # tree. 950 # NOTE: Making the actual subprogram defining, too. 951 952 subprogram = Subprogram(lambda_, 1, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) 953 self.current_subprograms.append(subprogram) 954 subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] 955 self.current_subprograms.pop() 956 self._visitFunction(lambda_, subprogram) 957 958 # Get the subprogram reference to the lambda. 959 960 return LoadRef(lambda_, 1, ref=subprogram) 961 962 def visitList(self, list): 963 return self._visitBuiltin(list, "list") 964 965 def visitMod(self, mod): 966 return self._visitBinary(mod, "__mod__", "__rmod__") 967 968 def visitMul(self, mul): 969 return self._visitBinary(mul, "__mul__", "__rmul__") 970 971 def visitName(self, name): 972 result = LoadName(name, 1, name=name.name) 973 return result 974 975 def _visitNot(self, expr, not_=None): 976 invocation = InvokeFunction( 977 not_, # NOTE: May need a real original node. 978 expr=LoadAttr( 979 expr=expr, 980 name="__bool__" 981 ), 982 ) 983 if not_ is not None: 984 result = Not(not_, 1, expr=invocation) 985 else: 986 result = Not(expr=invocation) 987 return result 988 989 def visitNot(self, not_): 990 return self._visitNot(self.dispatch(not_.expr), not_) 991 992 def visitOr(self, or_): 993 994 """ 995 Make a subprogram for the 'or_' node and record its contents inside the 996 subprogram. Convert... 997 998 Or (test) 999 (test) 1000 ... 1001 1002 ...to: 1003 1004 Subprogram -> Conditional (test) -> ReturnFromBlock ... 1005 (else) -> Conditional (test) -> ReturnFromBlock ... 1006 (else) -> ... 1007 """ 1008 1009 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 1010 self.current_subprograms.append(subprogram) 1011 1012 # In the subprogram, make instructions which store each operand, test 1013 # for each operand's truth status, and if appropriate return from the 1014 # subprogram with the value of the operand. 1015 1016 last = or_.nodes[-1] 1017 results = nodes = [] 1018 1019 for node in or_.nodes: 1020 expr = self.dispatch(node) 1021 1022 # Return from the subprogram where the test is satisfied. 1023 1024 if node is not last: 1025 nodes.append(StoreTemp(expr=expr)) 1026 invocation = InvokeFunction(or_, expr=LoadAttr(expr=LoadTemp(), name="__bool__")) 1027 test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) 1028 nodes.append(test) 1029 1030 # Put subsequent operations in the else section of this conditional. 1031 1032 nodes = test.else_ = [ReleaseTemp()] 1033 1034 # For the last operation, return the result. 1035 1036 else: 1037 nodes.append( 1038 ReturnFromBlock(expr=expr) 1039 ) 1040 1041 # Finish the subprogram definition. 1042 1043 subprogram.code = results 1044 1045 self.current_subprograms.pop() 1046 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1047 1048 # Make an invocation of the subprogram. 1049 1050 result = InvokeBlock(or_, 1, produces_result=1) 1051 result.expr = LoadRef(ref=subprogram) 1052 return result 1053 1054 def visitPass(self, pass_): 1055 return Pass(pass_, 1) 1056 1057 def visitPower(self, power): 1058 return self._visitBinary(power, "__pow__", "__rpow__") 1059 1060 def visitPrint(self, print_): 1061 1062 """ 1063 Convert... 1064 1065 Print (dest) -> 1066 (nodes) 1067 1068 ...to: 1069 1070 StoreTemp (index) -> "print" 1071 (expr) -> LoadAttr (expr) -> (dest) 1072 (name) -> "write" 1073 InvokeFunction (expr) -> LoadTemp (index) -> "print" 1074 (args) -> [(node)] 1075 ReleaseTemp (index) -> "print" 1076 """ 1077 1078 if print_.dest is not None: 1079 dest = self.dispatch(print_.dest) 1080 else: 1081 dest = self.dispatch(compiler.ast.Name("stdout")) 1082 1083 result = Assign(print_, 1, 1084 code=[ 1085 StoreTemp( 1086 index="print", 1087 expr=LoadAttr( 1088 expr=dest, 1089 name="write" 1090 ) 1091 ) 1092 ] 1093 ) 1094 1095 for node in print_.nodes: 1096 result.code.append( 1097 InvokeFunction( 1098 print_, 1099 expr=LoadTemp(index="print"), 1100 args=[self.dispatch(node)], 1101 star=None, 1102 dstar=None 1103 ) 1104 ) 1105 1106 result.code.append( 1107 ReleaseTemp(index="print") 1108 ) 1109 1110 return result 1111 1112 def visitPrintnl(self, printnl): 1113 result = self.visitPrint(printnl) 1114 result.code.insert( 1115 len(result.code) - 1, 1116 InvokeFunction( 1117 printnl, 1118 expr=LoadTemp(index="print"), 1119 args=[self.dispatch(compiler.ast.Const("\n"))], 1120 star=None, 1121 dstar=None 1122 ) 1123 ) 1124 return result 1125 1126 def visitRaise(self, raise_): 1127 result = Raise(raise_, 1) 1128 if raise_.expr2 is None: 1129 result.expr = self.dispatch(raise_.expr1) 1130 else: 1131 result.expr = InvokeFunction( 1132 raise_, 1133 expr=self.dispatch(raise_.expr1), 1134 args=[self.dispatch(raise_.expr2)], 1135 star=None, 1136 dstar=None 1137 ) 1138 if raise_.expr3 is not None: 1139 result.traceback = self.dispatch(raise_.expr3) 1140 else: 1141 result.traceback = None 1142 return result 1143 1144 def visitReturn(self, return_): 1145 result = ReturnFromFunction(return_, 1, 1146 expr=self.dispatch(return_.value) 1147 ) 1148 return result 1149 1150 def _visitSlice(self, slice, expr, lower, upper, flags, value=None): 1151 if flags == "OP_ASSIGN": 1152 result = InvokeFunction(slice, 1, 1153 expr=LoadAttr( 1154 expr=expr, 1155 name="__setslice__" 1156 ), 1157 star=None, 1158 dstar=None, 1159 args=[lower, upper, value] 1160 ) 1161 elif flags == "OP_APPLY": 1162 args = [] 1163 result = InvokeFunction(slice, 1, 1164 expr=LoadAttr( 1165 expr=expr, 1166 name="__getslice__" 1167 ), 1168 star=None, 1169 dstar=None, 1170 args=[lower, upper] 1171 ) 1172 elif flags == "OP_DELETE": 1173 args = [] 1174 result = InvokeFunction(slice, 1, 1175 expr=LoadAttr( 1176 expr=expr, 1177 name="__delslice__" 1178 ), 1179 star=None, 1180 dstar=None, 1181 args=[lower, upper] 1182 ) 1183 else: 1184 raise NotImplementedError, flags 1185 1186 return result 1187 1188 def visitSlice(self, slice, in_sequence=0): 1189 return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), 1190 self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) 1191 1192 def visitStmt(self, stmt): 1193 return self.dispatches(stmt.nodes) 1194 1195 def visitSub(self, sub): 1196 return self._visitBinary(sub, "__sub__", "__rsub__") 1197 1198 def _visitSubscript(self, subscript, expr, subs, flags, value=None): 1199 if flags == "OP_ASSIGN": 1200 result = InvokeFunction(subscript, 1, 1201 expr=LoadAttr( 1202 expr=expr, 1203 name="__setitem__" 1204 ), 1205 star=None, 1206 dstar=None, 1207 args=[subs, value] 1208 ) 1209 elif flags == "OP_APPLY": 1210 args = [] 1211 result = InvokeFunction(subscript, 1, 1212 expr=LoadAttr( 1213 expr=expr, 1214 name="__getitem__" 1215 ), 1216 star=None, 1217 dstar=None, 1218 args=[subs] 1219 ) 1220 elif flags == "OP_DELETE": 1221 args = [] 1222 result = InvokeFunction(subscript, 1, 1223 expr=LoadAttr( 1224 expr=expr, 1225 name="__delitem__" 1226 ), 1227 star=None, 1228 dstar=None, 1229 args=[subs] 1230 ) 1231 else: 1232 raise NotImplementedError, flags 1233 1234 return result 1235 1236 def _visitSubscriptSubs(self, node, subs): 1237 if len(subs) == 1: 1238 return self.dispatch(subs[0]) 1239 else: 1240 return InvokeFunction(node, 1, 1241 expr=LoadName(name="tuple"), 1242 args=self.dispatches(subs), 1243 star=None, 1244 dstar=None 1245 ) 1246 1247 def visitSubscript(self, subscript, in_sequence=0): 1248 return self._visitSubscript( 1249 subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, 1250 self._visitAssNameOrAttr(subscript, in_sequence) 1251 ) 1252 1253 def visitTryExcept(self, tryexcept): 1254 1255 """ 1256 Make conditionals for each handler associated with a 'tryexcept' node. 1257 1258 Convert... 1259 1260 TryExcept (body) 1261 (else) 1262 (spec/assign/stmt) 1263 ... 1264 1265 ...to: 1266 1267 Try (body) 1268 (else) 1269 (handler) -> Conditional (test) -> (stmt) 1270 (body) -> ... 1271 (else) -> Conditional (test) -> (stmt) 1272 (body) -> ... 1273 (else) -> ... 1274 """ 1275 1276 result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) 1277 1278 if tryexcept.body is not None: 1279 result.body = self.dispatch(tryexcept.body) 1280 if tryexcept.else_ is not None: 1281 result.else_ = self.dispatch(tryexcept.else_) 1282 1283 results = nodes = [] 1284 catch_all = 0 1285 1286 for spec, assign, stmt in tryexcept.handlers: 1287 1288 # If no specification exists, produce an unconditional block. 1289 1290 if spec is None: 1291 nodes += self.dispatch(stmt) 1292 catch_all = 1 1293 1294 # Produce an exception value check. 1295 1296 else: 1297 test = Conditional( 1298 isolate_test=1, 1299 test=CheckExc(expr=LoadExc(), choices=self._visitTryExcept(spec)) 1300 ) 1301 test.body = [] 1302 1303 if assign is not None: 1304 test.body.append( 1305 Assign( 1306 code=[ 1307 StoreTemp(expr=LoadExc()), 1308 self.dispatch(assign), 1309 ReleaseTemp() 1310 ] 1311 ) 1312 ) 1313 1314 test.body += self.dispatch(stmt) 1315 nodes.append(test) 1316 nodes = test.else_ = [] 1317 1318 # Add a raise operation to deal with unhandled exceptions. 1319 1320 if not catch_all: 1321 nodes.append( 1322 Raise( 1323 expr=LoadExc()) 1324 ) 1325 1326 result.handler = results 1327 return result 1328 1329 def _visitTryExcept(self, spec): 1330 1331 "Return a list of nodes for the given exception type 'spec'." 1332 1333 if isinstance(spec, compiler.ast.Tuple): 1334 nodes = [] 1335 for node in spec.nodes: 1336 nodes += self._visitTryExcept(node) 1337 else: 1338 nodes = [self.dispatch(spec)] 1339 return nodes 1340 1341 def visitTryFinally(self, tryfinally): 1342 result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) 1343 if tryfinally.body is not None: 1344 result.body = self.dispatch(tryfinally.body) 1345 if tryfinally.final is not None: 1346 result.finally_ = self.dispatch(tryfinally.final) 1347 return result 1348 1349 def visitTuple(self, tuple): 1350 return self._visitBuiltin(tuple, "tuple") 1351 1352 def visitUnaryAdd(self, unaryadd): 1353 return self._visitUnary(unaryadd, "__pos__") 1354 1355 def visitUnarySub(self, unarysub): 1356 return self._visitUnary(unarysub, "__neg__") 1357 1358 def visitWhile(self, while_): 1359 1360 """ 1361 Make a subprogram for the 'while' node and record its contents inside the 1362 subprogram. Convert... 1363 1364 While (test) -> (body) 1365 (else) 1366 1367 ...to: 1368 1369 Subprogram -> Conditional (test) -> (body) -> Invoke subprogram 1370 (else) -> Conditional (test) -> ReturnFromBlock ... 1371 (else) -> ... 1372 """ 1373 1374 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) 1375 self.current_subprograms.append(subprogram) 1376 1377 # Include a conditional statement in the subprogram. 1378 # Inside the conditional, add a recursive invocation to the subprogram 1379 # if the test condition was satisfied. 1380 # Return within the main section of the loop. 1381 1382 test = Conditional( 1383 test=InvokeFunction( 1384 while_, 1385 expr=LoadAttr( 1386 expr=self.dispatch(while_.test), 1387 name="__bool__"), 1388 ), 1389 body=self.dispatch(while_.body) + [ 1390 InvokeBlock( 1391 while_, 1392 expr=LoadRef(ref=subprogram) 1393 ), 1394 ReturnFromBlock() 1395 ], 1396 else_=[] 1397 ) 1398 1399 # Provide the else section, if present, along with an explicit return. 1400 1401 if while_.else_ is not None: 1402 test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] 1403 1404 # Finish the subprogram definition. 1405 1406 subprogram.code = [test] 1407 1408 self.current_subprograms.pop() 1409 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1410 1411 # Make an invocation of the subprogram. 1412 1413 result = InvokeBlock(while_, 1, 1414 expr=LoadRef(ref=subprogram) 1415 ) 1416 1417 # Make nice annotations for the viewer. 1418 1419 while_._test_call = subprogram.code[0].test 1420 1421 return result 1422 1423 # Convenience methods. 1424 1425 def _visitBinary(self, binary, left_name, right_name): 1426 1427 """ 1428 Emulate the current mechanisms by producing nodes as follows: 1429 1430 InvokeBlock -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) 1431 (else) 1432 (handler) -> Conditional (test) -> CheckExc (expr) -> LoadExc 1433 (choices) -> LoadName TypeError 1434 (body) -> ReturnFromBlock (expr) -> y.__radd__(x) 1435 (else) 1436 """ 1437 1438 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 1439 self.current_subprograms.append(subprogram) 1440 1441 subprogram.code = [ 1442 Try(binary, 1, 1443 body=[ 1444 ReturnFromBlock( 1445 expr=InvokeFunction( 1446 binary, 1447 expr=LoadAttr(expr=self.dispatch(binary.left), name=left_name), 1448 args=[self.dispatch(binary.right)], 1449 star=None, 1450 dstar=None) 1451 ) 1452 ], 1453 else_=[], 1454 finally_=[], 1455 handler=[ 1456 Conditional( 1457 test=CheckExc(expr=LoadExc(), choices=[LoadName(name="TypeError")]), 1458 body=[ 1459 ReturnFromBlock( 1460 expr=InvokeFunction( 1461 binary, 1462 expr=LoadAttr(expr=self.dispatch(binary.right), name=right_name), 1463 args=[self.dispatch(binary.left)], 1464 star=None, 1465 dstar=None) 1466 ) 1467 ], 1468 else_=[] 1469 ) 1470 ] 1471 ) 1472 ] 1473 1474 self.current_subprograms.pop() 1475 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1476 1477 result = InvokeBlock( 1478 binary, 1479 produces_result=1, 1480 expr=LoadRef(ref=subprogram) 1481 ) 1482 1483 # Make nice annotations for the viewer. 1484 1485 binary._left_call = subprogram.code[0].body[0].expr 1486 binary._right_call = subprogram.code[0].handler[0].body[0].expr 1487 1488 return result 1489 1490 def _visitBuiltin(self, builtin, name): 1491 result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) 1492 return result 1493 1494 def _visitUnary(self, unary, name): 1495 result = InvokeFunction(unary, 1, 1496 expr=LoadAttr( 1497 expr=self.dispatch(unary.expr), 1498 name=name 1499 ) 1500 ) 1501 1502 # Make nice annotations for the viewer. 1503 1504 unary._unary_call = result 1505 1506 return result 1507 1508 # Convenience functions. 1509 1510 def simplify(filename, builtins=0, module_name=None): 1511 1512 """ 1513 Simplify the module stored in the file with the given 'filename'. 1514 1515 If the optional 'builtins' parameter is set to a true value (the default 1516 being a false value), then the module is considered as the builtins module. 1517 """ 1518 1519 simplifier = Simplifier(builtins) 1520 module = compiler.parseFile(filename) 1521 compiler.misc.set_filename(filename, module) 1522 if builtins: 1523 name = module_name or "__builtins__" 1524 else: 1525 path, ext = os.path.splitext(filename) 1526 path, name = os.path.split(path) 1527 name = module_name or name 1528 simplified = simplifier.process(module, name) 1529 return simplified 1530 1531 # vim: tabstop=4 expandtab shiftwidth=4