[pypy-commit] pypy framestate: Create BCInstruction class
rlamy
noreply at buildbot.pypy.org
Mon Nov 24 17:29:27 CET 2014
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: framestate
Changeset: r74661:e6c01f65048e
Date: 2014-11-19 19:15 +0000
http://bitbucket.org/pypy/pypy/changeset/e6c01f65048e/
Log: Create BCInstruction class
diff --git a/rpython/flowspace/bytecode.py b/rpython/flowspace/bytecode.py
--- a/rpython/flowspace/bytecode.py
+++ b/rpython/flowspace/bytecode.py
@@ -35,8 +35,6 @@
"""
A wrapper around a native code object of the host interpreter
"""
- opnames = host_bytecode_spec.method_names
-
def __init__(self, argcount, nlocals, stacksize, flags,
code, consts, names, varnames, filename,
name, firstlineno, lnotab, freevars):
@@ -85,7 +83,7 @@
"""
Decode the instruction starting at position ``offset``.
- Returns (next_offset, opname, oparg).
+ Returns (next_offset, instruction).
"""
co_code = self.co_code
opnum = ord(co_code[offset])
@@ -110,9 +108,20 @@
if opnum in opcode.hasjrel:
oparg += next_offset
- opname = self.opnames[opnum]
- return next_offset, opname, oparg
+ return next_offset, BCInstruction(opnum, oparg, pos)
@property
def is_generator(self):
return bool(self.co_flags & CO_GENERATOR)
+
+OPNAMES = host_bytecode_spec.method_names
+
+class BCInstruction(object):
+ """A bytecode instruction, comprising an opcode and an optional argument."""
+ def __init__(self, opcode, arg, offset=-1):
+ self.name = OPNAMES[opcode]
+ self.arg = arg
+ self.offset = offset
+
+ def eval(self, ctx):
+ return getattr(ctx, self.name)(self.arg)
diff --git a/rpython/flowspace/flowcontext.py b/rpython/flowspace/flowcontext.py
--- a/rpython/flowspace/flowcontext.py
+++ b/rpython/flowspace/flowcontext.py
@@ -503,9 +503,9 @@
def handle_bytecode(self, next_offset):
self.last_offset = next_offset
- next_offset, methodname, oparg = self.pycode.read(next_offset)
+ next_offset, instr = self.pycode.read(next_offset)
try:
- offset = getattr(self, methodname)(oparg)
+ res = instr.eval(self)
return offset if offset is not None else next_offset
except FlowSignal as signal:
return self.unroll(signal)
More information about the pypy-commit
mailing list