From f2a7153067561a82f5aaf9edd1cb9e01ee12cb6b Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 19 Mar 2017 22:14:18 +0000 Subject: Completely working recursion and with correct frame --- c_compiler/src/expression.cpp | 1 + c_compiler/src/function.cpp | 2 -- c_compiler/test/in/recursion.c | 13 ++++++------- 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'c_compiler') diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index e19d845..98ca018 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -146,6 +146,7 @@ VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindin } std::cout << "\tjal\t" << postfix_expression_->id() << "\n\tnop\n"; + std::cout << "\tsw\t$2," << bindings.currentExpressionStackPosition() << "($fp)\n"; return bindings; } diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 55d8a2c..35e02f4 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -59,8 +59,6 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings, unsigne unsigned max_depth = 0; statement_->countExpressionDepth(max_depth); - std::cout << "# max depth: " << max_depth << "\n"; - if(max_argument_count < 4) max_argument_count = 4; diff --git a/c_compiler/test/in/recursion.c b/c_compiler/test/in/recursion.c index 3c16e33..8a1a170 100644 --- a/c_compiler/test/in/recursion.c +++ b/c_compiler/test/in/recursion.c @@ -1,13 +1,12 @@ int fact(int n) { - if(n <= 1) { - return 1; - } else { - int x = fact(n-1); - return x * n; - } + if(n <= 1) { + return 1; + } else { + return fact(n-1) * n; + } } int main() { - return fact(5); + return fact(5); } -- cgit