コード例 #1
0
ファイル: ast.cpp プロジェクト: coopersimon/Compiler
void n_func_call::code_gen(status& stat, std::ostream& out)
{
	// first, save the current return address in preallocated space.
	out << "\tsw\t$ra,-8($fp)\n";
	// next, push argument registers/temp registers in use on the stack.
	stat.push_registers(out);
	// next, load arguments into registers/stack.
	right->code_gen(stat, out);
	// next, call the function
	std::stringstream id;
	left->print(id);
	out << "\t.option pic0\n"; // pic = position independent code
	out << "\tjal\t" << id.str() << "\n";
	out << "\tnop\n";
	out << "\t.option pic2\n"; // would I be honest if I said I knew what these meant? no.
	// next, pop arguments 4+ off the stack.
	stat.unlock_arg_registers(out);
	// store return value in preallocated register: 
	/***ASSUMING INT RETURN!!!!****/
	if (stat.get_register() >= 0)
		out << "\tmove\t$t" << stat.get_register() << ",$v0\n";
	// next, pop argument registers back off the stack.
	stat.pop_registers(out);
	// finally, reload return address.
	out << "\tlw\t$ra,-8($fp)\n";
	return;
}