Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ pub(crate) const fn modrm(mode: u8, reg: u8, rm: u8) -> u8 {
(mode << 6) | ((reg & 7) << 3) | (rm & 7)
}

/// RBP and R13 need a displacement even at zero. RSP and R12 need a SIB byte.
pub(crate) fn append_base_displacement(bytes: &mut Vec<u8>, reg: u8, base: u8, disp: i32) {
let mode = if disp == 0 && base & 7 != 5 {
0
} else if i8::try_from(disp).is_ok() {
1
} else {
2
};
bytes.push(modrm(mode, reg, base));
if base & 7 == 4 {
bytes.push(0x24);
}
match mode {
0 => {}
1 => bytes.push(disp as u8),
_ => bytes.extend(disp.to_le_bytes()),
}
}

pub(crate) fn rr64(bytes: &mut Vec<u8>, opcode: u8, reg: u8, rm: u8) {
bytes.extend([rex64(reg, 0, rm), opcode, modrm(3, reg, rm)]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::emit::{modrm, rex, rex64, rr64};
use super::emit::{append_base_displacement, modrm, rex, rex64, rr64};
use super::request_validation::{guard_canonical, resolve_registers};
use super::{
MachineAlternativeFamily as Family, MachineAlternativeKey, MachineEncodedControlEffect,
Expand Down Expand Up @@ -119,11 +119,8 @@ fn ordinary_request(
if narrow_load {
bytes.push(0x0f);
}
bytes.extend([opcode, modrm(2, reg, base)]);
if base & 7 == 4 {
bytes.push(0x24);
}
bytes.extend(disp.to_le_bytes());
bytes.push(opcode);
append_base_displacement(&mut bytes, reg, base, disp);

let catalog = crate::x86_64_register_constraint_catalog_for(physical);
let constraint = catalog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::emit::{modrm, rex64};
use super::super::emit::{append_base_displacement, modrm, rex64};
use super::super::request_validation::byte_loop_registers as loop_regs;
use super::super::{
MachineAlternativeFamily as Family, MachineAlternativeKey, MachineEncodedControlEffect,
Expand Down Expand Up @@ -28,27 +28,6 @@ fn emit_vex3(
bytes.extend([0xc4, byte1, byte2, opcode]);
}

fn emit_mem_base_disp(bytes: &mut Vec<u8>, reg: u8, base: u8, disp: i32) {
if disp == 0 && (base & 7) != 5 {
bytes.push(modrm(0, reg, base));
if (base & 7) == 4 {
bytes.push(0x24);
}
} else if let Ok(disp8) = i8::try_from(disp) {
bytes.push(modrm(1, reg, base));
if (base & 7) == 4 {
bytes.push(0x24);
}
bytes.push(disp8 as u8);
} else {
bytes.push(modrm(2, reg, base));
if (base & 7) == 4 {
bytes.push(0x24);
}
bytes.extend(disp.to_le_bytes());
}
}

fn emit_mem_sib(bytes: &mut Vec<u8>, reg: u8, base: u8, index: u8, disp: i32) -> Result<(), Error> {
if index == 4 {
return Err(Error::EncodedFormMismatch);
Expand All @@ -67,7 +46,7 @@ fn emit_mem_sib(bytes: &mut Vec<u8>, reg: u8, base: u8, index: u8, disp: i32) ->

fn emit_vmovups_load_ymm(bytes: &mut Vec<u8>, dst_ymm: u8, base: u8, disp: i32) {
emit_vex3(bytes, 1, 0, false, true, 0, dst_ymm, 0, base, 0x10);
emit_mem_base_disp(bytes, dst_ymm, base, disp);
append_base_displacement(bytes, dst_ymm, base, disp);
}

fn emit_vmovups_load_ymm_sib(
Expand All @@ -83,7 +62,7 @@ fn emit_vmovups_load_ymm_sib(

fn emit_vmovups_store_xmm(bytes: &mut Vec<u8>, base: u8, src_xmm: u8) {
emit_vex3(bytes, 1, 0, false, false, 0, src_xmm, 0, base, 0x11);
emit_mem_base_disp(bytes, src_xmm, base, 0);
append_base_displacement(bytes, src_xmm, base, 0);
}

fn emit_vmovss_load_sib(
Expand All @@ -99,7 +78,7 @@ fn emit_vmovss_load_sib(

fn emit_vmovss_store(bytes: &mut Vec<u8>, base: u8, src_xmm: u8) {
emit_vex3(bytes, 1, 2, false, false, 0, src_xmm, 0, base, 0x11);
emit_mem_base_disp(bytes, src_xmm, base, 0);
append_base_displacement(bytes, src_xmm, base, 0);
}

fn emit_vmulps_ymm(bytes: &mut Vec<u8>, dst: u8, lhs: u8, rhs: u8) {
Expand Down Expand Up @@ -144,7 +123,7 @@ fn emit_vzeroupper(bytes: &mut Vec<u8>) {

fn emit_mov_r64_mem(bytes: &mut Vec<u8>, dst: u8, base: u8, disp: i32) {
bytes.extend([rex64(dst, 0, base), 0x8b]);
emit_mem_base_disp(bytes, dst, base, disp);
append_base_displacement(bytes, dst, base, disp);
}

fn emit_clamp_u64_imm32(bytes: &mut Vec<u8>, reg: u8, max_val: i32) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,138 @@ fn views(p: &ValidatedPhysicalRegisterModel, names: &[&str]) -> Vec<RegisterView
.collect()
}

#[test]
fn ordinary_memory_forms_preserve_width_and_use_the_shortest_fixed_displacement() {
let physical = phys();
for (kind, family, base, displacement, expected) in [
(
Kind::Load8 { byte_offset: 0 },
Family::Load8,
"rsi",
0,
&[0x48, 0x0f, 0xb6, 0x06][..],
),
(
Kind::Load16 { byte_offset: 0 },
Family::Load16,
"rbp",
0,
&[0x48, 0x0f, 0xb7, 0x45, 0],
),
(
Kind::Load32 { byte_offset: 0 },
Family::Load32,
"r13",
0,
&[0x41, 0x8b, 0x45, 0],
),
(
Kind::Load64 { byte_offset: 0 },
Family::Load64,
"r12",
0,
&[0x49, 0x8b, 0x04, 0x24],
),
(
Kind::AddressOffset { byte_offset: 128 },
Family::AddressOffset,
"r12",
128,
&[0x49, 0x8d, 0x84, 0x24, 0x80, 0, 0, 0],
),
(
Kind::Store {
byte_offset: 0,
byte_size: 1,
},
Family::Store,
"rsi",
0,
&[0x40, 0x88, 0x06],
),
(
Kind::Store {
byte_offset: 127,
byte_size: 2,
},
Family::Store,
"r12",
127,
&[0x66, 0x41, 0x89, 0x44, 0x24, 0x7f],
),
(
Kind::Store {
byte_offset: 1,
byte_size: 4,
},
Family::Store,
"r13",
1,
&[0x41, 0x89, 0x45, 1],
),
(
Kind::Store {
byte_offset: 128,
byte_size: 8,
},
Family::Store,
"rbp",
128,
&[0x48, 0x89, 0x85, 0x80, 0, 0, 0],
),
] {
let key = Key { family, variant: 0 };
let operands = views(physical, &[base, "rax"]);
let encoded =
encode_x86_64_selected_memory_form(physical, kind, key, &operands, displacement)
.unwrap();
assert_eq!(encoded.bytes(), expected);
assert_eq!(
validate_x86_64_selected_memory_form(
physical,
kind,
key,
&operands,
displacement,
expected
)
.unwrap(),
encoded
);
let mut changed = expected.to_vec();
*changed.last_mut().unwrap() ^= 1;
assert!(
validate_x86_64_selected_memory_form(
physical,
kind,
key,
&operands,
displacement,
&changed
)
.is_err()
);
}
}

#[test]
fn base_displacement_handles_stack_and_signed_byte_boundaries() {
// RSP is reserved for the frame and cannot be passed as an ordinary virtual operand.
for (base, disp, expected) in [
(4, 0, &[0x04, 0x24][..]),
(4, -128, &[0x44, 0x24, 0x80]),
(4, -129, &[0x84, 0x24, 0x7f, 0xff, 0xff, 0xff]),
(5, 0, &[0x45, 0]),
(13, 0, &[0x45, 0]),
(12, 127, &[0x44, 0x24, 0x7f]),
(12, 128, &[0x84, 0x24, 0x80, 0, 0, 0]),
] {
let mut bytes = Vec::new();
super::emit::append_base_displacement(&mut bytes, 0, base, disp);
assert_eq!(bytes, expected);
}
}

#[test]
fn scalar_and_control_golden_vectors_round_trip_and_reject_mutations() {
let p = phys();
Expand Down Expand Up @@ -338,7 +470,7 @@ fn scalar_and_control_golden_vectors_round_trip_and_reject_mutations() {
let planned = catalog
.declarations
.iter()
.flat_map(|declaration| &declaration.alternatives)
.flat_map(|declaration| declaration.alternatives.iter())
.find(|alternative| alternative.key == key)
.unwrap();
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ fn expected_size(
resolved(width * 8 + (width - 1) * 7, width * 9 + (width - 1) * 7)
}
StorePacked => resolved(32, 83),
Store => resolved(7, 9),
AddressOffset | Load32 | Load64 => resolved(7, 8),
Load8 | Load16 => resolved(8, 9),
Store => resolved(3, 9),
AddressOffset | Load32 | Load64 => resolved(3, 8),
Load8 | Load16 => resolved(4, 9),
Load8Indexed => MachineSizeKnowledge::ExactBytes(9),
// REX, opcode, ModRM and SIB, plus a zero disp8 for an rbp or r13
// base; the 16-bit form adds the 0F escape byte.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(super) fn declaration(
byte_count: u16::from(form.bytes),
}
},
crate::encoder_resolved(7, Some(9)),
crate::encoder_resolved(3, Some(9)),
),

Semantic::PackedAddI16x8
Expand Down Expand Up @@ -551,15 +551,15 @@ pub(super) fn declaration(
vec![0, 1],
vec![],
MachineEncodedMemoryEffect::WritePointerV1 { pointer_operand: 0 },
crate::encoder_resolved(7, Some(9)),
crate::encoder_resolved(3, Some(9)),
),
Semantic::AddressOffset => (
MachineMemoryEffect::NoneV1,
MachineTrapBehavior::NeverV1,
vec![0],
vec![1],
MachineEncodedMemoryEffect::NoneV1,
crate::encoder_resolved(7, Some(8)),
crate::encoder_resolved(3, Some(8)),
),
Semantic::Load16Indexed | Semantic::Load32Indexed | Semantic::Load64Indexed => {
let element_bytes = semantic.indexed_load_bytes().expect("indexed load width");
Expand Down Expand Up @@ -598,7 +598,7 @@ pub(super) fn declaration(
pointer_operand: 0,
byte_count: if semantic == Semantic::Load8 { 1 } else { 2 },
},
crate::encoder_resolved(8, Some(9)),
crate::encoder_resolved(4, Some(9)),
),
Semantic::Load32 | Semantic::Load64 => (
MachineMemoryEffect::ReadPointerV1,
Expand All @@ -609,7 +609,7 @@ pub(super) fn declaration(
pointer_operand: 0,
byte_count: if semantic == Semantic::Load32 { 4 } else { 8 },
},
crate::encoder_resolved(7, Some(8)),
crate::encoder_resolved(3, Some(8)),
),
Semantic::Store64 => (
MachineMemoryEffect::WriteFrameStorageV1,
Expand Down
Loading