Skip to content

Commit

Permalink
Separate frontend and backend sources
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Jul 31, 2023
1 parent 7b562a1 commit dbbb2ef
Show file tree
Hide file tree
Showing 23 changed files with 43 additions and 38 deletions.
39 changes: 20 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)))

Expand Down
3 changes: 2 additions & 1 deletion compile_flags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-I./src/as
-I./src/cc
-I./src/cc/backend
-I./src/cc/frontend
-I./src/util
2 changes: 1 addition & 1 deletion src/cc/codegen.c → src/cc/backend/codegen.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "codegen.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/codegen_expr.c → src/cc/backend/codegen_expr.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "codegen.h"

#include <assert.h>
Expand Down
2 changes: 1 addition & 1 deletion src/cc/emit_util.c → src/cc/backend/emit_util.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "emit_util.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/ir.c → src/cc/backend/ir.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "ir.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/regalloc.c → src/cc/backend/regalloc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "regalloc.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/ast.c → src/cc/frontend/ast.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "ast.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/lexer.c → src/cc/frontend/lexer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "lexer.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/parser.c → src/cc/frontend/parser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "parser.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/parser_expr.c → src/cc/frontend/parser_expr.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "parser.h"

#include <assert.h>
Expand Down
2 changes: 1 addition & 1 deletion src/cc/type.c → src/cc/frontend/type.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "type.h"

#include <assert.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cc/var.c → src/cc/frontend/var.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../config.h"
#include "../../config.h"
#include "var.h"

#include <assert.h>
Expand Down
File renamed without changes.
17 changes: 10 additions & 7 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -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:=
Expand Down Expand Up @@ -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) $^
Expand All @@ -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) $^
Expand All @@ -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) $^

Expand Down

0 comments on commit dbbb2ef

Please sign in to comment.