Assembler

Struct Assembler 

Source
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:

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.

Programs compiled with an empty kernel cannot use the `syscall` instruction.

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:

Implementations§

Source§

impl Assembler

Constructors

Source

pub fn new(source_manager: Arc<dyn SourceManager>) -> Assembler

Start building an Assembler

Source

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.

Source

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.

Source

pub fn with_debug_mode(self, yes: bool) -> Assembler

Puts the assembler into the debug mode.

Source

pub fn set_debug_mode(&mut self, yes: bool)

Sets the debug mode flag of the assembler

Source§

impl Assembler

Dependency Management

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.

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.

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 pathmodule 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”

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 and library 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 in library. The resulting artifact does not require library to be provided to the VM when executing it, as all procedure references were resolved ahead of time.

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.

Source

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.

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

pub fn with_static_library( self, library: impl AsRef<Library>, ) -> Result<Assembler, Report>

Statically link against library during assembly.

See Self::link_static_library

Source§

impl Assembler

Public Accessors

Source

pub fn warnings_as_errors(&self) -> bool

Returns true if this assembler promotes warning diagnostics as errors by default.

Source

pub fn in_debug_mode(&self) -> bool

Returns true if this assembler was instantiated in debug mode.

Source

pub fn kernel(&self) -> &Kernel

Returns a reference to the kernel for this assembler.

If the assembler was instantiated without a kernel, the internal kernel will be empty.

Source§

impl Assembler

Compilation/Assembly

Source

pub fn assemble_library( self, modules: impl IntoIterator<Item = impl Parse>, ) -> Result<Library, Report>

Assembles a set of modules into a Library.

§Errors

Returns an error if parsing or compilation of the specified modules fails.

Source

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
Source

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.

Source

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.

Source

pub fn assemble_program(self, source: impl Parse) -> Result<Program, Report>

Compiles the provided module into a Program. The resulting program can be executed on Miden VM.

§Errors

Returns an error if parsing or compilation of the specified program fails, or if the source doesn’t have an entrypoint.

Source§

impl Assembler

This impl block contains no items.

Procedure Invocation

Source§

impl Assembler

This impl block contains no items.

Instruction Compilation

Trait Implementations§

Source§

impl Clone for Assembler

Source§

fn clone(&self) -> Assembler

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Assembler

Source§

fn default() -> Assembler

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more