From 07c0a816be9c9f3342272623e69ae0d134ebe46a Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 28 Mar 2017 20:37:19 +0100 Subject: Completely done --- Notes.org | 16 +++++++++++----- c_compiler/src/declaration.cpp | 18 +++++++++++++++--- test_deliverable/testcases/test_ASSIGNMULT.c | 14 ++++++++++++++ test_deliverable/testcases/test_ASSIGNMULT_driver.c | 6 ++++++ 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 test_deliverable/testcases/test_ASSIGNMULT.c create mode 100644 test_deliverable/testcases/test_ASSIGNMULT_driver.c diff --git a/Notes.org b/Notes.org index a3046ae..dfc260f 100644 --- a/Notes.org +++ b/Notes.org @@ -126,13 +126,13 @@ - The declaration class should only be in charge of storing it in the right location in the stack and adding that to the bindings. -**** TODO Add more expressions +**** DONE Add more expressions CLOCK: [2017-03-17 Fri 17:08]--[2017-03-17 Fri 20:59] => 3:51 CLOCK: [2017-03-17 Fri 13:21]--[2017-03-17 Fri 15:43] => 2:22 Expressions like > or < or == etc.. -**** TODO Add more statements +**** DONE Add more statements ***** DONE If statement CLOCK: [2017-03-18 Sat 08:42]--[2017-03-18 Sat 09:57] => 1:15 ***** DONE For statement @@ -147,18 +147,24 @@ I have to store the temporary expression in normal registers. -**** TODO Convert cout to printf +**** DONE Convert cout to printf convert all cout to printf -**** TODO Add global variables +**** DONE Add global variables Change code so that it also prints global variables. This should be done with the globalAsm function. I might have to change the print Asm and the global Asm function in the declarations. -**** TODO Add post and pre increment +**** DONE Add post and pre increment CLOCK: [2017-03-20 Mon 15:12] For post increment I will have a special class that handles that, for pre increment I will rewrite in terms of addition, which should be possible. +**** DONE Arrays + Finished arrays + +**** DONE Pointers + Finished pointers +**** TODO Add initializer list count of expression depth diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 0865619..7c7791e 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -201,6 +201,7 @@ Bindings ArrayDeclaration::localAsm(Bindings bindings, int &label_count) const if(getId() != "") { + int stack_position = bindings.currentStackPosition(); std::shared_ptr array_declaration( std::dynamic_pointer_cast(declarator_)); @@ -213,9 +214,20 @@ Bindings ArrayDeclaration::localAsm(Bindings bindings, int &label_count) const } std::shared_ptr initializer; - initializer = std::static_pointer_cast(initializer_); - initializer->printInitializerAsm(bindings, label_count, array_sizes.size()-1, array_sizes, type_->type()); - + if(initializer_ != nullptr) + { + initializer = std::static_pointer_cast(initializer_); + initializer->printInitializerAsm(bindings, label_count, array_sizes.size()-1, array_sizes, type_->type()); + } + else + { + int sum = 1; + std::for_each(array_sizes.begin(), array_sizes.end(), [&] (int n) { + sum *= n; + }); + sum *= getType()->getSize(); + bindings.increaseStackPosition(sum); + } // reverse vector to store in binding std::reverse(array_sizes.begin(), array_sizes.end()); bindings.insertBinding(getId(), type_, stack_position, array_sizes); diff --git a/test_deliverable/testcases/test_ASSIGNMULT.c b/test_deliverable/testcases/test_ASSIGNMULT.c new file mode 100644 index 0000000..215b0f8 --- /dev/null +++ b/test_deliverable/testcases/test_ASSIGNMULT.c @@ -0,0 +1,14 @@ +int assignmult() +{ + int x[10][10]; + int j = 0; + int k = 0; + + for(j = 0; j < 10; ++j) + for(k = 0; k < 10; ++k) + { + x[j][k] = j*10+k; + } + + return x[4][2]; +} diff --git a/test_deliverable/testcases/test_ASSIGNMULT_driver.c b/test_deliverable/testcases/test_ASSIGNMULT_driver.c new file mode 100644 index 0000000..857d60f --- /dev/null +++ b/test_deliverable/testcases/test_ASSIGNMULT_driver.c @@ -0,0 +1,6 @@ +int assignmult(); + +int main() +{ + return !(42 == assignmult()); +} -- cgit