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(
44            vec![],
45            vec![],
46            SierraApChange::Known { new_vars_only: true },
47        ))
48    }
49}
50
51/// Disable ap tracking.
52/// This Libfunc is used to disable ap tracking to allow merging branches that some have unknown ap
53/// change, without actually revoking the local stack.
54#[derive(Default)]
55pub struct DisableApTrackingLibfunc {}
56impl NoGenericArgsGenericLibfunc for DisableApTrackingLibfunc {
57    const STR_ID: &'static str = "disable_ap_tracking";
58
59    fn specialize_signature(
60        &self,
61        _context: &dyn SignatureSpecializationContext,
62    ) -> Result<LibfuncSignature, SpecializationError> {
63        Ok(LibfuncSignature::new_non_branch(
64            vec![],
65            vec![],
66            SierraApChange::Known { new_vars_only: true },
67        ))
68    }
69}