aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
authorpacien2018-05-08 22:51:11 +0200
committerpacien2018-05-08 22:51:11 +0200
commit337ca2a847e6c5f7555c90807f13412c349b753f (patch)
treee4fa5378be47b0b80a66f14a19ec0fcc6accce17 /src/generator.c
parent5732ffaa722bda7c5957324e79e61ad8f3811e2f (diff)
downloadtpc-compiler-337ca2a847e6c5f7555c90807f13412c349b753f.tar.gz
Extract generators and reformat grammar
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c60
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
99void gen_assign(int ident, Scope scope) { 99int 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
103void gen_or(int left, int right, int idx) { 114void 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
220int 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
235int gen_negate_expr(int type) {
236 printf(";!F\npop rax\nxor rax,1\npush rax\n");
237 return type;
238}
239
240int 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
253int 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
260int gen_char(int value, Scope scope) {
261 if(scope == LOCAL) printf("push %d\n", value); // TODO: remove if?
262 return CHAR;
263}
264