]> git.mdlowis.com Git - proto/obnc.git/commitdiff
add calls to module init functions
authormike lowis <mike@mdlowis.com>
Fri, 23 Apr 2021 02:00:35 +0000 (22:00 -0400)
committermike lowis <mike@mdlowis.com>
Fri, 23 Apr 2021 02:00:35 +0000 (22:00 -0400)
cerise/TODO.md [new file with mode: 0644]
cerise/cerise.h
cerise/codegen.c
cerise/parser.c
cerise/tests/Module.m
cerise/tests/Module.s

diff --git a/cerise/TODO.md b/cerise/TODO.md
new file mode 100644 (file)
index 0000000..a75a7b7
--- /dev/null
@@ -0,0 +1 @@
+* Optimize variable initialization to use constants
index b110dbc619db8dff517dec5ddeb78363e2cb5107..3d36eb47e6c19caf8490b78a368b59b7817f1b90 100644 (file)
@@ -107,6 +107,13 @@ typedef struct Symbol {
     ImmValue imm;
 } Symbol;
 
+typedef struct Module {
+    struct Module* next;
+    Symbol* symbols;
+    char* name;
+    char* alias;
+} Module;
+
 typedef struct {
     enum {
         ITEM_CONST, ITEM_VAR
@@ -120,6 +127,7 @@ typedef struct {
     LexFile* done;
     LexFile* file;
     Tok tok;
+    Module* imports;
     Symbol* scope;
 } Parser;
 
@@ -152,6 +160,7 @@ void codegen_setint(Item* item, Type* type, long long val);
 void codegen_setreal(Item* item, double val);
 void codegen_setstr(Item* item, char* val);
 
+void codegen_import(char* name);
 void codegen_var(char* name, Type* type);
 void codegen_main(char* modname);
 void codegen_startproc(char* name, long long localsz);
index 18f8c4fa8054afd54e673f052f146e56ddcfd3be..af270d420dd0ba6c7f400d2d4b9cd5b9b6178ccd 100644 (file)
@@ -186,29 +186,23 @@ static void const_unop(int op, Item* a)
     }
 }
 
+void codegen_import(char* name)
+{
+    printf("    call    %s\n", name);
+}
+
 void codegen_var(char* name, Type* type)
 {
-    if (type->form == FORM_BOOL || type->form == FORM_INT)
-    {
-        printf(".data\n");
-        printf("%s:\n", name);
-        printf("    .zero %d\n\n", type->size);
-    }
-    else if (type->form == FORM_REAL)
-    {
-    }
-    else
-    {
-        assert(!"not supported");
-    }
+    printf("    .data\n");
+    printf("%s:\n", name);
+    printf("    .zero %d\n\n", type->size);
 }
 
 void codegen_main(char* modname)
 {
     codegen_startproc("main", 0);
-//    op_l  (OP_CALL, modname);
-    printf("    call     %s\n", modname);
-    printf("    xor      %%rax, %%rax\n");
+    printf("    call    %s\n", modname);
+    printf("    xor     %%rax, %%rax\n");
     codegen_endproc();
 }
 
@@ -218,27 +212,22 @@ void codegen_startproc(char* name, long long localsz)
     printf("    .globl %s\n", name);
     printf("%s:\n", name);
 
-//    op_r  (OP_PUSH, RBP);
-    printf("    pushq    %%rbp\n");
+    printf("    pushq   %%rbp\n");
     if (localsz > 0)
     {
-//    op_rr (OP_MOVE, RSP, RBP);
-    printf("    movq     %%rsp, %%rbp\n");
-//    op_ri (OP_SUB, localsz, RSP);
-        printf("    sub      $%lld, %%rsp\n\n", localsz);
+        printf("    movq    %%rsp, %%rbp\n");
+        printf("    sub     $%lld, %%rsp\n\n", localsz);
     }
     else
     {
-//        op_rr (OP_MOVE, RSP, RBP);
-        printf("    movq     %%rsp, %%rbp\n\n");
+        printf("    movq    %%rsp, %%rbp\n\n");
     }
 }
 
 void codegen_endproc(void)
 {
-//    op_r  (OP_POP,  RBP);
-//    op_0  (OP_RET);
-    printf("\n    pop      %%rbp\n");
+    puts("");
+    printf("    pop     %%rbp\n");
     printf("    ret\n\n");
 }
 
@@ -346,7 +335,10 @@ void codegen_store(Item* a, Item* b)
     {
         printf("    movq    $%lld, %s(%%rip)\n", b->imm.i, a->imm.s);
     }
-    (void)a, (void)b;
+    else
+    {
+        assert(!"bad store op");
+    }
 }
 
 
index 58e01afd2eb9fd832513551c41e214e70be32171..0a0cbf631c8907fd48fe082060d233ac80db5a8e 100644 (file)
@@ -484,7 +484,7 @@ RULE(var_decl)
             sym = sym->next;
         }
 
-        if (!accept(p, ','))
+        if (!matches(p, IDENT))
         {
             break;
         }
@@ -512,7 +512,7 @@ RULE(const_decl)
         sym->imm = item->imm;
         sym->type = item->type;
 
-        if (!accept(p, ','))
+        if (!matches(p, IDENT))
         {
             break;
         }
@@ -592,63 +592,56 @@ RULE(statement_seq)
                 /* TODO: add function calls and other complex stuff */
                 error(p, "expected assignment");
             }
+            expect(p, ';');
         }
         else if (matches(p, IF))
         {
+            assert("!not implemented");
         }
         else
         {
             error(p, "expected a statement");
         }
-        expect(p, ';');
-        if (matches_oneof(p, (int[]){END, 0}))
+
+        if (matches(p, END))
         {
             break;
         }
     }
 }
 
-RULE(declaration_seq)
-{
-    if (accept(p, CONST))
-    {
-        const_decl(p, item);
-        expect(p, ';');
-    }
-
-    if (accept(p, TYPE))
-    {
-        type_decl(p, item);
-        expect(p, ';');
-    }
-
-    if (accept(p, VAR))
-    {
-        var_decl(p, item);
-        expect(p, ';');
-    }
-
-    /* WHILE sym = OSS.procedure DO ProcedureDecl; Check(OSS.semicolon, "; expected") END ; */
-}
-
 RULE(import_list)
 {
     (void)item;
     expect(p, IMPORT);
     while (1)
     {
-        expect(p, IDENT);
+        Module* m = calloc(1, sizeof(Module));
+        m->name = expect_text(p, IDENT);
         if (accept(p, '='))
         {
-            expect(p, IDENT);
+            m->alias   = m->name;
+            m->name    = expect_text(p, IDENT);
+            m->next    = p->imports;
+            p->imports = m;
         }
-        if (matches(p, ';'))
+
+        if (!matches(p, IDENT))
         {
             break;
         }
-        expect(p, ',');
     }
-    expect(p, ';');
+
+    /* reverse the list so when we init it happens in the right order */
+    Module* imports = p->imports;
+    p->imports = NULL;
+    while (imports)
+    {
+        Module* m = imports;
+        imports = m->next;
+        m->next = p->imports;
+        p->imports = m;
+    }
 }
 
 RULE(module)
@@ -656,29 +649,46 @@ RULE(module)
     expect(p, MODULE);
     char* sname = expect_text(p, IDENT);
         /* TODO: Check that it matches filename here */
-    expect(p, ';');
+    (void)sname;
 
     if (matches(p, IMPORT))
     {
         import_list(p, item);
     }
 
-    declaration_seq(p, item);
+    if (accept(p, CONST))
+    {
+        const_decl(p, item);
+    }
 
-    if (accept(p, BEGIN))
+    if (accept(p, TYPE))
     {
-        codegen_startproc("Module", 0);
-        statement_seq(p, item);
-        codegen_endproc();
+        type_decl(p, item);
     }
 
-    expect(p, END);
-    char* ename = expect_text(p, IDENT);
-    if (strcmp(sname, ename))
+    if (accept(p, VAR))
     {
-        error(p, "Expected module name '%s', recieved '%s' instead", sname, ename);
+        var_decl(p, item);
     }
-    expect(p, ';');
+
+    codegen_startproc("Module", 0);
+    if (accept(p, BEGIN))
+    {
+        for (Module* m = p->imports; m; m = m->next)
+        {
+            codegen_import(m->name);
+        }
+        statement_seq(p, item);
+        expect(p, END);
+    }
+    codegen_endproc();
+
+//    char* ename = expect_text(p, IDENT);
+//    if (strcmp(sname, ename))
+//    {
+//        error(p, "Expected module name '%s', recieved '%s' instead", sname, ename);
+//    }
+//    expect(p, ';');
 }
 
 static inline char* file_load(char* path)
index 1b9e81169613091e8c728c8b2410865be57bb9bc..7699a0b6893e7588a5fd20a83a5dbaf02ea33d68 100644 (file)
@@ -1,14 +1,16 @@
-module Module;
+module Module
+
+#import 
+#    A = Foo
+#    B = Bar
 
 const
-    A = true,
+    A = true
     B = 42
-    ;
 
 var
-    a : Bool,
+    a : Bool
     b : Int
-    ;
 
 begin
   a = true;
@@ -16,4 +18,4 @@ begin
   b = 24;
   b = B;
 #  c = a + b;
-end Module;
+end
index 72c4981fce30e54120108b0fbb9713136f070de4..40e5fe3f20befab61e85c0bbcad890085283bcd5 100644 (file)
@@ -1,34 +1,34 @@
-.data
+    .data
 a:
     .zero 1
 
-.data
+    .data
 b:
     .zero 8
 
     .text
     .globl Module
 Module:
-    pushq    %rbp
-    movq     %rsp, %rbp
+    pushq   %rbp
+    movq    %rsp, %rbp
 
     movq    $1, a(%rip)
     movq    $1, a(%rip)
     movq    $24, b(%rip)
     movq    $42, b(%rip)
 
-    pop      %rbp
+    pop     %rbp
     ret
 
     .text
     .globl main
 main:
-    pushq    %rbp
-    movq     %rsp, %rbp
+    pushq   %rbp
+    movq    %rsp, %rbp
 
-    call     Module
-    xor      %rax, %rax
+    call    Module
+    xor     %rax, %rax
 
-    pop      %rbp
+    pop     %rbp
     ret