diff options
author | Adam NAILI | 2018-06-05 21:05:22 +0200 |
---|---|---|
committer | Adam NAILI | 2018-06-05 21:05:22 +0200 |
commit | 56e31e3b8a1a569f42140547ffda5a91ef451b47 (patch) | |
tree | ab7412ef90736f1ccc386670b024b48b593f34b0 | |
parent | a67cd40065f790383776ec3fffa364f6443f7ee7 (diff) | |
download | tpc-compiler-56e31e3b8a1a569f42140547ffda5a91ef451b47.tar.gz |
Fix call of call of functions with parameters
-rw-r--r-- | res/test_parameters.tpc | 17 | ||||
-rw-r--r-- | src/symbol_table.c | 4 | ||||
-rw-r--r-- | src/symbol_table.h | 1 | ||||
-rw-r--r-- | src/tpc.y | 11 |
4 files changed, 16 insertions, 17 deletions
diff --git a/res/test_parameters.tpc b/res/test_parameters.tpc index d7b2061..479fb60 100644 --- a/res/test_parameters.tpc +++ b/res/test_parameters.tpc | |||
@@ -2,16 +2,17 @@ | |||
2 | 2 | ||
3 | /* Test file for simplified translator of a declaration of variables in C */ | 3 | /* Test file for simplified translator of a declaration of variables in C */ |
4 | 4 | ||
5 | entier test(entier a,entier b,entier c){ | 5 | void test2(caractere x,caractere y,caractere z){ |
6 | entier x; | 6 | print(x); |
7 | entier y; | 7 | print(y); |
8 | y = 1; | 8 | print(z); |
9 | x = 21; | 9 | } |
10 | print(x+y); | 10 | |
11 | return a*b+c; | 11 | void test(void){ |
12 | test2('a','b','c'); | ||
12 | } | 13 | } |
13 | 14 | ||
14 | entier main(void) { | 15 | entier main(void) { |
15 | print(test(6,2,3)); | 16 | test(); |
16 | return 0; | 17 | return 0; |
17 | } | 18 | } |
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() { | |||
190 | } | 190 | } |
191 | 191 | ||
192 | void loc_clean_table() { | 192 | void loc_clean_table() { |
193 | int i; | ||
194 | for (i = 0; i < loc_symbol_table.size; i++) { | ||
195 | fprintf(output, "pop eax\n"); | ||
196 | } | ||
197 | loc_symbol_table.size = 0; | 193 | loc_symbol_table.size = 0; |
198 | } | 194 | } |
199 | 195 | ||
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); | |||
55 | int loc_lookup(const char name[]); | 55 | int loc_lookup(const char name[]); |
56 | int loc_get_addr(const char name[]); | 56 | int loc_get_addr(const char name[]); |
57 | void loc_display_table(); | 57 | void loc_display_table(); |
58 | void loc_clean_table(); | ||
58 | void check_expected_type(int type_to_check, int type_expected); | 59 | void check_expected_type(int type_to_check, int type_expected); |
59 | 60 | ||
60 | #endif | 61 | #endif |
@@ -23,7 +23,8 @@ static Type return_type = VOID_T; | |||
23 | static int bss_done = 0; | 23 | static int bss_done = 0; |
24 | static int num_label = 0; | 24 | static int num_label = 0; |
25 | static int num_if = 0; | 25 | static int num_if = 0; |
26 | static int nb_param = 0; | 26 | static int nb_param[255]; |
27 | static int num_scope = -1; | ||
27 | static char fname[64]; | 28 | static char fname[64]; |
28 | %} | 29 | %} |
29 | 30 | ||
@@ -90,11 +91,11 @@ DeclFoncts: | |||
90 | ; | 91 | ; |
91 | DeclFonct: | 92 | DeclFonct: |
92 | EnTeteFonct { scope = LOCAL; } | 93 | EnTeteFonct { scope = LOCAL; } |
93 | Corps { gen_function_end_declaration(fname,return_type,nb_param); scope = GLOBAL; return_type = VOID_T; } | 94 | Corps { gen_function_end_declaration(fname,return_type,nb_param[num_scope]); scope = GLOBAL; return_type = VOID_T; num_scope--; loc_clean_table(); } |
94 | ; | 95 | ; |
95 | EnTeteFonct: | 96 | EnTeteFonct: |
96 | TYPE IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, $<type>1);} '(' Parametres ')' { nb_param = $<num>6 ; scope = GLOBAL;} | 97 | TYPE IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, $<type>1);} '(' Parametres ')' { nb_param[++num_scope] = $<num>6 ; scope = GLOBAL;} |
97 | | VOID IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, VOID_T);} '(' Parametres ')' { nb_param = $<num>6 ; scope = GLOBAL; } | 98 | | VOID IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, VOID_T);} '(' Parametres ')' { nb_param[++num_scope] = $<num>6 ; scope = GLOBAL; } |
98 | ; | 99 | ; |
99 | 100 | ||
100 | PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; | 101 | PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; |
@@ -108,7 +109,7 @@ ListTypVar: | |||
108 | | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } | 109 | | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } |
109 | ; | 110 | ; |
110 | Corps: | 111 | Corps: |
111 | '{' {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 '}' | 112 | '{' {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 '}' |
112 | ; | 113 | ; |
113 | SuiteInstr: | 114 | SuiteInstr: |
114 | SuiteInstr Instr | 115 | SuiteInstr Instr |