Skip to content

Commit

Permalink
feat(assembler): Remove MaslLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Jul 30, 2024
1 parent acb7e41 commit 64b444b
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 107 deletions.
6 changes: 6 additions & 0 deletions assembly/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ impl Assembler {
Ok(())
}

/// Adds the compiled library to provide modules for the compilation.
pub fn with_compiled_library(mut self, library: CompiledLibrary) -> Result<Self, Report> {
self.add_compiled_library(library)?;
Ok(self)
}

/// Adds the library to provide modules for the compilation.
pub fn with_library<L>(mut self, library: &L) -> Result<Self, Report>
where
Expand Down
4 changes: 1 addition & 3 deletions assembly/src/library/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ pub enum CompiledLibraryError {
procedure_path: FullyQualifiedProcedureName,
},
#[error("exports are not in the same namespace. All namespaces: {namespaces:?}")]
InconsistentNamespaces {
namespaces: Vec<LibraryNamespace>
},
InconsistentNamespaces { namespaces: Vec<LibraryNamespace> },
#[error(transparent)]
Kernel(#[from] KernelError),
}
27 changes: 27 additions & 0 deletions assembly/src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod tests;
// ================================================================================================

/// Represents a library where all modules were compiled into a [`MastForest`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompiledLibrary {
mast_forest: MastForest,
// a path for every `root` in the associated MAST forest
Expand Down Expand Up @@ -172,6 +173,18 @@ impl CompiledLibrary {
}
}

impl Serializable for CompiledLibrary {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.write_into_with_options(target, AstSerdeOptions::default())
}
}

impl Deserializable for CompiledLibrary {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
Self::read_from_with_options(source, AstSerdeOptions::default())
}
}

#[cfg(feature = "std")]
mod use_std_library {
use super::*;
Expand All @@ -181,6 +194,7 @@ mod use_std_library {
use masl::{LibraryEntry, WalkLibrary};
use miette::{Context, Report};
use std::{fs, io, path::Path};
use vm_core::utils::ReadAdapter;

impl CompiledLibrary {
/// File extension for the Assembly Library.
Expand Down Expand Up @@ -304,6 +318,19 @@ mod use_std_library {

Assembler::default().assemble_library(modules.into_values())
}

pub fn deserialize_from_file(path: impl AsRef<Path>) -> Result<Self, DeserializationError> {
let path = path.as_ref();
let mut file = fs::File::open(path).map_err(|err| {
DeserializationError::InvalidValue(format!(
"failed to open file at {}: {err}",
path.to_string_lossy()
))
})?;
let mut adapter = ReadAdapter::new(&mut file);

Self::read_from(&mut adapter)
}
}
}
// KERNEL LIBRARY
Expand Down
46 changes: 23 additions & 23 deletions assembly/src/library/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use alloc::{string::ToString, sync::Arc, vec::Vec};

use vm_core::utils::{Deserializable, Serializable, SliceReader};
use vm_core::utils::SliceReader;

use super::{Library, LibraryNamespace, LibraryPath, MaslLibrary, Version};
use super::LibraryPath;
use crate::{
ast::{AstSerdeOptions, Module, ModuleKind},
ast::{AstSerdeOptions, Module, ModuleKind, ProcedureName},
diagnostics::{IntoDiagnostic, Report, SourceFile},
library::CompiledLibrary,
testing::TestContext,
Assembler,
};

macro_rules! parse_module {
Expand Down Expand Up @@ -44,23 +46,28 @@ fn masl_locations_serialization() -> Result<(), Report> {
let modules = vec![foo, bar];

// serialize/deserialize the bundle with locations
let namespace = LibraryNamespace::new("test").unwrap();
let version = Version::min();
let bundle = MaslLibrary::new(namespace, version, modules.iter().cloned(), Vec::new())?;
let bundle = Assembler::default().assemble_library(modules.iter().cloned()).unwrap();

let mut bytes = Vec::new();
bundle.write_into(&mut bytes);
let deserialized = MaslLibrary::read_from(&mut SliceReader::new(&bytes)).unwrap();
bundle.write_into_with_options(&mut bytes, AstSerdeOptions::new(true, true));
let deserialized = CompiledLibrary::read_from_with_options(
&mut SliceReader::new(&bytes),
AstSerdeOptions::new(true, false),
)
.unwrap();
assert_eq!(bundle, deserialized);

// serialize/deserialize the bundle without locations
let namespace = LibraryNamespace::new("test").unwrap();
let bundle = MaslLibrary::new(namespace, version, modules, Vec::new())?;
let bundle = Assembler::default().assemble_library(modules.iter().cloned()).unwrap();

// serialize/deserialize the bundle
let mut bytes = Vec::new();
bundle.write_into_with_options(&mut bytes, AstSerdeOptions::new(true, false));
let deserialized = MaslLibrary::read_from(&mut SliceReader::new(&bytes)).unwrap();
let deserialized = CompiledLibrary::read_from_with_options(
&mut SliceReader::new(&bytes),
AstSerdeOptions::new(true, false),
)
.unwrap();
assert_eq!(bundle, deserialized);

Ok(())
Expand All @@ -79,20 +86,13 @@ fn get_module_by_path() -> Result<(), Report> {
let modules = vec![foo];

// create the bundle with locations
let namespace = LibraryNamespace::new("test")?;
let version = Version::min();
let bundle = MaslLibrary::new(namespace, version, modules, Vec::new())?;
let bundle = Assembler::default().assemble_library(modules.iter().cloned()).unwrap();

// get AST associated with "test::foo" path
let foo_ast = bundle.get_module(&LibraryPath::new("test::foo").unwrap()).unwrap();
let foo_expected = "export.foo
add
end
let foo_module_info = bundle.into_module_infos().next().unwrap();
assert_eq!(foo_module_info.path(), &LibraryPath::new("test::foo").unwrap());

";
assert_eq!(foo_ast.to_string(), foo_expected);

assert!(bundle.get_module(&LibraryPath::new("test::bar").unwrap()).is_none());
let (_, foo_proc) = foo_module_info.procedure_infos().next().unwrap();
assert_eq!(foo_proc.name, ProcedureName::new("foo").unwrap());

Ok(())
}
6 changes: 4 additions & 2 deletions miden/benches/program_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ fn program_compilation(c: &mut Criterion) {
exec.sha256::hash_2to1
end";
bench.iter(|| {
let assembler = Assembler::default()
.with_library(&StdLibrary::default())
let mut assembler = Assembler::default();
assembler
.add_compiled_library(StdLibrary::default().into())
.expect("failed to load stdlib");

assembler.assemble_program(source).expect("Failed to compile test source.")
});
});
Expand Down
5 changes: 3 additions & 2 deletions miden/benches/program_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ fn program_execution(c: &mut Criterion) {
begin
exec.sha256::hash_2to1
end";
let assembler = Assembler::default()
.with_library(&StdLibrary::default())
let mut assembler = Assembler::default();
assembler
.add_compiled_library(StdLibrary::default().into())
.expect("failed to load stdlib");
let program: Program =
assembler.assemble_program(source).expect("Failed to compile test source.");
Expand Down
12 changes: 8 additions & 4 deletions miden/src/cli/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use assembly::{
ast::AstSerdeOptions,
diagnostics::{IntoDiagnostic, Report},
LibraryNamespace, MaslLibrary, Version,
library::CompiledLibrary,
LibraryNamespace, Version,
};
use clap::Parser;
use std::path::PathBuf;
Expand Down Expand Up @@ -40,11 +42,13 @@ impl BundleCmd {

let library_namespace =
namespace.parse::<LibraryNamespace>().expect("invalid base namespace");
let version = self.version.parse::<Version>().expect("invalid cargo version");
let stdlib = MaslLibrary::read_from_dir(&self.dir, library_namespace, version)?;
// TODO: Add version to `Library`
let _version = self.version.parse::<Version>().expect("invalid cargo version");
let stdlib = CompiledLibrary::from_dir(&self.dir, library_namespace)?;

// write the masl output
stdlib.write_to_dir(self.dir.clone()).into_diagnostic()?;
let options = AstSerdeOptions::new(false, false);
stdlib.write_to_dir(self.dir.clone(), options).into_diagnostic()?;

println!("Built library {}", namespace);

Expand Down
28 changes: 16 additions & 12 deletions miden/src/cli/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use assembly::{
ast::{Module, ModuleKind},
diagnostics::{IntoDiagnostic, Report, WrapErr},
Assembler, Library, LibraryNamespace, MaslLibrary,
library::CompiledLibrary,
Assembler, LibraryNamespace,
};
use miden_vm::{
crypto::{MerkleStore, MerkleTree, NodeIndex, PartialMerkleTree, RpoDigest, SimpleSmt},
Expand Down Expand Up @@ -404,20 +405,21 @@ impl ProgramFile {

/// Compiles this program file into a [Program].
#[instrument(name = "compile_program", skip_all)]
pub fn compile<'a, I, L>(&self, debug: &Debug, libraries: I) -> Result<Program, Report>
pub fn compile<'a, I>(&self, debug: &Debug, libraries: I) -> Result<Program, Report>
where
I: IntoIterator<Item = &'a L>,
L: ?Sized + Library + 'static,
I: IntoIterator<Item = &'a CompiledLibrary>,
{
// compile program
let mut assembler = Assembler::default()
.with_debug_mode(debug.is_on())
.with_library(&StdLibrary::default())
let mut assembler = Assembler::default().with_debug_mode(debug.is_on());
assembler
.add_compiled_library(StdLibrary::default().into())
.wrap_err("Failed to load stdlib")?;

assembler = assembler
.with_libraries(libraries.into_iter())
.wrap_err("Failed to load libraries")?;
for library in libraries {
assembler
.add_compiled_library(library.clone())
.wrap_err("Failed to load libraries")?;
}

let program: Program = assembler
.assemble_program(self.ast.as_ref())
Expand Down Expand Up @@ -529,7 +531,7 @@ impl ProgramHash {
// LIBRARY FILE
// ================================================================================================
pub struct Libraries {
pub libraries: Vec<MaslLibrary>,
pub libraries: Vec<CompiledLibrary>,
}

impl Libraries {
Expand All @@ -543,7 +545,9 @@ impl Libraries {
let mut libraries = Vec::new();

for path in paths {
let library = MaslLibrary::read_from_file(path)?;
// TODO(plafer): How to create a `Report` from an error that doesn't derive
// `Diagnostic`?
let library = CompiledLibrary::deserialize_from_file(path).unwrap();
libraries.push(library);
}

Expand Down
2 changes: 1 addition & 1 deletion miden/src/examples/blake3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn generate_blake3_program(n: usize) -> Program {
);

Assembler::default()
.with_library(&StdLibrary::default())
.with_compiled_library(StdLibrary::default().into())
.unwrap()
.assemble_program(program)
.unwrap()
Expand Down
22 changes: 12 additions & 10 deletions miden/src/repl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assembly::{Assembler, Library, MaslLibrary};
use assembly::{library::CompiledLibrary, Assembler};
use miden_vm::{math::Felt, DefaultHost, StackInputs, Word};
use processor::ContextId;
use rustyline::{error::ReadlineError, DefaultEditor};
Expand Down Expand Up @@ -151,13 +151,13 @@ pub fn start_repl(library_paths: &Vec<PathBuf>, use_stdlib: bool) {
// load libraries from files
let mut provided_libraries = Vec::new();
for path in library_paths {
let library = MaslLibrary::read_from_file(path)
let library = CompiledLibrary::deserialize_from_file(path)
.map_err(|e| format!("Failed to read library: {e}"))
.unwrap();
provided_libraries.push(library);
}
if use_stdlib {
provided_libraries.push(MaslLibrary::from(StdLibrary::default()));
provided_libraries.push(StdLibrary::default().into());
}

println!("========================== Miden REPL ============================");
Expand Down Expand Up @@ -303,14 +303,16 @@ pub fn start_repl(library_paths: &Vec<PathBuf>, use_stdlib: bool) {
#[allow(clippy::type_complexity)]
fn execute(
program: String,
provided_libraries: &[MaslLibrary],
provided_libraries: &[CompiledLibrary],
) -> Result<(Vec<(u64, Word)>, Vec<Felt>), String> {
// compile program
let mut assembler = Assembler::default();

assembler = assembler
.with_libraries(provided_libraries.iter())
.map_err(|err| format!("{err}"))?;
for library in provided_libraries {
assembler
.add_compiled_library(library.clone())
.map_err(|err| format!("{err}"))?;
}

let program = assembler.assemble_program(program).map_err(|err| format!("{err}"))?;

Expand Down Expand Up @@ -357,16 +359,16 @@ fn read_mem_address(mem_str: &str) -> Result<u64, String> {
/// all available modules if no module name was provided.
fn handle_use_command(
line: String,
provided_libraries: &Vec<MaslLibrary>,
provided_libraries: &[CompiledLibrary],
imported_modules: &mut BTreeSet<String>,
) {
let tokens: Vec<&str> = line.split_whitespace().collect();

match tokens.len() {
1 => {
println!("Modules available for importing:");
for lib in provided_libraries {
lib.modules().for_each(|module| println!("{}", module.path()));
for lib in provided_libraries.iter().cloned() {
lib.into_module_infos().for_each(|module| println!("{}", module.path()));
}
}
2 => {
Expand Down
2 changes: 1 addition & 1 deletion miden/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ where
{
let program = Assembler::default()
.with_debug_mode(true)
.with_library(&StdLibrary::default())?
.with_compiled_library(StdLibrary::default().into())?
.assemble_program(program)?;
let mut execution_details = ExecutionDetails::default();

Expand Down
4 changes: 3 additions & 1 deletion miden/tests/integration/flow_control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ fn simple_dyncall() {
#[allow(unused)]
#[test]
fn procref() {
let mut assembler = Assembler::default().with_library(&StdLibrary::default()).unwrap();
let mut assembler = Assembler::default()
.with_compiled_library(StdLibrary::default().into())
.unwrap();

let module_source = "
use.std::math::u64
Expand Down
Loading

0 comments on commit 64b444b

Please sign in to comment.