From 2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Fri, 4 May 2018 09:14:41 +0200 Subject: DIVSTAR working : 'It's working ! It's working' said Anakin --- res/test_add.tpc | 1 + res/test_mul.tpc | 12 ++++++++++++ src/symboltable.c | 6 ++++++ src/tpc.y | 37 ++++++++++++++++++++++++++++++++----- 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 res/test_mul.tpc diff --git a/res/test_add.tpc b/res/test_add.tpc index 01e0403..2f6b9dd 100644 --- a/res/test_add.tpc +++ b/res/test_add.tpc @@ -1,6 +1,7 @@ /* test-table-symboles.tpc */ /* Test file for simplified translator of a declaration of variables in C */ +entier a; entier main(void) { entier a,b,res; diff --git a/res/test_mul.tpc b/res/test_mul.tpc new file mode 100644 index 0000000..f3b6705 --- /dev/null +++ b/res/test_mul.tpc @@ -0,0 +1,12 @@ +/* test-table-symboles.tpc */ + +/* Test file for simplified translator of a declaration of variables in C */ +entier a; + +entier main(void) { + entier a,b,res; + a = 2; + b = 3; + res = a % b; + print(res); +} diff --git a/src/symboltable.c b/src/symboltable.c index 5c63d3f..d20b0dc 100644 --- a/src/symboltable.c +++ b/src/symboltable.c @@ -94,6 +94,12 @@ int loc_lookup(const char name[]) { return loc_symbol_table.entries[count].type; } } + //Check in global table + for (count = 0; count < glo_symbol_table.size; count++) { + if (!strcmp(glo_symbol_table.entries[count].name, name)) { + return glo_symbol_table.entries[count].type; + } + } fprintf(stderr, "No definition of the variable %s near line %d\n", name, lineno); return -1; diff --git a/src/tpc.y b/src/tpc.y index 387b3ef..41fa1d2 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -2,6 +2,19 @@ /* * UPEM / Compilation / Projet * Pacien TRAN-GIRARD, Adam NAILI + * + * + * + * + * + * + * TODO : + * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.) + * - Evaluation paresseuse + * - Gestion des tableaux + * + * + * */ #include @@ -160,7 +173,8 @@ Exp: | EB ; EB: - EB OR TB {check_expected_type($1,INT);check_expected_type($3,INT);} + EB {check_expected_type($1,INT); printf("pop rax\n");} + OR TB {check_expected_type($4,INT);} | TB ; TB: @@ -187,7 +201,20 @@ E: | T ; T: - T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT);} + T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT); + if($2 == '*'){ + printf(";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n"); + } + else{ + printf(";E / T\npop rax\npop rcx\nxor rdx,rdx\nidiv rcx\n"); + if ($2 == '/'){ + printf("push rax\n"); + } + else{ + printf("push rdx\n"); + } + } + } | F ; F: @@ -196,15 +223,15 @@ F: printf(";+F\n"); } else{ - printf(";-F\npop rdx\nxor eax,eax\nsub eax,edx\npush rax\n"); + printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n"); } } | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} | '(' Exp ')' {$$ = $2;} | LValue {if(status == GLOBAL) {$$ = glo_lookup($1);printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($1),$1);} else {$$ = loc_lookup($1); printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($1),$1);}} - | NUM {$$ = INT; printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique - | CARACTERE {$$ = CHAR; printf("push %d\n",$1);} + | NUM {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique + | CARACTERE {$$ = CHAR; if(status == LOCAL) printf("push %d\n",$1);} | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement ; LValue: -- cgit v1.2.3