]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added codegen for imports
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 5 Dec 2022 02:39:04 +0000 (21:39 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 5 Dec 2022 02:39:04 +0000 (21:39 -0500)
cerise/backend/ssa/codegen.c
cerise/build.sh
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/tests/A.m
cerise/tests/Module.m

index 5f414cc94601ecc2b2ed33d512de3cf7eac65d3c..66ae53a989cb816e363809785be24e964fb5420d 100644 (file)
@@ -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
index 250a46751897524c3a36fb2456dbe5f2a6f888bc..d66bb4019c19c9234c4d9ba0361b1d8f2712f3a8 100755 (executable)
@@ -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
 
index d4cfc780810bcb43430afdc5f1f2eea55db48c54..14fae8817f6f3d85177d3b0766a776387da7529f 100644 (file)
@@ -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);
+
index df41d194d3b0546f39516c444bcff20bdffa83ef..d213473eccfbfeb96d36803d40626d7ab4ec4a39 100644 (file)
@@ -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);
 }
index 19283985596bf388fbee00dc446eb4becd7d0690..b1b8685c3f4cf3f23f8b49172eacfca1e0bee090 100644 (file)
@@ -1,7 +1,7 @@
 const
   FOO* = 42
 
-procedure Foo*()
+procedure Foo*() : Int
 forward
 
 procedure Foo*() : Int
index 31c546616ce8aa1108992a3c8c6e0118000422da..7dca4f887f5e3b599452f3ca924a0b615d7ed7f6 100644 (file)
@@ -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