From 196e84b11515d10fb4023a7d5975b2cd0b2d1271 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 6 May 2017 15:59:20 +0100 Subject: Fixed decrement --- c_compiler/include/bindings.hpp | 60 +- c_compiler/include/declaration.hpp | 84 +- c_compiler/include/expression.hpp | 266 ++--- c_compiler/include/function.hpp | 20 +- c_compiler/include/node.hpp | 8 +- c_compiler/include/statement.hpp | 222 ++-- c_compiler/include/translation_unit.hpp | 12 +- c_compiler/include/type.hpp | 316 +++--- c_compiler/src/bindings.cpp | 116 +-- c_compiler/src/compiler_main.cpp | 32 +- c_compiler/src/declaration.cpp | 260 ++--- c_compiler/src/expression.cpp | 1056 ++++++++++---------- c_compiler/src/function.cpp | 188 ++-- c_compiler/src/statement.cpp | 788 +++++++-------- c_compiler/src/translation_unit.cpp | 30 +- c_compiler/src/type.cpp | 216 ++-- test_deliverable/testcases/test_QUICKSORT.c | 31 + test_deliverable/testcases/test_QUICKSORT_driver.c | 9 + 18 files changed, 1877 insertions(+), 1837 deletions(-) create mode 100644 test_deliverable/testcases/test_QUICKSORT.c create mode 100644 test_deliverable/testcases/test_QUICKSORT_driver.c diff --git a/c_compiler/include/bindings.hpp b/c_compiler/include/bindings.hpp index a8d8eba..7c2a264 100644 --- a/c_compiler/include/bindings.hpp +++ b/c_compiler/include/bindings.hpp @@ -13,50 +13,50 @@ typedef std::shared_ptr TypePtr; // struct containing information on the variable declaration struct DeclarationData { - TypePtr type; - int stack_position; - std::vector array_sizes; + TypePtr type; + int stack_position; + std::vector array_sizes; }; // stores bindings for the current scope and where they are in the stack class Bindings { private: - static std::vector string_literals; + static std::vector string_literals; - std::unordered_map bindings_; - std::string break_label_; - std::string continue_label_; - int stack_counter_; - int expression_stack_; + std::unordered_map bindings_; + std::string break_label_; + std::string continue_label_; + int stack_counter_; + int expression_stack_; public: - Bindings(); + Bindings(); - void insertBinding(const std::string &id, const TypePtr &type, const int &stack_position); - void insertBinding(const std::string &id, const TypePtr &type, const int &stack_position, const std::vector array_sizes); - int insertStringLiteral(const std::string &string_literal); - void increaseStackPosition(); - void increaseStackPosition(const int &position); - void setStackPosition(const int &stack_counter); - void nextExpressionStackPosition(); - void setExpressionStackPosition(const int &stack_counter); + void insertBinding(const std::string &id, const TypePtr &type, const int &stack_position); + void insertBinding(const std::string &id, const TypePtr &type, const int &stack_position, const std::vector array_sizes); + int insertStringLiteral(const std::string &string_literal); + void increaseStackPosition(); + void increaseStackPosition(const int &position); + void setStackPosition(const int &stack_counter); + void nextExpressionStackPosition(); + void setExpressionStackPosition(const int &stack_counter); - TypePtr getType(const std::string &id) const; - const std::vector &getArraySizes(const std::string &id) const; + TypePtr getType(const std::string &id) const; + const std::vector &getArraySizes(const std::string &id) const; - std::string breakLabel(); - std::string breakLabel(const std::string &label); - std::string continueLabel(); - std::string continueLabel(const std::string &label); + std::string breakLabel(); + std::string breakLabel(const std::string &label); + std::string continueLabel(); + std::string continueLabel(const std::string &label); - int currentStackPosition() const; - int stackPosition(const std::string &id) const; - int currentExpressionStackPosition() const; + int currentStackPosition() const; + int stackPosition(const std::string &id) const; + int currentExpressionStackPosition() const; - std::pair::const_iterator, std::vector::const_iterator> - getStringLiteralIterator() const; + std::pair::const_iterator, std::vector::const_iterator> + getStringLiteralIterator() const; - bool bindingExists(const std::string &id) const; + bool bindingExists(const std::string &id) const; }; diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index ef45737..752bece 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -12,66 +12,66 @@ typedef std::shared_ptr DeclarationPtr; class Declaration : public Node { protected: - DeclarationPtr next_declaration_; - DeclarationPtr next_list_declaration_; - ExpressionPtr initializer_; - TypePtr type_; - bool extern_declaration_; + DeclarationPtr next_declaration_; + DeclarationPtr next_list_declaration_; + ExpressionPtr initializer_; + TypePtr type_; + bool extern_declaration_; public: - Declaration(Expression *initializer); - Declaration(ExpressionPtr initializer); + Declaration(Expression *initializer); + Declaration(ExpressionPtr initializer); - virtual void print() const = 0; - virtual void printXml() const = 0; - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - virtual Bindings localAsm(Bindings bindings, int &label_count) const = 0; - virtual void countDeclarations(int &declaration_count) const = 0; - virtual std::string getId() const = 0; + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + virtual Bindings localAsm(Bindings bindings, int &label_count) const = 0; + virtual void countDeclarations(int &declaration_count) const = 0; + virtual std::string getId() const = 0; - void linkDeclaration(Declaration *next_declaration); - void linkListDeclaration(Declaration *next_list_declaration); - void setType(TypePtr type); - void setInitializer(Expression *initializer); - void setExternDeclaration(bool is_extern); + void linkDeclaration(Declaration *next_declaration); + void linkListDeclaration(Declaration *next_list_declaration); + void setType(TypePtr type); + void setInitializer(Expression *initializer); + void setExternDeclaration(bool is_extern); - DeclarationPtr getNext() const; - DeclarationPtr getNextListItem() const; - ExpressionPtr getInitializer() const; - TypePtr getType() const; + DeclarationPtr getNext() const; + DeclarationPtr getNextListItem() const; + ExpressionPtr getInitializer() const; + TypePtr getType() const; }; class IdentifierDeclaration : public Declaration { private: - std::string id_; + std::string id_; public: - IdentifierDeclaration(const std::string &id="", Expression *initializer=nullptr); - IdentifierDeclaration(const std::string &id, ExpressionPtr initializer); + IdentifierDeclaration(const std::string &id="", Expression *initializer=nullptr); + IdentifierDeclaration(const std::string &id, ExpressionPtr initializer); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual Bindings localAsm(Bindings bindings, int &label_count) const; - virtual void countDeclarations(int &declaration_count) const; - virtual std::string getId() const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual Bindings localAsm(Bindings bindings, int &label_count) const; + virtual void countDeclarations(int &declaration_count) const; + virtual std::string getId() const; }; class ArrayDeclaration : public Declaration { private: - int size_; - DeclarationPtr declarator_; + int size_; + DeclarationPtr declarator_; public: - ArrayDeclaration(Declaration *declarator, ExpressionPtr initializer, const int &size=0); + ArrayDeclaration(Declaration *declarator, ExpressionPtr initializer, const int &size=0); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual Bindings localAsm(Bindings bindings, int &label_count) const; - virtual void countDeclarations(int &declaration_count) const; - virtual std::string getId() const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual Bindings localAsm(Bindings bindings, int &label_count) const; + virtual void countDeclarations(int &declaration_count) const; + virtual std::string getId() const; - int getSize() const; - DeclarationPtr getNextArrayDeclaration() const; + int getSize() const; + DeclarationPtr getNextArrayDeclaration() const; }; #endif diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index b0f1733..d136bdf 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -15,320 +15,320 @@ typedef std::shared_ptr ExpressionPtr; class Expression : public Node { protected: - ExpressionPtr next_expression_; + ExpressionPtr next_expression_; public: - virtual Bindings printAsm(Bindings bindings, int& label_count) const = 0; + virtual Bindings printAsm(Bindings bindings, int& label_count) const = 0; - virtual int constantFold() const; - virtual void print() const; - virtual void printXml() const; - virtual void countArguments(int &argument_count) const; - virtual void expressionDepth(int &depth_count) const; - virtual std::string id() const; - virtual TypePtr getType(const Bindings &bindings) const = 0; + virtual int constantFold() const; + virtual void print() const; + virtual void printXml() const; + virtual void countArguments(int &argument_count) const; + virtual void expressionDepth(int &depth_count) const; + virtual std::string id() const; + virtual TypePtr getType(const Bindings &bindings) const = 0; - void linkExpression(Expression* next_expression); - ExpressionPtr nextExpression() const; + void linkExpression(Expression* next_expression); + ExpressionPtr nextExpression() const; }; class OperationExpression : public Expression { protected: - ExpressionPtr lhs_; - ExpressionPtr rhs_; + ExpressionPtr lhs_; + ExpressionPtr rhs_; public: - OperationExpression(Expression *lhs, Expression *rhs); - OperationExpression(ExpressionPtr lhs, Expression *rhs); + OperationExpression(Expression *lhs, Expression *rhs); + OperationExpression(ExpressionPtr lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - virtual int constantFold() const; - virtual void expressionDepth(int &depth_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual int constantFold() const; + virtual void expressionDepth(int &depth_count) const; + virtual TypePtr getType(const Bindings &bindings) const; - ExpressionPtr getLhs() const; - ExpressionPtr getRhs() const; + ExpressionPtr getLhs() const; + ExpressionPtr getRhs() const; - void evaluateExpression(Bindings bindings, int &label_count) const; + void evaluateExpression(Bindings bindings, int &label_count) const; }; class UnaryExpression : public Expression { public: - virtual void expressionDepth(int &depth_count) const; - virtual void pointerPosition(Bindings bindings) const; - virtual void stackPosition(Bindings bindings, int &depth_count) const; + virtual void expressionDepth(int &depth_count) const; + virtual void pointerPosition(Bindings bindings) const; + virtual void stackPosition(Bindings bindings, int &depth_count) const; }; class PostfixArrayElement : public UnaryExpression { private: - ExpressionPtr postfix_expression_; - ExpressionPtr index_expression_; + ExpressionPtr postfix_expression_; + ExpressionPtr index_expression_; public: - PostfixArrayElement(Expression *postfix_expression, Expression *index_expression); + PostfixArrayElement(Expression *postfix_expression, Expression *index_expression); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void expressionDepth(int &depth_count) const; - virtual void stackPosition(Bindings bindings, int &depth_count) const; - virtual TypePtr getType(const Bindings &bindings) const; - virtual std::string id() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void expressionDepth(int &depth_count) const; + virtual void stackPosition(Bindings bindings, int &depth_count) const; + virtual TypePtr getType(const Bindings &bindings) const; + virtual std::string id() const; - ExpressionPtr getIndex() const; - ExpressionPtr getPostfix() const; + ExpressionPtr getIndex() const; + ExpressionPtr getPostfix() const; }; class PostfixFunctionCall : public UnaryExpression { private: - ExpressionPtr postfix_expression_; - ExpressionPtr argument_expression_list_; + ExpressionPtr postfix_expression_; + ExpressionPtr argument_expression_list_; public: - PostfixFunctionCall(Expression *argument_expression_list = nullptr); + PostfixFunctionCall(Expression *argument_expression_list = nullptr); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void countArguments(int &argument_count) const; - virtual void expressionDepth(int &depth_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void countArguments(int &argument_count) const; + virtual void expressionDepth(int &depth_count) const; + virtual TypePtr getType(const Bindings &bindings) const; - void setPostfixExpression(Expression *postfix_expression); + void setPostfixExpression(Expression *postfix_expression); }; class PostfixPostIncDecExpression : public UnaryExpression { private: - std::string operator_; - ExpressionPtr postfix_expression_; + std::string operator_; + ExpressionPtr postfix_expression_; public: - PostfixPostIncDecExpression(const std::string &_operator, Expression *postfix_expression); + PostfixPostIncDecExpression(const std::string &_operator, Expression *postfix_expression); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual TypePtr getType(const Bindings &bindings) const; }; class UnaryPreIncDecExpression : public UnaryExpression { private: - std::string operator_; - ExpressionPtr unary_expression_; + std::string operator_; + ExpressionPtr unary_expression_; public: - UnaryPreIncDecExpression(const std::string &_operator, Expression *unary_expression); + UnaryPreIncDecExpression(const std::string &_operator, Expression *unary_expression); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual TypePtr getType(const Bindings &bindings) const; }; class OperatorUnaryExpression : public UnaryExpression { private: - std::string operator_; - ExpressionPtr cast_expression_; + std::string operator_; + ExpressionPtr cast_expression_; public: - OperatorUnaryExpression(const std::string &_operator, Expression *cast_expression); + OperatorUnaryExpression(const std::string &_operator, Expression *cast_expression); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void stackPosition(Bindings bindings, int &depth_count) const; - virtual TypePtr getType(const Bindings &bindings) const; - std::string getOperator() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void stackPosition(Bindings bindings, int &depth_count) const; + virtual TypePtr getType(const Bindings &bindings) const; + std::string getOperator() const; }; class CastExpression : public Expression { private: - TypePtr type_; - ExpressionPtr expression_; + TypePtr type_; + ExpressionPtr expression_; public: - CastExpression(Type *type, Expression *expression); + CastExpression(Type *type, Expression *expression); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void expressionDepth(int &depth_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void expressionDepth(int &depth_count) const; + virtual TypePtr getType(const Bindings &bindings) const; }; class AdditiveExpression : public OperationExpression { private: - std::string operator_; + std::string operator_; public: - AdditiveExpression(Expression *lhs, const std::string &_operator, Expression *rhs); + AdditiveExpression(Expression *lhs, const std::string &_operator, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class MultiplicativeExpression : public OperationExpression { private: - std::string operator_; + std::string operator_; public: - MultiplicativeExpression(Expression *lhs, const std::string &_operator, Expression *rhs); + MultiplicativeExpression(Expression *lhs, const std::string &_operator, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class ShiftExpression : public OperationExpression { private: - std::string operator_; + std::string operator_; public: - ShiftExpression(Expression *lhs, const std::string &_operator, Expression *rhs); + ShiftExpression(Expression *lhs, const std::string &_operator, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class RelationalExpression : public OperationExpression { private: - std::string operator_; + std::string operator_; public: - RelationalExpression(Expression *lhs, const std::string &_operator, Expression *rhs); + RelationalExpression(Expression *lhs, const std::string &_operator, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class EqualityExpression : public OperationExpression { private: - std::string operator_; + std::string operator_; public: - EqualityExpression(Expression *lhs, const std::string &_operator, Expression *rhs); + EqualityExpression(Expression *lhs, const std::string &_operator, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class AndExpression : public OperationExpression { public: - AndExpression(Expression *lhs, Expression *rhs); + AndExpression(Expression *lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class ExclusiveOrExpression : public OperationExpression { public: - ExclusiveOrExpression(Expression *lhs, Expression *rhs); + ExclusiveOrExpression(Expression *lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class InclusiveOrExpression : public OperationExpression { public: - InclusiveOrExpression(Expression *lhs, Expression *rhs); + InclusiveOrExpression(Expression *lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class LogicalAndExpression : public OperationExpression { public: - LogicalAndExpression(Expression *lhs, Expression *rhs); + LogicalAndExpression(Expression *lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class LogicalOrExpression : public OperationExpression { public: - LogicalOrExpression(Expression *lhs, Expression *rhs); + LogicalOrExpression(Expression *lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; }; class ConditionalExpression : public Expression { private: - ExpressionPtr logical_or_; - ExpressionPtr expression_; - ExpressionPtr conditional_expression_; + ExpressionPtr logical_or_; + ExpressionPtr expression_; + ExpressionPtr conditional_expression_; public: - ConditionalExpression(Expression *logical_or, Expression *expression, - Expression *conditional_expression); + ConditionalExpression(Expression *logical_or, Expression *expression, + Expression *conditional_expression); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual TypePtr getType(const Bindings &bindings) const; }; class AssignmentExpression : public OperationExpression { public: - AssignmentExpression(Expression *lhs, Expression *rhs); - AssignmentExpression(ExpressionPtr lhs, Expression *rhs); + AssignmentExpression(Expression *lhs, Expression *rhs); + AssignmentExpression(ExpressionPtr lhs, Expression *rhs); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; }; class Identifier : public UnaryExpression { private: - std::string id_; + std::string id_; public: - Identifier(const std::string &id); + Identifier(const std::string &id); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void pointerPosition(Bindings bindings) const; - virtual void stackPosition(Bindings bindings, int &depth_count) const; - virtual std::string id() const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void pointerPosition(Bindings bindings) const; + virtual void stackPosition(Bindings bindings, int &depth_count) const; + virtual std::string id() const; + virtual TypePtr getType(const Bindings &bindings) const; }; class StringLiteral : public UnaryExpression { private: - std::string string_content_; + std::string string_content_; public: - StringLiteral(const std::string &string_content); + StringLiteral(const std::string &string_content); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual TypePtr getType(const Bindings &bindings) const; }; class Constant : public UnaryExpression { private: - int32_t constant_; + int32_t constant_; public: - Constant(const int32_t &constant); + Constant(const int32_t &constant); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual int constantFold() const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual int constantFold() const; + virtual TypePtr getType(const Bindings &bindings) const; }; class Initializer : public Expression { private: - ExpressionPtr next_initializer_; + ExpressionPtr next_initializer_; public: - Initializer(Expression *next_initializer); + Initializer(Expression *next_initializer); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr getType(const Bindings &bindings) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual TypePtr getType(const Bindings &bindings) const; - void printInitializerAsm(Bindings &bindings, int &label_count, int position, const std::vector &iteration_vector, const TypePtr &type) const; - ExpressionPtr getNext() const; + void printInitializerAsm(Bindings &bindings, int &label_count, int position, const std::vector &iteration_vector, const TypePtr &type) const; + ExpressionPtr getNext() const; }; #endif diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp index 2227f52..1c76749 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -17,20 +17,20 @@ typedef std::shared_ptr FunctionPtr; class Function : public Node { protected: - TypePtr type_; - std::string id_; - StatementPtr statement_; - DeclarationPtr parameter_list_; + TypePtr type_; + std::string id_; + StatementPtr statement_; + DeclarationPtr parameter_list_; public: - Function(const std::string& id, Statement* statement, DeclarationPtr parameter_list = nullptr); + Function(const std::string& id, Statement* statement, DeclarationPtr parameter_list = nullptr); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int& label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int& label_count) const; - void printParameterAsm(Bindings& bindings, int& frame_offset) const; - void countParameters(int& parameter_count) const; + void printParameterAsm(Bindings& bindings, int& frame_offset) const; + void countParameters(int& parameter_count) const; }; diff --git a/c_compiler/include/node.hpp b/c_compiler/include/node.hpp index 1612aaf..6945b21 100644 --- a/c_compiler/include/node.hpp +++ b/c_compiler/include/node.hpp @@ -13,11 +13,11 @@ class Bindings; class Node { public: - virtual ~Node() {} + virtual ~Node() {} - virtual void print() const = 0; - virtual void printXml() const = 0; - virtual Bindings printAsm(Bindings bindings, int& label_count) const = 0; + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual Bindings printAsm(Bindings bindings, int& label_count) const = 0; }; diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp index 94c1a36..a01307f 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -16,227 +16,227 @@ typedef std::shared_ptr StatementPtr; class Statement : public Node { protected: - StatementPtr next_statement_; + StatementPtr next_statement_; public: - Statement(Statement *statement = nullptr); + Statement(Statement *statement = nullptr); - virtual void print() const = 0; - virtual void printXml() const = 0; - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - virtual void countVariables(int &var_count) const = 0; - virtual void countArguments(int &argument_count) const = 0; - virtual void countExpressionDepth(int &depth_count) const = 0; + virtual void countVariables(int &var_count) const = 0; + virtual void countArguments(int &argument_count) const = 0; + virtual void countExpressionDepth(int &depth_count) const = 0; - virtual int constantFold() const; - virtual ExpressionPtr getExpression() const; - virtual bool isDefault() const; + virtual int constantFold() const; + virtual ExpressionPtr getExpression() const; + virtual bool isDefault() const; - void linkStatement(Statement *next); - StatementPtr getNext() const; + void linkStatement(Statement *next); + StatementPtr getNext() const; }; class LabelStatement : public Statement { private: - std::string label_; - StatementPtr statement_; + std::string label_; + StatementPtr statement_; public: - LabelStatement(const std::string &label, Statement *statement); + LabelStatement(const std::string &label, Statement *statement); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class CaseStatement : public Statement { private: - ExpressionPtr constant_expression_; - StatementPtr statement_; - bool default_; + ExpressionPtr constant_expression_; + StatementPtr statement_; + bool default_; public: - CaseStatement(Statement *statement, Expression *constant_expression_ = nullptr, const bool &_default = false); - - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; - virtual int constantFold() const; - virtual ExpressionPtr getExpression() const; - virtual bool isDefault() const; + CaseStatement(Statement *statement, Expression *constant_expression_ = nullptr, const bool &_default = false); + + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; + virtual int constantFold() const; + virtual ExpressionPtr getExpression() const; + virtual bool isDefault() const; }; class CompoundStatement : public Statement { protected: - DeclarationPtr declaration_; - StatementPtr statement_; + DeclarationPtr declaration_; + StatementPtr statement_; public: - CompoundStatement(Declaration *declaration = nullptr, Statement *statement = nullptr); - CompoundStatement(Statement *statement); + CompoundStatement(Declaration *declaration = nullptr, Statement *statement = nullptr); + CompoundStatement(Statement *statement); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; - StatementPtr getStatementList() const; + StatementPtr getStatementList() const; }; class IfElseStatement : public Statement { private: - ExpressionPtr condition_; - StatementPtr if_; - StatementPtr else_; + ExpressionPtr condition_; + StatementPtr if_; + StatementPtr else_; public: - IfElseStatement(Expression *condition, Statement *_if, Statement *_else = nullptr); + IfElseStatement(Expression *condition, Statement *_if, Statement *_else = nullptr); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class SwitchStatement : public Statement { private: - ExpressionPtr condition_; - StatementPtr statement_; + ExpressionPtr condition_; + StatementPtr statement_; public: - SwitchStatement(Expression *condition, Statement *statement); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + SwitchStatement(Expression *condition, Statement *statement); + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class ExpressionStatement : public Statement { protected: - ExpressionPtr expression_; + ExpressionPtr expression_; public: - ExpressionStatement(Expression *expression = nullptr); + ExpressionStatement(Expression *expression = nullptr); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class JumpStatement : public Statement { public: - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - virtual void print() const; - virtual void printXml() const; + virtual void print() const; + virtual void printXml() const; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class ReturnStatement : public JumpStatement { private: - ExpressionPtr expression_; + ExpressionPtr expression_; public: - ReturnStatement(Expression *expression = nullptr); + ReturnStatement(Expression *expression = nullptr); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class BreakStatement : public JumpStatement { public: - BreakStatement(); + BreakStatement(); - virtual Bindings printAsm(Bindings bindings, int &) const; + virtual Bindings printAsm(Bindings bindings, int &) const; }; class ContinueStatement : public JumpStatement { public: - ContinueStatement(); + ContinueStatement(); - virtual Bindings printAsm(Bindings bindings, int &) const; + virtual Bindings printAsm(Bindings bindings, int &) const; }; class GotoStatement : public JumpStatement { private: - std::string label_; + std::string label_; public: - GotoStatement(const std::string &label); + GotoStatement(const std::string &label); - virtual Bindings printAsm(Bindings bindings, int &) const; + virtual Bindings printAsm(Bindings bindings, int &) const; }; class IterationStatement : public Statement { protected: - ExpressionPtr condition_; - StatementPtr statement_; + ExpressionPtr condition_; + StatementPtr statement_; public: - IterationStatement(Expression *condition, Statement *statement); + IterationStatement(Expression *condition, Statement *statement); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - virtual void countVariables(int &var_count) const; - virtual void countArguments(int &argument_count) const; - virtual void countExpressionDepth(int &depth_count) const; + virtual void countVariables(int &var_count) const; + virtual void countArguments(int &argument_count) const; + virtual void countExpressionDepth(int &depth_count) const; }; class WhileLoop : public IterationStatement { private: - bool is_while_; + bool is_while_; public: - WhileLoop(Expression *condition, Statement *statement, const bool &is_while = true); + WhileLoop(Expression *condition, Statement *statement, const bool &is_while = true); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; }; class ForLoop : public IterationStatement { private: - ExpressionPtr initializer_; - ExpressionPtr incrementer_; + ExpressionPtr initializer_; + ExpressionPtr incrementer_; public: - ForLoop(Expression *initializer, Expression *condition, Expression *incrementer, Statement *statement); + ForLoop(Expression *initializer, Expression *condition, Expression *incrementer, Statement *statement); - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; }; diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp index 04a9d16..c554f91 100644 --- a/c_compiler/include/translation_unit.hpp +++ b/c_compiler/include/translation_unit.hpp @@ -12,16 +12,16 @@ typedef std::shared_ptr NodePtr; class TranslationUnit : public Node { protected: - std::vector translation_unit_; + std::vector translation_unit_; public: - TranslationUnit(Node* external_declaration); + TranslationUnit(Node* external_declaration); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int& label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int& label_count) const; - void push(Node* external_declaration); + void push(Node* external_declaration); }; diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp index 7da2100..e0fa4d9 100644 --- a/c_compiler/include/type.hpp +++ b/c_compiler/include/type.hpp @@ -13,223 +13,223 @@ typedef std::shared_ptr TypePtr; class Type : public Node { public: - virtual void print() const = 0; - virtual void printXml() const = 0; - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - virtual TypePtr type() = 0; - virtual TypePtr type(Type *type_ptr) = 0; - virtual TypePtr type(TypePtr type_ptr) = 0; - - virtual void increaseStackPosition(Bindings &bindings) const = 0; - virtual void load() const = 0; - virtual void load(const int ®, const int &position) const = 0; - virtual void store() const = 0; - virtual void store(const int &position) const = 0; - virtual void store(const int ®, const int &position) const = 0; - virtual int getSize() const = 0; + virtual TypePtr type() = 0; + virtual TypePtr type(Type *type_ptr) = 0; + virtual TypePtr type(TypePtr type_ptr) = 0; + + virtual void increaseStackPosition(Bindings &bindings) const = 0; + virtual void load() const = 0; + virtual void load(const int ®, const int &position) const = 0; + virtual void store() const = 0; + virtual void store(const int &position) const = 0; + virtual void store(const int ®, const int &position) const = 0; + virtual int getSize() const = 0; - virtual void setSigned(bool _signed); - virtual void setExtern(bool _extern); - virtual void setStatic(bool _static); - virtual void setConst(bool _const); - virtual void setSize(int size); + virtual void setSigned(bool _signed); + virtual void setExtern(bool _extern); + virtual void setStatic(bool _static); + virtual void setConst(bool _const); + virtual void setSize(int size); }; class Array : public Type { private: - int size_; - TypePtr type_; + int size_; + TypePtr type_; public: - Array(const int &size, TypePtr type_ = nullptr); + Array(const int &size, TypePtr type_ = nullptr); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr type(); - virtual TypePtr type(Type *type_ptr); - virtual TypePtr type(TypePtr type_ptr); - - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); + + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Pointer : public Type { private: - TypePtr type_; + TypePtr type_; public: - Pointer(); + Pointer(); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr type(); - virtual TypePtr type(Type *type_ptr); - virtual TypePtr type(TypePtr type_ptr); - - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; - - void pointerLoad() const; - void pointerLoad(const int ®, const int &position) const; - void pointerStore() const; - void pointerStore(const int &position) const; - void pointerStore(const int ®, const int &position) const; + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); + + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; + + void pointerLoad() const; + void pointerLoad(const int ®, const int &position) const; + void pointerStore() const; + void pointerStore(const int &position) const; + void pointerStore(const int ®, const int &position) const; }; class TypeContainer : public Type { protected: - TypePtr type_; - int size_; - bool extern_; - bool static_; - bool const_; - bool signed_; + TypePtr type_; + int size_; + bool extern_; + bool static_; + bool const_; + bool signed_; public: - TypeContainer(); + TypeContainer(); - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual TypePtr type(); - virtual TypePtr type(Type *type_ptr); - virtual TypePtr type(TypePtr type_ptr); - - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); + + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; - virtual void setSigned(bool _signed); - virtual void setExtern(bool _extern); - virtual void setStatic(bool _static); - virtual void setConst(bool _const); - virtual void setSize(int size); + virtual void setSigned(bool _signed); + virtual void setExtern(bool _extern); + virtual void setStatic(bool _static); + virtual void setConst(bool _const); + virtual void setSize(int size); }; class Specifier : public Type { public: - virtual void print() const = 0; - virtual void printXml() const = 0; - virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; - - virtual void increaseStackPosition(Bindings &bindings) const = 0; - virtual void load() const = 0; - virtual void load(const int ®, const int &position) const = 0; - virtual void store() const = 0; - virtual void store(const int &position) const = 0; - virtual void store(const int ®, const int &position) const = 0; - virtual int getSize() const = 0; + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual Bindings printAsm(Bindings bindings, int &label_count) const = 0; + + virtual void increaseStackPosition(Bindings &bindings) const = 0; + virtual void load() const = 0; + virtual void load(const int ®, const int &position) const = 0; + virtual void store() const = 0; + virtual void store(const int &position) const = 0; + virtual void store(const int ®, const int &position) const = 0; + virtual int getSize() const = 0; - virtual TypePtr type(); - virtual TypePtr type(Type *type_ptr); - virtual TypePtr type(TypePtr type_ptr); + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); }; class Int : public Specifier { public: - Int(); - - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + Int(); + + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Short : public Specifier { public: - Short(); - - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + Short(); + + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Void : public Specifier { public: - Void(); - - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + Void(); + + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Char : public Specifier { public: - Char(); - - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + Char(); + + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Float : public Specifier { public: - Float(); - - virtual void print() const; - virtual void printXml() const; - virtual Bindings printAsm(Bindings bindings, int &label_count) const; - virtual void increaseStackPosition(Bindings &bindings) const; - virtual void load() const; - virtual void load(const int ®, const int &position) const; - virtual void store() const; - virtual void store(const int &position) const; - virtual void store(const int ®, const int &position) const; - virtual int getSize() const; + Float(); + + virtual void print() const; + virtual void printXml() const; + virtual Bindings printAsm(Bindings bindings, int &label_count) const; + virtual void increaseStackPosition(Bindings &bindings) const; + virtual void load() const; + virtual void load(const int ®, const int &position) const; + virtual void store() const; + virtual void store(const int &position) const; + virtual void store(const int ®, const int &position) const; + virtual int getSize() const; }; #endif diff --git a/c_compiler/src/bindings.cpp b/c_compiler/src/bindings.cpp index 7deda13..e6cd027 100644 --- a/c_compiler/src/bindings.cpp +++ b/c_compiler/src/bindings.cpp @@ -5,145 +5,145 @@ std::vector Bindings::string_literals; // Bindings definition Bindings::Bindings() - : break_label_(""), continue_label_(""), stack_counter_(0), expression_stack_(16) + : break_label_(""), continue_label_(""), stack_counter_(0), expression_stack_(16) {} void Bindings::insertBinding(const std::string &id,const TypePtr &type, const int &stack_position) { - auto binding = bindings_.find(id); + auto binding = bindings_.find(id); - if(binding == bindings_.end()) - { - DeclarationData decl_data; - decl_data.type = type; - decl_data.stack_position = stack_position; - bindings_.insert(std::make_pair(id, decl_data)); - } - else - { - (*binding).second.stack_position = stack_position; - (*binding).second.type = type; - } + if(binding == bindings_.end()) + { + DeclarationData decl_data; + decl_data.type = type; + decl_data.stack_position = stack_position; + bindings_.insert(std::make_pair(id, decl_data)); + } + else + { + (*binding).second.stack_position = stack_position; + (*binding).second.type = type; + } } void Bindings::insertBinding(const std::string &id, const TypePtr &type, const int &stack_position, const std::vector array_sizes) { - auto binding = bindings_.find(id); + auto binding = bindings_.find(id); - if(binding == bindings_.end()) - { - DeclarationData decl_data; - decl_data.type = type; - decl_data.stack_position = stack_position; - decl_data.array_sizes = array_sizes; - bindings_.insert(std::make_pair(id, decl_data)); - } - else - { - (*binding).second.stack_position = stack_position; - (*binding).second.type = type; - (*binding).second.array_sizes = array_sizes; - } + if(binding == bindings_.end()) + { + DeclarationData decl_data; + decl_data.type = type; + decl_data.stack_position = stack_position; + decl_data.array_sizes = array_sizes; + bindings_.insert(std::make_pair(id, decl_data)); + } + else + { + (*binding).second.stack_position = stack_position; + (*binding).second.type = type; + (*binding).second.array_sizes = array_sizes; + } } int Bindings::insertStringLiteral(const std::string &string_literal) { - string_literals.push_back(string_literal); - return (int)string_literals.size()-1; + string_literals.push_back(string_literal); + return (int)string_literals.size()-1; } void Bindings::increaseStackPosition() { - stack_counter_ += 4; + stack_counter_ += 4; } void Bindings::increaseStackPosition(const int &position) { - stack_counter_ += position; + stack_counter_ += position; } void Bindings::setStackPosition(const int &stack_counter) { - stack_counter_ = stack_counter; + stack_counter_ = stack_counter; } void Bindings::nextExpressionStackPosition() { - expression_stack_ += 4; + expression_stack_ += 4; } void Bindings::setExpressionStackPosition(const int &stack_counter) { - expression_stack_ = stack_counter; + expression_stack_ = stack_counter; } TypePtr Bindings::getType(const std::string &id) const { - auto binding = bindings_.find(id); - return (*binding).second.type; + auto binding = bindings_.find(id); + return (*binding).second.type; } const std::vector &Bindings::getArraySizes(const std::string &id) const { - auto binding = bindings_.find(id); - return (*binding).second.array_sizes; + auto binding = bindings_.find(id); + return (*binding).second.array_sizes; } std::string Bindings::breakLabel() { - return break_label_; + return break_label_; } std::string Bindings::breakLabel(const std::string &label) { - break_label_ = label; - return break_label_; + break_label_ = label; + return break_label_; } std::string Bindings::continueLabel() { - return continue_label_; + return continue_label_; } std::string Bindings::continueLabel(const std::string &label) { - continue_label_ = label; - return continue_label_; + continue_label_ = label; + return continue_label_; } int Bindings::currentStackPosition() const { - return stack_counter_; + return stack_counter_; } int Bindings::stackPosition(const std::string &id) const { - auto binding = bindings_.find(id); + auto binding = bindings_.find(id); - if(binding != bindings_.end()) - return (*binding).second.stack_position; + if(binding != bindings_.end()) + return (*binding).second.stack_position; - else return 0; + else return 0; } int Bindings::currentExpressionStackPosition() const { - return expression_stack_; + return expression_stack_; } std::pair::const_iterator, std::vector::const_iterator> Bindings::getStringLiteralIterator() const { - return std::make_pair(string_literals.begin(), string_literals.end()); + return std::make_pair(string_literals.begin(), string_literals.end()); } bool Bindings::bindingExists(const std::string &id) const { - auto binding = bindings_.find(id); + auto binding = bindings_.find(id); - if(binding == bindings_.end()) - return false; + if(binding == bindings_.end()) + return false; - else - return true; + else + return true; } diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index 4ed3bff..f9a8ec5 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -8,21 +8,21 @@ Node* parseAST(); int main(int, char**) { - try - { - std::unique_ptr ast(parseAST()); - Bindings bindings; - int label_count = 0; - ast->printAsm(bindings, label_count); - } - catch(const std::exception& e) - { - fprintf(stderr, "%s\n", e.what()); - } - catch(...) - { - fprintf(stderr, "Error : Exception thrown\n"); - } + try + { + std::unique_ptr ast(parseAST()); + Bindings bindings; + int label_count = 0; + ast->printAsm(bindings, label_count); + } + catch(const std::exception& e) + { + fprintf(stderr, "%s\n", e.what()); + } + catch(...) + { + fprintf(stderr, "Error : Exception thrown\n"); + } - return 0; + return 0; } diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 7c7791e..9918294 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -12,261 +12,261 @@ // Declaration definition Declaration::Declaration(Expression *initializer) - : initializer_(initializer), extern_declaration_(false) + : initializer_(initializer), extern_declaration_(false) {} Declaration::Declaration(ExpressionPtr initializer) - : initializer_(initializer), extern_declaration_(false) + : initializer_(initializer), extern_declaration_(false) {} void Declaration::linkDeclaration(Declaration* next_declaration) { - DeclarationPtr decl_ptr(next_declaration); - next_declaration_ = decl_ptr; + DeclarationPtr decl_ptr(next_declaration); + next_declaration_ = decl_ptr; } void Declaration::linkListDeclaration(Declaration* next_declaration) { - DeclarationPtr decl_ptr(next_declaration); - next_list_declaration_ = decl_ptr; + DeclarationPtr decl_ptr(next_declaration); + next_list_declaration_ = decl_ptr; } void Declaration::setType(TypePtr type) { - type_ = type; + type_ = type; } void Declaration::setInitializer(Expression* initializer) { - ExpressionPtr expression_ptr(initializer); - initializer_ = expression_ptr; + ExpressionPtr expression_ptr(initializer); + initializer_ = expression_ptr; } void Declaration::setExternDeclaration(bool is_extern) { - extern_declaration_ = is_extern; + extern_declaration_ = is_extern; } DeclarationPtr Declaration::getNext() const { - return next_declaration_; + return next_declaration_; } DeclarationPtr Declaration::getNextListItem() const { - return next_list_declaration_; + return next_list_declaration_; } ExpressionPtr Declaration::getInitializer() const { - return initializer_; + return initializer_; } TypePtr Declaration::getType() const { - return type_; + return type_; } // IdentifierDeclaration definition IdentifierDeclaration::IdentifierDeclaration(const std::string &id, Expression *initializer) - : Declaration(initializer), id_(id) + : Declaration(initializer), id_(id) {} IdentifierDeclaration::IdentifierDeclaration(const std::string &id, ExpressionPtr initializer) - : Declaration(initializer), id_(id) + : Declaration(initializer), id_(id) {} void IdentifierDeclaration::print() const { - if(next_declaration_ != nullptr) - next_declaration_->print(); + if(next_declaration_ != nullptr) + next_declaration_->print(); - if(id_ != "") - printf("%s\n", id_.c_str()); + if(id_ != "") + printf("%s\n", id_.c_str()); } void IdentifierDeclaration::printXml() const { - if(next_declaration_ != nullptr) - next_declaration_->printXml(); + if(next_declaration_ != nullptr) + next_declaration_->printXml(); - if(next_list_declaration_ != nullptr) - next_list_declaration_->printXml(); + if(next_list_declaration_ != nullptr) + next_list_declaration_->printXml(); - if(id_ != "") - printf("", id_.c_str()); + if(id_ != "") + printf("", id_.c_str()); } Bindings IdentifierDeclaration::printAsm(Bindings bindings, int& label_count) const { - (void)label_count; - if(!extern_declaration_) - { - if(initializer_ == nullptr) - printf("\t.comm\t%s,4,4\n", id_.c_str()); - else - printf("\t.data\n\t.globl\t%s\n%s:\n\t.word\t%d\n", - id_.c_str(), id_.c_str(), initializer_->constantFold()); - } + (void)label_count; + if(!extern_declaration_) + { + if(initializer_ == nullptr) + printf("\t.comm\t%s,4,4\n", id_.c_str()); + else + printf("\t.data\n\t.globl\t%s\n%s:\n\t.word\t%d\n", + id_.c_str(), id_.c_str(), initializer_->constantFold()); + } - bindings.insertBinding(id_, type_, -1); - return bindings; + bindings.insertBinding(id_, type_, -1); + return bindings; } Bindings IdentifierDeclaration::localAsm(Bindings bindings, int& label_count) const { - if(next_declaration_ != nullptr) - bindings = next_declaration_->localAsm(bindings, label_count); + if(next_declaration_ != nullptr) + bindings = next_declaration_->localAsm(bindings, label_count); - if(next_list_declaration_ != nullptr) - bindings = next_list_declaration_->localAsm(bindings, label_count); + if(next_list_declaration_ != nullptr) + bindings = next_list_declaration_->localAsm(bindings, label_count); - if(id_ != "") - { - int stack_position = bindings.currentStackPosition(); - if(initializer_ != nullptr) + if(id_ != "") { - initializer_->printAsm(bindings, label_count); - type_->store(stack_position); + int stack_position = bindings.currentStackPosition(); + if(initializer_ != nullptr) + { + initializer_->printAsm(bindings, label_count); + type_->store(stack_position); + } + bindings.insertBinding(id_, type_, stack_position); + type_->increaseStackPosition(bindings); } - bindings.insertBinding(id_, type_, stack_position); - type_->increaseStackPosition(bindings); - } - return bindings; + return bindings; } void IdentifierDeclaration::countDeclarations(int &declaration_count) const { - if(next_declaration_ != nullptr) - next_declaration_->countDeclarations(declaration_count); + if(next_declaration_ != nullptr) + next_declaration_->countDeclarations(declaration_count); - if(next_list_declaration_ != nullptr) - next_list_declaration_->countDeclarations(declaration_count); + if(next_list_declaration_ != nullptr) + next_list_declaration_->countDeclarations(declaration_count); - ++declaration_count; + ++declaration_count; } std::string IdentifierDeclaration::getId() const { - return id_; + return id_; } // Array declaration class ArrayDeclaration::ArrayDeclaration(Declaration *declarator, ExpressionPtr initializer, const int &size) - : Declaration(initializer), size_(size), declarator_(declarator) + : Declaration(initializer), size_(size), declarator_(declarator) {} void ArrayDeclaration::print() const { - if(next_declaration_ != nullptr) - next_declaration_->print(); - if(next_list_declaration_ != nullptr) - next_list_declaration_->print(); + if(next_declaration_ != nullptr) + next_declaration_->print(); + if(next_list_declaration_ != nullptr) + next_list_declaration_->print(); - printf("Array Declaration\n"); - declarator_->print(); + printf("Array Declaration\n"); + declarator_->print(); } void ArrayDeclaration::printXml() const { - if(next_declaration_ != nullptr) - next_declaration_->printXml(); - if(next_list_declaration_ != nullptr) - next_list_declaration_->printXml(); - declarator_->printXml(); + if(next_declaration_ != nullptr) + next_declaration_->printXml(); + if(next_list_declaration_ != nullptr) + next_list_declaration_->printXml(); + declarator_->printXml(); } Bindings ArrayDeclaration::printAsm(Bindings bindings, int &label_count) const { - if(next_declaration_ != nullptr) - bindings = next_declaration_->printAsm(bindings, label_count); - if(next_list_declaration_ != nullptr) - bindings = next_list_declaration_->printAsm(bindings, label_count); - bindings = declarator_->printAsm(bindings, label_count); + if(next_declaration_ != nullptr) + bindings = next_declaration_->printAsm(bindings, label_count); + if(next_list_declaration_ != nullptr) + bindings = next_list_declaration_->printAsm(bindings, label_count); + bindings = declarator_->printAsm(bindings, label_count); - return bindings; + return bindings; } Bindings ArrayDeclaration::localAsm(Bindings bindings, int &label_count) const { - if(next_declaration_ != nullptr) - bindings = next_declaration_->localAsm(bindings, label_count); - if(next_list_declaration_ != nullptr) - bindings = next_list_declaration_->localAsm(bindings, label_count); + if(next_declaration_ != nullptr) + bindings = next_declaration_->localAsm(bindings, label_count); + if(next_list_declaration_ != nullptr) + bindings = next_list_declaration_->localAsm(bindings, label_count); - if(getId() != "") - { - - int stack_position = bindings.currentStackPosition(); - std::shared_ptr array_declaration( - std::dynamic_pointer_cast(declarator_)); - std::vector array_sizes = { size_ }; - while(array_declaration != nullptr) + if(getId() != "") { - array_sizes.push_back(array_declaration->getSize()); - array_declaration = std::dynamic_pointer_cast - (array_declaration->getNextArrayDeclaration()); - } - - std::shared_ptr initializer; - if(initializer_ != nullptr) - { - initializer = std::static_pointer_cast(initializer_); - initializer->printInitializerAsm(bindings, label_count, array_sizes.size()-1, array_sizes, type_->type()); - } - else - { - int sum = 1; - std::for_each(array_sizes.begin(), array_sizes.end(), [&] (int n) { - sum *= n; - }); - sum *= getType()->getSize(); - bindings.increaseStackPosition(sum); + + int stack_position = bindings.currentStackPosition(); + std::shared_ptr array_declaration( + std::dynamic_pointer_cast(declarator_)); + std::vector array_sizes = { size_ }; + while(array_declaration != nullptr) + { + array_sizes.push_back(array_declaration->getSize()); + array_declaration = std::dynamic_pointer_cast + (array_declaration->getNextArrayDeclaration()); + } + + std::shared_ptr initializer; + if(initializer_ != nullptr) + { + initializer = std::static_pointer_cast(initializer_); + initializer->printInitializerAsm(bindings, label_count, array_sizes.size()-1, array_sizes, type_->type()); + } + else + { + int sum = 1; + std::for_each(array_sizes.begin(), array_sizes.end(), [&] (int n) { + sum *= n; + }); + sum *= getType()->getSize(); + bindings.increaseStackPosition(sum); + } + // reverse vector to store in binding + std::reverse(array_sizes.begin(), array_sizes.end()); + bindings.insertBinding(getId(), type_, stack_position, array_sizes); } - // reverse vector to store in binding - std::reverse(array_sizes.begin(), array_sizes.end()); - bindings.insertBinding(getId(), type_, stack_position, array_sizes); - } - return bindings; + return bindings; } void ArrayDeclaration::countDeclarations(int &declaration_count) const { - if(next_declaration_ != nullptr) - next_declaration_->countDeclarations(declaration_count); - if(next_list_declaration_ != nullptr) - next_list_declaration_->countDeclarations(declaration_count); - - std::shared_ptr array_declaration( - std::dynamic_pointer_cast(declarator_)); - int size = size_; - while(array_declaration != nullptr) - { - size *= array_declaration->getSize(); - array_declaration = std::dynamic_pointer_cast( - array_declaration->getNextArrayDeclaration()); - } - - declaration_count += size; + if(next_declaration_ != nullptr) + next_declaration_->countDeclarations(declaration_count); + if(next_list_declaration_ != nullptr) + next_list_declaration_->countDeclarations(declaration_count); + + std::shared_ptr array_declaration( + std::dynamic_pointer_cast(declarator_)); + int size = size_; + while(array_declaration != nullptr) + { + size *= array_declaration->getSize(); + array_declaration = std::dynamic_pointer_cast( + array_declaration->getNextArrayDeclaration()); + } + + declaration_count += size; } std::string ArrayDeclaration::getId() const { - return declarator_->getId(); + return declarator_->getId(); } int ArrayDeclaration::getSize() const { - return size_; + return size_; } DeclarationPtr ArrayDeclaration::getNextArrayDeclaration() const { - return declarator_; + return declarator_; } diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 0250206..775cdaa 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -9,15 +9,15 @@ int Expression::constantFold() const { - throw std::runtime_error("Error : Cannot constant fold this expression"); + throw std::runtime_error("Error : Cannot constant fold this expression"); } void Expression::print() const { - if(next_expression_ != nullptr) - next_expression_->print(); + if(next_expression_ != nullptr) + next_expression_->print(); - printf("Expression\n"); + printf("Expression\n"); } void Expression::printXml() const @@ -25,99 +25,99 @@ void Expression::printXml() const void Expression::countArguments(int &) const { - // by default don't do anything to the count + // by default don't do anything to the count } void Expression::expressionDepth(int &depth_count) const { - if(next_expression_ != nullptr) - next_expression_->expressionDepth(depth_count); + if(next_expression_ != nullptr) + next_expression_->expressionDepth(depth_count); } std::string Expression::id() const { - // by default return empty id, which cannot be valid. - return ""; + // by default return empty id, which cannot be valid. + return ""; } TypePtr Expression::getType(const Bindings &) const { - // by default return largest size, which is 32 bits - return std::make_shared(); + // by default return largest size, which is 32 bits + return std::make_shared(); } void Expression::linkExpression(Expression *next_expression) { - ExpressionPtr expression_ptr(next_expression); - next_expression_ = expression_ptr; + ExpressionPtr expression_ptr(next_expression); + next_expression_ = expression_ptr; } ExpressionPtr Expression::nextExpression() const { - return next_expression_; + return next_expression_; } // OperationExpression definition OperationExpression::OperationExpression(Expression *lhs, Expression *rhs) - : lhs_(lhs), rhs_(rhs) + : lhs_(lhs), rhs_(rhs) {} OperationExpression::OperationExpression(ExpressionPtr lhs, Expression *rhs) - : lhs_(lhs), rhs_(rhs) + : lhs_(lhs), rhs_(rhs) {} int OperationExpression::constantFold() const { - throw std::runtime_error("Error : Cannot constant fold expression"); + throw std::runtime_error("Error : Cannot constant fold expression"); } void OperationExpression::expressionDepth(int &depth_count) const { - int lhs_depth_count = depth_count; - int rhs_depth_count = depth_count+1; + int lhs_depth_count = depth_count; + int rhs_depth_count = depth_count+1; - lhs_->expressionDepth(lhs_depth_count); - rhs_->expressionDepth(rhs_depth_count); + lhs_->expressionDepth(lhs_depth_count); + rhs_->expressionDepth(rhs_depth_count); - if(lhs_depth_count > rhs_depth_count) - depth_count = lhs_depth_count; - else - depth_count = rhs_depth_count; + if(lhs_depth_count > rhs_depth_count) + depth_count = lhs_depth_count; + else + depth_count = rhs_depth_count; } TypePtr OperationExpression::getType(const Bindings &bindings) const { - return lhs_->getType(bindings); + return lhs_->getType(bindings); } ExpressionPtr OperationExpression::getLhs() const { - return lhs_; + return lhs_; } ExpressionPtr OperationExpression::getRhs() const { - return rhs_; + return rhs_; } void OperationExpression::evaluateExpression(Bindings bindings, int &label_count) const { - // I can just evaluate the lhs with the same entry stack position - lhs_->printAsm(bindings, label_count); + // I can just evaluate the lhs with the same entry stack position + lhs_->printAsm(bindings, label_count); - // store this stack position - int lhs_stack_position = bindings.currentExpressionStackPosition(); + // store this stack position + int lhs_stack_position = bindings.currentExpressionStackPosition(); - // now have to increase the expression stack position for the rhs - bindings.nextExpressionStackPosition(); - rhs_->printAsm(bindings, label_count); + // now have to increase the expression stack position for the rhs + bindings.nextExpressionStackPosition(); + rhs_->printAsm(bindings, label_count); - // now I have them evaluated at two positions in the stack and can load both into registers - // $2 and $3 - printf("\tlw\t$2,%d($fp)\n", lhs_stack_position); - printf("\tlw\t$3,%d($fp)\n", bindings.currentExpressionStackPosition()); + // now I have them evaluated at two positions in the stack and can load both into registers + // $2 and $3 + printf("\tlw\t$2,%d($fp)\n", lhs_stack_position); + printf("\tlw\t$3,%d($fp)\n", bindings.currentExpressionStackPosition()); } @@ -125,375 +125,375 @@ void OperationExpression::evaluateExpression(Bindings bindings, int &label_count void UnaryExpression::expressionDepth(int &depth_count) const { - ++depth_count; + ++depth_count; } void UnaryExpression::pointerPosition(Bindings bindings) const { - throw std::runtime_error("Error : Cannot get pointer position"); + throw std::runtime_error("Error : Cannot get pointer position"); } void UnaryExpression::stackPosition(Bindings, int &) const { - throw std::runtime_error("Error : Cannot get stack position of expression"); + throw std::runtime_error("Error : Cannot get stack position of expression"); } // PostfixArrayElement PostfixArrayElement::PostfixArrayElement(Expression *postfix_expression, Expression *index_expression) - : postfix_expression_(postfix_expression), index_expression_(index_expression) + : postfix_expression_(postfix_expression), index_expression_(index_expression) {} Bindings PostfixArrayElement::printAsm(Bindings bindings, int &label_count) const { - stackPosition(bindings, label_count); + stackPosition(bindings, label_count); - TypePtr type_ptr = postfix_expression_->getType(bindings); - std::shared_ptr pointer_type_ptr; - pointer_type_ptr = std::dynamic_pointer_cast(type_ptr); - if(pointer_type_ptr != nullptr) - pointer_type_ptr->pointerLoad(); - else - type_ptr->load(); - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + TypePtr type_ptr = postfix_expression_->getType(bindings); + std::shared_ptr pointer_type_ptr; + pointer_type_ptr = std::dynamic_pointer_cast(type_ptr); + if(pointer_type_ptr != nullptr) + pointer_type_ptr->pointerLoad(); + else + type_ptr->load(); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } void PostfixArrayElement::stackPosition(Bindings bindings, int &label_count) const { - std::shared_ptr array_element - (std::dynamic_pointer_cast(postfix_expression_)); + std::shared_ptr array_element + (std::dynamic_pointer_cast(postfix_expression_)); - std::vector array_sizes = bindings.getArraySizes(postfix_expression_->id()); + std::vector array_sizes = bindings.getArraySizes(postfix_expression_->id()); - int counter = 1; - printf("\tmove\t$t1,$0\n"); - index_expression_->printAsm(bindings, label_count); - printf("\taddu\t$t1,$t1,$2\n"); - while(array_element != nullptr) - { - array_element->getIndex()->printAsm(bindings, label_count); - int sum = 0; - std::for_each(array_sizes.end()-counter, array_sizes.end(), [&] (int n) { - sum += n; - }); - printf("\tmul\t$2,$2,%d\n", sum); - printf("\taddu\t$t1,$t1,$2\n"); - array_element = std::dynamic_pointer_cast(array_element->getPostfix()); - counter++; - } - auto identifier_expression = std::make_shared(postfix_expression_->id()); - identifier_expression->stackPosition(bindings, label_count); - printf("\tsll\t$t1,$t1,%d\n", postfix_expression_->getType(bindings)->getSize()/2); - printf("\taddu\t$t0,$t0,$t1\n"); + int counter = 1; + printf("\tmove\t$t1,$0\n"); + index_expression_->printAsm(bindings, label_count); + printf("\taddu\t$t1,$t1,$2\n"); + while(array_element != nullptr) + { + array_element->getIndex()->printAsm(bindings, label_count); + int sum = 0; + std::for_each(array_sizes.end()-counter, array_sizes.end(), [&] (int n) { + sum += n; + }); + printf("\tmul\t$2,$2,%d\n", sum); + printf("\taddu\t$t1,$t1,$2\n"); + array_element = std::dynamic_pointer_cast(array_element->getPostfix()); + counter++; + } + auto identifier_expression = std::make_shared(postfix_expression_->id()); + identifier_expression->stackPosition(bindings, label_count); + printf("\tsll\t$t1,$t1,%d\n", postfix_expression_->getType(bindings)->getSize()/2); + printf("\taddu\t$t0,$t0,$t1\n"); } void PostfixArrayElement::expressionDepth(int &depth_count) const { - if(nextExpression() != nullptr) - nextExpression()->expressionDepth(depth_count); + if(nextExpression() != nullptr) + nextExpression()->expressionDepth(depth_count); - if(index_expression_ != nullptr) - index_expression_->expressionDepth(depth_count); + if(index_expression_ != nullptr) + index_expression_->expressionDepth(depth_count); } TypePtr PostfixArrayElement::getType(const Bindings &bindings) const { - return postfix_expression_->getType(bindings); + return postfix_expression_->getType(bindings); } std::string PostfixArrayElement::id() const { - return postfix_expression_->id(); + return postfix_expression_->id(); } ExpressionPtr PostfixArrayElement::getIndex() const { - if(index_expression_ == nullptr) - throw std::runtime_error("Error : No index expression found"); - return index_expression_; + if(index_expression_ == nullptr) + throw std::runtime_error("Error : No index expression found"); + return index_expression_; } ExpressionPtr PostfixArrayElement::getPostfix() const { - if(postfix_expression_ == nullptr) - throw std::runtime_error("Error : No index expression postfix"); - return postfix_expression_; + if(postfix_expression_ == nullptr) + throw std::runtime_error("Error : No index expression postfix"); + return postfix_expression_; } // PostfixFunctionCall PostfixFunctionCall::PostfixFunctionCall(Expression *argument_expression_list) - : argument_expression_list_(argument_expression_list) + : argument_expression_list_(argument_expression_list) {} Bindings PostfixFunctionCall::printAsm(Bindings bindings, int &label_count) const { - std::vector argument_vector; - ExpressionPtr current_argument = argument_expression_list_; - int argument_counter = 0; + std::vector argument_vector; + ExpressionPtr current_argument = argument_expression_list_; + int argument_counter = 0; - while(current_argument != nullptr) - { - argument_vector.push_back(current_argument); - current_argument = current_argument->nextExpression(); - } + while(current_argument != nullptr) + { + argument_vector.push_back(current_argument); + current_argument = current_argument->nextExpression(); + } - for(auto itr = argument_vector.rbegin(); itr != argument_vector.rend(); ++itr) - { - (*itr)->printAsm(bindings, label_count); + for(auto itr = argument_vector.rbegin(); itr != argument_vector.rend(); ++itr) + { + (*itr)->printAsm(bindings, label_count); - if(argument_counter < 4) - printf("\tmove\t$%d,$2\n", 4+argument_counter); - else - (*itr)->getType(bindings)->store(4*argument_counter); + if(argument_counter < 4) + printf("\tmove\t$%d,$2\n", 4+argument_counter); + else + (*itr)->getType(bindings)->store(4*argument_counter); - argument_counter++; - } + argument_counter++; + } - printf("\tjal\t%s\n\tnop\n\tsw\t$2,%d($fp)\n", - postfix_expression_->id().c_str(), bindings.currentExpressionStackPosition()); - return bindings; + printf("\tjal\t%s\n\tnop\n\tsw\t$2,%d($fp)\n", + postfix_expression_->id().c_str(), bindings.currentExpressionStackPosition()); + return bindings; } void PostfixFunctionCall::countArguments(int &argument_count) const { - ExpressionPtr current_argument = argument_expression_list_; + ExpressionPtr current_argument = argument_expression_list_; - argument_count = 0; + argument_count = 0; - while(current_argument != nullptr) - { - argument_count++; - current_argument = current_argument->nextExpression(); - } + while(current_argument != nullptr) + { + argument_count++; + current_argument = current_argument->nextExpression(); + } } void PostfixFunctionCall::setPostfixExpression(Expression *postfix_expression) { - ExpressionPtr expression_ptr(postfix_expression); - postfix_expression_ = expression_ptr; + ExpressionPtr expression_ptr(postfix_expression); + postfix_expression_ = expression_ptr; } void PostfixFunctionCall::expressionDepth(int &depth_count) const { - if(argument_expression_list_ != nullptr) - argument_expression_list_->expressionDepth(depth_count); + if(argument_expression_list_ != nullptr) + argument_expression_list_->expressionDepth(depth_count); } TypePtr PostfixFunctionCall::getType(const Bindings &) const { - return std::make_shared(); + return std::make_shared(); } // Post increment and decrement definition PostfixPostIncDecExpression::PostfixPostIncDecExpression(const std::string &_operator, Expression *postfix_expression) - : operator_(_operator), postfix_expression_(postfix_expression) + : operator_(_operator), postfix_expression_(postfix_expression) {} Bindings PostfixPostIncDecExpression::printAsm(Bindings bindings, int &label_count) const { - postfix_expression_->printAsm(bindings, label_count); - if(operator_ == "++") - printf("\taddiu\t$3,$2,1\n"); - else if(operator_ == "--") - printf("\tsubiu\t$3,$2,1\n"); - else - throw std::runtime_error("Error : '"+operator_+"' not recognized"); - - 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, label_count); - TypePtr tmp_ptr = postfix_expression_->getType(bindings); - if(std::dynamic_pointer_cast(tmp_ptr)) - { - printf("\tsb\t$3,0($t0)\n"); - } - else if(std::dynamic_pointer_cast(tmp_ptr)) - { - printf("\tsh\t$3,0($t0)\n"); - } - else - { - printf("\tsw\t$3,0($t0)\n"); - } + postfix_expression_->printAsm(bindings, label_count); + if(operator_ == "++") + printf("\taddiu\t$3,$2,1\n"); + else if(operator_ == "--") + printf("\tsubiu\t$3,$2,1\n"); + else + throw std::runtime_error("Error : '"+operator_+"' not recognized"); + + 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, label_count); + TypePtr tmp_ptr = postfix_expression_->getType(bindings); + if(std::dynamic_pointer_cast(tmp_ptr)) + { + printf("\tsb\t$3,0($t0)\n"); + } + else if(std::dynamic_pointer_cast(tmp_ptr)) + { + printf("\tsh\t$3,0($t0)\n"); + } + else + { + printf("\tsw\t$3,0($t0)\n"); + } - return bindings; + return bindings; } TypePtr PostfixPostIncDecExpression::getType(const Bindings &bindings) const { - return postfix_expression_->getType(bindings); + return postfix_expression_->getType(bindings); } // Pre increment and decrement implementation UnaryPreIncDecExpression::UnaryPreIncDecExpression(const std::string &_operator, Expression *unary_expression) - : operator_(_operator), unary_expression_(unary_expression) + : operator_(_operator), unary_expression_(unary_expression) {} Bindings UnaryPreIncDecExpression::printAsm(Bindings bindings, int &label_count) const { - unary_expression_->printAsm(bindings, label_count); - if(operator_ == "++") - printf("\taddi\t$2,$2,1\n"); - else if(operator_ == "--") - printf("\tsubi\t$2,$2,1\n"); - else - throw std::runtime_error("Error : '"+operator_+"' not recognized"); + unary_expression_->printAsm(bindings, label_count); + if(operator_ == "++") + printf("\taddi\t$2,$2,1\n"); + else if(operator_ == "--") + printf("\taddi\t$2,$2,65535\n"); + else + throw std::runtime_error("Error : '"+operator_+"' not recognized"); - std::shared_ptr unary_expression; - unary_expression = std::static_pointer_cast(unary_expression_); + 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, label_count); - unary_expression_->getType(bindings)->store(); - return bindings; + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + unary_expression->stackPosition(bindings, label_count); + unary_expression_->getType(bindings)->store(); + return bindings; } TypePtr UnaryPreIncDecExpression::getType(const Bindings &bindings) const { - return unary_expression_->getType(bindings); + return unary_expression_->getType(bindings); } // Operator unary definition OperatorUnaryExpression::OperatorUnaryExpression(const std::string &_operator, Expression *cast_expression) - : operator_(_operator), cast_expression_(cast_expression) + : operator_(_operator), cast_expression_(cast_expression) {} Bindings OperatorUnaryExpression::printAsm(Bindings bindings, int &label_count) const { - if(operator_ == "!") - { - cast_expression_->printAsm(bindings, label_count); - printf("\tsltu\t$2,$2,1\n\tandi\t$2,$2,0x00ff\n"); - } - else if(operator_ == "~") - { - cast_expression_->printAsm(bindings, label_count); - printf("\tnor\t$2,$0,$2\n"); - } - else if(operator_ == "&") - { - std::shared_ptr unary_expression; - unary_expression = std::static_pointer_cast(cast_expression_); - unary_expression->stackPosition(bindings, label_count); - printf("\tmove\t$2,$t0\n"); - } - else if(operator_ == "-") - { - cast_expression_->printAsm(bindings, label_count); - printf("\tsubu\t$2,$0,$2\n"); - } - else if(operator_ == "*") - { - cast_expression_->printAsm(bindings, label_count); - printf("\tlw\t$2,0($2)\n"); - } + if(operator_ == "!") + { + cast_expression_->printAsm(bindings, label_count); + printf("\tsltu\t$2,$2,1\n\tandi\t$2,$2,0x00ff\n"); + } + else if(operator_ == "~") + { + cast_expression_->printAsm(bindings, label_count); + printf("\tnor\t$2,$0,$2\n"); + } + else if(operator_ == "&") + { + std::shared_ptr unary_expression; + unary_expression = std::static_pointer_cast(cast_expression_); + unary_expression->stackPosition(bindings, label_count); + printf("\tmove\t$2,$t0\n"); + } + else if(operator_ == "-") + { + cast_expression_->printAsm(bindings, label_count); + printf("\tsubu\t$2,$0,$2\n"); + } + else if(operator_ == "*") + { + cast_expression_->printAsm(bindings, label_count); + printf("\tlw\t$2,0($2)\n"); + } - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + return bindings; } void OperatorUnaryExpression::stackPosition(Bindings bindings, int &label_count) const { - if(operator_ == "*") - { - std::shared_ptr unary_expression; - unary_expression = std::static_pointer_cast(cast_expression_); - unary_expression->stackPosition(bindings, label_count); - } + if(operator_ == "*") + { + std::shared_ptr unary_expression; + unary_expression = std::static_pointer_cast(cast_expression_); + unary_expression->stackPosition(bindings, label_count); + } } TypePtr OperatorUnaryExpression::getType(const Bindings &bindings) const { - return cast_expression_->getType(bindings); + return cast_expression_->getType(bindings); } std::string OperatorUnaryExpression::getOperator() const { - return operator_; + return operator_; } // CastExpression definition CastExpression::CastExpression(Type *type, Expression *expression) - : type_(type), expression_(expression) + : type_(type), expression_(expression) {} Bindings CastExpression::printAsm(Bindings bindings, int &label_count) const { - return bindings; + return bindings; } void CastExpression::expressionDepth(int &depth_count) const { - if(nextExpression() != nullptr) - nextExpression()->expressionDepth(depth_count); + if(nextExpression() != nullptr) + nextExpression()->expressionDepth(depth_count); - expression_->expressionDepth(depth_count); + expression_->expressionDepth(depth_count); } TypePtr CastExpression::getType(const Bindings &) const { - return type_; + return type_; } // Additive Expression definition AdditiveExpression::AdditiveExpression(Expression *lhs, const std::string &_operator, Expression *rhs) - : OperationExpression(lhs, rhs), operator_(_operator) + : OperationExpression(lhs, rhs), operator_(_operator) {} Bindings AdditiveExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); + evaluateExpression(bindings, label_count); - // when its a pointer added to a non pointer, multiply non pointer by pointer type - if(std::dynamic_pointer_cast(lhs_->getType(bindings)) != nullptr && - std::dynamic_pointer_cast(rhs_->getType(bindings)) == nullptr && - std::dynamic_pointer_cast(lhs_) != nullptr) - { - printf("\tsll\t$3,$3,%d\n", lhs_->getType(bindings)->getSize()/2); - } - else if(std::dynamic_pointer_cast(lhs_->getType(bindings)) == nullptr && - std::dynamic_pointer_cast(rhs_->getType(bindings)) != nullptr && - std::dynamic_pointer_cast(rhs_) != nullptr) - { - printf("\tsll\t$2,$2,%d\n", rhs_->getType(bindings)->getSize()/2); - } + // when its a pointer added to a non pointer, multiply non pointer by pointer type + if(std::dynamic_pointer_cast(lhs_->getType(bindings)) != nullptr && + std::dynamic_pointer_cast(rhs_->getType(bindings)) == nullptr && + std::dynamic_pointer_cast(lhs_) != nullptr) + { + printf("\tsll\t$3,$3,%d\n", lhs_->getType(bindings)->getSize()/2); + } + else if(std::dynamic_pointer_cast(lhs_->getType(bindings)) == nullptr && + std::dynamic_pointer_cast(rhs_->getType(bindings)) != nullptr && + std::dynamic_pointer_cast(rhs_) != nullptr) + { + printf("\tsll\t$2,$2,%d\n", rhs_->getType(bindings)->getSize()/2); + } - if(operator_ == "+") - printf("\taddu\t$2,$2,$3\n"); - else if(operator_ == "-") - printf("\tsubu\t$2,$2,$3\n"); - else - throw std::runtime_error("Error : '"+operator_+"' not recognized"); + if(operator_ == "+") + printf("\taddu\t$2,$2,$3\n"); + else if(operator_ == "-") + printf("\tsubu\t$2,$2,$3\n"); + else + throw std::runtime_error("Error : '"+operator_+"' not recognized"); - // now I have to store it back into the original stack position - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + // now I have to store it back into the original stack position + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + return bindings; } int AdditiveExpression::constantFold() const { - if(operator_ == "+") - return lhs_->constantFold()+rhs_->constantFold(); - return lhs_->constantFold()-rhs_->constantFold(); + if(operator_ == "+") + return lhs_->constantFold()+rhs_->constantFold(); + return lhs_->constantFold()-rhs_->constantFold(); } @@ -501,556 +501,556 @@ int AdditiveExpression::constantFold() const MultiplicativeExpression::MultiplicativeExpression(Expression *lhs, const std::string &_operator, Expression *rhs) - : OperationExpression(lhs, rhs), operator_(_operator) + : OperationExpression(lhs, rhs), operator_(_operator) {} Bindings MultiplicativeExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); - - // then perform the right operation - if(operator_ == "*") - { - printf("\tmul\t$2,$2,$3\n"); - } - else if(operator_ == "/" || operator_ == "%") - { - printf("\tdiv\t$2,$3\n"); - if(operator_ == "/") - printf("\tmflo\t$2\n"); + evaluateExpression(bindings, label_count); + + // then perform the right operation + if(operator_ == "*") + { + printf("\tmul\t$2,$2,$3\n"); + } + else if(operator_ == "/" || operator_ == "%") + { + printf("\tdiv\t$2,$3\n"); + if(operator_ == "/") + printf("\tmflo\t$2\n"); + else + printf("\tmfhi\t$2\n"); + } else - printf("\tmfhi\t$2\n"); - } - else - { - throw std::runtime_error("Error : '"+operator_+"' not recognized"); - } + { + throw std::runtime_error("Error : '"+operator_+"' not recognized"); + } - // finally store result back into the stack position - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + // finally store result back into the stack position + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + return bindings; } int MultiplicativeExpression::constantFold() const { - if(operator_ == "*") - return lhs_->constantFold()*rhs_->constantFold(); - else if(operator_ == "/") - return lhs_->constantFold()/rhs_->constantFold(); - return lhs_->constantFold()%rhs_->constantFold(); + if(operator_ == "*") + return lhs_->constantFold()*rhs_->constantFold(); + else if(operator_ == "/") + return lhs_->constantFold()/rhs_->constantFold(); + return lhs_->constantFold()%rhs_->constantFold(); } // ShiftExpression definition ShiftExpression::ShiftExpression(Expression* lhs, const std::string &_operator, Expression *rhs) - : OperationExpression(lhs, rhs), operator_(_operator) + : OperationExpression(lhs, rhs), operator_(_operator) {} Bindings ShiftExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); + evaluateExpression(bindings, label_count); - if(operator_ == "<<") - { - printf("\tsll\t$2,$2,$3\n"); - } - else if(operator_ == ">>") - { - printf("\tsra\t$2,$2,$3\n"); - } - else - { - throw std::runtime_error("Error : '"+operator_+"' not recognized"); - } + if(operator_ == "<<") + { + printf("\tsll\t$2,$2,$3\n"); + } + else if(operator_ == ">>") + { + printf("\tsra\t$2,$2,$3\n"); + } + else + { + throw std::runtime_error("Error : '"+operator_+"' not recognized"); + } - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + return bindings; } int ShiftExpression::constantFold() const { - if(operator_ == "<<") - return lhs_->constantFold()<constantFold(); - return lhs_->constantFold()>>rhs_->constantFold(); + if(operator_ == "<<") + return lhs_->constantFold()<constantFold(); + return lhs_->constantFold()>>rhs_->constantFold(); } // RelationalExpression definition RelationalExpression::RelationalExpression(Expression* lhs, const std::string &_operator, Expression *rhs) - : OperationExpression(lhs, rhs), operator_(_operator) + : OperationExpression(lhs, rhs), operator_(_operator) {} Bindings RelationalExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); - - if(operator_ == "<") - { - printf("\tslt\t$2,$2,$3\n"); - } - else if(operator_ == ">") - { - printf("\tslt\t$2,$3,$2\n"); - } - else if(operator_ == "<=") - { - printf("\tslt\t$2,$3,$2\n\txori\t$2,$2,0x1\n"); - } - else if(operator_ == ">=") - { - printf("\tslt\t$2,$2,$3\n\txori\t$2,$2,0x1\n"); - } - else - { - throw std::runtime_error("Error : '"+operator_+"' not recognized"); - } - - // TODO might get rid of this - printf("\tandi\t$2,$2,0x00ff\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + evaluateExpression(bindings, label_count); + + if(operator_ == "<") + { + printf("\tslt\t$2,$2,$3\n"); + } + else if(operator_ == ">") + { + printf("\tslt\t$2,$3,$2\n"); + } + else if(operator_ == "<=") + { + printf("\tslt\t$2,$3,$2\n\txori\t$2,$2,0x1\n"); + } + else if(operator_ == ">=") + { + printf("\tslt\t$2,$2,$3\n\txori\t$2,$2,0x1\n"); + } + else + { + throw std::runtime_error("Error : '"+operator_+"' not recognized"); + } + + // TODO might get rid of this + printf("\tandi\t$2,$2,0x00ff\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int RelationalExpression::constantFold() const { - if(operator_ == "<") - { - return lhs_->constantFold()constantFold(); - } - else if(operator_ == ">") - { - return lhs_->constantFold()>rhs_->constantFold(); - } - else if(operator_ == "<=") - { - return lhs_->constantFold()<=rhs_->constantFold(); - } + if(operator_ == "<") + { + return lhs_->constantFold()constantFold(); + } + else if(operator_ == ">") + { + return lhs_->constantFold()>rhs_->constantFold(); + } + else if(operator_ == "<=") + { + return lhs_->constantFold()<=rhs_->constantFold(); + } - return lhs_->constantFold()>=rhs_->constantFold(); + return lhs_->constantFold()>=rhs_->constantFold(); } // EqualityExpression definition EqualityExpression::EqualityExpression(Expression *lhs, const std::string &_operator, Expression *rhs) - : OperationExpression(lhs, rhs), operator_(_operator) + : OperationExpression(lhs, rhs), operator_(_operator) {} Bindings EqualityExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); - printf("\txor\t$2,$2,$3\n"); + evaluateExpression(bindings, label_count); + printf("\txor\t$2,$2,$3\n"); - if(operator_ == "==") - { - printf("\tsltiu\t$2,$2,1\n"); - } - else if(operator_ == "!=") - { - printf("\tsltu\t$2,$0,$2\n"); - } - else - { - throw std::runtime_error("Error : '"+operator_+"' not recognized"); - } + if(operator_ == "==") + { + printf("\tsltiu\t$2,$2,1\n"); + } + else if(operator_ == "!=") + { + printf("\tsltu\t$2,$0,$2\n"); + } + else + { + throw std::runtime_error("Error : '"+operator_+"' not recognized"); + } - // TODO Work out why it is necessary to remove bytes 3 and 2. - printf("\tandi\t$2,$2,0x00ff\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + // TODO Work out why it is necessary to remove bytes 3 and 2. + printf("\tandi\t$2,$2,0x00ff\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int EqualityExpression::constantFold() const { - if(operator_ == "==") - return lhs_->constantFold()==rhs_->constantFold(); - return lhs_->constantFold()!=rhs_->constantFold(); + if(operator_ == "==") + return lhs_->constantFold()==rhs_->constantFold(); + return lhs_->constantFold()!=rhs_->constantFold(); } // AndExpression definition AndExpression::AndExpression(Expression *lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} Bindings AndExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); - printf("\tand\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + evaluateExpression(bindings, label_count); + printf("\tand\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int AndExpression::constantFold() const { - return lhs_->constantFold()&rhs_->constantFold(); + return lhs_->constantFold()&rhs_->constantFold(); } // ExclusiveOrExpression definition ExclusiveOrExpression::ExclusiveOrExpression(Expression *lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} Bindings ExclusiveOrExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); - printf("\txor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + evaluateExpression(bindings, label_count); + printf("\txor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int ExclusiveOrExpression::constantFold() const { - return lhs_->constantFold()^rhs_->constantFold(); + return lhs_->constantFold()^rhs_->constantFold(); } // InclusiveOrExpression definition InclusiveOrExpression::InclusiveOrExpression(Expression *lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} Bindings InclusiveOrExpression::printAsm(Bindings bindings, int &label_count) const { - evaluateExpression(bindings, label_count); - printf("\tor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + evaluateExpression(bindings, label_count); + printf("\tor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int InclusiveOrExpression::constantFold() const { - return lhs_->constantFold()|rhs_->constantFold(); + return lhs_->constantFold()|rhs_->constantFold(); } // LogicalAndExpression definition LogicalAndExpression::LogicalAndExpression(Expression *lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} Bindings LogicalAndExpression::printAsm(Bindings bindings, int &label_count) const { - int log_and = label_count++; - lhs_->printAsm(bindings, label_count); - printf("\tbeq\t$2,$0,$%d_log_and_load_0\n\tnop\n", log_and); - rhs_->printAsm(bindings, label_count); - printf("\tbeq\t$2,$0,$%d_log_and_load_0\n\tnop\n", log_and); - printf("\tli\t$2,1\n\tb\t$%d_log_and_end\n\tnop\n", log_and); - printf("$%d_log_and_load_0:\n\tmove\t$2,$0\n$%d_log_and_end:\n", log_and, log_and); - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + int log_and = label_count++; + lhs_->printAsm(bindings, label_count); + printf("\tbeq\t$2,$0,$%d_log_and_load_0\n\tnop\n", log_and); + rhs_->printAsm(bindings, label_count); + printf("\tbeq\t$2,$0,$%d_log_and_load_0\n\tnop\n", log_and); + printf("\tli\t$2,1\n\tb\t$%d_log_and_end\n\tnop\n", log_and); + printf("$%d_log_and_load_0:\n\tmove\t$2,$0\n$%d_log_and_end:\n", log_and, log_and); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int LogicalAndExpression::constantFold() const { - return lhs_->constantFold()&&rhs_->constantFold(); + return lhs_->constantFold()&&rhs_->constantFold(); } // LogicalOrExpression definition LogicalOrExpression::LogicalOrExpression(Expression *lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} Bindings LogicalOrExpression::printAsm(Bindings bindings, int &label_count) const { - int log_or = label_count++; - lhs_->printAsm(bindings, label_count); - printf("\tbne\t$2,$0,$%d_log_or_load_1\n\tnop\n", log_or); - rhs_->printAsm(bindings, label_count); - printf("\tbeq\t$2,$0,$%d_log_or_load_0\n\tnop\n", log_or); - printf("$%d_log_or_load_1:\n\tli\t$2,1\n\tb\t$%d_log_or_end\n\tnop\n", log_or, log_or); - printf("$%d_log_or_load_0:\n\tmove\t$2,$0\n$%d_log_or_end:\n", log_or, log_or); - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + int log_or = label_count++; + lhs_->printAsm(bindings, label_count); + printf("\tbne\t$2,$0,$%d_log_or_load_1\n\tnop\n", log_or); + rhs_->printAsm(bindings, label_count); + printf("\tbeq\t$2,$0,$%d_log_or_load_0\n\tnop\n", log_or); + printf("$%d_log_or_load_1:\n\tli\t$2,1\n\tb\t$%d_log_or_end\n\tnop\n", log_or, log_or); + printf("$%d_log_or_load_0:\n\tmove\t$2,$0\n$%d_log_or_end:\n", log_or, log_or); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int LogicalOrExpression::constantFold() const { - return lhs_->constantFold()&&rhs_->constantFold(); + return lhs_->constantFold()&&rhs_->constantFold(); } // ConditionalExpression definition ConditionalExpression::ConditionalExpression(Expression *logical_or, - Expression *expression, - Expression *conditional_expression) - : logical_or_(logical_or), expression_(expression), - conditional_expression_(conditional_expression) + Expression *expression, + Expression *conditional_expression) + : logical_or_(logical_or), expression_(expression), + conditional_expression_(conditional_expression) {} Bindings ConditionalExpression::printAsm(Bindings bindings, int &label_count) const { - return bindings; + return bindings; } TypePtr ConditionalExpression::getType(const Bindings &bindings) const { - return std::make_shared(); + return std::make_shared(); } // Assignment Expression definition AssignmentExpression::AssignmentExpression(Expression *lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} AssignmentExpression::AssignmentExpression(ExpressionPtr lhs, Expression *rhs) - : OperationExpression(lhs, rhs) + : OperationExpression(lhs, rhs) {} Bindings AssignmentExpression::printAsm(Bindings bindings, int &label_count) const { - // TODO add stack and store results in there, also for addition and multiplication. + // 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 - std::shared_ptr lhs_postfix; - lhs_postfix = std::static_pointer_cast(lhs_); + // get the current location of lhs in the stack so that I can store result there + std::shared_ptr lhs_postfix; + lhs_postfix = std::static_pointer_cast(lhs_); - // get the current available stack position - int expression_stack_position = bindings.currentExpressionStackPosition(); + // get the current available stack position + int expression_stack_position = bindings.currentExpressionStackPosition(); - // evaluate rhs and get the result back at the stack position I assigned - // don't have to change the stack position as there is no lhs to evaluate - rhs_->printAsm(bindings, label_count); - bindings.nextExpressionStackPosition(); + // evaluate rhs and get the result back at the stack position I assigned + // don't have to change the stack position as there is no lhs to evaluate + rhs_->printAsm(bindings, label_count); + bindings.nextExpressionStackPosition(); - std::shared_ptr rhs_tmp_string; - std::shared_ptr rhs_tmp_address; - rhs_tmp_string = std::dynamic_pointer_cast(rhs_); - rhs_tmp_address = std::dynamic_pointer_cast(rhs_); + std::shared_ptr rhs_tmp_string; + std::shared_ptr rhs_tmp_address; + rhs_tmp_string = std::dynamic_pointer_cast(rhs_); + rhs_tmp_address = std::dynamic_pointer_cast(rhs_); - // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways - if(rhs_tmp_string != nullptr) - { - lhs_postfix->pointerPosition(bindings); - } - else if(rhs_tmp_address != nullptr && rhs_tmp_address->getOperator() == "&") - { - lhs_postfix->pointerPosition(bindings); - } - else - { - lhs_postfix->stackPosition(bindings, label_count); - } + // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways + if(rhs_tmp_string != nullptr) + { + lhs_postfix->pointerPosition(bindings); + } + else if(rhs_tmp_address != nullptr && rhs_tmp_address->getOperator() == "&") + { + lhs_postfix->pointerPosition(bindings); + } + else + { + lhs_postfix->stackPosition(bindings, label_count); + } - // now the result of the rhs will be in that stack position, so we can load it into $2 - printf("\tlw\t$2,%d($fp)\n", expression_stack_position); + // now the result of the rhs will be in that stack position, so we can load it into $2 + printf("\tlw\t$2,%d($fp)\n", expression_stack_position); - TypePtr lhs_type = lhs_->getType(bindings); - // check if lhs is trying to access an array - std::shared_ptr lhs_tmp; - lhs_tmp = std::dynamic_pointer_cast(lhs_); - - if(lhs_tmp != nullptr) - { - std::shared_ptr lhs_pointer_type; - lhs_pointer_type = std::dynamic_pointer_cast(lhs_type); - if(lhs_pointer_type != nullptr) + TypePtr lhs_type = lhs_->getType(bindings); + // check if lhs is trying to access an array + std::shared_ptr lhs_tmp; + lhs_tmp = std::dynamic_pointer_cast(lhs_); + + if(lhs_tmp != nullptr) { - lhs_pointer_type->pointerStore(); + std::shared_ptr lhs_pointer_type; + lhs_pointer_type = std::dynamic_pointer_cast(lhs_type); + if(lhs_pointer_type != nullptr) + { + lhs_pointer_type->pointerStore(); + } + else + { + lhs_type->store(); + } } else { - lhs_type->store(); + lhs_type->store(); } - } - else - { - lhs_type->store(); - } - return bindings; + return bindings; } // Identifier definition Identifier::Identifier(const std::string &id) - : id_(id) + : id_(id) {} Bindings Identifier::printAsm(Bindings bindings, int &) const { - if(bindings.bindingExists(id_)) - { - int stack_position = bindings.stackPosition(id_); - if(stack_position == -1) + if(bindings.bindingExists(id_)) { - // it's a global variable - printf("\tlui\t$2,%%hi(%s)\n\tlw\t$2,%%lo(%s)($2)\n", id_.c_str(), id_.c_str()); + int stack_position = bindings.stackPosition(id_); + if(stack_position == -1) + { + // it's a global variable + printf("\tlui\t$2,%%hi(%s)\n\tlw\t$2,%%lo(%s)($2)\n", id_.c_str(), id_.c_str()); + } + else + { + if(std::dynamic_pointer_cast(bindings.getType(id_)) != nullptr) + { + printf("\taddiu\t$2,$fp,%d\n", stack_position); + } + else + { + bindings.getType(id_)->load(2, stack_position); + } + } } else { - if(std::dynamic_pointer_cast(bindings.getType(id_)) != nullptr) - { - printf("\taddiu\t$2,$fp,%d\n", stack_position); - } - else - { - bindings.getType(id_)->load(2, stack_position); - } + throw std::runtime_error("Error : Can't find '"+id_+"' in current scope binding"); } - } - else - { - throw std::runtime_error("Error : Can't find '"+id_+"' in current scope binding"); - } - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } void Identifier::pointerPosition(Bindings bindings) const { - if(bindings.bindingExists(id_)) - { - printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); - return; - } + if(bindings.bindingExists(id_)) + { + printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); + return; + } - throw std::runtime_error("Error : '"+id_+"' not yet declared"); + throw std::runtime_error("Error : '"+id_+"' not yet declared"); } void Identifier::stackPosition(Bindings bindings, int &) const { - if(bindings.bindingExists(id_)) - { - if(std::dynamic_pointer_cast(bindings.getType(id_)) != nullptr) - printf("\tlw\t$t0,%d($fp)\n", bindings.stackPosition(id_)); - else - printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); - return; - } + if(bindings.bindingExists(id_)) + { + if(std::dynamic_pointer_cast(bindings.getType(id_)) != nullptr) + printf("\tlw\t$t0,%d($fp)\n", bindings.stackPosition(id_)); + else + printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); + return; + } - throw std::runtime_error("Error : '"+id_+"' not yet declared"); + throw std::runtime_error("Error : '"+id_+"' not yet declared"); } std::string Identifier::id() const { - return id_; + return id_; } TypePtr Identifier::getType(const Bindings &bindings) const { - return bindings.getType(id_); + return bindings.getType(id_); } // String literal definition StringLiteral::StringLiteral(const std::string &string_content) - : string_content_(string_content) + : string_content_(string_content) {} Bindings StringLiteral::printAsm(Bindings bindings, int &) const { - int label = bindings.insertStringLiteral(string_content_); - printf("\tlui\t$2,%%hi($%d_string)\n\taddiu\t$2,$2,%%lo($%d_string)\n", label, label); - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + int label = bindings.insertStringLiteral(string_content_); + printf("\tlui\t$2,%%hi($%d_string)\n\taddiu\t$2,$2,%%lo($%d_string)\n", label, label); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } TypePtr StringLiteral::getType(const Bindings &) const { - std::shared_ptr tmp_pointer_ptr; - tmp_pointer_ptr->type(std::make_shared()); - return tmp_pointer_ptr; + std::shared_ptr tmp_pointer_ptr; + tmp_pointer_ptr->type(std::make_shared()); + return tmp_pointer_ptr; } // Constant definition Constant::Constant(const int32_t &constant) - : constant_(constant) + : constant_(constant) {} Bindings Constant::printAsm(Bindings bindings, int &) const { - // constant only has to load to $2 because the other expression will take care of the rest - printf("\tli\t$2,%d\n", constant_); - printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); - return bindings; + // constant only has to load to $2 because the other expression will take care of the rest + printf("\tli\t$2,%d\n", constant_); + printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); + return bindings; } int Constant::constantFold() const { - return constant_; + return constant_; } TypePtr Constant::getType(const Bindings &) const { - return std::make_shared(); + return std::make_shared(); } // Initializer definition Initializer::Initializer(Expression *next_initializer) - : next_initializer_(next_initializer) + : next_initializer_(next_initializer) {} Bindings Initializer::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } TypePtr Initializer::getType(const Bindings &bindings) const { - return next_initializer_->getType(bindings); + return next_initializer_->getType(bindings); } void Initializer::printInitializerAsm(Bindings &bindings, int &label_count, int position, const - std::vector &iteration_vector, const TypePtr &type) const -{ - std::shared_ptr next_initializer - (std::dynamic_pointer_cast(next_initializer_)); - ExpressionPtr current_expression = next_initializer_; - std::vector expression_vector; - - while(current_expression != nullptr) - { - expression_vector.push_back(current_expression); - current_expression = current_expression->nextExpression(); - } - - std::reverse(expression_vector.begin(), expression_vector.end()); - int size = (int)expression_vector.size(); - for(int i = size; i < iteration_vector[position]; ++i) - { - expression_vector.emplace_back(nullptr); - } - - for(int i = 0; i < iteration_vector[position]; ++i) - { - next_initializer = std::dynamic_pointer_cast(expression_vector[i]); - if(next_initializer != nullptr) + std::vector &iteration_vector, const TypePtr &type) const +{ + std::shared_ptr next_initializer + (std::dynamic_pointer_cast(next_initializer_)); + ExpressionPtr current_expression = next_initializer_; + std::vector expression_vector; + + while(current_expression != nullptr) { - next_initializer->printInitializerAsm(bindings, label_count, - position-1, iteration_vector, type); + expression_vector.push_back(current_expression); + current_expression = current_expression->nextExpression(); } - else + + std::reverse(expression_vector.begin(), expression_vector.end()); + int size = (int)expression_vector.size(); + for(int i = size; i < iteration_vector[position]; ++i) + { + expression_vector.emplace_back(nullptr); + } + + for(int i = 0; i < iteration_vector[position]; ++i) { - if(expression_vector[i] != nullptr) - { - Bindings temp_bindings = bindings; - expression_vector[i]->printAsm(temp_bindings, label_count); - type->store(bindings.currentStackPosition()); - } - type->increaseStackPosition(bindings); + next_initializer = std::dynamic_pointer_cast(expression_vector[i]); + if(next_initializer != nullptr) + { + next_initializer->printInitializerAsm(bindings, label_count, + position-1, iteration_vector, type); + } + else + { + if(expression_vector[i] != nullptr) + { + Bindings temp_bindings = bindings; + expression_vector[i]->printAsm(temp_bindings, label_count); + type->store(bindings.currentStackPosition()); + } + type->increaseStackPosition(bindings); + } } - } } ExpressionPtr Initializer::getNext() const { - return next_initializer_; + return next_initializer_; } diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 29c3993..52dbc55 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -7,145 +7,145 @@ // Function definition Function::Function(const std::string& id, Statement* statement, DeclarationPtr parameter_list) - : id_(id), statement_(statement), parameter_list_(parameter_list) + : id_(id), statement_(statement), parameter_list_(parameter_list) {} void Function::print() const { - printf("%s\n", id_.c_str()); + printf("%s\n", id_.c_str()); - if(parameter_list_ != nullptr) - parameter_list_->print(); + if(parameter_list_ != nullptr) + parameter_list_->print(); - if(statement_ != nullptr) - statement_->print(); + if(statement_ != nullptr) + statement_->print(); } void Function::printXml() const { - printf("\n", id_.c_str()); + printf("\n", id_.c_str()); - DeclarationPtr parameter = parameter_list_; - std::vector parameter_vec; + DeclarationPtr parameter = parameter_list_; + std::vector parameter_vec; - while(parameter != nullptr) - { - parameter_vec.push_back(parameter->getId()); - parameter = parameter->getNext(); - } - - for(auto itr = parameter_vec.rbegin(); itr != parameter_vec.rend(); ++itr) - { - printf("", (*itr).c_str()); - } + while(parameter != nullptr) + { + parameter_vec.push_back(parameter->getId()); + parameter = parameter->getNext(); + } + + for(auto itr = parameter_vec.rbegin(); itr != parameter_vec.rend(); ++itr) + { + printf("", (*itr).c_str()); + } - if(statement_ != nullptr) - statement_->printXml(); + if(statement_ != nullptr) + statement_->printXml(); - printf("\n"); + printf("\n"); } Bindings Function::printAsm(Bindings bindings, int& label_count) const { - Bindings original_bindings = bindings; - // Counting all the variables being declared in the function - int variable_count = 0; - if(statement_ != nullptr) - statement_->countVariables(variable_count); + Bindings original_bindings = bindings; + // Counting all the variables being declared in the function + int variable_count = 0; + if(statement_ != nullptr) + statement_->countVariables(variable_count); - int max_argument_count = 0; - // Count the maximum number of arguments - statement_->countArguments(max_argument_count); + int max_argument_count = 0; + // Count the maximum number of arguments + statement_->countArguments(max_argument_count); - int max_depth = 0; - statement_->countExpressionDepth(max_depth); + int max_depth = 0; + statement_->countExpressionDepth(max_depth); - if(max_argument_count < 4) - max_argument_count = 4; + if(max_argument_count < 4) + max_argument_count = 4; - int parameter_count = 0; - countParameters(parameter_count); + int parameter_count = 0; + countParameters(parameter_count); - // This adds 2 to store the frame pointer and the return address - int memory_needed = 4*(variable_count+max_argument_count+max_depth+2); + // This adds 2 to store the frame pointer and the return address + int memory_needed = 4*(variable_count+max_argument_count+max_depth+2); - printf("\t.text\n\t.align\t2\n\t.globl\t%s\n%s:\n\taddiu\t$sp,$sp,-%d\n\tsw\t", - id_.c_str(), id_.c_str(), memory_needed); - printf("$31,%d($sp)\n\tsw\t$fp,%d($sp)\n\tmove\t$fp,$sp\n", memory_needed-4, memory_needed-8); + printf("\t.text\n\t.align\t2\n\t.globl\t%s\n%s:\n\taddiu\t$sp,$sp,-%d\n\tsw\t", + id_.c_str(), id_.c_str(), memory_needed); + 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*4); - bindings.setExpressionStackPosition((max_argument_count+variable_count)*4); + // set the stack counter to the right value + bindings.setStackPosition(max_argument_count*4); + bindings.setExpressionStackPosition((max_argument_count+variable_count)*4); - printParameterAsm(bindings, memory_needed); + printParameterAsm(bindings, memory_needed); - // Prints the asm for the compound statement in the function - statement_->printAsm(bindings, label_count); + // Prints the asm for the compound statement in the function + statement_->printAsm(bindings, label_count); - printf("\tmove\t$2,$0\n0:\n\tmove\t$sp,$fp\n\tlw\t$31,%d($sp)\n", memory_needed-4); - printf("\tlw\t$fp,%d", memory_needed-8); - printf("($sp)\n\taddiu\t$sp,$sp,%d\n\tjr\t$31\n\tnop\n", memory_needed); - - auto string_lit_iterator = bindings.getStringLiteralIterator(); - if(string_lit_iterator.first != string_lit_iterator.second) - { - printf("\n\t.rdata\n\t.align\t2\n"); - for(auto itr = string_lit_iterator.first; itr != string_lit_iterator.second; ++itr) + printf("\tmove\t$2,$0\n0:\n\tmove\t$sp,$fp\n\tlw\t$31,%d($sp)\n", memory_needed-4); + printf("\tlw\t$fp,%d", memory_needed-8); + printf("($sp)\n\taddiu\t$sp,$sp,%d\n\tjr\t$31\n\tnop\n", memory_needed); + + auto string_lit_iterator = bindings.getStringLiteralIterator(); + if(string_lit_iterator.first != string_lit_iterator.second) { - printf("$%d_string:\n\t.ascii\t", int(itr-string_lit_iterator.first)); - printf("\"%s\\000\"", (*itr).c_str()); + printf("\n\t.rdata\n\t.align\t2\n"); + for(auto itr = string_lit_iterator.first; itr != string_lit_iterator.second; ++itr) + { + printf("$%d_string:\n\t.ascii\t", int(itr-string_lit_iterator.first)); + printf("\"%s\\000\"", (*itr).c_str()); + } } - } - return original_bindings; + return original_bindings; } void Function::printParameterAsm(Bindings& bindings, int& frame_offset) const { - std::vector parameter_vector; - DeclarationPtr parameter_list = parameter_list_; - - while(parameter_list != nullptr) - { - parameter_vector.push_back(parameter_list); - parameter_list = parameter_list->getNext(); - } - - for(auto itr = parameter_vector.rbegin(); itr != parameter_vector.rend(); ++itr) - { - int i = itr-parameter_vector.rbegin(); - bindings.insertBinding((*itr)->getId(), (*itr)->getType(), i*4+frame_offset); - TypePtr parameter_type = (*itr)->getType(); - if(i < 4) - { - parameter_type->store(4+i, 4*i+frame_offset); + std::vector parameter_vector; + DeclarationPtr parameter_list = parameter_list_; + + while(parameter_list != nullptr) + { + parameter_vector.push_back(parameter_list); + parameter_list = parameter_list->getNext(); } - else + + for(auto itr = parameter_vector.rbegin(); itr != parameter_vector.rend(); ++itr) { - if(std::dynamic_pointer_cast(parameter_type) != nullptr) - { - if(std::dynamic_pointer_cast(parameter_type->type()) != nullptr) + int i = itr-parameter_vector.rbegin(); + bindings.insertBinding((*itr)->getId(), (*itr)->getType(), i*4+frame_offset); + TypePtr parameter_type = (*itr)->getType(); + if(i < 4) { - printf("\tlw\t$2,%d($fp)\n", i*4+frame_offset); - printf("\tsb\t$2,%d($fp)\n", i*4+frame_offset); + parameter_type->store(4+i, 4*i+frame_offset); } - else if(std::dynamic_pointer_cast(parameter_type->type()) != nullptr) + else { - printf("\tlw\t$2,%d($fp)\n", i*4+frame_offset); - printf("\tsh\t$2,%d($fp)\n", i*4+frame_offset); + if(std::dynamic_pointer_cast(parameter_type) != nullptr) + { + if(std::dynamic_pointer_cast(parameter_type->type()) != nullptr) + { + printf("\tlw\t$2,%d($fp)\n", i*4+frame_offset); + printf("\tsb\t$2,%d($fp)\n", i*4+frame_offset); + } + else if(std::dynamic_pointer_cast(parameter_type->type()) != nullptr) + { + printf("\tlw\t$2,%d($fp)\n", i*4+frame_offset); + printf("\tsh\t$2,%d($fp)\n", i*4+frame_offset); + } + } } - } } - } } void Function::countParameters(int& parameter_count) const { - DeclarationPtr parameter_list = parameter_list_; + DeclarationPtr parameter_list = parameter_list_; - while(parameter_list != nullptr) - { - parameter_count++; - parameter_list = parameter_list->getNext(); - } + while(parameter_list != nullptr) + { + parameter_count++; + parameter_list = parameter_list->getNext(); + } } diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index 309e2d8..207cb6d 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -8,581 +8,581 @@ // General base Statement definition Statement::Statement(Statement *statement) - : next_statement_(statement) + : next_statement_(statement) {} int Statement::constantFold() const { - throw std::runtime_error("Error : not implemented"); + throw std::runtime_error("Error : not implemented"); } ExpressionPtr Statement::getExpression() const { - return nullptr; + return nullptr; } bool Statement::isDefault() const { - return false; + return false; } void Statement::linkStatement(Statement *next) { - StatementPtr statement_ptr(next); - next_statement_ = statement_ptr; + StatementPtr statement_ptr(next); + next_statement_ = statement_ptr; } StatementPtr Statement::getNext() const { - return next_statement_; + return next_statement_; } // Label Statement definition LabelStatement::LabelStatement(const std::string &label, Statement *statement) - : label_(label), statement_(statement) + : label_(label), statement_(statement) {} void LabelStatement::print() const { - if(next_statement_ != nullptr) - next_statement_->print(); + if(next_statement_ != nullptr) + next_statement_->print(); - printf("Label Statement\n"); + printf("Label Statement\n"); } void LabelStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); } Bindings LabelStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - printf("%s:\n", label_.c_str()); + printf("%s:\n", label_.c_str()); - statement_->printAsm(bindings, label_count); + statement_->printAsm(bindings, label_count); - return bindings; + return bindings; } void LabelStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); - statement_->countVariables(var_count); + statement_->countVariables(var_count); } void LabelStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - statement_->countArguments(argument_count); + statement_->countArguments(argument_count); } void LabelStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - } + int previous_depth_count = depth_count; + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } - statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; } // Case Statement definition CaseStatement::CaseStatement(Statement *statement, Expression *constant_expression, const bool &_default) - : constant_expression_(constant_expression), statement_(statement), default_(_default) + : constant_expression_(constant_expression), statement_(statement), default_(_default) {} void CaseStatement::print() const { - if(next_statement_ != nullptr) - next_statement_->print(); + if(next_statement_ != nullptr) + next_statement_->print(); - printf("Case Statement\n"); + printf("Case Statement\n"); } void CaseStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); } Bindings CaseStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - statement_->printAsm(bindings, label_count); + statement_->printAsm(bindings, label_count); - return bindings; + return bindings; } void CaseStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); - statement_->countVariables(var_count); + statement_->countVariables(var_count); } void CaseStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - statement_->countArguments(argument_count); + statement_->countArguments(argument_count); } void CaseStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - } + int previous_depth_count = depth_count; + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } - statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; } int CaseStatement::constantFold() const { - return constant_expression_->constantFold(); + return constant_expression_->constantFold(); } ExpressionPtr CaseStatement::getExpression() const { - return constant_expression_; + return constant_expression_; } bool CaseStatement::isDefault() const { - return default_; + return default_; } // Compound Statement definition CompoundStatement::CompoundStatement(Declaration *declaration, Statement *statement) - : Statement(), declaration_(declaration), statement_(statement) + : Statement(), declaration_(declaration), statement_(statement) {} CompoundStatement::CompoundStatement(Statement *statement) - : statement_(statement) + : statement_(statement) {} void CompoundStatement::print() const { - if(declaration_ != nullptr) - declaration_->print(); + if(declaration_ != nullptr) + declaration_->print(); - if(statement_ != nullptr) - statement_->print(); + if(statement_ != nullptr) + statement_->print(); } void CompoundStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); - printf("\n"); + printf("\n"); - if(declaration_ != nullptr) - declaration_->printXml(); + if(declaration_ != nullptr) + declaration_->printXml(); - if(statement_ != nullptr) - statement_->printXml(); + if(statement_ != nullptr) + statement_->printXml(); - printf("\n"); + printf("\n"); } Bindings CompoundStatement::printAsm(Bindings bindings, int &label_count) const { - Bindings outer_scope_bindings = bindings; + Bindings outer_scope_bindings = bindings; - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - if(declaration_ != nullptr) - bindings = declaration_->localAsm(bindings, label_count); + if(declaration_ != nullptr) + bindings = declaration_->localAsm(bindings, label_count); - if(statement_ != nullptr) - statement_->printAsm(bindings, label_count); + if(statement_ != nullptr) + statement_->printAsm(bindings, label_count); - return outer_scope_bindings; + return outer_scope_bindings; } void CompoundStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); - if(declaration_ != nullptr) - declaration_->countDeclarations(var_count); + if(declaration_ != nullptr) + declaration_->countDeclarations(var_count); - if(statement_ != nullptr) - statement_->countVariables(var_count); + if(statement_ != nullptr) + statement_->countVariables(var_count); } void CompoundStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - if(statement_ != nullptr) - statement_->countArguments(argument_count); + if(statement_ != nullptr) + statement_->countArguments(argument_count); } void CompoundStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - } + int previous_depth_count = depth_count; + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } - if(statement_ != nullptr) - { - statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - } + if(statement_ != nullptr) + { + statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + } } StatementPtr CompoundStatement::getStatementList() const { - return statement_; + return statement_; } // If Else Statement definition IfElseStatement::IfElseStatement(Expression *condition, Statement *_if, Statement *_else) - : Statement(), condition_(condition), if_(_if), else_(_else) {} + : Statement(), condition_(condition), if_(_if), else_(_else) {} void IfElseStatement::print() const { - condition_->print(); - if_->print(); - if(else_ != nullptr) - else_->print(); + condition_->print(); + if_->print(); + if(else_ != nullptr) + else_->print(); } void IfElseStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); - if(if_ != nullptr) - if_->printXml(); + if(if_ != nullptr) + if_->printXml(); - if(else_ != nullptr) - else_->printXml(); + if(else_ != nullptr) + else_->printXml(); } Bindings IfElseStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - int if_label = label_count++; + int if_label = label_count++; - condition_->printAsm(bindings, label_count); - printf("\tbeq\t$2,$0,$%d_else\n\tnop\n", if_label); + condition_->printAsm(bindings, label_count); + printf("\tbeq\t$2,$0,$%d_else\n\tnop\n", if_label); - if_->printAsm(bindings, label_count); + if_->printAsm(bindings, label_count); - printf("\tb\t$%d_if_end\n\tnop\n$%d_else:\n", if_label, if_label); + printf("\tb\t$%d_if_end\n\tnop\n$%d_else:\n", if_label, if_label); - if(else_ != nullptr) - else_->printAsm(bindings, label_count); + if(else_ != nullptr) + else_->printAsm(bindings, label_count); - printf("$%d_if_end:\n", if_label); + printf("$%d_if_end:\n", if_label); - return bindings; + return bindings; } void IfElseStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); - if(if_ != nullptr) - if_->countVariables(var_count); + if(if_ != nullptr) + if_->countVariables(var_count); - if(else_ != nullptr) - else_->countVariables(var_count); + if(else_ != nullptr) + else_->countVariables(var_count); } void IfElseStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - if(if_ != nullptr) - if_->countArguments(argument_count); + if(if_ != nullptr) + if_->countArguments(argument_count); - if(else_ != nullptr) - else_->countArguments(argument_count); + if(else_ != nullptr) + else_->countArguments(argument_count); } void IfElseStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; + int previous_depth_count = depth_count; + + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); + depth_count = 1; + condition_->expressionDepth(depth_count); if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + depth_count = previous_depth_count; previous_depth_count = depth_count; - } - - depth_count = 1; - condition_->expressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - - if_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - - if(else_ != nullptr) - { - else_->countExpressionDepth(depth_count); + + if_->countExpressionDepth(depth_count); if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - } + depth_count = previous_depth_count; + previous_depth_count = depth_count; + + if(else_ != nullptr) + { + else_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + } } // Switch Statement definition SwitchStatement::SwitchStatement(Expression *condition, Statement *statement) - : condition_(condition), statement_(statement) + : condition_(condition), statement_(statement) {} void SwitchStatement::print() const { - if(next_statement_ != nullptr) - next_statement_->print(); + if(next_statement_ != nullptr) + next_statement_->print(); - printf("Switch Statement\n"); - condition_->print(); - statement_->print(); + printf("Switch Statement\n"); + condition_->print(); + statement_->print(); } void SwitchStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); - condition_->printXml(); - statement_->printXml(); + condition_->printXml(); + statement_->printXml(); } Bindings SwitchStatement::printAsm(Bindings bindings, int &label_count) const { - int switch_count = label_count++; - std::shared_ptr comp_statement; - StatementPtr case_statement_list; - std::vector case_statement_vector; + int switch_count = label_count++; + std::shared_ptr comp_statement; + StatementPtr case_statement_list; + std::vector case_statement_vector; - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - condition_->printAsm(bindings, label_count); + condition_->printAsm(bindings, label_count); - comp_statement = std::dynamic_pointer_cast(statement_); - if(comp_statement == nullptr) - throw std::runtime_error("Error : not implemented"); + comp_statement = std::dynamic_pointer_cast(statement_); + if(comp_statement == nullptr) + throw std::runtime_error("Error : not implemented"); - bindings.breakLabel("$"+std::to_string(switch_count)+"_break_switch"); + bindings.breakLabel("$"+std::to_string(switch_count)+"_break_switch"); - case_statement_list = comp_statement->getStatementList(); - while(case_statement_list != nullptr) - { - case_statement_vector.push_back(case_statement_list); - case_statement_list = case_statement_list->getNext(); - } - - bool is_default = false; - - for(auto itr = case_statement_vector.rbegin(); itr != case_statement_vector.rend(); ++itr) - { - if((*itr)->getExpression() != nullptr) + case_statement_list = comp_statement->getStatementList(); + while(case_statement_list != nullptr) { - int jump_label = (*itr)->constantFold(); - printf("\tli\t$3,%d\n\tbeq\t$2,$3,$%d_%d_switch\n\tnop\n", - jump_label, switch_count, jump_label); + case_statement_vector.push_back(case_statement_list); + case_statement_list = case_statement_list->getNext(); } - if(!is_default) + + bool is_default = false; + + for(auto itr = case_statement_vector.rbegin(); itr != case_statement_vector.rend(); ++itr) { - is_default = (*itr)->isDefault(); + if((*itr)->getExpression() != nullptr) + { + int jump_label = (*itr)->constantFold(); + printf("\tli\t$3,%d\n\tbeq\t$2,$3,$%d_%d_switch\n\tnop\n", + jump_label, switch_count, jump_label); + } + if(!is_default) + { + is_default = (*itr)->isDefault(); + } } - } - if(is_default) - printf("\tb\t$%d_default_switch\n\tnop\n", switch_count); + if(is_default) + printf("\tb\t$%d_default_switch\n\tnop\n", switch_count); - for(auto itr = case_statement_vector.rbegin(); itr != case_statement_vector.rend(); ++itr) - { - if((*itr)->getExpression() != nullptr) - printf("$%d_%d_switch:\n", switch_count, (*itr)->constantFold()); + for(auto itr = case_statement_vector.rbegin(); itr != case_statement_vector.rend(); ++itr) + { + if((*itr)->getExpression() != nullptr) + printf("$%d_%d_switch:\n", switch_count, (*itr)->constantFold()); - if((*itr)->isDefault()) - printf("$%d_default_switch:\n", switch_count); - (*itr)->linkStatement(nullptr); - (*itr)->printAsm(bindings, label_count); - } + if((*itr)->isDefault()) + printf("$%d_default_switch:\n", switch_count); + (*itr)->linkStatement(nullptr); + (*itr)->printAsm(bindings, label_count); + } - printf("$%d_break_switch:\n", switch_count); - return bindings; + printf("$%d_break_switch:\n", switch_count); + return bindings; } void SwitchStatement::countVariables(int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(label_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(label_count); - statement_->countVariables(label_count); + statement_->countVariables(label_count); } void SwitchStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - statement_->countArguments(argument_count); - int previous_argument_count = argument_count; - condition_->countArguments(argument_count); + statement_->countArguments(argument_count); + int previous_argument_count = argument_count; + condition_->countArguments(argument_count); - if(previous_argument_count > argument_count) - argument_count = previous_argument_count; + if(previous_argument_count > argument_count) + argument_count = previous_argument_count; } void SwitchStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; + int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } + + depth_count = 1; + statement_->countExpressionDepth(depth_count); if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + depth_count = previous_depth_count; previous_depth_count = depth_count; - } - - depth_count = 1; - statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - depth_count = 1; - condition_->expressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + depth_count = 1; + condition_->expressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; } // Expression Statement definition ExpressionStatement::ExpressionStatement(Expression *expr) - : Statement(), expression_(expr) + : Statement(), expression_(expr) {} void ExpressionStatement::print() const { - if(next_statement_ != nullptr) - next_statement_->print(); + if(next_statement_ != nullptr) + next_statement_->print(); - printf("Expression Statement\n"); - if(expression_ != nullptr) - expression_->print(); + printf("Expression Statement\n"); + if(expression_ != nullptr) + expression_->print(); } void ExpressionStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); } Bindings ExpressionStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - if(expression_ != nullptr) - expression_->printAsm(bindings, label_count); + if(expression_ != nullptr) + expression_->printAsm(bindings, label_count); - return bindings; + return bindings; } void ExpressionStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); } void ExpressionStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - int tmp_argument_count = argument_count; + int tmp_argument_count = argument_count; - if(expression_ != nullptr) - expression_->countArguments(argument_count); + if(expression_ != nullptr) + expression_->countArguments(argument_count); - if(tmp_argument_count > argument_count) - argument_count = tmp_argument_count; + if(tmp_argument_count > argument_count) + argument_count = tmp_argument_count; } void ExpressionStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; + int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - } + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } - if(expression_ != nullptr) - { - depth_count = 1; - expression_->expressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - } + if(expression_ != nullptr) + { + depth_count = 1; + expression_->expressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + } } @@ -593,87 +593,87 @@ void JumpStatement::print() const void JumpStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); } void JumpStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); } void JumpStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); } void JumpStatement::countExpressionDepth(int &depth_count) const { - if(next_statement_ != nullptr) - next_statement_->countExpressionDepth(depth_count); + if(next_statement_ != nullptr) + next_statement_->countExpressionDepth(depth_count); } // Return statement definition ReturnStatement::ReturnStatement(Expression *expression) - : expression_(expression) + : expression_(expression) {} Bindings ReturnStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - if(expression_ != nullptr) - expression_->printAsm(bindings, label_count); + if(expression_ != nullptr) + expression_->printAsm(bindings, label_count); - printf("\tj\t0f\n\tnop\n"); + printf("\tj\t0f\n\tnop\n"); - return bindings; + return bindings; } void ReturnStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); } void ReturnStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - int tmp_argument_count = argument_count; + int tmp_argument_count = argument_count; - if(expression_ != nullptr) - expression_->countArguments(argument_count); + if(expression_ != nullptr) + expression_->countArguments(argument_count); - if(tmp_argument_count > argument_count) - argument_count = tmp_argument_count; + if(tmp_argument_count > argument_count) + argument_count = tmp_argument_count; } void ReturnStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; + int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - } + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } - if(expression_ != nullptr) - { - depth_count = 1; - expression_->expressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - } + if(expression_ != nullptr) + { + depth_count = 1; + expression_->expressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + } } @@ -684,11 +684,11 @@ BreakStatement::BreakStatement() Bindings BreakStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - printf("\tb\t%s\n\tnop\n", bindings.breakLabel().c_str()); - return bindings; + printf("\tb\t%s\n\tnop\n", bindings.breakLabel().c_str()); + return bindings; } @@ -699,34 +699,34 @@ ContinueStatement::ContinueStatement() Bindings ContinueStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - printf("\tb\t%s\n\tnop\n", bindings.continueLabel().c_str()); - return bindings; + printf("\tb\t%s\n\tnop\n", bindings.continueLabel().c_str()); + return bindings; } // Goto statement GotoStatement::GotoStatement(const std::string &label) - : label_(label) + : label_(label) {} Bindings GotoStatement::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - printf("\tb\t%s\n\tnop\n", label_.c_str()); - return bindings; + printf("\tb\t%s\n\tnop\n", label_.c_str()); + return bindings; } // Iteration Statement definition IterationStatement::IterationStatement(Expression *condition, Statement *statement) - : condition_(condition), statement_(statement) + : condition_(condition), statement_(statement) {} void IterationStatement::print() const @@ -734,107 +734,107 @@ void IterationStatement::print() const void IterationStatement::printXml() const { - if(next_statement_ != nullptr) - next_statement_->printXml(); + if(next_statement_ != nullptr) + next_statement_->printXml(); - if(statement_ != nullptr) - statement_->printXml(); + if(statement_ != nullptr) + statement_->printXml(); } void IterationStatement::countVariables(int &var_count) const { - if(next_statement_ != nullptr) - next_statement_->countVariables(var_count); + if(next_statement_ != nullptr) + next_statement_->countVariables(var_count); - if(statement_ != nullptr) - statement_->countVariables(var_count); + if(statement_ != nullptr) + statement_->countVariables(var_count); } void IterationStatement::countArguments(int &argument_count) const { - if(next_statement_ != nullptr) - next_statement_->countArguments(argument_count); + if(next_statement_ != nullptr) + next_statement_->countArguments(argument_count); - if(statement_ != nullptr) - statement_->countArguments(argument_count); + if(statement_ != nullptr) + statement_->countArguments(argument_count); } void IterationStatement::countExpressionDepth(int &depth_count) const { - int previous_depth_count = depth_count; + int previous_depth_count = depth_count; - if(next_statement_ != nullptr) - { - next_statement_->countExpressionDepth(depth_count); + if(next_statement_ != nullptr) + { + next_statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; + previous_depth_count = depth_count; + } + + depth_count = 1; + condition_->expressionDepth(depth_count); if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + depth_count = previous_depth_count; previous_depth_count = depth_count; - } - - depth_count = 1; - condition_->expressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; - previous_depth_count = depth_count; - statement_->countExpressionDepth(depth_count); - if(previous_depth_count > depth_count) - depth_count = previous_depth_count; + statement_->countExpressionDepth(depth_count); + if(previous_depth_count > depth_count) + depth_count = previous_depth_count; } // While Loop definition WhileLoop::WhileLoop(Expression *condition, Statement *statement, const bool &is_while) - : IterationStatement(condition, statement), is_while_(is_while) + : IterationStatement(condition, statement), is_while_(is_while) {} Bindings WhileLoop::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - Bindings initial_bindings = bindings; + Bindings initial_bindings = bindings; - int while_label = label_count++; + int while_label = label_count++; - bindings.continueLabel("$"+std::to_string(while_label)+"_while_cond"); - bindings.breakLabel("$"+std::to_string(while_label)+"_while_break"); + bindings.continueLabel("$"+std::to_string(while_label)+"_while_cond"); + bindings.breakLabel("$"+std::to_string(while_label)+"_while_break"); - if(is_while_) - printf("\tb\t$%d_while_cond\n\tnop\n", while_label); - printf("$%d_while_body:\n", while_label); - statement_->printAsm(bindings, label_count); - printf("$%d_while_cond:\n", while_label); - condition_->printAsm(bindings, label_count); - printf("\tbne\t$2,$0,$%d_while_body\n\tnop\n$%d_while_break:\n", while_label, while_label); + if(is_while_) + printf("\tb\t$%d_while_cond\n\tnop\n", while_label); + printf("$%d_while_body:\n", while_label); + statement_->printAsm(bindings, label_count); + printf("$%d_while_cond:\n", while_label); + condition_->printAsm(bindings, label_count); + printf("\tbne\t$2,$0,$%d_while_body\n\tnop\n$%d_while_break:\n", while_label, while_label); - return initial_bindings; + return initial_bindings; } ForLoop::ForLoop(Expression *initializer, Expression *condition, Expression *incrementer, Statement *statement) - : IterationStatement(condition, statement), initializer_(initializer), incrementer_(incrementer) + : IterationStatement(condition, statement), initializer_(initializer), incrementer_(incrementer) {} Bindings ForLoop::printAsm(Bindings bindings, int &label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); + if(next_statement_ != nullptr) + next_statement_->printAsm(bindings, label_count); - Bindings initial_bindings = bindings; + Bindings initial_bindings = bindings; - int for_label = label_count++; + int for_label = label_count++; - bindings.continueLabel("$"+std::to_string(for_label)+"_for_cond"); - bindings.breakLabel("$"+std::to_string(for_label)+"_for_break"); + bindings.continueLabel("$"+std::to_string(for_label)+"_for_cond"); + bindings.breakLabel("$"+std::to_string(for_label)+"_for_break"); - initializer_->printAsm(bindings, label_count); - printf("\tb\t$%d_for_cond\n\tnop\n$%d_for_body:\n", for_label, for_label); - statement_->printAsm(bindings, label_count); - incrementer_->printAsm(bindings, label_count); - printf("$%d_for_cond:\n", for_label); - condition_->printAsm(bindings, label_count); - printf("\tbne\t$2,$0,$%d_for_body\n\tnop\n$%d_for_break:\n", for_label, for_label); + initializer_->printAsm(bindings, label_count); + printf("\tb\t$%d_for_cond\n\tnop\n$%d_for_body:\n", for_label, for_label); + statement_->printAsm(bindings, label_count); + incrementer_->printAsm(bindings, label_count); + printf("$%d_for_cond:\n", for_label); + condition_->printAsm(bindings, label_count); + printf("\tbne\t$2,$0,$%d_for_body\n\tnop\n$%d_for_break:\n", for_label, for_label); - return initial_bindings; + return initial_bindings; } diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp index 3dd8a7b..63f897c 100644 --- a/c_compiler/src/translation_unit.cpp +++ b/c_compiler/src/translation_unit.cpp @@ -6,36 +6,36 @@ TranslationUnit::TranslationUnit(Node* external_declaration) { - push(external_declaration); + push(external_declaration); } void TranslationUnit::print() const { - for(auto& node : translation_unit_) { - node->print(); - } + for(auto& node : translation_unit_) { + node->print(); + } } void TranslationUnit::printXml() const { - printf("\n\n"); - for(auto& node : translation_unit_) { - node->printXml(); - } - printf("\n"); + printf("\n\n"); + for(auto& node : translation_unit_) { + node->printXml(); + } + printf("\n"); } Bindings TranslationUnit::printAsm(Bindings bindings, int& label_count) const { - for(auto& node : translation_unit_) { - bindings = node->printAsm(bindings, label_count); - } + for(auto& node : translation_unit_) { + bindings = node->printAsm(bindings, label_count); + } - return bindings; + return bindings; } void TranslationUnit::push(Node* external_declaration) { - NodePtr node_ptr(external_declaration); - translation_unit_.push_back(node_ptr); + NodePtr node_ptr(external_declaration); + translation_unit_.push_back(node_ptr); } diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp index 675230c..9432326 100644 --- a/c_compiler/src/type.cpp +++ b/c_compiler/src/type.cpp @@ -8,39 +8,39 @@ void Type::setSigned(bool) { - throw std::runtime_error("Error : cannot set sign"); + throw std::runtime_error("Error : cannot set sign"); } void Type::setExtern(bool) { - throw std::runtime_error("Error : cannot set extern"); + throw std::runtime_error("Error : cannot set extern"); } void Type::setStatic(bool) { - throw std::runtime_error("Error : cannot set static"); + throw std::runtime_error("Error : cannot set static"); } void Type::setConst(bool) { - throw std::runtime_error("Error : cannot set const"); + throw std::runtime_error("Error : cannot set const"); } void Type::setSize(int) { - throw std::runtime_error("Error : cannot set size"); + throw std::runtime_error("Error : cannot set size"); } // Array definition Array::Array(const int &size, TypePtr type) - : size_(size), type_(type) + : size_(size), type_(type) {} void Array::print() const { - printf("Array\n"); + printf("Array\n"); } void Array::printXml() const @@ -48,63 +48,63 @@ void Array::printXml() const Bindings Array::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } TypePtr Array::type() { - return type_; + return type_; } TypePtr Array::type(Type *type_ptr) { - TypePtr sh_type_ptr(type_ptr); - type_ = sh_type_ptr; - return type_; + TypePtr sh_type_ptr(type_ptr); + type_ = sh_type_ptr; + return type_; } TypePtr Array::type(TypePtr type_ptr) { - type_ = type_ptr; - return type_; + type_ = type_ptr; + return type_; } void Array::increaseStackPosition(Bindings &bindings) const { - for(int i = 0; i < size_; ++i) - { - type_->increaseStackPosition(bindings); - } + for(int i = 0; i < size_; ++i) + { + type_->increaseStackPosition(bindings); + } } void Array::load() const { - type_->load(); + type_->load(); } void Array::load(const int ®, const int &position) const { - type_->load(reg, position); + type_->load(reg, position); } void Array::store() const { - type_->store(); + type_->store(); } void Array::store(const int &position) const { - type_->store(position); + type_->store(position); } void Array::store(const int ®, const int &position) const { - type_->store(reg, position); + type_->store(reg, position); } int Array::getSize() const { - return type_->getSize(); + return type_->getSize(); } @@ -115,7 +115,7 @@ Pointer::Pointer() void Pointer::print() const { - printf("Pointer\n"); + printf("Pointer\n"); } void Pointer::printXml() const @@ -123,99 +123,99 @@ void Pointer::printXml() const Bindings Pointer::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } TypePtr Pointer::type() { - return type_; + return type_; } TypePtr Pointer::type(Type *type_ptr) { - TypePtr sh_type_ptr(type_ptr); - type_ = sh_type_ptr; - return type_; + TypePtr sh_type_ptr(type_ptr); + type_ = sh_type_ptr; + return type_; } TypePtr Pointer::type(TypePtr type_ptr) { - type_ = type_ptr; - return type_; + type_ = type_ptr; + return type_; } void Pointer::increaseStackPosition(Bindings &bindings) const { - bindings.increaseStackPosition(4); + bindings.increaseStackPosition(4); } void Pointer::load() const { - printf("\tlw\t$2,0($t0)\n"); + printf("\tlw\t$2,0($t0)\n"); } void Pointer::load(const int ®, const int &position) const { - printf("\tlw\t$%d,%d($fp)\n", reg, position); + printf("\tlw\t$%d,%d($fp)\n", reg, position); } void Pointer::store() const { - printf("\tsw\t$2,0($t0)\n"); + printf("\tsw\t$2,0($t0)\n"); } void Pointer::store(const int &position) const { - printf("\tsw\t$2,%d($fp)\n", position); + printf("\tsw\t$2,%d($fp)\n", position); } void Pointer::store(const int ®, const int &position) const { - printf("\tsw\t$%d,%d($fp)\n", reg, position); + printf("\tsw\t$%d,%d($fp)\n", reg, position); } int Pointer::getSize() const { - return type_->getSize(); + return type_->getSize(); } void Pointer::pointerLoad() const { - type_->load(); + type_->load(); } void Pointer::pointerLoad(const int ®, const int &position) const { - type_->load(reg, position); + type_->load(reg, position); } void Pointer::pointerStore() const { - type_->store(); + type_->store(); } void Pointer::pointerStore(const int &position) const { - type_->store(position); + type_->store(position); } void Pointer::pointerStore(const int ®, const int &position) const { - type_->store(reg, position); + type_->store(reg, position); } // TypeContainer definition TypeContainer::TypeContainer() - : type_(nullptr), size_(32), extern_(false), static_(false), const_(false), signed_(true) + : type_(nullptr), size_(32), extern_(false), static_(false), const_(false), signed_(true) {} void TypeContainer::print() const { - printf("TypeContainer : "); - type_->print(); + printf("TypeContainer : "); + type_->print(); } void TypeContainer::printXml() const @@ -223,87 +223,87 @@ void TypeContainer::printXml() const Bindings TypeContainer::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } TypePtr TypeContainer::type() { - return type_; + return type_; } TypePtr TypeContainer::type(Type *type_ptr) { - TypePtr new_type_ptr(type_ptr); - type_ = new_type_ptr; + TypePtr new_type_ptr(type_ptr); + type_ = new_type_ptr; - return type_; + return type_; } TypePtr TypeContainer::type(TypePtr type_ptr) { - type_ = type_ptr; + type_ = type_ptr; - return type_; + return type_; } void TypeContainer::increaseStackPosition(Bindings &bindings) const { - type_->increaseStackPosition(bindings); + type_->increaseStackPosition(bindings); } void TypeContainer::load() const { - type_->load(); + type_->load(); } void TypeContainer::load(const int ®, const int &position) const { - type_->load(reg, position); + type_->load(reg, position); } void TypeContainer::store() const { - type_->store(); + type_->store(); } void TypeContainer::store(const int &position) const { - type_->store(position); + type_->store(position); } void TypeContainer::store(const int ®, const int &position) const { - type_->store(reg, position); + type_->store(reg, position); } int TypeContainer::getSize() const { - return type_->getSize(); + return type_->getSize(); } void TypeContainer::setSigned(bool _signed) { - signed_ = _signed; + signed_ = _signed; } void TypeContainer::setExtern(bool _extern) { - extern_ = _extern; + extern_ = _extern; } void TypeContainer::setStatic(bool _static) { - static_ = _static; + static_ = _static; } void TypeContainer::setConst(bool _const) { - const_ = _const; + const_ = _const; } void TypeContainer::setSize(int size) { - size_ = size; + size_ = size; } @@ -311,17 +311,17 @@ void TypeContainer::setSize(int size) TypePtr Specifier::type() { - throw std::runtime_error("Error : Cannot get type"); + throw std::runtime_error("Error : Cannot get type"); } TypePtr Specifier::type(Type *) { - throw std::runtime_error("Error : Cannot get type"); + throw std::runtime_error("Error : Cannot get type"); } TypePtr Specifier::type(TypePtr) { - throw std::runtime_error("Error : Cannot get type"); + throw std::runtime_error("Error : Cannot get type"); } @@ -332,7 +332,7 @@ Int::Int() void Int::print() const { - printf("Int\n"); + printf("Int\n"); } void Int::printXml() const @@ -340,42 +340,42 @@ void Int::printXml() const Bindings Int::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } void Int::increaseStackPosition(Bindings &bindings) const { - bindings.increaseStackPosition(4); + bindings.increaseStackPosition(4); } void Int::load() const { - printf("\tlw\t$2,0($t0)\n"); + printf("\tlw\t$2,0($t0)\n"); } void Int::load(const int ®, const int &position) const { - printf("\tlw\t$%d,%d($fp)\n", reg, position); + printf("\tlw\t$%d,%d($fp)\n", reg, position); } void Int::store() const { - printf("\tsw\t$2,0($t0)\n"); + printf("\tsw\t$2,0($t0)\n"); } void Int::store(const int &position) const { - printf("\tsw\t$2,%d($fp)\n", position); + printf("\tsw\t$2,%d($fp)\n", position); } void Int::store(const int ®, const int &position) const { - printf("\tsw\t$%d,%d($fp)\n", reg, position); + printf("\tsw\t$%d,%d($fp)\n", reg, position); } int Int::getSize() const { - return 4; + return 4; } @@ -386,7 +386,7 @@ Void::Void() void Void::print() const { - printf("Void\n"); + printf("Void\n"); } void Void::printXml() const @@ -394,7 +394,7 @@ void Void::printXml() const Bindings Void::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } void Void::increaseStackPosition(Bindings &) const @@ -417,7 +417,7 @@ void Void::store(const int &, const int &) const int Void::getSize() const { - return 0; + return 0; } @@ -428,7 +428,7 @@ Short::Short() void Short::print() const { - printf("Short\n"); + printf("Short\n"); } void Short::printXml() const @@ -436,42 +436,42 @@ void Short::printXml() const Bindings Short::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } void Short::increaseStackPosition(Bindings &bindings) const { - bindings.increaseStackPosition(2); + bindings.increaseStackPosition(2); } void Short::load() const { - printf("\tlhu\t$2,0($t0)\n"); + printf("\tlhu\t$2,0($t0)\n"); } void Short::load(const int ®, const int &position) const { - printf("\tlhu\t$%d,%d($fp)\n", reg, position); + printf("\tlhu\t$%d,%d($fp)\n", reg, position); } void Short::store() const { - printf("\tsh\t$2,0($t0)\n"); + printf("\tsh\t$2,0($t0)\n"); } void Short::store(const int &position) const { - printf("\tsh\t$2,%d($fp)\n", position); + printf("\tsh\t$2,%d($fp)\n", position); } void Short::store(const int ®, const int &position) const { - printf("\tsh\t$%d,%d($fp)\n", reg, position); + printf("\tsh\t$%d,%d($fp)\n", reg, position); } int Short::getSize() const { - return 2; + return 2; } @@ -482,7 +482,7 @@ Char::Char() void Char::print() const { - printf("Char\n"); + printf("Char\n"); } void Char::printXml() const @@ -490,42 +490,42 @@ void Char::printXml() const Bindings Char::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } void Char::increaseStackPosition(Bindings &bindings) const { - bindings.increaseStackPosition(1); + bindings.increaseStackPosition(1); } void Char::load() const { - printf("\tlbu\t$2,0($t0)\n"); + printf("\tlbu\t$2,0($t0)\n"); } void Char::load(const int ®, const int &position) const { - printf("\tlbu\t$%d,%d($fp)\n", reg, position); + printf("\tlbu\t$%d,%d($fp)\n", reg, position); } void Char::store() const { - printf("\tsb\t$2,0($t0)\n"); + printf("\tsb\t$2,0($t0)\n"); } void Char::store(const int &position) const { - printf("\tsb\t$2,%d($fp)\n", position); + printf("\tsb\t$2,%d($fp)\n", position); } void Char::store(const int ®, const int &position) const { - printf("\tsb\t$%d,%d($fp)\n", reg, position); + printf("\tsb\t$%d,%d($fp)\n", reg, position); } int Char::getSize() const { - return 1; + return 1; } @@ -536,7 +536,7 @@ Float::Float() void Float::print() const { - printf("Float\n"); + printf("Float\n"); } void Float::printXml() const @@ -544,40 +544,40 @@ void Float::printXml() const Bindings Float::printAsm(Bindings bindings, int &) const { - return bindings; + return bindings; } void Float::increaseStackPosition(Bindings &bindings) const { - bindings.increaseStackPosition(4); + bindings.increaseStackPosition(4); } void Float::load() const { - throw std::runtime_error("Error : Cannot load float yet"); + throw std::runtime_error("Error : Cannot load float yet"); } void Float::load(const int &, const int &) const { - throw std::runtime_error("Error : Cannot load float yet"); + throw std::runtime_error("Error : Cannot load float yet"); } void Float::store() const { - throw std::runtime_error("Error : Cannot store float yet"); + throw std::runtime_error("Error : Cannot store float yet"); } void Float::store(const int &) const { - throw std::runtime_error("Error : Cannot store float yet"); + throw std::runtime_error("Error : Cannot store float yet"); } void Float::store(const int &, const int &) const { - throw std::runtime_error("Error : Cannot store float yet"); + throw std::runtime_error("Error : Cannot store float yet"); } int Float::getSize() const { - return 4; + return 4; } diff --git a/test_deliverable/testcases/test_QUICKSORT.c b/test_deliverable/testcases/test_QUICKSORT.c new file mode 100644 index 0000000..adceccd --- /dev/null +++ b/test_deliverable/testcases/test_QUICKSORT.c @@ -0,0 +1,31 @@ +void quickSort(int*, int, int); +int partition(int*, int, int); + +void quickSort(int *a, int l, int r) +{ + int j; + + if( l < r ) + { + j = partition( a, l, r); + quickSort( a, l, j-1); + quickSort( a, j+1, r); + } + +} + +int partition(int *a, int l, int r) { + int pivot, i, j, t; + pivot = a[l]; + i = l; j = r+1; + + while( 1) + { + do ++i; while( a[i] <= pivot && i <= r ); + do --j; while( a[j] > pivot ); + if( i >= j ) break; + t = a[i]; a[i] = a[j]; a[j] = t; + } + t = a[l]; a[l] = a[j]; a[j] = t; + return j; +} diff --git a/test_deliverable/testcases/test_QUICKSORT_driver.c b/test_deliverable/testcases/test_QUICKSORT_driver.c new file mode 100644 index 0000000..9ba336e --- /dev/null +++ b/test_deliverable/testcases/test_QUICKSORT_driver.c @@ -0,0 +1,9 @@ +void quickSort(int*, int, int); + +int main() +{ + int a[9] = { 7, 12, 1, -2, 0, 15, 4, 11, 9}; + quickSort( a, 0, 8); + + return !( a[0]==-2 && a[1]==0 && a[2]==1 && a[3]==4 && a[4]==7 && a[5]==9 && a[6]==11 && a[7]==12 && a[8]==15 ); +} -- cgit