1
//! The eval surface's open BLOCK frames (0014 4a-i): each collects the arms
2
//! of the `return-from`s evaluated against it, so `eval_block` joins them
3
//! with the tail exactly as the compiled block does.
4

            
5
use std::sync::{Arc, Mutex, PoisonError};
6

            
7
use crate::ast::Expr;
8
use crate::compiler::join::Arm;
9

            
10
use super::table::SymbolTable;
11

            
12
/// One open BLOCK: its label and the exit arms evaluated against it, shared
13
/// by every clone of the table made while it is open.
14
#[derive(Debug, Clone)]
15
pub(super) struct EvalBlock {
16
    name: String,
17
    exits: Arc<Mutex<Vec<Arm>>>,
18
}
19

            
20
impl SymbolTable {
21
    /// Runs `f` with a BLOCK named `name` open; returns its result and the
22
    /// exit arms recorded against the block.
23
42856
    pub(crate) fn with_eval_block<T>(
24
42856
        &mut self,
25
42856
        name: &str,
26
42856
        f: impl FnOnce(&mut Self) -> T,
27
42856
    ) -> (T, Vec<Arm>) {
28
42856
        let exits = Arc::new(Mutex::new(Vec::new()));
29
42856
        self.eval_blocks.push(EvalBlock {
30
42856
            name: name.to_string(),
31
42856
            exits: Arc::clone(&exits),
32
42856
        });
33
42856
        let result = f(self);
34
42856
        self.eval_blocks.pop();
35
42856
        let arms = std::mem::take(&mut *exits.lock().unwrap_or_else(PoisonError::into_inner));
36
42856
        (result, arms)
37
42856
    }
38

            
39
    /// Records `arm` against the innermost open BLOCK named `name`, unless
40
    /// the walk is in dead code.
41
173665
    pub(crate) fn record_eval_exit(&self, name: &str, arm: Arm) {
42
173665
        if self.dead_code {
43
10
            return;
44
173655
        }
45
173655
        if let Some(block) = self.eval_blocks.iter().rev().find(|b| b.name == name) {
46
19609
            block
47
19609
                .exits
48
19609
                .lock()
49
19609
                .unwrap_or_else(PoisonError::into_inner)
50
19609
                .push(arm);
51
154046
        }
52
173665
    }
53

            
54
    /// Runs `f` with the NIL block of the loop `looped` open.
55
8127
    pub(crate) fn with_loop_block<T>(&mut self, looped: Expr, f: impl FnOnce(&mut Self) -> T) -> T {
56
8127
        self.loop_blocks.push(looped);
57
8127
        let result = f(self);
58
8127
        self.loop_blocks.pop();
59
8127
        result
60
8127
    }
61

            
62
    /// Whether the loop `name args…` is the innermost one whose NIL block is
63
    /// open.
64
    #[must_use]
65
476836
    pub(crate) fn is_loop_block_open(&self, name: &str, args: &[Expr]) -> bool {
66
476836
        matches!(
67
476836
            self.loop_blocks.last(),
68
17320
            Some(Expr::List(items)) if matches!(items.split_first(),
69
17320
                Some((Expr::Symbol(head), rest)) if head == name && rest == args)
70
        )
71
476836
    }
72

            
73
    /// The work units a loop unrolled here may spend: the enclosing unroll's
74
    /// share of one iteration, or what the budget has left.
75
    #[must_use]
76
118614
    pub(crate) fn unroll_allowance(&self) -> u64 {
77
118614
        let remaining = self.measurements().remaining();
78
118614
        self.unroll_share
79
118614
            .map_or(remaining, |share| share.min(remaining))
80
118614
    }
81

            
82
    /// Runs `f` with `share` units as the allowance of the loops nested in it.
83
10744672
    pub(crate) fn with_unroll_allowance<T>(
84
10744672
        &mut self,
85
10744672
        share: u64,
86
10744672
        f: impl FnOnce(&mut Self) -> T,
87
10744672
    ) -> T {
88
10744672
        let outer = self.unroll_share.replace(share);
89
10744672
        let result = f(self);
90
10744672
        self.unroll_share = outer;
91
10744672
        result
92
10744672
    }
93

            
94
    /// Forgets the open loop blocks: a function body is its own scope.
95
5030954
    pub(super) fn close_loop_blocks(&mut self) {
96
5030954
        self.loop_blocks.clear();
97
5030954
    }
98

            
99
    /// Whether any BLOCK is open on the eval surface.
100
    #[must_use]
101
612974
    pub(crate) fn has_eval_block(&self) -> bool {
102
612974
        !self.eval_blocks.is_empty()
103
612974
    }
104

            
105
    /// Whether the walk is past a form that always diverges, where codegen
106
    /// records no exit.
107
    #[must_use]
108
4583200
    pub(crate) fn is_dead_code(&self) -> bool {
109
4583200
        self.dead_code
110
4583200
    }
111

            
112
4590959
    pub(crate) fn set_dead_code(&mut self, dead: bool) {
113
4590959
        self.dead_code = dead;
114
4590959
    }
115
}
116

            
117
#[cfg(test)]
118
#[path = "eval_blocks_tests.rs"]
119
mod tests;