summaryrefslogtreecommitdiff
path: root/symbol.c
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2012-09-25 12:58:05 +0200
committerArnold D. Robbins <arnold@skeeve.com>2012-09-25 12:58:05 +0200
commitb9a82851866f84ca306a2802b4ca50089a2fe683 (patch)
tree1f9928af8fc5f46589275992e6c6e7295e7a5924 /symbol.c
parenteb2698f49247c94c84e1e2c2304ba94d96c89bc9 (diff)
downloadgawk-b9a82851866f84ca306a2802b4ca50089a2fe683.tar.gz
First cut at SYMTAB and FUNCTAB.
Diffstat (limited to 'symbol.c')
-rw-r--r--symbol.c297
1 files changed, 159 insertions, 138 deletions
diff --git a/symbol.c b/symbol.c
index dd89796e..61408c49 100644
--- a/symbol.c
+++ b/symbol.c
@@ -30,19 +30,44 @@ extern INSTRUCTION *rule_list;
#define HASHSIZE 1021
-static NODE *variables[HASHSIZE];
static int func_count; /* total number of functions */
static int var_count; /* total number of global variables and functions */
static NODE *symbol_list;
static void (*install_func)(NODE *) = NULL;
static NODE *make_symbol(char *name, NODETYPE type);
-static NODE *install(char *name, NODE *hp, NODETYPE type);
+static NODE *install(char *name, NODE *parm, NODETYPE type);
static void free_bcpool(INSTRUCTION *pl);
static AWK_CONTEXT *curr_ctxt = NULL;
static int ctxt_level;
+static NODE *global_table, *param_table, *func_table;
+NODE *symbol_table;
+
+/* Use a flag to avoid a strcmp() call inside install() */
+static bool installing_specials = false;
+
+/* init_symbol_table --- make sure the symbol tables are initialized */
+
+void
+init_symbol_table()
+{
+ getnode(global_table);
+ memset(global_table, '\0', sizeof(NODE));
+ init_array(global_table);
+
+ getnode(param_table);
+ memset(param_table, '\0', sizeof(NODE));
+ init_array(param_table);
+
+ installing_specials = true;
+ func_table = install_symbol(estrdup("FUNCTAB", 7), Node_var_array);
+
+ symbol_table = install_symbol(estrdup("SYMTAB", 6), Node_var_array);
+ installing_specials = false;
+}
+
/*
* install_symbol:
* Install a global name in the symbol table, even if it is already there.
@@ -56,24 +81,41 @@ install_symbol(char *name, NODETYPE type)
}
-/* lookup --- find the most recent global or param node for name
+/*
+ * lookup --- find the most recent global or param node for name
* installed by install_symbol
*/
NODE *
lookup(const char *name)
{
- NODE *hp;
- size_t len;
- int hash1;
-
- len = strlen(name);
- hash1 = hash(name, len, (unsigned long) HASHSIZE, NULL);
- for (hp = variables[hash1]; hp != NULL; hp = hp->hnext) {
- if (hp->hlength == len && strncmp(hp->hname, name, len) == 0)
- return hp->hvalue;
+ NODE *n;
+ NODE *tmp;
+ /* ``It's elephants, all the way down.'' */
+ NODE *tables[] = {
+ param_table, /* parameters shadow everything */
+ global_table, /* SYMTAB and FUNCTAB found first, can't be redefined */
+ func_table, /* then functions */
+ symbol_table, /* then globals */
+ NULL,
+ };
+ int i;
+
+ tmp = make_string(name, strlen(name));
+
+ n = NULL;
+ for (i = 0; tables[i] != NULL; i++) {
+ if (tables[i]->table_size == 0)
+ continue;
+ n = in_array(tables[i], tmp);
+ if (n != NULL) {
+ unref(tmp);
+ return n;
+ }
}
- return NULL;
+
+ unref(tmp);
+ return n; /* NULL */
}
/* make_params --- allocate function parameters for the symbol table */
@@ -81,7 +123,7 @@ lookup(const char *name)
NODE *
make_params(char **pnames, int pcount)
{
- NODE *hp, *parms;
+ NODE *p, *parms;
int i;
if (pcount <= 0 || pnames == NULL)
@@ -90,12 +132,10 @@ make_params(char **pnames, int pcount)
emalloc(parms, NODE *, pcount * sizeof(NODE), "make_params");
memset(parms, '\0', pcount * sizeof(NODE));
- for (i = 0, hp = parms; i < pcount; i++, hp++) {
- hp->type = Node_param_list;
- hp->hname = pnames[i]; /* shadows pname and vname */
- hp->hlength = strlen(pnames[i]);
- hp->param_cnt = i;
- hp->hvalue = hp; /* points to itself */
+ for (i = 0, p = parms; i < pcount; i++, p++) {
+ p->type = Node_param_list;
+ p->param = pnames[i]; /* shadows pname and vname */
+ p->param_cnt = i;
}
return parms;
@@ -117,7 +157,7 @@ install_params(NODE *func)
)
return;
for (i = 0; i < pcount; i++)
- (void) install(NULL, parms + i, Node_param_list);
+ (void) install(parms[i].param, parms + i, Node_param_list);
}
@@ -128,8 +168,8 @@ install_params(NODE *func)
void
remove_params(NODE *func)
{
- NODE *parms, *p, *prev, *n;
- int i, pcount, hash1;
+ NODE *parms, *p;
+ int i, pcount;
if (func == NULL)
return;
@@ -140,22 +180,15 @@ remove_params(NODE *func)
return;
for (i = pcount - 1; i >= 0; i--) {
+ NODE *tmp;
+
p = parms + i;
- hash1 = p->hcode;
- if (hash1 < 0 || hash1 >= HASHSIZE)
- continue;
- for (prev = NULL, n = variables[hash1]; n != NULL;
- prev = n, n = n->hnext) {
- if (n == p)
- break;
- }
- if (n == NULL)
- continue;
- if (prev == NULL)
- variables[hash1] = n->hnext; /* param at the head of the chain */
- else
- prev->hnext = n->hnext; /* param not at the head */
+ tmp = make_string(p->vname, strlen(p->vname));
+ (void) assoc_remove(param_table, tmp);
+ unref(tmp);
}
+
+ assoc_clear(param_table); /* shazzam! */
}
@@ -164,33 +197,16 @@ remove_params(NODE *func)
NODE *
remove_symbol(NODE *r)
{
- NODE *prev, *hp;
- int hash1;
-
- hash1 = hash(r->vname, strlen(r->vname), (unsigned long) HASHSIZE, NULL);
- for (prev = NULL, hp = variables[hash1]; hp != NULL;
- prev = hp, hp = hp->hnext) {
- if (hp->hvalue == r)
- break;
- }
+ NODE *n = in_array(symbol_table, r);
- if (hp == NULL)
- return NULL;
- assert(hp->hcode == hash1);
-
- if (prev == NULL)
- variables[hash1] = hp->hnext; /* symbol at the head of chain */
- else
- prev->hnext = hp->hnext; /* symbol not at the head */
-
- if (r->type == Node_param_list)
- return r; /* r == hp */
- if (r->type == Node_func)
- func_count--;
- if (r->type != Node_ext_func)
- var_count--;
- freenode(hp);
- return r;
+ if (n == NULL)
+ return n;
+
+ n = dupnode(n);
+
+ (void) assoc_remove(symbol_table, r);
+
+ return n;
}
@@ -248,46 +264,56 @@ destroy_symbol(NODE *r)
static NODE *
make_symbol(char *name, NODETYPE type)
{
- NODE *hp, *r;
+ NODE *r;
- getnode(hp);
- hp->type = Node_hashnode;
- hp->hlength = strlen(name);
- hp->hname = name;
getnode(r);
memset(r, '\0', sizeof(NODE));
- hp->hvalue = r;
if (type == Node_var_array)
init_array(r);
else if (type == Node_var)
r->var_value = dupnode(Nnull_string);
r->vname = name;
r->type = type;
- return hp;
+
+ return r;
}
/* install --- install a global name or function parameter in the symbol table */
static NODE *
-install(char *name, NODE *hp, NODETYPE type)
+install(char *name, NODE *parm, NODETYPE type)
{
- int hash1;
NODE *r;
+ NODE **aptr;
+ NODE *table;
+ NODE *n_name;
+
+ n_name = make_string(name, strlen(name));
+ table = symbol_table;
+
+ if (type == Node_param_list) {
+ table = param_table;
+ } else if (type == Node_func || type == Node_ext_func) {
+ table = func_table;
+ } else if (installing_specials) {
+ table = global_table;
+ }
- if (hp == NULL) {
+ if (parm != NULL) {
+ r = parm;
+ } else {
/* global symbol */
- hp = make_symbol(name, type);
+ r = make_symbol(name, type);
if (type == Node_func)
func_count++;
- if (type != Node_ext_func)
+ if (type != Node_ext_func && table != global_table)
var_count++; /* total, includes Node_func */
}
- r = hp->hvalue;
- hash1 = hash(hp->hname, hp->hlength, (unsigned long) HASHSIZE, NULL);
- hp->hcode = hash1;
- hp->hnext = variables[hash1];
- variables[hash1] = hp;
+ aptr = assoc_lookup(table, n_name);
+ unref(*aptr);
+ *aptr = r;
+ unref(n_name);
if (install_func)
(*install_func)(r);
@@ -322,30 +348,33 @@ get_symbols(SYMBOL_TYPE what, int sort)
{
int i;
NODE **table;
- NODE *hp, *r;
+ NODE **list;
+ NODE *r;
long j, count = 0;
+ long max;
+ NODE *the_table;
- if (what == FUNCTION)
+ if (what == FUNCTION) {
count = func_count;
- else /* if (what == VARIABLE) */
+ the_table = func_table;
+ } else { /* what == VARIABLE */
count = var_count;
+ the_table = symbol_table;
+ update_global_values();
+ }
emalloc(table, NODE **, (count + 1) * sizeof(NODE *), "symbol_list");
- if (what == VARIABLE)
- update_global_values();
- for (i = j = 0; i < HASHSIZE; i++)
- for (hp = variables[i]; hp != NULL; hp = hp->hnext) {
- if (hp->type != Node_hashnode)
- continue;
- r = hp->hvalue;
- if (r->type == Node_ext_func)
- continue;
- if (what == FUNCTION && r->type == Node_func)
- table[j++] = r;
- else if (what == VARIABLE)
- table[j++] = r;
- }
+ max = the_table->table_size * 2;
+ list = assoc_list(the_table, "@unsorted", ASORTI);
+ for (i = j = 0; i < max; i += 2) {
+ r = list[i+1];
+ if (r->type == Node_ext_func)
+ continue;
+ if (what == VARIABLE || r->type == Node_func)
+ table[j++] = r;
+ }
+ efree(list);
if (sort && count > 1)
qsort(table, count, sizeof(NODE *), comp_symbol); /* Shazzam! */
@@ -417,25 +446,8 @@ foreach_func(NODE **table, int (*pfunc)(INSTRUCTION *, void *), void *data)
void
release_all_vars()
{
- int i;
- NODE *hp, *r, *next;
-
- for (i = 0; i < HASHSIZE; i++)
- for (hp = variables[i]; hp != NULL; hp = next) {
- next = hp->hnext;
- if (hp->type != Node_hashnode)
- continue;
- r = hp->hvalue;
- if (r->type == Node_func || r->type == Node_ext_func)
- continue;
- if (r->type == Node_var_array)
- assoc_clear(r);
- else if (r->type == Node_var)
- unref(r->var_value);
- efree(r->vname);
- freenode(r);
- freenode(hp);
- }
+ assoc_clear(symbol_table);
+ assoc_clear(global_table);
}
@@ -446,12 +458,12 @@ release_all_vars()
void
append_symbol(NODE *r)
{
- NODE *hp;
+ NODE *p;
- getnode(hp);
- hp->lnode = r;
- hp->rnode = symbol_list->rnode;
- symbol_list->rnode = hp;
+ getnode(p);
+ p->lnode = r;
+ p->rnode = symbol_list->rnode;
+ symbol_list->rnode = p;
}
/* release_symbol --- free symbol list and optionally remove symbol from symbol table */
@@ -459,17 +471,17 @@ append_symbol(NODE *r)
void
release_symbols(NODE *symlist, int keep_globals)
{
- NODE *hp, *next;
+ NODE *p, *next;
- for (hp = symlist->rnode; hp != NULL; hp = next) {
+ for (p = symlist->rnode; p != NULL; p = next) {
if (! keep_globals) {
/* destroys globals, function, and params
* if still in symbol table
*/
- destroy_symbol(hp->lnode);
+ destroy_symbol(p->lnode);
}
- next = hp->rnode;
- freenode(hp);
+ next = p->rnode;
+ freenode(p);
}
symlist->rnode = NULL;
}
@@ -479,12 +491,19 @@ release_symbols(NODE *symlist, int keep_globals)
void
load_symbols()
{
- NODE *hp, *r;
+ NODE *r;
NODE *tmp;
NODE *sym_array;
NODE **aptr;
- long i;
- NODE *user, *extension, *variable, *scalar,*array;
+ long i, j, max;
+ NODE *user, *extension, *untyped, *scalar, *array;
+ NODE **list;
+ NODE *tables[] = {
+ func_table,
+ symbol_table,
+ global_table,
+ NULL
+ };
if (PROCINFO_node == NULL)
return;
@@ -506,15 +525,16 @@ load_symbols()
user = make_string("user", 4);
extension = make_string("extension", 9);
scalar = make_string("scalar", 6);
- variable = make_string("variable", 8);
+ untyped = make_string("untyped", 7);
array = make_string("array", 5);
- for (i = 0; i < HASHSIZE; i++) {
- for (hp = variables[i]; hp != NULL; hp = hp->hnext) {
- if (hp->type != Node_hashnode)
- continue;
-
- r = hp->hvalue;
+ for (i = 0; tables[i] != NULL; i++) {
+ list = assoc_list(tables[i], "@unsorted", ASORTI);
+ max = tables[i]->table_size * 2;
+ if (max == 0)
+ continue;
+ for (j = 0; j < max; j += 2) {
+ r = list[j+1];
if ( r->type == Node_ext_func
|| r->type == Node_func
|| r->type == Node_var
@@ -538,7 +558,7 @@ load_symbols()
*aptr = dupnode(array);
break;
case Node_var_new:
- *aptr = dupnode(variable);
+ *aptr = dupnode(untyped);
break;
default:
cant_happen();
@@ -546,12 +566,13 @@ load_symbols()
}
}
}
+ efree(list);
}
unref(user);
unref(extension);
unref(scalar);
- unref(variable);
+ unref(untyped);
unref(array);
}