diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-16 15:14:26 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-16 15:14:26 +0000 |
commit | 03e21a7ef589fa52d27eab85d669ca854e8ac2b8 (patch) | |
tree | 4dec7c89693c74e268756ce156ea6cdac3a41722 /c_compiler/src/function.cpp | |
parent | 667a766552e2002ae7cf7969d78aaeba906d3759 (diff) | |
download | Compiler-03e21a7ef589fa52d27eab85d669ca854e8ac2b8.tar.gz Compiler-03e21a7ef589fa52d27eab85d669ca854e8ac2b8.zip |
Still works and added correct frame size
Diffstat (limited to 'c_compiler/src/function.cpp')
-rw-r--r-- | c_compiler/src/function.cpp | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 2152c71..24cea4c 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -49,16 +49,23 @@ void Function::printXml() const VariableStackBindings Function::printAsm(VariableStackBindings bindings) const { // Counting all the variables being declared in the function - unsigned count = 0; + unsigned variable_count = 0; if(statement_ != nullptr) - statement_->countVariables(count); + statement_->countVariables(variable_count); + + unsigned max_argument_count = 0; + // Count the maximum number of arguments + statement_->countArguments(max_argument_count); + + if(max_argument_count < 4) + max_argument_count = 4; - // This includes the space for the old frame counter and (return address)? - unsigned memory_needed = 4*count+8; + // This adds 2 to store the frame pointer and the return address + unsigned memory_needed = 4*(variable_count + max_argument_count + 2); - std::cout << "\t.text\n\t.globl\t" << id_ << std::endl << id_ << ":\n\taddiu\t$sp,$sp,-" - << memory_needed << "\n\tsw\t$fp," << memory_needed-4 << "($sp)\n\tmove\t$fp,$sp" - << std::endl; + std::cout << "\t.text\n\t.globl\t" << id_ << "\n" << id_ << ":\n\taddiu\t$sp,$sp,-" + << memory_needed << "\n\tsw\t$31," << memory_needed-4 << "($sp)\n" << "\tsw\t$fp," + << memory_needed-8 << "($sp)\n\tmove\t$fp,$sp\n"; // TODO print asm for parameters @@ -66,7 +73,7 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings) const statement_->printAsm(bindings); std::cout << "\tmove\t$sp,$fp\n\tlw\t$fp," << memory_needed-4 << "($sp)\n\taddiu\t$sp,$sp," - << memory_needed << "\n\tjr\t$31\n\tnop" << std::endl; + << memory_needed << "\n\tjr\t$31\n\tnop\n"; return bindings; } |