void n_for::code_gen(status& stat, std::ostream& out) { if (init != NULL) // initialising expression init->code_gen(stat, out); // start of loop: this is a lot like a while loop. stat.set_continue_label(); out << stat.get_continue_label() << ":\n"; stat.lock_register(out); stat.set_assign_expr(); // check expression cond->code_gen(stat, out); stat.set_break_label(); // exit loop if expression equals zero out << "\tbeqz\t$t" << stat.get_register() << "," << stat.get_break_label() << "\n"; out << "\tnop\n"; stat.unlock_register(out); stat.set_assign_expr(); // loop statement left->code_gen(stat, out); if (right != NULL) // loop expression (something like i++) right->code_gen(stat, out); out << "\tb\t" << stat.get_continue_label() << "\n"; out << "\tnop\n"; out << stat.get_break_label() << ":\n"; stat.reset_labels(true); 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; }
void n_dowhile::code_gen(status& stat, std::ostream& out) { // label signifying the start of the loop stat.set_continue_label(); out << stat.get_continue_label() << ":\n"; // loop statement stat.set_break_label(); left->code_gen(stat, out); stat.lock_register(out); stat.set_assign_expr(); // check expression right->code_gen(stat, out); // do another loop if expression is non zero out << "\tbnez\t$t" << stat.get_register() << "," << stat.get_continue_label() << "\n"; out << "\tnop\n"; out << stat.get_break_label() << ":\n"; stat.unlock_register(out); stat.set_assign_expr(); stat.reset_labels(true); return; }
void n_while::code_gen(status& stat, std::ostream& out) { // label signifying the start of the loop stat.set_continue_label(); out << stat.get_continue_label() << ":\n"; stat.lock_register(out); stat.set_assign_expr(); // check expression left->code_gen(stat, out); stat.set_break_label(); // exit loop if expression equals zero out << "\tbeqz\t$t" << stat.get_register() << "," << stat.get_break_label() << "\n"; out << "\tnop\n"; stat.unlock_register(out); stat.set_assign_expr(); // loop statement right->code_gen(stat, out); // return to top of loop out << "\tb\t" << stat.get_continue_label() << "\n"; out << "\tnop\n"; out << stat.get_break_label() << ":\n"; stat.reset_labels(true); return; }