
"""
opcode module - potentially shared between dis and other modules which
operate on bytecodes (e.g. peephole optimizers).
"""

__all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
           "haslocal", "hascompare", "hasfree", "opname", "opmap",
           "HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"]

# It's a chicken-and-egg I'm afraid:
# We're imported before _opcode's made.
# With exception unheeded
# (stack_effect is not needed)
# Both our chickens and eggs are allayed.
#     --Larry Hastings, 2013/11/23

try:
    from _opcode import stack_effect
    __all__.append('stack_effect')
except ImportError:
    pass

cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
        'is not', 'exception match', 'BAD')

hasconst = []
hasname = []
hasjrel = []
hasjabs = []
haslocal = []
hascompare = []
hasfree = []
hasnargs = [] # unused

opmap = {}
opname = ['<%r>' % (op,) for op in range(256)]

def def_op(name, op):
    opname[op] = name
    opmap[name] = op

def name_op(name, op):
    def_op(name, op)
    hasname.append(op)

def jrel_op(name, op):
    def_op(name, op)
    hasjrel.append(op)

def jabs_op(name, op):
    def_op(name, op)
    hasjabs.append(op)

# Instruction opcodes for compiled code
# Blank lines correspond to available opcodes

def_op('POP_BLOCK', 1)
def_op('UNARY_INVERT', 2)
def_op('INPLACE_FLOOR_DIVIDE', 3)
def_op('POP_TOP', 4)
def_op('RETURN_VALUE', 5)

def_op('BINARY_SUBTRACT', 9)
def_op('POP_EXCEPT', 10)
def_op('BINARY_ADD', 11)
def_op('BINARY_XOR', 12)

def_op('UNARY_POSITIVE', 15)

def_op('ROT_TWO', 16)
def_op('INPLACE_ADD', 17)

def_op('YIELD_FROM', 19)
def_op('GET_YIELD_FROM_ITER', 20)

def_op('GET_AWAITABLE', 22)
def_op('GET_ITER', 23)
def_op('IMPORT_STAR', 24)
def_op('UNARY_NOT', 25)
def_op('SETUP_ANNOTATIONS', 26)
def_op('INPLACE_MODULO', 27)
def_op('GET_AITER', 28)
def_op('GET_ANEXT', 29)

def_op('UNARY_NEGATIVE', 50)
def_op('BINARY_RSHIFT', 51)
def_op('INPLACE_XOR', 52)

def_op('WITH_CLEANUP_FINISH', 55)
def_op('INPLACE_TRUE_DIVIDE', 56)
def_op('INPLACE_POWER', 57)

def_op('INPLACE_SUBTRACT', 59)
def_op('INPLACE_AND', 60)
def_op('END_FINALLY', 61)
def_op('YIELD_VALUE', 62)
def_op('BREAK_LOOP', 63)
def_op('BINARY_SUBSCR', 64)
def_op('LOAD_BUILD_CLASS', 65)
def_op('BEFORE_ASYNC_WITH', 66)
def_op('BINARY_AND', 67)
def_op('INPLACE_OR', 68)
def_op('BINARY_POWER', 69)

def_op('BINARY_MULTIPLY', 70)
def_op('ROT_THREE', 71)
def_op('BINARY_LSHIFT', 72)
def_op('INPLACE_LSHIFT', 73)

def_op('STORE_SUBSCR', 75)
def_op('BINARY_OR', 76)
def_op('BINARY_MATRIX_MULTIPLY', 77)
def_op('BINARY_TRUE_DIVIDE', 78)
def_op('WITH_CLEANUP_START', 79)
def_op('NOP', 80)
def_op('PRINT_EXPR', 81)
def_op('DUP_TOP', 82)

def_op('INPLACE_RSHIFT', 83)
def_op('DELETE_SUBSCR', 84)
def_op('BINARY_FLOOR_DIVIDE', 85)
def_op('BINARY_MODULO', 86)
def_op('INPLACE_MULTIPLY', 87)
def_op('INPLACE_MATRIX_MULTIPLY', 88)
def_op('DUP_TOP_TWO', 89)

HAVE_ARGUMENT = 90              # Opcodes from here have an argument:

def_op('CALL_FUNCTION', 90)    # #args
def_op('BUILD_MAP_UNPACK', 91)
def_op('LIST_APPEND', 92)
jabs_op('JUMP_ABSOLUTE', 93)        # ""
jabs_op('JUMP_IF_TRUE_OR_POP', 94)  # ""
def_op('LOAD_CLASSDEREF', 95)
hasfree.append(95)
def_op('MAP_ADD', 96)
def_op('CALL_FUNCTION_EX', 97)  # Flags
jrel_op('JUMP_FORWARD', 98)    # Number of bytes to skip
def_op('EXTENDED_ARG', 100)
EXTENDED_ARG = 100
def_op('STORE_FAST', 101)       # Local variable number
haslocal.append(101)
def_op('LOAD_CONST', 102)       # Index in const list
hasconst.append(102)
def_op('BUILD_MAP_UNPACK_WITH_CALL', 103)
name_op('STORE_ANNOTATION', 104) # Index in name list
def_op('MAKE_FUNCTION', 105)    # Flags
def_op('UNPACK_SEQUENCE', 106)   # Number of tuple items
def_op('RAISE_VARARGS', 107)    # Number of raise arguments (1, 2, or 3)

def_op('BUILD_CONST_KEY_MAP', 108)
jrel_op('SETUP_EXCEPT', 109)    # ""
jrel_op('SETUP_ASYNC_WITH', 110)
name_op('LOAD_NAME', 111)       # Index in name list
def_op('FORMAT_VALUE', 112)
name_op('DELETE_NAME', 113)      # ""

name_op('DELETE_GLOBAL', 114)    # ""

def_op('BUILD_LIST_UNPACK', 115)
jabs_op('POP_JUMP_IF_FALSE', 116)    # ""
jrel_op('SETUP_LOOP', 119)      # Distance to target address
jabs_op('POP_JUMP_IF_TRUE', 120)     # ""

name_op('IMPORT_FROM', 121)     # Index in name list
name_op('IMPORT_NAME', 122)     # Index in name list
def_op('BUILD_TUPLE_UNPACK', 124)
def_op('BUILD_TUPLE_UNPACK_WITH_CALL', 125)
def_op('BUILD_SET_UNPACK', 126)
jrel_op('FOR_ITER', 127)
def_op('SET_ADD', 130)

jabs_op('CONTINUE_LOOP', 131)   # Target address
def_op('DELETE_DEREF', 132)
hasfree.append(132)
def_op('COMPARE_OP', 133)       # Comparison operator
hascompare.append(133)
def_op('LOAD_FAST', 135)        # Local variable number
haslocal.append(135)
def_op('BUILD_MAP', 136)        # Number of dict entries
def_op('STORE_DEREF', 137)
hasfree.append(137)
def_op('LOAD_CLOSURE', 138)
hasfree.append(138)

name_op('STORE_ATTR', 141)       # Index in name list
name_op('STORE_GLOBAL', 142)     # ""

jabs_op('JUMP_IF_FALSE_OR_POP', 143) # Target byte offset from beginning of code

def_op('BUILD_TUPLE', 144)      # Number of tuple items
def_op('DELETE_FAST', 145)      # Local variable number
haslocal.append(145)

def_op('LOAD_DEREF', 146)
hasfree.append(146)

jrel_op('SETUP_WITH', 147)
def_op('BUILD_SET', 148)        # Number of set items

jrel_op('SETUP_FINALLY', 149)   # ""
name_op('STORE_NAME', 150)       # Index in name list
name_op('DELETE_ATTR', 151)      # ""
def_op('BUILD_STRING', 152)
def_op('CALL_FUNCTION_KW', 153)  # #args + #kwargs

def_op('BUILD_SLICE', 154)      # Number of items

name_op('LOAD_ATTR', 155)       # Index in name list
def_op('BUILD_LIST', 156)       # Number of list items
def_op('UNPACK_EX', 157)
name_op('LOAD_GLOBAL', 158)     # Index in name list

del def_op, name_op, jrel_op, jabs_op
