Skip to main content

miden_core/operations/decorators/
assembly_op.rs

1use alloc::string::String;
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: String,
19    op: String,
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: String,
29        num_cycles: u8,
30        op: String,
31    ) -> Self {
32        Self { location, context_name, op, num_cycles }
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) -> &str {
42        &self.context_name
43    }
44
45    /// Returns the number of VM cycles taken to execute the assembly instruction of this decorator.
46    pub const fn num_cycles(&self) -> u8 {
47        self.num_cycles
48    }
49
50    /// Returns the assembly instruction corresponding to this decorator.
51    pub fn op(&self) -> &str {
52        &self.op
53    }
54
55    // STATE MUTATORS
56    // --------------------------------------------------------------------------------------------
57
58    /// Change cycles corresponding to an AsmOp decorator 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}