diff --git a/dcb-exe/src/exe/inst.rs b/dcb-exe/src/exe/inst.rs index 2d0d5a1..a6cdd43 100644 --- a/dcb-exe/src/exe/inst.rs +++ b/dcb-exe/src/exe/inst.rs @@ -21,18 +21,19 @@ // Modules pub mod basic; pub mod directive; +pub mod fmt; pub mod iter; pub mod pseudo; pub mod reg; // Exports pub use directive::Directive; +pub use fmt::InstFmt; pub use iter::ParseIter; pub use reg::Register; // Imports use crate::Pos; -use std::fmt; /// An assembler instruction. #[derive(PartialEq, Eq, Clone, Copy, Debug)] @@ -65,7 +66,7 @@ impl InstFmt for Inst { } } - fn fmt(&self, pos: Pos, bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, pos: Pos, bytes: &[u8], f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::Basic(inst) => inst.fmt(pos, bytes, f), Self::Pseudo(inst) => inst.fmt(pos, bytes, f), @@ -73,41 +74,3 @@ impl InstFmt for Inst { } } } - -/// A formattable basic instruction -/// -/// This trait defines formatting for all instruction, which may require the -/// instruction's current position (for relative instructions, such as the -/// branching instructions), as well as the byte array containing the entire -/// executable. -pub trait InstFmt { - /// Returns this instruction's mnemonic - fn mnemonic(&self) -> &'static str; - - /// Formats this instruction given it's position and input bytes - fn fmt(&self, pos: Pos, bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result; - - /// Returns a wrapped value that may be formatted using [`fmt::Display`] - fn fmt_value<'a>(&'a self, pos: Pos, bytes: &'a [u8]) -> InstFmtWrapper { - InstFmtWrapper { inst: self, pos, bytes } - } -} - -/// Wrapper over [`InstFmt`] values to be displayed using [`fmt::Display`] -#[derive(Clone, Copy, Debug)] -pub struct InstFmtWrapper<'a, T: ?Sized + InstFmt> { - /// Value - pub inst: &'a T, - - /// Position - pub pos: Pos, - - /// Bytes - pub bytes: &'a [u8], -} - -impl<'a, T: ?Sized + InstFmt> fmt::Display for InstFmtWrapper<'a, T> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.inst.fmt(self.pos, self.bytes, f) - } -} diff --git a/dcb-exe/src/exe/inst/fmt.rs b/dcb-exe/src/exe/inst/fmt.rs new file mode 100644 index 0000000..dc97fae --- /dev/null +++ b/dcb-exe/src/exe/inst/fmt.rs @@ -0,0 +1,45 @@ +//! Instruction formatting +//! +//! See the [`InstFmt`] type for more details. + +// Imports +use crate::Pos; +use std::fmt; + +/// A formattable basic instruction +/// +/// This trait defines formatting for all instruction, which may require the +/// instruction's current position (for relative instructions, such as the +/// branching instructions), as well as the byte array containing the entire +/// executable. +pub trait InstFmt { + /// Returns this instruction's mnemonic + fn mnemonic(&self) -> &'static str; + + /// Formats this instruction + fn fmt(&self, pos: Pos, bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result; + + /// Returns a wrapped value that may be formatted using [`fmt::Display`] + fn fmt_value<'a>(&'a self, pos: Pos, bytes: &'a [u8]) -> InstFmtWrapper { + InstFmtWrapper { inst: self, pos, bytes } + } +} + +/// Wrapper over [`InstFmt`] values to be displayed using [`fmt::Display`] +#[derive(Clone, Copy, Debug)] +pub struct InstFmtWrapper<'a, T: ?Sized + InstFmt> { + /// Value + pub inst: &'a T, + + /// Position + pub pos: Pos, + + /// Bytes + pub bytes: &'a [u8], +} + +impl<'a, T: ?Sized + InstFmt> fmt::Display for InstFmtWrapper<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inst.fmt(self.pos, self.bytes, f) + } +} diff --git a/dcb-exe/src/exe/inst/raw.rs b/dcb-exe/src/exe/inst/raw.rs deleted file mode 100644 index f5a9bdf..0000000 --- a/dcb-exe/src/exe/inst/raw.rs +++ /dev/null @@ -1,29 +0,0 @@ -//! Raw instructions - -// Imports -use crate::exe::Pos; - -/// A raw instruction -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub struct Raw { - /// The raw encoding of the instruction - pub repr: u32, - - /// The position of this instruction - pub pos: Pos, -} - -/* -/// Raw instruction decoding -/// -/// Implementors should be atomic about the consumed and -/// returned iterator, that is, consume the least possible -/// input in order to produce an atomic part of themselves. -pub trait FromRawIter: Sized { - /// Returned iterator from [`decode`]. - type Decoded: IntoIterator; - - /// Attempts to decode an instruction from an iterator of raw instructions - fn decode + Clone>(iter: &mut I) -> Self::Decoded; -} -*/