aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--res/test_add.tpc1
-rw-r--r--res/test_mul.tpc12
-rw-r--r--src/symboltable.c6
-rw-r--r--src/tpc.y37
4 files changed, 51 insertions, 5 deletions
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 @@
1/* test-table-symboles.tpc */ 1/* test-table-symboles.tpc */
2 2
3/* Test file for simplified translator of a declaration of variables in C */ 3/* Test file for simplified translator of a declaration of variables in C */
4entier a;
4 5
5entier main(void) { 6entier main(void) {
6 entier a,b,res; 7 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 @@
1/* test-table-symboles.tpc */
2
3/* Test file for simplified translator of a declaration of variables in C */
4entier a;
5
6entier main(void) {
7 entier a,b,res;
8 a = 2;
9 b = 3;
10 res = a % b;
11 print(res);
12}
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[]) {
94 return loc_symbol_table.entries[count].type; 94 return loc_symbol_table.entries[count].type;
95 } 95 }
96 } 96 }
97 //Check in global table
98 for (count = 0; count < glo_symbol_table.size; count++) {
99 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
100 return glo_symbol_table.entries[count].type;
101 }
102 }
97 fprintf(stderr, "No definition of the variable %s near line %d\n", name, 103 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
98 lineno); 104 lineno);
99 return -1; 105 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 @@
2/* 2/*
3 * UPEM / Compilation / Projet 3 * UPEM / Compilation / Projet
4 * Pacien TRAN-GIRARD, Adam NAILI 4 * Pacien TRAN-GIRARD, Adam NAILI
5 *
6 *
7 *
8 *
9 *
10 *
11 * TODO :
12 * - 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.)
13 * - Evaluation paresseuse
14 * - Gestion des tableaux
15 *
16 *
17 *
5 */ 18 */
6 19
7#include <stdio.h> 20#include <stdio.h>
@@ -160,7 +173,8 @@ Exp:
160 | EB 173 | EB
161 ; 174 ;
162EB: 175EB:
163 EB OR TB {check_expected_type($1,INT);check_expected_type($3,INT);} 176 EB {check_expected_type($1,INT); printf("pop rax\n");}
177 OR TB {check_expected_type($4,INT);}
164 | TB 178 | TB
165 ; 179 ;
166TB: 180TB:
@@ -187,7 +201,20 @@ E:
187 | T 201 | T
188 ; 202 ;
189T: 203T:
190 T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT);} 204 T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT);
205 if($2 == '*'){
206 printf(";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n");
207 }
208 else{
209 printf(";E / T\npop rax\npop rcx\nxor rdx,rdx\nidiv rcx\n");
210 if ($2 == '/'){
211 printf("push rax\n");
212 }
213 else{
214 printf("push rdx\n");
215 }
216 }
217 }
191 | F 218 | F
192 ; 219 ;
193F: 220F:
@@ -196,15 +223,15 @@ F:
196 printf(";+F\n"); 223 printf(";+F\n");
197 } 224 }
198 else{ 225 else{
199 printf(";-F\npop rdx\nxor eax,eax\nsub eax,edx\npush rax\n"); 226 printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n");
200 } 227 }
201 } 228 }
202 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} 229 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");}
203 | '(' Exp ')' {$$ = $2;} 230 | '(' Exp ')' {$$ = $2;}
204 | LValue {if(status == GLOBAL) {$$ = glo_lookup($<ident>1);printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);} 231 | LValue {if(status == GLOBAL) {$$ = glo_lookup($<ident>1);printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);}
205 else {$$ = loc_lookup($<ident>1); printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);}} 232 else {$$ = loc_lookup($<ident>1); printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);}}
206 | NUM {$$ = INT; printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique 233 | NUM {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique
207 | CARACTERE {$$ = CHAR; printf("push %d\n",$1);} 234 | CARACTERE {$$ = CHAR; if(status == LOCAL) printf("push %d\n",$1);}
208 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement 235 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement
209 ; 236 ;
210LValue: 237LValue: