From d07aa27c361c424e1da383ae49e98e5dfb33425e Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 5 Jun 2018 22:16:36 +0200 Subject: handle decl. of read-only vars --- src/generator.c | 4 ++-- src/symbol_table.c | 28 ++++++++++++++++++++++------ src/symbol_table.h | 4 ++++ 3 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/generator.c b/src/generator.c index 33fade9..0040c02 100644 --- a/src/generator.c +++ b/src/generator.c @@ -96,12 +96,12 @@ void gen_const_declaration() { void gen_const(const char name[], int value, Scope scope) { switch (scope) { case LOCAL: - loc_addVar(name, INT); // TODO: make read only + loc_addConst(name); fprintf(output, "push %d\n", value); return; case GLOBAL: - glo_addVar(name, INT); // TODO: make read only + glo_addConst(name); fprintf(output, "%s: db QWORD %d\n", name, value); return; } diff --git a/src/symbol_table.c b/src/symbol_table.c index 483de4c..08124ac 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -66,7 +66,7 @@ int fun_lookup(const char name[], int nb_param) { return -1; } -void glo_addVar(const char name[], int type) { +static void glo_add(const char name[], int type, bool read_only) { int count; for (count = 0; count < glo_symbol_table.size; count++) { if (!strcmp(glo_symbol_table.entries[count].name, name)) { @@ -82,8 +82,16 @@ void glo_addVar(const char name[], int type) { } strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; - glo_symbol_table.entries[glo_symbol_table.size - 1].addr = - (glo_symbol_table.size - 1) * 8; + glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1) * 8; + glo_symbol_table.entries[glo_symbol_table.size - 1].read_only = read_only; +} + +void glo_addVar(const char name[], int type) { + glo_add(name, type, false); +} + +void glo_addConst(const char name[]) { + glo_add(name, INT, true); } // Verifies that the variable exists and returns the type @@ -125,7 +133,7 @@ void glo_display_table() { fprintf(output, "\n"); } -void loc_addVar(const char name[], int type) { +static void loc_add(const char name[], int type, bool read_only) { int count; for (count = 0; count < loc_symbol_table.size; count++) { if (!strcmp(loc_symbol_table.entries[count].name, name)) { @@ -141,8 +149,16 @@ void loc_addVar(const char name[], int type) { } strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; - loc_symbol_table.entries[loc_symbol_table.size - 1].addr = - (loc_symbol_table.size - 1) * 8 + 8; + loc_symbol_table.entries[loc_symbol_table.size - 1].addr = (loc_symbol_table.size - 1) * 8 + 8; + loc_symbol_table.entries[loc_symbol_table.size - 1].read_only = read_only; +} + +void loc_addVar(const char name[], int type) { + loc_add(name, type, false); +} + +void loc_addConst(const char name[]) { + loc_add(name, INT, true); } int loc_lookup(const char name[]) { diff --git a/src/symbol_table.h b/src/symbol_table.h index d0ac440..b2c8879 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -9,6 +9,7 @@ #include #include #include +#include #define MAXNAME 32 #define MAXSYMBOLS 256 @@ -24,6 +25,7 @@ typedef struct { char name[MAXNAME]; int type; int addr; + bool read_only; } STentry; typedef struct { @@ -48,10 +50,12 @@ void fun_add(const char name[], int rt_type, int nb_par); void fun_display_table(); int fun_lookup(const char name[], int nb_param); void glo_addVar(const char name[], int type); +void glo_addConst(const char name[]); int glo_lookup(const char name[]); int glo_get_addr(const char name[]); void glo_display_table(); void loc_addVar(const char name[], int type); +void loc_addConst(const char name[]); int loc_lookup(const char name[]); int loc_get_addr(const char name[]); void loc_display_table(); -- cgit v1.2.3