1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! This module gives users to instantiate values that Cranelift understands. These values are used,
//! for example, during interpretation and for wrapping immediates.
use crate::ir::immediates::{Ieee32, Ieee64, Offset32};
use crate::ir::{types, ConstantData, Type};
use core::fmt::{self, Display, Formatter};

/// Represent a data value. Where [Value] is an SSA reference, [DataValue] is the type + value
/// that would be referred to by a [Value].
///
/// [Value]: crate::ir::Value
#[allow(missing_docs)]
#[derive(Clone, Debug, PartialOrd)]
pub enum DataValue {
    I8(i8),
    I16(i16),
    I32(i32),
    I64(i64),
    I128(i128),
    F32(Ieee32),
    F64(Ieee64),
    V128([u8; 16]),
    V64([u8; 8]),
}

impl PartialEq for DataValue {
    fn eq(&self, other: &Self) -> bool {
        use DataValue::*;
        match (self, other) {
            (I8(l), I8(r)) => l == r,
            (I8(_), _) => false,
            (I16(l), I16(r)) => l == r,
            (I16(_), _) => false,
            (I32(l), I32(r)) => l == r,
            (I32(_), _) => false,
            (I64(l), I64(r)) => l == r,
            (I64(_), _) => false,
            (I128(l), I128(r)) => l == r,
            (I128(_), _) => false,
            (F32(l), F32(r)) => l.as_f32() == r.as_f32(),
            (F32(_), _) => false,
            (F64(l), F64(r)) => l.as_f64() == r.as_f64(),
            (F64(_), _) => false,
            (V128(l), V128(r)) => l == r,
            (V128(_), _) => false,
            (V64(l), V64(r)) => l == r,
            (V64(_), _) => false,
        }
    }
}

impl DataValue {
    /// Try to cast an immediate integer (a wrapped `i64` on most Cranelift instructions) to the
    /// given Cranelift [Type].
    pub fn from_integer(imm: i128, ty: Type) -> Result<DataValue, DataValueCastFailure> {
        match ty {
            types::I8 => Ok(DataValue::I8(imm as i8)),
            types::I16 => Ok(DataValue::I16(imm as i16)),
            types::I32 => Ok(DataValue::I32(imm as i32)),
            types::I64 => Ok(DataValue::I64(imm as i64)),
            types::I128 => Ok(DataValue::I128(imm)),
            _ => Err(DataValueCastFailure::FromInteger(imm, ty)),
        }
    }

    /// Return the Cranelift IR [Type] for this [DataValue].
    pub fn ty(&self) -> Type {
        match self {
            DataValue::I8(_) => types::I8,
            DataValue::I16(_) => types::I16,
            DataValue::I32(_) => types::I32,
            DataValue::I64(_) => types::I64,
            DataValue::I128(_) => types::I128,
            DataValue::F32(_) => types::F32,
            DataValue::F64(_) => types::F64,
            DataValue::V128(_) => types::I8X16, // A default type.
            DataValue::V64(_) => types::I8X8,   // A default type.
        }
    }

    /// Return true if the value is a vector (i.e. `DataValue::V128`).
    pub fn is_vector(&self) -> bool {
        match self {
            DataValue::V128(_) | DataValue::V64(_) => true,
            _ => false,
        }
    }

    fn swap_bytes(self) -> Self {
        match self {
            DataValue::I8(i) => DataValue::I8(i.swap_bytes()),
            DataValue::I16(i) => DataValue::I16(i.swap_bytes()),
            DataValue::I32(i) => DataValue::I32(i.swap_bytes()),
            DataValue::I64(i) => DataValue::I64(i.swap_bytes()),
            DataValue::I128(i) => DataValue::I128(i.swap_bytes()),
            DataValue::F32(f) => DataValue::F32(Ieee32::with_bits(f.bits().swap_bytes())),
            DataValue::F64(f) => DataValue::F64(Ieee64::with_bits(f.bits().swap_bytes())),
            DataValue::V128(mut v) => {
                v.reverse();
                DataValue::V128(v)
            }
            DataValue::V64(mut v) => {
                v.reverse();
                DataValue::V64(v)
            }
        }
    }

    /// Converts `self` to big endian from target's endianness.
    pub fn to_be(self) -> Self {
        if cfg!(target_endian = "big") {
            self
        } else {
            self.swap_bytes()
        }
    }

    /// Converts `self` to little endian from target's endianness.
    pub fn to_le(self) -> Self {
        if cfg!(target_endian = "little") {
            self
        } else {
            self.swap_bytes()
        }
    }

    /// Write a [DataValue] to a slice in native-endian byte order.
    ///
    /// # Panics:
    ///
    /// Panics if the slice does not have enough space to accommodate the [DataValue]
    pub fn write_to_slice_ne(&self, dst: &mut [u8]) {
        match self {
            DataValue::I8(i) => dst[..1].copy_from_slice(&i.to_ne_bytes()[..]),
            DataValue::I16(i) => dst[..2].copy_from_slice(&i.to_ne_bytes()[..]),
            DataValue::I32(i) => dst[..4].copy_from_slice(&i.to_ne_bytes()[..]),
            DataValue::I64(i) => dst[..8].copy_from_slice(&i.to_ne_bytes()[..]),
            DataValue::I128(i) => dst[..16].copy_from_slice(&i.to_ne_bytes()[..]),
            DataValue::F32(f) => dst[..4].copy_from_slice(&f.bits().to_ne_bytes()[..]),
            DataValue::F64(f) => dst[..8].copy_from_slice(&f.bits().to_ne_bytes()[..]),
            DataValue::V128(v) => dst[..16].copy_from_slice(&v[..]),
            DataValue::V64(v) => dst[..8].copy_from_slice(&v[..]),
        };
    }

    /// Write a [DataValue] to a slice in big-endian byte order.
    ///
    /// # Panics:
    ///
    /// Panics if the slice does not have enough space to accommodate the [DataValue]
    pub fn write_to_slice_be(&self, dst: &mut [u8]) {
        self.clone().to_be().write_to_slice_ne(dst);
    }

    /// Write a [DataValue] to a slice in little-endian byte order.
    ///
    /// # Panics:
    ///
    /// Panics if the slice does not have enough space to accommodate the [DataValue]
    pub fn write_to_slice_le(&self, dst: &mut [u8]) {
        self.clone().to_le().write_to_slice_ne(dst);
    }

    /// Read a [DataValue] from a slice using a given [Type] with native-endian byte order.
    ///
    /// # Panics:
    ///
    /// Panics if the slice does not have enough space to accommodate the [DataValue]
    pub fn read_from_slice_ne(src: &[u8], ty: Type) -> Self {
        match ty {
            types::I8 => DataValue::I8(i8::from_ne_bytes(src[..1].try_into().unwrap())),
            types::I16 => DataValue::I16(i16::from_ne_bytes(src[..2].try_into().unwrap())),
            types::I32 => DataValue::I32(i32::from_ne_bytes(src[..4].try_into().unwrap())),
            types::I64 => DataValue::I64(i64::from_ne_bytes(src[..8].try_into().unwrap())),
            types::I128 => DataValue::I128(i128::from_ne_bytes(src[..16].try_into().unwrap())),
            types::F32 => DataValue::F32(Ieee32::with_bits(u32::from_ne_bytes(
                src[..4].try_into().unwrap(),
            ))),
            types::F64 => DataValue::F64(Ieee64::with_bits(u64::from_ne_bytes(
                src[..8].try_into().unwrap(),
            ))),
            _ if ty.is_vector() => {
                if ty.bytes() == 16 {
                    DataValue::V128(src[..16].try_into().unwrap())
                } else if ty.bytes() == 8 {
                    DataValue::V64(src[..8].try_into().unwrap())
                } else {
                    unimplemented!()
                }
            }
            _ => unimplemented!(),
        }
    }

    /// Read a [DataValue] from a slice using a given [Type] in big-endian byte order.
    ///
    /// # Panics:
    ///
    /// Panics if the slice does not have enough space to accommodate the [DataValue]
    pub fn read_from_slice_be(src: &[u8], ty: Type) -> Self {
        DataValue::read_from_slice_ne(src, ty).to_be()
    }

    /// Read a [DataValue] from a slice using a given [Type] in little-endian byte order.
    ///
    /// # Panics:
    ///
    /// Panics if the slice does not have enough space to accommodate the [DataValue]
    pub fn read_from_slice_le(src: &[u8], ty: Type) -> Self {
        DataValue::read_from_slice_ne(src, ty).to_le()
    }

    /// Write a [DataValue] to a memory location in native-endian byte order.
    pub unsafe fn write_value_to(&self, p: *mut u128) {
        let size = self.ty().bytes() as usize;
        self.write_to_slice_ne(std::slice::from_raw_parts_mut(p as *mut u8, size));
    }

    /// Read a [DataValue] from a memory location using a given [Type] in native-endian byte order.
    pub unsafe fn read_value_from(p: *const u128, ty: Type) -> Self {
        DataValue::read_from_slice_ne(
            std::slice::from_raw_parts(p as *const u8, ty.bytes() as usize),
            ty,
        )
    }

    /// Performs a bitwise comparison over the contents of [DataValue].
    ///
    /// Returns true if all bits are equal.
    ///
    /// This behaviour is different from PartialEq for NaN floats.
    pub fn bitwise_eq(&self, other: &DataValue) -> bool {
        match (self, other) {
            // We need to bit compare the floats to ensure that we produce the correct values
            // on NaN's. The test suite expects to assert the precise bit pattern on NaN's or
            // works around it in the tests themselves.
            (DataValue::F32(a), DataValue::F32(b)) => a.bits() == b.bits(),
            (DataValue::F64(a), DataValue::F64(b)) => a.bits() == b.bits(),

            // We don't need to worry about F32x4 / F64x2 Since we compare V128 which is already the
            // raw bytes anyway
            (a, b) => a == b,
        }
    }
}

/// Record failures to cast [DataValue].
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub enum DataValueCastFailure {
    TryInto(Type, Type),
    FromInteger(i128, Type),
}

// This is manually implementing Error and Display instead of using thiserror to reduce the amount
// of dependencies used by Cranelift.
impl std::error::Error for DataValueCastFailure {}

impl Display for DataValueCastFailure {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            DataValueCastFailure::TryInto(from, to) => {
                write!(
                    f,
                    "unable to cast data value of type {} to type {}",
                    from, to
                )
            }
            DataValueCastFailure::FromInteger(val, to) => {
                write!(
                    f,
                    "unable to cast i64({}) to a data value of type {}",
                    val, to
                )
            }
        }
    }
}

/// Helper for creating conversion implementations for [DataValue].
macro_rules! build_conversion_impl {
    ( $rust_ty:ty, $data_value_ty:ident, $cranelift_ty:ident ) => {
        impl From<$rust_ty> for DataValue {
            fn from(data: $rust_ty) -> Self {
                DataValue::$data_value_ty(data)
            }
        }

        impl TryInto<$rust_ty> for DataValue {
            type Error = DataValueCastFailure;
            fn try_into(self) -> Result<$rust_ty, Self::Error> {
                if let DataValue::$data_value_ty(v) = self {
                    Ok(v)
                } else {
                    Err(DataValueCastFailure::TryInto(
                        self.ty(),
                        types::$cranelift_ty,
                    ))
                }
            }
        }
    };
}
build_conversion_impl!(i8, I8, I8);
build_conversion_impl!(i16, I16, I16);
build_conversion_impl!(i32, I32, I32);
build_conversion_impl!(i64, I64, I64);
build_conversion_impl!(i128, I128, I128);
build_conversion_impl!(Ieee32, F32, F32);
build_conversion_impl!(Ieee64, F64, F64);
build_conversion_impl!([u8; 16], V128, I8X16);
build_conversion_impl!([u8; 8], V64, I8X8);
impl From<Offset32> for DataValue {
    fn from(o: Offset32) -> Self {
        DataValue::from(Into::<i32>::into(o))
    }
}

impl Display for DataValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            DataValue::I8(dv) => write!(f, "{}", dv),
            DataValue::I16(dv) => write!(f, "{}", dv),
            DataValue::I32(dv) => write!(f, "{}", dv),
            DataValue::I64(dv) => write!(f, "{}", dv),
            DataValue::I128(dv) => write!(f, "{}", dv),
            // The Ieee* wrappers here print the expected syntax.
            DataValue::F32(dv) => write!(f, "{}", dv),
            DataValue::F64(dv) => write!(f, "{}", dv),
            // Again, for syntax consistency, use ConstantData, which in this case displays as hex.
            DataValue::V128(dv) => write!(f, "{}", ConstantData::from(&dv[..])),
            DataValue::V64(dv) => write!(f, "{}", ConstantData::from(&dv[..])),
        }
    }
}

/// Helper structure for printing bracket-enclosed vectors of [DataValue]s.
/// - for empty vectors, display `[]`
/// - for single item vectors, display `42`, e.g.
/// - for multiple item vectors, display `[42, 43, 44]`, e.g.
pub struct DisplayDataValues<'a>(pub &'a [DataValue]);

impl<'a> Display for DisplayDataValues<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.0.len() == 1 {
            write!(f, "{}", self.0[0])
        } else {
            write!(f, "[")?;
            write_data_value_list(f, &self.0)?;
            write!(f, "]")
        }
    }
}

/// Helper function for displaying `Vec<DataValue>`.
pub fn write_data_value_list(f: &mut Formatter<'_>, list: &[DataValue]) -> fmt::Result {
    match list.len() {
        0 => Ok(()),
        1 => write!(f, "{}", list[0]),
        _ => {
            write!(f, "{}", list[0])?;
            for dv in list.iter().skip(1) {
                write!(f, ", {}", dv)?;
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn type_conversions() {
        assert_eq!(DataValue::V128([0; 16]).ty(), types::I8X16);
        assert_eq!(
            TryInto::<[u8; 16]>::try_into(DataValue::V128([0; 16])).unwrap(),
            [0; 16]
        );
        assert_eq!(
            TryInto::<i32>::try_into(DataValue::V128([0; 16])).unwrap_err(),
            DataValueCastFailure::TryInto(types::I8X16, types::I32)
        );
    }
}