aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ir_abi0.c
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2026-03-18 11:33:41 +0100
committerlemon <lsof@mailbox.org>2026-03-18 11:33:41 +0100
commit1d9e19fb3bb941cdc28e9d4c3063d3e213fd8312 (patch)
treee18eddb587f91455a439c0fd4f1bb3b3216ea2df /src/ir_abi0.c
parent1fee6a61abdf2cf332fffbc50bf7adc1842feb40 (diff)
Refactor: use typedefs and CamelCase for aggregate types
Looks nicer
Diffstat (limited to 'src/ir_abi0.c')
-rw-r--r--src/ir_abi0.c134
1 files changed, 67 insertions, 67 deletions
diff --git a/src/ir_abi0.c b/src/ir_abi0.c
index a3687d9..cd7ce27 100644
--- a/src/ir_abi0.c
+++ b/src/ir_abi0.c
@@ -7,10 +7,10 @@
** exactly narg `arg` instructions with no other instructions in between
**/
-struct abiargsvec { vec_of(struct abiarg); };
+typedef vec_of(ABIArg) ABIArgVec;
static int
-abiret(struct abiarg abiret[2], struct abiargsvec *abiargs, uchar *r2off, int *ni, union irtype retty)
+abiret(ABIArg abiret[2], ABIArgVec *abiargs, uchar *r2off, int *ni, IRType retty)
{
short r[2];
uchar cls[2];
@@ -18,7 +18,7 @@ abiret(struct abiarg abiret[2], struct abiargsvec *abiargs, uchar *r2off, int *n
retreg = mctarg->abiret(r, cls, r2off, ni, retty);
if (retty.isagg) {
if (!retreg) {
- vpush(abiargs, ((struct abiarg) { cls2type(KPTR), .reg = r[1] }));
+ vpush(abiargs, ((ABIArg) { cls2type(KPTR), .reg = r[1] }));
if (r[0] == -1) {
memset(abiret, 0, 2*sizeof *abiret);
} else {
@@ -38,32 +38,32 @@ abiret(struct abiarg abiret[2], struct abiargsvec *abiargs, uchar *r2off, int *n
}
static int
-abiarg(struct abiargsvec *abiargs, uchar *r2off, int *ni, int *nf, int *ns, union irtype ty)
+abiarg(ABIArgVec *abiargs, uchar *r2off, int *ni, int *nf, int *ns, IRType ty)
{
short r[2];
uchar cls[2];
int ret = mctarg->abiarg(r, cls, r2off, ni, nf, ns, ty);
if (!ret) { /* in stack */
- vpush(abiargs, ((struct abiarg) { ty, .isstk = 1, .stk = r[0] }));
+ vpush(abiargs, ((ABIArg) { ty, .isstk = 1, .stk = r[0] }));
} else if (ret == 1 && ty.isagg && cls[0] == KPTR) { /* aggregate by pointer */
- vpush(abiargs, ((struct abiarg) { cls2type(cls[0]), .reg = r[0] }));
+ vpush(abiargs, ((ABIArg) { cls2type(cls[0]), .reg = r[0] }));
} else { /* by regs */
- vpush(abiargs, ((struct abiarg) { cls2type(cls[0]), .reg = r[0] }));
+ vpush(abiargs, ((ABIArg) { cls2type(cls[0]), .reg = r[0] }));
if (ret == 2)
- vpush(abiargs, ((struct abiarg) { cls2type(cls[1]), .reg = r[1] }));
+ vpush(abiargs, ((ABIArg) { cls2type(cls[1]), .reg = r[1] }));
}
return ret;
}
-static struct instr
-copyparam(struct function *fn, int *curi, int param, struct abiarg abi)
+static Instr
+copyparam(Function *fn, int *curi, int param, ABIArg abi)
{
- struct instr par = mkinstr(Oparam, abi.ty.cls, mkref(RICON, param), mktyperef(abi.ty));
+ Instr par = mkinstr(Oparam, abi.ty.cls, mkref(RICON, param), mktyperef(abi.ty));
if (!abi.isstk) { /* reg */
assert(!abi.ty.isagg);
return par;
}
- par.r = mktyperef((union irtype){.cls = KPTR});
+ par.r = mktyperef((IRType){.cls = KPTR});
if (!abi.ty.isagg) { /* scalar in stack */
enum op ld;
par.cls = KPTR;
@@ -83,24 +83,24 @@ copyparam(struct function *fn, int *curi, int param, struct abiarg abi)
}
static void
-patchparam(struct function *fn, int *curi, int *param, int tydat, int nabi, struct abiarg abi[2], uchar r2off)
+patchparam(Function *fn, int *curi, int *param, int tydat, int nabi, ABIArg abi[2], uchar r2off)
{
- struct block *blk = fn->entry;
+ Block *blk = fn->entry;
assert(in_range(nabi,1,2));
for (; *curi < blk->ins.n; ++*curi) {
- struct instr *ins = &instrtab[blk->ins.p[*curi]];
+ Instr *ins = &instrtab[blk->ins.p[*curi]];
if (ins->op != Oparam) continue;
assert(ins->r.t == RTYPE
- && ins->r.i == (tydat < 0 ? abi[0].ty : (union irtype){.isagg=1, .dat=tydat}).bits);
+ && ins->r.i == (tydat < 0 ? abi[0].ty : (IRType){.isagg=1, .dat=tydat}).bits);
if (abi[0].ty.isagg || tydat < 0) {
/* aggregate in stack or scalar, just copy */
assert(nabi == 1);
*ins = copyparam(fn, curi, *param, abi[0]);
} else { /* aggregate in registers, materialize */
- union ref alloc, r[2];
- struct instr st;
- const struct typedata *td;
+ Ref alloc, r[2];
+ Instr st;
+ const TypeData *td;
uint nalloc;
uint align;
@@ -124,7 +124,7 @@ patchparam(struct function *fn, int *curi, int *param, int tydat, int nabi, stru
st = mkinstr(cls2store[abi[0].ty.cls], 0, alloc, r[0]);
insertinstr(blk, ++*curi, st);
if (nabi > 1) {
- struct instr tmp = mkinstr(Oadd, KPTR, alloc, mkref(RICON, r2off));
+ Instr tmp = mkinstr(Oadd, KPTR, alloc, mkref(RICON, r2off));
st = mkinstr(cls2store[abi[1].ty.cls], 0, insertinstr(blk, ++*curi, tmp), r[1]);
insertinstr(blk, ++*curi, st);
}
@@ -136,7 +136,7 @@ patchparam(struct function *fn, int *curi, int *param, int tydat, int nabi, stru
}
static void
-load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct abiarg abi[2], uchar r2off, struct block *blk, int *curi)
+load2regs(Ref out[2], IRType typ, Ref src, int nabi, ABIArg abi[2], uchar r2off, Block *blk, int *curi)
{
uint align = typedata[typ.dat].align;
uint siz = typedata[typ.dat].siz;
@@ -153,8 +153,8 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab
/* XXX this generates pretty bad code for small-alignment structs even on platforms where unaligned loads are available.. */
if (align >= 4) {
for (int i = 0; i < nabi; ++i) {
- struct instr ins = {0};
- union ref temp;
+ Instr ins = {0};
+ Ref temp;
switch (ins.cls = abi[i].ty.cls) {
default: assert(0);
case KI32: ins.op = Oloadu32; break;
@@ -165,7 +165,7 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab
if (i == 0)
ins.l = src;
else {
- struct instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, r2off));
+ Instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, r2off));
ins.l = insertinstr(blk, (*curi)++, adr);
}
temp = insertinstr(blk, (*curi)++, ins);
@@ -174,8 +174,8 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab
}
} else {
for (int i = 0; i < nabi; ++i) {
- struct instr ld = {0};
- union ref reg, temp;
+ Instr ld = {0};
+ Ref reg, temp;
uint n = cls2siz[abi[i].ty.cls] / align;
assert(n > 0);
ld.op = Oloadu8 + ilog2(align)*2;
@@ -184,12 +184,12 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab
if (i+o == 0)
ld.l = src;
else {
- struct instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, (i == 0 ? 0 : r2off) + o*align));
+ Instr adr = mkinstr(Oadd, KPTR, src, mkref(RICON, (i == 0 ? 0 : r2off) + o*align));
ld.l = insertinstr(blk, (*curi)++, adr);
}
temp = insertinstr(blk, (*curi)++, ld);
if (o > 0) {
- union ref t = insertinstr(blk, (*curi)++, mkinstr(Oshl, ld.cls, temp, mkref(RICON, o*align*8)));
+ Ref t = insertinstr(blk, (*curi)++, mkinstr(Oshl, ld.cls, temp, mkref(RICON, o*align*8)));
reg = insertinstr(blk, (*curi)++, mkinstr(Oior, ld.cls, reg, t));
} else {
reg = temp;
@@ -202,20 +202,20 @@ load2regs(union ref out[2], union irtype typ, union ref src, int nabi, struct ab
}
static int
-patcharg(struct block *blk, int *icall, struct call *call,
- int argidx, int nabi, struct abiarg abi[2], uchar r2off)
+patcharg(Block *blk, int *icall, IRCall *call,
+ int argidx, int nabi, ABIArg abi[2], uchar r2off)
{
int arginst = *icall - (call->narg - argidx);
- struct instr *arg = &instrtab[blk->ins.p[arginst]];
+ Instr *arg = &instrtab[blk->ins.p[arginst]];
assert(arg->op == Oarg && arg->l.t == RTYPE);
if (ref2type(arg->l).isagg) { /* aggregate argument */
if (abi[0].ty.isagg) { /* aggregate in stack */
/* XXX do this better.. */
/* ptr %dst = arg <stk dst> */
/* (blit %dst, %src) */
- union ref dst = mkref(RTMP, arg - instrtab);
+ Ref dst = mkref(RTMP, arg - instrtab);
uint align = typedata[abi->ty.dat].align, siz = typedata[abi->ty.dat].siz;
- union ref src = arg->r;
+ Ref src = arg->r;
if (src.t == RTMP && oisalloca(instrtab[src.i].op)) {
align = 1 << (instrtab[src.i].op - Oalloca1);
}
@@ -223,9 +223,9 @@ patcharg(struct block *blk, int *icall, struct call *call,
arg->cls = KPTR;
arg->r = mkref(RICON, abi->stk);
for (uint off = 0; off < siz; off += align) {
- union ref sadr = off == 0 ? src : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, src, mkref(RICON, off)));
- union ref tmp = insertinstr(blk, ++arginst, mkinstr(Oloads8+2*ilog2(align), align < 8 ? KI32 : KI64, sadr));
- union ref dadr = off == 0 ? dst : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, dst, mkref(RICON, off)));
+ Ref sadr = off == 0 ? src : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, src, mkref(RICON, off)));
+ Ref tmp = insertinstr(blk, ++arginst, mkinstr(Oloads8+2*ilog2(align), align < 8 ? KI32 : KI64, sadr));
+ Ref dadr = off == 0 ? dst : insertinstr(blk, ++arginst, mkinstr(Oadd, KPTR, dst, mkref(RICON, off)));
insertinstr(blk, ++arginst, mkinstr(Ostorei8+ilog2(align), 0, dadr, tmp));
}
*icall = arginst + (call->narg - argidx);
@@ -234,7 +234,7 @@ patcharg(struct block *blk, int *icall, struct call *call,
arg->cls = KPTR;
return 1;
} else { /* aggregate in registers */
- union ref r[2];
+ Ref r[2];
delinstr(blk, arginst);
load2regs(r, ref2type(arg->l), arg->r, nabi, abi, r2off, blk, &arginst);
for (int i = 0; i < nabi; ++i)
@@ -247,24 +247,24 @@ patcharg(struct block *blk, int *icall, struct call *call,
}
}
void
-abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi)
+abi0_call(Function *fn, Instr *ins, Block *blk, int *curi)
{
- union ref retmem;
- struct abiarg abiargsbuf[32];
- struct abiargsvec abiargs = {VINIT(abiargsbuf, countof(abiargsbuf))};
+ Ref retmem;
+ ABIArg abiargsbuf[32];
+ ABIArgVec abiargs = VINIT(abiargsbuf, countof(abiargsbuf));
bool sretarghidden = 0;
int ni, nf, ns, vararg, nret = 0;
- struct call *call = &calltab.p[ins->r.i];
+ IRCall *call = &calltab.p[ins->r.i];
vararg = call->vararg;
ni = nf = ns = 0;
assert(!ins->cls == !call->ret.bits);
nret = abiret(call->abiret, &abiargs, &call->r2off, &ni, call->ret);
if (call->ret.isagg) { /* adjust struct return */
- union irtype retty = call->ret;
- struct typedata *td = &typedata[retty.dat];
+ IRType retty = call->ret;
+ TypeData *td = &typedata[retty.dat];
uint align = td->align, ralign;
- struct instr alloca;
+ Instr alloca;
int ialloca;
for (int i = 0; i < nret; ++i)
align = align < (ralign = cls2siz[call->abiret[i].ty.cls]) ? ralign : align;
@@ -281,14 +281,14 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi)
if (!nret) /* hidden pointer argument */
insertinstr(blk, (*curi)++ - call->narg,
- mkinstr(Oarg, 0, mktyperef((union irtype){.cls=KPTR}), retmem));
+ mkinstr(Oarg, 0, mktyperef((IRType){.cls=KPTR}), retmem));
}
/* adjust args */
for (int i = 0, i2 = ni + sretarghidden; i < call->narg; ++i) {
int arginst = *curi - (call->narg - i);
- struct instr *arg = &instrtab[blk->ins.p[arginst]];
- union irtype pty = ref2type(arg->l);
+ Instr *arg = &instrtab[blk->ins.p[arginst]];
+ IRType pty = ref2type(arg->l);
uchar r2off;
int first = abiargs.n;
int ret = abiarg(&abiargs, &r2off, &ni, &nf, &ns, pty);
@@ -310,8 +310,8 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi)
* must be emitted for the register allocator to know */
}
} else { /* aggregate returned in regs */
- union ref r[2];
- struct instr ret2;
+ Ref r[2];
+ Instr ret2;
assert(in_range(nret, 1, 2));
ins->cls = call->abiret[0].ty.cls;
r[0] = mkref(RTMP, ins - instrtab);
@@ -320,11 +320,11 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi)
r[1] = insertinstr(blk, ++*curi, ret2);
}
for (int i = 0; i < nret; ++i) {
- struct instr store = { cls2store[call->abiret[i].ty.cls] };
+ Instr store = { cls2store[call->abiret[i].ty.cls] };
if (i == 0) {
store.l = retmem;
} else {
- struct instr addr = mkinstr(Oadd, KPTR, retmem, mkref(RICON, call->r2off));
+ Instr addr = mkinstr(Oadd, KPTR, retmem, mkref(RICON, call->r2off));
store.l = insertinstr(blk, ++*curi, addr);
}
store.r = r[i];
@@ -333,25 +333,25 @@ abi0_call(struct function *fn, struct instr *ins, struct block *blk, int *curi)
}
}
- if (call->ret.isagg) call->ret = (union irtype){0};
+ if (call->ret.isagg) call->ret = (IRType){0};
call->vararg = vararg;
- call->abiarg = alloccopy(fn->arena, abiargs.p, abiargs.n * sizeof(struct abiarg), 0);
+ call->abiarg = alloccopy(fn->arena, abiargs.p, abiargs.n * sizeof(ABIArg), 0);
call->narg = abiargs.n;
vfree(&abiargs);
}
void
-abi0(struct function *fn)
+abi0(Function *fn)
{
- struct abiarg abiargsbuf[32];
+ ABIArg abiargsbuf[32];
uint nparam = typedata[fn->fnty.dat].nmemb;
- const union type *paramty = typedata[fn->fnty.dat].param;
- struct abiargsvec abiargs = {VINIT(abiargsbuf, countof(abiargsbuf))};
+ const Type *paramty = typedata[fn->fnty.dat].param;
+ ABIArgVec abiargs = VINIT(abiargsbuf, countof(abiargsbuf));
int rvovar = -1;
int ni = 0, nf = 0, ns = 0, istart = 0;
uchar r2off;
- struct block *blk;
- union ref sret = {0};
+ Block *blk;
+ Ref sret = {0};
FREQUIRE(FNUSE);
@@ -360,12 +360,12 @@ abi0(struct function *fn)
} else {
fn->nabiret = abiret(fn->abiret, &abiargs, &r2off, &ni, mkirtype(fn->retty));
if (!fn->nabiret && isagg(fn->retty)) { /* ret agg by hidden pointer */
- struct instr param = copyparam(fn, NULL, 0, abiargs.p[0]);
+ Instr param = copyparam(fn, NULL, 0, abiargs.p[0]);
sret = insertinstr(fn->entry, 0, param);
++istart;
/* increment real param ordinals */
for (int i = 1; i < fn->entry->ins.n; ++i) {
- struct instr *ins = &instrtab[fn->entry->ins.p[i]];
+ Instr *ins = &instrtab[fn->entry->ins.p[i]];
if (ins->op == Oparam) ++ins->l.i;
}
}
@@ -373,7 +373,7 @@ abi0(struct function *fn)
/* adjust params */
for (int i = 0, param = abiargs.n; i < nparam; ++i) {
- union irtype pty = mkirtype(paramty[i]);
+ IRType pty = mkirtype(paramty[i]);
int first = abiargs.n;
uchar r2off;
int ret = abiarg(&abiargs, &r2off, &ni, &nf, &ns, pty);
@@ -389,7 +389,7 @@ abi0(struct function *fn)
* (return value optimization (RVO)) */
blk = fn->entry;
do {
- union ref arg = blk->jmp.arg[0];
+ Ref arg = blk->jmp.arg[0];
if (blk->jmp.t != Jret) continue;
if (!arg.bits) continue;
if (arg.t != RTMP || !oisalloca(instrtab[arg.i].op)) {
@@ -412,7 +412,7 @@ abi0(struct function *fn)
do {
/* adjust vaargs and calls */
for (int iinstr = 0; iinstr < blk->ins.n; ++iinstr) {
- struct instr *ins = &instrtab[blk->ins.p[iinstr]];
+ Instr *ins = &instrtab[blk->ins.p[iinstr]];
if (ins->op == Ovastart) mctarg->vastart(fn, blk, &iinstr);
else if (ins->op == Ovaarg) mctarg->vaarg(fn, blk, &iinstr);
else if (ins->op == Ocall) abi0_call(fn, ins, blk, &iinstr);
@@ -422,7 +422,7 @@ abi0(struct function *fn)
if (isagg(fn->retty) && blk->jmp.t == Jret && blk->jmp.arg[0].bits) {
assert(!blk->jmp.arg[1].bits);
if (fn->nabiret) { /* aggregate return in register(s) */
- union ref r[2];
+ Ref r[2];
int curi = blk->ins.n;
load2regs(r, mkirtype(fn->retty), blk->jmp.arg[0], fn->nabiret, fn->abiret, r2off, blk, &curi);
for (int i = 0; i < fn->nabiret; ++i) {
@@ -433,7 +433,7 @@ abi0(struct function *fn)
/* aggregate return (arg[0] is pointer to return value) */
if (rvovar == -1) {
/* blit %sret, %arg */
- union irtype typ = mkirtype(fn->retty);
+ IRType typ = mkirtype(fn->retty);
insertinstr(blk, blk->ins.n, mkarginstr(typ, sret));
insertinstr(blk, blk->ins.n, mkarginstr(typ, blk->jmp.arg[0]));
insertinstr(blk, blk->ins.n, mkintrin(INstructcopy, 0, 2));