void n_jump_stat::code_gen(status& stat, std::ostream& out) { if (type == "continue") { out << "\tb\t" << stat.get_continue_label() << "\n"; } else if (type == "break") { out << "\tb\t" << stat.get_break_label() << "\n"; } else if (type == "return") { if (left != NULL) { stat.lock_register(out); stat.set_assign_expr(); left->code_gen(stat, out); out << "\tmove\t$v0,$t" << stat.get_register() << "\n"; stat.set_assign_expr(); stat.unlock_register(out); out << "\tb\tr" << stat.get_function_name() << "\n"; out << "\tnop\n"; } } else // goto { out << "\tj\tl_" << type << "\n"; } return; }
void n_func_def::code_gen(status& stat, std::ostream& out) { // starting pseudo ops stat.change_text_data("text", out); out << "\t.globl\t" << stat.get_function_name() << "\n"; out << "\t.ent\t" << stat.get_function_name() << "\n"; out << "\t.type\t" << stat.get_function_name() << ", @function\n"; out << stat.get_function_name() << ":\n"; // dealing with the stack and frame: // status contains the function number and number of variables in the function. // function 0 is global, main will probably be function 1 (not really relevant here) // we need 8 + (vars*4) space on the stack. All variables assigned 4 bytes for simplicity. int stack_space = 8 + stat.size_variables(); out << "\taddiu\t" << "$sp,$sp,-" << stack_space << "\n"; // stack grows down... out << "\tsw\t" << "$fp," << (stack_space - 4) << "($sp)\n"; // store the old fp at the top of the stack out << "\taddi\t" << "$fp,$sp," << stack_space << "\n"; // set new fp at the top of the stack frame. if (left != NULL) left->code_gen(stat, out); // the function body. out << "\tmove\t$v0,$zero\n"; // return zero by default if (stat.number_returns() > 0) out << "r" << stat.get_function_name() << ":\n"; // label to branch to and return out << "\tlw\t" << "$fp," << (stack_space - 4) << "($sp)\n"; // restore the old fp out << "\taddiu\t" << "$sp,$sp," << stack_space << "\n"; // move the stack back up. out << "\tj\t$ra\n"; // return out << "\tnop\n"; out << "\t.end\t" << stat.get_function_name() << "\n\n"; stat.remove_parameters(); return; }