From dec9dc403e540ef4424debfdd866c73ca93adc83 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 26 Mar 2017 16:08:07 +0100 Subject: arrays and pointers working --- c_compiler/include/type.hpp | 32 +++++++++++++++------ c_compiler/src/expression.cpp | 36 +++++++++++++++++++---- c_compiler/src/type.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 14 deletions(-) diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp index d29a927..04bd7a8 100644 --- a/c_compiler/include/type.hpp +++ b/c_compiler/include/type.hpp @@ -26,7 +26,8 @@ public: 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 void store(const int ®, const int &position) const = 0; + virtual int getSize() const = 0; virtual void setSigned(bool _signed); virtual void setExtern(bool _extern); @@ -56,7 +57,8 @@ public: 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 void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Pointer : public Type @@ -80,6 +82,13 @@ public: 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 @@ -108,7 +117,8 @@ public: 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 void store(const int ®, const int &position) const; + virtual int getSize() const; virtual void setSigned(bool _signed); virtual void setExtern(bool _extern); @@ -129,7 +139,8 @@ public: 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 void store(const int ®, const int &position) const = 0; + virtual int getSize() const = 0; virtual TypePtr type(); virtual TypePtr type(Type *type_ptr); @@ -149,7 +160,8 @@ public: 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 void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Short : public Specifier @@ -165,7 +177,8 @@ public: 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 void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Void : public Specifier @@ -181,7 +194,8 @@ public: 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 void store(const int ®, const int &position) const; + virtual int getSize() const; }; class Char : public Specifier @@ -198,6 +212,7 @@ public: 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 @@ -213,7 +228,8 @@ public: 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 void store(const int ®, const int &position) const; + virtual int getSize() const; }; #endif diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 2dc3a54..a323153 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -151,7 +151,8 @@ void PostfixArrayElement::stackPosition(VariableStackBindings bindings, int &lab unary_expression = std::static_pointer_cast(postfix_expression_); unary_expression->stackPosition(bindings, label_count); - printf("\tli\t$3,4\n\tmul\t$2,$2,$3\n\taddu\t$t0,$t0,$2\n"); + printf("\tli\t$3,%d\n\tmul\t$2,$2,$3\n\taddu\t$t0,$t0,$2\n", + unary_expression->getType(bindings)->getSize()); } void PostfixArrayElement::expressionDepth(int &depth_count) const @@ -355,7 +356,6 @@ void OperatorUnaryExpression::stackPosition(VariableStackBindings bindings, int std::shared_ptr unary_expression; unary_expression = std::static_pointer_cast(cast_expression_); unary_expression->stackPosition(bindings, label_count); - printf("\tlw\t$t0,0($t0)\n"); } } @@ -754,9 +754,30 @@ VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindi 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 - TypePtr tmp_ptr = lhs_->getType(bindings); - tmp_ptr->load(2, expression_stack_position); - tmp_ptr->store(); + TypePtr lhs_type = lhs_->getType(bindings); + lhs_type->load(2, expression_stack_position); + + // 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) + { + lhs_pointer_type->pointerStore(); + } + else + { + lhs_type->store(); + } + } + else + { + lhs_type->store(); + } + return bindings; } @@ -802,7 +823,10 @@ void Identifier::stackPosition(VariableStackBindings bindings, int &) const { if(bindings.bindingExists(id_)) { - printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); + if(std::dynamic_pointer_cast(bindings.getType(id_)) != nullptr) + printf("\tlw\t$3,%d($fp)\n\tmove\t$t0,$3\n", bindings.stackPosition(id_)); + else + printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_)); return; } diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp index 6a8f45c..251ebc5 100644 --- a/c_compiler/src/type.cpp +++ b/c_compiler/src/type.cpp @@ -102,6 +102,11 @@ void Array::store(const int ®, const int &position) const type_->store(reg, position); } +int Array::getSize() const +{ + return type_->getSize(); +} + // Pointer definition @@ -169,6 +174,37 @@ void Pointer::store(const int ®, const int &position) const printf("\tsw\t$%d,%d($fp)\n", reg, position); } +int Pointer::getSize() const +{ + return type_->getSize(); +} + +void Pointer::pointerLoad() const +{ + type_->load(); +} + +void Pointer::pointerLoad(const int ®, const int &position) const +{ + type_->load(reg, position); +} + +void Pointer::pointerStore() const +{ + + type_->store(); +} + +void Pointer::pointerStore(const int &position) const +{ + type_->store(position); +} + +void Pointer::pointerStore(const int ®, const int &position) const +{ + type_->store(reg, position); +} + // TypeContainer definition @@ -240,6 +276,11 @@ void TypeContainer::store(const int ®, const int &position) const type_->store(reg, position); } +int TypeContainer::getSize() const +{ + return type_->getSize(); +} + void TypeContainer::setSigned(bool _signed) { signed_ = _signed; @@ -332,6 +373,11 @@ void Int::store(const int ®, const int &position) const printf("\tsw\t$%d,%d($fp)\n", reg, position); } +int Int::getSize() const +{ + return 4; +} + // Void definition @@ -369,6 +415,11 @@ void Void::store(const int &) const void Void::store(const int &, const int &) const {} +int Void::getSize() const +{ + return 0; +} + // Short definition @@ -418,6 +469,11 @@ void Short::store(const int ®, const int &position) const printf("\tsh\t$%d,%d($fp)\n", reg, position); } +int Short::getSize() const +{ + return 2; +} + // Char definition @@ -467,6 +523,11 @@ void Char::store(const int ®, const int &position) const printf("\tsb\t$%d,%d($fp)\n", reg, position); } +int Char::getSize() const +{ + return 1; +} + // Float definition @@ -515,3 +576,8 @@ void Float::store(const int &, const int &) const { throw std::runtime_error("Error : Cannot store float yet"); } + +int Float::getSize() const +{ + return 4; +} -- cgit