Skip to main content

Crate binbun_aarch64

Crate binbun_aarch64 

Source
Expand description

A64 instruction decoder.

The tables in generated/ are produced by cargo xtask codegen from Arm’s machine-readable A-profile specification (AARCHMRS, the BSD package in third_party/aarchmrs) and binbun’s hand-written tables of decode-time UNDEFINED checks (xtask/src/aarch64/undefined.rs); the code around them is written by hand. The crate is no_std; its one dependency is binbun-isa, the vocabulary of control flow analyses share.

Decoding finds the word’s Encoding (Arm’s name for one bit pattern of an instruction, e.g. ADD_32_addsub_imm): every A64 encoding of the 2026-09 specification is decoded, whatever features it needs, including those of unannounced architecture versions (Encoding::fat). Which words decode agrees with LLVM 22’s disassembler with every feature enabled, on all 2^32 words, apart from encodings newer than LLVM and two LLVM quirks documented in tests/oracle.rs. Where the architecture makes a word CONSTRAINED UNPREDICTABLE (should-be bits not as specified, overlapping registers), the word decodes if LLVM decodes it and is Error::Unpredictable otherwise.

Operands (Instruction::operands, typed: operand) and text (Instruction::display, or Instruction::format into a token Sink) are computed from the word on demand, after alias selection, and the text is LLVM’s (every feature, hexadecimal immediates), with labels as the addresses they name, as llvm-objdump writes them; LabelStyle::Relative writes them as offsets, as LLVM’s C API does. Text equals LLVM 22’s for every word both decode, apart from a few places where LLVM’s text contradicts Arm’s syntax (typos, an annotation, an alias Arm withdrew), listed in tests/oracle.rs.

For analyses: how an instruction transfers control (Instruction::flow) and the condition it tests (Instruction::condition); AArch64 implements binbun_isa::Architecture; Decoder sweeps a buffer.

use binbun_aarch64::{Encoding, Error, LabelStyle, decode, decode_bytes};

let add = decode(0x8b02_0020)?; // add x0, x1, x2
assert_eq!(add.encoding(), Encoding::ADD_64_addsub_shift);
assert_eq!(add.encoding().mnemonic(), "ADD");
assert_eq!(decode_bytes(&[0x1f, 0x20, 0x03, 0xd5])?.encoding(), Encoding::NOP_HI_hints);
// add w0, w1, w2 with shift == '11', a reserved shift type
assert_eq!(decode(0x0bc2_0020), Err(Error::Undefined));

// Operands and text, as LLVM writes them; aliases are resolved.
let mov = decode(0xaa01_03e0)?; // orr x0, xzr, x1
assert_eq!(mov.mnemonic(), "mov");
assert_eq!(mov.display(0x1000).to_string(), "mov\tx0, x1");

// Labels: the address by default, the offset as LLVM's C API writes it.
let b = decode(0x1400_0010)?; // b +0x40
assert_eq!(b.display(0x1000).to_string(), "b\t0x1040");
assert_eq!(b.display_with(0x1000, LabelStyle::Relative).to_string(), "b\t#0x40");

Re-exports§

pub use operand::Operand;
pub use operand::Operands;

Modules§

operand
Operands: what an instruction’s syntax names, as typed values.

Structs§

AArch64
The A64 instruction set, for analyses written against binbun_isa::Architecture.
Accesses
The access of each operand, in the order of Instruction::operands.
Decoder
Decodes the instructions of a buffer one after another (a linear sweep).
Display
An instruction’s text, from Instruction::display or Instruction::display_with.
FlagSet
A set of the condition flags N, Z, C and V.
Flags
The condition flags an instruction reads and writes.
Implicit
Registers an instruction uses without naming them as operands.
Instruction
A decoded A64 instruction: the word and its encoding.
MemoryAccess
An instruction’s memory access.
UnknownName
Error of parsing a name that names nothing.

Enums§

Access
How an instruction accesses an operand.
CompareOperand
What a Condition::Compare compares its register with.
Condition
The condition an instruction tests: whether a conditional branch is taken, a conditional trap faults, or a conditional select or compare (csel, ccmp, fcsel) takes its first alternative.
Encoding
An A64 encoding, as Arm’s specification names it.
Error
Why bytes do not decode to an A64 instruction.
Feature
An architecture feature (FEAT_*) that A64 encodings require.
LabelStyle
How labels (the PC-relative addresses of branches, adr, adrp and literal loads) are written.
MemoryKind
What kind of memory access an instruction makes.
Mode
The processor mode instructions are decoded for: AArch64 has one instruction set (A32 and T32 are AArch32’s, which this crate does not decode).
TokenKind
What a piece of text is, for colouring.

Traits§

Sink
Receives an instruction’s text as tokens.

Functions§

decode
Decodes one instruction word.
decode_bytes
Decodes the instruction at the start of bytes (little-endian; A64 instructions are always 4 bytes).