basic::jmp now implements InstTargetFmt.

This commit is contained in:
2021-01-12 02:28:22 +00:00
parent eb00824fbf
commit dbfdc01a2e
2 changed files with 18 additions and 22 deletions

View File

@@ -4,7 +4,7 @@
use crate::{
exe::inst::{
basic::{Decodable, Encodable},
InstFmt,
InstTarget, InstTargetFmt,
},
Pos,
};
@@ -51,12 +51,6 @@ pub struct Inst {
}
impl Inst {
/// Returns the target of this instruction
#[must_use]
pub fn target(self, pos: Pos) -> Pos {
Self::target_of(self.imm, pos)
}
/// Returns the target using an immediate
#[must_use]
pub fn target_of(imm: u32, pos: Pos) -> Pos {
@@ -90,10 +84,15 @@ impl Encodable for Inst {
}
}
impl InstFmt for Inst {
fn fmt(&self, pos: Pos, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl InstTarget for Inst {
fn target(&self, pos: Pos) -> Pos {
Self::target_of(self.imm, pos)
}
}
impl InstTargetFmt for Inst {
fn fmt(&self, _pos: Pos, target: impl std::fmt::Display, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mnemonic = self.kind.mnemonic();
let target = self.target(pos);
write!(f, "{mnemonic} {target}")
}

View File

@@ -166,19 +166,16 @@ fn main() -> Result<(), anyhow::Error> {
/// Returns a display-able for an instruction inside a possible function
#[must_use]
#[rustfmt::skip]
pub fn inst_display<'a>(inst: &'a Inst, exe: &'a Exe, func: Option<&'a Func>, pos: Pos) -> impl fmt::Display + 'a {
dcb_util::DisplayWrapper::new(move |f| {
match inst {
Inst::Basic (basic ::Inst::Cond (inst)) => write!(f, "{}", self::inst_target_fmt(inst, pos, self::inst_target(exe, func, inst.target(pos)))),
Inst::Basic (basic ::Inst::Jmp (inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Basic (basic ::Inst::Load (inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Basic (basic ::Inst::Store (inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Pseudo(pseudo::Inst::LoadImm(inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Pseudo(pseudo::Inst::Load (inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Pseudo(pseudo::Inst::Store (inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
inst => write!(f, "{}", self::inst_fmt(inst, pos)),
}
dcb_util::DisplayWrapper::new(move |f| match inst {
Inst::Basic(basic::Inst::Cond(inst)) => write!(f, "{}", self::inst_target_fmt(inst, pos, self::inst_target(exe, func, inst.target(pos)))),
Inst::Basic(basic::Inst::Jmp(basic::jmp::Inst::Imm(inst))) => {
write!(f, "{}", self::inst_target_fmt(inst, pos, self::inst_target(exe, func, inst.target(pos))))
},
Inst::Pseudo(pseudo::Inst::LoadImm(inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Pseudo(pseudo::Inst::Load(inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
Inst::Pseudo(pseudo::Inst::Store(inst)) => write!(f, "{}", self::inst_fmt(inst, pos)),
inst => write!(f, "{}", self::inst_fmt(inst, pos)),
})
}