From 64c74014d00580baeeca2837bd2e8ecf06bc4a2e Mon Sep 17 00:00:00 2001
From: Serge Radinovich <47865535+sergerad@users.noreply.github.com>
Date: Sun, 21 Jul 2024 06:41:50 +1200
Subject: [PATCH] feat: add more functions for ensuring nodes via
MastForestBuilder (#1404)
---
CHANGELOG.md | 9 +-
assembly/src/assembler/basic_block_builder.rs | 5 +-
.../src/assembler/instruction/procedures.rs | 26 ++--
assembly/src/assembler/mast_forest_builder.rs | 62 ++++++++-
assembly/src/assembler/mod.rs | 27 ++--
assembly/src/assembler/tests.rs | 128 +++++++-----------
core/src/mast/node/mod.rs | 2 +-
core/src/mast/serialization/info.rs | 2 +-
core/src/mast/serialization/tests.rs | 2 +-
processor/src/decoder/tests.rs | 2 +-
10 files changed, 139 insertions(+), 126 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 72392ea618..b8751ad390 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,8 +6,8 @@
- Added error codes support for the `mtree_verify` instruction (#1328).
- Added support for immediate values for `lt`, `lte`, `gt`, `gte` comparison instructions (#1346).
-- Change MAST to a table-based representation (#1349)
-- Introduce `MastForestStore` (#1359)
+- Changed MAST to a table-based representation (#1349)
+- Introduced `MastForestStore` (#1359)
- Adjusted prover's metal acceleration code to work with 0.9 versions of the crates (#1357)
- Added support for immediate values for `u32lt`, `u32lte`, `u32gt`, `u32gte`, `u32min` and `u32max` comparison instructions (#1358).
- Added support for the `nop` instruction, which corresponds to the VM opcode of the same name, and has the same semantics. This is implemented for use by compilers primarily.
@@ -16,9 +16,10 @@
- Added support for immediate values for `u32and`, `u32or`, `u32xor` and `u32not` bitwise instructions (#1362).
- Optimized `std::sys::truncate_stuck` procedure (#1384).
- Updated CI and Makefile to standardise it accross Miden repositories (#1342).
-- Add serialization/deserialization for `MastForest` (#1370)
+- Added serialization/deserialization for `MastForest` (#1370)
- Updated CI to support `CHANGELOG.md` modification checking and `no changelog` label (#1406)
-- Introduce `MastForestError` to enforce `MastForest` node count invariant (#1394)
+- Introduced `MastForestError` to enforce `MastForest` node count invariant (#1394)
+- Added functions to `MastForestBuilder` to allow ensuring of nodes with fewer LOC (#1404)
#### Changed
diff --git a/assembly/src/assembler/basic_block_builder.rs b/assembly/src/assembler/basic_block_builder.rs
index 2545d4eea8..5263faeff2 100644
--- a/assembly/src/assembler/basic_block_builder.rs
+++ b/assembly/src/assembler/basic_block_builder.rs
@@ -4,7 +4,7 @@ use super::{
};
use alloc::{borrow::Borrow, string::ToString, vec::Vec};
use vm_core::{
- mast::{MastForestError, MastNode, MastNodeId},
+ mast::{MastForestError, MastNodeId},
AdviceInjector, AssemblyOp, Operation,
};
@@ -134,8 +134,7 @@ impl BasicBlockBuilder {
let ops = self.ops.drain(..).collect();
let decorators = self.decorators.drain(..).collect();
- let basic_block_node = MastNode::new_basic_block_with_decorators(ops, decorators);
- let basic_block_node_id = mast_forest_builder.ensure_node(basic_block_node)?;
+ let basic_block_node_id = mast_forest_builder.ensure_block(ops, Some(decorators))?;
Ok(Some(basic_block_node_id))
} else if !self.decorators.is_empty() {
diff --git a/assembly/src/assembler/instruction/procedures.rs b/assembly/src/assembler/instruction/procedures.rs
index d6a96f0f1b..a86474fce7 100644
--- a/assembly/src/assembler/instruction/procedures.rs
+++ b/assembly/src/assembler/instruction/procedures.rs
@@ -6,7 +6,7 @@ use crate::{
};
use smallvec::SmallVec;
-use vm_core::mast::{MastForest, MastNode, MastNodeId};
+use vm_core::mast::{MastForest, MastNodeId};
/// Procedure Invocation
impl Assembler {
@@ -96,8 +96,7 @@ impl Assembler {
None => {
// If the MAST root called isn't known to us, make it an external
// reference.
- let external_node = MastNode::new_external(mast_root);
- mast_forest_builder.ensure_node(external_node)?
+ mast_forest_builder.ensure_external(mast_root)?
}
}
}
@@ -107,13 +106,11 @@ impl Assembler {
None => {
// If the MAST root called isn't known to us, make it an external
// reference.
- let external_node = MastNode::new_external(mast_root);
- mast_forest_builder.ensure_node(external_node)?
+ mast_forest_builder.ensure_external(mast_root)?
}
};
- let call_node = MastNode::new_call(callee_id, mast_forest_builder.forest());
- mast_forest_builder.ensure_node(call_node)?
+ mast_forest_builder.ensure_call(callee_id)?
}
InvokeKind::SysCall => {
let callee_id = match mast_forest_builder.find_procedure_root(mast_root) {
@@ -121,14 +118,11 @@ impl Assembler {
None => {
// If the MAST root called isn't known to us, make it an external
// reference.
- let external_node = MastNode::new_external(mast_root);
- mast_forest_builder.ensure_node(external_node)?
+ mast_forest_builder.ensure_external(mast_root)?
}
};
- let syscall_node =
- MastNode::new_syscall(callee_id, mast_forest_builder.forest());
- mast_forest_builder.ensure_node(syscall_node)?
+ mast_forest_builder.ensure_syscall(callee_id)?
}
}
};
@@ -141,7 +135,7 @@ impl Assembler {
&self,
mast_forest_builder: &mut MastForestBuilder,
) -> Result