From 40436975204fe296d30362e624e805ade97b4a1b Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 18 Jan 2020 14:01:36 -0800 Subject: some of the work to get yaxpeax-arm no-std --- src/armv7.rs | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'src/armv7.rs') diff --git a/src/armv7.rs b/src/armv7.rs index f85038f..50cde07 100644 --- a/src/armv7.rs +++ b/src/armv7.rs @@ -3,19 +3,21 @@ use std::fmt::{self, Display, Formatter}; -use yaxpeax_arch::{Arch, Colorize, Colored, ColorSettings, Decoder, LengthedInstruction, ShowContextual, YaxColors}; +use yaxpeax_arch::{Arch, Colorize, Decoder, LengthedInstruction, NoColors, ShowContextual, YaxColors}; pub struct ConditionedOpcode(pub Opcode, pub ConditionCode); impl Display for ConditionedOpcode { - fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { write!(f, "{}{}", self.0, self.1) } } +pub struct NoContext; + #[allow(non_snake_case)] -impl ShowContextual], T> for Instruction { - fn contextualize(&self, colors: Option<&ColorSettings>, _address: u32, _context: Option<&[Option]>, out: &mut T) -> std::fmt::Result { +impl > ShowContextual for Instruction { + fn contextualize(&self, colors: &Y, _address: u32, _context: Option<&NoContext>, out: &mut T) -> fmt::Result { match self.opcode { Opcode::LDR(true, false, false) => { match self.operands { @@ -169,8 +171,8 @@ impl ShowContextual], T> for Instructi } } -impl Colorize for ConditionedOpcode { - fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result { +impl > Colorize for ConditionedOpcode { + fn colorize(&self, colors: &Y, out: &mut T) -> fmt::Result { match self.0 { Opcode::Incomplete(_) | Opcode::Invalid => { write!(out, "{}", colors.invalid_op(self)) }, @@ -248,7 +250,7 @@ impl Colorize for ConditionedOpcode { } impl Display for Opcode { - fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { match self { Opcode::Incomplete(word) => { write!(f, "incomplete: {:#x}", word) }, Opcode::Invalid => { write!(f, "invalid") }, @@ -467,9 +469,8 @@ impl yaxpeax_arch::Instruction for Instruction { fn well_defined(&self) -> bool { true } } -#[allow(non_snake_case)] -impl Instruction { - pub fn blank() -> Instruction { +impl Default for Instruction { + fn default() -> Self { Instruction { condition: ConditionCode::AL, opcode: Opcode::Invalid, @@ -477,13 +478,16 @@ impl Instruction { s: false } } - pub fn set_S(&mut self, value: bool) { +} + +impl Instruction { + fn set_s(&mut self, value: bool) { self.s = value; } - pub fn S(&self) -> bool { self.s } + pub fn s(&self) -> bool { self.s } } -fn format_reg_list(f: &mut T, mut list: u16, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> { +fn format_reg_list>(f: &mut T, mut list: u16, colors: &Y) -> Result<(), fmt::Error> { write!(f, "{{")?; let mut i = 0; let mut tail = false; @@ -504,7 +508,7 @@ fn format_reg_list(f: &mut T, mut list: u16, colors: Option< } #[allow(non_snake_case)] -fn format_shift(f: &mut T, Rm: u8, shift: ShiftSpec, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> { +fn format_shift>(f: &mut T, Rm: u8, shift: ShiftSpec, colors: &Y) -> Result<(), fmt::Error> { fn shift_tpe_to_str(tpe: u8) -> &'static str { match tpe { 0b00 => "lsl", @@ -532,7 +536,7 @@ fn format_shift(f: &mut T, Rm: u8, shift: ShiftSpec, colors: } #[allow(non_snake_case)] -fn format_reg_shift_mem(f: &mut T, Rd: u8, Rm: u8, shift: ShiftSpec, add: bool, pre: bool, wback: bool, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> { +fn format_reg_shift_mem>(f: &mut T, Rd: u8, Rm: u8, shift: ShiftSpec, add: bool, pre: bool, wback: bool, colors: &Y) -> Result<(), fmt::Error> { let op = if add { "" } else { "-" }; match (pre, wback) { @@ -557,7 +561,7 @@ fn format_reg_shift_mem(f: &mut T, Rd: u8, Rm: u8, shift: Sh } #[allow(non_snake_case)] -fn format_reg_imm_mem(f: &mut T, Rn: u8, imm: u32, add: bool, pre: bool, wback: bool, colors: Option<&ColorSettings>) -> Result<(), std::fmt::Error> { +fn format_reg_imm_mem>(f: &mut T, Rn: u8, imm: u32, add: bool, pre: bool, wback: bool, colors: &Y) -> Result<(), fmt::Error> { if imm != 0 { let op = if add { "" } else { "-" }; @@ -592,7 +596,7 @@ fn format_reg_imm_mem(f: &mut T, Rn: u8, imm: u32, add: bool } } } -fn reg_name_colorize(num: u8, colors: Option<&ColorSettings>) -> Colored<&'static str> { +fn reg_name_colorize>(num: u8, colors: &Y) -> impl fmt::Display { match num { 0 => colors.register("r0"), 1 => colors.register("r1"), @@ -615,8 +619,8 @@ fn reg_name_colorize(num: u8, colors: Option<&ColorSettings>) -> Colored<&'stati } impl Display for Instruction { - fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { - self.contextualize(None, 0, None, f) + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + self.contextualize(&NoColors, 0, Some(&NoContext), f) } } @@ -650,7 +654,7 @@ pub enum ConditionCode { } impl Display for ConditionCode { - fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { match self { ConditionCode::EQ => write!(f, "eq"), ConditionCode::NE => write!(f, "ne"), @@ -705,10 +709,6 @@ pub struct InstDecoder {} impl Decoder for InstDecoder { type Error = DecodeError; - fn decode>(&self, bytes: T) -> Result { - let mut blank = Instruction::blank(); - self.decode_into(&mut blank, bytes).map(|_: ()| blank) - } fn decode_into>(&self, inst: &mut Instruction, bytes: T) -> Result<(), Self::Error> { fn read_word>(bytes: T) -> Result { let mut iter = bytes.into_iter(); @@ -797,7 +797,7 @@ impl Decoder for InstDecoder { ((word >> 12) & 0x0f) as u8, ((word >> 16) & 0x0f) as u8 ]; - inst.set_S(s); + inst.set_s(s); match op { 0b000 => { inst.opcode = Opcode::MUL; @@ -1035,7 +1035,7 @@ impl Decoder for InstDecoder { unreachable!(); } inst.opcode = DATA_PROCESSING_OPCODES[opcode as usize]; - inst.set_S(s); + inst.set_s(s); // at this point we know this is a data processing instruction // either immediate shift or register shift @@ -1115,7 +1115,7 @@ impl Decoder for InstDecoder { unreachable!(); } inst.opcode = DATA_PROCESSING_OPCODES[opcode as usize]; - inst.set_S(s); + inst.set_s(s); let (Rn, imm) = { let imm = word & 0x0000ffff; -- cgit v1.1