aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-27 16:47:29 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-03-27 16:47:29 +0100
commit77559b13dc6200a83808e5a592a67463b0e89598 (patch)
tree2f603993b63a184fe795d69d96ed559ab658cac2
parentd860de923abeac05182e736cb67bb3615f5a71b4 (diff)
downloadCompiler-77559b13dc6200a83808e5a592a67463b0e89598.tar.gz
Compiler-77559b13dc6200a83808e5a592a67463b0e89598.zip
Changing declarations
-rw-r--r--c_compiler/include/declaration.hpp61
-rw-r--r--c_compiler/src/c_parser.y34
-rw-r--r--c_compiler/src/declaration.cpp147
-rw-r--r--c_compiler/src/function.cpp10
-rwxr-xr-xrun_test_deliverable.sh10
-rw-r--r--test_deliverable/testcases/test_MULTDIM0.c9
-rw-r--r--test_deliverable/testcases/test_MULTDIM0_driver.c6
7 files changed, 171 insertions, 106 deletions
diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp
index 5c8771b..b91835e 100644
--- a/c_compiler/include/declaration.hpp
+++ b/c_compiler/include/declaration.hpp
@@ -1,5 +1,5 @@
-#ifndef AST_DECLARATION_HPP
-#define AST_DECLARATION_HPP
+#ifndef DECLARATION_HPP
+#define DECLARATION_HPP
#include "node.hpp"
#include "type.hpp"
@@ -8,31 +8,28 @@
#include <memory>
class Declaration;
-
typedef std::shared_ptr<Declaration> DeclarationPtr;
-
class Declaration : public Node {
protected:
- TypePtr type_;
- std::string id_;
- ExpressionPtr initializer_;
DeclarationPtr next_declaration_;
- DeclarationPtr next_list_declaration_;
- bool extern_declaration_;
+ DeclarationPtr next_list_declaration_;
+ ExpressionPtr initializer_;
+ TypePtr type_;
+ bool extern_declaration_;
public:
- Declaration(const std::string &id = "", Expression *initializer = nullptr);
- Declaration(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;
+ 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;
- void linkDeclaration(Declaration *next_declaration);
+ void linkDeclaration(Declaration *next_declaration);
void linkListDeclaration(Declaration *next_list_declaration);
-
void setType(TypePtr type);
void setInitializer(Expression *initializer);
void setExternDeclaration(bool is_extern);
@@ -40,20 +37,38 @@ public:
DeclarationPtr getNext() const;
DeclarationPtr getNextListItem() const;
ExpressionPtr getInitializer() const;
- std::string getId() const;
TypePtr getType() const;
};
+class IdentifierDeclaration : public Declaration {
+private:
+ std::string id_;
+public:
+ 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;
+};
+
class ArrayDeclaration : public Declaration
{
private:
int size_;
+ DeclarationPtr declarator_;
public:
- ArrayDeclaration(const std::string &id = "", ExpressionPtr initializer = nullptr, 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 void countDeclarations(int &declaration_count) const;
+ virtual std::string getId() const;
};
#endif
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y
index 81293ba..feb5d0b 100644
--- a/c_compiler/src/c_parser.y
+++ b/c_compiler/src/c_parser.y
@@ -127,8 +127,8 @@ Parameter: DeclarationSpecifierList Declarator
$$->setType(tmp_type);
delete $1;
}
- | DeclarationSpecifierList { $$ = new Declaration(); delete $1; }
- | DeclarationSpecifierList T_MULT { $$ = new Declaration(); delete $2; delete $1; }
+ | DeclarationSpecifierList { $$ = new IdentifierDeclaration(); delete $1; }
+ | DeclarationSpecifierList T_MULT { $$ = new IdentifierDeclaration(); delete $2; delete $1; }
;
// Declaration
@@ -202,8 +202,9 @@ Declarator: DirectDeclarator { $$ = $1; }
Pointer:
T_MULT { $$ = new Pointer(); delete $1; }
- | T_MULT Pointer { $$ = $2; delete $1; }
- | T_MULT TypeQualifierList Pointer { $$ = $3; delete $1; delete $2; }
+ | T_MULT Pointer { $$ = $2; delete $1; $$->type(new Pointer()); }
+ | T_MULT TypeQualifierList Pointer
+ { $$ = $3; delete $1; delete $2; $$->type(new Pointer()); }
;
TypeQualifierList:
@@ -217,23 +218,36 @@ TypeQualifier:
;
DirectDeclarator:
- T_IDENTIFIER { $$ = new Declaration(*$1); delete $1; }
+ T_IDENTIFIER { $$ = new IdentifierDeclaration(*$1); delete $1; }
| T_LRB Declarator T_RRB { $$ = $2; }
| DirectDeclarator T_LSB ConditionalExpression T_RSB
{
- $$ = new ArrayDeclaration($1->getId(), $1->getInitializer(), $3->constantFold());
+ $$ = new ArrayDeclaration($1, $1->getInitializer(), $3->constantFold());
TypePtr tmp_ptr = std::make_shared<Array>($3->constantFold());
- $$->setType(tmp_ptr);
+ if($$->getType() == nullptr)
+ $$->setType(tmp_ptr);
+ else
+ $$->getType()->type(tmp_ptr);
+ }
+
+ | DirectDeclarator T_LSB T_RSB
+ {
+ $$ = new ArrayDeclaration($1, $1->getInitializer());
+ TypePtr tmp_ptr = std::make_shared<Array>(0);
+ if($$->getType() == nullptr)
+ $$->setType(tmp_ptr);
+ else
+ $$->getType()->type(tmp_ptr);
}
- | DirectDeclarator T_LSB T_RSB { $$ = $1; }
+
| DirectDeclarator T_LRB T_RRB { $$ = $1; $$->setExternDeclaration(true); }
| DirectDeclarator T_LRB ParameterTypeList T_RRB
{ $1->linkDeclaration($3); $$ = $1; $$->setExternDeclaration(true); }
| DirectDeclarator T_LRB IdentifierList T_RRB { $$ = $1; $$->setExternDeclaration(true); }
;
-IdentifierList: T_IDENTIFIER { $$ = new Declaration(); }
- | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declaration(); }
+IdentifierList: T_IDENTIFIER { $$ = new IdentifierDeclaration(); }
+ | IdentifierList T_CMA T_IDENTIFIER { $$ = new IdentifierDeclaration(); }
Initializer: AssignmentExpression { $$ = $1; }
| T_LCB InitializerList T_RCB { $$ = $2; }
diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp
index 594c91b..9f672db 100644
--- a/c_compiler/src/declaration.cpp
+++ b/c_compiler/src/declaration.cpp
@@ -9,15 +9,74 @@
// Declaration definition
-Declaration::Declaration(const std::string &id, Expression *initializer)
- : id_(id), initializer_(initializer), extern_declaration_(false)
+Declaration::Declaration(Expression *initializer)
+ : initializer_(initializer), extern_declaration_(false)
{}
-Declaration::Declaration(const std::string &id, ExpressionPtr initializer)
- :id_(id), initializer_(initializer), extern_declaration_(false)
+Declaration::Declaration(ExpressionPtr initializer)
+ : initializer_(initializer), extern_declaration_(false)
{}
-void Declaration::print() const
+void Declaration::linkDeclaration(Declaration* next_declaration)
+{
+ 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;
+}
+
+void Declaration::setType(TypePtr type)
+{
+ type_ = type;
+}
+
+void Declaration::setInitializer(Expression* initializer)
+{
+ ExpressionPtr expression_ptr(initializer);
+ initializer_ = expression_ptr;
+}
+
+void Declaration::setExternDeclaration(bool is_extern)
+{
+ extern_declaration_ = is_extern;
+}
+
+DeclarationPtr Declaration::getNext() const
+{
+ return next_declaration_;
+}
+
+DeclarationPtr Declaration::getNextListItem() const
+{
+ return next_list_declaration_;
+}
+
+ExpressionPtr Declaration::getInitializer() const
+{
+ return initializer_;
+}
+
+TypePtr Declaration::getType() const
+{
+ return type_;
+}
+
+
+// IdentifierDeclaration definition
+
+IdentifierDeclaration::IdentifierDeclaration(const std::string &id, Expression *initializer)
+ : Declaration(initializer), id_(id)
+{}
+
+IdentifierDeclaration::IdentifierDeclaration(const std::string &id, ExpressionPtr initializer)
+ : Declaration(initializer), id_(id)
+{}
+
+void IdentifierDeclaration::print() const
{
if(next_declaration_ != nullptr)
next_declaration_->print();
@@ -26,7 +85,7 @@ void Declaration::print() const
printf("%s\n", id_.c_str());
}
-void Declaration::printXml() const
+void IdentifierDeclaration::printXml() const
{
if(next_declaration_ != nullptr)
next_declaration_->printXml();
@@ -38,7 +97,7 @@ void Declaration::printXml() const
printf("<Variable id=\"%s\" />", id_.c_str());
}
-Bindings Declaration::printAsm(Bindings bindings, int& label_count) const
+Bindings IdentifierDeclaration::printAsm(Bindings bindings, int& label_count) const
{
(void)label_count;
if(!extern_declaration_)
@@ -54,7 +113,7 @@ Bindings Declaration::printAsm(Bindings bindings, int& label_count) const
return bindings;
}
-Bindings Declaration::localAsm(Bindings bindings, int& label_count) const
+Bindings IdentifierDeclaration::localAsm(Bindings bindings, int& label_count) const
{
if(next_declaration_ != nullptr)
bindings = next_declaration_->localAsm(bindings, label_count);
@@ -77,7 +136,7 @@ Bindings Declaration::localAsm(Bindings bindings, int& label_count) const
return bindings;
}
-void Declaration::countDeclarations(int &declaration_count) const
+void IdentifierDeclaration::countDeclarations(int &declaration_count) const
{
if(next_declaration_ != nullptr)
next_declaration_->countDeclarations(declaration_count);
@@ -88,67 +147,24 @@ void Declaration::countDeclarations(int &declaration_count) const
++declaration_count;
}
-void Declaration::linkDeclaration(Declaration* next_declaration)
-{
- 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;
-}
-
-void Declaration::setType(TypePtr type)
-{
- type_ = type;
-}
-
-void Declaration::setInitializer(Expression* initializer)
-{
- ExpressionPtr expression_ptr(initializer);
- initializer_ = expression_ptr;
-}
-
-void Declaration::setExternDeclaration(bool is_extern)
-{
- extern_declaration_ = is_extern;
-}
-
-DeclarationPtr Declaration::getNext() const
-{
- return next_declaration_;
-}
-
-DeclarationPtr Declaration::getNextListItem() const
-{
- return next_list_declaration_;
-}
-
-ExpressionPtr Declaration::getInitializer() const
-{
- return initializer_;
-}
-
-std::string Declaration::getId() const
+std::string IdentifierDeclaration::getId() const
{
return id_;
}
-TypePtr Declaration::getType() const
-{
- return type_;
-}
-
// Array declaration class
-ArrayDeclaration::ArrayDeclaration(const std::string &id, ExpressionPtr initializer, const int &size)
- : Declaration(id, initializer), size_(size)
+ArrayDeclaration::ArrayDeclaration(Declaration *declarator, ExpressionPtr initializer, const int &size)
+ : Declaration(initializer), size_(size), declarator_(declarator)
{}
-Bindings ArrayDeclaration::printAsm(Bindings bindings, int &label_count) const
+void ArrayDeclaration::print() const
+{
+ printf("Array Declaration\n");
+}
+
+Bindings ArrayDeclaration::printAsm(Bindings bindings, int &) const
{
return bindings;
}
@@ -161,7 +177,7 @@ Bindings ArrayDeclaration::localAsm(Bindings bindings, int &label_count) const
if(next_list_declaration_ != nullptr)
bindings = next_list_declaration_->localAsm(bindings, label_count);
- if(id_ != "")
+ if(getId() != "")
{
int stack_position = bindings.currentStackPosition();
if(initializer_ != nullptr)
@@ -183,7 +199,7 @@ Bindings ArrayDeclaration::localAsm(Bindings bindings, int &label_count) const
}
}
- bindings.insertBinding(id_, type_, stack_position);
+ bindings.insertBinding(getId(), type_, stack_position);
type_->increaseStackPosition(bindings);
}
@@ -200,3 +216,8 @@ void ArrayDeclaration::countDeclarations(int &declaration_count) const
declaration_count += size_;
}
+
+std::string ArrayDeclaration::getId() const
+{
+ return declarator_->getId();
+}
diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp
index 5cdb33d..29c3993 100644
--- a/c_compiler/src/function.cpp
+++ b/c_compiler/src/function.cpp
@@ -34,12 +34,10 @@ void Function::printXml() const
parameter = parameter->getNext();
}
- for(std::vector<std::string>::reverse_iterator itr = parameter_vec.rbegin();
- itr != parameter_vec.rend(); ++itr)
+ for(auto itr = parameter_vec.rbegin(); itr != parameter_vec.rend(); ++itr)
{
printf("<Parameter id=\"%s\" />", (*itr).c_str());
}
-
if(statement_ != nullptr)
statement_->printXml();
@@ -84,7 +82,8 @@ Bindings Function::printAsm(Bindings bindings, int& label_count) const
// 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\tlw\t$fp,%d", memory_needed-4, memory_needed-8);
+ 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();
@@ -93,7 +92,8 @@ Bindings Function::printAsm(Bindings bindings, int& label_count) const
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\"%s\\000\"", int(itr-string_lit_iterator.first), (*itr).c_str());
+ printf("$%d_string:\n\t.ascii\t", int(itr-string_lit_iterator.first));
+ printf("\"%s\\000\"", (*itr).c_str());
}
}
diff --git a/run_test_deliverable.sh b/run_test_deliverable.sh
index bbb36b9..32809ff 100755
--- a/run_test_deliverable.sh
+++ b/run_test_deliverable.sh
@@ -35,11 +35,11 @@ for DRIVER in test_deliverable/testcases/*_driver.c ; do
fi
# Compile test function with compiler under test to assembly
- # cat $TESTCODE | $COMPILER > working/$NAME.s 2> working/${NAME}.compile.stderr
- # if [[ $? -ne 0 ]]; then
- # printf "\e[1;31mError\e[0m : Compiler returned error message.\n"
- # continue
- # fi
+ cat $TESTCODE | $COMPILER > working/$NAME.s 2> working/${NAME}.compile.stderr
+ if [[ $? -ne 0 ]]; then
+ printf "\e[1;31mError\e[0m : Compiler returned error message.\n"
+ continue
+ fi
# Link driver object and assembly into executable
mips-linux-gnu-gcc -static working/${NAME}.s working/${NAME}_driver.o -o working/${NAME}.elf 2> working/${NAME}.link.stderr
diff --git a/test_deliverable/testcases/test_MULTDIM0.c b/test_deliverable/testcases/test_MULTDIM0.c
new file mode 100644
index 0000000..e126fbc
--- /dev/null
+++ b/test_deliverable/testcases/test_MULTDIM0.c
@@ -0,0 +1,9 @@
+int multdim0(int a, int b, int c, int d)
+{
+ int f[2][2] = {
+ { a, b, },
+ { c, d, },
+ };
+
+ return f[0][1];
+}
diff --git a/test_deliverable/testcases/test_MULTDIM0_driver.c b/test_deliverable/testcases/test_MULTDIM0_driver.c
new file mode 100644
index 0000000..6645569
--- /dev/null
+++ b/test_deliverable/testcases/test_MULTDIM0_driver.c
@@ -0,0 +1,6 @@
+int multdim0(int, int, int, int);
+
+int main()
+{
+ return !( 21383 == multdim0(289, 21383, 894, 8393) );
+}