From dbbb2ef384cc81085fa668a5e5a02ba080cca228 Mon Sep 17 00:00:00 2001 From: tyfkda Date: Sun, 30 Jul 2023 08:34:16 +0900 Subject: [PATCH] Separate frontend and backend sources --- Makefile | 39 +++++++++++++++-------------- compile_flags.txt | 3 ++- src/cc/{ => backend}/codegen.c | 2 +- src/cc/{ => backend}/codegen.h | 0 src/cc/{ => backend}/codegen_expr.c | 2 +- src/cc/{ => backend}/emit_util.c | 2 +- src/cc/{ => backend}/emit_util.h | 0 src/cc/{ => backend}/ir.c | 2 +- src/cc/{ => backend}/ir.h | 0 src/cc/{ => backend}/regalloc.c | 2 +- src/cc/{ => backend}/regalloc.h | 0 src/cc/{ => frontend}/ast.c | 2 +- src/cc/{ => frontend}/ast.h | 0 src/cc/{ => frontend}/lexer.c | 2 +- src/cc/{ => frontend}/lexer.h | 0 src/cc/{ => frontend}/parser.c | 2 +- src/cc/{ => frontend}/parser.h | 0 src/cc/{ => frontend}/parser_expr.c | 2 +- src/cc/{ => frontend}/type.c | 2 +- src/cc/{ => frontend}/type.h | 0 src/cc/{ => frontend}/var.c | 2 +- src/cc/{ => frontend}/var.h | 0 tests/Makefile | 17 +++++++------ 23 files changed, 43 insertions(+), 38 deletions(-) rename src/cc/{ => backend}/codegen.c (99%) rename src/cc/{ => backend}/codegen.h (100%) rename src/cc/{ => backend}/codegen_expr.c (99%) rename src/cc/{ => backend}/emit_util.c (99%) rename src/cc/{ => backend}/emit_util.h (100%) rename src/cc/{ => backend}/ir.c (99%) rename src/cc/{ => backend}/ir.h (100%) rename src/cc/{ => backend}/regalloc.c (99%) rename src/cc/{ => backend}/regalloc.h (100%) rename src/cc/{ => frontend}/ast.c (99%) rename src/cc/{ => frontend}/ast.h (100%) rename src/cc/{ => frontend}/lexer.c (99%) rename src/cc/{ => frontend}/lexer.h (100%) rename src/cc/{ => frontend}/parser.c (99%) rename src/cc/{ => frontend}/parser.h (100%) rename src/cc/{ => frontend}/parser_expr.c (99%) rename src/cc/{ => frontend}/type.c (99%) rename src/cc/{ => frontend}/type.h (100%) rename src/cc/{ => frontend}/var.c (99%) rename src/cc/{ => frontend}/var.h (100%) diff --git a/Makefile b/Makefile index e8a498803..dff17c6b7 100644 --- a/Makefile +++ b/Makefile @@ -25,14 +25,15 @@ ifeq ("$(ARCHTYPE)", "") endif CC1_ARCH_DIR:=$(CC1_DIR)/arch/$(ARCHTYPE) +CC1_FE_DIR:=$(CC1_DIR)/frontend +CC1_BE_DIR:=$(CC1_DIR)/backend OPTIMIZE:=-O2 -g3 CFLAGS:=-ansi -std=c11 -pedantic -MMD -Wall -Wextra -Werror -Wold-style-definition \ -Wno-missing-field-initializers -Wno-typedef-redefinition -Wno-empty-body \ -Wno-gnu-zero-variadic-macro-arguments \ -D_DEFAULT_SOURCE $(OPTIMIZE) \ - -I$(CC1_DIR) -I$(AS_DIR) -I$(UTIL_DIR) \ - -I$(CC1_ARCH_DIR) + -I$(CC1_FE_DIR) -I$(CC1_BE_DIR) -I$(CC1_ARCH_DIR) -I$(AS_DIR) -I$(UTIL_DIR) ifneq ("$(NO_FLONUM)","") CFLAGS+=-D__NO_FLONUM endif @@ -56,7 +57,7 @@ EXES:=xcc cc1 cpp as ld xcc_SRCS:=$(wildcard $(XCC_DIR)/*.c) \ $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c -cc1_SRCS:=$(wildcard $(CC1_DIR)/*.c) \ +cc1_SRCS:=$(wildcard $(CC1_FE_DIR)/*.c) $(wildcard $(CC1_BE_DIR)/*.c) $(wildcard $(CC1_DIR)/*.c) \ $(wildcard $(CC1_ARCH_DIR)/*.c) \ $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c cpp_SRCS:=$(wildcard $(CPP_DIR)/*.c) \ @@ -96,8 +97,8 @@ $(OBJ_DIR)/%.o: $(1)/%.c $(PARENT_DEPS) @mkdir -p $(OBJ_DIR) $(CC) $(CFLAGS) -c -o $$@ $$< endef -XCC_SRC_DIRS:=$(XCC_DIR) $(CC1_DIR) $(CC1_ARCH_DIR) $(CPP_DIR) $(AS_DIR) $(LD_DIR) $(UTIL_DIR) \ - $(DEBUG_DIR) +XCC_SRC_DIRS:=$(XCC_DIR) $(CC1_FE_DIR) $(CC1_BE_DIR) $(CC1_DIR) $(CC1_ARCH_DIR) $(CPP_DIR) \ + $(AS_DIR) $(LD_DIR) $(UTIL_DIR) $(DEBUG_DIR) $(foreach D, $(XCC_SRC_DIRS), $(eval $(call DEFINE_OBJ_TARGET,$(D)))) .PHONY: test @@ -168,9 +169,9 @@ WCC_DIR:=src/wcc WCC_CFLAGS:=$(CFLAGS) -I$(CPP_DIR) -DTARGET_WASM WCC_SRCS:=$(wildcard $(WCC_DIR)/*.c) \ - $(CC1_DIR)/lexer.c $(CC1_DIR)/type.c $(CC1_DIR)/var.c $(CC1_DIR)/ast.c $(CC1_DIR)/parser.c \ - $(CC1_DIR)/parser_expr.c $(CPP_DIR)/preprocessor.c $(CPP_DIR)/pp_parser.c $(CPP_DIR)/macro.c \ - $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c + $(CC1_FE_DIR)/lexer.c $(CC1_FE_DIR)/type.c $(CC1_FE_DIR)/var.c $(CC1_FE_DIR)/ast.c \ + $(CC1_FE_DIR)/parser.c $(CC1_FE_DIR)/parser_expr.c $(CPP_DIR)/preprocessor.c \ + $(CPP_DIR)/pp_parser.c $(CPP_DIR)/macro.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c WCC_OBJS:=$(addprefix $(WCC_OBJ_DIR)/,$(notdir $(WCC_SRCS:.c=.o))) WCC_LIBS:=$(LIBSRC_DIR)/_wasm/crt0.c $(LIBSRC_DIR)/_wasm/libc.c @@ -182,7 +183,7 @@ $(WCC_OBJ_DIR)/%.o: $(1)/%.c $(PARENT_DEPS) @mkdir -p $(WCC_OBJ_DIR) $(CC) $(WCC_CFLAGS) -c -o $$@ $$< endef -WCC_SRC_DIRS:=$(WCC_DIR) $(CC1_DIR) $(CPP_DIR) $(UTIL_DIR) +WCC_SRC_DIRS:=$(WCC_DIR) $(CC1_FE_DIR) $(CC1_BE_DIR) $(CC1_DIR) $(CPP_DIR) $(UTIL_DIR) $(foreach D, $(WCC_SRC_DIRS), $(eval $(call DEFINE_WCCOBJ_TARGET,$(D)))) WCC_CRT0_SRCS:=$(wildcard $(LIBSRC_DIR)/_wasm/crt0/*.c) @@ -243,7 +244,7 @@ test-wcc-self-hosting: $(WCC_TARGET)cc.wasm: $(WCC_SRCS) $(WCC_LIBS) $(WCC_PARENT) $(HOST_WCC) -o $@ \ - -I$(CC1_DIR) -I$(CPP_DIR) -I$(UTIL_DIR) \ + -I$(CC1_FE_DIR) -I$(CPP_DIR) -I$(UTIL_DIR) \ $(WCC_SRCS) #### www @@ -274,20 +275,20 @@ release-wcc: assets DEBUG_EXES:=dump_expr dump_ir dump_type DEBUG_CFLAGS:=$(subst -MMD,,$(CFLAGS)) -dump_expr_SRCS:=$(DEBUG_DIR)/dump_expr.c $(CC1_DIR)/parser_expr.c $(CC1_DIR)/parser.c \ - $(CC1_DIR)/lexer.c $(CC1_DIR)/type.c $(CC1_DIR)/ast.c $(CC1_DIR)/var.c \ +dump_expr_SRCS:=$(DEBUG_DIR)/dump_expr.c $(CC1_FE_DIR)/parser_expr.c $(CC1_FE_DIR)/parser.c \ + $(CC1_FE_DIR)/lexer.c $(CC1_FE_DIR)/type.c $(CC1_FE_DIR)/ast.c $(CC1_FE_DIR)/var.c \ $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c dump_expr_OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(dump_expr_SRCS:.c=.o))) -dump_ir_SRCS:=$(DEBUG_DIR)/dump_ir.c $(CC1_DIR)/parser_expr.c $(CC1_DIR)/parser.c \ - $(CC1_DIR)/lexer.c $(CC1_DIR)/type.c $(CC1_DIR)/ast.c $(CC1_DIR)/var.c $(CC1_DIR)/builtin.c \ - $(CC1_DIR)/codegen_expr.c $(CC1_DIR)/codegen.c $(CC1_DIR)/ir.c $(CC1_DIR)/regalloc.c \ - $(CC1_ARCH_DIR)/emit_code.c $(CC1_DIR)/emit_util.c $(CC1_ARCH_DIR)/ir_$(ARCHTYPE).c \ - $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c +dump_ir_SRCS:=$(DEBUG_DIR)/dump_ir.c $(CC1_FE_DIR)/parser_expr.c $(CC1_FE_DIR)/parser.c \ + $(CC1_FE_DIR)/lexer.c $(CC1_FE_DIR)/type.c $(CC1_FE_DIR)/ast.c $(CC1_FE_DIR)/var.c \ + $(CC1_BE_DIR)/codegen_expr.c $(CC1_BE_DIR)/codegen.c $(CC1_BE_DIR)/ir.c \ + $(CC1_BE_DIR)/regalloc.c $(CC1_BE_DIR)/emit_util.c $(CC1_ARCH_DIR)/emit_code.c \ + $(CC1_ARCH_DIR)/ir_$(ARCHTYPE).c $(CC1_DIR)/builtin.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c dump_ir_OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(dump_ir_SRCS:.c=.o))) -dump_type_SRCS:=$(DEBUG_DIR)/dump_type.c $(CC1_DIR)/parser_expr.c $(CC1_DIR)/parser.c \ - $(CC1_DIR)/lexer.c $(CC1_DIR)/type.c $(CC1_DIR)/ast.c $(CC1_DIR)/var.c \ +dump_typ_SRCS:=$(DEBUG_DIR)/dump_type.c $(CC1_FE_DIR)/parser_expr.c $(CC1_FE_DIR)/parser.c \ + $(CC1_FE_DIR)/lexer.c $(CC1_FE_DIR)/type.c $(CC1_FE_DIR)/ast.c $(CC1_FE_DIR)/var.c \ $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c dump_type_OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(dump_type_SRCS:.c=.o))) diff --git a/compile_flags.txt b/compile_flags.txt index ad6c8492b..428209e83 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ -I./src/as --I./src/cc +-I./src/cc/backend +-I./src/cc/frontend -I./src/util diff --git a/src/cc/codegen.c b/src/cc/backend/codegen.c similarity index 99% rename from src/cc/codegen.c rename to src/cc/backend/codegen.c index e223b70f0..1cecdd891 100644 --- a/src/cc/codegen.c +++ b/src/cc/backend/codegen.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "codegen.h" #include diff --git a/src/cc/codegen.h b/src/cc/backend/codegen.h similarity index 100% rename from src/cc/codegen.h rename to src/cc/backend/codegen.h diff --git a/src/cc/codegen_expr.c b/src/cc/backend/codegen_expr.c similarity index 99% rename from src/cc/codegen_expr.c rename to src/cc/backend/codegen_expr.c index 24b034083..40a916dc1 100644 --- a/src/cc/codegen_expr.c +++ b/src/cc/backend/codegen_expr.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "codegen.h" #include diff --git a/src/cc/emit_util.c b/src/cc/backend/emit_util.c similarity index 99% rename from src/cc/emit_util.c rename to src/cc/backend/emit_util.c index ee4145e47..388fab16e 100644 --- a/src/cc/emit_util.c +++ b/src/cc/backend/emit_util.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "emit_util.h" #include diff --git a/src/cc/emit_util.h b/src/cc/backend/emit_util.h similarity index 100% rename from src/cc/emit_util.h rename to src/cc/backend/emit_util.h diff --git a/src/cc/ir.c b/src/cc/backend/ir.c similarity index 99% rename from src/cc/ir.c rename to src/cc/backend/ir.c index c78a98fdd..a72bc02a2 100644 --- a/src/cc/ir.c +++ b/src/cc/backend/ir.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "ir.h" #include diff --git a/src/cc/ir.h b/src/cc/backend/ir.h similarity index 100% rename from src/cc/ir.h rename to src/cc/backend/ir.h diff --git a/src/cc/regalloc.c b/src/cc/backend/regalloc.c similarity index 99% rename from src/cc/regalloc.c rename to src/cc/backend/regalloc.c index 64cdde287..55fabc2ad 100644 --- a/src/cc/regalloc.c +++ b/src/cc/backend/regalloc.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "regalloc.h" #include diff --git a/src/cc/regalloc.h b/src/cc/backend/regalloc.h similarity index 100% rename from src/cc/regalloc.h rename to src/cc/backend/regalloc.h diff --git a/src/cc/ast.c b/src/cc/frontend/ast.c similarity index 99% rename from src/cc/ast.c rename to src/cc/frontend/ast.c index 0471f3ac6..9c11f161b 100644 --- a/src/cc/ast.c +++ b/src/cc/frontend/ast.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "ast.h" #include diff --git a/src/cc/ast.h b/src/cc/frontend/ast.h similarity index 100% rename from src/cc/ast.h rename to src/cc/frontend/ast.h diff --git a/src/cc/lexer.c b/src/cc/frontend/lexer.c similarity index 99% rename from src/cc/lexer.c rename to src/cc/frontend/lexer.c index 916cc80e6..ed14389b1 100644 --- a/src/cc/lexer.c +++ b/src/cc/frontend/lexer.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "lexer.h" #include diff --git a/src/cc/lexer.h b/src/cc/frontend/lexer.h similarity index 100% rename from src/cc/lexer.h rename to src/cc/frontend/lexer.h diff --git a/src/cc/parser.c b/src/cc/frontend/parser.c similarity index 99% rename from src/cc/parser.c rename to src/cc/frontend/parser.c index 277dc2110..2e31b9378 100644 --- a/src/cc/parser.c +++ b/src/cc/frontend/parser.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "parser.h" #include diff --git a/src/cc/parser.h b/src/cc/frontend/parser.h similarity index 100% rename from src/cc/parser.h rename to src/cc/frontend/parser.h diff --git a/src/cc/parser_expr.c b/src/cc/frontend/parser_expr.c similarity index 99% rename from src/cc/parser_expr.c rename to src/cc/frontend/parser_expr.c index 35e4c5f9a..2f7b31fda 100644 --- a/src/cc/parser_expr.c +++ b/src/cc/frontend/parser_expr.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "parser.h" #include diff --git a/src/cc/type.c b/src/cc/frontend/type.c similarity index 99% rename from src/cc/type.c rename to src/cc/frontend/type.c index 3eafba212..356420e6f 100644 --- a/src/cc/type.c +++ b/src/cc/frontend/type.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "type.h" #include diff --git a/src/cc/type.h b/src/cc/frontend/type.h similarity index 100% rename from src/cc/type.h rename to src/cc/frontend/type.h diff --git a/src/cc/var.c b/src/cc/frontend/var.c similarity index 99% rename from src/cc/var.c rename to src/cc/frontend/var.c index dbbd6f8a5..9d1c7de07 100644 --- a/src/cc/var.c +++ b/src/cc/frontend/var.c @@ -1,4 +1,4 @@ -#include "../config.h" +#include "../../config.h" #include "var.h" #include diff --git a/src/cc/var.h b/src/cc/frontend/var.h similarity index 100% rename from src/cc/var.h rename to src/cc/frontend/var.h diff --git a/tests/Makefile b/tests/Makefile index 1d096a47b..34b3a465e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,13 +1,16 @@ ROOT_DIR:=.. SRC_DIR:=$(ROOT_DIR)/src -CC_DIR:=$(SRC_DIR)/cc +CC1_DIR:=$(SRC_DIR)/cc UTIL_DIR:=$(SRC_DIR)/util DEBUG_DIR:=$(SRC_DIR)/_debug +CC1_FE_DIR:=$(CC1_DIR)/frontend +CC1_BE_DIR:=$(CC1_DIR)/backend + CFLAGS:=-ansi -std=c11 -Wall -Wextra -Werror \ -Wold-style-definition -Wno-missing-field-initializers -Wno-typedef-redefinition \ -Wno-empty-body -CFLAGS+=-I$(CC_DIR) -I$(UTIL_DIR) +CFLAGS+=-I$(CC1_FE_DIR) -I$(UTIL_DIR) CFLAGS+=-D_POSIX_C_SOURCE=200809L # for getline PREFIX:= @@ -99,8 +102,8 @@ test-link: link_test # $(XCC) @echo '## Link test' @./link_test -INITIALIZER_SRCS:=initializer_test.c $(CC_DIR)/parser.c $(CC_DIR)/parser_expr.c $(CC_DIR)/lexer.c \ - $(CC_DIR)/var.c $(CC_DIR)/type.c $(CC_DIR)/ast.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c \ +INITIALIZER_SRCS:=initializer_test.c $(CC1_FE_DIR)/parser.c $(CC1_FE_DIR)/parser_expr.c $(CC1_FE_DIR)/lexer.c \ + $(CC1_FE_DIR)/var.c $(CC1_FE_DIR)/type.c $(CC1_FE_DIR)/ast.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c \ $(DEBUG_DIR)/dump_expr.c initializer_test: $(INITIALIZER_SRCS) $(CC) -o$@ -DNO_MAIN_DUMP_EXPR $(CFLAGS) $^ @@ -113,8 +116,8 @@ UTIL_SRCS:=util_test.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c util_test: $(UTIL_SRCS) $(CC) -o$@ $(CFLAGS) $^ -PARSER_SRCS:=parser_test.c $(CC_DIR)/parser_expr.c $(CC_DIR)/lexer.c $(CC_DIR)/parser.c \ - $(CC_DIR)/type.c $(CC_DIR)/ast.c $(CC_DIR)/var.c \ +PARSER_SRCS:=parser_test.c $(CC1_FE_DIR)/parser_expr.c $(CC1_FE_DIR)/lexer.c $(CC1_FE_DIR)/parser.c \ + $(CC1_FE_DIR)/type.c $(CC1_FE_DIR)/ast.c $(CC1_FE_DIR)/var.c \ $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c parser_test: $(PARSER_SRCS) $(CC) -o$@ $(CFLAGS) $^ @@ -129,7 +132,7 @@ dvaltest: $(FVAL_SRCS) flotest.inc # $(XCC) fvaltest: $(FVAL_SRCS) flotest.inc # $(XCC) $(XCC) -o$@ -Werror -DUSE_SINGLE $(FVAL_SRCS) -TYPE_SRCS:=print_type_test.c $(CC_DIR)/type.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c +TYPE_SRCS:=print_type_test.c $(CC1_FE_DIR)/type.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c print_type_test: $(TYPE_SRCS) $(CC) -o $@ $(CFLAGS) $^