blob: dc5ce4679dead14fa6c2036e6a9e04c6e6b17358 [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` + ')',
Guido van Rossum934a4ce1996-09-12 17:39:36 +000043 elif op in haslocal:
44 print '(' + co.co_varnames[oparg] + ')',
Guido van Rossum217a5fa1990-12-26 15:40:07 +000045 print
46
47def findlabels(code):
48 labels = []
49 n = len(code)
50 i = 0
51 while i < n:
52 c = code[i]
53 op = ord(c)
54 i = i+1
55 if op >= HAVE_ARGUMENT:
56 oparg = ord(code[i]) + ord(code[i+1])*256
57 i = i+2
58 label = -1
59 if op in hasjrel:
60 label = i+oparg
61 elif op in hasjabs:
62 label = oparg
63 if label >= 0:
64 if label not in labels:
65 labels.append(label)
66 return labels
67
68hasconst = []
69hasname = []
70hasjrel = []
71hasjabs = []
Guido van Rossum934a4ce1996-09-12 17:39:36 +000072haslocal = []
Guido van Rossum217a5fa1990-12-26 15:40:07 +000073
Guido van Rossume65cce51993-11-08 15:05:21 +000074opname = [''] * 256
75for op in range(256): opname[op] = '<' + `op` + '>'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000076
77def def_op(name, op):
78 opname[op] = name
79
80def name_op(name, op):
81 opname[op] = name
82 hasname.append(op)
83
84def jrel_op(name, op):
85 opname[op] = name
86 hasjrel.append(op)
87
88def jabs_op(name, op):
89 opname[op] = name
90 hasjabs.append(op)
91
92# Instruction opcodes for compiled code
93
94def_op('STOP_CODE', 0)
95def_op('POP_TOP', 1)
96def_op('ROT_TWO', 2)
97def_op('ROT_THREE', 3)
98def_op('DUP_TOP', 4)
99
100def_op('UNARY_POSITIVE', 10)
101def_op('UNARY_NEGATIVE', 11)
102def_op('UNARY_NOT', 12)
103def_op('UNARY_CONVERT', 13)
104def_op('UNARY_CALL', 14)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000105def_op('UNARY_INVERT', 15)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000106
Guido van Rossum6e21ceb1996-07-21 02:16:53 +0000107def_op('BINARY_POWER', 19)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000108def_op('BINARY_MULTIPLY', 20)
109def_op('BINARY_DIVIDE', 21)
110def_op('BINARY_MODULO', 22)
111def_op('BINARY_ADD', 23)
112def_op('BINARY_SUBTRACT', 24)
113def_op('BINARY_SUBSCR', 25)
114def_op('BINARY_CALL', 26)
115
116def_op('SLICE+0', 30)
117def_op('SLICE+1', 31)
118def_op('SLICE+2', 32)
119def_op('SLICE+3', 33)
120
121def_op('STORE_SLICE+0', 40)
122def_op('STORE_SLICE+1', 41)
123def_op('STORE_SLICE+2', 42)
124def_op('STORE_SLICE+3', 43)
125
126def_op('DELETE_SLICE+0', 50)
127def_op('DELETE_SLICE+1', 51)
128def_op('DELETE_SLICE+2', 52)
129def_op('DELETE_SLICE+3', 53)
130
131def_op('STORE_SUBSCR', 60)
132def_op('DELETE_SUBSCR', 61)
133
134def_op('PRINT_EXPR', 70)
135def_op('PRINT_ITEM', 71)
136def_op('PRINT_NEWLINE', 72)
137
138def_op('BREAK_LOOP', 80)
139def_op('RAISE_EXCEPTION', 81)
140def_op('LOAD_LOCALS', 82)
141def_op('RETURN_VALUE', 83)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142def_op('EXEC_STMT', 85)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000143def_op('BUILD_FUNCTION', 86)
144def_op('POP_BLOCK', 87)
145def_op('END_FINALLY', 88)
146def_op('BUILD_CLASS', 89)
147
148HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
149
150name_op('STORE_NAME', 90) # Index in name list
151name_op('DELETE_NAME', 91) # ""
152def_op('UNPACK_TUPLE', 92) # Number of tuple items
153def_op('UNPACK_LIST', 93) # Number of list items
Guido van Rossuma594fab1991-12-16 13:09:28 +0000154def_op('UNPACK_ARG', 94) # Number of arguments expected
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000155name_op('STORE_ATTR', 95) # Index in name list
Guido van Rossuma594fab1991-12-16 13:09:28 +0000156name_op('DELETE_ATTR', 96) # ""
157name_op('STORE_GLOBAL', 97) # ""
158name_op('DELETE_GLOBAL', 98) # ""
Guido van Rossum8379ed51993-03-30 19:13:03 +0000159name_op('UNPACK_VARARG', 99) # Minimal number of arguments
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000160
161def_op('LOAD_CONST', 100) # Index in const list
162hasconst.append(100)
163name_op('LOAD_NAME', 101) # Index in name list
164def_op('BUILD_TUPLE', 102) # Number of tuple items
165def_op('BUILD_LIST', 103) # Number of list items
166def_op('BUILD_MAP', 104) # Always zero for now
167name_op('LOAD_ATTR', 105) # Index in name list
168def_op('COMPARE_OP', 106) # Comparison operator
169name_op('IMPORT_NAME', 107) # Index in name list
170name_op('IMPORT_FROM', 108) # Index in name list
171
172jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
173jrel_op('JUMP_IF_FALSE', 111) # ""
174jrel_op('JUMP_IF_TRUE', 112) # ""
175jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
176jrel_op('FOR_LOOP', 114) # Number of bytes to skip
177
Guido van Rossuma594fab1991-12-16 13:09:28 +0000178name_op('LOAD_LOCAL', 115) # Index in name list
179name_op('LOAD_GLOBAL', 116) # Index in name list
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180def_op('SET_FUNC_ARGS', 117) # Argcount
Guido van Rossuma594fab1991-12-16 13:09:28 +0000181
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000182jrel_op('SETUP_LOOP', 120) # Distance to target address
183jrel_op('SETUP_EXCEPT', 121) # ""
184jrel_op('SETUP_FINALLY', 122) # ""
185
Guido van Rossum8379ed51993-03-30 19:13:03 +0000186def_op('RESERVE_FAST', 123) # Number of local variables
187hasconst.append(123)
188def_op('LOAD_FAST', 124) # Local variable number
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000189haslocal.append(124)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000190def_op('STORE_FAST', 125) # Local variable number
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000191haslocal.append(125)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000192def_op('DELETE_FAST', 126) # Local variable number
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000193haslocal.append(126)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000194
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000195def_op('SET_LINENO', 127) # Current line number
196SET_LINENO = 127
Guido van Rossum6e21ceb1996-07-21 02:16:53 +0000197
198def_op('RAISE_VARARGS', 130)
199def_op('CALL_FUNCTION', 131)
200def_op('MAKE_FUNCTION', 132)
Guido van Rossum56a73381996-07-30 16:26:07 +0000201def_op('BUILD_SLICE', 133)