1
//! The BLOCK joins a pass observes (0014 slice 4.3b). A pass discarded to
2
//! learn one block's join hands every nested block its join as well, so each
3
//! block compiles at most twice however deep it is nested.
4
//!
5
//! `seen` holds each finished block's join in the order the blocks started;
6
//! `expected` is a discarded pass's list, consumed from `next` by the pass
7
//! after it. `ContextSnapshot` journals both. `probe` is the innermost BLOCK
8
//! pass that can still be discarded: a nested block whose own pass is
9
//! unsettled defers its second pass to it.
10

            
11
use std::mem;
12

            
13
use super::CompileContext;
14
use crate::compiler::observations::{ObservedBlock, StructuralType};
15

            
16
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17
pub(in crate::compiler) enum Probe {
18
    /// No enclosing BLOCK pass can be discarded.
19
    #[default]
20
    Closed,
21
    Clean,
22
    /// A nested block left its pass unsettled for this one to discard.
23
    Dirty,
24
}
25

            
26
impl Probe {
27
60734
    pub(in crate::compiler) fn is_open(self) -> bool {
28
60734
        self != Self::Closed
29
60734
    }
30
}
31

            
32
#[derive(Debug, Default)]
33
pub(super) struct BlockJoins {
34
    seen: Vec<ObservedBlock>,
35
    expected: Vec<ObservedBlock>,
36
    next: usize,
37
    probe: Probe,
38
}
39

            
40
/// The journalled part of [`BlockJoins`].
41
#[derive(Clone, Copy, Debug)]
42
pub(super) struct BlockJoinsMark {
43
    seen: usize,
44
    next: usize,
45
}
46

            
47
impl CompileContext {
48
161154
    pub(super) fn block_joins_mark(&self) -> BlockJoinsMark {
49
161154
        BlockJoinsMark {
50
161154
            seen: self.block_joins.seen.len(),
51
161154
            next: self.block_joins.next,
52
161154
        }
53
161154
    }
54

            
55
102652
    pub(super) fn restore_block_joins(&mut self, mark: BlockJoinsMark) {
56
102652
        self.block_joins.seen.truncate(mark.seen);
57
102652
        self.block_joins.next = mark.next;
58
102652
    }
59

            
60
    /// Where a block starting now records its join, ahead of its nested ones.
61
81405
    pub(in crate::compiler) fn block_join_start(&self) -> usize {
62
81405
        self.block_joins.seen.len()
63
81405
    }
64

            
65
48872
    pub(in crate::compiler) fn record_block_join(&mut self, start: usize, block: ObservedBlock) {
66
48872
        self.block_joins.seen.insert(start, block);
67
48872
    }
68

            
69
    /// The joins recorded since `start`.
70
10180
    pub(in crate::compiler) fn take_block_joins(&mut self, start: usize) -> Vec<ObservedBlock> {
71
10180
        self.block_joins.seen.split_off(start)
72
10180
    }
73

            
74
    /// The next expected join, when its key is this block's.
75
60737
    pub(in crate::compiler) fn expected_block_join(
76
60737
        &mut self,
77
60737
        name: &str,
78
60737
        body_len: usize,
79
60737
    ) -> Option<StructuralType> {
80
60737
        let joins = &mut self.block_joins;
81
60737
        let block = joins
82
60737
            .expected
83
60737
            .get(joins.next)
84
58089
            .filter(|block| block.name == name && block.body_len == body_len)?;
85
2648
        let join = block.join.clone();
86
2648
        joins.next += 1;
87
2648
        Some(join)
88
60737
    }
89

            
90
    /// Runs `pass` expecting `blocks`, then restores the outer expectation.
91
27257
    pub(in crate::compiler) fn expecting_block_joins<T>(
92
27257
        &mut self,
93
27257
        blocks: Vec<ObservedBlock>,
94
27257
        pass: impl FnOnce(&mut Self) -> T,
95
27257
    ) -> T {
96
27257
        let outer = mem::replace(&mut self.block_joins.expected, blocks);
97
27257
        let outer_next = mem::replace(&mut self.block_joins.next, 0);
98
27257
        let result = pass(self);
99
27257
        self.block_joins.expected = outer;
100
27257
        self.block_joins.next = outer_next;
101
27257
        result
102
27257
    }
103

            
104
    /// Opens a discardable BLOCK pass, returning the enclosing one.
105
60734
    pub(in crate::compiler) fn open_block_probe(&mut self) -> Probe {
106
60734
        mem::replace(&mut self.block_joins.probe, Probe::Clean)
107
60734
    }
108

            
109
    /// Runs `pass` with no enclosing BLOCK pass to defer to: a function body
110
    /// is compiled apart from the blocks around its call.
111
20659
    pub(in crate::compiler) fn closing_block_probe<T>(
112
20659
        &mut self,
113
20659
        pass: impl FnOnce(&mut Self) -> T,
114
20659
    ) -> T {
115
20659
        let outer = mem::replace(&mut self.block_joins.probe, Probe::Closed);
116
20659
        let result = pass(self);
117
20659
        self.block_joins.probe = outer;
118
20659
        result
119
20659
    }
120

            
121
    /// Whether a nested block deferred to the open pass.
122
55448
    pub(in crate::compiler) fn block_probe_dirty(&self) -> bool {
123
55448
        self.block_joins.probe == Probe::Dirty
124
55448
    }
125

            
126
    /// Reinstates `outer`, dirtied when the closed pass deferred to it.
127
60733
    pub(in crate::compiler) fn close_block_probe(&mut self, outer: Probe, deferred: bool) {
128
60733
        self.block_joins.probe = if deferred { Probe::Dirty } else { outer };
129
60733
    }
130
}