Repository navigation
Expand file tree
/
Copy pathbytecode.py
More file actions
323 lines (272 loc) · 9.64 KB
/
Copy pathbytecode.py
File metadata and controls
323 lines (272 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
From NumbaPro
"""
from __future__ import print_function, division, absolute_import
import dis
import sys
import inspect
from collections import namedtuple
from numba import utils, targets
from numba.config import PYVERSION
opcode_info = namedtuple('opcode_info', ['argsize'])
def get_function_object(obj):
"""
Objects that wraps function should provide a "__numba__" magic attribute
that contains a name of an attribute that contains the actual python
function object.
"""
attr = getattr(obj, "__numba__", None)
if attr:
return getattr(obj, attr)
return obj
def get_code_object(obj):
"Shamelessly borrowed from llpython"
return getattr(obj, '__code__', getattr(obj, 'func_code', None))
def _make_bytecode_table():
if sys.version_info[:2] == (2, 6): # python 2.6
version_specific = [
('JUMP_IF_FALSE', 2),
('JUMP_IF_TRUE', 2),
]
elif sys.version_info[:2] >= (2, 7): # python 2.7+
version_specific = [
('POP_JUMP_IF_FALSE', 2),
('POP_JUMP_IF_TRUE', 2),
('JUMP_IF_TRUE_OR_POP', 2),
('JUMP_IF_FALSE_OR_POP', 2),
]
if sys.version_info[0] == 2:
version_specific += [
('BINARY_DIVIDE', 0),
('DUP_TOPX', 2),
('INPLACE_DIVIDE', 0),
('PRINT_ITEM', 0),
('PRINT_NEWLINE', 0),
('SLICE+0', 0),
('SLICE+1', 0),
('SLICE+2', 0),
('SLICE+3', 0),
('STORE_SLICE+0', 0),
('STORE_SLICE+1', 0),
('STORE_SLICE+2', 0),
('STORE_SLICE+3', 0),
]
elif sys.version_info[0] == 3:
version_specific += [
('DUP_TOP_TWO', 0)
]
bytecodes = [
# opname, operandlen
('BINARY_ADD', 0),
('BINARY_TRUE_DIVIDE', 0),
('BINARY_MULTIPLY', 0),
('BINARY_SUBSCR', 0),
('BINARY_SUBTRACT', 0),
('BINARY_FLOOR_DIVIDE', 0),
('BINARY_MODULO', 0),
('BINARY_POWER', 0),
('BINARY_AND', 0),
('BINARY_OR', 0),
('BINARY_XOR', 0),
('BINARY_LSHIFT', 0),
('BINARY_RSHIFT', 0),
('BREAK_LOOP', 0),
('BUILD_LIST', 2),
('BUILD_SLICE', 2),
('BUILD_TUPLE', 2),
('CALL_FUNCTION', 2),
('COMPARE_OP', 2),
('DUP_TOP', 0),
('FOR_ITER', 2),
('GET_ITER', 0),
('INPLACE_ADD', 0),
('INPLACE_SUBTRACT', 0),
('INPLACE_MULTIPLY', 0),
('INPLACE_TRUE_DIVIDE', 0),
('INPLACE_FLOOR_DIVIDE', 0),
('INPLACE_MODULO', 0),
('INPLACE_POWER', 0),
('INPLACE_AND', 0),
('INPLACE_OR', 0),
('INPLACE_XOR', 0),
('INPLACE_LSHIFT', 0),
('INPLACE_RSHIFT', 0),
('JUMP_ABSOLUTE', 2),
('JUMP_FORWARD', 2),
('LOAD_ATTR', 2),
('LOAD_CONST', 2),
('LOAD_FAST', 2),
('LOAD_GLOBAL', 2),
('POP_BLOCK', 0),
('POP_TOP', 0),
('RAISE_VARARGS', 2),
('RETURN_VALUE', 0),
('ROT_THREE', 0),
('ROT_TWO', 0),
('SETUP_LOOP', 2),
('STORE_FAST', 2),
# ('STORE_ATTR', 2), # not supported
('STORE_SUBSCR', 0),
('UNARY_POSITIVE', 0),
('UNARY_NEGATIVE', 0),
('UNARY_INVERT', 0),
('UNARY_NOT', 0),
('UNPACK_SEQUENCE', 2),
] + version_specific
return dict((dis.opmap[opname], opcode_info(argsize=argsize))
for opname, argsize in bytecodes)
def _as_opcodes(seq):
lst = []
for s in seq:
c = dis.opmap.get(s)
if c is not None:
lst.append(c)
return lst
BYTECODE_TABLE = _make_bytecode_table()
JREL_OPS = frozenset(dis.hasjrel)
JABS_OPS = frozenset(dis.hasjabs)
JUMP_OPS = JREL_OPS | JABS_OPS
TERM_OPS = frozenset(_as_opcodes(['RETURN_VALUE', 'RAISE_VARARGS']))
class ByteCodeInst(object):
'''
Attributes
----------
- offset:
byte offset of opcode
- opcode:
opcode integer value
- arg:
instruction arg
- lineno:
-1 means unknown
'''
__slots__ = 'offset', 'next', 'opcode', 'opname', 'arg', 'lineno'
def __init__(self, offset, opcode, arg):
self.offset = offset
self.next = offset + BYTECODE_TABLE[opcode].argsize + 1
self.opcode = opcode
self.opname = dis.opname[opcode]
self.arg = arg
self.lineno = -1 # unknown line number
@classmethod
def get(cls, offset, opname, arg):
return cls(offset, dis.opmap[opname], arg)
@property
def is_jump(self):
return self.opcode in JUMP_OPS
@property
def is_terminator(self):
return self.opcode in TERM_OPS
def get_jump_target(self):
assert self.is_jump
if self.opcode in JREL_OPS:
return self.next + self.arg
else:
assert self.opcode in JABS_OPS
return self.arg
def __repr__(self):
return '%s(arg=%s, lineno=%d)' % (self.opname, self.arg, self.lineno)
class ByteCodeIter(object):
def __init__(self, code):
self.code = code
if PYVERSION > (3, 0):
self.iter = enumerate(self.code.co_code)
else:
self.iter = ((i, ord(x)) for i, x in enumerate(self.code.co_code))
def __iter__(self):
return self
def next(self):
offset, opcode = next(self.iter)
try:
info = BYTECODE_TABLE[opcode]
except KeyError:
ts = "offset=%d opcode=%x opname=%s"
tv = offset, opcode, dis.opname[opcode]
raise NotImplementedError(ts % tv)
if info.argsize:
arg = self.read_arg(info.argsize)
else:
arg = None
return offset, ByteCodeInst(offset=offset, opcode=opcode, arg=arg)
__next__ = next
def read_arg(self, size):
buf = 0
for i in range(size):
_offset, byte = utils.iter_next(self.iter)
buf |= byte << (8 * i)
return buf
class ByteCodeOperation(object):
def __init__(self, inst, args):
self.inst = inst
self.args = args
class ByteCodeSupportError(Exception):
pass
class ByteCodeBase(object):
__slots__ = 'func', 'func_name', 'argspec', 'filename', 'co_names', \
'co_varnames', 'co_consts', 'table', 'labels'
def __init__(self, func, func_name, argspec, filename, co_names,
co_varnames, co_consts, table, labels):
self.func = func
self.module = inspect.getmodule(func)
self.func_name = func_name
self.argspec = argspec
self.filename = filename
self.co_names = co_names
self.co_varnames = co_varnames
self.co_consts = co_consts
self.table = table
self.labels = labels
self.firstlineno = min(inst.lineno for inst in self.table.values())
def __iter__(self):
return utils.dict_itervalues(self.table)
def __getitem__(self, offset):
return self.table[offset]
def __contains__(self, offset):
return offset in self.table
def dump(self):
def label_marker(i):
if i[1].offset in self.labels:
return '>'
else:
return ' '
return '\n'.join('%s %10d\t%s' % ((label_marker(i),) + i)
for i in utils.dict_iteritems(self.table))
class CustomByteCode(ByteCodeBase):
pass
class ByteCode(ByteCodeBase):
def __init__(self, func):
func = get_function_object(func)
code = get_code_object(func)
if not code:
raise ByteCodeSupportError("%s does not provide its bytecode" %
func)
if code.co_freevars:
raise ByteCodeSupportError("does not support freevars")
if code.co_cellvars:
raise ByteCodeSupportError("does not support cellvars")
table = utils.SortedMap(ByteCodeIter(code))
labels = set(dis.findlabels(code.co_code))
labels.add(0)
self._mark_lineno(table, code)
super(ByteCode, self).__init__(func=func,
func_name=func.__name__,
argspec=inspect.getargspec(func),
filename=code.co_filename,
co_names=code.co_names,
co_varnames=code.co_varnames,
co_consts=code.co_consts,
table=table,
labels=list(sorted(labels)))
@classmethod
def _mark_lineno(cls, table, code):
'''Fill the lineno info for all bytecode inst
'''
for offset, lineno in dis.findlinestarts(code):
if offset in table:
table[offset].lineno = lineno
known = -1
for inst in table.values():
if inst.lineno >= 0:
known = inst.lineno
else:
inst.lineno = known