waffle/src/bin/waffle-util.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

//! WAFFLE command-line tool.
use anyhow::Result;
use log::debug;
use std::path::PathBuf;
use structopt::StructOpt;
2021-11-13 19:52:30 -06:00
use waffle::Module;
#[derive(Debug, StructOpt)]
#[structopt(name = "waffle-util", about = "WAFFLE utility.")]
struct Options {
#[structopt(short, long)]
debug: bool,
#[structopt(subcommand)]
command: Command,
}
#[derive(Debug, StructOpt)]
enum Command {
#[structopt(name = "print-ir", about = "Parse Wasm and print resulting IR")]
PrintIR {
#[structopt(help = "Wasm file to parse")]
wasm: PathBuf,
},
2021-12-24 16:12:49 -06:00
#[structopt(name = "roundtrip", about = "Round-trip Wasm through IR")]
RoundTrip {
#[structopt(help = "Wasm file to parse", short = "i")]
input: PathBuf,
#[structopt(help = "Wasm file to produce", short = "o")]
output: PathBuf,
},
}
fn main() -> Result<()> {
let opts = Options::from_args();
let mut logger = env_logger::Builder::from_default_env();
if opts.debug {
logger.filter_level(log::LevelFilter::Debug);
}
let _ = logger.try_init();
match opts.command {
Command::PrintIR { wasm } => {
let bytes = std::fs::read(wasm)?;
debug!("Loaded {} bytes of Wasm data", bytes.len());
2021-11-13 19:52:30 -06:00
let module = Module::from_wasm_bytes(&bytes[..])?;
println!("{:?}", module);
}
2021-12-24 16:12:49 -06:00
Command::RoundTrip { input, output } => {
let bytes = std::fs::read(input)?;
debug!("Loaded {} bytes of Wasm data", bytes.len());
let module = Module::from_wasm_bytes(&bytes[..])?;
let produced = module.to_wasm_bytes();
std::fs::write(output, &produced[..])?;
}
}
Ok(())
}