Skip to main content

binbun_x86/
error.rs

1//! Decoding modes and errors.
2
3use core::fmt;
4
5/// The processor mode instructions are decoded for.
6///
7/// Compatibility mode decodes like protected mode of the same width.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub enum Mode {
10    /// Real-address mode with 16-bit code segments.
11    Real16,
12    /// Real-address mode with a 32-bit code segment (`CS.D = 1`).
13    Real32,
14    /// 16-bit protected or compatibility mode.
15    Bits16,
16    /// 32-bit protected or compatibility mode.
17    Bits32,
18    /// 64-bit mode.
19    Bits64,
20}
21
22impl Mode {
23    /// Default operand and address size in bits: 16, 32 or 64.
24    pub const fn bits(self) -> u32 {
25        match self {
26            Self::Real16 | Self::Bits16 => 16,
27            Self::Real32 | Self::Bits32 => 32,
28            Self::Bits64 => 64,
29        }
30    }
31
32    /// Whether this is real-address mode.
33    pub const fn is_real(self) -> bool {
34        matches!(self, Self::Real16 | Self::Real32)
35    }
36}
37
38/// Why bytes do not decode to an instruction. The variants follow XED's
39/// `xed_error_enum_t`.
40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
41#[repr(u8)]
42pub enum DecodeError {
43    /// The bytes end before the instruction does.
44    TooShort = 1,
45    /// The instruction would be longer than 15 bytes.
46    TooLong,
47    /// No instruction has this encoding.
48    Invalid,
49    /// The instruction does not exist in the decoding mode.
50    InvalidMode,
51    /// A register field encodes no valid register.
52    BadRegister,
53    /// `LOCK` on an instruction that does not allow it.
54    BadLockPrefix,
55    /// `66`, `F2` or `F3` where none is allowed (e.g. before VEX).
56    BadLegacyPrefix,
57    /// `REX` where none is allowed (e.g. before VEX or REX2).
58    BadRexPrefix,
59    /// An opcode map that does not exist.
60    BadMap,
61    /// `EVEX.V'` is 0 outside 64-bit mode.
62    BadEvexVPrime,
63    /// `EVEX.z` is set without a mask register.
64    BadEvexZeroingWithoutMask,
65    /// `EVEX.L'L` is 3 without embedded rounding.
66    BadEvexLength,
67    /// An AVX2 gather's index, destination and mask registers are not all
68    /// different.
69    GatherRegisters,
70    /// Registers that must differ are the same.
71    BadRegisterMatch,
72}
73
74impl fmt::Display for DecodeError {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        f.write_str(match self {
77            Self::TooShort => "instruction is truncated",
78            Self::TooLong => "instruction is longer than 15 bytes",
79            Self::Invalid => "invalid instruction",
80            Self::InvalidMode => "instruction is invalid in this mode",
81            Self::BadRegister => "invalid register encoding",
82            Self::BadLockPrefix => "LOCK prefix is not allowed",
83            Self::BadLegacyPrefix => "66, F2 or F3 prefix is not allowed",
84            Self::BadRexPrefix => "REX prefix is not allowed",
85            Self::BadMap => "invalid opcode map",
86            Self::BadEvexVPrime => "EVEX.V' must be 1 outside 64-bit mode",
87            Self::BadEvexZeroingWithoutMask => "EVEX.z requires a mask register",
88            Self::BadEvexLength => "EVEX.L'L is 3 without embedded rounding",
89            Self::GatherRegisters => "gather index, destination and mask registers must differ",
90            Self::BadRegisterMatch => "registers that must differ are the same",
91        })
92    }
93}
94
95impl core::error::Error for DecodeError {}
96
97impl DecodeError {
98    const ALL: [Self; 14] = [
99        Self::TooShort,
100        Self::TooLong,
101        Self::Invalid,
102        Self::InvalidMode,
103        Self::BadRegister,
104        Self::BadLockPrefix,
105        Self::BadLegacyPrefix,
106        Self::BadRexPrefix,
107        Self::BadMap,
108        Self::BadEvexVPrime,
109        Self::BadEvexZeroingWithoutMask,
110        Self::BadEvexLength,
111        Self::GatherRegisters,
112        Self::BadRegisterMatch,
113    ];
114
115    /// The error numbered `code` (`error as u8`), `None` for 0.
116    #[inline]
117    pub fn from_code(code: u8) -> Option<Self> {
118        Self::ALL.get(usize::from(code).checked_sub(1)?).copied()
119    }
120}