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/generator.c | 6 +++--- src/generator.h | 4 ++-- src/tpc.y | 20 +++++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/generator.c b/src/generator.c index 37581e0..b8a0b8f 100644 --- a/src/generator.c +++ b/src/generator.c @@ -90,13 +90,13 @@ void gen_const_declaration() { fun_display_table(); } -Type gen_function_declaration(const char name[], int return_type, int nb_param) { - fun_add(name, return_type, nb_param); +Type gen_function_declaration(const char name[], int return_type) { fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); return return_type; } -void gen_function_end_declaration() { +void gen_function_end_declaration(const char name[], int return_type, int nb_param) { + fun_add(name, return_type, nb_param); fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); } diff --git a/src/generator.h b/src/generator.h index 082e359..41a769a 100644 --- a/src/generator.h +++ b/src/generator.h @@ -21,8 +21,8 @@ FILE *output; void gen_prologue(); void gen_prologue_continue(int *bss_done); void gen_const_declaration(); -Type gen_function_declaration(const char name[], int return_type, int nb_param); -void gen_function_end_declaration(); +Type gen_function_declaration(const char name[], int return_type); +void gen_function_end_declaration(const char name[], int return_type, int nb_param); void gen_function_return(Type expect, Type actual); Type gen_function_call(const char name[], int nb_param); void gen_declaration(const char name[], int type, Scope scope); 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