From 1b84570aa320c79b6a18d2d77ea559907c2ce108 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Wed, 26 Oct 2022 22:06:07 +0300 Subject: [PATCH] Intoduce ir_emit.c that shuould keep common part for different targets --- .gitignore | 4 ++-- Makefile | 12 ++++++------ ir_aarch64.dasc | 28 ---------------------------- ir_emit.c | 38 ++++++++++++++++++++++++++++++++++++++ ir_x86.dasc | 28 ---------------------------- 5 files changed, 46 insertions(+), 64 deletions(-) create mode 100644 ir_emit.c diff --git a/.gitignore b/.gitignore index 05e8e59..bdb9077 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,8 @@ *.dot *.pdf ir_fold_hash.h -ir_x86.c -ir_aarch64.c +ir_emit_x86.h +ir_emit_aarch64.h minilua gen_ir_fold_hash ir_test diff --git a/Makefile b/Makefile index a9c48ff..684c309 100644 --- a/Makefile +++ b/Makefile @@ -41,13 +41,13 @@ endif all: $(BUILD_DIR)/ir $(BUILD_DIR)/ir_test $(BUILD_DIR)/ir: $(BUILD_DIR)/ir_main.o $(BUILD_DIR)/ir.o $(BUILD_DIR)/ir_strtab.o $(BUILD_DIR)/ir_cfg.o \ - $(BUILD_DIR)/ir_sccp.o $(BUILD_DIR)/ir_gcm.o $(BUILD_DIR)/ir_ra.o $(BUILD_DIR)/ir_$(DASM_ARCH).o \ + $(BUILD_DIR)/ir_sccp.o $(BUILD_DIR)/ir_gcm.o $(BUILD_DIR)/ir_ra.o $(BUILD_DIR)/ir_emit.o \ $(BUILD_DIR)/ir_load.o $(BUILD_DIR)/ir_save.o $(BUILD_DIR)/ir_emit_c.o $(BUILD_DIR)/ir_dump.o \ $(BUILD_DIR)/ir_disasm.o $(BUILD_DIR)/ir_gdb.o $(BUILD_DIR)/ir_perf.o $(BUILD_DIR)/ir_check.o $(CC) $(CFLAGS) $(LDFLAGS) -o $@ -lcapstone $^ $(BUILD_DIR)/ir_test: $(BUILD_DIR)/ir_test.o $(BUILD_DIR)/ir.o $(BUILD_DIR)/ir_strtab.o $(BUILD_DIR)/ir_cfg.o \ - $(BUILD_DIR)/ir_sccp.o $(BUILD_DIR)/ir_gcm.o $(BUILD_DIR)/ir_ra.o $(BUILD_DIR)/ir_$(DASM_ARCH).o \ + $(BUILD_DIR)/ir_sccp.o $(BUILD_DIR)/ir_gcm.o $(BUILD_DIR)/ir_ra.o $(BUILD_DIR)/ir_emit.o \ $(BUILD_DIR)/ir_save.o $(BUILD_DIR)/ir_dump.o $(BUILD_DIR)/ir_disasm.o $(BUILD_DIR)/ir_gdb.o \ $(BUILD_DIR)/ir_perf.o $(BUILD_DIR)/ir_check.o $(CC) $(CFLAGS) $(LDFLAGS) -o $@ -lcapstone $^ @@ -96,11 +96,11 @@ $(BUILD_DIR)/gen_ir_fold_hash: $(SRC_DIR)/gen_ir_fold_hash.c $(SRC_DIR)/ir_strta $(BUILD_DIR)/minilua: $(SRC_DIR)/dynasm/minilua.c $(BUILD_CC) $(SRC_DIR)/dynasm/minilua.c -lm -o $@ -$(BUILD_DIR)/ir_$(DASM_ARCH).c: $(SRC_DIR)/ir_$(DASM_ARCH).dasc $(SRC_DIR)/dynasm/*.lua $(BUILD_DIR)/minilua +$(BUILD_DIR)/ir_emit_$(DASM_ARCH).h: $(SRC_DIR)/ir_$(DASM_ARCH).dasc $(SRC_DIR)/dynasm/*.lua $(BUILD_DIR)/minilua $(BUILD_DIR)/minilua $(SRC_DIR)/dynasm/dynasm.lua $(DASM_FLAGS) -o $@ $(SRC_DIR)/ir_$(DASM_ARCH).dasc -$(BUILD_DIR)/ir_$(DASM_ARCH).o: $(BUILD_DIR)/ir_$(DASM_ARCH).c $(SRC_DIR)/ir.h $(SRC_DIR)/ir_private.h \ +$(BUILD_DIR)/ir_emit.o: $(SRC_DIR)/ir_emit.c $(BUILD_DIR)/ir_emit_$(DASM_ARCH).h $(SRC_DIR)/ir.h $(SRC_DIR)/ir_private.h \ $(SRC_DIR)/ir_$(DASM_ARCH).h - $(CC) $(CFLAGS) -I$(SRC_DIR) -o $@ -c $< + $(CC) $(CFLAGS) -I$(BUILD_DIR) -o $@ -c $< test: $(BUILD_DIR)/ir $(BUILD_DIR)/ir $(SRC_DIR)/test.ir --dump --save 2>$(BUILD_DIR)/test.log @@ -110,7 +110,7 @@ test: $(BUILD_DIR)/ir clean: rm -rf $(BUILD_DIR)/ir $(BUILD_DIR)/ir_test $(BUILD_DIR)/*.o \ - $(BUILD_DIR)/minilua $(BUILD_DIR)/ir_$(DASM_ARCH).c \ + $(BUILD_DIR)/minilua $(BUILD_DIR)/ir_emit_$(DASM_ARCH).h \ $(BUILD_DIR)/ir_fold_hash.h $(BUILD_DIR)/gen_ir_fold_hash \ $(BUILD_DIR)/ir.dot $(BUILD_DIR)/ir.pdf $(BUILD_DIR)/test.log find $(SRC_DIR)/tests -type f -name '*.diff' -delete diff --git a/ir_aarch64.dasc b/ir_aarch64.dasc index ef44b6a..b4d5a8f 100644 --- a/ir_aarch64.dasc +++ b/ir_aarch64.dasc @@ -1,31 +1,3 @@ -#include "ir.h" -#include "ir_aarch64.h" -#include "ir_private.h" - -#define DASM_M_GROW(ctx, t, p, sz, need) \ - do { \ - size_t _sz = (sz), _need = (need); \ - if (_sz < _need) { \ - if (_sz < 16) _sz = 16; \ - while (_sz < _need) _sz += _sz; \ - (p) = (t *)ir_mem_realloc((p), _sz); \ - (sz) = _sz; \ - } \ - } while(0) - -#define DASM_M_FREE(ctx, p, sz) ir_mem_free(p) - -#if IR_DEBUG -# define DASM_CHECKS -#endif - -#include "dynasm/dasm_proto.h" -#include "dynasm/dasm_arm64.h" - -#if defined(__GNUC__) -# pragma GCC diagnostic ignored "-Warray-bounds" -#endif - |.arch arm64 |.actionlist dasm_actions diff --git a/ir_emit.c b/ir_emit.c new file mode 100644 index 0000000..002d0c3 --- /dev/null +++ b/ir_emit.c @@ -0,0 +1,38 @@ +#include "ir.h" + +#define DASM_M_GROW(ctx, t, p, sz, need) \ + do { \ + size_t _sz = (sz), _need = (need); \ + if (_sz < _need) { \ + if (_sz < 16) _sz = 16; \ + while (_sz < _need) _sz += _sz; \ + (p) = (t *)ir_mem_realloc((p), _sz); \ + (sz) = _sz; \ + } \ + } while(0) + +#define DASM_M_FREE(ctx, p, sz) ir_mem_free(p) + +#if IR_DEBUG +# define DASM_CHECKS +#endif + +#if defined(__GNUC__) +# pragma GCC diagnostic ignored "-Warray-bounds" +#endif + +#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64) +# include "ir_x86.h" +# include "ir_private.h" +# include "dynasm/dasm_proto.h" +# include "dynasm/dasm_x86.h" +# include "ir_emit_x86.h" +#elif defined(IR_TARGET_AARCH64) +# include "ir_aarch64.h" +# include "ir_private.h" +# include "dynasm/dasm_proto.h" +# include "dynasm/dasm_arm64.h" +# include "ir_emit_aarch64.h" +#else +# error "Unknown IR target" +#endif diff --git a/ir_x86.dasc b/ir_x86.dasc index 134ce05..1662a37 100644 --- a/ir_x86.dasc +++ b/ir_x86.dasc @@ -1,31 +1,3 @@ -#include "ir.h" -#include "ir_x86.h" -#include "ir_private.h" - -#define DASM_M_GROW(ctx, t, p, sz, need) \ - do { \ - size_t _sz = (sz), _need = (need); \ - if (_sz < _need) { \ - if (_sz < 16) _sz = 16; \ - while (_sz < _need) _sz += _sz; \ - (p) = (t *)ir_mem_realloc((p), _sz); \ - (sz) = _sz; \ - } \ - } while(0) - -#define DASM_M_FREE(ctx, p, sz) ir_mem_free(p) - -#if IR_DEBUG -# define DASM_CHECKS -#endif - -#include "dynasm/dasm_proto.h" -#include "dynasm/dasm_x86.h" - -#if defined(__GNUC__) -# pragma GCC diagnostic ignored "-Warray-bounds" -#endif - |.if X64 |.arch x64 |.else