blob: eac0b63162b5523598ece0ec8aa406e027868db8 [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
Guido van Rossumc2e20742006-02-27 22:32:47 +000044# Blank lines correspond to available opcodes
Skip Montanaro3249ccd2003-02-27 21:27:52 +000045
46def_op('STOP_CODE', 0)
47def_op('POP_TOP', 1)
48def_op('ROT_TWO', 2)
49def_op('ROT_THREE', 3)
50def_op('DUP_TOP', 4)
51def_op('ROT_FOUR', 5)
52
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
Nick Coghlan650f0d02007-04-15 12:05:43 +000060def_op('SET_ADD', 17)
Raymond Hettingerdd80f762004-03-07 07:31:06 +000061def_op('LIST_APPEND', 18)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000062def_op('BINARY_POWER', 19)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000063def_op('BINARY_MULTIPLY', 20)
Neal Norwitzc6d210c2006-03-16 06:02:10 +000064
Skip Montanaro3249ccd2003-02-27 21:27:52 +000065def_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)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000073
Christian Heimes99170a52007-12-19 02:07:34 +000074def_op('STORE_MAP', 54)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000075def_op('INPLACE_ADD', 55)
76def_op('INPLACE_SUBTRACT', 56)
77def_op('INPLACE_MULTIPLY', 57)
Neal Norwitze7086d42006-03-17 08:59:09 +000078
Skip Montanaro3249ccd2003-02-27 21:27:52 +000079def_op('INPLACE_MODULO', 59)
80def_op('STORE_SUBSCR', 60)
81def_op('DELETE_SUBSCR', 61)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000082def_op('BINARY_LSHIFT', 62)
83def_op('BINARY_RSHIFT', 63)
84def_op('BINARY_AND', 64)
85def_op('BINARY_XOR', 65)
86def_op('BINARY_OR', 66)
87def_op('INPLACE_POWER', 67)
88def_op('GET_ITER', 68)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000089def_op('STORE_LOCALS', 69)
Skip Montanaro3249ccd2003-02-27 21:27:52 +000090
91def_op('PRINT_EXPR', 70)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000092def_op('LOAD_BUILD_CLASS', 71)
Georg Brandl88fc6642007-02-09 21:28:07 +000093
Skip Montanaro3249ccd2003-02-27 21:27:52 +000094def_op('INPLACE_LSHIFT', 75)
95def_op('INPLACE_RSHIFT', 76)
96def_op('INPLACE_AND', 77)
97def_op('INPLACE_XOR', 78)
98def_op('INPLACE_OR', 79)
99def_op('BREAK_LOOP', 80)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000100def_op('WITH_CLEANUP', 81)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000101
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000102def_op('RETURN_VALUE', 83)
103def_op('IMPORT_STAR', 84)
Thomas Wouters00e41de2007-02-23 19:56:57 +0000104def_op('MAKE_BYTES', 85)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000105def_op('YIELD_VALUE', 86)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000106def_op('POP_BLOCK', 87)
107def_op('END_FINALLY', 88)
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) # ""
120def_op('DUP_TOPX', 99) # number of items to duplicate
121def_op('LOAD_CONST', 100) # Index in const list
122hasconst.append(100)
123name_op('LOAD_NAME', 101) # Index in name list
124def_op('BUILD_TUPLE', 102) # Number of tuple items
125def_op('BUILD_LIST', 103) # Number of list items
Guido van Rossum86e58e22006-08-28 15:27:34 +0000126def_op('BUILD_SET', 104) # Number of set items
Christian Heimes99170a52007-12-19 02:07:34 +0000127def_op('BUILD_MAP', 105) # Number of dict entries (upto 255)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000128name_op('LOAD_ATTR', 106) # Index in name list
129def_op('COMPARE_OP', 107) # Comparison operator
130hascompare.append(107)
131name_op('IMPORT_NAME', 108) # Index in name list
132name_op('IMPORT_FROM', 109) # Index in name list
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000133
134jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
135jrel_op('JUMP_IF_FALSE', 111) # ""
136jrel_op('JUMP_IF_TRUE', 112) # ""
137jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
138
139name_op('LOAD_GLOBAL', 116) # Index in name list
140
141jabs_op('CONTINUE_LOOP', 119) # Target address
142jrel_op('SETUP_LOOP', 120) # Distance to target address
143jrel_op('SETUP_EXCEPT', 121) # ""
144jrel_op('SETUP_FINALLY', 122) # ""
145
146def_op('LOAD_FAST', 124) # Local variable number
147haslocal.append(124)
148def_op('STORE_FAST', 125) # Local variable number
149haslocal.append(125)
150def_op('DELETE_FAST', 126) # Local variable number
151haslocal.append(126)
152
153def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
154def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
155def_op('MAKE_FUNCTION', 132) # Number of args with default values
156def_op('BUILD_SLICE', 133) # Number of items
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000157def_op('MAKE_CLOSURE', 134)
158def_op('LOAD_CLOSURE', 135)
159hasfree.append(135)
160def_op('LOAD_DEREF', 136)
161hasfree.append(136)
162def_op('STORE_DEREF', 137)
163hasfree.append(137)
164
165def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
166def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
167def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
Skip Montanaro3249ccd2003-02-27 21:27:52 +0000168def_op('EXTENDED_ARG', 143)
169EXTENDED_ARG = 143
170
171del def_op, name_op, jrel_op, jabs_op