diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-10 12:48:53 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-10 12:48:53 +0000 |
commit | 28251a0c6f4e31c63c12746ffa77e05c669ef80d (patch) | |
tree | e1a9dfad8db410a0ae0db0e56f1bfc9accac6262 /c_compiler/src | |
parent | 3e145bf08b1ffcccb4df8f2fc34f5bb95b5b250c (diff) | |
download | Compiler-28251a0c6f4e31c63c12746ffa77e05c669ef80d.tar.gz Compiler-28251a0c6f4e31c63c12746ffa77e05c669ef80d.zip |
Changing printasm
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/c_parser.y | 15 | ||||
-rw-r--r-- | c_compiler/src/compiler_main.cpp | 5 | ||||
-rw-r--r-- | c_compiler/src/declaration.cpp | 2 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 8 | ||||
-rw-r--r-- | c_compiler/src/function.cpp | 11 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 22 | ||||
-rw-r--r-- | c_compiler/src/translation_unit.cpp | 4 | ||||
-rw-r--r-- | c_compiler/src/type.cpp | 2 |
8 files changed, 45 insertions, 24 deletions
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 5866c57..a42463c 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -1,8 +1,15 @@ %code requires{ -#include "ast.hpp" +#include "node.hpp" +#include "translation_unit.hpp" +#include "function.hpp" +#include "declaration.hpp" +#include "statement.hpp" +#include "expression.hpp" +#include "type.hpp" -extern TranslationUnit* g_root; // A way of getting the AST out + +extern Node* g_root; // A way of getting the AST out //! This is to fix problems when generating C++ // We are declaring the functions provided by Flex, so @@ -338,9 +345,9 @@ Constant: %% -TranslationUnit* g_root; // Definition of variable (to match declaration earlier) +Node* g_root; // Definition of variable (to match declaration earlier) -TranslationUnit* parseAST() { +Node* parseAST() { g_root = 0; yyparse(); return g_root; diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index 2ba8e3d..a7d7eb3 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -5,8 +5,11 @@ int main(int argc, char *argv[]) { Node* ast = parseAST(); + + VariableStackBindings bindings; + int32_t var_count; - ast->printasm(); + ast->printasm(bindings, var_count); return 0; } diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 7b924b3..0f125b0 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -29,7 +29,7 @@ void Declaration::printxml() const std::cout << "<Variable id=\""<< id << "\" />" << std::endl; } -void Declaration::printasm() const +void Declaration::printasm(VariableStackBindings bindings, int32_t& var_count) const { if(init == nullptr) std::cout << "\t.comm\t" << id << ",4,4" << std::endl; diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 76a6c43..11fa66e 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -1,5 +1,6 @@ -#include "ast.hpp" +#include "expression.hpp" +#include <iostream> // Expression definition @@ -16,7 +17,7 @@ Identifier::Identifier(const std::string& id) : m_id(id) {} -void Identifier::printasm() const +void Identifier::printasm(VariableStackBindings bindings) const {} @@ -26,7 +27,8 @@ Constant::Constant(const int32_t& constant) : m_constant(constant) {} -void Constant::printasm() const +void Constant::printasm(VariableStackBindings bindings) const { std::cout << "\tli\t$2," << m_constant << std::endl; } + diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 5475ec9..090e070 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -1,4 +1,9 @@ -#include "ast.hpp" +#include "function.hpp" +#include "statement.hpp" +#include "declaration.hpp" + +#include <iostream> +#include <vector> // Function definition @@ -43,7 +48,7 @@ void Function::printxml() const std::cout << "</Function>" << std::endl; } -void Function::printasm() const +VariableStackBindings Function::printasm(VariableStackBindings bindings) const { // Counting all the variables being declared in the function int32_t count = 0; @@ -60,7 +65,7 @@ void Function::printasm() const // TODO print asm for parameters // Prints the asm for the compound statement in the function - statement->printasm(); + statement->printasm(bindings); 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; diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index 99b2879..7b98631 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -1,4 +1,8 @@ -#include "ast.hpp" +#include "statement.hpp" +#include "declaration.hpp" +#include "expression.hpp" + +#include <iostream> // General base Statement definition @@ -48,15 +52,15 @@ void CompoundStatement::printxml() const std::cout << "</Scope>" << std::endl; } -void CompoundStatement::printasm() const +VariableStackBindings CompoundStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const { if(next_statement != nullptr) - next_statement->printasm(); + next_statement->printasm(bindings, var_count); // TODO printasm for the declaration if(m_statement != nullptr) - m_statement->printasm(); + m_statement->printasm(bindings, var_count); } void CompoundStatement::count_variables(int32_t& var_count) const @@ -106,7 +110,7 @@ void SelectionStatement::printxml() const m_else->printxml(); } -void SelectionStatement::printasm() const +VariableStackBindings SelectionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const {} void SelectionStatement::count_variables(int32_t& var_count) const @@ -134,7 +138,7 @@ void ExpressionStatement::print() const void ExpressionStatement::printxml() const {} -void ExpressionStatement::printasm() const +VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const {} void ExpressionStatement::count_variables(int32_t& var_count) const @@ -159,9 +163,9 @@ void JumpStatement::printxml() const next_statement->printxml(); } -void JumpStatement::printasm() const +VariableStackBindings JumpStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const { - m_expr->printasm(); + m_expr->printasm(bindings, var_count); } void JumpStatement::count_variables(int32_t& var_count) const @@ -188,7 +192,7 @@ void IterationStatement::printxml() const m_statement->printxml(); } -void IterationStatement::printasm() const +VariableStackBindings IterationStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const {} 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 a3566d7..e85237b 100644 --- a/c_compiler/src/translation_unit.cpp +++ b/c_compiler/src/translation_unit.cpp @@ -24,10 +24,10 @@ void TranslationUnit::printxml() const std::cout << "</Program>" << std::endl; } -void TranslationUnit::printasm() const +void TranslationUnit::printasm(VariableStackBindings bindings, int32_t& var_count) const { for(auto& node : translation_unit) { - node->printasm(); + node->printasm(bindings, var_count); } } diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp index b7c3fec..13496e9 100644 --- a/c_compiler/src/type.cpp +++ b/c_compiler/src/type.cpp @@ -11,7 +11,7 @@ void Type::print() const void Type::printxml() const {} -void Type::printasm() const +void Type::printasm(VariableStackBindings bindings, int32_t& var_count) const {} |