From 628b18fb15ee3d6633c73d404f109d01f0a7ece2 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Sun, 3 Jun 2018 13:37:00 +0200 Subject: .bss section management (globals) --- res/test_global.tpc | 12 ++++++++++ src/generator.c | 65 ++++++++++++++++++++++++++++++++--------------------- src/generator.h | 2 +- src/symbol_table.c | 1 - src/tpc.y | 8 ++++--- 5 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 res/test_global.tpc diff --git a/res/test_global.tpc b/res/test_global.tpc new file mode 100644 index 0000000..789f290 --- /dev/null +++ b/res/test_global.tpc @@ -0,0 +1,12 @@ +/* test-table-symboles.tpc */ + +/* Test file for simplified translator of a declaration of variables in C */ +entier r1,b,s,c,r2 ; +caractere letter, digit, punct; + +entier main(void) { + r1=12; + r2=13; + b = r1 + r2; + print(b); +} diff --git a/src/generator.c b/src/generator.c index 8329aee..c038b7f 100644 --- a/src/generator.c +++ b/src/generator.c @@ -14,27 +14,30 @@ void gen_prologue() { fprintf(output, "format_int db \"%%d\",10,0\n\n"); fprintf(output, "section .bss\n"); } -void gen_prologue_continue(){ - fprintf(output, "globals: resq %d\n", nb_globals); - fprintf(output, "section .text\n\nglobal _start\n"); - fprintf(output, "print: ;print needs an argument in rax\n"); - fprintf(output, "push rbp\n"); - fprintf(output, "mov rbp, rsp\n"); - fprintf(output, "push rsi\n"); - fprintf(output, "mov rsi, rax\n"); - fprintf(output, "mov rdi, format_int\n"); - fprintf(output, "mov rax, 0\n"); - fprintf(output, "call printf WRT ..plt\n"); - fprintf(output, "pop rsi\n"); - fprintf(output, "pop rbp\n"); - fprintf(output, "ret\n"); - fprintf(output, "\n_start:\n"); - fprintf(output, "push rbp\nmov rbp, rsp\n\n"); +void gen_prologue_continue(int *bss_done){ + if (*bss_done == 0){ + fprintf(output, "globals: resq %d\n", nb_globals); + fprintf(output, "section .text\n\nglobal _start\n"); + fprintf(output, "print: ;print needs an argument in rax\n"); + fprintf(output, "push rbp\n"); + fprintf(output, "mov rbp, rsp\n"); + fprintf(output, "push rsi\n"); + fprintf(output, "mov rsi, rax\n"); + fprintf(output, "mov rdi, format_int\n"); + fprintf(output, "mov rax, 0\n"); + fprintf(output, "call printf WRT ..plt\n"); + fprintf(output, "pop rsi\n"); + fprintf(output, "pop rbp\n"); + fprintf(output, "ret\n"); + fprintf(output, "\n_start:\n"); + fprintf(output, "push rbp\nmov rbp, rsp\n\n"); + *bss_done = 1; + } } void gen_const_declaration() { fprintf(output, "mov rax,60 \n"); fprintf(output, "mov rdi,0 \n"); - fprintf(output, "syscall \n"); + fprintf(output, "syscall \n\n"); fprintf(output, ";global table\n"); glo_display_table(); fprintf(output, ";local table\n"); @@ -100,15 +103,20 @@ void gen_ifelse_end(int idx) { // ----- OPERATORS ----- int gen_assign(const char ident[], Scope scope) { - const char instr_fmt[] = "pop QWORD [rbp - %d] ;%s\n"; - + int g_addr = glo_get_addr(ident); switch (scope) { case GLOBAL: - fprintf(output, instr_fmt, glo_get_addr(ident), ident); + fprintf(output, "pop QWORD [globals + %d] ;%s\n", glo_get_addr(ident), ident); return glo_lookup(ident); case LOCAL: - fprintf(output, instr_fmt, loc_get_addr(ident), ident); - return loc_lookup(ident); + if (g_addr == -1) { + fprintf(output, "pop QWORD [rbp - %d] ;%s\n", loc_get_addr(ident), ident); + return loc_lookup(ident); + } + else { + fprintf(output, "pop QWORD [globals + %d] ;%s\n", g_addr, ident); + return glo_lookup(ident); + } default: exit(1); } @@ -241,13 +249,20 @@ int gen_negate_expr(int type) { } int gen_value(const char ident[], Scope scope) { + int g_addr = glo_get_addr(ident); switch (scope) { case GLOBAL: - fprintf(output, "push QWORD [rbp - %d] ;%s\n", glo_get_addr(ident), ident); + fprintf(output, "push QWORD [globals + %d] ;%s\n", glo_get_addr(ident), ident); return glo_lookup(ident); case LOCAL: - fprintf(output, "push QWORD [rbp - %d] ;%s\n", loc_get_addr(ident), ident); - return loc_lookup(ident); + if (g_addr == -1) { + fprintf(output, "push QWORD [rbp - %d] ;%s\n", loc_get_addr(ident), ident); + return loc_lookup(ident); + } + else { + fprintf(output, "push QWORD [globals + %d] ;%s\n", g_addr, ident); + return glo_lookup(ident); + } default: exit(1); } diff --git a/src/generator.h b/src/generator.h index 825ec8c..1a4c96a 100644 --- a/src/generator.h +++ b/src/generator.h @@ -17,7 +17,7 @@ extern int nb_globals; FILE *output; void gen_prologue(); -void gen_prologue_continue(); +void gen_prologue_continue(int *bss_done); void gen_const_declaration(); void gen_declaration(const char name[], int type, Scope scope); diff --git a/src/symbol_table.c b/src/symbol_table.c index 8e6db97..6dc3e64 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -47,7 +47,6 @@ int glo_lookup(const char name[]) { int glo_get_addr(const char name[]) { int count; - for (count = 0; count < glo_symbol_table.size; count++) { if (!strcmp(glo_symbol_table.entries[count].name, name)) { return glo_symbol_table.entries[count].addr; diff --git a/src/tpc.y b/src/tpc.y index b5ab84c..2938094 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -21,6 +21,7 @@ extern int lineno; int yylex(); void yyerror(char *); static Scope scope = GLOBAL; +static int bss_done = 0; static int num_label = 0; static int num_if = 0; %} @@ -91,10 +92,11 @@ DeclFonct: Corps { scope = GLOBAL; } ; EnTeteFonct: - TYPE IDENT Prologue '(' Parametres ')' -| VOID IDENT Prologue '(' Parametres ')' + TYPE IDENT PrologueCont '(' Parametres ')' +| VOID IDENT PrologueCont '(' Parametres ')' ; -Prologue: {gen_prologue_continue();}; + +PrologueCont: {gen_prologue_continue(&bss_done);}; Parametres: VOID -- cgit v1.2.3