Add print-func command to waffle-util.

This commit is contained in:
Chris Fallin 2023-04-01 00:00:48 -07:00
parent e84a5368ef
commit 1311069c6a

View file

@ -5,7 +5,7 @@ use log::debug;
use std::path::PathBuf;
use structopt::StructOpt;
use waffle::InterpContext;
use waffle::{FrontendOptions, Module};
use waffle::{entity::EntityRef, FrontendOptions, Func, Module};
#[derive(Debug, StructOpt)]
#[structopt(name = "waffle-util", about = "WAFFLE utility.")]
@ -40,6 +40,13 @@ enum Command {
#[structopt(help = "Wasm file to parse")]
wasm: PathBuf,
},
#[structopt(name = "print-func", about = "Parse Wasm and print one function body")]
PrintFunc {
#[structopt(help = "Wasm file to parse")]
wasm: PathBuf,
#[structopt(help = "Index of Wasm function to print")]
func: usize,
},
#[structopt(name = "roundtrip", about = "Round-trip Wasm through IR")]
RoundTrip {
#[structopt(help = "Wasm file to parse", short = "i")]
@ -85,6 +92,19 @@ fn main() -> Result<()> {
apply_options(&opts, &mut module)?;
println!("{}", module.display());
}
Command::PrintFunc { wasm, func } => {
let bytes = std::fs::read(wasm)?;
debug!("Loaded {} bytes of Wasm data", bytes.len());
let mut module = Module::from_wasm_bytes(&bytes[..], &options)?;
apply_options(&opts, &mut module)?;
println!(
"{}",
module.funcs[Func::new(*func)]
.body()
.unwrap()
.display_verbose("", Some(&module))
);
}
Command::RoundTrip { input, output } => {
let bytes = std::fs::read(input)?;
debug!("Loaded {} bytes of Wasm data", bytes.len());