From acb223497d84c65139a7eaaac395b42f112249ac Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Tue, 7 Jul 2026 11:07:19 +0200 Subject: x64/ARM64/MIPS64: Fix constant bit shift code generation. Reported by Sergey Kaplun. #1480 --- src/lj_asm_arm64.h | 3 ++- src/lj_asm_mips.h | 3 ++- src/lj_asm_x86.h | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lj_asm_arm64.h b/src/lj_asm_arm64.h index dfc5490fa..d068b153a 100644 --- a/src/lj_asm_arm64.h +++ b/src/lj_asm_arm64.h @@ -1549,7 +1549,8 @@ static void asm_bitshift(ASMState *as, IRIns *ir, A64Ins ai, A64Shift sh) int32_t shmask = irt_is64(ir->t) ? 63 : 31; if (irref_isk(ir->op2)) { /* Constant shifts. */ Reg left, dest = ra_dest(as, ir, RSET_GPR); - int32_t shift = (IR(ir->op2)->i & shmask); + IRIns *irr = IR(ir->op2); + int32_t shift = ((irr->o == IR_KINT ? irr->i : (int32_t)ir_kint64(irr)->u64) & shmask); IRIns *irl = IR(ir->op1); if (shmask == 63) ai += A64I_UBFMx - A64I_UBFMw; diff --git a/src/lj_asm_mips.h b/src/lj_asm_mips.h index a54af233f..7565f442e 100644 --- a/src/lj_asm_mips.h +++ b/src/lj_asm_mips.h @@ -2089,7 +2089,8 @@ static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) { Reg dest = ra_dest(as, ir, RSET_GPR); if (irref_isk(ir->op2)) { /* Constant shifts. */ - uint32_t shift = (uint32_t)IR(ir->op2)->i; + IRIns *irr = IR(ir->op2); + uint32_t shift = (uint32_t)(LJ_32 || irr->o == IR_KINT) ? (uint32_t)irr->i : (uint32_t)ir_kint64(irr)->u64; if (LJ_64 && irt_is64(ir->t)) mik |= (shift & 32) ? MIPSI_D32 : MIPSI_D; emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), (shift & 31)); diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h index 3d68baefd..943cf2b77 100644 --- a/src/lj_asm_x86.h +++ b/src/lj_asm_x86.h @@ -2318,9 +2318,10 @@ static void asm_bitshift(ASMState *as, IRIns *ir, x86Shift xs, x86Op xv) IRIns *irr = IR(rref); Reg dest; if (irref_isk(rref)) { /* Constant shifts. */ - int shift; + int32_t shift; dest = ra_dest(as, ir, RSET_GPR); - shift = irr->i & (irt_is64(ir->t) ? 63 : 31); + shift = (LJ_32 || irr->o == IR_KINT) ? irr->i : (int32_t)ir_kint64(irr)->u64; + shift &= (irt_is64(ir->t) ? 63 : 31); if (!xv && shift && (as->flags & JIT_F_BMI2)) { Reg left = asm_fuseloadm(as, ir->op1, RSET_GPR, irt_is64(ir->t)); if (left != dest) { /* BMI2 rotate right by constant. */ -- cgit v1.2.3-55-g6feb