aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2018-06-06 15:25:08 +0200
committerpacien2018-06-06 15:25:08 +0200
commit771a1470cf4630670c8bb4ba8436bfc7cd2e38ca (patch)
tree218444b47c4a68c6b33230a8ca346f97559647db /src
parentc0feca77799b859a5331417595ac0190bb999e0d (diff)
downloadtpc-compiler-771a1470cf4630670c8bb4ba8436bfc7cd2e38ca.tar.gz
extract func param passing
Diffstat (limited to 'src')
-rw-r--r--src/generator.c7
-rw-r--r--src/generator.h4
-rw-r--r--src/tpc.y5
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) {
137 return return_type; 137 return return_type;
138} 138}
139 139
140void gen_function_param_passing(int nb_params) {
141 for(int i = 1; i <= nb_params; i++) {
142 fprintf(output, "mov r8, [rbp + %d]\n", (nb_params - i + 2) * 8);
143 fprintf(output, "mov [rbp - %d], r8\n", i * 8);
144 }
145}
146
140void gen_declaration(const char name[], Type type, Scope scope) { 147void gen_declaration(const char name[], Type type, Scope scope) {
141 switch (scope) { 148 switch (scope) {
142 case GLOBAL: 149 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();
20void gen_const(const char name[], int value, Scope scope); 20void gen_const(const char name[], int value, Scope scope);
21 21
22Type gen_function_declaration(const char name[], int return_type); 22Type gen_function_declaration(const char name[], int return_type);
23void gen_tab_declaration(const char name[], Scope scope, int size);
24void gen_function_end_declaration(const char name[], Type return_type, int nb_param); 23void gen_function_end_declaration(const char name[], Type return_type, int nb_param);
25void gen_function_return(Type expect, Type actual); 24void gen_function_return(Type expect, Type actual);
26Type gen_function_call(const char name[], int nb_param); 25Type gen_function_call(const char name[], int nb_param);
26void gen_function_param_passing(int nb_params);
27 27
28void gen_tab_declaration(const char name[], Scope scope, int size);
28void gen_declaration(const char name[], Type type, Scope scope); 29void gen_declaration(const char name[], Type type, Scope scope);
29void gen_check(const char name[], Scope scope); 30void gen_check(const char name[], Scope scope);
30 31
31void gen_reade(const char name[], Scope scope); 32void gen_reade(const char name[], Scope scope);
32void gen_readc(const char name[], Scope scope); 33void gen_readc(const char name[], Scope scope);
33
34void gen_print(Type type); 34void gen_print(Type type);
35 35
36void gen_if_label(int idx); 36void 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:
114| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } 114| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; }
115; 115;
116Corps: 116Corps:
117 '{' { int i; 117 '{' { gen_function_param_passing(nb_param[num_scope]); }
118 for(i=1;i<=nb_param[num_scope];i++){
119 fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param[num_scope]-i+2)*8, i*8);
120 } }
121 DeclConsts DeclVars SuiteInstr '}' 118 DeclConsts DeclVars SuiteInstr '}'
122; 119;
123SuiteInstr: 120SuiteInstr: