Better assembly formatting of jump instructions

This commit is contained in:
rocky
2016-07-25 00:36:39 -04:00
parent aed4d23c34
commit 285444e19a
6 changed files with 47 additions and 31 deletions

View File

@@ -1,12 +1,12 @@
# Python 2.7 # Python 2.7
# Embedded file name: simple_source/branching/05_if.py # Embedded file name: simple_source/branching/05_if.py
6 0 LOAD_NAME 0 'True' 6 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 '15' 3 POP_JUMP_IF_FALSE 15 'to 15'
7 6 LOAD_NAME 1 'False' 7 6 LOAD_NAME 1 'False'
9 STORE_NAME 2 'b' 9 STORE_NAME 2 'b'
12 JUMP_FORWARD 0 '15' 12 JUMP_FORWARD 0 'to 15'
15_0 COME_FROM '12' 15_0 COME_FROM '12'
15 LOAD_CONST 0 None 15 LOAD_CONST 0 ''
18 RETURN_VALUE 18 RETURN_VALUE ''

View File

@@ -1,15 +1,15 @@
# Python 2.7 # Python 2.7
# Embedded file name: simple_source/branching/05_ifelse.py # Embedded file name: simple_source/branching/05_ifelse.py
3 0 LOAD_NAME 0 'True' 3 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 '15' 3 POP_JUMP_IF_FALSE 15 'to 15'
4 6 LOAD_CONST 0 1 4 6 LOAD_CONST 0 1
9 STORE_NAME 1 'b' 9 STORE_NAME 1 'b'
12 JUMP_FORWARD 6 '21' 12 JUMP_FORWARD 6 'to 21'
6 15 LOAD_CONST 1 2 6 15 LOAD_CONST 1 2
18 STORE_NAME 2 'd' 18 STORE_NAME 2 'd'
21_0 COME_FROM '12' 21_0 COME_FROM '12'
21 LOAD_CONST 2 None 21 LOAD_CONST 2 ''
24 RETURN_VALUE 24 RETURN_VALUE ''

View File

@@ -219,10 +219,12 @@ class Scanner2(scan.Scanner):
if offset not in replace: if offset not in replace:
tokens.append(Token( tokens.append(Token(
opname, oparg, pattr, offset, linestart, op, has_arg)) opname, oparg, pattr, offset, linestart, op,
has_arg, self.opc))
else: else:
tokens.append(Token( tokens.append(Token(
replace[offset], oparg, pattr, offset, linestart, op, has_arg)) replace[offset], oparg, pattr, offset, linestart,
op, has_arg, self.opc))
pass pass
pass pass

View File

@@ -284,10 +284,12 @@ class Scanner26(scan.Scanner2):
if offset not in replace: if offset not in replace:
tokens.append(Token( tokens.append(Token(
op_name, oparg, pattr, offset, linestart, op, has_arg)) op_name, oparg, pattr, offset, linestart, op,
has_arg, self.opc))
else: else:
tokens.append(Token( tokens.append(Token(
replace[offset], oparg, pattr, offset, linestart, op, has_arg)) replace[offset], oparg, pattr, offset, linestart, op,
has_arg, self.opc))
pass pass
pass pass

View File

@@ -179,7 +179,7 @@ class Scanner3(scan.Scanner):
for jump_offset in jump_targets[inst.offset]: for jump_offset in jump_targets[inst.offset]:
tokens.append(Token('COME_FROM', None, repr(jump_offset), tokens.append(Token('COME_FROM', None, repr(jump_offset),
offset='%s_%s' % (inst.offset, jump_idx), offset='%s_%s' % (inst.offset, jump_idx),
has_arg = True)) has_arg = True, opc=self.opc))
jump_idx += 1 jump_idx += 1
pass pass
pass pass
@@ -229,7 +229,8 @@ class Scanner3(scan.Scanner):
offset = inst.offset, offset = inst.offset,
linestart = inst.starts_line, linestart = inst.starts_line,
op = op, op = op,
has_arg = (op >= op3.HAVE_ARGUMENT) has_arg = (op >= op3.HAVE_ARGUMENT),
opc = self.opc
) )
) )
continue continue
@@ -290,7 +291,8 @@ class Scanner3(scan.Scanner):
offset = inst.offset, offset = inst.offset,
linestart = inst.starts_line, linestart = inst.starts_line,
op = op, op = op,
has_arg = (op >= op3.HAVE_ARGUMENT) has_arg = (op >= op3.HAVE_ARGUMENT),
opc = self.opc
) )
) )
pass pass

View File

@@ -21,7 +21,7 @@ class Token:
# attr = argval # attr = argval
# pattr = argrepr # pattr = argrepr
def __init__(self, type_, attr=None, pattr=None, offset=-1, def __init__(self, type_, attr=None, pattr=None, offset=-1,
linestart=None, op=None, has_arg=None): linestart=None, op=None, has_arg=None, opc=None):
self.type = intern(type_) self.type = intern(type_)
self.op = op self.op = op
self.has_arg = has_arg self.has_arg = has_arg
@@ -29,6 +29,7 @@ class Token:
self.pattr = pattr self.pattr = pattr
self.offset = offset self.offset = offset
self.linestart = linestart self.linestart = linestart
self.opc = opc
def __eq__(self, o): def __eq__(self, o):
""" '==', but it's okay if offsets and linestarts are different""" """ '==', but it's okay if offsets and linestarts are different"""
@@ -49,13 +50,22 @@ class Token:
('%9s %-18s %r' % (self.offset, self.type, pattr))) ('%9s %-18s %r' % (self.offset, self.type, pattr)))
def format(self): def format(self):
prefix = '\n%3d ' % self.linestart if self.linestart else (' ' * 6) prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 6)
offset_opname = '%9s %-18s' % (self.offset, self.type) offset_opname = '%6s %-17s' % (self.offset, self.type)
argstr = "%6d " % self.attr if isinstance(self.attr, int) else (' '*7) argstr = "%6d " % self.attr if isinstance(self.attr, int) else (' '*7)
if self.has_arg: if self.pattr:
return "%s%s%s %r" % (prefix, offset_opname, argstr, self.pattr) pattr = self.pattr
if self.opc:
if self.op in self.opc.hasjrel:
pattr = "to " + self.pattr
elif self.op in self.opc.hasjabs:
pattr = "to " + self.pattr
pass
# And so on. See xdis/bytecode.py
pass
else: else:
return "%s%s" % (prefix, offset_opname) pattr = ''
return "%s%s%s %r" % (prefix, offset_opname, argstr, pattr)
def __hash__(self): def __hash__(self):
return hash(self.type) return hash(self.type)