From 894cba0dde913d86472430eae8b7ba0353711b4e Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 15 Jan 2020 01:14:50 -0800 Subject: no_std!! this makes yaxpeax-arch no_std. generally nothing has changed w.r.t downstream crates, but a lot to do with colorization has been moved tweaked to make it no_std-friendly (specifically, allowing `termion` to be an optional dependency) this also makes address parsing optional, in the hopes that decode-only use cases don't need to involve as much machinery when building. --- src/address/mod.rs | 163 ++++++++++++++++++ src/color.rs | 386 +++++++++++++++++++++++++++++++++++++++++ src/display.rs | 151 +++++++++------- src/lib.rs | 498 +++-------------------------------------------------- 4 files changed, 657 insertions(+), 541 deletions(-) create mode 100644 src/address/mod.rs create mode 100644 src/color.rs (limited to 'src') diff --git a/src/address/mod.rs b/src/address/mod.rs new file mode 100644 index 0000000..7146d01 --- /dev/null +++ b/src/address/mod.rs @@ -0,0 +1,163 @@ +use core::hash::Hash; + +use core::fmt::{self, Debug, Display, Formatter}; + +use core::ops::{Add, Sub, AddAssign, SubAssign}; + +use num_traits::identities; +use num_traits::{Bounded, WrappingAdd, WrappingSub, CheckedAdd, CheckedSub}; + +#[cfg(feature="use-serde")] +use serde::{Deserialize, Serialize}; + +pub trait AddressBase where Self: + Debug + Display + AddressDisplay + + Copy + Clone + Sized + Hash + + Ord + Eq + PartialEq + Bounded + + Add + Sub + + AddAssign + SubAssign + + WrappingAdd + WrappingSub + + CheckedAdd + CheckedSub + + Hash + + identities::One + identities::Zero { + fn to_linear(&self) -> usize; +} + +#[cfg(all(feature="use-serde", feature="address-parse"))] +pub trait Address where Self: + AddressBase + + Serialize + for<'de> Deserialize<'de> + + AddrParse { +} + +#[cfg(all(feature="use-serde", not(feature="address-parse")))] +pub trait Address where Self: + AddressBase + + Serialize + for<'de> Deserialize<'de> { +} + +#[cfg(all(not(feature="use-serde"), feature="address-parse"))] +pub trait Address where Self: + AddressBase + AddrParse { +} + +#[cfg(all(not(feature="use-serde"), not(feature="address-parse")))] +pub trait Address where Self: AddressBase { } + +impl AddressBase for u16 { + fn to_linear(&self) -> usize { *self as usize } +} + +impl Address for u16 {} + +impl AddressBase for u32 { + fn to_linear(&self) -> usize { *self as usize } +} + +impl Address for u32 {} + +impl AddressBase for u64 { + fn to_linear(&self) -> usize { *self as usize } +} + +impl Address for u64 {} + +impl AddressBase for usize { + fn to_linear(&self) -> usize { *self } +} + +impl Address for usize {} + +pub trait AddressDisplay { + fn show(&self, f: &mut Formatter) -> fmt::Result; +} + +impl AddressDisplay for usize { + fn show(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{:#x}", self) + } +} + +impl AddressDisplay for u64 { + fn show(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{:#x}", self) + } +} + +impl AddressDisplay for u32 { + fn show(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{:#x}", self) + } +} + +impl AddressDisplay for u16 { + fn show(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{:#x}", self) + } +} + +/* + * TODO: this should be FromStr. + * that would require newtyping address primitives, though + * + * this is not out of the question, BUT is way more work than + * i want to put in right now + * + * this is one of those "clean it up later" situations + */ +#[cfg(feature="address-parse")] +use core::str::FromStr; + +#[cfg(feature="address-parse")] +pub trait AddrParse: Sized { + type Err; + fn parse_from(s: &str) -> Result; +} + +#[cfg(feature="address-parse")] +impl AddrParse for usize { + type Err = core::num::ParseIntError; + fn parse_from(s: &str) -> Result { + if s.starts_with("0x") { + usize::from_str_radix(&s[2..], 16) + } else { + usize::from_str(s) + } + } +} + +#[cfg(feature="address-parse")] +impl AddrParse for u64 { + type Err = core::num::ParseIntError; + fn parse_from(s: &str) -> Result { + if s.starts_with("0x") { + u64::from_str_radix(&s[2..], 16) + } else { + u64::from_str(s) + } + } +} + +#[cfg(feature="address-parse")] +impl AddrParse for u32 { + type Err = core::num::ParseIntError; + fn parse_from(s: &str) -> Result { + if s.starts_with("0x") { + u32::from_str_radix(&s[2..], 16) + } else { + u32::from_str(s) + } + } +} + +#[cfg(feature="address-parse")] +impl AddrParse for u16 { + type Err = core::num::ParseIntError; + fn parse_from(s: &str) -> Result { + if s.starts_with("0x") { + u16::from_str_radix(&s[2..], 16) + } else { + u16::from_str(s) + } + } +} diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..4bcbd15 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,386 @@ +use core::fmt::{self, Display, Formatter}; + +pub enum Colored { + Color(T, Colorer, Colorer), + Just(T) +} + +impl Display for Colored { + fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + match self { + Colored::Color(t, before, after) => { + write!(fmt, "{}{}{}", before, t, after) + }, + Colored::Just(t) => { + write!(fmt, "{}", t) + } + } + } +} + +pub trait YaxColors { + fn arithmetic_op(&self, t: T) -> Colored; + fn stack_op(&self, t: T) -> Colored; + fn nop_op(&self, t: T) -> Colored; + fn stop_op(&self, t: T) -> Colored; + fn control_flow_op(&self, t: T) -> Colored; + fn data_op(&self, t: T) -> Colored; + fn comparison_op(&self, t: T) -> Colored; + fn invalid_op(&self, t: T) -> Colored; + fn platform_op(&self, t: T) -> Colored; + fn misc_op(&self, t: T) -> Colored; + + fn register(&self, t: T) -> Colored; + fn program_counter(&self, t: T) -> Colored; + fn number(&self, t: T) -> Colored; + fn zero(&self, t: T) -> Colored; + fn one(&self, t: T) -> Colored; + fn minus_one(&self, t: T) -> Colored; + fn address(&self, t: T) -> Colored; + fn symbol(&self, t: T) -> Colored; + fn function(&self, t: T) -> Colored; +} + +pub struct NoColors; + +impl YaxColors<&'static str> for NoColors { + fn arithmetic_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn stack_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn nop_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn stop_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn control_flow_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn data_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn comparison_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn invalid_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn platform_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn misc_op(&self, t: T) -> Colored { + Colored::Just(t) + } + fn register(&self, t: T) -> Colored { + Colored::Just(t) + } + fn program_counter(&self, t: T) -> Colored { + Colored::Just(t) + } + fn number(&self, t: T) -> Colored { + Colored::Just(t) + } + fn zero(&self, t: T) -> Colored { + Colored::Just(t) + } + fn one(&self, t: T) -> Colored { + Colored::Just(t) + } + fn minus_one(&self, t: T) -> Colored { + Colored::Just(t) + } + fn address(&self, t: T) -> Colored { + Colored::Just(t) + } + fn symbol(&self, t: T) -> Colored { + Colored::Just(t) + } + fn function(&self, t: T) -> Colored { + Colored::Just(t) + } +} + +pub trait Colorize + ?Sized> { + fn colorize(&self, colors: &Y, out: &mut T) -> fmt::Result; +} + +#[cfg(feature="colors")] +pub use termion_color::ColorSettings; + +#[cfg(feature="colors")] +mod termion_color { + use core::fmt::Display; + + use termion::color; + use serde::Serialize; + + use crate::color::{Colored, YaxColors}; + + #[cfg(feature="use-serde")] + impl Serialize for ColorSettings { + fn serialize(&self, serializer: S) -> Result { + use serde::ser::SerializeStruct; + let s = serializer.serialize_struct("ColorSettings", 0)?; + s.end() + } + } + + pub struct ColorSettings { + arithmetic: color::Fg<&'static dyn color::Color>, + stack: color::Fg<&'static dyn color::Color>, + nop: color::Fg<&'static dyn color::Color>, + stop: color::Fg<&'static dyn color::Color>, + control: color::Fg<&'static dyn color::Color>, + data: color::Fg<&'static dyn color::Color>, + comparison: color::Fg<&'static dyn color::Color>, + invalid: color::Fg<&'static dyn color::Color>, + platform: color::Fg<&'static dyn color::Color>, + misc: color::Fg<&'static dyn color::Color>, + + register: color::Fg<&'static dyn color::Color>, + program_counter: color::Fg<&'static dyn color::Color>, + + number: color::Fg<&'static dyn color::Color>, + zero: color::Fg<&'static dyn color::Color>, + one: color::Fg<&'static dyn color::Color>, + minus_one: color::Fg<&'static dyn color::Color>, + + function: color::Fg<&'static dyn color::Color>, + symbol: color::Fg<&'static dyn color::Color>, + address: color::Fg<&'static dyn color::Color>, + } + + #[cfg(feature="colorize")] + impl Default for ColorSettings { + fn default() -> ColorSettings { + ColorSettings { + arithmetic: color::Fg(&color::LightYellow), + stack: color::Fg(&color::Magenta), + nop: color::Fg(&color::Blue), + stop: color::Fg(&color::LightRed), + control: color::Fg(&color::Green), + data: color::Fg(&color::LightMagenta), + comparison: color::Fg(&color::Yellow), + invalid: color::Fg(&color::Red), + platform: color::Fg(&color::Cyan), + misc: color::Fg(&color::LightCyan), + + register: color::Fg(&color::Cyan), + program_counter: color::Fg(&color::Red), + + number: color::Fg(&color::White), + zero: color::Fg(&color::White), + one: color::Fg(&color::White), + minus_one: color::Fg(&color::White), + + function: color::Fg(&color::LightGreen), + symbol: color::Fg(&color::LightGreen), + address: color::Fg(&color::Green), + } + } + } + + impl YaxColors> for ColorSettings { + fn arithmetic_op(&self, t: T) -> Colored> { + Colored::Color(t, self.arithmetic, color::Fg(&color::Reset)) + } + fn stack_op(&self, t: T) -> Colored> { + Colored::Color(t, self.stack, color::Fg(&color::Reset)) + } + fn nop_op(&self, t: T) -> Colored> { + Colored::Color(t, self.nop, color::Fg(&color::Reset)) + } + fn stop_op(&self, t: T) -> Colored> { + Colored::Color(t, self.stop, color::Fg(&color::Reset)) + } + fn control_flow_op(&self, t: T) -> Colored> { + Colored::Color(t, self.control, color::Fg(&color::Reset)) + } + fn data_op(&self, t: T) -> Colored> { + Colored::Color(t, self.data, color::Fg(&color::Reset)) + } + fn comparison_op(&self, t: T) -> Colored> { + Colored::Color(t, self.comparison, color::Fg(&color::Reset)) + } + fn invalid_op(&self, t: T) -> Colored> { + Colored::Color(t, self.invalid, color::Fg(&color::Reset)) + } + fn misc_op(&self, t: T) -> Colored> { + Colored::Color(t, self.misc, color::Fg(&color::Reset)) + } + fn platform_op(&self, t: T) -> Colored> { + Colored::Color(t, self.platform, color::Fg(&color::Reset)) + } + + fn register(&self, t: T) -> Colored> { + Colored::Color(t, self.register, color::Fg(&color::Reset)) + } + fn program_counter(&self, t: T) -> Colored> { + Colored::Color(t, self.program_counter, color::Fg(&color::Reset)) + } + fn number(&self, t: T) -> Colored> { + Colored::Color(t, self.number, color::Fg(&color::Reset)) + } + fn zero(&self, t: T) -> Colored> { + Colored::Color(t, self.zero, color::Fg(&color::Reset)) + } + fn one(&self, t: T) -> Colored> { + Colored::Color(t, self.one, color::Fg(&color::Reset)) + } + fn minus_one(&self, t: T) -> Colored> { + Colored::Color(t, self.minus_one, color::Fg(&color::Reset)) + } + fn address(&self, t: T) -> Colored> { + Colored::Color(t, self.address, color::Fg(&color::Reset)) + } + fn symbol(&self, t: T) -> Colored> { + Colored::Color(t, self.symbol, color::Fg(&color::Reset)) + } + fn function(&self, t: T) -> Colored> { + Colored::Color(t, self.function, color::Fg(&color::Reset)) + } + } + + impl <'a> YaxColors>for Option<&'a ColorSettings> { + fn arithmetic_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.arithmetic_op(t) } + None => { Colored::Just(t) } + } + } + fn stack_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.stack_op(t) } + None => { Colored::Just(t) } + } + } + fn nop_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.nop_op(t) } + None => { Colored::Just(t) } + } + } + fn stop_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.stop_op(t) } + None => { Colored::Just(t) } + } + } + fn control_flow_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.control_flow_op(t) } + None => { Colored::Just(t) } + } + } + fn data_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.data_op(t) } + None => { Colored::Just(t) } + } + } + fn comparison_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.comparison_op(t) } + None => { Colored::Just(t) } + } + } + fn invalid_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.invalid_op(t) } + None => { Colored::Just(t) } + } + } + fn misc_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.misc_op(t) } + None => { Colored::Just(t) } + } + } + fn platform_op(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.platform_op(t) } + None => { Colored::Just(t) } + } + } + + fn register(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.register(t) } + None => { Colored::Just(t) } + } + } + fn program_counter(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.program_counter(t) } + None => { Colored::Just(t) } + } + } + fn number(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.number(t) } + None => { Colored::Just(t) } + } + } + fn zero(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.zero(t) } + None => { Colored::Just(t) } + } + } + fn one(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.one(t) } + None => { Colored::Just(t) } + } + } + fn minus_one(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.minus_one(t) } + None => { Colored::Just(t) } + } + } + fn address(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.address(t) } + None => { Colored::Just(t) } + } + } + fn symbol(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.symbol(t) } + None => { Colored::Just(t) } + } + } + fn function(&self, t: T) -> Colored> { + match self { + Some(colors) => { colors.function(t) } + None => { Colored::Just(t) } + } + } + } +} + +/* + * can this be a derivable trait or something? + */ +/* +impl Display for T { + fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + self.colorize(None, fmt) + } +} +*/ + +/* + * and make this auto-derive from a ShowContextual impl? + */ +/* +impl Colorize for T where T: ShowContextual { + fn colorize(&self, colors: Option<&ColorSettings>, fmt: &mut Formatter) -> fmt::Result { + self.contextualize(colors, None, fmt) + } +} +*/ diff --git a/src/display.rs b/src/display.rs index 5ba9cd1..50d63d5 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,3 +1,9 @@ +use crate::YaxColors; + +use core::fmt; +use core::num::Wrapping; +use core::ops::Neg; + pub enum NumberStyleHint { Signed, HexSigned, @@ -11,105 +17,120 @@ pub enum NumberStyleHint { HexUnsignedWithSign } -pub fn format_number_i32(i: i32, hint: NumberStyleHint) -> String { +pub fn format_number_i32>(colors: &Y, f: &mut W, i: i32, hint: NumberStyleHint) -> fmt::Result { match hint { NumberStyleHint::Signed => { - format!("{}", i) + write!(f, "{}", colors.number(i)) }, NumberStyleHint::HexSigned => { - if i == std::i32::MIN { - format!("-0x7fffffff") - } else if i < 0 { - format!("-{:#x}", -i) - } else { - format!("{:#x}", i) - } + write!(f, "{}", colors.number(signed_i32_hex(i))) }, NumberStyleHint::Unsigned => { - format!("{}", i as u32) + write!(f, "{}", colors.number(i as u32)) }, NumberStyleHint::HexUnsigned => { - format!("{:#x}", i as u32) + write!(f, "{}", colors.number(u32_hex(i as u32))) }, NumberStyleHint::SignedWithSignSplit => { - if i == std::i32::MIN { - format!("- 2147483647") + if i == core::i32::MIN { + write!(f, "- {}", colors.number("2147483647")) } else if i < 0 { - format!("- {}", -i) + write!(f, "- {}", colors.number(-Wrapping(i))) } else { - format!("+ {}", i) + write!(f, "+ {}", colors.number(i)) } } NumberStyleHint::HexSignedWithSignSplit => { - if i == std::i32::MIN { - format!("- 0x7fffffff") + if i == core::i32::MIN { + write!(f, "- {}", colors.number("0x7fffffff")) } else if i < 0 { - format!("- {:#x}", -i) + write!(f, "- {}", colors.number(u32_hex((-Wrapping(i)).0 as u32))) } else { - format!("+ {:#x}", i) + write!(f, "+ {}", colors.number(u32_hex(i as u32))) } }, NumberStyleHint::HexSignedWithSign => { - if i == std::i32::MIN { - format!("-0x7fffffff") - } else if i < 0 { - format!("-{:#x}", -i) - } else { - format!("+{:#x}", i) - } + write!(f, "{}", signed_i32_hex(i)) }, NumberStyleHint::SignedWithSign => { - format!("{:+}", i) + write!(f, "{:+}", i) } NumberStyleHint::HexUnsignedWithSign => { - format!("{:+#x}", i as u32) + write!(f, "{:+#x}", i as u32) }, NumberStyleHint::UnsignedWithSign => { - format!("{:+}", i as u32) + write!(f, "{:+}", i as u32) } } } -pub fn signed_i8_hex(imm: i8) -> String { - let (sign, imm) = if imm == std::i8::MIN { - (false, imm) - } else if imm < 0 { - (true, -imm) - } else { - (false, imm) - }; - format!("{}{:#x}", if sign { "-" } else { "" } , imm) +pub struct SignedHexDisplay { + value: T, + negative: bool +} + +impl fmt::Display for SignedHexDisplay where Wrapping: Neg, as Neg>::Output: fmt::LowerHex { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.negative { + write!(f, "-{:#x}", -Wrapping(self.value)) + } else { + write!(f, "{:#x}", self.value) + } + } +} + +pub fn u8_hex(value: u8) -> SignedHexDisplay { + SignedHexDisplay { + value: value as i8, + negative: false, + } +} + +pub fn signed_i8_hex(imm: i8) -> SignedHexDisplay { + SignedHexDisplay { + value: imm, + negative: imm < 0, + } +} + +pub fn u16_hex(value: u16) -> SignedHexDisplay { + SignedHexDisplay { + value: value as i16, + negative: false, + } } -pub fn signed_i16_hex(imm: i16) -> String { - let (sign, imm) = if imm == std::i16::MIN { - (false, imm) - } else if imm < 0 { - (true, -imm) - } else { - (false, imm) - }; - format!("{}{:#x}", if sign { "-" } else { "" } , imm) +pub fn signed_i16_hex(imm: i16) -> SignedHexDisplay { + SignedHexDisplay { + value: imm, + negative: imm < 0, + } } -pub fn signed_i32_hex(imm: i32) -> String { - let (sign, imm) = if imm == std::i32::MIN { - (false, imm) - } else if imm < 0 { - (true, -imm) - } else { - (false, imm) - }; - format!("{}{:#x}", if sign { "-" } else { "" } , imm) +pub fn u32_hex(value: u32) -> SignedHexDisplay { + SignedHexDisplay { + value: value as i32, + negative: false, + } } -pub fn signed_i64_hex(imm: i64) -> String { - let (sign, imm) = if imm == std::i64::MIN { - (false, imm) - } else if imm < 0 { - (true, -imm) - } else { - (false, imm) - }; - format!("{}{:#x}", if sign { "-" } else { "" } , imm) +pub fn signed_i32_hex(imm: i32) -> SignedHexDisplay { + SignedHexDisplay { + value: imm, + negative: imm < 0, + } +} + +pub fn u64_hex(value: u64) -> SignedHexDisplay { + SignedHexDisplay { + value: value as i64, + negative: false, + } +} + +pub fn signed_i64_hex(imm: i64) -> SignedHexDisplay { + SignedHexDisplay { + value: imm, + negative: imm < 0, + } } diff --git a/src/lib.rs b/src/lib.rs index c5b0610..f503741 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,163 +1,31 @@ -extern crate num_traits; -extern crate termion; -#[cfg(feature="use-serde")] -extern crate serde; - -use std::str::FromStr; -use std::hash::Hash; +#![no_std] -use std::fmt::{Debug, Display, Formatter}; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; -use std::ops::{Add, Sub, AddAssign, SubAssign}; - -use num_traits::identities; -use num_traits::{Bounded, WrappingAdd, WrappingSub, CheckedAdd, CheckedSub}; - -use termion::color; +use core::fmt::{self, Debug, Display}; +use core::hash::Hash; +extern crate num_traits; #[cfg(feature="use-serde")] -use serde::{Serialize, Deserialize}; - -pub mod display; - // This is pretty wonk.. -pub trait AddressDisplay { - fn stringy(&self) -> String; -} - -/* - * TODO: this should be FromStr. - * that would require newtyping address primitives, though - * - * this is not out of the question, BUT is way more work than - * i want to put in right now - * - * this is one of those "clean it up later" situations - */ -pub trait AddrParse: Sized { - type Err; - fn parse_from(s: &str) -> Result; -} +extern crate serde; +#[cfg(feature="colors")] +extern crate termion; #[cfg(feature="use-serde")] -pub trait Address where Self: - Serialize + for<'de> Deserialize<'de> + - Debug + Display + AddressDisplay + - Copy + Clone + Sized + Hash + - Ord + Eq + PartialEq + Bounded + - Add + Sub + - AddAssign + SubAssign + - WrappingAdd + WrappingSub + - CheckedAdd + CheckedSub + - AddrParse + - Hash + - identities::One + identities::Zero { - fn to_linear(&self) -> usize; - -} -#[cfg(not(feature="use-serde"))] -pub trait Address where Self: - Debug + Display + AddressDisplay + - Copy + Clone + Sized + Hash + - Ord + Eq + PartialEq + Bounded + - Add + Sub + - AddAssign + SubAssign + - WrappingAdd + WrappingSub + - CheckedAdd + CheckedSub + - AddrParse + - Hash + - identities::One + identities::Zero { - fn to_linear(&self) -> usize; - -} -/* -impl Address for T where T: Sized + Ord + Add + From + Into { - fn to_linear(&self) -> usize { *self.into() } -} -*/ - -impl AddrParse for usize { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result { - if s.starts_with("0x") { - usize::from_str_radix(&s[2..], 16) - } else { - usize::from_str(s) - } - } -} - -impl AddressDisplay for usize { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} - -impl AddrParse for u64 { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result { - if s.starts_with("0x") { - u64::from_str_radix(&s[2..], 16) - } else { - u64::from_str(s) - } - } -} - -impl AddressDisplay for u64 { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} - -impl AddrParse for u32 { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result { - if s.starts_with("0x") { - u32::from_str_radix(&s[2..], 16) - } else { - u32::from_str(s) - } - } -} - -impl AddressDisplay for u32 { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} - -impl AddrParse for u16 { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result { - if s.starts_with("0x") { - u16::from_str_radix(&s[2..], 16) - } else { - u16::from_str(s) - } - } -} - -impl AddressDisplay for u16 { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} +use serde::{Serialize, Deserialize}; -impl Address for u16 { - fn to_linear(&self) -> usize { *self as usize } -} +mod address; +pub use address::{Address, AddressBase}; -impl Address for u32 { - fn to_linear(&self) -> usize { *self as usize } -} +mod color; +pub use color::{Colorize, NoColors, YaxColors}; -impl Address for u64 { - fn to_linear(&self) -> usize { *self as usize } -} +#[cfg(feature="colors")] +pub use color::ColorSettings; -impl Address for usize { - fn to_linear(&self) -> usize { *self } -} +pub mod display; pub trait DecodeError { fn data_exhausted(&self) -> bool; @@ -204,335 +72,13 @@ pub trait Instruction { fn well_defined(&self) -> bool; } -#[cfg(feature="use-serde")] -impl Serialize for ColorSettings { - fn serialize(&self, serializer: S) -> Result { - use serde::ser::SerializeStruct; - let s = serializer.serialize_struct("ColorSettings", 0)?; - s.end() - } -} - -#[allow(dead_code)] -pub struct ColorSettings { - arithmetic: color::Fg<&'static dyn color::Color>, - stack: color::Fg<&'static dyn color::Color>, - nop: color::Fg<&'static dyn color::Color>, - stop: color::Fg<&'static dyn color::Color>, - control: color::Fg<&'static dyn color::Color>, - data: color::Fg<&'static dyn color::Color>, - comparison: color::Fg<&'static dyn color::Color>, - invalid: color::Fg<&'static dyn color::Color>, - platform: color::Fg<&'static dyn color::Color>, - misc: color::Fg<&'static dyn color::Color>, - - register: color::Fg<&'static dyn color::Color>, - program_counter: color::Fg<&'static dyn color::Color>, - - number: color::Fg<&'static dyn color::Color>, - zero: color::Fg<&'static dyn color::Color>, - one: color::Fg<&'static dyn color::Color>, - minus_one: color::Fg<&'static dyn color::Color>, - - function: color::Fg<&'static dyn color::Color>, - symbol: color::Fg<&'static dyn color::Color>, - address: color::Fg<&'static dyn color::Color> -} - -impl Default for ColorSettings { - fn default() -> ColorSettings { - ColorSettings { - /* - arithmetic: color::Fg(&color::LightYellow), - stack: color::Fg(&color::Magenta), - nop: color::Fg(&color::Blue), - stop: color::Fg(&color::LightRed), - control: color::Fg(&color::Green), - data: color::Fg(&color::LightMagenta), - comparison: color::Fg(&color::Yellow), - invalid: color::Fg(&color::Red), - platform: color::Fg(&color::Cyan), - misc: color::Fg(&color::LightCyan), - */ - arithmetic: color::Fg(&color::Yellow), - stack: color::Fg(&color::Magenta), - nop: color::Fg(&color::Blue), - stop: color::Fg(&color::Red), - control: color::Fg(&color::Red), - data: color::Fg(&color::Yellow), - comparison: color::Fg(&color::Yellow), - invalid: color::Fg(&color::Red), - platform: color::Fg(&color::LightBlue), - misc: color::Fg(&color::LightCyan), - - register: color::Fg(&color::Cyan), - program_counter: color::Fg(&color::Red), - - number: color::Fg(&color::White), - zero: color::Fg(&color::White), - one: color::Fg(&color::White), - minus_one: color::Fg(&color::White), - - function: color::Fg(&color::LightGreen), - symbol: color::Fg(&color::LightGreen), - address: color::Fg(&color::Green) - } - } -} - -pub trait YaxColors { - fn arithmetic_op(&self, t: T) -> Colored; - fn stack_op(&self, t: T) -> Colored; - fn nop_op(&self, t: T) -> Colored; - fn stop_op(&self, t: T) -> Colored; - fn control_flow_op(&self, t: T) -> Colored; - fn data_op(&self, t: T) -> Colored; - fn comparison_op(&self, t: T) -> Colored; - fn invalid_op(&self, t: T) -> Colored; - fn platform_op(&self, t: T) -> Colored; - fn misc_op(&self, t: T) -> Colored; - - fn register(&self, t: T) -> Colored; - fn program_counter(&self, t: T) -> Colored; - fn number(&self, t: T) -> Colored; - fn zero(&self, t: T) -> Colored; - fn one(&self, t: T) -> Colored; - fn minus_one(&self, t: T) -> Colored; - fn address(&self, t: T) -> Colored; - fn symbol(&self, t: T) -> Colored; - fn function(&self, t: T) -> Colored; -} - -impl YaxColors for ColorSettings { - fn arithmetic_op(&self, t: T) -> Colored { - Colored::Color(t, self.arithmetic) - } - fn stack_op(&self, t: T) -> Colored { - Colored::Color(t, self.stack) - } - fn nop_op(&self, t: T) -> Colored { - Colored::Color(t, self.nop) - } - fn stop_op(&self, t: T) -> Colored { - Colored::Color(t, self.stop) - } - fn control_flow_op(&self, t: T) -> Colored { - Colored::Color(t, self.control) - } - fn data_op(&self, t: T) -> Colored { - Colored::Color(t, self.data) - } - fn comparison_op(&self, t: T) -> Colored { - Colored::Color(t, self.comparison) - } - fn invalid_op(&self, t: T) -> Colored { - Colored::Color(t, self.invalid) - } - fn misc_op(&self, t: T) -> Colored { - Colored::Color(t, self.misc) - } - fn platform_op(&self, t: T) -> Colored { - Colored::Color(t, self.platform) - } - - fn register(&self, t: T) -> Colored { - Colored::Color(t, self.register) - } - fn program_counter(&self, t: T) -> Colored { - Colored::Color(t, self.program_counter) - } - fn number(&self, t: T) -> Colored { - Colored::Color(t, self.number) - } - fn zero(&self, t: T) -> Colored { - Colored::Color(t, self.zero) - } - fn one(&self, t: T) -> Colored { - Colored::Color(t, self.one) - } - fn minus_one(&self, t: T) -> Colored { - Colored::Color(t, self.minus_one) - } - fn address(&self, t: T) -> Colored { - Colored::Color(t, self.address) - } - fn symbol(&self, t: T) -> Colored { - Colored::Color(t, self.symbol) - } - fn function(&self, t: T) -> Colored { - Colored::Color(t, self.function) - } -} - -impl <'a> YaxColors for Option<&'a ColorSettings> { - fn arithmetic_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.arithmetic_op(t) } - None => { Colored::Just(t) } - } - } - fn stack_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.stack_op(t) } - None => { Colored::Just(t) } - } - } - fn nop_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.nop_op(t) } - None => { Colored::Just(t) } - } - } - fn stop_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.stop_op(t) } - None => { Colored::Just(t) } - } - } - fn control_flow_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.control_flow_op(t) } - None => { Colored::Just(t) } - } - } - fn data_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.data_op(t) } - None => { Colored::Just(t) } - } - } - fn comparison_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.comparison_op(t) } - None => { Colored::Just(t) } - } - } - fn invalid_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.invalid_op(t) } - None => { Colored::Just(t) } - } - } - fn misc_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.misc_op(t) } - None => { Colored::Just(t) } - } - } - fn platform_op(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.platform_op(t) } - None => { Colored::Just(t) } - } - } - - fn register(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.register(t) } - None => { Colored::Just(t) } - } - } - fn program_counter(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.program_counter(t) } - None => { Colored::Just(t) } - } - } - fn number(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.number(t) } - None => { Colored::Just(t) } - } - } - fn zero(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.zero(t) } - None => { Colored::Just(t) } - } - } - fn one(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.one(t) } - None => { Colored::Just(t) } - } - } - fn minus_one(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.minus_one(t) } - None => { Colored::Just(t) } - } - } - fn address(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.address(t) } - None => { Colored::Just(t) } - } - } - fn symbol(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.symbol(t) } - None => { Colored::Just(t) } - } - } - fn function(&self, t: T) -> Colored { - match self { - Some(colors) => { colors.function(t) } - None => { Colored::Just(t) } - } - } -} - -pub enum Colored { - Color(T, color::Fg<&'static dyn color::Color>), - Just(T) -} - -impl Display for Colored { - fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { - match self { - Colored::Color(t, color) => { - write!(fmt, "{}{}{}", color, t, color::Fg(color::Reset)) - }, - Colored::Just(t) => { - write!(fmt, "{}", t) - } - } - } -} - -/* - * can this be a derivable trait or something? - */ -/* -impl Display for T { - fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { - self.colorize(None, fmt) - } -} -*/ - -pub trait Colorize { - fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result; -} - -/* - * and make this auto-derive from a ShowContextual impl? - */ -/* -impl Colorize for T where T: ShowContextual { - fn colorize(&self, colors: Option<&ColorSettings>, fmt: &mut Formatter) -> std::fmt::Result { - self.contextualize(colors, None, fmt) - } -} -*/ - -pub trait ShowContextual { - fn contextualize(&self, colors: Option<&ColorSettings>, address: Addr, context: Option<&Ctx>, out: &mut T) -> std::fmt::Result; +pub trait ShowContextual> { + fn contextualize(&self, colors: &Y, address: Addr, context: Option<&Ctx>, out: &mut T) -> fmt::Result; } /* -impl > ShowContextual for U { - fn contextualize(&self, colors: Option<&ColorSettings>, context: Option<&C>, out: &mut T) -> std::fmt::Result { +impl > ShowContextual for U { + fn contextualize(&self, colors: Option<&ColorSettings>, context: Option<&C>, out: &mut T) -> fmt::Result { self.colorize(colors, out) } } -- cgit v1.1