Removed the dcb-exe::exe::raw module (from last commit).

Moved `InstFmt` to it's own module.
This commit is contained in:
Filipe Rodrigues 2021-01-09 15:07:28 +00:00
parent c0b1ee4fc5
commit 88db3def6f
3 changed files with 48 additions and 69 deletions

View File

@ -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<Self> {
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)
}
}

View File

@ -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<Self> {
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)
}
}

View File

@ -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<Item = (Pos, Self)>;
/// Attempts to decode an instruction from an iterator of raw instructions
fn decode<I: Iterator<Item = Raw> + Clone>(iter: &mut I) -> Self::Decoded;
}
*/