diff --git a/.gitignore b/.gitignore index 4c441b5..d0196b7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,10 @@ out out.asm output.txt -/target +/target1 +/target2 30cc +30cc_30cc 30cc_gcc # Prerequisites diff --git a/Makefile b/Makefile index 1bbe591..8fdc30d 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,43 @@ .PHONY=run -CC ?= cc -CFLAGS := -std=gnu99 -Og -ggdb +C_FILES := $(filter-out $(wildcard examples/*), $(shell find . -type f -name '*.c' -not -path './examples/*')) +H_FILES := $(filter-out $(wildcard examples/*), $(shell find . -type f -name '*.h' -not -path './examples/*')) -BIN := a.out +# Compile 30cc with gcc +STAGE1_CC ?= cc +STAGE1_CFLAGS := -std=gnu99 -Og -ggdb +STAGE1_BIN := ./30cc_gcc -$(BIN): *.h *.c parser/*.c parser/*.h parser/expr/*.c parser/expr/*.h codegen/*.c codegen/*.h preprocess/*.c preprocess/*.h - $(CC) $(CFLAGS) *.c parser/*.c codegen/*.c parser/expr/*.c preprocess/*.c +# Compile 30cc with gcc-generated 30cc +STAGE2_CC := ./30cc_gcc +STAGE2_BIN := ./30cc_30cc + +# Compile 30cc with 30cc-generated 30cc +STAGE3_CC := ./30cc_30cc +STAGE3_BIN := ./30cc + +$(STAGE1_BIN): $(H_FILES) $(C_FILES) + $(STAGE1_CC) $(STAGE1_CFLAGS) $(C_FILES) -o $(STAGE1_BIN) + +$(STAGE2_BIN): $(STAGE1_BIN) **/*.h **/*.c + rm -rf target1 + for file in $(C_FILES) ; do \ + mkdir -p target1/$$(dirname $${file}); \ + $(STAGE2_CC) $${file} --asm > target1/$${file}.asm; \ + nasm -f elf64 target1/$${file}.asm -o target1/$${file}.o; \ + done + ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o $(STAGE2_BIN) $$(find target1 -type f -name '*.o') + rm -rf target1 + +$(STAGE3_BIN): $(STAGE2_BIN) **/*.h **/*.c + rm -rf target2 + for file in $(C_FILES) ; do \ + mkdir -p target2/$$(dirname $${file}); \ + $(STAGE2_CC) $${file} --asm > target2/$${file}.asm; \ + nasm -f elf64 target2/$${file}.asm -o target2/$${file}.o; \ + done + ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o $(STAGE3_BIN) $$(find target2 -type f -name '*.o') + rm -rf target2 run: $(BIN) @./a.out $(program) --asm > out.asm diff --git a/README.md b/README.md index 15cbf67..c959ebf 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ - You'll first need to bootstrap the compiler by running `make`. This will compile the 30cc compiler and store its binary in `a.out`. - Then run `./build.py`. This will use the bootstrapped 30cc-compiler to compile 30cc itself. It then again uses the 30cc-compiled compiler to compile 30cc once again. The final compiler is then stored as `./30cc`. - In the end, you will have 3 binary files which should all behave the same: - 1. `a.out` which is the bootstrapped gcc-compiled version of 30cc - 2. `30cc_gcc` which is the output of gcc-compiled 30cc compiler, compiling the 30cc compiler + 1. `30cc_gcc` which is the bootstrapped gcc-compiled version of 30cc + 2. `30cc_30cc` which is the output of gcc-compiled 30cc compiler, compiling the 30cc compiler 3. `30cc` which is the output of 30cc-compiled 30cc compiler, compiling the 30cc compiler Running independent source-files through `make`: