-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbols.c
394 lines (346 loc) · 8.66 KB
/
symbols.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (C) 1999-2012 Core Technologies.
//
// This file is part of tpasm.
//
// tpasm is free software; you can redistribute it and/or modify
// it under the terms of the tpasm LICENSE AGREEMENT.
//
// tpasm 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
// tpasm LICENSE AGREEMENT for more details.
//
// You should have received a copy of the tpasm LICENSE AGREEMENT
// along with tpasm; see the file "LICENSE.TXT".
// Low level symbol table handling
#include "include.h"
#define ST_MAX_HASH_TABLE_BITS 10 // maximum number of bits that will be used for a hash table lookup
struct SYM_TABLE_NODE
{
SYM_TABLE_NODE
*next,
*prev,
*hashNext,
*hashPrev;
void
*data;
unsigned int
hashValue; // value that the key below hashes to
char
name[1]; // variable length array which contains the name of the symbol
};
struct SYM_TABLE
{
SYM_TABLE_NODE
*first, // linearly linked table
*last;
unsigned int
hashTableBits; // tells how many bits of the hash value are used to index the hash list (also implies the size of the hash list)
SYM_TABLE_NODE
*hashList[1]; // variable length hash table
};
static unsigned int STHash(const char *name)
// Return the full hash value for the given name.
{
unsigned int
hash;
unsigned int
count;
unsigned char
c;
hash=0;
for(count=0;(c=name[count]);count++)
{
c=tolower(c); // make hash case-insensitive so we can search either way
hash^=1<<(c&0x0F);
hash^=((c>>4)&0x0F)<<(count&0x07);
}
return(hash);
}
static inline bool STCompNames(const char *name1,const char *name2)
// Compares the given names and returns true if they are the same.
{
return(strcmp(name1,name2)==0);
}
static inline bool STCompNamesNoCase(const char *name1,const char *name2)
// Compares the given names and returns true if they are the same.
{
return(strcasecmp(name1,name2)==0);
}
SYM_TABLE_NODE *STFindNode(SYM_TABLE *table,const char *name)
// Find the node with the given name.
// return NULL if none could be located
{
unsigned int
hashIndex;
SYM_TABLE_NODE
*node;
hashIndex=STHash(name)&((1<<table->hashTableBits)-1);
node=table->hashList[hashIndex];
while(node&&!STCompNames(name,node->name))
{
node=node->hashNext;
}
return(node);
}
SYM_TABLE_NODE *STFindNodeNoCase(SYM_TABLE *table,const char *name)
// Find the first node with the given (case insensitive) name.
// return NULL if none could be located
{
unsigned int
hashIndex;
SYM_TABLE_NODE
*node;
hashIndex=STHash(name)&((1<<table->hashTableBits)-1);
node=table->hashList[hashIndex];
while(node&&!STCompNamesNoCase(name,node->name))
{
node=node->hashNext;
}
return(node);
}
void STRemoveEntry(SYM_TABLE *table,SYM_TABLE_NODE *node)
// Removes any entry with the given name from the table.
{
if(node->hashNext) // first unlink it from the hash table
{
node->hashNext->hashPrev=node->hashPrev;
}
if(node->hashPrev)
{
node->hashPrev->hashNext=node->hashNext;
}
else
{
table->hashList[node->hashValue&((1<<table->hashTableBits)-1)]=node->hashNext;
}
if(node->prev==NULL) // then unlink it from the ordered list
{
table->first=node->next;
if(node->next==NULL)
{
table->last=NULL;
}
else
{
node->next->prev=NULL;
}
}
else
{
node->prev->next=node->next;
if(node->next==NULL)
{
table->last=node->prev;
}
else
{
node->next->prev=node->prev;
}
}
DisposePtr(node); // and then destroy it
}
static SYM_TABLE_NODE *STCreateNode(const char *name,void *data)
// Returns a new node containing the given name and data.
// NOTE: the hash value is calculated at this time, and placed into
// the node as well
{
SYM_TABLE_NODE
*newNode;
if((newNode=(SYM_TABLE_NODE*)NewPtr(sizeof(SYM_TABLE_NODE)+strlen(name)+1)))
{
newNode->data=data;
newNode->hashValue=STHash(name);
strcpy(newNode->name,name);
return(newNode);
}
return(NULL);
}
SYM_TABLE_NODE *STAddEntryAtStart(SYM_TABLE *table,const char *name,void *data)
// Adds the given name and data to table as the first entry.
// It is allowed for two names in the table to have the same value, but
// it is not guaranteed which will be returned when searching
{
SYM_TABLE_NODE
*newNode;
unsigned int
hashIndex;
if((newNode=STCreateNode(name,data)))
{
hashIndex=newNode->hashValue&((1<<table->hashTableBits)-1);
if(table->hashList[hashIndex])
{
table->hashList[hashIndex]->hashPrev=newNode;
}
newNode->hashNext=table->hashList[hashIndex]; // link into hash list
newNode->hashPrev=NULL;
table->hashList[hashIndex]=newNode;
if(table->first) // then link into ordered list
{
table->first->prev=newNode;
}
else
{
table->last=newNode; // first entry in the table
}
newNode->next=table->first;
newNode->prev=NULL;
table->first=newNode;
}
return(newNode);
}
SYM_TABLE_NODE *STAddEntryAtEnd(SYM_TABLE *table,const char *name,void *data)
// Adds the given name and data to table as the last entry.
// It is allowed for two names in the table to have the same value, but
// it is not guaranteed which will be returned when searching
{
SYM_TABLE_NODE
*newNode;
unsigned int
hashIndex;
if((newNode=STCreateNode(name,data)))
{
hashIndex=newNode->hashValue&((1<<table->hashTableBits)-1);
if(table->hashList[hashIndex])
{
table->hashList[hashIndex]->hashPrev=newNode;
}
newNode->hashNext=table->hashList[hashIndex]; // link into hash list
newNode->hashPrev=NULL;
table->hashList[hashIndex]=newNode;
if(table->last) // then link into ordered list
{
table->last->next=newNode;
}
else
{
table->first=newNode; // first entry in the table
}
newNode->prev=table->last;
newNode->next=NULL;
table->last=newNode;
}
return(newNode);
}
void *STFindDataForName(SYM_TABLE *table,const char *name)
// Finds the data for name in the table.
// Returns pointer to the node's data on success, NULL otherwise.
{
SYM_TABLE_NODE
*curNode;
if((curNode=STFindNode(table,name)))
{
return(curNode->data);
}
return(NULL);
}
void *STFindDataForNameNoCase(SYM_TABLE *table,const char *name)
// Finds the data for (case insensitive) name in the table.
// Returns pointer to the node's data on success, NULL otherwise.
{
SYM_TABLE_NODE
*curNode;
if((curNode=STFindNodeNoCase(table,name)))
{
return(curNode->data);
}
return(NULL);
}
SYM_TABLE_NODE *STFindFirstEntry(SYM_TABLE *table)
// Return the first node linked to table
{
return(table->first);
}
SYM_TABLE_NODE *STFindLastEntry(SYM_TABLE *table)
// Return the last node linked to table
{
return(table->last);
}
SYM_TABLE_NODE *STFindNextEntry(SYM_TABLE *table,SYM_TABLE_NODE *previousEntry)
// return the entry just after previousEntry in table
{
return(previousEntry->next);
}
SYM_TABLE_NODE *STFindPrevEntry(SYM_TABLE *table,SYM_TABLE_NODE *nextEntry)
// return the entry just before nextEntry in table
{
return(nextEntry->prev);
}
const char *STNodeName(SYM_TABLE_NODE *node)
// Return the name of the passed symbol table node.
{
return(node->name);
}
void *STNodeData(SYM_TABLE_NODE *node)
// Return the data of the passed symbol table node.
{
return(node->data);
}
unsigned int STNumEntries(SYM_TABLE *table)
// Returns the number of entries in the table.
{
SYM_TABLE_NODE
*curEnt;
unsigned int
numEnts;
numEnts=0;
curEnt=table->first;
while(curEnt!=NULL)
{
numEnts++;
curEnt=curEnt->next;
}
return(numEnts);
}
void STDisposeSymbolTable(SYM_TABLE *table)
// Dispose of all memory allocated for the given symbol table. Note that
// the links are not maintained during the process.
{
SYM_TABLE_NODE
*curEnt,
*tmpEnt;
curEnt=table->first;
while(curEnt)
{
tmpEnt=curEnt->next;
DisposePtr(curEnt);
curEnt=tmpEnt;
}
DisposePtr(table);
}
SYM_TABLE *STNewSymbolTable(unsigned int expectedEntries)
// Create and initialize a new symbol table.
// expectedEntries is used to work out the size of the hash table.
// If it is passed as 0, it suggests that the number of entries could
// be arbitrarily large.
{
SYM_TABLE
*table;
unsigned int
count;
unsigned int
hashTableBits;
if(expectedEntries)
{
hashTableBits=0;
while((hashTableBits<ST_MAX_HASH_TABLE_BITS)&&(((unsigned int)(1<<hashTableBits))<expectedEntries/2)) // shoot for two entries per hash bucket
{
hashTableBits++;
}
}
else
{
hashTableBits=ST_MAX_HASH_TABLE_BITS;
}
if((table=(SYM_TABLE*)NewPtr(sizeof(SYM_TABLE)+(1<<hashTableBits)*sizeof(SYM_TABLE_NODE *))))
{
table->first=table->last=NULL;
table->hashTableBits=hashTableBits;
for(count=0;count<(unsigned int)(1<<hashTableBits);count++)
{
table->hashList[count]=NULL;
}
return(table);
}
return(NULL);
}