From 858fec0ba29569cf4cae2eab86073142708c63cc Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 21 Sep 2022 21:50:03 -0400 Subject: [PATCH] added type emitting code for records --- cerise/backend/ssa/codegen.c | 18 ++++- cerise/src/ssa.c | 13 ++++ cerise/tests/Module.m | 128 +++++++---------------------------- 3 files changed, 53 insertions(+), 106 deletions(-) diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 72aa317..e8cc367 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -77,7 +77,23 @@ void emit_type(Type* type) break; case FORM_RECORD: - printf("%%%s", type->name); + if (type->name) + { + printf("%%%s", type->name); + } + else + { + printf("{ "); + for (Field* field = type->fields; field; field = field->next) + { + emit_type(field->type); + if (field->next) + { + printf(", "); + } + } + printf(" }"); + } break; case FORM_PROC: diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index e9521de..a5a2980 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -204,9 +204,22 @@ SsaNode* ssa_store(Parser* p, SsaNode* dest, SsaNode* value) SsaNode* ssa_fieldref(Parser* p, SsaNode* record, char* fname) { +#if 1 (void)p, (void)record, (void)fname; assert(!"record field references unimplemented"); return NULL; +#else + if (record->mode == MODE_MEMORY && record->code == '[') + { + record = load(p, record); + } + index = load(p, index); + SsaNode* node = ssa_node('[', MODE_MEMORY); + node->type = array->type->base; + node->left = array; + node->right = index; + return node; +#endif } SsaNode* ssa_index(Parser* p, SsaNode* array, SsaNode* index) diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index b837bd4..0c8f260 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -1,15 +1,25 @@ module Module +type + FooRec = record + a : Int + b : record + c : Int + end + d : array 5 of Int + end + var vBool* : Bool vInt* : Int vReal : Real vIntArray : array 42 of Int vIntArray2 : array 5 of array 5 of Int -# vRec : record -# a : Int -# b : array 5 of Int -# end + vRec1 : FooRec + vRec2 : record + a : Int + b : array 5 of Int + end procedure TestReturnVoid() begin @@ -160,107 +170,15 @@ begin vIntArray2[0][1] = vIntArray2[1][1] + vIntArray2[2][1]; end -#procedure TestRecordAccess() -#begin -#end +procedure TestRecordAccess() +begin +# vRec2.a = vRec2.a + vRec2.a; +end -#import -# Foo -# Bar3 = Bar2 -# -#const -# A* = true -# B* = 42 -# C* = 42.0 -# D = -B -# E = -C -# F = not A -# G = B + 2 - 2 * 2 -# H = false or A -# -#type -# TypeA* = Int -# TypeB* = array 5*B of Int -# TypeC* = array 5 of array 10 of Int -# TypeD* = record -# x,y : Int -# label : array 10 of Int -# dim : record -# w,h : Int -# end -# end -# TypeE* = record -# i : Int -# a : array 5 of Int -# end -# TypeF* = array 5 of TypeE -# -#var -# a* : Bool -# b* : Int -# c : Int -# d : Real -# e : Real -# f : array 5 of array 10 of Int -# g : TypeD -# h : array 5 of Int -# i : TypeF -# j : TypeA -# -##procedure Foo*(e : Int, z : Int, q1 : TypeD, q2 : array 5 of Int) : Int -## const FOO = 2 -## type foo = Int -## var -## z1 : Int -## q : array 5 of array 10 of Int -##begin -### e = q; -## c = 1; -## z1 = 2; -## return z1; -##end -# -##procedure Bar*(a : Int) : Int -##begin -## a = 42; -## return a; -##end -## -##procedure Baz*(a : Int) -##begin -## if (1 > 2) then -## 42; -## else -## 24; -## end -##end -# +procedure TestNestedRecordAccess() begin -## # Unary ops -## b = +b; -## b = -b; -## -## # Function calls -## c = Foo(1,2); -## e[0] = 1; -## Foo(1,2); -## e[2][1] = 1 + e[2][1]; -## e[b] = 1; -## e[1][2] = 1; -## c = e[1][c]; -## c = f.dim.w; -## f.dim.h = 0; -## f.label[0] = 42; -# -## c = 4; -## g[c] = 42; -## e[0][9] = 42; -# -## c = Bar(42); -## c = 42; -## c = 24; -# -## Bar(Foo.testint); -## Bar(Bar2.testint); -## Bar(Bar3.testint); +# vRec2.a.b = vRec2.a.b + vRec2.a.b; end + +#begin +#end -- 2.49.0