diff options
author | Adam NAILI | 2018-06-05 19:53:58 +0200 |
---|---|---|
committer | Adam NAILI | 2018-06-05 19:53:58 +0200 |
commit | a67cd40065f790383776ec3fffa364f6443f7ee7 (patch) | |
tree | 420f183d70f4278cebe2e670964c064598337bea | |
parent | 7138ea2f185c6963cfd09a13cfc289d4d1452858 (diff) | |
download | tpc-compiler-a67cd40065f790383776ec3fffa364f6443f7ee7.tar.gz |
Parameters handling (without checking types on parameters)
-rw-r--r-- | res/test_parameters.tpc | 17 | ||||
-rw-r--r-- | src/generator.c | 6 | ||||
-rw-r--r-- | src/generator.h | 4 | ||||
-rw-r--r-- | src/tpc.y | 20 |
4 files changed, 33 insertions, 14 deletions
diff --git a/res/test_parameters.tpc b/res/test_parameters.tpc new file mode 100644 index 0000000..d7b2061 --- /dev/null +++ b/res/test_parameters.tpc | |||
@@ -0,0 +1,17 @@ | |||
1 | /* test-table-symboles.tpc */ | ||
2 | |||
3 | /* Test file for simplified translator of a declaration of variables in C */ | ||
4 | |||
5 | entier test(entier a,entier b,entier c){ | ||
6 | entier x; | ||
7 | entier y; | ||
8 | y = 1; | ||
9 | x = 21; | ||
10 | print(x+y); | ||
11 | return a*b+c; | ||
12 | } | ||
13 | |||
14 | entier main(void) { | ||
15 | print(test(6,2,3)); | ||
16 | return 0; | ||
17 | } | ||
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() { | |||
90 | fun_display_table(); | 90 | fun_display_table(); |
91 | } | 91 | } |
92 | 92 | ||
93 | Type gen_function_declaration(const char name[], int return_type, int nb_param) { | 93 | Type gen_function_declaration(const char name[], int return_type) { |
94 | fun_add(name, return_type, nb_param); | ||
95 | fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); | 94 | fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); |
96 | return return_type; | 95 | return return_type; |
97 | } | 96 | } |
98 | 97 | ||
99 | void gen_function_end_declaration() { | 98 | void gen_function_end_declaration(const char name[], int return_type, int nb_param) { |
99 | fun_add(name, return_type, nb_param); | ||
100 | fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); | 100 | fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); |
101 | } | 101 | } |
102 | 102 | ||
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; | |||
21 | void gen_prologue(); | 21 | void gen_prologue(); |
22 | void gen_prologue_continue(int *bss_done); | 22 | void gen_prologue_continue(int *bss_done); |
23 | void gen_const_declaration(); | 23 | void gen_const_declaration(); |
24 | Type gen_function_declaration(const char name[], int return_type, int nb_param); | 24 | Type gen_function_declaration(const char name[], int return_type); |
25 | void gen_function_end_declaration(); | 25 | void gen_function_end_declaration(const char name[], int return_type, int nb_param); |
26 | void gen_function_return(Type expect, Type actual); | 26 | void gen_function_return(Type expect, Type actual); |
27 | Type gen_function_call(const char name[], int nb_param); | 27 | Type gen_function_call(const char name[], int nb_param); |
28 | void gen_declaration(const char name[], int type, Scope scope); | 28 | void gen_declaration(const char name[], int type, Scope scope); |
@@ -23,6 +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; | ||
27 | static char fname[64]; | ||
26 | %} | 28 | %} |
27 | 29 | ||
28 | %union { | 30 | %union { |
@@ -88,14 +90,14 @@ DeclFoncts: | |||
88 | ; | 90 | ; |
89 | DeclFonct: | 91 | DeclFonct: |
90 | EnTeteFonct { scope = LOCAL; } | 92 | EnTeteFonct { scope = LOCAL; } |
91 | Corps { gen_function_end_declaration(); scope = GLOBAL; return_type = VOID_T; } | 93 | Corps { gen_function_end_declaration(fname,return_type,nb_param); scope = GLOBAL; return_type = VOID_T; } |
92 | ; | 94 | ; |
93 | EnTeteFonct: | 95 | EnTeteFonct: |
94 | TYPE IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($<ident>2, $<type>1, $5); } | 96 | TYPE IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, $<type>1);} '(' Parametres ')' { nb_param = $<num>6 ; scope = GLOBAL;} |
95 | | VOID IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($<ident>2, VOID_T, $5); } | 97 | | VOID IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, VOID_T);} '(' Parametres ')' { nb_param = $<num>6 ; scope = GLOBAL; } |
96 | ; | 98 | ; |
97 | 99 | ||
98 | PrologueCont: {gen_prologue_continue(&bss_done);}; | 100 | PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; |
99 | 101 | ||
100 | Parametres: | 102 | Parametres: |
101 | VOID {$$ = 0;} | 103 | VOID {$$ = 0;} |
@@ -106,7 +108,7 @@ ListTypVar: | |||
106 | | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } | 108 | | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } |
107 | ; | 109 | ; |
108 | Corps: | 110 | Corps: |
109 | '{' DeclConsts DeclVars SuiteInstr '}' | 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 '}' |
110 | ; | 112 | ; |
111 | SuiteInstr: | 113 | SuiteInstr: |
112 | SuiteInstr Instr | 114 | SuiteInstr Instr |
@@ -115,8 +117,8 @@ SuiteInstr: | |||
115 | Instr: | 117 | Instr: |
116 | Exp ';' | 118 | Exp ';' |
117 | | ';' | 119 | | ';' |
118 | | RETURN Exp ';' { gen_function_return(return_type, $<type>2); scope = GLOBAL; return_type = VOID_T; } | 120 | | RETURN Exp ';' { gen_function_return(return_type, $<type>2); } |
119 | | RETURN ';' { gen_function_return(return_type, VOID_T); scope = GLOBAL; return_type = VOID_T; } | 121 | | RETURN ';' { gen_function_return(return_type, VOID_T);} |
120 | | READE '(' IDENT ')' ';' { gen_reade($<ident>3); } | 122 | | READE '(' IDENT ')' ';' { gen_reade($<ident>3); } |
121 | | READC '(' IDENT ')' ';' { gen_readc($<ident>3); } | 123 | | READC '(' IDENT ')' ';' { gen_readc($<ident>3); } |
122 | | PRINT '(' Exp ')' ';' { gen_print($<type>3);} | 124 | | PRINT '(' Exp ')' ';' { gen_print($<type>3);} |
@@ -166,8 +168,8 @@ F: | |||
166 | | IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } | 168 | | IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } |
167 | ; | 169 | ; |
168 | LValue: | 170 | LValue: |
169 | IDENT { $$ = $1; gen_check($<ident>1, scope); } | 171 | IDENT { gen_check($<ident>1, scope); } |
170 | | IDENT '[' Exp ']' { $$ = $1; gen_check($<ident>1, scope); } | 172 | | IDENT '[' Exp ']' { gen_check($<ident>1, scope); } |
171 | ; | 173 | ; |
172 | Arguments: | 174 | Arguments: |
173 | ListExp | 175 | ListExp |