forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsym.c
235 lines (204 loc) · 7.22 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
#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
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->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;
}