-
Notifications
You must be signed in to change notification settings - Fork 1
/
CirEnv.c
305 lines (259 loc) · 6.99 KB
/
CirEnv.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
#include "cir_internal.h"
#include <assert.h>
#define TABLE_SIZE 503
#define GLOBAL_TABLE_SIZE 5303
//#define TABLE_SIZE 2111
typedef struct NameItem {
CirName key;
enum {
NAME_ITEM_VAR,
NAME_ITEM_TYPEDEF,
NAME_ITEM_ENUM
} type;
union {
CirVarId varId;
CirTypedefId typedefId;
CirEnumItemId enumItemId;
} value;
CirVarId varId;
} NameItem;
typedef struct TagItem {
CirName key;
bool isEnum;
union {
CirCompId compId;
CirEnumId enumId;
} u;
} TagItem;
typedef struct Scope {
NameItem *names;
TagItem *tags;
size_t tableSize;
} Scope;
static Scope scopes[CIR_MAX_SCOPES];
static uint32_t scopeStackTop;
static const NameItem *
findNameItem(const Scope *scope, CirName name)
{
size_t tableSize = scope->tableSize;
for (uint32_t i = name % tableSize; scope->names[i].key; i = (i + 1) % tableSize) {
if (scope->names[i].key == name) {
return &scope->names[i];
}
}
return NULL;
}
static void
replaceNameItem(Scope *scope, const NameItem *item)
{
uint32_t i;
size_t tableSize = scope->tableSize;
for (i = item->key % tableSize; scope->names[i].key && scope->names[i].key != item->key; i = (i + 1) % tableSize);
scope->names[i] = *item;
}
static const TagItem *
findTagItem(const Scope *scope, CirName name)
{
size_t tableSize = scope->tableSize;
for (uint32_t i = name % tableSize; scope->tags[i].key; i = (i + 1) % tableSize) {
if (scope->tags[i].key == name) {
return &scope->tags[i];
}
}
return NULL;
}
static void
replaceTagItem(Scope *scope, const TagItem *item)
{
uint32_t i;
size_t tableSize = scope->tableSize;
for (i = item->key % tableSize; scope->tags[i].key && scope->tags[i].key != item->key; i = (i + 1) % tableSize);
scope->tags[i] = *item;
}
void
CirEnv__pushScope(size_t tableSize)
{
if (scopeStackTop >= CIR_MAX_SCOPES)
cir_fatal("too many nested scopes");
scopes[scopeStackTop].names = cir__zalloc(sizeof(NameItem) * tableSize);
scopes[scopeStackTop].tags = cir__zalloc(sizeof(TagItem) * tableSize);
scopes[scopeStackTop].tableSize = tableSize;
scopeStackTop++;
}
void
CirEnv__pushGlobalScope(void)
{
CirEnv__pushScope(GLOBAL_TABLE_SIZE);
}
void
CirEnv__pushLocalScope(void)
{
CirEnv__pushScope(TABLE_SIZE);
}
void
CirEnv__popScope(void)
{
if (!scopeStackTop)
cir_fatal("no more scopes to pop");
scopeStackTop--;
cir__xfree(scopes[scopeStackTop].names);
cir__xfree(scopes[scopeStackTop].tags);
}
bool
CirEnv__isGlobal(void)
{
return scopeStackTop <= 1;
}
int
CirEnv__findLocalName(CirName name, CirVarId *varId, CirTypedefId *typedefId, CirEnumItemId *enumItemId)
{
for (uint32_t i = 0; i < scopeStackTop; i++) {
const NameItem *nameItem = findNameItem(&scopes[scopeStackTop - i - 1], name);
if (nameItem) {
switch (nameItem->type) {
case NAME_ITEM_VAR:
*varId = nameItem->value.varId;
return 1;
case NAME_ITEM_TYPEDEF:
*typedefId = nameItem->value.typedefId;
return 2;
case NAME_ITEM_ENUM:
*enumItemId = nameItem->value.enumItemId;
return 3;
default:
cir_bug("unreachable");
}
}
}
return 0;
}
int
CirEnv__findGlobalName(CirName name, CirVarId *varId, CirTypedefId *typedefId, CirEnumItemId *enumItemId)
{
if (!scopeStackTop)
cir_bug("No global scope present");
const NameItem *nameItem = findNameItem(&scopes[0], name);
if (!nameItem)
return 0;
switch (nameItem->type) {
case NAME_ITEM_VAR:
*varId = nameItem->value.varId;
return 1;
case NAME_ITEM_TYPEDEF:
*typedefId = nameItem->value.typedefId;
return 2;
case NAME_ITEM_ENUM:
*enumItemId = nameItem->value.enumItemId;
return 3;
default:
cir_bug("unreachable");
}
}
int
CirEnv__findCurrentScopeName(CirName name, CirVarId *varId, CirTypedefId *typedefId, CirEnumItemId *enumItemId)
{
if (!scopeStackTop)
cir_bug("No current scope present");
const NameItem *nameItem = findNameItem(&scopes[scopeStackTop - 1], name);
if (!nameItem)
return 0;
switch (nameItem->type) {
case NAME_ITEM_VAR:
*varId = nameItem->value.varId;
return 1;
case NAME_ITEM_TYPEDEF:
*typedefId = nameItem->value.typedefId;
return 2;
case NAME_ITEM_ENUM:
*enumItemId = nameItem->value.enumItemId;
return 3;
default:
cir_bug("unreachable");
}
}
void
CirEnv__setLocalNameAsVar(CirVarId vid)
{
if (!scopeStackTop)
cir_bug("No current scope present");
CirName name = CirVar_getName(vid);
if (!name)
cir_bug("Var has no name!");
NameItem nameItem;
nameItem.key = name;
nameItem.type = NAME_ITEM_VAR;
nameItem.value.varId = vid;
replaceNameItem(&scopes[scopeStackTop - 1], &nameItem);
}
void
CirEnv__setLocalNameAsTypedef(CirTypedefId tid)
{
if (!scopeStackTop)
cir_bug("No current scope present");
CirName name = CirTypedef_getName(tid);
if (!name)
cir_bug("Typedef has no name!");
NameItem nameItem;
nameItem.key = name;
nameItem.type = NAME_ITEM_TYPEDEF;
nameItem.value.typedefId = tid;
replaceNameItem(&scopes[scopeStackTop - 1], &nameItem);
}
void
CirEnv__setLocalNameAsEnumItem(CirEnumItemId enumItemId)
{
if (!scopeStackTop)
cir_bug("No current scope present");
CirName name = CirEnumItem_getName(enumItemId);
assert(name);
NameItem nameItem;
nameItem.key = name;
nameItem.type = NAME_ITEM_ENUM;
nameItem.value.enumItemId = enumItemId;
replaceNameItem(&scopes[scopeStackTop - 1], &nameItem);
}
int
CirEnv__findLocalTag(CirName name, CirCompId *cid, CirEnumId *enumId)
{
for (uint32_t i = 0; i < scopeStackTop; i++) {
const TagItem *tagItem = findTagItem(&scopes[scopeStackTop - i - 1], name);
if (!tagItem)
continue;
if (tagItem->isEnum) {
*enumId = tagItem->u.enumId;
return 2;
} else {
*cid = tagItem->u.compId;
return 1;
}
}
return 0;
}
void
CirEnv__setLocalTagAsComp(CirCompId cid)
{
if (!scopeStackTop)
cir_bug("no current scope present");
CirName name = CirComp_getName(cid);
if (!name)
cir_bug("CirComp has no name!");
TagItem tagItem;
tagItem.key = name;
tagItem.isEnum = false;
tagItem.u.compId = cid;
replaceTagItem(&scopes[scopeStackTop - 1], &tagItem);
}
void
CirEnv__setLocalTagAsEnum(CirEnumId enumId)
{
if (!scopeStackTop)
cir_bug("no current scope present");
CirName name = CirEnum_getName(enumId);
if (!name)
cir_bug("CirEnum has no name!");
TagItem tagItem;
tagItem.key = name;
tagItem.isEnum = true;
tagItem.u.enumId = enumId;
replaceTagItem(&scopes[scopeStackTop - 1], &tagItem);
}