1 //! Constants 2 //! 3 //! The constant pool defined here allows Cranelift to avoid emitting the same constant multiple 4 //! times. As constants are inserted in the pool, a handle is returned; the handle is a Cranelift 5 //! Entity. Inserting the same data multiple times will always return the same handle. 6 //! 7 //! Future work could include: 8 //! - ensuring alignment of constants within the pool, 9 //! - bucketing constants by size. 10 11 use crate::ir::immediates::{IntoBytes, V128Imm}; 12 use crate::ir::Constant; 13 use crate::HashMap; 14 use alloc::collections::BTreeMap; 15 use alloc::vec::Vec; 16 use core::fmt; 17 use core::iter::FromIterator; 18 use core::slice::Iter; 19 use core::str::{from_utf8, FromStr}; 20 use cranelift_entity::EntityRef; 21 22 #[cfg(feature = "enable-serde")] 23 use serde::{Deserialize, Serialize}; 24 25 /// This type describes the actual constant data. Note that the bytes stored in this structure are 26 /// expected to be in little-endian order; this is due to ease-of-use when interacting with 27 /// WebAssembly values, which are [little-endian by design]. 28 /// 29 /// [little-endian by design]: https://github.com/WebAssembly/design/blob/master/Portability.md 30 #[derive(Clone, Hash, Eq, PartialEq, Debug, Default)] 31 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 32 pub struct ConstantData(Vec<u8>); 33 34 impl FromIterator<u8> for ConstantData { 35 fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self { 36 let v = iter.into_iter().collect(); 37 Self(v) 38 } 39 } 40 41 impl From<Vec<u8>> for ConstantData { 42 fn from(v: Vec<u8>) -> Self { 43 Self(v) 44 } 45 } 46 47 impl From<&[u8]> for ConstantData { 48 fn from(v: &[u8]) -> Self { 49 Self(v.to_vec()) 50 } 51 } 52 53 impl From<V128Imm> for ConstantData { 54 fn from(v: V128Imm) -> Self { 55 Self(v.to_vec()) 56 } 57 } 58 59 impl ConstantData { 60 /// Return the number of bytes in the constant. 61 pub fn len(&self) -> usize { 62 self.0.len() 63 } 64 65 /// Check if the constant contains any bytes. 66 pub fn is_empty(&self) -> bool { 67 self.0.is_empty() 68 } 69 70 /// Return the data as a slice. 71 pub fn as_slice(&self) -> &[u8] { 72 self.0.as_slice() 73 } 74 75 /// Convert the data to a vector. 76 pub fn into_vec(self) -> Vec<u8> { 77 self.0 78 } 79 80 /// Iterate over the constant's bytes. 81 pub fn iter(&self) -> Iter<u8> { 82 self.0.iter() 83 } 84 85 /// Add new bytes to the constant data. 86 pub fn append(mut self, bytes: impl IntoBytes) -> Self { 87 let mut to_add = bytes.into_bytes(); 88 self.0.append(&mut to_add); 89 self 90 } 91 92 /// Expand the size of the constant data to `expected_size` number of bytes by adding zeroes 93 /// in the high-order byte slots. 94 pub fn expand_to(mut self, expected_size: usize) -> Self { 95 if self.len() > expected_size { 96 panic!( 97 "The constant data is already expanded beyond {} bytes", 98 expected_size 99 ) 100 } 101 self.0.resize(expected_size, 0); 102 self 103 } 104 } 105 106 impl fmt::Display for ConstantData { 107 /// Print the constant data in hexadecimal format, e.g. 0x000102030405060708090a0b0c0d0e0f. 108 /// This function will flip the stored order of bytes--little-endian--to the more readable 109 /// big-endian ordering. 110 /// 111 /// ``` 112 /// use cranelift_codegen::ir::ConstantData; 113 /// let data = ConstantData::from([3, 2, 1, 0, 0].as_ref()); // note the little-endian order 114 /// assert_eq!(data.to_string(), "0x0000010203"); 115 /// ``` 116 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 117 if !self.is_empty() { 118 write!(f, "0x")?; 119 for b in self.0.iter().rev() { 120 write!(f, "{:02x}", b)?; 121 } 122 } 123 Ok(()) 124 } 125 } 126 127 impl FromStr for ConstantData { 128 type Err = &'static str; 129 130 /// Parse a hexadecimal string to `ConstantData`. This is the inverse of `Display::fmt`. 131 /// 132 /// ``` 133 /// use cranelift_codegen::ir::ConstantData; 134 /// let c: ConstantData = "0x000102".parse().unwrap(); 135 /// assert_eq!(c.into_vec(), [2, 1, 0]); 136 /// ``` 137 fn from_str(s: &str) -> Result<Self, &'static str> { 138 if s.len() <= 2 || &s[0..2] != "0x" { 139 return Err("Expected a hexadecimal string, e.g. 0x1234"); 140 } 141 142 // clean and check the string 143 let cleaned: Vec<u8> = s[2..] 144 .as_bytes() 145 .iter() 146 .filter(|&&b| b as char != '_') 147 .cloned() 148 .collect(); // remove 0x prefix and any intervening _ characters 149 150 if cleaned.is_empty() { 151 Err("Hexadecimal string must have some digits") 152 } else if cleaned.len() % 2 != 0 { 153 Err("Hexadecimal string must have an even number of digits") 154 } else if cleaned.len() > 32 { 155 Err("Hexadecimal string has too many digits to fit in a 128-bit vector") 156 } else { 157 let mut buffer = Vec::with_capacity((s.len() - 2) / 2); 158 for i in (0..cleaned.len()).step_by(2) { 159 let pair = from_utf8(&cleaned[i..i + 2]) 160 .or_else(|_| Err("Unable to parse hexadecimal pair as UTF-8"))?; 161 let byte = u8::from_str_radix(pair, 16) 162 .or_else(|_| Err("Unable to parse as hexadecimal"))?; 163 buffer.insert(0, byte); 164 } 165 Ok(Self(buffer)) 166 } 167 } 168 } 169 170 /// Maintains the mapping between a constant handle (i.e. [`Constant`](crate::ir::Constant)) and 171 /// its constant data (i.e. [`ConstantData`](crate::ir::ConstantData)). 172 #[derive(Clone)] 173 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 174 pub struct ConstantPool { 175 /// This mapping maintains the insertion order as long as Constants are created with 176 /// sequentially increasing integers. 177 handles_to_values: BTreeMap<Constant, ConstantData>, 178 179 /// This mapping is unordered (no need for lexicographic ordering) but allows us to map 180 /// constant data back to handles. 181 values_to_handles: HashMap<ConstantData, Constant>, 182 } 183 184 impl ConstantPool { 185 /// Create a new constant pool instance. 186 pub fn new() -> Self { 187 Self { 188 handles_to_values: BTreeMap::new(), 189 values_to_handles: HashMap::new(), 190 } 191 } 192 193 /// Empty the constant pool of all data. 194 pub fn clear(&mut self) { 195 self.handles_to_values.clear(); 196 self.values_to_handles.clear(); 197 } 198 199 /// Insert constant data into the pool, returning a handle for later referencing; when constant 200 /// data is inserted that is a duplicate of previous constant data, the existing handle will be 201 /// returned. 202 pub fn insert(&mut self, constant_value: ConstantData) -> Constant { 203 if self.values_to_handles.contains_key(&constant_value) { 204 *self.values_to_handles.get(&constant_value).unwrap() 205 } else { 206 let constant_handle = Constant::new(self.len()); 207 self.set(constant_handle, constant_value); 208 constant_handle 209 } 210 } 211 212 /// Retrieve the constant data given a handle. 213 pub fn get(&self, constant_handle: Constant) -> &ConstantData { 214 assert!(self.handles_to_values.contains_key(&constant_handle)); 215 self.handles_to_values.get(&constant_handle).unwrap() 216 } 217 218 /// Link a constant handle to its value. This does not de-duplicate data but does avoid 219 /// replacing any existing constant values. use `set` to tie a specific `const42` to its value; 220 /// use `insert` to add a value and return the next available `const` entity. 221 pub fn set(&mut self, constant_handle: Constant, constant_value: ConstantData) { 222 let replaced = self 223 .handles_to_values 224 .insert(constant_handle, constant_value.clone()); 225 assert!( 226 replaced.is_none(), 227 "attempted to overwrite an existing constant {:?}: {:?} => {:?}", 228 constant_handle, 229 &constant_value, 230 replaced.unwrap() 231 ); 232 self.values_to_handles 233 .insert(constant_value, constant_handle); 234 } 235 236 /// Iterate over the constants in insertion order. 237 pub fn iter(&self) -> impl Iterator<Item = (&Constant, &ConstantData)> { 238 self.handles_to_values.iter() 239 } 240 241 /// Iterate over mutable entries in the constant pool in insertion order. 242 pub fn entries_mut(&mut self) -> impl Iterator<Item = &mut ConstantData> { 243 self.handles_to_values.values_mut() 244 } 245 246 /// Return the number of constants in the pool. 247 pub fn len(&self) -> usize { 248 self.handles_to_values.len() 249 } 250 251 /// Return the combined size of all of the constant values in the pool. 252 pub fn byte_size(&self) -> usize { 253 self.values_to_handles.keys().map(|c| c.len()).sum() 254 } 255 } 256 257 #[cfg(test)] 258 mod tests { 259 use super::*; 260 use std::string::ToString; 261 262 #[test] 263 fn empty() { 264 let sut = ConstantPool::new(); 265 assert_eq!(sut.len(), 0); 266 } 267 268 #[test] 269 fn insert() { 270 let mut sut = ConstantPool::new(); 271 sut.insert(vec![1, 2, 3].into()); 272 sut.insert(vec![4, 5, 6].into()); 273 assert_eq!(sut.len(), 2); 274 } 275 276 #[test] 277 fn insert_duplicate() { 278 let mut sut = ConstantPool::new(); 279 let a = sut.insert(vec![1, 2, 3].into()); 280 sut.insert(vec![4, 5, 6].into()); 281 let b = sut.insert(vec![1, 2, 3].into()); 282 assert_eq!(a, b); 283 } 284 285 #[test] 286 fn clear() { 287 let mut sut = ConstantPool::new(); 288 sut.insert(vec![1, 2, 3].into()); 289 assert_eq!(sut.len(), 1); 290 291 sut.clear(); 292 assert_eq!(sut.len(), 0); 293 } 294 295 #[test] 296 fn iteration_order() { 297 let mut sut = ConstantPool::new(); 298 sut.insert(vec![1, 2, 3].into()); 299 sut.insert(vec![4, 5, 6].into()); 300 sut.insert(vec![1, 2, 3].into()); 301 let data = sut.iter().map(|(_, v)| v).collect::<Vec<&ConstantData>>(); 302 assert_eq!(data, vec![&vec![1, 2, 3].into(), &vec![4, 5, 6].into()]); 303 } 304 305 #[test] 306 fn get() { 307 let mut sut = ConstantPool::new(); 308 let data = vec![1, 2, 3]; 309 let handle = sut.insert(data.clone().into()); 310 assert_eq!(sut.get(handle), &data.into()); 311 } 312 313 #[test] 314 fn set() { 315 let mut sut = ConstantPool::new(); 316 let handle = Constant::with_number(42).unwrap(); 317 let data = vec![1, 2, 3]; 318 sut.set(handle, data.clone().into()); 319 assert_eq!(sut.get(handle), &data.into()); 320 } 321 322 #[test] 323 #[should_panic] 324 fn disallow_overwriting_constant() { 325 let mut sut = ConstantPool::new(); 326 let handle = Constant::with_number(42).unwrap(); 327 sut.set(handle, vec![].into()); 328 sut.set(handle, vec![1].into()); 329 } 330 331 #[test] 332 #[should_panic] 333 fn get_nonexistent_constant() { 334 let sut = ConstantPool::new(); 335 let a = Constant::with_number(42).unwrap(); 336 sut.get(a); // panics, only use constants returned by ConstantPool 337 } 338 339 #[test] 340 fn display_constant_data() { 341 assert_eq!(ConstantData::from([0].as_ref()).to_string(), "0x00"); 342 assert_eq!(ConstantData::from([42].as_ref()).to_string(), "0x2a"); 343 assert_eq!( 344 ConstantData::from([3, 2, 1, 0].as_ref()).to_string(), 345 "0x00010203" 346 ); 347 assert_eq!( 348 ConstantData::from(3735928559u32.to_le_bytes().as_ref()).to_string(), 349 "0xdeadbeef" 350 ); 351 assert_eq!( 352 ConstantData::from(0x0102030405060708u64.to_le_bytes().as_ref()).to_string(), 353 "0x0102030405060708" 354 ); 355 } 356 357 #[test] 358 fn iterate_over_constant_data() { 359 let c = ConstantData::from([1, 2, 3].as_ref()); 360 let mut iter = c.iter(); 361 assert_eq!(iter.next(), Some(&1)); 362 assert_eq!(iter.next(), Some(&2)); 363 assert_eq!(iter.next(), Some(&3)); 364 assert_eq!(iter.next(), None); 365 } 366 367 #[test] 368 fn add_to_constant_data() { 369 let d = ConstantData::from([1, 2].as_ref()); 370 let e = d.append(i16::from(3u8)); 371 assert_eq!(e.into_vec(), vec![1, 2, 3, 0]) 372 } 373 374 #[test] 375 fn extend_constant_data() { 376 let d = ConstantData::from([1, 2].as_ref()); 377 assert_eq!(d.expand_to(4).into_vec(), vec![1, 2, 0, 0]) 378 } 379 380 #[test] 381 #[should_panic] 382 fn extend_constant_data_to_invalid_length() { 383 ConstantData::from([1, 2].as_ref()).expand_to(1); 384 } 385 386 #[test] 387 fn parse_constant_data_and_restringify() { 388 // Verify that parsing of `from` succeeds and stringifies to `to`. 389 fn parse_ok(from: &str, to: &str) { 390 let parsed = from.parse::<ConstantData>().unwrap(); 391 assert_eq!(parsed.to_string(), to); 392 } 393 394 // Verify that parsing of `from` fails with `error_msg`. 395 fn parse_err(from: &str, error_msg: &str) { 396 let parsed = from.parse::<ConstantData>(); 397 assert!( 398 parsed.is_err(), 399 "Expected a parse error but parsing succeeded: {}", 400 from 401 ); 402 assert_eq!(parsed.err().unwrap(), error_msg); 403 } 404 405 parse_ok("0x00", "0x00"); 406 parse_ok("0x00000042", "0x00000042"); 407 parse_ok( 408 "0x0102030405060708090a0b0c0d0e0f00", 409 "0x0102030405060708090a0b0c0d0e0f00", 410 ); 411 parse_ok("0x_0000_0043_21", "0x0000004321"); 412 413 parse_err("", "Expected a hexadecimal string, e.g. 0x1234"); 414 parse_err("0x", "Expected a hexadecimal string, e.g. 0x1234"); 415 parse_err( 416 "0x042", 417 "Hexadecimal string must have an even number of digits", 418 ); 419 parse_err( 420 "0x00000000000000000000000000000000000000000000000000", 421 "Hexadecimal string has too many digits to fit in a 128-bit vector", 422 ); 423 parse_err("0xrstu", "Unable to parse as hexadecimal"); 424 parse_err("0x__", "Hexadecimal string must have some digits"); 425 } 426 427 #[test] 428 fn verify_stored_bytes_in_constant_data() { 429 assert_eq!("0x01".parse::<ConstantData>().unwrap().into_vec(), [1]); 430 assert_eq!(ConstantData::from([1, 0].as_ref()).0, [1, 0]); 431 assert_eq!(ConstantData::from(vec![1, 0, 0, 0]).0, [1, 0, 0, 0]); 432 } 433 434 #[test] 435 fn check_constant_data_endianness_as_uimm128() { 436 fn parse_to_uimm128(from: &str) -> Vec<u8> { 437 from.parse::<ConstantData>() 438 .unwrap() 439 .expand_to(16) 440 .into_vec() 441 } 442 443 assert_eq!( 444 parse_to_uimm128("0x42"), 445 [0x42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 446 ); 447 assert_eq!( 448 parse_to_uimm128("0x00"), 449 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 450 ); 451 assert_eq!( 452 parse_to_uimm128("0x12345678"), 453 [0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 454 ); 455 assert_eq!( 456 parse_to_uimm128("0x1234_5678"), 457 [0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 458 ); 459 } 460 } 461