annotate Lib/compiler/pyassem.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents edfed0e32ced
children d234dd21374a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
1 """A flow graph representation for Python bytecode"""
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3 import dis
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 36926
diff changeset
4 import types
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
5 import sys
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
6
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
7 from compiler import misc
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
8 from compiler.consts \
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
9 import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
10
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
11 class FlowGraph:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
12 def __init__(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
13 self.current = self.entry = Block()
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
14 self.exit = Block("exit")
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
15 self.blocks = misc.Set()
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
16 self.blocks.add(self.entry)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
17 self.blocks.add(self.exit)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
18
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
19 def startBlock(self, block):
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
20 if self._debug:
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
21 if self.current:
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
22 print "end", repr(self.current)
17453
f3b6ac128de9 pyassem.py:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17449
diff changeset
23 print " next", self.current.next
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
24 print " prev", self.current.prev
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
25 print " ", self.current.get_children()
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
26 print repr(block)
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
27 self.current = block
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
28
17453
f3b6ac128de9 pyassem.py:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17449
diff changeset
29 def nextBlock(self, block=None):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
30 # XXX think we need to specify when there is implicit transfer
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
31 # from one block to the next. might be better to represent this
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
32 # with explicit JUMP_ABSOLUTE instructions that are optimized
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
33 # out when they are unnecessary.
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
34 #
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
35 # I think this strategy works: each block has a child
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
36 # designated as "next" which is returned as the last of the
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
37 # children. because the nodes in a graph are emitted in
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
38 # reverse post order, the "next" block will always be emitted
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
39 # immediately after its parent.
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
40 # Worry: maintaining this invariant could be tricky
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
41 if block is None:
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
42 block = self.newBlock()
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
43
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
44 # Note: If the current block ends with an unconditional control
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
45 # transfer, then it is techically incorrect to add an implicit
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
46 # transfer to the block graph. Doing so results in code generation
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
47 # for unreachable blocks. That doesn't appear to be very common
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
48 # with Python code and since the built-in compiler doesn't optimize
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
49 # it out we don't either.
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
50 self.current.addNext(block)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
51 self.startBlock(block)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
52
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
53 def newBlock(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
54 b = Block()
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
55 self.blocks.add(b)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
56 return b
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
57
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
58 def startExitBlock(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
59 self.startBlock(self.exit)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
60
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
61 _debug = 0
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
62
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
63 def _enable_debug(self):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
64 self._debug = 1
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
65
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
66 def _disable_debug(self):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
67 self._debug = 0
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
68
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
69 def emit(self, *inst):
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
70 if self._debug:
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
71 print "\t", inst
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
72 if len(inst) == 2 and isinstance(inst[1], Block):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
73 self.current.addOutEdge(inst[1])
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
74 self.current.emit(inst)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
75
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
76 def getBlocksInOrder(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
77 """Return the blocks in reverse postorder
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
78
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
79 i.e. each node appears before all of its successors
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
80 """
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
81 order = order_blocks(self.entry, self.exit)
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
82 return order
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
83
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
84 def getBlocks(self):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
85 return self.blocks.elements()
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
86
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
87 def getRoot(self):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
88 """Return nodes appropriate for use with dominator"""
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
89 return self.entry
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
90
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
91 def getContainedGraphs(self):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
92 l = []
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
93 for b in self.getBlocks():
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
94 l.extend(b.getContainedGraphs())
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
95 return l
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
96
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
97
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
98 def order_blocks(start_block, exit_block):
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
99 """Order blocks so that they are emitted in the right order"""
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
100 # Rules:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
101 # - when a block has a next block, the next block must be emitted just after
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
102 # - when a block has followers (relative jumps), it must be emitted before
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
103 # them
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
104 # - all reachable blocks must be emitted
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
105 order = []
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
106
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
107 # Find all the blocks to be emitted.
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
108 remaining = set()
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
109 todo = [start_block]
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
110 while todo:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
111 b = todo.pop()
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
112 if b in remaining:
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
113 continue
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
114 remaining.add(b)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
115 for c in b.get_children():
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
116 if c not in remaining:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
117 todo.append(c)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
118
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
119 # A block is dominated by another block if that block must be emitted
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
120 # before it.
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
121 dominators = {}
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
122 for b in remaining:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
123 if __debug__ and b.next:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
124 assert b is b.next[0].prev[0], (b, b.next)
51920
ef8fe9088696 Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51404
diff changeset
125 # Make sure every block appears in dominators, even if no
ef8fe9088696 Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51404
diff changeset
126 # other block must precede it.
ef8fe9088696 Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51404
diff changeset
127 dominators.setdefault(b, set())
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
128 # preceeding blocks dominate following blocks
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
129 for c in b.get_followers():
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
130 while 1:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
131 dominators.setdefault(c, set()).add(b)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
132 # Any block that has a next pointer leading to c is also
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
133 # dominated because the whole chain will be emitted at once.
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
134 # Walk backwards and add them all.
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
135 if c.prev and c.prev[0] is not b:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
136 c = c.prev[0]
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
137 else:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
138 break
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
139
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
140 def find_next():
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
141 # Find a block that can be emitted next.
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
142 for b in remaining:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
143 for c in dominators[b]:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
144 if c in remaining:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
145 break # can't emit yet, dominated by a remaining block
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
146 else:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
147 return b
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
148 assert 0, 'circular dependency, cannot find next block'
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
149
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
150 b = start_block
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
151 while 1:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
152 order.append(b)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
153 remaining.discard(b)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
154 if b.next:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
155 b = b.next[0]
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
156 continue
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
157 elif b is not exit_block and not b.has_unconditional_transfer():
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
158 order.append(exit_block)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
159 if not remaining:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
160 break
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
161 b = find_next()
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
162 return order
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
163
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
164
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
165 class Block:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
166 _count = 0
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
167
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
168 def __init__(self, label=''):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
169 self.insts = []
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
170 self.outEdges = set()
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
171 self.label = label
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
172 self.bid = Block._count
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
173 self.next = []
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
174 self.prev = []
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
175 Block._count = Block._count + 1
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
176
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
177 def __repr__(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
178 if self.label:
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
179 return "<block %s id=%d>" % (self.label, self.bid)
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
180 else:
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
181 return "<block id=%d>" % (self.bid)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
182
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
183 def __str__(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
184 insts = map(str, self.insts)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
185 return "<block %s %d:\n%s>" % (self.label, self.bid,
23710
44b9977d4848 Remove uses of string module and stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents: 20547
diff changeset
186 '\n'.join(insts))
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
187
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
188 def emit(self, inst):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
189 op = inst[0]
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
190 self.insts.append(inst)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
191
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
192 def getInstructions(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
193 return self.insts
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
194
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
195 def addOutEdge(self, block):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
196 self.outEdges.add(block)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
197
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
198 def addNext(self, block):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
199 self.next.append(block)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
200 assert len(self.next) == 1, map(str, self.next)
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
201 block.prev.append(self)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
202 assert len(block.prev) == 1, map(str, block.prev)
15380
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
203
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
204 _uncond_transfer = ('RETURN_VALUE', 'RAISE_VARARGS',
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
205 'JUMP_ABSOLUTE', 'JUMP_FORWARD', 'CONTINUE_LOOP',
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
206 )
15380
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
207
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
208 def has_unconditional_transfer(self):
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
209 """Returns True if there is an unconditional transfer to an other block
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
210 at the end of this block. This means there is no risk for the bytecode
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
211 executer to go past this block's bytecode."""
15380
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
212 try:
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
213 op, arg = self.insts[-1]
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
214 except (IndexError, ValueError):
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
215 return
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
216 return op in self._uncond_transfer
15380
bb006ad00d9e move pruneNext method to correct object (doh!)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15379
diff changeset
217
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
218 def get_children(self):
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
219 return list(self.outEdges) + self.next
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
220
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
221 def get_followers(self):
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
222 """Get the whole list of followers, including the next block."""
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
223 followers = set(self.next)
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
224 # Blocks that must be emitted *after* this one, because of
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
225 # bytecode offsets (e.g. relative jumps) pointing to them.
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
226 for inst in self.insts:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
227 if inst[0] in PyFlowGraph.hasjrel:
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
228 followers.add(inst[1])
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
229 return followers
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
230
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
231 def getContainedGraphs(self):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
232 """Return all graphs contained within this block.
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
233
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
234 For example, a MAKE_FUNCTION block will contain a reference to
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
235 the graph for the function body.
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
236 """
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
237 contained = []
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
238 for inst in self.insts:
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
239 if len(inst) == 1:
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
240 continue
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
241 op = inst[1]
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
242 if hasattr(op, 'graph'):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
243 contained.append(op.graph)
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
244 return contained
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
245
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
246 # flags for code objects
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
247
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
248 # the FlowGraph is transformed in place; it exists in one of these states
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
249 RAW = "RAW"
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
250 FLAT = "FLAT"
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
251 CONV = "CONV"
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
252 DONE = "DONE"
11808
cf8fa92ebf3d the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11791
diff changeset
253
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
254 class PyFlowGraph(FlowGraph):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
255 super_init = FlowGraph.__init__
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
256
19558
02251b048bbb Modify name conversion to be (hopefully) a bit more efficient.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19532
diff changeset
257 def __init__(self, name, filename, args=(), optimized=0, klass=None):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
258 self.super_init()
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
259 self.name = name
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
260 self.filename = filename
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
261 self.docstring = None
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
262 self.args = args # XXX
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
263 self.argcount = getArgCount(args)
19558
02251b048bbb Modify name conversion to be (hopefully) a bit more efficient.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19532
diff changeset
264 self.klass = klass
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
265 if optimized:
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
266 self.flags = CO_OPTIMIZED | CO_NEWLOCALS
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
267 else:
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
268 self.flags = 0
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
269 self.consts = []
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
270 self.names = []
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
271 # Free variables found by the symbol table scan, including
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
272 # variables used only in nested scopes, are included here.
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
273 self.freevars = []
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
274 self.cellvars = []
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
275 # The closure list is used to track the order of cell
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
276 # variables and free variables in the resulting code object.
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
277 # The offsets used by LOAD_CLOSURE/LOAD_DEREF refer to both
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
278 # kinds of variables.
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
279 self.closure = []
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
280 self.varnames = list(args) or []
11808
cf8fa92ebf3d the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11791
diff changeset
281 for i in range(len(self.varnames)):
cf8fa92ebf3d the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11791
diff changeset
282 var = self.varnames[i]
cf8fa92ebf3d the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11791
diff changeset
283 if isinstance(var, TupleArg):
cf8fa92ebf3d the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11791
diff changeset
284 self.varnames[i] = var.getName()
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
285 self.stage = RAW
11791
fd4814b59b73 fix argcount generation for arg lists containing tuple unpacks
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11787
diff changeset
286
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
287 def setDocstring(self, doc):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
288 self.docstring = doc
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
289
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
290 def setFlag(self, flag):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
291 self.flags = self.flags | flag
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
292 if flag == CO_VARARGS:
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
293 self.argcount = self.argcount - 1
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
294
19920
63930159ca3c Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19619
diff changeset
295 def checkFlag(self, flag):
63930159ca3c Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19619
diff changeset
296 if self.flags & flag:
63930159ca3c Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19619
diff changeset
297 return 1
63930159ca3c Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19619
diff changeset
298
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
299 def setFreeVars(self, names):
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
300 self.freevars = list(names)
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
301
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
302 def setCellVars(self, names):
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
303 self.cellvars = names
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
304
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
305 def getCode(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
306 """Get a Python code object"""
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
307 assert self.stage == RAW
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
308 self.computeStackDepth()
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
309 self.flattenGraph()
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
310 assert self.stage == FLAT
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
311 self.convertArgs()
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
312 assert self.stage == CONV
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
313 self.makeByteCode()
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
314 assert self.stage == DONE
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26979
diff changeset
315 return self.newCodeObject()
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
316
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
317 def dump(self, io=None):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
318 if io:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
319 save = sys.stdout
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
320 sys.stdout = io
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
321 pc = 0
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
322 for t in self.insts:
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
323 opname = t[0]
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
324 if opname == "SET_LINENO":
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
325 print
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
326 if len(t) == 1:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
327 print "\t", "%3d" % pc, opname
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
328 pc = pc + 1
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
329 else:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
330 print "\t", "%3d" % pc, opname, t[1]
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
331 pc = pc + 3
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
332 if io:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
333 sys.stdout = save
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
334
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
335 def computeStackDepth(self):
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
336 """Compute the max stack depth.
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
337
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
338 Approach is to compute the stack effect of each basic block.
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
339 Then find the path through the code with the largest total
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
340 effect.
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
341 """
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
342 depth = {}
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
343 exit = None
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
344 for b in self.getBlocks():
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
345 depth[b] = findDepth(b.getInstructions())
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
346
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
347 seen = {}
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
348
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
349 def max_depth(b, d):
50257
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 44034
diff changeset
350 if b in seen:
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
351 return d
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
352 seen[b] = 1
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
353 d = d + depth[b]
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
354 children = b.get_children()
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
355 if children:
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
356 return max([max_depth(c, d) for c in children])
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
357 else:
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
358 if not b.label == "exit":
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
359 return max_depth(self.exit, d)
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
360 else:
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
361 return d
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
362
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
363 self.stacksize = max_depth(self.entry, 0)
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
364
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
365 def flattenGraph(self):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
366 """Arrange the blocks in order and resolve jumps"""
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
367 assert self.stage == RAW
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
368 self.insts = insts = []
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
369 pc = 0
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
370 begin = {}
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
371 end = {}
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
372 for b in self.getBlocksInOrder():
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
373 begin[b] = pc
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
374 for inst in b.getInstructions():
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
375 insts.append(inst)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
376 if len(inst) == 1:
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
377 pc = pc + 1
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
378 elif inst[0] != "SET_LINENO":
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
379 # arg takes 2 bytes
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
380 pc = pc + 3
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
381 end[b] = pc
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
382 pc = 0
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
383 for i in range(len(insts)):
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
384 inst = insts[i]
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
385 if len(inst) == 1:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
386 pc = pc + 1
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
387 elif inst[0] != "SET_LINENO":
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
388 pc = pc + 3
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
389 opname = inst[0]
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
390 if opname in self.hasjrel:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
391 oparg = inst[1]
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
392 offset = begin[oparg] - pc
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
393 insts[i] = opname, offset
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
394 elif opname in self.hasjabs:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
395 insts[i] = opname, begin[inst[1]]
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
396 self.stage = FLAT
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
397
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
398 hasjrel = set()
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
399 for i in dis.hasjrel:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
400 hasjrel.add(dis.opname[i])
51404
42faa8054c3d Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 50257
diff changeset
401 hasjabs = set()
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
402 for i in dis.hasjabs:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
403 hasjabs.add(dis.opname[i])
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
404
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
405 def convertArgs(self):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
406 """Convert arguments from symbolic to concrete form"""
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
407 assert self.stage == FLAT
17408
694b8c3f25e7 Make sure the docstring is always entered as the first element in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15380
diff changeset
408 self.consts.insert(0, self.docstring)
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
409 self.sort_cellvars()
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
410 for i in range(len(self.insts)):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
411 t = self.insts[i]
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
412 if len(t) == 2:
19558
02251b048bbb Modify name conversion to be (hopefully) a bit more efficient.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19532
diff changeset
413 opname, oparg = t
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
414 conv = self._converters.get(opname, None)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
415 if conv:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
416 self.insts[i] = opname, conv(self, oparg)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
417 self.stage = CONV
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
418
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
419 def sort_cellvars(self):
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
420 """Sort cellvars in the order of varnames and prune from freevars.
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
421 """
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
422 cells = {}
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
423 for name in self.cellvars:
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
424 cells[name] = 1
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
425 self.cellvars = [name for name in self.varnames
50257
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 44034
diff changeset
426 if name in cells]
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
427 for name in self.cellvars:
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
428 del cells[name]
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
429 self.cellvars = self.cellvars + cells.keys()
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
430 self.closure = self.cellvars + self.freevars
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
431
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
432 def _lookupName(self, name, list):
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
433 """Return index of name in list, appending if necessary
19558
02251b048bbb Modify name conversion to be (hopefully) a bit more efficient.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19532
diff changeset
434
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
435 This routine uses a list instead of a dictionary, because a
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
436 dictionary can't store two different keys if the keys have the
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
437 same value but different types, e.g. 2 and 2L. The compiler
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
438 must treat these two separately, so it does an explicit type
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
439 comparison before comparing the values.
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
440 """
15205
04e76e08e10f Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15185
diff changeset
441 t = type(name)
04e76e08e10f Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15185
diff changeset
442 for i in range(len(list)):
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
443 if t == type(list[i]) and list[i] == name:
11809
e2b633991e01 changes to _lookupName
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11808
diff changeset
444 return i
e2b633991e01 changes to _lookupName
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11808
diff changeset
445 end = len(list)
e2b633991e01 changes to _lookupName
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11808
diff changeset
446 list.append(name)
e2b633991e01 changes to _lookupName
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11808
diff changeset
447 return end
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
448
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
449 _converters = {}
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
450 def _convert_LOAD_CONST(self, arg):
15379
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
451 if hasattr(arg, 'getCode'):
7a82f84290f7 Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15205
diff changeset
452 arg = arg.getCode()
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
453 return self._lookupName(arg, self.consts)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
454
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
455 def _convert_LOAD_FAST(self, arg):
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
456 self._lookupName(arg, self.names)
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
457 return self._lookupName(arg, self.varnames)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
458 _convert_STORE_FAST = _convert_LOAD_FAST
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
459 _convert_DELETE_FAST = _convert_LOAD_FAST
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
460
19532
202d8dd65eb5 XXX_NAME ops should affect varnames
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18065
diff changeset
461 def _convert_LOAD_NAME(self, arg):
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
462 if self.klass is None:
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
463 self._lookupName(arg, self.varnames)
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
464 return self._lookupName(arg, self.names)
19532
202d8dd65eb5 XXX_NAME ops should affect varnames
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18065
diff changeset
465
202d8dd65eb5 XXX_NAME ops should affect varnames
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18065
diff changeset
466 def _convert_NAME(self, arg):
19619
5663b883ea5d Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19572
diff changeset
467 if self.klass is None:
5663b883ea5d Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19572
diff changeset
468 self._lookupName(arg, self.varnames)
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
469 return self._lookupName(arg, self.names)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
470 _convert_STORE_NAME = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
471 _convert_DELETE_NAME = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
472 _convert_IMPORT_NAME = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
473 _convert_IMPORT_FROM = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
474 _convert_STORE_ATTR = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
475 _convert_LOAD_ATTR = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
476 _convert_DELETE_ATTR = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
477 _convert_LOAD_GLOBAL = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
478 _convert_STORE_GLOBAL = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
479 _convert_DELETE_GLOBAL = _convert_NAME
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
480
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
481 def _convert_DEREF(self, arg):
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
482 self._lookupName(arg, self.names)
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
483 self._lookupName(arg, self.varnames)
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
484 return self._lookupName(arg, self.closure)
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
485 _convert_LOAD_DEREF = _convert_DEREF
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
486 _convert_STORE_DEREF = _convert_DEREF
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
487
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
488 def _convert_LOAD_CLOSURE(self, arg):
19563
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
489 self._lookupName(arg, self.varnames)
d9ecb2170354 Undo change from list to dict for handling varnames, consts, etc.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19558
diff changeset
490 return self._lookupName(arg, self.closure)
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
491
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
492 _cmp = list(dis.cmp_op)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
493 def _convert_COMPARE_OP(self, arg):
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
494 return self._cmp.index(arg)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
495
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
496 # similarly for other opcodes...
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
497
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
498 for name, obj in locals().items():
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
499 if name[:9] == "_convert_":
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
500 opname = name[9:]
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
501 _converters[opname] = obj
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
502 del name, obj, opname
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
503
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
504 def makeByteCode(self):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
505 assert self.stage == CONV
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
506 self.lnotab = lnotab = LineAddrTable()
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
507 for t in self.insts:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
508 opname = t[0]
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
509 if len(t) == 1:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
510 lnotab.addCode(self.opnum[opname])
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
511 else:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
512 oparg = t[1]
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
513 if opname == "SET_LINENO":
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
514 lnotab.nextLine(oparg)
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
515 continue
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
516 hi, lo = twobyte(oparg)
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
517 try:
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
518 lnotab.addCode(self.opnum[opname], lo, hi)
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
519 except ValueError:
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
520 print opname, oparg
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
521 print self.opnum[opname], lo, hi
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
522 raise
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
523 self.stage = DONE
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
524
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
525 opnum = {}
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
526 for num in range(len(dis.opname)):
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
527 opnum[dis.opname[num]] = num
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
528 del num
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
529
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
530 def newCodeObject(self):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
531 assert self.stage == DONE
19920
63930159ca3c Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19619
diff changeset
532 if (self.flags & CO_NEWLOCALS) == 0:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
533 nlocals = 0
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
534 else:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
535 nlocals = len(self.varnames)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
536 argcount = self.argcount
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
537 if self.flags & CO_VARKEYWORDS:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
538 argcount = argcount - 1
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 36926
diff changeset
539 return types.CodeType(argcount, nlocals, self.stacksize, self.flags,
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
540 self.lnotab.getCode(), self.getConsts(),
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
541 tuple(self.names), tuple(self.varnames),
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
542 self.filename, self.name, self.lnotab.firstline,
17436
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
543 self.lnotab.getTable(), tuple(self.freevars),
98f69245bb69 Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17408
diff changeset
544 tuple(self.cellvars))
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
545
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
546 def getConsts(self):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
547 """Return a tuple for the const slot of the code object
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
548
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
549 Must convert references to code (MAKE_FUNCTION) to code
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
550 objects recursively.
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
551 """
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
552 l = []
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
553 for elt in self.consts:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
554 if isinstance(elt, PyFlowGraph):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
555 elt = elt.getCode()
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
556 l.append(elt)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
557 return tuple(l)
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
558
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
559 def isJump(opname):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
560 if opname[:4] == 'JUMP':
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
561 return 1
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
562
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
563 class TupleArg:
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
564 """Helper for marking func defs with nested tuples in arglist"""
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
565 def __init__(self, count, names):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
566 self.count = count
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
567 self.names = names
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
568 def __repr__(self):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
569 return "TupleArg(%s, %s)" % (self.count, self.names)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
570 def getName(self):
17449
51c8ce1a2e25 Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17436
diff changeset
571 return ".%d" % self.count
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
572
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
573 def getArgCount(args):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
574 argcount = len(args)
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
575 if args:
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
576 for arg in args:
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
577 if isinstance(arg, TupleArg):
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
578 numNames = len(misc.flatten(arg.names))
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
579 argcount = argcount - numNames
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
580 return argcount
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
581
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
582 def twobyte(val):
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
583 """Convert an int argument into high and low bytes"""
35946
b2eb1589c22b No need for types, use isinstance
Neal Norwitz <nnorwitz@gmail.com>
parents: 35742
diff changeset
584 assert isinstance(val, int)
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
585 return divmod(val, 256)
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
586
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
587 class LineAddrTable:
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
588 """lnotab
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
589
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
590 This class builds the lnotab, which is documented in compile.c.
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
591 Here's a brief recap:
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
592
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
593 For each SET_LINENO instruction after the first one, two bytes are
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
594 added to lnotab. (In some cases, multiple two-byte entries are
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
595 added.) The first byte is the distance in bytes between the
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
596 instruction for the last SET_LINENO and the current SET_LINENO.
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
597 The second byte is offset in line numbers. If either offset is
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
598 greater than 255, multiple two-byte entries are added -- see
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
599 compile.c for the delicate details.
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
600 """
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
601
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
602 def __init__(self):
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
603 self.code = []
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
604 self.codeOffset = 0
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
605 self.firstline = 0
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
606 self.lastline = 0
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
607 self.lastoff = 0
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
608 self.lnotab = []
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
609
11924
4b8bc9ab8175 revise arguments for addCode method on lnotab. take several numbers
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11829
diff changeset
610 def addCode(self, *args):
4b8bc9ab8175 revise arguments for addCode method on lnotab. take several numbers
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11829
diff changeset
611 for arg in args:
4b8bc9ab8175 revise arguments for addCode method on lnotab. take several numbers
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11829
diff changeset
612 self.code.append(chr(arg))
4b8bc9ab8175 revise arguments for addCode method on lnotab. take several numbers
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11829
diff changeset
613 self.codeOffset = self.codeOffset + len(args)
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
614
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
615 def nextLine(self, lineno):
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
616 if self.firstline == 0:
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
617 self.firstline = lineno
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
618 self.lastline = lineno
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
619 else:
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
620 # compute deltas
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
621 addr = self.codeOffset - self.lastoff
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
622 line = lineno - self.lastline
14362
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
623 # Python assumes that lineno always increases with
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
624 # increasing bytecode address (lnotab is unsigned char).
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
625 # Depending on when SET_LINENO instructions are emitted
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
626 # this is not always true. Consider the code:
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
627 # a = (1,
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
628 # b)
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
629 # In the bytecode stream, the assignment to "a" occurs
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
630 # after the loading of "b". This works with the C Python
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
631 # compiler because it only generates a SET_LINENO instruction
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
632 # for the assignment.
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
633 if line >= 0:
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
634 push = self.lnotab.append
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
635 while addr > 255:
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
636 push(255); push(0)
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
637 addr -= 255
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
638 while line > 255:
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
639 push(addr); push(255)
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
640 line -= 255
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
641 addr = 0
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
642 if addr > 0 or line > 0:
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17453
diff changeset
643 push(addr); push(line)
14362
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
644 self.lastline = lineno
a665bdc4922f patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13941
diff changeset
645 self.lastoff = self.codeOffset
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
646
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
647 def getCode(self):
23710
44b9977d4848 Remove uses of string module and stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents: 20547
diff changeset
648 return ''.join(self.code)
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
649
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
650 def getTable(self):
23710
44b9977d4848 Remove uses of string module and stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents: 20547
diff changeset
651 return ''.join(map(chr, self.lnotab))
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
652
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
653 class StackDepthTracker:
12009
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
654 # XXX 1. need to keep track of stack depth on jumps
127f70f59b02 complete rewrite
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11924
diff changeset
655 # XXX 2. at least partly as a result, this code is broken
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
656
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
657 def findDepth(self, insts, debug=0):
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
658 depth = 0
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
659 maxDepth = 0
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
660 for i in insts:
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
661 opname = i[0]
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
662 if debug:
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
663 print i,
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
664 delta = self.effect.get(opname, None)
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
665 if delta is not None:
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
666 depth = depth + delta
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
667 else:
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
668 # now check patterns
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
669 for pat, pat_delta in self.patterns:
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
670 if opname[:len(pat)] == pat:
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
671 delta = pat_delta
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
672 depth = depth + delta
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
673 break
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
674 # if we still haven't found a match
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
675 if delta is None:
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
676 meth = getattr(self, opname, None)
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
677 if meth is not None:
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
678 depth = depth + meth(i[1])
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
679 if depth > maxDepth:
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
680 maxDepth = depth
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
681 if debug:
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
682 print depth, maxDepth
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
683 return maxDepth
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
684
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
685 effect = {
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
686 'POP_TOP': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
687 'DUP_TOP': 1,
58250
edfed0e32ced Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58229
diff changeset
688 'LIST_APPEND': -1,
edfed0e32ced Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58229
diff changeset
689 'SET_ADD': -1,
edfed0e32ced Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58229
diff changeset
690 'MAP_ADD': -2,
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
691 'SLICE+1': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
692 'SLICE+2': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
693 'SLICE+3': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
694 'STORE_SLICE+0': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
695 'STORE_SLICE+1': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
696 'STORE_SLICE+2': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
697 'STORE_SLICE+3': -3,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
698 'DELETE_SLICE+0': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
699 'DELETE_SLICE+1': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
700 'DELETE_SLICE+2': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
701 'DELETE_SLICE+3': -3,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
702 'STORE_SUBSCR': -3,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
703 'DELETE_SUBSCR': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
704 # PRINT_EXPR?
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
705 'PRINT_ITEM': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
706 'RETURN_VALUE': -1,
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23710
diff changeset
707 'YIELD_VALUE': -1,
19619
5663b883ea5d Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19572
diff changeset
708 'EXEC_STMT': -3,
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
709 'BUILD_CLASS': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
710 'STORE_NAME': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
711 'STORE_ATTR': -2,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
712 'DELETE_ATTR': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
713 'STORE_GLOBAL': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
714 'BUILD_MAP': 1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
715 'COMPARE_OP': -1,
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
716 'STORE_FAST': -1,
15185
100beebde9df Fix SF bug #116263: support for from .. import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14362
diff changeset
717 'IMPORT_STAR': -1,
36820
c1c46210b4b7 Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents: 36709
diff changeset
718 'IMPORT_NAME': -1,
15185
100beebde9df Fix SF bug #116263: support for from .. import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14362
diff changeset
719 'IMPORT_FROM': 1,
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
720 'LOAD_ATTR': 0, # unlike other loads
19572
476eb7dceeec Improve stack depth computation for try/except and try/finally
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19565
diff changeset
721 # close enough...
476eb7dceeec Improve stack depth computation for try/except and try/finally
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19565
diff changeset
722 'SETUP_EXCEPT': 3,
476eb7dceeec Improve stack depth computation for try/except and try/finally
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19565
diff changeset
723 'SETUP_FINALLY': 3,
19619
5663b883ea5d Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19572
diff changeset
724 'FOR_ITER': 1,
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36824
diff changeset
725 'WITH_CLEANUP': -1,
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
726 }
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
727 # use pattern match
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
728 patterns = [
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
729 ('BINARY_', -1),
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
730 ('LOAD_', 1),
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
731 ]
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
732
13941
d56dd1530ce1 Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents: 12563
diff changeset
733 def UNPACK_SEQUENCE(self, count):
19565
3fa29cf7319d Fix off-by-one errors in code to find depth of stack.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19563
diff changeset
734 return count-1
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
735 def BUILD_TUPLE(self, count):
19565
3fa29cf7319d Fix off-by-one errors in code to find depth of stack.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19563
diff changeset
736 return -count+1
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
737 def BUILD_LIST(self, count):
19565
3fa29cf7319d Fix off-by-one errors in code to find depth of stack.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19563
diff changeset
738 return -count+1
58229
c2fdf25329ff Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 51920
diff changeset
739 def BUILD_SET(self, count):
c2fdf25329ff Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 51920
diff changeset
740 return -count+1
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
741 def CALL_FUNCTION(self, argc):
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
742 hi, lo = divmod(argc, 256)
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
743 return -(lo + hi * 2)
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
744 def CALL_FUNCTION_VAR(self, argc):
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
745 return self.CALL_FUNCTION(argc)-1
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
746 def CALL_FUNCTION_KW(self, argc):
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
747 return self.CALL_FUNCTION(argc)-1
12563
b87d75798d81 patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12009
diff changeset
748 def CALL_FUNCTION_VAR_KW(self, argc):
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
749 return self.CALL_FUNCTION(argc)-2
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
750 def MAKE_FUNCTION(self, argc):
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
751 return -argc
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
752 def MAKE_CLOSURE(self, argc):
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
753 # XXX need to account for free variables too!
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
754 return -argc
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
755 def BUILD_SLICE(self, argc):
11829
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
756 if argc == 2:
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
757 return -1
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
758 elif argc == 3:
ecdc78148811 satisfy the tabnanny (thanks to MH for noticing the problem)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11809
diff changeset
759 return -2
20495
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
760 def DUP_TOPX(self, argc):
68fefa982c5c Vastly improved stacksize calculation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19920
diff changeset
761 return argc
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20495
diff changeset
762
11755
57fb3501ab22 split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
763 findDepth = StackDepthTracker().findDepth