From 13e036c9ffcf670869b4682f8f6993cc6667ab07 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Fri, 4 May 2018 11:19:08 +0200 Subject: IF/ELSE --- src/test_if.tpc | 43 +++++++++++++++++++++++++++++++++++++++++++ src/tpc.y | 15 ++++++++++----- 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/test_if.tpc (limited to 'src') 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 @@ +extern printf +section .data +format_int db "%d",10,0 + +section .bss +section .text + +global _start +print: ;print needs an argument in rax +push rbp +mov rbp, rsp +push rsi +mov rsi, rax +mov rdi, format_int +mov rax, 0 +call printf WRT ..plt +pop rsi +pop rbp +ret + +_start: +push rbp +mov rbp, rsp + +push 0 +push 1 +pop QWORD [rbp - 0] ;bool +push QWORD [rbp - 0] ;bool +pop rax +cmp rax,0 +jz .end_if0 +push QWORD [rbp - 0] ;bool +pop rax +call print +.end_if0 +mov rax,60 +mov rdi,0 +syscall +;global table + +;local table +;entier: bool, pos: 0 + 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 @@ * * * 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 - * + * - Tableau des fonctions * - * + * - remettre car conflit pour l'instant-> */ #include @@ -26,7 +27,8 @@ void yyerror(char *); #define GLOBAL 0 #define LOCAL 1 static int status = GLOBAL; - +static int num_label = 0; +static int num_if = 0; %} %union { @@ -155,11 +157,14 @@ Instr: | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($3); else loc_lookup($3);} | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} - | IF '(' Exp ')' Instr - | IF '(' Exp ')' Instr ELSE Instr + | IF '(' Exp IfHandling')' Instr IfEndHandling + | IF '(' Exp IfHandling')' Instr IfEndHandling ELSE Instr IfElseEndHandling | WHILE '(' Exp ')' Instr | '{' SuiteInstr '}' ; +IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$$ = num_if++);}; +IfEndHandling: {printf(".end_if%d\n",$-2);}; +IfElseEndHandling: {printf(".end_if%d\n",$-4);}; Exp: LValue '=' Exp {if(status == GLOBAL){ $$ = glo_lookup($1); -- cgit v1.2.3