Lines
100 %
Functions
97.44 %
Branches
//! The eval surface's open BLOCK frames (0014 4a-i): each collects the arms
//! of the `return-from`s evaluated against it, so `eval_block` joins them
//! with the tail exactly as the compiled block does.
use std::sync::{Arc, Mutex, PoisonError};
use crate::ast::Expr;
use crate::compiler::join::Arm;
use super::table::SymbolTable;
/// One open BLOCK: its label and the exit arms evaluated against it, shared
/// by every clone of the table made while it is open.
#[derive(Debug, Clone)]
pub(super) struct EvalBlock {
name: String,
exits: Arc<Mutex<Vec<Arm>>>,
}
impl SymbolTable {
/// Runs `f` with a BLOCK named `name` open; returns its result and the
/// exit arms recorded against the block.
pub(crate) fn with_eval_block<T>(
&mut self,
name: &str,
f: impl FnOnce(&mut Self) -> T,
) -> (T, Vec<Arm>) {
let exits = Arc::new(Mutex::new(Vec::new()));
self.eval_blocks.push(EvalBlock {
name: name.to_string(),
exits: Arc::clone(&exits),
});
let result = f(self);
self.eval_blocks.pop();
let arms = std::mem::take(&mut *exits.lock().unwrap_or_else(PoisonError::into_inner));
(result, arms)
/// Records `arm` against the innermost open BLOCK named `name`, unless
/// the walk is in dead code.
pub(crate) fn record_eval_exit(&self, name: &str, arm: Arm) {
if self.dead_code {
return;
if let Some(block) = self.eval_blocks.iter().rev().find(|b| b.name == name) {
block
.exits
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push(arm);
/// Runs `f` with the NIL block of the loop `looped` open.
pub(crate) fn with_loop_block<T>(&mut self, looped: Expr, f: impl FnOnce(&mut Self) -> T) -> T {
self.loop_blocks.push(looped);
self.loop_blocks.pop();
result
/// Whether the loop `name argsā¦` is the innermost one whose NIL block is
/// open.
#[must_use]
pub(crate) fn is_loop_block_open(&self, name: &str, args: &[Expr]) -> bool {
matches!(
self.loop_blocks.last(),
Some(Expr::List(items)) if matches!(items.split_first(),
Some((Expr::Symbol(head), rest)) if head == name && rest == args)
)
/// The work units a loop unrolled here may spend: the enclosing unroll's
/// share of one iteration, or what the budget has left.
pub(crate) fn unroll_allowance(&self) -> u64 {
let remaining = self.measurements().remaining();
self.unroll_share
.map_or(remaining, |share| share.min(remaining))
/// Runs `f` with `share` units as the allowance of the loops nested in it.
pub(crate) fn with_unroll_allowance<T>(
share: u64,
) -> T {
let outer = self.unroll_share.replace(share);
self.unroll_share = outer;
/// Forgets the open loop blocks: a function body is its own scope.
pub(super) fn close_loop_blocks(&mut self) {
self.loop_blocks.clear();
/// Whether any BLOCK is open on the eval surface.
pub(crate) fn has_eval_block(&self) -> bool {
!self.eval_blocks.is_empty()
/// Whether the walk is past a form that always diverges, where codegen
/// records no exit.
pub(crate) fn is_dead_code(&self) -> bool {
self.dead_code
pub(crate) fn set_dead_code(&mut self, dead: bool) {
self.dead_code = dead;
#[cfg(test)]
#[path = "eval_blocks_tests.rs"]
mod tests;