miden_processor/fast/
sys_ops.rs1use miden_core::{Felt, mast::MastForest, sys_events::SystemEvent};
2
3use super::{ExecutionError, FastProcessor, ONE};
4use crate::{
5 AsyncHost, BaseHost, ErrorContext, FMP_MIN,
6 operations::sys_ops::sys_event_handlers::handle_system_event, system::FMP_MAX,
7};
8
9impl FastProcessor {
10 #[inline(always)]
12 pub fn op_assert(
13 &mut self,
14 err_code: Felt,
15 host: &mut impl BaseHost,
16 program: &MastForest,
17 err_ctx: &impl ErrorContext,
18 ) -> Result<(), ExecutionError> {
19 if self.stack_get(0) != ONE {
20 let process = &mut self.state();
21 host.on_assert_failed(process, err_code);
22 let err_msg = program.resolve_error_message(err_code);
23 return Err(ExecutionError::failed_assertion(
24 process.clk(),
25 err_code,
26 err_msg,
27 err_ctx,
28 ));
29 }
30 self.decrement_stack_size();
31 Ok(())
32 }
33
34 pub fn op_fmpadd(&mut self) {
36 let fmp = self.fmp;
37 let top = self.stack_get_mut(0);
38
39 *top += fmp;
40 }
41
42 pub fn op_fmpupdate(&mut self) -> Result<(), ExecutionError> {
44 let top = self.stack_get(0);
45
46 let new_fmp = self.fmp + top;
47 let new_fmp_int = new_fmp.as_int();
48 if !(FMP_MIN..=FMP_MAX).contains(&new_fmp_int) {
49 return Err(ExecutionError::InvalidFmpValue(self.fmp, new_fmp));
50 }
51
52 self.fmp = new_fmp;
53 self.decrement_stack_size();
54 Ok(())
55 }
56
57 pub fn op_sdepth(&mut self) -> Result<(), ExecutionError> {
59 let depth = self.stack_depth();
60 self.increment_stack_size()?;
61 self.stack_write(0, depth.into());
62
63 Ok(())
64 }
65
66 pub fn op_caller(&mut self) -> Result<(), ExecutionError> {
68 if !self.in_syscall {
69 return Err(ExecutionError::CallerNotInSyscall);
70 }
71
72 let caller_hash = self.caller_hash;
73 self.stack_write_word(0, &caller_hash);
74
75 Ok(())
76 }
77
78 pub fn op_clk(&mut self) -> Result<(), ExecutionError> {
80 self.increment_stack_size()?;
81 self.stack_write(0, self.clk.into());
82 Ok(())
83 }
84
85 #[inline(always)]
87 pub async fn op_emit(
88 &mut self,
89 event_id: u32,
90 host: &mut impl AsyncHost,
91 err_ctx: &impl ErrorContext,
92 ) -> Result<(), ExecutionError> {
93 let mut process = self.state();
94 if let Some(system_event) = SystemEvent::from_event_id(event_id) {
96 handle_system_event(&mut process, system_event, err_ctx)
97 } else {
98 let clk = process.clk();
99 let mutations = host
100 .on_event(&process, event_id)
101 .await
102 .map_err(|err| ExecutionError::event_error(err, event_id, err_ctx))?;
103 self.advice
104 .apply_mutations(mutations)
105 .map_err(|err| ExecutionError::advice_error(err, clk, err_ctx))?;
106 Ok(())
107 }
108 }
109}