Skip to main content

miden_core/operations/debug_metadata/
assembly_op.rs

1use alloc::sync::Arc;
2use core::fmt;
3
4use miden_debug_types::Location;
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8// ASSEMBLY OP
9// ================================================================================================
10
11/// Contains information corresponding to an assembly instruction (only applicable in debug mode).
12#[derive(Clone, Debug, Eq, PartialEq)]
13#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14#[cfg_attr(all(feature = "arbitrary", test), miden_test_serde_macros::serde_test)]
15pub struct AssemblyOp {
16    #[cfg_attr(feature = "serde", serde(default))]
17    location: Option<Location>,
18    context_name: Arc<str>,
19    op: Arc<str>,
20    num_cycles: u8,
21}
22
23impl AssemblyOp {
24    /// Returns [AssemblyOp] instantiated with the specified assembly instruction string and number
25    /// of cycles it takes to execute the assembly instruction.
26    pub fn new(
27        location: Option<Location>,
28        context_name: impl Into<Arc<str>>,
29        num_cycles: u8,
30        op: impl Into<Arc<str>>,
31    ) -> Self {
32        Self {
33            location,
34            context_name: context_name.into(),
35            op: op.into(),
36            num_cycles,
37        }
38    }
39
40    /// Returns the [Location] for this operation, if known
41    pub fn location(&self) -> Option<&Location> {
42        self.location.as_ref()
43    }
44
45    /// Returns the context name for this operation.
46    pub fn context_name(&self) -> &Arc<str> {
47        &self.context_name
48    }
49
50    /// Returns the number of VM cycles taken to execute the assembly instruction.
51    pub const fn num_cycles(&self) -> u8 {
52        self.num_cycles
53    }
54
55    /// Returns the assembly instruction corresponding to this source mapping.
56    pub fn op(&self) -> &Arc<str> {
57        &self.op
58    }
59
60    // STATE MUTATORS
61    // --------------------------------------------------------------------------------------------
62
63    /// Change cycles corresponding to this AssemblyOp to the specified number of cycles.
64    pub fn set_num_cycles(&mut self, num_cycles: u8) {
65        self.num_cycles = num_cycles;
66    }
67
68    /// Change the [Location] of this [AssemblyOp]
69    pub fn set_location(&mut self, location: Location) {
70        self.location = Some(location);
71    }
72}
73
74impl fmt::Display for AssemblyOp {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        write!(
77            f,
78            "context={}, operation={}, cost={}",
79            self.context_name, self.op, self.num_cycles,
80        )
81    }
82}