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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go.h"
static struct
{
NodeList* list;
Node* mapname;
Type* type;
} xxx;
enum
{
TC_xxx,
TC_unknown, // class
TC_struct,
TC_array,
TC_slice,
TC_map,
TS_start, // state
TS_middle,
TS_end,
};
/*
* the init code (thru initfix) reformats the
* var = ...
* statements, rewriting the automatic
* variables with the static variables.
* this allows the code generator to
* generate DATA statements instead
* of assignment statements.
* it is quadradic, may need to change.
* it is extremely fragile knowing exactly
* how the code from (struct|array|map)lit
* will look. ideally the lit routines could
* write the code in this form, but ...
*/
static int
typeclass(Type *t)
{
if(t != T)
switch(t->etype) {
case TSTRUCT:
return TC_struct;
case TARRAY:
if(t->bound >= 0)
return TC_array;
return TC_slice;
case TMAP:
return TC_map;
}
return TC_unknown;
}
void
initlin(NodeList *l)
{
Node *n;
for(; l; l=l->next) {
n = l->n;
initlin(n->ninit);
n->ninit = nil;
xxx.list = list(xxx.list, n);
switch(n->op) {
default:
print("o = %O\n", n->op);
break;
case OCALL:
// call to mapassign1
case OAS:
break;
}
}
}
int
inittmp(Node *n)
{
if(n != N)
if(n->op == ONAME)
if(n->sym != S)
if(n->class == PAUTO)
if(memcmp(n->sym->name, "autotmp_", 8) == 0)
return 1;
return 0;
}
int
sametmp(Node *n1, Node *n2)
{
if(inittmp(n1))
if(n1->xoffset == n2->xoffset)
return 1;
return 0;
}
Node*
findarg(Node *n, char *arg, char *fn)
{
Node *a;
NodeList *l;
if(n == N || n->op != OCALL ||
n->left == N || n->left->sym == S ||
strcmp(n->left->sym->name, fn) != 0)
return N;
for(l=n->list; l; l=l->next) {
a = l->n;
if(a->op == OAS &&
a->left != N && a->right != N &&
a->left->op == OINDREG &&
a->left->sym != S)
if(strcmp(a->left->sym->name, arg) == 0)
return a->right;
}
return N;
}
Node*
slicerewrite(Node *n)
{
Node *nel;
Type *t;
int b;
Node *a;
while(n->op == OCONVNOP)
n = n->left;
// call to newarray - find nel argument
nel = findarg(n, "nel", "newarray");
if(nel == N || !isslice(n->type))
goto no;
b = mpgetfix(nel->val.u.xval);
t = shallow(n->type);
t->bound = b;
// special hack for zero-size array
// invent an l-value to point at
if(b == 0)
a = staticname(types[TBOOL]);
else
a = staticname(t);
a = nod(OCOMPSLICE, a, N);
a->type = n->type;
return a;
no:
return N;
}
Node*
maprewrite(Node *n)
{
Node *nel;
Type *ta, *tb;
Node *a;
// call to newarray - find nel argument
nel = findarg(n, "hint", "newmap");
if(nel == N)
goto no;
ta = n->type;
if(ta->etype != TMAP)
goto no;
// create a new type from map[index]value
// [0]struct { a index; b value) }
tb = typ(TFIELD);
tb->type = ta->down;
tb->sym = lookup("key");
tb->nname = newname(tb->sym);
tb->down = typ(TFIELD);
tb->down->type = ta->type;
tb->down->sym = lookup("val");
tb->down->nname = newname(tb->down->sym);
ta = typ(TSTRUCT);
ta->type = tb;
tb = typ(TARRAY);
tb->type = ta;
tb->bound = 0;
dowidth(tb);
a = staticname(tb);
a = nod(OCOMPMAP, a, N);
a->type = n->type;
// save stuff for this iteration
xxx.mapname = a->left;
xxx.type = tb;
return a;
no:
return N;
}
// convert the call to mapassign1
// into static[i].key = k, static[i].val = v
Node*
mapindex(Node *n)
{
Node *index, *val, *key, *a, *b, *r;
// pull all the primatives
key = findarg(n, "key", "mapassign1");
if(key == N)
return N;
val = findarg(n, "val", "mapassign1");
if(val == N)
return N;
index = nodintconst(xxx.type->bound);
xxx.type->bound++;
dowidth(xxx.type);
// build tree
a = nod(OINDEX, xxx.mapname, index);
a = nod(ODOT, a, newname(lookup("key")));
a = nod(OAS, a, key);
b = nod(OINDEX, xxx.mapname, index);
b = nod(ODOT, b, newname(lookup("val")));
b = nod(OAS, b, val);
r = liststmt(list(list1(a), b));
walkstmt(r);
return r;
}
// for a copy out reference, A = B,
// look through the whole structure
// and substitute references of B to A.
// some rewrite goes on also.
void
initsub(Node *n, Node *nam)
{
Node *r, *w, *c;
NodeList *l;
int class, state;
// we could probably get a little more
// out of this if we allow minimal simple
// expression on the right (eg OADDR-ONAME)
if(n->op != ONAME)
return;
class = typeclass(nam->type);
state = TS_start;
switch(class) {
case TC_struct:
goto str;
case TC_array:
goto ary;
case TC_slice:
goto sli;
case TC_map:
goto map;
}
return;
str:
for(l=xxx.list; l; l=l->next) {
r = l->n;
if(r->op != OAS && r->op != OEMPTY)
continue;
// optional first usage "nam = N"
if(r->right == N && sametmp(r->left, nam)) {
if(state != TS_start) {
dump("", r);
fatal("initsub: str-first and state=%d", state);
}
state = TS_middle;
r->op = OEMPTY;
continue;
}
// last usage "n = nam"
if(r->left != N && sametmp(r->right, nam)) {
if(state == TS_end) {
dump("", r);
fatal("initsub: str-last and state=%d", state);
}
state = TS_end;
r->op = OEMPTY;
continue;
}
// middle usage "(nam DOT name) AS expr"
if(r->left->op != ODOT || !sametmp(r->left->left, nam))
continue;
if(state == TS_end) {
dump("", r);
fatal("initsub: str-middle and state=%d", state);
}
state = TS_middle;
r->left->left = n;
}
return;
ary:
for(l=xxx.list; l; l=l->next) {
r = l->n;
if(r->op != OAS && r->op != OEMPTY)
continue;
// optional first usage "nam = N"
if(r->right == N && sametmp(r->left, nam)) {
if(state != TS_start) {
dump("", r);
fatal("initsub: ary-first and state=%d", state);
}
state = TS_middle;
r->op = OEMPTY;
continue;
}
// last usage "n = nam"
if(r->left != N && sametmp(r->right, nam)) {
if(state == TS_end) {
dump("", r);
fatal("initsub: ary-last and state=%d", state);
}
state = TS_end;
r->op = OEMPTY;
continue;
}
// middle usage "(nam INDEX literal) = expr"
if(r->left->op != OINDEX || !sametmp(r->left->left, nam))
continue;
if(state == TS_end) {
dump("", r);
fatal("initsub: ary-middle and state=%d", state);
}
state = TS_middle;
r->left->left = n;
}
return;
sli:
w = N;
for(l=xxx.list; l; l=l->next) {
r = l->n;
if(r->op != OAS && r->op != OEMPTY)
continue;
// first usage "nam = (newarray CALL args)"
if(r->right != N && sametmp(r->left, nam)) {
w = slicerewrite(r->right);
if(w == N)
continue;
if(state != TS_start) {
dump("", r);
fatal("initsub: sli-first and state=%d", state);
}
state = TS_middle;
r->right = w;
r->left = n;
continue;
}
// last usage "n = nam"
if(r->left != N && sametmp(r->right, nam)) {
if(state != TS_middle) {
dump("", r);
setlineno(r);
fatal("initsub: sli-last and state=%d", state);
}
state = TS_end;
r->op = OEMPTY;
continue;
}
// middle usage "(nam INDEX literal) = expr"
if(r->left->op != OINDEX || !sametmp(r->left->left, nam))
continue;
if(state != TS_middle) {
dump("", r);
fatal("initsub: sli-middle and state=%d", state);
}
state = TS_middle;
r->left->left = w->left;
}
return;
map:
return;
w = N;
for(l=xxx.list; l; l=l->next) {
r = l->n;
if(r->op == OCALL) {
// middle usage "(CALL mapassign1 key, val, map)"
c = mapindex(r);
if(c == N)
continue;
state = TS_middle;
*r = *c;
continue;
}
if(r->op != OAS && r->op != OEMPTY)
continue;
// first usage "nam = (newmap CALL args)"
if(r->right != N && sametmp(r->left, nam)) {
w = maprewrite(r->right);
if(w == N)
continue;
if(state != TS_start) {
dump("", r);
fatal("initsub: map-first and state=%d", state);
}
state = TS_middle;
r->right = w;
r->left = n;
continue;
}
// last usage "n = nam"
if(r->left != N && sametmp(r->right, nam)) {
if(state != TS_middle) {
dump("", r);
fatal("initsub: map-last and state=%d", state);
}
state = TS_end;
r->op = OEMPTY;
continue;
}
}
return;
}
NodeList*
initfix(NodeList *l)
{
Node *r;
xxx.list = nil;
initlin(l);
if(0)
return xxx.list;
// look for the copy-out reference
for(l=xxx.list; l; l=l->next) {
r = l->n;
if(r->op == OAS)
if(inittmp(r->right))
initsub(r->left, r->right);
}
return xxx.list;
}
|