1use core::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub enum Mode {
10 Real16,
12 Real32,
14 Bits16,
16 Bits32,
18 Bits64,
20}
21
22impl Mode {
23 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 pub const fn is_real(self) -> bool {
34 matches!(self, Self::Real16 | Self::Real32)
35 }
36}
37
38#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
41#[repr(u8)]
42pub enum DecodeError {
43 TooShort = 1,
45 TooLong,
47 Invalid,
49 InvalidMode,
51 BadRegister,
53 BadLockPrefix,
55 BadLegacyPrefix,
57 BadRexPrefix,
59 BadMap,
61 BadEvexVPrime,
63 BadEvexZeroingWithoutMask,
65 BadEvexLength,
67 GatherRegisters,
70 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 #[inline]
117 pub fn from_code(code: u8) -> Option<Self> {
118 Self::ALL.get(usize::from(code).checked_sub(1)?).copied()
119 }
120}