Skip to content

Commit

Permalink
Add basic pulley disassembly support to clif-util (#9721)
Browse files Browse the repository at this point in the history
* Add basic pulley disassembly support to `clif-util`

Helpful when running `clif-util compile -D --target pulley64` over the
input.

* Fix non-pulley build
  • Loading branch information
alexcrichton authored Dec 4, 2024
1 parent 350a6e2 commit f40e53e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ rustc-hash = { workspace = true }
# Note that this just enables `trace-log` for `clif-util` and doesn't turn it on
# for all of Cranelift, which would be bad.
regalloc2 = { workspace = true, features = ["trace-log"] }
pulley-interpreter = { workspace = true, optional = true }

[features]
default = [
Expand All @@ -64,3 +65,4 @@ disas = ["capstone"]
souper-harvest = ["cranelift-codegen/souper-harvest", "rayon"]
all-arch = ["cranelift-codegen/all-arch"]
all-native-arch = ["cranelift-codegen/all-native-arch"]
pulley = ['cranelift-codegen/pulley', 'dep:pulley-interpreter']
15 changes: 14 additions & 1 deletion cranelift/src/disasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,22 @@ pub fn print_traps(traps: &[MachTrap]) -> String {
cfg_if! {
if #[cfg(feature = "disas")] {
pub fn print_disassembly(func: &Function, isa: &dyn TargetIsa, mem: &[u8]) -> Result<()> {
#[cfg(feature = "pulley")]
let is_pulley = match isa.triple().architecture {
target_lexicon::Architecture::Pulley32 | target_lexicon::Architecture::Pulley64 => true,
_ => false,
};
println!("\nDisassembly of {} bytes <{}>:", mem.len(), func.name);

#[cfg(feature = "pulley")]
if is_pulley {
let mut disas = pulley_interpreter::disas::Disassembler::new(mem);
pulley_interpreter::decode::Decoder::decode_all(&mut disas)?;
println!("{}", disas.disas());
return Ok(());
}
let cs = isa.to_capstone().map_err(|e| anyhow::format_err!("{}", e))?;

println!("\nDisassembly of {} bytes <{}>:", mem.len(), func.name);
let insns = cs.disasm_all(&mem, 0x0).unwrap();
for i in insns.iter() {
let mut line = String::new();
Expand Down
2 changes: 1 addition & 1 deletion pulley/src/disas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<'a> OpVisitor for Disassembler<'a> {
write!(&mut self.disas, "{space}{byte:02x}").unwrap();
need_space = true;
}
for _ in 0..11_usize.saturating_sub(size) {
for _ in 0..12_usize.saturating_sub(size) {
write!(&mut self.disas, " ").unwrap();
}
}
Expand Down

0 comments on commit f40e53e

Please sign in to comment.