blob: a639fe322b7626f68c4222b590753f8d0fda1491 [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
11cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
12 'is not', 'exception match', 'BAD')
13
14hasconst = []
15hasname = []
16hasjrel = []
17hasjabs = []
18haslocal = []
19hascompare = []
20hasfree = []
Alexander Belopolsky74482202012-06-07 14:28:14 -040021hasnargs = []
Skip Montanaro3249ccd2003-02-27 21:27:52 +000022
23opmap = {}
24opname = [''] * 256
Walter Dörwald70a6b492004-02-12 17:35:32 +000025for op in range(256): opname[op] = '<%r>' % (op,)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000026del op
27
28def def_op(name, op):
29 opname[op] = name
30 opmap[name] = op
31
32def name_op(name, op):
33 def_op(name, op)
34 hasname.append(op)
35
36def jrel_op(name, op):
37 def_op(name, op)
38 hasjrel.append(op)
39
40def jabs_op(name, op):
41 def_op(name, op)
42 hasjabs.append(op)
43
44# Instruction opcodes for compiled code
Guido van Rossumc2e20742006-02-27 22:32:47 +000045# Blank lines correspond to available opcodes
Skip Montanaro3249ccd2003-02-27 21:27:52 +000046
Skip Montanaro3249ccd2003-02-27 21:27:52 +000047def_op('POP_TOP', 1)
48def_op('ROT_TWO', 2)
49def_op('ROT_THREE', 3)
50def_op('DUP_TOP', 4)
Antoine Pitrou74a69fa2010-09-04 18:43:52 +000051def_op('DUP_TOP_TWO', 5)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000052
Raymond Hettinger9c18e812004-06-21 16:31:15 +000053def_op('NOP', 9)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000054def_op('UNARY_POSITIVE', 10)
55def_op('UNARY_NEGATIVE', 11)
56def_op('UNARY_NOT', 12)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000057
58def_op('UNARY_INVERT', 15)
59
Skip Montanaro3249ccd2003-02-27 21:27:52 +000060def_op('BINARY_POWER', 19)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000061def_op('BINARY_MULTIPLY', 20)
Neal Norwitzc6d210c2006-03-16 06:02:10 +000062
Skip Montanaro3249ccd2003-02-27 21:27:52 +000063def_op('BINARY_MODULO', 22)
64def_op('BINARY_ADD', 23)
65def_op('BINARY_SUBTRACT', 24)
66def_op('BINARY_SUBSCR', 25)
67def_op('BINARY_FLOOR_DIVIDE', 26)
68def_op('BINARY_TRUE_DIVIDE', 27)
69def_op('INPLACE_FLOOR_DIVIDE', 28)
70def_op('INPLACE_TRUE_DIVIDE', 29)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000071
Christian Heimes99170a52007-12-19 02:07:34 +000072def_op('STORE_MAP', 54)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000073def_op('INPLACE_ADD', 55)
74def_op('INPLACE_SUBTRACT', 56)
75def_op('INPLACE_MULTIPLY', 57)
Neal Norwitze7086d42006-03-17 08:59:09 +000076
Skip Montanaro3249ccd2003-02-27 21:27:52 +000077def_op('INPLACE_MODULO', 59)
78def_op('STORE_SUBSCR', 60)
79def_op('DELETE_SUBSCR', 61)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000080def_op('BINARY_LSHIFT', 62)
81def_op('BINARY_RSHIFT', 63)
82def_op('BINARY_AND', 64)
83def_op('BINARY_XOR', 65)
84def_op('BINARY_OR', 66)
85def_op('INPLACE_POWER', 67)
86def_op('GET_ITER', 68)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000087def_op('STORE_LOCALS', 69)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000088
89def_op('PRINT_EXPR', 70)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000090def_op('LOAD_BUILD_CLASS', 71)
Nick Coghlan1f7ce622012-01-13 21:43:40 +100091def_op('YIELD_FROM', 72)
Georg Brandl88fc6642007-02-09 21:28:07 +000092
Skip Montanaro3249ccd2003-02-27 21:27:52 +000093def_op('INPLACE_LSHIFT', 75)
94def_op('INPLACE_RSHIFT', 76)
95def_op('INPLACE_AND', 77)
96def_op('INPLACE_XOR', 78)
97def_op('INPLACE_OR', 79)
98def_op('BREAK_LOOP', 80)
Guido van Rossumc2e20742006-02-27 22:32:47 +000099def_op('WITH_CLEANUP', 81)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000100
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000101def_op('RETURN_VALUE', 83)
102def_op('IMPORT_STAR', 84)
Georg Brandldbdbf752008-07-20 21:38:50 +0000103
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000104def_op('YIELD_VALUE', 86)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000105def_op('POP_BLOCK', 87)
106def_op('END_FINALLY', 88)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000107def_op('POP_EXCEPT', 89)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000108
109HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
110
111name_op('STORE_NAME', 90) # Index in name list
112name_op('DELETE_NAME', 91) # ""
113def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
114jrel_op('FOR_ITER', 93)
Thomas Wouterse8c3d262008-03-18 20:13:50 +0000115def_op('UNPACK_EX', 94)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000116name_op('STORE_ATTR', 95) # Index in name list
117name_op('DELETE_ATTR', 96) # ""
118name_op('STORE_GLOBAL', 97) # ""
119name_op('DELETE_GLOBAL', 98) # ""
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000120def_op('LOAD_CONST', 100) # Index in const list
121hasconst.append(100)
122name_op('LOAD_NAME', 101) # Index in name list
123def_op('BUILD_TUPLE', 102) # Number of tuple items
124def_op('BUILD_LIST', 103) # Number of list items
Guido van Rossum86e58e22006-08-28 15:27:34 +0000125def_op('BUILD_SET', 104) # Number of set items
Christian Heimes99170a52007-12-19 02:07:34 +0000126def_op('BUILD_MAP', 105) # Number of dict entries (upto 255)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000127name_op('LOAD_ATTR', 106) # Index in name list
128def_op('COMPARE_OP', 107) # Comparison operator
129hascompare.append(107)
130name_op('IMPORT_NAME', 108) # Index in name list
131name_op('IMPORT_FROM', 109) # Index in name list
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000132
133jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000134jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
135jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
136jabs_op('JUMP_ABSOLUTE', 113) # ""
137jabs_op('POP_JUMP_IF_FALSE', 114) # ""
138jabs_op('POP_JUMP_IF_TRUE', 115) # ""
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000139
140name_op('LOAD_GLOBAL', 116) # Index in name list
141
142jabs_op('CONTINUE_LOOP', 119) # Target address
143jrel_op('SETUP_LOOP', 120) # Distance to target address
144jrel_op('SETUP_EXCEPT', 121) # ""
145jrel_op('SETUP_FINALLY', 122) # ""
146
147def_op('LOAD_FAST', 124) # Local variable number
148haslocal.append(124)
149def_op('STORE_FAST', 125) # Local variable number
150haslocal.append(125)
151def_op('DELETE_FAST', 126) # Local variable number
152haslocal.append(126)
153
154def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
155def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400156hasnargs.append(131)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000157def_op('MAKE_FUNCTION', 132) # Number of args with default values
158def_op('BUILD_SLICE', 133) # Number of items
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000159def_op('MAKE_CLOSURE', 134)
160def_op('LOAD_CLOSURE', 135)
161hasfree.append(135)
162def_op('LOAD_DEREF', 136)
163hasfree.append(136)
164def_op('STORE_DEREF', 137)
165hasfree.append(137)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000166def_op('DELETE_DEREF', 138)
167hasfree.append(138)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000168
169def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400170hasnargs.append(140)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000171def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400172hasnargs.append(141)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000173def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400174hasnargs.append(142)
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000175
176jrel_op('SETUP_WITH', 143)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000177
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000178def_op('LIST_APPEND', 145)
179def_op('SET_ADD', 146)
180def_op('MAP_ADD', 147)
181
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000182def_op('EXTENDED_ARG', 144)
183EXTENDED_ARG = 144
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000184
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000185del def_op, name_op, jrel_op, jabs_op