114b39bc2SSaúl Cabrera use crate::{isa::reg::Reg, masm::StackSlot}; 21b5c4ae8SSaúl Cabrera use smallvec::SmallVec; 314b39bc2SSaúl Cabrera use wasmparser::{Ieee32, Ieee64}; 4496237c2SNick Fitzgerald use wasmtime_environ::WasmValType; 514b39bc2SSaúl Cabrera 614b39bc2SSaúl Cabrera /// A typed register value used to track register values in the value 714b39bc2SSaúl Cabrera /// stack. 814b39bc2SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)] 914b39bc2SSaúl Cabrera pub struct TypedReg { 1014b39bc2SSaúl Cabrera /// The physical register. 1114b39bc2SSaúl Cabrera pub reg: Reg, 1214b39bc2SSaúl Cabrera /// The type associated to the physical register. 13496237c2SNick Fitzgerald pub ty: WasmValType, 1414b39bc2SSaúl Cabrera } 1514b39bc2SSaúl Cabrera 1614b39bc2SSaúl Cabrera impl TypedReg { 174b288ba8SSaúl Cabrera /// Create a new [`TypedReg`]. 18496237c2SNick Fitzgerald pub fn new(ty: WasmValType, reg: Reg) -> Self { 1914b39bc2SSaúl Cabrera Self { ty, reg } 2014b39bc2SSaúl Cabrera } 2114b39bc2SSaúl Cabrera 224b288ba8SSaúl Cabrera /// Create an i64 [`TypedReg`]. 2314b39bc2SSaúl Cabrera pub fn i64(reg: Reg) -> Self { 2414b39bc2SSaúl Cabrera Self { 25496237c2SNick Fitzgerald ty: WasmValType::I64, 2614b39bc2SSaúl Cabrera reg, 2714b39bc2SSaúl Cabrera } 2814b39bc2SSaúl Cabrera } 29a109d2abSSaúl Cabrera 30f0162a40SSaúl Cabrera /// Create an i32 [`TypedReg`]. 31a109d2abSSaúl Cabrera pub fn i32(reg: Reg) -> Self { 32a109d2abSSaúl Cabrera Self { 33496237c2SNick Fitzgerald ty: WasmValType::I32, 34a109d2abSSaúl Cabrera reg, 35a109d2abSSaúl Cabrera } 36a109d2abSSaúl Cabrera } 3717091e6fSJeffrey Charles 3817091e6fSJeffrey Charles /// Create an f64 [`TypedReg`]. 3917091e6fSJeffrey Charles pub fn f64(reg: Reg) -> Self { 4017091e6fSJeffrey Charles Self { 41496237c2SNick Fitzgerald ty: WasmValType::F64, 4217091e6fSJeffrey Charles reg, 4317091e6fSJeffrey Charles } 4417091e6fSJeffrey Charles } 4517091e6fSJeffrey Charles 4617091e6fSJeffrey Charles /// Create an f32 [`TypedReg`]. 4717091e6fSJeffrey Charles pub fn f32(reg: Reg) -> Self { 4817091e6fSJeffrey Charles Self { 49496237c2SNick Fitzgerald ty: WasmValType::F32, 5017091e6fSJeffrey Charles reg, 5117091e6fSJeffrey Charles } 5217091e6fSJeffrey Charles } 5314b39bc2SSaúl Cabrera } 5414b39bc2SSaúl Cabrera 5514b39bc2SSaúl Cabrera impl From<TypedReg> for Reg { 5614b39bc2SSaúl Cabrera fn from(tr: TypedReg) -> Self { 5714b39bc2SSaúl Cabrera tr.reg 5814b39bc2SSaúl Cabrera } 5914b39bc2SSaúl Cabrera } 6014b39bc2SSaúl Cabrera 6114b39bc2SSaúl Cabrera /// A local value. 6214b39bc2SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)] 6314b39bc2SSaúl Cabrera pub struct Local { 6414b39bc2SSaúl Cabrera /// The index of the local. 6514b39bc2SSaúl Cabrera pub index: u32, 6614b39bc2SSaúl Cabrera /// The type of the local. 67496237c2SNick Fitzgerald pub ty: WasmValType, 6814b39bc2SSaúl Cabrera } 6914b39bc2SSaúl Cabrera 7014b39bc2SSaúl Cabrera /// A memory value. 7114b39bc2SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)] 7214b39bc2SSaúl Cabrera pub struct Memory { 7314b39bc2SSaúl Cabrera /// The type associated with the memory offset. 74496237c2SNick Fitzgerald pub ty: WasmValType, 7514b39bc2SSaúl Cabrera /// The stack slot corresponding to the memory value. 7614b39bc2SSaúl Cabrera pub slot: StackSlot, 7714b39bc2SSaúl Cabrera } 78835abbcdSSaúl Cabrera 79835abbcdSSaúl Cabrera /// Value definition to be used within the shadow stack. 80a50c4972SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)] 81835abbcdSSaúl Cabrera pub(crate) enum Val { 82835abbcdSSaúl Cabrera /// I32 Constant. 83835abbcdSSaúl Cabrera I32(i32), 847c5c7e4bSSaúl Cabrera /// I64 Constant. 857c5c7e4bSSaúl Cabrera I64(i64), 8614b39bc2SSaúl Cabrera /// F32 Constant. 8714b39bc2SSaúl Cabrera F32(Ieee32), 8814b39bc2SSaúl Cabrera /// F64 Constant. 8914b39bc2SSaúl Cabrera F64(Ieee64), 9014b39bc2SSaúl Cabrera /// A register value. 9114b39bc2SSaúl Cabrera Reg(TypedReg), 92835abbcdSSaúl Cabrera /// A local slot. 9314b39bc2SSaúl Cabrera Local(Local), 94835abbcdSSaúl Cabrera /// Offset to a memory location. 9514b39bc2SSaúl Cabrera Memory(Memory), 9614b39bc2SSaúl Cabrera } 9714b39bc2SSaúl Cabrera 9814b39bc2SSaúl Cabrera impl From<TypedReg> for Val { 9914b39bc2SSaúl Cabrera fn from(tr: TypedReg) -> Self { 10014b39bc2SSaúl Cabrera Val::Reg(tr) 10114b39bc2SSaúl Cabrera } 10214b39bc2SSaúl Cabrera } 10314b39bc2SSaúl Cabrera 10414b39bc2SSaúl Cabrera impl From<Local> for Val { 10514b39bc2SSaúl Cabrera fn from(local: Local) -> Self { 10614b39bc2SSaúl Cabrera Val::Local(local) 10714b39bc2SSaúl Cabrera } 10814b39bc2SSaúl Cabrera } 10914b39bc2SSaúl Cabrera 11014b39bc2SSaúl Cabrera impl From<Memory> for Val { 11114b39bc2SSaúl Cabrera fn from(mem: Memory) -> Self { 11214b39bc2SSaúl Cabrera Val::Memory(mem) 11314b39bc2SSaúl Cabrera } 114835abbcdSSaúl Cabrera } 115835abbcdSSaúl Cabrera 1164b288ba8SSaúl Cabrera impl TryFrom<u32> for Val { 1174b288ba8SSaúl Cabrera type Error = anyhow::Error; 1184b288ba8SSaúl Cabrera fn try_from(value: u32) -> Result<Self, Self::Error> { 1194b288ba8SSaúl Cabrera i32::try_from(value).map(Val::i32).map_err(Into::into) 1204b288ba8SSaúl Cabrera } 1214b288ba8SSaúl Cabrera } 1224b288ba8SSaúl Cabrera 123835abbcdSSaúl Cabrera impl Val { 124835abbcdSSaúl Cabrera /// Create a new I32 constant value. 125835abbcdSSaúl Cabrera pub fn i32(v: i32) -> Self { 126835abbcdSSaúl Cabrera Self::I32(v) 127835abbcdSSaúl Cabrera } 128835abbcdSSaúl Cabrera 1297c5c7e4bSSaúl Cabrera /// Create a new I64 constant value. 1307c5c7e4bSSaúl Cabrera pub fn i64(v: i64) -> Self { 1317c5c7e4bSSaúl Cabrera Self::I64(v) 1327c5c7e4bSSaúl Cabrera } 1337c5c7e4bSSaúl Cabrera 13414b39bc2SSaúl Cabrera /// Create a new F32 constant value. 13514b39bc2SSaúl Cabrera pub fn f32(v: Ieee32) -> Self { 13614b39bc2SSaúl Cabrera Self::F32(v) 13714b39bc2SSaúl Cabrera } 13814b39bc2SSaúl Cabrera 13914b39bc2SSaúl Cabrera pub fn f64(v: Ieee64) -> Self { 14014b39bc2SSaúl Cabrera Self::F64(v) 14114b39bc2SSaúl Cabrera } 14214b39bc2SSaúl Cabrera 143835abbcdSSaúl Cabrera /// Create a new Reg value. 144496237c2SNick Fitzgerald pub fn reg(reg: Reg, ty: WasmValType) -> Self { 14514b39bc2SSaúl Cabrera Self::Reg(TypedReg { reg, ty }) 146835abbcdSSaúl Cabrera } 147835abbcdSSaúl Cabrera 148835abbcdSSaúl Cabrera /// Create a new Local value. 149496237c2SNick Fitzgerald pub fn local(index: u32, ty: WasmValType) -> Self { 15014b39bc2SSaúl Cabrera Self::Local(Local { index, ty }) 15114b39bc2SSaúl Cabrera } 15214b39bc2SSaúl Cabrera 15314b39bc2SSaúl Cabrera /// Create a Memory value. 154496237c2SNick Fitzgerald pub fn mem(ty: WasmValType, slot: StackSlot) -> Self { 15514b39bc2SSaúl Cabrera Self::Memory(Memory { ty, slot }) 156835abbcdSSaúl Cabrera } 157835abbcdSSaúl Cabrera 158835abbcdSSaúl Cabrera /// Check whether the value is a register. 159835abbcdSSaúl Cabrera pub fn is_reg(&self) -> bool { 160835abbcdSSaúl Cabrera match *self { 161835abbcdSSaúl Cabrera Self::Reg(_) => true, 162835abbcdSSaúl Cabrera _ => false, 163835abbcdSSaúl Cabrera } 164835abbcdSSaúl Cabrera } 165835abbcdSSaúl Cabrera 166*0e9121daSFrankReh /// Check whether the value is a memory offset. 167af4d94c8SSaúl Cabrera pub fn is_mem(&self) -> bool { 168af4d94c8SSaúl Cabrera match *self { 169af4d94c8SSaúl Cabrera Self::Memory(_) => true, 170af4d94c8SSaúl Cabrera _ => false, 171af4d94c8SSaúl Cabrera } 172af4d94c8SSaúl Cabrera } 173af4d94c8SSaúl Cabrera 17455f9a4bdSJeffrey Charles /// Check whether the value is a constant. 17555f9a4bdSJeffrey Charles pub fn is_const(&self) -> bool { 17655f9a4bdSJeffrey Charles match *self { 17755f9a4bdSJeffrey Charles Val::I32(_) | Val::I64(_) | Val::F32(_) | Val::F64(_) => true, 17855f9a4bdSJeffrey Charles _ => false, 17955f9a4bdSJeffrey Charles } 18055f9a4bdSJeffrey Charles } 18155f9a4bdSJeffrey Charles 182321294a5SJeffrey Charles /// Check whether the value is local with a particular index. 183321294a5SJeffrey Charles pub fn is_local_at_index(&self, index: u32) -> bool { 184321294a5SJeffrey Charles match *self { 185321294a5SJeffrey Charles Self::Local(Local { index: i, .. }) if i == index => true, 186321294a5SJeffrey Charles _ => false, 187321294a5SJeffrey Charles } 188321294a5SJeffrey Charles } 189321294a5SJeffrey Charles 190835abbcdSSaúl Cabrera /// Get the register representation of the value. 191835abbcdSSaúl Cabrera /// 192835abbcdSSaúl Cabrera /// # Panics 193835abbcdSSaúl Cabrera /// This method will panic if the value is not a register. 194f0162a40SSaúl Cabrera pub fn unwrap_reg(&self) -> TypedReg { 195835abbcdSSaúl Cabrera match self { 19614b39bc2SSaúl Cabrera Self::Reg(tr) => *tr, 197835abbcdSSaúl Cabrera v => panic!("expected value {:?} to be a register", v), 198835abbcdSSaúl Cabrera } 199835abbcdSSaúl Cabrera } 200835abbcdSSaúl Cabrera 201835abbcdSSaúl Cabrera /// Get the integer representation of the value. 202835abbcdSSaúl Cabrera /// 203835abbcdSSaúl Cabrera /// # Panics 204835abbcdSSaúl Cabrera /// This method will panic if the value is not an i32. 205f0162a40SSaúl Cabrera pub fn unwrap_i32(&self) -> i32 { 206835abbcdSSaúl Cabrera match self { 207835abbcdSSaúl Cabrera Self::I32(v) => *v, 208835abbcdSSaúl Cabrera v => panic!("expected value {:?} to be i32", v), 209835abbcdSSaúl Cabrera } 210835abbcdSSaúl Cabrera } 211835abbcdSSaúl Cabrera 2127c5c7e4bSSaúl Cabrera /// Get the integer representation of the value. 2137c5c7e4bSSaúl Cabrera /// 2147c5c7e4bSSaúl Cabrera /// # Panics 2157c5c7e4bSSaúl Cabrera /// This method will panic if the value is not an i64. 216f0162a40SSaúl Cabrera pub fn unwrap_i64(&self) -> i64 { 2177c5c7e4bSSaúl Cabrera match self { 2187c5c7e4bSSaúl Cabrera Self::I64(v) => *v, 2197c5c7e4bSSaúl Cabrera v => panic!("expected value {:?} to be i64", v), 2207c5c7e4bSSaúl Cabrera } 2217c5c7e4bSSaúl Cabrera } 2227c5c7e4bSSaúl Cabrera 223f0162a40SSaúl Cabrera /// Returns the underlying memory value if it is one, panics otherwise. 224f0162a40SSaúl Cabrera pub fn unwrap_mem(&self) -> Memory { 225f0162a40SSaúl Cabrera match self { 226f0162a40SSaúl Cabrera Self::Memory(m) => *m, 227f0162a40SSaúl Cabrera v => panic!("expected value {:?} to be a Memory", v), 228f0162a40SSaúl Cabrera } 229f0162a40SSaúl Cabrera } 230f0162a40SSaúl Cabrera 231835abbcdSSaúl Cabrera /// Check whether the value is an i32 constant. 232835abbcdSSaúl Cabrera pub fn is_i32_const(&self) -> bool { 233835abbcdSSaúl Cabrera match *self { 234835abbcdSSaúl Cabrera Self::I32(_) => true, 235835abbcdSSaúl Cabrera _ => false, 236835abbcdSSaúl Cabrera } 237835abbcdSSaúl Cabrera } 2387c5c7e4bSSaúl Cabrera 2397c5c7e4bSSaúl Cabrera /// Check whether the value is an i64 constant. 2407c5c7e4bSSaúl Cabrera pub fn is_i64_const(&self) -> bool { 2417c5c7e4bSSaúl Cabrera match *self { 2427c5c7e4bSSaúl Cabrera Self::I64(_) => true, 2437c5c7e4bSSaúl Cabrera _ => false, 2447c5c7e4bSSaúl Cabrera } 2457c5c7e4bSSaúl Cabrera } 24614b39bc2SSaúl Cabrera 24714b39bc2SSaúl Cabrera /// Get the type of the value. 248496237c2SNick Fitzgerald pub fn ty(&self) -> WasmValType { 24914b39bc2SSaúl Cabrera match self { 250496237c2SNick Fitzgerald Val::I32(_) => WasmValType::I32, 251496237c2SNick Fitzgerald Val::I64(_) => WasmValType::I64, 252496237c2SNick Fitzgerald Val::F32(_) => WasmValType::F32, 253496237c2SNick Fitzgerald Val::F64(_) => WasmValType::F64, 25414b39bc2SSaúl Cabrera Val::Reg(r) => r.ty, 25514b39bc2SSaúl Cabrera Val::Memory(m) => m.ty, 25614b39bc2SSaúl Cabrera Val::Local(l) => l.ty, 25714b39bc2SSaúl Cabrera } 25814b39bc2SSaúl Cabrera } 259835abbcdSSaúl Cabrera } 260835abbcdSSaúl Cabrera 261835abbcdSSaúl Cabrera /// The shadow stack used for compilation. 262835abbcdSSaúl Cabrera #[derive(Default, Debug)] 263835abbcdSSaúl Cabrera pub(crate) struct Stack { 2641b5c4ae8SSaúl Cabrera // NB: The 64 is chosen arbitrarily. We can adjust as we see fit. 2651b5c4ae8SSaúl Cabrera inner: SmallVec<[Val; 64]>, 266835abbcdSSaúl Cabrera } 267835abbcdSSaúl Cabrera 268835abbcdSSaúl Cabrera impl Stack { 269835abbcdSSaúl Cabrera /// Allocate a new stack. 270835abbcdSSaúl Cabrera pub fn new() -> Self { 271835abbcdSSaúl Cabrera Self { 272835abbcdSSaúl Cabrera inner: Default::default(), 273835abbcdSSaúl Cabrera } 274835abbcdSSaúl Cabrera } 275835abbcdSSaúl Cabrera 276321294a5SJeffrey Charles /// Returns true if the stack contains a local with the provided index 277321294a5SJeffrey Charles /// except if the only time the local appears is the top element. 278321294a5SJeffrey Charles pub fn contains_latent_local(&self, index: u32) -> bool { 279321294a5SJeffrey Charles self.inner 280321294a5SJeffrey Charles .iter() 281321294a5SJeffrey Charles // Iterate top-to-bottom so we can skip the top element and stop 282321294a5SJeffrey Charles // when we see a memory element. 283321294a5SJeffrey Charles .rev() 284321294a5SJeffrey Charles // The local is not latent if it's the top element because the top 285321294a5SJeffrey Charles // element will be popped next which materializes the local. 286321294a5SJeffrey Charles .skip(1) 287321294a5SJeffrey Charles // Stop when we see a memory element because that marks where we 288321294a5SJeffrey Charles // spilled up to so there will not be any locals past this point. 289321294a5SJeffrey Charles .take_while(|v| !v.is_mem()) 290321294a5SJeffrey Charles .any(|v| v.is_local_at_index(index)) 291321294a5SJeffrey Charles } 292321294a5SJeffrey Charles 293a109d2abSSaúl Cabrera /// Extend the stack with the given elements. 294a109d2abSSaúl Cabrera pub fn extend(&mut self, values: impl IntoIterator<Item = Val>) { 295a109d2abSSaúl Cabrera self.inner.extend(values); 296a109d2abSSaúl Cabrera } 297a109d2abSSaúl Cabrera 298a109d2abSSaúl Cabrera /// Inserts many values at the given index. 2991b5c4ae8SSaúl Cabrera pub fn insert_many(&mut self, at: usize, values: &[Val]) { 300a109d2abSSaúl Cabrera debug_assert!(at <= self.len()); 3011b5c4ae8SSaúl Cabrera 3021b5c4ae8SSaúl Cabrera if at == self.len() { 3031b5c4ae8SSaúl Cabrera self.inner.extend_from_slice(values); 304a109d2abSSaúl Cabrera } else { 3051b5c4ae8SSaúl Cabrera self.inner.insert_from_slice(at, values); 306a109d2abSSaúl Cabrera } 30720c58362SSaúl Cabrera } 30820c58362SSaúl Cabrera 309af4d94c8SSaúl Cabrera /// Get the length of the stack. 310af4d94c8SSaúl Cabrera pub fn len(&self) -> usize { 311af4d94c8SSaúl Cabrera self.inner.len() 312af4d94c8SSaúl Cabrera } 313af4d94c8SSaúl Cabrera 314835abbcdSSaúl Cabrera /// Push a value to the stack. 315835abbcdSSaúl Cabrera pub fn push(&mut self, val: Val) { 3167690c500STrevor Elliott self.inner.push(val); 317835abbcdSSaúl Cabrera } 318835abbcdSSaúl Cabrera 319835abbcdSSaúl Cabrera /// Peek into the top in the stack. 3207c5c7e4bSSaúl Cabrera pub fn peek(&self) -> Option<&Val> { 3217690c500STrevor Elliott self.inner.last() 322835abbcdSSaúl Cabrera } 323835abbcdSSaúl Cabrera 324af4d94c8SSaúl Cabrera /// Returns an iterator referencing the last n items of the stack, 325af4d94c8SSaúl Cabrera /// in bottom-most to top-most order. 326af4d94c8SSaúl Cabrera pub fn peekn(&self, n: usize) -> impl Iterator<Item = &Val> + '_ { 327af4d94c8SSaúl Cabrera let len = self.len(); 328af4d94c8SSaúl Cabrera assert!(n <= len); 329af4d94c8SSaúl Cabrera 330af4d94c8SSaúl Cabrera let partition = len - n; 3317690c500STrevor Elliott self.inner[partition..].into_iter() 332af4d94c8SSaúl Cabrera } 333af4d94c8SSaúl Cabrera 334835abbcdSSaúl Cabrera /// Pops the top element of the stack, if any. 335835abbcdSSaúl Cabrera pub fn pop(&mut self) -> Option<Val> { 3367690c500STrevor Elliott self.inner.pop() 337835abbcdSSaúl Cabrera } 338835abbcdSSaúl Cabrera 3397c5c7e4bSSaúl Cabrera /// Pops the element at the top of the stack if it is an i32 const; 340835abbcdSSaúl Cabrera /// returns `None` otherwise. 341835abbcdSSaúl Cabrera pub fn pop_i32_const(&mut self) -> Option<i32> { 342835abbcdSSaúl Cabrera match self.peek() { 343f0162a40SSaúl Cabrera Some(v) => v.is_i32_const().then(|| self.pop().unwrap().unwrap_i32()), 344835abbcdSSaúl Cabrera _ => None, 345835abbcdSSaúl Cabrera } 346835abbcdSSaúl Cabrera } 347835abbcdSSaúl Cabrera 3487c5c7e4bSSaúl Cabrera /// Pops the element at the top of the stack if it is an i64 const; 3497c5c7e4bSSaúl Cabrera /// returns `None` otherwise. 3507c5c7e4bSSaúl Cabrera pub fn pop_i64_const(&mut self) -> Option<i64> { 3517c5c7e4bSSaúl Cabrera match self.peek() { 352f0162a40SSaúl Cabrera Some(v) => v.is_i64_const().then(|| self.pop().unwrap().unwrap_i64()), 3537c5c7e4bSSaúl Cabrera _ => None, 3547c5c7e4bSSaúl Cabrera } 3557c5c7e4bSSaúl Cabrera } 3567c5c7e4bSSaúl Cabrera 357835abbcdSSaúl Cabrera /// Pops the element at the top of the stack if it is a register; 358835abbcdSSaúl Cabrera /// returns `None` otherwise. 35914b39bc2SSaúl Cabrera pub fn pop_reg(&mut self) -> Option<TypedReg> { 360835abbcdSSaúl Cabrera match self.peek() { 361f0162a40SSaúl Cabrera Some(v) => v.is_reg().then(|| self.pop().unwrap().unwrap_reg()), 362835abbcdSSaúl Cabrera _ => None, 363835abbcdSSaúl Cabrera } 364835abbcdSSaúl Cabrera } 365835abbcdSSaúl Cabrera 366835abbcdSSaúl Cabrera /// Pops the given register if it is at the top of the stack; 367835abbcdSSaúl Cabrera /// returns `None` otherwise. 36814b39bc2SSaúl Cabrera pub fn pop_named_reg(&mut self, reg: Reg) -> Option<TypedReg> { 369835abbcdSSaúl Cabrera match self.peek() { 37014b39bc2SSaúl Cabrera Some(v) => { 371f0162a40SSaúl Cabrera (v.is_reg() && v.unwrap_reg().reg == reg).then(|| self.pop().unwrap().unwrap_reg()) 37214b39bc2SSaúl Cabrera } 373835abbcdSSaúl Cabrera _ => None, 374835abbcdSSaúl Cabrera } 375835abbcdSSaúl Cabrera } 376835abbcdSSaúl Cabrera 377835abbcdSSaúl Cabrera /// Get a mutable reference to the inner stack representation. 3781b5c4ae8SSaúl Cabrera pub fn inner_mut(&mut self) -> &mut SmallVec<[Val; 64]> { 379835abbcdSSaúl Cabrera &mut self.inner 380835abbcdSSaúl Cabrera } 38150733725SSaúl Cabrera 382446a7f5eSSaúl Cabrera /// Get a reference to the inner stack representation. 3831b5c4ae8SSaúl Cabrera pub fn inner(&self) -> &SmallVec<[Val; 64]> { 384446a7f5eSSaúl Cabrera &self.inner 385446a7f5eSSaúl Cabrera } 386446a7f5eSSaúl Cabrera 38750733725SSaúl Cabrera /// Calculates the size of, in bytes, of the top n [Memory] entries 38850733725SSaúl Cabrera /// in the value stack. 38950733725SSaúl Cabrera pub fn sizeof(&self, top: usize) -> u32 { 39050733725SSaúl Cabrera self.peekn(top).fold(0, |acc, v| { 39150733725SSaúl Cabrera if v.is_mem() { 39250733725SSaúl Cabrera acc + v.unwrap_mem().slot.size 39350733725SSaúl Cabrera } else { 39450733725SSaúl Cabrera acc 39550733725SSaúl Cabrera } 39650733725SSaúl Cabrera }) 39750733725SSaúl Cabrera } 398835abbcdSSaúl Cabrera } 399835abbcdSSaúl Cabrera 400835abbcdSSaúl Cabrera #[cfg(test)] 401835abbcdSSaúl Cabrera mod tests { 402835abbcdSSaúl Cabrera use super::{Stack, Val}; 403835abbcdSSaúl Cabrera use crate::isa::reg::Reg; 404496237c2SNick Fitzgerald use wasmtime_environ::WasmValType; 405835abbcdSSaúl Cabrera 406835abbcdSSaúl Cabrera #[test] 407835abbcdSSaúl Cabrera fn test_pop_i32_const() { 408835abbcdSSaúl Cabrera let mut stack = Stack::new(); 409835abbcdSSaúl Cabrera stack.push(Val::i32(33i32)); 410835abbcdSSaúl Cabrera assert_eq!(33, stack.pop_i32_const().unwrap()); 411835abbcdSSaúl Cabrera 412496237c2SNick Fitzgerald stack.push(Val::local(10, WasmValType::I32)); 413835abbcdSSaúl Cabrera assert!(stack.pop_i32_const().is_none()); 414835abbcdSSaúl Cabrera } 415835abbcdSSaúl Cabrera 416835abbcdSSaúl Cabrera #[test] 417835abbcdSSaúl Cabrera fn test_pop_reg() { 418835abbcdSSaúl Cabrera let mut stack = Stack::new(); 419835abbcdSSaúl Cabrera let reg = Reg::int(2usize); 420496237c2SNick Fitzgerald stack.push(Val::reg(reg, WasmValType::I32)); 421835abbcdSSaúl Cabrera stack.push(Val::i32(4)); 422835abbcdSSaúl Cabrera 423835abbcdSSaúl Cabrera assert_eq!(None, stack.pop_reg()); 424835abbcdSSaúl Cabrera let _ = stack.pop().unwrap(); 42514b39bc2SSaúl Cabrera assert_eq!(reg, stack.pop_reg().unwrap().reg); 426835abbcdSSaúl Cabrera } 427835abbcdSSaúl Cabrera 428835abbcdSSaúl Cabrera #[test] 429835abbcdSSaúl Cabrera fn test_pop_named_reg() { 430835abbcdSSaúl Cabrera let mut stack = Stack::new(); 431835abbcdSSaúl Cabrera let reg = Reg::int(2usize); 432496237c2SNick Fitzgerald stack.push(Val::reg(reg, WasmValType::I32)); 433496237c2SNick Fitzgerald stack.push(Val::reg(Reg::int(4), WasmValType::I32)); 434835abbcdSSaúl Cabrera 435835abbcdSSaúl Cabrera assert_eq!(None, stack.pop_named_reg(reg)); 436835abbcdSSaúl Cabrera let _ = stack.pop().unwrap(); 43714b39bc2SSaúl Cabrera assert_eq!(reg, stack.pop_named_reg(reg).unwrap().reg); 438835abbcdSSaúl Cabrera } 439835abbcdSSaúl Cabrera } 440