aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-03 16:24:07 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-03-03 16:24:07 +0000
commit522cc9d286c5d35ca25ebaa85374f5f9214a7f6e (patch)
treee4c944cce2fcd736834e35d8e5d06f984b57a976
parent446c2394ec8970198d645bbbb462c67b9e3f1b1e (diff)
downloadCompiler-522cc9d286c5d35ca25ebaa85374f5f9214a7f6e.tar.gz
Compiler-522cc9d286c5d35ca25ebaa85374f5f9214a7f6e.zip
Still working on ast
-rw-r--r--c_compiler/include/base.hpp5
-rw-r--r--c_compiler/include/expression.hpp6
-rw-r--r--c_compiler/include/function.hpp18
-rw-r--r--c_compiler/include/primitives.hpp34
-rw-r--r--c_compiler/include/statement.hpp40
-rw-r--r--c_compiler/src/c_parser.y30
-rw-r--r--c_compiler/src/compiler_main.cpp (renamed from c_compiler/src/parser_main.cpp)4
-rw-r--r--c_compiler/src/expression.cpp11
-rw-r--r--c_compiler/src/function.cpp18
-rw-r--r--c_compiler/src/primitives.cpp35
-rw-r--r--c_compiler/src/statement.cpp53
-rw-r--r--makefile5
-rwxr-xr-xtestbin0 -> 8384 bytes
-rw-r--r--test.c8
14 files changed, 186 insertions, 81 deletions
diff --git a/c_compiler/include/base.hpp b/c_compiler/include/base.hpp
index ae7cfd7..eff825a 100644
--- a/c_compiler/include/base.hpp
+++ b/c_compiler/include/base.hpp
@@ -109,10 +109,7 @@ protected:
const Base* type;
public:
- BasePrimitive(const std::string& _id)
- : id(_id), type(new EmptyNode) {}
-
- BasePrimitive(const std::string& _id, const Base* _type)
+ BasePrimitive(const std::string& _id = "", const Base* _type = new EmptyNode)
: id(_id), type(_type) {}
virtual ~BasePrimitive() {
diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp
index 9eb9efd..1e2bf04 100644
--- a/c_compiler/include/expression.hpp
+++ b/c_compiler/include/expression.hpp
@@ -6,11 +6,9 @@
class Expression : public BaseNode {
private:
public:
- Expression() : BaseNode() {}
+ Expression(const Base* expr = new EmptyNode);
- virtual void print() const override {}
- virtual void printxml() const override {}
- virtual void printasm() const override {}
+ virtual void printasm() const override;
};
#endif
diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp
index 955420d..b1ee88d 100644
--- a/c_compiler/include/function.hpp
+++ b/c_compiler/include/function.hpp
@@ -9,22 +9,10 @@ protected:
std::string id;
public:
- Function(const std::string& _id, const BaseList* _param_list, const BaseNode* _comp_statement)
- : BaseNode(_param_list, _comp_statement), id(_id) {}
+ Function(const std::string& _id, const BaseList* _param_list, const BaseNode* _comp_statement);
- virtual void printxml() const override {
- std::cout << "<Function id=\"" << id << "\">" << std::endl;
- leftNode->printxml();
- rightNode->printxml();
- std::cout << "</Function>" << std::endl;
- }
-};
-
-
-class ParamList : public BaseList {
-public:
- ParamList() : BaseList() {}
- ParamList(const Base* _param) : BaseList(_param) {}
+ virtual void printxml() const override;
+ virtual void printasm() const override;
};
diff --git a/c_compiler/include/primitives.hpp b/c_compiler/include/primitives.hpp
index f4c5087..d433072 100644
--- a/c_compiler/include/primitives.hpp
+++ b/c_compiler/include/primitives.hpp
@@ -1,26 +1,40 @@
-#ifndef AST_PRIMITIVES_HPP
-#define AST_PRIMITIVES_HPP
+#ifndef PRIMITIVES_HPP
+#define PRIMITIVES_HPP
#include "ast.hpp"
+#include <cstdint>
+
+
+class ParamList : public BaseList {
+public:
+ ParamList();
+ ParamList(const Base* _param);
+};
+
class Declarator : public BasePrimitive {
public:
- Declarator(const std::string& _id) : BasePrimitive(_id) {}
+ Declarator(const std::string& _id);
- virtual void printxml() const {
- std::cout << "<Variable id=\"" << id << "\" />" << std::endl;
- }
+ virtual void printxml() const;
};
class Parameter : public BasePrimitive {
public:
- Parameter(const std::string& _id) : BasePrimitive(_id) {}
+ Parameter(const std::string& _id);
+
+ virtual void printxml() const;
+};
+
+class Immediate : public BasePrimitive {
+protected:
+ int32_t imm;
+public:
+ Immediate(const int32_t& _imm);
- virtual void printxml() const {
- std::cout << "<Parameter id=\"" << id << "\" />" << std::endl;
- }
+ virtual void printasm() const override;
};
diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp
index 451d368..bf15aa5 100644
--- a/c_compiler/include/statement.hpp
+++ b/c_compiler/include/statement.hpp
@@ -6,61 +6,41 @@
class Statement : public BaseNode {
public:
- Statement() : BaseNode() {}
-
- Statement(const Base* _el) : BaseNode(_el) {}
+ Statement(const Base* _left = new EmptyNode, const Base* _right = new EmptyNode);
};
class StatementList : public BaseList {
public:
- StatementList(const Base* _statement) : BaseList(_statement) {}
+ StatementList(const Base* _statement);
};
class CompoundStatement : public Statement {
public:
- CompoundStatement() : Statement() {}
- CompoundStatement(const Base* _el) : Statement(_el) {}
-
- CompoundStatement(const Base* _dec, const Base* _statement) {
- leftNode = _dec;
- rightNode = _statement;
- }
+ CompoundStatement(const Base* _dec = new EmptyNode, const Base* _statement = new EmptyNode);
- virtual void printxml() const override {
- std::cout << "<Scope>" << std::endl;
- leftNode->printxml();
- rightNode->printxml();
- std::cout << "</Scope>" << std::endl;
- }
+ virtual void printxml() const override;
};
class SelectionStatement : public Statement {
public:
- SelectionStatement() : Statement() {}
- SelectionStatement(const Base* _el) : Statement(_el) {}
-
- SelectionStatement(const Base* _if, const Base* _else) {
- leftNode = _if;
- rightNode = _else;
- }
+ SelectionStatement(const Base* _if, const Base* _else = new EmptyNode);
};
class ExpressionStatement : public Statement {
public:
- ExpressionStatement() : Statement() {}
- ExpressionStatement(const Base* expr) : Statement(expr) {}
+ ExpressionStatement(const Base* expr = new EmptyNode);
};
class JumpStatement : public Statement {
public:
- JumpStatement() : Statement() {}
- JumpStatement(const Base* _el) : Statement(_el) {}
+ JumpStatement(const Base* _el);
+
+ virtual void printasm() const override;
};
class IterationStatement : public Statement {
public:
- IterationStatement() : Statement() {}
- IterationStatement(const Base* _el) : Statement(_el) {}
+ IterationStatement(const Base* _el);
};
#endif
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y
index 2f9f1b8..374ee46 100644
--- a/c_compiler/src/c_parser.y
+++ b/c_compiler/src/c_parser.y
@@ -37,17 +37,17 @@ void yyerror(const char *);
StatementList ArgumentExpressionList
%type <base_node> ExtDeclaration FuncDef Declaration DeclarationSpec DeclarationSpec_T
- InitDeclarator Statement CompoundStatement CompoundStatement_2
+ Statement CompoundStatement CompoundStatement_2 PrimaryExpression
SelectionStatement ExpressionStatement JumpStatement IterationStatement
Expression AssignmentExpression ConditionalExpression LogicalOrExpression
LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression
AndExpression EqualityExpression RelationalExpression ShiftExpression
AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression
- PostfixExpression PostfixExpression2 PrimaryExpression
+ PostfixExpression PostfixExpression2
-%type <base_prim> Parameter ParamDeclarator Declarator DirectDeclarator
+%type <base_prim> Parameter ParamDeclarator Declarator DirectDeclarator InitDeclarator Constant
-%type <number> T_INT_CONST Constant
+%type <number> T_INT_CONST
%type <string> T_IDENTIFIER ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT
@@ -65,7 +65,7 @@ ROOT:
ExtDef:
ExtDeclaration { $$ = new ExternalDefinition($1); }
- | ExtDef ExtDeclaration { $$->push($2); }
+| ExtDef ExtDeclaration { $$->push($2); }
;
ExtDeclaration:
@@ -76,7 +76,7 @@ ExtDeclaration:
// FUNCTION DEFINITION
FuncDef:
- DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); }
+ DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); }
;
ParameterList:
@@ -140,8 +140,8 @@ DirectDeclarator:
;
IdentifierList:
- T_IDENTIFIER { $$ = new Declarator(*$1); }
- | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declarator(*$3); }
+ T_IDENTIFIER { $$ = new BaseList(); }
+ | IdentifierList T_CMA T_IDENTIFIER { $$ = new BaseList(); }
;
// Statement
@@ -181,7 +181,7 @@ ExpressionStatement:
;
JumpStatement:
- T_RETURN ExpressionStatement { $$ = $2; }
+ T_RETURN ExpressionStatement { $$ = new JumpStatement($2); }
;
IterationStatement:
@@ -299,22 +299,22 @@ PostfixExpression:
PostfixExpression2:
T_RRB { $$ = new Expression(); }
- | ArgumentExpressionList T_RRB { $$ = $1; }
+ | ArgumentExpressionList T_RRB { $$ = new BaseNode; }
;
ArgumentExpressionList:
- AssignmentExpression { $$ = $1; }
- | ArgumentExpressionList T_CMA AssignmentExpression { $$ = $3; }
+ AssignmentExpression { $$ = new BaseList; }
+ | ArgumentExpressionList T_CMA AssignmentExpression { $$ = new BaseList; }
;
PrimaryExpression:
T_IDENTIFIER { $$ = new Expression(); }
- | Constant { $$ = new Expression(); }
+ | Constant { $$ = new Expression($1); }
| T_LRB Expression T_RRB { $$ = $2; }
;
Constant:
- T_INT_CONST { $$ = $1; }
+ T_INT_CONST { $$ = new Immediate($1); }
;
%%
@@ -322,7 +322,7 @@ Constant:
const BaseList* g_root; // Definition of variable (to match declaration earlier)
const BaseList* parseAST() {
- g_root = NULL;
+ g_root = 0;
yyparse();
return g_root;
}
diff --git a/c_compiler/src/parser_main.cpp b/c_compiler/src/compiler_main.cpp
index 8922417..d551b8b 100644
--- a/c_compiler/src/parser_main.cpp
+++ b/c_compiler/src/compiler_main.cpp
@@ -3,9 +3,9 @@
#include <iostream>
int main(int argc, char *argv[]) {
- const Base *ast = parseAST();
+ const BaseList* ast = parseAST();
- ast->print();
+ ast->printasm();
return 0;
}
diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp
new file mode 100644
index 0000000..cf2b58f
--- /dev/null
+++ b/c_compiler/src/expression.cpp
@@ -0,0 +1,11 @@
+#include "ast.hpp"
+
+
+// Expression definition
+
+Expression::Expression(const Base* _expr)
+ : BaseNode(_expr) {}
+
+void Expression::printasm() const {
+ leftNode->printasm();
+}
diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp
new file mode 100644
index 0000000..420c782
--- /dev/null
+++ b/c_compiler/src/function.cpp
@@ -0,0 +1,18 @@
+#include "ast.hpp"
+
+Function::Function(const std::string& _id, const BaseList* _param_list,
+ const BaseNode* _comp_statement)
+ : BaseNode(_param_list, _comp_statement), id(_id) {}
+
+void Function::printxml() const {
+ std::cout << "<Function id=\"" << id << "\">" << std::endl;
+ leftNode->printxml();
+ rightNode->printxml();
+ std::cout << "</Function>" << std::endl;
+}
+
+void Function::printasm() const {
+ std::cout << id << ":\n\taddiu\t$sp,$sp,-24\n\tsw\t$fp,20($sp)\n\tmove\t$fp,$sp" << std::endl;
+ rightNode->printasm();
+ std::cout << "\tmove\t$sp,$fp\n\tlw\t$fp,20($sp)\n\taddiu\t$sp,$sp,24\n\tjr\t$31\n\tnop" << std::endl;
+}
diff --git a/c_compiler/src/primitives.cpp b/c_compiler/src/primitives.cpp
new file mode 100644
index 0000000..e3ec0eb
--- /dev/null
+++ b/c_compiler/src/primitives.cpp
@@ -0,0 +1,35 @@
+#include "ast.hpp"
+
+
+// Parameter list definition (used in functions)
+
+ParamList::ParamList() : BaseList() {}
+
+ParamList::ParamList(const Base* _param) : BaseList(_param) {}
+
+
+// Declarator defintion, used when declaring a variable
+
+Declarator::Declarator(const std::string& _id) : BasePrimitive(_id) {}
+
+void Declarator::printxml() const {
+ std::cout << "<Variable id=\"" << id << "\" />" << std::endl;
+}
+
+
+// Parameter class defition
+
+Parameter::Parameter(const std::string& _id) : BasePrimitive(_id) {}
+
+void Parameter::printxml() const {
+ std::cout << "<Parameter id=\"" << id << "\" />" << std::endl;
+}
+
+
+// Immediate class definition, used when loading an immediate value
+
+Immediate::Immediate(const int32_t& _imm) : BasePrimitive(), imm(_imm) {}
+
+void Immediate::printasm() const {
+ std::cout << "\tli\t$2," << imm << "\n\tsw\t$2,8($fp)" << std::endl;
+}
diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp
new file mode 100644
index 0000000..e828eaa
--- /dev/null
+++ b/c_compiler/src/statement.cpp
@@ -0,0 +1,53 @@
+#include "ast.hpp"
+
+
+// General base Statement definition
+
+Statement::Statement(const Base* _left, const Base* _right)
+ : BaseNode(_left, _right) {}
+
+
+// Statement list definition
+
+StatementList::StatementList(const Base* _statement)
+ : BaseList(_statement) {}
+
+
+// Compound Statement definition
+
+CompoundStatement::CompoundStatement(const Base* _dec, const Base* _statement)
+ : Statement(_dec, _statement) {}
+
+void CompoundStatement::printxml() const {
+ std::cout << "<Scope>" << std::endl;
+ leftNode->printxml();
+ rightNode->printxml();
+ std::cout << "</Scope>" << std::endl;
+}
+
+
+// Selection Statement definition
+
+SelectionStatement::SelectionStatement(const Base* _if, const Base* _else)
+ : Statement(_if, _else) {}
+
+
+// Expression Statement definition
+
+ExpressionStatement::ExpressionStatement(const Base* _expr)
+ : Statement(_expr) {}
+
+
+// Jump Statement definition
+
+JumpStatement::JumpStatement(const Base* _el) : Statement(_el) {}
+
+void JumpStatement::printasm() const {
+ leftNode->printasm();
+ std::cout << "\tlw\t$2,8($fp)" << std::endl;
+}
+
+
+// Iteration Statement definition
+
+IterationStatement::IterationStatement(const Base* _el) : Statement(_el) {}
diff --git a/makefile b/makefile
index cf2b7e1..64d746d 100644
--- a/makefile
+++ b/makefile
@@ -67,7 +67,10 @@ $(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp : $(PARSRCDIR)/c_par
@echo " bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp"; bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp
# Make the c_parser
-bin/c_compiler : $(COMPBUILDDIR)/parser_main.o $(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o
+bin/c_compiler: $(COMPBUILDDIR)/compiler_main.o $(COMPBUILDDIR)/function.o \
+ $(COMPBUILDDIR)/statement.o $(COMPBUILDDIR)/primitives.o \
+ $(COMPBUILDDIR)/expression.o \
+ $(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o
@echo "Linking..."
@echo " mkdir -p bin"; mkdir -p bin
@echo " $(CC) $^ -o $@"; $(CC) $^ -o $@
diff --git a/test b/test
new file mode 100755
index 0000000..c544276
--- /dev/null
+++ b/test
Binary files differ
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..8285b7f
--- /dev/null
+++ b/test.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main() {
+ int a;
+ for(a = 0; a < 10; ++a) {
+ printf("Hello\n");
+ }
+}