harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 6f822c4aeb48683f63c58afcc654e717d20ca7a8
parent bd606e7cc64ffad5cdbd2bcb67cceb7c3db8c262
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed,  4 Aug 2021 13:20:54 +0200

gen: simplify label generation

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Minclude/gen.h | 2++
Msrc/gen.c | 13++++---------
Msrc/genutil.c | 13+++++++++++--
3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/include/gen.h b/include/gen.h @@ -72,6 +72,8 @@ struct qbe_value mkcopy(struct gen_context *ctx, struct qbe_value mkqtmp(struct gen_context *ctx, const struct qbe_type *qtype, const char *fmt); struct qbe_value mkrtfunc(struct gen_context *ctx, const char *name); +struct qbe_value mklabel(struct gen_context *ctx, + struct qbe_statement *stmt, const char *fmt); // qinstr.c enum qbe_instr alloc_for_align(size_t align); diff --git a/src/gen.c b/src/gen.c @@ -261,17 +261,12 @@ gen_expr_assert(struct gen_context *ctx, const struct expression *expr) return gv_void; } - struct qbe_statement failedl = {0}, passedl = {0}; - struct qbe_value bfailed = {0}, bpassed = {0}; - struct qbe_value rtfunc = mkrtfunc(ctx, "rt.abort"); struct gen_value msg; - + struct qbe_statement failedl, passedl; + struct qbe_value rtfunc = mkrtfunc(ctx, "rt.abort"); if (expr->assert.cond) { - bfailed.kind = QV_LABEL; - bfailed.name = strdup(genl(&failedl, &ctx->id, "failed.%d")); - bpassed.kind = QV_LABEL; - bpassed.name = strdup(genl(&passedl, &ctx->id, "passed.%d")); - + struct qbe_value bfailed = mklabel(ctx, &failedl, "failed.%d"); + struct qbe_value bpassed = mklabel(ctx, &passedl, "passed.%d"); struct gen_value cond = gen_expr(ctx, expr->assert.cond); struct qbe_value qcond = mkqval(ctx, &cond); pushi(ctx->current, NULL, Q_JNZ, &qcond, &bpassed, &bfailed, NULL); diff --git a/src/genutil.c b/src/genutil.c @@ -54,8 +54,8 @@ mkcopy(struct gen_context *ctx, struct gen_value *value, const char *fmt) return copy; } -struct qbe_value mkqtmp(struct gen_context *ctx, - const struct qbe_type *qtype, const char *fmt) +struct qbe_value +mkqtmp(struct gen_context *ctx, const struct qbe_type *qtype, const char *fmt) { return (struct qbe_value){ .kind = QV_TEMPORARY, @@ -83,3 +83,12 @@ mkrtfunc(struct gen_context *ctx, const char *name) .type = ctx->arch.ptr, }; } + +struct qbe_value +mklabel(struct gen_context *ctx, struct qbe_statement *stmt, const char *fmt) +{ + struct qbe_value val; + val.kind = QV_LABEL; + val.name = strdup(genl(stmt, &ctx->id, "failed.%d")); + return val; +}