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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* tsym.c -- functions for symbol table management.
*/
#include "../h/gsupport.h"
#include "tproto.h"
#include "tglobals.h"
#include "ttoken.h"
#include "tsym.h"
#include "keyword.h"
#include "lfile.h"
/*
* Prototypes.
*/
static struct tgentry *alcglob
(struct tgentry *blink, char *name,int flag,int nargs);
static struct tcentry *alclit
(struct tcentry *blink, char *name, int len,int flag);
static struct tlentry *alcloc
(struct tlentry *blink, char *name,int flag);
static struct tcentry *clookup (char *id,int flag);
static struct tgentry *glookup (char *id);
static struct tlentry *llookup (char *id);
static void putglob
(char *id,int id_type, int n_args);
/*
* Keyword table.
*/
struct keyent {
char *keyname;
int keyid;
};
#define KDef(p,n) Lit(p), n,
static struct keyent keytab[] = {
#include "../h/kdefs.h"
NULL, -1
};
/*
* loc_init - clear the local and constant symbol tables.
*/
void loc_init()
{
struct tlentry *lptr, *lptr1;
struct tcentry *cptr, *cptr1;
int i;
/*
* Clear local table, freeing entries.
*/
for (i = 0; i < lhsize; i++) {
for (lptr = lhash[i]; lptr != NULL; lptr = lptr1) {
lptr1 = lptr->l_blink;
free((char *)lptr);
}
lhash[i] = NULL;
}
lfirst = NULL;
llast = NULL;
/*
* Clear constant table, freeing entries.
*/
for (i = 0; i < lchsize; i++) {
for (cptr = chash[i]; cptr != NULL; cptr = cptr1) {
cptr1 = cptr->c_blink;
free((char *)cptr);
}
chash[i] = NULL;
}
cfirst = NULL;
clast = NULL;
}
/*
* install - put an identifier into the global or local symbol table.
* The basic idea here is to look in the right table and install
* the identifier if it isn't already there. Some semantic checks
* are performed.
*/
void install(name, flag, argcnt)
char *name;
int flag, argcnt;
{
union {
struct tgentry *gp;
struct tlentry *lp;
} p;
switch (flag) {
case F_Global: /* a variable in a global declaration */
if ((p.gp = glookup(name)) == NULL)
putglob(name, flag, argcnt);
else
p.gp->g_flag |= flag;
break;
case F_Proc|F_Global: /* procedure declaration */
case F_Record|F_Global: /* record declaration */
case F_Builtin|F_Global: /* external declaration */
if ((p.gp = glookup(name)) == NULL)
putglob(name, flag, argcnt);
else if ((p.gp->g_flag & (~F_Global)) == 0) { /* superfluous global
declaration for
record or proc */
p.gp->g_flag |= flag;
p.gp->g_nargs = argcnt;
}
else /* the user can't make up his mind */
tfatal("inconsistent redeclaration", name);
break;
case F_Static: /* static declaration */
case F_Dynamic: /* local declaration (possibly implicit?) */
case F_Argument: /* formal parameter */
if ((p.lp = llookup(name)) == NULL)
putloc(name,flag);
else if (p.lp->l_flag == flag) /* previously declared as same type */
tfatal("redeclared identifier", name);
else /* previously declared as different type */
tfatal("inconsistent redeclaration", name);
break;
default:
tsyserr("install: unrecognized symbol table flag.");
}
}
/*
* putloc - make a local symbol table entry and return the index
* of the entry in lhash. alcloc does the work if there is a collision.
*/
int putloc(id,id_type)
char *id;
int id_type;
{
register struct tlentry *ptr;
if ((ptr = llookup(id)) == NULL) { /* add to head of hash chain */
ptr = lhash[lhasher(id)];
lhash[lhasher(id)] = alcloc(ptr, id, id_type);
return lhash[lhasher(id)]->l_index;
}
return ptr->l_index;
}
/*
* putglob makes a global symbol table entry. alcglob does the work if there
* is a collision.
*/
static void putglob(id, id_type, n_args)
char *id;
int id_type, n_args;
{
register struct tgentry *ptr;
if ((ptr = glookup(id)) == NULL) { /* add to head of hash chain */
ptr = ghash[ghasher(id)];
ghash[ghasher(id)] = alcglob(ptr, id, id_type, n_args);
}
}
/*
* putlit makes a constant symbol table entry and returns the table "index"
* of the constant. alclit does the work if there is a collision.
*/
int putlit(id, idtype, len)
char *id;
int len, idtype;
{
register struct tcentry *ptr;
if ((ptr = clookup(id,idtype)) == NULL) { /* add to head of hash chain */
ptr = chash[chasher(id)];
chash[chasher(id)] = alclit(ptr, id, len, idtype);
return chash[chasher(id)]->c_index;
}
return ptr->c_index;
}
/*
* llookup looks up id in local symbol table and returns pointer to
* to it if found or NULL if not present.
*/
static struct tlentry *llookup(id)
char *id;
{
register struct tlentry *ptr;
ptr = lhash[lhasher(id)];
while (ptr != NULL && ptr->l_name != id)
ptr = ptr->l_blink;
return ptr;
}
/*
* glookup looks up id in global symbol table and returns pointer to
* to it if found or NULL if not present.
*/
static struct tgentry *glookup(id)
char *id;
{
register struct tgentry *ptr;
ptr = ghash[ghasher(id)];
while (ptr != NULL && ptr->g_name != id) {
ptr = ptr->g_blink;
}
return ptr;
}
/*
* clookup looks up id in constant symbol table and returns pointer to
* to it if found or NULL if not present.
*/
static struct tcentry *clookup(id,flag)
char *id;
int flag;
{
register struct tcentry *ptr;
ptr = chash[chasher(id)];
while (ptr != NULL && (ptr->c_name != id || ptr->c_flag != flag))
ptr = ptr->c_blink;
return ptr;
}
/*
* klookup looks up keyword named by id in keyword table and returns
* its number (keyid).
*/
int klookup(id)
register char *id;
{
register struct keyent *kp;
for (kp = keytab; kp->keyid >= 0; kp++)
if (strcmp(kp->keyname,id) == 0)
return (kp->keyid);
return 0;
}
/*
* alcloc allocates a local symbol table entry, fills in fields with
* specified values and returns the new entry.
*/
static struct tlentry *alcloc(blink, name, flag)
struct tlentry *blink;
char *name;
int flag;
{
register struct tlentry *lp;
lp = NewStruct(tlentry);
lp->l_blink = blink;
lp->l_name = name;
lp->l_flag = flag;
lp->l_next = NULL;
if (lfirst == NULL) {
lfirst = lp;
lp->l_index = 0;
}
else {
llast->l_next = lp;
lp->l_index = llast->l_index + 1;
}
llast = lp;
return lp;
}
/*
* alcglob allocates a global symbol table entry, fills in fields with
* specified values and returns offset of new entry.
*/
static struct tgentry *alcglob(blink, name, flag, nargs)
struct tgentry *blink;
char *name;
int flag, nargs;
{
register struct tgentry *gp;
gp = NewStruct(tgentry);
gp->g_blink = blink;
gp->g_name = name;
gp->g_flag = flag;
gp->g_nargs = nargs;
gp->g_next = NULL;
if (gfirst == NULL) {
gfirst = gp;
gp->g_index = 0;
}
else {
glast->g_next = gp;
gp->g_index = glast->g_index + 1;
}
glast = gp;
return gp;
}
/*
* alclit allocates a constant symbol table entry, fills in fields with
* specified values and returns the new entry.
*/
static struct tcentry *alclit(blink, name, len, flag)
struct tcentry *blink;
char *name;
int len, flag;
{
register struct tcentry *cp;
cp = NewStruct(tcentry);
cp->c_blink = blink;
cp->c_name = name;
cp->c_length = len;
cp->c_flag = flag;
cp->c_next = NULL;
if (cfirst == NULL) {
cfirst = cp;
cp->c_index = 0;
}
else {
clast->c_next = cp;
cp->c_index = clast->c_index + 1;
}
clast = cp;
return cp;
}
/*
* lout dumps local symbol table to fd, which is a .u1 file.
*/
void lout(fd)
FILE *fd;
{
register struct tlentry *lp;
for (lp = lfirst; lp != NULL; lp = lp->l_next)
writecheck(fprintf(fd, "\tlocal\t%d,%06o,%s\n",
lp->l_index, lp->l_flag, lp->l_name));
}
/*
* constout dumps constant symbol table to fd, which is a .u1 file.
*/
void constout(fd)
FILE *fd;
{
register int l;
register char *c;
register struct tcentry *cp;
for (cp = cfirst; cp != NULL; cp = cp->c_next) {
writecheck(fprintf(fd, "\tcon\t%d,%06o", cp->c_index, cp->c_flag));
if (cp->c_flag & F_IntLit)
writecheck(fprintf(fd,",%d,%s\n",(int)strlen(cp->c_name),cp->c_name));
else if (cp->c_flag & F_RealLit)
writecheck(fprintf(fd, ",%s\n", cp->c_name));
else {
c = cp->c_name;
l = cp->c_length;
writecheck(fprintf(fd, ",%d", l));
while (l--)
writecheck(fprintf(fd, ",%03o", *c++ & 0377));
writecheck(putc('\n', fd));
}
}
}
/*
* rout dumps a record declaration for name to file fd, which is a .u2 file.
*/
void rout(fd,name)
FILE *fd;
char *name;
{
register struct tlentry *lp;
int n;
if (llast == NULL)
n = 0;
else
n = llast->l_index + 1;
writecheck(fprintf(fd, "record\t%s,%d\n", name, n));
for (lp = lfirst; lp != NULL; lp = lp->l_next)
writecheck(fprintf(fd, "\t%d,%s\n", lp->l_index, lp->l_name));
}
/*
* gout writes various items to fd, which is a .u2 file. These items
* include: implicit status, tracing activation, link directives,
* invocable directives, and the global table.
*/
void gout(fd)
FILE *fd;
{
register char *name;
register struct tgentry *gp;
int n;
struct lfile *lfl;
struct invkl *ivl;
if (uwarn)
name = "error";
else
name = "local";
writecheck(fprintf(fd, "impl\t%s\n", name));
if (trace)
writecheck(fprintf(fd, "trace\n"));
lfl = lfiles;
while (lfl) {
writecheck(fprintf(fd,"link\t%s.u1\n",lfl->lf_name));
lfl = lfl->lf_link;
}
lfiles = 0;
for (ivl = invkls; ivl != NULL; ivl = ivl->iv_link)
writecheck(fprintf(fd, "invocable\t%s\n", ivl->iv_name));
invkls = NULL;
if (glast == NULL)
n = 0;
else
n = glast->g_index + 1;
writecheck(fprintf(fd, "global\t%d\n", n));
for (gp = gfirst; gp != NULL; gp = gp->g_next)
writecheck(fprintf(fd, "\t%d,%06o,%s,%d\n", gp->g_index, gp->g_flag,
gp->g_name, gp->g_nargs));
}
|