Lines
100 %
Functions
Branches
//! The BLOCK joins a pass observes (0014 slice 4.3b). A pass discarded to
//! learn one block's join hands every nested block its join as well, so each
//! block compiles at most twice however deep it is nested.
//!
//! `seen` holds each finished block's join in the order the blocks started;
//! `expected` is a discarded pass's list, consumed from `next` by the pass
//! after it. `ContextSnapshot` journals both. `probe` is the innermost BLOCK
//! pass that can still be discarded: a nested block whose own pass is
//! unsettled defers its second pass to it.
use std::mem;
use super::CompileContext;
use crate::compiler::observations::{ObservedBlock, StructuralType};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(in crate::compiler) enum Probe {
/// No enclosing BLOCK pass can be discarded.
#[default]
Closed,
Clean,
/// A nested block left its pass unsettled for this one to discard.
Dirty,
}
impl Probe {
pub(in crate::compiler) fn is_open(self) -> bool {
self != Self::Closed
#[derive(Debug, Default)]
pub(super) struct BlockJoins {
seen: Vec<ObservedBlock>,
expected: Vec<ObservedBlock>,
next: usize,
probe: Probe,
/// The journalled part of [`BlockJoins`].
#[derive(Clone, Copy, Debug)]
pub(super) struct BlockJoinsMark {
seen: usize,
impl CompileContext {
pub(super) fn block_joins_mark(&self) -> BlockJoinsMark {
BlockJoinsMark {
seen: self.block_joins.seen.len(),
next: self.block_joins.next,
pub(super) fn restore_block_joins(&mut self, mark: BlockJoinsMark) {
self.block_joins.seen.truncate(mark.seen);
self.block_joins.next = mark.next;
/// Where a block starting now records its join, ahead of its nested ones.
pub(in crate::compiler) fn block_join_start(&self) -> usize {
self.block_joins.seen.len()
pub(in crate::compiler) fn record_block_join(&mut self, start: usize, block: ObservedBlock) {
self.block_joins.seen.insert(start, block);
/// The joins recorded since `start`.
pub(in crate::compiler) fn take_block_joins(&mut self, start: usize) -> Vec<ObservedBlock> {
self.block_joins.seen.split_off(start)
/// The next expected join, when its key is this block's.
pub(in crate::compiler) fn expected_block_join(
&mut self,
name: &str,
body_len: usize,
) -> Option<StructuralType> {
let joins = &mut self.block_joins;
let block = joins
.expected
.get(joins.next)
.filter(|block| block.name == name && block.body_len == body_len)?;
let join = block.join.clone();
joins.next += 1;
Some(join)
/// Runs `pass` expecting `blocks`, then restores the outer expectation.
pub(in crate::compiler) fn expecting_block_joins<T>(
blocks: Vec<ObservedBlock>,
pass: impl FnOnce(&mut Self) -> T,
) -> T {
let outer = mem::replace(&mut self.block_joins.expected, blocks);
let outer_next = mem::replace(&mut self.block_joins.next, 0);
let result = pass(self);
self.block_joins.expected = outer;
self.block_joins.next = outer_next;
result
/// Opens a discardable BLOCK pass, returning the enclosing one.
pub(in crate::compiler) fn open_block_probe(&mut self) -> Probe {
mem::replace(&mut self.block_joins.probe, Probe::Clean)
/// Runs `pass` with no enclosing BLOCK pass to defer to: a function body
/// is compiled apart from the blocks around its call.
pub(in crate::compiler) fn closing_block_probe<T>(
let outer = mem::replace(&mut self.block_joins.probe, Probe::Closed);
self.block_joins.probe = outer;
/// Whether a nested block deferred to the open pass.
pub(in crate::compiler) fn block_probe_dirty(&self) -> bool {
self.block_joins.probe == Probe::Dirty
/// Reinstates `outer`, dirtied when the closed pass deferred to it.
pub(in crate::compiler) fn close_block_probe(&mut self, outer: Probe, deferred: bool) {
self.block_joins.probe = if deferred { Probe::Dirty } else { outer };