blob: 4c826a7730cdb08f941b4c0acb58eb080457394e [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",
Alexander Belopolsky74482202012-06-07 14:28:14 -04009 "HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"]
Skip Montanaro3249ccd2003-02-27 21:27:52 +000010
Larry Hastings3a907972013-11-23 14:49:22 -080011# 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
Skip Montanaro3249ccd2003-02-27 21:27:52 +000024cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
25 'is not', 'exception match', 'BAD')
26
27hasconst = []
28hasname = []
29hasjrel = []
30hasjabs = []
31haslocal = []
32hascompare = []
33hasfree = []
Alexander Belopolsky74482202012-06-07 14:28:14 -040034hasnargs = []
Skip Montanaro3249ccd2003-02-27 21:27:52 +000035
36opmap = {}
37opname = [''] * 256
Walter Dörwald70a6b492004-02-12 17:35:32 +000038for op in range(256): opname[op] = '<%r>' % (op,)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000039del op
40
41def def_op(name, op):
42 opname[op] = name
43 opmap[name] = op
44
45def name_op(name, op):
46 def_op(name, op)
47 hasname.append(op)
48
49def jrel_op(name, op):
50 def_op(name, op)
51 hasjrel.append(op)
52
53def jabs_op(name, op):
54 def_op(name, op)
55 hasjabs.append(op)
56
57# Instruction opcodes for compiled code
Guido van Rossumc2e20742006-02-27 22:32:47 +000058# Blank lines correspond to available opcodes
Skip Montanaro3249ccd2003-02-27 21:27:52 +000059
Skip Montanaro3249ccd2003-02-27 21:27:52 +000060def_op('POP_TOP', 1)
61def_op('ROT_TWO', 2)
62def_op('ROT_THREE', 3)
63def_op('DUP_TOP', 4)
Antoine Pitrou74a69fa2010-09-04 18:43:52 +000064def_op('DUP_TOP_TWO', 5)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000065
Raymond Hettinger9c18e812004-06-21 16:31:15 +000066def_op('NOP', 9)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000067def_op('UNARY_POSITIVE', 10)
68def_op('UNARY_NEGATIVE', 11)
69def_op('UNARY_NOT', 12)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000070
71def_op('UNARY_INVERT', 15)
72
Benjamin Petersond51374e2014-04-09 23:55:56 -040073def_op('BINARY_MATRIX_MULTIPLY', 16)
74def_op('INPLACE_MATRIX_MULTIPLY', 17)
75
Skip Montanaro3249ccd2003-02-27 21:27:52 +000076def_op('BINARY_POWER', 19)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000077def_op('BINARY_MULTIPLY', 20)
Neal Norwitzc6d210c2006-03-16 06:02:10 +000078
Skip Montanaro3249ccd2003-02-27 21:27:52 +000079def_op('BINARY_MODULO', 22)
80def_op('BINARY_ADD', 23)
81def_op('BINARY_SUBTRACT', 24)
82def_op('BINARY_SUBSCR', 25)
83def_op('BINARY_FLOOR_DIVIDE', 26)
84def_op('BINARY_TRUE_DIVIDE', 27)
85def_op('INPLACE_FLOOR_DIVIDE', 28)
86def_op('INPLACE_TRUE_DIVIDE', 29)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000087
Yury Selivanov75445082015-05-11 22:57:16 -040088def_op('GET_AITER', 50)
89def_op('GET_ANEXT', 51)
90def_op('BEFORE_ASYNC_WITH', 52)
91
Skip Montanaro3249ccd2003-02-27 21:27:52 +000092def_op('INPLACE_ADD', 55)
93def_op('INPLACE_SUBTRACT', 56)
94def_op('INPLACE_MULTIPLY', 57)
Neal Norwitze7086d42006-03-17 08:59:09 +000095
Skip Montanaro3249ccd2003-02-27 21:27:52 +000096def_op('INPLACE_MODULO', 59)
97def_op('STORE_SUBSCR', 60)
98def_op('DELETE_SUBSCR', 61)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000099def_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)
Yury Selivanov5376ba92015-06-22 12:19:30 -0400106def_op('GET_YIELD_FROM_ITER', 69)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000107
108def_op('PRINT_EXPR', 70)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000109def_op('LOAD_BUILD_CLASS', 71)
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000110def_op('YIELD_FROM', 72)
Yury Selivanov75445082015-05-11 22:57:16 -0400111def_op('GET_AWAITABLE', 73)
Georg Brandl88fc6642007-02-09 21:28:07 +0000112
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000113def_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('BREAK_LOOP', 80)
Yury Selivanov75445082015-05-11 22:57:16 -0400119def_op('WITH_CLEANUP_START', 81)
120def_op('WITH_CLEANUP_FINISH', 82)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000121
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000122def_op('RETURN_VALUE', 83)
123def_op('IMPORT_STAR', 84)
Georg Brandldbdbf752008-07-20 21:38:50 +0000124
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000125def_op('YIELD_VALUE', 86)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000126def_op('POP_BLOCK', 87)
127def_op('END_FINALLY', 88)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000128def_op('POP_EXCEPT', 89)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000129
130HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
131
132name_op('STORE_NAME', 90) # Index in name list
133name_op('DELETE_NAME', 91) # ""
134def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
135jrel_op('FOR_ITER', 93)
Thomas Wouterse8c3d262008-03-18 20:13:50 +0000136def_op('UNPACK_EX', 94)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000137name_op('STORE_ATTR', 95) # Index in name list
138name_op('DELETE_ATTR', 96) # ""
139name_op('STORE_GLOBAL', 97) # ""
140name_op('DELETE_GLOBAL', 98) # ""
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000141def_op('LOAD_CONST', 100) # Index in const list
142hasconst.append(100)
143name_op('LOAD_NAME', 101) # Index in name list
144def_op('BUILD_TUPLE', 102) # Number of tuple items
145def_op('BUILD_LIST', 103) # Number of list items
Guido van Rossum86e58e22006-08-28 15:27:34 +0000146def_op('BUILD_SET', 104) # Number of set items
Christian Heimes99170a52007-12-19 02:07:34 +0000147def_op('BUILD_MAP', 105) # Number of dict entries (upto 255)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000148name_op('LOAD_ATTR', 106) # Index in name list
149def_op('COMPARE_OP', 107) # Comparison operator
150hascompare.append(107)
151name_op('IMPORT_NAME', 108) # Index in name list
152name_op('IMPORT_FROM', 109) # Index in name list
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000153
154jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000155jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
156jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
157jabs_op('JUMP_ABSOLUTE', 113) # ""
158jabs_op('POP_JUMP_IF_FALSE', 114) # ""
159jabs_op('POP_JUMP_IF_TRUE', 115) # ""
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000160
161name_op('LOAD_GLOBAL', 116) # Index in name list
162
163jabs_op('CONTINUE_LOOP', 119) # Target address
164jrel_op('SETUP_LOOP', 120) # Distance to target address
165jrel_op('SETUP_EXCEPT', 121) # ""
166jrel_op('SETUP_FINALLY', 122) # ""
167
168def_op('LOAD_FAST', 124) # Local variable number
169haslocal.append(124)
170def_op('STORE_FAST', 125) # Local variable number
171haslocal.append(125)
172def_op('DELETE_FAST', 126) # Local variable number
173haslocal.append(126)
174
175def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
176def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400177hasnargs.append(131)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000178def_op('MAKE_FUNCTION', 132) # Number of args with default values
179def_op('BUILD_SLICE', 133) # Number of items
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000180def_op('MAKE_CLOSURE', 134)
181def_op('LOAD_CLOSURE', 135)
182hasfree.append(135)
183def_op('LOAD_DEREF', 136)
184hasfree.append(136)
185def_op('STORE_DEREF', 137)
186hasfree.append(137)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000187def_op('DELETE_DEREF', 138)
188hasfree.append(138)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000189
190def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400191hasnargs.append(140)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000192def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400193hasnargs.append(141)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000194def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400195hasnargs.append(142)
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000196
197jrel_op('SETUP_WITH', 143)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000198
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000199def_op('LIST_APPEND', 145)
200def_op('SET_ADD', 146)
201def_op('MAP_ADD', 147)
202
Benjamin Peterson3b0431d2013-04-30 09:41:40 -0400203def_op('LOAD_CLASSDEREF', 148)
204hasfree.append(148)
205
Yury Selivanov75445082015-05-11 22:57:16 -0400206jrel_op('SETUP_ASYNC_WITH', 154)
207
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000208def_op('EXTENDED_ARG', 144)
209EXTENDED_ARG = 144
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000210
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400211def_op('BUILD_LIST_UNPACK', 149)
212def_op('BUILD_MAP_UNPACK', 150)
213def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
214def_op('BUILD_TUPLE_UNPACK', 152)
215def_op('BUILD_SET_UNPACK', 153)
216
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000217del def_op, name_op, jrel_op, jabs_op