From 7d5c692313a70bfd6aa2f51d9ed6069def2d4ee2 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 9 Jan 2022 01:04:51 +0900 Subject: [PATCH] [gamecode] Generate the new opcodes enum too It turns out I'll be tweaking it more than I expected. --- include/QF/progs/pr_comp.h | 170 +----------------------------------- libs/gamecode/Makemodule.am | 13 ++- libs/gamecode/opcodes.py | 55 +++++++++--- 3 files changed, 54 insertions(+), 184 deletions(-) diff --git a/include/QF/progs/pr_comp.h b/include/QF/progs/pr_comp.h index 9e819ad38..9b30bf170 100644 --- a/include/QF/progs/pr_comp.h +++ b/include/QF/progs/pr_comp.h @@ -427,175 +427,9 @@ typedef enum { #define OP_BREAK 0x8000 typedef enum { - // 0 0000 load from [a,b] -> c - OP_LOAD_E_1, OP_LOAD_E_2, OP_LOAD_E_3, OP_LOAD_E_4, - OP_LOAD_B_1, OP_LOAD_B_2, OP_LOAD_B_3, OP_LOAD_B_4, - OP_LOAD_C_1, OP_LOAD_C_2, OP_LOAD_C_3, OP_LOAD_C_4, - OP_LOAD_D_1, OP_LOAD_D_2, OP_LOAD_D_3, OP_LOAD_D_4, - // 0 0001 store from c -> [a, b] - OP_STORE_A_1, OP_STORE_A_2, OP_STORE_A_3, OP_STORE_A_4, // redundant? - OP_STORE_B_1, OP_STORE_B_2, OP_STORE_B_3, OP_STORE_B_4, - OP_STORE_C_1, OP_STORE_C_2, OP_STORE_C_3, OP_STORE_C_4, - OP_STORE_D_1, OP_STORE_D_2, OP_STORE_D_3, OP_STORE_D_4, - // 0 0010 push from [a,b] to the stack - OP_PUSH_A_1, OP_PUSH_A_2, OP_PUSH_A_3, OP_PUSH_A_4, - OP_PUSH_B_1, OP_PUSH_B_2, OP_PUSH_B_3, OP_PUSH_B_4, - OP_PUSH_C_1, OP_PUSH_C_2, OP_PUSH_C_3, OP_PUSH_C_4, - OP_PUSH_D_1, OP_PUSH_D_2, OP_PUSH_D_3, OP_PUSH_D_4, - // 0 0011 pop from the stack to [a,b] - OP_POP_A_1, OP_POP_A_2, OP_POP_A_3, OP_POP_A_4, - OP_POP_B_1, OP_POP_B_2, OP_POP_B_3, OP_POP_B_4, - OP_POP_C_1, OP_POP_C_2, OP_POP_C_3, OP_POP_C_4, - OP_POP_D_1, OP_POP_D_2, OP_POP_D_3, OP_POP_D_4, - // 0 0100 flow control - OP_IFNOT_A, OP_IFNOT_B, OP_IFNOT_C, OP_IFNOT_D, - OP_IF_A, OP_IF_B, OP_IF_C, OP_IF_D, - OP_JUMP_A, OP_JUMP_B, OP_JUMP_C, OP_JUMP_D, - OP_CALL_A, OP_CALL_B, OP_CALL_C, OP_CALL_D, - // 0 0101 calls - OP_CALL_1, OP_CALL_2, OP_CALL_3, OP_CALL_4, - OP_CALL_5, OP_CALL_6, OP_CALL_7, OP_CALL_8, - OP_RETURN_1, OP_RETURN_2, OP_RETURN_3, OP_RETURN_4, - OP_RETURN_0, OP_WITH, OP_STATE_ft, OP_STATE_ftt, - // 0 0110 flow control 2 - OP_IFA_A, OP_IFA_B, OP_IFA_C, OP_IFA_D, - OP_IFBE_A, OP_IFBE_B, OP_IFBE_C, OP_IFBE_D, - OP_IFB_A, OP_IFB_B, OP_IFB_C, OP_IFB_D, - OP_IFAE_A, OP_IFAE_B, OP_IFAE_C, OP_IFAE_D, - // 0 0111 interpreted vector multiplication - // C complex - // V vector (3d) - // Q quaternion - OP_CDOT_F, OP_VDOT_F, OP_QDOT_F, OP_CROSS_F, - OP_CMUL_F, OP_QVMUL_F, OP_VQMUL_F, OP_QMUL_F, - OP_CDOT_D, OP_VDOT_D, OP_QDOT_D, OP_CROSS_D, - OP_CMUL_D, OP_QVMUL_D, OP_VQMUL_D, OP_QMUL_D, - // comparison - // 0 1000 == - OP_EQ_I_1, OP_EQ_I_2, OP_EQ_I_3, OP_EQ_I_4, - OP_EQ_F_1, OP_EQ_F_2, OP_EQ_F_3, OP_EQ_F_4, - OP_EQ_L_1, OP_EQ_L_2, OP_EQ_L_3, OP_EQ_L_4, - OP_EQ_D_1, OP_EQ_D_2, OP_EQ_D_3, OP_EQ_D_4, - // 0 1001 < - OP_LT_I_1, OP_LT_I_2, OP_LT_I_3, OP_LT_I_4, - OP_LT_F_1, OP_LT_F_2, OP_LT_F_3, OP_LT_F_4, - OP_LT_L_1, OP_LT_L_2, OP_LT_L_3, OP_LT_L_4, - OP_LT_D_1, OP_LT_D_2, OP_LT_D_3, OP_LT_D_4, - // 0 1010 > - OP_GT_I_1, OP_GT_I_2, OP_GT_I_3, OP_GT_I_4, - OP_GT_F_1, OP_GT_F_2, OP_GT_F_3, OP_GT_F_4, - OP_GT_L_1, OP_GT_L_2, OP_GT_L_3, OP_GT_L_4, - OP_GT_D_1, OP_GT_D_2, OP_GT_D_3, OP_GT_D_4, - // 0 1011 - OP_spare_1, OP_spare_2, OP_spare_3, OP_spare_4, - OP_spare_5, OP_spare_6, OP_spare_7, OP_spare_8, - OP_spare_9, OP_spare_10, OP_spare_11, OP_spare_12, - OP_spare_13, OP_spare_14, OP_spare_15, OP_spare_16, - // comparison - // 0 1100 != - OP_NE_I_1, OP_NE_I_2, OP_NE_I_3, OP_NE_I_4, - OP_NE_F_1, OP_NE_F_2, OP_NE_F_3, OP_NE_F_4, - OP_NE_L_1, OP_NE_L_2, OP_NE_L_3, OP_NE_L_4, - OP_NE_D_1, OP_NE_D_2, OP_NE_D_3, OP_NE_D_4, - // 0 1101 >= - OP_GE_I_1, OP_GE_I_2, OP_GE_I_3, OP_GE_I_4, - OP_GE_F_1, OP_GE_F_2, OP_GE_F_3, OP_GE_F_4, - OP_GE_L_1, OP_GE_L_2, OP_GE_L_3, OP_GE_L_4, - OP_GE_D_1, OP_GE_D_2, OP_GE_D_3, OP_GE_D_4, - // 0 1110 <= - OP_LE_I_1, OP_LE_I_2, OP_LE_I_3, OP_LE_I_4, - OP_LE_F_1, OP_LE_F_2, OP_LE_F_3, OP_LE_F_4, - OP_LE_L_1, OP_LE_L_2, OP_LE_L_3, OP_LE_L_4, - OP_LE_D_1, OP_LE_D_2, OP_LE_D_3, OP_LE_D_4, - // 0 1111 - OP_spare_17, OP_spare_18, OP_spare_19, OP_spare_20, - OP_spare_21, OP_spare_22, OP_spare_23, OP_spare_24, - OP_spare_25, OP_spare_26, OP_spare_27, OP_spare_28, - OP_spare_29, OP_spare_30, OP_spare_31, OP_spare_32, - - // 1 0000 c = a * b - OP_MUL_I_1, OP_MUL_I_2, OP_MUL_I_3, OP_MUL_I_4, - OP_MUL_F_1, OP_MUL_F_2, OP_MUL_F_3, OP_MUL_F_4, - OP_MUL_L_1, OP_MUL_L_2, OP_MUL_L_3, OP_MUL_L_4, - OP_MUL_D_1, OP_MUL_D_2, OP_MUL_D_3, OP_MUL_D_4, - // 1 0001 c = a / b - OP_DIV_I_1, OP_DIV_I_2, OP_DIV_I_3, OP_DIV_I_4, - OP_DIV_F_1, OP_DIV_F_2, OP_DIV_F_3, OP_DIV_F_4, - OP_DIV_L_1, OP_DIV_L_2, OP_DIV_L_3, OP_DIV_L_4, - OP_DIV_D_1, OP_DIV_D_2, OP_DIV_D_3, OP_DIV_D_4, - // 1 0010 c = a % b (remainder, C %) - OP_REM_I_1, OP_REM_I_2, OP_REM_I_3, OP_REM_I_4, - OP_REM_F_1, OP_REM_F_2, OP_REM_F_3, OP_REM_F_4, - OP_REM_L_1, OP_REM_L_2, OP_REM_L_3, OP_REM_L_4, - OP_REM_D_1, OP_REM_D_2, OP_REM_D_3, OP_REM_D_4, - // 1 0011 c = a %% b (true modulo, python %) - OP_MOD_I_1, OP_MOD_I_2, OP_MOD_I_3, OP_MOD_I_4, - OP_MOD_F_1, OP_MOD_F_2, OP_MOD_F_3, OP_MOD_F_4, - OP_MOD_L_1, OP_MOD_L_2, OP_MOD_L_3, OP_MOD_L_4, - OP_MOD_D_1, OP_MOD_D_2, OP_MOD_D_3, OP_MOD_D_4, - // 1 0100 c = a + b - OP_ADD_I_1, OP_ADD_I_2, OP_ADD_I_3, OP_ADD_I_4, - OP_ADD_F_1, OP_ADD_F_2, OP_ADD_F_3, OP_ADD_F_4, - OP_ADD_L_1, OP_ADD_L_2, OP_ADD_L_3, OP_ADD_L_4, - OP_ADD_D_1, OP_ADD_D_2, OP_ADD_D_3, OP_ADD_D_4, - // 1 0101 c = a - b - OP_SUB_I_1, OP_SUB_I_2, OP_SUB_I_3, OP_SUB_I_4, - OP_SUB_F_1, OP_SUB_F_2, OP_SUB_F_3, OP_SUB_F_4, - OP_SUB_L_1, OP_SUB_L_2, OP_SUB_L_3, OP_SUB_L_4, - OP_SUB_D_1, OP_SUB_D_2, OP_SUB_D_3, OP_SUB_D_4, - // 1 0110 c = a << b (string ops mixed in) - OP_SHL_I_1, OP_SHL_I_2, OP_SHL_I_3, OP_SHL_I_4, - OP_EQ_S, OP_LT_S, OP_GT_S, OP_ADD_S, - OP_SHL_L_1, OP_SHL_L_2, OP_SHL_L_3, OP_SHL_L_4, - OP_CMP_S, OP_GE_S, OP_LE_S, OP_NOT_S, //OP_CMP_S doubles as NE - // 1 0111 c = a >> b - OP_ASR_I_1, OP_ASR_I_2, OP_ASR_I_3, OP_ASR_I_4, - OP_SHR_u_1, OP_SHR_u_2, OP_SHR_u_3, OP_SHR_u_4, - OP_ASR_L_1, OP_ASR_L_2, OP_ASR_L_3, OP_ASR_L_4, - OP_SHR_U_1, OP_SHR_U_2, OP_SHR_U_3, OP_SHR_U_4, - // 1 1000 c = a (& | ^) b or ~a (bitwise ops) - OP_BITAND_I_1, OP_BITAND_I_2, OP_BITAND_I_3, OP_BITAND_I_4, - OP_BITOR_I_1, OP_BITOR_I_2, OP_BITOR_I_3, OP_BITOR_I_4, - OP_BITXOR_I_1, OP_BITXOR_I_2, OP_BITXOR_I_3, OP_BITXOR_I_4, - OP_BITNOT_I_1, OP_BITNOT_I_2, OP_BITNOT_I_3, OP_BITNOT_I_4, - // 1 1001 < unsigned with swizzle and scale mixed in - OP_LT_u_1, OP_LT_u_2, OP_LT_u_3, OP_LT_u_4, - OP_SWIZZLE_F, OP_SCALE_F_2, OP_SCALE_F_3, OP_SCALE_F_4, - OP_LT_U_1, OP_LT_U_2, OP_LT_U_3, OP_LT_U_4, - OP_SWIZZLE_D, OP_SCALE_D_2, OP_SCALE_D_3, OP_SCALE_D_4, - // 1 1010 > unsigned and conversions - OP_GT_u_1, OP_GT_u_2, OP_GT_u_3, OP_GT_u_4, - OP_CONV_IF, OP_CONV_LD, OP_CONV_uF, OP_CONV_UD, - OP_GT_U_1, OP_GT_U_2, OP_GT_U_3, OP_GT_U_4, - OP_CONV_FI, OP_CONV_DL, OP_CONV_Fu, OP_CONV_DU, - // 1 1011 lea, with, etc - OP_LEA_A, OP_LEA_B, OP_LEA_C, OP_LEA_D, - OP_LEA_E, OP_ANY_2, OP_ANY_3, OP_ANY_4, - OP_PUSHREGS, OP_ALL_2, OP_ALL_3, OP_ALL_4, - OP_POPREGS, OP_NONE_2, OP_NONE_3, OP_NONE_4, - // 1 1100 c = a (&& || ^^) b or !a (logical ops (no short circuit)) - OP_AND_I_1, OP_AND_I_2, OP_AND_I_3, OP_AND_I_4, - OP_OR_I_1, OP_OR_I_2, OP_OR_I_3, OP_OR_I_4, - OP_XOR_I_1, OP_XOR_I_2, OP_XOR_I_3, OP_XOR_I_4, - OP_NOT_I_1, OP_NOT_I_2, OP_NOT_I_3, OP_NOT_I_4, - // 1 1101 >= unsigned with q v4 mul and moves mixed in - OP_GE_u_1, OP_GE_u_2, OP_GE_u_3, OP_GE_u_4, - OP_QV4MUL_F, OP_MOVE_I, OP_MOVE_P, OP_MOVE_PI, - OP_GE_U_1, OP_GE_U_2, OP_GE_U_3, OP_GE_U_4, - OP_QV4MUL_D, OP_MEMSET_I, OP_MEMSET_P, OP_MEMSET_PI, - // 1 1110 <= unsigned with v4 q mul and conversion mixed in - OP_LE_u_1, OP_LE_u_2, OP_LE_u_3, OP_LE_u_4, - OP_V4QMUL_F, OP_CONV_IL_1, OP_CONV_uU_1, OP_CONV_FD_1, - OP_LE_U_1, OP_LE_U_2, OP_LE_U_3, OP_LE_U_4, - OP_V4QMUL_D, OP_CONV_LI_1, OP_CONV_Uu_1, OP_CONV_DF_1, - // 1 1111 - OP_spare_33, OP_spare_34, OP_spare_35, OP_spare_36, - OP_spare_37, OP_spare_38, OP_spare_39, OP_spare_40, - OP_spare_41, OP_spare_42, OP_spare_43, OP_spare_44, - OP_spare_45, OP_spare_46, OP_spare_47, OP_spare_48, - - +#include "QF/progs/pr_opcode.hinc" } pr_opcode_e; + #define OP_A_SHIFT (9) #define OP_B_SHIFT (11) #define OP_C_SHIFT (13) diff --git a/libs/gamecode/Makemodule.am b/libs/gamecode/Makemodule.am index 620edc7ee..e3c9b5441 100644 --- a/libs/gamecode/Makemodule.am +++ b/libs/gamecode/Makemodule.am @@ -25,10 +25,19 @@ noinst_PYTHON += $(opcodes_py) opcodes_py = $(srcdir)/libs/gamecode/opcodes.py pr_opcode_cinc = $(top_builddir)/libs/gamecode/pr_opcode.cinc +pr_opcode_hinc = $(top_builddir)/include/QF/progs/pr_opcode.hinc pr_opcode_src = \ - ${pr_opcode_cinc} + ${pr_opcode_cinc} \ + ${pr_opcode_hinc} libs/gamecode/pr_opcode.lo: libs/gamecode/pr_opcode.c ${pr_opcode_src} +BUILT_SOURCES += $(pr_opcode_cinc) $(pr_opcode_hinc) + $(pr_opcode_cinc): $(opcodes_py) - $(V_PY)$(PYTHON) $(opcodes_py) > $(pr_opcode_cinc).t && \ + $(V_PY)$(PYTHON) $(opcodes_py) table > $(pr_opcode_cinc).t && \ $(am__mv) $(pr_opcode_cinc).t $(pr_opcode_cinc) + +$(pr_opcode_hinc): $(opcodes_py) + $(V_PY) mkdir -p `dirname $(pr_opcode_hinc)` &&\ + $(PYTHON) $(opcodes_py) enum > $(pr_opcode_hinc).t && \ + $(am__mv) $(pr_opcode_hinc).t $(pr_opcode_hinc) diff --git a/libs/gamecode/opcodes.py b/libs/gamecode/opcodes.py index b38763156..0c18dafe6 100644 --- a/libs/gamecode/opcodes.py +++ b/libs/gamecode/opcodes.py @@ -36,6 +36,8 @@ n 1111 nnnn 0 1011 nnnn """ +import copy + load_fmt = [ "%Ga.%Gb(%Ea), %gc", "*%Ga, %gc", @@ -184,6 +186,15 @@ convert_formats = { ], }, } +convert2_formats = copy.deepcopy (convert_formats) +convert2_formats["args"]["op_conv"] = [None, "IL", "uU", "FD", + None, "LI", "Uu", "DF"] +convert2_formats["args"]["cnv_types"] = [ + [None, None], + ["ev_integer", "ev_long"], + ["ev_uinteger", "ev_ulong"], + ["ev_float", "ev_double"], +] lea_formats = { "opcode": "OP_LEA_{op_mode[mm]}", "mnemonic": "lea", @@ -505,7 +516,7 @@ group_map = { "compare": compare_formats, "compare2": compare2_formats, "convert": convert_formats, - "convert2": convert_formats, + "convert2": convert2_formats, "lea": lea_formats, "lea_e": lea_e_formats, "load": load_formats, @@ -598,6 +609,12 @@ def process_opcode(opcode, group): inst["wd"] = "{%s}" % eval(f'''f"{gm['widths']}"''', params) inst["ty"] = "{%s}" % eval(f'''f"{gm['types']}"''', params) +import sys + +if len (sys.argv) < 2 or sys.argv[1] not in ["enum", "table"]: + sys.stderr.write ("specify output type: enum or table\n") + sys.exit(1) + lines = bitmap_txt.split('\n') for l in lines: if not l: @@ -612,16 +629,26 @@ for l in lines: for i, group in enumerate(opcodes): if group is not None: process_opcode (i, group) -N = 8 -W = 9 -#for i in range(len(opcodes)//N): -# print("%03x" % (i << 3), " ".join(map(lambda op: "%-*.*s" % (W, W, op and op["op"] or None), opcodes[i*N:i*N+N]))) -for i, group in enumerate(opcodes): - if group is not None: - print(eval('f"[{op}] = {{\\n' - '\\t.opname = {on},\\n' - '\\t.mnemonic = {mn},\\n' - '\\t.widths = {wd},\\n' - '\\t.types = {ty},\\n' - '\\t.fmt = {fmt},\\n' - '}},"', group)) +if sys.argv[1] == "enum": + N = 4 + W = 15 + for i in range(len(opcodes)//N): + row = opcodes[i * N:i * N + N] + #row = map(lambda op: (op and op["op"] or f"OP_spare_{i}"), row) + row = [(op and op["op"] or f"OP_spare_{i*N+x}") + for x, op in enumerate(row)] + row = map(lambda op: f"%-{W}.{W}s" % (op + ','), row) + if sys.argv[2:3] == ["debug"]: + print("%03x" % (i << 3), " ".join(row)) + else: + print(" ".join(row)) +elif sys.argv[1] == "table": + for i, group in enumerate(opcodes): + if group is not None: + print(eval('f"[{op}] = {{\\n' + '\\t.opname = {on},\\n' + '\\t.mnemonic = {mn},\\n' + '\\t.widths = {wd},\\n' + '\\t.types = {ty},\\n' + '\\t.fmt = {fmt},\\n' + '}},"', group))