From ff80ebc85fe768848153f53463eec19290fb38a6 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 30 Aug 2023 23:12:34 -0400 Subject: [PATCH] eliminated the stdlib --- cerise/.gitignore | 3 ++- cerise/build.sh | 2 -- cerise/inc/cerise.h | 1 + cerise/runtime/_start.c | 12 ++++++++++++ cerise/src/grammar.c | 17 ++++++++++------- cerise/tests/A.m | 2 +- cerise/tests/Module.m | 6 +++--- 7 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 cerise/runtime/_start.c diff --git a/cerise/.gitignore b/cerise/.gitignore index 4396af6..924a66b 100644 --- a/cerise/.gitignore +++ b/cerise/.gitignore @@ -1,3 +1,4 @@ *.l *.a -*.o \ No newline at end of file +*.o +Module diff --git a/cerise/build.sh b/cerise/build.sh index eb6c105..c845045 100755 --- a/cerise/build.sh +++ b/cerise/build.sh @@ -17,5 +17,3 @@ ar -rc libcerise.a runtime/*.o $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \ && ./cerisec tests/Module.m \ -rm -f Module - diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index f1d62fd..2f18c43 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -243,6 +243,7 @@ typedef struct { Module* mods; bool genmain; + bool dirty; } Parser; // src/stdlib.c diff --git a/cerise/runtime/_start.c b/cerise/runtime/_start.c new file mode 100644 index 0000000..41a279c --- /dev/null +++ b/cerise/runtime/_start.c @@ -0,0 +1,12 @@ +extern int main(); + +__asm ( + ".global _start\n" + "_start:\n" + " movl (%rsp), %edi\n" + " lea 8(%rsp), %rsi\n" + " call main\n" + " movl %eax, %edi\n" + " movl $60, %eax\n" + " syscall\n" +); diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index fde350f..cf993b0 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -768,9 +768,9 @@ static void init_parser(Parser* p, char* path, bool genmain) symbol_new(p, 0, "String", SYM_TYPE, 0)->type = &StringType; } -static void compile_module(Parser* p, char* path, bool genmain) +static bool compile_module(Parser* p, char* path, bool genmain) { - /* parser the file and generate the code */ + /* parse the file and generate the code */ init_parser(p, path, genmain); codegen_init(p); module(p); @@ -778,7 +778,8 @@ static void compile_module(Parser* p, char* path, bool genmain) char* asm_path = get_fpath(path, 's'); char* obj_path = get_fpath(path, 'o'); - if (fs_modtime(obj_path) < fs_modtime(path)) + bool dirty = fs_modtime(obj_path) < fs_modtime(path); + if (p->dirty || dirty) { /* run llc to generate assembly listing */ char* llc_cmd = strmcat("llc -opaque-pointers -o \"", asm_path , "\" \"", p->outpath, "\"", 0); @@ -790,7 +791,7 @@ static void compile_module(Parser* p, char* path, bool genmain) // remove(p->outpath); /* compile the object file now */ - char* as_cmd = strmcat("clang -static -c -o \"", obj_path, "\" \"", asm_path, "\"", 0); + char* as_cmd = strmcat("clang -nostdlib -flto -static -O2 -c -o \"", obj_path, "\" \"", asm_path, "\"", 0); printf("%s\n", as_cmd); if (system(as_cmd) != 0) { @@ -798,6 +799,7 @@ static void compile_module(Parser* p, char* path, bool genmain) } remove(asm_path); } + return dirty; } void add_module(Parser* p, char* modname, char* modpath) @@ -832,7 +834,7 @@ static void import(Parser* curr, char* modname, char* alias) /* parse the module */ Parser p = {0}; - compile_module(&p, path, false); + curr->dirty = compile_module(&p, path, false) || curr->dirty; (void)alias; /* copy module references over */ @@ -869,9 +871,10 @@ void compile(char* path) { Parser p = {0}; compile_module(&p, path, true); - char* link_cmd = strmcat("clang -static -g -o ", p.name, " ", 0); + char* link_cmd = strmcat("clang -O2 -g -nostdlib -static -flto -o ", p.name, " ", 0); for (size_t i = 0; i < p.nmods; i++) { + printf("%lu: %s\n", i, p.mods[i].path); if (!p.mods[i].path) continue; char* object = strdup(p.mods[i].path); object[strlen(object)-1] = 'o'; @@ -879,7 +882,7 @@ void compile(char* path) } char* object = strdup(p.outpath); object[strlen(object)-1] = 'o'; - link_cmd = strmcat(link_cmd, "\"", object, "\" ", "-L. ", "-lm ", "-lcerise", 0); + link_cmd = strmcat(link_cmd, "\"", object, "\" ", "-L. ", " ", "-lcerise", 0); puts(link_cmd); if (system(link_cmd) != 0) { diff --git a/cerise/tests/A.m b/cerise/tests/A.m index bc9076d..439a852 100644 --- a/cerise/tests/A.m +++ b/cerise/tests/A.m @@ -23,5 +23,5 @@ begin end begin - Baz("abc", "def"); +# Baz("abc", "def"); end \ No newline at end of file diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index a70872d..185f4fb 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -121,9 +121,9 @@ begin vReal = 1.0 / vReal; vReal = vReal / vReal; - vReal = vReal % 1.0; - vReal = 1.0 % vReal; - vReal = vReal % vReal; +# vReal = vReal % 1.0; +# vReal = 1.0 % vReal; +# vReal = vReal % vReal; end procedure TestRealCompOps() -- 2.49.0