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::convert::TryInto; 6 use core::fmt::{self, Display, Formatter}; 7 use core::ptr; 8 9 /// Represent a data value. Where [Value] is an SSA reference, [DataValue] is the type + value 10 /// that would be referred to by a [Value]. 11 /// 12 /// [Value]: crate::ir::Value 13 #[allow(missing_docs)] 14 #[derive(Clone, Debug, PartialEq, PartialOrd)] 15 pub enum DataValue { 16 B(bool), 17 I8(i8), 18 I16(i16), 19 I32(i32), 20 I64(i64), 21 U8(u8), 22 U16(u16), 23 U32(u32), 24 U64(u64), 25 F32(Ieee32), 26 F64(Ieee64), 27 V128([u8; 16]), 28 } 29 30 impl DataValue { 31 /// Try to cast an immediate integer (a wrapped `i64` on most Cranelift instructions) to the 32 /// given Cranelift [Type]. 33 pub fn from_integer(imm: i64, ty: Type) -> Result<DataValue, DataValueCastFailure> { 34 match ty { 35 types::I8 => Ok(DataValue::I8(imm as i8)), 36 types::I16 => Ok(DataValue::I16(imm as i16)), 37 types::I32 => Ok(DataValue::I32(imm as i32)), 38 types::I64 => Ok(DataValue::I64(imm)), 39 _ => Err(DataValueCastFailure::FromInteger(imm, ty)), 40 } 41 } 42 43 /// Return the Cranelift IR [Type] for this [DataValue]. 44 pub fn ty(&self) -> Type { 45 match self { 46 DataValue::B(_) => types::B8, // A default type. 47 DataValue::I8(_) | DataValue::U8(_) => types::I8, 48 DataValue::I16(_) | DataValue::U16(_) => types::I16, 49 DataValue::I32(_) | DataValue::U32(_) => types::I32, 50 DataValue::I64(_) | DataValue::U64(_) => types::I64, 51 DataValue::F32(_) => types::F32, 52 DataValue::F64(_) => types::F64, 53 DataValue::V128(_) => types::I8X16, // A default type. 54 } 55 } 56 57 /// Return true if the value is a vector (i.e. `DataValue::V128`). 58 pub fn is_vector(&self) -> bool { 59 match self { 60 DataValue::V128(_) => true, 61 _ => false, 62 } 63 } 64 65 /// Write a [DataValue] to a memory location. 66 pub unsafe fn write_value_to(&self, p: *mut u128) { 67 match self { 68 DataValue::B(b) => ptr::write(p as *mut bool, *b), 69 DataValue::I8(i) => ptr::write(p as *mut i8, *i), 70 DataValue::I16(i) => ptr::write(p as *mut i16, *i), 71 DataValue::I32(i) => ptr::write(p as *mut i32, *i), 72 DataValue::I64(i) => ptr::write(p as *mut i64, *i), 73 DataValue::F32(f) => ptr::write(p as *mut Ieee32, *f), 74 DataValue::F64(f) => ptr::write(p as *mut Ieee64, *f), 75 DataValue::V128(b) => ptr::write(p as *mut [u8; 16], *b), 76 _ => unimplemented!(), 77 } 78 } 79 80 /// Read a [DataValue] from a memory location using a given [Type]. 81 pub unsafe fn read_value_from(p: *const u128, ty: Type) -> Self { 82 match ty { 83 types::I8 => DataValue::I8(ptr::read(p as *const i8)), 84 types::I16 => DataValue::I16(ptr::read(p as *const i16)), 85 types::I32 => DataValue::I32(ptr::read(p as *const i32)), 86 types::I64 => DataValue::I64(ptr::read(p as *const i64)), 87 types::F32 => DataValue::F32(ptr::read(p as *const Ieee32)), 88 types::F64 => DataValue::F64(ptr::read(p as *const Ieee64)), 89 _ if ty.is_bool() => DataValue::B(ptr::read(p as *const bool)), 90 _ if ty.is_vector() && ty.bytes() == 16 => { 91 DataValue::V128(ptr::read(p as *const [u8; 16])) 92 } 93 _ => unimplemented!(), 94 } 95 } 96 } 97 98 /// Record failures to cast [DataValue]. 99 #[derive(Debug, PartialEq)] 100 #[allow(missing_docs)] 101 pub enum DataValueCastFailure { 102 TryInto(Type, Type), 103 FromInteger(i64, Type), 104 } 105 106 // This is manually implementing Error and Display instead of using thiserror to reduce the amount 107 // of dependencies used by Cranelift. 108 impl std::error::Error for DataValueCastFailure {} 109 110 impl Display for DataValueCastFailure { 111 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 112 match self { 113 DataValueCastFailure::TryInto(from, to) => { 114 write!( 115 f, 116 "unable to cast data value of type {} to type {}", 117 from, to 118 ) 119 } 120 DataValueCastFailure::FromInteger(val, to) => { 121 write!( 122 f, 123 "unable to cast i64({}) to a data value of type {}", 124 val, to 125 ) 126 } 127 } 128 } 129 } 130 131 /// Helper for creating conversion implementations for [DataValue]. 132 macro_rules! build_conversion_impl { 133 ( $rust_ty:ty, $data_value_ty:ident, $cranelift_ty:ident ) => { 134 impl From<$rust_ty> for DataValue { 135 fn from(data: $rust_ty) -> Self { 136 DataValue::$data_value_ty(data) 137 } 138 } 139 140 impl TryInto<$rust_ty> for DataValue { 141 type Error = DataValueCastFailure; 142 fn try_into(self) -> Result<$rust_ty, Self::Error> { 143 if let DataValue::$data_value_ty(v) = self { 144 Ok(v) 145 } else { 146 Err(DataValueCastFailure::TryInto( 147 self.ty(), 148 types::$cranelift_ty, 149 )) 150 } 151 } 152 } 153 }; 154 } 155 build_conversion_impl!(bool, B, B8); 156 build_conversion_impl!(i8, I8, I8); 157 build_conversion_impl!(i16, I16, I16); 158 build_conversion_impl!(i32, I32, I32); 159 build_conversion_impl!(i64, I64, I64); 160 build_conversion_impl!(u8, U8, I8); 161 build_conversion_impl!(u16, U16, I16); 162 build_conversion_impl!(u32, U32, I32); 163 build_conversion_impl!(u64, U64, I64); 164 build_conversion_impl!(Ieee32, F32, F32); 165 build_conversion_impl!(Ieee64, F64, F64); 166 build_conversion_impl!([u8; 16], V128, I8X16); 167 impl From<Offset32> for DataValue { 168 fn from(o: Offset32) -> Self { 169 DataValue::from(Into::<i32>::into(o)) 170 } 171 } 172 173 impl Display for DataValue { 174 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 175 match self { 176 DataValue::B(dv) => write!(f, "{}", dv), 177 DataValue::I8(dv) => write!(f, "{}", dv), 178 DataValue::I16(dv) => write!(f, "{}", dv), 179 DataValue::I32(dv) => write!(f, "{}", dv), 180 DataValue::I64(dv) => write!(f, "{}", dv), 181 DataValue::U8(dv) => write!(f, "{}", dv), 182 DataValue::U16(dv) => write!(f, "{}", dv), 183 DataValue::U32(dv) => write!(f, "{}", dv), 184 DataValue::U64(dv) => write!(f, "{}", dv), 185 // The Ieee* wrappers here print the expected syntax. 186 DataValue::F32(dv) => write!(f, "{}", dv), 187 DataValue::F64(dv) => write!(f, "{}", dv), 188 // Again, for syntax consistency, use ConstantData, which in this case displays as hex. 189 DataValue::V128(dv) => write!(f, "{}", ConstantData::from(&dv[..])), 190 } 191 } 192 } 193 194 /// Helper structure for printing bracket-enclosed vectors of [DataValue]s. 195 /// - for empty vectors, display `[]` 196 /// - for single item vectors, display `42`, e.g. 197 /// - for multiple item vectors, display `[42, 43, 44]`, e.g. 198 pub struct DisplayDataValues<'a>(pub &'a [DataValue]); 199 200 impl<'a> Display for DisplayDataValues<'a> { 201 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 202 if self.0.len() == 1 { 203 write!(f, "{}", self.0[0]) 204 } else { 205 write!(f, "[")?; 206 write_data_value_list(f, &self.0)?; 207 write!(f, "]") 208 } 209 } 210 } 211 212 /// Helper function for displaying `Vec<DataValue>`. 213 pub fn write_data_value_list(f: &mut Formatter<'_>, list: &[DataValue]) -> fmt::Result { 214 match list.len() { 215 0 => Ok(()), 216 1 => write!(f, "{}", list[0]), 217 _ => { 218 write!(f, "{}", list[0])?; 219 for dv in list.iter().skip(1) { 220 write!(f, ", {}", dv)?; 221 } 222 Ok(()) 223 } 224 } 225 } 226 227 #[cfg(test)] 228 mod test { 229 use super::*; 230 231 #[test] 232 fn type_conversions() { 233 assert_eq!(DataValue::B(true).ty(), types::B8); 234 assert_eq!( 235 TryInto::<bool>::try_into(DataValue::B(false)).unwrap(), 236 false 237 ); 238 assert_eq!( 239 TryInto::<i32>::try_into(DataValue::B(false)).unwrap_err(), 240 DataValueCastFailure::TryInto(types::B8, types::I32) 241 ); 242 243 assert_eq!(DataValue::V128([0; 16]).ty(), types::I8X16); 244 assert_eq!( 245 TryInto::<[u8; 16]>::try_into(DataValue::V128([0; 16])).unwrap(), 246 [0; 16] 247 ); 248 assert_eq!( 249 TryInto::<i32>::try_into(DataValue::V128([0; 16])).unwrap_err(), 250 DataValueCastFailure::TryInto(types::I8X16, types::I32) 251 ); 252 } 253 } 254