From 3f4175478c7189a25f81d1f87ccaa23602cc4362 Mon Sep 17 00:00:00 2001 From: angie Date: Wed, 4 Dec 2024 21:07:24 -0300 Subject: [PATCH] wiring up with python --- Cargo.lock | 2 +- src/spimdisasm/Cargo.toml | 1 + .../src/context/builder/context_builder.rs | 79 +++++++++++++++++++ .../builder/context_builder_finder_heater.rs | 46 +++++++++++ src/spimdisasm/src/lib.rs | 1 + .../src/relocation/relocation_info.rs | 4 +- src/spimdisasm/src/size.rs | 2 +- .../src/symbols/display/function_display.rs | 9 +++ .../src/symbols/display/sym_noload_display.rs | 2 +- 9 files changed, 141 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4ef179..2eeb26c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,7 +168,7 @@ dependencies = [ [[package]] name = "rabbitizer" version = "2.0.0-dev0" -source = "git+https://github.com/Decompollaborate/rabbitizer.git?branch=%F0%9F%A6%80#677adc0ed8262663bdec76089680d34752f319c2" +source = "git+https://github.com/Decompollaborate/rabbitizer.git?branch=%F0%9F%A6%80#a10119e32be2e3be246179eac3dc20a4e0d035cc" dependencies = [ "bitflags", ] diff --git a/src/spimdisasm/Cargo.toml b/src/spimdisasm/Cargo.toml index 5009d86..52f3494 100644 --- a/src/spimdisasm/Cargo.toml +++ b/src/spimdisasm/Cargo.toml @@ -29,6 +29,7 @@ pyo3 = ["dep:pyo3", "std", "rabbitizer/std"] # TODO: "rabbitizer/pyo3" [dependencies] rabbitizer = { git = "https://github.com/Decompollaborate/rabbitizer.git", branch = "🦀" } +# rabbitizer = { path = "../../../rabbitizer/src/rabbitizer" } polonius-the-crab = "0.4.2" # TODO: get rid of the polonius stuff when the new borrow checker has been released. pyo3 = { version = "0.23.2", optional = true } # bitflags = { version = "2.6.0", default-features = false } diff --git a/src/spimdisasm/src/context/builder/context_builder.rs b/src/spimdisasm/src/context/builder/context_builder.rs index ae97d6e..9ed6faa 100644 --- a/src/spimdisasm/src/context/builder/context_builder.rs +++ b/src/spimdisasm/src/context/builder/context_builder.rs @@ -40,6 +40,10 @@ impl ContextBuilder { #[cfg(feature = "pyo3")] pub(crate) mod python_bindings { + use rabbitizer::Vram; + + use crate::rom_address::RomAddress; + use super::*; #[pymethods] @@ -54,6 +58,81 @@ pub(crate) mod python_bindings { // pub fn py_global_segment(&mut self) -> SegmentModifier { // self.global_segment() // } + #[pyo3(signature = (name, vram, rom))] + pub fn add_symbol( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment().add_symbol(name, Vram::new(vram), rom); + } + + #[pyo3(signature = (name, vram, rom))] + pub fn add_function( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment() + .add_function(name, Vram::new(vram), rom); + } + + #[pyo3(signature = (name, vram, rom))] + pub fn add_branch_label( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment() + .add_branch_label(name, Vram::new(vram), rom); + } + + #[pyo3(signature = (name, vram, rom))] + pub fn add_jumptable( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment() + .add_jumptable(name, Vram::new(vram), rom); + } + + #[pyo3(signature = (name, vram, rom))] + pub fn add_jumptable_label( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment() + .add_jumptable_label(name, Vram::new(vram), rom); + } + + #[pyo3(signature = (name, vram, rom))] + pub fn add_gcc_except_table( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment() + .add_gcc_except_table(name, Vram::new(vram), rom); + } + + #[pyo3(signature = (name, vram, rom))] + pub fn add_gcc_except_table_label( + &mut self, + name: String, + vram: u32, // Vram // TODO + rom: Option, + ) { + self.global_segment() + .add_gcc_except_table_label(name, Vram::new(vram), rom); + } #[pyo3(name = "process")] pub fn py_process(&self) -> ContextBuilderOverlay { diff --git a/src/spimdisasm/src/context/builder/context_builder_finder_heater.rs b/src/spimdisasm/src/context/builder/context_builder_finder_heater.rs index 815811e..88892a9 100644 --- a/src/spimdisasm/src/context/builder/context_builder_finder_heater.rs +++ b/src/spimdisasm/src/context/builder/context_builder_finder_heater.rs @@ -94,10 +94,56 @@ impl ContextBuilderFinderHeater { #[cfg(feature = "pyo3")] pub(crate) mod python_bindings { + use std::borrow::Cow; + use super::*; #[pymethods] impl ContextBuilderFinderHeater { + #[pyo3(name = "preanalyze_text")] + pub fn py_preanalyze_text( + &mut self, + settings: &SectionExecutableSettings, + raw_bytes: Cow<[u8]>, + rom: RomAddress, + vram: u32, // Vram, // TODO + ) { + self.preanalyze_text(settings, &raw_bytes, rom, Vram::new(vram)); + } + + #[pyo3(name = "preanalyze_data")] + pub fn py_preanalyze_data( + &mut self, + settings: &SectionDataSettings, + raw_bytes: Cow<[u8]>, + rom: RomAddress, + vram: u32, // Vram, // TODO + ) { + self.preanalyze_data(settings, &raw_bytes, rom, Vram::new(vram)); + } + + #[pyo3(name = "preanalyze_rodata")] + pub fn py_preanalyze_rodata( + &mut self, + settings: &SectionDataSettings, + raw_bytes: Cow<[u8]>, + rom: RomAddress, + vram: u32, // Vram, // TODO + ) { + self.preanalyze_rodata(settings, &raw_bytes, rom, Vram::new(vram)); + } + + #[pyo3(name = "preanalyze_gcc_except_table")] + pub fn py_preanalyze_gcc_except_table( + &mut self, + settings: &SectionDataSettings, + raw_bytes: Cow<[u8]>, + rom: RomAddress, + vram: u32, // Vram, // TODO + ) { + self.preanalyze_gcc_except_table(settings, &raw_bytes, rom, Vram::new(vram)); + } + #[pyo3(name = "process")] pub fn py_process(&self) -> ContextBuilderFinderHeaterOverlays { self.clone().process() diff --git a/src/spimdisasm/src/lib.rs b/src/spimdisasm/src/lib.rs index 15788d0..701937e 100644 --- a/src/spimdisasm/src/lib.rs +++ b/src/spimdisasm/src/lib.rs @@ -54,6 +54,7 @@ fn spimdisasm(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) } diff --git a/src/spimdisasm/src/relocation/relocation_info.rs b/src/spimdisasm/src/relocation/relocation_info.rs index 64faec1..837e2f3 100644 --- a/src/spimdisasm/src/relocation/relocation_info.rs +++ b/src/spimdisasm/src/relocation/relocation_info.rs @@ -77,8 +77,8 @@ impl<'ctx, 'rel, 'prnt> RelocationInfoDisplay<'ctx, 'rel, 'prnt> { if let Some(referenced_segment) = context.find_referenced_segment(*vram, segment_info) { - if let Some(sym_metadata) = referenced_segment - .find_symbol(*vram, FindSettings::new().with_allow_addend(false)) + if let Some(sym_metadata) = + referenced_segment.find_symbol(*vram, FindSettings::default()) { RelocSymState::Sym(*vram, sym_metadata) } else { diff --git a/src/spimdisasm/src/size.rs b/src/spimdisasm/src/size.rs index a8bcabf..b92698f 100644 --- a/src/spimdisasm/src/size.rs +++ b/src/spimdisasm/src/size.rs @@ -68,7 +68,7 @@ impl fmt::Debug for Size { write!(f, "Size {{ 0x{:02X} }}", self.inner) } } -impl fmt::UpperHex for Size { +impl fmt::Display for Size { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "0x{:02X}", self.inner) } diff --git a/src/spimdisasm/src/symbols/display/function_display.rs b/src/spimdisasm/src/symbols/display/function_display.rs index 0b03269..6d23768 100644 --- a/src/spimdisasm/src/symbols/display/function_display.rs +++ b/src/spimdisasm/src/symbols/display/function_display.rs @@ -98,6 +98,15 @@ impl FunctionDisplay<'_, '_, '_> { return label */ + if self.settings.asm_label_indentation > 0 { + write!( + f, + "{:width$}", + " ", + width = self.settings.asm_label_indentation as usize + )?; + } + // PLACEHOLDER: write!( f, diff --git a/src/spimdisasm/src/symbols/display/sym_noload_display.rs b/src/spimdisasm/src/symbols/display/sym_noload_display.rs index eba5cb9..1e77482 100644 --- a/src/spimdisasm/src/symbols/display/sym_noload_display.rs +++ b/src/spimdisasm/src/symbols/display/sym_noload_display.rs @@ -75,7 +75,7 @@ impl fmt::Display for SymNoloadDisplay<'_, '_, '_> { .display_asm_comment(f, None, self.sym.vram_range().start(), None)?; write!( f, - " .space 0x{:02X}{}", + " .space {}{}", self.sym.size(), self.settings.common.line_end() )?;