From 139684efc49e572ff3f237205e5f3eefa9dca0dd Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 28 May 2021 14:21:26 -0400 Subject: [PATCH] partial fix-up of array access. Need to generate index calculations --- cerise/TODO.md | 5 +++-- cerise/backend/c99/codegen.c | 23 ++++++++++++----------- cerise/inc/cerise.h | 5 +++++ cerise/tests/Module.m | 7 +++++-- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/cerise/TODO.md b/cerise/TODO.md index e7a684c..ec0cbf3 100644 --- a/cerise/TODO.md +++ b/cerise/TODO.md @@ -1,10 +1,10 @@ # Doing -* Cleanup the lexer +* Fix array access in structs # Up Next -* Fix array access in structs +* Implement symbol file generation and import * Implement module scoped identifiers * Implement an SSA backend * Implement pointers @@ -13,6 +13,7 @@ # Backlog +* Cleanup the lexer * Implement record extension * Implement logical operators: and, or, not * Implement string types diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index 71378b1..b16c489 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -92,12 +92,13 @@ static void make_var(Parser* p, Symbol* sym, int isref) if (sym->type->form == FORM_ARRAY) { Type* type = sym->type; - for (; type->form == FORM_ARRAY; type = type->base); - printf("%s %s", TypeNames[type->form], name); - for (type = sym->type; type->form == FORM_ARRAY; type = type->base) + long size = 1; + for (; type->form == FORM_ARRAY; type = type->base) { - printf("[%d]", type->size); + size = size * type->size; } + size = size * type->size; + printf("Byte %s[%ld]", name, size); } else if (sym->type->form == FORM_RECORD) { @@ -128,7 +129,7 @@ static int declare_temp(Parser* p, Type* type, int isref) .name = name, .type = type, }; - if (type->form == FORM_RECORD) + if ((type->form == FORM_RECORD) || (type->form == FORM_ARRAY)) { printf(" Byte* %s", name); } @@ -440,9 +441,8 @@ void codegen_return(Parser* p, Item* item) void codegen_index(Parser* p, Item* array, Item* index) { - /* load array and index if not already */ - load_var(p, index); load_var(p, array); + load_var(p, index); /* perform range checking */ if (index->mode == ITEM_CONST) @@ -465,12 +465,13 @@ void codegen_index(Parser* p, Item* array, Item* index) printf(" __CHECK_RANGE(%s < %d);\n", temp(index), array->type->size); } - /* emit the operation */ + /* emit the operation to calculate offset */ array->type = array->type->base; array->mode = ITEM_INDEX; - int dest_reg = declare_temp(p, array->type, 1); - printf(" = &%s[%s];\n", temp(array), temp(index)); - array->reg = dest_reg; + +// int dest_reg = declare_temp(p, array->type, 1); +// printf(" = &%s[%s];\n", temp(array), temp(index)); +// array->reg = dest_reg; } Field* get_field(Parser* p, Type* type, char* name) diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 941562f..ffc7439 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -246,6 +246,11 @@ typedef struct { Operation** ops; } Block; +/* , + */ +/* +, */ +/* , */ +/* , , */ + /* typedef struct AnfNode { diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 4421a0f..ebf4627 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -27,6 +27,7 @@ var d : Real e : array 5 of array 10 of Int f : TypeD + g : array 5 of Int procedure Foo*(e : Int, z : Int, q1 : TypeD, q2 : array 5 of Int) : Int const FOO = 2 @@ -113,6 +114,8 @@ begin # e[1][2] = 1; # c = e[1][c]; # c = f.dim.w; - f.dim.h = 0; - f.label[0] = 42; +# f.dim.h = 0; +# f.label[0] = 42; + + g[1] = 42; end -- 2.49.0