aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-26 16:08:07 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-03-26 16:08:07 +0100
commitdec9dc403e540ef4424debfdd866c73ca93adc83 (patch)
tree83df1a3139509594e11b984f15cc11b4626e1b5d
parent19896f53d80deadcf09d3a1256524cc6f2e4adb6 (diff)
downloadCompiler-dec9dc403e540ef4424debfdd866c73ca93adc83.tar.gz
Compiler-dec9dc403e540ef4424debfdd866c73ca93adc83.zip
arrays and pointers working
-rw-r--r--c_compiler/include/type.hpp32
-rw-r--r--c_compiler/src/expression.cpp36
-rw-r--r--c_compiler/src/type.cpp66
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 &reg, const int &position) const = 0;
virtual void store() const = 0;
virtual void store(const int &position) const = 0;
- virtual void store(const int &reg, const int &position) const = 0;
+ virtual void store(const int &reg, 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 &reg, const int &position) const;
virtual void store() const;
virtual void store(const int &position) const;
- virtual void store(const int &reg, const int &position) const;
+ virtual void store(const int &reg, 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 &reg, const int &position) const;
+ virtual int getSize() const;
+
+ void pointerLoad() const;
+ void pointerLoad(const int &reg, const int &position) const;
+ void pointerStore() const;
+ void pointerStore(const int &position) const;
+ void pointerStore(const int &reg, const int &position) const;
};
class TypeContainer : public Type
@@ -108,7 +117,8 @@ public:
virtual void load(const int &reg, const int &position) const;
virtual void store() const;
virtual void store(const int &position) const;
- virtual void store(const int &reg, const int &position) const;
+ virtual void store(const int &reg, 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 &reg, const int &position) const = 0;
virtual void store() const = 0;
virtual void store(const int &position) const = 0;
- virtual void store(const int &reg, const int &position) const = 0;
+ virtual void store(const int &reg, 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 &reg, const int &position) const;
virtual void store() const;
virtual void store(const int &position) const;
- virtual void store(const int &reg, const int &position) const;
+ virtual void store(const int &reg, const int &position) const;
+ virtual int getSize() const;
};
class Short : public Specifier
@@ -165,7 +177,8 @@ public:
virtual void load(const int &reg, const int &position) const;
virtual void store() const;
virtual void store(const int &position) const;
- virtual void store(const int &reg, const int &position) const;
+ virtual void store(const int &reg, const int &position) const;
+ virtual int getSize() const;
};
class Void : public Specifier
@@ -181,7 +194,8 @@ public:
virtual void load(const int &reg, const int &position) const;
virtual void store() const;
virtual void store(const int &position) const;
- virtual void store(const int &reg, const int &position) const;
+ virtual void store(const int &reg, 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 &reg, const int &position) const;
+ virtual int getSize() const;
};
class Float : public Specifier
@@ -213,7 +228,8 @@ public:
virtual void load(const int &reg, const int &position) const;
virtual void store() const;
virtual void store(const int &position) const;
- virtual void store(const int &reg, const int &position) const;
+ virtual void store(const int &reg, 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<UnaryExpression>(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<UnaryExpression> unary_expression;
unary_expression = std::static_pointer_cast<UnaryExpression>(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<PostfixArrayElement> lhs_tmp;
+ lhs_tmp = std::dynamic_pointer_cast<PostfixArrayElement>(lhs_);
+ if(lhs_tmp != nullptr)
+ {
+ std::shared_ptr<Pointer> lhs_pointer_type;
+ lhs_pointer_type = std::dynamic_pointer_cast<Pointer>(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<Pointer>(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 &reg, 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 &reg, 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 &reg, 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 &reg, const int &position) const
+{
+ type_->store(reg, position);
+}
+
// TypeContainer definition
@@ -240,6 +276,11 @@ void TypeContainer::store(const int &reg, 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 &reg, 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 &reg, 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 &reg, 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;
+}