From 1311069c6a5cabf5411669388baff5a71c58343b Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sat, 1 Apr 2023 00:00:48 -0700 Subject: [PATCH] Add print-func command to waffle-util. --- src/bin/waffle-util.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/bin/waffle-util.rs b/src/bin/waffle-util.rs index 5f1c7e3..2f13230 100644 --- a/src/bin/waffle-util.rs +++ b/src/bin/waffle-util.rs @@ -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());