-
Notifications
You must be signed in to change notification settings - Fork 0
/
sym.c
204 lines (166 loc) · 5.6 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
#include "defs.h"
#include "data.h"
#include "decl.h"
static void appendsym(struct symtable **head, struct symtable **tail,
struct symtable *node) {
if (head == NULL || tail == NULL || node == NULL)
fatal("either head, tail or node is NULL in appendsym()");
if (*tail) {
(*tail)->next = node;
*tail = node;
} else
*head = *tail = node;
node->next = NULL;
}
static struct symtable *newsym(char *name, int type, struct symtable *ctype,
int stype, int class, int nelems, int posn) {
struct symtable *node = (struct symtable *)malloc(sizeof(struct symtable));
if (node == NULL)
fatal("unable to malloc a symbol table node in newsym()");
node->name = strdup(name);
node->type = type;
node->ctype = ctype;
node->stype = stype;
node->class = class;
node->nelems = nelems;
node->posn = posn;
if (ptrtype(type) || inttype(type))
node->size = nelems * typesize(type, ctype);
node->initlist = NULL;
node->next = NULL;
node->member = NULL;
return (node);
}
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);
if (type == P_STRUCT || type == P_UNION)
sym->size = ctype->size;
appendsym(&Globhead, &Globtail, sym);
return (sym);
}
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);
if (type == P_STRUCT || type == P_UNION)
sym->size = ctype->size;
appendsym(&Loclhead, &Locltail, sym);
return (sym);
}
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);
}
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);
if (type == P_STRUCT || type == P_UNION)
sym->size = ctype->size;
appendsym(&Membhead, &Membtail, sym);
return (sym);
}
struct symtable *addstruct(char *name) {
struct symtable *sym = newsym(name, P_STRUCT, NULL, 0, C_STRUCT, 0, 0);
appendsym(&Structhead, &Structtail, sym);
return (sym);
}
struct symtable *addunion(char *name) {
struct symtable *sym = newsym(name, P_UNION, NULL, 0, C_UNION, 0, 0);
appendsym(&Unionhead, &Uniontail, sym);
return (sym);
}
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);
}
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);
}
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);
}
struct symtable *findglob(char *s) {
return (findsyminlist(s, Globhead, 0));
}
struct symtable *findlocl(char *s) {
struct symtable *node;
if (Functionid) {
node = findsyminlist(s, Functionid->member, 0);
if (node)
return (node);
}
return (findsyminlist(s, Loclhead, 0));
}
struct symtable *findsymbol(char *s) {
struct symtable *node;
if (Functionid) {
node = findsyminlist(s, Functionid->member, 0);
if (node)
return (node);
}
node = findsyminlist(s, Loclhead, 0);
if (node)
return (node);
return (findsyminlist(s, Globhead, 0));
}
struct symtable *findmember(char *s) {
return (findsyminlist(s, Membhead, 0));
}
struct symtable *findstruct(char *s) {
return (findsyminlist(s, Structhead, 0));
}
struct symtable *findunion(char *s) {
return (findsyminlist(s, Unionhead, 0));
}
struct symtable *findenumtype(char *s) {
return (findsyminlist(s, Enumhead, C_ENUMTYPE));
}
struct symtable *findenumval(char *s) {
return (findsyminlist(s, Enumhead, C_ENUMVAL));
}
struct symtable *findtypedef(char *s) {
return (findsyminlist(s, Typehead, 0));
}
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;
}
void freeloclsyms(void) {
Loclhead = Locltail = NULL;
Functionid = NULL;
}
void freestaticsyms(void) {
struct symtable *g;
if (Globhead == NULL)
return;
g = ((struct symtable *)malloc(sizeof(struct symtable)));
g->next = Globhead;
while (g->next != NULL) {
if (g->next->class == C_STATIC) {
if (g->next == Globhead) {
Globhead = Globhead->next;
}
g->next = g->next->next;
if (g->next == NULL) {
Globtail = g;
}
} else {
g = g->next;
}
}
}