Commits

Mark Shannon committed 22c18ce Draft

Reuse BINARY_XX and INPLACE_XXX opcodes for trace opcodes

Comments (0)

Files changed (7)

HotPy/trace_makeopcodetargets.py

 op_used = [ False ] * 256
 last_used_op = 0 # Don't use 0
 for op, code in opcode.opmap.items():
-    op_used[code] = True
-    ops[code] =  op
+    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

HotPy/trace_opcode_header_gen.py

+
+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' ]
+
+names =  [ '' ] * 256
+op_used = [ False ] * 256
+last_used_op = 0 # Don't use 0
+opcode_list = [ '/* Opcode %d unused */' % n for n in range(256)]
+for op, code in opcode.opmap.items():
+    if op.startswith("BINARY_") or op.startswith("INPLACE_"):
+        names[code] = '%s' % op
+    else:
+        opcode_list[code] = '/* Non-trace opcode %s = %d */' % (op, code)
+        op_used[code] = True
+        names[code] = '%s' % op
+
+def add_op(name, op):
+    op_used[op] = True
+    opcode_list[op] = "#define %s %d" %(name.upper(), op)
+    if names[op]:
+        names[op] += ' or ' + name.upper()
+    else:
+        names[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)
+
+for line in opcode_list:
+    print (line)
+
+print()
+print('#ifdef WITH_INSTRUCTION_NAMES')
+print('char * _HotPy_Instruction_Names[] = {')
+for op, name in enumerate(names):
+    if name:
+        print ('"%s",' % name)
+    else:
+        print ('"%s",' % op)
+print('};\n')
+print('#else')
+print('extern char *_HotPy_Instruction_Names[];')
+print('#endif\n')
+print('#define USED_OPCODES %d' % (last_used_op +1 ))
+print()

Include/trace_opcode.h

 #define STOP_ITERATION_OR_RERAISE 16
 #define SUB_TYPE 17
 #define KW_DICT_MERGE 18
-/* Non-trace opcode BINARY_POWER = 19 */
-/* Non-trace opcode BINARY_MULTIPLY = 20 */
-#define MAKE_GENERATOR 21
-/* Non-trace opcode BINARY_MODULO = 22 */
-/* Non-trace opcode BINARY_ADD = 23 */
-/* Non-trace opcode BINARY_SUBTRACT = 24 */
-/* Non-trace opcode BINARY_SUBSCR = 25 */
-/* Non-trace opcode BINARY_FLOOR_DIVIDE = 26 */
-/* Non-trace opcode BINARY_TRUE_DIVIDE = 27 */
-/* Non-trace opcode INPLACE_FLOOR_DIVIDE = 28 */
-/* Non-trace opcode INPLACE_TRUE_DIVIDE = 29 */
-#define ENTER_FRAME 30
-#define NEW_FUNCTION 31
-#define NEW_CLOSURE 32
+#define MAKE_GENERATOR 19
+#define ENTER_FRAME 20
+#define NEW_FUNCTION 21
+#define NEW_CLOSURE 22
+#define TRACE_END 23
+#define GET_GLOBAL_AND_BUILTINS 24
+#define SAVE_EXC_STATE 25
+#define SWAP_EXC_STATE 26
+#define RESTORE_CLEAR_EXC_STATE 27
+#define SETUP_EXCEPT_HANDLER 28
+#define CLEAR_FRAME 29
+/* Opcode 30 unused */
+/* Opcode 31 unused */
+/* Opcode 32 unused */
 /* Non-trace opcode GET_CLASS_ATTR = 33 */
 /* Non-trace opcode HAS_CLASS_ATTR = 34 */
 /* Non-trace opcode OVERRIDES_GENERIC_GETATTRIBUTE = 35 */
 /* Non-trace opcode OVERRIDES_GENERIC_SETATTR = 47 */
 /* Non-trace opcode DESCRIPTOR_GET = 48 */
 /* Non-trace opcode DESCRIPTOR_SET = 49 */
-#define TRACE_END 50
-#define GET_GLOBAL_AND_BUILTINS 51
-#define SAVE_EXC_STATE 52
-#define SWAP_EXC_STATE 53
+/* Opcode 50 unused */
+/* Opcode 51 unused */
+/* Opcode 52 unused */
+/* Opcode 53 unused */
 /* Non-trace opcode STORE_MAP = 54 */
-/* Non-trace opcode INPLACE_ADD = 55 */
-/* Non-trace opcode INPLACE_SUBTRACT = 56 */
-/* Non-trace opcode INPLACE_MULTIPLY = 57 */
-#define RESTORE_CLEAR_EXC_STATE 58
-/* Non-trace opcode INPLACE_MODULO = 59 */
+/* Opcode 55 unused */
+/* Opcode 56 unused */
+/* Opcode 57 unused */
+/* Opcode 58 unused */
+/* Opcode 59 unused */
 /* Non-trace opcode STORE_SUBSCR = 60 */
 /* Non-trace opcode DELETE_SUBSCR = 61 */
-/* Non-trace opcode BINARY_LSHIFT = 62 */
-/* Non-trace opcode BINARY_RSHIFT = 63 */
-/* Non-trace opcode BINARY_AND = 64 */
-/* Non-trace opcode BINARY_XOR = 65 */
-/* Non-trace opcode BINARY_OR = 66 */
-/* Non-trace opcode INPLACE_POWER = 67 */
+/* Opcode 62 unused */
+/* Opcode 63 unused */
+/* Opcode 64 unused */
+/* Opcode 65 unused */
+/* Opcode 66 unused */
+/* Opcode 67 unused */
 /* Non-trace opcode GET_ITER = 68 */
 /* Non-trace opcode STORE_LOCALS = 69 */
 /* Non-trace opcode PRINT_EXPR = 70 */
 /* Non-trace opcode LOAD_BUILD_CLASS = 71 */
 /* Non-trace opcode YIELD_FROM = 72 */
-#define SETUP_EXCEPT_HANDLER 73
-#define CLEAR_FRAME 74
-/* Non-trace opcode INPLACE_LSHIFT = 75 */
-/* Non-trace opcode INPLACE_RSHIFT = 76 */
-/* Non-trace opcode INPLACE_AND = 77 */
-/* Non-trace opcode INPLACE_XOR = 78 */
-/* Non-trace opcode INPLACE_OR = 79 */
+/* Opcode 73 unused */
+/* Opcode 74 unused */
+/* Opcode 75 unused */
+/* Opcode 76 unused */
+/* Opcode 77 unused */
+/* Opcode 78 unused */
+/* Opcode 79 unused */
 /* Non-trace opcode BREAK_LOOP = 80 */
 /* Non-trace opcode WITH_CLEANUP = 81 */
 /* Non-trace opcode ALLOCATE = 82 */
 
 #ifdef WITH_INSTRUCTION_NAMES
 char * _HotPy_Instruction_Names[] = {
-NULL,
+"0",
 "POP_TOP",
 "ROT_TWO",
 "ROT_THREE",
 "STOP_ITERATION_OR_RERAISE",
 "SUB_TYPE",
 "KW_DICT_MERGE",
-"BINARY_POWER",
-"BINARY_MULTIPLY",
-"MAKE_GENERATOR",
-"BINARY_MODULO",
-"BINARY_ADD",
-"BINARY_SUBTRACT",
-"BINARY_SUBSCR",
-"BINARY_FLOOR_DIVIDE",
-"BINARY_TRUE_DIVIDE",
-"INPLACE_FLOOR_DIVIDE",
-"INPLACE_TRUE_DIVIDE",
-"ENTER_FRAME",
+"BINARY_POWER or MAKE_GENERATOR",
+"BINARY_MULTIPLY or ENTER_FRAME",
 "NEW_FUNCTION",
-"NEW_CLOSURE",
+"BINARY_MODULO or NEW_CLOSURE",
+"BINARY_ADD or TRACE_END",
+"BINARY_SUBTRACT or GET_GLOBAL_AND_BUILTINS",
+"BINARY_SUBSCR or SAVE_EXC_STATE",
+"BINARY_FLOOR_DIVIDE or SWAP_EXC_STATE",
+"BINARY_TRUE_DIVIDE or RESTORE_CLEAR_EXC_STATE",
+"INPLACE_FLOOR_DIVIDE or SETUP_EXCEPT_HANDLER",
+"INPLACE_TRUE_DIVIDE or CLEAR_FRAME",
+"30",
+"31",
+"32",
 "GET_CLASS_ATTR",
 "HAS_CLASS_ATTR",
 "OVERRIDES_GENERIC_GETATTRIBUTE",
 "OVERRIDES_GENERIC_SETATTR",
 "DESCRIPTOR_GET",
 "DESCRIPTOR_SET",
-"TRACE_END",
-"GET_GLOBAL_AND_BUILTINS",
-"SAVE_EXC_STATE",
-"SWAP_EXC_STATE",
+"50",
+"51",
+"52",
+"53",
 "STORE_MAP",
 "INPLACE_ADD",
 "INPLACE_SUBTRACT",
 "INPLACE_MULTIPLY",
-"RESTORE_CLEAR_EXC_STATE",
+"58",
 "INPLACE_MODULO",
 "STORE_SUBSCR",
 "DELETE_SUBSCR",
 "PRINT_EXPR",
 "LOAD_BUILD_CLASS",
 "YIELD_FROM",
-"SETUP_EXCEPT_HANDLER",
-"CLEAR_FRAME",
+"73",
+"74",
 "INPLACE_LSHIFT",
 "INPLACE_RSHIFT",
 "INPLACE_AND",
 "BINARY_OP_KV",
 "BINARY_OP_VK",
 "LOAD_MEMBER",
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
-NULL,
+"220",
+"221",
+"222",
+"223",
+"224",
+"225",
+"226",
+"227",
+"228",
+"229",
+"230",
+"231",
+"232",
+"233",
+"234",
+"235",
+"236",
+"237",
+"238",
+"239",
+"240",
+"241",
+"242",
+"243",
+"244",
+"245",
+"246",
+"247",
+"248",
+"249",
+"250",
+"251",
+"252",
+"253",
+"254",
+"255",
 };
 
 #else
 	$(OPCODETARGETGEN) $(OPCODETARGETS_H)
 
 $(TRACE_OPCODETARGETS_H): $(TRACE_OPCODETARGETGEN_FILES)
-	./python -S $(TRACE_OPCODETARGETGEN) -t $(TRACE_OPCODETARGETS_H)
+	./python -Xtracing=no -S $(TRACE_OPCODETARGETGEN) -t $(TRACE_OPCODETARGETS_H)
 
 $(REGISTER_OPCODETARGETS_H): $(REGISTER_OPCODETARGETGEN_FILES)
-	./python -S $(TRACE_OPCODETARGETGEN) -r $(REGISTER_OPCODETARGETS_H)
+	./python -Xtracing=no -S $(TRACE_OPCODETARGETGEN) -r $(REGISTER_OPCODETARGETS_H)
 
 $(srcdir)/Include/register_macros.h: $(srcdir)/HotPy/gen_format_code.py
 	$(srcdir)/HotPy/gen_format_code.py d $(srcdir)/Include/register_macros.h

Python/register_opcode_targets.h

 &&TARGET_STOP_ITERATION_OR_RERAISE,
 &&TARGET_SUB_TYPE,
 &&TARGET_KW_DICT_MERGE,
-&&_unknown_opcode, /* BINARY_POWER */
-&&_unknown_opcode, /* BINARY_MULTIPLY */
 &&TARGET_MAKE_GENERATOR,
-&&_unknown_opcode, /* BINARY_MODULO */
-&&_unknown_opcode, /* BINARY_ADD */
-&&_unknown_opcode, /* BINARY_SUBTRACT */
-&&_unknown_opcode, /* BINARY_SUBSCR */
-&&_unknown_opcode, /* BINARY_FLOOR_DIVIDE */
-&&_unknown_opcode, /* BINARY_TRUE_DIVIDE */
-&&_unknown_opcode, /* INPLACE_FLOOR_DIVIDE */
-&&_unknown_opcode, /* INPLACE_TRUE_DIVIDE */
 &&TARGET_ENTER_FRAME,
 &&TARGET_NEW_FUNCTION,
 &&TARGET_NEW_CLOSURE,
+&&_unknown_opcode, /* TRACE_END */
+&&TARGET_GET_GLOBAL_AND_BUILTINS,
+&&TARGET_SAVE_EXC_STATE,
+&&TARGET_SWAP_EXC_STATE,
+&&TARGET_RESTORE_CLEAR_EXC_STATE,
+&&TARGET_SETUP_EXCEPT_HANDLER,
+&&TARGET_CLEAR_FRAME,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&TARGET_GET_CLASS_ATTR,
 &&TARGET_HAS_CLASS_ATTR,
 &&TARGET_OVERRIDES_GENERIC_GETATTRIBUTE,
 &&TARGET_OVERRIDES_GENERIC_SETATTR,
 &&TARGET_DESCRIPTOR_GET,
 &&_unknown_opcode, /* DESCRIPTOR_SET */
-&&_unknown_opcode, /* TRACE_END */
-&&TARGET_GET_GLOBAL_AND_BUILTINS,
-&&TARGET_SAVE_EXC_STATE,
-&&TARGET_SWAP_EXC_STATE,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&TARGET_STORE_MAP,
-&&_unknown_opcode, /* INPLACE_ADD */
-&&_unknown_opcode, /* INPLACE_SUBTRACT */
-&&_unknown_opcode, /* INPLACE_MULTIPLY */
-&&TARGET_RESTORE_CLEAR_EXC_STATE,
-&&_unknown_opcode, /* INPLACE_MODULO */
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&_unknown_opcode, /* STORE_SUBSCR */
 &&_unknown_opcode, /* DELETE_SUBSCR */
-&&_unknown_opcode, /* BINARY_LSHIFT */
-&&_unknown_opcode, /* BINARY_RSHIFT */
-&&_unknown_opcode, /* BINARY_AND */
-&&_unknown_opcode, /* BINARY_XOR */
-&&_unknown_opcode, /* BINARY_OR */
-&&_unknown_opcode, /* INPLACE_POWER */
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&_unknown_opcode, /* GET_ITER */
 &&TARGET_STORE_LOCALS,
 &&_unknown_opcode, /* PRINT_EXPR */
 &&_unknown_opcode, /* LOAD_BUILD_CLASS */
 &&_unknown_opcode, /* YIELD_FROM */
-&&TARGET_SETUP_EXCEPT_HANDLER,
-&&TARGET_CLEAR_FRAME,
-&&_unknown_opcode, /* INPLACE_LSHIFT */
-&&_unknown_opcode, /* INPLACE_RSHIFT */
-&&_unknown_opcode, /* INPLACE_AND */
-&&_unknown_opcode, /* INPLACE_XOR */
-&&_unknown_opcode, /* INPLACE_OR */
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&_unknown_opcode, /* BREAK_LOOP */
 &&_unknown_opcode, /* WITH_CLEANUP */
 &&TARGET_ALLOCATE,

Python/trace_logger.c

 static void
 dead_locals(HotPyOptimiser *x)
 {
-    fprintf(OUT, "#Dead locals\n", INDENT);
+    fprintf(OUT, "%s#Dead locals\n", INDENT);
     x->next->dead_locals(x->next);
 }
 

Python/trace_opcode_targets.h

 &&TARGET_STOP_ITERATION_OR_RERAISE,
 &&TARGET_SUB_TYPE,
 &&TARGET_KW_DICT_MERGE,
-&&_unknown_opcode, /* BINARY_POWER */
-&&_unknown_opcode, /* BINARY_MULTIPLY */
 &&TARGET_MAKE_GENERATOR,
-&&_unknown_opcode, /* BINARY_MODULO */
-&&_unknown_opcode, /* BINARY_ADD */
-&&_unknown_opcode, /* BINARY_SUBTRACT */
-&&_unknown_opcode, /* BINARY_SUBSCR */
-&&_unknown_opcode, /* BINARY_FLOOR_DIVIDE */
-&&_unknown_opcode, /* BINARY_TRUE_DIVIDE */
-&&_unknown_opcode, /* INPLACE_FLOOR_DIVIDE */
-&&_unknown_opcode, /* INPLACE_TRUE_DIVIDE */
 &&TARGET_ENTER_FRAME,
 &&TARGET_NEW_FUNCTION,
 &&TARGET_NEW_CLOSURE,
+&&_unknown_opcode, /* TRACE_END */
+&&TARGET_GET_GLOBAL_AND_BUILTINS,
+&&TARGET_SAVE_EXC_STATE,
+&&TARGET_SWAP_EXC_STATE,
+&&TARGET_RESTORE_CLEAR_EXC_STATE,
+&&TARGET_SETUP_EXCEPT_HANDLER,
+&&TARGET_CLEAR_FRAME,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&TARGET_GET_CLASS_ATTR,
 &&TARGET_HAS_CLASS_ATTR,
 &&TARGET_OVERRIDES_GENERIC_GETATTRIBUTE,
 &&TARGET_OVERRIDES_GENERIC_SETATTR,
 &&TARGET_DESCRIPTOR_GET,
 &&_unknown_opcode, /* DESCRIPTOR_SET */
-&&_unknown_opcode, /* TRACE_END */
-&&TARGET_GET_GLOBAL_AND_BUILTINS,
-&&TARGET_SAVE_EXC_STATE,
-&&TARGET_SWAP_EXC_STATE,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&TARGET_STORE_MAP,
-&&_unknown_opcode, /* INPLACE_ADD */
-&&_unknown_opcode, /* INPLACE_SUBTRACT */
-&&_unknown_opcode, /* INPLACE_MULTIPLY */
-&&TARGET_RESTORE_CLEAR_EXC_STATE,
-&&_unknown_opcode, /* INPLACE_MODULO */
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&_unknown_opcode, /* STORE_SUBSCR */
 &&_unknown_opcode, /* DELETE_SUBSCR */
-&&_unknown_opcode, /* BINARY_LSHIFT */
-&&_unknown_opcode, /* BINARY_RSHIFT */
-&&_unknown_opcode, /* BINARY_AND */
-&&_unknown_opcode, /* BINARY_XOR */
-&&_unknown_opcode, /* BINARY_OR */
-&&_unknown_opcode, /* INPLACE_POWER */
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&_unknown_opcode, /* GET_ITER */
 &&TARGET_STORE_LOCALS,
 &&_unknown_opcode, /* PRINT_EXPR */
 &&_unknown_opcode, /* LOAD_BUILD_CLASS */
 &&_unknown_opcode, /* YIELD_FROM */
-&&TARGET_SETUP_EXCEPT_HANDLER,
-&&TARGET_CLEAR_FRAME,
-&&_unknown_opcode, /* INPLACE_LSHIFT */
-&&_unknown_opcode, /* INPLACE_RSHIFT */
-&&_unknown_opcode, /* INPLACE_AND */
-&&_unknown_opcode, /* INPLACE_XOR */
-&&_unknown_opcode, /* INPLACE_OR */
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
+&&_unknown_opcode,
 &&_unknown_opcode, /* BREAK_LOOP */
 &&_unknown_opcode, /* WITH_CLEANUP */
 &&TARGET_ALLOCATE,