Skip to main content

miden_processor/fast/
step.rs

1//! This module defines items relevant to controlling execution stopping conditions.
2
3use alloc::sync::Arc;
4use core::ops::ControlFlow;
5
6use miden_core::{mast::MastForest, program::Kernel};
7use miden_mast_package::debug_info::{DebugSourceNodeId, PackageDebugInfo};
8
9use crate::{
10    ExecutionError, FastProcessor, Stopper,
11    continuation_stack::{Continuation, ContinuationStack},
12};
13
14// RESUME CONTEXT
15// ===============================================================================================
16
17/// The context required to resume execution of a program from the last point at which it was
18/// stopped.
19#[derive(Debug)]
20pub struct ResumeContext {
21    pub(crate) current_forest: Arc<MastForest>,
22    pub(crate) continuation_stack: ContinuationStack<Arc<MastForest>>,
23    pub(crate) kernel: Kernel,
24    pub(crate) package_debug_info: Option<Arc<PackageDebugInfo>>,
25}
26
27impl ResumeContext {
28    /// Returns a reference to the continuation stack.
29    pub fn continuation_stack(&self) -> &ContinuationStack<Arc<MastForest>> {
30        &self.continuation_stack
31    }
32
33    /// Returns a reference to the MAST forest being currently executed.
34    pub fn current_forest(&self) -> &Arc<MastForest> {
35        &self.current_forest
36    }
37
38    /// Returns a reference to the kernel being currently executed.
39    pub fn kernel(&self) -> &Kernel {
40        &self.kernel
41    }
42}
43
44// STOPPERS
45// ===============================================================================================
46
47/// A [`Stopper`] that never stops execution (except for returning an error when the maximum cycle
48/// count is exceeded).
49pub struct NeverStopper;
50
51impl Stopper for NeverStopper {
52    type Processor = FastProcessor;
53    type Forest = Arc<MastForest>;
54
55    #[inline(always)]
56    fn should_stop(
57        &self,
58        processor: &FastProcessor,
59        continuation_stack: &ContinuationStack<Arc<MastForest>>,
60        _continuation_after_stop: impl FnOnce() -> Option<(
61            Continuation<Arc<MastForest>>,
62            Option<DebugSourceNodeId>,
63        )>,
64    ) -> ControlFlow<BreakReason<Arc<MastForest>>> {
65        check_if_max_cycles_exceeded(processor)?;
66        check_if_continuation_stack_too_large(processor, continuation_stack)
67    }
68}
69
70/// A [`Stopper`] that always stops execution after each single step. An error is returned if the
71/// maximum cycle count is exceeded.
72pub struct StepStopper;
73
74impl Stopper for StepStopper {
75    type Processor = FastProcessor;
76    type Forest = Arc<MastForest>;
77
78    #[inline(always)]
79    fn should_stop(
80        &self,
81        processor: &FastProcessor,
82        continuation_stack: &ContinuationStack<Arc<MastForest>>,
83        continuation_after_stop: impl FnOnce() -> Option<(
84            Continuation<Arc<MastForest>>,
85            Option<DebugSourceNodeId>,
86        )>,
87    ) -> ControlFlow<BreakReason<Arc<MastForest>>> {
88        check_if_max_cycles_exceeded(processor)?;
89        check_if_continuation_stack_too_large(processor, continuation_stack)?;
90
91        ControlFlow::Break(BreakReason::Stopped(continuation_after_stop()))
92    }
93}
94
95/// Checks if the maximum cycle count has been exceeded, returning a `BreakReason::Err` if so.
96#[inline(always)]
97fn check_if_max_cycles_exceeded<F>(processor: &FastProcessor) -> ControlFlow<BreakReason<F>> {
98    if processor.clk > processor.options.max_cycles() as usize {
99        ControlFlow::Break(BreakReason::Err(ExecutionError::CycleLimitExceeded(
100            processor.options.max_cycles(),
101        )))
102    } else {
103        ControlFlow::Continue(())
104    }
105}
106
107/// Checks if the continuation stack size exceeds the maximum allowed, returning a
108/// `BreakReason::Err` if so.
109#[inline(always)]
110fn check_if_continuation_stack_too_large<F>(
111    processor: &FastProcessor,
112    continuation_stack: &ContinuationStack<F>,
113) -> ControlFlow<BreakReason<F>> {
114    if continuation_stack.len() > processor.options.max_num_continuations() {
115        ControlFlow::Break(BreakReason::Err(ExecutionError::Internal(
116            "continuation stack size exceeded the allowed maximum",
117        )))
118    } else {
119        ControlFlow::Continue(())
120    }
121}
122
123// BREAK REASON
124// ===============================================================================================
125
126/// The reason why execution was interrupted.
127#[derive(Debug)]
128pub enum BreakReason<F> {
129    /// An execution error occurred
130    Err(ExecutionError),
131    /// Execution was stopped by a [`Stopper`]. Provides the continuation to add to the continuation
132    /// stack before returning, if any. The mental model to have in mind when choosing the
133    /// continuation to add on a call to `FastProcessor::increment_clk()` is:
134    ///
135    /// "If execution is stopped here, does the current continuation stack properly encode the next
136    /// step of execution?"
137    ///
138    /// If yes, then `None` should be returned. If not, then the continuation that runs the next
139    /// step in `FastProcessor::execute_impl()` should be returned.
140    Stopped(Option<(Continuation<F>, Option<DebugSourceNodeId>)>),
141}