Skip to content

Commit ebb775b

Browse files
k0kubunXrXr
andauthored
ZJIT: Fix "immediate value too large" on cmp for x86_64 (ruby#14125)
Co-authored-by: Alan Wu <[email protected]>
1 parent bcd2105 commit ebb775b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

test/ruby/test_zjit.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ def test(a, b) = a == b
283283
}, insns: [:opt_eq], call_threshold: 2
284284
end
285285

286+
def test_opt_eq_with_minus_one
287+
assert_compiles '[false, true]', %q{
288+
def test(a) = a == -1
289+
test(1) # profile opt_eq
290+
[test(0), test(-1)]
291+
}, insns: [:opt_eq], call_threshold: 2
292+
end
293+
286294
def test_opt_neq_dynamic
287295
# TODO(max): Don't split this test; instead, run all tests with and without
288296
# profiling.

zjit/src/backend/x86_64/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl Assembler
381381
mov(cb, Assembler::SCRATCH0, opnd.into());
382382
Assembler::SCRATCH0
383383
} else {
384-
opnd.into()
384+
imm_opnd(*value as i64)
385385
}
386386
},
387387
_ => opnd.into()
@@ -963,7 +963,9 @@ mod tests {
963963
asm.cmp(Opnd::Reg(RAX_REG), Opnd::UImm(0xFF));
964964
asm.compile_with_num_regs(&mut cb, 0);
965965

966-
assert_eq!(format!("{:x}", cb), "4881f8ff000000");
966+
assert_disasm!(cb, "4881f8ff000000", "
967+
0x0: cmp rax, 0xff
968+
");
967969
}
968970

969971
#[test]
@@ -973,7 +975,22 @@ mod tests {
973975
asm.cmp(Opnd::Reg(RAX_REG), Opnd::UImm(0xFFFF_FFFF_FFFF));
974976
asm.compile_with_num_regs(&mut cb, 0);
975977

976-
assert_eq!(format!("{:x}", cb), "49bbffffffffffff00004c39d8");
978+
assert_disasm!(cb, "49bbffffffffffff00004c39d8", "
979+
0x0: movabs r11, 0xffffffffffff
980+
0xa: cmp rax, r11
981+
");
982+
}
983+
984+
#[test]
985+
fn test_emit_cmp_64_bits() {
986+
let (mut asm, mut cb) = setup_asm();
987+
988+
asm.cmp(Opnd::Reg(RAX_REG), Opnd::UImm(0xFFFF_FFFF_FFFF_FFFF));
989+
asm.compile_with_num_regs(&mut cb, 0);
990+
991+
assert_disasm!(cb, "4883f8ff", "
992+
0x0: cmp rax, -1
993+
");
977994
}
978995

979996
#[test]
@@ -1051,7 +1068,9 @@ mod tests {
10511068
asm.test(Opnd::Reg(RAX_REG), Opnd::UImm(0xFF));
10521069
asm.compile_with_num_regs(&mut cb, 0);
10531070

1054-
assert_eq!(format!("{:x}", cb), "f6c0ff");
1071+
assert_disasm!(cb, "48f7c0ff000000", "
1072+
0x0: test rax, 0xff
1073+
");
10551074
}
10561075

10571076
#[test]

0 commit comments

Comments
 (0)