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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! This module contains the [`ValidationStack`] data structure
//!
//! The [`ValidationStack`] is a unified stack, in the sense that it unifies both
//! [`ValidationStackEntry::Val`] and [`ValidationStackEntry::Label`]. It therefore mixes type
//! information with structured control flow information.
#![allow(unused)] // TODO remove this once sidetable implementation lands
use super::Result;
use alloc::vec::Vec;

use crate::{Error, ValType};

#[derive(Debug, PartialEq, Eq)]
pub(super) struct ValidationStack {
    stack: Vec<ValidationStackEntry>,
}

impl ValidationStack {
    /// Initialize a new ValidationStack
    pub(super) fn new() -> Self {
        Self { stack: Vec::new() }
    }

    pub(super) fn len(&self) -> usize {
        self.stack.len()
    }

    pub(super) fn push_valtype(&mut self, valtype: ValType) {
        self.stack.push(ValidationStackEntry::Val(valtype));
    }

    pub(super) fn push_label(&mut self, label_info: LabelInfo) {
        self.stack.push(ValidationStackEntry::Label(label_info));
    }

    /// This puts an unspecified element on top of the stack.
    /// While the top of the stack is unspecified, arbitrary value types can be popped.
    /// To undo this, a new label has to be pushed or an existing one has to be popped.
    ///
    /// See the documentation for [`ValidationStackEntry::UnspecifiedValTypes`] for more info.
    pub(super) fn make_unspecified(&mut self) {
        // Pop everything until next label or until the stack is empty.
        // This is okay, because these values cannot be accessed during execution ever again.
        while let Some(entry) = self.stack.last() {
            match entry {
                ValidationStackEntry::Val(_) | ValidationStackEntry::UnspecifiedValTypes => {
                    self.stack.pop();
                }
                ValidationStackEntry::Label(_) => break,
            }
        }

        self.stack.push(ValidationStackEntry::UnspecifiedValTypes)
    }

    /// Pop a [`ValidationStackEntry`] from the [`ValidationStack`]
    ///
    /// # Returns
    ///
    /// - Returns `Ok(_)` with the former top-most [`ValidationStackEntry`] inside, if the stack had
    ///   at least one element.
    /// - Returns `Err(_)` if the stack was already empty.
    fn pop(&mut self) -> Result<ValidationStackEntry> {
        self.stack
            .pop()
            .ok_or(Error::InvalidValidationStackValType(None))
    }

    /// Assert the top-most [`ValidationStackEntry`] is a specific [`ValType`], after popping it from the [`ValidationStack`]
    ///
    /// # Returns
    ///
    /// - Returns `Ok(())` if the top-most [`ValidationStackEntry`] is a [`ValType`] identical to
    ///   `expected_ty`.
    /// - Returns `Err(_)` otherwise.
    pub(super) fn assert_pop_val_type(&mut self, expected_ty: ValType) -> Result<()> {
        if let Some(ValidationStackEntry::UnspecifiedValTypes) = self.stack.last() {
            // An unspecified value is always correct, and will never disappear by popping.
            return Ok(());
        }

        match self.pop()? {
            ValidationStackEntry::Val(ty) => (ty == expected_ty)
                .then_some(())
                .ok_or(Error::InvalidValidationStackValType(Some(ty))),
            ValidationStackEntry::Label(li) => Err(Error::FoundLabel(li.kind)),
            ValidationStackEntry::UnspecifiedValTypes => {
                unreachable!("we just checked if the topmost entry is of this type")
            }
        }
    }

    /// Asserts that the values on top of the stack match those of a value iterator
    ///
    /// The last element of `expected_val_types` is compared to the top-most
    /// [`ValidationStackEntry`], the second last `expected_val_types` element to the second top-most
    /// [`ValidationStackEntry`] etc.
    ///
    /// Any occurence of the [`ValidationStackEntry::Label`] variant in the stack tail will cause an
    /// error. This method does not mutate the [`ValidationStack::stack`] in any way.
    ///
    /// # Returns
    ///
    /// - `Ok(_)`, the tail of the stack matches the `expected_val_types`
    /// - `Err(_)` otherwise
    pub(super) fn assert_val_types_on_top(&self, expected_val_types: &[ValType]) -> Result<()> {
        let stack_tail = self
            .stack
            .get(self.stack.len() - expected_val_types.len()..)
            .ok_or(Error::InvalidValType)?;

        // Now we check the valtypes in reverse.
        // That way we can stop checking if we encounter an `UnspecifiedValTypes`.

        let mut current_expected_valtype = expected_val_types.iter().rev();
        for entry in stack_tail.iter().rev() {
            match entry {
                ValidationStackEntry::Label(label) => return Err(Error::EndInvalidValueStack),
                ValidationStackEntry::Val(valtype) => {
                    if Some(valtype) != current_expected_valtype.next() {
                        return Err(Error::EndInvalidValueStack);
                    }
                }
                ValidationStackEntry::UnspecifiedValTypes => {
                    // In case we find an `UnspecifiedValTypes`, we pretend that all expected valtypes are found.
                    // That's because this entry can expand to every possible combination of valtypes.
                    return Ok(());
                }
            }
        }

        Ok(())
    }

    /// Asserts that the valtypes on the stack match the expected valtypes.
    ///
    /// This starts by comparing the top-most valtype with the last element from `expected_val_types` and then continues downwards on the stack.
    /// If a label is reached and not all `expected_val_types` have been checked, the assertion fails.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if all expected valtypes were found
    /// - `Err(_)` otherwise
    pub(super) fn assert_val_types(&self, expected_val_types: &[ValType]) -> Result<()> {
        let topmost_label_index = self.find_topmost_label_idx();

        let first_valtype = topmost_label_index.map(|idx| idx + 1).unwrap_or(0);

        // Now we check the valtypes in reverse.
        // That way we can stop checking if we encounter an `UnspecifiedValTypes`.

        let mut current_expected_valtype = expected_val_types.iter().rev();
        for entry in self.stack[first_valtype..].iter().rev() {
            match entry {
                ValidationStackEntry::Label(_) => unreachable!(
                    "we started at the top-most label so we cannot find any more labels"
                ),
                ValidationStackEntry::Val(valtype) => {
                    if Some(valtype) != current_expected_valtype.next() {
                        return Err(Error::EndInvalidValueStack);
                    }
                }
                ValidationStackEntry::UnspecifiedValTypes => {
                    return Ok(());
                }
            }
        }

        Ok(())
    }

    /// A helper to find the index of the top-most label in [`ValidationStack::stack`]
    fn find_topmost_label_idx(&self) -> Option<usize> {
        self.stack
            .iter()
            .enumerate()
            .rev()
            .find(|(_idx, entry)| matches!(entry, ValidationStackEntry::Label(_)))
            .map(|(idx, _entry)| idx)
    }

    /// Searches for the top-most label, then pops the label and all entry on top of that label.
    /// Only the label's [`LabelInfo`] is returned.
    ///
    /// # Returns
    ///
    /// - `Ok(LabelInfo)` if a label has been found and popped
    /// - `None` if no label was found on the stack
    fn pop_label_and_above(&mut self) -> Option<LabelInfo> {
        /// Delete all the values until the topmost label or until the stack is empty
        match self.find_topmost_label_idx() {
            Some(idx) => {
                if self.stack.len() > idx + 1 {
                    self.stack.drain((idx + 1)..);
                }
            }
            None => self.stack.clear(),
        }

        // Pop the label itself
        match self.pop() {
            Ok(ValidationStackEntry::Label(info)) => Some(info),
            Ok(_) => unreachable!(
                "we just removed everything until the next label, thus new topmost entry must be a label"
            ),
            Err(_) => None,
        }
    }

    /// Return true if the stack has at least one remaining label
    pub(super) fn has_remaining_label(&self) -> bool {
        self.stack
            .iter()
            .any(|e| matches!(e, ValidationStackEntry::Label(_)))
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum ValidationStackEntry {
    /// A value
    Val(ValType),

    /// A label
    Label(LabelInfo),

    /// Special variant to encode that any possible number of [`ValType`]s could be here
    ///
    /// Caused by `return` and `unreachable`, as both can push an arbitrary number of values to the stack.
    ///
    /// When this variant is pushed onto the stack, all valtypes until the next lower label are deleted.
    /// They are not needed anymore because this variant can expand to all of them.
    UnspecifiedValTypes,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct LabelInfo {
    pub(crate) kind: LabelKind,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LabelKind {
    Block,
    Loop,
    If,
}

#[cfg(test)]
mod tests {
    use crate::{NumType, RefType, ValType};

    use super::{LabelInfo, LabelKind, ValidationStack};

    #[test]
    fn push_then_pop() {
        let mut stack = ValidationStack::new();

        stack.push_valtype(ValType::NumType(NumType::F64));
        stack.push_valtype(ValType::NumType(NumType::I32));
        stack.push_valtype(ValType::VecType);
        stack.push_valtype(ValType::RefType(RefType::ExternRef));

        stack
            .assert_pop_val_type(ValType::RefType(RefType::ExternRef))
            .unwrap();
        stack.assert_pop_val_type(ValType::VecType).unwrap();
        stack
            .assert_pop_val_type(ValType::NumType(NumType::I32))
            .unwrap();
        stack
            .assert_pop_val_type(ValType::NumType(NumType::F64))
            .unwrap();
    }

    #[test]
    fn labels() {
        let mut stack = ValidationStack::new();

        stack.push_valtype(ValType::NumType(NumType::I64));
        stack.push_label(LabelInfo {
            kind: LabelKind::Block,
        });

        stack.push_label(LabelInfo {
            kind: LabelKind::Loop,
        });

        stack.push_valtype(ValType::VecType);

        // This removes the `ValType::VecType` and the `LabelKind::Loop` label
        let popped_label = stack.pop_label_and_above().unwrap();
        assert_eq!(
            popped_label,
            LabelInfo {
                kind: LabelKind::Loop,
            }
        );

        let popped_label = stack.pop_label_and_above().unwrap();
        assert_eq!(
            popped_label,
            LabelInfo {
                kind: LabelKind::Block,
            }
        );

        // The first valtype should still be there
        stack.assert_pop_val_type(ValType::NumType(NumType::I64));
    }

    #[test]
    fn assert_valtypes() {
        let mut stack = ValidationStack::new();

        stack.push_valtype(ValType::NumType(NumType::F64));
        stack.push_valtype(ValType::NumType(NumType::I32));
        stack.push_valtype(ValType::NumType(NumType::F32));

        stack
            .assert_val_types(&[
                ValType::NumType(NumType::F64),
                ValType::NumType(NumType::I32),
                ValType::NumType(NumType::F32),
            ])
            .unwrap();

        stack.push_label(LabelInfo {
            kind: LabelKind::Block,
        });
        stack.push_valtype(ValType::NumType(NumType::I32));

        stack
            .assert_val_types(&[ValType::NumType(NumType::I32)])
            .unwrap();
    }

    #[test]
    fn assert_emtpy_valtypes() {
        let mut stack = ValidationStack::new();

        stack.assert_val_types(&[]).unwrap();

        stack.push_valtype(ValType::NumType(NumType::I32));
        stack.push_label(LabelInfo {
            kind: LabelKind::Block,
        });

        // Valtypes separated by a label should also not be detected
        stack.assert_val_types(&[]).unwrap();
    }

    #[test]
    fn assert_valtypes_on_top() {
        let mut stack = ValidationStack::new();

        stack.assert_val_types_on_top(&[]).unwrap();

        stack.push_valtype(ValType::NumType(NumType::I32));
        stack.push_valtype(ValType::NumType(NumType::F32));
        stack.push_valtype(ValType::NumType(NumType::I64));

        // There are always zero valtypes on top of the stack
        stack.assert_val_types_on_top(&[]).unwrap();

        stack
            .assert_val_types_on_top(&[ValType::NumType(NumType::I64)])
            .unwrap();

        stack
            .assert_val_types_on_top(&[
                ValType::NumType(NumType::F32),
                ValType::NumType(NumType::I64),
            ])
            .unwrap();

        stack
            .assert_val_types_on_top(&[
                ValType::NumType(NumType::I32),
                ValType::NumType(NumType::F32),
                ValType::NumType(NumType::I64),
            ])
            .unwrap();
    }

    #[test]
    fn unspecified() {
        let mut stack = ValidationStack::new();
        stack.push_label(LabelInfo {
            kind: LabelKind::Block,
        });

        stack.make_unspecified();

        // Now we can pop as many valtypes from the stack as we want
        stack
            .assert_pop_val_type(ValType::NumType(NumType::I32))
            .unwrap();

        stack
            .assert_pop_val_type(ValType::RefType(RefType::ExternRef))
            .unwrap();

        // Let's remove the unspecified entry and the first label
        let popped_label = stack.pop_label_and_above().unwrap();
        assert_eq!(
            popped_label,
            LabelInfo {
                kind: LabelKind::Block,
            }
        );

        // Now there are no values left on the stack
        assert_eq!(stack.assert_val_types(&[]), Ok(()));
    }
}