From e0c57830ee93b96efca7d7f21c6a2709e45e3769 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 27 May 2021 15:55:05 -0400 Subject: [PATCH] cleaned up record access code --- cerise/backend/c99/codegen.c | 18 ++++++++---------- cerise/tests/Module.m | 5 +++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index 5e90fd1..6f00c37 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -26,7 +26,7 @@ static char* TypeNames[FORM_COUNT] = { [FORM_BOOL] = "Bool", [FORM_INT] = "Int", [FORM_REAL] = "Real", - [FORM_STRING] = "char*" + [FORM_STRING] = "Byte*" }; static char* Operators[] = { @@ -101,7 +101,7 @@ static void make_var(Parser* p, Symbol* sym, int isref) } else if (sym->type->form == FORM_RECORD) { - printf("char %s[%d]", name, sym->type->size); + printf("Byte %s[%d]", name, sym->type->size); } else { @@ -130,7 +130,7 @@ static int declare_temp(Parser* p, Type* type, int isref) }; if (type->form == FORM_RECORD) { - printf(" char* %s", name); + printf(" Byte* %s", name); } else { @@ -149,7 +149,7 @@ static char* temp(Item* a) } else if (a->mode == ITEM_FIELD) { - snprintf(name, sizeof(name), "*((%s)_T%d)", typetoptr(a->type), a->reg); + snprintf(name, sizeof(name), "*((%s)(_T%d + %d))", typetoptr(a->type), a->reg, a->offset); } else { @@ -178,7 +178,7 @@ static void load_var(Parser* p, Item* item) item->reg = declare_temp(p, item->type, isref); if (item->type->form == FORM_RECORD) { - printf(" = (char*)&%s[0];\n", item->imm.s); + printf(" = (Byte*)&%s[0];\n", item->imm.s); } else { @@ -230,9 +230,11 @@ static void unary_op(Parser* p, int op, Item* a) void codegen_startmod(Parser* p) { (void)p; + printf("typedef char Byte;\n"); printf("typedef _Bool Bool;\n"); printf("typedef long Int;\n"); printf("typedef double Real;\n"); + puts(""); } void codegen_endmod(Parser* p) @@ -369,7 +371,6 @@ void codegen_store(Parser* p, Item* a, Item* b) else if (a->mode == ITEM_FIELD) { load_var(p, b); -// printf(" *((%s)%s) = %s;\n", typetoptr(a->type), temp(a), temp(b)); printf(" %s = %s;\n", temp(a), temp(b)); } else @@ -494,8 +495,5 @@ void codegen_field(Parser* p, Item* record, char* name) Field* f = get_field(p, record->type, name); record->mode = ITEM_FIELD; record->type = f->type; - printf(" char* _T%d", p->curr_reg); - p->curr_reg++; - printf(" = _T%d + %ld;\n", record->reg, f->offset); - record->reg = p->curr_reg-1; + record->offset += f->offset; } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 60afadf..665c1eb 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -14,7 +14,7 @@ type TypeC = array 5 of array 10 of Int TypeD = record x,y : Int - label : array 10 of Real + label : array 10 of Int dim : record w,h : Int end @@ -112,6 +112,7 @@ begin # e[b] = 1; # e[1][2] = 1; # c = e[1][c]; - c = f.dim.w; +# c = f.dim.w; f.dim.h = 0; +# f.label[0] = 42; end -- 2.49.0