Factor out byteorder in cranelift

This removes an existing dependency on the byteorder crate in favor of
using std equivalents directly.

While not an issue for wasmtime per se, cranelift is now part of the
critical path of building and testing Rust, and minimizing dependencies,
even small ones, can help reduce the time and bandwidth required.
This commit is contained in:
Jubilee Young
2021-04-21 20:15:41 -07:00
parent 9637bc5a09
commit a8c956ede1
5 changed files with 26 additions and 31 deletions

2
Cargo.lock generated
View File

@@ -544,7 +544,6 @@ name = "cranelift-codegen"
version = "0.73.0" version = "0.73.0"
dependencies = [ dependencies = [
"bincode", "bincode",
"byteorder",
"cranelift-bforest", "cranelift-bforest",
"cranelift-codegen-meta", "cranelift-codegen-meta",
"cranelift-codegen-shared", "cranelift-codegen-shared",
@@ -591,7 +590,6 @@ name = "cranelift-filetests"
version = "0.73.0" version = "0.73.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"byteorder",
"cranelift-codegen", "cranelift-codegen",
"cranelift-frontend", "cranelift-frontend",
"cranelift-interpreter", "cranelift-interpreter",

View File

@@ -24,7 +24,6 @@ bincode = { version = "1.2.1", optional = true }
gimli = { version = "0.23.0", default-features = false, features = ["write"], optional = true } gimli = { version = "0.23.0", default-features = false, features = ["write"], optional = true }
smallvec = { version = "1.6.1" } smallvec = { version = "1.6.1" }
thiserror = "1.0.4" thiserror = "1.0.4"
byteorder = { version = "1.3.2", default-features = false }
peepmatic = { path = "../peepmatic", optional = true, version = "0.73.0" } peepmatic = { path = "../peepmatic", optional = true, version = "0.73.0" }
peepmatic-traits = { path = "../peepmatic/crates/traits", 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" } peepmatic-runtime = { path = "../peepmatic/crates/runtime", optional = true, version = "0.73.0" }

View File

@@ -3,7 +3,6 @@
use crate::isa::unwind::input; use crate::isa::unwind::input;
use crate::result::{CodegenError, CodegenResult}; use crate::result::{CodegenError, CodegenResult};
use alloc::vec::Vec; use alloc::vec::Vec;
use byteorder::{ByteOrder, LittleEndian};
use log::warn; use log::warn;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -31,13 +30,13 @@ impl<'a> Writer<'a> {
self.offset += 1; self.offset += 1;
} }
fn write_u16<T: ByteOrder>(&mut self, v: u16) { fn write_u16_le(&mut self, v: u16) {
T::write_u16(&mut self.buf[self.offset..(self.offset + 2)], v); self.buf[self.offset..(self.offset + 2)].copy_from_slice(&v.to_le_bytes());
self.offset += 2; self.offset += 2;
} }
fn write_u32<T: ByteOrder>(&mut self, v: u32) { fn write_u32_le(&mut self, v: u32) {
T::write_u32(&mut self.buf[self.offset..(self.offset + 4)], v); self.buf[self.offset..(self.offset + 4)].copy_from_slice(&v.to_le_bytes());
self.offset += 4; self.offset += 4;
} }
} }
@@ -121,11 +120,11 @@ impl UnwindCode {
let scaled_stack_offset = stack_offset / 16; let scaled_stack_offset = stack_offset / 16;
if scaled_stack_offset <= core::u16::MAX as u32 { if scaled_stack_offset <= core::u16::MAX as u32 {
writer.write_u8((*reg << 4) | (op_small as u8)); 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 { } else {
writer.write_u8((*reg << 4) | (op_large as u8)); writer.write_u8((*reg << 4) | (op_large as u8));
writer.write_u16::<LittleEndian>(*stack_offset as u16); writer.write_u16_le(*stack_offset as u16);
writer.write_u16::<LittleEndian>((stack_offset >> 16) as u16); writer.write_u16_le((stack_offset >> 16) as u16);
} }
} }
Self::StackAlloc { Self::StackAlloc {
@@ -143,10 +142,10 @@ impl UnwindCode {
); );
} else if *size <= LARGE_ALLOC_16BIT_MAX_SIZE { } else if *size <= LARGE_ALLOC_16BIT_MAX_SIZE {
writer.write_u8(UnwindOperation::LargeStackAlloc as u8); writer.write_u8(UnwindOperation::LargeStackAlloc as u8);
writer.write_u16::<LittleEndian>((*size / 8) as u16); writer.write_u16_le((*size / 8) as u16);
} else { } else {
writer.write_u8((1 << 4) | (UnwindOperation::LargeStackAlloc as u8)); writer.write_u8((1 << 4) | (UnwindOperation::LargeStackAlloc as u8));
writer.write_u32::<LittleEndian>(*size); writer.write_u32_le(*size);
} }
} }
Self::SetFPReg { instruction_offset } => { Self::SetFPReg { instruction_offset } => {
@@ -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 // 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 { if (node_count & 1) == 1 {
writer.write_u16::<LittleEndian>(0); writer.write_u16_le(0);
} }
// Ensure the correct number of bytes was emitted // Ensure the correct number of bytes was emitted

View File

@@ -16,7 +16,6 @@ cranelift-interpreter = { path = "../interpreter", version = "0.73.0" }
cranelift-native = { path = "../native", version = "0.73.0" } cranelift-native = { path = "../native", version = "0.73.0" }
cranelift-reader = { path = "../reader", version = "0.73.0" } cranelift-reader = { path = "../reader", version = "0.73.0" }
cranelift-preopt = { path = "../preopt", 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" file-per-thread-logger = "0.1.2"
filecheck = "0.5.0" filecheck = "0.5.0"
gimli = { version = "0.23.0", default-features = false, features = ["read"] } gimli = { version = "0.23.0", default-features = false, features = ["read"] }

View File

@@ -72,7 +72,6 @@ impl SubTest for TestUnwind {
} }
mod windowsx64 { mod windowsx64 {
use byteorder::{ByteOrder, LittleEndian};
use std::fmt::Write; use std::fmt::Write;
pub fn dump<W: Write>(text: &mut W, mem: &[u8]) { pub fn dump<W: Write>(text: &mut W, mem: &[u8]) {
@@ -165,23 +164,24 @@ mod windowsx64 {
let op_and_info = mem[1]; let op_and_info = mem[1];
let op = UnwindOperation::from(op_and_info & 0xF); let op = UnwindOperation::from(op_and_info & 0xF);
let info = (op_and_info & 0xF0) >> 4; let info = (op_and_info & 0xF0) >> 4;
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]))
}
(_, _) => panic!("not enough bytes to unwind value"),
};
let value = match op { let value = match (&op, info) {
UnwindOperation::LargeStackAlloc => match info { (UnwindOperation::LargeStackAlloc, 0) => unwind_le_bytes(2),
0 => UnwindValue::U16(LittleEndian::read_u16(&mem[2..])), (UnwindOperation::LargeStackAlloc, 1) => unwind_le_bytes(4),
1 => UnwindValue::U32(LittleEndian::read_u32(&mem[2..])), (UnwindOperation::LargeStackAlloc, _) => {
_ => panic!("unexpected stack alloc info value"), panic!("unexpected stack alloc info value")
},
UnwindOperation::SaveNonvolatileRegister => {
UnwindValue::U16(LittleEndian::read_u16(&mem[2..]))
}
UnwindOperation::SaveNonvolatileRegisterFar => {
UnwindValue::U32(LittleEndian::read_u32(&mem[2..]))
}
UnwindOperation::SaveXmm128 => UnwindValue::U16(LittleEndian::read_u16(&mem[2..])),
UnwindOperation::SaveXmm128Far => {
UnwindValue::U32(LittleEndian::read_u32(&mem[2..]))
} }
(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, _ => UnwindValue::None,
}; };