Skip to content

Commit 84ed9b2

Browse files
committed
cranelift: Use DataValue accessors for trampoline.
1 parent cf7689d commit 84ed9b2

File tree

2 files changed

+16
-49
lines changed

2 files changed

+16
-49
lines changed

cranelift/codegen/src/data_value.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ impl DataValue {
110110
types::F64 => DataValue::F64(Ieee64::with_bits(u64::from_le_bytes(
111111
src[..8].try_into().unwrap(),
112112
))),
113-
_ if ty.is_bool() => DataValue::B(src[..16] != [0; 16]),
113+
_ if ty.is_bool() => {
114+
// Only `ty.bytes()` are guaranteed to be written
115+
// so we can only test the first n bytes of `src`
116+
117+
let size = ty.bytes() as usize;
118+
DataValue::B(src[..size].iter().any(|&i| i != 0))
119+
}
114120
_ if ty.is_vector() && ty.bytes() == 16 => {
115121
DataValue::V128(src[..16].try_into().unwrap())
116122
}
@@ -120,6 +126,8 @@ impl DataValue {
120126

121127
/// Write a [DataValue] to a memory location.
122128
pub unsafe fn write_value_to(&self, p: *mut u128) {
129+
// Since `DataValue` does not have type info for bools we always
130+
// write out a full 16 byte slot.
123131
let size = match self.ty() {
124132
ty if ty.is_bool() => 16,
125133
ty => ty.bytes() as usize,
@@ -130,12 +138,10 @@ impl DataValue {
130138

131139
/// Read a [DataValue] from a memory location using a given [Type].
132140
pub unsafe fn read_value_from(p: *const u128, ty: Type) -> Self {
133-
let size = match ty {
134-
ty if ty.is_bool() => 16,
135-
ty => ty.bytes() as usize,
136-
};
137-
138-
DataValue::read_from_slice(std::slice::from_raw_parts(p as *const u8, size), ty)
141+
DataValue::read_from_slice(
142+
std::slice::from_raw_parts(p as *const u8, ty.bytes() as usize),
143+
ty,
144+
)
139145
}
140146
}
141147

cranelift/filetests/src/function_runner.rs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Provides functionality for compiling and running CLIF IR for `run` tests.
2-
use core::{mem, ptr};
2+
use core::mem;
33
use cranelift_codegen::binemit::{NullRelocSink, NullStackMapSink, NullTrapSink};
44
use cranelift_codegen::data_value::DataValue;
5-
use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
65
use cranelift_codegen::ir::{condcodes::IntCC, Function, InstBuilder, Signature, Type};
76
use cranelift_codegen::isa::{BackendVariant, TargetIsa};
87
use cranelift_codegen::{ir, settings, CodegenError, Context};
@@ -204,7 +203,7 @@ impl UnboxedValues {
204203
param.value_type
205204
);
206205
unsafe {
207-
Self::write_value_to(arg, slot);
206+
arg.write_value_to(slot);
208207
}
209208
}
210209

@@ -224,50 +223,12 @@ impl UnboxedValues {
224223

225224
// Extract the returned values from this vector.
226225
for (slot, param) in self.0.iter().zip(&signature.returns) {
227-
let value = unsafe { Self::read_value_from(slot, param.value_type) };
226+
let value = unsafe { DataValue::read_value_from(slot, param.value_type) };
228227
returns.push(value);
229228
}
230229

231230
returns
232231
}
233-
234-
/// Write a [DataValue] to a memory location.
235-
unsafe fn write_value_to(v: &DataValue, p: *mut u128) {
236-
match v {
237-
DataValue::B(b) => ptr::write(p as *mut bool, *b),
238-
DataValue::I8(i) => ptr::write(p as *mut i8, *i),
239-
DataValue::I16(i) => ptr::write(p as *mut i16, *i),
240-
DataValue::I32(i) => ptr::write(p as *mut i32, *i),
241-
DataValue::I64(i) => ptr::write(p as *mut i64, *i),
242-
DataValue::F32(f) => ptr::write(p as *mut Ieee32, *f),
243-
DataValue::F64(f) => ptr::write(p as *mut Ieee64, *f),
244-
DataValue::V128(b) => ptr::write(p as *mut [u8; 16], *b),
245-
_ => unimplemented!(),
246-
}
247-
}
248-
249-
/// Read a [DataValue] from a memory location using a given [Type].
250-
unsafe fn read_value_from(p: *const u128, ty: Type) -> DataValue {
251-
match ty {
252-
ir::types::I8 => DataValue::I8(ptr::read(p as *const i8)),
253-
ir::types::I16 => DataValue::I16(ptr::read(p as *const i16)),
254-
ir::types::I32 => DataValue::I32(ptr::read(p as *const i32)),
255-
ir::types::I64 => DataValue::I64(ptr::read(p as *const i64)),
256-
ir::types::F32 => DataValue::F32(ptr::read(p as *const Ieee32)),
257-
ir::types::F64 => DataValue::F64(ptr::read(p as *const Ieee64)),
258-
_ if ty.is_bool() => match ty.bytes() {
259-
1 => DataValue::B(ptr::read(p as *const i8) != 0),
260-
2 => DataValue::B(ptr::read(p as *const i16) != 0),
261-
4 => DataValue::B(ptr::read(p as *const i32) != 0),
262-
8 => DataValue::B(ptr::read(p as *const i64) != 0),
263-
_ => unimplemented!(),
264-
},
265-
_ if ty.is_vector() && ty.bytes() == 16 => {
266-
DataValue::V128(ptr::read(p as *const [u8; 16]))
267-
}
268-
_ => unimplemented!(),
269-
}
270-
}
271232
}
272233

273234
/// Compile a [Function] to its executable bytes in memory.

0 commit comments

Comments
 (0)