From 5a8e9d9df2ec822e1fcad9aa12608e4f7fa7a9f6 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 22 Apr 2021 16:45:18 -0400 Subject: [PATCH] added code to define variables --- cerise/cerise.h | 1 + cerise/codegen.c | 22 ++++++++++++++++++++-- cerise/parser.c | 1 + cerise/tests/Module.s | 9 +++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cerise/cerise.h b/cerise/cerise.h index 98bfe07..b110dbc 100644 --- a/cerise/cerise.h +++ b/cerise/cerise.h @@ -152,6 +152,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_var(char* name, Type* type); void codegen_main(char* modname); void codegen_startproc(char* name, long long localsz); void codegen_endproc(void); diff --git a/cerise/codegen.c b/cerise/codegen.c index 08848ef..18f8c4f 100644 --- a/cerise/codegen.c +++ b/cerise/codegen.c @@ -4,12 +4,12 @@ Type BoolType = { .form = FORM_BOOL, - .size = sizeof(int) + .size = sizeof(bool) }; Type IntType = { .form = FORM_INT, - .size = sizeof(int) + .size = sizeof(long) }; Type RealType = { @@ -186,11 +186,29 @@ static void const_unop(int op, Item* a) } } +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"); + } +} + void codegen_main(char* modname) { codegen_startproc("main", 0); // op_l (OP_CALL, modname); printf(" call %s\n", modname); + printf(" xor %%rax, %%rax\n"); codegen_endproc(); } diff --git a/cerise/parser.c b/cerise/parser.c index c315d8d..58e01af 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -480,6 +480,7 @@ RULE(var_decl) for (int i = 0; i < nsyms; i++) { sym->type = type->type; + codegen_var(sym->name, sym->type); sym = sym->next; } diff --git a/cerise/tests/Module.s b/cerise/tests/Module.s index 91f16b4..72c4981 100644 --- a/cerise/tests/Module.s +++ b/cerise/tests/Module.s @@ -1,3 +1,11 @@ +.data +a: + .zero 1 + +.data +b: + .zero 8 + .text .globl Module Module: @@ -19,6 +27,7 @@ main: movq %rsp, %rbp call Module + xor %rax, %rax pop %rbp ret -- 2.49.0