1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
mod basic_block_node;
use alloc::{boxed::Box, vec::Vec};
use core::fmt;
pub(crate) use basic_block_node::collect_immediate_placements;
pub use basic_block_node::{
BATCH_SIZE as OP_BATCH_SIZE, BasicBlockNode, BasicBlockNodeBuilder,
GROUP_SIZE as OP_GROUP_SIZE, OpBatch,
};
use derive_more::From;
use miden_utils_core_derive::MastNodeExt;
mod call_node;
pub use call_node::{CallNode, CallNodeBuilder};
mod dyn_node;
pub use dyn_node::{DynNode, DynNodeBuilder};
mod external;
pub use external::{ExternalNode, ExternalNodeBuilder};
mod join_node;
pub use join_node::{JoinNode, JoinNodeBuilder};
mod split_node;
use miden_crypto::{Felt, Word};
use miden_formatting::prettier::PrettyPrint;
pub use split_node::{SplitNode, SplitNodeBuilder};
mod loop_node;
#[cfg(any(test, feature = "arbitrary"))]
pub use basic_block_node::arbitrary;
pub use loop_node::{LoopNode, LoopNodeBuilder};
mod mast_forest_contributor;
pub(super) use mast_forest_contributor::fingerprint_with_child_fingerprints;
pub use mast_forest_contributor::{MastForestContributor, MastNodeBuilder};
use crate::mast::{MastForest, MastNodeId};
pub trait MastNodeExt {
/// Returns a commitment/hash of the node.
fn digest(&self) -> Word;
/// Returns a display formatter for this node.
fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn fmt::Display + 'a>;
/// Returns a pretty printer for this node.
fn to_pretty_print<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn PrettyPrint + 'a>;
/// Returns true if the this node has children.
fn has_children(&self) -> bool;
/// Appends the NodeIds of the children of this node, if any, to the vector.
fn append_children_to(&self, target: &mut Vec<MastNodeId>);
/// Executes the given closure for each child of this node.
fn for_each_child<F>(&self, f: F)
where
F: FnMut(MastNodeId);
/// Returns the domain of this node.
fn domain(&self) -> Felt;
/// Converts this node into its corresponding builder, reusing allocated data where possible.
type Builder: MastForestContributor;
fn to_builder(self, forest: &MastForest) -> Self::Builder;
}
// MAST NODE
// ================================================================================================
#[derive(Debug, Clone, PartialEq, Eq, From, MastNodeExt)]
#[mast_node_ext(builder = "MastNodeBuilder")]
pub enum MastNode {
Block(BasicBlockNode),
Join(JoinNode),
Split(SplitNode),
Loop(LoopNode),
Call(CallNode),
Dyn(DynNode),
External(ExternalNode),
}
// ------------------------------------------------------------------------------------------------
/// Public accessors
impl MastNode {
/// Returns true if this node is an external node.
pub fn is_external(&self) -> bool {
matches!(self, MastNode::External(_))
}
/// Returns true if this node is a Dyn node.
pub fn is_dyn(&self) -> bool {
matches!(self, MastNode::Dyn(_))
}
/// Returns true if this node is a basic block.
pub fn is_basic_block(&self) -> bool {
matches!(self, Self::Block(_))
}
/// Returns the inner basic block node if the [`MastNode`] wraps a [`BasicBlockNode`]; `None`
/// otherwise.
pub fn get_basic_block(&self) -> Option<&BasicBlockNode> {
match self {
MastNode::Block(basic_block_node) => Some(basic_block_node),
_ => None,
}
}
/// Unwraps the inner basic block node if the [`MastNode`] wraps a [`BasicBlockNode`]; panics
/// otherwise.
///
/// # Panics
/// Panics if the [`MastNode`] does not wrap a [`BasicBlockNode`].
pub fn unwrap_basic_block(&self) -> &BasicBlockNode {
match self {
Self::Block(basic_block_node) => basic_block_node,
other => unwrap_failed(other, "basic block"),
}
}
/// Unwraps the inner join node if the [`MastNode`] wraps a [`JoinNode`]; panics otherwise.
///
/// # Panics
/// - if the [`MastNode`] does not wrap a [`JoinNode`].
pub fn unwrap_join(&self) -> &JoinNode {
match self {
Self::Join(join_node) => join_node,
other => unwrap_failed(other, "join"),
}
}
/// Unwraps the inner split node if the [`MastNode`] wraps a [`SplitNode`]; panics otherwise.
///
/// # Panics
/// - if the [`MastNode`] does not wrap a [`SplitNode`].
pub fn unwrap_split(&self) -> &SplitNode {
match self {
Self::Split(split_node) => split_node,
other => unwrap_failed(other, "split"),
}
}
/// Unwraps the inner loop node if the [`MastNode`] wraps a [`LoopNode`]; panics otherwise.
///
/// # Panics
/// - if the [`MastNode`] does not wrap a [`LoopNode`].
pub fn unwrap_loop(&self) -> &LoopNode {
match self {
Self::Loop(loop_node) => loop_node,
other => unwrap_failed(other, "loop"),
}
}
/// Unwraps the inner call node if the [`MastNode`] wraps a [`CallNode`]; panics otherwise.
///
/// # Panics
/// - if the [`MastNode`] does not wrap a [`CallNode`].
pub fn unwrap_call(&self) -> &CallNode {
match self {
Self::Call(call_node) => call_node,
other => unwrap_failed(other, "call"),
}
}
/// Unwraps the inner dynamic node if the [`MastNode`] wraps a [`DynNode`]; panics otherwise.
///
/// # Panics
/// - if the [`MastNode`] does not wrap a [`DynNode`].
pub fn unwrap_dyn(&self) -> &DynNode {
match self {
Self::Dyn(dyn_node) => dyn_node,
other => unwrap_failed(other, "dyn"),
}
}
/// Unwraps the inner external node if the [`MastNode`] wraps a [`ExternalNode`]; panics
/// otherwise.
///
/// # Panics
/// - if the [`MastNode`] does not wrap a [`ExternalNode`].
pub fn unwrap_external(&self) -> &ExternalNode {
match self {
Self::External(external_node) => external_node,
other => unwrap_failed(other, "external"),
}
}
}
// HELPERS
// ===============================================================================================
/// This function is analogous to the `unwrap_failed()` function used in the implementation of
/// `core::result::Result` `unwrap_*()` methods.
#[cold]
#[inline(never)]
#[track_caller]
fn unwrap_failed(node: &MastNode, expected: &str) -> ! {
let actual = match node {
MastNode::Block(_) => "basic block",
MastNode::Join(_) => "join",
MastNode::Split(_) => "split",
MastNode::Loop(_) => "loop",
MastNode::Call(_) => "call",
MastNode::Dyn(_) => "dynamic",
MastNode::External(_) => "external",
};
panic!("tried to unwrap {expected} node, but got {actual}");
}