1
//! What a discarded `recompile` pass reports to the next one.
2
//!
3
//! `ContextSnapshot::restore` resets the closure registry and the local
4
//! pool, so a `ClosureSigId` or a local index minted in a discarded pass
5
//! dangles afterwards. An observation therefore carries only structural
6
//! types: a closure is its chain of parameter lists ending in a non-closure
7
//! result, re-interned by the next pass, and a pair is its `PairElement`.
8

            
9
use crate::ast::{EntityKind, PairElement, StructId, WasmType};
10
use crate::compiler::context::CompileContext;
11
use crate::error::{Error, Result};
12

            
13
/// A non-closure type: nothing that names a signature or a local.
14
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15
pub(crate) enum Leaf {
16
    I32,
17
    Bool,
18
    Ratio,
19
    StringRef,
20
    SymbolRef,
21
    Commodity,
22
    PairRef(PairElement),
23
    AnyRef,
24
    EntityRef(EntityKind),
25
    StructRef(StructId),
26
}
27

            
28
impl From<Leaf> for WasmType {
29
29933
    fn from(leaf: Leaf) -> Self {
30
29933
        match leaf {
31
14617
            Leaf::I32 => Self::I32,
32
210
            Leaf::Bool => Self::Bool,
33
2172
            Leaf::Ratio => Self::Ratio,
34
9
            Leaf::StringRef => Self::StringRef,
35
            Leaf::SymbolRef => Self::SymbolRef,
36
1
            Leaf::Commodity => Self::Commodity,
37
847
            Leaf::PairRef(element) => Self::PairRef(element),
38
8540
            Leaf::AnyRef => Self::AnyRef,
39
1
            Leaf::EntityRef(kind) => Self::EntityRef(kind),
40
3536
            Leaf::StructRef(id) => Self::StructRef(id),
41
        }
42
29933
    }
43
}
44

            
45
impl TryFrom<WasmType> for Leaf {
46
    type Error = Error;
47

            
48
76167
    fn try_from(ty: WasmType) -> Result<Self> {
49
76167
        match ty {
50
42524
            WasmType::I32 => Ok(Self::I32),
51
4351
            WasmType::Bool => Ok(Self::Bool),
52
2654
            WasmType::Ratio => Ok(Self::Ratio),
53
3565
            WasmType::StringRef => Ok(Self::StringRef),
54
            WasmType::SymbolRef => Ok(Self::SymbolRef),
55
1
            WasmType::Commodity => Ok(Self::Commodity),
56
3782
            WasmType::PairRef(element) => Ok(Self::PairRef(element)),
57
15752
            WasmType::AnyRef => Ok(Self::AnyRef),
58
1
            WasmType::EntityRef(kind) => Ok(Self::EntityRef(kind)),
59
3536
            WasmType::StructRef(id) => Ok(Self::StructRef(id)),
60
1
            WasmType::Closure(_) => Err(Error::Compile(
61
1
                "a closure-typed closure parameter cannot be observed".to_string(),
62
1
            )),
63
        }
64
76167
    }
65
}
66

            
67
/// `arrows` empty: a plain `result`. Otherwise a closure taking `arrows[0]`
68
/// and returning the chain `arrows[1..] → result`.
69
#[derive(Clone, Debug, PartialEq, Eq)]
70
pub(crate) struct StructuralType {
71
    pub(crate) arrows: Vec<Vec<Leaf>>,
72
    pub(crate) result: Leaf,
73
}
74

            
75
impl StructuralType {
76
    /// Spells `ty` out against the signatures `ctx` holds now.
77
76126
    pub(crate) fn of(mut ty: WasmType, ctx: &CompileContext) -> Result<Self> {
78
76126
        let mut arrows = Vec::new();
79
76375
        while let WasmType::Closure(sig) = ty {
80
250
            let sig = ctx.closure_sig(sig)?;
81
250
            arrows.push(
82
250
                sig.params
83
250
                    .iter()
84
42
                    .map(|param| Leaf::try_from(*param))
85
1
                    .collect::<Result<_>>()?,
86
            );
87
249
            ty = sig.result;
88
        }
89
        Ok(Self {
90
76125
            arrows,
91
76125
            result: Leaf::try_from(ty)?,
92
        })
93
76126
    }
94

            
95
    /// The [`WasmType`] this names in `ctx`, interning closure signatures.
96
29904
    pub(crate) fn intern(&self, ctx: &mut CompileContext) -> Result<WasmType> {
97
29904
        self.arrows
98
29904
            .iter()
99
29904
            .rev()
100
29904
            .try_fold(self.result.into(), |result, params| {
101
17
                let params: Vec<WasmType> = params.iter().copied().map(Into::into).collect();
102
                Ok(WasmType::Closure(
103
17
                    ctx.intern_closure_signature(&params, result)?,
104
                ))
105
17
            })
106
29904
    }
107
}
108

            
109
/// A finished BLOCK's join, keyed by what both passes can see of the block.
110
#[derive(Clone, Debug, PartialEq, Eq)]
111
pub(crate) struct ObservedBlock {
112
    pub(crate) name: String,
113
    pub(crate) body_len: usize,
114
    pub(crate) join: StructuralType,
115
}
116

            
117
/// What a discarded pass observed.
118
#[derive(Clone, Debug, PartialEq, Eq)]
119
pub(crate) struct Observations {
120
    /// The type the probed form left on the stack.
121
    pub(crate) result: StructuralType,
122
    /// The joins of the BLOCKs nested in the probed form, in the order they
123
    /// started.
124
    pub(crate) blocks: Vec<ObservedBlock>,
125
}
126

            
127
#[cfg(test)]
128
#[path = "observations_tests.rs"]
129
mod tests;