Skip to main content

binbun_x86/
lib.rs

1//! x86 and x86-64 instruction decoder.
2//!
3//! The tables in `generated/` are produced by `cargo xtask codegen` from Intel
4//! XED's datafiles (`third_party/xed`); the decoding logic around them is
5//! written by hand. The crate is `no_std`; its only dependency is
6//! [`binbun_isa`], whose traits it implements ([`X86`]) and whose control-flow
7//! vocabulary [`Instruction::flow`] returns.
8
9#![no_std]
10
11mod cpuid;
12mod decode;
13mod decoder;
14mod error;
15mod fast;
16mod generated;
17mod ild;
18mod isa;
19
20use core::fmt;
21
22pub use cpuid::{CpuidField, CpuidRegister};
23#[cfg(feature = "internal-reference")]
24#[doc(hidden)]
25pub use decode::decode_reference;
26pub use decode::flags::{Flag, FlagAction, FlagEffect, FlagSet, Flags};
27pub use decode::flow::{Condition, relative_target};
28pub use decode::format::Intel;
29pub use decode::operand::{Action, ElementBase, Memory, Operand, Value, Visibility};
30pub use decode::{Attributes, Encoding, Instruction, decode, decode_flow};
31pub use decoder::Decoder;
32pub use error::{DecodeError, Mode};
33pub use generated::form::{Attribute, Category, Extension, Form, IsaSet};
34pub use generated::mnemonic::Mnemonic;
35pub use generated::operand::{ElementType, OperandName, OperandWidth};
36pub use generated::register::{Register, RegisterClass};
37pub use ild::{MAX_LENGTH, decode_length};
38pub use isa::X86;
39
40/// Most operands an instruction has: the longest
41/// [`Instruction::operands`].
42pub const MAX_OPERANDS: usize = {
43    let mut most = 0;
44    let mut rest: &[decode::InstructionInfo] = &generated::form::INSTRUCTIONS;
45    while let [info, tail @ ..] = rest {
46        if info.operand_count as usize > most {
47            most = info.operand_count as usize;
48        }
49        rest = tail;
50    }
51    most
52};
53
54/// Error of parsing a name that names nothing.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub struct UnknownName;
57
58impl fmt::Display for UnknownName {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.write_str("unknown name")
61    }
62}
63
64impl core::error::Error for UnknownName {}