1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/* Symbol table support
Copyright (C) 1998 James Bowman
This file is part of gpasm.
gpasm is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
gpasm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with gpasm; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include "stdhdr.h"
#include "gpasm.h"
#include "symbol.h"
#include "lst.h"
#define HASH_SIZE 173 /* Too small and we get collisions. Too big
* and we use up memory and run slow.. */
struct symbol {
char *name;
void *annotation;
struct symbol *next;
};
struct symbol_table {
int count;
int case_insensitive;
int (*compare)(const char *__s1, const char *__s2);
struct symbol *hash_table[HASH_SIZE];
struct symbol_table *prev;
};
/************************************************************************/
/* Base the hash func on the 1st, 2nd, 3rd and last characters of the
string, and its length. */
static int hashfunc(struct symbol_table *t, char *s)
{
union {
char b[4];
unsigned long ul;
} change;
int len;
change.ul = 0;
len = strlen(s);
change.b[0] = s[0];
if (len > 1) {
change.b[1] = s[1];
change.b[3] = s[len - 1];
if (len > 2)
change.b[2] = s[2];
}
if (t->case_insensitive) {
change.ul &= 0x1f1f1f1f;
}
change.ul += (len << 3);
return (change.ul % HASH_SIZE);
}
struct symbol_table *push_symbol_table(struct symbol_table * table,
int case_insensitive)
{
struct symbol_table *new = calloc(sizeof(*new), 1);
new->case_insensitive = case_insensitive;
if (case_insensitive)
new->compare = strcasecmp;
else
new->compare = strcmp;
new->prev = table;
return new;
}
struct symbol_table *pop_symbol_table(struct symbol_table *table)
{
struct symbol_table *prev;
prev = table->prev;
return prev;
}
struct symbol *add_symbol(struct symbol_table *table, char *name)
{
struct symbol *r;
int index = hashfunc(table, name);
table->count++;
r = table->hash_table[index];
while (r && ((*table->compare)(name, r->name) != 0))
r = r->next;
if (!r) { /* No match */
r = malloc(sizeof(*r));
r->name = strdup(name);
r->next = table->hash_table[index];
r->annotation = NULL;
table->hash_table[index] = r;
}
return r;
}
struct symbol *get_symbol(struct symbol_table *table, char *name)
{
struct symbol *r = NULL;
if (table != NULL) {
int index = hashfunc(table, name);
r = table->hash_table[index];
while (r && ((*table->compare)(name, r->name) != 0))
r = r->next;
/* If r is still NULL, we didn't match. Try the prev table on the stack */
if (r == NULL)
r = get_symbol(table->prev, name);
}
return r;
}
void annotate_symbol(struct symbol *s, void *a)
{
s->annotation = a;
}
/************************************************************************/
char *get_symbol_name(struct symbol *s)
{
return s->name;
}
void *get_symbol_annotation(struct symbol *s)
{
return s->annotation;
}
/************************************************************************/
static int symbol_compare(const void *p0,
const void *p1)
{
struct symbol
*s0 = *(struct symbol **)p0,
*s1 = *(struct symbol **)p1;
return strcmp(s0->name, s1->name);
}
void dump_symbol_table(struct symbol_table *table)
{
int i;
const char *symbol_format = "%-32s %08X";
struct symbol **lst, **ps, *s;
char buf[BUFSIZ];
lst_line("");
lst_line("SYMBOL TABLE");
sprintf(buf, "%-32s %-8s", " LABEL", " VALUE");
lst_line(buf);
lst_line("");
ps = lst = malloc(table->count * sizeof(lst[0]));
for (i = 0; i < HASH_SIZE; i++)
for (s = table->hash_table[i]; s; s = s->next)
*ps++ = s;
assert(ps == &lst[table->count]);
qsort(lst, table->count, sizeof(lst[0]), symbol_compare);
for (i = 0; i < table->count; i++) {
struct variable *var;
var = get_symbol_annotation(lst[i]);
sprintf(buf,
symbol_format,
get_symbol_name(lst[i]),
var ? var->value : 0);
lst_line(buf);
}
}
|