From 83db95939a158991befc25ace78c1fd3479a4152 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Tue, 12 Jan 2021 02:40:15 +0000 Subject: [PATCH] `pseudo::{load, store}` now implement `InstTarget` and `InstTagetFmt`. --- dcb-exe/src/exe/inst/pseudo/load.rs | 14 ++++++++++---- dcb-exe/src/exe/inst/pseudo/store.rs | 14 ++++++++++---- dcb-tools/src/decompiler/main.rs | 5 +++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/dcb-exe/src/exe/inst/pseudo/load.rs b/dcb-exe/src/exe/inst/pseudo/load.rs index b2284c6..4cca1b5 100644 --- a/dcb-exe/src/exe/inst/pseudo/load.rs +++ b/dcb-exe/src/exe/inst/pseudo/load.rs @@ -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}") diff --git a/dcb-exe/src/exe/inst/pseudo/store.rs b/dcb-exe/src/exe/inst/pseudo/store.rs index b1a9ae9..97fac38 100644 --- a/dcb-exe/src/exe/inst/pseudo/store.rs +++ b/dcb-exe/src/exe/inst/pseudo/store.rs @@ -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}") diff --git a/dcb-tools/src/decompiler/main.rs b/dcb-tools/src/decompiler/main.rs index 9c82930..50f4479 100644 --- a/dcb-tools/src/decompiler/main.rs +++ b/dcb-tools/src/decompiler/main.rs @@ -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)), }) }