From: Michael D. Lowis Date: Mon, 5 Dec 2022 02:39:04 +0000 (-0500) Subject: added codegen for imports X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=32537ee210187939d05f700c7c9eca8ce97d815d;p=proto%2Fobnc.git added codegen for imports --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 5f414cc..66ae53a 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -149,9 +149,18 @@ void codegen_symbol(Parser* p, Symbol* sym) } else if (sym->class == SYM_PROC) { - printf("define "); + char* modname = (sym->module == 0 + ? p->name : symbol_getbyid(p, sym->module)->name); + if (sym->forward) + { + printf("declare "); + } + else + { + printf("define "); + } emit_type(sym->type->base); - printf(" @%s(", sym->name); + printf(" @%s_%s(", modname, sym->name); for (Field* f = sym->type->fields; f; f = f->next) { emit_type(f->type); @@ -163,6 +172,19 @@ void codegen_symbol(Parser* p, Symbol* sym) } printf(")\n"); } + else if (sym->class == SYM_MODULE) + { + if (sym->module == 0) + { + printf("define "); + } + else + { + printf("declare "); + } + printf(" void @%s(", sym->name); + printf(")\n"); + } } static void topsort(Bitset* set, SsaBlock** sorted, SsaBlock* block) @@ -521,3 +543,18 @@ void codegen_block(Parser* p, SsaBlock* block) printf("}\n"); puts(""); } + +void codegen_main(Parser* p) +{ + if (!p->codegen) return; // Bail if just importing symbols + printf("define i32 @main(i32 %%0, i8* %%1)\n{\n"); + for (size_t i = 0; i < p->nsyms; i++) + { + if (p->syms[i].class == SYM_MODULE) + { + printf(" call void @%s()\n", p->syms[i].name); + } + } + printf(" ret i32 0\n"); + printf("}\n"); +} \ No newline at end of file diff --git a/cerise/build.sh b/cerise/build.sh index 250a467..d66bb40 100755 --- a/cerise/build.sh +++ b/cerise/build.sh @@ -8,12 +8,9 @@ ctags -R & grep -rh TODO src/*.* backend/*/*.* | sed -E -e 's/ *\/\/ *//' -e 's/ *\/\* *(.*) *\*\//\1/' # Now build and test it - $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \ -&& ./cerisec tests/Module.m -#&& $CCCMD -D CERISE_TESTS -o cerisec-test src/*.c backend/test/*.c \ -#&& ./cerisec-test \ -#&& (./cerisec tests/Module.m | tee test.l) \ -#&& llc test.l +$CCCMD -o cerisec src/*.c "backend/ssa"/*.c \ +&& (./cerisec tests/Module.m | tee test.l) \ +&& llc test.l rm -f Module diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index d4cfc78..14fae88 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -212,6 +212,7 @@ typedef struct Symbol { int export : 1; int global : 1; int forward : 1; + int defined : 1; } Symbol; typedef struct { @@ -335,3 +336,5 @@ extern Type VoidType, BoolType, IntType, RealType, StringType; void codegen_init(Parser* p); void codegen_symbol(Parser* p, Symbol* sym); void codegen_block(Parser* p, SsaBlock* block); +void codegen_main(Parser* p); + diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index df41d19..d213473 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -66,9 +66,7 @@ static SsaNode* qualident(Parser* p) { expect(p, '.'); char* name = expect_text(p, IDENT); - size_t qsymid = symbol_getid(p, symid, name, -1); - printf("%ld.%ld\n", symid, qsymid); - symid=qsymid; + symid = symbol_getid(p, symid, name, -1); } /* make the identifier with the final index */ @@ -568,8 +566,8 @@ Symbol* proc_type(Parser* p) if (proc->forward) { - // check the type here - proc->forward = 0; + // TODO: check the type here + proc->defined = 1; } codegen_symbol(p, proc); return proc; @@ -620,11 +618,11 @@ void proc_decl(Parser* p) var_decl(p); } - if (accept(p, FORWARD)) + if (accept(p, FORWARD) || accept(p, EXTERN)) { proc->forward = 1; } - else if (!accept(p, EXTERN)) + else // if (!accept(p, EXTERN)) { proc_body(p, proc->type); } @@ -684,7 +682,7 @@ static void module(Parser* p) } /* now let's define the module init function */ - Symbol* sym = symbol_new(p, 0, p->name, SYM_PROC, 0); + Symbol* sym = symbol_new(p, 0, p->name, SYM_MODULE, 0); Type* proctype = symbol_newtype(p); proctype->form = FORM_PROC; proctype->base = &VoidType; @@ -700,16 +698,23 @@ static void module(Parser* p) if (!matches(p, END)) { block->links[0] = statement_seq(p); + ssa_return(p, NULL); } else { -// block->links[0] = ssa_block(p); -// ssa_return(p, NULL); + block->links[0] = ssa_block(p); + block->links[0]->links[0] = p->curr_join; + p->curr_block = block->links[0]; + ssa_return(p, NULL); } expect(p, END); } else { + block->links[0] = ssa_block(p); + block->links[0]->links[0] = p->curr_join; + p->curr_block = block->links[0]; + ssa_return(p, NULL); } ssa_join(p); codegen_block(p, block); @@ -718,6 +723,8 @@ static void module(Parser* p) { error(p, "expected end of file"); } + codegen_main(p); + EXIT_RULE(); } @@ -762,8 +769,9 @@ static void import(Parser* curr, char* modname, char* alias) /* copy exported symbols to our symbol table */ Symbol* sym = symbol_new(curr, 0, modname, SYM_MODULE, 0); + sym->forward = 1; + codegen_symbol(curr, sym); size_t modid = sym - &curr->syms[0]; - printf("%lu\n", modid); for (size_t i = 0; i < p.nsyms; i++) { if (p.syms[i].export) @@ -771,6 +779,8 @@ static void import(Parser* curr, char* modname, char* alias) Symbol* sym = symbol_new(curr, 0, p.syms[i].name, p.syms[i].class, 0); *sym = p.syms[i]; sym->module = modid; + sym->forward = 1; + codegen_symbol(curr, sym); } } } @@ -779,6 +789,6 @@ void compile(char* path) { Parser p = {0}; init_parser(&p, path); -// codegen_init(&p); + codegen_init(&p); module(&p); } diff --git a/cerise/tests/A.m b/cerise/tests/A.m index 1928398..b1b8685 100644 --- a/cerise/tests/A.m +++ b/cerise/tests/A.m @@ -1,7 +1,7 @@ const FOO* = 42 -procedure Foo*() +procedure Foo*() : Int forward procedure Foo*() : Int diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 31c5466..7dca4f8 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -1,8 +1,8 @@ import A -#const -# FOO* = 42 +const + FOO* = 42 type FooRec* = record @@ -199,6 +199,6 @@ begin end begin - TestReturnVoid(); - A.Foo(); +# TestReturnVoid(); +# A.Foo(); end