Skip to content

Commit

Permalink
refactor: use assembler instead of source manager when building libra…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
bobbinth committed Aug 13, 2024
1 parent d1679d6 commit 13fb3ba
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 45 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Changelog

## 0.10.1 (2024-08-10)
## 0.10.3 (2024-08-11)

#### Enhancements

- Exposed `KernelLibrary` struct publicly. (#1443).

#### Changes

- [BREAKING] Replaced `SourceManager` parameter with `Assembler` in `Library::from_dir` (#1443).
- [BREAKING] Moved `Library` export to the root of the `miden-assembly` crate. (#1443).

## 0.10.2 (2024-08-10)

#### Enhancements

Expand Down
5 changes: 5 additions & 0 deletions assembly/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ impl Assembler {
self.module_graph.kernel()
}

/// Returns a link to the source manager used by this assembler.
pub fn source_manager(&self) -> Arc<dyn SourceManager> {
self.source_manager.clone()
}

#[cfg(any(test, feature = "testing"))]
#[doc(hidden)]
pub fn module_graph(&self) -> &ModuleGraph {
Expand Down
7 changes: 0 additions & 7 deletions assembly/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,3 @@ impl From<Report> for AssemblyError {
Self::Other(RelatedError::new(report))
}
}

#[derive(Debug, thiserror::Error, Diagnostic)]
pub enum CompiledLibraryError {
#[error("Invalid exports: MAST forest has {roots_len} procedure roots, but exports have {exports_len}")]
#[diagnostic()]
InvalidExports { exports_len: usize, roots_len: usize },
}
13 changes: 9 additions & 4 deletions assembly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ pub mod ast;
mod compile;
pub mod diagnostics;
mod errors;
pub mod library;
mod library;
mod parser;
mod sema;
#[cfg(any(test, feature = "testing"))]
pub mod testing;
#[cfg(test)]
mod tests;

/// Re-exported for downstream crates
// Re-exported for downstream crates

/// Merkelized abstract syntax tree (MAST) components defining Miden VM programs.
pub use vm_core::mast;
pub use vm_core::utils;

Expand All @@ -40,8 +42,11 @@ pub use self::{
DefaultSourceManager, Report, SourceFile, SourceId, SourceManager, SourceSpan, Span,
Spanned,
},
errors::{AssemblyError, CompiledLibraryError},
library::{LibraryError, LibraryNamespace, LibraryPath, PathError, Version},
errors::AssemblyError,
library::{
KernelLibrary, Library, LibraryError, LibraryNamespace, LibraryPath, LibraryPathComponent,
PathError, Version, VersionError,
},
parser::ModuleParser,
};

Expand Down
28 changes: 12 additions & 16 deletions assembly/src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn content_hash(

#[cfg(feature = "std")]
mod use_std_library {
use std::{collections::btree_map::Entry, fs, io, path::Path, sync::Arc};
use std::{collections::btree_map::Entry, fs, io, path::Path};

use masl::{LibraryEntry, WalkLibrary};
use miette::{Context, Report};
Expand All @@ -211,7 +211,7 @@ mod use_std_library {
use super::*;
use crate::{
ast::{self, ModuleKind},
diagnostics::{IntoDiagnostic, SourceManager},
diagnostics::IntoDiagnostic,
Assembler,
};

Expand Down Expand Up @@ -284,7 +284,7 @@ mod use_std_library {
pub fn from_dir(
path: impl AsRef<Path>,
namespace: LibraryNamespace,
source_manager: Arc<dyn SourceManager>,
assembler: Assembler,
) -> Result<Self, Report> {
let path = path.as_ref();
if !path.is_dir() {
Expand All @@ -299,7 +299,7 @@ mod use_std_library {
return Err(Report::msg("mod.masm is not allowed in the root directory"));
}

Self::compile_modules_from_dir(namespace, path, source_manager)
Self::compile_modules_from_dir(namespace, path, assembler)
}

/// Read the contents (modules) of this library from `dir`, returning any errors that occur
Expand All @@ -312,7 +312,7 @@ mod use_std_library {
fn compile_modules_from_dir(
namespace: LibraryNamespace,
dir: &Path,
source_manager: Arc<dyn SourceManager>,
assembler: Assembler,
) -> Result<Self, Report> {
let mut modules = BTreeMap::default();

Expand All @@ -326,7 +326,8 @@ mod use_std_library {
}
// Parse module at the given path
let mut parser = ast::Module::parser(ModuleKind::Library);
let ast = parser.parse_file(name.clone(), &source_path, &source_manager)?;
let ast =
parser.parse_file(name.clone(), &source_path, &assembler.source_manager())?;
match modules.entry(name) {
Entry::Occupied(ref entry) => {
return Err(LibraryError::DuplicateModulePath(entry.key().clone()))
Expand All @@ -347,9 +348,7 @@ mod use_std_library {
.into());
}

Assembler::new(source_manager)
.with_debug_mode(true)
.assemble_library(modules.into_values())
assembler.assemble_library(modules.into_values())
}

pub fn deserialize_from_file(path: impl AsRef<Path>) -> Result<Self, DeserializationError> {
Expand Down Expand Up @@ -518,10 +517,10 @@ impl Deserializable for KernelLibrary {

#[cfg(feature = "std")]
mod use_std_kernel {
use std::{io, path::Path, sync::Arc};
use std::{io, path::Path};

use super::*;
use crate::diagnostics::{Report, SourceManager};
use crate::{diagnostics::Report, Assembler};

impl KernelLibrary {
/// Write the library to a target file
Expand All @@ -533,11 +532,8 @@ mod use_std_kernel {
///
/// This is essentially a wrapper around [Library::from_dir], which then validates
/// that the resulting [Library] is a valid [KernelLibrary].
pub fn from_dir(
path: impl AsRef<Path>,
source_manager: Arc<dyn SourceManager>,
) -> Result<Self, Report> {
let library = Library::from_dir(path, LibraryNamespace::Kernel, source_manager)?;
pub fn from_dir(path: impl AsRef<Path>, assembler: Assembler) -> Result<Self, Report> {
let library = Library::from_dir(path, LibraryNamespace::Kernel, assembler)?;

Ok(Self::try_from(library)?)
}
Expand Down
10 changes: 4 additions & 6 deletions miden/src/cli/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;

use assembly::{
diagnostics::{IntoDiagnostic, Report},
library::Library,
LibraryNamespace, Version,
Assembler, Library, LibraryNamespace,
};
use clap::Parser;

Expand Down Expand Up @@ -40,12 +39,11 @@ impl BundleCmd {
.into_owned(),
};

let source_manager = Arc::new(assembly::DefaultSourceManager::default());
let library_namespace =
namespace.parse::<LibraryNamespace>().expect("invalid base namespace");
// TODO: Add version to `Library`
let _version = self.version.parse::<Version>().expect("invalid cargo version");
let stdlib = Library::from_dir(&self.dir, library_namespace, source_manager)?;
// let version = self.version.parse::<Version>().expect("invalid cargo version");
let stdlib = Library::from_dir(&self.dir, library_namespace, Assembler::default())?;

// write the masl output
let output_file = self
Expand Down
3 changes: 1 addition & 2 deletions miden/src/cli/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use std::{
use assembly::{
ast::{Module, ModuleKind},
diagnostics::{IntoDiagnostic, Report, WrapErr},
library::Library,
Assembler, LibraryNamespace,
Assembler, Library, LibraryNamespace,
};
use miden_vm::{
crypto::{MerkleStore, MerkleTree, NodeIndex, PartialMerkleTree, RpoDigest, SimpleSmt},
Expand Down
2 changes: 1 addition & 1 deletion miden/src/repl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::BTreeSet, path::PathBuf};

use assembly::{library::Library, Assembler};
use assembly::{Assembler, Library};
use miden_vm::{math::Felt, DefaultHost, StackInputs, Word};
use processor::ContextId;
use rustyline::{error::ReadlineError, DefaultEditor};
Expand Down
10 changes: 4 additions & 6 deletions stdlib/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{env, path::Path, sync::Arc};
use std::{env, path::Path};

use assembly::{
diagnostics::{IntoDiagnostic, Result},
library::Library,
LibraryNamespace, Version,
Assembler, Library, LibraryNamespace,
};

// CONSTANTS
Expand All @@ -26,11 +25,10 @@ fn main() -> Result<()> {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let asm_dir = Path::new(manifest_dir).join(ASM_DIR_PATH);

let source_manager = Arc::new(assembly::DefaultSourceManager::default());
let namespace = "std".parse::<LibraryNamespace>().expect("invalid base namespace");
// TODO: Add version to `Library`
let _version = env!("CARGO_PKG_VERSION").parse::<Version>().expect("invalid cargo version");
let stdlib = Library::from_dir(asm_dir, namespace, source_manager)?;
//let version = env!("CARGO_PKG_VERSION").parse::<Version>().expect("invalid cargo version");
let stdlib = Library::from_dir(asm_dir, namespace, Assembler::default())?;

// write the masl output
let build_dir = env::var("OUT_DIR").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate alloc;

use assembly::{library::Library, mast::MastForest, utils::Deserializable};
use assembly::{mast::MastForest, utils::Deserializable, Library};

// STANDARD LIBRARY
// ================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use alloc::{
vec::Vec,
};

use assembly::library::Library;
use assembly::Library;
// EXPORTS
// ================================================================================================
pub use assembly::{diagnostics::Report, LibraryPath, SourceFile, SourceManager};
Expand Down

0 comments on commit 13fb3ba

Please sign in to comment.