Debian Codegraph @

rpython/flowspace/bytecode.py

raw symbols SCIP
1 """
2 Bytecode handling classes and functions for use by the flow space.
3 """
4 from rpython.tool.stdlib_opcode import host_bytecode_spec
5 from opcode import EXTENDED_ARG, HAVE_ARGUMENT
6 import opcode
7 from rpython.flowspace.argument import Signature
8
9 try:
10 from __pypy__ import _promote
11 except ImportError:
12 _promote = lambda x: x
13
14 CO_GENERATOR = 0x0020
15 CO_VARARGS = 0x0004
16 CO_VARKEYWORDS = 0x0008
17
18 def cpython_code_signature(code):
19 "([list-of-arg-names], vararg-name-or-None, kwarg-name-or-None)."
20 argcount = code.co_argcount
21 argnames = list(code.co_varnames[:argcount])
22 if code.co_flags & CO_VARARGS:
23 varargname = code.co_varnames[argcount]
24 argcount += 1
25 else:
26 varargname = None
27 if code.co_flags & CO_VARKEYWORDS:
28 kwargname = code.co_varnames[argcount]
29 argcount += 1
30 else:
31 kwargname = None
32 return Signature(argnames, varargname, kwargname)
33
34
35 class BytecodeCorruption(Exception):
36 pass
37
38 HASJREL = b"".join([chr(_opnum in opcode.hasjrel) for _opnum in range(256)])
39
40 class HostCode(object):
41 """
42 A wrapper around a native code object of the host interpreter
43 """
44 opnames = tuple(host_bytecode_spec.method_names)
45
46 def __init__(self, argcount, nlocals, stacksize, flags,
47 code, consts, names, varnames, filename,
48 name, firstlineno, lnotab, freevars):
49 """Initialize a new code object"""
50 assert nlocals >= 0
51 self.co_argcount = argcount
52 self.co_nlocals = nlocals
53 self.co_stacksize = stacksize
54 self.co_flags = flags
55 self.co_code = code
56 self.consts = consts
57 self.names = names
58 self.co_varnames = varnames
59 self.co_freevars = freevars
60 self.co_filename = filename
61 self.co_name = name
62 self.co_firstlineno = firstlineno
63 self.co_lnotab = lnotab
64 self.signature = cpython_code_signature(self)
65
66 @classmethod
67 def _from_code(cls, code):
68 """Initialize the code object from a real (CPython) one.
69 """
70 return cls(code.co_argcount,
71 code.co_nlocals,
72 code.co_stacksize,
73 code.co_flags,
74 code.co_code,
75 list(code.co_consts),
76 list(code.co_names),
77 list(code.co_varnames),
78 code.co_filename,
79 code.co_name,
80 code.co_firstlineno,
81 code.co_lnotab,
82 list(code.co_freevars))
83
84 @property
85 def formalargcount(self):
86 """Total number of arguments passed into the frame, including *vararg
87 and **varkwarg, if they exist."""
88 return self.signature.scope_length()
89
90 def read(self, offset):
91 """
92 Decode the instruction starting at position ``offset``.
93
94 Returns (next_offset, opname, oparg).
95 """
96 co_code = self.co_code
97 opnum = ord(co_code[offset])
98 next_offset = offset + 1
99
100 if opnum >= HAVE_ARGUMENT:
101 lo = ord(co_code[next_offset])
102 hi = ord(co_code[next_offset + 1])
103 next_offset += 2
104 oparg = (hi * 256) | lo
105 else:
106 oparg = 0
107
108 while opnum == EXTENDED_ARG:
109 opnum = ord(co_code[next_offset])
110 if opnum < HAVE_ARGUMENT:
111 raise BytecodeCorruption
112 lo = ord(co_code[next_offset + 1])
113 hi = ord(co_code[next_offset + 2])
114 next_offset += 3
115 oparg = (oparg * 65536) | (hi * 256) | lo
116
117 if ord(HASJREL[opnum]):
118 oparg += next_offset
119 opname = self.opnames[_promote(opnum)]
120 return next_offset, opname, oparg
121
122 @property
123 def is_generator(self):
124 return bool(self.co_flags & CO_GENERATOR)