From: Michael D. Lowis Date: Wed, 7 Dec 2022 04:11:58 +0000 (-0500) Subject: compilation is working for the main module. Need to add compilation of imported modul... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a8e185eed49ab2e48e0e5d12731d0ea416101a38;p=proto%2Fobnc.git compilation is working for the main module. Need to add compilation of imported modules and a linking step --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 0c2e273..d234662 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -30,14 +30,12 @@ void emit_type(Parser* p, Type* type); static void fout(Parser* p, char* fmt, ...) { - (void)p, (void)fmt; va_list args; va_start(args, fmt); - vfprintf(stdout, fmt, args); + vfprintf(p->out, fmt, args); va_end(args); } - void declare_record_type(Parser* p, Type* type) { if (!type->name) diff --git a/cerise/build.sh b/cerise/build.sh index d66bb40..5f25435 100755 --- a/cerise/build.sh +++ b/cerise/build.sh @@ -9,8 +9,7 @@ grep -rh TODO src/*.* backend/*/*.* | sed -E -e 's/ *\/\/ *//' -e 's/ *\/\* *(.* # Now build and test it $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \ -&& (./cerisec tests/Module.m | tee test.l) \ -&& llc test.l +&& ./cerisec tests/Module.m \ rm -f Module diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 14fae88..2f69062 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -220,6 +220,8 @@ typedef struct { LexFile* file; Tok tok; char* name; + char* outpath; + FILE* out; long curr_reg; size_t scope; size_t blockid; @@ -240,12 +242,14 @@ typedef struct { // src/stdlib.c void fatal(char* estr); void* emalloc(size_t size); +char* strmcat(char* first, ...); // src/fs.c void fs_init(void); char* fs_read(char* path); char* fs_modname(char* path); char* fs_modfind(Parser* p, char* path); +bool fs_exists(char* path); // src/lex.c void lexfile(Parser* ctx, char* path); diff --git a/cerise/src/fs.c b/cerise/src/fs.c index 1c1d944..5c70c43 100644 --- a/cerise/src/fs.c +++ b/cerise/src/fs.c @@ -7,26 +7,6 @@ size_t Num_Paths = 0; char** Paths = NULL; -static char* strmcat(char* first, ...) { - va_list args; - /* calculate the length of the final string */ - size_t len = strlen(first); - va_start(args, first); - for (char* s = NULL; (s = va_arg(args, char*));) - len += strlen(s); - va_end(args); - /* allocate the final string and copy the args into it */ - char *str = malloc(len+1), *curr = str; - while (first && *first) *(curr++) = *(first++); - va_start(args, first); - for (char* s = NULL; (s = va_arg(args, char*));) - while (s && *s) *(curr++) = *(s++); - va_end(args); - /* null terminate and return */ - *curr = '\0'; - return str; -} - void fs_init(void) { /* get path variable */ @@ -104,7 +84,7 @@ static char* check_modpath(char* path, char* name) { char* retpath = NULL; char* full_path = strmcat(path, "/", name, ".m", 0); - if (access(full_path, F_OK) == 0) + if (fs_exists(full_path)) { retpath = full_path; } @@ -125,3 +105,9 @@ char* fs_modfind(Parser* p, char* name) } return path; } + +bool fs_exists(char* path) +{ + return (access(path, F_OK) == 0); +} + diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index d213473..3d08f08 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -728,6 +728,13 @@ static void module(Parser* p) EXIT_RULE(); } +static char* get_fpath(char* base, char ext) +{ + char* path = strdup(base); + path[strlen(path)-1] = ext; + return path; +} + static void init_parser(Parser* p, char* path) { char* fcontents = fs_read(path); @@ -737,6 +744,8 @@ static void init_parser(Parser* p, char* path) p->file->fbeg = fcontents; p->file->fpos = fcontents; p->curr_reg = 0; + p->outpath = get_fpath(path, 'l'); + p->out = fopen(p->outpath, "wb"); if (p->name == NULL) { fatal("could not determine module name from path"); @@ -791,4 +800,28 @@ void compile(char* path) init_parser(&p, path); codegen_init(&p); module(&p); + fclose(p.out); + + char* asm_path = get_fpath(path, 's'); +// char* c_path = get_fpath(path, 'c'); + char* obj_path = get_fpath(path, 'o'); + + /* run llc to generate assembly listing */ + char* llc_cmd = strmcat("llc -o \"", asm_path , "\" \"", p.outpath, "\"", 0); + printf("%s\n", llc_cmd); + if (system(llc_cmd) != 0) + { + fatal("assembler failed"); + } + remove(p.outpath); + + char* as_cmd = strmcat("cc -c -o \"", obj_path, "\" \"", asm_path, "\" ", +// (fs_exists(c_path) ? strmcat("\"", c_path, "\"", 0) : ""), + 0); + printf("%s\n", as_cmd); + if (system(as_cmd) != 0) + { + fatal("assembler failed"); + } + remove(asm_path); } diff --git a/cerise/src/stdlib.c b/cerise/src/stdlib.c index 95520ec..2dace09 100644 --- a/cerise/src/stdlib.c +++ b/cerise/src/stdlib.c @@ -12,3 +12,24 @@ void* emalloc(size_t size) if (!ptr) fatal("malloc()"); return ptr; } + +char* strmcat(char* first, ...) +{ + va_list args; + /* calculate the length of the final string */ + size_t len = strlen(first); + va_start(args, first); + for (char* s = NULL; (s = va_arg(args, char*));) + len += strlen(s); + va_end(args); + /* allocate the final string and copy the args into it */ + char *str = emalloc(len+1), *curr = str; + while (first && *first) *(curr++) = *(first++); + va_start(args, first); + for (char* s = NULL; (s = va_arg(args, char*));) + while (s && *s) *(curr++) = *(s++); + va_end(args); + /* null terminate and return */ + *curr = '\0'; + return str; +} diff --git a/cerise/tests/A.l b/cerise/tests/A.l new file mode 100644 index 0000000..e69de29 diff --git a/cerise/tests/Module.s b/cerise/tests/Module.s deleted file mode 100644 index 6c0af9f..0000000 --- a/cerise/tests/Module.s +++ /dev/null @@ -1,5 +0,0 @@ -a -b -c - -// c = ...