diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-26 03:03:48 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-26 03:03:48 +0100 |
commit | 19896f53d80deadcf09d3a1256524cc6f2e4adb6 (patch) | |
tree | bb3682e25e1f07b8c4790038403c0bd8d056e23d /c_compiler/src | |
parent | d08786b22e454e177e0642b5f9e4b19a12a891b1 (diff) | |
download | Compiler-19896f53d80deadcf09d3a1256524cc6f2e4adb6.tar.gz Compiler-19896f53d80deadcf09d3a1256524cc6f2e4adb6.zip |
normally other types should be working now
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/c_parser.y | 18 | ||||
-rw-r--r-- | c_compiler/src/compiler_main.cpp | 2 | ||||
-rw-r--r-- | c_compiler/src/declaration.cpp | 14 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 110 | ||||
-rw-r--r-- | c_compiler/src/function.cpp | 39 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 104 | ||||
-rw-r--r-- | c_compiler/src/translation_unit.cpp | 2 | ||||
-rw-r--r-- | c_compiler/src/type.cpp | 136 |
8 files changed, 292 insertions, 133 deletions
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 8f91ff0..1ef8d06 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -109,9 +109,21 @@ ParameterList: | ParameterList T_CMA Parameter { $3->linkDeclaration($$); $$ = $3; } ; -Parameter: DeclarationSpecifierList Declarator { $$ = new Declaration($2->getId()); delete $1; } - | DeclarationSpecifierList { $$ = new Declaration(""); } - | DeclarationSpecifierList T_MULT { $$ = new Declaration(""); delete $2; } +Parameter: DeclarationSpecifierList Declarator + { + $$ = $2; + std::shared_ptr<Type> tmp_type; + if($$->getType() == nullptr) + tmp_type = std::make_shared<TypeContainer>(); + else + tmp_type = $$->getType(); + + tmp_type->type($1->type()); + $$->setType(tmp_type); + delete $1; + } + | DeclarationSpecifierList { $$ = new Declaration(); delete $1; } + | DeclarationSpecifierList T_MULT { $$ = new Declaration(); delete $2; delete $1; } ; // Declaration diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index 4ac641c..dc2fc17 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -12,7 +12,7 @@ int main(int, char**) { std::unique_ptr<Node> ast(parseAST()); VariableStackBindings bindings; - unsigned label_count = 0; + int label_count = 0; ast->printAsm(bindings, label_count); } catch(const std::exception& e) diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 0298fe6..95db2b5 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -38,7 +38,7 @@ void Declaration::printXml() const printf("<Variable id=\"%s\" />", id_.c_str()); } -VariableStackBindings Declaration::printAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings Declaration::printAsm(VariableStackBindings bindings, int& label_count) const { (void)label_count; if(!extern_declaration_) @@ -54,7 +54,7 @@ VariableStackBindings Declaration::printAsm(VariableStackBindings bindings, unsi return bindings; } -VariableStackBindings Declaration::localAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings Declaration::localAsm(VariableStackBindings bindings, int& label_count) const { if(next_declaration_ != nullptr) bindings = next_declaration_->localAsm(bindings, label_count); @@ -77,7 +77,7 @@ VariableStackBindings Declaration::localAsm(VariableStackBindings bindings, unsi return bindings; } -void Declaration::countDeclarations(unsigned &declaration_count) const +void Declaration::countDeclarations(int &declaration_count) const { if(next_declaration_ != nullptr) next_declaration_->countDeclarations(declaration_count); @@ -144,16 +144,16 @@ TypePtr Declaration::getType() const // Array declaration class -ArrayDeclaration::ArrayDeclaration(const std::string &id, ExpressionPtr initializer, const unsigned &size) +ArrayDeclaration::ArrayDeclaration(const std::string &id, ExpressionPtr initializer, const int &size) : Declaration(id, initializer), size_(size) {} -VariableStackBindings ArrayDeclaration::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ArrayDeclaration::printAsm(VariableStackBindings bindings, int &label_count) const { return bindings; } -VariableStackBindings ArrayDeclaration::localAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ArrayDeclaration::localAsm(VariableStackBindings bindings, int &label_count) const { if(next_declaration_ != nullptr) bindings = next_declaration_->localAsm(bindings, label_count); @@ -190,7 +190,7 @@ VariableStackBindings ArrayDeclaration::localAsm(VariableStackBindings bindings, return bindings; } -void ArrayDeclaration::countDeclarations(unsigned &declaration_count) const +void ArrayDeclaration::countDeclarations(int &declaration_count) const { if(next_declaration_ != nullptr) next_declaration_->countDeclarations(declaration_count); diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index fe5f1cf..2dc3a54 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -22,12 +22,12 @@ void Expression::print() const void Expression::printXml() const {} -void Expression::countArguments(unsigned &) const +void Expression::countArguments(int &) const { // by default don't do anything to the count } -void Expression::expressionDepth(unsigned &depth_count) const +void Expression::expressionDepth(int &depth_count) const { if(next_expression_ != nullptr) next_expression_->expressionDepth(depth_count); @@ -72,10 +72,10 @@ int OperationExpression::constantFold() const throw std::runtime_error("Error : Cannot constant fold expression"); } -void OperationExpression::expressionDepth(unsigned &depth_count) const +void OperationExpression::expressionDepth(int &depth_count) const { - unsigned lhs_depth_count = depth_count; - unsigned 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); @@ -101,7 +101,7 @@ ExpressionPtr OperationExpression::getRhs() const return rhs_; } -void OperationExpression::evaluateExpression(VariableStackBindings bindings, unsigned &label_count) const +void OperationExpression::evaluateExpression(VariableStackBindings bindings, int &label_count) const { // I can just evaluate the lhs with the same entry stack position lhs_->printAsm(bindings, label_count); @@ -115,14 +115,14 @@ void OperationExpression::evaluateExpression(VariableStackBindings bindings, uns // 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\tlw\t$3,%d($fp)\n", - lhs_stack_position, bindings.currentExpressionStackPosition()); + lhs_->getType(bindings)->load(2, lhs_stack_position); + printf("\tlw\t$3,%d($fp)\n", bindings.currentExpressionStackPosition()); } // Unary expression definition -void UnaryExpression::stackPosition(VariableStackBindings, unsigned &) const +void UnaryExpression::stackPosition(VariableStackBindings, int &) const { throw std::runtime_error("Error : Cannot get stack position of expression"); } @@ -134,15 +134,16 @@ PostfixArrayElement::PostfixArrayElement(Expression *postfix_expression, Express : postfix_expression_(postfix_expression), index_expression_(index_expression) {} -VariableStackBindings PostfixArrayElement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings PostfixArrayElement::printAsm(VariableStackBindings bindings, int &label_count) const { stackPosition(bindings, label_count); - printf("\tlw\t$2,0($t0)\n"); + TypePtr type_ptr = postfix_expression_->getType(bindings); + type_ptr->load(); printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); return bindings; } -void PostfixArrayElement::stackPosition(VariableStackBindings bindings, unsigned &label_count) const +void PostfixArrayElement::stackPosition(VariableStackBindings bindings, int &label_count) const { index_expression_->printAsm(bindings, label_count); @@ -153,7 +154,7 @@ void PostfixArrayElement::stackPosition(VariableStackBindings bindings, unsigned printf("\tli\t$3,4\n\tmul\t$2,$2,$3\n\taddu\t$t0,$t0,$2\n"); } -void PostfixArrayElement::expressionDepth(unsigned &depth_count) const +void PostfixArrayElement::expressionDepth(int &depth_count) const { if(nextExpression() != nullptr) nextExpression()->expressionDepth(depth_count); @@ -174,11 +175,11 @@ PostfixFunctionCall::PostfixFunctionCall(Expression *argument_expression_list) : argument_expression_list_(argument_expression_list) {} -VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindings, int &label_count) const { std::vector<ExpressionPtr> argument_vector; ExpressionPtr current_argument = argument_expression_list_; - unsigned argument_counter = 0; + int argument_counter = 0; while(current_argument != nullptr) { @@ -193,7 +194,7 @@ VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindin if(argument_counter < 4) printf("\tmove\t$%d,$2\n", 4+argument_counter); else - printf("\tsw\t$2,%d($fp)\n", 4*argument_counter); + (*itr)->getType(bindings)->store(4*argument_counter); argument_counter++; } @@ -203,7 +204,7 @@ VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindin return bindings; } -void PostfixFunctionCall::countArguments(unsigned &argument_count) const +void PostfixFunctionCall::countArguments(int &argument_count) const { ExpressionPtr current_argument = argument_expression_list_; @@ -222,7 +223,7 @@ void PostfixFunctionCall::setPostfixExpression(Expression *postfix_expression) postfix_expression_ = expression_ptr; } -void PostfixFunctionCall::expressionDepth(unsigned &depth_count) const +void PostfixFunctionCall::expressionDepth(int &depth_count) const { if(argument_expression_list_ != nullptr) argument_expression_list_->expressionDepth(depth_count); @@ -240,7 +241,7 @@ PostfixPostIncDecExpression::PostfixPostIncDecExpression(const std::string &_ope : operator_(_operator), postfix_expression_(postfix_expression) {} -VariableStackBindings PostfixPostIncDecExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings PostfixPostIncDecExpression::printAsm(VariableStackBindings bindings, int &label_count) const { postfix_expression_->printAsm(bindings, label_count); if(operator_ == "++") @@ -255,8 +256,20 @@ VariableStackBindings PostfixPostIncDecExpression::printAsm(VariableStackBinding printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); unary_expression->stackPosition(bindings, label_count); - printf("\tsw\t$3,0($t0)\n"); - + TypePtr tmp_ptr = postfix_expression_->getType(bindings); + if(std::dynamic_pointer_cast<Char>(tmp_ptr)) + { + printf("\tsb\t$3,0($t0)\n"); + } + else if(std::dynamic_pointer_cast<Short>(tmp_ptr)) + { + printf("\tsh\t$3,0($t0)\n"); + } + else + { + printf("\tsw\t$3,0($t0)\n"); + } + return bindings; } @@ -272,7 +285,7 @@ UnaryPreIncDecExpression::UnaryPreIncDecExpression(const std::string &_operator, : operator_(_operator), unary_expression_(unary_expression) {} -VariableStackBindings UnaryPreIncDecExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings UnaryPreIncDecExpression::printAsm(VariableStackBindings bindings, int &label_count) const { unary_expression_->printAsm(bindings, label_count); if(operator_ == "++") @@ -287,7 +300,7 @@ VariableStackBindings UnaryPreIncDecExpression::printAsm(VariableStackBindings b printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); unary_expression->stackPosition(bindings, label_count); - printf("\tsw\t$2,0($t0)\n"); + unary_expression_->getType(bindings)->store(); return bindings; } @@ -303,7 +316,7 @@ OperatorUnaryExpression::OperatorUnaryExpression(const std::string &_operator, E : operator_(_operator), cast_expression_(cast_expression) {} -VariableStackBindings OperatorUnaryExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings OperatorUnaryExpression::printAsm(VariableStackBindings bindings, int &label_count) const { cast_expression_->printAsm(bindings, label_count); if(operator_ == "!") @@ -335,7 +348,7 @@ VariableStackBindings OperatorUnaryExpression::printAsm(VariableStackBindings bi return bindings; } -void OperatorUnaryExpression::stackPosition(VariableStackBindings bindings, unsigned &label_count) const +void OperatorUnaryExpression::stackPosition(VariableStackBindings bindings, int &label_count) const { if(operator_ == "*") { @@ -358,12 +371,12 @@ CastExpression::CastExpression(Type *type, Expression *expression) : type_(type), expression_(expression) {} -VariableStackBindings CastExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings CastExpression::printAsm(VariableStackBindings bindings, int &label_count) const { return bindings; } -void CastExpression::expressionDepth(unsigned &depth_count) const +void CastExpression::expressionDepth(int &depth_count) const { if(nextExpression() != nullptr) nextExpression()->expressionDepth(depth_count); @@ -383,7 +396,7 @@ AdditiveExpression::AdditiveExpression(Expression *lhs, const std::string &_oper : OperationExpression(lhs, rhs), operator_(_operator) {} -VariableStackBindings AdditiveExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings AdditiveExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); @@ -417,7 +430,7 @@ MultiplicativeExpression::MultiplicativeExpression(Expression *lhs, const std::s : OperationExpression(lhs, rhs), operator_(_operator) {} -VariableStackBindings MultiplicativeExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings MultiplicativeExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); @@ -462,7 +475,7 @@ ShiftExpression::ShiftExpression(Expression* lhs, const std::string &_operator, : OperationExpression(lhs, rhs), operator_(_operator) {} -VariableStackBindings ShiftExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ShiftExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); @@ -498,7 +511,7 @@ RelationalExpression::RelationalExpression(Expression* lhs, const std::string &_ : OperationExpression(lhs, rhs), operator_(_operator) {} -VariableStackBindings RelationalExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings RelationalExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); @@ -553,7 +566,7 @@ EqualityExpression::EqualityExpression(Expression *lhs, const std::string &_oper : OperationExpression(lhs, rhs), operator_(_operator) {} -VariableStackBindings EqualityExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings EqualityExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); printf("\txor\t$2,$2,$3\n"); @@ -590,7 +603,7 @@ AndExpression::AndExpression(Expression *lhs, Expression *rhs) : OperationExpression(lhs, rhs) {} -VariableStackBindings AndExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings AndExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); printf("\tand\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); @@ -609,7 +622,7 @@ ExclusiveOrExpression::ExclusiveOrExpression(Expression *lhs, Expression *rhs) : OperationExpression(lhs, rhs) {} -VariableStackBindings ExclusiveOrExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ExclusiveOrExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); printf("\txor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); @@ -628,7 +641,7 @@ InclusiveOrExpression::InclusiveOrExpression(Expression *lhs, Expression *rhs) : OperationExpression(lhs, rhs) {} -VariableStackBindings InclusiveOrExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings InclusiveOrExpression::printAsm(VariableStackBindings bindings, int &label_count) const { evaluateExpression(bindings, label_count); printf("\tor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); @@ -647,9 +660,9 @@ LogicalAndExpression::LogicalAndExpression(Expression *lhs, Expression *rhs) : OperationExpression(lhs, rhs) {} -VariableStackBindings LogicalAndExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings LogicalAndExpression::printAsm(VariableStackBindings bindings, int &label_count) const { - unsigned log_and = label_count++; + 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); @@ -672,9 +685,9 @@ LogicalOrExpression::LogicalOrExpression(Expression *lhs, Expression *rhs) : OperationExpression(lhs, rhs) {} -VariableStackBindings LogicalOrExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings LogicalOrExpression::printAsm(VariableStackBindings bindings, int &label_count) const { - unsigned log_or = label_count++; + 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); @@ -700,7 +713,7 @@ ConditionalExpression::ConditionalExpression(Expression *logical_or, conditional_expression_(conditional_expression) {} -VariableStackBindings ConditionalExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ConditionalExpression::printAsm(VariableStackBindings bindings, int &label_count) const { return bindings; } @@ -721,7 +734,7 @@ AssignmentExpression::AssignmentExpression(ExpressionPtr lhs, Expression *rhs) : OperationExpression(lhs, rhs) {} -VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindings, int &label_count) const { // TODO add stack and store results in there, also for addition and multiplication. @@ -740,9 +753,10 @@ VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindi // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways 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); - printf("\tsw\t$2,0($t0)\n"); + // now the result of the rhs will be in that stack position, so we can load it into $2 + TypePtr tmp_ptr = lhs_->getType(bindings); + tmp_ptr->load(2, expression_stack_position); + tmp_ptr->store(); return bindings; } @@ -753,7 +767,7 @@ Identifier::Identifier(const std::string &id) : id_(id) {} -VariableStackBindings Identifier::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Identifier::printAsm(VariableStackBindings bindings, int &) const { if(bindings.bindingExists(id_)) { @@ -771,7 +785,7 @@ VariableStackBindings Identifier::printAsm(VariableStackBindings bindings, unsig } else { - printf("\tlw\t$2,%d($fp)\n", stack_position); + bindings.getType(id_)->load(2, stack_position); } } } @@ -784,7 +798,7 @@ VariableStackBindings Identifier::printAsm(VariableStackBindings bindings, unsig return bindings; } -void Identifier::stackPosition(VariableStackBindings bindings, unsigned &) const +void Identifier::stackPosition(VariableStackBindings bindings, int &) const { if(bindings.bindingExists(id_)) { @@ -812,7 +826,7 @@ Constant::Constant(const int32_t &constant) : constant_(constant) {} -VariableStackBindings Constant::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Constant::printAsm(VariableStackBindings 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_); diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 71a4f3f..8ce3be0 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -47,29 +47,29 @@ void Function::printXml() const printf("</Function>\n"); } -VariableStackBindings Function::printAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings Function::printAsm(VariableStackBindings bindings, int& label_count) const { VariableStackBindings original_bindings = bindings; // Counting all the variables being declared in the function - unsigned variable_count = 0; + int variable_count = 0; if(statement_ != nullptr) statement_->countVariables(variable_count); - unsigned max_argument_count = 0; + int max_argument_count = 0; // Count the maximum number of arguments statement_->countArguments(max_argument_count); - unsigned max_depth = 0; + int max_depth = 0; statement_->countExpressionDepth(max_depth); if(max_argument_count < 4) max_argument_count = 4; - unsigned parameter_count = 0; + int parameter_count = 0; countParameters(parameter_count); // This adds 2 to store the frame pointer and the return address - unsigned memory_needed = 4*(variable_count+max_argument_count+max_depth+2); + int memory_needed = 4*(variable_count+max_argument_count+max_depth+2); // make frame double word aligned if(memory_needed % 8 != 0) @@ -94,7 +94,7 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings, unsigne return original_bindings; } -void Function::printParameterAsm(VariableStackBindings& bindings, unsigned& frame_offset) const +void Function::printParameterAsm(VariableStackBindings& bindings, int& frame_offset) const { std::vector<DeclarationPtr> parameter_vector; DeclarationPtr parameter_list = parameter_list_; @@ -107,14 +107,33 @@ void Function::printParameterAsm(VariableStackBindings& bindings, unsigned& fram for(auto itr = parameter_vector.rbegin(); itr != parameter_vector.rend(); ++itr) { - unsigned i = itr-parameter_vector.rbegin(); + 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("\tsw\t$%d,%d($fp)\n", 4+i, i*4+frame_offset); + { + parameter_type->store(4+i, 4*i+frame_offset); + } + else + { + if(std::dynamic_pointer_cast<TypeContainer>(parameter_type) != nullptr) + { + if(std::dynamic_pointer_cast<Char>(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<Short>(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(unsigned& parameter_count) const +void Function::countParameters(int& parameter_count) const { DeclarationPtr parameter_list = parameter_list_; diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index 35bda61..0b871e8 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -58,7 +58,7 @@ void LabelStatement::printXml() const next_statement_->printXml(); } -VariableStackBindings LabelStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings LabelStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -70,7 +70,7 @@ VariableStackBindings LabelStatement::printAsm(VariableStackBindings bindings, u return bindings; } -void LabelStatement::countVariables(unsigned &var_count) const +void LabelStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); @@ -78,7 +78,7 @@ void LabelStatement::countVariables(unsigned &var_count) const statement_->countVariables(var_count); } -void LabelStatement::countArguments(unsigned &argument_count) const +void LabelStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); @@ -86,9 +86,9 @@ void LabelStatement::countArguments(unsigned &argument_count) const statement_->countArguments(argument_count); } -void LabelStatement::countExpressionDepth(unsigned &depth_count) const +void LabelStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { next_statement_->countExpressionDepth(depth_count); @@ -123,7 +123,7 @@ void CaseStatement::printXml() const next_statement_->printXml(); } -VariableStackBindings CaseStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings CaseStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -133,7 +133,7 @@ VariableStackBindings CaseStatement::printAsm(VariableStackBindings bindings, un return bindings; } -void CaseStatement::countVariables(unsigned &var_count) const +void CaseStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); @@ -141,7 +141,7 @@ void CaseStatement::countVariables(unsigned &var_count) const statement_->countVariables(var_count); } -void CaseStatement::countArguments(unsigned &argument_count) const +void CaseStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); @@ -149,9 +149,9 @@ void CaseStatement::countArguments(unsigned &argument_count) const statement_->countArguments(argument_count); } -void CaseStatement::countExpressionDepth(unsigned &depth_count) const +void CaseStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { next_statement_->countExpressionDepth(depth_count); @@ -216,7 +216,7 @@ void CompoundStatement::printXml() const printf("</Scope>\n"); } -VariableStackBindings CompoundStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings CompoundStatement::printAsm(VariableStackBindings bindings, int &label_count) const { VariableStackBindings outer_scope_bindings = bindings; @@ -232,7 +232,7 @@ VariableStackBindings CompoundStatement::printAsm(VariableStackBindings bindings return outer_scope_bindings; } -void CompoundStatement::countVariables(unsigned &var_count) const +void CompoundStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); @@ -244,7 +244,7 @@ void CompoundStatement::countVariables(unsigned &var_count) const statement_->countVariables(var_count); } -void CompoundStatement::countArguments(unsigned &argument_count) const +void CompoundStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); @@ -253,9 +253,9 @@ void CompoundStatement::countArguments(unsigned &argument_count) const statement_->countArguments(argument_count); } -void CompoundStatement::countExpressionDepth(unsigned &depth_count) const +void CompoundStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { next_statement_->countExpressionDepth(depth_count); @@ -303,12 +303,12 @@ void IfElseStatement::printXml() const else_->printXml(); } -VariableStackBindings IfElseStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings IfElseStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); - unsigned 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); @@ -325,7 +325,7 @@ VariableStackBindings IfElseStatement::printAsm(VariableStackBindings bindings, return bindings; } -void IfElseStatement::countVariables(unsigned &var_count) const +void IfElseStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); @@ -337,7 +337,7 @@ void IfElseStatement::countVariables(unsigned &var_count) const else_->countVariables(var_count); } -void IfElseStatement::countArguments(unsigned &argument_count) const +void IfElseStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); @@ -349,9 +349,9 @@ void IfElseStatement::countArguments(unsigned &argument_count) const else_->countArguments(argument_count); } -void IfElseStatement::countExpressionDepth(unsigned &depth_count) const +void IfElseStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { @@ -406,9 +406,9 @@ void SwitchStatement::printXml() const statement_->printXml(); } -VariableStackBindings SwitchStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings SwitchStatement::printAsm(VariableStackBindings bindings, int &label_count) const { - unsigned switch_count = label_count++; + int switch_count = label_count++; std::shared_ptr<CompoundStatement> comp_statement; StatementPtr case_statement_list; std::vector<StatementPtr> case_statement_vector; @@ -465,7 +465,7 @@ VariableStackBindings SwitchStatement::printAsm(VariableStackBindings bindings, return bindings; } -void SwitchStatement::countVariables(unsigned &label_count) const +void SwitchStatement::countVariables(int &label_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(label_count); @@ -473,22 +473,22 @@ void SwitchStatement::countVariables(unsigned &label_count) const statement_->countVariables(label_count); } -void SwitchStatement::countArguments(unsigned &argument_count) const +void SwitchStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); statement_->countArguments(argument_count); - unsigned previous_argument_count = argument_count; + int previous_argument_count = argument_count; condition_->countArguments(argument_count); if(previous_argument_count > argument_count) argument_count = previous_argument_count; } -void SwitchStatement::countExpressionDepth(unsigned &depth_count) const +void SwitchStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { @@ -533,7 +533,7 @@ void ExpressionStatement::printXml() const next_statement_->printXml(); } -VariableStackBindings ExpressionStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ExpressionStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -544,18 +544,18 @@ VariableStackBindings ExpressionStatement::printAsm(VariableStackBindings bindin return bindings; } -void ExpressionStatement::countVariables(unsigned &var_count) const +void ExpressionStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); } -void ExpressionStatement::countArguments(unsigned &argument_count) const +void ExpressionStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); - unsigned tmp_argument_count = argument_count; + int tmp_argument_count = argument_count; if(expression_ != nullptr) expression_->countArguments(argument_count); @@ -564,9 +564,9 @@ void ExpressionStatement::countArguments(unsigned &argument_count) const argument_count = tmp_argument_count; } -void ExpressionStatement::countExpressionDepth(unsigned &depth_count) const +void ExpressionStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { @@ -597,19 +597,19 @@ void JumpStatement::printXml() const next_statement_->printXml(); } -void JumpStatement::countVariables(unsigned &var_count) const +void JumpStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); } -void JumpStatement::countArguments(unsigned &argument_count) const +void JumpStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); } -void JumpStatement::countExpressionDepth(unsigned &depth_count) const +void JumpStatement::countExpressionDepth(int &depth_count) const { if(next_statement_ != nullptr) next_statement_->countExpressionDepth(depth_count); @@ -622,7 +622,7 @@ ReturnStatement::ReturnStatement(Expression *expression) : expression_(expression) {} -VariableStackBindings ReturnStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ReturnStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -635,18 +635,18 @@ VariableStackBindings ReturnStatement::printAsm(VariableStackBindings bindings, return bindings; } -void ReturnStatement::countVariables(unsigned &var_count) const +void ReturnStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); } -void ReturnStatement::countArguments(unsigned &argument_count) const +void ReturnStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); - unsigned tmp_argument_count = argument_count; + int tmp_argument_count = argument_count; if(expression_ != nullptr) expression_->countArguments(argument_count); @@ -655,9 +655,9 @@ void ReturnStatement::countArguments(unsigned &argument_count) const argument_count = tmp_argument_count; } -void ReturnStatement::countExpressionDepth(unsigned &depth_count) const +void ReturnStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { @@ -682,7 +682,7 @@ void ReturnStatement::countExpressionDepth(unsigned &depth_count) const BreakStatement::BreakStatement() {} -VariableStackBindings BreakStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings BreakStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -697,7 +697,7 @@ VariableStackBindings BreakStatement::printAsm(VariableStackBindings bindings, u ContinueStatement::ContinueStatement() {} -VariableStackBindings ContinueStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ContinueStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -713,7 +713,7 @@ GotoStatement::GotoStatement(const std::string &label) : label_(label) {} -VariableStackBindings GotoStatement::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings GotoStatement::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -741,7 +741,7 @@ void IterationStatement::printXml() const statement_->printXml(); } -void IterationStatement::countVariables(unsigned &var_count) const +void IterationStatement::countVariables(int &var_count) const { if(next_statement_ != nullptr) next_statement_->countVariables(var_count); @@ -750,7 +750,7 @@ void IterationStatement::countVariables(unsigned &var_count) const statement_->countVariables(var_count); } -void IterationStatement::countArguments(unsigned &argument_count) const +void IterationStatement::countArguments(int &argument_count) const { if(next_statement_ != nullptr) next_statement_->countArguments(argument_count); @@ -759,9 +759,9 @@ void IterationStatement::countArguments(unsigned &argument_count) const statement_->countArguments(argument_count); } -void IterationStatement::countExpressionDepth(unsigned &depth_count) const +void IterationStatement::countExpressionDepth(int &depth_count) const { - unsigned previous_depth_count = depth_count; + int previous_depth_count = depth_count; if(next_statement_ != nullptr) { @@ -789,7 +789,7 @@ WhileLoop::WhileLoop(Expression *condition, Statement *statement, const bool &is : IterationStatement(condition, statement), is_while_(is_while) {} -VariableStackBindings WhileLoop::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings WhileLoop::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -816,7 +816,7 @@ ForLoop::ForLoop(Expression *initializer, Expression *condition, Expression *inc : IterationStatement(condition, statement), initializer_(initializer), incrementer_(incrementer) {} -VariableStackBindings ForLoop::printAsm(VariableStackBindings bindings, unsigned &label_count) const +VariableStackBindings ForLoop::printAsm(VariableStackBindings bindings, int &label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp index 74847b6..41d1171 100644 --- a/c_compiler/src/translation_unit.cpp +++ b/c_compiler/src/translation_unit.cpp @@ -25,7 +25,7 @@ void TranslationUnit::printXml() const printf("</Program>\n"); } -VariableStackBindings TranslationUnit::printAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings TranslationUnit::printAsm(VariableStackBindings bindings, int& label_count) const { for(auto& node : translation_unit_) { bindings = node->printAsm(bindings, label_count); diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp index e05a03e..6a8f45c 100644 --- a/c_compiler/src/type.cpp +++ b/c_compiler/src/type.cpp @@ -46,7 +46,7 @@ void Array::print() const void Array::printXml() const {} -VariableStackBindings Array::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Array::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -77,16 +77,31 @@ void Array::increaseStackPosition(VariableStackBindings &bindings) const } } +void Array::load() const +{ + type_->load(); +} + void Array::load(const int ®, const int &position) const { type_->load(reg, position); } +void Array::store() const +{ + type_->store(); +} + void Array::store(const int &position) const { type_->store(position); } +void Array::store(const int ®, const int &position) const +{ + type_->store(reg, position); +} + // Pointer definition @@ -101,7 +116,7 @@ void Pointer::print() const void Pointer::printXml() const {} -VariableStackBindings Pointer::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Pointer::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -129,14 +144,29 @@ void Pointer::increaseStackPosition(VariableStackBindings &bindings) const bindings.increaseStackPosition(4); } +void Pointer::load() const +{ + 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); } +void Pointer::store() const +{ + printf("\tsw\t$2,0($t0)\n"); +} + void Pointer::store(const int &position) const { - type_->store(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); } @@ -155,7 +185,7 @@ void TypeContainer::print() const void TypeContainer::printXml() const {} -VariableStackBindings TypeContainer::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings TypeContainer::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -185,16 +215,31 @@ void TypeContainer::increaseStackPosition(VariableStackBindings &bindings) const type_->increaseStackPosition(bindings); } +void TypeContainer::load() const +{ + type_->load(); +} + void TypeContainer::load(const int ®, const int &position) const { type_->load(reg, position); } +void TypeContainer::store() const +{ + type_->store(); +} + void TypeContainer::store(const int &position) const { type_->store(position); } +void TypeContainer::store(const int ®, const int &position) const +{ + type_->store(reg, position); +} + void TypeContainer::setSigned(bool _signed) { signed_ = _signed; @@ -252,7 +297,7 @@ void Int::print() const void Int::printXml() const {} -VariableStackBindings Int::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Int::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -262,16 +307,31 @@ void Int::increaseStackPosition(VariableStackBindings &bindings) const bindings.increaseStackPosition(4); } +void Int::load() const +{ + 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); } +void Int::store() const +{ + printf("\tsw\t$2,0($t0)\n"); +} + void Int::store(const int &position) const { 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); +} + // Void definition @@ -286,7 +346,7 @@ void Void::print() const void Void::printXml() const {} -VariableStackBindings Void::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Void::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -294,12 +354,21 @@ VariableStackBindings Void::printAsm(VariableStackBindings bindings, unsigned &) void Void::increaseStackPosition(VariableStackBindings &) const {} +void Void::load() const +{} + void Void::load(const int &, const int &) const {} +void Void::store() const +{} + void Void::store(const int &) const {} +void Void::store(const int &, const int &) const +{} + // Short definition @@ -314,7 +383,7 @@ void Short::print() const void Short::printXml() const {} -VariableStackBindings Short::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Short::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -324,9 +393,19 @@ void Short::increaseStackPosition(VariableStackBindings &bindings) const bindings.increaseStackPosition(2); } +void Short::load() const +{ + printf("\tlhu\t$2,0($t0)\n"); +} + void Short::load(const int ®, const int &position) const { - printf("\tlh\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"); } void Short::store(const int &position) const @@ -334,6 +413,11 @@ void Short::store(const int &position) const 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); +} + // Char definition @@ -348,7 +432,7 @@ void Char::print() const void Char::printXml() const {} -VariableStackBindings Char::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Char::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -358,9 +442,19 @@ void Char::increaseStackPosition(VariableStackBindings &bindings) const bindings.increaseStackPosition(1); } +void Char::load() const +{ + printf("\tlbu\t$2,0($t0)\n"); +} + void Char::load(const int ®, const int &position) const { - printf("\tlb\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"); } void Char::store(const int &position) const @@ -368,6 +462,11 @@ void Char::store(const int &position) const 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); +} + // Float definition @@ -382,7 +481,7 @@ void Float::print() const void Float::printXml() const {} -VariableStackBindings Float::printAsm(VariableStackBindings bindings, unsigned &) const +VariableStackBindings Float::printAsm(VariableStackBindings bindings, int &) const { return bindings; } @@ -392,12 +491,27 @@ void Float::increaseStackPosition(VariableStackBindings &bindings) const bindings.increaseStackPosition(4); } +void Float::load() const +{ + 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"); } +void Float::store() const +{ + throw std::runtime_error("Error : Cannot store float yet"); +} + void Float::store(const int &) const { 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"); +} |