aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2018-05-04 11:19:08 +0200
committerAdam NAILI2018-05-04 11:19:08 +0200
commit13e036c9ffcf670869b4682f8f6993cc6667ab07 (patch)
treea64fd28439b60634930a2ba8ad40145ec8038185
parent2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81 (diff)
downloadtpc-compiler-13e036c9ffcf670869b4682f8f6993cc6667ab07.tar.gz
IF/ELSE
-rw-r--r--res/test_if.tpc13
-rw-r--r--src/test_if.tpc43
-rw-r--r--src/tpc.y15
3 files changed, 66 insertions, 5 deletions
diff --git a/res/test_if.tpc b/res/test_if.tpc
new file mode 100644
index 0000000..04f7505
--- /dev/null
+++ b/res/test_if.tpc
@@ -0,0 +1,13 @@
1/* test-table-symboles.tpc */
2
3/* Test file for simplified translator of a declaration of variables in C */
4
5entier main(void) {
6 entier bool;
7 bool = 1;
8 if(bool){
9 if(bool){
10 print (bool);
11 }
12 }
13}
diff --git a/src/test_if.tpc b/src/test_if.tpc
new file mode 100644
index 0000000..356061f
--- /dev/null
+++ b/src/test_if.tpc
@@ -0,0 +1,43 @@
1extern printf
2section .data
3format_int db "%d",10,0
4
5section .bss
6section .text
7
8global _start
9print: ;print needs an argument in rax
10push rbp
11mov rbp, rsp
12push rsi
13mov rsi, rax
14mov rdi, format_int
15mov rax, 0
16call printf WRT ..plt
17pop rsi
18pop rbp
19ret
20
21_start:
22push rbp
23mov rbp, rsp
24
25push 0
26push 1
27pop QWORD [rbp - 0] ;bool
28push QWORD [rbp - 0] ;bool
29pop rax
30cmp rax,0
31jz .end_if0
32push QWORD [rbp - 0] ;bool
33pop rax
34call print
35.end_if0
36mov rax,60
37mov rdi,0
38syscall
39;global table
40
41;local table
42;entier: bool, pos: 0
43
diff --git a/src/tpc.y b/src/tpc.y
index 41fa1d2..340ce5c 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -9,12 +9,13 @@
9 * 9 *
10 * 10 *
11 * TODO : 11 * TODO :
12 * ------
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 * - 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 * - Evaluation paresseuse
14 * - Gestion des tableaux 15 * - Gestion des tableaux
15 * 16 * - Tableau des fonctions
16 * 17 *
17 * 18 * - remettre car conflit pour l'instant->
18 */ 19 */
19 20
20#include <stdio.h> 21#include <stdio.h>
@@ -26,7 +27,8 @@ void yyerror(char *);
26#define GLOBAL 0 27#define GLOBAL 0
27#define LOCAL 1 28#define LOCAL 1
28static int status = GLOBAL; 29static int status = GLOBAL;
29 30static int num_label = 0;
31static int num_if = 0;
30%} 32%}
31 33
32%union { 34%union {
@@ -155,11 +157,14 @@ Instr:
155 | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); 157 | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3);
156 else loc_lookup($<ident>3);} 158 else loc_lookup($<ident>3);}
157 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} 159 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");}
158 | IF '(' Exp ')' Instr 160 | IF '(' Exp IfHandling')' Instr IfEndHandling
159 | IF '(' Exp ')' Instr ELSE Instr 161 | IF '(' Exp IfHandling')' Instr IfEndHandling ELSE Instr IfElseEndHandling
160 | WHILE '(' Exp ')' Instr 162 | WHILE '(' Exp ')' Instr
161 | '{' SuiteInstr '}' 163 | '{' SuiteInstr '}'
162 ; 164 ;
165IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);};
166IfEndHandling: {printf(".end_if%d\n",$<num>-2);};
167IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);};
163Exp: 168Exp:
164 LValue '=' Exp {if(status == GLOBAL){ 169 LValue '=' Exp {if(status == GLOBAL){
165 $$ = glo_lookup($<ident>1); 170 $$ = glo_lookup($<ident>1);