commit e09493582414a36300d137e656ec6a3416d0ccc0
parent 3f2465f7172f16aa32adb09e3e2b1ac4137ebcd4
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 9 Aug 2021 15:44:30 +0200
gen: ensure cap side-effects occur first in alloc
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -374,6 +374,12 @@ gen_alloc_slice_at(struct gen_context *ctx,
//
// The current approach will cause the [4096]int initializer to be
// stack-allocated and copied into the new allocated space.
+ struct qbe_value qcap;
+ if (expr->alloc.cap) {
+ struct gen_value cap = gen_expr(ctx, expr->alloc.cap);
+ qcap = mkqval(ctx, &cap);
+ }
+
struct gen_value init = gen_expr(ctx, expr->alloc.expr);
struct qbe_value qinit = mkqval(ctx, &init);
@@ -397,11 +403,7 @@ gen_alloc_slice_at(struct gen_context *ctx,
default: abort(); // Invariant
}
- struct qbe_value qcap;
- if (expr->alloc.cap) {
- struct gen_value cap = gen_expr(ctx, expr->alloc.cap);
- qcap = mkqval(ctx, &cap);
- } else {
+ if (!expr->alloc.cap) {
qcap = length;
}