blob: 0bd1ee679cfdaeed3826df934160f74d852e2ceb [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
Skip Montanaro3249ccd2003-02-27 21:27:52 +000073def_op('BINARY_POWER', 19)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000074def_op('BINARY_MULTIPLY', 20)
Neal Norwitzc6d210c2006-03-16 06:02:10 +000075
Skip Montanaro3249ccd2003-02-27 21:27:52 +000076def_op('BINARY_MODULO', 22)
77def_op('BINARY_ADD', 23)
78def_op('BINARY_SUBTRACT', 24)
79def_op('BINARY_SUBSCR', 25)
80def_op('BINARY_FLOOR_DIVIDE', 26)
81def_op('BINARY_TRUE_DIVIDE', 27)
82def_op('INPLACE_FLOOR_DIVIDE', 28)
83def_op('INPLACE_TRUE_DIVIDE', 29)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000084
Christian Heimes99170a52007-12-19 02:07:34 +000085def_op('STORE_MAP', 54)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000086def_op('INPLACE_ADD', 55)
87def_op('INPLACE_SUBTRACT', 56)
88def_op('INPLACE_MULTIPLY', 57)
Neal Norwitze7086d42006-03-17 08:59:09 +000089
Skip Montanaro3249ccd2003-02-27 21:27:52 +000090def_op('INPLACE_MODULO', 59)
91def_op('STORE_SUBSCR', 60)
92def_op('DELETE_SUBSCR', 61)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000093def_op('BINARY_LSHIFT', 62)
94def_op('BINARY_RSHIFT', 63)
95def_op('BINARY_AND', 64)
96def_op('BINARY_XOR', 65)
97def_op('BINARY_OR', 66)
98def_op('INPLACE_POWER', 67)
99def_op('GET_ITER', 68)
100
101def_op('PRINT_EXPR', 70)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000102def_op('LOAD_BUILD_CLASS', 71)
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000103def_op('YIELD_FROM', 72)
Georg Brandl88fc6642007-02-09 21:28:07 +0000104
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000105def_op('INPLACE_LSHIFT', 75)
106def_op('INPLACE_RSHIFT', 76)
107def_op('INPLACE_AND', 77)
108def_op('INPLACE_XOR', 78)
109def_op('INPLACE_OR', 79)
110def_op('BREAK_LOOP', 80)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000111def_op('WITH_CLEANUP', 81)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000112
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000113def_op('RETURN_VALUE', 83)
114def_op('IMPORT_STAR', 84)
Georg Brandldbdbf752008-07-20 21:38:50 +0000115
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000116def_op('YIELD_VALUE', 86)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000117def_op('POP_BLOCK', 87)
118def_op('END_FINALLY', 88)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000119def_op('POP_EXCEPT', 89)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000120
121HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
122
123name_op('STORE_NAME', 90) # Index in name list
124name_op('DELETE_NAME', 91) # ""
125def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
126jrel_op('FOR_ITER', 93)
Thomas Wouterse8c3d262008-03-18 20:13:50 +0000127def_op('UNPACK_EX', 94)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000128name_op('STORE_ATTR', 95) # Index in name list
129name_op('DELETE_ATTR', 96) # ""
130name_op('STORE_GLOBAL', 97) # ""
131name_op('DELETE_GLOBAL', 98) # ""
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000132def_op('LOAD_CONST', 100) # Index in const list
133hasconst.append(100)
134name_op('LOAD_NAME', 101) # Index in name list
135def_op('BUILD_TUPLE', 102) # Number of tuple items
136def_op('BUILD_LIST', 103) # Number of list items
Guido van Rossum86e58e22006-08-28 15:27:34 +0000137def_op('BUILD_SET', 104) # Number of set items
Christian Heimes99170a52007-12-19 02:07:34 +0000138def_op('BUILD_MAP', 105) # Number of dict entries (upto 255)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000139name_op('LOAD_ATTR', 106) # Index in name list
140def_op('COMPARE_OP', 107) # Comparison operator
141hascompare.append(107)
142name_op('IMPORT_NAME', 108) # Index in name list
143name_op('IMPORT_FROM', 109) # Index in name list
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000144
145jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000146jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
147jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
148jabs_op('JUMP_ABSOLUTE', 113) # ""
149jabs_op('POP_JUMP_IF_FALSE', 114) # ""
150jabs_op('POP_JUMP_IF_TRUE', 115) # ""
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000151
152name_op('LOAD_GLOBAL', 116) # Index in name list
153
154jabs_op('CONTINUE_LOOP', 119) # Target address
155jrel_op('SETUP_LOOP', 120) # Distance to target address
156jrel_op('SETUP_EXCEPT', 121) # ""
157jrel_op('SETUP_FINALLY', 122) # ""
158
159def_op('LOAD_FAST', 124) # Local variable number
160haslocal.append(124)
161def_op('STORE_FAST', 125) # Local variable number
162haslocal.append(125)
163def_op('DELETE_FAST', 126) # Local variable number
164haslocal.append(126)
165
166def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
167def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400168hasnargs.append(131)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000169def_op('MAKE_FUNCTION', 132) # Number of args with default values
170def_op('BUILD_SLICE', 133) # Number of items
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000171def_op('MAKE_CLOSURE', 134)
172def_op('LOAD_CLOSURE', 135)
173hasfree.append(135)
174def_op('LOAD_DEREF', 136)
175hasfree.append(136)
176def_op('STORE_DEREF', 137)
177hasfree.append(137)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000178def_op('DELETE_DEREF', 138)
179hasfree.append(138)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000180
181def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400182hasnargs.append(140)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000183def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400184hasnargs.append(141)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000185def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400186hasnargs.append(142)
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000187
188jrel_op('SETUP_WITH', 143)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000189
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000190def_op('LIST_APPEND', 145)
191def_op('SET_ADD', 146)
192def_op('MAP_ADD', 147)
193
Benjamin Peterson3b0431d2013-04-30 09:41:40 -0400194def_op('LOAD_CLASSDEREF', 148)
195hasfree.append(148)
196
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000197def_op('EXTENDED_ARG', 144)
198EXTENDED_ARG = 144
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000199
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000200del def_op, name_op, jrel_op, jabs_op