From 67fc7933df394848eeb86a45f37e35ded032e5e9 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Mon, 14 May 2018 15:48:01 +0200 Subject: Not working properly: Beginning of the implementation of globals -> need to modify if(global) else --- src/generator.c | 10 +++++++--- src/generator.h | 3 +++ src/tpc.y | 7 +++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/generator.c b/src/generator.c index e547a23..88f32d7 100644 --- a/src/generator.c +++ b/src/generator.c @@ -13,7 +13,11 @@ void gen_prologue() { printf("extern printf\n"); printf("section .data\n"); printf("format_int db \"%%d\",10,0\n\n"); - printf("section .bss\nsection .text\n\nglobal _start\n"); + printf("section .bss\n"); +} +void gen_prologue_continue(){ + printf("globals: resq %d\n", nb_globals); + printf("section .text\n\nglobal _start\n"); printf("print: ;print needs an argument in rax\n"); printf("push rbp\n"); printf("mov rbp, rsp\n"); @@ -28,7 +32,6 @@ void gen_prologue() { printf("\n_start:\n"); printf("push rbp\nmov rbp, rsp\n\n"); } - void gen_const_declaration() { printf("mov rax,60 \n"); printf("mov rdi,0 \n"); @@ -43,13 +46,14 @@ void gen_declaration(const char name[], int type, Scope scope) { switch (scope) { case GLOBAL: glo_addVar(name, type); + nb_globals++; break; case LOCAL: loc_addVar(name, type); + printf("push 0\n"); break; } - printf("push 0\n"); } // ----- READ AND PRINT FUNCTIONS ----- diff --git a/src/generator.h b/src/generator.h index c907f66..daa7339 100644 --- a/src/generator.h +++ b/src/generator.h @@ -11,7 +11,10 @@ typedef enum scope { LOCAL } Scope; +extern int nb_globals; + void gen_prologue(); +void gen_prologue_continue(); void gen_const_declaration(); void gen_declaration(const char name[], int type, Scope scope); diff --git a/src/tpc.y b/src/tpc.y index 17b8718..6da63ef 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -12,6 +12,7 @@ */ #include +int nb_globals = 0; #include "symbol_table.h" #include "generator.h" @@ -89,9 +90,11 @@ DeclFonct: Corps { scope = GLOBAL; } ; EnTeteFonct: - TYPE IDENT '(' Parametres ')' -| VOID IDENT '(' Parametres ')' + TYPE IDENT Prologue '(' Parametres ')' +| VOID IDENT Prologue '(' Parametres ')' ; +Prologue: {gen_prologue_continue();}; + Parametres: VOID | ListTypVar -- cgit v1.2.3