@@ -4,8 +4,8 @@ use crate::register::Register;
44
55use binaryninja:: {
66 architecture:: {
7- Architecture , CoreArchitecture , CustomArchitectureHandle , FlagCondition ,
8- InstructionInfo , UnusedIntrinsic , UnusedRegisterStack , UnusedRegisterStackInfo ,
7+ Architecture , CoreArchitecture , CustomArchitectureHandle , FlagCondition , InstructionInfo ,
8+ UnusedIntrinsic , UnusedRegisterStack , UnusedRegisterStackInfo ,
99 } ,
1010 disassembly:: { InstructionTextToken , InstructionTextTokenKind } ,
1111 llil:: { LiftedExpr , Lifter } ,
@@ -17,8 +17,10 @@ use msp430_asm::{
1717 single_operand:: SingleOperand , two_operand:: TwoOperand ,
1818} ;
1919
20+ use binaryninja:: architecture:: {
21+ BranchKind , FlagClassId , FlagGroupId , FlagId , FlagWriteId , RegisterId ,
22+ } ;
2023use log:: error;
21- use binaryninja:: architecture:: { BranchKind , FlagClassId , FlagGroupId , FlagId , FlagWriteId , RegisterId } ;
2224
2325const MIN_MNEMONIC : usize = 9 ;
2426
@@ -83,81 +85,51 @@ impl Architecture for Msp430 {
8385
8486 match inst {
8587 Instruction :: Jnz ( inst) => {
86- info. add_branch (
87- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
88- ) ;
89- info. add_branch (
90- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
91- ) ;
88+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
89+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
9290 }
9391 Instruction :: Jz ( inst) => {
94- info. add_branch (
95- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
96- ) ;
97- info. add_branch (
98- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
99- ) ;
92+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
93+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
10094 }
10195 Instruction :: Jlo ( inst) => {
102- info. add_branch (
103- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
104- ) ;
105- info. add_branch (
106- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
107- ) ;
96+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
97+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
10898 }
10999 Instruction :: Jc ( inst) => {
110- info. add_branch (
111- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
112- ) ;
113- info. add_branch (
114- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
115- ) ;
100+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
101+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
116102 }
117103 Instruction :: Jn ( inst) => {
118- info. add_branch (
119- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
120- ) ;
121- info. add_branch (
122- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
123- ) ;
104+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
105+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
124106 }
125107 Instruction :: Jge ( inst) => {
126- info. add_branch (
127- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
128- ) ;
129- info. add_branch (
130- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
131- ) ;
108+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
109+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
132110 }
133111 Instruction :: Jl ( inst) => {
134- info. add_branch (
135- BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
136- ) ;
137- info. add_branch (
138- BranchKind :: False ( addr + inst. size ( ) as u64 ) ,
139- ) ;
112+ info. add_branch ( BranchKind :: True ( offset_to_absolute ( addr, inst. offset ( ) ) ) ) ;
113+ info. add_branch ( BranchKind :: False ( addr + inst. size ( ) as u64 ) ) ;
140114 }
141115 Instruction :: Jmp ( inst) => {
142- info. add_branch (
143- BranchKind :: Unconditional ( offset_to_absolute ( addr, inst. offset ( ) ) ) ,
144- ) ;
116+ info. add_branch ( BranchKind :: Unconditional ( offset_to_absolute (
117+ addr,
118+ inst. offset ( ) ,
119+ ) ) ) ;
145120 }
146121 Instruction :: Br ( inst) => match inst. destination ( ) {
147- Some ( Operand :: RegisterDirect ( _) ) => {
148- info. add_branch ( BranchKind :: Indirect )
122+ Some ( Operand :: RegisterDirect ( _) ) => info. add_branch ( BranchKind :: Indirect ) ,
123+ Some ( Operand :: Indexed ( _) ) => info. add_branch ( BranchKind :: Indirect ) ,
124+ Some ( Operand :: Absolute ( value) ) => {
125+ info. add_branch ( BranchKind :: Unconditional ( * value as u64 ) )
149126 }
150- Some ( Operand :: Indexed ( _) ) => {
151- info. add_branch ( BranchKind :: Indirect )
152- }
153- Some ( Operand :: Absolute ( value) ) => info. add_branch (
154- BranchKind :: Unconditional ( * value as u64 ) ,
155- ) ,
156127 Some ( Operand :: Symbolic ( offset) ) => info. add_branch (
157128 BranchKind :: Unconditional ( ( addr as i64 + * offset as i64 ) as u64 ) ,
158129 ) ,
159- Some ( Operand :: Immediate ( addr) ) => info
160- . add_branch ( BranchKind :: Unconditional ( * addr as u64 ) ) ,
130+ Some ( Operand :: Immediate ( addr) ) => {
131+ info. add_branch ( BranchKind :: Unconditional ( * addr as u64 ) )
132+ }
161133 Some ( Operand :: Constant ( _) ) => {
162134 info. add_branch ( BranchKind :: Unconditional ( addr) )
163135 }
@@ -168,24 +140,16 @@ impl Architecture for Msp430 {
168140 None => { }
169141 } ,
170142 Instruction :: Call ( inst) => match inst. source ( ) {
171- Operand :: RegisterDirect ( _) => {
172- info. add_branch ( BranchKind :: Indirect )
173- }
174- Operand :: Indexed ( _) => {
175- info. add_branch ( BranchKind :: Indirect )
176- }
143+ Operand :: RegisterDirect ( _) => info. add_branch ( BranchKind :: Indirect ) ,
144+ Operand :: Indexed ( _) => info. add_branch ( BranchKind :: Indirect ) ,
177145 Operand :: Absolute ( value) => {
178146 info. add_branch ( BranchKind :: Call ( * value as u64 ) )
179147 }
180- Operand :: Symbolic ( offset) => info. add_branch (
181- BranchKind :: Call ( ( addr as i64 + * offset as i64 ) as u64 ) ,
182- ) ,
183- Operand :: Immediate ( addr) => {
184- info. add_branch ( BranchKind :: Call ( * addr as u64 ) )
185- }
186- Operand :: Constant ( _) => {
187- info. add_branch ( BranchKind :: Call ( addr) )
148+ Operand :: Symbolic ( offset) => {
149+ info. add_branch ( BranchKind :: Call ( ( addr as i64 + * offset as i64 ) as u64 ) )
188150 }
151+ Operand :: Immediate ( addr) => info. add_branch ( BranchKind :: Call ( * addr as u64 ) ) ,
152+ Operand :: Constant ( _) => info. add_branch ( BranchKind :: Call ( addr) ) ,
189153 Operand :: RegisterIndirect ( _)
190154 | Operand :: RegisterIndirectAutoIncrement ( _) => {
191155 info. add_branch ( BranchKind :: Indirect )
@@ -468,7 +432,7 @@ fn generate_single_operand_tokens(
468432 if inst. mnemonic ( ) . len ( ) < MIN_MNEMONIC {
469433 let padding = " " . repeat ( MIN_MNEMONIC - inst. mnemonic ( ) . len ( ) ) ;
470434 res. push ( InstructionTextToken :: new (
471- & padding,
435+ padding,
472436 InstructionTextTokenKind :: Text ,
473437 ) )
474438 }
@@ -489,13 +453,13 @@ fn generate_jxx_tokens(inst: &impl Jxx, addr: u64) -> Vec<InstructionTextToken>
489453 if inst. mnemonic ( ) . len ( ) < MIN_MNEMONIC {
490454 let padding = " " . repeat ( MIN_MNEMONIC - inst. mnemonic ( ) . len ( ) ) ;
491455 res. push ( InstructionTextToken :: new (
492- & padding,
456+ padding,
493457 InstructionTextTokenKind :: Text ,
494458 ) )
495459 }
496460
497461 res. push ( InstructionTextToken :: new (
498- & format ! ( "0x{fixed_addr:4x}" ) ,
462+ format ! ( "0x{fixed_addr:4x}" ) ,
499463 InstructionTextTokenKind :: CodeRelativeAddress {
500464 value : fixed_addr,
501465 size : None ,
@@ -514,7 +478,7 @@ fn generate_two_operand_tokens(inst: &impl TwoOperand, addr: u64) -> Vec<Instruc
514478 if inst. mnemonic ( ) . len ( ) < MIN_MNEMONIC {
515479 let padding = " " . repeat ( MIN_MNEMONIC - inst. mnemonic ( ) . len ( ) ) ;
516480 res. push ( InstructionTextToken :: new (
517- & padding,
481+ padding,
518482 InstructionTextTokenKind :: Text ,
519483 ) )
520484 }
@@ -578,7 +542,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
578542 InstructionTextTokenKind :: Register ,
579543 ) ] ,
580544 _ => vec ! [ InstructionTextToken :: new(
581- & format!( "r{r}" ) ,
545+ format!( "r{r}" ) ,
582546 InstructionTextTokenKind :: Register ,
583547 ) ] ,
584548 } ,
@@ -674,10 +638,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
674638 } ,
675639 ) ,
676640 InstructionTextToken :: new( "(" , InstructionTextTokenKind :: Text ) ,
677- InstructionTextToken :: new(
678- & format!( "r{r}" ) ,
679- InstructionTextTokenKind :: Register ,
680- ) ,
641+ InstructionTextToken :: new( format!( "r{r}" ) , InstructionTextTokenKind :: Register ) ,
681642 InstructionTextToken :: new( ")" , InstructionTextTokenKind :: Text ) ,
682643 ]
683644 }
@@ -691,7 +652,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
691652
692653 vec ! [
693654 InstructionTextToken :: new( "@" , InstructionTextTokenKind :: Text ) ,
694- InstructionTextToken :: new( & r_text, InstructionTextTokenKind :: Register ) ,
655+ InstructionTextToken :: new( r_text, InstructionTextTokenKind :: Register ) ,
695656 ]
696657 }
697658 Operand :: RegisterIndirectAutoIncrement ( r) => {
@@ -703,32 +664,29 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
703664
704665 vec ! [
705666 InstructionTextToken :: new( "@" , InstructionTextTokenKind :: Text ) ,
706- InstructionTextToken :: new( & r_text, InstructionTextTokenKind :: Register ) ,
667+ InstructionTextToken :: new( r_text, InstructionTextTokenKind :: Register ) ,
707668 InstructionTextToken :: new( "+" , InstructionTextTokenKind :: Text ) ,
708669 ]
709670 }
710671 Operand :: Symbolic ( i) => {
711672 let value = ( addr as i64 + * i as i64 ) as u64 ;
712673 vec ! [ InstructionTextToken :: new(
713- & format!( "{value:#x}" ) ,
714- InstructionTextTokenKind :: CodeRelativeAddress {
715- value,
716- size: None ,
717- } ,
674+ format!( "{value:#x}" ) ,
675+ InstructionTextTokenKind :: CodeRelativeAddress { value, size: None } ,
718676 ) ]
719677 }
720678 Operand :: Immediate ( i) => {
721679 if call {
722680 vec ! [ InstructionTextToken :: new(
723- & format!( "{i:#x}" ) ,
681+ format!( "{i:#x}" ) ,
724682 InstructionTextTokenKind :: CodeRelativeAddress {
725683 value: * i as u64 ,
726684 size: None ,
727685 } ,
728686 ) ]
729687 } else {
730688 vec ! [ InstructionTextToken :: new(
731- & format!( "{i:#x}" ) ,
689+ format!( "{i:#x}" ) ,
732690 InstructionTextTokenKind :: PossibleAddress {
733691 value: * i as u64 ,
734692 size: None ,
@@ -739,15 +697,15 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
739697 Operand :: Absolute ( a) => {
740698 if call {
741699 vec ! [ InstructionTextToken :: new(
742- & format!( "{a:#x}" ) ,
700+ format!( "{a:#x}" ) ,
743701 InstructionTextTokenKind :: CodeRelativeAddress {
744702 value: * a as u64 ,
745703 size: None ,
746704 } ,
747705 ) ]
748706 } else {
749707 vec ! [ InstructionTextToken :: new(
750- & format!( "{a:#x}" ) ,
708+ format!( "{a:#x}" ) ,
751709 InstructionTextTokenKind :: PossibleAddress {
752710 value: * a as u64 ,
753711 size: None ,
@@ -765,7 +723,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
765723 vec ! [
766724 InstructionTextToken :: new( "#" , InstructionTextTokenKind :: Text ) ,
767725 InstructionTextToken :: new(
768- & num_text,
726+ num_text,
769727 InstructionTextTokenKind :: Integer {
770728 value: * i as u64 ,
771729 size: None ,
0 commit comments