cairo_lang_sierra/extensions/modules/
ap_tracking.rs

1use crate::define_libfunc_hierarchy;
2use crate::extensions::lib_func::{
3    LibfuncSignature, SierraApChange, SignatureSpecializationContext,
4};
5use crate::extensions::{NoGenericArgsGenericLibfunc, SpecializationError};
6
7define_libfunc_hierarchy! {
8    pub enum ApTrackingLibfunc {
9        Revoke(RevokeApTrackingLibfunc),
10        Enable(EnableApTrackingLibfunc),
11        Disable(DisableApTrackingLibfunc),
12    }, ApTrackingConcreteLibfunc
13}
14
15/// Revoke the ap tracking.
16/// This libfunc changes `ap_tracking` state to unknown,
17/// allowing a path with known ap tracking to converge with a path with unknown ap tracking.
18#[derive(Default)]
19pub struct RevokeApTrackingLibfunc {}
20impl NoGenericArgsGenericLibfunc for RevokeApTrackingLibfunc {
21    const STR_ID: &'static str = "revoke_ap_tracking";
22
23    fn specialize_signature(
24        &self,
25        _context: &dyn SignatureSpecializationContext,
26    ) -> Result<LibfuncSignature, SpecializationError> {
27        Ok(LibfuncSignature::new_non_branch(vec![], vec![], SierraApChange::Unknown))
28    }
29}
30
31/// Enable ap tracking.
32/// This Libfunc is used to enable ap tracking to allow branches that may diverge and merge after
33/// this point to have an aligned ap.
34#[derive(Default)]
35pub struct EnableApTrackingLibfunc {}
36impl NoGenericArgsGenericLibfunc for EnableApTrackingLibfunc {
37    const STR_ID: &'static str = "enable_ap_tracking";
38
39    fn specialize_signature(
40        &self,
41        _context: &dyn SignatureSpecializationContext,
42    ) -> Result<LibfuncSignature, SpecializationError> {
43        Ok(LibfuncSignature::new_non_branch(vec![], vec![], SierraApChange::Known {
44            new_vars_only: true,
45        }))
46    }
47}
48
49/// Disable ap tracking.
50/// This Libfunc is used to disable ap tracking to allow merging branches that some have unknown ap
51/// change, without actually revoking the local stack.
52#[derive(Default)]
53pub struct DisableApTrackingLibfunc {}
54impl NoGenericArgsGenericLibfunc for DisableApTrackingLibfunc {
55    const STR_ID: &'static str = "disable_ap_tracking";
56
57    fn specialize_signature(
58        &self,
59        _context: &dyn SignatureSpecializationContext,
60    ) -> Result<LibfuncSignature, SpecializationError> {
61        Ok(LibfuncSignature::new_non_branch(vec![], vec![], SierraApChange::Known {
62            new_vars_only: true,
63        }))
64    }
65}