use gumdrop::Options;
#[derive(Debug, Options)]
struct MyOptions {
#[options()]
help: bool,
#[options(default = "true")]
allow_pseudo: bool,
#[options(free)]
input: String,
#[options()]
output: Option<String>,
}
impl MyOptions {
fn new() -> Self {
let mut opts = MyOptions::parse_args_default_or_exit();
opts.resolve_extras();
opts
}
fn resolve_extras(&mut self) {
use std::path::Path;
if self.output.is_none() {
let input_path: &Path = &Path::new(&self.input);
let file_stem: &str = input_path
.file_stem()
.expect("Failed to find file stem of input file")
.to_str()
.expect("file stem of input file wasn't valid utf8");
self.output = Some(format!("./{}.s", file_stem));
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let opts = MyOptions::new();
dbg!(&opts);
let code: Vec<u32> = riscv_asm::parse_elf_from_path(&opts.input)?;
println!("ASM:");
for (idx, word) in code.iter().cloned().enumerate() {
print!(" 0x{:>03x}: ", std::mem::size_of::<u32>() * idx);
for byte in word.to_le_bytes().iter() {
print!("{:02x} ", byte);
}
let o_instr = riscv_asm::decode_opcode(word);
let instr_text = if let Some(instr) = o_instr {
format!("{:?}", instr)
} else {
format!("0x{:08}, 0b_{:b}", word, word)
};
print!(" {:<25}", instr_text);
println!();
}
println!();
Ok(())
}