From ce2d6ceba62a368dc83c74ce3f68aa7cd547d82d Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Thu, 29 Jun 2023 23:49:20 +0300 Subject: [PATCH] Fixed non-boolean constant GUARD condition checks --- ir_aarch64.dasc | 5 +++-- ir_private.h | 12 ++++++++++++ ir_x86.dasc | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ir_aarch64.dasc b/ir_aarch64.dasc index c7cc5fe..35594bf 100644 --- a/ir_aarch64.dasc +++ b/ir_aarch64.dasc @@ -3956,8 +3956,9 @@ static void ir_emit_guard(ir_ctx *ctx, ir_ref def, ir_insn *insn) IR_ASSERT(IR_IS_TYPE_INT(type)); if (IR_IS_CONST_REF(insn->op2)) { - if ((insn->op == IR_GUARD && insn->op2 == IR_FALSE) || - (insn->op == IR_GUARD_NOT && insn->op2 == IR_TRUE)) { + bool is_true = ir_ref_is_true(ctx, insn->op2); + + if ((insn->op == IR_GUARD && !is_true) || (insn->op == IR_GUARD_NOT && is_true)) { if (IR_IS_CONST_REF(insn->op3)) { void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]); diff --git a/ir_private.h b/ir_private.h index 093d4f6..acfb72c 100644 --- a/ir_private.h +++ b/ir_private.h @@ -773,6 +773,18 @@ IR_ALWAYS_INLINE bool ir_const_is_true(const ir_insn *v) return 0; } +IR_ALWAYS_INLINE bool ir_ref_is_true(ir_ctx *ctx, ir_ref ref) +{ + if (ref == IR_TRUE) { + return 1; + } else if (ref == IR_FALSE) { + return 0; + } else { + IR_ASSERT(IR_IS_CONST_REF(ref)); + return ir_const_is_true(&ctx->ir_base[ref]); + } +} + /* IR OP flags */ #define IR_OP_FLAG_OPERANDS_SHIFT 3 diff --git a/ir_x86.dasc b/ir_x86.dasc index 2dfe4e4..50175b0 100644 --- a/ir_x86.dasc +++ b/ir_x86.dasc @@ -6840,8 +6840,9 @@ static bool ir_emit_guard(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn) IR_ASSERT(IR_IS_TYPE_INT(type)); if (IR_IS_CONST_REF(insn->op2)) { - if ((insn->op == IR_GUARD && insn->op2 == IR_FALSE) || - (insn->op == IR_GUARD_NOT && insn->op2 == IR_TRUE)) { + bool is_true = ir_ref_is_true(ctx, insn->op2); + + if ((insn->op == IR_GUARD && !is_true) || (insn->op == IR_GUARD_NOT && is_true)) { addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]); if (sizeof(void*) == 4 || IR_MAY_USE_32BIT_ADDR(addr)) { | jmp aword &addr