-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom.c
753 lines (717 loc) · 13.3 KB
/
com.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#include "yo.h"
static Node* ecom(Node *nto, Node *n);
static Inst **breaks;
static Inst **conts;
static Node **bcsps;
static int scp;
static Node *scps[MaxScope];
static void
pushscp(Node *n)
{
assert(scp < MaxScope);
scps[scp++] = n;
}
static void
popscp(void)
{
scp -= 1;
}
static Node*
curscp(void)
{
if(scp == 0) return nil;
else return scps[scp-1];
}
static Node*
sumark(Node *n)
{
if(n == nil)
return nil;
Node *l = n->l;
Node *r = n->r;
n->temps = 0;
n->addable = Rcant;
if(l){
sumark(l);
n->temps = l->temps;
}
if(r){
sumark(r);
if(r->temps == n->temps) n->temps++;
else if(r->temps > n->temps) n->temps = r->temps;
}
switch(n->op){
case Oindex:
n->addable = Radr;
break;
case Oxref:
switch(l->addable){
case Rreg: n->addable = Radr; break;
case Rareg: n->addable = Rreg; break;
case Raadr: n->addable = Radr; break;
default: break;
}
break;
case Oadr:
switch(l->addable){
case Rreg: n->addable = Rareg; break;
case Radr: n->addable = Raadr; break;
default: break;
}
break;
case Oname:
switch(n->decl->store){
case Dtype: break;
case Darg:
case Dlocal: n->addable = Rreg; break;
case Dpkg:
case Dfn: n->addable = Rpc; break;
case Dglobal: n->addable = Rreg; break;
default:
assert(0);
}
break;
case Oconst:
switch(n->ty->kind){
case Tbool:
case Tint: n->addable = Rconst; break;
default: assert(0);
}
break;
case Omul:
case Oadd:
case Osub:
if(r->addable == Rconst){
switch(l->addable){
case Rcant:
case Rconst: break;
case Rareg: n->addable = Rareg; break;
case Rreg:
case Raadr: n->addable = Raadr; break;
case Radr: n->addable = Rreg; break;
default: assert(0);
}
}
break;
default:
break;
}
if(n->addable < Rcant) n->temps = 0;
else if(n->temps == 0) n->temps = 1;
return n;
}
static void
arrcom(Node *nto, Node *n)
{
Node sz = {.op=Oconst,.val=0,.addable=Rconst,.ty=tint};
Node toff = {0}, tadd={0}, tadr={0}, fake={0}, *e=nil;
toff.op = Oconst;
toff.ty = tint;
tadr.op = Oadr;
tadr.l = nto;
tadr.ty = tint;
tadd.op = Oadd;
tadd.l = &tadr;
tadd.r = &toff;
tadd.ty = tint;
fake.op = Oxref;
fake.l = &tadd;
sumark(&fake);
assert(fake.addable < Rcant);
fake.ty = n->ty->tof;
genmove(nto->ty, nto, nto);
toff.val += IBY2WD; sz.val = nto->ty->len;
genmove(tint, &sz, &fake);
toff.val += IBY2WD; sz.val = nto->ty->len;
genmove(tint, &sz, &fake);
toff.val += IBY2WD; sz.val = nto->ty->tof->size;
genmove(tint, &sz, &fake);
for(e = n->l; e != nil; e = e->r){
toff.val += nto->ty->tof->size;
ecom(&fake, e->l);
}
}
static int
tupblk0(Node *n, Decl **dd)
{
Decl *d = nil;
switch(n->op){
case Otup:
for(n = n->l; n; n=n->r)
if(tupblk0(n->l, dd) == 0)
return 0;
return 1;
case Oname:
if(n->decl == nil)
return 0;
d = *dd;
if(d && d->next != n->decl)
return 0;
int nid = n->decl->nid;
if(d == nil && nid == 1)
return 0;
if(d != nil && nid != 0)
return 0;
*dd = n->decl;
return 1;
}
return 0;
}
static Node*
tupblk(Node *n)
{
if(n->op != Otup)
return nil;
Decl *d = nil;
if(tupblk0(n, &d) == 0)
return nil;
while(n->op == Otup)
n = n->l->l;
assert(n->op == Oname);
return n;
}
static void
tuplcom(Node *n, Node *nto)
{
Decl *d = nil;
Node toff = {0}, tadd={0}, tadr={0}, fake={0}, tas={0}, *e=nil, *as;
toff.op = Oconst;
toff.ty = tint;
tadr.op = Oadr;
tadr.l = n;
tadr.ty = tint;
tadd.op = Oadd;
tadd.l = &tadr;
tadd.r = &toff;
tadd.ty = tint;
fake.op = Oxref;
fake.l = &tadd;
sumark(&fake);
assert(fake.addable < Rcant);
d = nto->ty->ids;
for(e = nto->l; e != nil; e = e->r){
as = e->l;
if(as->op != Oname || as->decl != nildecl){
toff.val = d->offset;
fake.ty = d->ty;
if(as->addable < Rcant)
genmove(d->ty, &fake, as);
else{
tas.op = Oas;
tas.ty = d->ty;
tas.l = as;
tas.r = &fake;
tas.addable = Rcant;
ecom(nil, &tas);
}
}
d = d->next;
}
}
static void
tuplrcom(Node *n, Node *nto)
{
Decl *de = nto->ty->ids;
Node *s = nil, *d = nil, tas ={0};
for(s=n->l, d=nto->l; s!=nil&&d!=nil; s=s->r, d=d->r){
if(d->l->op != Oname || d->l->decl != nil){
tas.op = Oas;
tas.ty = de->ty;
tas.l = d->l;
tas.r = s->l;
sumark(&tas);
ecom(nil, &tas);
}
de = de->next;
}
assert(s == nil &&d == nil);
}
static void
tupcom(Node *nto, Node *n)
{
Decl *d = nil;
Node toff = {0}, tadd={0}, tadr={0}, fake={0}, *e=nil;
toff.op = Oconst;
toff.ty = tint;
tadr.op = Oadr;
tadr.l = nto;
tadr.ty = tint;
tadd.op = Oadd;
tadd.l = &tadr;
tadd.r = &toff;
tadd.ty = tint;
fake.op = Oxref;
fake.l = &tadd;
sumark(&fake);
assert(fake.addable < Rcant);
d = n->ty->ids;
for(e = n->l; e != nil; e = e->r){
toff.val = d->offset;
fake.ty = d->ty;
ecom(&fake, e->l);
d = d->next;
}
}
static Node*
eacom(Node *n, Node *reg, Node *t)
{
Node *l = n->l;
if(n->op != Oxref){
Node *tmp = talloc(reg, n->ty, t);
ecom(tmp, n);
return reg;
}
if(l->op==Oadd && l->r->op == Oconst){
if(l->l->op == Oadr){
l->l->l = eacom(l->l->l, reg, t);
sumark(n);
assert(n->addable < Rcant);
return n;
}else
assert(0);
}else if(l->op == Oadr){
assert(0);
}else{
talloc(reg, l->ty, t);
ecom(reg, l);
n->l = reg;
n->addable = Radr;
}
return n;
}
static void
assertcall(Decl *i, Node *j)
{
assert((i==nil) == (j==nil));
for(; i && j && j->op==Oseq; i=i->next, j=j->r){
assert(eqtype(i->ty, j->l->ty));
}
assert((j==nil) == (i==nil));
}
static void
callcom(int op, Node *n, Node *ret)
{
Node *args = n->r, *fn = n->l;
Node tadd = {0}, pass={0}, toff={0}, frame={0};
assertcall(fn->ty->ids, args);
talloc(&frame, tint, nil);
toff.op = Oconst;
toff.addable = Rconst;
toff.ty = tint;
tadd.op = Oadd;
tadd.addable = Raadr;
tadd.l = &frame;
tadd.r = &toff;
tadd.ty = tint;
pass.op = Oxref;
pass.ty = tint;
pass.op = Oxref;
pass.addable = Radr;
pass.l = &tadd;
Inst *in = genrawop(IFRAME, nil, nil, &frame);
in->sm = Adesc;
in->s.decl = fn->decl;
Decl *d = fn->ty->ids;
int off = 0;
for(Node *a = args; a; a=a->r, d=d->next){
off = d->offset;
toff.val = off;
pass.ty = d->ty;
ecom(&pass, a->l);
}
/* pass return value */
if(ret && ret->ty != tnone){
toff.val = REGRET * IBY2WD;
pass.ty = fn->ty->tof;
in = genrawop(ILEA, ret, nil, &pass);
}
if(fn->op != Opkg){
in = genrawop(ICALL, &frame, nil, nil);
in->dm = Apc;
in->d.decl = fn->decl;
}else{
in = genrawop(PCALL, &frame, nil, nil);
in->mm = Aimm;
in->m.offset = fn->decl->pc->m.reg;
in->dm = Aimm;
in->d.offset = fn->decl->pc->pc;
}
tfree(&frame);
}
static Node*
rewrite(Node *n)
{
if(n == nil)
return nil;
Type *t = nil;
Decl *d = nil;
Node *l = n->l, *r = n->r;
switch(n->op){
case Opkg: return n;
case Oexport: n = n->l; break;
case Odas: n->op = Oas; return rewrite(n);
case Odot:
d = r->decl;
switch(d->store){
case Dpkg: return r;
case Dfield: break;
case Dfn:
assert(r->l == nil);
n->op = Oname;
n->decl = d;
n->r = nil;
n->l = nil;
return n;
default:
assert(0);
}
r->op = Oconst;
r->val = d->offset;
r->ty = tint;
n->l = mkunray(Oadr, l);
n->l->ty = tint;
n->op = Oadd;
n = mkunray(Oxref, n);
n->ty = n->l->ty;
n->l->ty = tint;
n->l = rewrite(n->l);
return n;
case Oindex:
t = n->ty;
n->r = mkn(Omul, mkconst(IBY2WD), n->r);
n->r->ty = tint;
n->op = Oadd;
n->ty = tint;
n = mkunray(Oxref, n);
n->ty = t;
break;
default:
n->l = rewrite(l);
n->r = rewrite(r);
break;
}
return n;
}
static Node*
simplifiy(Node *n)
{
if(n==nil)
return nil;
return rewrite(n);
}
static int
tupaliased(Node *n, Node *e)
{
for(;;){
if(e == nil) return 0;
if(e->op == Oname && e->decl == n->decl) return 1;
if(tupaliased(n, e->l)) return 1;
e = e->r;
}
}
static int
tupsaliased(Node *n, Node *e)
{
for(;;){
if(n == nil) return 0;
if(n->op == Oname && tupaliased(n, e)) return 1;
if(tupsaliased(n->l, e)) return 1;
n = n->r;
}
}
static int
swaprelop(int op)
{
switch(op){
case Oeq: return Oneq;
case Oneq: return Oeq;
case Olt: return Ogt;
case Ogt: return Olt;
case Ogeq: return Oleq;
case Oleq: return Ogeq;
default: return op;
}
}
static Inst*
cmpcom(Node *nto, Node *n)
{
Node tl={0}, tr={0};
Node *l = n->l;
Node *r = n->r;
int op = n->op;
Inst *i;
switch(op){
case Ogt:
case Ogeq:
op = swaprelop(op);
Node *t = l;
l = r;
r = t;
}
if(r->addable < Ralways){
if(l->addable >= Rcant)
l = eacom(l, &tl, nil);
}else if(l->temps <= r->temps){
r = ecom(talloc(&tr, r->ty, nil), r);
if(l->addable >= Rcant)
l = eacom(l, &tl, nil);
}else{
l = eacom(l, &tl, nil);
r = eacom(l, talloc(&tr, r->ty, nil), r);
}
i = genop(op, l, r, nto);
tfree(&tl);
tfree(&tr);
return i;
}
static Inst*
brcom(Node *n, int ift, Inst *b)
{
Inst *bb = nil;
Node nto={0};
Node f = (Node){.op=Oconst,.addable=Rconst,.ty=tbool,.val=1};
if(ift)
f.val = 0;
ecom(talloc(&nto, tbool, nil), n);
bb = genrawop(IBEQW, &nto, &f, nil);
bb->branch = b;
tfree(&nto);
return bb;
}
static Node*
ecom(Node *nto, Node *n)
{
Node *l = n->l; Node *r = n->r;
Node tr={0},tl={0},tt={0},*tn=nil;
int op = n->op;
if(n->addable < Rcant){
if(nto)
genmove(n->ty, n, nto);
return nto;
}
switch(op){
case Oas:
if(l->op == Oslice)
assert(0);
if(l->op == Otup){
if(tupsaliased(r, l) == 0){
if((tn = tupblk(l))){
tn->ty = n->ty;
ecom(tn, r);
if(nto)
genmove(n->ty, tn, nto);
break;
}
if((tn = tupblk(r))){
tn->ty = n->ty;
ecom(tn, l);
if(nto)
genmove(n->ty, tn, nto);
break;
}
if(nto==nil && r->op == Otup){
tuplrcom(r, l);
break;
}
}
if(r->addable >= Ralways||r->op!=Oname
||tupaliased(r, l)){
talloc(&tr, n->ty, nil);
ecom(&tr, r);
r = &tr;
}
tuplcom(r, n->l);
if(nto)
genmove(n->ty, r, nto);
tfree(&tr);
break;
}
if(l->addable >= Rcant)
l = eacom(l, &tl, nto);
ecom(l, r);
if(nto)
genmove(nto->ty, l, nto);
tfree(&tl);
tfree(&tr);
break;
case Olen: genrawop(ILEN, n->l, nil, nto); break;
case Oarray: arrcom(nto, n); break;
case Ostruct: tupcom(nto, n); break;
case Oslice:
if(l->addable >= Rcant)
l = eacom(l, &tl, nto);
if(r->l->addable >= Rcant)
r->l = ecom(talloc(&tr,r->l->ty,nil), r->l);
if(r->r->addable >= Rcant)
r->r = ecom(talloc(&tt,r->r->ty,nil), r->r);
genmove(n->ty, l, nto);
genrawop(ISLICE, r->l, r->r, nto);
tfree(&tl);
tfree(&tr);
tfree(&tt);
break;
case Otup:
if((tn = tupblk(n)) != nil){
tn->ty = n->ty;
genmove(n->ty, tn, nto);
break;
}
tupcom(nto, n);
break;
case Oxref:
n = eacom(n, &tl, nto);
genmove(n->ty, n, nto);
tfree(&tl);
break;
case Oref:
genrawop(ILEA, l, nil, nto);
break;
case Ocall:
callcom(op, n, nto);
break;
case Oandand: case Ooror:
case Oeq: case Oneq:
case Olt: case Ogt:
case Ogeq: case Oleq:
cmpcom(nto, n);
break;
case Omul:
case Oadd:
case Osub:
if(nto==nil)
break;
if(l->addable < Ralways){
if(r->addable >= Rcant)
r = eacom(r, &tr, nto);
}else if(r->temps <= l->temps){
l = ecom(talloc(&tl, l->ty, nto), l);
if(r->addable >= Rcant)
r = eacom(r, &tr, nil);
}else{
r = eacom(r, &tr, nto);
l = ecom(talloc(&tl, l->ty, nil), l);
}
if(sameaddr(nto, l))
genop(op, r, nil, nto);
else if(sameaddr(nto, r))
genop(op, l, nil, nto);
else
genop(op, r, l, nto);
tfree(&tl);
tfree(&tr);
break;
}
return nto;
}
static void
scom(Node *n)
{
Inst *p, *pp;
Node *l, *r;
for(; n; n = n->r){
l = n->l;
r = n->r;
switch(n->op){
case Ovardecl:
return;
case Oscope:
pushscp(n);
scom(n->l);
scom(n->r);
popscp();
return;
case Oif:
pushblock();
pushblock();
n->l = simplifiy(n->l);
sumark(n->l);
p = brcom(l, 1, nil);
tfreenow();
popblock();
scom(r->l);
if(r->r){
pp = p;
p = genrawop(IJMP, nil, nil, nil);
ipatch(pp, nextinst());
scom(r->r);
}
ipatch(p, nextinst());
popblock();
return;
case Ofor:
pushblock();
pp = nextinst();
n->l = l = simplifiy(n->l);
sumark(l);
p = brcom(l, 1, nil);
tfreenow();
popblock();
assert(loopdep < maxloopdep);
breaks[loopdep] = nil;
conts[loopdep] = nil;
bcsps[loopdep] = curscp();
loopdep += 1;
scom(n->r->l);
loopdep -= 1;
ipatch(conts[loopdep], nextinst());
if(n->r->r){
pushblock();
scom(n->r->r);
popblock();
}
repushblock(lastinst->block);
ipatch(genrawop(IJMP,nil,nil,nil),pp);
popblock();
ipatch(p, nextinst());
ipatch(breaks[loopdep], nextinst());
return;
case Oret:
pushblock();
if(n->l){
Node ret={0};
n->l = simplifiy(n->l);
sumark(n->l);
ecom(retalloc(&ret, n->l), n->l);
tfreenow();
}
genrawop(IRET, nil, nil, nil);
popblock();
break;
case Oseq:
scom(n->l);
break;
default:
pushblock();
n = simplifiy(n);
sumark(n);
ecom(nil, n);
tfreenow();
popblock();
return;
}
}
}
void
fncom(Decl *d)
{
Node *n = d->init;
d->pc = nextinst();
tinit();
loopdep = scp = 0;
breaks = new(sizeof(breaks[0]) * maxloopdep);
conts = new(sizeof(conts[0]) * maxloopdep);
bcsps = new(sizeof(bcsps[0]) * maxloopdep);
for(Node *p=n->r; p; p=p->r){
if(p->op != Oseq){
scom(p);
break;
}else
scom(p->l);
}
free(breaks);
free(conts);
free(bcsps);
Decl *locs = concatdecl(d->locals, tdecls());
d->offset += idoffsets(locs, d->offset, IBY2WD);
d->locals = locs;
}