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