1 //! This module gives users to instantiate values that Cranelift understands. These values are used,
2 //! for example, during interpretation and for wrapping immediates.
3 use crate::ir::immediates::{Ieee32, Ieee64, Offset32};
4 use crate::ir::{types, ConstantData, Type};
5 use core::fmt::{self, Display, Formatter};
6 
7 /// Represent a data value. Where [Value] is an SSA reference, [DataValue] is the type + value
8 /// that would be referred to by a [Value].
9 ///
10 /// [Value]: crate::ir::Value
11 #[allow(missing_docs)]
12 #[derive(Clone, Debug, PartialOrd)]
13 pub enum DataValue {
14     I8(i8),
15     I16(i16),
16     I32(i32),
17     I64(i64),
18     I128(i128),
19     F32(Ieee32),
20     F64(Ieee64),
21     V128([u8; 16]),
22     V64([u8; 8]),
23 }
24 
25 impl PartialEq for DataValue {
26     fn eq(&self, other: &Self) -> bool {
27         use DataValue::*;
28         match (self, other) {
29             (I8(l), I8(r)) => l == r,
30             (I8(_), _) => false,
31             (I16(l), I16(r)) => l == r,
32             (I16(_), _) => false,
33             (I32(l), I32(r)) => l == r,
34             (I32(_), _) => false,
35             (I64(l), I64(r)) => l == r,
36             (I64(_), _) => false,
37             (I128(l), I128(r)) => l == r,
38             (I128(_), _) => false,
39             (F32(l), F32(r)) => l.as_f32() == r.as_f32(),
40             (F32(_), _) => false,
41             (F64(l), F64(r)) => l.as_f64() == r.as_f64(),
42             (F64(_), _) => false,
43             (V128(l), V128(r)) => l == r,
44             (V128(_), _) => false,
45             (V64(l), V64(r)) => l == r,
46             (V64(_), _) => false,
47         }
48     }
49 }
50 
51 impl DataValue {
52     /// Try to cast an immediate integer (a wrapped `i64` on most Cranelift instructions) to the
53     /// given Cranelift [Type].
54     pub fn from_integer(imm: i128, ty: Type) -> Result<DataValue, DataValueCastFailure> {
55         match ty {
56             types::I8 => Ok(DataValue::I8(imm as i8)),
57             types::I16 => Ok(DataValue::I16(imm as i16)),
58             types::I32 => Ok(DataValue::I32(imm as i32)),
59             types::I64 => Ok(DataValue::I64(imm as i64)),
60             types::I128 => Ok(DataValue::I128(imm)),
61             _ => Err(DataValueCastFailure::FromInteger(imm, ty)),
62         }
63     }
64 
65     /// Return the Cranelift IR [Type] for this [DataValue].
66     pub fn ty(&self) -> Type {
67         match self {
68             DataValue::I8(_) => types::I8,
69             DataValue::I16(_) => types::I16,
70             DataValue::I32(_) => types::I32,
71             DataValue::I64(_) => types::I64,
72             DataValue::I128(_) => types::I128,
73             DataValue::F32(_) => types::F32,
74             DataValue::F64(_) => types::F64,
75             DataValue::V128(_) => types::I8X16, // A default type.
76             DataValue::V64(_) => types::I8X8,   // A default type.
77         }
78     }
79 
80     /// Return true if the value is a vector (i.e. `DataValue::V128`).
81     pub fn is_vector(&self) -> bool {
82         match self {
83             DataValue::V128(_) | DataValue::V64(_) => true,
84             _ => false,
85         }
86     }
87 
88     fn swap_bytes(self) -> Self {
89         match self {
90             DataValue::I8(i) => DataValue::I8(i.swap_bytes()),
91             DataValue::I16(i) => DataValue::I16(i.swap_bytes()),
92             DataValue::I32(i) => DataValue::I32(i.swap_bytes()),
93             DataValue::I64(i) => DataValue::I64(i.swap_bytes()),
94             DataValue::I128(i) => DataValue::I128(i.swap_bytes()),
95             DataValue::F32(f) => DataValue::F32(Ieee32::with_bits(f.bits().swap_bytes())),
96             DataValue::F64(f) => DataValue::F64(Ieee64::with_bits(f.bits().swap_bytes())),
97             DataValue::V128(mut v) => {
98                 v.reverse();
99                 DataValue::V128(v)
100             }
101             DataValue::V64(mut v) => {
102                 v.reverse();
103                 DataValue::V64(v)
104             }
105         }
106     }
107 
108     /// Converts `self` to big endian from target's endianness.
109     pub fn to_be(self) -> Self {
110         if cfg!(target_endian = "big") {
111             self
112         } else {
113             self.swap_bytes()
114         }
115     }
116 
117     /// Converts `self` to little endian from target's endianness.
118     pub fn to_le(self) -> Self {
119         if cfg!(target_endian = "little") {
120             self
121         } else {
122             self.swap_bytes()
123         }
124     }
125 
126     /// Write a [DataValue] to a slice in native-endian byte order.
127     ///
128     /// # Panics:
129     ///
130     /// Panics if the slice does not have enough space to accommodate the [DataValue]
131     pub fn write_to_slice_ne(&self, dst: &mut [u8]) {
132         match self {
133             DataValue::I8(i) => dst[..1].copy_from_slice(&i.to_ne_bytes()[..]),
134             DataValue::I16(i) => dst[..2].copy_from_slice(&i.to_ne_bytes()[..]),
135             DataValue::I32(i) => dst[..4].copy_from_slice(&i.to_ne_bytes()[..]),
136             DataValue::I64(i) => dst[..8].copy_from_slice(&i.to_ne_bytes()[..]),
137             DataValue::I128(i) => dst[..16].copy_from_slice(&i.to_ne_bytes()[..]),
138             DataValue::F32(f) => dst[..4].copy_from_slice(&f.bits().to_ne_bytes()[..]),
139             DataValue::F64(f) => dst[..8].copy_from_slice(&f.bits().to_ne_bytes()[..]),
140             DataValue::V128(v) => dst[..16].copy_from_slice(&v[..]),
141             DataValue::V64(v) => dst[..8].copy_from_slice(&v[..]),
142         };
143     }
144 
145     /// Write a [DataValue] to a slice in big-endian byte order.
146     ///
147     /// # Panics:
148     ///
149     /// Panics if the slice does not have enough space to accommodate the [DataValue]
150     pub fn write_to_slice_be(&self, dst: &mut [u8]) {
151         self.clone().to_be().write_to_slice_ne(dst);
152     }
153 
154     /// Write a [DataValue] to a slice in little-endian byte order.
155     ///
156     /// # Panics:
157     ///
158     /// Panics if the slice does not have enough space to accommodate the [DataValue]
159     pub fn write_to_slice_le(&self, dst: &mut [u8]) {
160         self.clone().to_le().write_to_slice_ne(dst);
161     }
162 
163     /// Read a [DataValue] from a slice using a given [Type] with native-endian byte order.
164     ///
165     /// # Panics:
166     ///
167     /// Panics if the slice does not have enough space to accommodate the [DataValue]
168     pub fn read_from_slice_ne(src: &[u8], ty: Type) -> Self {
169         match ty {
170             types::I8 => DataValue::I8(i8::from_ne_bytes(src[..1].try_into().unwrap())),
171             types::I16 => DataValue::I16(i16::from_ne_bytes(src[..2].try_into().unwrap())),
172             types::I32 => DataValue::I32(i32::from_ne_bytes(src[..4].try_into().unwrap())),
173             types::I64 => DataValue::I64(i64::from_ne_bytes(src[..8].try_into().unwrap())),
174             types::I128 => DataValue::I128(i128::from_ne_bytes(src[..16].try_into().unwrap())),
175             types::F32 => DataValue::F32(Ieee32::with_bits(u32::from_ne_bytes(
176                 src[..4].try_into().unwrap(),
177             ))),
178             types::F64 => DataValue::F64(Ieee64::with_bits(u64::from_ne_bytes(
179                 src[..8].try_into().unwrap(),
180             ))),
181             _ if ty.is_vector() => {
182                 if ty.bytes() == 16 {
183                     DataValue::V128(src[..16].try_into().unwrap())
184                 } else if ty.bytes() == 8 {
185                     DataValue::V64(src[..8].try_into().unwrap())
186                 } else {
187                     unimplemented!()
188                 }
189             }
190             _ => unimplemented!(),
191         }
192     }
193 
194     /// Read a [DataValue] from a slice using a given [Type] in big-endian byte order.
195     ///
196     /// # Panics:
197     ///
198     /// Panics if the slice does not have enough space to accommodate the [DataValue]
199     pub fn read_from_slice_be(src: &[u8], ty: Type) -> Self {
200         DataValue::read_from_slice_ne(src, ty).to_be()
201     }
202 
203     /// Read a [DataValue] from a slice using a given [Type] in little-endian byte order.
204     ///
205     /// # Panics:
206     ///
207     /// Panics if the slice does not have enough space to accommodate the [DataValue]
208     pub fn read_from_slice_le(src: &[u8], ty: Type) -> Self {
209         DataValue::read_from_slice_ne(src, ty).to_le()
210     }
211 
212     /// Write a [DataValue] to a memory location in native-endian byte order.
213     pub unsafe fn write_value_to(&self, p: *mut u128) {
214         let size = self.ty().bytes() as usize;
215         self.write_to_slice_ne(std::slice::from_raw_parts_mut(p as *mut u8, size));
216     }
217 
218     /// Read a [DataValue] from a memory location using a given [Type] in native-endian byte order.
219     pub unsafe fn read_value_from(p: *const u128, ty: Type) -> Self {
220         DataValue::read_from_slice_ne(
221             std::slice::from_raw_parts(p as *const u8, ty.bytes() as usize),
222             ty,
223         )
224     }
225 
226     /// Performs a bitwise comparison over the contents of [DataValue].
227     ///
228     /// Returns true if all bits are equal.
229     ///
230     /// This behaviour is different from PartialEq for NaN floats.
231     pub fn bitwise_eq(&self, other: &DataValue) -> bool {
232         match (self, other) {
233             // We need to bit compare the floats to ensure that we produce the correct values
234             // on NaN's. The test suite expects to assert the precise bit pattern on NaN's or
235             // works around it in the tests themselves.
236             (DataValue::F32(a), DataValue::F32(b)) => a.bits() == b.bits(),
237             (DataValue::F64(a), DataValue::F64(b)) => a.bits() == b.bits(),
238 
239             // We don't need to worry about F32x4 / F64x2 Since we compare V128 which is already the
240             // raw bytes anyway
241             (a, b) => a == b,
242         }
243     }
244 }
245 
246 /// Record failures to cast [DataValue].
247 #[derive(Debug, PartialEq)]
248 #[allow(missing_docs)]
249 pub enum DataValueCastFailure {
250     TryInto(Type, Type),
251     FromInteger(i128, Type),
252 }
253 
254 // This is manually implementing Error and Display instead of using thiserror to reduce the amount
255 // of dependencies used by Cranelift.
256 impl std::error::Error for DataValueCastFailure {}
257 
258 impl Display for DataValueCastFailure {
259     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
260         match self {
261             DataValueCastFailure::TryInto(from, to) => {
262                 write!(
263                     f,
264                     "unable to cast data value of type {} to type {}",
265                     from, to
266                 )
267             }
268             DataValueCastFailure::FromInteger(val, to) => {
269                 write!(
270                     f,
271                     "unable to cast i64({}) to a data value of type {}",
272                     val, to
273                 )
274             }
275         }
276     }
277 }
278 
279 /// Helper for creating conversion implementations for [DataValue].
280 macro_rules! build_conversion_impl {
281     ( $rust_ty:ty, $data_value_ty:ident, $cranelift_ty:ident ) => {
282         impl From<$rust_ty> for DataValue {
283             fn from(data: $rust_ty) -> Self {
284                 DataValue::$data_value_ty(data)
285             }
286         }
287 
288         impl TryInto<$rust_ty> for DataValue {
289             type Error = DataValueCastFailure;
290             fn try_into(self) -> Result<$rust_ty, Self::Error> {
291                 if let DataValue::$data_value_ty(v) = self {
292                     Ok(v)
293                 } else {
294                     Err(DataValueCastFailure::TryInto(
295                         self.ty(),
296                         types::$cranelift_ty,
297                     ))
298                 }
299             }
300         }
301     };
302 }
303 build_conversion_impl!(i8, I8, I8);
304 build_conversion_impl!(i16, I16, I16);
305 build_conversion_impl!(i32, I32, I32);
306 build_conversion_impl!(i64, I64, I64);
307 build_conversion_impl!(i128, I128, I128);
308 build_conversion_impl!(Ieee32, F32, F32);
309 build_conversion_impl!(Ieee64, F64, F64);
310 build_conversion_impl!([u8; 16], V128, I8X16);
311 build_conversion_impl!([u8; 8], V64, I8X8);
312 impl From<Offset32> for DataValue {
313     fn from(o: Offset32) -> Self {
314         DataValue::from(Into::<i32>::into(o))
315     }
316 }
317 
318 impl Display for DataValue {
319     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
320         match self {
321             DataValue::I8(dv) => write!(f, "{}", dv),
322             DataValue::I16(dv) => write!(f, "{}", dv),
323             DataValue::I32(dv) => write!(f, "{}", dv),
324             DataValue::I64(dv) => write!(f, "{}", dv),
325             DataValue::I128(dv) => write!(f, "{}", dv),
326             // The Ieee* wrappers here print the expected syntax.
327             DataValue::F32(dv) => write!(f, "{}", dv),
328             DataValue::F64(dv) => write!(f, "{}", dv),
329             // Again, for syntax consistency, use ConstantData, which in this case displays as hex.
330             DataValue::V128(dv) => write!(f, "{}", ConstantData::from(&dv[..])),
331             DataValue::V64(dv) => write!(f, "{}", ConstantData::from(&dv[..])),
332         }
333     }
334 }
335 
336 /// Helper structure for printing bracket-enclosed vectors of [DataValue]s.
337 /// - for empty vectors, display `[]`
338 /// - for single item vectors, display `42`, e.g.
339 /// - for multiple item vectors, display `[42, 43, 44]`, e.g.
340 pub struct DisplayDataValues<'a>(pub &'a [DataValue]);
341 
342 impl<'a> Display for DisplayDataValues<'a> {
343     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
344         if self.0.len() == 1 {
345             write!(f, "{}", self.0[0])
346         } else {
347             write!(f, "[")?;
348             write_data_value_list(f, &self.0)?;
349             write!(f, "]")
350         }
351     }
352 }
353 
354 /// Helper function for displaying `Vec<DataValue>`.
355 pub fn write_data_value_list(f: &mut Formatter<'_>, list: &[DataValue]) -> fmt::Result {
356     match list.len() {
357         0 => Ok(()),
358         1 => write!(f, "{}", list[0]),
359         _ => {
360             write!(f, "{}", list[0])?;
361             for dv in list.iter().skip(1) {
362                 write!(f, ", {}", dv)?;
363             }
364             Ok(())
365         }
366     }
367 }
368 
369 #[cfg(test)]
370 mod test {
371     use super::*;
372 
373     #[test]
374     fn type_conversions() {
375         assert_eq!(DataValue::V128([0; 16]).ty(), types::I8X16);
376         assert_eq!(
377             TryInto::<[u8; 16]>::try_into(DataValue::V128([0; 16])).unwrap(),
378             [0; 16]
379         );
380         assert_eq!(
381             TryInto::<i32>::try_into(DataValue::V128([0; 16])).unwrap_err(),
382             DataValueCastFailure::TryInto(types::I8X16, types::I32)
383         );
384     }
385 }
386