1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! 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");
//! # Ok::<(), Error>(())
//! ```
use fmt;
pub use ;
pub use Decoder;
pub use Error;
pub use ;
pub use ;
pub use Encoding;
pub use Feature;
pub use ;
pub use ;
use syntax;
/// Error of parsing a name that names nothing.
;
/// A decoded A64 instruction: the word and its encoding.
/// Decodes one instruction word.
///
/// # Errors
///
/// [`Error::Unallocated`] if no encoding has the word, [`Error::Undefined`]
/// if its encoding's decode checks reject it, [`Error::Unpredictable`] if it
/// is a CONSTRAINED UNPREDICTABLE form LLVM rejects.
/// Decodes the instruction at the start of `bytes` (little-endian; A64
/// instructions are always 4 bytes).
///
/// # Errors
///
/// [`Error::TooShort`] if `bytes` has fewer than 4 bytes; otherwise as
/// [`decode`].