From fa48dde1ff16267055508144d606c42197d73a95 Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Sun, 25 Aug 2024 15:24:40 -0600 Subject: [PATCH] Remove .slotnum field on symbol tables; slight compile cleanup The .slotnum fields in symbol tables are superfluous, so remove them. Also clean up a few functions in compile.c. --- toys/pending/awk.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/toys/pending/awk.c b/toys/pending/awk.c index 24cfb6a4..e94d26f1 100644 --- a/toys/pending/awk.c +++ b/toys/pending/awk.c @@ -213,7 +213,6 @@ enum spec_var_names { ARGC=1, ARGV, CONVFMT, ENVIRON, FILENAME, FNR, FS, NF, struct symtab_slot { // global symbol table entry unsigned flags; - int slotnum; char *name; }; @@ -266,7 +265,6 @@ struct zstring { struct functab_slot { // function symbol table entry unsigned flags; - int slotnum; char *name; struct zlist function_locals; int zcode_addr; @@ -1113,15 +1111,15 @@ static int havetok(int tk) } //// code and "literal" emitters -static void gen2cd(int op, int n) +static void gencd(int op) { - zlist_append(&TT.zcode, &op); - TT.zcode_last = zlist_append(&TT.zcode, &n); + TT.zcode_last = zlist_append(&TT.zcode, &op); } -static void gencd(int op) +static void gen2cd(int op, int n) { - TT.zcode_last = zlist_append(&TT.zcode, &op); + gencd(op); + gencd(n); } static int make_literal_str_val(char *s) @@ -1151,8 +1149,7 @@ static int make_literal_num_val(double num) static int make_uninit_val(void) { - struct zvalue v = uninit_zvalue; - return zlist_append(&TT.literals, &v); + return zlist_append(&TT.literals, &uninit_zvalue); } //// END code and "literal" emitters @@ -1166,10 +1163,9 @@ static int find_func_def_entry(char *s) static int add_func_def_entry(char *s) { - struct functab_slot ent = {0, 0, 0, {0, 0, 0, 0}, 0}; + struct functab_slot ent = {0, 0, {0, 0, 0, 0}, 0}; ent.name = xstrdup(s); int slotnum = zlist_append(&TT.func_def_table, &ent); - FUNC_DEF[slotnum].slotnum = slotnum; return slotnum; } @@ -1182,10 +1178,9 @@ static int find_global(char *s) static int add_global(char *s) { - struct symtab_slot ent = {0, 0, 0}; + struct symtab_slot ent = {0, 0}; ent.name = xstrdup(s); int slotnum = zlist_append(&TT.globals_table, &ent); - GLOBAL[slotnum].slotnum = slotnum; return slotnum; } @@ -1198,10 +1193,9 @@ static int find_local_entry(char *s) static int add_local_entry(char *s) { - struct symtab_slot ent = {0, 0, 0}; + struct symtab_slot ent = {0, 0}; ent.name = xstrdup(s); int slotnum = zlist_append(&TT.locals_table, &ent); - LOCAL[slotnum].slotnum = slotnum; return slotnum; } @@ -1211,11 +1205,11 @@ static int find_or_add_var_name(void) int globals_ent = 0; int locals_ent = find_local_entry(TT.tokstr); // in local symbol table? if (locals_ent) { - slotnum = -LOCAL[locals_ent].slotnum; + slotnum = -locals_ent; } else { globals_ent = find_global(TT.tokstr); if (!globals_ent) globals_ent = add_global(TT.tokstr); - slotnum = GLOBAL[globals_ent].slotnum; + slotnum = globals_ent; if (find_func_def_entry(TT.tokstr)) // POSIX: The same name shall not be used both as a variable name // with global scope and as the name of a function. @@ -1528,9 +1522,7 @@ static void function_call(void) // push placeholder for return value, push placeholder for return addr, // push args, then push number of args, then: // for builtins: gen opcode (e.g. tkgsub) - // for user func: gen (tkfunc, function location) - // if function not yet defined, location will be filled in when defined - // the location slots will be chained from the symbol table + // for user func: gen (tkfunc, number-of-args) int functk = 0, funcnum = 0; char builtin_name[16]; // be sure it's long enough for all builtins if (ISTOK(tkbuiltin)) { -- 2.39.2