Skip to content

Conversation

@greenhat
Copy link
Contributor

@greenhat greenhat commented Dec 23, 2025

Close #724
Close #816

This PR:

  • Fixes --emit=hir and masm and preserves Miden package output location;
  • Adds the --emit=wat[=<path>] option to emit wat files;
  • Adds the --emit=inter[=<path>] option to emit wat, hir, masm files;
  • Pass all the cargo miden build options to the midenc (instead of only --emit);
  • Adds MIDENC_EXPAND=[<path>] env var handling in the compiler test suite to emit Rust code with expanded macros;

@greenhat greenhat marked this pull request as ready for review December 26, 2025 15:10
@greenhat greenhat requested a review from bitwalker December 26, 2025 15:10
Copy link
Collaborator

@bitwalker bitwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I have a few minor things I'd like to tweak, but overall I think this should do nicely 👍

/// working directory. When set to a non-empty value other than `1`, it is treated as the output
/// directory.
fn maybe_dump_cargo_expand(test: &CargoTest, rustflags_env: Option<&str>) {
let Some(value) = std::env::var_os("MIDENC_EXPAND") else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's more verbose, but could we use MIDENC_EMIT_MACRO_EXPAND for this? For a couple of reasons:

  • It keeps our environment variables a bit more structured, and eases future additions.
  • It is much clearer what the purpose of it is, and it's relationship to --emit
  • LLMs can probably better understand it. Which isn't a priority for me, but doesn't hurt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done.


/// Returns the subset of [OutputType] values considered "intermediate" for convenience
/// emission (WAT, HIR, MASM).
pub fn inter() -> [OutputType; 3] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer just ir for this (since all of these are intermediate representations, it's better shorthand, and easier to remember than "inter" IMO).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Funnily enough, initially I thought about naming it ir but then I decided to go with inter to emphasize that this is a group of multiple intermediate formats and not the HIR representation.


/// Returns the subset of [OutputType] values considered "intermediate" for convenience
/// emission (WAT, HIR, MASM).
pub fn inter() -> [OutputType; 3] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make the return type &'static [OutputType] here, and mark it const, that way we don't have to hardcode the size of the array in the signature, and it's just as useful at compile-time as an array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/// Emit a common set of intermediate artifacts (WAT, HIR, MASM) into a directory.
///
/// The directory path is interpreted relative to the session working directory.
Inter {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest we name this variant Subset, and definite it like so:

enum OutputTypeSpec {
    // ...
    Subset {
        output_types: SmallVec<[OutputType; 3]>,
        path: Option<OutputFile>,
    },
    // ...
}

This makes OutputTypeSpec a bit more versatile, and doesn't bake into it the assumption that emitting multiple outputs has to be just intermediate representations, but any combination of two or more output types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Done.

PossibleValue::new("mast").help("Merkelized Abstract Syntax Tree (text)"),
PossibleValue::new("masl").help("Merkelized Abstract Syntax Tree (binary)"),
PossibleValue::new("masp").help("Miden Assembly Package Format (binary)"),
PossibleValue::new("inter").help("WAT + HIR + MASM (text, optional directory)"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per my other comment, let's name this value ir instead of inter

if output_types.is_empty() {
let has_final_output = output_types
.keys()
.any(|ty| matches!(ty, OutputType::Mast | OutputType::Masl | OutputType::Masp));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove the Masl output type - it is no longer useful, and I'm in the process of removing Library entirely from the assembler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


use super::*;

/// A wrapper that emits WebAssembly text format (WAT).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Probably better to define this in the midenc-frontend-wasm crate, and it'll reduce the clutter in this file as an extra benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

///
/// The format matches the [core::fmt::Display] implementation for [MasmComponent], which is
/// defined in `codegen/masm/src/artifact.rs`.
struct MasmComponentEmit<'a>(&'a MasmComponent);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to WasmEmit, let's move this to the midenc-codegen-masm crate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@greenhat
Copy link
Contributor Author

Rebased and ready for another round.

@greenhat
Copy link
Contributor Author

Don't merge before #810 . I removed merge_midenc_flags in this PR and it is changed in #810. I need to port changes first.

@greenhat
Copy link
Contributor Author

Rebased again after #810 was merged. Ready for merge.

@greenhat greenhat merged commit e8de1ae into next Dec 30, 2025
15 checks passed
@greenhat greenhat deleted the i724-fix-emit branch December 30, 2025 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IR dumping infrastructure (stop expect tests abuse) midenc option --emit hir=./target does not emit when passed along with -o

3 participants