commit b6c92a66ff54e467c4e6a17bdafb47e8ac95e311
parent 54e90c96c0b78b2b80b07563cf9f25cb2574e2e1
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 2 Aug 2021 18:42:21 +0200
gen: introduce mkqtmp, removes spurious copy
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
3 files changed, 17 insertions(+), 8 deletions(-)
diff --git a/include/gen.h b/include/gen.h
@@ -68,7 +68,9 @@ struct gen_value mktemp(struct gen_context *ctx,
struct qbe_value mkqval(struct gen_context *ctx, struct gen_value *value);
struct qbe_value mklval(struct gen_context *ctx, struct gen_value *value);
struct qbe_value mkcopy(struct gen_context *ctx,
- struct gen_value *value, const char *fmt);
+ struct gen_value *value, const char *fmt);
+struct qbe_value mkqtmp(struct gen_context *ctx,
+ const struct qbe_type *qtype, const char *fmt);
// qinstr.c
enum qbe_instr alloc_for_align(size_t align);
diff --git a/src/gen.c b/src/gen.c
@@ -178,13 +178,14 @@ gen_access_field(struct gen_context *ctx, const struct expression *expr)
{
const struct struct_field *field = expr->access.field;
struct gen_value glval = gen_expr(ctx, expr->access._struct);
- struct qbe_value qlval = mkcopy(ctx, &glval, "object.%d");
+ struct qbe_value qlval = mkqval(ctx, &glval);
+ struct qbe_value qfval = mkqtmp(ctx, ctx->arch.ptr, "field.%d");
struct qbe_value offs = constl(field->offset);
- pushi(ctx->current, &qlval, Q_ADD, &qlval, &offs, NULL);
+ pushi(ctx->current, &qfval, Q_ADD, &qlval, &offs, NULL);
return (struct gen_value){
.kind = GV_TEMP,
.type = field->type,
- .name = qlval.name,
+ .name = qfval.name,
};
}
diff --git a/src/genutil.c b/src/genutil.c
@@ -48,13 +48,19 @@ struct qbe_value
mkcopy(struct gen_context *ctx, struct gen_value *value, const char *fmt)
{
struct qbe_value qval = mkqval(ctx, value);
- struct qbe_value copy = {
+ struct qbe_value copy = mkqtmp(ctx, ctx->arch.ptr, fmt);
+ pushi(ctx->current, ©, Q_COPY, &qval, NULL);
+ return copy;
+}
+
+struct qbe_value mkqtmp(struct gen_context *ctx,
+ const struct qbe_type *qtype, const char *fmt)
+{
+ return (struct qbe_value){
.kind = QV_TEMPORARY,
- .type = ctx->arch.ptr,
+ .type = qtype,
.name = gen_name(ctx, fmt),
};
- pushi(ctx->current, ©, Q_COPY, &qval, NULL);
- return copy;
}
struct gen_value