diff options
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/c_lexer.flex | 2 | ||||
-rw-r--r-- | c_compiler/src/c_parser.y | 28 | ||||
-rw-r--r-- | c_compiler/src/compiler_main.cpp | 2 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 24 | ||||
-rw-r--r-- | c_compiler/src/function.cpp | 16 | ||||
-rw-r--r-- | c_compiler/src/initializer.cpp | 28 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 15 |
7 files changed, 57 insertions, 58 deletions
diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index 7690d71..b225522 100644 --- a/c_compiler/src/c_lexer.flex +++ b/c_compiler/src/c_lexer.flex @@ -1,8 +1,6 @@ %option noyywrap %{ -// Avoid error "error: fileno was not declared in this scope" -extern "C" int fileno(FILE *stream); #include "c_parser.tab.hpp" diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index c643f10..5866c57 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -22,7 +22,6 @@ void yyerror(const char *); Declaration* declaration; Expression* expression; Type* type; - Initializer* initializer; double number; std::string* string; } @@ -57,13 +56,12 @@ void yyerror(const char *); AndExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression + Constant %type <type> DeclarationSpec %type <string> Declarator DirectDeclarator -%type <initializer> 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 @@ -202,7 +200,7 @@ ExpressionStatement: ; JumpStatement: - T_RETURN ExpressionStatement { $$ = new JumpStatement(); } + T_RETURN Expression T_SC { $$ = new JumpStatement($2); } ; IterationStatement: @@ -298,7 +296,7 @@ UnaryExpression: | T_INCDEC UnaryExpression { $$ = $2; } | UnaryOperator CastExpression { $$ = $2; } | T_SIZEOF UnaryExpression { $$ = $2; } - | T_SIZEOF T_LRB DeclarationSpec T_RRB { $$ = new Expression(); } + | T_SIZEOF T_LRB DeclarationSpec T_RRB { $$ = new Constant(0); } ; UnaryOperator: @@ -313,29 +311,29 @@ PostfixExpression: PrimaryExpression { $$ = $1; } | PostfixExpression T_LSB Expression T_RSB { $$ = $3; } | PostfixExpression T_LRB PostfixExpression2 { $$ = $3; } - | PostfixExpression T_DOT T_IDENTIFIER { $$ = new Expression(); } - | PostfixExpression T_ARROW T_IDENTIFIER { $$ = new Expression(); } - | PostfixExpression T_INCDEC { $$ = new Expression(); } + | PostfixExpression T_DOT T_IDENTIFIER { $$ = $1; } + | PostfixExpression T_ARROW T_IDENTIFIER { $$ = $1; } + | PostfixExpression T_INCDEC { $$ = $1; } ; PostfixExpression2: - T_RRB { $$ = new Expression(); } - | ArgumentExpressionList T_RRB { $$ = new Expression; } + T_RRB { $$ = new Constant(0); } + | ArgumentExpressionList T_RRB { $$ = new Constant(0); } ; ArgumentExpressionList: - AssignmentExpression { $$ = new Expression; } - | ArgumentExpressionList T_CMA AssignmentExpression { $$ = new Expression; } + AssignmentExpression { $$ = $1; } + | ArgumentExpressionList T_CMA AssignmentExpression { $$ = $1; } ; PrimaryExpression: - T_IDENTIFIER { $$ = new Expression(); } - | Constant { $$ = new Expression($1); } + T_IDENTIFIER { $$ = new Identifier(*$1); } + | Constant { $$ = $1; } | T_LRB Expression T_RRB { $$ = $2; } ; Constant: - T_INT_CONST { $$ = new Initializer(); } + T_INT_CONST { $$ = new Constant($1); } ; %% diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index b550eba..e0222a3 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -6,8 +6,6 @@ int main(int argc, char *argv[]) { Node* ast = parseAST(); - ast->printxml(); - ast->printasm(); return 0; diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 4b09581..76a6c43 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -3,14 +3,30 @@ // Expression definition -Expression::Expression(const Node* expr) -{} - void Expression::print() const {} void Expression::printxml() const {} -void Expression::printasm() const + +// Identifier definition + +Identifier::Identifier(const std::string& id) + : m_id(id) +{} + +void Identifier::printasm() const {} + + +// Constant definition + +Constant::Constant(const int32_t& constant) + : m_constant(constant) +{} + +void Constant::printasm() 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 acd5547..5475ec9 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -45,9 +45,23 @@ void Function::printxml() const void Function::printasm() const { + // Counting all the variables being declared in the function int32_t count = 0; if(statement != nullptr) statement->count_variables(count); - std::cout << id << ": " << count << " variables defined" << std::endl; + // This includes the space for the old frame counter and (return address)? + int32_t memory_needed = 4*count+8; + + std::cout << "\t.text\n\t.globl\t" << id << std::endl << id << ":\n\taddiu\t$sp,$sp,-" + << memory_needed << "\n\tsw\t$fp," << memory_needed-4 << "($sp)\n\tmove\t$fp,$sp" + << std::endl; + + // TODO print asm for parameters + + // Prints the asm for the compound statement in the function + statement->printasm(); + + 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/initializer.cpp b/c_compiler/src/initializer.cpp deleted file mode 100644 index 580d81f..0000000 --- a/c_compiler/src/initializer.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "ast.hpp" - - -// Initializer definition - -Initializer::Initializer() -{} - -void Initializer::print() const -{} - -void Initializer::printxml() const -{} - -void Initializer::printasm() const -{} - - -// Integer definition - -Integer::Integer() : Initializer() -{} - - -// String Literal definition - -StringLiteral::StringLiteral() : Initializer() -{} diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index dcd2271..99b2879 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -49,7 +49,15 @@ void CompoundStatement::printxml() const } void CompoundStatement::printasm() const -{} +{ + if(next_statement != nullptr) + next_statement->printasm(); + + // TODO printasm for the declaration + + if(m_statement != nullptr) + m_statement->printasm(); +} void CompoundStatement::count_variables(int32_t& var_count) const { @@ -67,13 +75,9 @@ void CompoundStatement::count_variables(int32_t& var_count) const while(declaration_list != nullptr) { var_count++; - std::cout << declaration_list->getType() << std::endl; - declaration_list = declaration_list->getNextListItem(); } - std::cout << declaration->getType() << std::endl; - var_count++; declaration = declaration->getNext(); @@ -158,7 +162,6 @@ void JumpStatement::printxml() const void JumpStatement::printasm() const { m_expr->printasm(); - std::cout << "\tlw\t$2,8($fp)" << std::endl; } void JumpStatement::count_variables(int32_t& var_count) const |