use alloc::sync::Arc;
use core::ops::ControlFlow;
use miden_core::{FMP_ADDR, FMP_INIT_VALUE};
use crate::{
BreakReason, ContextId, Host, MapExecErr, Stopper,
continuation_stack::Continuation,
execution::{
ExecutionState, finalize_clock_cycle, finalize_clock_cycle_with_continuation,
get_next_ctx_id,
},
mast::{CallNode, MastForest, MastNodeExt, MastNodeId},
operation::OperationError,
processor::{MemoryInterface, Processor, SystemInterface},
tracer::Tracer,
};
#[inline(always)]
pub(super) fn start_call_node<P, H, S, T>(
state: &mut ExecutionState<'_, P, H, S, T>,
call_node: &CallNode,
current_node_id: MastNodeId,
current_forest: &Arc<MastForest>,
) -> ControlFlow<BreakReason>
where
P: Processor,
H: Host,
S: Stopper<Processor = P>,
T: Tracer<Processor = P>,
{
state.tracer.start_clock_cycle(
state.processor,
Continuation::StartNode(current_node_id),
state.continuation_stack,
current_forest,
);
state
.processor
.execute_before_enter_decorators(current_node_id, current_forest, state.host)?;
state.processor.save_context_and_truncate_stack();
let callee_hash = current_forest[call_node.callee()].digest();
if call_node.is_syscall() {
if !state.kernel.contains_proc(callee_hash) {
let err = OperationError::SyscallTargetNotInKernel { proc_root: callee_hash };
return ControlFlow::Break(BreakReason::Err(err.with_context(
current_forest,
current_node_id,
state.host,
)));
}
state.tracer.record_kernel_proc_access(callee_hash);
state.processor.system_mut().set_ctx(ContextId::root());
} else {
let new_ctx: ContextId = get_next_ctx_id(state.processor);
state.processor.system_mut().set_ctx(new_ctx);
state.processor.system_mut().set_caller_hash(callee_hash);
if let Err(err) = state
.processor
.memory_mut()
.write_element(new_ctx, FMP_ADDR, FMP_INIT_VALUE)
.map_exec_err(current_forest, current_node_id, state.host)
{
return ControlFlow::Break(BreakReason::Err(err));
}
state.tracer.record_memory_write_element(
FMP_INIT_VALUE,
FMP_ADDR,
new_ctx,
state.processor.system().clock(),
);
}
state.continuation_stack.push_finish_call(current_node_id);
state.continuation_stack.push_start_node(call_node.callee());
finalize_clock_cycle(
state.processor,
state.tracer,
state.stopper,
state.continuation_stack,
current_forest,
)
}
#[inline(always)]
pub(super) fn finish_call_node<P, H, S, T>(
state: &mut ExecutionState<'_, P, H, S, T>,
node_id: MastNodeId,
current_forest: &Arc<MastForest>,
) -> ControlFlow<BreakReason>
where
P: Processor,
H: Host,
S: Stopper<Processor = P>,
T: Tracer<Processor = P>,
{
state.tracer.start_clock_cycle(
state.processor,
Continuation::FinishCall(node_id),
state.continuation_stack,
current_forest,
);
if let Err(e) = state.processor.restore_context() {
return ControlFlow::Break(BreakReason::Err(e.with_context(
current_forest,
node_id,
state.host,
)));
}
finalize_clock_cycle_with_continuation(
state.processor,
state.tracer,
state.stopper,
state.continuation_stack,
|| Some(Continuation::AfterExitDecorators(node_id)),
current_forest,
)?;
state
.processor
.execute_after_exit_decorators(node_id, current_forest, state.host)
}