pseudo::{load, store} now implement InstTarget and InstTagetFmt.

This commit is contained in:
2021-01-12 02:40:15 +00:00
parent 12f2abc638
commit 83db95939a
3 changed files with 23 additions and 10 deletions

View File

@@ -2,7 +2,7 @@
// Imports
use crate::{
exe::inst::{basic, InstFmt, InstSize, Register},
exe::inst::{basic, InstSize, InstTarget, InstTargetFmt, Register},
Pos,
};
use int_conv::{Join, SignExtended, Signed};
@@ -52,9 +52,15 @@ impl InstSize for Inst {
}
}
impl InstFmt for Inst {
fn fmt(&self, _pos: crate::Pos, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let Self { dst, kind, target } = self;
impl InstTarget for Inst {
fn target(&self, _pos: Pos) -> Pos {
self.target
}
}
impl InstTargetFmt for Inst {
fn fmt(&self, _pos: crate::Pos, target: impl std::fmt::Display, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let Self { dst, kind, .. } = self;
let mnemonic = kind.mnemonic();
write!(f, "{mnemonic} {dst}, {target}")

View File

@@ -2,7 +2,7 @@
// Imports
use crate::{
exe::inst::{basic, InstFmt, InstSize, Register},
exe::inst::{basic, InstSize, InstTarget, InstTargetFmt, Register},
Pos,
};
use int_conv::{Join, SignExtended, Signed};
@@ -52,9 +52,15 @@ impl InstSize for Inst {
}
}
impl InstFmt for Inst {
fn fmt(&self, _pos: crate::Pos, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let Self { dst, kind, target } = self;
impl InstTarget for Inst {
fn target(&self, _pos: Pos) -> Pos {
self.target
}
}
impl InstTargetFmt for Inst {
fn fmt(&self, _pos: crate::Pos, target: impl std::fmt::Display, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let Self { dst, kind, .. } = self;
let mnemonic = kind.mnemonic();
write!(f, "{mnemonic} {dst}, {target}")

View File

@@ -167,6 +167,7 @@ fn main() -> Result<(), anyhow::Error> {
/// Returns a display-able for an instruction inside a possible function
#[must_use]
pub fn inst_display<'a>(inst: &'a Inst, exe: &'a Exe, func: Option<&'a Func>, pos: Pos) -> impl fmt::Display + 'a {
// Overload the target of as many as possible using `inst_target`.
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))) => {
@@ -179,8 +180,8 @@ pub fn inst_display<'a>(inst: &'a Inst, exe: &'a Exe, func: Option<&'a Func>, po
..
},
)) => write!(f, "{}", self::inst_target_fmt(inst, pos, self::inst_target(exe, func, Pos(*target)))),
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::Pseudo(pseudo::Inst::Load(inst)) => write!(f, "{}", self::inst_target_fmt(inst, pos, self::inst_target(exe, func, inst.target(pos)))),
Inst::Pseudo(pseudo::Inst::Store(inst)) => write!(f, "{}", self::inst_target_fmt(inst, pos, self::inst_target(exe, func, inst.target(pos)))),
inst => write!(f, "{}", self::inst_fmt(inst, pos)),
})
}