blob: 846dc32ba149ce3205d207fb1790f8341407d3c5 [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)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000025 if op == SET_LINENO and i > 0: print # Extra blank line
26 if i == lasti: print '-->',
Guido van Rossum217a5fa1990-12-26 15:40:07 +000027 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
Guido van Rossume65cce51993-11-08 15:05:21 +000071opname = [''] * 256
72for op in range(256): opname[op] = '<' + `op` + '>'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000073
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)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000102def_op('UNARY_INVERT', 15)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000103
104def_op('BINARY_MULTIPLY', 20)
105def_op('BINARY_DIVIDE', 21)
106def_op('BINARY_MODULO', 22)
107def_op('BINARY_ADD', 23)
108def_op('BINARY_SUBTRACT', 24)
109def_op('BINARY_SUBSCR', 25)
110def_op('BINARY_CALL', 26)
111
112def_op('SLICE+0', 30)
113def_op('SLICE+1', 31)
114def_op('SLICE+2', 32)
115def_op('SLICE+3', 33)
116
117def_op('STORE_SLICE+0', 40)
118def_op('STORE_SLICE+1', 41)
119def_op('STORE_SLICE+2', 42)
120def_op('STORE_SLICE+3', 43)
121
122def_op('DELETE_SLICE+0', 50)
123def_op('DELETE_SLICE+1', 51)
124def_op('DELETE_SLICE+2', 52)
125def_op('DELETE_SLICE+3', 53)
126
127def_op('STORE_SUBSCR', 60)
128def_op('DELETE_SUBSCR', 61)
129
130def_op('PRINT_EXPR', 70)
131def_op('PRINT_ITEM', 71)
132def_op('PRINT_NEWLINE', 72)
133
134def_op('BREAK_LOOP', 80)
135def_op('RAISE_EXCEPTION', 81)
136def_op('LOAD_LOCALS', 82)
137def_op('RETURN_VALUE', 83)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138def_op('LOAD_GLOBALS', 84)
139def_op('EXEC_STMT', 85)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000140def_op('BUILD_FUNCTION', 86)
141def_op('POP_BLOCK', 87)
142def_op('END_FINALLY', 88)
143def_op('BUILD_CLASS', 89)
144
145HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
146
147name_op('STORE_NAME', 90) # Index in name list
148name_op('DELETE_NAME', 91) # ""
149def_op('UNPACK_TUPLE', 92) # Number of tuple items
150def_op('UNPACK_LIST', 93) # Number of list items
Guido van Rossuma594fab1991-12-16 13:09:28 +0000151def_op('UNPACK_ARG', 94) # Number of arguments expected
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000152name_op('STORE_ATTR', 95) # Index in name list
Guido van Rossuma594fab1991-12-16 13:09:28 +0000153name_op('DELETE_ATTR', 96) # ""
154name_op('STORE_GLOBAL', 97) # ""
155name_op('DELETE_GLOBAL', 98) # ""
Guido van Rossum8379ed51993-03-30 19:13:03 +0000156name_op('UNPACK_VARARG', 99) # Minimal number of arguments
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000157
158def_op('LOAD_CONST', 100) # Index in const list
159hasconst.append(100)
160name_op('LOAD_NAME', 101) # Index in name list
161def_op('BUILD_TUPLE', 102) # Number of tuple items
162def_op('BUILD_LIST', 103) # Number of list items
163def_op('BUILD_MAP', 104) # Always zero for now
164name_op('LOAD_ATTR', 105) # Index in name list
165def_op('COMPARE_OP', 106) # Comparison operator
166name_op('IMPORT_NAME', 107) # Index in name list
167name_op('IMPORT_FROM', 108) # Index in name list
168
169jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
170jrel_op('JUMP_IF_FALSE', 111) # ""
171jrel_op('JUMP_IF_TRUE', 112) # ""
172jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
173jrel_op('FOR_LOOP', 114) # Number of bytes to skip
174
Guido van Rossuma594fab1991-12-16 13:09:28 +0000175name_op('LOAD_LOCAL', 115) # Index in name list
176name_op('LOAD_GLOBAL', 116) # Index in name list
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177def_op('SET_FUNC_ARGS', 117) # Argcount
Guido van Rossuma594fab1991-12-16 13:09:28 +0000178
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000179jrel_op('SETUP_LOOP', 120) # Distance to target address
180jrel_op('SETUP_EXCEPT', 121) # ""
181jrel_op('SETUP_FINALLY', 122) # ""
182
Guido van Rossum8379ed51993-03-30 19:13:03 +0000183def_op('RESERVE_FAST', 123) # Number of local variables
184hasconst.append(123)
185def_op('LOAD_FAST', 124) # Local variable number
186def_op('STORE_FAST', 125) # Local variable number
187def_op('DELETE_FAST', 126) # Local variable number
188
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000189def_op('SET_LINENO', 127) # Current line number
190SET_LINENO = 127