cranelift: Sign extend Imm64 immediates

When an instruction has an `Imm64` immediate, but operates on values of a
narrower width, we need to sign extend the value.

Fixes #1095
This commit is contained in:
Nick Fitzgerald
2020-05-12 14:19:45 -07:00
parent 0bc0503f3f
commit 9b867b09c7
4 changed files with 103 additions and 1 deletions

View File

@@ -274,6 +274,39 @@ impl InstructionData {
}
}
}
#[inline]
pub(crate) fn sign_extend_immediates(&mut self, ctrl_typevar: Type) {
if ctrl_typevar.is_invalid() {
return;
}
let bit_width = ctrl_typevar.bits();
match self {
Self::BinaryImm {
opcode,
arg: _,
imm,
} => {
if matches!(opcode, Opcode::SdivImm | Opcode::SremImm) {
imm.sign_extend_from_width(bit_width);
}
}
Self::IntCompareImm {
opcode,
arg: _,
cond,
imm,
} => {
debug_assert_eq!(*opcode, Opcode::IcmpImm);
if cond.unsigned() != *cond {
imm.sign_extend_from_width(bit_width);
}
}
_ => {}
}
}
}
/// Information about branch and jump instructions.