diff options
author | Adam NAILI | 2018-05-04 11:19:08 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-04 11:19:08 +0200 |
commit | 13e036c9ffcf670869b4682f8f6993cc6667ab07 (patch) | |
tree | a64fd28439b60634930a2ba8ad40145ec8038185 /src | |
parent | 2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81 (diff) | |
download | tpc-compiler-13e036c9ffcf670869b4682f8f6993cc6667ab07.tar.gz |
IF/ELSE
Diffstat (limited to 'src')
-rw-r--r-- | src/test_if.tpc | 43 | ||||
-rw-r--r-- | src/tpc.y | 15 |
2 files changed, 53 insertions, 5 deletions
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 @@ | |||
1 | extern printf | ||
2 | section .data | ||
3 | format_int db "%d",10,0 | ||
4 | |||
5 | section .bss | ||
6 | section .text | ||
7 | |||
8 | global _start | ||
9 | print: ;print needs an argument in rax | ||
10 | push rbp | ||
11 | mov rbp, rsp | ||
12 | push rsi | ||
13 | mov rsi, rax | ||
14 | mov rdi, format_int | ||
15 | mov rax, 0 | ||
16 | call printf WRT ..plt | ||
17 | pop rsi | ||
18 | pop rbp | ||
19 | ret | ||
20 | |||
21 | _start: | ||
22 | push rbp | ||
23 | mov rbp, rsp | ||
24 | |||
25 | push 0 | ||
26 | push 1 | ||
27 | pop QWORD [rbp - 0] ;bool | ||
28 | push QWORD [rbp - 0] ;bool | ||
29 | pop rax | ||
30 | cmp rax,0 | ||
31 | jz .end_if0 | ||
32 | push QWORD [rbp - 0] ;bool | ||
33 | pop rax | ||
34 | call print | ||
35 | .end_if0 | ||
36 | mov rax,60 | ||
37 | mov rdi,0 | ||
38 | syscall | ||
39 | ;global table | ||
40 | |||
41 | ;local table | ||
42 | ;entier: bool, pos: 0 | ||
43 | |||
@@ -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 |
28 | static int status = GLOBAL; | 29 | static int status = GLOBAL; |
29 | 30 | static int num_label = 0; | |
31 | static 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 | ; |
165 | IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);}; | ||
166 | IfEndHandling: {printf(".end_if%d\n",$<num>-2);}; | ||
167 | IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);}; | ||
163 | Exp: | 168 | Exp: |
164 | LValue '=' Exp {if(status == GLOBAL){ | 169 | LValue '=' Exp {if(status == GLOBAL){ |
165 | $$ = glo_lookup($<ident>1); | 170 | $$ = glo_lookup($<ident>1); |