diff options
author | pacien | 2018-06-05 13:41:13 +0200 |
---|---|---|
committer | pacien | 2018-06-05 13:41:13 +0200 |
commit | 8f6dd273479bdc7789d40a235b0afb6598fd0435 (patch) | |
tree | 3f30d4b2869e3460a587ab9e7708afe4a8cacca7 /src | |
parent | 7a966d25b34f4bd37f32a18f7e8a62b6f97186e6 (diff) | |
download | tpc-compiler-8f6dd273479bdc7789d40a235b0afb6598fd0435.tar.gz |
Handle func return
Diffstat (limited to 'src')
-rw-r--r-- | src/generator.c | 14 | ||||
-rw-r--r-- | src/generator.h | 3 | ||||
-rw-r--r-- | src/tpc.y | 11 |
3 files changed, 20 insertions, 8 deletions
diff --git a/src/generator.c b/src/generator.c index 7a960bf..3f9cc3b 100644 --- a/src/generator.c +++ b/src/generator.c | |||
@@ -91,16 +91,26 @@ void gen_const_declaration() { | |||
91 | fun_display_table(); | 91 | fun_display_table(); |
92 | } | 92 | } |
93 | 93 | ||
94 | void gen_function_declaration(const char name[], int return_type, | 94 | Type gen_function_declaration(const char name[], int return_type, int nb_param) { |
95 | int nb_param) { | ||
96 | fun_add(name, return_type, nb_param); | 95 | fun_add(name, return_type, nb_param); |
97 | fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); | 96 | fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); |
97 | return return_type; | ||
98 | } | 98 | } |
99 | 99 | ||
100 | void gen_function_end_declaration() { | 100 | void gen_function_end_declaration() { |
101 | fprintf(output, "mov rsp, rbp\npop rbp\nret\n"); | 101 | fprintf(output, "mov rsp, rbp\npop rbp\nret\n"); |
102 | } | 102 | } |
103 | 103 | ||
104 | void gen_function_return(Type expect, Type actual) { | ||
105 | if (actual != expect) { | ||
106 | fprintf(stderr, "Return type mismatch at line %d.", lineno); | ||
107 | exit(1); | ||
108 | } | ||
109 | |||
110 | if (actual != VOID) fprintf(output, "pop rax\n"); | ||
111 | gen_function_end_declaration(); | ||
112 | } | ||
113 | |||
104 | int gen_function_call(const char name[], int nb_param) { | 114 | int gen_function_call(const char name[], int nb_param) { |
105 | Type return_type = fun_lookup(name, nb_param); | 115 | Type return_type = fun_lookup(name, nb_param); |
106 | fprintf(output, "call %s\n", name); | 116 | fprintf(output, "call %s\n", name); |
diff --git a/src/generator.h b/src/generator.h index 939ab13..205fc01 100644 --- a/src/generator.h +++ b/src/generator.h | |||
@@ -19,8 +19,9 @@ FILE *output; | |||
19 | void gen_prologue(); | 19 | void gen_prologue(); |
20 | void gen_prologue_continue(int *bss_done); | 20 | void gen_prologue_continue(int *bss_done); |
21 | void gen_const_declaration(); | 21 | void gen_const_declaration(); |
22 | void gen_function_declaration(const char name[], int return_type, int nb_param); | 22 | Type gen_function_declaration(const char name[], int return_type, int nb_param); |
23 | void gen_function_end_declaration(); | 23 | void gen_function_end_declaration(); |
24 | void gen_function_return(Type expect, Type actual); | ||
24 | int gen_function_call(const char name[], int nb_param); | 25 | int gen_function_call(const char name[], int nb_param); |
25 | void gen_declaration(const char name[], int type, Scope scope); | 26 | void gen_declaration(const char name[], int type, Scope scope); |
26 | void gen_check(const char name[], Scope scope); | 27 | void gen_check(const char name[], Scope scope); |
@@ -20,6 +20,7 @@ extern int lineno; | |||
20 | int yylex(); | 20 | int yylex(); |
21 | void yyerror(char *); | 21 | void yyerror(char *); |
22 | static Scope scope = GLOBAL; | 22 | static Scope scope = GLOBAL; |
23 | static Type return_type = VOID; | ||
23 | static int bss_done = 0; | 24 | static int bss_done = 0; |
24 | static int num_label = 0; | 25 | static int num_label = 0; |
25 | static int num_if = 0; | 26 | static int num_if = 0; |
@@ -88,11 +89,11 @@ DeclFoncts: | |||
88 | ; | 89 | ; |
89 | DeclFonct: | 90 | DeclFonct: |
90 | EnTeteFonct { scope = LOCAL; } | 91 | EnTeteFonct { scope = LOCAL; } |
91 | Corps { gen_function_end_declaration(); scope = GLOBAL; } | 92 | Corps { gen_function_end_declaration(); scope = GLOBAL; return_type = VOID; } |
92 | ; | 93 | ; |
93 | EnTeteFonct: | 94 | EnTeteFonct: |
94 | TYPE IDENT PrologueCont '(' Parametres ')' {gen_function_declaration($<ident>2, $<type>1, $5);} | 95 | TYPE IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($<ident>2, $<type>1, $5); } |
95 | | VOID IDENT PrologueCont '(' Parametres ')' {gen_function_declaration($<ident>2, 2, $5);} | 96 | | VOID IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($<ident>2, VOID, $5); } |
96 | ; | 97 | ; |
97 | 98 | ||
98 | PrologueCont: {gen_prologue_continue(&bss_done);}; | 99 | PrologueCont: {gen_prologue_continue(&bss_done);}; |
@@ -115,8 +116,8 @@ SuiteInstr: | |||
115 | Instr: | 116 | Instr: |
116 | Exp ';' | 117 | Exp ';' |
117 | | ';' | 118 | | ';' |
118 | | RETURN Exp ';' | 119 | | RETURN Exp ';' { gen_function_return(return_type, $<type>2); scope = GLOBAL; return_type = VOID; } |
119 | | RETURN ';' | 120 | | RETURN ';' { gen_function_return(return_type, VOID); scope = GLOBAL; return_type = VOID; } |
120 | | READE '(' IDENT ')' ';' { gen_reade($<ident>3); } | 121 | | READE '(' IDENT ')' ';' { gen_reade($<ident>3); } |
121 | | READC '(' IDENT ')' ';' { gen_readc($<ident>3); } | 122 | | READC '(' IDENT ')' ';' { gen_readc($<ident>3); } |
122 | | PRINT '(' Exp ')' ';' { gen_print($<type>3);} | 123 | | PRINT '(' Exp ')' ';' { gen_print($<type>3);} |