aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/generator.c b/src/generator.c
index bbb2af5..553161a 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -12,9 +12,8 @@ void gen_prologue() {
12 fprintf(output, "section .data\n"); 12 fprintf(output, "section .data\n");
13} 13}
14 14
15void gen_prologue_continue(int *bss_done) { 15void gen_prologue_continue(bool *bss_done) {
16 if (*bss_done != 0) 16 if (!*bss_done) return;
17 return;
18 17
19 fprintf(output, "format_int db \"%%d\",10,0\n"); 18 fprintf(output, "format_int db \"%%d\",10,0\n");
20 fprintf(output, "format_char db \"%%c\",10,0\n"); 19 fprintf(output, "format_char db \"%%c\",10,0\n");
@@ -115,8 +114,7 @@ Type gen_function_declaration(const char name[], int return_type) {
115 return return_type; 114 return return_type;
116} 115}
117 116
118void gen_function_end_declaration(const char name[], int return_type, 117void gen_function_end_declaration(const char name[], Type return_type, int nb_param) {
119 int nb_param) {
120 fun_add(name, return_type, nb_param); 118 fun_add(name, return_type, nb_param);
121 fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); 119 fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n");
122} 120}
@@ -139,7 +137,7 @@ Type gen_function_call(const char name[], int nb_param) {
139 return return_type; 137 return return_type;
140} 138}
141 139
142void gen_declaration(const char name[], int type, Scope scope) { 140void gen_declaration(const char name[], Type type, Scope scope) {
143 switch (scope) { 141 switch (scope) {
144 case GLOBAL: 142 case GLOBAL:
145 glo_addVar(name, type); 143 glo_addVar(name, type);
@@ -220,7 +218,7 @@ void gen_readc(const char name[], Scope scope) {
220 fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr); 218 fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr);
221} 219}
222 220
223void gen_print(int type) { 221void gen_print(Type type) {
224 // check if the name exists in both tables 222 // check if the name exists in both tables
225 fprintf(output, "pop rax\n"); 223 fprintf(output, "pop rax\n");
226 switch (type) { 224 switch (type) {
@@ -264,7 +262,7 @@ void gen_ifelse_end(int idx) {
264 262
265// ----- OPERATORS ----- 263// ----- OPERATORS -----
266 264
267int gen_assign(const char ident[], Scope scope) { 265static int gen_assign_simple(const char ident[], Scope scope) {
268 int l_addr = loc_get_addr(ident); 266 int l_addr = loc_get_addr(ident);
269 int g_addr = glo_get_addr(ident); 267 int g_addr = glo_get_addr(ident);
270 268
@@ -290,7 +288,7 @@ int gen_assign(const char ident[], Scope scope) {
290 } 288 }
291} 289}
292 290
293int gen_assign_tab(const char ident[], Scope scope) { 291static int gen_assign_tab(const char ident[], Scope scope) {
294 int l_addr = loc_get_addr(ident); 292 int l_addr = loc_get_addr(ident);
295 int g_addr = glo_get_addr(ident); 293 int g_addr = glo_get_addr(ident);
296 294
@@ -322,6 +320,13 @@ int gen_assign_tab(const char ident[], Scope scope) {
322 } 320 }
323} 321}
324 322
323int gen_assign(const char ident[], Scope scope) {
324 switch (loc_lookup(ident)) {
325 case TAB: return gen_assign_tab(ident, scope);
326 default: return gen_assign_simple(ident, scope);
327 }
328}
329
325void gen_or(int left, int right, int idx) { 330void gen_or(int left, int right, int idx) {
326 check_expected_types(left, INT, TAB); 331 check_expected_types(left, INT, TAB);
327 check_expected_types(right, INT, TAB); 332 check_expected_types(right, INT, TAB);
@@ -430,7 +435,7 @@ void gen_divstar(char op, int left, int right) {
430 } 435 }
431} 436}
432 437
433int gen_signed_expr(char op, int type) { 438int gen_signed_expr(char op, Type type) {
434 check_expected_types(type, INT, TAB); 439 check_expected_types(type, INT, TAB);
435 switch (op) { 440 switch (op) {
436 case '+': 441 case '+':
@@ -446,13 +451,13 @@ int gen_signed_expr(char op, int type) {
446 return type; 451 return type;
447} 452}
448 453
449int gen_negate_expr(int type) { 454int gen_negate_expr(Type type) {
450 check_expected_types(type, INT, TAB); 455 check_expected_types(type, INT, TAB);
451 fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n"); 456 fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n");
452 return type; 457 return type;
453} 458}
454 459
455int gen_value(const char ident[], Scope scope) { 460static int gen_value_simple(const char ident[], Scope scope) {
456 int l_addr = loc_get_addr(ident); 461 int l_addr = loc_get_addr(ident);
457 462
458 switch (scope) { 463 switch (scope) {
@@ -475,7 +480,8 @@ int gen_value(const char ident[], Scope scope) {
475 exit(1); 480 exit(1);
476 } 481 }
477} 482}
478int gen_value_tab(const char ident[], Scope scope) { 483
484static int gen_value_tab(const char ident[], Scope scope) {
479 int l_addr = loc_get_addr(ident); 485 int l_addr = loc_get_addr(ident);
480 int g_addr = glo_get_addr(ident); 486 int g_addr = glo_get_addr(ident);
481 switch (scope) { 487 switch (scope) {
@@ -500,6 +506,14 @@ int gen_value_tab(const char ident[], Scope scope) {
500 exit(1); 506 exit(1);
501 } 507 }
502} 508}
509
510int gen_value(const char ident[], Scope scope) {
511 switch (loc_lookup(ident)) {
512 case TAB: return gen_value_tab(ident, scope);
513 default: return gen_value_simple(ident, scope);
514 }
515}
516
503int gen_num(int value, Scope scope) { 517int gen_num(int value, Scope scope) {
504 fprintf(output, "push %d\n", value); 518 fprintf(output, "push %d\n", value);
505 return INT; 519 return INT;