Author: Ronan Lamy <[email protected]>
Branch: framestate
Changeset: r74670:7818088e6df6
Date: 2014-05-10 17:17 +0100
http://bitbucket.org/pypy/pypy/changeset/7818088e6df6/
Log: store opcode definitions in the bc_reader object
diff --git a/rpython/flowspace/bytecode.py b/rpython/flowspace/bytecode.py
--- a/rpython/flowspace/bytecode.py
+++ b/rpython/flowspace/bytecode.py
@@ -80,6 +80,22 @@
return bc_reader.read(self, offset)
class BytecodeReader(object):
+ def __init__(self, opnames):
+ self.opnames = opnames
+ self.num2cls = {}
+
+ def register_name(self, name, InstrClass):
+ num = self.opnames.index(name)
+ self.num2cls[num] = InstrClass
+ return num
+
+ def register_opcode(self, cls):
+ """Class decorator: register opcode class as real Python opcode"""
+ name = cls.__name__
+ cls.name = name
+ cls.num = self.register_name(name, cls)
+ return cls
+
def read(self, code, offset):
"""
Decode the instruction starting at position ``offset``.
@@ -112,23 +128,19 @@
elif opnum in opcode.hasname:
oparg = code.names[oparg]
try:
- op = BCInstruction.num2op[opnum].decode(oparg, offset, code)
+ op = self.num2cls[opnum].decode(oparg, offset, code)
except KeyError:
- op = GenericOpcode(opnum, oparg, offset)
+ op = GenericOpcode(self.opnames[opnum], opnum, oparg, offset)
return next_offset, op
-bc_reader = BytecodeReader()
+bc_reader = BytecodeReader(host_bytecode_spec.method_names)
-OPNAMES = host_bytecode_spec.method_names
-
class BCInstruction(object):
"""
A bytecode instruction, comprising an opcode and an optional argument.
"""
- num2op = {}
-
def __init__(self, arg, offset=-1):
self.arg = arg
self.offset = offset
@@ -140,21 +152,12 @@
def eval(self, ctx):
pass
- @classmethod
- def register_name(cls, name, op_class):
- try:
- num = OPNAMES.index(name)
- cls.num2op[num] = op_class
- return num
- except ValueError:
- return -1
-
def __repr__(self):
return "%s(%s)" % (self.name, self.arg)
class GenericOpcode(BCInstruction):
- def __init__(self, opcode, arg, offset=-1):
- self.name = OPNAMES[opcode]
+ def __init__(self, name, opcode, arg, offset=-1):
+ self.name = name
self.num = opcode
self.arg = arg
self.offset = offset
@@ -163,14 +166,7 @@
return getattr(ctx, self.name)(self.arg)
-def register_opcode(cls):
- """Class decorator: register opcode class as real Python opcode"""
- name = cls.__name__
- cls.name = name
- cls.num = BCInstruction.register_name(name, cls)
- return cls
-
-@register_opcode
+@bc_reader.register_opcode
class LOAD_CONST(BCInstruction):
@staticmethod
def decode(arg, offset, code):
diff --git a/rpython/flowspace/test/test_objspace.py
b/rpython/flowspace/test/test_objspace.py
--- a/rpython/flowspace/test/test_objspace.py
+++ b/rpython/flowspace/test/test_objspace.py
@@ -8,7 +8,7 @@
from rpython.translator.simplify import simplify_graph
from rpython.flowspace.objspace import build_flow
from rpython.flowspace.flowcontext import FlowingError, FlowContext
-from rpython.flowspace.bytecode import BCInstruction
+from rpython.flowspace.bytecode import bc_reader
from rpython.conftest import option
from rpython.tool.stdlib_opcode import host_bytecode_spec
@@ -57,7 +57,7 @@
opnames = set(host_bytecode_spec.method_names)
methods = set([name for name in dir(FlowContext) if name.upper() == name])
handled_elsewhere = set(['EXTENDED_ARG'])
- opcode_classes = set([cls.name for cls in BCInstruction.num2op.values()])
+ opcode_classes = set([cls.name for cls in bc_reader.num2cls.values()])
missing = opnames - methods - handled_elsewhere - opcode_classes
assert not missing
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit