Skip to content

Commit

Permalink
Add pyo3 feature and expose structs that spimdisasm will re-expose
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Jan 16, 2025
1 parent 1bb6af3 commit 5760504
Show file tree
Hide file tree
Showing 16 changed files with 403 additions and 137 deletions.
97 changes: 97 additions & 0 deletions Cargo.lock

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

32 changes: 0 additions & 32 deletions pyproject.toml

This file was deleted.

2 changes: 1 addition & 1 deletion src/rab-disasmdis/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn main() {

let endian = args.endian;
let mut data = Data::new();
let flags = rabbitizer::instr::InstructionFlags::new(rabbitizer::IsaVersion::MIPS_III, None)
let flags = rabbitizer::instr::InstructionFlags::new(rabbitizer::IsaVersion::MIPS_III)
.with_isa_extension(None)
.with_all_pseudos(args.pseudos);
let vram = rabbitizer::vram::Vram::new(0x8000_0000);
Expand Down
4 changes: 3 additions & 1 deletion src/rabbitizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ path = "src/lib.rs"
[features]
default = []
std = []

bindings_c = []
bindings_python = []
pyo3 = ["std", "dep:pyo3"]

[dependencies]
bitflags = { version = "2.6.0", default-features = false }
pyo3 = { version = "0.23.2", optional = true }

[dev-dependencies]
criterion = { version ="0.3", features = ["html_reports"]}
Expand Down
4 changes: 2 additions & 2 deletions src/rabbitizer/benches/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rabbitizer::{

fn decode_none(c: &mut Criterion) {
let vram = Vram::new(0x80000000);
let flags = InstructionFlags::new(IsaVersion::MIPS_III, None);
let flags = InstructionFlags::new(IsaVersion::MIPS_III);

c.bench_function("decode NONE: nop", |b| {
b.iter(|| Instruction::new(black_box(0x00000000), vram, flags))
Expand All @@ -33,7 +33,7 @@ fn decode_none(c: &mut Criterion) {

fn display_none(c: &mut Criterion) {
let vram = Vram::new(0x80000000);
let flags = InstructionFlags::new(IsaVersion::MIPS_III, None);
let flags = InstructionFlags::new(IsaVersion::MIPS_III);
let display_flags = InstructionDisplayFlags::new_gnu_as();

c.bench_function("display NONE: nop", |b| {
Expand Down
39 changes: 39 additions & 0 deletions src/rabbitizer/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: © 2022-2025 Decompollaborate
# SPDX-License-Identifier: MIT

[project]
name = "rabbitizer"
# Version should be synced with include/common/RabbitizerVersion.h
version = "2.0.0-dev0"
description = "MIPS instruction decoder"
readme = "README.md"
license = {file = "../../LICENSE"}
requires-python = ">=3.9"
authors = [
{ name="Anghelo Carvajal", email="[email protected]" },
]
classifiers = [
"Programming Language :: Python :: 3",

"License :: OSI Approved :: MIT License",

"Topic :: Software Development :: Disassemblers",

"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",

"Typing :: Typed",
]

[project.urls]
"Repository" = "https://github.com/Decompollaborate/rabbitizer"
"Bug Tracker" = "https://github.com/Decompollaborate/rabbitizer/issues"
# "Changelog" = ""

[build-system]
requires = ["maturin>=1,<2"]
build-backend = "maturin"

[tool.maturin]
features = ["pyo3/extension-module", "pyo3"]
cargo-extra-args = "--crate-type=cdylib"
68 changes: 68 additions & 0 deletions src/rabbitizer/src/display_flags.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* SPDX-FileCopyrightText: © 2024-2025 Decompollaborate */
/* SPDX-License-Identifier: MIT */

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "pyo3", pyclass(module = "rabbitizer"))]
pub struct InstructionDisplayFlags {
/// Enables using named registers. This option takes precedence over the other named register options
named_registers: bool,
Expand Down Expand Up @@ -312,3 +316,67 @@ impl Default for InstructionDisplayFlags {
Self::default()
}
}

#[cfg(feature = "pyo3")]
pub(crate) mod python_bindings {
use super::*;

#[pymethods]
impl InstructionDisplayFlags {
#[new]
#[must_use]
pub const fn py_new() -> Self {
Self::new()
}

#[pyo3(name = "new_gnu_as")]
#[staticmethod]
#[must_use]
pub const fn py_new_gnu_as() -> Self {
Self::new_gnu_as()
}

#[pyo3(name = "new_legacy_as")]
#[staticmethod]
#[must_use]
pub const fn py_new_legacy_as() -> Self {
Self::new_legacy_as()
}

#[pyo3(name = "set_named_gpr")]
pub fn py_set_named_gpr(&mut self, named_gpr: bool) {
self.named_gpr = named_gpr;
}
#[pyo3(name = "set_named_fpr")]
pub fn py_set_named_fpr(&mut self, named_fpr: bool) {
self.named_fpr = named_fpr;
}
#[pyo3(name = "set_opcode_ljust")]
pub fn py_set_opcode_ljust(&mut self, opcode_ljust: u32) {
self.opcode_ljust = opcode_ljust;
}
#[pyo3(name = "set_expand_jalr")]
pub fn py_set_expand_jalr(&mut self, expand_jalr: bool) {
self.expand_jalr = expand_jalr;
}
#[pyo3(name = "set_gnu_div")]
pub fn py_set_gnu_div(&mut self, gnu_div: bool) {
self.gnu_div = gnu_div;
}
#[pyo3(name = "set_sn64_break_fix")]
pub fn py_set_sn64_break_fix(&mut self, sn64_break_fix: bool) {
self.sn64_break_fix = sn64_break_fix;
}
#[pyo3(name = "set_r5900_modern_gas_instrs_workarounds")]
pub fn py_set_r5900_modern_gas_instrs_workarounds(
&mut self,
r5900_modern_gas_instrs_workarounds: bool,
) {
self.r5900_modern_gas_instrs_workarounds = r5900_modern_gas_instrs_workarounds;
}
#[pyo3(name = "set_r5900_use_dollar")]
pub fn py_set_r5900_use_dollar(&mut self, r5900_use_dollar: bool) {
self.r5900_use_dollar = r5900_use_dollar;
}
}
}
3 changes: 3 additions & 0 deletions src/rabbitizer/src/generated/enum_isa_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

/* Automatically generated. DO NOT MODIFY */

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
#[cfg_attr(feature = "pyo3", pyclass(module = "rabbitizer", eq))]
pub enum IsaExtension {
RSP,
R3000GTE,
Expand Down
3 changes: 3 additions & 0 deletions src/rabbitizer/src/generated/enum_isa_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

/* Automatically generated. DO NOT MODIFY */

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
#[cfg_attr(feature = "pyo3", pyclass(module = "rabbitizer", eq))]
pub enum IsaVersion {
MIPS_I,
MIPS_II,
Expand Down
Loading

0 comments on commit 5760504

Please sign in to comment.