mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-06 17:35:40 +00:00
Moved displaying context to it's own module and renamed it.
This commit is contained in:
parent
475c3c241b
commit
53a214602e
89
dcb-tools/dcb-decompiler/src/display_ctx.rs
Normal file
89
dcb-tools/dcb-decompiler/src/display_ctx.rs
Normal file
@ -0,0 +1,89 @@
|
||||
//! Display context
|
||||
|
||||
// Imports
|
||||
use dcb_exe::{inst, ExeReader, Func, Pos};
|
||||
use std::fmt;
|
||||
|
||||
/// Displaying context for instructions.
|
||||
pub struct DisplayCtx<'a> {
|
||||
/// Executable
|
||||
exe: &'a ExeReader,
|
||||
|
||||
/// Current function, if any.
|
||||
func: Option<&'a Func>,
|
||||
|
||||
/// Current Position
|
||||
pos: Pos,
|
||||
}
|
||||
|
||||
impl<'a> DisplayCtx<'a> {
|
||||
/// Creates a new display context
|
||||
pub fn new(exe: &'a ExeReader, func: Option<&'a Func>, pos: Pos) -> Self {
|
||||
Self { exe, func, pos }
|
||||
}
|
||||
}
|
||||
|
||||
/// Label display for `DisplayCtx::pos_label`
|
||||
pub enum LabelDisplay<'a> {
|
||||
/// Label within the current function
|
||||
CurFuncLabel(&'a str),
|
||||
|
||||
/// Label within another function
|
||||
OtherFuncLabel { func: &'a str, label: &'a str },
|
||||
|
||||
/// A function itself
|
||||
OtherFunc { func: &'a str },
|
||||
|
||||
/// A data
|
||||
Data { name: &'a str },
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for LabelDisplay<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
LabelDisplay::CurFuncLabel(label) => write!(f, ".{label}"),
|
||||
LabelDisplay::OtherFuncLabel { func, label } => write!(f, "{func}.{label}"),
|
||||
LabelDisplay::OtherFunc { func } => write!(f, "{func}"),
|
||||
LabelDisplay::Data { name } => write!(f, "{name}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> inst::DisplayCtx for DisplayCtx<'a> {
|
||||
type Label = LabelDisplay<'a>;
|
||||
|
||||
fn cur_pos(&self) -> Pos {
|
||||
self.pos
|
||||
}
|
||||
|
||||
fn pos_label(&self, pos: Pos) -> Option<(Self::Label, i64)> {
|
||||
// Try to get a label for the current function, if it exists
|
||||
if let Some(label) = self.func.and_then(|func| func.labels.get(&pos)) {
|
||||
return Some((LabelDisplay::CurFuncLabel(label), 0));
|
||||
}
|
||||
|
||||
// Try to get a function from it
|
||||
if let Some(func) = self.exe.func_table().get_containing(pos) {
|
||||
// And then one of it's labels
|
||||
if let Some(label) = func.labels.get(&pos) {
|
||||
return Some((
|
||||
LabelDisplay::OtherFuncLabel {
|
||||
func: &func.name,
|
||||
label,
|
||||
},
|
||||
0,
|
||||
));
|
||||
}
|
||||
|
||||
// Else just any position in it
|
||||
return Some((LabelDisplay::OtherFunc { func: &func.name }, pos - func.start_pos));
|
||||
}
|
||||
|
||||
// Else try a data
|
||||
if let Some(data) = self.exe.data_table().get_containing(pos) {
|
||||
return Some((LabelDisplay::Data { name: data.name() }, pos - data.start_pos()));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
@ -4,11 +4,15 @@
|
||||
|
||||
// Modules
|
||||
mod cli;
|
||||
mod display_ctx;
|
||||
|
||||
// Exports
|
||||
use display_ctx::DisplayCtx;
|
||||
|
||||
// Imports
|
||||
use anyhow::Context;
|
||||
use dcb_exe::{
|
||||
inst::{basic, parse::LineArgExpr, DisplayCtx, Inst, InstDisplay, InstFmtArg, ParseCtx},
|
||||
inst::{basic, parse::LineArgExpr, Inst, InstDisplay, InstFmtArg, ParseCtx},
|
||||
reader::{iter::ExeItem, DeserializeOpts},
|
||||
Data, ExeReader, Func, Pos,
|
||||
};
|
||||
@ -281,7 +285,7 @@ pub fn inst_display<'a>(
|
||||
// Overload the target of as many as possible using `inst_target`.
|
||||
dcb_util::DisplayWrapper::new(move |f| {
|
||||
// Build the context and get the mnemonic + args
|
||||
let ctx = Ctx { exe, func, pos };
|
||||
let ctx = DisplayCtx::new(exe, func, pos);
|
||||
let mnemonic = inst.mnemonic(&ctx);
|
||||
let args = inst.args(&ctx);
|
||||
|
||||
@ -350,79 +354,6 @@ pub fn inst_display<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
/// Display context
|
||||
struct Ctx<'a> {
|
||||
/// Exe
|
||||
exe: &'a ExeReader,
|
||||
|
||||
/// Function
|
||||
func: Option<&'a Func>,
|
||||
|
||||
/// Current Position
|
||||
pos: Pos,
|
||||
}
|
||||
|
||||
/// Label display for `DisplayCtx::pos_label`
|
||||
enum LabelDisplay<'a> {
|
||||
CurFuncLabel(&'a str),
|
||||
|
||||
OtherFuncLabel { func: &'a str, label: &'a str },
|
||||
|
||||
OtherFunc { func: &'a str },
|
||||
|
||||
Data { name: &'a str },
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for LabelDisplay<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
LabelDisplay::CurFuncLabel(label) => write!(f, ".{label}"),
|
||||
LabelDisplay::OtherFuncLabel { func, label } => write!(f, "{func}.{label}"),
|
||||
LabelDisplay::OtherFunc { func } => write!(f, "{func}"),
|
||||
LabelDisplay::Data { name } => write!(f, "{name}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DisplayCtx for Ctx<'a> {
|
||||
type Label = LabelDisplay<'a>;
|
||||
|
||||
fn cur_pos(&self) -> Pos {
|
||||
self.pos
|
||||
}
|
||||
|
||||
fn pos_label(&self, pos: Pos) -> Option<(Self::Label, i64)> {
|
||||
// Try to get a label for the current function, if it exists
|
||||
if let Some(label) = self.func.and_then(|func| func.labels.get(&pos)) {
|
||||
return Some((LabelDisplay::CurFuncLabel(label), 0));
|
||||
}
|
||||
|
||||
// Try to get a function from it
|
||||
if let Some(func) = self.exe.func_table().get_containing(pos) {
|
||||
// And then one of it's labels
|
||||
if let Some(label) = func.labels.get(&pos) {
|
||||
return Some((
|
||||
LabelDisplay::OtherFuncLabel {
|
||||
func: &func.name,
|
||||
label,
|
||||
},
|
||||
0,
|
||||
));
|
||||
}
|
||||
|
||||
// Else just any position in it
|
||||
return Some((LabelDisplay::OtherFunc { func: &func.name }, pos - func.start_pos));
|
||||
}
|
||||
|
||||
// Else try a data
|
||||
if let Some(data) = self.exe.data_table().get_containing(pos) {
|
||||
return Some((LabelDisplay::Data { name: data.name() }, pos - data.start_pos()));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Argument position
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user