forked from MatthieuDartiailh/bytecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
221 lines (195 loc) · 6.69 KB
/
__init__.py
File metadata and controls
221 lines (195 loc) · 6.69 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
__all__ = [
"BinaryOp",
"Bytecode",
"Compare",
"CompilerFlags",
"ConcreteBytecode",
"ConcreteInstr",
"ControlFlowGraph",
"Instr",
"Label",
"SetLineno",
"__version__",
]
from io import StringIO
from typing import List, Union
# import needed to use it in bytecode.py
from bytecode.bytecode import (
BaseBytecode,
Bytecode,
_BaseBytecodeList,
_InstrList,
)
# import needed to use it in bytecode.py
from bytecode.cfg import BasicBlock, ControlFlowGraph
# import needed to use it in bytecode.py
from bytecode.concrete import (
ConcreteBytecode,
ConcreteInstr,
_ConvertBytecodeToConcrete,
)
from bytecode.flags import CompilerFlags
# import needed to use it in bytecode.py
from bytecode.instr import (
UNSET,
BinaryOp,
CellVar,
Compare,
FreeVar,
Instr,
Intrinsic1Op,
Intrinsic2Op,
Label,
SetLineno,
TryBegin,
TryEnd,
)
from bytecode.version import __version__
def format_bytecode(
bytecode: Union[Bytecode, ConcreteBytecode, ControlFlowGraph],
*,
lineno: bool = False,
) -> str:
try_begins: List[TryBegin] = []
def format_line(index, line):
nonlocal cur_lineno, prev_lineno
if lineno:
if cur_lineno != prev_lineno:
line = "L.% 3s % 3s: %s" % (cur_lineno, index, line)
prev_lineno = cur_lineno
else:
line = " % 3s: %s" % (index, line)
else:
line = line
return line
def format_instr(instr, labels=None):
text = instr.name
arg = instr._arg
if arg is not UNSET:
if isinstance(arg, Label):
try:
arg = "<%s>" % labels[arg]
except KeyError:
arg = "<error: unknown label>"
elif isinstance(arg, BasicBlock):
try:
arg = "<%s>" % labels[id(arg)]
except KeyError:
arg = "<error: unknown block>"
else:
arg = repr(arg)
text = "%s %s" % (text, arg)
return text
def format_try_begin(instr: TryBegin, labels: dict) -> str:
if isinstance(instr.target, Label):
try:
arg = "<%s>" % labels[instr.target]
except KeyError:
arg = "<error: unknown label>"
else:
try:
arg = "<%s>" % labels[id(instr.target)]
except KeyError:
arg = "<error: unknown label>"
line = "TryBegin %s -> %s [%s]" % (
len(try_begins),
arg,
instr.stack_depth,
) + (" last_i" if instr.push_lasti else "")
# Track the seen try begin
try_begins.append(instr)
return line
def format_try_end(instr: TryEnd) -> str:
i = try_begins.index(instr.entry) if instr.entry in try_begins else "<unknwon>"
return "TryEnd (%s)" % i
buffer = StringIO()
indent = " " * 4
cur_lineno = bytecode.first_lineno
prev_lineno = None
if isinstance(bytecode, ConcreteBytecode):
offset = 0
for c_instr in bytecode:
fields = []
if c_instr.lineno is not None:
cur_lineno = c_instr.lineno
if lineno:
fields.append(format_instr(c_instr))
line = "".join(fields)
line = format_line(offset, line)
else:
fields.append("% 3s %s" % (offset, format_instr(c_instr)))
line = "".join(fields)
buffer.write(line + "\n")
if isinstance(c_instr, ConcreteInstr):
offset += c_instr.size
if bytecode.exception_table:
buffer.write("\n")
buffer.write("Exception table:\n")
for entry in bytecode.exception_table:
buffer.write(
f"{entry.start_offset} to {entry.stop_offset} -> "
f"{entry.target} [{entry.stack_depth}]"
+ (" lasti" if entry.push_lasti else "")
+ "\n"
)
elif isinstance(bytecode, Bytecode):
labels: dict[Label, str] = {}
for index, instr in enumerate(bytecode):
if isinstance(instr, Label):
labels[instr] = "label_instr%s" % index
for index, instr in enumerate(bytecode):
if isinstance(instr, Label):
label = labels[instr]
line = "%s:" % label
if index != 0:
buffer.write("\n")
elif isinstance(instr, TryBegin):
line = indent + format_line(index, format_try_begin(instr, labels))
indent += " "
elif isinstance(instr, TryEnd):
indent = indent[:-2]
line = indent + format_line(index, format_try_end(instr))
else:
if instr.lineno is not None:
cur_lineno = instr.lineno
line = format_instr(instr, labels)
line = indent + format_line(index, line)
buffer.write(line + "\n")
buffer.write("\n")
elif isinstance(bytecode, ControlFlowGraph):
cfg_labels = {}
for block_index, block in enumerate(bytecode, 1):
cfg_labels[id(block)] = "block%s" % block_index
for block in bytecode:
buffer.write("%s:\n" % cfg_labels[id(block)])
seen_instr = False
for index, instr in enumerate(block):
if isinstance(instr, TryBegin):
line = indent + format_line(
index, format_try_begin(instr, cfg_labels)
)
indent += " "
elif isinstance(instr, TryEnd):
if seen_instr:
indent = indent[:-2]
line = indent + format_line(index, format_try_end(instr))
else:
if isinstance(instr, Instr):
seen_instr = True
if instr.lineno is not None:
cur_lineno = instr.lineno
line = format_instr(instr, cfg_labels)
line = indent + format_line(index, line)
buffer.write(line + "\n")
if block.next_block is not None:
buffer.write(indent + "-> %s\n" % cfg_labels[id(block.next_block)])
buffer.write("\n")
else:
raise TypeError("unknown bytecode class")
return buffer.getvalue()[:-1]
def dump_bytecode(
bytecode: Union[Bytecode, ConcreteBytecode, ControlFlowGraph],
*,
lineno: bool = False,
) -> None:
print(format_bytecode(bytecode, lineno=lineno))