forked from totalspectrum/spin2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.c
314 lines (289 loc) · 7.07 KB
/
symbol.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include "symbol.h"
#include "util/util.h"
extern int gl_caseSensitive;
/*
* hash function
*/
unsigned
RawSymbolHash(const char *str)
{
unsigned hash = 987654321;
unsigned c;
while (*str) {
c = (*str++) & ~0x20; // ignore case distinction
hash = hash * 65537;
hash = hash ^ c;
}
return hash;
}
unsigned
SymbolHash(const char *str)
{
unsigned hash = RawSymbolHash(str);
return hash % SYMTABLE_HASH_SIZE;
}
/* find a symbol in the table */
//extern inline Symbol *FindSymbol(SymbolTable *table, const char *name);
Symbol *
FindSymbolEx(SymbolTable *table, const char *name,int forceCaseSens)
{
unsigned hash;
Symbol *sym;
int nocase = ((table->flags & SYMTAB_FLAG_NOCASE) != 0) && !gl_caseSensitive;
if (forceCaseSens) nocase = forceCaseSens < 0; // 1 forces case sensitive, -1 forces case insensitive
hash = SymbolHash(name);
sym = table->hash[hash];
if (nocase) {
while (sym) {
if (!strcasecmp(sym->our_name, name)) {
return sym;
}
sym = sym->next;
}
} else {
while (sym) {
if (!strcmp(sym->our_name, name)) {
return sym;
}
sym = sym->next;
}
}
/* symbol was not found, give up */
/* it's up to our caller to look through a containing context */
return NULL;
}
//
// like FindSymbol, but goes back through previous contexts too
//
Symbol *
FindSymbolInContext(SymbolTable *table, const char *name)
{
Symbol *sym;
if (!table) return NULL;
sym = FindSymbolEx(table, name, 0);
if (sym) return sym;
return FindSymbolInContext(table->next, name);
}
/* find a symbol in a table or in any of its links */
/* code to find a symbol */
static Symbol *
doLookupSymbolInTable(SymbolTable *table, const char *name, int level)
{
Symbol *sym = NULL;
if (!table) return NULL;
sym = FindSymbol(table, name);
if (!sym) {
if (table->next) {
sym = doLookupSymbolInTable(table->next, name, level);
}
}
if (sym && IsAlias(sym)) {
// have to look it up again
Symbol *alias;
level++;
if (level > 32) {
//ERROR(NULL, "recursive definition for symbol %s", name);
return NULL;
}
alias = doLookupSymbolInTable(table, (const char *)sym->v.ptr, level);
if (alias) {
return alias;
} else {
return sym;
}
}
return sym;
}
/* find a symbol in a table or in any of its links */
/* code to find a symbol */
Symbol *
LookupSymbolInTable(SymbolTable *table, const char *name)
{
return doLookupSymbolInTable(table, name, 0);
}
/*
* find a symbol by offset
* this is slow (we have to search the whole table) and
* not guaranteed by us to be unique
* Also note: if "origsym" is non-NULL then it's a pointer to
* the last symbol we tried to find, and it's used to cycle
* through RESULT, PARAMETER, LOCALS as Spin needs
*/
Symbol *
FindSymbolByOffsetAndKind(SymbolTable *table, int offset, int kind)
{
Symbol *sym = NULL;
int hash;
for (hash = 0; hash < SYMTABLE_HASH_SIZE; hash++) {
sym = table->hash[hash];
while (sym) {
if (sym->offset == offset && sym->kind == kind)
{
return sym;
}
sym = sym->next;
}
}
/* could not find it */
if (kind == SYM_RESULT) {
/* look for first parameter */
kind = SYM_PARAMETER;
sym = FindSymbolByOffsetAndKind(table, 0, SYM_PARAMETER);
}
if (!sym && kind == SYM_PARAMETER) {
sym = FindSymbolByOffsetAndKind(table, 0, SYM_LOCALVAR);
}
return sym;
}
/*
* iterate over all symbols in a table
*/
void
IterateOverSymbols(SymbolTable *table, SymbolFunc func, void *arg)
{
Symbol *sym;
int more;
for (sym = table->i_first; sym; sym = sym->i_next) {
more = func(sym, arg);
if (!more) return;
}
}
/*
* create a new symbol
*/
Symbol *
NewSymbol(void)
{
Symbol *sym = (Symbol *)calloc(1, sizeof(*sym));
return sym;
}
/*
* add a symbol to the table
* returns NULL if there was a conflict
*/
Symbol *
AddSymbol(SymbolTable *table, const char *name, int type, void *val, const char *user_name)
{
unsigned hash;
Symbol *sym;
int nocase = ((table->flags & SYMTAB_FLAG_NOCASE) != 0) && !gl_caseSensitive;
hash = SymbolHash(name);
sym = table->hash[hash];
if (nocase) {
while (sym) {
if (!strcasecmp(sym->our_name, name)) {
if (sym->kind == SYM_WEAK_ALIAS) {
// it's OK to override aliases
break;
}
return NULL;
}
sym = sym->next;
}
} else {
while (sym) {
if (!strcmp(sym->our_name, name)) {
if (sym->kind == SYM_WEAK_ALIAS) {
// it's OK to override aliases
break;
}
return NULL;
}
sym = sym->next;
}
}
if (!sym) {
sym = NewSymbol();
sym->next = table->hash[hash];
table->hash[hash] = sym;
// now link into global list
if (table->i_last) {
table->i_last->i_next = sym;
table->i_last = sym;
} else {
table->i_last = table->i_first = sym;
}
}
sym->our_name = name;
sym->user_name = user_name ? user_name : name;
sym->kind = (Symtype)type;
sym->v.ptr = val;
sym->module = 0;
return sym;
}
Symbol *
AddInternalSymbol(SymbolTable *table, const char *name, int type, void *val, const char *user_name)
{
Symbol *sym;
sym = AddSymbol(table, name, type, val, user_name);
if (sym) {
sym->flags |= SYMF_INTERNAL;
}
return sym;
}
static int tmpvarnum = 1;
static int tmpvarmax = 99999;
/*
* set the base and max for temporary variable names
* returns the old base
*/
int
SetTempVariableBase(int base, int max)
{
int oldbase = tmpvarnum;
tmpvarnum = base;
if (max > 0) {
tmpvarmax = max;
}
return oldbase;
}
/*
* create a temporary variable name
*/
char *
NewTemporaryVariable(const char *prefix, int *counter)
{
char *str;
char buf[32];
int countval;
if (!prefix) {
prefix = "_tmp_";
}
if (!counter) {
counter = &tmpvarnum;
}
countval = *counter;
sprintf(buf, "_%04d", countval);
str = strdupcat(prefix, buf);
countval++;
if (countval > tmpvarmax) {
fprintf(stderr, "Temporary variable limit of %d exceeded", tmpvarmax);
abort();
}
*counter = countval;
return str;
}
/*
* debug: dump all symbols
*/
static const char *
safe_str(const char *s)
{
if (!s) return "<null>";
return s;
}
static int
PrintSymbol(Symbol *sym, void *arg)
{
printf("%s (%s)\n", safe_str(sym->user_name), safe_str(sym->our_name));
return 1;
}
void
DumpSymbolTable(SymbolTable *table)
{
IterateOverSymbols(table, PrintSymbol, NULL);
}