xref: /wasmtime-44.0.1/winch/codegen/src/stack.rs (revision 496237c2)
114b39bc2SSaúl Cabrera use crate::{isa::reg::Reg, masm::StackSlot};
214b39bc2SSaúl Cabrera use wasmparser::{Ieee32, Ieee64};
3*496237c2SNick Fitzgerald use wasmtime_environ::WasmValType;
414b39bc2SSaúl Cabrera 
514b39bc2SSaúl Cabrera /// A typed register value used to track register values in the value
614b39bc2SSaúl Cabrera /// stack.
714b39bc2SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)]
814b39bc2SSaúl Cabrera pub struct TypedReg {
914b39bc2SSaúl Cabrera     /// The physical register.
1014b39bc2SSaúl Cabrera     pub reg: Reg,
1114b39bc2SSaúl Cabrera     /// The type associated to the physical register.
12*496237c2SNick Fitzgerald     pub ty: WasmValType,
1314b39bc2SSaúl Cabrera }
1414b39bc2SSaúl Cabrera 
1514b39bc2SSaúl Cabrera impl TypedReg {
164b288ba8SSaúl Cabrera     /// Create a new [`TypedReg`].
17*496237c2SNick Fitzgerald     pub fn new(ty: WasmValType, reg: Reg) -> Self {
1814b39bc2SSaúl Cabrera         Self { ty, reg }
1914b39bc2SSaúl Cabrera     }
2014b39bc2SSaúl Cabrera 
214b288ba8SSaúl Cabrera     /// Create an i64 [`TypedReg`].
2214b39bc2SSaúl Cabrera     pub fn i64(reg: Reg) -> Self {
2314b39bc2SSaúl Cabrera         Self {
24*496237c2SNick Fitzgerald             ty: WasmValType::I64,
2514b39bc2SSaúl Cabrera             reg,
2614b39bc2SSaúl Cabrera         }
2714b39bc2SSaúl Cabrera     }
28a109d2abSSaúl Cabrera 
29f0162a40SSaúl Cabrera     /// Create an i32 [`TypedReg`].
30a109d2abSSaúl Cabrera     pub fn i32(reg: Reg) -> Self {
31a109d2abSSaúl Cabrera         Self {
32*496237c2SNick Fitzgerald             ty: WasmValType::I32,
33a109d2abSSaúl Cabrera             reg,
34a109d2abSSaúl Cabrera         }
35a109d2abSSaúl Cabrera     }
3617091e6fSJeffrey Charles 
3717091e6fSJeffrey Charles     /// Create an f64 [`TypedReg`].
3817091e6fSJeffrey Charles     pub fn f64(reg: Reg) -> Self {
3917091e6fSJeffrey Charles         Self {
40*496237c2SNick Fitzgerald             ty: WasmValType::F64,
4117091e6fSJeffrey Charles             reg,
4217091e6fSJeffrey Charles         }
4317091e6fSJeffrey Charles     }
4417091e6fSJeffrey Charles 
4517091e6fSJeffrey Charles     /// Create an f32 [`TypedReg`].
4617091e6fSJeffrey Charles     pub fn f32(reg: Reg) -> Self {
4717091e6fSJeffrey Charles         Self {
48*496237c2SNick Fitzgerald             ty: WasmValType::F32,
4917091e6fSJeffrey Charles             reg,
5017091e6fSJeffrey Charles         }
5117091e6fSJeffrey Charles     }
5214b39bc2SSaúl Cabrera }
5314b39bc2SSaúl Cabrera 
5414b39bc2SSaúl Cabrera impl From<TypedReg> for Reg {
5514b39bc2SSaúl Cabrera     fn from(tr: TypedReg) -> Self {
5614b39bc2SSaúl Cabrera         tr.reg
5714b39bc2SSaúl Cabrera     }
5814b39bc2SSaúl Cabrera }
5914b39bc2SSaúl Cabrera 
6014b39bc2SSaúl Cabrera /// A local value.
6114b39bc2SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)]
6214b39bc2SSaúl Cabrera pub struct Local {
6314b39bc2SSaúl Cabrera     /// The index of the local.
6414b39bc2SSaúl Cabrera     pub index: u32,
6514b39bc2SSaúl Cabrera     /// The type of the local.
66*496237c2SNick Fitzgerald     pub ty: WasmValType,
6714b39bc2SSaúl Cabrera }
6814b39bc2SSaúl Cabrera 
6914b39bc2SSaúl Cabrera /// A memory value.
7014b39bc2SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)]
7114b39bc2SSaúl Cabrera pub struct Memory {
7214b39bc2SSaúl Cabrera     /// The type associated with the memory offset.
73*496237c2SNick Fitzgerald     pub ty: WasmValType,
7414b39bc2SSaúl Cabrera     /// The stack slot corresponding to the memory value.
7514b39bc2SSaúl Cabrera     pub slot: StackSlot,
7614b39bc2SSaúl Cabrera }
77835abbcdSSaúl Cabrera 
78835abbcdSSaúl Cabrera /// Value definition to be used within the shadow stack.
79a50c4972SSaúl Cabrera #[derive(Debug, Eq, PartialEq, Copy, Clone)]
80835abbcdSSaúl Cabrera pub(crate) enum Val {
81835abbcdSSaúl Cabrera     /// I32 Constant.
82835abbcdSSaúl Cabrera     I32(i32),
837c5c7e4bSSaúl Cabrera     /// I64 Constant.
847c5c7e4bSSaúl Cabrera     I64(i64),
8514b39bc2SSaúl Cabrera     /// F32 Constant.
8614b39bc2SSaúl Cabrera     F32(Ieee32),
8714b39bc2SSaúl Cabrera     /// F64 Constant.
8814b39bc2SSaúl Cabrera     F64(Ieee64),
8914b39bc2SSaúl Cabrera     /// A register value.
9014b39bc2SSaúl Cabrera     Reg(TypedReg),
91835abbcdSSaúl Cabrera     /// A local slot.
9214b39bc2SSaúl Cabrera     Local(Local),
93835abbcdSSaúl Cabrera     /// Offset to a memory location.
9414b39bc2SSaúl Cabrera     Memory(Memory),
9514b39bc2SSaúl Cabrera }
9614b39bc2SSaúl Cabrera 
9714b39bc2SSaúl Cabrera impl From<TypedReg> for Val {
9814b39bc2SSaúl Cabrera     fn from(tr: TypedReg) -> Self {
9914b39bc2SSaúl Cabrera         Val::Reg(tr)
10014b39bc2SSaúl Cabrera     }
10114b39bc2SSaúl Cabrera }
10214b39bc2SSaúl Cabrera 
10314b39bc2SSaúl Cabrera impl From<Local> for Val {
10414b39bc2SSaúl Cabrera     fn from(local: Local) -> Self {
10514b39bc2SSaúl Cabrera         Val::Local(local)
10614b39bc2SSaúl Cabrera     }
10714b39bc2SSaúl Cabrera }
10814b39bc2SSaúl Cabrera 
10914b39bc2SSaúl Cabrera impl From<Memory> for Val {
11014b39bc2SSaúl Cabrera     fn from(mem: Memory) -> Self {
11114b39bc2SSaúl Cabrera         Val::Memory(mem)
11214b39bc2SSaúl Cabrera     }
113835abbcdSSaúl Cabrera }
114835abbcdSSaúl Cabrera 
1154b288ba8SSaúl Cabrera impl TryFrom<u32> for Val {
1164b288ba8SSaúl Cabrera     type Error = anyhow::Error;
1174b288ba8SSaúl Cabrera     fn try_from(value: u32) -> Result<Self, Self::Error> {
1184b288ba8SSaúl Cabrera         i32::try_from(value).map(Val::i32).map_err(Into::into)
1194b288ba8SSaúl Cabrera     }
1204b288ba8SSaúl Cabrera }
1214b288ba8SSaúl Cabrera 
122835abbcdSSaúl Cabrera impl Val {
123835abbcdSSaúl Cabrera     /// Create a new I32 constant value.
124835abbcdSSaúl Cabrera     pub fn i32(v: i32) -> Self {
125835abbcdSSaúl Cabrera         Self::I32(v)
126835abbcdSSaúl Cabrera     }
127835abbcdSSaúl Cabrera 
1287c5c7e4bSSaúl Cabrera     /// Create a new I64 constant value.
1297c5c7e4bSSaúl Cabrera     pub fn i64(v: i64) -> Self {
1307c5c7e4bSSaúl Cabrera         Self::I64(v)
1317c5c7e4bSSaúl Cabrera     }
1327c5c7e4bSSaúl Cabrera 
13314b39bc2SSaúl Cabrera     /// Create a new F32 constant value.
13414b39bc2SSaúl Cabrera     pub fn f32(v: Ieee32) -> Self {
13514b39bc2SSaúl Cabrera         Self::F32(v)
13614b39bc2SSaúl Cabrera     }
13714b39bc2SSaúl Cabrera 
13814b39bc2SSaúl Cabrera     pub fn f64(v: Ieee64) -> Self {
13914b39bc2SSaúl Cabrera         Self::F64(v)
14014b39bc2SSaúl Cabrera     }
14114b39bc2SSaúl Cabrera 
142835abbcdSSaúl Cabrera     /// Create a new Reg value.
143*496237c2SNick Fitzgerald     pub fn reg(reg: Reg, ty: WasmValType) -> Self {
14414b39bc2SSaúl Cabrera         Self::Reg(TypedReg { reg, ty })
145835abbcdSSaúl Cabrera     }
146835abbcdSSaúl Cabrera 
147835abbcdSSaúl Cabrera     /// Create a new Local value.
148*496237c2SNick Fitzgerald     pub fn local(index: u32, ty: WasmValType) -> Self {
14914b39bc2SSaúl Cabrera         Self::Local(Local { index, ty })
15014b39bc2SSaúl Cabrera     }
15114b39bc2SSaúl Cabrera 
15214b39bc2SSaúl Cabrera     /// Create a Memory value.
153*496237c2SNick Fitzgerald     pub fn mem(ty: WasmValType, slot: StackSlot) -> Self {
15414b39bc2SSaúl Cabrera         Self::Memory(Memory { ty, slot })
155835abbcdSSaúl Cabrera     }
156835abbcdSSaúl Cabrera 
157835abbcdSSaúl Cabrera     /// Check whether the value is a register.
158835abbcdSSaúl Cabrera     pub fn is_reg(&self) -> bool {
159835abbcdSSaúl Cabrera         match *self {
160835abbcdSSaúl Cabrera             Self::Reg(_) => true,
161835abbcdSSaúl Cabrera             _ => false,
162835abbcdSSaúl Cabrera         }
163835abbcdSSaúl Cabrera     }
164835abbcdSSaúl Cabrera 
165af4d94c8SSaúl Cabrera     /// Check wheter the value is a memory offset.
166af4d94c8SSaúl Cabrera     pub fn is_mem(&self) -> bool {
167af4d94c8SSaúl Cabrera         match *self {
168af4d94c8SSaúl Cabrera             Self::Memory(_) => true,
169af4d94c8SSaúl Cabrera             _ => false,
170af4d94c8SSaúl Cabrera         }
171af4d94c8SSaúl Cabrera     }
172af4d94c8SSaúl Cabrera 
17355f9a4bdSJeffrey Charles     /// Check whether the value is a constant.
17455f9a4bdSJeffrey Charles     pub fn is_const(&self) -> bool {
17555f9a4bdSJeffrey Charles         match *self {
17655f9a4bdSJeffrey Charles             Val::I32(_) | Val::I64(_) | Val::F32(_) | Val::F64(_) => true,
17755f9a4bdSJeffrey Charles             _ => false,
17855f9a4bdSJeffrey Charles         }
17955f9a4bdSJeffrey Charles     }
18055f9a4bdSJeffrey Charles 
181321294a5SJeffrey Charles     /// Check whether the value is local with a particular index.
182321294a5SJeffrey Charles     pub fn is_local_at_index(&self, index: u32) -> bool {
183321294a5SJeffrey Charles         match *self {
184321294a5SJeffrey Charles             Self::Local(Local { index: i, .. }) if i == index => true,
185321294a5SJeffrey Charles             _ => false,
186321294a5SJeffrey Charles         }
187321294a5SJeffrey Charles     }
188321294a5SJeffrey Charles 
189835abbcdSSaúl Cabrera     /// Get the register representation of the value.
190835abbcdSSaúl Cabrera     ///
191835abbcdSSaúl Cabrera     /// # Panics
192835abbcdSSaúl Cabrera     /// This method will panic if the value is not a register.
193f0162a40SSaúl Cabrera     pub fn unwrap_reg(&self) -> TypedReg {
194835abbcdSSaúl Cabrera         match self {
19514b39bc2SSaúl Cabrera             Self::Reg(tr) => *tr,
196835abbcdSSaúl Cabrera             v => panic!("expected value {:?} to be a register", v),
197835abbcdSSaúl Cabrera         }
198835abbcdSSaúl Cabrera     }
199835abbcdSSaúl Cabrera 
200835abbcdSSaúl Cabrera     /// Get the integer representation of the value.
201835abbcdSSaúl Cabrera     ///
202835abbcdSSaúl Cabrera     /// # Panics
203835abbcdSSaúl Cabrera     /// This method will panic if the value is not an i32.
204f0162a40SSaúl Cabrera     pub fn unwrap_i32(&self) -> i32 {
205835abbcdSSaúl Cabrera         match self {
206835abbcdSSaúl Cabrera             Self::I32(v) => *v,
207835abbcdSSaúl Cabrera             v => panic!("expected value {:?} to be i32", v),
208835abbcdSSaúl Cabrera         }
209835abbcdSSaúl Cabrera     }
210835abbcdSSaúl Cabrera 
2117c5c7e4bSSaúl Cabrera     /// Get the integer representation of the value.
2127c5c7e4bSSaúl Cabrera     ///
2137c5c7e4bSSaúl Cabrera     /// # Panics
2147c5c7e4bSSaúl Cabrera     /// This method will panic if the value is not an i64.
215f0162a40SSaúl Cabrera     pub fn unwrap_i64(&self) -> i64 {
2167c5c7e4bSSaúl Cabrera         match self {
2177c5c7e4bSSaúl Cabrera             Self::I64(v) => *v,
2187c5c7e4bSSaúl Cabrera             v => panic!("expected value {:?} to be i64", v),
2197c5c7e4bSSaúl Cabrera         }
2207c5c7e4bSSaúl Cabrera     }
2217c5c7e4bSSaúl Cabrera 
222f0162a40SSaúl Cabrera     /// Returns the underlying memory value if it is one, panics otherwise.
223f0162a40SSaúl Cabrera     pub fn unwrap_mem(&self) -> Memory {
224f0162a40SSaúl Cabrera         match self {
225f0162a40SSaúl Cabrera             Self::Memory(m) => *m,
226f0162a40SSaúl Cabrera             v => panic!("expected value {:?} to be a Memory", v),
227f0162a40SSaúl Cabrera         }
228f0162a40SSaúl Cabrera     }
229f0162a40SSaúl Cabrera 
230835abbcdSSaúl Cabrera     /// Check whether the value is an i32 constant.
231835abbcdSSaúl Cabrera     pub fn is_i32_const(&self) -> bool {
232835abbcdSSaúl Cabrera         match *self {
233835abbcdSSaúl Cabrera             Self::I32(_) => true,
234835abbcdSSaúl Cabrera             _ => false,
235835abbcdSSaúl Cabrera         }
236835abbcdSSaúl Cabrera     }
2377c5c7e4bSSaúl Cabrera 
2387c5c7e4bSSaúl Cabrera     /// Check whether the value is an i64 constant.
2397c5c7e4bSSaúl Cabrera     pub fn is_i64_const(&self) -> bool {
2407c5c7e4bSSaúl Cabrera         match *self {
2417c5c7e4bSSaúl Cabrera             Self::I64(_) => true,
2427c5c7e4bSSaúl Cabrera             _ => false,
2437c5c7e4bSSaúl Cabrera         }
2447c5c7e4bSSaúl Cabrera     }
24514b39bc2SSaúl Cabrera 
24614b39bc2SSaúl Cabrera     /// Get the type of the value.
247*496237c2SNick Fitzgerald     pub fn ty(&self) -> WasmValType {
24814b39bc2SSaúl Cabrera         match self {
249*496237c2SNick Fitzgerald             Val::I32(_) => WasmValType::I32,
250*496237c2SNick Fitzgerald             Val::I64(_) => WasmValType::I64,
251*496237c2SNick Fitzgerald             Val::F32(_) => WasmValType::F32,
252*496237c2SNick Fitzgerald             Val::F64(_) => WasmValType::F64,
25314b39bc2SSaúl Cabrera             Val::Reg(r) => r.ty,
25414b39bc2SSaúl Cabrera             Val::Memory(m) => m.ty,
25514b39bc2SSaúl Cabrera             Val::Local(l) => l.ty,
25614b39bc2SSaúl Cabrera         }
25714b39bc2SSaúl Cabrera     }
258835abbcdSSaúl Cabrera }
259835abbcdSSaúl Cabrera 
260835abbcdSSaúl Cabrera /// The shadow stack used for compilation.
261835abbcdSSaúl Cabrera #[derive(Default, Debug)]
262835abbcdSSaúl Cabrera pub(crate) struct Stack {
2637690c500STrevor Elliott     inner: Vec<Val>,
264835abbcdSSaúl Cabrera }
265835abbcdSSaúl Cabrera 
266835abbcdSSaúl Cabrera impl Stack {
267835abbcdSSaúl Cabrera     /// Allocate a new stack.
268835abbcdSSaúl Cabrera     pub fn new() -> Self {
269835abbcdSSaúl Cabrera         Self {
270835abbcdSSaúl Cabrera             inner: Default::default(),
271835abbcdSSaúl Cabrera         }
272835abbcdSSaúl Cabrera     }
273835abbcdSSaúl Cabrera 
274321294a5SJeffrey Charles     /// Returns true if the stack contains a local with the provided index
275321294a5SJeffrey Charles     /// except if the only time the local appears is the top element.
276321294a5SJeffrey Charles     pub fn contains_latent_local(&self, index: u32) -> bool {
277321294a5SJeffrey Charles         self.inner
278321294a5SJeffrey Charles             .iter()
279321294a5SJeffrey Charles             // Iterate top-to-bottom so we can skip the top element and stop
280321294a5SJeffrey Charles             // when we see a memory element.
281321294a5SJeffrey Charles             .rev()
282321294a5SJeffrey Charles             // The local is not latent if it's the top element because the top
283321294a5SJeffrey Charles             // element will be popped next which materializes the local.
284321294a5SJeffrey Charles             .skip(1)
285321294a5SJeffrey Charles             // Stop when we see a memory element because that marks where we
286321294a5SJeffrey Charles             // spilled up to so there will not be any locals past this point.
287321294a5SJeffrey Charles             .take_while(|v| !v.is_mem())
288321294a5SJeffrey Charles             .any(|v| v.is_local_at_index(index))
289321294a5SJeffrey Charles     }
290321294a5SJeffrey Charles 
291a109d2abSSaúl Cabrera     /// Extend the stack with the given elements.
292a109d2abSSaúl Cabrera     pub fn extend(&mut self, values: impl IntoIterator<Item = Val>) {
293a109d2abSSaúl Cabrera         self.inner.extend(values);
294a109d2abSSaúl Cabrera     }
295a109d2abSSaúl Cabrera 
296a109d2abSSaúl Cabrera     /// Inserts many values at the given index.
297a109d2abSSaúl Cabrera     pub fn insert_many(&mut self, at: usize, values: impl IntoIterator<Item = Val>) {
298a109d2abSSaúl Cabrera         debug_assert!(at <= self.len());
299a109d2abSSaúl Cabrera         // If last, simply extend.
300a109d2abSSaúl Cabrera         if at == self.inner.len() {
301a109d2abSSaúl Cabrera             self.inner.extend(values);
302a109d2abSSaúl Cabrera         } else {
303a109d2abSSaúl Cabrera             let mut tail = self.inner.split_off(at);
304a109d2abSSaúl Cabrera             self.inner.extend(values);
305a109d2abSSaúl Cabrera             self.inner.append(&mut tail);
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.
3787690c500STrevor Elliott     pub fn inner_mut(&mut self) -> &mut Vec<Val> {
379835abbcdSSaúl Cabrera         &mut self.inner
380835abbcdSSaúl Cabrera     }
38150733725SSaúl Cabrera 
382446a7f5eSSaúl Cabrera     /// Get a reference to the inner stack representation.
383446a7f5eSSaúl Cabrera     pub fn inner(&self) -> &Vec<Val> {
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;
404*496237c2SNick 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 
412*496237c2SNick 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);
420*496237c2SNick 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);
432*496237c2SNick Fitzgerald         stack.push(Val::reg(reg, WasmValType::I32));
433*496237c2SNick 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