diff options
| -rw-r--r-- | src/display.rs | 1 | ||||
| -rw-r--r-- | src/display/display_rules.rs | 40 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/display.rs b/src/display.rs index 0d294a2..b5451f2 100644 --- a/src/display.rs +++ b/src/display.rs @@ -16,6 +16,7 @@ pub use display_sink::InstructionTextSink; mod display_rules; pub use display_rules::{ArchDisplayRules}; +pub use display_rules::{DefaultRules, AbsoluteAddressFormatter}; /// translate a byte in range `[0, 15]` to a lowercase base-16 digit. /// diff --git a/src/display/display_rules.rs b/src/display/display_rules.rs index e6da133..abf4f95 100644 --- a/src/display/display_rules.rs +++ b/src/display/display_rules.rs @@ -1,3 +1,5 @@ +use crate::address::AddressBase; + /// `ArchDisplayRules` allows client code to control some aspects of instruction formatting. there /// are relatively few elements of instruction formatting that generalize across architectures, so /// the expected use of this trait is with additional architecture-specific `DisplayRules` traits @@ -84,3 +86,41 @@ pub trait ArchDisplayRules<A: crate::Arch, S: crate::display::DisplaySink + ?Siz Ok(false) } } + +pub struct DefaultRules { _pad: () } + +impl<A: crate::Arch, S: crate::display::DisplaySink + ?Sized> ArchDisplayRules<A, S> for DefaultRules {} + +impl DefaultRules { + pub fn new() -> Self { Self { _pad: () } } +} + +pub struct AbsoluteAddressFormatter<A: crate::Arch> { + addr: A::Address, +} + +impl<A: crate::Arch, S: crate::display::DisplaySink + ?Sized> ArchDisplayRules<A, S> for AbsoluteAddressFormatter<A> { + fn instr_addr(&self) -> Option<A::Address> { + Some(self.addr) + } + + fn emit_address(&self, addr: A::Address, s: &mut S) -> Result<bool, core::fmt::Error> { + s.span_start_immediate(); + s.span_end_immediate(); + s.write_fixed_size("0x")?; + s.write_u64(addr.to_linear() as u64)?; + s.span_end_immediate(); + Ok(true) + } +} + +impl<A: crate::Arch> AbsoluteAddressFormatter<A> { + pub fn new(addr: A::Address) -> Self { + Self { addr } + } + + pub fn advance(&mut self, instr: &A::Instruction) { + use crate::LengthedInstruction; + self.addr += instr.len(); + } +} |
