aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2018-06-05 13:41:13 +0200
committerpacien2018-06-05 13:41:13 +0200
commit8f6dd273479bdc7789d40a235b0afb6598fd0435 (patch)
tree3f30d4b2869e3460a587ab9e7708afe4a8cacca7 /src
parent7a966d25b34f4bd37f32a18f7e8a62b6f97186e6 (diff)
downloadtpc-compiler-8f6dd273479bdc7789d40a235b0afb6598fd0435.tar.gz
Handle func return
Diffstat (limited to 'src')
-rw-r--r--src/generator.c14
-rw-r--r--src/generator.h3
-rw-r--r--src/tpc.y11
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
94void gen_function_declaration(const char name[], int return_type, 94Type 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
100void gen_function_end_declaration() { 100void 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
104void 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
104int gen_function_call(const char name[], int nb_param) { 114int 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;
19void gen_prologue(); 19void gen_prologue();
20void gen_prologue_continue(int *bss_done); 20void gen_prologue_continue(int *bss_done);
21void gen_const_declaration(); 21void gen_const_declaration();
22void gen_function_declaration(const char name[], int return_type, int nb_param); 22Type gen_function_declaration(const char name[], int return_type, int nb_param);
23void gen_function_end_declaration(); 23void gen_function_end_declaration();
24void gen_function_return(Type expect, Type actual);
24int gen_function_call(const char name[], int nb_param); 25int gen_function_call(const char name[], int nb_param);
25void gen_declaration(const char name[], int type, Scope scope); 26void gen_declaration(const char name[], int type, Scope scope);
26void gen_check(const char name[], Scope scope); 27void gen_check(const char name[], Scope scope);
diff --git a/src/tpc.y b/src/tpc.y
index e8f2e41..3bd3df4 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -20,6 +20,7 @@ extern int lineno;
20int yylex(); 20int yylex();
21void yyerror(char *); 21void yyerror(char *);
22static Scope scope = GLOBAL; 22static Scope scope = GLOBAL;
23static Type return_type = VOID;
23static int bss_done = 0; 24static int bss_done = 0;
24static int num_label = 0; 25static int num_label = 0;
25static int num_if = 0; 26static int num_if = 0;
@@ -88,11 +89,11 @@ DeclFoncts:
88; 89;
89DeclFonct: 90DeclFonct:
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;
93EnTeteFonct: 94EnTeteFonct:
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
98PrologueCont: {gen_prologue_continue(&bss_done);}; 99PrologueCont: {gen_prologue_continue(&bss_done);};
@@ -115,8 +116,8 @@ SuiteInstr:
115Instr: 116Instr:
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);}