aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-26 17:23:00 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-03-26 17:23:00 +0100
commite9657092063e786a52fefcfa4c528bac07472908 (patch)
tree01ac8abdc1ad5af82b0e48f163cd1e2b7cadced0
parentdec9dc403e540ef4424debfdd866c73ca93adc83 (diff)
downloadCompiler-e9657092063e786a52fefcfa4c528bac07472908.tar.gz
Compiler-e9657092063e786a52fefcfa4c528bac07472908.zip
working printf and added ellipsis
-rw-r--r--c_compiler/src/c_lexer.flex1
-rw-r--r--c_compiler/src/c_parser.y11
-rw-r--r--c_compiler/src/declaration.cpp2
-rw-r--r--c_compiler/src/expression.cpp7
-rwxr-xr-xtest_deliverable/c_compiler_ref.sh2
-rw-r--r--test_deliverable/testcases/test_MAINSHARR.c8
-rw-r--r--test_deliverable/testcases/test_MAINSHARR_driver.c10
-rw-r--r--test_deliverable/testcases/test_PRINTF.c6
-rw-r--r--test_deliverable/testcases/test_PRINTF_driver.c8
-rw-r--r--test_deliverable/testcases/test_SHORTARRAY.c10
-rw-r--r--test_deliverable/testcases/test_SHORTARRAY_driver.c7
11 files changed, 66 insertions, 6 deletions
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 <declaration> ParameterList Parameter DeclarationList Declaration InitDeclaratorList
+%type <declaration> 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> pointer_type_ptr;
+ pointer_type_ptr = std::dynamic_pointer_cast<Pointer>(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 <stdio.h>
+
+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] );
+}