Skip to content

Commit ace9ba8

Browse files
committed
More rust cleanup
- 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
1 parent 8b87d99 commit ace9ba8

27 files changed

+805
-615
lines changed

arch/msp430/src/architecture.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use msp430_asm::{
1818
};
1919

2020
use log::error;
21-
use binaryninja::architecture::BranchKind;
21+
use binaryninja::architecture::{BranchKind, FlagClassId, FlagGroupId, FlagId, FlagWriteId, RegisterId};
2222

2323
const MIN_MNEMONIC: usize = 9;
2424

@@ -344,14 +344,14 @@ impl Architecture for Msp430 {
344344
None
345345
}
346346

347-
fn register_from_id(&self, id: u32) -> Option<Self::Register> {
347+
fn register_from_id(&self, id: RegisterId) -> Option<Self::Register> {
348348
match id.try_into() {
349349
Ok(register) => Some(register),
350350
Err(_) => None,
351351
}
352352
}
353353

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

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

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

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

arch/msp430/src/flag.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use binaryninja::architecture;
2-
use binaryninja::architecture::FlagRole;
2+
use binaryninja::architecture::{FlagClassId, FlagGroupId, FlagId, FlagRole, FlagWriteId};
33

44
use std::borrow::Cow;
55
use std::collections::HashMap;
@@ -25,7 +25,7 @@ impl architecture::Flag for Flag {
2525
}
2626
}
2727

28-
fn role(&self, _class: Option<Self::FlagClass>) -> architecture::FlagRole {
28+
fn role(&self, _class: Option<Self::FlagClass>) -> FlagRole {
2929
match self {
3030
Self::C => FlagRole::CarryFlagRole,
3131
Self::Z => FlagRole::ZeroFlagRole,
@@ -34,20 +34,20 @@ impl architecture::Flag for Flag {
3434
}
3535
}
3636

37-
fn id(&self) -> u32 {
37+
fn id(&self) -> FlagId {
3838
match self {
3939
Self::C => 0,
4040
Self::Z => 1,
4141
Self::N => 2,
4242
Self::V => 8,
43-
}
43+
}.into()
4444
}
4545
}
4646

47-
impl TryFrom<u32> for Flag {
47+
impl TryFrom<FlagId> for Flag {
4848
type Error = ();
49-
fn try_from(flag: u32) -> Result<Self, Self::Error> {
50-
match flag {
49+
fn try_from(flag: FlagId) -> Result<Self, Self::Error> {
50+
match flag.0 {
5151
0 => Ok(Self::C),
5252
1 => Ok(Self::Z),
5353
2 => Ok(Self::N),
@@ -65,7 +65,7 @@ impl architecture::FlagClass for FlagClass {
6565
unimplemented!()
6666
}
6767

68-
fn id(&self) -> u32 {
68+
fn id(&self) -> FlagClassId {
6969
unimplemented!()
7070
}
7171
}
@@ -81,7 +81,7 @@ impl architecture::FlagGroup for FlagGroup {
8181
unimplemented!()
8282
}
8383

84-
fn id(&self) -> u32 {
84+
fn id(&self) -> FlagGroupId {
8585
unimplemented!()
8686
}
8787

@@ -119,13 +119,13 @@ impl architecture::FlagWrite for FlagWrite {
119119
None
120120
}
121121

122-
fn id(&self) -> u32 {
122+
fn id(&self) -> FlagWriteId {
123123
match self {
124124
Self::All => 1,
125125
Self::Nz => 2,
126126
Self::Nvz => 3,
127127
Self::Cnz => 4,
128-
}
128+
}.into()
129129
}
130130

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

141-
impl TryFrom<u32> for FlagWrite {
141+
impl TryFrom<FlagWriteId> for FlagWrite {
142142
type Error = ();
143143

144-
fn try_from(value: u32) -> Result<Self, Self::Error> {
145-
match value {
144+
fn try_from(value: FlagWriteId) -> Result<Self, Self::Error> {
145+
match value.0 {
146146
1 => Ok(Self::All),
147147
2 => Ok(Self::Nz),
148148
3 => Ok(Self::Nvz),

arch/msp430/src/register.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use binaryninja::architecture;
2-
use binaryninja::architecture::ImplicitRegisterExtend;
2+
use binaryninja::architecture::{ImplicitRegisterExtend, RegisterId};
33

44
use std::borrow::Cow;
55

@@ -23,15 +23,15 @@ pub enum Register {
2323
R15,
2424
}
2525

26-
impl TryFrom<u32> for Register {
26+
impl TryFrom<RegisterId> for Register {
2727
type Error = ();
2828

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

56+
// TODO: Get rid of this and lift all u32 vals to a proper register id.
57+
impl TryFrom<u32> for Register {
58+
type Error = ();
59+
60+
fn try_from(id: u32) -> Result<Self, Self::Error> {
61+
Register::try_from(RegisterId(id))
62+
}
63+
}
64+
5665
impl architecture::Register for Register {
5766
type InfoType = Self;
5867

@@ -81,7 +90,7 @@ impl architecture::Register for Register {
8190
*self
8291
}
8392

84-
fn id(&self) -> u32 {
93+
fn id(&self) -> RegisterId {
8594
match self {
8695
Self::Pc => 0,
8796
Self::Sp => 1,
@@ -99,7 +108,7 @@ impl architecture::Register for Register {
99108
Self::R13 => 13,
100109
Self::R14 => 14,
101110
Self::R15 => 15,
102-
}
111+
}.into()
103112
}
104113
}
105114

0 commit comments

Comments
 (0)