From: mike lowis Date: Tue, 1 Jun 2021 04:04:57 +0000 (-0400) Subject: checkpoint commit X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=228cc70572665888e71d73d855e28f5f89199500;p=proto%2Fobnc.git checkpoint commit --- diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index b16c489..ebffc48 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -146,7 +146,7 @@ static char* temp(Item* a) char name[32]; if (a->mode == ITEM_INDEX) { - snprintf(name, sizeof(name), "(*_T%d)", a->reg); + snprintf(name, sizeof(name), "(*_T%d + _T%lld)", a->reg, a->imm.i); } else if (a->mode == ITEM_FIELD) { @@ -459,19 +459,27 @@ void codegen_index(Parser* p, Item* array, Item* index) { error(p, "negative array index"); } + index->imm.i = (index->imm.i * size_of(array->type->base)); } else { printf(" __CHECK_RANGE(%s < %d);\n", temp(index), array->type->size); + int dest_reg = declare_temp(p, index->type, 0); + printf(" = %s * %ld;\n", temp(index), size_of(array->type->base)); + index->reg = dest_reg; + } + + if (array->mode == ITEM_INDEX) + { + int dest_reg = declare_temp(p, index->type, 0); + printf(" = _T%lld + %s;\n", array->imm.i, temp(index)); + index->reg = dest_reg; } /* 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; + array->imm.i = index->reg; } Field* get_field(Parser* p, Type* type, char* name) diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index ffc7439..bf5d62a 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -198,6 +198,10 @@ void dump_item(Item* a); void dump_symbol(Symbol* a); void dump_type(Type* a); +// src/align.c +long align_item(long offset, long size); +long size_of(Type* type); + /* Backend Code Generation *****************************************************************************/ extern Type BoolType, IntType, RealType, StringType; diff --git a/cerise/src/align.c b/cerise/src/align.c new file mode 100644 index 0000000..42d7e76 --- /dev/null +++ b/cerise/src/align.c @@ -0,0 +1,29 @@ +#include "cerise.h" + +long align_item(long offset, long size) +{ + offset--; + if (size >= 8u) + { + offset += (8 - (offset & (8 - 1))); + } + else if (size >= 4u) + { + offset += (4 - (offset & (4 - 1))); + } + else if (size >= 2u) + { + offset += (2 - (offset & (2 - 1))); + } + return offset; +} + +long size_of(Type* type) +{ + long size = 1; + for (; type->form == FORM_ARRAY; type = type->base) + { + size = (size * type->size); + } + return (size * type->size); +} diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index e17d873..d82edbb 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -63,34 +63,6 @@ static void init_item(Item* item, Symbol* sym) } } -static long align_item(long offset, long size) -{ - offset--; - if (size >= 8u) - { - offset += (8 - (offset & (8 - 1))); - } - else if (size >= 4u) - { - offset += (4 - (offset & (4 - 1))); - } - else if (size >= 2u) - { - offset += (2 - (offset & (2 - 1))); - } - return offset; -} - -static long size_of(Type* type) -{ - long size = 1; - for (; type->form == FORM_ARRAY; type = type->base) - { - size = (size * type->size); - } - return (size * type->size); -} - Field* add_field(Parser* p, Type* type, char* name, bool export) { Field* prev = NULL; diff --git a/cerise/ssa.md b/cerise/ssa.md new file mode 100644 index 0000000..a842482 --- /dev/null +++ b/cerise/ssa.md @@ -0,0 +1,43 @@ +type symbol = string + +type expr = + | Var of symbol + | Num of int + | Plus of expr * expr + | Assign of symbol * expr + +type mem = + Var of symbol + | Temp of symbol + | Addr of symbol + +type operand = + Const of int + | Mem of mem + +type cond = + Bool of operand + | Not of operand + | Eq of operand * operand + | Leq of operand * operand + | Le of operand * operand + +type rhs = + Plus of operand * operand + | Times of operand * operand + | Id of operand + +type instr= + Read of symbol + | Write of symbol + | Lab of symbol + | Assign of symbol * rhs + | AssignRI of operand * operand * operand + | AssignLI of operand * operand * operand + | BranchComp of cond * label + | Halt + | Nop + +type tree = + Oneline of instr + | Seq of tree * tree \ No newline at end of file diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index ebf4627..e23d994 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -117,5 +117,7 @@ begin # f.dim.h = 0; # f.label[0] = 42; - g[1] = 42; + c = 4; + g[c] = 42; +# e[0][9] = 42; end