pub struct Assembler { /* private fields */ }
Expand description
The Assembler produces a Merkelized Abstract Syntax Tree (MAST) from Miden Assembly sources, as an artifact of one of three types:
- A kernel library (see
KernelLibrary
) - A library (see
Library
) - A program (see
Program
)
Assembled artifacts can additionally reference or include code from previously assembled libraries.
§Usage
Depending on your needs, there are multiple ways of using the assembler, starting with the type of artifact you want to produce:
- If you wish to produce an executable program, you will call
Self::assemble_program
with the source module which contains the program entrypoint. - If you wish to produce a library for use in other executables, you will call
Self::assemble_library
with the source module(s) whose exports form the public API of the library. - If you wish to produce a kernel library, you will call
Self::assemble_kernel
with the source module(s) whose exports form the public API of the kernel.
In the case where you are assembling a library or program, you also need to determine if you need to specify a kernel. You will need to do so if any of your code needs to call into the kernel directly.
- If a kernel is needed, you should construct an
Assembler
usingAssembler::with_kernel
- Otherwise, you should construct an
Assembler
usingAssembler::new
Lastly, you need to provide inputs to the assembler which it will use at link time to resolve
references to procedures which are externally-defined (i.e. not defined in any of the modules
provided to the assemble_*
function you called). There are a few different ways to do this:
- If you have source code, or a
ast::Module
, seeSelf::compile_and_statically_link
- If you need to reference procedures from a previously assembled
Library
, but do not want to include the MAST of those procedures in the assembled artifact, you want to dynamically link that library, seeSelf::link_dynamic_library
for more. - If you want to incorporate referenced procedures from a previously assembled
Library
into the assembled artifact, you want to statically link that library, seeSelf::link_static_library
for more.
Implementations§
Source§impl Assembler
Constructors
impl Assembler
Constructors
Sourcepub fn with_kernel(
source_manager: Arc<dyn SourceManager>,
kernel_lib: KernelLibrary,
) -> Assembler
pub fn with_kernel( source_manager: Arc<dyn SourceManager>, kernel_lib: KernelLibrary, ) -> Assembler
Start building an Assembler
with a kernel defined by the provided KernelLibrary.
Sourcepub fn with_warnings_as_errors(self, yes: bool) -> Assembler
pub fn with_warnings_as_errors(self, yes: bool) -> Assembler
Sets the default behavior of this assembler with regard to warning diagnostics.
When true, any warning diagnostics that are emitted will be promoted to errors.
Sourcepub fn with_debug_mode(self, yes: bool) -> Assembler
pub fn with_debug_mode(self, yes: bool) -> Assembler
Puts the assembler into the debug mode.
Sourcepub fn set_debug_mode(&mut self, yes: bool)
pub fn set_debug_mode(&mut self, yes: bool)
Sets the debug mode flag of the assembler
Source§impl Assembler
Dependency Management
impl Assembler
Dependency Management
Sourcepub fn compile_and_statically_link(
&mut self,
module: impl Parse,
) -> Result<&mut Assembler, Report>
pub fn compile_and_statically_link( &mut self, module: impl Parse, ) -> Result<&mut Assembler, Report>
Ensures module
is compiled, and then statically links it into the final artifact.
The given module must be a library module, or an error will be returned.
Sourcepub fn compile_and_statically_link_all(
&mut self,
modules: impl IntoIterator<Item = impl Parse>,
) -> Result<&mut Assembler, Report>
pub fn compile_and_statically_link_all( &mut self, modules: impl IntoIterator<Item = impl Parse>, ) -> Result<&mut Assembler, Report>
Ensures every module in modules
is compiled, and then statically links them into the final
artifact.
All of the given modules must be library modules, or an error will be returned.
Sourcepub fn compile_and_statically_link_from_dir(
&mut self,
namespace: LibraryNamespace,
dir: impl AsRef<Path>,
) -> Result<(), Report>
pub fn compile_and_statically_link_from_dir( &mut self, namespace: LibraryNamespace, dir: impl AsRef<Path>, ) -> Result<(), Report>
Compiles all Miden Assembly modules in the provided directory, and then statically links them into the final artifact.
When compiling each module, the path of the module is derived by appending path components
corresponding to the relative path of the module in dir
, to namespace
. If a source file
named mod.masm
is found, the resulting module will derive its path using the path
components of the parent directory, rather than the file name.
For example, let’s assume we call this function with the namespace my_lib
, for a
directory at path ~/masm
. Now, let’s look at how various file system paths would get
translated to their corresponding module paths:
file path | module path |
---|---|
~/masm/mod.masm | “my_lib” |
~/masm/foo.masm | “my_lib::foo” |
~/masm/bar/mod.masm | “my_lib::bar” |
~/masm/bar/baz.masm | “my_lib::bar::baz” |
Sourcepub fn link_library(
&mut self,
library: impl AsRef<Library>,
kind: LinkLibraryKind,
) -> Result<(), Report>
pub fn link_library( &mut self, library: impl AsRef<Library>, kind: LinkLibraryKind, ) -> Result<(), Report>
Links the final artifact against library
.
The way in which procedures referenced in library
will be linked by the final artifact is
determined by kind
:
LinkLibraryKind::Dynamic
inserts a reference to the procedure in the assembled MAST, but not the MAST of the procedure itself. Consequently, it is necessary to provide both the assembled artifact andlibrary
to the VM when executing the program, otherwise the procedure reference will not be resolvable at runtime.LinkLibraryKind::Static
includes the MAST of the referenced procedure in the final artifact, including any code reachable from that procedure contained inlibrary
. The resulting artifact does not requirelibrary
to be provided to the VM when executing it, as all procedure references were resolved ahead of time.
Sourcepub fn link_dynamic_library(
&mut self,
library: impl AsRef<Library>,
) -> Result<(), Report>
pub fn link_dynamic_library( &mut self, library: impl AsRef<Library>, ) -> Result<(), Report>
Dynamically link against library
during assembly.
This makes it possible to resolve references to procedures exported by the library during assembly, without including code from the library into the assembled artifact.
Dynamic linking produces smaller binaries, but requires you to provide library
to the VM
at runtime when executing the assembled artifact.
Internally, calls to procedures exported from library
will be lowered to a
miden_core::mast::ExternalNode
in the resulting MAST. These nodes represent an indirect
reference to the root MAST node of the referenced procedure. These indirect references
are resolved at runtime by the processor when executed.
One consequence of these types of references, is that in the case where multiple procedures have the same MAST root, but different decorators, it is not (currently) possible for the processor to distinguish between which specific procedure (and its resulting decorators) the caller intended to reference, and so any of them might be chosen.
In order to reduce the chance of this producing confusing diagnostics or debugger output, it is not recommended to export multiple procedures with the same MAST root, but differing decorators, from a library. There are scenarios where this might be necessary, such as when renaming a procedure, or moving it between modules, while keeping the original definition around during a deprecation period. It is just something to be aware of if you notice, for example, unexpected procedure paths or source locations in diagnostics - it could be due to this edge case.
Sourcepub fn with_dynamic_library(
self,
library: impl AsRef<Library>,
) -> Result<Assembler, Report>
pub fn with_dynamic_library( self, library: impl AsRef<Library>, ) -> Result<Assembler, Report>
Dynamically link against library
during assembly.
See Self::link_dynamic_library
for more details.
Sourcepub fn link_static_library(
&mut self,
library: impl AsRef<Library>,
) -> Result<(), Report>
pub fn link_static_library( &mut self, library: impl AsRef<Library>, ) -> Result<(), Report>
Statically link against library
during assembly.
This makes it possible to resolve references to procedures exported by the library during assembly, and ensure that the referenced procedure and any code reachable from it in that library, are included in the assembled artifact.
Static linking produces larger binaries, but allows you to produce self-contained artifacts
that avoid the requirement that you provide library
to the VM at runtime.
Source§impl Assembler
Public Accessors
impl Assembler
Public Accessors
Sourcepub fn warnings_as_errors(&self) -> bool
pub fn warnings_as_errors(&self) -> bool
Returns true if this assembler promotes warning diagnostics as errors by default.
Sourcepub fn in_debug_mode(&self) -> bool
pub fn in_debug_mode(&self) -> bool
Returns true if this assembler was instantiated in debug mode.
Source§impl Assembler
Compilation/Assembly
impl Assembler
Compilation/Assembly
Sourcepub fn assemble_library(
self,
modules: impl IntoIterator<Item = impl Parse>,
) -> Result<Library, Report>
pub fn assemble_library( self, modules: impl IntoIterator<Item = impl Parse>, ) -> Result<Library, Report>
Sourcepub fn assemble_library_from_dir(
self,
path: impl AsRef<Path>,
namespace: LibraryNamespace,
) -> Result<Library, Report>
pub fn assemble_library_from_dir( self, path: impl AsRef<Path>, namespace: LibraryNamespace, ) -> Result<Library, Report>
Assemble a Library from a standard Miden Assembly project layout.
The standard layout dictates that a given path is the root of a namespace, and the
directory hierarchy corresponds to the namespace hierarchy. A .masm
file found in a
given subdirectory of the root, will be parsed with its LibraryPath set based on
where it resides in the directory structure.
This function recursively parses the entire directory structure under path
, ignoring
any files which do not have the .masm
extension.
For example, let’s say I call this function like so:
use miden_assembly::{Assembler, LibraryNamespace};
Assembler::default()
.assemble_library_from_dir("~/masm/std", LibraryNamespace::new("std").unwrap());
Here’s how we would handle various files under this path:
- ~/masm/std/sys.masm -> Parsed as “std::sys”
- ~/masm/std/crypto/hash.masm -> Parsed as “std::crypto::hash”
- ~/masm/std/math/u32.masm -> Parsed as “std::math::u32”
- ~/masm/std/math/u64.masm -> Parsed as “std::math::u64”
- ~/masm/std/math/README.md -> Ignored
Sourcepub fn assemble_kernel(
self,
module: impl Parse,
) -> Result<KernelLibrary, Report>
pub fn assemble_kernel( self, module: impl Parse, ) -> Result<KernelLibrary, Report>
Assembles the provided module into a KernelLibrary intended to be used as a Kernel.
§Errors
Returns an error if parsing or compilation of the specified modules fails.
Sourcepub fn assemble_kernel_from_dir(
self,
sys_module_path: impl AsRef<Path>,
lib_dir: Option<impl AsRef<Path>>,
) -> Result<KernelLibrary, Report>
pub fn assemble_kernel_from_dir( self, sys_module_path: impl AsRef<Path>, lib_dir: Option<impl AsRef<Path>>, ) -> Result<KernelLibrary, Report>
Assemble a KernelLibrary from a standard Miden Assembly kernel project layout.
The kernel library will export procedures defined by the module at sys_module_path
.
If the optional lib_dir
is provided, all modules under this directory will be available
from the kernel module under the $kernel
namespace. For example, if lib_dir
is set to
“~/masm/lib”, the files will be accessible in the kernel module as follows:
- ~/masm/lib/foo.masm -> Can be imported as “$kernel::foo”
- ~/masm/lib/bar/baz.masm -> Can be imported as “$kernel::bar::baz”
Note: this is a temporary structure which will likely change once https://github.com/0xMiden/miden-vm/issues/1436 is implemented.
impl Assembler
Procedure Invocation
impl Assembler
Instruction Compilation
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Assembler
impl !RefUnwindSafe for Assembler
impl !Send for Assembler
impl !Sync for Assembler
impl Unpin for Assembler
impl !UnwindSafe for Assembler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more