Skip to content

Commit 4d5e729

Browse files
committed
Sync disassembly command with trepan3k
1 parent d1ac30d commit 4d5e729

1 file changed

Lines changed: 37 additions & 19 deletions

File tree

trepan/lib/disassemble.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
# FIXME: see if we can use more of Lib/dis in Python3
44
"""Disassembly Routines"""
55

6-
import inspect, sys, types
6+
import inspect
7+
import sys
8+
import types
79

10+
from pygments.token import Comment
811
from xdis import (
12+
IS_PYPY,
913
Bytecode,
1014
findlabels,
1115
findlinestarts,
1216
get_instructions_bytes,
1317
get_opcode,
1418
)
19+
from xdis.instruction import Instruction
1520
from xdis.std import distb
16-
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
21+
from xdis.version_info import PYTHON_VERSION_TRIPLE
1722

1823
from trepan.lib.format import (
1924
Arrow,
20-
Comment,
2125
Details,
2226
Hex,
2327
Integer,
@@ -32,10 +36,10 @@
3236

3337
def _try_compile(source, name):
3438
"""Attempts to compile the given source, first as an expression and
35-
then as a statement if the first approach fails.
39+
then as a statement if the first approach fails.
3640
37-
Utility function to accept strings in functions that otherwise
38-
expect code objects
41+
Utility function to accept strings in functions that otherwise
42+
expect code objects
3943
"""
4044
try:
4145
c = compile(source, name, "eval")
@@ -153,7 +157,6 @@ def dis(
153157
return disassemble(
154158
msg,
155159
msg_nocr,
156-
section,
157160
x,
158161
lasti=lasti,
159162
start_line=start_line,
@@ -165,7 +168,11 @@ def dis(
165168
asm_format=asm_format,
166169
)
167170
elif isinstance(x, str): # Source code
168-
return disassemble_string(msg, msg_nocr, x,)
171+
return disassemble_string(
172+
msg,
173+
msg_nocr,
174+
x,
175+
)
169176
else:
170177
errmsg("Don't know how to disassemble %s objects." % type(x).__name__)
171178
return None, None
@@ -174,7 +181,6 @@ def dis(
174181
def disassemble(
175182
msg,
176183
msg_nocr,
177-
section,
178184
co,
179185
lasti=-1,
180186
start_line=-1,
@@ -250,7 +256,9 @@ def disassemble_bytes(
250256

251257
labels = findlabels(code, opc)
252258

253-
null_print = lambda x: None
259+
def null_print(x):
260+
return None
261+
254262
if start_line > cur_line:
255263
msg_nocr = null_print
256264
msg = null_print
@@ -315,7 +323,7 @@ def disassemble_bytes(
315323
msg_nocr(format_token(Hex, "%02x" % instr.opcode, highlight=highlight))
316324
if instr.inst_size == 1:
317325
# Not 3.6 or later
318-
msg_nocr(" " * 5)
326+
msg_nocr(" " * (2 * 3))
319327
if instr.inst_size == 2:
320328
# Must by Python 3.6 or later
321329
msg_nocr(" ")
@@ -352,13 +360,20 @@ def disassemble_bytes(
352360
hasattr(opc, "opcode_extended_fmt")
353361
and opc.opname[op] in opc.opcode_extended_fmt
354362
):
355-
new_repr = opc.opcode_extended_fmt[opc.opname[op]](
363+
tos_str, start_offset = opc.opcode_extended_fmt[opc.opname[op]](
356364
opc, list(reversed(instructions))
357365
)
358-
if new_repr:
359-
argrepr = new_repr
366+
if start_offset is not None:
367+
argrepr = tos_str
368+
new_instruction = list(instructions[-1])
369+
new_instruction[-2] = tos_str
370+
new_instruction[-1] = start_offset
371+
del instructions[-1]
372+
instructions.append(Instruction(*new_instruction))
373+
360374
pass
361375
elif asm_format in ("extended", "extended-bytes"):
376+
# Note: instr.arg is also None
362377
op = instr.opcode
363378
if (
364379
hasattr(opc, "opcode_extended_fmt")
@@ -369,7 +384,7 @@ def disassemble_bytes(
369384
)
370385
if new_repr:
371386
argrepr = new_repr
372-
if argrepr is None:
387+
if argrepr is None or argrepr == "":
373388
if instr.arg is not None:
374389
msg(format_token(Integer, str(instr.arg), highlight=highlight))
375390
else:
@@ -378,9 +393,7 @@ def disassemble_bytes(
378393
pass
379394
else:
380395
# Column: Opcode argument details
381-
msg_nocr(format_token(Symbol, "(", highlight=highlight))
382-
msg_nocr(format_token(Details, argrepr, highlight=highlight))
383-
msg(format_token(Symbol, ")", highlight=highlight))
396+
msg(format_token(Details, argrepr, highlight=highlight))
384397
pass
385398

386399
return code, offset
@@ -393,18 +406,22 @@ def msg(msg_str):
393406
print(msg_str)
394407
return
395408

409+
396410
def msg_nocr(msg_str):
397411
sys.stdout.write(msg_str)
398412
return
399413

414+
400415
def errmsg(msg_str):
401416
msg("*** " + msg_str)
402417
return
403418

419+
404420
def section(msg_str):
405421
msg("=== " + msg_str + " ===")
406422
return
407423

424+
408425
curframe = inspect.currentframe()
409426
# dis(msg, msg_nocr, errmsg, section, curframe,
410427
# start_line=10, end_line=40, highlight='dark')
@@ -413,7 +430,8 @@ def section(msg_str):
413430
# dis(msg, msg_nocr, errmsg, section, curframe,
414431
# start_offset=10, end_offset=20, highlight='dark')
415432
print("-" * 40)
416-
for asm_format in ("std", "extended", "bytes", "extended-bytes"):
433+
# for asm_format in ("std", "extended", "bytes", "extended-bytes"):
434+
for asm_format in ("extended", "bytes", "extended-bytes"):
417435
print("Format is", asm_format)
418436
dis(msg, msg_nocr, section, errmsg, disassemble, asm_format=asm_format)
419437
print("=" * 30)

0 commit comments

Comments
 (0)