miden_core/mast/node/
call_node.rs1use alloc::vec::Vec;
2use core::fmt;
3
4use miden_crypto::{Felt, hash::rpo::RpoDigest};
5use miden_formatting::{
6 hex::ToHex,
7 prettier::{Document, PrettyPrint, const_text, nl, text},
8};
9
10use super::MastNodeExt;
11use crate::{
12 OPCODE_CALL, OPCODE_SYSCALL,
13 chiplets::hasher,
14 mast::{DecoratorId, MastForest, MastForestError, MastNodeId, Remapping},
15};
16
17#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct CallNode {
28 callee: MastNodeId,
29 is_syscall: bool,
30 digest: RpoDigest,
31 before_enter: Vec<DecoratorId>,
32 after_exit: Vec<DecoratorId>,
33}
34
35impl CallNode {
38 pub const CALL_DOMAIN: Felt = Felt::new(OPCODE_CALL as u64);
40 pub const SYSCALL_DOMAIN: Felt = Felt::new(OPCODE_SYSCALL as u64);
42}
43
44impl CallNode {
47 pub fn new(callee: MastNodeId, mast_forest: &MastForest) -> Result<Self, MastForestError> {
49 if callee.as_usize() >= mast_forest.nodes.len() {
50 return Err(MastForestError::NodeIdOverflow(callee, mast_forest.nodes.len()));
51 }
52 let digest = {
53 let callee_digest = mast_forest[callee].digest();
54
55 hasher::merge_in_domain(&[callee_digest, RpoDigest::default()], Self::CALL_DOMAIN)
56 };
57
58 Ok(Self {
59 callee,
60 is_syscall: false,
61 digest,
62 before_enter: Vec::new(),
63 after_exit: Vec::new(),
64 })
65 }
66
67 pub fn new_unsafe(callee: MastNodeId, digest: RpoDigest) -> Self {
70 Self {
71 callee,
72 is_syscall: false,
73 digest,
74 before_enter: Vec::new(),
75 after_exit: Vec::new(),
76 }
77 }
78
79 pub fn new_syscall(
82 callee: MastNodeId,
83 mast_forest: &MastForest,
84 ) -> Result<Self, MastForestError> {
85 if callee.as_usize() >= mast_forest.nodes.len() {
86 return Err(MastForestError::NodeIdOverflow(callee, mast_forest.nodes.len()));
87 }
88 let digest = {
89 let callee_digest = mast_forest[callee].digest();
90
91 hasher::merge_in_domain(&[callee_digest, RpoDigest::default()], Self::SYSCALL_DOMAIN)
92 };
93
94 Ok(Self {
95 callee,
96 is_syscall: true,
97 digest,
98 before_enter: Vec::new(),
99 after_exit: Vec::new(),
100 })
101 }
102
103 pub fn new_syscall_unsafe(callee: MastNodeId, digest: RpoDigest) -> Self {
106 Self {
107 callee,
108 is_syscall: true,
109 digest,
110 before_enter: Vec::new(),
111 after_exit: Vec::new(),
112 }
113 }
114}
115
116impl CallNode {
119 pub fn digest(&self) -> RpoDigest {
138 self.digest
139 }
140
141 pub fn callee(&self) -> MastNodeId {
143 self.callee
144 }
145
146 pub fn is_syscall(&self) -> bool {
148 self.is_syscall
149 }
150
151 pub fn domain(&self) -> Felt {
153 if self.is_syscall() {
154 Self::SYSCALL_DOMAIN
155 } else {
156 Self::CALL_DOMAIN
157 }
158 }
159
160 pub fn before_enter(&self) -> &[DecoratorId] {
162 &self.before_enter
163 }
164
165 pub fn after_exit(&self) -> &[DecoratorId] {
167 &self.after_exit
168 }
169}
170
171impl CallNode {
173 pub fn remap_children(&self, remapping: &Remapping) -> Self {
174 let mut node = self.clone();
175 node.callee = node.callee.remap(remapping);
176 node
177 }
178
179 pub fn append_before_enter(&mut self, decorator_ids: &[DecoratorId]) {
181 self.before_enter.extend_from_slice(decorator_ids);
182 }
183
184 pub fn append_after_exit(&mut self, decorator_ids: &[DecoratorId]) {
186 self.after_exit.extend_from_slice(decorator_ids);
187 }
188}
189
190impl MastNodeExt for CallNode {
191 fn decorators(&self) -> impl Iterator<Item = (usize, DecoratorId)> {
192 self.before_enter.iter().chain(&self.after_exit).copied().enumerate()
193 }
194}
195
196impl CallNode {
200 pub(super) fn to_pretty_print<'a>(
201 &'a self,
202 mast_forest: &'a MastForest,
203 ) -> impl PrettyPrint + 'a {
204 CallNodePrettyPrint { node: self, mast_forest }
205 }
206
207 pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
208 CallNodePrettyPrint { node: self, mast_forest }
209 }
210}
211
212struct CallNodePrettyPrint<'a> {
213 node: &'a CallNode,
214 mast_forest: &'a MastForest,
215}
216
217impl CallNodePrettyPrint<'_> {
218 fn concatenate_decorators(
221 &self,
222 decorator_ids: &[DecoratorId],
223 prepend: Document,
224 append: Document,
225 ) -> Document {
226 let decorators = decorator_ids
227 .iter()
228 .map(|&decorator_id| self.mast_forest[decorator_id].render())
229 .reduce(|acc, doc| acc + const_text(" ") + doc)
230 .unwrap_or_default();
231
232 if decorators.is_empty() {
233 decorators
234 } else {
235 prepend + decorators + append
236 }
237 }
238
239 fn single_line_pre_decorators(&self) -> Document {
240 self.concatenate_decorators(self.node.before_enter(), Document::Empty, const_text(" "))
241 }
242
243 fn single_line_post_decorators(&self) -> Document {
244 self.concatenate_decorators(self.node.after_exit(), const_text(" "), Document::Empty)
245 }
246
247 fn multi_line_pre_decorators(&self) -> Document {
248 self.concatenate_decorators(self.node.before_enter(), Document::Empty, nl())
249 }
250
251 fn multi_line_post_decorators(&self) -> Document {
252 self.concatenate_decorators(self.node.after_exit(), nl(), Document::Empty)
253 }
254}
255
256impl PrettyPrint for CallNodePrettyPrint<'_> {
257 fn render(&self) -> Document {
258 let call_or_syscall = {
259 let callee_digest = self.mast_forest[self.node.callee].digest();
260 if self.node.is_syscall {
261 const_text("syscall")
262 + const_text(".")
263 + text(callee_digest.as_bytes().to_hex_with_prefix())
264 } else {
265 const_text("call")
266 + const_text(".")
267 + text(callee_digest.as_bytes().to_hex_with_prefix())
268 }
269 };
270
271 let single_line = self.single_line_pre_decorators()
272 + call_or_syscall.clone()
273 + self.single_line_post_decorators();
274 let multi_line =
275 self.multi_line_pre_decorators() + call_or_syscall + self.multi_line_post_decorators();
276
277 single_line | multi_line
278 }
279}
280
281impl fmt::Display for CallNodePrettyPrint<'_> {
282 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283 use crate::prettier::PrettyPrint;
284 self.pretty_print(f)
285 }
286}