From fd25256a37696de23d8f6c99827a97b63733845d Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 12 Mar 2017 11:08:03 +0000 Subject: Have to improve reg allocation --- c_compiler/include/bindings.hpp | 5 +++++ c_compiler/include/expression.hpp | 12 ++++++++++++ c_compiler/src/bindings.cpp | 24 +++++++++++++++++++++++- c_compiler/src/c_lexer.flex | 6 +++--- c_compiler/src/c_parser.y | 2 +- c_compiler/src/expression.cpp | 33 ++++++++++++++++++++++++++++++++- c_compiler/src/statement.cpp | 10 ++++++++-- c_compiler/test/in/Add.c | 2 ++ c_compiler/test/in/AddMult.c | 10 ++++++++++ c_compiler/test/in/Mult.c | 9 +++++++++ 10 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 c_compiler/test/in/AddMult.c create mode 100644 c_compiler/test/in/Mult.c (limited to 'c_compiler') diff --git a/c_compiler/include/bindings.hpp b/c_compiler/include/bindings.hpp index 983a10d..5dd1e1f 100644 --- a/c_compiler/include/bindings.hpp +++ b/c_compiler/include/bindings.hpp @@ -25,16 +25,21 @@ class VariableStackBindings private: std::map bindings; int32_t stack_counter; + int8_t current_register; public: VariableStackBindings(); void insertBinding(std::string id, TypePtr type, int32_t stack_position); void increaseStackPosition(); + void resetRegister(); + void increaseRegister(); int32_t getCurrentStackPosition() const; int32_t getStackPosition(const std::string& id) const; + int8_t getCurrentRegister() const; + bool bindingExists(const std::string& id) const; }; diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index df95a1e..a4939a9 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -54,6 +54,18 @@ public: }; +class MultiplicativeExpression : public OperationExpression +{ +private: + std::string operation; + +public: + MultiplicativeExpression(Expression* _lhs, const std::string& _operation, Expression* _rhs); + + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; +}; + + class Identifier : public Expression { private: diff --git a/c_compiler/src/bindings.cpp b/c_compiler/src/bindings.cpp index 4315407..80375e7 100644 --- a/c_compiler/src/bindings.cpp +++ b/c_compiler/src/bindings.cpp @@ -1,10 +1,12 @@ #include "bindings.hpp" +#include + // VariableStackBindings definition VariableStackBindings::VariableStackBindings() - : stack_counter(4) + : stack_counter(4), current_register(2) {} void VariableStackBindings::insertBinding(std::string id, TypePtr type, int32_t stack_position) @@ -21,6 +23,21 @@ void VariableStackBindings::increaseStackPosition() stack_counter += 4; } +void VariableStackBindings::resetRegister() +{ + current_register = 2; +} + +void VariableStackBindings::increaseRegister() +{ + if(current_register == 15) + current_register = 24; + else if(current_register == 25) + std::cerr << "Error : cannot allocate more registers" << std::endl; + else + current_register++; +} + int32_t VariableStackBindings::getCurrentStackPosition() const { return stack_counter; @@ -36,6 +53,11 @@ int32_t VariableStackBindings::getStackPosition(const std::string &id) const else return 0; } +int8_t VariableStackBindings::getCurrentRegister() const +{ + return current_register; +} + bool VariableStackBindings::bindingExists(const std::string &id) const { auto binding = bindings.find(id); diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index f6f2dd3..716e1ac 100644 --- a/c_compiler/src/c_lexer.flex +++ b/c_compiler/src/c_lexer.flex @@ -59,9 +59,9 @@ ALL . [!][=] { return T_EQUALITY_OP; } ([<>][=])|[<>] { return T_REL_OP; } [<>][<>] { return T_SHIFT_OP; } -[*] { return T_MULT; } -[\/] { return T_DIV; } -[%] { return T_REM; } +[*] { yylval.string = new std::string(yytext); return T_MULT; } +[\/] { yylval.string = new std::string(yytext); return T_DIV; } +[%] { yylval.string = new std::string(yytext); return T_REM; } [~] { return T_TILDE; } [!] { return T_NOT; } [.] { return T_DOT; } diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index f262f58..5a35c63 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -283,7 +283,7 @@ AdditiveExpression: MultiplicativeExpression: CastExpression { $$ = $1; } - | MultiplicativeExpression MultDivRemOP CastExpression { $$ = $3; } +| MultiplicativeExpression MultDivRemOP CastExpression { $$ = new MultiplicativeExpression($1, *$2, $3); delete $2; } ; MultDivRemOP: diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 6c1ba97..dea282f 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -69,7 +69,7 @@ VariableStackBindings AdditiveExpression::printasm(VariableStackBindings binding // currently using signed and sub because I only have signed numbers implemented // must update this as I add more types if(operation == "+") - std::cout << "\tadd\t$2,$2,$3" << std::endl; + std::cout << "\tadd\t$2,$3,$2" << std::endl; else if(operation == "-") std::cout << "\tsub\t$2,$3,$2" << std::endl; else @@ -79,6 +79,37 @@ VariableStackBindings AdditiveExpression::printasm(VariableStackBindings binding } +// Multiplicative Expression definition + + +MultiplicativeExpression::MultiplicativeExpression(Expression* _lhs, const std::string& _operation, Expression* _rhs) + : OperationExpression(_lhs, _rhs), operation(_operation) +{} + +VariableStackBindings MultiplicativeExpression::printasm(VariableStackBindings bindings) const +{ + lhs->printasm(bindings); + + std::cout << "\tmove\t$3,$2" << std::endl; + + rhs->printasm(bindings); + + // then perform the right operation + if(operation == "*") + std::cout << "\tmul\t$2,$3,$2" << std::endl; + else if(operation == "/" || operation == "%") { + std::cout << "\tdiv\t$3,$2" << std::endl; + if(operation == "/") + std::cout << "\tmflo\t$2" << std::endl; + else + std::cout << "\tmfhi\t$2" << std::endl; + } else + std::cerr << "Don't recognize symbol '" << operation << "'" << std::endl; + + return bindings; +} + + // Identifier definition Identifier::Identifier(const std::string& id) diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index fa85df0..f86329f 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -153,9 +153,12 @@ VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindin { if(next_statement != nullptr) next_statement->printasm(bindings); - + if(m_expr != nullptr) + { + bindings.resetRegister(); m_expr->printasm(bindings); + } return bindings; } @@ -188,8 +191,11 @@ VariableStackBindings JumpStatement::printasm(VariableStackBindings bindings) co next_statement->printasm(bindings); if(m_expr != nullptr) + { + bindings.resetRegister(); m_expr->printasm(bindings); - + } + return bindings; } diff --git a/c_compiler/test/in/Add.c b/c_compiler/test/in/Add.c index 43e557e..b672efb 100644 --- a/c_compiler/test/in/Add.c +++ b/c_compiler/test/in/Add.c @@ -8,6 +8,8 @@ int main() { z = x + y + x + y - x; } } + + z = 2 + z - 2; return z; } diff --git a/c_compiler/test/in/AddMult.c b/c_compiler/test/in/AddMult.c new file mode 100644 index 0000000..a8fa566 --- /dev/null +++ b/c_compiler/test/in/AddMult.c @@ -0,0 +1,10 @@ +int main() { + int x; + int a = 8; + int b = 5; + int c = 7; + int d = 3; + x = a * b + c * d; + + return x; +} diff --git a/c_compiler/test/in/Mult.c b/c_compiler/test/in/Mult.c new file mode 100644 index 0000000..4855b54 --- /dev/null +++ b/c_compiler/test/in/Mult.c @@ -0,0 +1,9 @@ +int main() { + int x = 4*2; + int y = 7; + int z = 14 % 8; + + z = x * y * z / z * x; + + return z; +} -- cgit