Skip to content

Commit

Permalink
wiring up with python
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Dec 5, 2024
1 parent 6ed438a commit 3f41754
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions src/spimdisasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
79 changes: 79 additions & 0 deletions src/spimdisasm/src/context/builder/context_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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<RomAddress>,
) {
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<RomAddress>,
) {
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<RomAddress>,
) {
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<RomAddress>,
) {
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<RomAddress>,
) {
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<RomAddress>,
) {
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<RomAddress>,
) {
self.global_segment()
.add_gcc_except_table_label(name, Vram::new(vram), rom);
}

#[pyo3(name = "process")]
pub fn py_process(&self) -> ContextBuilderOverlay {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/spimdisasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn spimdisasm(m: &Bound<'_, PyModule>) -> PyResult<()> {

m.add_class::<symbols::display::FunctionDisplaySettings>()?;
m.add_class::<symbols::display::SymDataDisplaySettings>()?;
m.add_class::<symbols::display::SymNoloadDisplaySettings>()?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/spimdisasm/src/relocation/relocation_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/spimdisasm/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions src/spimdisasm/src/symbols/display/function_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/spimdisasm/src/symbols/display/sym_noload_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)?;
Expand Down

0 comments on commit 3f41754

Please sign in to comment.