aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-16 17:29:33 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-03-16 17:29:33 +0000
commit9ab7f5825f225cb42a6a0e8950f841a10db82f1b (patch)
tree501642ec709e01ea9b387be86962d9d756a6f946 /c_compiler
parent03e21a7ef589fa52d27eab85d669ca854e8ac2b8 (diff)
downloadCompiler-9ab7f5825f225cb42a6a0e8950f841a10db82f1b.tar.gz
Compiler-9ab7f5825f225cb42a6a0e8950f841a10db82f1b.zip
working, added function calls
Diffstat (limited to 'c_compiler')
-rw-r--r--c_compiler/include/expression.hpp3
-rw-r--r--c_compiler/src/expression.cpp33
-rw-r--r--c_compiler/src/function.cpp2
3 files changed, 36 insertions, 2 deletions
diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp
index ba1a556..0f14584 100644
--- a/c_compiler/include/expression.hpp
+++ b/c_compiler/include/expression.hpp
@@ -29,6 +29,8 @@ public:
virtual int postfixStackPosition(VariableStackBindings bindings) const;
virtual void setPostfixExpression(Expression* postfix_expression);
+ virtual std::string id() const;
+
void linkExpression(Expression* next_expression);
ExpressionPtr nextExpression() const;
};
@@ -231,6 +233,7 @@ public:
virtual VariableStackBindings printAsm(VariableStackBindings bindings) const;
virtual int postfixStackPosition(VariableStackBindings bindings) const;
+ virtual std::string id() const;
};
diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp
index 52f847e..55ecb54 100644
--- a/c_compiler/src/expression.cpp
+++ b/c_compiler/src/expression.cpp
@@ -1,6 +1,7 @@
#include "expression.hpp"
#include <iostream>
+#include <vector>
// Expression definition
@@ -32,6 +33,11 @@ void Expression::setPostfixExpression(Expression *postfix_expression)
(void)postfix_expression;
}
+std::string Expression::id() const
+{
+ return "";
+}
+
void Expression::linkExpression(Expression *next_expression)
{
ExpressionPtr expression_ptr(next_expression);
@@ -82,9 +88,27 @@ PostfixFunctionCall::PostfixFunctionCall(Expression* argument_expression_list)
VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindings) const
{
- if(argument_expression_list_ != nullptr) {
-
+ std::vector<ExpressionPtr> argument_vector;
+ ExpressionPtr current_argument = argument_expression_list_;
+ unsigned argument_counter = 0;
+
+ while(current_argument != nullptr) {
+ argument_vector.push_back(current_argument);
+ current_argument = current_argument->nextExpression();
}
+
+ for(auto itr = argument_vector.rbegin(); itr != argument_vector.rend(); ++itr) {
+ (*itr)->printAsm(bindings);
+
+ if(argument_counter < 4)
+ std::cout << "\tmove\t$" << 4+argument_counter << ",$2\n";
+ else
+ std::cout << "\tsw\t$2," << 4*argument_counter << "($fp)\n";
+
+ argument_counter++;
+ }
+
+ std::cout << "\tjal\t" << postfix_expression_->id() << "\n\tnop\n";
return bindings;
}
@@ -379,6 +403,11 @@ int Identifier::postfixStackPosition(VariableStackBindings bindings) const
return -1;
}
+std::string Identifier::id() const
+{
+ return id_;
+}
+
// Constant definition
diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp
index 24cea4c..2686287 100644
--- a/c_compiler/src/function.cpp
+++ b/c_compiler/src/function.cpp
@@ -62,6 +62,8 @@ VariableStackBindings Function::printAsm(VariableStackBindings bindings) const
// This adds 2 to store the frame pointer and the return address
unsigned memory_needed = 4*(variable_count + max_argument_count + 2);
+ if(memory_needed % 8 != 0)
+ memory_needed += 4;
std::cout << "\t.text\n\t.globl\t" << id_ << "\n" << id_ << ":\n\taddiu\t$sp,$sp,-"
<< memory_needed << "\n\tsw\t$31," << memory_needed-4 << "($sp)\n" << "\tsw\t$fp,"