From e9657092063e786a52fefcfa4c528bac07472908 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 26 Mar 2017 17:23:00 +0100 Subject: working printf and added ellipsis --- c_compiler/src/c_lexer.flex | 1 + c_compiler/src/c_parser.y | 11 ++++++++--- c_compiler/src/declaration.cpp | 2 +- c_compiler/src/expression.cpp | 7 ++++++- test_deliverable/c_compiler_ref.sh | 2 +- test_deliverable/testcases/test_MAINSHARR.c | 8 ++++++++ test_deliverable/testcases/test_MAINSHARR_driver.c | 10 ++++++++++ test_deliverable/testcases/test_PRINTF.c | 6 ++++++ test_deliverable/testcases/test_PRINTF_driver.c | 8 ++++++++ test_deliverable/testcases/test_SHORTARRAY.c | 10 ++++++++++ test_deliverable/testcases/test_SHORTARRAY_driver.c | 7 +++++++ 11 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 test_deliverable/testcases/test_MAINSHARR.c create mode 100644 test_deliverable/testcases/test_MAINSHARR_driver.c create mode 100644 test_deliverable/testcases/test_PRINTF.c create mode 100644 test_deliverable/testcases/test_PRINTF_driver.c create mode 100644 test_deliverable/testcases/test_SHORTARRAY.c create mode 100644 test_deliverable/testcases/test_SHORTARRAY_driver.c diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index 7ed46ae..8dcef35 100644 --- a/c_compiler/src/c_lexer.flex +++ b/c_compiler/src/c_lexer.flex @@ -54,6 +54,7 @@ ALL . (default) { return T_DEFAULT; } (switch) { return T_SWITCH; } +[.][.][.] { return T_ELLIPSIS; } [;] { return T_SC; } [,] { return T_CMA; } [(] { return T_LRB; } diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 1ef8d06..2c5d215 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -41,7 +41,7 @@ void yyerror(const char *); T_VOID T_CHAR T_SHORT T_INT T_LONG T_FLOAT T_DOUBLE T_SIGNED T_UNSIGNED T_TYPEDEF T_EXTERN T_STATIC T_AUTO T_REGISTER T_CONST T_VOLATILE T_GOTO T_BREAK T_CONTINUE - T_CASE T_DEFAULT T_SWITCH + T_CASE T_DEFAULT T_SWITCH T_ELLIPSIS %nonassoc T_RRB %nonassoc T_ELSE @@ -57,7 +57,7 @@ void yyerror(const char *); SelectionStatement ExpressionStatement JumpStatement IterationStatement LabeledStatement -%type ParameterList Parameter DeclarationList Declaration InitDeclaratorList +%type ParameterTypeList ParameterList Parameter DeclarationList Declaration InitDeclaratorList InitDeclarator IdentifierList Declarator DirectDeclarator @@ -104,6 +104,10 @@ FunctionDefinition: { $$ = new Function($2->getId(), $3, $2->getNext()); delete $1; } ; +ParameterTypeList: + ParameterList { $$ = $1; } + | ParameterList T_CMA T_ELLIPSIS { $$ = $1; } + ; ParameterList: Parameter { $$ = $1; } | ParameterList T_CMA Parameter { $3->linkDeclaration($$); $$ = $3; } @@ -222,7 +226,8 @@ DirectDeclarator: } | DirectDeclarator T_LSB T_RSB { $$ = $1; } | DirectDeclarator T_LRB T_RRB { $$ = $1; $$->setExternDeclaration(true); } - | DirectDeclarator T_LRB ParameterList T_RRB { $1->linkDeclaration($3); $$ = $1; $$->setExternDeclaration(true); } + | DirectDeclarator T_LRB ParameterTypeList T_RRB + { $1->linkDeclaration($3); $$ = $1; $$->setExternDeclaration(true); } | DirectDeclarator T_LRB IdentifierList T_RRB { $$ = $1; $$->setExternDeclaration(true); } ; diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 95db2b5..a3ee5d3 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -179,7 +179,7 @@ VariableStackBindings ArrayDeclaration::localAsm(VariableStackBindings bindings, { int initializer_count = itr-initializer_vector.rbegin(); (*itr)->printAsm(bindings, label_count); - type_->store(stack_position+4*initializer_count); + type_->store(stack_position+type_->getSize()*initializer_count); } } diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index a323153..466d8f7 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -138,7 +138,12 @@ VariableStackBindings PostfixArrayElement::printAsm(VariableStackBindings bindin { stackPosition(bindings, label_count); TypePtr type_ptr = postfix_expression_->getType(bindings); - type_ptr->load(); + std::shared_ptr pointer_type_ptr; + pointer_type_ptr = std::dynamic_pointer_cast(type_ptr); + if(pointer_type_ptr != nullptr) + pointer_type_ptr->pointerLoad(); + else + type_ptr->load(); printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition()); return bindings; } diff --git a/test_deliverable/c_compiler_ref.sh b/test_deliverable/c_compiler_ref.sh index a55b56d..7fe89e8 100755 --- a/test_deliverable/c_compiler_ref.sh +++ b/test_deliverable/c_compiler_ref.sh @@ -1,4 +1,4 @@ #!/bin/bash -mips-linux-gnu-gcc -c -S -x c - -o - +mips-linux-gnu-gcc -std=c89 -c -S -x c - -o - exit $? diff --git a/test_deliverable/testcases/test_MAINSHARR.c b/test_deliverable/testcases/test_MAINSHARR.c new file mode 100644 index 0000000..bbf9ba0 --- /dev/null +++ b/test_deliverable/testcases/test_MAINSHARR.c @@ -0,0 +1,8 @@ +short *shortarray(short int *array, short a, short int b, short signed int c, short d, short e); + +int main() +{ + short signed int array[5] = {4, 23, 5, 6, 2}; + short *arr = shortarray(array, 39, 59, 145, 23, 329); + return !( 329 == arr[3] ); +} diff --git a/test_deliverable/testcases/test_MAINSHARR_driver.c b/test_deliverable/testcases/test_MAINSHARR_driver.c new file mode 100644 index 0000000..af2929a --- /dev/null +++ b/test_deliverable/testcases/test_MAINSHARR_driver.c @@ -0,0 +1,10 @@ +short *shortarray(short int *array, short a, short int b, short signed int c, short d, short e) +{ + array[4] = a; + array[0] = b; + array[2] = c; + array[1] = d; + array[3] = e; + + return array; +} diff --git a/test_deliverable/testcases/test_PRINTF.c b/test_deliverable/testcases/test_PRINTF.c new file mode 100644 index 0000000..9c4e005 --- /dev/null +++ b/test_deliverable/testcases/test_PRINTF.c @@ -0,0 +1,6 @@ +int printf(const char *format, ...); + +int printf_(const char *format) +{ + return printf(format); +} diff --git a/test_deliverable/testcases/test_PRINTF_driver.c b/test_deliverable/testcases/test_PRINTF_driver.c new file mode 100644 index 0000000..9de1039 --- /dev/null +++ b/test_deliverable/testcases/test_PRINTF_driver.c @@ -0,0 +1,8 @@ +#include + +int printf_(const char *); + +int main() +{ + printf_("Hello World!: "); +} diff --git a/test_deliverable/testcases/test_SHORTARRAY.c b/test_deliverable/testcases/test_SHORTARRAY.c new file mode 100644 index 0000000..af2929a --- /dev/null +++ b/test_deliverable/testcases/test_SHORTARRAY.c @@ -0,0 +1,10 @@ +short *shortarray(short int *array, short a, short int b, short signed int c, short d, short e) +{ + array[4] = a; + array[0] = b; + array[2] = c; + array[1] = d; + array[3] = e; + + return array; +} diff --git a/test_deliverable/testcases/test_SHORTARRAY_driver.c b/test_deliverable/testcases/test_SHORTARRAY_driver.c new file mode 100644 index 0000000..d9a35e4 --- /dev/null +++ b/test_deliverable/testcases/test_SHORTARRAY_driver.c @@ -0,0 +1,7 @@ +short *shortarray(short int *array, short a, short int b, short signed int c, short d, short e); + +int main() +{ + short signed int array[5] = {4, 23, 5, 6, 2}; + return !( 324 == shortarray(array, 39, 59, 145, 23, 324)[3] ); +} -- cgit