BSD 3 development
[unix-history] / usr / src / cmd / struct / 1.hash.c
CommitLineData
42d6e430
BJ
1#include <stdio.h>
2#include "1.incl.h"
3#include "1.defs.h"
4#include"def.h"
5
6extern int match[], symclass[], action[], newstate[];
7extern char symbol[];
8long *hashtab;
9int *value, *chain;
10
11extern FILE *infd;
12
13
14parse()
15 {int i,j,found,current, someread;
16 char c;
17
18 hash_init();
19 routinit();
20 line_init();
21
22 someread = 0; /* indicates haven't read part of a routine */
23
24 empseek(0);
25 endbuf = getline(&endline, &endchar, &endcom, & comchar);
26 if (progress && endbuf != -1) fprintf(stderr,"parsing\n");
27 while(endbuf != -1) /* getline returns -1 when no more input */
28 {
29 someread = 1;
30 if (progress > 0)
31 {
32 for (i = begline; i <= endline; i++)
33 if (!(i % progress)) fprintf(stderr,"parsing line %d\n",i);
34 }
35 current = 0;
36 for (i = 0; i < endbuf; i++)
37 {
38
39 c = buffer[i];
40 if(c != '~')
41 {
42 found = 0;
43 if ( (current < 0 || current >= snum) && current != ABORT)
44 {
45 strerr("in parsing:","","");
46 fprintf(stderr,"line %d of file, parser in invalid state", begline,current);
47 fprintf(stderr,"treating it as straight line code\n");
48 current = ABORT;
49 }
50 else
51 for (j = match[current]; j < match[current + 1]; j++)
52 {
53 if ((symclass[j] == 0 && c == symbol[j]) ||
54 (symclass[j] != 0 && classmatch(c,symclass[j]) ))
55 {found = 1; break;
56 }
57 }
58 if (!found)
59 {
60 error("in syntax:","","");
61 fprintf(stderr,"between lines %d and %d of file\n",begline, endline);
62 if (debug)
63 fprintf(stderr,"symbol '%c' does not match entries for state %d\n",c,current);
64 fprintf(stderr,"treating it as straight line code\n");
65 current = ABORT;
66 }
67 else if (!action[j])
68 current = newstate[j];
69 else
70 {
71 current = act(action[j],c,i);
72 if (current == nulls) current = newstate[j];
73 }
74 if (current == ABORT) break;
75 if (current == endrt)
76 {
77 return(1);
78 }
79 }
80 }
81 line_init();
82 endbuf = getline(&endline, &endchar, &endcom,&comchar);
83 }
84 if (someread) return(1);
85 else return(0);
86 }
87
88
89hash_init()
90 {
91 int i;
92 hashtab = challoc(sizeof(*hashtab) * maxhash);
93 chain = challoc(sizeof(*chain) * maxhash);
94 value = challoc(sizeof(*value) * maxhash);
95 for (i = 0; i < maxhash; i++)
96 {
97 hashtab[i] = -1L;
98 value[i] = -2;
99 chain[i] = 0;
100 }
101 }
102
103
104hash_check()
105 {
106 int i;
107 for (i = 0; i < maxhash; ++i)
108 if (value[i] == -2 && hashtab[i] != -1L)
109 {
110 error("in syntax; label used but does not appear as statement label:","","");
111 fprintf(stderr,"%D\n",hashtab[i]);
112 routerr = 1;
113 }
114 }
115
116hash_free()
117 {
118 chfree(hashtab,sizeof(*hashtab) * maxhash);
119 hashtab = 0;
120 chfree(chain,sizeof(*chain) * maxhash);
121 chain = 0;
122 chfree(value,sizeof(*value) * maxhash);
123 value = 0;
124 }
125hash(x)
126long x;
127 {
128 int quo, rem, hcount, temp;
129
130 ASSERT(x >= 0L, hash);
131 quo = x/maxhash;
132 rem = x - (quo * maxhash);
133 if (quo == 0) quo = 1;
134
135 temp = rem;
136 for (hcount=0; (hashtab[temp] != -1L) && (hashtab[temp] != x) && (hcount<maxhash); hcount++)
137 temp = (temp + quo)%maxhash;
138 if(hcount>=maxhash) faterr("hash table overflow - too many labels","","");
139 hashtab[temp] = x;
140 return(temp);
141 }
142
143addref(x,ptr) /* put ptr in chain for x or assign value of x to *ptr */
144long x;
145int *ptr;
146 {
147 int index;
148 index = hash(x);
149
150 if (value[index] == -1)
151 { /* x already assigned value */
152 *ptr = chain[index];
153 return;
154 }
155
156 /* add ptr to chain */
157
158 if (chain[index] == 0)
159 *ptr = 0;
160 else
161 *ptr = chain[index];
162 chain[index] = ptr;
163 }
164
165fixvalue (x,ptr)
166long x;
167int ptr;
168 {
169 int *temp1, *temp2, index, temp0;
170 index = hash(x);
171
172 while (index != -2)
173 { /* trace chain of linked labels */
174
175 if (value[index] == -1)
176 {
177 error("in syntax: ","","");
178 fprintf(stderr,"attempt to redefine value of label %D between lines %d and %d\n",
179 x,begline,endline);
180 routerr = 1;
181 return;
182 }
183
184 temp1 = &chain[index]; /* trace chain for each label */
185 while (temp1 != 0)
186 {
187 temp2 = *temp1;
188 *temp1 = ptr;
189 temp1 = temp2;
190 }
191 temp0 = index;
192 index = value[index];
193 value[temp0] = -1;
194 }
195 }
196
197connect(x,y)
198long x,y;
199 {
200 int *temp, index, temp2;
201 index = hash(x);
202
203 if (value[index] == -1)
204 fixvalue(y, chain[index]);
205 else
206 {
207 if (y == implicit)
208 { /* attach implicit chain to x chain */
209 temp = &chain[index];
210
211 while (*temp != 0)
212 temp = *temp;
213
214 *temp = chain[hash(y)];
215 }
216 temp2 = index; /* attach y linked labels to x linked labels */
217 while (value[temp2] >= 0)
218 temp2 = value[temp2];
219 if (y == implicit)
220 value[temp2] = value[hash(y)];
221 else
222 value[temp2] = hash(y);
223 }
224 if (y == implicit) clear(y);
225 }
226
227
228clear(x)
229long x;
230 {
231 int index;
232 index = hash(x);
233 value[index] = -2;
234 chain[index] = 0;
235 hashtab[index] = -1L;
236 }
237
238