From: Michael D. Lowis Date: Fri, 9 Dec 2022 02:27:54 +0000 (-0500) Subject: added linking X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=b36473cb038be58eb867d07edb39dcb0b381aaf1;p=proto%2Fobnc.git added linking --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index b76b441..ab73c04 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -541,6 +541,7 @@ void codegen_main(Parser* p) { fout(p, " call void @%s()\n", p->mods[i]); } + fout(p, " call void @%s()\n", p->name); fout(p, " ret i32 0\n"); fout(p, "}\n"); } \ No newline at end of file diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 5d6c3f9..ab3bab5 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -215,6 +215,11 @@ typedef struct Symbol { int defined : 1; } Symbol; +typedef struct { + char* name; + char* path; +} Module; + typedef struct { LexFile* done; LexFile* file; @@ -238,7 +243,7 @@ typedef struct { size_t mmods; size_t nmods; - char** mods; + Module* mods; bool genmain; } Parser; diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 00d1497..ef7abc5 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -788,7 +788,7 @@ static void compile_module(Parser* p, char* path, bool genmain) remove(p->outpath); /* compile the object file now */ - char* as_cmd = strmcat("cc -c -o \"", obj_path, "\" \"", asm_path, "\"", 0); + char* as_cmd = strmcat("clang -c -o \"", obj_path, "\" \"", asm_path, "\"", 0); printf("%s\n", as_cmd); if (system(as_cmd) != 0) { @@ -798,12 +798,12 @@ static void compile_module(Parser* p, char* path, bool genmain) } } -void add_module(Parser* p, char* modname) +void add_module(Parser* p, char* modname, char* modpath) { bool found = false; for (size_t i = 0; !found && i < p->nmods; i++) { - found = !strcmp(p->mods[i], modname); + found = !strcmp(p->mods[i].name, modname); } if (!found) @@ -811,9 +811,11 @@ void add_module(Parser* p, char* modname) if (p->nmods == p->mmods) { p->mmods = (p->mmods ? (p->mmods << 1) : 8u); - p->mods = realloc(p->mods, p->mmods * sizeof(char*)); + p->mods = realloc(p->mods, p->mmods * sizeof(Module)); } - p->mods[p->nmods++] = modname; + p->mods[p->nmods].name = modname; + p->mods[p->nmods].path = modpath; + p->nmods++; } } @@ -834,7 +836,7 @@ static void import(Parser* curr, char* modname, char* alias) /* copy module references over */ for (size_t i = 0; i < p.nmods; i++) { - add_module(curr, p.mods[i]); + add_module(curr, p.mods[i].name, p.mods[i].path); } /* copy exported symbols to our symbol table */ @@ -853,7 +855,10 @@ static void import(Parser* curr, char* modname, char* alias) } else if (p.syms[i].class == SYM_MODULE) { - add_module(curr, p.syms[i].name); + add_module( + curr, + p.syms[i].name, + fs_modfind(curr, p.syms[i].name)); } } } @@ -862,4 +867,19 @@ void compile(char* path) { Parser p = {0}; compile_module(&p, path, true); + char* link_cmd = strmcat("clang -o ", p.name, " ", 0); + for (size_t i = 0; i < p.nmods; i++) + { + char* object = strdup(p.mods[i].path); + object[strlen(object)-1] = 'o'; + link_cmd = strmcat(link_cmd, "\"", object, "\" ", 0); + } + char* object = strdup(p.outpath); + object[strlen(object)-1] = 'o'; + link_cmd = strmcat(link_cmd, "\"", object, "\" ", "-lm", 0); + puts(link_cmd); + if (system(link_cmd) != 0) + { + fatal("linker failed"); + } }