diff --git a/assignments/PA5/cgen.cc b/assignments/PA5/cgen.cc index e7a0b4a..cf52926 100644 --- a/assignments/PA5/cgen.cc +++ b/assignments/PA5/cgen.cc @@ -601,7 +601,39 @@ void CgenClassTable::code_dispatchTable() { } } -void CgenClassTable::code_prototypeObject() {} +void CgenClassTable::code_prototypeObject() { + auto int_default = inttable.lookup_string("0"); + auto str_default = stringtable.lookup_string(""); + auto bool_default = &falsebool; + + for (auto node : nodes) { + str << WORD << "-1\n"; // GC tag + str << node->name << PROTOBJ_SUFFIX << LABEL; // proto obj label + str << WORD << node->get_class_tag() << endl; // class tag + str << WORD << node->get_object_size() << endl; // object size + str << WORD << node->get_name() << DISPTAB_SUFFIX << endl; // dispatch ptr + for (auto attr : *node->get_attributes()) { + if (attr->type_decl == Int) { + str << WORD; + int_default->code_ref(str); + str << endl; + } else if (attr->type_decl == Bool) { + str << WORD; + bool_default->code_ref(str); + str << endl; + } else if (attr->type_decl == Str) { + str << WORD; + str_default->code_ref(str); + str << endl; + } else { + // for other types(including _prim_slot type), set to void(null pointer) + str << WORD; + str << 0; + str << endl; + } + } + } +} CgenClassTable::CgenClassTable(Classes classes, ostream &s) : str(s) { enterscope(); @@ -903,11 +935,10 @@ void CgenClassTable::code() { nodes[0]->traverse_generate_object(); // nodes[0] is the Object class code_dispatchTable(); - // Add your code to emit - // - prototype objects - // - class_nameTab - // - dispatch tables - // + + if (cgen_debug) + cout << "coding prototype objects" << endl; + code_prototypeObject(); if (cgen_debug) cout << "coding global text" << endl; diff --git a/assignments/PA5/example.cl b/assignments/PA5/example.cl index 163f1a9..020fb7b 100644 --- a/assignments/PA5/example.cl +++ b/assignments/PA5/example.cl @@ -21,6 +21,7 @@ class B inherits A { class C inherits A { attr_C_1 : String <- "This is C\n"; + attr_C_2 : SELF_TYPE; method_common(x1: Int) : SELF_TYPE { self };