compiler

Unnamed Compiled Systems Language Project
git clone http://frotz.net/git/compiler.git
Log | Files | Refs

commit 6c0d662cbed9802c4cecf322b3572db3ed833e64
parent c034c869e4a912a40c969b04d193de50a16b51c8
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 12 Dec 2021 08:08:50 -0800

some project level cleanup

- bin/compiler is now bin/cold (compiler old)
- bin/compiler2 is now bin/cast (compiler ast based)
- bin/cir will be the ast->ir "compiler3"
- update runtest scripts to reflect this
- update compiler2 source to allow selecting the backend (ast->risc5 / ast->ir)

Diffstat:
MMakefile | 18+++++++++++-------
Msrc/codegen-risc5-simple.c | 76+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/compiler2.c | 82+++++--------------------------------------------------------------------------
Mtest/runtest.sh | 2+-
Mtest/runtest2.sh | 4++--
5 files changed, 94 insertions(+), 88 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ -all: bin/compiler bin/compiler2 bin/fs bin/r5d bin/r5e bin/mkinstab +all: bin/cold bin/cast bin/fs bin/r5d bin/r5e bin/mkinstab runtests: out/test/summary.txt @@ -20,14 +20,18 @@ CFLAGS += -Wno-unused-but-set-variable -Wno-unused-variable CC := gcc -bin/compiler: src/compiler.c src/risc5dis.c out/risc5ins.h +bin/cold: src/compiler.c src/risc5dis.c out/risc5ins.h @mkdir -p bin $(CC) -o $@ $(CFLAGS) -Wno-unused-result -DC src/compiler.c src/risc5dis.c -bin/compiler2: src/compiler2.c src/codegen-risc5-simple.c src/risc5dis.c out/risc5ins.h +bin/cast: src/compiler2.c src/codegen-risc5-simple.c src/risc5dis.c out/risc5ins.h @mkdir -p bin $(CC) -o $@ $(CFLAGS) -Wno-unused-result -DC src/compiler2.c src/risc5dis.c +bin/cir: src/compiler2.c src/codegen-ir.c src/risc5dis.c out/risc5ins.h + @mkdir -p bin + $(CC) -o $@ $(CFLAGS) -Wno-unused-result -DIR -DC src/compiler2.c src/risc5dis.c + bin/rewriter: src/rewriter.c @mkdir -p bin $(CC) -o $@ $(CFLAGS) src/rewriter.c @@ -64,22 +68,22 @@ out/risc5ins.h: src/risc5ins.txt bin/mkinstab # fail to be compiled by the rule that depends on src+log *or* # we fail to depend on the .log for tests with both... -out/test/%.txt: test/%.src test/%.log bin/compiler bin/r5e test/runtest.sh +out/test/%.txt: test/%.src test/%.log bin/cold bin/r5e test/runtest.sh @mkdir -p out/test @rm -f $@ @test/runtest.sh $< $@ -out/test/%.txt: test/%.src bin/compiler bin/r5e test/runtest.sh +out/test/%.txt: test/%.src bin/cold bin/r5e test/runtest.sh @mkdir -p out/test @rm -f $@ @test/runtest.sh $< $@ -out/test2/%.txt: test/%.src test/%.log bin/compiler2 bin/r5e test/runtest2.sh +out/test2/%.txt: test/%.src test/%.log bin/cast bin/r5e test/runtest2.sh @mkdir -p out/test2 @rm -f $@ @test/runtest2.sh $< $@ -out/test2/%.txt: test/%.src bin/compiler2 bin/r5e test/runtest2.sh +out/test2/%.txt: test/%.src bin/cast bin/r5e test/runtest2.sh @mkdir -p out/test2 @rm -f $@ @test/runtest2.sh $< $@ diff --git a/src/codegen-risc5-simple.c b/src/codegen-risc5-simple.c @@ -3,6 +3,8 @@ // ------------------------------------------------------------------ +#include "risc5.h" + // R0 is used for returns // R11 is for instruction combo temporary // FP, SB, SP, LR have fixed uses @@ -709,7 +711,7 @@ void gen_func(Ast node) { emit_br(AL, LR); } -void gen_risc5_simple(Ast node) { +void gen_program(Ast node) { gen_trace( "gen_risc5_simple()\n"); emit_movi(SB, 0); // placeholder SB load @@ -744,3 +746,75 @@ void gen_risc5_simple(Ast node) { // TODO: copy ro globals after code // TODO: SB should neg-index into ro, pos-index into rw } + +// ================================================================ + +void binary_write(const char* outname) { + int fd = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0644); + if (fd < 0) { + error("cannot open '%s' to write", outname); + } + u32 n = 0; + while (n < ctx.pc) { + if (write(fd, ctx.code + (n/4), sizeof(u32)) != sizeof(u32)) { + error("error writing '%s'", outname); + } + n += 4; + } + n = 0; + while (n < ctx.gp) { + if (write(fd, ctx.data + (n/4), sizeof(u32)) != sizeof(u32)) { + error("error writing '%s'", outname); + } + n += 4; + } + close(fd); +} + +void listing_write(const char* listfn, const char* srcfn) { + FILE* fin = fopen(srcfn, "r"); + if (fin == NULL) { + error("cannot re-read '%s'\n", srcfn); + } + FILE* fout = fopen(listfn, "w"); + if (fout == NULL) { + error("cannot write '%s'\n", listfn); + } + u32 n = 0; + u32 line = 1; + char buf[1024]; + while (n < ctx.pc) { + u32 ins = ctx.code[n/4]; + if ((line < ctx.xref[n/4]) && fin) { + fprintf(fout, "\n"); + while (line < ctx.xref[n/4]) { + if (fgets(buf, sizeof(buf), fin) == nil) { + fin = nil; + break; + } + u32 i = 0; + while (buf[i] != 0) { + if (buf[i] > ' ') { + fprintf(fout,"%s", buf); + break; + } + i++; + } + line++; + } + fprintf(fout, "\n"); + } + risc5dis(n, ins, buf); + fprintf(fout, "%08x: %08x %s\n", n, ins, buf); + n += 4; + } + n = 0; + while (n < ctx.gp) { + fprintf(fout, "%08x: %08x\n", ctx.pc + n, ctx.data[n >> 2]); + n += 4; + } + fclose(fout); + if (fin) { + fclose(fin); + } +} diff --git a/src/compiler2.c b/src/compiler2.c @@ -14,8 +14,6 @@ #include <unistd.h> #include <sys/stat.h> -#include "risc5.h" - // builtin types #define nil 0 typedef uint32_t u32; @@ -2170,7 +2168,11 @@ void ast_dump_graphs(Ast node) { } } +#if IR +#include "codegen-ir.c" +#else #include "codegen-risc5-simple.c" +#endif #if 0 void type_dump_all() { @@ -2190,80 +2192,6 @@ void type_dump_all() { // ================================================================ -void binary_write(const char* outname) { - int fd = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0644); - if (fd < 0) { - error("cannot open '%s' to write", outname); - } - u32 n = 0; - while (n < ctx.pc) { - if (write(fd, ctx.code + (n/4), sizeof(u32)) != sizeof(u32)) { - error("error writing '%s'", outname); - } - n += 4; - } - n = 0; - while (n < ctx.gp) { - if (write(fd, ctx.data + (n/4), sizeof(u32)) != sizeof(u32)) { - error("error writing '%s'", outname); - } - n += 4; - } - close(fd); -} - -void listing_write(const char* listfn, const char* srcfn) { - FILE* fin = fopen(srcfn, "r"); - if (fin == NULL) { - error("cannot re-read '%s'\n", srcfn); - } - FILE* fout = fopen(listfn, "w"); - if (fout == NULL) { - error("cannot write '%s'\n", listfn); - } - u32 n = 0; - u32 line = 1; - char buf[1024]; - while (n < ctx.pc) { - u32 ins = ctx.code[n/4]; -#if 1 - if ((line < ctx.xref[n/4]) && fin) { - fprintf(fout, "\n"); - while (line < ctx.xref[n/4]) { - if (fgets(buf, sizeof(buf), fin) == nil) { - fin = nil; - break; - } - u32 i = 0; - while (buf[i] != 0) { - if (buf[i] > ' ') { - fprintf(fout,"%s", buf); - break; - } - i++; - } - line++; - } - fprintf(fout, "\n"); - } -#endif - risc5dis(n, ins, buf); - fprintf(fout, "%08x: %08x %s\n", n, ins, buf); - n += 4; - } - n = 0; - while (n < ctx.gp) { - fprintf(fout, "%08x: %08x\n", ctx.pc + n, ctx.data[n >> 2]); - n += 4; - } - fclose(fout); - if (fin) { - fclose(fin); - } -} - -// ================================================================ - i32 main(int argc, args argv) { str outname = "out.bin"; str lstname = nil; @@ -2368,7 +2296,7 @@ i32 main(int argc, args argv) { ast_dump_graphs(a); } - gen_risc5_simple(a); + gen_program(a); binary_write(outname); diff --git a/test/runtest.sh b/test/runtest.sh @@ -12,7 +12,7 @@ msg="${txt%.txt}.msg" gold="${src%.src}.log" #echo "RUNTEST: $src: compiling..." -if bin/compiler -o "$bin" -l "$lst" "$src" 2> "$msg"; then +if bin/cold -o "$bin" -l "$lst" "$src" 2> "$msg"; then # success! if [[ "$txt" == *"-err"* ]]; then # but this was an error test, so... diff --git a/test/runtest2.sh b/test/runtest2.sh @@ -13,8 +13,8 @@ msg="${txt%.txt}.msg" gold="${src%.src}.log" #echo "RUNTEST2: $src: compiling..." -#echo bin/compiler2 -o "$bin" -l "$lst" "$src" "$msg" -if bin/compiler2 -o "$bin" -a "$ast" -l "$lst" "$src" 2> "$msg"; then +#echo bin/cast -o "$bin" -l "$lst" "$src" "$msg" +if bin/cast -o "$bin" -a "$ast" -l "$lst" "$src" 2> "$msg"; then # success! if [[ "$txt" == *"-err"* ]]; then # but this was an error test, so...