blob: fe57fb954cf652e571a1aeb7dc2ef3660bb14f90 [file] [log] [blame]
Guido van Rossum217a5fa1990-12-26 15:40:07 +00001# Disassembler
2
3import sys
4import string
5
6def dis():
7 tb = sys.last_traceback
8 while tb.tb_next: tb = tb.tb_next
9 distb(tb)
10
11def distb(tb):
12 disassemble(tb.tb_frame.f_code, tb.tb_lasti)
13
14def disco(co):
15 disassemble(co, -1)
16
17def disassemble(co, lasti):
18 code = co.co_code
19 labels = findlabels(code)
20 n = len(code)
21 i = 0
22 while i < n:
23 c = code[i]
24 op = ord(c)
25 if op = SET_LINENO and i > 0: print # Extra blank line
26 if i = lasti: print '-->',
27 else: print ' ',
28 if i in labels: print '>>',
29 else: print ' ',
30 print string.rjust(`i`, 4),
31 print string.ljust(opname[op], 15),
32 i = i+1
33 if op >= HAVE_ARGUMENT:
34 oparg = ord(code[i]) + ord(code[i+1])*256
35 i = i+2
36 print string.rjust(`oparg`, 5),
37 if op in hasconst:
38 print '(' + `co.co_consts[oparg]` + ')',
39 elif op in hasname:
40 print '(' + co.co_names[oparg] + ')',
41 elif op in hasjrel:
42 print '(to ' + `i + oparg` + ')',
43 print
44
45def findlabels(code):
46 labels = []
47 n = len(code)
48 i = 0
49 while i < n:
50 c = code[i]
51 op = ord(c)
52 i = i+1
53 if op >= HAVE_ARGUMENT:
54 oparg = ord(code[i]) + ord(code[i+1])*256
55 i = i+2
56 label = -1
57 if op in hasjrel:
58 label = i+oparg
59 elif op in hasjabs:
60 label = oparg
61 if label >= 0:
62 if label not in labels:
63 labels.append(label)
64 return labels
65
66hasconst = []
67hasname = []
68hasjrel = []
69hasjabs = []
70
71opname = range(256)
72for op in opname: opname[op] = '<' + `op` + '>'
73
74def def_op(name, op):
75 opname[op] = name
76
77def name_op(name, op):
78 opname[op] = name
79 hasname.append(op)
80
81def jrel_op(name, op):
82 opname[op] = name
83 hasjrel.append(op)
84
85def jabs_op(name, op):
86 opname[op] = name
87 hasjabs.append(op)
88
89# Instruction opcodes for compiled code
90
91def_op('STOP_CODE', 0)
92def_op('POP_TOP', 1)
93def_op('ROT_TWO', 2)
94def_op('ROT_THREE', 3)
95def_op('DUP_TOP', 4)
96
97def_op('UNARY_POSITIVE', 10)
98def_op('UNARY_NEGATIVE', 11)
99def_op('UNARY_NOT', 12)
100def_op('UNARY_CONVERT', 13)
101def_op('UNARY_CALL', 14)
102
103def_op('BINARY_MULTIPLY', 20)
104def_op('BINARY_DIVIDE', 21)
105def_op('BINARY_MODULO', 22)
106def_op('BINARY_ADD', 23)
107def_op('BINARY_SUBTRACT', 24)
108def_op('BINARY_SUBSCR', 25)
109def_op('BINARY_CALL', 26)
110
111def_op('SLICE+0', 30)
112def_op('SLICE+1', 31)
113def_op('SLICE+2', 32)
114def_op('SLICE+3', 33)
115
116def_op('STORE_SLICE+0', 40)
117def_op('STORE_SLICE+1', 41)
118def_op('STORE_SLICE+2', 42)
119def_op('STORE_SLICE+3', 43)
120
121def_op('DELETE_SLICE+0', 50)
122def_op('DELETE_SLICE+1', 51)
123def_op('DELETE_SLICE+2', 52)
124def_op('DELETE_SLICE+3', 53)
125
126def_op('STORE_SUBSCR', 60)
127def_op('DELETE_SUBSCR', 61)
128
129def_op('PRINT_EXPR', 70)
130def_op('PRINT_ITEM', 71)
131def_op('PRINT_NEWLINE', 72)
132
133def_op('BREAK_LOOP', 80)
134def_op('RAISE_EXCEPTION', 81)
135def_op('LOAD_LOCALS', 82)
136def_op('RETURN_VALUE', 83)
137def_op('REQUIRE_ARGS', 84)
138def_op('REFUSE_ARGS', 85)
139def_op('BUILD_FUNCTION', 86)
140def_op('POP_BLOCK', 87)
141def_op('END_FINALLY', 88)
142def_op('BUILD_CLASS', 89)
143
144HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
145
146name_op('STORE_NAME', 90) # Index in name list
147name_op('DELETE_NAME', 91) # ""
148def_op('UNPACK_TUPLE', 92) # Number of tuple items
149def_op('UNPACK_LIST', 93) # Number of list items
150# unused: 94
151name_op('STORE_ATTR', 95) # Index in name list
152name_op('DELETE_ATTR', 96) # ""
153
154def_op('LOAD_CONST', 100) # Index in const list
155hasconst.append(100)
156name_op('LOAD_NAME', 101) # Index in name list
157def_op('BUILD_TUPLE', 102) # Number of tuple items
158def_op('BUILD_LIST', 103) # Number of list items
159def_op('BUILD_MAP', 104) # Always zero for now
160name_op('LOAD_ATTR', 105) # Index in name list
161def_op('COMPARE_OP', 106) # Comparison operator
162name_op('IMPORT_NAME', 107) # Index in name list
163name_op('IMPORT_FROM', 108) # Index in name list
164
165jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
166jrel_op('JUMP_IF_FALSE', 111) # ""
167jrel_op('JUMP_IF_TRUE', 112) # ""
168jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
169jrel_op('FOR_LOOP', 114) # Number of bytes to skip
170
171jrel_op('SETUP_LOOP', 120) # Distance to target address
172jrel_op('SETUP_EXCEPT', 121) # ""
173jrel_op('SETUP_FINALLY', 122) # ""
174
175def_op('SET_LINENO', 127) # Current line number
176SET_LINENO = 127