Lines
96.08 %
Functions
83.33 %
Branches
100 %
//! What a discarded `recompile` pass reports to the next one.
//!
//! `ContextSnapshot::restore` resets the closure registry and the local
//! pool, so a `ClosureSigId` or a local index minted in a discarded pass
//! dangles afterwards. An observation therefore carries only structural
//! types: a closure is its chain of parameter lists ending in a non-closure
//! result, re-interned by the next pass, and a pair is its `PairElement`.
use crate::ast::{EntityKind, PairElement, StructId, WasmType};
use crate::compiler::context::CompileContext;
use crate::error::{Error, Result};
/// A non-closure type: nothing that names a signature or a local.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Leaf {
I32,
Bool,
Ratio,
StringRef,
SymbolRef,
Commodity,
PairRef(PairElement),
AnyRef,
EntityRef(EntityKind),
StructRef(StructId),
}
impl From<Leaf> for WasmType {
fn from(leaf: Leaf) -> Self {
match leaf {
Leaf::I32 => Self::I32,
Leaf::Bool => Self::Bool,
Leaf::Ratio => Self::Ratio,
Leaf::StringRef => Self::StringRef,
Leaf::SymbolRef => Self::SymbolRef,
Leaf::Commodity => Self::Commodity,
Leaf::PairRef(element) => Self::PairRef(element),
Leaf::AnyRef => Self::AnyRef,
Leaf::EntityRef(kind) => Self::EntityRef(kind),
Leaf::StructRef(id) => Self::StructRef(id),
impl TryFrom<WasmType> for Leaf {
type Error = Error;
fn try_from(ty: WasmType) -> Result<Self> {
match ty {
WasmType::I32 => Ok(Self::I32),
WasmType::Bool => Ok(Self::Bool),
WasmType::Ratio => Ok(Self::Ratio),
WasmType::StringRef => Ok(Self::StringRef),
WasmType::SymbolRef => Ok(Self::SymbolRef),
WasmType::Commodity => Ok(Self::Commodity),
WasmType::PairRef(element) => Ok(Self::PairRef(element)),
WasmType::AnyRef => Ok(Self::AnyRef),
WasmType::EntityRef(kind) => Ok(Self::EntityRef(kind)),
WasmType::StructRef(id) => Ok(Self::StructRef(id)),
WasmType::Closure(_) => Err(Error::Compile(
"a closure-typed closure parameter cannot be observed".to_string(),
)),
/// `arrows` empty: a plain `result`. Otherwise a closure taking `arrows[0]`
/// and returning the chain `arrows[1..] → result`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct StructuralType {
pub(crate) arrows: Vec<Vec<Leaf>>,
pub(crate) result: Leaf,
impl StructuralType {
/// Spells `ty` out against the signatures `ctx` holds now.
pub(crate) fn of(mut ty: WasmType, ctx: &CompileContext) -> Result<Self> {
let mut arrows = Vec::new();
while let WasmType::Closure(sig) = ty {
let sig = ctx.closure_sig(sig)?;
arrows.push(
sig.params
.iter()
.map(|param| Leaf::try_from(*param))
.collect::<Result<_>>()?,
);
ty = sig.result;
Ok(Self {
arrows,
result: Leaf::try_from(ty)?,
})
/// The [`WasmType`] this names in `ctx`, interning closure signatures.
pub(crate) fn intern(&self, ctx: &mut CompileContext) -> Result<WasmType> {
self.arrows
.rev()
.try_fold(self.result.into(), |result, params| {
let params: Vec<WasmType> = params.iter().copied().map(Into::into).collect();
Ok(WasmType::Closure(
ctx.intern_closure_signature(¶ms, result)?,
))
/// A finished BLOCK's join, keyed by what both passes can see of the block.
pub(crate) struct ObservedBlock {
pub(crate) name: String,
pub(crate) body_len: usize,
pub(crate) join: StructuralType,
/// What a discarded pass observed.
pub(crate) struct Observations {
/// The type the probed form left on the stack.
pub(crate) result: StructuralType,
/// The joins of the BLOCKs nested in the probed form, in the order they
/// started.
pub(crate) blocks: Vec<ObservedBlock>,
#[cfg(test)]
#[path = "observations_tests.rs"]
mod tests;