Skip to content

Commit

Permalink
replace offsets by labels
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Nov 16, 2023
1 parent 5470f75 commit 54cb8ca
Show file tree
Hide file tree
Showing 2 changed files with 520 additions and 560 deletions.
47 changes: 30 additions & 17 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ def _get_jump_target(op, arg, offset):
target = None
return target

def _label_string(label, width=None, suffix=''):
if label is not None:
lbl = f'{label}'
padding = width - len(lbl) if width is not None else 0
assert padding >= 0
return f"L{lbl}{suffix}{'':<{padding}}"
else:
return ' ' * (width + 1 + len(suffix))

class Instruction(_Instruction):
"""Details for a bytecode operation.
Expand All @@ -331,7 +340,8 @@ class Instruction(_Instruction):
"""

@staticmethod
def _get_argval_argrepr(op, arg, offset, co_consts, names, varname_from_oparg):
def _get_argval_argrepr(op, arg, offset, co_consts, names, varname_from_oparg,
labels_map, label_width):
get_name = None if names is None else names.__getitem__
argval = None
argrepr = ''
Expand Down Expand Up @@ -361,13 +371,13 @@ def _get_argval_argrepr(op, arg, offset, co_consts, names, varname_from_oparg):
argval, argrepr = _get_name_info(arg, get_name)
elif deop in hasjabs:
argval = arg*2
argrepr = "to " + repr(argval)
argrepr = f"to {_label_string(labels_map[argval])}"
elif deop in hasjrel:
signed_arg = -arg if _is_backward_jump(deop) else arg
argval = offset + 2 + signed_arg*2
caches = _get_cache_size(_all_opname[deop])
argval += 2 * caches
argrepr = "to " + repr(argval)
argrepr = f"to {_label_string(labels_map[argval])}"
elif deop in (LOAD_FAST_LOAD_FAST, STORE_FAST_LOAD_FAST, STORE_FAST_STORE_FAST):
arg1 = arg >> 4
arg2 = arg & 15
Expand Down Expand Up @@ -399,14 +409,21 @@ def _get_argval_argrepr(op, arg, offset, co_consts, names, varname_from_oparg):

@classmethod
def _create(cls, op, arg, offset, start_offset, starts_line, line_number,
label, positions,
co_consts=None, varname_from_oparg=None, names=None):
positions,
co_consts=None, varname_from_oparg=None, names=None,
labels_map=None):

label_width = len(str(len(labels_map)))
argval, argrepr = cls._get_argval_argrepr(
op, arg, offset,
co_consts, names, varname_from_oparg)
return Instruction(_all_opname[op], op, arg, argval, argrepr,
offset, start_offset, starts_line, line_number,
label, positions)
co_consts, names, varname_from_oparg, labels_map,
label_width)
label = labels_map.get(offset, None)
instr = Instruction(_all_opname[op], op, arg, argval, argrepr,
offset, start_offset, starts_line, line_number,
label, positions)
instr.label_width = label_width
return instr

@property
def oparg(self):
Expand Down Expand Up @@ -474,12 +491,7 @@ def _disassemble(self, lineno_width=3, mark_as_current=False, offset_width=4):
else:
fields.append(' ')
# Column: Jump target marker
if self.is_jump_target:
fields.append('>>')
else:
fields.append(' ')
# Column: Instruction offset from start of code sequence
fields.append(repr(self.offset).rjust(offset_width))
fields.append(_label_string(self.label, width=self.label_width, suffix=': '))
# Column: Opcode name
fields.append(self.opname.ljust(_OPNAME_WIDTH))
# Column: Opcode argument
Expand Down Expand Up @@ -637,8 +649,9 @@ def make_labels_map(original_code, exception_entries):
op = code[offset]

yield Instruction._create(op, arg, offset, start_offset, starts_line, line_number,
labels_map.get(offset, None), positions, co_consts=co_consts,
varname_from_oparg=varname_from_oparg, names=names)
positions, co_consts=co_consts,
varname_from_oparg=varname_from_oparg, names=names,
labels_map=labels_map)

caches = _get_cache_size(_all_opname[deop])
if not caches:
Expand Down
Loading

0 comments on commit 54cb8ca

Please sign in to comment.