use super::{Block, Expr, Machine, RegRef, Stmt, flags};
pub const DIRECTION: u16 = super::x86_64::DIRECTION;
pub const ADDRESS: u16 = u16::MAX;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Mode {
Analysis,
Execution,
}
pub(super) struct Located<M> {
inner: M,
mode: Mode,
address: u64,
}
impl<M: Machine> Located<M> {
pub(super) fn new(inner: M, mode: Mode) -> Self {
Self {
inner,
mode,
address: 0,
}
}
}
fn marker(address: u64) -> Stmt {
Stmt::Assign {
dest: RegRef {
index: ADDRESS,
width: 64,
},
value: Expr::Const(address),
at: None,
}
}
impl<M: Machine> Machine for Located<M> {
type Instruction = M::Instruction;
fn branch_target(&self, instruction: &Self::Instruction, pc: u64) -> Option<u64> {
self.inner.branch_target(instruction, pc)
}
fn out(&mut self) -> &mut super::Emitter {
self.inner.out()
}
fn origin(&self, instruction: &Self::Instruction, pc: u64) -> u64 {
self.inner.origin(instruction, pc)
}
fn name(&self, instruction: &Self::Instruction) -> String {
self.inner.name(instruction)
}
fn written(&mut self, instruction: &Self::Instruction) -> Option<Vec<u16>> {
self.inner.written(instruction)
}
fn sets_flags(&self, instruction: &Self::Instruction) -> bool {
self.inner.sets_flags(instruction)
}
fn reads_flags(&self, instruction: &Self::Instruction) -> bool {
self.inner.reads_flags(instruction)
}
fn step(&mut self, instruction: &Self::Instruction, pc: u64) -> bool {
if self.mode == Mode::Execution {
self.inner.out().stmts.push(marker(pc));
self.address = pc;
}
self.inner.step(instruction, pc)
}
fn enter_block(&mut self) {
self.inner.enter_block();
}
fn rules(&self) -> super::constants::Rules {
self.inner.rules()
}
fn take_extra_blocks(&mut self) -> Vec<(Block, Option<flags::Flags>)> {
let mut blocks = self.inner.take_extra_blocks();
if self.mode == Mode::Execution {
for (block, _) in &mut blocks {
block.stmts.insert(0, marker(self.address));
}
}
blocks
}
}