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", "hasnargs"] 10 11# 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 24cmp_op = ('<', '<=', '==', '!=', '>', '>=') 25 26hasconst = [] 27hasname = [] 28hasjrel = [] 29hasjabs = [] 30haslocal = [] 31hascompare = [] 32hasfree = [] 33hasnargs = [] # unused 34 35opmap = {} 36opname = ['<%r>' % (op,) for op in range(256)] 37 38def def_op(name, op): 39 opname[op] = name 40 opmap[name] = op 41 42def name_op(name, op): 43 def_op(name, op) 44 hasname.append(op) 45 46def jrel_op(name, op): 47 def_op(name, op) 48 hasjrel.append(op) 49 50def jabs_op(name, op): 51 def_op(name, op) 52 hasjabs.append(op) 53 54# Instruction opcodes for compiled code 55# Blank lines correspond to available opcodes 56 57def_op('POP_TOP', 1) 58def_op('ROT_TWO', 2) 59def_op('ROT_THREE', 3) 60def_op('DUP_TOP', 4) 61def_op('DUP_TOP_TWO', 5) 62def_op('ROT_FOUR', 6) 63 64def_op('NOP', 9) 65def_op('UNARY_POSITIVE', 10) 66def_op('UNARY_NEGATIVE', 11) 67def_op('UNARY_NOT', 12) 68 69def_op('UNARY_INVERT', 15) 70 71def_op('BINARY_MATRIX_MULTIPLY', 16) 72def_op('INPLACE_MATRIX_MULTIPLY', 17) 73 74def_op('BINARY_POWER', 19) 75def_op('BINARY_MULTIPLY', 20) 76 77def_op('BINARY_MODULO', 22) 78def_op('BINARY_ADD', 23) 79def_op('BINARY_SUBTRACT', 24) 80def_op('BINARY_SUBSCR', 25) 81def_op('BINARY_FLOOR_DIVIDE', 26) 82def_op('BINARY_TRUE_DIVIDE', 27) 83def_op('INPLACE_FLOOR_DIVIDE', 28) 84def_op('INPLACE_TRUE_DIVIDE', 29) 85 86def_op('RERAISE', 48) 87def_op('WITH_EXCEPT_START', 49) 88def_op('GET_AITER', 50) 89def_op('GET_ANEXT', 51) 90def_op('BEFORE_ASYNC_WITH', 52) 91 92def_op('END_ASYNC_FOR', 54) 93def_op('INPLACE_ADD', 55) 94def_op('INPLACE_SUBTRACT', 56) 95def_op('INPLACE_MULTIPLY', 57) 96 97def_op('INPLACE_MODULO', 59) 98def_op('STORE_SUBSCR', 60) 99def_op('DELETE_SUBSCR', 61) 100def_op('BINARY_LSHIFT', 62) 101def_op('BINARY_RSHIFT', 63) 102def_op('BINARY_AND', 64) 103def_op('BINARY_XOR', 65) 104def_op('BINARY_OR', 66) 105def_op('INPLACE_POWER', 67) 106def_op('GET_ITER', 68) 107def_op('GET_YIELD_FROM_ITER', 69) 108 109def_op('PRINT_EXPR', 70) 110def_op('LOAD_BUILD_CLASS', 71) 111def_op('YIELD_FROM', 72) 112def_op('GET_AWAITABLE', 73) 113def_op('LOAD_ASSERTION_ERROR', 74) 114def_op('INPLACE_LSHIFT', 75) 115def_op('INPLACE_RSHIFT', 76) 116def_op('INPLACE_AND', 77) 117def_op('INPLACE_XOR', 78) 118def_op('INPLACE_OR', 79) 119 120def_op('LIST_TO_TUPLE', 82) 121def_op('RETURN_VALUE', 83) 122def_op('IMPORT_STAR', 84) 123def_op('SETUP_ANNOTATIONS', 85) 124def_op('YIELD_VALUE', 86) 125def_op('POP_BLOCK', 87) 126 127def_op('POP_EXCEPT', 89) 128 129HAVE_ARGUMENT = 90 # Opcodes from here have an argument: 130 131name_op('STORE_NAME', 90) # Index in name list 132name_op('DELETE_NAME', 91) # "" 133def_op('UNPACK_SEQUENCE', 92) # Number of tuple items 134jrel_op('FOR_ITER', 93) 135def_op('UNPACK_EX', 94) 136name_op('STORE_ATTR', 95) # Index in name list 137name_op('DELETE_ATTR', 96) # "" 138name_op('STORE_GLOBAL', 97) # "" 139name_op('DELETE_GLOBAL', 98) # "" 140def_op('LOAD_CONST', 100) # Index in const list 141hasconst.append(100) 142name_op('LOAD_NAME', 101) # Index in name list 143def_op('BUILD_TUPLE', 102) # Number of tuple items 144def_op('BUILD_LIST', 103) # Number of list items 145def_op('BUILD_SET', 104) # Number of set items 146def_op('BUILD_MAP', 105) # Number of dict entries 147name_op('LOAD_ATTR', 106) # Index in name list 148def_op('COMPARE_OP', 107) # Comparison operator 149hascompare.append(107) 150name_op('IMPORT_NAME', 108) # Index in name list 151name_op('IMPORT_FROM', 109) # Index in name list 152 153jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip 154jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code 155jabs_op('JUMP_IF_TRUE_OR_POP', 112) # "" 156jabs_op('JUMP_ABSOLUTE', 113) # "" 157jabs_op('POP_JUMP_IF_FALSE', 114) # "" 158jabs_op('POP_JUMP_IF_TRUE', 115) # "" 159 160name_op('LOAD_GLOBAL', 116) # Index in name list 161 162def_op('IS_OP', 117) 163def_op('CONTAINS_OP', 118) 164 165jabs_op('JUMP_IF_NOT_EXC_MATCH', 121) 166jrel_op('SETUP_FINALLY', 122) # Distance to target address 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 177def_op('MAKE_FUNCTION', 132) # Flags 178def_op('BUILD_SLICE', 133) # Number of items 179def_op('LOAD_CLOSURE', 135) 180hasfree.append(135) 181def_op('LOAD_DEREF', 136) 182hasfree.append(136) 183def_op('STORE_DEREF', 137) 184hasfree.append(137) 185def_op('DELETE_DEREF', 138) 186hasfree.append(138) 187 188def_op('CALL_FUNCTION_KW', 141) # #args + #kwargs 189def_op('CALL_FUNCTION_EX', 142) # Flags 190 191jrel_op('SETUP_WITH', 143) 192 193def_op('LIST_APPEND', 145) 194def_op('SET_ADD', 146) 195def_op('MAP_ADD', 147) 196 197def_op('LOAD_CLASSDEREF', 148) 198hasfree.append(148) 199 200def_op('EXTENDED_ARG', 144) 201EXTENDED_ARG = 144 202 203jrel_op('SETUP_ASYNC_WITH', 154) 204 205def_op('FORMAT_VALUE', 155) 206def_op('BUILD_CONST_KEY_MAP', 156) 207def_op('BUILD_STRING', 157) 208 209name_op('LOAD_METHOD', 160) 210def_op('CALL_METHOD', 161) 211 212def_op('LIST_EXTEND', 162) 213def_op('SET_UPDATE', 163) 214def_op('DICT_MERGE', 164) 215def_op('DICT_UPDATE', 165) 216 217del def_op, name_op, jrel_op, jabs_op 218