use alloc::vec::Vec;
use core::fmt;
use miden_crypto::{Felt, hash::rpo::RpoDigest};
use miden_formatting::{
hex::ToHex,
prettier::{Document, PrettyPrint, const_text, nl, text},
};
use super::MastNodeExt;
use crate::{
OPCODE_CALL, OPCODE_SYSCALL,
chiplets::hasher,
mast::{DecoratorId, MastForest, MastForestError, MastNodeId, Remapping},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallNode {
callee: MastNodeId,
is_syscall: bool,
digest: RpoDigest,
before_enter: Vec<DecoratorId>,
after_exit: Vec<DecoratorId>,
}
impl CallNode {
pub const CALL_DOMAIN: Felt = Felt::new(OPCODE_CALL as u64);
pub const SYSCALL_DOMAIN: Felt = Felt::new(OPCODE_SYSCALL as u64);
}
impl CallNode {
pub fn new(callee: MastNodeId, mast_forest: &MastForest) -> Result<Self, MastForestError> {
if callee.as_usize() >= mast_forest.nodes.len() {
return Err(MastForestError::NodeIdOverflow(callee, mast_forest.nodes.len()));
}
let digest = {
let callee_digest = mast_forest[callee].digest();
hasher::merge_in_domain(&[callee_digest, RpoDigest::default()], Self::CALL_DOMAIN)
};
Ok(Self {
callee,
is_syscall: false,
digest,
before_enter: Vec::new(),
after_exit: Vec::new(),
})
}
pub fn new_unsafe(callee: MastNodeId, digest: RpoDigest) -> Self {
Self {
callee,
is_syscall: false,
digest,
before_enter: Vec::new(),
after_exit: Vec::new(),
}
}
pub fn new_syscall(
callee: MastNodeId,
mast_forest: &MastForest,
) -> Result<Self, MastForestError> {
if callee.as_usize() >= mast_forest.nodes.len() {
return Err(MastForestError::NodeIdOverflow(callee, mast_forest.nodes.len()));
}
let digest = {
let callee_digest = mast_forest[callee].digest();
hasher::merge_in_domain(&[callee_digest, RpoDigest::default()], Self::SYSCALL_DOMAIN)
};
Ok(Self {
callee,
is_syscall: true,
digest,
before_enter: Vec::new(),
after_exit: Vec::new(),
})
}
pub fn new_syscall_unsafe(callee: MastNodeId, digest: RpoDigest) -> Self {
Self {
callee,
is_syscall: true,
digest,
before_enter: Vec::new(),
after_exit: Vec::new(),
}
}
}
impl CallNode {
pub fn digest(&self) -> RpoDigest {
self.digest
}
pub fn callee(&self) -> MastNodeId {
self.callee
}
pub fn is_syscall(&self) -> bool {
self.is_syscall
}
pub fn domain(&self) -> Felt {
if self.is_syscall() {
Self::SYSCALL_DOMAIN
} else {
Self::CALL_DOMAIN
}
}
pub fn before_enter(&self) -> &[DecoratorId] {
&self.before_enter
}
pub fn after_exit(&self) -> &[DecoratorId] {
&self.after_exit
}
}
impl CallNode {
pub fn remap_children(&self, remapping: &Remapping) -> Self {
let mut node = self.clone();
node.callee = node.callee.remap(remapping);
node
}
pub fn append_before_enter(&mut self, decorator_ids: &[DecoratorId]) {
self.before_enter.extend_from_slice(decorator_ids);
}
pub fn append_after_exit(&mut self, decorator_ids: &[DecoratorId]) {
self.after_exit.extend_from_slice(decorator_ids);
}
}
impl MastNodeExt for CallNode {
fn decorators(&self) -> impl Iterator<Item = (usize, DecoratorId)> {
self.before_enter.iter().chain(&self.after_exit).copied().enumerate()
}
}
impl CallNode {
pub(super) fn to_pretty_print<'a>(
&'a self,
mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
CallNodePrettyPrint { node: self, mast_forest }
}
pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
CallNodePrettyPrint { node: self, mast_forest }
}
}
struct CallNodePrettyPrint<'a> {
node: &'a CallNode,
mast_forest: &'a MastForest,
}
impl CallNodePrettyPrint<'_> {
fn concatenate_decorators(
&self,
decorator_ids: &[DecoratorId],
prepend: Document,
append: Document,
) -> Document {
let decorators = decorator_ids
.iter()
.map(|&decorator_id| self.mast_forest[decorator_id].render())
.reduce(|acc, doc| acc + const_text(" ") + doc)
.unwrap_or_default();
if decorators.is_empty() {
decorators
} else {
prepend + decorators + append
}
}
fn single_line_pre_decorators(&self) -> Document {
self.concatenate_decorators(self.node.before_enter(), Document::Empty, const_text(" "))
}
fn single_line_post_decorators(&self) -> Document {
self.concatenate_decorators(self.node.after_exit(), const_text(" "), Document::Empty)
}
fn multi_line_pre_decorators(&self) -> Document {
self.concatenate_decorators(self.node.before_enter(), Document::Empty, nl())
}
fn multi_line_post_decorators(&self) -> Document {
self.concatenate_decorators(self.node.after_exit(), nl(), Document::Empty)
}
}
impl PrettyPrint for CallNodePrettyPrint<'_> {
fn render(&self) -> Document {
let call_or_syscall = {
let callee_digest = self.mast_forest[self.node.callee].digest();
if self.node.is_syscall {
const_text("syscall")
+ const_text(".")
+ text(callee_digest.as_bytes().to_hex_with_prefix())
} else {
const_text("call")
+ const_text(".")
+ text(callee_digest.as_bytes().to_hex_with_prefix())
}
};
let single_line = self.single_line_pre_decorators()
+ call_or_syscall.clone()
+ self.single_line_post_decorators();
let multi_line =
self.multi_line_pre_decorators() + call_or_syscall + self.multi_line_post_decorators();
single_line | multi_line
}
}
impl fmt::Display for CallNodePrettyPrint<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::prettier::PrettyPrint;
self.pretty_print(f)
}
}