ack/modules/src/idf/idf_pkg.body

168 lines
3 KiB
Plaintext
Raw Normal View History

1987-01-05 17:20:13 +00:00
/* SYMBOL TABLE HANDLING */
2016-09-17 22:23:42 +00:00
#include <alloc.h>
1987-01-05 17:20:13 +00:00
2016-09-17 22:23:42 +00:00
#define IDF_HASHSIZE 307 /* size of hashtable, must be odd */
1987-01-05 17:20:13 +00:00
2016-09-17 22:23:42 +00:00
#define IDF_STARTHASH(hs) (hs = 0)
#define IDF_ENHASH(hs, ch) (hs = (hs << 2) + ch)
#define IDF_STOPHASH(hs) (hs = hs % IDF_HASHSIZE)
1987-01-05 17:20:13 +00:00
2016-09-17 22:23:42 +00:00
static struct idf* IDF_hashtable[IDF_HASHSIZE];
/* All identifiers can in principle be reached through
1988-09-26 15:05:00 +00:00
IDF_hashtable; IDF_hashtable[hc] is the start of a chain of
1987-01-05 17:20:13 +00:00
idf's whose tags all hash to hc.
Any identifier is entered into this
list, regardless of the nature of its declaration
(variable, selector, structure tag, etc.).
*/
2016-09-17 22:23:42 +00:00
static struct idf* IDF_new(char* tg, int size, int cpy);
1993-11-10 11:14:28 +00:00
2016-09-17 22:23:42 +00:00
void init_idf()
1993-11-10 11:14:28 +00:00
{
}
1993-10-22 14:09:28 +00:00
2016-09-17 22:23:42 +00:00
static struct idf* IDF_new(char* tg, int size, int cpy)
1987-01-05 17:20:13 +00:00
{
static int nidf;
2016-09-17 22:23:42 +00:00
static struct idf* pidf;
1991-03-14 13:43:45 +00:00
static struct idf null_idf;
2016-09-17 22:23:42 +00:00
register struct idf* id;
1987-01-05 17:20:13 +00:00
#define NIDS 50
2016-09-17 22:23:42 +00:00
#define IBUFSIZ 2048
1987-01-05 17:20:13 +00:00
static unsigned int icnt;
2016-09-17 22:23:42 +00:00
static char* ip;
register char* p;
1987-01-05 17:20:13 +00:00
2016-09-17 22:23:42 +00:00
if (!nidf--)
{
1987-01-05 17:20:13 +00:00
nidf += NIDS;
2016-09-17 22:23:42 +00:00
pidf = (struct idf*)Malloc(NIDS * sizeof(struct idf));
1987-01-05 17:20:13 +00:00
}
1991-03-14 13:46:53 +00:00
id = pidf;
1991-03-14 13:43:45 +00:00
pidf++;
*id = null_idf;
2016-09-17 22:23:42 +00:00
if (cpy)
{
if (size > icnt)
{
icnt = size > IBUFSIZ ? size : IBUFSIZ;
1987-01-05 17:20:13 +00:00
p = Malloc(icnt);
}
2016-09-17 22:23:42 +00:00
else
p = ip;
1987-01-05 17:20:13 +00:00
icnt -= size;
1991-03-14 13:43:45 +00:00
id->id_text = p;
2016-09-17 22:23:42 +00:00
while (size--)
{
1987-01-05 17:20:13 +00:00
*p++ = *tg++;
}
ip = p;
}
2016-09-17 22:23:42 +00:00
else
id->id_text = tg;
1991-03-14 13:43:45 +00:00
return id;
1987-01-05 17:20:13 +00:00
}
2016-09-17 22:23:42 +00:00
#ifdef IDF_DEBUG
void hash_stat(void)
1987-01-05 17:20:13 +00:00
{
register int i;
1991-06-24 16:35:08 +00:00
int total_count = 0;
1987-01-05 17:20:13 +00:00
print("Hash table tally:\n");
2016-09-17 22:23:42 +00:00
for (i = 0; i < IDF_HASHSIZE; i++)
{
register struct idf* notch = IDF_hashtable[i];
1987-01-05 17:20:13 +00:00
register int cnt = 0;
2016-09-17 22:23:42 +00:00
print("%d ", i);
while (notch)
{
1987-01-05 17:20:13 +00:00
cnt++;
1991-06-24 16:35:08 +00:00
print("'%s' ", notch->id_text);
1987-08-06 14:20:11 +00:00
notch = notch->id_next;
1987-01-05 17:20:13 +00:00
}
1991-06-24 16:35:08 +00:00
print("%d\n", cnt);
total_count += cnt;
1987-01-05 17:20:13 +00:00
}
1991-06-24 16:35:08 +00:00
print("total = %d\n", total_count);
1987-01-05 17:20:13 +00:00
print("End hash table tally\n");
}
1991-06-24 16:35:08 +00:00
2016-09-17 22:23:42 +00:00
void idfappfun(int (*fun)(), int opt)
1991-06-24 16:35:08 +00:00
{
2016-09-17 22:23:42 +00:00
register int i;
1991-06-24 16:35:08 +00:00
2016-09-17 22:23:42 +00:00
for (i = 0; i < IDF_HASHSIZE; i++)
{
register struct idf* notch = IDF_hashtable[i];
1991-06-24 16:35:08 +00:00
2016-09-17 22:23:42 +00:00
while (notch)
{
1991-06-24 16:35:08 +00:00
(*fun)(notch, opt);
notch = notch->id_next;
}
}
}
2016-09-17 22:23:42 +00:00
#endif /* IDF_DEBUG */
1987-01-05 17:20:13 +00:00
2016-09-17 22:23:42 +00:00
struct idf* str2idf(char tg[], int cpy)
1987-01-05 17:20:13 +00:00
{
/* str2idf() returns an entry in the symbol table for the
identifier tg. If necessary, an entry is created.
*/
2016-09-17 22:23:42 +00:00
register char* cp = tg;
struct idf** hook;
register struct idf* notch;
register unsigned int hash;
1991-03-14 13:43:45 +00:00
register int c;
1987-01-05 17:20:13 +00:00
int size;
1988-09-26 15:05:00 +00:00
IDF_STARTHASH(hash);
2016-09-17 22:23:42 +00:00
while (c = *cp++)
{
1991-03-14 13:43:45 +00:00
IDF_ENHASH(hash, c);
1987-01-05 17:20:13 +00:00
}
1988-09-26 15:05:00 +00:00
IDF_STOPHASH(hash);
1991-03-14 13:43:45 +00:00
size = cp - tg;
1987-01-05 17:20:13 +00:00
/* The tag tg with length size and known hash value hash is
looked up in the identifier table; if not found, it is
entered if cpy >= 0. A pointer to it is returned.
Notice that the chains of idf's are sorted alphabetically.
*/
1988-09-26 15:05:00 +00:00
hook = &IDF_hashtable[hash];
1987-01-05 17:20:13 +00:00
2016-09-17 22:23:42 +00:00
while ((notch = *hook))
{
register char* s1 = tg;
1987-01-05 17:20:13 +00:00
cp = notch->id_text;
2016-09-17 22:23:42 +00:00
while (!(c = (*s1 - *cp++)))
{
if (*s1++ == '\0')
{
1987-01-05 17:20:13 +00:00
break;
}
}
2016-09-17 22:23:42 +00:00
if (c == 0)
return notch;
if (c < 0)
break;
1987-08-06 14:20:11 +00:00
hook = &notch->id_next;
1987-01-05 17:20:13 +00:00
}
/* a new struct idf must be inserted at the hook */
2016-09-17 22:23:42 +00:00
if (cpy < 0)
return 0;
1988-09-26 15:05:00 +00:00
notch = IDF_new(tg, size, cpy);
1987-08-06 14:20:11 +00:00
notch->id_next = *hook;
2016-09-17 22:23:42 +00:00
*hook = notch; /* hooked in */
1987-01-05 17:20:13 +00:00
return notch;
}