aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2021-10-25 16:55:13 -0700
committerMichael Forney <mforney@mforney.org>2021-10-25 17:01:43 -0700
commit42fc7b33d3987fc394e5c0d5ce7cb606b110296d (patch)
treef58252b4330a2346b606192076cd329d711ba6ec
parente79005956c888e7bafb602df1137f881b34775ec (diff)
qbe: Add helper function for mkinst
This simplifies the creation of allocation instructions in the start block.
-rw-r--r--qbe.c27
1 files 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 *