From e380a481b8f129b50ee49be818ad889bedcd3b2f Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 20 Mar 2019 03:14:39 -0700 Subject: update and impl new display-related triats --- Cargo.toml | 1 + src/display.rs | 66 ++++++++++++++++++++++++++++++++++++++-------------------- src/lib.rs | 1 + 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 58e3a3c..384bae6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ A rust msp430 decoder (specifically the microcorruption flavor) [dependencies] yaxpeax-arch = { path = "../../yaxpeax-arch" } +"termion" = "1.4.0" [[test]] name = "test" diff --git a/src/display.rs b/src/display.rs index 7864248..b51ae9b 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,25 +1,38 @@ -use std; -use std::fmt::{Display, Formatter}; +use ::{MSP430, Operand, Opcode, Instruction, Width}; -use ::{Operand, Opcode, Instruction, Width}; +use std::collections::HashMap; +use std::fmt::{Display, Formatter}; +use std::hash::Hash; +use std::rc::Rc; +use std; +use termion::color; +use yaxpeax_arch::{Arch, Colorize, ColorSettings, LengthedInstruction, ShowContextual}; impl Display for Instruction { fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { - write!(f, "{}", self.opcode)?; + let mut s = String::new(); + self.contextualize(None, 0, None, &mut s).unwrap(); + write!(f, "{}", s) + } +} + +impl ShowContextual<::Address, [Option], T> for Instruction { + fn contextualize(&self, colors: Option<&ColorSettings>, address: ::Address, context: Option<&[Option]>, out: &mut T) -> std::fmt::Result { + write!(out, "{}", self.opcode)?; match self.op_width { - Width::B => { write!(f, ".b")? }, + Width::B => { write!(out, ".b")? }, Width::W => { } }; match self.operands[0] { Operand::Nothing => return Ok(()), x @ _ => { - write!(f, " {}", x)?; + write!(out, " {}", x)?; } }; match self.operands[1] { Operand::Nothing => return Ok(()), x @ _ => { - write!(f, ", {}", x)?; + write!(out, ", {}", x)?; } }; Ok(()) @@ -64,6 +77,14 @@ impl Display for Opcode { impl Display for Operand { fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { + let mut s = String::new(); + self.colorize(None, &mut s).unwrap(); + write!(f, "{}", s) + } +} + +impl Colorize for Operand { + fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result { fn signed_hex(num: i16) -> String { if num >= 0 { format!("+{:#x}", num) @@ -73,51 +94,50 @@ impl Display for Operand { } match self { Operand::Register(reg) => { - write!(f, "R{}", reg) + write!(out, "R{}", reg) }, Operand::Indexed(reg, offset) => { - write!(f, "{}(R{})", signed_hex(*offset as i16), reg) + write!(out, "{}(R{})", signed_hex(*offset as i16), reg) }, Operand::RegisterIndirect(reg) => { - write!(f, "@R{}", reg) + write!(out, "@R{}", reg) }, Operand::IndirectAutoinc(reg) => { - write!(f, "@R{}+", reg) + write!(out, "@R{}+", reg) }, Operand::Offset(offset) => { - write!(f, "${}", signed_hex(*offset as i16)) + write!(out, "${}", signed_hex(*offset as i16)) }, Operand::Symbolic(offset) => { - write!(f, "{}(PC)", signed_hex(*offset as i16)) + write!(out, "{}(PC)", signed_hex(*offset as i16)) }, Operand::Immediate(imm) => { - write!(f, "#0x{:x}", imm) + write!(out, "#0x{:x}", imm) }, Operand::Absolute(offset) => { - write!(f, "&0x{:x}", offset) + write!(out, "&0x{:x}", offset) }, Operand::Const4 => { - write!(f, "4") + write!(out, "4") }, Operand::Const8 => { - write!(f, "8") + write!(out, "8") }, Operand::Const0 => { - write!(f, "0") + write!(out, "0") }, Operand::Const1 => { - write!(f, "1") + write!(out, "1") }, Operand::Const2 => { - write!(f, "2") + write!(out, "2") }, Operand::ConstNeg1 => { - write!(f, "-1") + write!(out, "-1") }, Operand::Nothing => { - write!(f, "") + write!(out, "") } } } } - diff --git a/src/lib.rs b/src/lib.rs index c215b68..81ceceb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ extern crate yaxpeax_arch; +extern crate termion; use yaxpeax_arch::{Arch, Decodable, LengthedInstruction}; -- cgit v1.1