From 58e0712bb4f624e02bffc877bf39f5fd45acc2e4 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 14:36:25 +0200 Subject: extract assign and val-retrieval --- src/generator.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/generator.c') diff --git a/src/generator.c b/src/generator.c index 44fdeab..b75f7a8 100644 --- a/src/generator.c +++ b/src/generator.c @@ -265,7 +265,7 @@ void gen_ifelse_end(int idx) { // ----- OPERATORS ----- -int gen_assign(const char ident[], Scope scope) { +static int gen_assign_simple(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); @@ -291,7 +291,7 @@ int gen_assign(const char ident[], Scope scope) { } } -int gen_assign_tab(const char ident[], Scope scope) { +static int gen_assign_tab(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); @@ -317,6 +317,13 @@ int gen_assign_tab(const char ident[], Scope scope) { } } +int gen_assign(const char ident[], Scope scope) { + switch (loc_lookup(ident)) { + case TAB: return gen_assign_tab(ident, scope); + default: return gen_assign_simple(ident, scope); + } +} + void gen_or(int left, int right, int idx) { check_expected_types(left, INT,TAB); check_expected_types(right, INT,TAB); @@ -447,7 +454,7 @@ int gen_negate_expr(int type) { return type; } -int gen_value(const char ident[], Scope scope) { +static int gen_value_simple(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); switch (scope) { @@ -470,7 +477,8 @@ int gen_value(const char ident[], Scope scope) { exit(1); } } -int gen_value_tab(const char ident[], Scope scope) { + +static int gen_value_tab(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); switch (scope) { @@ -489,6 +497,14 @@ int gen_value_tab(const char ident[], Scope scope) { exit(1); } } + +int gen_value(const char ident[], Scope scope) { + switch (loc_lookup(ident)) { + case TAB: return gen_value_tab(ident, scope); + default: return gen_value_simple(ident, scope); + } +} + int gen_num(int value, Scope scope) { fprintf(output, "push %d\n", value); return INT; -- cgit v1.2.3 From 00b8cb6be126d6cfeacf932b5595df9824cdf412 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 15:00:35 +0200 Subject: reformat and unify types --- src/generator.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/generator.c') diff --git a/src/generator.c b/src/generator.c index 1b11b67..553161a 100644 --- a/src/generator.c +++ b/src/generator.c @@ -12,9 +12,8 @@ void gen_prologue() { fprintf(output, "section .data\n"); } -void gen_prologue_continue(int *bss_done) { - if (*bss_done != 0) - return; +void gen_prologue_continue(bool *bss_done) { + if (!*bss_done) return; fprintf(output, "format_int db \"%%d\",10,0\n"); fprintf(output, "format_char db \"%%c\",10,0\n"); @@ -115,8 +114,7 @@ Type gen_function_declaration(const char name[], int return_type) { return return_type; } -void gen_function_end_declaration(const char name[], int return_type, - int nb_param) { +void gen_function_end_declaration(const char name[], Type return_type, int nb_param) { fun_add(name, return_type, nb_param); fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); } @@ -139,7 +137,7 @@ Type gen_function_call(const char name[], int nb_param) { return return_type; } -void gen_declaration(const char name[], int type, Scope scope) { +void gen_declaration(const char name[], Type type, Scope scope) { switch (scope) { case GLOBAL: glo_addVar(name, type); @@ -220,7 +218,7 @@ void gen_readc(const char name[], Scope scope) { fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr); } -void gen_print(int type) { +void gen_print(Type type) { // check if the name exists in both tables fprintf(output, "pop rax\n"); switch (type) { @@ -437,7 +435,7 @@ void gen_divstar(char op, int left, int right) { } } -int gen_signed_expr(char op, int type) { +int gen_signed_expr(char op, Type type) { check_expected_types(type, INT, TAB); switch (op) { case '+': @@ -453,7 +451,7 @@ int gen_signed_expr(char op, int type) { return type; } -int gen_negate_expr(int type) { +int gen_negate_expr(Type type) { check_expected_types(type, INT, TAB); fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n"); return type; -- cgit v1.2.3