1
//! The homes of struct fields: every instance of a layout shares one home per
2
//! field, typed like a global's home by the join of every store into it.
3

            
4
use std::collections::btree_map::Entry;
5
use std::fmt;
6

            
7
use super::{Home, HomeKey, locked};
8
use crate::ast::{StructId, WasmType};
9
use crate::compiler::join::{Arm, reconcile_arms};
10
use crate::runtime::SymbolTable;
11

            
12
impl fmt::Display for HomeKey {
13
5
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14
5
        match self {
15
3
            Self::Global(name) => write!(f, "global {name}"),
16
1
            Self::Field(id, idx) => write!(f, "field {idx} of {id}"),
17
1
            Self::Binder(_, _, var, _) => write!(f, "variable {var}"),
18
        }
19
5
    }
20
}
21

            
22
impl SymbolTable {
23
    /// The type a read of field `idx` of `id` unboxes to: the home's type this
24
    /// attempt, opening it from the previous attempt's hint, else provisionally
25
    /// `AnyRef` for the first store to type.
26
82863
    pub(crate) fn field_type(&self, id: StructId, idx: u16) -> WasmType {
27
82863
        let key = HomeKey::Field(id, idx);
28
82863
        let mut book = locked(&self.homes);
29
82863
        let hint = book.hints.get(&key).copied();
30
82863
        book.homes
31
82863
            .entry(key)
32
7735
            .or_insert_with(|| match hint {
33
3343
                Some(ty) => Home::typed(vec![Arm::Value(ty)], None, ty),
34
4392
                None => Home::provisional(),
35
7735
            })
36
            .ty
37
82863
    }
38

            
39
    /// Records a store of `arm` into field `idx` of `id`. The first store a
40
    /// home sees types it, over the previous attempt's hint. A closure rides the
41
    /// slot as an `AnyRef`, which no read casts.
42
2065892
    pub(crate) fn note_field_store(&self, id: StructId, idx: u16, arm: Arm) {
43
2065892
        let arm = match arm {
44
1665
            Arm::Value(WasmType::Closure(_)) => Arm::Value(WasmType::AnyRef),
45
2064227
            other => other,
46
        };
47
2065892
        let key = HomeKey::Field(id, idx);
48
2065892
        let mut book = locked(&self.homes);
49
2065892
        let hint = book.hints.get(&key).copied();
50
2065892
        match book.homes.entry(key) {
51
1917881
            Entry::Occupied(home) => home.into_mut().arms.push(arm),
52
            Entry::Vacant(slot) => {
53
148011
                let base: Vec<Arm> = hint.map(Arm::Value).into_iter().chain([arm]).collect();
54
148011
                let ty = reconcile_arms(&base);
55
148011
                slot.insert(Home::typed(base, None, ty));
56
            }
57
        }
58
2065892
    }
59

            
60
    /// How many field and binder homes this attempt opened, each of which may
61
    /// widen.
62
    #[must_use]
63
90050
    pub(crate) fn site_home_count(&self) -> usize {
64
90050
        locked(&self.homes)
65
90050
            .homes
66
90050
            .keys()
67
70028
            .filter(|key| !matches!(key, HomeKey::Global(_)))
68
            .count()
69
90050
    }
70

            
71
    /// A home of this attempt narrower than the join of its stores.
72
3
    pub(crate) fn unsettled_home(&self) -> Option<HomeKey> {
73
3
        locked(&self.homes)
74
3
            .homes
75
3
            .iter()
76
4
            .find(|(_, home)| !home.arms.is_empty() && !home.holds_every_store())
77
3
            .map(|(key, _)| key.clone())
78
3
    }
79
}