diff options
author | Adam NAILI | 2018-05-04 09:14:41 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-04 09:14:41 +0200 |
commit | 2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81 (patch) | |
tree | b50adeb1fc3189df196fef8c1dec40e32589a50d /src | |
parent | c9c4fbbde6e4f4f3942777cd9930cf40375e0ea9 (diff) | |
download | tpc-compiler-2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81.tar.gz |
DIVSTAR working : 'It's working ! It's working' said Anakin
Diffstat (limited to 'src')
-rw-r--r-- | src/symboltable.c | 6 | ||||
-rw-r--r-- | src/tpc.y | 37 |
2 files changed, 38 insertions, 5 deletions
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; |
@@ -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 | ; |
162 | EB: | 175 | EB: |
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 | ; |
166 | TB: | 180 | TB: |
@@ -187,7 +201,20 @@ E: | |||
187 | | T | 201 | | T |
188 | ; | 202 | ; |
189 | T: | 203 | T: |
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 | ; |
193 | F: | 220 | F: |
@@ -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 | ; |
210 | LValue: | 237 | LValue: |