pub mod flags;
pub mod flow;
pub mod format;
pub mod operand;
use flags::FlagsRule;
use flow::FlowKind;
use binbun_isa::FlowStep;
use crate::fast;
use crate::generated::form::{Attribute, Category, Extension, Form, INSTRUCTIONS, IsaSet};
use crate::generated::handlers::{HANDLERS, HANDLERS64, Handler, generic};
use crate::generated::state::{Fields, State};
use crate::{DecodeError, Mnemonic, Mode, Register, RegisterClass};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Attributes(pub u128);
impl Attributes {
pub const fn contains(self, attribute: Attribute) -> bool {
self.0 & (1 << attribute as u8) != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InstructionInfo {
pub form: Form,
pub attributes: Attributes,
pub category: Category,
pub extension: Extension,
pub isa_set: IsaSet,
pub operands: u16,
pub operand_count: u8,
pub intel: &'static str,
pub flags: FlagsRule,
pub flow: FlowKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Instruction {
number: u16,
fields: Fields,
}
impl Instruction {
#[allow(clippy::len_without_is_empty, reason = "an instruction is never empty")]
pub const fn len(&self) -> usize {
self.fields.pos
}
#[expect(
clippy::indexing_slicing,
reason = "`decode` only returns numbers of table entries"
)]
#[inline]
fn info(&self) -> &'static InstructionInfo {
&INSTRUCTIONS[usize::from(self.number) - 1]
}
pub fn form(&self) -> Form {
self.info().form
}
#[inline]
pub fn mnemonic(&self) -> Mnemonic {
self.info().form.mnemonic()
}
pub fn attributes(&self) -> Attributes {
self.info().attributes
}
pub fn category(&self) -> Category {
self.info().category
}
pub fn extension(&self) -> Extension {
self.info().extension
}
pub fn isa_set(&self) -> IsaSet {
self.info().isa_set
}
pub const fn encoding(&self) -> Encoding {
match self.fields.vexvalid {
1 => Encoding::Vex,
2 => Encoding::Evex,
3 => Encoding::Xop,
_ => Encoding::Legacy,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Encoding {
Legacy,
Vex,
Evex,
Xop,
}
pub fn decode(bytes: &[u8], mode: Mode) -> Result<Instruction, DecodeError> {
if mode == Mode::Bits64 {
fast::decode(bytes)
} else {
decode_other(bytes, mode)
}
}
pub fn decode_flow(bytes: &[u8], mode: Mode, address: u64) -> Result<FlowStep, DecodeError> {
if mode == Mode::Bits64 {
fast::decode_flow(bytes, address)
} else {
decode_other(bytes, mode).map(|i| i.flow_step(address))
}
}
#[inline(never)]
fn decode_other(bytes: &[u8], mode: Mode) -> Result<Instruction, DecodeError> {
decode_in::<false>(bytes, mode)
}
#[cfg(feature = "internal-reference")]
#[doc(hidden)]
pub fn decode_reference(bytes: &[u8], mode: Mode) -> Result<Instruction, DecodeError> {
if mode == Mode::Bits64 {
decode_in::<true>(bytes, mode)
} else {
decode_other(bytes, mode)
}
}
#[expect(
clippy::inline_always,
reason = "each copy folds its mode into the inlined length decoder"
)]
#[inline(always)]
pub fn decode_in<const MODE64: bool>(bytes: &[u8], mode: Mode) -> Result<Instruction, DecodeError> {
let mut state = State::default();
state.load(bytes, mode);
let table = state.head();
let number = if state.out_of_bytes == 0 {
handler::<MODE64>(table, state.nominal_opcode)(&mut state)
} else {
0
};
if let Some(error) = DecodeError::from_code(state.error) {
return Err(error);
}
let info = usize::from(number)
.checked_sub(1)
.and_then(|index| INSTRUCTIONS.get(index))
.ok_or(DecodeError::Invalid)?;
if let Some(error) = finalize(&state, info) {
return Err(error);
}
Ok(Instruction {
number,
fields: state.fields,
})
}
#[inline]
fn handler<const MODE64: bool>(table: usize, opcode: u8) -> Handler {
let handlers = match HANDLERS64.get(table) {
Some(handlers) if MODE64 => Some(handlers),
_ => HANDLERS.get(table),
};
handlers
.and_then(|table| table.get(usize::from(opcode)))
.copied()
.unwrap_or(generic)
}
#[expect(
clippy::inline_always,
reason = "with a constant `number` the checks fold away"
)]
#[inline(always)]
pub fn checked(number: u16, fields: Fields) -> Option<Instruction> {
let info = usize::from(number)
.checked_sub(1)
.and_then(|index| INSTRUCTIONS.get(index))?;
if needs_finalize(&fields, info) && finalize(&fields, info).is_some() {
return None;
}
Some(Instruction { number, fields })
}
#[inline]
fn needs_finalize(s: &Fields, info: &InstructionInfo) -> bool {
s.lock != 0 || s.realmode != 0 || info.attributes.0 & FINALIZED != 0
}
const FINALIZED: u128 = 1 << Attribute::NoSrcDestMatch as u32
| 1 << Attribute::NoRegMatch as u32
| 1 << Attribute::Gather as u32
| 1 << Attribute::NoRipRel as u32;
fn finalize(s: &Fields, info: &InstructionInfo) -> Option<DecodeError> {
if !needs_finalize(s, info) {
return None;
}
let has = |attribute| info.attributes.contains(attribute);
if s.lock != 0 && !has(Attribute::Locked) {
return Some(DecodeError::BadLockPrefix);
}
let mut error = None;
let second = if has(Attribute::MaskopEvex) {
s.reg3
} else {
s.reg1
};
if has(Attribute::NoSrcDestMatch) && (s.reg0 == second || s.reg0 == s.reg2) {
error = Some(DecodeError::BadRegisterMatch);
}
if has(Attribute::NoRegMatch) && (s.reg0 == second || second == s.reg2 || s.reg0 == s.reg2) {
error = Some(DecodeError::BadRegisterMatch);
}
if has(Attribute::Gather) {
let id = vector_number;
if info.extension == Extension::Avx2gather {
let (index, dest, mask) = (id(s.index), id(s.reg0), id(s.reg1));
if index == mask || index == dest || mask == dest {
error = Some(DecodeError::GatherRegisters);
}
} else if has(Attribute::MaskopEvex) && id(s.index) == id(s.reg0) {
error = Some(DecodeError::GatherRegisters);
}
}
if has(Attribute::NoRipRel) && s.rm == 5 && s.mod_ == 0 && s.mode == 2 {
error = Some(DecodeError::Invalid);
}
if s.realmode != 0
&& (s.vexvalid != 0 || has(Attribute::ProtectedMode))
&& !matches!(info.isa_set, IsaSet::Sep | IsaSet::Smx)
{
error = Some(DecodeError::InvalidMode);
}
error
}
fn vector_number(raw: u16) -> u16 {
let first = match Register::from_raw(raw).map(Register::class) {
Some(RegisterClass::Zmm) => Register::ZMM0,
Some(RegisterClass::Ymm) => Register::YMM0,
_ => Register::XMM0,
};
raw.wrapping_sub(first as u16)
}