From a67cd40065f790383776ec3fffa364f6443f7ee7 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Tue, 5 Jun 2018 19:53:58 +0200 Subject: Parameters handling (without checking types on parameters) --- src/tpc.y | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/tpc.y') diff --git a/src/tpc.y b/src/tpc.y index 979e696..fedd193 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -23,6 +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 char fname[64]; %} %union { @@ -88,14 +90,14 @@ DeclFoncts: ; DeclFonct: EnTeteFonct { scope = LOCAL; } - Corps { gen_function_end_declaration(); scope = GLOBAL; return_type = VOID_T; } + Corps { gen_function_end_declaration(fname,return_type,nb_param); scope = GLOBAL; return_type = VOID_T; } ; EnTeteFonct: - TYPE IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($2, $1, $5); } -| VOID IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($2, VOID_T, $5); } + 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; } ; -PrologueCont: {gen_prologue_continue(&bss_done);}; +PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; Parametres: VOID {$$ = 0;} @@ -106,7 +108,7 @@ ListTypVar: | TYPE IDENT { gen_declaration($2, $1, scope); $$ = 1; } ; Corps: - '{' DeclConsts DeclVars SuiteInstr '}' + '{' {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 '}' ; SuiteInstr: SuiteInstr Instr @@ -115,8 +117,8 @@ SuiteInstr: Instr: Exp ';' | ';' -| RETURN Exp ';' { gen_function_return(return_type, $2); scope = GLOBAL; return_type = VOID_T; } -| RETURN ';' { gen_function_return(return_type, VOID_T); scope = GLOBAL; return_type = VOID_T; } +| RETURN Exp ';' { gen_function_return(return_type, $2); } +| RETURN ';' { gen_function_return(return_type, VOID_T);} | READE '(' IDENT ')' ';' { gen_reade($3); } | READC '(' IDENT ')' ';' { gen_readc($3); } | PRINT '(' Exp ')' ';' { gen_print($3);} @@ -166,8 +168,8 @@ F: | IDENT '(' Arguments ')' { $$ = gen_function_call($1,$3); } ; LValue: - IDENT { $$ = $1; gen_check($1, scope); } -| IDENT '[' Exp ']' { $$ = $1; gen_check($1, scope); } + IDENT { gen_check($1, scope); } +| IDENT '[' Exp ']' { gen_check($1, scope); } ; Arguments: ListExp -- cgit v1.2.3