From e027bef26e25af8e3998d06942872bdcb36ee551 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 24 Mar 2017 23:43:45 +0000 Subject: Fixing stuff --- c_compiler/include/expression.hpp | 3 +++ c_compiler/include/function.hpp | 3 +-- c_compiler/src/c_parser.y | 1 + c_compiler/src/expression.cpp | 30 ++++++++++++++++++++-- c_compiler/src/function.cpp | 17 +++++------- test_deliverable/testcases/test_EMPTY.c | 7 +++++ test_deliverable/testcases/test_EMPTY_driver.c | 8 ++++++ test_deliverable/testcases/test_FUNCCALL0.c | 6 +++++ test_deliverable/testcases/test_FUNCCALL0_driver.c | 11 ++++++++ 9 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 test_deliverable/testcases/test_EMPTY.c create mode 100644 test_deliverable/testcases/test_EMPTY_driver.c create mode 100644 test_deliverable/testcases/test_FUNCCALL0.c create mode 100644 test_deliverable/testcases/test_FUNCCALL0_driver.c diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index 1e7c557..dd3e186 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -69,6 +69,7 @@ public: PostfixArrayElement(Expression *postfix_expression, Expression *index_expression); virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; + virtual void expressionDepth(unsigned &depth_count) const; virtual void stackPosition(VariableStackBindings bindings) const; }; @@ -83,6 +84,7 @@ public: virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; virtual void countArguments(unsigned &argument_count) const; + virtual void expressionDepth(unsigned &depth_count) const; void setPostfixExpression(Expression *postfix_expression); }; @@ -136,6 +138,7 @@ public: CastExpression(Type *type, Expression *expression); virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; + virtual void expressionDepth(unsigned &depth_count) const; }; class AdditiveExpression : public OperationExpression diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp index 38e1898..86b81af 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -29,8 +29,7 @@ public: virtual void printXml() const; virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const; - void printParameterAsm(VariableStackBindings& bindings, unsigned& stack_offset, - unsigned& frame_offset) const; + void printParameterAsm(VariableStackBindings& bindings, unsigned& frame_offset) const; void countParameters(unsigned& parameter_count) const; }; diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index c2086fd..e899467 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -270,6 +270,7 @@ ExpressionStatement: ; JumpStatement: T_RETURN Expression T_SC { $$ = new ReturnStatement($2); } + | T_RETURN T_SC { $$ = new ReturnStatement(); } | T_BREAK T_SC { $$ = new BreakStatement(); } | T_CONTINUE T_SC { $$ = new ContinueStatement(); } | T_GOTO T_IDENTIFIER { $$ = new GotoStatement(*$2); } diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 4c00c94..98afad6 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -28,8 +28,11 @@ void Expression::countArguments(unsigned &argument_count) const (void)argument_count; } -void Expression::expressionDepth(unsigned &) const -{} +void Expression::expressionDepth(unsigned &depth_count) const +{ + if(next_expression_ != nullptr) + next_expression_->expressionDepth(depth_count); +} std::string Expression::id() const { @@ -131,6 +134,15 @@ void PostfixArrayElement::stackPosition(VariableStackBindings bindings) const } +void PostfixArrayElement::expressionDepth(unsigned &depth_count) const +{ + if(nextExpression() != nullptr) + nextExpression()->expressionDepth(depth_count); + + if(index_expression_ != nullptr) + index_expression_->expressionDepth(depth_count); +} + // PostfixFunctionCall @@ -186,6 +198,12 @@ void PostfixFunctionCall::setPostfixExpression(Expression *postfix_expression) postfix_expression_ = expression_ptr; } +void PostfixFunctionCall::expressionDepth(unsigned &depth_count) const +{ + if(argument_expression_list_ != nullptr) + argument_expression_list_->expressionDepth(depth_count); +} + // Post increment and decrement definition @@ -301,6 +319,14 @@ VariableStackBindings CastExpression::printAsm(VariableStackBindings bindings, u return bindings; } +void CastExpression::expressionDepth(unsigned &depth_count) const +{ + if(nextExpression() != nullptr) + nextExpression()->expressionDepth(depth_count); + + expression_->expressionDepth(depth_count); +} + // Additive Expression definition diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index a44347e..71a4f3f 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -69,7 +69,7 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings, unsigne countParameters(parameter_count); // This adds 2 to store the frame pointer and the return address - unsigned memory_needed = 4*(variable_count+max_argument_count+max_depth+parameter_count+2); + unsigned memory_needed = 4*(variable_count+max_argument_count+max_depth+2); // make frame double word aligned if(memory_needed % 8 != 0) @@ -80,10 +80,10 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings, unsigne printf("$31,%d($sp)\n\tsw\t$fp,%d($sp)\n\tmove\t$fp,$sp\n", memory_needed-4, memory_needed-8); // set the stack counter to the right value - bindings.setStackPosition((max_argument_count+parameter_count)*4); - bindings.setExpressionStackPosition((max_argument_count+parameter_count+variable_count)*4); + bindings.setStackPosition(max_argument_count*4); + bindings.setExpressionStackPosition((max_argument_count+variable_count)*4); - printParameterAsm(bindings, max_argument_count, memory_needed); + printParameterAsm(bindings, memory_needed); // Prints the asm for the compound statement in the function statement_->printAsm(bindings, label_count); @@ -94,8 +94,7 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings, unsigne return original_bindings; } -void Function::printParameterAsm(VariableStackBindings& bindings, unsigned& stack_offset, - unsigned& frame_offset) const +void Function::printParameterAsm(VariableStackBindings& bindings, unsigned& frame_offset) const { std::vector parameter_vector; DeclarationPtr parameter_list = parameter_list_; @@ -109,11 +108,9 @@ void Function::printParameterAsm(VariableStackBindings& bindings, unsigned& stac for(auto itr = parameter_vector.rbegin(); itr != parameter_vector.rend(); ++itr) { unsigned i = itr-parameter_vector.rbegin(); - bindings.insertBinding((*itr)->getId(), (*itr)->getType(), (i+stack_offset)*4); + bindings.insertBinding((*itr)->getId(), (*itr)->getType(), i*4+frame_offset); if(i < 4) - printf("\tsw\t$%d,%d($fp)\n", 4+i, (i+stack_offset)*4); - else - printf("\tlw\t$2,%d($fp)\n\tsw\t$2,%d($fp)\n", frame_offset+4*i, (i+stack_offset)*4); + printf("\tsw\t$%d,%d($fp)\n", 4+i, i*4+frame_offset); } } diff --git a/test_deliverable/testcases/test_EMPTY.c b/test_deliverable/testcases/test_EMPTY.c new file mode 100644 index 0000000..e37e8eb --- /dev/null +++ b/test_deliverable/testcases/test_EMPTY.c @@ -0,0 +1,7 @@ +void empty(int *a) +{ + ;;;;;;;;;;;;;;;;;;;;;;; + *a = 13; + ;;;;;;;;;;;;;;;;;;;;;;; + return; +} diff --git a/test_deliverable/testcases/test_EMPTY_driver.c b/test_deliverable/testcases/test_EMPTY_driver.c new file mode 100644 index 0000000..b23707a --- /dev/null +++ b/test_deliverable/testcases/test_EMPTY_driver.c @@ -0,0 +1,8 @@ +void empty(int *a); + +int main() +{ + int a = 39; + empty(&a); + return !( 13 == a ); +} diff --git a/test_deliverable/testcases/test_FUNCCALL0.c b/test_deliverable/testcases/test_FUNCCALL0.c new file mode 100644 index 0000000..a390817 --- /dev/null +++ b/test_deliverable/testcases/test_FUNCCALL0.c @@ -0,0 +1,6 @@ +int f(int, int, int, int, int); + +int g() +{ + return f(5, 3, 6, 4, 7); +} diff --git a/test_deliverable/testcases/test_FUNCCALL0_driver.c b/test_deliverable/testcases/test_FUNCCALL0_driver.c new file mode 100644 index 0000000..d13734e --- /dev/null +++ b/test_deliverable/testcases/test_FUNCCALL0_driver.c @@ -0,0 +1,11 @@ +int g(); + +int main() +{ + return !( 25 == g() ); +} + +int f(int a, int b, int c, int d, int e) +{ + return a+b+c+d+e; +} -- cgit