From e539805d39de73e25eeaa48a48730255c4ae695f Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 24 Mar 2017 17:32:19 +0000 Subject: Pointers working --- c_compiler/src/c_parser.y | 34 +++++++++------- c_compiler/src/compiler_main.cpp | 4 +- c_compiler/src/expression.cpp | 86 ++++++++++++++++++++++++++-------------- 3 files changed, 79 insertions(+), 45 deletions(-) (limited to 'c_compiler/src') diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 1bdfe12..c56b4b6 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -109,8 +109,8 @@ ParameterList: | ParameterList T_CMA Parameter { $3->linkDeclaration($$); $$ = $3; } ; -Parameter: DeclarationSpecifierList T_IDENTIFIER { $$ = new Declaration(*$2); delete $2; delete $1; } - | DeclarationSpecifierList {$$ = new Declaration(""); } +Parameter: DeclarationSpecifierList Declarator { $$ = new Declaration($2->getId()); delete $1; } + | DeclarationSpecifierList {$$ = new Declaration(""); } ; // Declaration @@ -179,24 +179,24 @@ InitDeclarator: Declarator { $$ = $1; } ; Declarator: DirectDeclarator { $$ = $1; } -| Pointer DirectDeclarator { $$ = $2; std::shared_ptr tmp($1); $$->setType(tmp); } + | Pointer DirectDeclarator { $$ = $2; std::shared_ptr tmp($1); $$->setType(tmp); } ; Pointer: -T_MULT { $$ = new Pointer(); delete $1; } -| T_MULT Pointer { $$ = $2; delete $1; } -| T_MULT TypeQualifierList Pointer { $$ = $3; delete $1; delete $2; } -; + T_MULT { $$ = new Pointer(); delete $1; } + | T_MULT Pointer { $$ = $2; delete $1; } + | T_MULT TypeQualifierList Pointer { $$ = $3; delete $1; delete $2; } + ; TypeQualifierList: -TypeQualifier { $$ = $1; } -| TypeQualifierList TypeQualifier { $$ = $2; delete $1; } -; + TypeQualifier { $$ = $1; } + | TypeQualifierList TypeQualifier { $$ = $2; delete $1; } + ; TypeQualifier: -T_CONST { $$ = new std::string("const"); } -| T_VOLATILE { $$ = new std::string("volatile"); } -; + T_CONST { $$ = new std::string("const"); } + | T_VOLATILE { $$ = new std::string("volatile"); } + ; DirectDeclarator: T_IDENTIFIER { $$ = new Declaration(*$1); delete $1; } @@ -417,7 +417,13 @@ UnaryOperator: T_AND { $$ = $1; } PostfixExpression: PrimaryExpression { $$ = $1; } | PostfixExpression T_LSB Expression T_RSB { $$ = new PostfixArrayElement(); } - | PostfixExpression T_LRB PostfixExpression2 { $$ = $3; $$->setPostfixExpression($1); } + | PostfixExpression T_LRB PostfixExpression2 + { + $$ = $3; + PostfixFunctionCall *tmp = dynamic_cast($$); + if(tmp != nullptr) + tmp->setPostfixExpression($1); + } | PostfixExpression T_DOT T_IDENTIFIER { $$ = $1; } | PostfixExpression T_ARROW T_IDENTIFIER { $$ = $1; } | PostfixExpression T_INCDEC diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index e9a12b9..4ac641c 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -15,9 +15,9 @@ int main(int, char**) unsigned label_count = 0; ast->printAsm(bindings, label_count); } - catch(std::string error_msg) + catch(const std::exception& e) { - fprintf(stderr, "%s\n", error_msg.c_str()); + fprintf(stderr, "%s\n", e.what()); } catch(...) { diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index b07a31e..af2687d 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -12,7 +12,12 @@ int Expression::constantFold() const } void Expression::print() const -{} +{ + if(next_expression_ != nullptr) + next_expression_->print(); + + printf("Expression\n"); +} void Expression::printXml() const {} @@ -26,18 +31,6 @@ void Expression::countArguments(unsigned &argument_count) const void Expression::expressionDepth(unsigned &) const {} -int Expression::postfixStackPosition(VariableStackBindings bindings) const -{ - (void)bindings; - throw std::runtime_error("Error : Can't call postfixStackExpression() on this type"); -} - -void Expression::setPostfixExpression(Expression *postfix_expression) -{ - // do nothing if expression isn't a postfix expression - (void)postfix_expression; -} - std::string Expression::id() const { // by default return empty id, which cannot be valid. @@ -68,7 +61,7 @@ OperationExpression::OperationExpression(ExpressionPtr lhs, Expression *rhs) int OperationExpression::constantFold() const { - throw std::runtime_error("Error : Cannot constant fold expression\n"); + throw std::runtime_error("Error : Cannot constant fold expression"); } void OperationExpression::expressionDepth(unsigned &depth_count) const @@ -113,6 +106,15 @@ void OperationExpression::evaluateExpression(VariableStackBindings bindings, uns lhs_stack_position, bindings.currentExpressionStackPosition()); } + +// Unary expression definition + +void UnaryExpression::stackPosition(VariableStackBindings) const +{ + throw std::runtime_error("Error : Cannot get stack position of expression"); +} + + // PostfixArrayElement PostfixArrayElement::PostfixArrayElement() @@ -189,14 +191,19 @@ VariableStackBindings PostfixPostIncDecExpression::printAsm(VariableStackBinding { postfix_expression_->printAsm(bindings, label_count); if(operator_ == "++") - printf("\taddi\t$3,$2,1\n"); + printf("\taddiu\t$3,$2,1\n"); else if(operator_ == "--") - printf("\tsubi\t$3,$2,1\n"); + printf("\tsubiu\t$3,$2,1\n"); else throw std::runtime_error("Error : '"+operator_+"' not recognized"); - printf("\tsw\t$2,%d($fp)\n\tsw\t$3,%d($fp)\n", bindings.currentExpressionStackPosition(), - postfix_expression_->postfixStackPosition(bindings)); + std::shared_ptr unary_expression; + unary_expression = std::static_pointer_cast(postfix_expression_); + + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + unary_expression->stackPosition(bindings); + printf("\tsw\t$3,0($t0)\n"); + return bindings; } @@ -216,9 +223,13 @@ VariableStackBindings UnaryPreIncDecExpression::printAsm(VariableStackBindings b printf("\tsubi\t$2,$2,1\n"); else throw std::runtime_error("Error : '"+operator_+"' not recognized"); - - printf("\tsw\t$2,%d($fp)\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition(), - unary_expression_->postfixStackPosition(bindings)); + + std::shared_ptr unary_expression; + unary_expression = std::static_pointer_cast(unary_expression_); + + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + unary_expression->stackPosition(bindings); + printf("\tsw\t$2,0($t0)\n"); return bindings; } @@ -242,7 +253,10 @@ VariableStackBindings OperatorUnaryExpression::printAsm(VariableStackBindings bi } else if(operator_ == "&") { - printf("\taddiu\t$2,$fp,%d\n", cast_expression_->postfixStackPosition(bindings)); + std::shared_ptr unary_expression; + unary_expression = std::static_pointer_cast(cast_expression_); + unary_expression->stackPosition(bindings); + printf("\tmove\t$2,$t0\n"); } else if(operator_ == "-") { @@ -258,6 +272,17 @@ VariableStackBindings OperatorUnaryExpression::printAsm(VariableStackBindings bi return bindings; } +void OperatorUnaryExpression::stackPosition(VariableStackBindings bindings) const +{ + if(operator_ == "*") + { + std::shared_ptr unary_expression; + unary_expression = std::static_pointer_cast(cast_expression_); + unary_expression->stackPosition(bindings); + printf("\tlw\t$t0,0($t0)\n"); + } +} + // CastExpression definition @@ -284,9 +309,9 @@ VariableStackBindings AdditiveExpression::printAsm(VariableStackBindings binding // TODO currently using signed and sub because I only have signed numbers implemented // must update this as I add more types if(operator_ == "+") - printf("\tadd\t$2,$2,$3\n"); + printf("\taddu\t$2,$2,$3\n"); else if(operator_ == "-") - printf("\tsub\t$2,$2,$3\n"); + printf("\tsubu\t$2,$2,$3\n"); else throw std::runtime_error("Error : '"+operator_+"' not recognized"); @@ -615,7 +640,8 @@ VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindi // TODO add stack and store results in there, also for addition and multiplication. // get the current location of lhs in the stack so that I can store result there - int store_stack_position = lhs_->postfixStackPosition(bindings); + std::shared_ptr lhs_postfix; + lhs_postfix = std::static_pointer_cast(lhs_); // get the current available stack position int expression_stack_position = bindings.currentExpressionStackPosition(); @@ -628,7 +654,8 @@ VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindi printf("\tlw\t$2,%d($fp)\n", expression_stack_position); // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways - printf("\tsw\t$2,%d($fp)\n", store_stack_position); + lhs_postfix->stackPosition(bindings); + printf("\tsw\t$2,0($t0)\n"); return bindings; } @@ -664,14 +691,15 @@ VariableStackBindings Identifier::printAsm(VariableStackBindings bindings, unsig return bindings; } -int Identifier::postfixStackPosition(VariableStackBindings bindings) const +void Identifier::stackPosition(VariableStackBindings bindings) const { if(bindings.bindingExists(id_)) { - return bindings.stackPosition(id_); + printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); + return; } - throw std::runtime_error("Error : Variable '"+id_+"' not yet declared"); + throw std::runtime_error("Error : '"+id_+"' not yet declared"); } std::string Identifier::id() const -- cgit