use binbun_isa::{Flow, FlowStep, Target};
use super::Instruction;
use super::operand::{Memory, displacement};
use crate::generated::operand::OperandName;
use crate::generated::state::Fields;
use crate::{Mnemonic, Register};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Condition {
Overflow,
NotOverflow,
Below,
NotBelow,
Zero,
NotZero,
BelowOrEqual,
Above,
Sign,
NotSign,
Parity,
NotParity,
Less,
NotLess,
LessOrEqual,
Greater,
False,
True,
CountZero,
Loop,
LoopZero,
LoopNotZero,
TransactionAbort,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FlowKind {
Next,
Jump,
Branch(Condition),
Call,
Return,
Trap,
ConditionalTrap,
SystemCall,
ExceptionReturn,
Halt,
}
#[expect(
clippy::inline_always,
reason = "inlined into the fast path, whose decoder state must not escape"
)]
#[inline(always)]
pub fn relative_target(instruction: &Instruction, address: u64, displacement: i64) -> u64 {
let s = &instruction.fields;
let mut target = address
.wrapping_add(instruction.len() as u64)
.wrapping_add(displacement.cast_unsigned());
if s.eosz == 1 && instruction.mnemonic() != Mnemonic::Xbegin {
target &= 0xffff;
}
if s.mode != 2 {
target &= 0xffff_ffff;
}
target
}
impl Instruction {
#[expect(
clippy::inline_always,
reason = "with a constant instruction number only what its flow needs remains"
)]
#[inline(always)]
pub(crate) fn flow_step(&self, address: u64) -> FlowStep {
let s = &self.fields;
let computed_address = (s.agen != 0
&& Register::from_raw(s.base0) == Some(Register::RIP)
&& Register::from_raw(s.index).is_none())
.then(|| {
address
.wrapping_add(self.len() as u64)
.wrapping_add(displacement(s).cast_unsigned())
});
FlowStep {
len: self.len(),
flow: self.flow(address),
computed_address,
}
}
#[expect(
clippy::inline_always,
reason = "inlined into the fast path, whose decoder state must not escape"
)]
#[inline(always)]
pub fn flow(&self, address: u64) -> Flow {
match self.info().flow {
FlowKind::Next => Flow::Next,
FlowKind::Jump => Flow::Jump(self.target(address)),
FlowKind::Branch(_) => Flow::Branch(self.target(address)),
FlowKind::Call => Flow::Call(self.target(address)),
FlowKind::Return => Flow::Return,
FlowKind::Trap => Flow::Trap { conditional: false },
FlowKind::ConditionalTrap => Flow::Trap { conditional: true },
FlowKind::SystemCall => Flow::SystemCall,
FlowKind::ExceptionReturn => Flow::ExceptionReturn,
FlowKind::Halt => Flow::Halt,
}
}
pub fn condition(&self) -> Option<Condition> {
self.mnemonic().condition()
}
#[expect(
clippy::inline_always,
reason = "with a constant instruction number only one arm remains"
)]
#[inline(always)]
fn target(&self, address: u64) -> Target {
let s = &self.fields;
let Some(template) = self.templates().next() else {
return Target::Indirect;
};
match template.name {
OperandName::Relbr => Target::Address(relative_target(self, address, s.disp)),
OperandName::Absbr => Target::Address(s.disp.cast_unsigned()),
OperandName::Ptr => Target::Far {
#[expect(
clippy::cast_possible_truncation,
reason = "the selector is a 16-bit immediate"
)]
selector: s.uimm0 as u16,
offset: s.disp.cast_unsigned() & mask(s.brdisp_width),
},
OperandName::Mem0 => self.pointer(address, &self.memory(0)),
OperandName::Agen
| OperandName::Base0
| OperandName::Base1
| OperandName::Imm0
| OperandName::Imm1
| OperandName::Mem1
| OperandName::Reg0
| OperandName::Reg1
| OperandName::Reg2
| OperandName::Reg3
| OperandName::Reg4
| OperandName::Reg5
| OperandName::Reg6
| OperandName::Reg7
| OperandName::Reg8
| OperandName::Reg9 => Target::Indirect,
}
}
fn pointer(&self, address: u64, memory: &Memory) -> Target {
let far = matches!(self.mnemonic(), Mnemonic::JmpFar | Mnemonic::CallFar);
let based = matches!(memory.segment, Some(Register::FS | Register::GS));
if far || based || memory.index.is_some() {
return Target::Indirect;
}
let next = address.wrapping_add(self.len() as u64);
let at = match memory.base {
Some(Register::RIP) => next.wrapping_add(memory.displacement.cast_unsigned()),
Some(Register::EIP) => {
next.wrapping_add(memory.displacement.cast_unsigned()) & 0xffff_ffff
}
None => memory.displacement.cast_unsigned() & mask(address_bits(&self.fields)),
Some(_) => return Target::Indirect,
};
match u8::try_from(memory.size) {
Ok(size) => Target::Pointer { address: at, size },
Err(_) => Target::Indirect,
}
}
}
fn address_bits(s: &Fields) -> u8 {
match s.easz {
1 => 16,
2 => 32,
_ => 64,
}
}
fn mask(bits: u8) -> u64 {
if bits >= 64 {
u64::MAX
} else {
(1 << bits) - 1
}
}
#[cfg(test)]
mod tests {
use super::FlowKind;
use crate::generated::form::INSTRUCTIONS;
use crate::generated::operand::{OperandName, SEQUENCES, template};
#[test]
fn direct_targets_belong_to_transfers() {
for info in &INSTRUCTIONS {
let first = usize::from(info.operands);
let names = || {
SEQUENCES[first..first + usize::from(info.operand_count)]
.iter()
.map(|&index| template(index).name)
};
let direct = names().any(|n| {
matches!(
n,
OperandName::Relbr | OperandName::Absbr | OperandName::Ptr
)
});
if direct {
assert!(
matches!(
info.flow,
FlowKind::Jump | FlowKind::Branch(_) | FlowKind::Call
),
"{:?} has a target but flow {:?}",
info.form,
info.flow
);
}
if matches!(info.flow, FlowKind::Branch(_)) {
assert!(names().any(|n| n == OperandName::Relbr), "{:?}", info.form);
}
}
}
}