From 42fc7b33d3987fc394e5c0d5ce7cb606b110296d Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Mon, 25 Oct 2021 16:55:13 -0700 Subject: qbe: Add helper function for mkinst This simplifies the creation of allocation instructions in the start block. --- qbe.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/qbe.c b/qbe.c index 78a2feb..88cd97b 100644 --- a/qbe.c +++ b/qbe.c @@ -225,13 +225,11 @@ static const char *const instname[] = { #undef OP }; -static struct value * -funcinst(struct func *f, int op, int class, struct value *arg0, struct value *arg1) +static struct inst * +mkinst(struct func *f, int op, int class, struct value *arg0, struct value *arg1) { struct inst *inst; - if (f->end->jump.kind) - return NULL; inst = xmalloc(sizeof(*inst)); inst->kind = op; inst->class = class; @@ -241,8 +239,18 @@ funcinst(struct func *f, int op, int class, struct value *arg0, struct value *ar functemp(f, &inst->res); else inst->res.kind = VALUE_NONE; - arrayaddptr(&f->end->insts, inst); + return inst; +} + +static struct value * +funcinst(struct func *f, int op, int class, struct value *arg0, struct value *arg1) +{ + struct inst *inst; + if (f->end->jump.kind) + return NULL; + inst = mkinst(f, op, class, arg0, arg1); + arrayaddptr(&f->end->insts, inst); return &inst->res; } @@ -267,14 +275,9 @@ funcalloc(struct func *f, struct decl *d) default: fatal("internal error: invalid alignment: %d\n", d->align); } - inst = xmalloc(sizeof(*inst)); - inst->kind = op; - functemp(f, &inst->res); - inst->class = ptrclass; - inst->arg[0] = mkintconst(d->type->size); - inst->arg[1] = NULL; - d->value = &inst->res; + inst = mkinst(f, op, ptrclass, mkintconst(size), NULL); arrayaddptr(&f->start->insts, inst); + d->value = &inst->res; } static struct value * -- cgit v1.2.3