]> git.mdlowis.com Git - proto/obnc.git/commitdiff
tweaked module code generation. global var references don't work yet
authormike lowis <mike@mdlowis.com>
Fri, 23 Apr 2021 02:56:20 +0000 (22:56 -0400)
committermike lowis <mike@mdlowis.com>
Fri, 23 Apr 2021 02:56:20 +0000 (22:56 -0400)
cerise/cerise.h
cerise/codegen.c
cerise/parser.c
cerise/tests/Module.s

index 3d36eb47e6c19caf8490b78a368b59b7817f1b90..d435505f4ea40e7d74795f409df73a9a44557872 100644 (file)
@@ -129,6 +129,7 @@ typedef struct {
     Tok tok;
     Module* imports;
     Symbol* scope;
+    char* name;
 } Parser;
 
 void lexfile(Parser* ctx, char* path);
@@ -160,10 +161,10 @@ 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);
+void codegen_imports(Parser* p);
+void codegen_global(Parser* p, char* name, Type* type);
+void codegen_main(Parser* p);
+void codegen_startproc(Parser* p, char* name, long long localsz);
 void codegen_endproc(void);
 void codegen_unop(int op, Item* a);
 void codegen_binop(int op, Item* a, Item* b);
index af270d420dd0ba6c7f400d2d4b9cd5b9b6178ccd..012eb18246abd3bb3182a2eaf40e73a73726c775 100644 (file)
@@ -186,31 +186,47 @@ static void const_unop(int op, Item* a)
     }
 }
 
-void codegen_import(char* name)
+void codegen_imports(Parser* p)
 {
-    printf("    call    %s\n", name);
+    for (Module* m = p->imports; m; m = m->next)
+    {
+        printf("    call    %s\n", m->name);
+    }
 }
 
-void codegen_var(char* name, Type* type)
+void codegen_global(Parser* p, char* name, Type* type)
 {
     printf("    .data\n");
-    printf("%s:\n", name);
+    printf("%s_%s:\n", p->name, name);
     printf("    .zero %d\n\n", type->size);
 }
 
-void codegen_main(char* modname)
+void codegen_main(Parser* p)
 {
-    codegen_startproc("main", 0);
-    printf("    call    %s\n", modname);
+    printf("    .text\n");
+    printf("    .globl main\n");
+    printf("main:\n");
+    printf("    pushq   %%rbp\n");
+    printf("    movq    %%rsp, %%rbp\n");
+    printf("    call    %s\n", p->name);
     printf("    xor     %%rax, %%rax\n");
-    codegen_endproc();
+    printf("    pop     %%rbp\n");
+    printf("    ret\n");
 }
 
-void codegen_startproc(char* name, long long localsz)
+void codegen_startproc(Parser* p, char* name, long long localsz)
 {
     printf("    .text\n");
-    printf("    .globl %s\n", name);
-    printf("%s:\n", name);
+    if (name)
+    {
+        printf("    .globl %s_%s\n", p->name, name);
+        printf("%s_%s:\n", p->name, name);
+    }
+    else
+    {
+        printf("    .globl %s\n", p->name);
+        printf("%s:\n", p->name);
+    }
 
     printf("    pushq   %%rbp\n");
     if (localsz > 0)
index 0a0cbf631c8907fd48fe082060d233ac80db5a8e..34382afc84fc3e01a775f5c6ca7493a326eacf14 100644 (file)
@@ -480,7 +480,7 @@ RULE(var_decl)
         for (int i = 0; i < nsyms; i++)
         {
             sym->type = type->type;
-            codegen_var(sym->name, sym->type);
+            codegen_global(p, sym->name, sym->type);
             sym = sym->next;
         }
 
@@ -647,9 +647,8 @@ RULE(import_list)
 RULE(module)
 {
     expect(p, MODULE);
-    char* sname = expect_text(p, IDENT);
+    p->name = expect_text(p, IDENT);
         /* TODO: Check that it matches filename here */
-    (void)sname;
 
     if (matches(p, IMPORT))
     {
@@ -671,13 +670,10 @@ RULE(module)
         var_decl(p, item);
     }
 
-    codegen_startproc("Module", 0);
+    codegen_startproc(p, NULL, 0);
     if (accept(p, BEGIN))
     {
-        for (Module* m = p->imports; m; m = m->next)
-        {
-            codegen_import(m->name);
-        }
+        codegen_imports(p);
         statement_seq(p, item);
         expect(p, END);
     }
@@ -714,18 +710,17 @@ static inline char* file_load(char* path)
 void compile(char* fname)
 {
     char* fcontents = file_load(fname);
-    module(
-        &(Parser){
-            .scope = &RealSym,
-            .file  = &(LexFile){
-                .path  = fname,
-                .fbeg  = fcontents,
-                .fpos  = fcontents,
-            }
-        },
-        &(Item){0}
-    );
-    codegen_main("Module");
+    Parser* p = &(Parser){
+        .name  = NULL,
+        .scope = &RealSym,
+        .file  = &(LexFile){
+            .path  = fname,
+            .fbeg  = fcontents,
+            .fpos  = fcontents,
+        }
+    };
+    module(p, &(Item){0});
+    codegen_main(p);
 }
 
 /* Grammar Unit Tests
index 40e5fe3f20befab61e85c0bbcad890085283bcd5..50a2c52c16beb2b17bc36a2e32f8337d6b72862b 100644 (file)
@@ -1,9 +1,9 @@
     .data
-a:
+Module_a:
     .zero 1
 
     .data
-b:
+Module_b:
     .zero 8
 
     .text
@@ -25,10 +25,7 @@ Module:
 main:
     pushq   %rbp
     movq    %rsp, %rbp
-
     call    Module
     xor     %rax, %rax
-
     pop     %rbp
     ret
-