forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsym.c
408 lines (361 loc) · 10.8 KB
/
sym.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "defs.h"
#include "data.h"
#include "decl.h"
// Symbol table functions
// Copyright (c) 2019 Warren Toomey, GPL3
// Append a node to the singly-linked list pointed to by head or tail
void appendsym(struct symtable **head, struct symtable **tail,
struct symtable *node) {
// Check for valid pointers
if (head == NULL || tail == NULL || node == NULL)
fatal("Either head, tail or node is NULL in appendsym");
// Append to the list
if (*tail) {
(*tail)->next = node;
*tail = node;
} else
*head = *tail = node;
node->next = NULL;
}
// Create a symbol node to be added to a symbol table list.
// Set up the node's:
// + type: char, int etc.
// + ctype: composite type pointer for struct/union
// + structural type: var, function, array etc.
// + size: number of elements, or endlabel: end label for a function
// + posn: Position information for local symbols
// Return a pointer to the new node
struct symtable *newsym(char *name, int type, struct symtable *ctype,
int stype, int class, int nelems, int posn) {
// Get a new node
struct symtable *node = (struct symtable *) malloc(sizeof(struct symtable));
if (node == NULL)
fatal("Unable to malloc a symbol table node in newsym");
// Fill in the values
if (name == NULL)
node->name = NULL;
else
node->name = strdup(name);
node->type = type;
node->ctype = ctype;
node->stype = stype;
node->class = class;
node->nelems = nelems;
// For pointers and integer types, set the size
// of the symbol. structs and union declarations
// manually set this up themselves.
if (ptrtype(type) || inttype(type))
node->size = nelems * typesize(type, ctype);
node->st_posn = posn;
node->next = NULL;
node->member = NULL;
node->initlist = NULL;
return (node);
}
// Add a symbol to the global symbol list
struct symtable *addglob(char *name, int type, struct symtable *ctype,
int stype, int class, int nelems, int posn) {
struct symtable *sym =
newsym(name, type, ctype, stype, class, nelems, posn);
// For structs and unions, copy the size from the type node
if (type == P_STRUCT || type == P_UNION)
sym->size = ctype->size;
appendsym(&Globhead, &Globtail, sym);
return (sym);
}
// Add a symbol to the local symbol list
struct symtable *addlocl(char *name, int type, struct symtable *ctype,
int stype, int nelems) {
struct symtable *sym = newsym(name, type, ctype, stype, C_LOCAL, nelems, 0);
// For structs and unions, copy the size from the type node
if (type == P_STRUCT || type == P_UNION)
sym->size = ctype->size;
appendsym(&Loclhead, &Locltail, sym);
return (sym);
}
// Add a symbol to the parameter list
struct symtable *addparm(char *name, int type, struct symtable *ctype,
int stype) {
struct symtable *sym = newsym(name, type, ctype, stype, C_PARAM, 1, 0);
appendsym(&Parmhead, &Parmtail, sym);
return (sym);
}
// Add a symbol to the temporary member list
struct symtable *addmemb(char *name, int type, struct symtable *ctype,
int stype, int nelems) {
struct symtable *sym =
newsym(name, type, ctype, stype, C_MEMBER, nelems, 0);
// For structs and unions, copy the size from the type node
if (type == P_STRUCT || type == P_UNION)
sym->size = ctype->size;
appendsym(&Membhead, &Membtail, sym);
return (sym);
}
// Add a struct to the struct list
struct symtable *addstruct(char *name) {
struct symtable *sym = newsym(name, P_STRUCT, NULL, 0, C_STRUCT, 0, 0);
appendsym(&Structhead, &Structtail, sym);
return (sym);
}
// Add a struct to the union list
struct symtable *addunion(char *name) {
struct symtable *sym = newsym(name, P_UNION, NULL, 0, C_UNION, 0, 0);
appendsym(&Unionhead, &Uniontail, sym);
return (sym);
}
// Add an enum type or value to the enum list.
// Class is C_ENUMTYPE or C_ENUMVAL.
// Use posn to store the int value.
struct symtable *addenum(char *name, int class, int value) {
struct symtable *sym = newsym(name, P_INT, NULL, 0, class, 0, value);
appendsym(&Enumhead, &Enumtail, sym);
return (sym);
}
// Add a typedef to the typedef list
struct symtable *addtypedef(char *name, int type, struct symtable *ctype) {
struct symtable *sym = newsym(name, type, ctype, 0, C_TYPEDEF, 0, 0);
appendsym(&Typehead, &Typetail, sym);
return (sym);
}
// Search for a symbol in a specific list.
// Return a pointer to the found node or NULL if not found.
// If class is not zero, also match on the given class
static struct symtable *findsyminlist(char *s, struct symtable *list,
int class) {
for (; list != NULL; list = list->next)
if ((list->name != NULL) && !strcmp(s, list->name))
if (class == 0 || class == list->class)
return (list);
return (NULL);
}
// Determine if the symbol s is in the global symbol table.
// Return a pointer to the found node or NULL if not found.
struct symtable *findglob(char *s) {
return (findsyminlist(s, Globhead, 0));
}
// Determine if the symbol s is in the local symbol table.
// Return a pointer to the found node or NULL if not found.
struct symtable *findlocl(char *s) {
struct symtable *node;
// Look for a parameter if we are in a function's body
if (Functionid) {
node = findsyminlist(s, Functionid->member, 0);
if (node)
return (node);
}
return (findsyminlist(s, Loclhead, 0));
}
// Determine if the symbol s is in the symbol table.
// Return a pointer to the found node or NULL if not found.
struct symtable *findsymbol(char *s) {
struct symtable *node;
// Look for a parameter if we are in a function's body
if (Functionid) {
node = findsyminlist(s, Functionid->member, 0);
if (node)
return (node);
}
// Otherwise, try the local and global symbol lists
node = findsyminlist(s, Loclhead, 0);
if (node)
return (node);
return (findsyminlist(s, Globhead, 0));
}
// Find a member in the member list
// Return a pointer to the found node or NULL if not found.
struct symtable *findmember(char *s) {
return (findsyminlist(s, Membhead, 0));
}
// Find a struct in the struct list
// Return a pointer to the found node or NULL if not found.
struct symtable *findstruct(char *s) {
return (findsyminlist(s, Structhead, 0));
}
// Find a struct in the union list
// Return a pointer to the found node or NULL if not found.
struct symtable *findunion(char *s) {
return (findsyminlist(s, Unionhead, 0));
}
// Find an enum type in the enum list
// Return a pointer to the found node or NULL if not found.
struct symtable *findenumtype(char *s) {
return (findsyminlist(s, Enumhead, C_ENUMTYPE));
}
// Find an enum value in the enum list
// Return a pointer to the found node or NULL if not found.
struct symtable *findenumval(char *s) {
return (findsyminlist(s, Enumhead, C_ENUMVAL));
}
// Find a type in the tyedef list
// Return a pointer to the found node or NULL if not found.
struct symtable *findtypedef(char *s) {
return (findsyminlist(s, Typehead, 0));
}
// Reset the contents of the symbol table
void clear_symtable(void) {
Globhead = Globtail = NULL;
Loclhead = Locltail = NULL;
Parmhead = Parmtail = NULL;
Membhead = Membtail = NULL;
Structhead = Structtail = NULL;
Unionhead = Uniontail = NULL;
Enumhead = Enumtail = NULL;
Typehead = Typetail = NULL;
}
// Clear all the entries in the local symbol table
void freeloclsyms(void) {
Loclhead = Locltail = NULL;
Parmhead = Parmtail = NULL;
Functionid = NULL;
}
// Remove all static symbols from the global symbol table
void freestaticsyms(void) {
// g points at current node, prev at the previous one
struct symtable *g, *prev = NULL;
// Walk the global table looking for static entries
for (g = Globhead; g != NULL; g = g->next) {
if (g->class == C_STATIC) {
// If there's a previous node, rearrange the prev pointer
// to skip over the current node. If not, g is the head,
// so do the same to Globhead
if (prev != NULL)
prev->next = g->next;
else
Globhead->next = g->next;
// If g is the tail, point Globtail at the previous node
// (if there is one), or Globhead
if (g == Globtail) {
if (prev != NULL)
Globtail = prev;
else
Globtail = Globhead;
}
}
}
// Point prev at g before we move up to the next node
prev = g;
}
// Dump a single symbol
static void dumpsym(struct symtable *sym, int indent) {
int i;
for (i = 0; i < indent; i++)
printf(" ");
switch (sym->type & (~0xf)) {
case P_VOID:
printf("void ");
break;
case P_CHAR:
printf("char ");
break;
case P_INT:
printf("int ");
break;
case P_LONG:
printf("long ");
break;
case P_STRUCT:
if (sym->ctype != NULL)
printf("struct %s ", sym->ctype->name);
else
printf("struct %s ", sym->name);
break;
case P_UNION:
if (sym->ctype != NULL)
printf("union %s ", sym->ctype->name);
else
printf("union %s ", sym->name);
break;
default:
printf("unknown type ");
}
for (i = 0; i < (sym->type & 0xf); i++)
printf("*");
printf("%s", sym->name);
switch (sym->stype) {
case S_VARIABLE:
break;
case S_FUNCTION:
printf("()");
break;
case S_ARRAY:
printf("[]");
break;
default:
printf(" unknown stype");
}
switch (sym->class) {
case C_GLOBAL:
printf(": global");
break;
case C_LOCAL:
printf(": local");
break;
case C_PARAM:
printf(": param");
break;
case C_EXTERN:
printf(": extern");
break;
case C_STATIC:
printf(": static");
break;
case C_STRUCT:
printf(": struct");
break;
case C_UNION:
printf(": union");
break;
case C_MEMBER:
printf(": member");
break;
case C_ENUMTYPE:
printf(": enumtype");
break;
case C_ENUMVAL:
printf(": enumval");
break;
case C_TYPEDEF:
printf(": typedef");
break;
default:
printf(": unknown class");
}
switch (sym->stype) {
case S_VARIABLE:
if (sym->class == C_ENUMVAL)
printf(", value %d\n", sym->st_posn);
else
printf(", size %d\n", sym->size);
break;
case S_FUNCTION:
printf(", %d params\n", sym->nelems);
break;
case S_ARRAY:
printf(", %d elems, size %d\n", sym->nelems, sym->size);
break;
}
switch (sym->type & (~0xf)) {
case P_STRUCT:
case P_UNION:
dumptable(sym->member, NULL, 4);
}
switch (sym->stype) {
case S_FUNCTION:
dumptable(sym->member, NULL, 4);
}
}
// Dump one symbol table
void dumptable(struct symtable *head, char *name, int indent) {
struct symtable *sym;
if (head != NULL && name != NULL)
printf("%s\n--------\n", name);
for (sym = head; sym != NULL; sym = sym->next)
dumpsym(sym, indent);
}
void dumpsymtables(void) {
dumptable(Globhead, "Global", 0);
printf("\n");
dumptable(Enumhead, "Enums", 0);
printf("\n");
dumptable(Typehead, "Typedefs", 0);
}