From 771a1470cf4630670c8bb4ba8436bfc7cd2e38ca Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 15:25:08 +0200 Subject: extract func param passing --- src/generator.c | 7 +++++++ src/generator.h | 4 ++-- src/tpc.y | 5 +---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/generator.c b/src/generator.c index 9948966..e7a33c8 100644 --- a/src/generator.c +++ b/src/generator.c @@ -137,6 +137,13 @@ Type gen_function_call(const char name[], int nb_param) { return return_type; } +void gen_function_param_passing(int nb_params) { + for(int i = 1; i <= nb_params; i++) { + fprintf(output, "mov r8, [rbp + %d]\n", (nb_params - i + 2) * 8); + fprintf(output, "mov [rbp - %d], r8\n", i * 8); + } +} + void gen_declaration(const char name[], Type type, Scope scope) { switch (scope) { case GLOBAL: diff --git a/src/generator.h b/src/generator.h index 94b03c8..bf48644 100644 --- a/src/generator.h +++ b/src/generator.h @@ -20,17 +20,17 @@ void gen_epilogue(); void gen_const(const char name[], int value, Scope scope); Type gen_function_declaration(const char name[], int return_type); -void gen_tab_declaration(const char name[], Scope scope, int size); void gen_function_end_declaration(const char name[], Type 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_function_param_passing(int nb_params); +void gen_tab_declaration(const char name[], Scope scope, int size); void gen_declaration(const char name[], Type type, Scope scope); void gen_check(const char name[], Scope scope); void gen_reade(const char name[], Scope scope); void gen_readc(const char name[], Scope scope); - void gen_print(Type type); void gen_if_label(int idx); diff --git a/src/tpc.y b/src/tpc.y index 1e6b6e2..266232f 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -114,10 +114,7 @@ ListTypVar: | TYPE IDENT { gen_declaration($2, $1, scope); $$ = 1; } ; Corps: - '{' { 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); - } } + '{' { gen_function_param_passing(nb_param[num_scope]); } DeclConsts DeclVars SuiteInstr '}' ; SuiteInstr: -- cgit v1.2.3