10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoopcode module - potentially shared between dis and other modules which
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaooperate on bytecodes (e.g. peephole optimizers).
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "haslocal", "hascompare", "hasfree", "opname", "opmap",
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "HAVE_ARGUMENT", "EXTENDED_ARG"]
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'is not', 'exception match', 'BAD')
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasconst = []
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasname = []
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasjrel = []
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasjabs = []
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohaslocal = []
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohascompare = []
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasfree = []
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoopmap = {}
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoopname = [''] * 256
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor op in range(256): opname[op] = '<%r>' % (op,)
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel op
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef def_op(name, op):
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    opname[op] = name
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    opmap[name] = op
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef name_op(name, op):
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def_op(name, op)
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    hasname.append(op)
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef jrel_op(name, op):
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def_op(name, op)
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    hasjrel.append(op)
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef jabs_op(name, op):
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def_op(name, op)
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    hasjabs.append(op)
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Instruction opcodes for compiled code
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Blank lines correspond to available opcodes
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STOP_CODE', 0)
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('POP_TOP', 1)
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('ROT_TWO', 2)
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('ROT_THREE', 3)
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DUP_TOP', 4)
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('ROT_FOUR', 5)
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('NOP', 9)
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('UNARY_POSITIVE', 10)
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('UNARY_NEGATIVE', 11)
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('UNARY_NOT', 12)
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('UNARY_CONVERT', 13)
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('UNARY_INVERT', 15)
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_POWER', 19)
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_MULTIPLY', 20)
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_DIVIDE', 21)
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_MODULO', 22)
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_ADD', 23)
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_SUBTRACT', 24)
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_SUBSCR', 25)
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_FLOOR_DIVIDE', 26)
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_TRUE_DIVIDE', 27)
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_FLOOR_DIVIDE', 28)
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_TRUE_DIVIDE', 29)
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('SLICE+0', 30)
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('SLICE+1', 31)
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('SLICE+2', 32)
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('SLICE+3', 33)
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_SLICE+0', 40)
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_SLICE+1', 41)
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_SLICE+2', 42)
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_SLICE+3', 43)
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DELETE_SLICE+0', 50)
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DELETE_SLICE+1', 51)
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DELETE_SLICE+2', 52)
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DELETE_SLICE+3', 53)
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_MAP', 54)
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_ADD', 55)
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_SUBTRACT', 56)
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_MULTIPLY', 57)
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_DIVIDE', 58)
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_MODULO', 59)
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_SUBSCR', 60)
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DELETE_SUBSCR', 61)
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_LSHIFT', 62)
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_RSHIFT', 63)
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_AND', 64)
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_XOR', 65)
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BINARY_OR', 66)
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_POWER', 67)
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('GET_ITER', 68)
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('PRINT_EXPR', 70)
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('PRINT_ITEM', 71)
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('PRINT_NEWLINE', 72)
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('PRINT_ITEM_TO', 73)
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('PRINT_NEWLINE_TO', 74)
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_LSHIFT', 75)
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_RSHIFT', 76)
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_AND', 77)
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_XOR', 78)
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('INPLACE_OR', 79)
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BREAK_LOOP', 80)
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('WITH_CLEANUP', 81)
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('LOAD_LOCALS', 82)
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('RETURN_VALUE', 83)
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('IMPORT_STAR', 84)
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('EXEC_STMT', 85)
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('YIELD_VALUE', 86)
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('POP_BLOCK', 87)
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('END_FINALLY', 88)
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BUILD_CLASS', 89)
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1240a8c90248264a8b26970b4473770bcc3df8515fJosh GaoHAVE_ARGUMENT = 90              # Opcodes from here have an argument:
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('STORE_NAME', 90)       # Index in name list
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('DELETE_NAME', 91)      # ""
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('UNPACK_SEQUENCE', 92)   # Number of tuple items
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojrel_op('FOR_ITER', 93)
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('LIST_APPEND', 94)
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('STORE_ATTR', 95)       # Index in name list
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('DELETE_ATTR', 96)      # ""
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('STORE_GLOBAL', 97)     # ""
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('DELETE_GLOBAL', 98)    # ""
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DUP_TOPX', 99)          # number of items to duplicate
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('LOAD_CONST', 100)       # Index in const list
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasconst.append(100)
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('LOAD_NAME', 101)       # Index in name list
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BUILD_TUPLE', 102)      # Number of tuple items
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BUILD_LIST', 103)       # Number of list items
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BUILD_SET', 104)        # Number of set items
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BUILD_MAP', 105)        # Number of dict entries (upto 255)
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('LOAD_ATTR', 106)       # Index in name list
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('COMPARE_OP', 107)       # Comparison operator
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohascompare.append(107)
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('IMPORT_NAME', 108)     # Index in name list
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('IMPORT_FROM', 109)     # Index in name list
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojrel_op('JUMP_FORWARD', 110)    # Number of bytes to skip
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojabs_op('JUMP_IF_TRUE_OR_POP', 112)  # ""
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojabs_op('JUMP_ABSOLUTE', 113)        # ""
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojabs_op('POP_JUMP_IF_FALSE', 114)    # ""
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojabs_op('POP_JUMP_IF_TRUE', 115)     # ""
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoname_op('LOAD_GLOBAL', 116)     # Index in name list
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojabs_op('CONTINUE_LOOP', 119)   # Target address
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojrel_op('SETUP_LOOP', 120)      # Distance to target address
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojrel_op('SETUP_EXCEPT', 121)    # ""
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojrel_op('SETUP_FINALLY', 122)   # ""
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('LOAD_FAST', 124)        # Local variable number
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohaslocal.append(124)
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_FAST', 125)       # Local variable number
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohaslocal.append(125)
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('DELETE_FAST', 126)      # Local variable number
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohaslocal.append(126)
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('RAISE_VARARGS', 130)    # Number of raise arguments (1, 2, or 3)
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('CALL_FUNCTION', 131)    # #args + (#kwargs << 8)
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('MAKE_FUNCTION', 132)    # Number of args with default values
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('BUILD_SLICE', 133)      # Number of items
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('MAKE_CLOSURE', 134)
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('LOAD_CLOSURE', 135)
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasfree.append(135)
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('LOAD_DEREF', 136)
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasfree.append(136)
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('STORE_DEREF', 137)
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohasfree.append(137)
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('CALL_FUNCTION_VAR', 140)     # #args + (#kwargs << 8)
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('CALL_FUNCTION_KW', 141)      # #args + (#kwargs << 8)
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('CALL_FUNCTION_VAR_KW', 142)  # #args + (#kwargs << 8)
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojrel_op('SETUP_WITH', 143)
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('EXTENDED_ARG', 145)
1880a8c90248264a8b26970b4473770bcc3df8515fJosh GaoEXTENDED_ARG = 145
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('SET_ADD', 146)
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef_op('MAP_ADD', 147)
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel def_op, name_op, jrel_op, jabs_op
193