diff options
Diffstat (limited to 'src/generator.c')
-rw-r--r-- | src/generator.c | 135 |
1 files changed, 76 insertions, 59 deletions
diff --git a/src/generator.c b/src/generator.c index b8a0b8f..33fade9 100644 --- a/src/generator.c +++ b/src/generator.c | |||
@@ -10,71 +10,74 @@ | |||
10 | void gen_prologue() { | 10 | void gen_prologue() { |
11 | fprintf(output, "extern printf\nextern scanf\n"); | 11 | fprintf(output, "extern printf\nextern scanf\n"); |
12 | fprintf(output, "section .data\n"); | 12 | fprintf(output, "section .data\n"); |
13 | } | ||
14 | |||
15 | void gen_prologue_continue(int *bss_done) { | ||
16 | if (*bss_done != 0) return; | ||
17 | |||
13 | fprintf(output, "format_int db \"%%d\",10,0\n"); | 18 | fprintf(output, "format_int db \"%%d\",10,0\n"); |
14 | fprintf(output, "format_char db \"%%c\",10,0\n"); | 19 | fprintf(output, "format_char db \"%%c\",10,0\n"); |
15 | fprintf(output, "format_int_in: db \"%%d\", 0\n"); | 20 | fprintf(output, "format_int_in: db \"%%d\", 0\n"); |
16 | fprintf(output, "format_char_in: db \"%%c\", 0\n\n"); | 21 | fprintf(output, "format_char_in: db \"%%c\", 0\n\n"); |
17 | fprintf(output, "section .bss\n"); | 22 | fprintf(output, "section .bss\n"); |
23 | |||
24 | fprintf(output, "globals: resq %d\n", nb_globals); | ||
25 | fprintf(output, "section .text\n\nglobal _start\n"); | ||
26 | fprintf(output, "\nprinte: ;print needs an argument in rax\n"); | ||
27 | fprintf(output, "push rbp\n"); | ||
28 | fprintf(output, "mov rbp, rsp\n"); | ||
29 | fprintf(output, "push rsi\n"); | ||
30 | fprintf(output, "push rdi\n"); | ||
31 | fprintf(output, "mov rsi, rax\n"); | ||
32 | fprintf(output, "mov rdi, format_int\n"); | ||
33 | fprintf(output, "mov rax, 0\n"); | ||
34 | fprintf(output, "call printf WRT ..plt\n"); | ||
35 | fprintf(output, "pop rdi\n"); | ||
36 | fprintf(output, "pop rsi\n"); | ||
37 | fprintf(output, "pop rbp\n"); | ||
38 | fprintf(output, "ret\n"); | ||
39 | fprintf(output, "\nprintc: ;print needs an argument in rax\n"); | ||
40 | fprintf(output, "push rbp\n"); | ||
41 | fprintf(output, "mov rbp, rsp\n"); | ||
42 | fprintf(output, "push rsi\n"); | ||
43 | fprintf(output, "push rdi\n"); | ||
44 | fprintf(output, "mov rsi, rax\n"); | ||
45 | fprintf(output, "mov rdi, format_char\n"); | ||
46 | fprintf(output, "mov rax, 0\n"); | ||
47 | fprintf(output, "call printf WRT ..plt\n"); | ||
48 | fprintf(output, "pop rdi\n"); | ||
49 | fprintf(output, "pop rsi\n"); | ||
50 | fprintf(output, "pop rbp\n"); | ||
51 | fprintf(output, "ret\n"); | ||
52 | fprintf(output, "\nreade: ;read needs an adress in rax\n"); | ||
53 | fprintf(output, "push rbp\n"); | ||
54 | fprintf(output, "mov rbp, rsp\n"); | ||
55 | fprintf(output, "push rsi\n"); | ||
56 | fprintf(output, "push rdi\n"); | ||
57 | fprintf(output, "mov rsi, rax\n"); | ||
58 | fprintf(output, "mov rdi, format_int_in\n"); | ||
59 | fprintf(output, "mov rax, 0\n"); | ||
60 | fprintf(output, "call scanf\n"); | ||
61 | fprintf(output, "pop rdi\n"); | ||
62 | fprintf(output, "pop rsi\n"); | ||
63 | fprintf(output, "pop rbp\n"); | ||
64 | fprintf(output, "ret\n"); | ||
65 | fprintf(output, "\nreadc: ;read needs an adress in rax\n"); | ||
66 | fprintf(output, "push rbp\n"); | ||
67 | fprintf(output, "mov rbp, rsp\n"); | ||
68 | fprintf(output, "push rsi\n"); | ||
69 | fprintf(output, "push rdi\n"); | ||
70 | fprintf(output, "mov rsi, rax\n"); | ||
71 | fprintf(output, "mov rdi, format_char_in\n"); | ||
72 | fprintf(output, "mov rax, 0\n"); | ||
73 | fprintf(output, "call scanf\n"); | ||
74 | fprintf(output, "pop rdi\n"); | ||
75 | fprintf(output, "pop rsi\n"); | ||
76 | fprintf(output, "pop rbp\n"); | ||
77 | fprintf(output, "ret\n"); | ||
78 | *bss_done = 1; | ||
18 | } | 79 | } |
19 | void gen_prologue_continue(int *bss_done) { | 80 | |
20 | if (*bss_done == 0) { | ||
21 | fprintf(output, "globals: resq %d\n", nb_globals); | ||
22 | fprintf(output, "section .text\n\nglobal _start\n"); | ||
23 | fprintf(output, "\nprinte: ;print needs an argument in rax\n"); | ||
24 | fprintf(output, "push rbp\n"); | ||
25 | fprintf(output, "mov rbp, rsp\n"); | ||
26 | fprintf(output, "push rsi\n"); | ||
27 | fprintf(output, "push rdi\n"); | ||
28 | fprintf(output, "mov rsi, rax\n"); | ||
29 | fprintf(output, "mov rdi, format_int\n"); | ||
30 | fprintf(output, "mov rax, 0\n"); | ||
31 | fprintf(output, "call printf WRT ..plt\n"); | ||
32 | fprintf(output, "pop rdi\n"); | ||
33 | fprintf(output, "pop rsi\n"); | ||
34 | fprintf(output, "pop rbp\n"); | ||
35 | fprintf(output, "ret\n"); | ||
36 | fprintf(output, "\nprintc: ;print needs an argument in rax\n"); | ||
37 | fprintf(output, "push rbp\n"); | ||
38 | fprintf(output, "mov rbp, rsp\n"); | ||
39 | fprintf(output, "push rsi\n"); | ||
40 | fprintf(output, "push rdi\n"); | ||
41 | fprintf(output, "mov rsi, rax\n"); | ||
42 | fprintf(output, "mov rdi, format_char\n"); | ||
43 | fprintf(output, "mov rax, 0\n"); | ||
44 | fprintf(output, "call printf WRT ..plt\n"); | ||
45 | fprintf(output, "pop rdi\n"); | ||
46 | fprintf(output, "pop rsi\n"); | ||
47 | fprintf(output, "pop rbp\n"); | ||
48 | fprintf(output, "ret\n"); | ||
49 | fprintf(output, "\nreade: ;read needs an adress in rax\n"); | ||
50 | fprintf(output, "push rbp\n"); | ||
51 | fprintf(output, "mov rbp, rsp\n"); | ||
52 | fprintf(output, "push rsi\n"); | ||
53 | fprintf(output, "push rdi\n"); | ||
54 | fprintf(output, "mov rsi, rax\n"); | ||
55 | fprintf(output, "mov rdi, format_int_in\n"); | ||
56 | fprintf(output, "mov rax, 0\n"); | ||
57 | fprintf(output, "call scanf\n"); | ||
58 | fprintf(output, "pop rdi\n"); | ||
59 | fprintf(output, "pop rsi\n"); | ||
60 | fprintf(output, "pop rbp\n"); | ||
61 | fprintf(output, "ret\n"); | ||
62 | fprintf(output, "\nreadc: ;read needs an adress in rax\n"); | ||
63 | fprintf(output, "push rbp\n"); | ||
64 | fprintf(output, "mov rbp, rsp\n"); | ||
65 | fprintf(output, "push rsi\n"); | ||
66 | fprintf(output, "push rdi\n"); | ||
67 | fprintf(output, "mov rsi, rax\n"); | ||
68 | fprintf(output, "mov rdi, format_char_in\n"); | ||
69 | fprintf(output, "mov rax, 0\n"); | ||
70 | fprintf(output, "call scanf\n"); | ||
71 | fprintf(output, "pop rdi\n"); | ||
72 | fprintf(output, "pop rsi\n"); | ||
73 | fprintf(output, "pop rbp\n"); | ||
74 | fprintf(output, "ret\n"); | ||
75 | *bss_done = 1; | ||
76 | } | ||
77 | } | ||
78 | void gen_const_declaration() { | 81 | void gen_const_declaration() { |
79 | fprintf(output, "\n_start:\n"); | 82 | fprintf(output, "\n_start:\n"); |
80 | fprintf(output, "push rbp\nmov rbp, rsp\n\n"); | 83 | fprintf(output, "push rbp\nmov rbp, rsp\n\n"); |
@@ -90,6 +93,20 @@ void gen_const_declaration() { | |||
90 | fun_display_table(); | 93 | fun_display_table(); |
91 | } | 94 | } |
92 | 95 | ||
96 | void gen_const(const char name[], int value, Scope scope) { | ||
97 | switch (scope) { | ||
98 | case LOCAL: | ||
99 | loc_addVar(name, INT); // TODO: make read only | ||
100 | fprintf(output, "push %d\n", value); | ||
101 | return; | ||
102 | |||
103 | case GLOBAL: | ||
104 | glo_addVar(name, INT); // TODO: make read only | ||
105 | fprintf(output, "%s: db QWORD %d\n", name, value); | ||
106 | return; | ||
107 | } | ||
108 | } | ||
109 | |||
93 | Type gen_function_declaration(const char name[], int return_type) { | 110 | Type gen_function_declaration(const char name[], int return_type) { |
94 | fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); | 111 | fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); |
95 | return return_type; | 112 | return return_type; |