diff options
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/bindings.cpp | 48 | ||||
-rw-r--r-- | c_compiler/src/c_parser.y | 5 | ||||
-rw-r--r-- | c_compiler/src/compiler_main.cpp | 10 | ||||
-rw-r--r-- | c_compiler/src/declaration.cpp | 48 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 18 | ||||
-rw-r--r-- | c_compiler/src/function.cpp | 3 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 50 | ||||
-rw-r--r-- | c_compiler/src/translation_unit.cpp | 11 | ||||
-rw-r--r-- | c_compiler/src/type.cpp | 11 |
9 files changed, 167 insertions, 37 deletions
diff --git a/c_compiler/src/bindings.cpp b/c_compiler/src/bindings.cpp new file mode 100644 index 0000000..e920783 --- /dev/null +++ b/c_compiler/src/bindings.cpp @@ -0,0 +1,48 @@ +#include "bindings.hpp" + + +// VariableStackBindings definition + +VariableStackBindings::VariableStackBindings() + : stack_counter(4) +{} + +void VariableStackBindings::insertBinding(std::string id, Type* type, int32_t stack_position) +{ + DeclarationData decl_data; + decl_data.type = type; + decl_data.stack_position = stack_position; + + bindings.insert(std::pair<std::string, DeclarationData>(id, decl_data)); +} + +void VariableStackBindings::increaseStackPosition() +{ + stack_counter += 4; +} + +int32_t VariableStackBindings::getCurrentStackPosition() const +{ + return stack_counter; +} + +int32_t VariableStackBindings::getStackPosition(const std::string &id) const +{ + auto binding = bindings.find(id); + + if(binding != bindings.end()) + return (*binding).second.stack_position; + + else return 0; +} + +bool VariableStackBindings::bindingExists(const std::string &id) const +{ + auto binding = bindings.find(id); + + if(binding == bindings.end()) + return false; + + else + return true; +} diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index a42463c..b3430ee 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -126,8 +126,7 @@ Declaration: tmp_decl->setType($1); tmp_decl = tmp_decl->getNextListItem(); } - } - ; + }; DeclarationSpec: T_VOID { $$ = new Void; } @@ -149,7 +148,7 @@ InitDeclaratorList: InitDeclarator: Declarator { $$ = new Declaration(*$1); } - | Declarator T_EQ AssignmentExpression { $$ = new Declaration(*$1); } +| Declarator T_EQ AssignmentExpression { $$ = new Declaration(*$1, $3); } ; Declarator: diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index a7d7eb3..69504c9 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -1,15 +1,17 @@ -#include "ast.hpp" +#include "node.hpp" +#include "bindings.hpp" #include <iostream> +Node* parseAST(); + int main(int argc, char *argv[]) { Node* ast = parseAST(); VariableStackBindings bindings; - int32_t var_count; - - ast->printasm(bindings, var_count); + + ast->printasm(bindings); return 0; } diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 0f125b0..9eff776 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -1,10 +1,15 @@ -#include "ast.hpp" +#include "declaration.hpp" +#include "bindings.hpp" +#include "type.hpp" +#include "expression.hpp" + +#include <iostream> // Declaration definition -Declaration::Declaration(const std::string& _id) - : id(_id) +Declaration::Declaration(const std::string& _id, Expression* _init) + : id(_id), init(_init) {} void Declaration::print() const @@ -29,14 +34,39 @@ void Declaration::printxml() const std::cout << "<Variable id=\""<< id << "\" />" << std::endl; } -void Declaration::printasm(VariableStackBindings bindings, int32_t& var_count) const +VariableStackBindings Declaration::printasm(VariableStackBindings bindings) const { - if(init == nullptr) - std::cout << "\t.comm\t" << id << ",4,4" << std::endl; - else { - std::cout << "\t.data\n\t.globl\t" << id << std::endl; - std::cout << id << ":\n\t.word\t" << std::endl; + // if(init == nullptr) + // std::cout << "\t.comm\t" << id << ",4,4" << std::endl; + // else { + // std::cout << "\t.data\n\t.globl\t" << id << std::endl; + // std::cout << id << ":\n\t.word\t" << std::endl; + // } + + // return bindings; + + if(next_decl != nullptr) + bindings = next_decl->printasm(bindings); + + if(list_next_decl != nullptr) + bindings = list_next_decl->printasm(bindings); + + if(id != "") { + if(init != nullptr) + init->printasm(bindings); + else + std::cout << "\tmove\t$2,$0" << std::endl; + + int32_t stack_position = bindings.getCurrentStackPosition(); + + std::cout << "\tsw\t$2," << stack_position << "($fp)" << std::endl; + + bindings.insertBinding(id, type, stack_position); + + bindings.increaseStackPosition(); } + + return bindings; } void Declaration::addDeclaration(Declaration* _next_decl) diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 11fa66e..d9a366b 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -1,4 +1,5 @@ #include "expression.hpp" +#include "bindings.hpp" #include <iostream> @@ -17,8 +18,17 @@ Identifier::Identifier(const std::string& id) : m_id(id) {} -void Identifier::printasm(VariableStackBindings bindings) const -{} +VariableStackBindings Identifier::printasm(VariableStackBindings bindings) const +{ + if(bindings.bindingExists(m_id)) { + int32_t stack_position = bindings.getStackPosition(m_id); + + std::cout << "\tlw\t$2," << stack_position << "($fp)" << std::endl; + } else + std::cerr << "Can't find identifier '" << m_id << "' in current scope binding" << std::endl; + + return bindings; +} // Constant definition @@ -27,8 +37,10 @@ Constant::Constant(const int32_t& constant) : m_constant(constant) {} -void Constant::printasm(VariableStackBindings bindings) const +VariableStackBindings Constant::printasm(VariableStackBindings bindings) const { std::cout << "\tli\t$2," << m_constant << std::endl; + + return bindings; } diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 090e070..6c41c7a 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -1,6 +1,7 @@ #include "function.hpp" #include "statement.hpp" #include "declaration.hpp" +#include "bindings.hpp" #include <iostream> #include <vector> @@ -69,4 +70,6 @@ VariableStackBindings Function::printasm(VariableStackBindings bindings) const std::cout << "\tmove\t$sp,$fp\n\tlw\t$fp," << memory_needed-4 << "($sp)\n\taddiu\t$sp,$sp," << memory_needed << "\n\tjr\t$31\n\tnop" << std::endl; + + return bindings; } diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index 7b98631..af382dc 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -1,6 +1,7 @@ #include "statement.hpp" #include "declaration.hpp" #include "expression.hpp" +#include "bindings.hpp" #include <iostream> @@ -52,15 +53,20 @@ void CompoundStatement::printxml() const std::cout << "</Scope>" << std::endl; } -VariableStackBindings CompoundStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +VariableStackBindings CompoundStatement::printasm(VariableStackBindings bindings) const { + VariableStackBindings outer_scope_bindings = bindings; + if(next_statement != nullptr) - next_statement->printasm(bindings, var_count); + next_statement->printasm(bindings); - // TODO printasm for the declaration + if(m_decl != nullptr) + bindings = m_decl->printasm(bindings); if(m_statement != nullptr) - m_statement->printasm(bindings, var_count); + m_statement->printasm(bindings); + + return outer_scope_bindings; } void CompoundStatement::count_variables(int32_t& var_count) const @@ -110,8 +116,10 @@ void SelectionStatement::printxml() const m_else->printxml(); } -VariableStackBindings SelectionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const -{} +VariableStackBindings SelectionStatement::printasm(VariableStackBindings bindings) const +{ + return bindings; +} void SelectionStatement::count_variables(int32_t& var_count) const { @@ -138,8 +146,18 @@ void ExpressionStatement::print() const void ExpressionStatement::printxml() const {} -VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const -{} +VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindings) const +{ + if(next_statement != nullptr) + next_statement->printasm(bindings); + + if(m_expr != nullptr) { + std::cout << "SHould print" << std::endl; + m_expr->printasm(bindings); + } + + return bindings; +} void ExpressionStatement::count_variables(int32_t& var_count) const { @@ -163,9 +181,15 @@ void JumpStatement::printxml() const next_statement->printxml(); } -VariableStackBindings JumpStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +VariableStackBindings JumpStatement::printasm(VariableStackBindings bindings) const { - m_expr->printasm(bindings, var_count); + if(next_statement != nullptr) + next_statement->printasm(bindings); + + if(m_expr != nullptr) + m_expr->printasm(bindings); + + return bindings; } void JumpStatement::count_variables(int32_t& var_count) const @@ -192,8 +216,10 @@ void IterationStatement::printxml() const m_statement->printxml(); } -VariableStackBindings IterationStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const -{} +VariableStackBindings IterationStatement::printasm(VariableStackBindings bindings) const +{ + return bindings; +} void IterationStatement::count_variables(int32_t& var_count) const { diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp index e85237b..4bddac2 100644 --- a/c_compiler/src/translation_unit.cpp +++ b/c_compiler/src/translation_unit.cpp @@ -1,4 +1,7 @@ -#include "ast.hpp" +#include "translation_unit.hpp" +#include "bindings.hpp" + +#include <iostream> // Translation Unit definition @@ -24,11 +27,13 @@ void TranslationUnit::printxml() const std::cout << "</Program>" << std::endl; } -void TranslationUnit::printasm(VariableStackBindings bindings, int32_t& var_count) const +VariableStackBindings TranslationUnit::printasm(VariableStackBindings bindings) const { for(auto& node : translation_unit) { - node->printasm(bindings, var_count); + node->printasm(bindings); } + + return bindings; } void TranslationUnit::push(Node* decl) diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp index 13496e9..6a89e2a 100644 --- a/c_compiler/src/type.cpp +++ b/c_compiler/src/type.cpp @@ -1,4 +1,7 @@ -#include "ast.hpp" +#include "type.hpp" +#include "bindings.hpp" + +#include <iostream> // Type definition @@ -11,8 +14,10 @@ void Type::print() const void Type::printxml() const {} -void Type::printasm(VariableStackBindings bindings, int32_t& var_count) const -{} +VariableStackBindings Type::printasm(VariableStackBindings bindings) const +{ + return bindings; +} // Pointer definition |