aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/generator.c b/src/generator.c
index c038b7f..d44bbd1 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -18,7 +18,7 @@ void gen_prologue_continue(int *bss_done){
18 if (*bss_done == 0){ 18 if (*bss_done == 0){
19 fprintf(output, "globals: resq %d\n", nb_globals); 19 fprintf(output, "globals: resq %d\n", nb_globals);
20 fprintf(output, "section .text\n\nglobal _start\n"); 20 fprintf(output, "section .text\n\nglobal _start\n");
21 fprintf(output, "print: ;print needs an argument in rax\n"); 21 fprintf(output, "\nprint: ;print needs an argument in rax\n");
22 fprintf(output, "push rbp\n"); 22 fprintf(output, "push rbp\n");
23 fprintf(output, "mov rbp, rsp\n"); 23 fprintf(output, "mov rbp, rsp\n");
24 fprintf(output, "push rsi\n"); 24 fprintf(output, "push rsi\n");
@@ -29,19 +29,35 @@ void gen_prologue_continue(int *bss_done){
29 fprintf(output, "pop rsi\n"); 29 fprintf(output, "pop rsi\n");
30 fprintf(output, "pop rbp\n"); 30 fprintf(output, "pop rbp\n");
31 fprintf(output, "ret\n"); 31 fprintf(output, "ret\n");
32 fprintf(output, "\n_start:\n");
33 fprintf(output, "push rbp\nmov rbp, rsp\n\n");
34 *bss_done = 1; 32 *bss_done = 1;
35 } 33 }
36} 34}
37void gen_const_declaration() { 35void gen_const_declaration() {
38 fprintf(output, "mov rax,60 \n"); 36 fprintf(output, "\n_start:\n");
37 fprintf(output, "push rbp\nmov rbp, rsp\n\n");
38 fprintf(output, "call main\n");
39 fprintf(output, "\nmov rax,60 \n");
39 fprintf(output, "mov rdi,0 \n"); 40 fprintf(output, "mov rdi,0 \n");
40 fprintf(output, "syscall \n\n"); 41 fprintf(output, "syscall \n\n");
41 fprintf(output, ";global table\n"); 42 fprintf(output, ";global table\n");
42 glo_display_table(); 43 glo_display_table();
43 fprintf(output, ";local table\n"); 44 fprintf(output, ";local table\n");
44 loc_display_table(); 45 loc_display_table();
46 fprintf(output, ";function table\n");
47 fun_display_table();
48}
49
50void gen_function_declaration(const char name[], int return_type, int nb_param){
51 fun_add(name,return_type,nb_param);
52 fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n",name);
53}
54
55void gen_function_end_declaration(){
56 fprintf(output, "mov rsp, rbp\npop rbp\nret\n");
57}
58int gen_function_call(const char name[], int nb_param){
59 fprintf(output,"call %s\n",name);
60 return fun_lookup(name,nb_param);
45} 61}
46 62
47void gen_declaration(const char name[], int type, Scope scope) { 63void gen_declaration(const char name[], int type, Scope scope) {