hotpy_2 / HotPy / trace_makeopcodetargets.py

#!./python

import opcode, sys, re

small_ops = [ 'make_frame', 'leave_frame', 'as_tuple', 'reraise',
              'stop_iteration_or_reraise',  'sub_type', 'kw_dict_merge',
              'make_generator', 'enter_frame', 'new_function', 'new_closure',
              'trace_end', 'get_global_and_builtins', 'save_exc_state', 'swap_exc_state',
              'restore_clear_exc_state', 'setup_except_handler', 'clear_frame' ]

with_arg_ops = [ 'binary_op', 'ensure_type_tos', 'ensure_type_nos', 'jump_back',
                 'fast_frame', 'set_lasti', 'trace_load_constant', 'pick',
                 'setup_for_iter', 'load_and_clear_register',
                 'call_object', 'pop_exit_if_true', 'exit', 'poly_exit',
                 'pop_exit_if_false', 'exit_if_different', 'load_register',
                 'store_register', 'call_cfunc_0', 'call_cfunc_1',
                 'call_cfunc_2', 'call_cmeth_0', 'call_cmeth_1',
                 'call_cmeth_2', 'no_keywords', 'no_args', 'one_arg',
                 'two_args', 'three_args', 'call_wrapper_unary',
                 'call_wrapper_next', 'call_wrapper_binary',
                 'call_wrapper_trinary_proc', 'exit_if_not_bool_or_int',
                 'value_from_object_dict_or_exit', 'gen_check_or_exit',
                 'exit_if_value_in_object_dict', 'load_slot', 'store_slot',
                 'load_slot_ex', 'store_member',
                 'call_wrapper_binary_null_proc',
                 'value_from_object_dict_and_jump',
                 'load_from_global_and_builtins',
                 'store_to_globals', 'one_plus_args',
                 'materialise_frame', 'set_in_object_dict_const',
                 'trace_descriptor_get', 'trace_descriptor_set' ]

register_ops = [ 'new_frame', 'push_register_to_stack', 'move',
                 'pop_stack_to_register', 'tuple_set_item', 'list_set_item',
                 'const_to_register', 'exit_if_false', 'exit_if_true',
                 'ensure_type', 'ensure_value', 'ensure_slot',
                 'value_from_object_dict_or_exit_const', 'setup_block',
                 'poly_type_exit', 'two_move', 'copy_move', 'fast_load_global',
                 'binary_op_kv', 'binary_op_vk', 'load_member' ]

ops = [ '' ] * 256
op_used = [ False ] * 256
last_used_op = 0 # Don't use 0
for op, code in opcode.opmap.items():
    if op.startswith("BINARY_") or op.startswith("INPLACE_"):
        pass
    else:
        op_used[code] = True
        ops[code] =  op

def add_op(name, op):
    op_used[op] = True
    ops[op] = name.upper()

def add_extra_op(name):
    global last_used_op
    last_used_op += 1
    while op_used[last_used_op]:
        last_used_op += 1
    add_op(name, last_used_op)

for op in small_ops:
    add_extra_op(op)

assert last_used_op < opcode.HAVE_ARGUMENT
last_used_op = opcode.HAVE_ARGUMENT

for op in with_arg_ops:
    add_extra_op(op)

for op in register_ops:
    add_extra_op(op)


TARGET_RE = re.compile(r' *TARGET(_WITH_ARG)?\((\w+)\)\n')

def opcode_targets(targets, out):
    def p(x):
        out.write(x)
    name_set = set()
    for line in targets.readlines():
        m = TARGET_RE.match(line)
        if m:
            name = m.group(2)
            name_set.add(name.upper())
    p('static void *opcode_targets[256] = {\n')
    assert len(ops) == 256
    for name in ops:
        if name in name_set:
            p('&&TARGET_%s,\n' % name)
        elif name:
            p('&&_unknown_opcode, /* %s */\n' % name)
        else:
            p('&&_unknown_opcode,\n')
    p('};\n')

def trace_targets():
    with open('Python/trace_opcode_targets.h', 'w') as out:
        with open('Python/trace_interpreter.c', 'r') as targets:
            opcode_targets(targets, out)

def reg_targets():
    with open('Python/register_opcode_targets.h', 'w') as out:
        with open('Python/register_interpreter.c', 'r') as targets:
            opcode_targets(targets, out)

if __name__ == '__main__':
    import sys
    if sys.argv[1] == '-r':
        reg_targets()
    elif sys.argv[1] == '-t':
        trace_targets()
    else:
        raise Exception("Expecting one of -r -t")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.