1
2"""
3opcode module - potentially shared between dis and other modules which
4operate on bytecodes (e.g. peephole optimizers).
5"""
6
7__all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
8           "haslocal", "hascompare", "hasfree", "opname", "opmap",
9           "HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"]
10
11# It's a chicken-and-egg I'm afraid:
12# We're imported before _opcode's made.
13# With exception unheeded
14# (stack_effect is not needed)
15# Both our chickens and eggs are allayed.
16#     --Larry Hastings, 2013/11/23
17
18try:
19    from _opcode import stack_effect
20    __all__.append('stack_effect')
21except ImportError:
22    pass
23
24cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
25        'is not', 'exception match', 'BAD')
26
27hasconst = []
28hasname = []
29hasjrel = []
30hasjabs = []
31haslocal = []
32hascompare = []
33hasfree = []
34hasnargs = [] # unused
35
36opmap = {}
37opname = ['<%r>' % (op,) for op in range(256)]
38
39def def_op(name, op):
40    opname[op] = name
41    opmap[name] = op
42
43def name_op(name, op):
44    def_op(name, op)
45    hasname.append(op)
46
47def jrel_op(name, op):
48    def_op(name, op)
49    hasjrel.append(op)
50
51def jabs_op(name, op):
52    def_op(name, op)
53    hasjabs.append(op)
54
55# Instruction opcodes for compiled code
56# Blank lines correspond to available opcodes
57
58def_op('POP_TOP', 1)
59def_op('ROT_TWO', 2)
60def_op('ROT_THREE', 3)
61def_op('DUP_TOP', 4)
62def_op('DUP_TOP_TWO', 5)
63def_op('ROT_FOUR', 6)
64
65def_op('NOP', 9)
66def_op('UNARY_POSITIVE', 10)
67def_op('UNARY_NEGATIVE', 11)
68def_op('UNARY_NOT', 12)
69
70def_op('UNARY_INVERT', 15)
71
72def_op('BINARY_MATRIX_MULTIPLY', 16)
73def_op('INPLACE_MATRIX_MULTIPLY', 17)
74
75def_op('BINARY_POWER', 19)
76def_op('BINARY_MULTIPLY', 20)
77
78def_op('BINARY_MODULO', 22)
79def_op('BINARY_ADD', 23)
80def_op('BINARY_SUBTRACT', 24)
81def_op('BINARY_SUBSCR', 25)
82def_op('BINARY_FLOOR_DIVIDE', 26)
83def_op('BINARY_TRUE_DIVIDE', 27)
84def_op('INPLACE_FLOOR_DIVIDE', 28)
85def_op('INPLACE_TRUE_DIVIDE', 29)
86
87def_op('GET_AITER', 50)
88def_op('GET_ANEXT', 51)
89def_op('BEFORE_ASYNC_WITH', 52)
90def_op('BEGIN_FINALLY', 53)
91def_op('END_ASYNC_FOR', 54)
92def_op('INPLACE_ADD', 55)
93def_op('INPLACE_SUBTRACT', 56)
94def_op('INPLACE_MULTIPLY', 57)
95
96def_op('INPLACE_MODULO', 59)
97def_op('STORE_SUBSCR', 60)
98def_op('DELETE_SUBSCR', 61)
99def_op('BINARY_LSHIFT', 62)
100def_op('BINARY_RSHIFT', 63)
101def_op('BINARY_AND', 64)
102def_op('BINARY_XOR', 65)
103def_op('BINARY_OR', 66)
104def_op('INPLACE_POWER', 67)
105def_op('GET_ITER', 68)
106def_op('GET_YIELD_FROM_ITER', 69)
107
108def_op('PRINT_EXPR', 70)
109def_op('LOAD_BUILD_CLASS', 71)
110def_op('YIELD_FROM', 72)
111def_op('GET_AWAITABLE', 73)
112
113def_op('INPLACE_LSHIFT', 75)
114def_op('INPLACE_RSHIFT', 76)
115def_op('INPLACE_AND', 77)
116def_op('INPLACE_XOR', 78)
117def_op('INPLACE_OR', 79)
118def_op('WITH_CLEANUP_START', 81)
119def_op('WITH_CLEANUP_FINISH', 82)
120def_op('RETURN_VALUE', 83)
121def_op('IMPORT_STAR', 84)
122def_op('SETUP_ANNOTATIONS', 85)
123def_op('YIELD_VALUE', 86)
124def_op('POP_BLOCK', 87)
125def_op('END_FINALLY', 88)
126def_op('POP_EXCEPT', 89)
127
128HAVE_ARGUMENT = 90              # Opcodes from here have an argument:
129
130name_op('STORE_NAME', 90)       # Index in name list
131name_op('DELETE_NAME', 91)      # ""
132def_op('UNPACK_SEQUENCE', 92)   # Number of tuple items
133jrel_op('FOR_ITER', 93)
134def_op('UNPACK_EX', 94)
135name_op('STORE_ATTR', 95)       # Index in name list
136name_op('DELETE_ATTR', 96)      # ""
137name_op('STORE_GLOBAL', 97)     # ""
138name_op('DELETE_GLOBAL', 98)    # ""
139def_op('LOAD_CONST', 100)       # Index in const list
140hasconst.append(100)
141name_op('LOAD_NAME', 101)       # Index in name list
142def_op('BUILD_TUPLE', 102)      # Number of tuple items
143def_op('BUILD_LIST', 103)       # Number of list items
144def_op('BUILD_SET', 104)        # Number of set items
145def_op('BUILD_MAP', 105)        # Number of dict entries
146name_op('LOAD_ATTR', 106)       # Index in name list
147def_op('COMPARE_OP', 107)       # Comparison operator
148hascompare.append(107)
149name_op('IMPORT_NAME', 108)     # Index in name list
150name_op('IMPORT_FROM', 109)     # Index in name list
151
152jrel_op('JUMP_FORWARD', 110)    # Number of bytes to skip
153jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
154jabs_op('JUMP_IF_TRUE_OR_POP', 112)  # ""
155jabs_op('JUMP_ABSOLUTE', 113)        # ""
156jabs_op('POP_JUMP_IF_FALSE', 114)    # ""
157jabs_op('POP_JUMP_IF_TRUE', 115)     # ""
158
159name_op('LOAD_GLOBAL', 116)     # Index in name list
160
161jrel_op('SETUP_FINALLY', 122)   # Distance to target address
162
163def_op('LOAD_FAST', 124)        # Local variable number
164haslocal.append(124)
165def_op('STORE_FAST', 125)       # Local variable number
166haslocal.append(125)
167def_op('DELETE_FAST', 126)      # Local variable number
168haslocal.append(126)
169
170def_op('RAISE_VARARGS', 130)    # Number of raise arguments (1, 2, or 3)
171def_op('CALL_FUNCTION', 131)    # #args
172def_op('MAKE_FUNCTION', 132)    # Flags
173def_op('BUILD_SLICE', 133)      # Number of items
174def_op('LOAD_CLOSURE', 135)
175hasfree.append(135)
176def_op('LOAD_DEREF', 136)
177hasfree.append(136)
178def_op('STORE_DEREF', 137)
179hasfree.append(137)
180def_op('DELETE_DEREF', 138)
181hasfree.append(138)
182
183def_op('CALL_FUNCTION_KW', 141)  # #args + #kwargs
184def_op('CALL_FUNCTION_EX', 142)  # Flags
185
186jrel_op('SETUP_WITH', 143)
187
188def_op('LIST_APPEND', 145)
189def_op('SET_ADD', 146)
190def_op('MAP_ADD', 147)
191
192def_op('LOAD_CLASSDEREF', 148)
193hasfree.append(148)
194
195def_op('EXTENDED_ARG', 144)
196EXTENDED_ARG = 144
197
198def_op('BUILD_LIST_UNPACK', 149)
199def_op('BUILD_MAP_UNPACK', 150)
200def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
201def_op('BUILD_TUPLE_UNPACK', 152)
202def_op('BUILD_SET_UNPACK', 153)
203
204jrel_op('SETUP_ASYNC_WITH', 154)
205
206def_op('FORMAT_VALUE', 155)
207def_op('BUILD_CONST_KEY_MAP', 156)
208def_op('BUILD_STRING', 157)
209def_op('BUILD_TUPLE_UNPACK_WITH_CALL', 158)
210
211name_op('LOAD_METHOD', 160)
212def_op('CALL_METHOD', 161)
213jrel_op('CALL_FINALLY', 162)
214def_op('POP_FINALLY', 163)
215
216del def_op, name_op, jrel_op, jabs_op
217