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; }
void n_switch::code_gen(status& stat, std::ostream& out) { stat.lock_register(out); stat.set_assign_expr(); // check expression left->code_gen(stat, out); stat.set_assign_expr(); stat.set_break_label(); // list of cases right->code_gen(stat, out); stat.unlock_register(out); out << stat.get_break_label() << ":\n"; out << stat.get_case_label() << ":\n"; stat.reset_labels(false); return; }