diff options
author | pacien | 2018-05-08 22:51:11 +0200 |
---|---|---|
committer | pacien | 2018-05-08 22:51:11 +0200 |
commit | 337ca2a847e6c5f7555c90807f13412c349b753f (patch) | |
tree | e4fa5378be47b0b80a66f14a19ec0fcc6accce17 /src/generator.c | |
parent | 5732ffaa722bda7c5957324e79e61ad8f3811e2f (diff) | |
download | tpc-compiler-337ca2a847e6c5f7555c90807f13412c349b753f.tar.gz |
Extract generators and reformat grammar
Diffstat (limited to 'src/generator.c')
-rw-r--r-- | src/generator.c | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/src/generator.c b/src/generator.c index 004e559..e547a23 100644 --- a/src/generator.c +++ b/src/generator.c | |||
@@ -96,8 +96,19 @@ void gen_ifelse_end(int idx) { | |||
96 | 96 | ||
97 | // ----- OPERATORS ----- | 97 | // ----- OPERATORS ----- |
98 | 98 | ||
99 | void gen_assign(int ident, Scope scope) { | 99 | int gen_assign(const char ident[], Scope scope) { |
100 | // TODO: what's "$$" and how do we extract it? | 100 | const char instr_fmt[] = "pop QWORD [rbp - %d] ;%s\n"; |
101 | |||
102 | switch (scope) { | ||
103 | case GLOBAL: | ||
104 | printf(instr_fmt, glo_get_addr(ident), ident); | ||
105 | return glo_lookup(ident); | ||
106 | case LOCAL: | ||
107 | printf(instr_fmt, loc_get_addr(ident), ident); | ||
108 | return loc_lookup(ident); | ||
109 | default: | ||
110 | exit(1); | ||
111 | } | ||
101 | } | 112 | } |
102 | 113 | ||
103 | void gen_or(int left, int right, int idx) { | 114 | void gen_or(int left, int right, int idx) { |
@@ -206,3 +217,48 @@ void gen_divstar(char op, int left, int right) { | |||
206 | } | 217 | } |
207 | } | 218 | } |
208 | 219 | ||
220 | int gen_signed_expr(char op, int type) { | ||
221 | switch (op) { | ||
222 | case '+': | ||
223 | printf(";+F\n"); | ||
224 | break; | ||
225 | case '-': | ||
226 | printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n"); | ||
227 | break; | ||
228 | default: | ||
229 | exit(1); | ||
230 | } | ||
231 | |||
232 | return type; | ||
233 | } | ||
234 | |||
235 | int gen_negate_expr(int type) { | ||
236 | printf(";!F\npop rax\nxor rax,1\npush rax\n"); | ||
237 | return type; | ||
238 | } | ||
239 | |||
240 | int gen_value(const char ident[], Scope scope) { | ||
241 | switch (scope) { | ||
242 | case GLOBAL: | ||
243 | printf("push QWORD [rbp - %d] ;%s\n", glo_get_addr(ident), ident); | ||
244 | return glo_lookup(ident); | ||
245 | case LOCAL: | ||
246 | printf("push QWORD [rbp - %d] ;%s\n", loc_get_addr(ident), ident); | ||
247 | return loc_lookup(ident); | ||
248 | default: | ||
249 | exit(1); | ||
250 | } | ||
251 | } | ||
252 | |||
253 | int gen_num(int value, Scope scope) { | ||
254 | if (scope == LOCAL) printf("push %d\n", value); // TODO: remove if? | ||
255 | // stored for the semantic analysis. | ||
256 | |||
257 | return INT; | ||
258 | } | ||
259 | |||
260 | int gen_char(int value, Scope scope) { | ||
261 | if(scope == LOCAL) printf("push %d\n", value); // TODO: remove if? | ||
262 | return CHAR; | ||
263 | } | ||
264 | |||