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
|
#include "all.h"
typedef struct Class_ Class;
typedef struct Insl Insl;
typedef struct Params Params;
enum {
Cstk = 1, /* pass on the stack */
Cptr = 2, /* replaced by a pointer */
};
struct Class_ {
char class;
char ishfa;
struct {
char base;
uchar size;
} hfa;
uint size;
Typ *t;
uchar nreg;
uchar ngp;
uchar nfp;
int reg[4];
int cls[4];
};
struct Insl {
Ins i;
Insl *link;
};
struct Params {
uint ngp;
uint nfp;
uint nstk;
};
static int gpreg[12] = {R0, R1, R2, R3, R4, R5, R6, R7};
static int fpreg[12] = {V0, V1, V2, V3, V4, V5, V6, V7};
/* layout of call's second argument (RCall)
*
* 29 13 9 5 2 0
* |0.00|x|xxxx|xxxx|xxx|xx| range
* | | | | ` gp regs returned (0..2)
* | | | ` fp regs returned (0..4)
* | | ` gp regs passed (0..8)
* | ` fp regs passed (0..8)
* ` is x8 used (0..1)
*/
static int
isfloatv(Typ *t, char *cls)
{
Field *f;
uint n;
for (n=0; n<t->nunion; n++)
for (f=t->fields[n]; f->type != FEnd; f++)
switch (f->type) {
case Fs:
if (*cls == Kd)
return 0;
*cls = Ks;
break;
case Fd:
if (*cls == Ks)
return 0;
*cls = Kd;
break;
case FTyp:
if (isfloatv(&typ[f->len], cls))
break;
/* fall through */
default:
return 0;
}
return 1;
}
static void
typclass(Class *c, Typ *t, int *gp, int *fp)
{
uint64_t sz;
uint n;
sz = (t->size + 7) & -8;
c->t = t;
c->class = 0;
c->ngp = 0;
c->nfp = 0;
if (t->align > 4)
err("alignments larger than 16 are not supported");
if (t->dark || sz > 16 || sz == 0) {
/* large structs are replaced by a
* pointer to some caller-allocated
* memory */
c->class |= Cptr;
c->size = 8;
return;
}
c->size = sz;
c->hfa.base = Kx;
c->ishfa = isfloatv(t, &c->hfa.base);
c->hfa.size = t->size/(KWIDE(c->hfa.base) ? 8 : 4);
if (c->ishfa)
for (n=0; n<c->hfa.size; n++, c->nfp++) {
c->reg[n] = *fp++;
c->cls[n] = c->hfa.base;
}
else
for (n=0; n<sz/8; n++, c->ngp++) {
c->reg[n] = *gp++;
c->cls[n] = Kl;
}
c->nreg = n;
}
static void
sttmps(Ref tmp[], int cls[], uint nreg, Ref mem, Fn *fn)
{
static int st[] = {
[Kw] = Ostorew, [Kl] = Ostorel,
[Ks] = Ostores, [Kd] = Ostored
};
uint n;
uint64_t off;
Ref r;
assert(nreg <= 4);
off = 0;
for (n=0; n<nreg; n++) {
tmp[n] = newtmp("abi", cls[n], fn);
r = newtmp("abi", Kl, fn);
emit(st[cls[n]], 0, R, tmp[n], r);
emit(Oadd, Kl, r, mem, getcon(off, fn));
off += KWIDE(cls[n]) ? 8 : 4;
}
}
/* todo, may read out of bounds */
static void
ldregs(int reg[], int cls[], int n, Ref mem, Fn *fn)
{
int i;
uint64_t off;
Ref r;
off = 0;
for (i=0; i<n; i++) {
r = newtmp("abi", Kl, fn);
emit(Oload, cls[i], TMP(reg[i]), r, R);
emit(Oadd, Kl, r, mem, getcon(off, fn));
off += KWIDE(cls[i]) ? 8 : 4;
}
}
static void
selret(Blk *b, Fn *fn)
{
int j, k, cty;
Ref r;
Class cr;
j = b->jmp.type;
if (!isret(j) || j == Jret0)
return;
r = b->jmp.arg;
b->jmp.type = Jret0;
if (j == Jretc) {
typclass(&cr, &typ[fn->retty], gpreg, fpreg);
cty = (cr.nfp << 2) | cr.ngp;
if (cr.class & Cptr) {
assert(rtype(fn->retr) == RTmp);
blit(fn->retr, 0, r, cr.t->size, fn);
} else
ldregs(cr.reg, cr.cls, cr.nreg, r, fn);
} else {
k = j - Jretw;
if (KBASE(k) == 0) {
emit(Ocopy, k, TMP(R0), r, R);
cty = 1;
} else {
emit(Ocopy, k, TMP(V0), r, R);
cty = 1 << 2;
}
}
b->jmp.arg = CALL(cty);
}
static int
argsclass(Ins *i0, Ins *i1, Class *carg, Ref *env)
{
int ngp, nfp, *gp, *fp;
Class *c;
Ins *i;
gp = gpreg;
fp = fpreg;
ngp = 8;
nfp = 8;
for (i=i0, c=carg; i<i1; i++, c++)
switch (i->op) {
case Opar:
case Oarg:
c->cls[0] = i->cls;
c->size = 8;
if (KBASE(i->cls) == 0 && ngp > 0) {
ngp--;
c->reg[0] = *gp++;
break;
}
if (KBASE(i->cls) == 1 && nfp > 0) {
nfp--;
c->reg[0] = *fp++;
break;
}
c->class |= Cstk;
break;
case Oparc:
case Oargc:
typclass(c, &typ[i->arg[0].val], gp, fp);
if (c->class & Cptr) {
if (ngp > 0) {
ngp--;
c->reg[0] = *gp++;
c->cls[0] = Kl;
break;
}
} else if (c->ngp <= ngp) {
if (c->nfp <= nfp) {
ngp -= c->ngp;
nfp -= c->nfp;
gp += c->ngp;
fp += c->nfp;
break;
} else
nfp = 0;
} else
ngp = 0;
c->class |= Cstk;
break;
case Opare:
*env = i->to;
break;
case Oarge:
*env = i->arg[0];
break;
case Oargv:
break;
default:
die("unreachable");
}
return ((gp-gpreg) << 5) | ((fp-fpreg) << 9);
}
bits
arm64_retregs(Ref r, int p[2])
{
bits b;
int ngp, nfp;
assert(rtype(r) == RCall);
ngp = r.val & 3;
nfp = (r.val >> 2) & 7;
if (p) {
p[0] = ngp;
p[1] = nfp;
}
b = 0;
while (ngp--)
b |= BIT(R0+ngp);
while (nfp--)
b |= BIT(V0+nfp);
return b;
}
bits
arm64_argregs(Ref r, int p[2])
{
bits b;
int ngp, nfp, x8;
assert(rtype(r) == RCall);
ngp = (r.val >> 5) & 15;
nfp = (r.val >> 9) & 15;
x8 = (r.val >> 13) & 1;
if (p) {
p[0] = ngp + x8;
p[1] = nfp;
}
b = 0;
while (ngp--)
b |= BIT(R0+ngp);
while (nfp--)
b |= BIT(V0+nfp);
return b | ((bits)x8 << R8);
}
static void
stkblob(Ref r, Class *c, Fn *fn, Insl **ilp)
{
Insl *il;
int al;
uint64_t sz;
il = alloc(sizeof *il);
al = c->t->align - 2; /* NAlign == 3 */
if (al < 0)
al = 0;
sz = c->class & Cptr ? c->t->size : c->size;
il->i = (Ins){Oalloc+al, Kl, r, {getcon(sz, fn)}};
il->link = *ilp;
*ilp = il;
}
static void
selcall(Fn *fn, Ins *i0, Ins *i1, Insl **ilp)
{
Ins *i;
Class *ca, *c, cr;
int cty, envc;
uint n;
uint64_t stk, off;
Ref r, rstk, env, tmp[4];
env = R;
ca = alloc((i1-i0) * sizeof ca[0]);
cty = argsclass(i0, i1, ca, &env);
stk = 0;
for (i=i0, c=ca; i<i1; i++, c++) {
if (c->class & Cptr) {
i->arg[0] = newtmp("abi", Kl, fn);
stkblob(i->arg[0], c, fn, ilp);
i->op = Oarg;
}
if (c->class & Cstk)
stk += c->size;
}
stk += stk & 15;
rstk = getcon(stk, fn);
if (stk)
emit(Oadd, Kl, TMP(SP), TMP(SP), rstk);
if (!req(i1->arg[1], R)) {
typclass(&cr, &typ[i1->arg[1].val], gpreg, fpreg);
stkblob(i1->to, &cr, fn, ilp);
cty |= (cr.nfp << 2) | cr.ngp;
if (cr.class & Cptr) {
cty |= 1 << 13 | 1;
emit(Ocopy, Kw, R, TMP(R0), R);
} else {
sttmps(tmp, cr.cls, cr.nreg, i1->to, fn);
for (n=0; n<cr.nreg; n++) {
r = TMP(cr.reg[n]);
emit(Ocopy, cr.cls[n], tmp[n], r, R);
}
}
} else {
if (KBASE(i1->cls) == 0) {
emit(Ocopy, i1->cls, i1->to, TMP(R0), R);
cty |= 1;
} else {
emit(Ocopy, i1->cls, i1->to, TMP(V0), R);
cty |= 1 << 2;
}
}
emit(Ocall, 0, R, i1->arg[0], CALL(cty));
envc = !req(R, env);
if (envc)
die("todo (arm abi): env calls");
if (cty & (1 << 13))
/* struct return argument */
emit(Ocopy, Kl, TMP(R8), i1->to, R);
for (i=i0, c=ca; i<i1; i++, c++) {
if ((c->class & Cstk) != 0)
continue;
if (i->op == Oarg)
emit(Ocopy, *c->cls, TMP(*c->reg), i->arg[0], R);
if (i->op == Oargc)
ldregs(c->reg, c->cls, c->nreg, i->arg[1], fn);
}
off = 0;
for (i=i0, c=ca; i<i1; i++, c++) {
if ((c->class & Cstk) == 0)
continue;
if (i->op == Oarg) {
r = newtmp("abi", Kl, fn);
emit(Ostorel, 0, R, i->arg[0], r);
emit(Oadd, Kl, r, TMP(SP), getcon(off, fn));
}
if (i->op == Oargc)
blit(TMP(SP), off, i->arg[1], c->size, fn);
off += c->size;
}
if (stk)
emit(Osub, Kl, TMP(SP), TMP(SP), rstk);
for (i=i0, c=ca; i<i1; i++, c++)
if (c->class & Cptr)
blit(i->arg[0], 0, i->arg[1], c->t->size, fn);
}
static Params
selpar(Fn *fn, Ins *i0, Ins *i1)
{
Class *ca, *c, cr;
Insl *il;
Ins *i;
int n, s, cty;
Ref r, env, tmp[16], *t;
env = R;
ca = alloc((i1-i0) * sizeof ca[0]);
curi = &insb[NIns];
cty = argsclass(i0, i1, ca, &env);
fn->reg = arm64_argregs(CALL(cty), 0);
il = 0;
t = tmp;
for (i=i0, c=ca; i<i1; i++, c++) {
if (i->op != Oparc || (c->class & (Cptr|Cstk)))
continue;
sttmps(t, c->cls, c->nreg, i->to, fn);
stkblob(i->to, c, fn, &il);
t += c->nreg;
}
for (; il; il=il->link)
emiti(il->i);
if (fn->retty >= 0) {
typclass(&cr, &typ[fn->retty], gpreg, fpreg);
if (cr.class & Cptr) {
fn->retr = newtmp("abi", Kl, fn);
emit(Ocopy, Kl, fn->retr, TMP(R8), R);
fn->reg |= BIT(R8);
}
}
t = tmp;
for (i=i0, c=ca, s=2; i<i1; i++, c++) {
if (i->op == Oparc
&& (c->class & Cptr) == 0) {
if (c->class & Cstk) {
fn->tmp[i->to.val].slot = -s;
s += c->size / 8;
} else
for (n=0; n<c->nreg; n++) {
r = TMP(c->reg[n]);
emit(Ocopy, c->cls[n], *t++, r, R);
}
} else if (c->class & Cstk) {
r = newtmp("abi", Kl, fn);
emit(Oload, *c->cls, i->to, r, R);
emit(Oaddr, Kl, r, SLOT(-s), R);
s++;
} else {
r = TMP(*c->reg);
emit(Ocopy, *c->cls, i->to, r, R);
}
}
if (!req(R, env))
die("todo (arm abi): env calls");
return (Params){
.nstk = s - 2,
.ngp = (cty >> 5) & 15,
.nfp = (cty >> 9) & 15
};
}
static Blk *
split(Fn *fn, Blk *b)
{
Blk *bn;
++fn->nblk;
bn = blknew();
bn->nins = &insb[NIns] - curi;
idup(&bn->ins, curi, bn->nins);
curi = &insb[NIns];
bn->visit = ++b->visit;
(void)!snprintf(bn->name, NString, "%s.%d", b->name, b->visit);
bn->loop = b->loop;
bn->link = b->link;
b->link = bn;
return bn;
}
static void
chpred(Blk *b, Blk *bp, Blk *bp1)
{
Phi *p;
uint a;
for (p=b->phi; p; p=p->link) {
for (a=0; p->blk[a]!=bp; a++)
assert(a+1<p->narg);
p->blk[a] = bp1;
}
}
static void
selvaarg(Fn *fn, Blk *b, Ins *i)
{
Ref loc, lreg, lstk, nr, r0, r1, c8, c16, c24, c28, ap;
Blk *b0, *bstk, *breg;
int isgp;
c8 = getcon(8, fn);
c16 = getcon(16, fn);
c24 = getcon(24, fn);
c28 = getcon(28, fn);
ap = i->arg[0];
isgp = KBASE(i->cls) == 0;
/* @b [...]
r0 =l add ap, (24 or 28)
nr =l loadsw r0
r1 =w csltw nr, 0
jnz r1, @breg, @bstk
@breg
r0 =l add ap, (8 or 16)
r1 =l loadl r0
lreg =l add r1, nr
r0 =w add nr, (8 or 16)
r1 =l add ap, (24 or 28)
storew r0, r1
@bstk
lstk =l loadl ap
r0 =l add lstk, 8
storel r0, ap
@b0
%loc =l phi @breg %lreg, @bstk %lstk
i->to =(i->cls) load %loc
*/
loc = newtmp("abi", Kl, fn);
emit(Oload, i->cls, i->to, loc, R);
b0 = split(fn, b);
b0->jmp = b->jmp;
b0->s1 = b->s1;
b0->s2 = b->s2;
if (b->s1)
chpred(b->s1, b, b0);
if (b->s2 && b->s2 != b->s1)
chpred(b->s2, b, b0);
lreg = newtmp("abi", Kl, fn);
nr = newtmp("abi", Kl, fn);
r0 = newtmp("abi", Kw, fn);
r1 = newtmp("abi", Kl, fn);
emit(Ostorew, Kw, R, r0, r1);
emit(Oadd, Kl, r1, ap, isgp ? c24 : c28);
emit(Oadd, Kw, r0, nr, isgp ? c8 : c16);
r0 = newtmp("abi", Kl, fn);
r1 = newtmp("abi", Kl, fn);
emit(Oadd, Kl, lreg, r1, nr);
emit(Oload, Kl, r1, r0, R);
emit(Oadd, Kl, r0, ap, isgp ? c8 : c16);
breg = split(fn, b);
breg->jmp.type = Jjmp;
breg->s1 = b0;
lstk = newtmp("abi", Kl, fn);
r0 = newtmp("abi", Kl, fn);
emit(Ostorel, Kw, R, r0, ap);
emit(Oadd, Kl, r0, lstk, c8);
emit(Oload, Kl, lstk, ap, R);
bstk = split(fn, b);
bstk->jmp.type = Jjmp;
bstk->s1 = b0;
b0->phi = alloc(sizeof *b0->phi);
*b0->phi = (Phi){
.cls = Kl, .to = loc,
.narg = 2,
.blk = vnew(2, sizeof b0->phi->blk[0], Pfn),
.arg = vnew(2, sizeof b0->phi->arg[0], Pfn),
};
b0->phi->blk[0] = bstk;
b0->phi->blk[1] = breg;
b0->phi->arg[0] = lstk;
b0->phi->arg[1] = lreg;
r0 = newtmp("abi", Kl, fn);
r1 = newtmp("abi", Kw, fn);
b->jmp.type = Jjnz;
b->jmp.arg = r1;
b->s1 = breg;
b->s2 = bstk;
emit(Ocmpw+Cislt, Kw, r1, nr, CON_Z);
emit(Oloadsw, Kl, nr, r0, R);
emit(Oadd, Kl, r0, ap, isgp ? c24 : c28);
}
static void
selvastart(Fn *fn, Params p, Ref ap)
{
Ref r0, r1, rsave;
rsave = newtmp("abi", Kl, fn);
r0 = newtmp("abi", Kl, fn);
emit(Ostorel, Kw, R, r0, ap);
emit(Oadd, Kl, r0, rsave, getcon(p.nstk*8 + 192, fn));
r0 = newtmp("abi", Kl, fn);
r1 = newtmp("abi", Kl, fn);
emit(Ostorel, Kw, R, r1, r0);
emit(Oadd, Kl, r1, rsave, getcon(64, fn));
emit(Oadd, Kl, r0, ap, getcon(8, fn));
r0 = newtmp("abi", Kl, fn);
r1 = newtmp("abi", Kl, fn);
emit(Ostorel, Kw, R, r1, r0);
emit(Oadd, Kl, r1, rsave, getcon(192, fn));
emit(Oaddr, Kl, rsave, SLOT(-1), R);
emit(Oadd, Kl, r0, ap, getcon(16, fn));
r0 = newtmp("abi", Kl, fn);
emit(Ostorew, Kw, R, getcon((p.ngp-8)*8, fn), r0);
emit(Oadd, Kl, r0, ap, getcon(24, fn));
r0 = newtmp("abi", Kl, fn);
emit(Ostorew, Kw, R, getcon((p.nfp-8)*16, fn), r0);
emit(Oadd, Kl, r0, ap, getcon(28, fn));
}
void
arm64_abi(Fn *fn)
{
Blk *b;
Ins *i, *i0, *ip;
Insl *il;
int n;
Params p;
for (b=fn->start; b; b=b->link)
b->visit = 0;
/* lower parameters */
for (b=fn->start, i=b->ins; i<&b->ins[b->nins]; i++)
if (!ispar(i->op))
break;
p = selpar(fn, b->ins, i);
n = b->nins - (i - b->ins) + (&insb[NIns] - curi);
i0 = alloc(n * sizeof(Ins));
ip = icpy(ip = i0, curi, &insb[NIns] - curi);
ip = icpy(ip, i, &b->ins[b->nins] - i);
b->nins = n;
b->ins = i0;
/* lower calls, returns, and vararg instructions */
il = 0;
b = fn->start;
do {
if (!(b = b->link))
b = fn->start; /* do it last */
if (b->visit)
continue;
curi = &insb[NIns];
selret(b, fn);
for (i=&b->ins[b->nins]; i!=b->ins;)
switch ((--i)->op) {
default:
emiti(*i);
break;
case Ocall:
for (i0=i; i0>b->ins; i0--)
if (!isarg((i0-1)->op))
break;
selcall(fn, i0, i, &il);
i = i0;
break;
case Ovastart:
selvastart(fn, p, i->arg[0]);
break;
case Ovaarg:
selvaarg(fn, b, i);
break;
case Oarg:
case Oargc:
die("unreachable");
}
if (b == fn->start)
for (; il; il=il->link)
emiti(il->i);
b->nins = &insb[NIns] - curi;
idup(&b->ins, curi, b->nins);
} while (b != fn->start);
if (debug['A']) {
fprintf(stderr, "\n> After ABI lowering:\n");
printfn(fn, stderr);
}
}
|