mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-09 11:48:16 +00:00
basic::jmp now implements InstTargetFmt.
This commit is contained in:
@@ -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}")
|
||||
}
|
||||
|
||||
@@ -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)),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user