void n_ifelse::code_gen(status& stat, std::ostream& out) { stat.lock_register(out); stat.set_assign_expr(); // check expression left->code_gen(stat, out); // branch over if statement, if the expression equals zero std::string else_label = stat.label_gen(); out << "\tbeqz\t$t" << stat.get_register() << "," << else_label << "\n"; out << "\tnop\n"; stat.unlock_register(out); stat.set_assign_expr(); // if statement right->code_gen(stat, out); if (else_node != NULL) { std::string end_label = stat.label_gen(); // branch over else statement, if if branch was executed out << "\tb\t" << end_label << "\n"; out << "\tnop\n"; out << else_label << ":\n"; // else statement else_node->code_gen(stat, out); out << end_label << ":\n"; } else { out << else_label << ":\n"; } return; }
void n_ternary::code_gen(status& stat, std::ostream& out) { bool unlock = false; if (!stat.get_assign_expr()) { stat.lock_register(out); stat.set_assign_expr(); unlock = true; } cond->code_gen(stat, out); // the condition std::string zero_label = stat.label_gen(); out << "\tbeqz\t$t" << stat.get_register() << "," << zero_label << "\n"; out << "\tnop\n"; left->code_gen(stat, out); // the first expression std::string end_label = stat.label_gen(); out << "\tb\t" << end_label << "\n"; out << zero_label << ":\n"; right->code_gen(stat, out); // the second expression out << end_label << ":\n"; if (unlock) { stat.unlock_register(out); stat.set_assign_expr(); } return; }
void n_case::code_gen(status& stat, std::ostream& out) { if (left != NULL) // case: { stat.lock_register(out); stat.set_assign_expr(); int op2 = stat.get_register(); left->code_gen(stat, out); stat.unlock_register(out); stat.set_assign_expr(); std::string next_case_label = stat.label_gen(); // if the expressions are not equal, branch over statement out << "\tbne\t$t" << stat.get_register() << ",$t" << op2 << "," << next_case_label << "\n"; out << "\tnop\n"; // case body label if (stat.get_case_label() != "empty") out << stat.get_case_label() << ":\n"; stat.set_case_label(); if (right != NULL) right->code_gen(stat, out); // case body // fall through: need to jump to the next case body. out << "\tb\t" << stat.get_case_label() << "\n"; out << "\tnop\n"; out << next_case_label << ":\n"; } else // default: { if (right != NULL) right->code_gen(stat, out); } return; }