use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Mode {
Real16,
Real32,
Bits16,
Bits32,
Bits64,
}
impl Mode {
pub const fn bits(self) -> u32 {
match self {
Self::Real16 | Self::Bits16 => 16,
Self::Real32 | Self::Bits32 => 32,
Self::Bits64 => 64,
}
}
pub const fn is_real(self) -> bool {
matches!(self, Self::Real16 | Self::Real32)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum DecodeError {
TooShort = 1,
TooLong,
Invalid,
InvalidMode,
BadRegister,
BadLockPrefix,
BadLegacyPrefix,
BadRexPrefix,
BadMap,
BadEvexVPrime,
BadEvexZeroingWithoutMask,
BadEvexLength,
GatherRegisters,
BadRegisterMatch,
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::TooShort => "instruction is truncated",
Self::TooLong => "instruction is longer than 15 bytes",
Self::Invalid => "invalid instruction",
Self::InvalidMode => "instruction is invalid in this mode",
Self::BadRegister => "invalid register encoding",
Self::BadLockPrefix => "LOCK prefix is not allowed",
Self::BadLegacyPrefix => "66, F2 or F3 prefix is not allowed",
Self::BadRexPrefix => "REX prefix is not allowed",
Self::BadMap => "invalid opcode map",
Self::BadEvexVPrime => "EVEX.V' must be 1 outside 64-bit mode",
Self::BadEvexZeroingWithoutMask => "EVEX.z requires a mask register",
Self::BadEvexLength => "EVEX.L'L is 3 without embedded rounding",
Self::GatherRegisters => "gather index, destination and mask registers must differ",
Self::BadRegisterMatch => "registers that must differ are the same",
})
}
}
impl core::error::Error for DecodeError {}
impl DecodeError {
const ALL: [Self; 14] = [
Self::TooShort,
Self::TooLong,
Self::Invalid,
Self::InvalidMode,
Self::BadRegister,
Self::BadLockPrefix,
Self::BadLegacyPrefix,
Self::BadRexPrefix,
Self::BadMap,
Self::BadEvexVPrime,
Self::BadEvexZeroingWithoutMask,
Self::BadEvexLength,
Self::GatherRegisters,
Self::BadRegisterMatch,
];
#[inline]
pub fn from_code(code: u8) -> Option<Self> {
Self::ALL.get(usize::from(code).checked_sub(1)?).copied()
}
}