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) 70def_op('BINARY_MATRIX_MULTIPLY', 16) 71def_op('INPLACE_MATRIX_MULTIPLY', 17) 72 73def_op('BINARY_POWER', 19) 74def_op('BINARY_MULTIPLY', 20) 75 76def_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) 84def_op('GET_LEN', 30) 85def_op('MATCH_MAPPING', 31) 86def_op('MATCH_SEQUENCE', 32) 87def_op('MATCH_KEYS', 33) 88def_op('COPY_DICT_WITHOUT_KEYS', 34) 89 90def_op('WITH_EXCEPT_START', 49) 91def_op('GET_AITER', 50) 92def_op('GET_ANEXT', 51) 93def_op('BEFORE_ASYNC_WITH', 52) 94 95def_op('END_ASYNC_FOR', 54) 96def_op('INPLACE_ADD', 55) 97def_op('INPLACE_SUBTRACT', 56) 98def_op('INPLACE_MULTIPLY', 57) 99 100def_op('INPLACE_MODULO', 59) 101def_op('STORE_SUBSCR', 60) 102def_op('DELETE_SUBSCR', 61) 103def_op('BINARY_LSHIFT', 62) 104def_op('BINARY_RSHIFT', 63) 105def_op('BINARY_AND', 64) 106def_op('BINARY_XOR', 65) 107def_op('BINARY_OR', 66) 108def_op('INPLACE_POWER', 67) 109def_op('GET_ITER', 68) 110def_op('GET_YIELD_FROM_ITER', 69) 111def_op('PRINT_EXPR', 70) 112def_op('LOAD_BUILD_CLASS', 71) 113def_op('YIELD_FROM', 72) 114def_op('GET_AWAITABLE', 73) 115def_op('LOAD_ASSERTION_ERROR', 74) 116def_op('INPLACE_LSHIFT', 75) 117def_op('INPLACE_RSHIFT', 76) 118def_op('INPLACE_AND', 77) 119def_op('INPLACE_XOR', 78) 120def_op('INPLACE_OR', 79) 121 122def_op('LIST_TO_TUPLE', 82) 123def_op('RETURN_VALUE', 83) 124def_op('IMPORT_STAR', 84) 125def_op('SETUP_ANNOTATIONS', 85) 126def_op('YIELD_VALUE', 86) 127def_op('POP_BLOCK', 87) 128 129def_op('POP_EXCEPT', 89) 130 131HAVE_ARGUMENT = 90 # Opcodes from here have an argument: 132 133name_op('STORE_NAME', 90) # Index in name list 134name_op('DELETE_NAME', 91) # "" 135def_op('UNPACK_SEQUENCE', 92) # Number of tuple items 136jrel_op('FOR_ITER', 93) 137def_op('UNPACK_EX', 94) 138name_op('STORE_ATTR', 95) # Index in name list 139name_op('DELETE_ATTR', 96) # "" 140name_op('STORE_GLOBAL', 97) # "" 141name_op('DELETE_GLOBAL', 98) # "" 142def_op('ROT_N', 99) 143def_op('LOAD_CONST', 100) # Index in const list 144hasconst.append(100) 145name_op('LOAD_NAME', 101) # Index in name list 146def_op('BUILD_TUPLE', 102) # Number of tuple items 147def_op('BUILD_LIST', 103) # Number of list items 148def_op('BUILD_SET', 104) # Number of set items 149def_op('BUILD_MAP', 105) # Number of dict entries 150name_op('LOAD_ATTR', 106) # Index in name list 151def_op('COMPARE_OP', 107) # Comparison operator 152hascompare.append(107) 153name_op('IMPORT_NAME', 108) # Index in name list 154name_op('IMPORT_FROM', 109) # Index in name list 155jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip 156jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code 157jabs_op('JUMP_IF_TRUE_OR_POP', 112) # "" 158jabs_op('JUMP_ABSOLUTE', 113) # "" 159jabs_op('POP_JUMP_IF_FALSE', 114) # "" 160jabs_op('POP_JUMP_IF_TRUE', 115) # "" 161name_op('LOAD_GLOBAL', 116) # Index in name list 162def_op('IS_OP', 117) 163def_op('CONTAINS_OP', 118) 164def_op('RERAISE', 119) 165 166jabs_op('JUMP_IF_NOT_EXC_MATCH', 121) 167jrel_op('SETUP_FINALLY', 122) # Distance to target address 168 169def_op('LOAD_FAST', 124) # Local variable number 170haslocal.append(124) 171def_op('STORE_FAST', 125) # Local variable number 172haslocal.append(125) 173def_op('DELETE_FAST', 126) # Local variable number 174haslocal.append(126) 175 176def_op('GEN_START', 129) # Kind of generator/coroutine 177def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3) 178def_op('CALL_FUNCTION', 131) # #args 179def_op('MAKE_FUNCTION', 132) # Flags 180def_op('BUILD_SLICE', 133) # Number of items 181 182def_op('LOAD_CLOSURE', 135) 183hasfree.append(135) 184def_op('LOAD_DEREF', 136) 185hasfree.append(136) 186def_op('STORE_DEREF', 137) 187hasfree.append(137) 188def_op('DELETE_DEREF', 138) 189hasfree.append(138) 190 191def_op('CALL_FUNCTION_KW', 141) # #args + #kwargs 192def_op('CALL_FUNCTION_EX', 142) # Flags 193jrel_op('SETUP_WITH', 143) 194def_op('EXTENDED_ARG', 144) 195EXTENDED_ARG = 144 196def_op('LIST_APPEND', 145) 197def_op('SET_ADD', 146) 198def_op('MAP_ADD', 147) 199def_op('LOAD_CLASSDEREF', 148) 200hasfree.append(148) 201 202def_op('MATCH_CLASS', 152) 203 204jrel_op('SETUP_ASYNC_WITH', 154) 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) 211def_op('LIST_EXTEND', 162) 212def_op('SET_UPDATE', 163) 213def_op('DICT_MERGE', 164) 214def_op('DICT_UPDATE', 165) 215 216del def_op, name_op, jrel_op, jabs_op 217