Skip to content

Commit

Permalink
x64: Fix encoding of adc/sbb with immediate operands (bytecodeallianc…
Browse files Browse the repository at this point in the history
…e#8976)

This isn't reachable from Cranelift today but in some various testing
this is one of the issues I encountered. This fixes apparent copy/paste
bugs with the `Adc` and `Sbb` instructions with immediate operands.
  • Loading branch information
alexcrichton authored Jul 19, 2024
1 parent e7c7253 commit 3d7a1c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cranelift/codegen/src/isa/x64/inst/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ pub(crate) fn emit(
let mut rex = RexFlags::from(*size);
let (opcode_r, opcode_m, subopcode_i) = match op {
AluRmiROpcode::Add => (0x01, 0x03, 0),
AluRmiROpcode::Adc => (0x11, 0x03, 0),
AluRmiROpcode::Adc => (0x11, 0x03, 2),
AluRmiROpcode::Sub => (0x29, 0x2B, 5),
AluRmiROpcode::Sbb => (0x19, 0x2B, 5),
AluRmiROpcode::Sbb => (0x19, 0x2B, 3),
AluRmiROpcode::And => (0x21, 0x23, 4),
AluRmiROpcode::Or => (0x09, 0x0B, 1),
AluRmiROpcode::Xor => (0x31, 0x33, 6),
Expand Down
30 changes: 30 additions & 0 deletions cranelift/codegen/src/isa/x64/inst/emit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,36 @@ fn test_x64_emit() {
"4520FF",
"andb %r15b, %r15b, %r15b",
));
insns.push((
Inst::alu_rmi_r(
OperandSize::Size32,
AluRmiROpcode::Sbb,
RegMemImm::reg(r14),
w_r15,
),
"4519F7",
"sbbl %r15d, %r14d, %r15d",
));
insns.push((
Inst::alu_rmi_r(
OperandSize::Size64,
AluRmiROpcode::Sbb,
RegMemImm::imm(0),
w_r15,
),
"4983DF00",
"sbbq %r15, $0, %r15",
));
insns.push((
Inst::alu_rmi_r(
OperandSize::Size64,
AluRmiROpcode::Adc,
RegMemImm::imm(0),
w_r15,
),
"4983D700",
"adcq %r15, $0, %r15",
));

// ========================================================
// AluRM
Expand Down

0 comments on commit 3d7a1c8

Please sign in to comment.