avoid some gcc warnings
[qbe.git] / arm64 / abi.c
blob8bc9c2071b72062c2e4a724126a9dbb125bae107
1 #include "all.h"
3 typedef struct Class_ Class;
4 typedef struct Insl Insl;
5 typedef struct Params Params;
7 enum {
8 Cstk = 1, /* pass on the stack */
9 Cptr = 2, /* replaced by a pointer */
12 struct Class_ {
13 char class;
14 char ishfa;
15 struct {
16 char base;
17 uchar size;
18 } hfa;
19 uint size;
20 Typ *t;
21 uchar nreg;
22 uchar ngp;
23 uchar nfp;
24 int reg[4];
25 int cls[4];
28 struct Insl {
29 Ins i;
30 Insl *link;
33 struct Params {
34 uint ngp;
35 uint nfp;
36 uint nstk;
39 static int gpreg[12] = {R0, R1, R2, R3, R4, R5, R6, R7};
40 static int fpreg[12] = {V0, V1, V2, V3, V4, V5, V6, V7};
42 /* layout of call's second argument (RCall)
44 * 29 13 9 5 2 0
45 * |0.00|x|xxxx|xxxx|xxx|xx| range
46 * | | | | ` gp regs returned (0..2)
47 * | | | ` fp regs returned (0..4)
48 * | | ` gp regs passed (0..8)
49 * | ` fp regs passed (0..8)
50 * ` is x8 used (0..1)
53 static int
54 isfloatv(Typ *t, char *cls)
56 Field *f;
57 uint n;
59 for (n=0; n<t->nunion; n++)
60 for (f=t->fields[n]; f->type != FEnd; f++)
61 switch (f->type) {
62 case Fs:
63 if (*cls == Kd)
64 return 0;
65 *cls = Ks;
66 break;
67 case Fd:
68 if (*cls == Ks)
69 return 0;
70 *cls = Kd;
71 break;
72 case FTyp:
73 if (isfloatv(&typ[f->len], cls))
74 break;
75 /* fall through */
76 default:
77 return 0;
79 return 1;
82 static void
83 typclass(Class *c, Typ *t, int *gp, int *fp)
85 uint64_t sz;
86 uint n;
88 sz = (t->size + 7) & -8;
89 c->t = t;
90 c->class = 0;
91 c->ngp = 0;
92 c->nfp = 0;
94 if (t->align > 4)
95 err("alignments larger than 16 are not supported");
97 if (t->dark || sz > 16 || sz == 0) {
98 /* large structs are replaced by a
99 * pointer to some caller-allocated
100 * memory */
101 c->class |= Cptr;
102 c->size = 8;
103 return;
106 c->size = sz;
107 c->hfa.base = Kx;
108 c->ishfa = isfloatv(t, &c->hfa.base);
109 c->hfa.size = t->size/(KWIDE(c->hfa.base) ? 8 : 4);
111 if (c->ishfa)
112 for (n=0; n<c->hfa.size; n++, c->nfp++) {
113 c->reg[n] = *fp++;
114 c->cls[n] = c->hfa.base;
116 else
117 for (n=0; n<sz/8; n++, c->ngp++) {
118 c->reg[n] = *gp++;
119 c->cls[n] = Kl;
122 c->nreg = n;
125 static void
126 sttmps(Ref tmp[], int cls[], uint nreg, Ref mem, Fn *fn)
128 static int st[] = {
129 [Kw] = Ostorew, [Kl] = Ostorel,
130 [Ks] = Ostores, [Kd] = Ostored
132 uint n;
133 uint64_t off;
134 Ref r;
136 assert(nreg <= 4);
137 off = 0;
138 for (n=0; n<nreg; n++) {
139 tmp[n] = newtmp("abi", cls[n], fn);
140 r = newtmp("abi", Kl, fn);
141 emit(st[cls[n]], 0, R, tmp[n], r);
142 emit(Oadd, Kl, r, mem, getcon(off, fn));
143 off += KWIDE(cls[n]) ? 8 : 4;
147 static void
148 ldregs(int reg[], int cls[], int n, Ref mem, Fn *fn)
150 int i;
151 uint64_t off;
152 Ref r;
154 off = 0;
155 for (i=0; i<n; i++) {
156 r = newtmp("abi", Kl, fn);
157 emit(Oload, cls[i], TMP(reg[i]), r, R);
158 emit(Oadd, Kl, r, mem, getcon(off, fn));
159 off += KWIDE(cls[i]) ? 8 : 4;
163 static void
164 selret(Blk *b, Fn *fn)
166 int j, k, cty;
167 Ref r;
168 Class cr;
170 j = b->jmp.type;
172 if (!isret(j) || j == Jret0)
173 return;
175 r = b->jmp.arg;
176 b->jmp.type = Jret0;
178 if (j == Jretc) {
179 typclass(&cr, &typ[fn->retty], gpreg, fpreg);
180 cty = (cr.nfp << 2) | cr.ngp;
181 if (cr.class & Cptr) {
182 assert(rtype(fn->retr) == RTmp);
183 blit(fn->retr, 0, r, cr.t->size, fn);
184 } else
185 ldregs(cr.reg, cr.cls, cr.nreg, r, fn);
186 } else {
187 k = j - Jretw;
188 if (KBASE(k) == 0) {
189 emit(Ocopy, k, TMP(R0), r, R);
190 cty = 1;
191 } else {
192 emit(Ocopy, k, TMP(V0), r, R);
193 cty = 1 << 2;
197 b->jmp.arg = CALL(cty);
200 static int
201 argsclass(Ins *i0, Ins *i1, Class *carg, Ref *env)
203 int ngp, nfp, *gp, *fp;
204 Class *c;
205 Ins *i;
207 gp = gpreg;
208 fp = fpreg;
209 ngp = 8;
210 nfp = 8;
211 for (i=i0, c=carg; i<i1; i++, c++)
212 switch (i->op) {
213 case Opar:
214 case Oarg:
215 c->cls[0] = i->cls;
216 c->size = 8;
217 if (KBASE(i->cls) == 0 && ngp > 0) {
218 ngp--;
219 c->reg[0] = *gp++;
220 break;
222 if (KBASE(i->cls) == 1 && nfp > 0) {
223 nfp--;
224 c->reg[0] = *fp++;
225 break;
227 c->class |= Cstk;
228 break;
229 case Oparc:
230 case Oargc:
231 typclass(c, &typ[i->arg[0].val], gp, fp);
232 if (c->class & Cptr) {
233 if (ngp > 0) {
234 ngp--;
235 c->reg[0] = *gp++;
236 c->cls[0] = Kl;
237 break;
239 } else if (c->ngp <= ngp) {
240 if (c->nfp <= nfp) {
241 ngp -= c->ngp;
242 nfp -= c->nfp;
243 gp += c->ngp;
244 fp += c->nfp;
245 break;
246 } else
247 nfp = 0;
248 } else
249 ngp = 0;
250 c->class |= Cstk;
251 break;
252 case Opare:
253 *env = i->to;
254 break;
255 case Oarge:
256 *env = i->arg[0];
257 break;
260 return ((gp-gpreg) << 5) | ((fp-fpreg) << 9);
263 bits
264 arm64_retregs(Ref r, int p[2])
266 bits b;
267 int ngp, nfp;
269 assert(rtype(r) == RCall);
270 ngp = r.val & 3;
271 nfp = (r.val >> 2) & 7;
272 if (p) {
273 p[0] = ngp;
274 p[1] = nfp;
276 b = 0;
277 while (ngp--)
278 b |= BIT(R0+ngp);
279 while (nfp--)
280 b |= BIT(V0+nfp);
281 return b;
284 bits
285 arm64_argregs(Ref r, int p[2])
287 bits b;
288 int ngp, nfp, x8;
290 assert(rtype(r) == RCall);
291 ngp = (r.val >> 5) & 15;
292 nfp = (r.val >> 9) & 15;
293 x8 = (r.val >> 13) & 1;
294 if (p) {
295 p[0] = ngp + x8;
296 p[1] = nfp;
298 b = 0;
299 while (ngp--)
300 b |= BIT(R0+ngp);
301 while (nfp--)
302 b |= BIT(V0+nfp);
303 return b | ((bits)x8 << R8);
306 static void
307 stkblob(Ref r, Class *c, Fn *fn, Insl **ilp)
309 Insl *il;
310 int al;
312 il = alloc(sizeof *il);
313 al = c->t->align - 2; /* NAlign == 3 */
314 if (al < 0)
315 al = 0;
316 il->i = (Ins){Oalloc+al, Kl, r, {getcon(c->t->size, fn)}};
317 il->link = *ilp;
318 *ilp = il;
321 static void
322 selcall(Fn *fn, Ins *i0, Ins *i1, Insl **ilp)
324 Ins *i;
325 Class *ca, *c, cr;
326 int cty, envc;
327 uint n;
328 uint64_t stk, off;
329 Ref r, rstk, env, tmp[4];
331 env = R;
332 ca = alloc((i1-i0) * sizeof ca[0]);
333 cty = argsclass(i0, i1, ca, &env);
335 stk = 0;
336 for (i=i0, c=ca; i<i1; i++, c++) {
337 if (c->class & Cptr) {
338 i->arg[0] = newtmp("abi", Kl, fn);
339 stkblob(i->arg[0], c, fn, ilp);
340 i->op = Oarg;
342 if (c->class & Cstk)
343 stk += c->size;
345 stk += stk & 15;
346 rstk = getcon(stk, fn);
347 if (stk)
348 emit(Oadd, Kl, TMP(SP), TMP(SP), rstk);
350 if (!req(i1->arg[1], R)) {
351 typclass(&cr, &typ[i1->arg[1].val], gpreg, fpreg);
352 stkblob(i1->to, &cr, fn, ilp);
353 cty |= (cr.nfp << 2) | cr.ngp;
354 if (cr.class & Cptr) {
355 cty |= 1 << 13;
356 emit(Ocopy, Kw, R, TMP(R0), R);
357 } else {
358 sttmps(tmp, cr.cls, cr.nreg, i1->to, fn);
359 for (n=0; n<cr.nreg; n++) {
360 r = TMP(cr.reg[n]);
361 emit(Ocopy, cr.cls[n], tmp[n], r, R);
364 } else {
365 if (KBASE(i1->cls) == 0) {
366 emit(Ocopy, i1->cls, i1->to, TMP(R0), R);
367 cty |= 1;
368 } else {
369 emit(Ocopy, i1->cls, i1->to, TMP(V0), R);
370 cty |= 1 << 2;
374 envc = !req(R, env);
375 if (envc)
376 die("todo (arm abi): env calls");
377 emit(Ocall, 0, R, i1->arg[0], CALL(cty));
379 if (cty & (1 << 13))
380 /* struct return argument */
381 emit(Ocopy, Kl, TMP(R8), i1->to, R);
383 for (i=i0, c=ca; i<i1; i++, c++) {
384 if ((c->class & Cstk) != 0)
385 continue;
386 if (i->op != Oargc)
387 emit(Ocopy, *c->cls, TMP(*c->reg), i->arg[0], R);
388 else
389 ldregs(c->reg, c->cls, c->nreg, i->arg[1], fn);
392 off = 0;
393 for (i=i0, c=ca; i<i1; i++, c++) {
394 if ((c->class & Cstk) == 0)
395 continue;
396 if (i->op != Oargc) {
397 r = newtmp("abi", Kl, fn);
398 emit(Ostorel, 0, R, i->arg[0], r);
399 emit(Oadd, Kl, r, TMP(SP), getcon(off, fn));
400 } else
401 blit(TMP(SP), off, i->arg[1], c->size, fn);
402 off += c->size;
404 if (stk)
405 emit(Osub, Kl, TMP(SP), TMP(SP), rstk);
407 for (i=i0, c=ca; i<i1; i++, c++)
408 if (c->class & Cptr)
409 blit(i->arg[0], 0, i->arg[1], c->t->size, fn);
412 static Params
413 selpar(Fn *fn, Ins *i0, Ins *i1)
415 Class *ca, *c, cr;
416 Insl *il;
417 Ins *i;
418 int n, s, cty;
419 Ref r, env, tmp[16], *t;
421 env = R;
422 ca = alloc((i1-i0) * sizeof ca[0]);
423 curi = &insb[NIns];
425 cty = argsclass(i0, i1, ca, &env);
427 il = 0;
428 t = tmp;
429 for (i=i0, c=ca; i<i1; i++, c++) {
430 if (i->op != Oparc || (c->class & (Cptr|Cstk)))
431 continue;
432 sttmps(t, c->cls, c->nreg, i->to, fn);
433 stkblob(i->to, c, fn, &il);
434 t += c->nreg;
436 for (; il; il=il->link)
437 emiti(il->i);
439 if (fn->retty >= 0) {
440 typclass(&cr, &typ[fn->retty], gpreg, fpreg);
441 if (cr.class & Cptr) {
442 fn->retr = newtmp("abi", Kl, fn);
443 emit(Ocopy, Kl, fn->retr, TMP(R8), R);
447 t = tmp;
448 for (i=i0, c=ca, s=2; i<i1; i++, c++) {
449 if (i->op == Oparc
450 && (c->class & Cptr) == 0) {
451 if (c->class & Cstk) {
452 fn->tmp[i->to.val].slot = -s;
453 s += c->size / 8;
454 } else
455 for (n=0; n<c->nreg; n++) {
456 r = TMP(c->reg[n]);
457 emit(Ocopy, c->cls[n], *t++, r, R);
459 } else if (c->class & Cstk) {
460 r = newtmp("abi", Kl, fn);
461 emit(Oload, *c->cls, i->to, r, R);
462 emit(Oaddr, Kl, r, SLOT(-s), R);
463 s++;
464 } else {
465 r = TMP(*c->reg);
466 emit(Ocopy, *c->cls, i->to, r, R);
470 if (!req(R, env))
471 die("todo (arm abi): env calls");
473 return (Params){
474 .nstk = s - 2,
475 .ngp = (cty >> 5) & 15,
476 .nfp = (cty >> 9) & 15
480 static Blk *
481 split(Fn *fn, Blk *b)
483 Blk *bn;
485 ++fn->nblk;
486 bn = blknew();
487 bn->nins = &insb[NIns] - curi;
488 idup(&bn->ins, curi, bn->nins);
489 curi = &insb[NIns];
490 bn->visit = ++b->visit;
491 (void)!snprintf(bn->name, NString, "%s.%d", b->name, b->visit);
492 bn->loop = b->loop;
493 bn->link = b->link;
494 b->link = bn;
495 return bn;
498 static void
499 chpred(Blk *b, Blk *bp, Blk *bp1)
501 Phi *p;
502 uint a;
504 for (p=b->phi; p; p=p->link) {
505 for (a=0; p->blk[a]!=bp; a++)
506 assert(a+1<p->narg);
507 p->blk[a] = bp1;
511 static void
512 selvaarg(Fn *fn, Blk *b, Ins *i)
514 Ref loc, lreg, lstk, nr, r0, r1, c8, c16, c24, c28, ap;
515 Blk *b0, *bstk, *breg;
516 int isgp;
518 c8 = getcon(8, fn);
519 c16 = getcon(16, fn);
520 c24 = getcon(24, fn);
521 c28 = getcon(28, fn);
522 ap = i->arg[0];
523 isgp = KBASE(i->cls) == 0;
525 /* @b [...]
526 r0 =l add ap, (24 or 28)
527 nr =l loadsw r0
528 r1 =w csltw nr, 0
529 jnz r1, @breg, @bstk
530 @breg
531 r0 =l add ap, (8 or 16)
532 r1 =l loadl r0
533 lreg =l add r1, nr
534 r0 =w add nr, (8 or 16)
535 r1 =l add ap, (24 or 28)
536 storew r0, r1
537 @bstk
538 lstk =l loadl ap
539 r0 =l add lstk, 8
540 storel r0, ap
542 %loc =l phi @breg %lreg, @bstk %lstk
543 i->to =(i->cls) load %loc
546 loc = newtmp("abi", Kl, fn);
547 emit(Oload, i->cls, i->to, loc, R);
548 b0 = split(fn, b);
549 b0->jmp = b->jmp;
550 b0->s1 = b->s1;
551 b0->s2 = b->s2;
552 if (b->s1)
553 chpred(b->s1, b, b0);
554 if (b->s2 && b->s2 != b->s1)
555 chpred(b->s2, b, b0);
557 lreg = newtmp("abi", Kl, fn);
558 nr = newtmp("abi", Kl, fn);
559 r0 = newtmp("abi", Kw, fn);
560 r1 = newtmp("abi", Kl, fn);
561 emit(Ostorew, Kw, R, r0, r1);
562 emit(Oadd, Kl, r1, ap, isgp ? c24 : c28);
563 emit(Oadd, Kw, r0, nr, isgp ? c8 : c16);
564 r0 = newtmp("abi", Kl, fn);
565 r1 = newtmp("abi", Kl, fn);
566 emit(Oadd, Kl, lreg, r1, nr);
567 emit(Oload, Kl, r1, r0, R);
568 emit(Oadd, Kl, r0, ap, isgp ? c8 : c16);
569 breg = split(fn, b);
570 breg->jmp.type = Jjmp;
571 breg->s1 = b0;
573 lstk = newtmp("abi", Kl, fn);
574 r0 = newtmp("abi", Kl, fn);
575 emit(Ostorel, Kw, R, r0, ap);
576 emit(Oadd, Kl, r0, lstk, c8);
577 emit(Oload, Kl, lstk, ap, R);
578 bstk = split(fn, b);
579 bstk->jmp.type = Jjmp;
580 bstk->s1 = b0;
582 b0->phi = alloc(sizeof *b0->phi);
583 *b0->phi = (Phi){
584 .cls = Kl, .to = loc,
585 .narg = 2,
586 .blk = {bstk, breg},
587 .arg = {lstk, lreg},
589 r0 = newtmp("abi", Kl, fn);
590 r1 = newtmp("abi", Kw, fn);
591 b->jmp.type = Jjnz;
592 b->jmp.arg = r1;
593 b->s1 = breg;
594 b->s2 = bstk;
595 emit(Ocmpw+Cislt, Kw, r1, nr, CON_Z);
596 emit(Oloadsw, Kl, nr, r0, R);
597 emit(Oadd, Kl, r0, ap, isgp ? c24 : c28);
600 static void
601 selvastart(Fn *fn, Params p, Ref ap)
603 Ref r0, r1, rsave;
605 rsave = newtmp("abi", Kl, fn);
607 r0 = newtmp("abi", Kl, fn);
608 emit(Ostorel, Kw, R, r0, ap);
609 emit(Oadd, Kl, r0, rsave, getcon(p.nstk*8 + 192, fn));
611 r0 = newtmp("abi", Kl, fn);
612 r1 = newtmp("abi", Kl, fn);
613 emit(Ostorel, Kw, R, r1, r0);
614 emit(Oadd, Kl, r1, rsave, getcon(64, fn));
615 emit(Oadd, Kl, r0, ap, getcon(8, fn));
617 r0 = newtmp("abi", Kl, fn);
618 r1 = newtmp("abi", Kl, fn);
619 emit(Ostorel, Kw, R, r1, r0);
620 emit(Oadd, Kl, r1, rsave, getcon(192, fn));
621 emit(Oaddr, Kl, rsave, SLOT(-1), R);
622 emit(Oadd, Kl, r0, ap, getcon(16, fn));
624 r0 = newtmp("abi", Kl, fn);
625 emit(Ostorew, Kw, R, getcon((p.ngp-8)*8, fn), r0);
626 emit(Oadd, Kl, r0, ap, getcon(24, fn));
628 r0 = newtmp("abi", Kl, fn);
629 emit(Ostorew, Kw, R, getcon((p.nfp-8)*16, fn), r0);
630 emit(Oadd, Kl, r0, ap, getcon(28, fn));
633 void
634 arm64_abi(Fn *fn)
636 Blk *b;
637 Ins *i, *i0, *ip;
638 Insl *il;
639 int n;
640 Params p;
642 for (b=fn->start; b; b=b->link)
643 b->visit = 0;
645 /* lower parameters */
646 for (b=fn->start, i=b->ins; i<&b->ins[b->nins]; i++)
647 if (!ispar(i->op))
648 break;
649 p = selpar(fn, b->ins, i);
650 n = b->nins - (i - b->ins) + (&insb[NIns] - curi);
651 i0 = alloc(n * sizeof(Ins));
652 ip = icpy(ip = i0, curi, &insb[NIns] - curi);
653 ip = icpy(ip, i, &b->ins[b->nins] - i);
654 b->nins = n;
655 b->ins = i0;
657 /* lower calls, returns, and vararg instructions */
658 il = 0;
659 b = fn->start;
660 do {
661 if (!(b = b->link))
662 b = fn->start; /* do it last */
663 if (b->visit)
664 continue;
665 curi = &insb[NIns];
666 selret(b, fn);
667 for (i=&b->ins[b->nins]; i!=b->ins;)
668 switch ((--i)->op) {
669 default:
670 emiti(*i);
671 break;
672 case Ocall:
673 case Ovacall:
674 for (i0=i; i0>b->ins; i0--)
675 if (!isarg((i0-1)->op))
676 break;
677 selcall(fn, i0, i, &il);
678 i = i0;
679 break;
680 case Ovastart:
681 selvastart(fn, p, i->arg[0]);
682 break;
683 case Ovaarg:
684 selvaarg(fn, b, i);
685 break;
686 case Oarg:
687 case Oargc:
688 die("unreachable");
690 if (b == fn->start)
691 for (; il; il=il->link)
692 emiti(il->i);
693 b->nins = &insb[NIns] - curi;
694 idup(&b->ins, curi, b->nins);
695 } while (b != fn->start);
697 if (debug['A']) {
698 fprintf(stderr, "\n> After ABI lowering:\n");
699 printfn(fn, stderr);