From 004ae6efa48dfc989c0b13d0d605c8ded635735f Mon Sep 17 00:00:00 2001 From: mike lowis Date: Thu, 22 Apr 2021 22:56:20 -0400 Subject: [PATCH] tweaked module code generation. global var references don't work yet --- cerise/cerise.h | 9 +++++---- cerise/codegen.c | 38 +++++++++++++++++++++++++++----------- cerise/parser.c | 35 +++++++++++++++-------------------- cerise/tests/Module.s | 7 ++----- 4 files changed, 49 insertions(+), 40 deletions(-) diff --git a/cerise/cerise.h b/cerise/cerise.h index 3d36eb4..d435505 100644 --- a/cerise/cerise.h +++ b/cerise/cerise.h @@ -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); diff --git a/cerise/codegen.c b/cerise/codegen.c index af270d4..012eb18 100644 --- a/cerise/codegen.c +++ b/cerise/codegen.c @@ -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) diff --git a/cerise/parser.c b/cerise/parser.c index 0a0cbf6..34382af 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -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 diff --git a/cerise/tests/Module.s b/cerise/tests/Module.s index 40e5fe3..50a2c52 100644 --- a/cerise/tests/Module.s +++ b/cerise/tests/Module.s @@ -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 - -- 2.49.0