-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecl.c
315 lines (286 loc) · 4.91 KB
/
decl.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
#include "yo.h"
Decl *nildecl;
Decl *truedecl;
Decl *falsedecl;
Decl *_decl;
static Decl *scopes[MaxScope] = {0};
static Decl *tails[MaxScope] = {0};
static Node *scopenode[MaxScope] = {0};
static int scopekind[MaxScope] = {0};
static int scope;
static void freeloc(Decl *d);
static void recdecl(Node *n ,int store, int *id);
static int recmark(Node *n, int id);
void
declstart(void)
{
scope = ScopeNil;
scopes[scope] = nil;
tails[scope] = nil;
nildecl = mkdecl(Dglobal, tany);
nildecl->sym = enter("nil", 0);
installids(Dglobal, nildecl);
_decl = mkdecl(Dglobal, tany);
_decl->sym = enter("_", Lid);
installids(Dglobal, _decl);
truedecl = mkdecl(Dglobal, tbool);
truedecl->sym = enter("true", Lconst);
installids(Dglobal, truedecl);
falsedecl = mkdecl(Dglobal, tbool);
falsedecl->sym = enter("false", Lconst);
installids(Dglobal, falsedecl);
scope = ScopeGlobal;
scopes[scope] = nil;
tails[scope] = nil;
}
Decl*
popids(Decl *d)
{
for(; d; d=d->next){
if(d->sym){
d->sym->decl = d->old;
d->old = nil;
}
}
return popscope();
}
void
repushids(Decl *d)
{
assert(scope < MaxScope);
scope++;
scopes[scope] = nil;
tails[scope] = nil;
scopenode[scope] = nil;
scopekind[scope] = Sother;
for(; d; d=d->next){
if(d->scope != scope
&& (d->scope != ScopeGlobal||scope != ScopeGlobal +1))
assert(0);
Sym *s = d->sym;
if(s != nil){
if(s->decl != nil && s->decl->scope >= scope)
d->old = s->decl->old;
else
d->old = s->decl;
s->decl = d;
}
}
}
void
pushscope(Node *scp, int kind)
{
assert(scope+1 < MaxScope);
scope += 1;
scopes[scope] = nil;
tails[scope] = nil;
scopenode[scope] = scp;
scopekind[scope] = kind;
}
Decl*
popscope(void)
{
for(Decl *p=scopes[scope]; p; p=p->next){
if(p->sym){
p->sym->decl = p->old;
p->old = nil;
}
if(p->store == Dlocal)
freeloc(p);
}
return scopes[scope--];
}
void
installids(int store, Decl *ids)
{
if(ids == nil)
return;
Decl *last = nil;
for(Decl *d = ids; d; d=d->next){
d->scope = scope;
if(d->store == Dundef)
d->store = store;
Sym *s = d->sym;
if(s){
if(s->decl && s->decl->scope >= scope){
assert(0);
}else
d->old = s->decl;
s->decl = d;
}
last = d;
}
Decl *d = tails[scope];
if(d == nil)
scopes[scope] = ids;
else
d->next = ids;
tails[scope] = last;
}
void
fielddecled(Node *n)
{
for(; n; n=n->r)
switch(n->op){
case Oseq: fielddecled(n->l); break;
case Ofielddecl: installids(Dfield, n->decl); return;
case Ostrdecl: structdecled(n); return;
default: assert(0);
}
}
void
structdecled(Node *n)
{
Decl *d = n->ty->decl;
installids(Dtype, d);
pushscope(nil, Sother);
fielddecled(n->l);
n->ty->ids = popscope();
}
void
fndecled(Node *n)
{
Node *l = n->l;
assert(l->op == Oname);
Decl *d = l->decl->sym->decl;
assert(d == nil);
d = mkids(l->decl->sym, n->ty, nil);
installids(Dfn, d);
l->decl = d;
pushscope(nil, Sother);
installids(Darg, n->ty->ids);
n->ty->ids = popscope();
}
void
vardecled(Node *n)
{
int store = Dlocal;
if(scope == ScopeGlobal)
store = Dglobal;
Decl *ids = n->decl;
installids(store, ids);
Type *t = n->ty;
for(; ids; ids=ids->next)
ids->ty = t;
}
void
dasdecl(Node *n)
{
int store = 0;
if(scope == ScopeGlobal)
store = Dglobal;
else
store = Dlocal;
int id = 0;
recdecl(n, store, &id);
if(store == Dlocal && id > 1)
recmark(n, id);
}
Node*
vardecl(Decl *ids, Type *t)
{
Node *n = mkn(Ovardecl, mkn(Oseq, nil, nil), nil);
n->decl = ids;
n->ty = t;
return n;
}
Node*
mkdeclname(Decl *d)
{
Node *n = mkn(Oname, nil, nil);
n->decl = d;
n->ty = d->ty;
return n;
}
Node*
varinit(Decl *ids, Node *e)
{
Node *n = mkdeclname(ids);
if(ids->next == nil)
return mkn(Oas, n, e);
return mkn(Oas, n, varinit(ids->next, e));
}
Decl*
concatdecl(Decl *d1, Decl *d2)
{
if(d1 == nil)
return d2;
Decl *t = d1;
for(; t->next; t=t->next)
;
t->next = d2;
return d1;
}
int
decllen(Decl*d)
{
int l = 0;
for(;d;d=d->next)
++l;
return l;
}
int
sharelocal(Decl *d)
{
if(d->store != Dlocal)
return 0;
Type *t = d->ty;
for(Decl *dd = fndecls; dd; dd=dd->next){
assert(d != dd);
Type *tt = dd->ty;
if(dd->store != Dlocal || dd->link || dd->tref != 0)
continue;
if(t->size == tt->size){
d->link = dd;
dd->tref = 1;
return 1;
}
}
return 0;
}
static void
freeloc(Decl *d)
{
if(d->link)
d->link->tref = 0;
}
static void
recdecl(Node *n ,int store, int *id)
{
Decl *d;
switch(n->op){
case Otup:
for(n = n->l; n; n=n->r)
recdecl(n->l, store, id);
return;
case Oname:
assert(n->decl != nil);
d = mkids(n->decl->sym, nil, nil);
installids(store, d);
// old = d->old; assert(old == nil); warn shadwoing
n->decl = d;
d->das = 1;
if(*id >= 0)
*id += 1;
return;
default:
assert(0);
}
}
static int
recmark(Node *n, int id)
{
switch(n->op){
case Otup:
for(n = n->l; n; n = n->r)
id = recmark(n->l, id);
break;
case Oname:
n->decl->nid = id;
id = 0;
break;
default:
assert(0);
}
return id;
}