Skip to content

WIP: Add MASM dispaly in cargo miden #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/usage/cargo-miden.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ See `midenc run --help` for the inputs file format.

## Examples

Check out the [examples](https://github.com/0xPolygonMiden/compiler/tree/next/examples) for some `cargo-miden` project examples.
Check out the [examples](https://github.com/0xMiden/compiler/tree/next/examples) for some `cargo-miden` project examples.
31 changes: 23 additions & 8 deletions midenc-compile/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,19 +461,34 @@ impl Compiler {
log::trace!(target: "driver", "current working directory = {}", cwd.display());

// Determine if a specific output file has been requested
let output_file = match self.output_file {
Some(path) => Some(OutputFile::Real(path)),
None if self.stdout => Some(OutputFile::Stdout),
None => None,
std::dbg!(&self.stdout);
std::dbg!(&self.output_file);
let output_files = match (self.output_file, self.stdout) {
(Some(path), false) => Some(vec![OutputFile::Real(path)]),
(None, true) => Some(vec![OutputFile::Stdout]),
(Some(path), true) => Some(vec![OutputFile::Real(path), OutputFile::Stdout]),
_ => None,
};

// Initialize output types
let mut output_types = OutputTypes::new(self.output_types).unwrap_or_else(|err| err.exit());
if output_types.is_empty() {
output_types.insert(OutputType::Masp, output_file.clone());
} else if output_file.is_some() && output_types.get(&OutputType::Masp).is_some() {
if let Some(ref output_file) = output_files {
for file in output_file {
output_types.insert(OutputType::Masp, Some(file.clone()));
}
} else {
output_types.insert(OutputType::Masp, None);
}
} else if output_files.is_some() && output_types.get(&OutputType::Masp).is_some() {
// The -o flag overrides --emit
output_types.insert(OutputType::Masp, output_file.clone());
if let Some(ref output_file) = output_files {
for file in output_file {
output_types.insert(OutputType::Masp, Some(file.clone()));
}
} else {
output_types.insert(OutputType::Masp, None);
}
}

// Convert --exe or --lib to project type
Expand Down Expand Up @@ -518,7 +533,7 @@ impl Compiler {
Session::new(
inputs,
self.output_dir,
output_file,
output_files,
target_dir,
options,
emitter,
Expand Down
29 changes: 23 additions & 6 deletions midenc-session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Session {
pub fn new<I>(
inputs: I,
output_dir: Option<PathBuf>,
output_file: Option<OutputFile>,
output_files: Option<Vec<OutputFile>>,
target_dir: PathBuf,
options: Options,
emitter: Option<Arc<dyn Emitter>>,
Expand All @@ -130,13 +130,13 @@ impl Session {
{
let inputs = inputs.into_iter().collect::<Vec<_>>();

Self::make(inputs, output_dir, output_file, target_dir, options, emitter, source_manager)
Self::make(inputs, output_dir, output_files, target_dir, options, emitter, source_manager)
}

fn make(
inputs: Vec<InputFile>,
output_dir: Option<PathBuf>,
output_file: Option<OutputFile>,
output_files: Option<Vec<OutputFile>>,
target_dir: PathBuf,
options: Options,
emitter: Option<Arc<dyn Emitter>>,
Expand All @@ -156,9 +156,21 @@ impl Session {
.unwrap_or("<unset>".to_string())
);
log::debug!(
target: "driver",
" | output_file = {}",
output_file.as_ref().map(|of| of.to_string()).unwrap_or("<unset>".to_string())
target: "driver",
" | output_files = {}",
output_files.as_ref()
.map(|output_files| {
output_files
.iter()
.map(|of| of.to_string())
.reduce(|mut acc, e| {
acc.push_str(", ");
acc.push_str(e.as_str());
acc
})
.unwrap_or("<unset>".to_string())
})
.unwrap_or("<unset>".to_string())
);
log::debug!(target: "driver", " | target_dir = {}", target_dir.display());
}
Expand All @@ -168,6 +180,11 @@ impl Session {
emitter.unwrap_or_else(|| options.default_emitter()),
));

let output_file: Option<OutputFile> = if let Some(output_files) = output_files {
output_files.into_iter().filter(|of| of.parent().is_some()).reduce(|acc, _| acc)
} else {
None
};
let output_dir = output_dir
.as_deref()
.or_else(|| output_file.as_ref().and_then(|of| of.parent()))
Expand Down
2 changes: 1 addition & 1 deletion tools/cargo-miden/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

This crate provides a cargo extension that allows you to compile Rust code to Miden VM MASM.

See the [documentation](/docs/usage/cargo-miden.md) for more details.
See the [documentation](/docs/src/usage/cargo-miden.md) for more details.
7 changes: 7 additions & 0 deletions tools/cargo-miden/src/compile_masm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn wasm_to_masm(
wasm_file_path: &Path,
output_folder: &Path,
is_bin: bool,
flags: &Option<Vec<String>>,
) -> Result<PathBuf, Report> {
if !output_folder.exists() {
return Err(Report::msg(format!(
Expand Down Expand Up @@ -52,6 +53,12 @@ pub fn wasm_to_masm(
args.push(entrypoint_opt.as_ref());
}

if let Some(flags) = flags {
for flag in flags {
args.push(flag.as_ref())
}
}

let session = Rc::new(Compiler::new_session([input], None, args));
let context = Rc::new(Context::new(session));
midenc_compile::compile(context.clone())?;
Expand Down
31 changes: 29 additions & 2 deletions tools/cargo-miden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ enum Command {
New(NewCommand),
}

fn detect_flags<I, T>(args: I) -> Option<Vec<String>>
where
I: IntoIterator<Item = T>,
T: Into<String> + Clone,
{
let iter = args.into_iter().map(Into::into).skip_while(|arg| arg != "--");
let mut flags = Vec::new();
for item in iter {
if item == "--" {
continue;
} else if !item.starts_with("-") {
break;
} else if !item.starts_with("--") {
break;
};
flags.push(item);
}

if !flags.is_empty() {
Some(flags)
} else {
None
}
}

fn detect_subcommand<I, T>(args: I) -> Option<String>
where
I: IntoIterator<Item = T>,
Expand Down Expand Up @@ -106,6 +131,7 @@ where
// The first argument is the cargo-miden binary path
let args = args.skip_while(|arg| arg != "miden").collect::<Vec<_>>();
let subcommand = detect_subcommand(args.clone());
let flags = detect_flags(args.clone());

let outputs = match subcommand.as_deref() {
// Check for built-in command or no command (shows help)
Expand Down Expand Up @@ -261,8 +287,9 @@ where
// so far, we only support the Miden VM programs, unless `--lib` is
// specified (in our integration tests)
let is_bin = !args.contains(&"--lib".to_string());
let output = wasm_to_masm(&wasm, miden_out_dir.as_std_path(), is_bin)
.map_err(|e| anyhow::anyhow!("{e}"))?;
let output =
wasm_to_masm(&wasm, miden_out_dir.as_std_path(), is_bin, &flags)
.map_err(|e| anyhow::anyhow!("{e}"))?;
outputs.push(output);
}
outputs
Expand Down