Skip to content

Commit

Permalink
Merge pull request #2857 from workingjubilee/byte-less
Browse files Browse the repository at this point in the history
Factor out byteorder in cranelift
  • Loading branch information
peterhuene authored Apr 23, 2021
2 parents 9637bc5 + a8c956e commit 4a830b1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cranelift/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ bincode = { version = "1.2.1", optional = true }
gimli = { version = "0.23.0", default-features = false, features = ["write"], optional = true }
smallvec = { version = "1.6.1" }
thiserror = "1.0.4"
byteorder = { version = "1.3.2", default-features = false }
peepmatic = { path = "../peepmatic", optional = true, version = "0.73.0" }
peepmatic-traits = { path = "../peepmatic/crates/traits", optional = true, version = "0.73.0" }
peepmatic-runtime = { path = "../peepmatic/crates/runtime", optional = true, version = "0.73.0" }
Expand Down
21 changes: 10 additions & 11 deletions cranelift/codegen/src/isa/unwind/winx64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::isa::unwind::input;
use crate::result::{CodegenError, CodegenResult};
use alloc::vec::Vec;
use byteorder::{ByteOrder, LittleEndian};
use log::warn;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -31,13 +30,13 @@ impl<'a> Writer<'a> {
self.offset += 1;
}

fn write_u16<T: ByteOrder>(&mut self, v: u16) {
T::write_u16(&mut self.buf[self.offset..(self.offset + 2)], v);
fn write_u16_le(&mut self, v: u16) {
self.buf[self.offset..(self.offset + 2)].copy_from_slice(&v.to_le_bytes());
self.offset += 2;
}

fn write_u32<T: ByteOrder>(&mut self, v: u32) {
T::write_u32(&mut self.buf[self.offset..(self.offset + 4)], v);
fn write_u32_le(&mut self, v: u32) {
self.buf[self.offset..(self.offset + 4)].copy_from_slice(&v.to_le_bytes());
self.offset += 4;
}
}
Expand Down Expand Up @@ -121,11 +120,11 @@ impl UnwindCode {
let scaled_stack_offset = stack_offset / 16;
if scaled_stack_offset <= core::u16::MAX as u32 {
writer.write_u8((*reg << 4) | (op_small as u8));
writer.write_u16::<LittleEndian>(scaled_stack_offset as u16);
writer.write_u16_le(scaled_stack_offset as u16);
} else {
writer.write_u8((*reg << 4) | (op_large as u8));
writer.write_u16::<LittleEndian>(*stack_offset as u16);
writer.write_u16::<LittleEndian>((stack_offset >> 16) as u16);
writer.write_u16_le(*stack_offset as u16);
writer.write_u16_le((stack_offset >> 16) as u16);
}
}
Self::StackAlloc {
Expand All @@ -143,10 +142,10 @@ impl UnwindCode {
);
} else if *size <= LARGE_ALLOC_16BIT_MAX_SIZE {
writer.write_u8(UnwindOperation::LargeStackAlloc as u8);
writer.write_u16::<LittleEndian>((*size / 8) as u16);
writer.write_u16_le((*size / 8) as u16);
} else {
writer.write_u8((1 << 4) | (UnwindOperation::LargeStackAlloc as u8));
writer.write_u32::<LittleEndian>(*size);
writer.write_u32_le(*size);
}
}
Self::SetFPReg { instruction_offset } => {
Expand Down Expand Up @@ -248,7 +247,7 @@ impl UnwindInfo {

// To keep a 32-bit alignment, emit 2 bytes of padding if there's an odd number of 16-bit nodes
if (node_count & 1) == 1 {
writer.write_u16::<LittleEndian>(0);
writer.write_u16_le(0);
}

// Ensure the correct number of bytes was emitted
Expand Down
1 change: 0 additions & 1 deletion cranelift/filetests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ cranelift-interpreter = { path = "../interpreter", version = "0.73.0" }
cranelift-native = { path = "../native", version = "0.73.0" }
cranelift-reader = { path = "../reader", version = "0.73.0" }
cranelift-preopt = { path = "../preopt", version = "0.73.0" }
byteorder = { version = "1.3.2", default-features = false }
file-per-thread-logger = "0.1.2"
filecheck = "0.5.0"
gimli = { version = "0.23.0", default-features = false, features = ["read"] }
Expand Down
32 changes: 16 additions & 16 deletions cranelift/filetests/src/test_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl SubTest for TestUnwind {
}

mod windowsx64 {
use byteorder::{ByteOrder, LittleEndian};
use std::fmt::Write;

pub fn dump<W: Write>(text: &mut W, mem: &[u8]) {
Expand Down Expand Up @@ -165,23 +164,24 @@ mod windowsx64 {
let op_and_info = mem[1];
let op = UnwindOperation::from(op_and_info & 0xF);
let info = (op_and_info & 0xF0) >> 4;

let value = match op {
UnwindOperation::LargeStackAlloc => match info {
0 => UnwindValue::U16(LittleEndian::read_u16(&mem[2..])),
1 => UnwindValue::U32(LittleEndian::read_u32(&mem[2..])),
_ => panic!("unexpected stack alloc info value"),
},
UnwindOperation::SaveNonvolatileRegister => {
UnwindValue::U16(LittleEndian::read_u16(&mem[2..]))
}
UnwindOperation::SaveNonvolatileRegisterFar => {
UnwindValue::U32(LittleEndian::read_u32(&mem[2..]))
let unwind_le_bytes = |bytes| match (bytes, &mem[2..]) {
(2, &[b0, b1, ..]) => UnwindValue::U16(u16::from_le_bytes([b0, b1])),
(4, &[b0, b1, b2, b3, ..]) => {
UnwindValue::U32(u32::from_le_bytes([b0, b1, b2, b3]))
}
UnwindOperation::SaveXmm128 => UnwindValue::U16(LittleEndian::read_u16(&mem[2..])),
UnwindOperation::SaveXmm128Far => {
UnwindValue::U32(LittleEndian::read_u32(&mem[2..]))
(_, _) => panic!("not enough bytes to unwind value"),
};

let value = match (&op, info) {
(UnwindOperation::LargeStackAlloc, 0) => unwind_le_bytes(2),
(UnwindOperation::LargeStackAlloc, 1) => unwind_le_bytes(4),
(UnwindOperation::LargeStackAlloc, _) => {
panic!("unexpected stack alloc info value")
}
(UnwindOperation::SaveNonvolatileRegister, _) => unwind_le_bytes(2),
(UnwindOperation::SaveNonvolatileRegisterFar, _) => unwind_le_bytes(4),
(UnwindOperation::SaveXmm128, _) => unwind_le_bytes(2),
(UnwindOperation::SaveXmm128Far, _) => unwind_le_bytes(4),
_ => UnwindValue::None,
};

Expand Down

0 comments on commit 4a830b1

Please sign in to comment.