| 1 |
class _BaseOpcodeDesc(object): |
| 2 |
def __init__(self, bytecode_spec, name, index, methodname): |
| 3 |
self.bytecode_spec = bytecode_spec |
| 4 |
self.name = name |
| 5 |
self.methodname = methodname |
| 6 |
self.index = index |
| 7 |
self.hasarg = index >= self.HAVE_ARGUMENT |
| 8 |
|
| 9 |
def _freeze_(self): |
| 10 |
return True |
| 11 |
|
| 12 |
|
| 13 |
opcodeorder = [124, 125, 100, 105, 1, 131, 116, 111, 106, 83, 23, 93, 113, 25, 95, 64, 112, 66, 102, 110, 60, 92, 62, 120, 68, 87, 32, 136, 4, 103, 24, 63, 18, 65, 15, 55, 121, 3, 101, 22, 12, 80, 86, 135, 126, 90, 140, 104, 2, 33, 20, 108, 107, 31, 134, 132, 88, 30, 133, 130, 137, 141, 61, 122, 11, 40, 74, 73, 51, 96, 21, 42, 56, 85, 82, 89, 142, 77, 78, 79, 91, 76, 97, 57, 19, 43, 84, 50, 41, 99, 53, 26] |
| 14 |
|
| 15 |
def sortkey(self): |
| 16 |
try: |
| 17 |
i = self.opcodeorder.index(self.index) |
| 18 |
except ValueError: |
| 19 |
i = 1000000 |
| 20 |
return i, self.index |
| 21 |
|
| 22 |
def __cmp__(self, other): |
| 23 |
return (cmp(self.__class__, other.__class__) or |
| 24 |
cmp(self.sortkey(), other.sortkey())) |
| 25 |
|
| 26 |
def __str__(self): |
| 27 |
return "<OpcodeDesc code=%d name=%s at %x>" % (self.index, self.name, id(self)) |
| 28 |
|
| 29 |
__repr__ = __str__ |
| 30 |
|
| 31 |
class _baseopcodedesc: |
| 32 |
"""A namespace mapping OPCODE_NAME to _BaseOpcodeDescs.""" |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
class BytecodeSpec(object): |
| 37 |
"""A bunch of mappings describing a bytecode instruction set.""" |
| 38 |
|
| 39 |
def __init__(self, name, opmap, HAVE_ARGUMENT): |
| 40 |
"""NOT_RPYTHON.""" |
| 41 |
class OpcodeDesc(_BaseOpcodeDesc): |
| 42 |
HAVE_ARGUMENT = HAVE_ARGUMENT |
| 43 |
class opcodedesc(_baseopcodedesc): |
| 44 |
"""A namespace mapping OPCODE_NAME to OpcodeDescs.""" |
| 45 |
|
| 46 |
self.name = name |
| 47 |
self.OpcodeDesc = OpcodeDesc |
| 48 |
self.opcodedesc = opcodedesc |
| 49 |
self.HAVE_ARGUMENT = HAVE_ARGUMENT |
| 50 |
|
| 51 |
self.opmap = opmap |
| 52 |
|
| 53 |
self.method_names = tbl = ['MISSING_OPCODE'] * 256 |
| 54 |
|
| 55 |
self.opdescmap = {} |
| 56 |
for name, index in opmap.items(): |
| 57 |
tbl[index] = methodname = name.replace('+', '_') |
| 58 |
desc = OpcodeDesc(self, name, index, methodname) |
| 59 |
setattr(self.opcodedesc, methodname, desc) |
| 60 |
self.opdescmap[index] = desc |
| 61 |
|
| 62 |
self.ordered_opdescs = lst = self.opdescmap.values() |
| 63 |
lst.sort() |
| 64 |
|
| 65 |
def to_globals(self, globals_dict): |
| 66 |
"""NOT_RPYTHON. Add individual opcodes to the module constants.""" |
| 67 |
for name, value in self.opmap.iteritems(): |
| 68 |
|
| 69 |
if name.endswith('+0'): |
| 70 |
name = name[:-2] |
| 71 |
|
| 72 |
elif name.endswith(('+1', '+2', '+3')): |
| 73 |
continue |
| 74 |
globals_dict[name] = value |
| 75 |
|
| 76 |
def __str__(self): |
| 77 |
return "<%s bytecode>" % (self.name,) |
| 78 |
|
| 79 |
__repr__ = __str__ |
| 80 |
|
| 81 |
from opcode import opmap, HAVE_ARGUMENT |
| 82 |
|
| 83 |
host_bytecode_spec = BytecodeSpec('host', opmap, HAVE_ARGUMENT) |