blob: 9517c4314a3bb4b14d1412c16f783b02a0192bb7 [file] [log] [blame]
Skip Montanaro3249ccd2003-02-27 21:27:52 +00001
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"]
10
11cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
12 'is not', 'exception match', 'BAD')
13
14hasconst = []
15hasname = []
16hasjrel = []
17hasjabs = []
18haslocal = []
19hascompare = []
20hasfree = []
21
22opmap = {}
23opname = [''] * 256
Walter Dörwald70a6b492004-02-12 17:35:32 +000024for op in range(256): opname[op] = '<%r>' % (op,)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000025del op
26
27def def_op(name, op):
28 opname[op] = name
29 opmap[name] = op
30
31def name_op(name, op):
32 def_op(name, op)
33 hasname.append(op)
34
35def jrel_op(name, op):
36 def_op(name, op)
37 hasjrel.append(op)
38
39def jabs_op(name, op):
40 def_op(name, op)
41 hasjabs.append(op)
42
43# Instruction opcodes for compiled code
44
45def_op('STOP_CODE', 0)
46def_op('POP_TOP', 1)
47def_op('ROT_TWO', 2)
48def_op('ROT_THREE', 3)
49def_op('DUP_TOP', 4)
50def_op('ROT_FOUR', 5)
51
Raymond Hettinger9c18e812004-06-21 16:31:15 +000052def_op('NOP', 9)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000053def_op('UNARY_POSITIVE', 10)
54def_op('UNARY_NEGATIVE', 11)
55def_op('UNARY_NOT', 12)
56def_op('UNARY_CONVERT', 13)
57
58def_op('UNARY_INVERT', 15)
59
Raymond Hettingerdd80f762004-03-07 07:31:06 +000060def_op('LIST_APPEND', 18)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000061def_op('BINARY_POWER', 19)
62
63def_op('BINARY_MULTIPLY', 20)
64def_op('BINARY_DIVIDE', 21)
65def_op('BINARY_MODULO', 22)
66def_op('BINARY_ADD', 23)
67def_op('BINARY_SUBTRACT', 24)
68def_op('BINARY_SUBSCR', 25)
69def_op('BINARY_FLOOR_DIVIDE', 26)
70def_op('BINARY_TRUE_DIVIDE', 27)
71def_op('INPLACE_FLOOR_DIVIDE', 28)
72def_op('INPLACE_TRUE_DIVIDE', 29)
73
74def_op('SLICE+0', 30)
75def_op('SLICE+1', 31)
76def_op('SLICE+2', 32)
77def_op('SLICE+3', 33)
78
79def_op('STORE_SLICE+0', 40)
80def_op('STORE_SLICE+1', 41)
81def_op('STORE_SLICE+2', 42)
82def_op('STORE_SLICE+3', 43)
83
84def_op('DELETE_SLICE+0', 50)
85def_op('DELETE_SLICE+1', 51)
86def_op('DELETE_SLICE+2', 52)
87def_op('DELETE_SLICE+3', 53)
88
89def_op('INPLACE_ADD', 55)
90def_op('INPLACE_SUBTRACT', 56)
91def_op('INPLACE_MULTIPLY', 57)
92def_op('INPLACE_DIVIDE', 58)
93def_op('INPLACE_MODULO', 59)
94def_op('STORE_SUBSCR', 60)
95def_op('DELETE_SUBSCR', 61)
96
97def_op('BINARY_LSHIFT', 62)
98def_op('BINARY_RSHIFT', 63)
99def_op('BINARY_AND', 64)
100def_op('BINARY_XOR', 65)
101def_op('BINARY_OR', 66)
102def_op('INPLACE_POWER', 67)
103def_op('GET_ITER', 68)
104
105def_op('PRINT_EXPR', 70)
106def_op('PRINT_ITEM', 71)
107def_op('PRINT_NEWLINE', 72)
108def_op('PRINT_ITEM_TO', 73)
109def_op('PRINT_NEWLINE_TO', 74)
110def_op('INPLACE_LSHIFT', 75)
111def_op('INPLACE_RSHIFT', 76)
112def_op('INPLACE_AND', 77)
113def_op('INPLACE_XOR', 78)
114def_op('INPLACE_OR', 79)
115def_op('BREAK_LOOP', 80)
116
117def_op('LOAD_LOCALS', 82)
118def_op('RETURN_VALUE', 83)
119def_op('IMPORT_STAR', 84)
120def_op('EXEC_STMT', 85)
121def_op('YIELD_VALUE', 86)
122
123def_op('POP_BLOCK', 87)
124def_op('END_FINALLY', 88)
125def_op('BUILD_CLASS', 89)
126
127HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
128
129name_op('STORE_NAME', 90) # Index in name list
130name_op('DELETE_NAME', 91) # ""
131def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
132jrel_op('FOR_ITER', 93)
133
134name_op('STORE_ATTR', 95) # Index in name list
135name_op('DELETE_ATTR', 96) # ""
136name_op('STORE_GLOBAL', 97) # ""
137name_op('DELETE_GLOBAL', 98) # ""
138def_op('DUP_TOPX', 99) # number of items to duplicate
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_MAP', 104) # Always zero for now
145name_op('LOAD_ATTR', 105) # Index in name list
146def_op('COMPARE_OP', 106) # Comparison operator
147hascompare.append(106)
148name_op('IMPORT_NAME', 107) # Index in name list
149name_op('IMPORT_FROM', 108) # Index in name list
150
151jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
152jrel_op('JUMP_IF_FALSE', 111) # ""
153jrel_op('JUMP_IF_TRUE', 112) # ""
154jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
155
156name_op('LOAD_GLOBAL', 116) # Index in name list
157
158jabs_op('CONTINUE_LOOP', 119) # Target address
159jrel_op('SETUP_LOOP', 120) # Distance to target address
160jrel_op('SETUP_EXCEPT', 121) # ""
161jrel_op('SETUP_FINALLY', 122) # ""
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 + (#kwargs << 8)
172def_op('MAKE_FUNCTION', 132) # Number of args with default values
173def_op('BUILD_SLICE', 133) # Number of items
174
175def_op('MAKE_CLOSURE', 134)
176def_op('LOAD_CLOSURE', 135)
177hasfree.append(135)
178def_op('LOAD_DEREF', 136)
179hasfree.append(136)
180def_op('STORE_DEREF', 137)
181hasfree.append(137)
182
183def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
184def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
185def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
186
187def_op('EXTENDED_ARG', 143)
188EXTENDED_ARG = 143
189
190del def_op, name_op, jrel_op, jabs_op