Permalink
Browse files

pyassem: Refactor Assembler for clarity.

Got a regtest.sh different in core/process.pyc, most likely due to the
Run() method name overlapping.
  • Loading branch information...
Andy Chu
Andy Chu committed Mar 20, 2018
1 parent 7e528a4 commit 13db25e1e3692903c6a2e9bf9eb1884650a7d0b6
Showing with 29 additions and 40 deletions.
  1. +29 −40 opy/compiler2/pyassem.py
View
@@ -94,27 +94,6 @@ def FlattenGraph(blocks):
return insts
def Assemble(insts):
ass = Assembler()
for t in insts:
opname = t[0]
if len(t) == 1:
ass.addCode(OPNUM[opname])
else:
oparg = t[1]
if opname == "SET_LINENO":
ass.nextLine(oparg)
continue
hi, lo = divmod(oparg, 256)
try:
ass.addCode(OPNUM[opname], lo, hi)
except ValueError:
print(opname, oparg)
print(OPNUM[opname], lo, hi)
raise
return ass
class FlowGraph(object):
def __init__(self):
@@ -348,14 +327,6 @@ class PyFlowGraph(FlowGraph):
"""Something that gets turned into a single code object.
Code objects and consts are mutually recursive.
Instantiated by compile() (3 cases), and by AbstractFunctionCode and
AbstractClassCode.
TODO: Separate FlowGraph from PyFlowGraph.
Make a function
code_object = Assemble(flow_graph)
"""
def __init__(self, name, filename, optimized=0, klass=None):
@@ -452,9 +423,8 @@ def MakeCodeObject(self):
opname, oparg = t
method = enc.Get(opname)
if method:
insts[i] = opname, method(enc, oparg)
ass = Assemble(insts)
arg_index = method(enc, oparg)
insts[i] = opname, arg_index
if (self.flags & CO_NEWLOCALS) == 0:
nlocals = 0
@@ -464,14 +434,17 @@ def MakeCodeObject(self):
if self.flags & CO_VARKEYWORDS:
self.argcount -= 1
a = Assembler()
bytecode, firstline, lnotab = a.Run(insts)
return types.CodeType(
self.argcount, nlocals, stacksize, self.flags,
ass.Bytecode(),
bytecode,
tuple(self.consts),
tuple(self.names),
tuple(self.varnames),
self.filename, self.name, ass.firstline,
ass.LineNumberTable(),
self.filename, self.name, firstline,
lnotab,
tuple(self.freevars),
tuple(self.cellvars))
@@ -609,11 +582,27 @@ def nextLine(self, lineno):
self.lastline = lineno
self.lastoff = self.codeOffset
def Bytecode(self):
return ''.join(self.code)
def LineNumberTable(self):
return ''.join(chr(c) for c in self.lnotab)
def Run(self, insts):
for t in insts:
opname = t[0]
if len(t) == 1:
self.addCode(OPNUM[opname])
else:
oparg = t[1]
if opname == "SET_LINENO":
self.nextLine(oparg)
continue
hi, lo = divmod(oparg, 256)
try:
self.addCode(OPNUM[opname], lo, hi)
except ValueError:
print(opname, oparg)
print(OPNUM[opname], lo, hi)
raise
bytecode = ''.join(self.code)
lnotab = ''.join(chr(c) for c in self.lnotab)
return bytecode, self.firstline, lnotab
class StackDepthTracker(object):

0 comments on commit 13db25e

Please sign in to comment.