aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
authorpacien2018-06-05 13:41:13 +0200
committerpacien2018-06-05 13:41:13 +0200
commit8f6dd273479bdc7789d40a235b0afb6598fd0435 (patch)
tree3f30d4b2869e3460a587ab9e7708afe4a8cacca7 /src/generator.c
parent7a966d25b34f4bd37f32a18f7e8a62b6f97186e6 (diff)
downloadtpc-compiler-8f6dd273479bdc7789d40a235b0afb6598fd0435.tar.gz
Handle func return
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c14
1 files changed, 12 insertions, 2 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);