From 56e31e3b8a1a569f42140547ffda5a91ef451b47 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Tue, 5 Jun 2018 21:05:22 +0200 Subject: Fix call of call of functions with parameters --- src/symbol_table.c | 4 ---- src/symbol_table.h | 1 + src/tpc.y | 11 ++++++----- 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/symbol_table.c b/src/symbol_table.c index 64399bd..483de4c 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -190,10 +190,6 @@ void loc_display_table() { } void loc_clean_table() { - int i; - for (i = 0; i < loc_symbol_table.size; i++) { - fprintf(output, "pop eax\n"); - } loc_symbol_table.size = 0; } diff --git a/src/symbol_table.h b/src/symbol_table.h index a5dba69..d0ac440 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -55,6 +55,7 @@ void loc_addVar(const char name[], int type); int loc_lookup(const char name[]); int loc_get_addr(const char name[]); void loc_display_table(); +void loc_clean_table(); void check_expected_type(int type_to_check, int type_expected); #endif diff --git a/src/tpc.y b/src/tpc.y index fedd193..9878ce7 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -23,7 +23,8 @@ static Type return_type = VOID_T; static int bss_done = 0; static int num_label = 0; static int num_if = 0; -static int nb_param = 0; +static int nb_param[255]; +static int num_scope = -1; static char fname[64]; %} @@ -90,11 +91,11 @@ DeclFoncts: ; DeclFonct: EnTeteFonct { scope = LOCAL; } - Corps { gen_function_end_declaration(fname,return_type,nb_param); scope = GLOBAL; return_type = VOID_T; } + Corps { gen_function_end_declaration(fname,return_type,nb_param[num_scope]); scope = GLOBAL; return_type = VOID_T; num_scope--; loc_clean_table(); } ; EnTeteFonct: - TYPE IDENT PrologueCont {strcpy(fname,$2); return_type = gen_function_declaration($2, $1);} '(' Parametres ')' { nb_param = $6 ; scope = GLOBAL;} -| VOID IDENT PrologueCont {strcpy(fname,$2); return_type = gen_function_declaration($2, VOID_T);} '(' Parametres ')' { nb_param = $6 ; scope = GLOBAL; } + TYPE IDENT PrologueCont {strcpy(fname,$2); return_type = gen_function_declaration($2, $1);} '(' Parametres ')' { nb_param[++num_scope] = $6 ; scope = GLOBAL;} +| VOID IDENT PrologueCont {strcpy(fname,$2); return_type = gen_function_declaration($2, VOID_T);} '(' Parametres ')' { nb_param[++num_scope] = $6 ; scope = GLOBAL; } ; PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; @@ -108,7 +109,7 @@ ListTypVar: | TYPE IDENT { gen_declaration($2, $1, scope); $$ = 1; } ; Corps: - '{' {int i; for(i=1;i<=nb_param;i++){fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param-i+2)*8, i*8);} } DeclConsts DeclVars SuiteInstr '}' + '{' {int i; for(i=1;i<=nb_param[num_scope];i++){fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param[num_scope]-i+2)*8, i*8);} } DeclConsts DeclVars SuiteInstr '}' ; SuiteInstr: SuiteInstr Instr -- cgit v1.2.3