From 0399548ac215717e23d2ad76cb1417892b477c74 Mon Sep 17 00:00:00 2001 From: iximeow Date: Mon, 17 Jun 2024 01:35:52 -0700 Subject: might be an ok way to redesign colorization.... it turns out that yaxpeax-arch's notion of colorization has been broken from the start for systems that do markup without inline sequences (e.g. windows/cmd.exe before vt100 support) --- src/long_mode/display.rs | 74 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/src/long_mode/display.rs b/src/long_mode/display.rs index 3ccbb0f..b67acd4 100644 --- a/src/long_mode/display.rs +++ b/src/long_mode/display.rs @@ -362,6 +362,34 @@ impl Colorize for Operand { } } +enum TokenType { + Mnemonic, + Operand, + Immediate, + Register, + Offset, +} + +trait DisplaySink: fmt::Write { +// fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error>; +// fn write_char(&mut self, c: char) -> Result<(), core::fmt::Error>; + fn span_enter(&mut self, ty: TokenType); + fn span_end(&mut self, ty: TokenType); +} + +impl DisplaySink for T { + /* + fn write_str(&mut self) -> Result<(), core::fmt::Error> { + ::write_str(self, s) + } + fn write_char(&mut self) -> Result<(), core::fmt::Error> { + ::write_char(self, c) + } + */ + fn span_enter(&mut self, _ty: TokenType) { } + fn span_end(&mut self, _ty: TokenType) { } +} + struct ColorizingOperandVisitor<'a, T, Y> { instr: &'a Instruction, op_nr: u8, @@ -369,37 +397,57 @@ struct ColorizingOperandVisitor<'a, T, Y> { f: &'a mut T, } -impl crate::long_mode::OperandVisitor for ColorizingOperandVisitor<'_, T, Y> { +impl crate::long_mode::OperandVisitor for ColorizingOperandVisitor<'_, T, Y> { type Ok = (); type Error = core::fmt::Error; fn visit_u8(&mut self, imm: u8) -> Result { - write!(self.f, "{}", self.colors.number(u8_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", u8_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_i8(&mut self, imm: i8) -> Result { - write!(self.f, "{}", - self.colors.number(signed_i8_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", signed_i8_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_u16(&mut self, imm: u16) -> Result { - write!(self.f, "{}", self.colors.number(u16_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", u16_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_i16(&mut self, imm: i16) -> Result { - write!(self.f, "{}", - self.colors.number(signed_i16_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", signed_i16_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_u32(&mut self, imm: u32) -> Result { - write!(self.f, "{}", self.colors.number(u32_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", u32_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_i32(&mut self, imm: i32) -> Result { - write!(self.f, "{}", - self.colors.number(signed_i32_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", signed_i32_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_u64(&mut self, imm: u64) -> Result { - write!(self.f, "{}", self.colors.number(u64_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", u64_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_i64(&mut self, imm: i64) -> Result { - write!(self.f, "{}", - self.colors.number(signed_i64_hex(imm))) + self.f.span_enter(TokenType::Immediate); + write!(self.f, "{}", signed_i64_hex(imm))?; + self.f.span_end(TokenType::Immediate); + Ok(()) } fn visit_reg(&mut self, reg: RegSpec) -> Result { self.f.write_str(regspec_label(®)) -- cgit v1.1