|
|
1.1 root 1: /*
2: * C compiler.
3: * Final symbol table manager.
4: */
5: #ifdef vax
6: #include "INC$LIB:cc2.h"
7: #else
8: #include "cc2.h"
9: #endif
10:
11: /*
12: * Lookup a global (or static external) identifier in the symbol table.
13: * Return a pointer to the symbol union.
14: * If not there and the 'flag' is true,
15: * define the symbol at the current location.
16: */
17: SYM *
18: glookup(id, def)
19: char *id;
20: {
21: register SYM *sp;
22: register int h;
23:
24: sp = hash2[h = symbucket(id)];
25: while (sp != NULL) {
26: if ((sp->s_flag&S_LABNO)==0 && strcmp(id, sp->s_id)==0) {
27: if (def != 0) {
28: if ((sp->s_flag&S_DEF) != 0)
29: cbotch("global symbol \"%s\" redefined", sp->s_id);
30: sp->s_flag |= S_DEF;
31: }
32: return (sp);
33: }
34: sp = sp->s_fp;
35: }
36: if ((sp = (SYM *)malloc(sizeof(SYM)+strlen(id)+1)) == NULL)
37: cnomem("glookup");
38: sp->s_fp = hash2[h];
39: hash2[h] = sp;
40: sp->s_flag = 0;
41: sp->s_seg = SANY;
42: sp->s_value = 0;
43: sp->s_data = 0;
44: sp->s_type = 0;
45: if (def != 0)
46: sp->s_flag = S_DEF;
47: strcpy(sp->s_id, id);
48: return (sp);
49: }
50:
51: /*
52: * Lookup a local symbol.
53: * If the symbol is not there and the 'flag' is true,
54: * then define the symbol at the current location.
55: * Return a pointer to the symbol union.
56: */
57: SYM *
58: llookup(labno, def)
59: {
60: register SYM *sp;
61: register int h;
62:
63: sp = hash2[h = labno&SHMASK];
64: while (sp != NULL) {
65: if ((sp->s_flag&S_LABNO)!=0 && labno==sp->s_labno) {
66: if (def != 0) {
67: if ((sp->s_flag&S_DEF) != 0)
68: cbotch("local symbol \"L%d\" redefined", labno);
69: sp->s_flag |= S_DEF;
70: }
71: return (sp);
72: }
73: sp = sp->s_fp;
74: }
75: if ((sp = (SYM *) malloc(sizeof(SYM))) == NULL)
76: cnomem("llookup");
77: sp->s_fp = hash2[h];
78: hash2[h] = sp;
79: sp->s_flag = S_LABNO;
80: sp->s_seg = SANY;
81: sp->s_value = 0;
82: if (def != 0)
83: sp->s_flag = S_LABNO|S_DEF;
84: sp->s_labno = labno;
85: return (sp);
86: }
87:
88: /*
89: * Given an ASCII global or static external symbol name,
90: * figure out what hash bucket it goes in.
91: */
92: static
93: symbucket(p)
94: register char *p;
95: {
96: register int c, h;
97:
98: h = 0;
99: while ((c = *p++) != 0)
100: h += c;
101: return (h&SHMASK);
102: }
103:
104: #if OVERLAID
105: /*
106: * Run through the hash table,
107: * releasing all of the symbol table nodes and
108: * setting all of the hash table bases to NULL pointers.
109: * This frees up the space used by the
110: * optimizer phase if overlaid, and all makes sure
111: * that the hash table is clear if this is the
112: * second file in a monolithic compilation.
113: */
114: freesym2()
115: {
116: register SYM *sp1;
117: register SYM *sp2;
118: register int i;
119:
120: for (i=0; i<NSHASH; ++i) {
121: sp1 = hash2[i];
122: while (sp1 != NULL) {
123: sp2 = sp1->s_fp;
124: free((char *) sp1);
125: sp1 = sp2;
126: }
127: hash2[i] = NULL;
128: }
129: }
130: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.