Skip to content

Commit

Permalink
[cgen] code for assignment and static dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhevg committed Apr 17, 2016
1 parent 5bcb01c commit 7808736
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions cgen/cgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CodeGenerator: public TreeVisitor {
method_class *current_method;
void emit_function_entry(int tmp_count);
void emit_function_exit(int tmp_count, int parameter_count);
void dispatch(Expression callee, Symbol type, Symbol name, Expressions actuals, int n_temp);

public:
CodeGenerator(ClassTable *_class_table, ostream& _str):
Expand All @@ -58,10 +59,10 @@ class CodeGenerator: public TreeVisitor {

void before(method_class *node);
void after(method_class *node);

// void after(attr_class *node);
void after(formal_class *node);

void before(program_class *node);

void code(assign_class *expr, int n_temp);
void code(static_dispatch_class *expr, int n_temp);
void code(dispatch_class *expr, int n_temp);
Expand All @@ -87,6 +88,5 @@ class CodeGenerator: public TreeVisitor {
void code(bool_const_class *expr, int n_temp);
void code(no_expr_class *expr, int n_temp);

void before(program_class *node);
};

29 changes: 17 additions & 12 deletions cgen/cgen_expressions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,41 @@ void CodeGenerator::code(bool_const_class *expr, int n_temp) {
}

void CodeGenerator::code(assign_class *expr, int n_temp) {
//todo
expr->get_expr()->code(this, n_temp);
str << "#\tAssign to " << expr->get_name() << endl;
object_env.lookup(expr->get_name())->code_store(str);
}

void CodeGenerator::code(static_dispatch_class *expr, int n_temp) {
//todo
}

void CodeGenerator::code(dispatch_class *expr, int n_temp) {
str << "#\tcalling " << expr->get_name() << endl;
void CodeGenerator::dispatch(Expression callee, Symbol type, Symbol name, Expressions actuals, int n_temp) {
str << "#\tcalling " << name << endl;
emit_push(FP, str);
Expressions actuals = expr->get_actuals();
for (int i = 0; i < actuals->len(); i++) {
int idx = actuals->len() - i - 1;
str << "#\tcoding actual parameter #" << idx << endl;
actuals->nth(idx)->code(this, n_temp);
emit_push(ACC, str);
}
str << "#\tcallee object" << endl;
Expression callee = expr->get_callee();
callee->code(this, n_temp);
str << "#\tload pointer to dispatch table into $t1" << endl;
emit_load(T1, 2, ACC, str);
int method_offset = class_table->get_method_offset(callee->get_type(), expr->get_name());
int method_offset = class_table->get_method_offset(type, name);
if (method_offset > 0) {
str << "#\tload address of method " << expr->get_name() << " into $t1" << endl;
str << "#\tload address of method " << name << " into $t1" << endl;
emit_load(T1, method_offset, T1, str);
} else {
str << "#\tmethod " << expr->get_name() << " has offset 0" << endl;
str << "#\tmethod " << name << " has offset 0" << endl;
}
emit_jalr(T1, str);

}

void CodeGenerator::code(static_dispatch_class *expr, int n_temp) {
dispatch(expr->get_callee(), expr->get_type_name(), expr->get_name(), expr->get_actuals(), n_temp);
}

void CodeGenerator::code(dispatch_class *expr, int n_temp) {
dispatch(expr->get_callee(), expr->get_type(), expr->get_name(), expr->get_actuals(), n_temp);
}

void CodeGenerator::code(cond_class *expr, int n_temp) {
Expand Down

0 comments on commit 7808736

Please sign in to comment.