From c0feca77799b859a5331417595ac0190bb999e0d Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 15:10:11 +0200 Subject: extract while instr --- src/generator.c | 14 +++++++++++++- src/generator.h | 4 ++++ src/tpc.y | 6 +++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/generator.c b/src/generator.c index e4b79e3..9948966 100644 --- a/src/generator.c +++ b/src/generator.c @@ -74,7 +74,7 @@ void gen_prologue_continue(bool *bss_done) { fprintf(output, "pop rsi\n"); fprintf(output, "pop rbp\n"); fprintf(output, "ret\n"); - *bss_done = 1; + *bss_done = true; } void gen_epilogue() { @@ -523,3 +523,15 @@ int gen_char(int value, Scope scope) { fprintf(output, "push %d\n", value); return CHAR; } + +void gen_while_start(int id) { + fprintf(output,".upwhile%d:\n", id); +} + +void gen_while_cond(int id) { + fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", id); +} + +void gen_while_end(int id) { + fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n", id, id); +} diff --git a/src/generator.h b/src/generator.h index e1fd714..94b03c8 100644 --- a/src/generator.h +++ b/src/generator.h @@ -38,6 +38,10 @@ void gen_if_start(int idx); void gen_if_end(int idx); void gen_ifelse_end(int idx); +void gen_while_start(int id); +void gen_while_cond(int id); +void gen_while_end(int id); + int gen_assign(const char ident[], Scope scope); void gen_or(int left, int right, int idx); diff --git a/src/tpc.y b/src/tpc.y index 16dc5ad..1e6b6e2 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -135,9 +135,9 @@ Instr: | IF '(' Exp IfHandling')' Instr { gen_if_label($4); } | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling -| WHILE { fprintf(output,".upwhile%d:\n", num_while); } - '(' Exp { fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", num_while); } - ')' Instr { fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n", num_while, num_while); num_while++; } +| WHILE { gen_while_start(num_while); } + '(' Exp { gen_while_cond(num_while); } + ')' Instr { gen_while_end(num_while++); } | '{' SuiteInstr '}' ; IfHandling: { gen_if_start($$ = num_if++); }; -- cgit v1.2.3