Skip to content

Commit

Permalink
More rust cleanup
Browse files Browse the repository at this point in the history
- Architecture id's are now typed accordingly
- Fix some clippy lints
- Make instruction index public in LLIL
- Removed useless helper functions
- LLIL expressions and instruction indexes are now typed accordingly
  • Loading branch information
emesare committed Jan 2, 2025
1 parent 8b87d99 commit ace9ba8
Show file tree
Hide file tree
Showing 27 changed files with 805 additions and 615 deletions.
12 changes: 6 additions & 6 deletions arch/msp430/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use msp430_asm::{
};

use log::error;
use binaryninja::architecture::BranchKind;
use binaryninja::architecture::{BranchKind, FlagClassId, FlagGroupId, FlagId, FlagWriteId, RegisterId};

const MIN_MNEMONIC: usize = 9;

Expand Down Expand Up @@ -344,14 +344,14 @@ impl Architecture for Msp430 {
None
}

fn register_from_id(&self, id: u32) -> Option<Self::Register> {
fn register_from_id(&self, id: RegisterId) -> Option<Self::Register> {
match id.try_into() {
Ok(register) => Some(register),
Err(_) => None,
}
}

fn flag_from_id(&self, id: u32) -> Option<Self::Flag> {
fn flag_from_id(&self, id: FlagId) -> Option<Self::Flag> {
match id.try_into() {
Ok(flag) => Some(flag),
Err(_) => {
Expand All @@ -361,7 +361,7 @@ impl Architecture for Msp430 {
}
}

fn flag_write_from_id(&self, id: u32) -> Option<Self::FlagWrite> {
fn flag_write_from_id(&self, id: FlagWriteId) -> Option<Self::FlagWrite> {
match id.try_into() {
Ok(flag_write) => Some(flag_write),
Err(_) => {
Expand All @@ -371,11 +371,11 @@ impl Architecture for Msp430 {
}
}

fn flag_class_from_id(&self, _: u32) -> Option<Self::FlagClass> {
fn flag_class_from_id(&self, _: FlagClassId) -> Option<Self::FlagClass> {
None
}

fn flag_group_from_id(&self, _: u32) -> Option<Self::FlagGroup> {
fn flag_group_from_id(&self, _: FlagGroupId) -> Option<Self::FlagGroup> {
None
}

Expand Down
28 changes: 14 additions & 14 deletions arch/msp430/src/flag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use binaryninja::architecture;
use binaryninja::architecture::FlagRole;
use binaryninja::architecture::{FlagClassId, FlagGroupId, FlagId, FlagRole, FlagWriteId};

use std::borrow::Cow;
use std::collections::HashMap;
Expand All @@ -25,7 +25,7 @@ impl architecture::Flag for Flag {
}
}

fn role(&self, _class: Option<Self::FlagClass>) -> architecture::FlagRole {
fn role(&self, _class: Option<Self::FlagClass>) -> FlagRole {
match self {
Self::C => FlagRole::CarryFlagRole,
Self::Z => FlagRole::ZeroFlagRole,
Expand All @@ -34,20 +34,20 @@ impl architecture::Flag for Flag {
}
}

fn id(&self) -> u32 {
fn id(&self) -> FlagId {
match self {
Self::C => 0,
Self::Z => 1,
Self::N => 2,
Self::V => 8,
}
}.into()
}
}

impl TryFrom<u32> for Flag {
impl TryFrom<FlagId> for Flag {
type Error = ();
fn try_from(flag: u32) -> Result<Self, Self::Error> {
match flag {
fn try_from(flag: FlagId) -> Result<Self, Self::Error> {
match flag.0 {
0 => Ok(Self::C),
1 => Ok(Self::Z),
2 => Ok(Self::N),
Expand All @@ -65,7 +65,7 @@ impl architecture::FlagClass for FlagClass {
unimplemented!()
}

fn id(&self) -> u32 {
fn id(&self) -> FlagClassId {
unimplemented!()
}
}
Expand All @@ -81,7 +81,7 @@ impl architecture::FlagGroup for FlagGroup {
unimplemented!()
}

fn id(&self) -> u32 {
fn id(&self) -> FlagGroupId {
unimplemented!()
}

Expand Down Expand Up @@ -119,13 +119,13 @@ impl architecture::FlagWrite for FlagWrite {
None
}

fn id(&self) -> u32 {
fn id(&self) -> FlagWriteId {
match self {
Self::All => 1,
Self::Nz => 2,
Self::Nvz => 3,
Self::Cnz => 4,
}
}.into()
}

fn flags_written(&self) -> Vec<Self::FlagType> {
Expand All @@ -138,11 +138,11 @@ impl architecture::FlagWrite for FlagWrite {
}
}

impl TryFrom<u32> for FlagWrite {
impl TryFrom<FlagWriteId> for FlagWrite {
type Error = ();

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
fn try_from(value: FlagWriteId) -> Result<Self, Self::Error> {
match value.0 {
1 => Ok(Self::All),
2 => Ok(Self::Nz),
3 => Ok(Self::Nvz),
Expand Down
21 changes: 15 additions & 6 deletions arch/msp430/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use binaryninja::architecture;
use binaryninja::architecture::ImplicitRegisterExtend;
use binaryninja::architecture::{ImplicitRegisterExtend, RegisterId};

use std::borrow::Cow;

Expand All @@ -23,15 +23,15 @@ pub enum Register {
R15,
}

impl TryFrom<u32> for Register {
impl TryFrom<RegisterId> for Register {
type Error = ();

fn try_from(id: u32) -> Result<Self, Self::Error> {
fn try_from(id: RegisterId) -> Result<Self, Self::Error> {
// TODO: we should return separate errors if the id is between 0x7fff_ffff and 0xffff_ffff
// vs outside of that range. Temporary registers have have the high bit set which we
// shouldn't get, unless there is a bug in core. An id that isn't within that range but we
// don't handle is a bug in the architecture.
match id {
match id.0 {
0 => Ok(Self::Pc),
1 => Ok(Self::Sp),
2 => Ok(Self::Sr),
Expand All @@ -53,6 +53,15 @@ impl TryFrom<u32> for Register {
}
}

// TODO: Get rid of this and lift all u32 vals to a proper register id.
impl TryFrom<u32> for Register {
type Error = ();

fn try_from(id: u32) -> Result<Self, Self::Error> {
Register::try_from(RegisterId(id))
}
}

impl architecture::Register for Register {
type InfoType = Self;

Expand Down Expand Up @@ -81,7 +90,7 @@ impl architecture::Register for Register {
*self
}

fn id(&self) -> u32 {
fn id(&self) -> RegisterId {
match self {
Self::Pc => 0,
Self::Sp => 1,
Expand All @@ -99,7 +108,7 @@ impl architecture::Register for Register {
Self::R13 => 13,
Self::R14 => 14,
Self::R15 => 15,
}
}.into()
}
}

Expand Down
Loading

0 comments on commit ace9ba8

Please sign in to comment.