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