PageRenderTime 528ms CodeModel.GetById 228ms app.highlight 78ms RepoModel.GetById 126ms app.codeStats 0ms

/Lib/opcode.py

http://unladen-swallow.googlecode.com/
Python | 195 lines | 186 code | 5 blank | 4 comment | 0 complexity | 0b280fef8564336c2857621f42f15d7d MD5 | raw file
  1
  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
 24for op in range(256): opname[op] = '<%r>' % (op,)
 25del 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# Blank lines correspond to available opcodes
 45
 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
 53def_op('NOP', 9)
 54def_op('UNARY_POSITIVE', 10)
 55def_op('UNARY_NEGATIVE', 11)
 56def_op('UNARY_NOT', 12)
 57def_op('UNARY_CONVERT', 13)
 58
 59def_op('UNARY_INVERT', 15)
 60def_op('DUP_TOP_TWO', 16)
 61def_op('DUP_TOP_THREE', 17)
 62def_op('LIST_APPEND', 18)
 63def_op('BINARY_POWER', 19)
 64def_op('BINARY_MULTIPLY', 20)
 65def_op('BINARY_DIVIDE', 21)
 66def_op('BINARY_MODULO', 22)
 67def_op('BINARY_ADD', 23)
 68def_op('BINARY_SUBTRACT', 24)
 69def_op('BINARY_SUBSCR', 25)
 70def_op('BINARY_FLOOR_DIVIDE', 26)
 71def_op('BINARY_TRUE_DIVIDE', 27)
 72def_op('INPLACE_FLOOR_DIVIDE', 28)
 73def_op('INPLACE_TRUE_DIVIDE', 29)
 74def_op('SLICE_NONE', 30)
 75def_op('SLICE_LEFT', 31)
 76def_op('SLICE_RIGHT', 32)
 77def_op('SLICE_BOTH', 33)
 78def_op('RAISE_VARARGS_ZERO', 34)
 79def_op('RAISE_VARARGS_ONE', 35)
 80def_op('RAISE_VARARGS_TWO', 36)
 81def_op('RAISE_VARARGS_THREE', 37)
 82def_op('BUILD_SLICE_TWO', 38)
 83def_op('BUILD_SLICE_THREE', 39)
 84def_op('STORE_SLICE_NONE', 40)
 85def_op('STORE_SLICE_LEFT', 41)
 86def_op('STORE_SLICE_RIGHT', 42)
 87def_op('STORE_SLICE_BOTH', 43)
 88
 89def_op('DELETE_SLICE_NONE', 50)
 90def_op('DELETE_SLICE_LEFT', 51)
 91def_op('DELETE_SLICE_RIGHT', 52)
 92def_op('DELETE_SLICE_BOTH', 53)
 93
 94def_op('STORE_MAP', 54)
 95def_op('INPLACE_ADD', 55)
 96def_op('INPLACE_SUBTRACT', 56)
 97def_op('INPLACE_MULTIPLY', 57)
 98def_op('INPLACE_DIVIDE', 58)
 99def_op('INPLACE_MODULO', 59)
100def_op('STORE_SUBSCR', 60)
101def_op('DELETE_SUBSCR', 61)
102def_op('BINARY_LSHIFT', 62)
103def_op('BINARY_RSHIFT', 63)
104def_op('BINARY_AND', 64)
105def_op('BINARY_XOR', 65)
106def_op('BINARY_OR', 66)
107def_op('INPLACE_POWER', 67)
108def_op('GET_ITER', 68)
109
110# def_op('PRINT_EXPR', 70)  Replaced with the #@displayhook function.
111# def_op('PRINT_ITEM', 71)  Other PRINT_* opcodes replaced by #@print_stmt().
112# def_op('PRINT_NEWLINE', 72)
113# def_op('PRINT_ITEM_TO', 73)
114# def_op('PRINT_NEWLINE_TO', 74)
115def_op('INPLACE_LSHIFT', 75)
116def_op('INPLACE_RSHIFT', 76)
117def_op('INPLACE_AND', 77)
118def_op('INPLACE_XOR', 78)
119def_op('INPLACE_OR', 79)
120def_op('BREAK_LOOP', 80)
121def_op('WITH_CLEANUP', 81)
122# def_op('LOAD_LOCALS', 82)  Replaced with a function call to #@locals.
123def_op('RETURN_VALUE', 83)
124# def_op('IMPORT_STAR', 84)  Replaced with a function call to #@import_star.
125# def_op('EXEC_STMT', 85)  Replaced with a function call to #@exec.
126def_op('YIELD_VALUE', 86)
127def_op('POP_BLOCK', 87)
128def_op('END_FINALLY', 88)
129# def_op('BUILD_CLASS', xxx)  Replaced with a function call to #@buildclass.
130def_op('IMPORT_NAME', 89)
131
132HAVE_ARGUMENT = 90              # Opcodes from here have an argument:
133
134name_op('STORE_NAME', 90)       # Index in name list
135name_op('DELETE_NAME', 91)      # ""
136def_op('UNPACK_SEQUENCE', 92)   # Number of tuple items
137jrel_op('FOR_ITER', 93)
138
139name_op('STORE_ATTR', 95)       # Index in name list
140name_op('DELETE_ATTR', 96)      # ""
141name_op('STORE_GLOBAL', 97)     # ""
142name_op('DELETE_GLOBAL', 98)    # ""
143
144def_op('LOAD_CONST', 100)       # Index in const list
145hasconst.append(100)
146name_op('LOAD_NAME', 101)       # Index in name list
147def_op('BUILD_TUPLE', 102)      # Number of tuple items
148def_op('BUILD_LIST', 103)       # Number of list items
149def_op('BUILD_MAP', 104)        # Number of dict entries (upto 255)
150name_op('LOAD_ATTR', 105)       # Index in name list
151def_op('COMPARE_OP', 106)       # Comparison operator
152hascompare.append(106)
153
154# name_op('IMPORT_FROM', 108)   # Replaced by #@import_from.
155name_op('LOAD_METHOD', 109)     # Index in name list
156jrel_op('JUMP_FORWARD', 110)    # Number of bytes to skip
157jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
158jabs_op('JUMP_IF_TRUE_OR_POP', 112)  # ""
159jabs_op('JUMP_ABSOLUTE', 113)        # ""
160jabs_op('POP_JUMP_IF_FALSE', 114)    # ""
161jabs_op('POP_JUMP_IF_TRUE', 115)     # ""
162
163name_op('LOAD_GLOBAL', 116)     # Index in name list
164
165jabs_op('CONTINUE_LOOP', 119)   # Target address
166jrel_op('SETUP_LOOP', 120)      # Distance to target address
167jrel_op('SETUP_EXCEPT', 121)    # ""
168jrel_op('SETUP_FINALLY', 122)   # ""
169
170def_op('LOAD_FAST', 124)        # Local variable number
171haslocal.append(124)
172def_op('STORE_FAST', 125)       # Local variable number
173haslocal.append(125)
174def_op('DELETE_FAST', 126)      # Local variable number
175haslocal.append(126)
176
177def_op('CALL_FUNCTION', 131)    # #args + (#kwargs << 8)
178# def_op('MAKE_FUNCTION', 132)  Replaced by #@make_function calls.
179def_op('CALL_METHOD', 133)      # #args + (#kwargs << 8)
180
181def_op('MAKE_CLOSURE', 134)
182def_op('LOAD_CLOSURE', 135)
183hasfree.append(135)
184def_op('LOAD_DEREF', 136)
185hasfree.append(136)
186def_op('STORE_DEREF', 137)
187hasfree.append(137)
188
189def_op('CALL_FUNCTION_VAR', 140)     # #args + (#kwargs << 8)
190def_op('CALL_FUNCTION_KW', 141)      # #args + (#kwargs << 8)
191def_op('CALL_FUNCTION_VAR_KW', 142)  # #args + (#kwargs << 8)
192def_op('EXTENDED_ARG', 143)
193EXTENDED_ARG = 143
194
195del def_op, name_op, jrel_op, jabs_op