From 2644480b9e5bf50464a15925994f95f3126235e5 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Sat, 1 Apr 2023 16:51:03 +0800 Subject: [PATCH] Add GC support --- assignments/PA5/cgen.cc | 19 +++++++++++++++---- assignments/PA5/example.cl | 2 ++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/assignments/PA5/cgen.cc b/assignments/PA5/cgen.cc index 4b46a43..616fb95 100644 --- a/assignments/PA5/cgen.cc +++ b/assignments/PA5/cgen.cc @@ -356,6 +356,11 @@ static void emit_gc_check(char *source, ostream &s) { s << JAL << "_gc_check" << endl; } +static void emit_gc_assign(int offset, ostream &s) { + emit_addiu(A1, SELF, offset * WORD_SIZE, s); + emit_gc_assign(s); +} + /* | ........ | |------------| @@ -737,7 +742,7 @@ void CgenClassTable::gen_init_code() { for (auto i = 0U; i < attrs->size(); ++i) { auto attr_pair = (*attrs)[i]; loctab.add(LocationTableItem(attr_pair.second->name, Attribute, - i + DEFAULT_OBJFIELDS)); + i + DEFAULT_OBJFIELDS)); } for (auto i = 0U; i < attrs->size(); ++i) { auto attr_pair = (*attrs)[i]; @@ -754,9 +759,11 @@ void CgenClassTable::gen_init_code() { this->expr_type_decl = attr_pair.second->type_decl; attr_pair.second->init->code(str, this); emit_store(ACC, i + DEFAULT_OBJFIELDS, SELF, str); + if (cgen_Memmgr != GC_NOGC) + emit_gc_assign(i + DEFAULT_OBJFIELDS, str); } loctab.exitscope(); - emit_move(ACC, SELF, str); + emit_move(ACC, SELF, str); // remember to place the initialized object back to $a0, other code rely on // this behavior emit_procedure_footer(0, str); @@ -1240,8 +1247,11 @@ void assign_class::code(ostream &s, CgenClassTable *classtab) { emit_store(ACC, loc->offset, FP, s); } else { emit_store(ACC, loc->offset, SELF, s); + if (cgen_Memmgr != GC_NOGC) + emit_gc_assign(loc->offset, s); } } + /* static dispatch is a little bit simpler than normal dispatch Intuitively, first evaluate all the parameter expression and push stack. Note @@ -1262,7 +1272,7 @@ void static_dispatch_class::code(ostream &s, CgenClassTable *classtab) { auto actual_expr = this->actual->nth(actual_i); actual_expr->code(s, classtab); emit_push(ACC, s); // push param to stack - ++ nparams; + ++nparams; } this->expr->code(s, classtab); auto label_dispatch = classtab->alloc_label_index(); @@ -1301,7 +1311,7 @@ void dispatch_class::code(ostream &s, CgenClassTable *classtab) { auto actual_expr = this->actual->nth(actual_i); actual_expr->code(s, classtab); emit_push(ACC, s); // push param to stack - ++ nparams; + ++nparams; } this->expr->code(s, classtab); auto label_dispatch = classtab->alloc_label_index(); @@ -1332,6 +1342,7 @@ void dispatch_class::code(ostream &s, CgenClassTable *classtab) { // call the method whose address is in $t1, with self object in $a0 emit_jalr(T1, s); rel_stack_depth -= nparams; + // caller doesn't pop the params, so we need to manual set the stack depth } /* diff --git a/assignments/PA5/example.cl b/assignments/PA5/example.cl index 9205019..7da6f73 100644 --- a/assignments/PA5/example.cl +++ b/assignments/PA5/example.cl @@ -37,8 +37,10 @@ class C inherits A { method_3(x1: Int, x2: A, x3: String) : Object { { x1 <- x1 + 00010 - x1 / 10; + attr_C_1 <- x3; x2 <- x2.method_common(x1); x3 <- x3.concat("schade"); + attr_C_0 <- x1; } }; method_2(x1: Bool, x2: C) : Object {