blob: a012c17b477f85e33336e0036887d565b73cba19 [file] [log] [blame]
Guido van Rossum421c2241997-11-18 15:47:55 +00001"""Disassembler of Python byte code into mnemonics."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +00002
3import sys
4import string
Guido van Rossum18aef3c1997-03-14 04:15:43 +00005import types
Guido van Rossum217a5fa1990-12-26 15:40:07 +00006
Guido van Rossumbd307951997-01-17 20:05:04 +00007def dis(x=None):
Guido van Rossum421c2241997-11-18 15:47:55 +00008 """Disassemble classes, methods, functions, or code.
9
10 With no argument, disassemble the last traceback.
11
12 """
Guido van Rossumbd307951997-01-17 20:05:04 +000013 if not x:
14 distb()
Guido van Rossum18aef3c1997-03-14 04:15:43 +000015 return
16 if type(x) is types.InstanceType:
17 x = x.__class__
18 if hasattr(x, '__dict__'):
19 items = x.__dict__.items()
20 items.sort()
21 for name, x1 in items:
22 if type(x1) in (types.MethodType,
23 types.FunctionType,
24 types.CodeType):
25 print "Disassembly of %s:" % name
26 try:
27 dis(x1)
28 except TypeError, msg:
29 print "Sorry:", msg
30 print
Guido van Rossumbd307951997-01-17 20:05:04 +000031 else:
32 if hasattr(x, 'im_func'):
33 x = x.im_func
34 if hasattr(x, 'func_code'):
35 x = x.func_code
36 if hasattr(x, 'co_code'):
37 disassemble(x)
38 else:
Guido van Rossum00f86e61997-01-17 20:08:18 +000039 raise TypeError, \
Guido van Rossumbd307951997-01-17 20:05:04 +000040 "don't know how to disassemble %s objects" % \
41 type(x).__name__
Guido van Rossum217a5fa1990-12-26 15:40:07 +000042
Guido van Rossumbd307951997-01-17 20:05:04 +000043def distb(tb=None):
Guido van Rossum421c2241997-11-18 15:47:55 +000044 """Disassemble a traceback (default: last traceback)."""
Guido van Rossumbd307951997-01-17 20:05:04 +000045 if not tb:
46 try:
47 tb = sys.last_traceback
48 except AttributeError:
49 raise RuntimeError, "no last traceback to disassemble"
50 while tb.tb_next: tb = tb.tb_next
Guido van Rossum217a5fa1990-12-26 15:40:07 +000051 disassemble(tb.tb_frame.f_code, tb.tb_lasti)
52
Guido van Rossumbd307951997-01-17 20:05:04 +000053def disassemble(co, lasti=-1):
Guido van Rossum421c2241997-11-18 15:47:55 +000054 """Disassemble a code object."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +000055 code = co.co_code
56 labels = findlabels(code)
57 n = len(code)
58 i = 0
59 while i < n:
60 c = code[i]
61 op = ord(c)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000062 if op == SET_LINENO and i > 0: print # Extra blank line
63 if i == lasti: print '-->',
Guido van Rossum217a5fa1990-12-26 15:40:07 +000064 else: print ' ',
65 if i in labels: print '>>',
66 else: print ' ',
67 print string.rjust(`i`, 4),
68 print string.ljust(opname[op], 15),
69 i = i+1
70 if op >= HAVE_ARGUMENT:
71 oparg = ord(code[i]) + ord(code[i+1])*256
72 i = i+2
73 print string.rjust(`oparg`, 5),
74 if op in hasconst:
75 print '(' + `co.co_consts[oparg]` + ')',
76 elif op in hasname:
77 print '(' + co.co_names[oparg] + ')',
78 elif op in hasjrel:
79 print '(to ' + `i + oparg` + ')',
Guido van Rossum934a4ce1996-09-12 17:39:36 +000080 elif op in haslocal:
81 print '(' + co.co_varnames[oparg] + ')',
Guido van Rossumd0bc9cb1997-01-16 18:52:24 +000082 elif op in hascompare:
83 print '(' + cmp_op[oparg] + ')',
Guido van Rossum217a5fa1990-12-26 15:40:07 +000084 print
85
Guido van Rossumbd307951997-01-17 20:05:04 +000086disco = disassemble
87
Guido van Rossum217a5fa1990-12-26 15:40:07 +000088def findlabels(code):
Guido van Rossum421c2241997-11-18 15:47:55 +000089 """Detect all offsets in a byte code which are jump targets.
90
91 Return the list of offsets.
92
93 """
Guido van Rossum217a5fa1990-12-26 15:40:07 +000094 labels = []
95 n = len(code)
96 i = 0
97 while i < n:
98 c = code[i]
99 op = ord(c)
100 i = i+1
101 if op >= HAVE_ARGUMENT:
102 oparg = ord(code[i]) + ord(code[i+1])*256
103 i = i+2
104 label = -1
105 if op in hasjrel:
106 label = i+oparg
107 elif op in hasjabs:
108 label = oparg
109 if label >= 0:
110 if label not in labels:
111 labels.append(label)
112 return labels
113
Guido van Rossumd0bc9cb1997-01-16 18:52:24 +0000114cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
115 'is not', 'exception match', 'BAD')
116
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000117hasconst = []
118hasname = []
119hasjrel = []
120hasjabs = []
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000121haslocal = []
Guido van Rossumd0bc9cb1997-01-16 18:52:24 +0000122hascompare = []
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000123
Guido van Rossume65cce51993-11-08 15:05:21 +0000124opname = [''] * 256
125for op in range(256): opname[op] = '<' + `op` + '>'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000126
127def def_op(name, op):
128 opname[op] = name
129
130def name_op(name, op):
131 opname[op] = name
132 hasname.append(op)
133
134def jrel_op(name, op):
135 opname[op] = name
136 hasjrel.append(op)
137
138def jabs_op(name, op):
139 opname[op] = name
140 hasjabs.append(op)
141
142# Instruction opcodes for compiled code
143
144def_op('STOP_CODE', 0)
145def_op('POP_TOP', 1)
146def_op('ROT_TWO', 2)
147def_op('ROT_THREE', 3)
148def_op('DUP_TOP', 4)
149
150def_op('UNARY_POSITIVE', 10)
151def_op('UNARY_NEGATIVE', 11)
152def_op('UNARY_NOT', 12)
153def_op('UNARY_CONVERT', 13)
Guido van Rossumd0f23721997-05-09 03:21:44 +0000154
Guido van Rossum8379ed51993-03-30 19:13:03 +0000155def_op('UNARY_INVERT', 15)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000156
Guido van Rossum6e21ceb1996-07-21 02:16:53 +0000157def_op('BINARY_POWER', 19)
Guido van Rossumd0f23721997-05-09 03:21:44 +0000158
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000159def_op('BINARY_MULTIPLY', 20)
160def_op('BINARY_DIVIDE', 21)
161def_op('BINARY_MODULO', 22)
162def_op('BINARY_ADD', 23)
163def_op('BINARY_SUBTRACT', 24)
164def_op('BINARY_SUBSCR', 25)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000165
166def_op('SLICE+0', 30)
167def_op('SLICE+1', 31)
168def_op('SLICE+2', 32)
169def_op('SLICE+3', 33)
170
171def_op('STORE_SLICE+0', 40)
172def_op('STORE_SLICE+1', 41)
173def_op('STORE_SLICE+2', 42)
174def_op('STORE_SLICE+3', 43)
175
176def_op('DELETE_SLICE+0', 50)
177def_op('DELETE_SLICE+1', 51)
178def_op('DELETE_SLICE+2', 52)
179def_op('DELETE_SLICE+3', 53)
180
181def_op('STORE_SUBSCR', 60)
182def_op('DELETE_SUBSCR', 61)
Guido van Rossumd0f23721997-05-09 03:21:44 +0000183
Guido van Rossumd0bc9cb1997-01-16 18:52:24 +0000184def_op('BINARY_LSHIFT', 62)
185def_op('BINARY_RSHIFT', 63)
186def_op('BINARY_AND', 64)
187def_op('BINARY_XOR', 65)
188def_op('BINARY_OR', 66)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000189
190def_op('PRINT_EXPR', 70)
191def_op('PRINT_ITEM', 71)
192def_op('PRINT_NEWLINE', 72)
193
194def_op('BREAK_LOOP', 80)
Guido van Rossumd0f23721997-05-09 03:21:44 +0000195
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000196def_op('LOAD_LOCALS', 82)
197def_op('RETURN_VALUE', 83)
Guido van Rossumd0f23721997-05-09 03:21:44 +0000198
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199def_op('EXEC_STMT', 85)
Guido van Rossumd0f23721997-05-09 03:21:44 +0000200
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000201def_op('POP_BLOCK', 87)
202def_op('END_FINALLY', 88)
203def_op('BUILD_CLASS', 89)
204
205HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
206
207name_op('STORE_NAME', 90) # Index in name list
208name_op('DELETE_NAME', 91) # ""
209def_op('UNPACK_TUPLE', 92) # Number of tuple items
210def_op('UNPACK_LIST', 93) # Number of list items
Guido van Rossuma594fab1991-12-16 13:09:28 +0000211def_op('UNPACK_ARG', 94) # Number of arguments expected
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000212name_op('STORE_ATTR', 95) # Index in name list
Guido van Rossuma594fab1991-12-16 13:09:28 +0000213name_op('DELETE_ATTR', 96) # ""
214name_op('STORE_GLOBAL', 97) # ""
215name_op('DELETE_GLOBAL', 98) # ""
Guido van Rossum8379ed51993-03-30 19:13:03 +0000216name_op('UNPACK_VARARG', 99) # Minimal number of arguments
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000217
218def_op('LOAD_CONST', 100) # Index in const list
219hasconst.append(100)
220name_op('LOAD_NAME', 101) # Index in name list
221def_op('BUILD_TUPLE', 102) # Number of tuple items
222def_op('BUILD_LIST', 103) # Number of list items
223def_op('BUILD_MAP', 104) # Always zero for now
224name_op('LOAD_ATTR', 105) # Index in name list
225def_op('COMPARE_OP', 106) # Comparison operator
Guido van Rossumd0f23721997-05-09 03:21:44 +0000226hascompare.append(106)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000227name_op('IMPORT_NAME', 107) # Index in name list
228name_op('IMPORT_FROM', 108) # Index in name list
229
230jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
231jrel_op('JUMP_IF_FALSE', 111) # ""
232jrel_op('JUMP_IF_TRUE', 112) # ""
233jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
234jrel_op('FOR_LOOP', 114) # Number of bytes to skip
235
Guido van Rossuma594fab1991-12-16 13:09:28 +0000236name_op('LOAD_LOCAL', 115) # Index in name list
237name_op('LOAD_GLOBAL', 116) # Index in name list
Guido van Rossumd0f23721997-05-09 03:21:44 +0000238
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239def_op('SET_FUNC_ARGS', 117) # Argcount
Guido van Rossuma594fab1991-12-16 13:09:28 +0000240
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000241jrel_op('SETUP_LOOP', 120) # Distance to target address
242jrel_op('SETUP_EXCEPT', 121) # ""
243jrel_op('SETUP_FINALLY', 122) # ""
244
Guido van Rossum8379ed51993-03-30 19:13:03 +0000245def_op('LOAD_FAST', 124) # Local variable number
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000246haslocal.append(124)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000247def_op('STORE_FAST', 125) # Local variable number
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000248haslocal.append(125)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000249def_op('DELETE_FAST', 126) # Local variable number
Guido van Rossum934a4ce1996-09-12 17:39:36 +0000250haslocal.append(126)
Guido van Rossum8379ed51993-03-30 19:13:03 +0000251
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000252def_op('SET_LINENO', 127) # Current line number
253SET_LINENO = 127
Guido van Rossum6e21ceb1996-07-21 02:16:53 +0000254
255def_op('RAISE_VARARGS', 130)
256def_op('CALL_FUNCTION', 131)
257def_op('MAKE_FUNCTION', 132)
Guido van Rossum56a73381996-07-30 16:26:07 +0000258def_op('BUILD_SLICE', 133)