Getting started with midenc
The midenc executable is the command-line interface for the compiler driver. Use the separate
miden-debug executable to execute and debug compiled packages.
While it is a lower-level tool compared to cargo-miden, just like the difference between rustc and cargo, it provides a lot of functionality for emitting diagnostic information, controlling the output of the compiler, and configuring the compilation pipeline. Most users will want to use cargo-miden, but understanding midenc is helpful for those times where you need to get your hands dirty.
Installation
To install midenc, you have two choices:
- Install via
midenup, which also handles other toolchain components that you'll likely want. - Install from source
We'll cover installation from source here - see the midenup README for details on how to install Miden components that way.
First, clone the compiler repo:
git clone https://github.com/0xMiden/compiler
Then, run the following in your shell in the cloned repo folder:
cargo install --path midenc --locked
Usage
Once installed, you should be able to invoke the compiler, you should see output similar to this:
midenc --help
Usage: midenc [OPTIONS] [FILE]
Arguments:
[FILE]
The input file to compile
You may specify `-` to read from stdin, otherwise you must provide a path
Options:
-p, --package <SPEC>
Package(s) to build
--manifest-path <PATH>
Path to the project manifest to build
Either a `miden-project.toml`, or the `Cargo.toml` beside it. This names the project when
no input file is given. An input file may be given as well only when it is that same
manifest; a source file cannot be combined with `--manifest-path`. Absent both, the project
is the `miden-project.toml` in the working directory, or the `Cargo.toml` there when no
Miden manifest exists beside it. A relative path is resolved against the directory the
compiler is run from, not against `--working-dir`.
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
..snip..
The actual help output covers quite a bit more than that - see the actual command output for the full picture.
Compilation
See the help output for midenc for detailed information on its options and their behavior. However, the following is an example of how one might use midenc in practice:
midenc --entrypoint 'foo::main' \
-lextra \
-L ./masm \
--emit=hir=- \
-o out.masp \
target/wasm32-wasip1/release/foo.wasm
In this scenario, we are in the root of a Rust crate, named foo, which we have compiled for the wasm32-wasip1 target, which placed the resulting WebAssembly module in the target/wasm32-wasip1/release directory. This crate exports a function named main, which we want to use as the entrypoint of the program.
Additionally, our Rust code links against some hand-written Miden Assembly code, namespaced under extra, which can be found in ./masm/extra. We are telling midenc to link the extra library, and to add the ./masm directory to the library search path.
Lastly, we're configuring the output:
- We're using
--emitto requestmidencto dump Miden IR (hir) to stdout (specified via the-shorthand), in addition to the Miden package artifact (produced by default). - We're telling
midencto write the compiled output toout.maspin the current directory, rather than the default path that would have been used (target/miden/foo.masp).
Stopping early
--stop-after=CHECKPOINT ends compilation once the named phase has run, rather than building a package. It is most useful together with --emit, to look at an intermediate form without paying for the rest of the build:
midenc --stop-after=transform --emit=hir=- foo.wasm
CHECKPOINT is either an alias — parse, analyze, transform, lower, assemble — or a fully-qualified checkpoint id such as hir.initial. Which names are valid depends on the input: each frontend declares its own route, and a name that route does not reach is reported along with the names it does accept. A Miden Assembly input, for example, has no transform phase.
Rust panic strategy
For Rust builds, -C panic=STRATEGY selects how panics terminate execution:
immediate-abort(the default) traps without invoking the crate's panic handler. It can reduce code size and execution cycles because the handler and its panic-formatting code can be omitted.abortinvokes the crate's panic handler. The handler can report the panic before trapping. It should terminate withcore::arch::wasm32::unreachable().
abort can introduce call graph cycles, especially through panic formatting. Miden does not yet support these cycles, which is why immediate-abort remains the default. Fixes and workarounds are being explored in #1486. Projects that avoid these cycles can already opt in to abort and print static panic messages:
cargo miden build --release -C panic=abort
The selected strategy is passed through Rust flags, which take precedence over Cargo profile
panic settings. Both midenc and cargo miden build support inherited overrides through
RUSTFLAGS (whitespace-separated) or CARGO_ENCODED_RUSTFLAGS (separated by ASCII 0x1f).
These flags are appended after the selected strategy, and the last panic setting wins.
CARGO_ENCODED_RUSTFLAGS takes precedence over RUSTFLAGS whenever it is set.
Standalone Rust files built through a temporary Cargo project support both strategies.
Files compiled directly with rustc use prebuilt standard libraries that are incompatible
with immediate-abort. That route ignores a request for this strategy with a warning. Use a
Cargo project to enable immediate-abort for those files.
This option does not affect prebuilt WebAssembly input. Its panic behavior was determined when the module was compiled.
Debugging
See Debugging Programs for details on how to debug Miden programs using miden-debug.
Next steps
We have put together two useful guides to walk through more detail on compiling Rust to WebAssembly:
- To learn how to compile Rust to WebAssembly so that you can invoke
midencon the resulting Wasm module, see this guide. - If you already have a WebAssembly module, or know how to produce one, and want to learn how to compile it to Miden Assembly, see this guide.
To start from a working project rather than an empty one, use cargo miden new:
cargo miden new my-project # a full project scaffold
cargo miden new my-account --account # a single account component
The available templates are --account, --note, --tx-script,
--auth-component, and --program. Their sources live in this repository under
extra/templates
and are released independently of the compiler, so cargo miden new picks up
template updates without you reinstalling cargo-miden.