Add a --disasm option to clif-util wasm and compile (#713)

- Both the `wasm` and `compile` commands get this new subcommand, and it defaults to false.  This means that test runs with `wasm` can request disassembly (the main reason I am doing this) while test runs with `compile` now must request it, this changes current behavior.
- Switch to using context.compile_and_emit directly, and make the reloc and trap printers just accumulate output, not print it.  This allows us to factor the printing code into the disasm module.
This commit is contained in:
Lars T Hansen
2019-03-27 12:57:13 +01:00
committed by Benjamin Bouvier
parent 82c6867155
commit 141ccb9e9d
5 changed files with 283 additions and 186 deletions

View File

@@ -7,6 +7,7 @@
allow(clippy::too_many_arguments, clippy::cyclomatic_complexity)
)]
use crate::disasm::{print_all, PrintRelocs, PrintTraps};
use crate::utils::{parse_sets_and_triple, read_to_end};
use cranelift_codegen::print_errors::{pretty_error, pretty_verifier_error};
use cranelift_codegen::settings::FlagsOrIsa;
@@ -41,6 +42,7 @@ pub fn run(
flag_just_decode: bool,
flag_check_translation: bool,
flag_print: bool,
flag_print_disasm: bool,
flag_set: &[String],
flag_triple: &str,
flag_print_size: bool,
@@ -57,6 +59,7 @@ pub fn run(
flag_check_translation,
flag_print,
flag_print_size,
flag_print_disasm,
flag_report_times,
&path.to_path_buf(),
&name,
@@ -72,6 +75,7 @@ fn handle_module(
flag_check_translation: bool,
flag_print: bool,
flag_print_size: bool,
flag_print_disasm: bool,
flag_report_times: bool,
path: &PathBuf,
name: &str,
@@ -100,7 +104,7 @@ fn handle_module(
None => {
return Err(String::from(
"Error: the wasm command requires an explicit isa.",
))
));
}
};
@@ -157,27 +161,36 @@ fn handle_module(
for (def_index, func) in dummy_environ.info.function_bodies.iter() {
context.func = func.clone();
let mut saved_sizes = None;
let func_index = num_func_imports + def_index.index();
let mut mem = vec![];
let mut relocs = PrintRelocs::new(flag_print);
let mut traps = PrintTraps::new(flag_print);
if flag_check_translation {
if let Err(errors) = context.verify(fisa) {
return Err(pretty_verifier_error(&context.func, fisa.isa, None, errors));
}
} else {
let compiled_size = context
.compile(isa)
let (code_size, rodata_size) = context
.compile_and_emit(isa, &mut mem, &mut relocs, &mut traps)
.map_err(|err| pretty_error(&context.func, fisa.isa, err))?;
if flag_print_size {
println!(
"Function #{} code size: {} bytes",
func_index, compiled_size
func_index, code_size + rodata_size
);
total_module_code_size += compiled_size;
total_module_code_size += code_size + rodata_size;
println!(
"Function #{} bytecode size: {} bytes",
func_index,
dummy_environ.func_bytecode_sizes[def_index.index()]
);
}
if flag_print_disasm {
saved_sizes = Some((code_size, rodata_size));
}
}
if flag_print {
@@ -196,6 +209,10 @@ fn handle_module(
vprintln!(flag_verbose, "");
}
if let Some((code_size, rodata_size)) = saved_sizes {
print_all(isa, &mem, code_size, rodata_size, &relocs, &traps)?;
}
context.clear();
}