Remove all Sink traits
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
//! CLI tool to read Cranelift IR files and compile them into native code.
|
||||
|
||||
use crate::disasm::{print_all, PrintRelocs, PrintStackMaps, PrintTraps};
|
||||
use crate::disasm::print_all;
|
||||
use crate::utils::{parse_sets_and_triple, read_to_string};
|
||||
use anyhow::{Context as _, Result};
|
||||
use cranelift_codegen::binemit::{RelocSink, StackMapSink, TrapSink};
|
||||
use cranelift_codegen::print_errors::pretty_error;
|
||||
use cranelift_codegen::settings::FlagsOrIsa;
|
||||
use cranelift_codegen::timing;
|
||||
use cranelift_codegen::Context;
|
||||
use cranelift_codegen::{timing, MachReloc, MachStackMap, MachTrap};
|
||||
use cranelift_reader::{parse_test, ParseOptions};
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
@@ -69,10 +68,6 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
|
||||
};
|
||||
|
||||
for (func, _) in test_file.functions {
|
||||
let mut relocs = PrintRelocs::new(options.print);
|
||||
let mut traps = PrintTraps::new(options.print);
|
||||
let mut stack_maps = PrintStackMaps::new(options.print);
|
||||
|
||||
if let Some(isa) = isa {
|
||||
let mut context = Context::new();
|
||||
context.func = func;
|
||||
@@ -84,32 +79,6 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
|
||||
.map_err(|err| anyhow::anyhow!("{}", pretty_error(&context.func, err)))?;
|
||||
let result = context.mach_compile_result.as_ref().unwrap();
|
||||
let code_info = result.code_info();
|
||||
for &MachReloc {
|
||||
offset,
|
||||
srcloc,
|
||||
kind,
|
||||
ref name,
|
||||
addend,
|
||||
} in result.buffer.relocs()
|
||||
{
|
||||
relocs.reloc_external(offset, srcloc, kind, name, addend);
|
||||
}
|
||||
for &MachTrap {
|
||||
offset,
|
||||
srcloc,
|
||||
code,
|
||||
} in result.buffer.traps()
|
||||
{
|
||||
traps.trap(offset, srcloc, code);
|
||||
}
|
||||
for &MachStackMap {
|
||||
offset_end,
|
||||
ref stack_map,
|
||||
..
|
||||
} in result.buffer.stack_maps()
|
||||
{
|
||||
stack_maps.add_stack_map(offset_end, stack_map.clone());
|
||||
}
|
||||
|
||||
if options.print {
|
||||
println!("{}", context.func.display());
|
||||
@@ -120,9 +89,10 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
|
||||
isa,
|
||||
&mem,
|
||||
code_info.total_size,
|
||||
&relocs,
|
||||
&traps,
|
||||
&stack_maps,
|
||||
options.print,
|
||||
result.buffer.relocs(),
|
||||
result.buffer.traps(),
|
||||
result.buffer.stack_maps(),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,85 +1,53 @@
|
||||
use anyhow::Result;
|
||||
use cfg_if::cfg_if;
|
||||
use cranelift_codegen::isa::TargetIsa;
|
||||
use cranelift_codegen::{binemit, ir};
|
||||
use cranelift_codegen::{MachReloc, MachStackMap, MachTrap};
|
||||
use std::fmt::Write;
|
||||
|
||||
pub struct PrintRelocs {
|
||||
pub flag_print: bool,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl PrintRelocs {
|
||||
pub fn new(flag_print: bool) -> Self {
|
||||
Self {
|
||||
flag_print,
|
||||
text: String::new(),
|
||||
}
|
||||
pub fn print_relocs(relocs: &[MachReloc]) -> String {
|
||||
let mut text = String::new();
|
||||
for &MachReloc {
|
||||
kind,
|
||||
offset,
|
||||
srcloc: _,
|
||||
ref name,
|
||||
addend,
|
||||
} in relocs
|
||||
{
|
||||
writeln!(
|
||||
text,
|
||||
"reloc_external: {} {} {} at {}",
|
||||
kind, name, addend, offset
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
impl binemit::RelocSink for PrintRelocs {
|
||||
fn reloc_external(
|
||||
&mut self,
|
||||
where_: binemit::CodeOffset,
|
||||
_srcloc: ir::SourceLoc,
|
||||
r: binemit::Reloc,
|
||||
name: &ir::ExternalName,
|
||||
addend: binemit::Addend,
|
||||
) {
|
||||
if self.flag_print {
|
||||
writeln!(
|
||||
&mut self.text,
|
||||
"reloc_external: {} {} {} at {}",
|
||||
r, name, addend, where_
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
pub fn print_traps(traps: &[MachTrap]) -> String {
|
||||
let mut text = String::new();
|
||||
for &MachTrap {
|
||||
offset,
|
||||
srcloc: _,
|
||||
code,
|
||||
} in traps
|
||||
{
|
||||
writeln!(text, "trap: {} at {}", code, offset).unwrap();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
pub struct PrintTraps {
|
||||
pub flag_print: bool,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl PrintTraps {
|
||||
pub fn new(flag_print: bool) -> Self {
|
||||
Self {
|
||||
flag_print,
|
||||
text: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl binemit::TrapSink for PrintTraps {
|
||||
fn trap(&mut self, offset: binemit::CodeOffset, _srcloc: ir::SourceLoc, code: ir::TrapCode) {
|
||||
if self.flag_print {
|
||||
writeln!(&mut self.text, "trap: {} at {}", code, offset).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrintStackMaps {
|
||||
pub flag_print: bool,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl PrintStackMaps {
|
||||
pub fn new(flag_print: bool) -> Self {
|
||||
Self {
|
||||
flag_print,
|
||||
text: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl binemit::StackMapSink for PrintStackMaps {
|
||||
fn add_stack_map(&mut self, offset: binemit::CodeOffset, _: binemit::StackMap) {
|
||||
if self.flag_print {
|
||||
writeln!(&mut self.text, "add_stack_map at {}", offset).unwrap();
|
||||
}
|
||||
pub fn print_stack_maps(traps: &[MachStackMap]) -> String {
|
||||
let mut text = String::new();
|
||||
for &MachStackMap {
|
||||
offset,
|
||||
offset_end: _,
|
||||
stack_map: _,
|
||||
} in traps
|
||||
{
|
||||
writeln!(text, "add_stack_map at {}", offset).unwrap();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
@@ -193,13 +161,21 @@ pub fn print_all(
|
||||
isa: &dyn TargetIsa,
|
||||
mem: &[u8],
|
||||
code_size: u32,
|
||||
relocs: &PrintRelocs,
|
||||
traps: &PrintTraps,
|
||||
stack_maps: &PrintStackMaps,
|
||||
print: bool,
|
||||
relocs: &[MachReloc],
|
||||
traps: &[MachTrap],
|
||||
stack_maps: &[MachStackMap],
|
||||
) -> Result<()> {
|
||||
print_bytes(&mem);
|
||||
print_disassembly(isa, &mem[0..code_size as usize])?;
|
||||
println!("\n{}\n{}\n{}", &relocs.text, &traps.text, &stack_maps.text);
|
||||
if print {
|
||||
println!(
|
||||
"\n{}\n{}\n{}",
|
||||
print_relocs(relocs),
|
||||
print_traps(traps),
|
||||
print_stack_maps(stack_maps)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -7,15 +7,14 @@
|
||||
allow(clippy::too_many_arguments, clippy::cognitive_complexity)
|
||||
)]
|
||||
|
||||
use crate::disasm::{print_all, PrintRelocs, PrintStackMaps, PrintTraps};
|
||||
use crate::disasm::print_all;
|
||||
use crate::utils::parse_sets_and_triple;
|
||||
use anyhow::{Context as _, Result};
|
||||
use cranelift_codegen::binemit::{RelocSink, StackMapSink, TrapSink};
|
||||
use cranelift_codegen::ir::DisplayFunctionAnnotations;
|
||||
use cranelift_codegen::print_errors::{pretty_error, pretty_verifier_error};
|
||||
use cranelift_codegen::settings::FlagsOrIsa;
|
||||
use cranelift_codegen::timing;
|
||||
use cranelift_codegen::Context;
|
||||
use cranelift_codegen::{timing, MachReloc, MachStackMap, MachTrap};
|
||||
use cranelift_entity::EntityRef;
|
||||
use cranelift_wasm::{translate_module, DummyEnvironment, FuncIndex, ReturnMode};
|
||||
use std::io::Read;
|
||||
@@ -259,45 +258,17 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
|
||||
let mut saved_size = None;
|
||||
let func_index = num_func_imports + def_index.index();
|
||||
let mut mem = vec![];
|
||||
let mut relocs = PrintRelocs::new(options.print);
|
||||
let mut traps = PrintTraps::new(options.print);
|
||||
let mut stack_maps = PrintStackMaps::new(options.print);
|
||||
if options.check_translation {
|
||||
let (relocs, traps, stack_maps) = if options.check_translation {
|
||||
if let Err(errors) = context.verify(fisa) {
|
||||
anyhow::bail!("{}", pretty_verifier_error(&context.func, None, errors));
|
||||
}
|
||||
(vec![], vec![], vec![])
|
||||
} else {
|
||||
context
|
||||
.compile_and_emit(isa, &mut mem)
|
||||
.map_err(|err| anyhow::anyhow!("{}", pretty_error(&context.func, err)))?;
|
||||
let result = context.mach_compile_result.as_ref().unwrap();
|
||||
let code_info = result.code_info();
|
||||
for &MachReloc {
|
||||
offset,
|
||||
srcloc,
|
||||
kind,
|
||||
ref name,
|
||||
addend,
|
||||
} in result.buffer.relocs()
|
||||
{
|
||||
relocs.reloc_external(offset, srcloc, kind, name, addend);
|
||||
}
|
||||
for &MachTrap {
|
||||
offset,
|
||||
srcloc,
|
||||
code,
|
||||
} in result.buffer.traps()
|
||||
{
|
||||
traps.trap(offset, srcloc, code);
|
||||
}
|
||||
for &MachStackMap {
|
||||
offset_end,
|
||||
ref stack_map,
|
||||
..
|
||||
} in result.buffer.stack_maps()
|
||||
{
|
||||
stack_maps.add_stack_map(offset_end, stack_map.clone());
|
||||
}
|
||||
|
||||
if options.print_size {
|
||||
println!(
|
||||
@@ -315,7 +286,12 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
|
||||
if options.disasm {
|
||||
saved_size = Some(code_info.total_size);
|
||||
}
|
||||
}
|
||||
(
|
||||
result.buffer.relocs().to_vec(),
|
||||
result.buffer.traps().to_vec(),
|
||||
result.buffer.stack_maps().to_vec(),
|
||||
)
|
||||
};
|
||||
|
||||
if options.print {
|
||||
vprintln!(options.verbose, "");
|
||||
@@ -351,7 +327,15 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
|
||||
}
|
||||
|
||||
if let Some(total_size) = saved_size {
|
||||
print_all(isa, &mem, total_size, &relocs, &traps, &stack_maps)?;
|
||||
print_all(
|
||||
isa,
|
||||
&mem,
|
||||
total_size,
|
||||
options.print,
|
||||
&relocs,
|
||||
&traps,
|
||||
&stack_maps,
|
||||
)?;
|
||||
}
|
||||
|
||||
context.clear();
|
||||
|
||||
Reference in New Issue
Block a user