1#![warn(missing_docs)]
17
18use exarmo_aarch32::{Cond, Encoding, Instruction, ItState, Mnemonic};
19pub use exarmo_core::capi::{Branch, FlagEffect, Status, Str, TextSize, Token};
20use exarmo_core::capi::{Storage, bytes, deliver, fill, guarded, with_held, write_tokens};
21
22pub mod model;
23pub mod tables;
24
25use model::{COperand, SysReg};
26
27#[repr(C)]
29#[derive(Clone, Copy)]
30pub struct CInstruction {
31 opaque: [u64; 5],
32}
33
34unsafe impl Storage for CInstruction {
37 type Instruction = Instruction;
38}
39
40const _: () = assert!(std::mem::size_of::<Instruction>() <= std::mem::size_of::<CInstruction>());
41const _: () = assert!(std::mem::align_of::<Instruction>() <= std::mem::align_of::<CInstruction>());
42
43#[unsafe(no_mangle)]
49pub extern "C" fn exarmo_aarch32_instruction_size() -> usize {
50 std::mem::size_of::<CInstruction>()
51}
52
53#[repr(u8)]
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub enum ItKind {
57 Unknown = 0,
59 Outside = 1,
61 Inside = 2,
63}
64
65#[repr(C)]
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
68pub struct CItState {
69 pub kind: u8,
71 pub cond: u8,
74 pub mask: u8,
77}
78
79impl From<CItState> for ItState {
80 fn from(state: CItState) -> Self {
81 match state.kind {
82 1 => ItState::Outside,
83 2 => ItState::Inside {
84 cond: Cond::from_bits(state.cond).unwrap_or(Cond::Al),
85 mask: state.mask & 0xf,
86 },
87 _ => ItState::Unknown,
88 }
89 }
90}
91
92impl From<ItState> for CItState {
93 fn from(state: ItState) -> Self {
94 match state {
95 ItState::Unknown => CItState {
96 kind: ItKind::Unknown as u8,
97 cond: 0,
98 mask: 0,
99 },
100 ItState::Outside => CItState {
101 kind: ItKind::Outside as u8,
102 cond: 0,
103 mask: 0,
104 },
105 ItState::Inside { cond, mask } => CItState {
106 kind: ItKind::Inside as u8,
107 cond: cond as u8,
108 mask,
109 },
110 }
111 }
112}
113
114#[unsafe(no_mangle)]
121pub unsafe extern "C" fn exarmo_aarch32_decode_a32_word(
122 bits: u32,
123 out: *mut CInstruction,
124) -> Status {
125 if out.is_null() {
126 return Status::Failed;
127 }
128 unsafe { deliver(|| exarmo_aarch32::a32::decode_word(bits), out) }
129}
130
131#[unsafe(no_mangle)]
138pub unsafe extern "C" fn exarmo_aarch32_decode_a32(bits: u32, out: *mut CInstruction) -> Status {
139 unsafe { exarmo_aarch32_decode_a32_word(bits, out) }
140}
141
142#[unsafe(no_mangle)]
150pub unsafe extern "C" fn exarmo_aarch32_decode_a32_bytes(
151 bytes: *const u8,
152 len: usize,
153 out: *mut CInstruction,
154) -> Status {
155 match (unsafe { self::bytes(bytes, len) }, out.is_null()) {
156 (Some(code), false) => unsafe { deliver(|| exarmo_aarch32::a32::decode_bytes(code), out) },
157 _ => Status::Failed,
158 }
159}
160
161#[unsafe(no_mangle)]
168pub unsafe extern "C" fn exarmo_aarch32_decode_t32_word(
169 bits: u32,
170 state: CItState,
171 out: *mut CInstruction,
172) -> Status {
173 if out.is_null() {
174 return Status::Failed;
175 }
176 unsafe { deliver(|| exarmo_aarch32::t32::decode_word(bits, state.into()), out) }
177}
178
179#[unsafe(no_mangle)]
186pub unsafe extern "C" fn exarmo_aarch32_decode_t32(
187 bits: u32,
188 state: CItState,
189 out: *mut CInstruction,
190) -> Status {
191 unsafe { exarmo_aarch32_decode_t32_word(bits, state, out) }
192}
193
194#[unsafe(no_mangle)]
203pub unsafe extern "C" fn exarmo_aarch32_decode_t32_bytes(
204 bytes: *const u8,
205 len: usize,
206 state: CItState,
207 out: *mut CInstruction,
208) -> Status {
209 match (unsafe { self::bytes(bytes, len) }, out.is_null()) {
210 (Some(code), false) => unsafe {
211 deliver(
212 || exarmo_aarch32::t32::decode_bytes(code, state.into()),
213 out,
214 )
215 },
216 _ => Status::Failed,
217 }
218}
219
220#[unsafe(no_mangle)]
223pub extern "C" fn exarmo_aarch32_t32_length(hw1: u16) -> u8 {
224 guarded(0, || exarmo_aarch32::t32::length(hw1))
225}
226
227#[unsafe(no_mangle)]
236pub unsafe extern "C" fn exarmo_aarch32_it_state_after(
237 state: CItState,
238 inst: *const CInstruction,
239) -> CItState {
240 unsafe { with_held(inst, state, |inst| ItState::from(state).after(inst).into()) }
241}
242
243#[unsafe(no_mangle)]
251pub unsafe extern "C" fn exarmo_aarch32_instruction_length(inst: *const CInstruction) -> u8 {
252 unsafe { with_held(inst, 0, |inst| inst.encoding().length()) }
253}
254
255#[unsafe(no_mangle)]
264pub unsafe extern "C" fn exarmo_aarch32_instruction_unpredictable(
265 inst: *const CInstruction,
266) -> bool {
267 unsafe { with_held(inst, false, |inst| inst.unpredictable()) }
268}
269
270#[unsafe(no_mangle)]
278pub unsafe extern "C" fn exarmo_aarch32_instruction_encoding(inst: *const CInstruction) -> u32 {
279 let none = Encoding::COUNT as u32;
280 unsafe { with_held(inst, none, |inst| inst.encoding() as u32) }
281}
282
283#[unsafe(no_mangle)]
291pub unsafe extern "C" fn exarmo_aarch32_instruction_mnemonic(inst: *const CInstruction) -> u32 {
292 let none = Mnemonic::COUNT as u32;
293 unsafe { with_held(inst, none, |inst| inst.mnemonic() as u32) }
294}
295
296#[unsafe(no_mangle)]
299pub extern "C" fn exarmo_aarch32_encoding_mnemonic(encoding: u32) -> u32 {
300 Encoding::from_index(encoding as usize).map_or(Mnemonic::COUNT as u32, |e| e.mnemonic() as u32)
301}
302
303#[unsafe(no_mangle)]
305pub extern "C" fn exarmo_aarch32_encoding_name(encoding: u32) -> Str {
306 Encoding::from_index(encoding as usize).map_or(Str::EMPTY, |e| Str::of(e.name()))
307}
308
309#[unsafe(no_mangle)]
311pub extern "C" fn exarmo_aarch32_encoding_length(encoding: u32) -> u8 {
312 Encoding::from_index(encoding as usize).map_or(0, |e| e.length())
313}
314
315#[unsafe(no_mangle)]
317pub extern "C" fn exarmo_aarch32_mnemonic_name(mnemonic: u32) -> Str {
318 Mnemonic::from_index(mnemonic as usize).map_or(Str::EMPTY, |m| Str::of(m.name()))
319}
320
321#[unsafe(no_mangle)]
329pub unsafe extern "C" fn exarmo_aarch32_instruction_flags(inst: *const CInstruction) -> FlagEffect {
330 let none = FlagEffect::from(exarmo_aarch32::FlagEffect::NONE);
331 unsafe { with_held(inst, none, |inst| inst.flags().into()) }
332}
333
334#[unsafe(no_mangle)]
342pub unsafe extern "C" fn exarmo_aarch32_instruction_branch(
343 inst: *const CInstruction,
344 address: u64,
345) -> Branch {
346 unsafe { with_held(inst, Branch::NONE, |inst| inst.at(address).branch().into()) }
347}
348
349#[unsafe(no_mangle)]
358pub unsafe extern "C" fn exarmo_aarch32_instruction_sysreg(
359 inst: *const CInstruction,
360 out: *mut SysReg,
361) -> bool {
362 match unsafe { with_held(inst, None, |inst| inst.system_register()) } {
363 Some(reg) if !out.is_null() => {
364 unsafe { out.write(reg.into()) };
365 true
366 }
367 _ => false,
368 }
369}
370
371#[unsafe(no_mangle)]
374pub extern "C" fn exarmo_aarch32_sysreg_name(space: u8, encoding: u32, write: bool) -> Str {
375 exarmo_aarch32::SysRegSpace::from_bits(space)
376 .and_then(|space| exarmo_aarch32::SysReg::new(space, encoding, write).name())
377 .map_or(Str::EMPTY, Str::of)
378}
379
380#[unsafe(no_mangle)]
388pub unsafe extern "C" fn exarmo_aarch32_instruction_operands(
389 inst: *const CInstruction,
390 out: *mut COperand,
391 capacity: usize,
392) -> usize {
393 unsafe {
394 with_held(inst, 0, |inst| {
395 fill(
396 out,
397 capacity,
398 inst.operands().iter().map(|op| COperand::from(*op)),
399 )
400 })
401 }
402}
403
404#[unsafe(no_mangle)]
412pub unsafe extern "C" fn exarmo_aarch32_instruction_tokens(
413 inst: *const CInstruction,
414 address: u64,
415 text: *mut u8,
416 text_capacity: usize,
417 tokens: *mut Token,
418 token_capacity: usize,
419) -> TextSize {
420 unsafe { write_tokens(inst, address, text, text_capacity, tokens, token_capacity) }
421}
422
423#[unsafe(no_mangle)]
430pub unsafe extern "C" fn exarmo_aarch32_instruction_text(
431 inst: *const CInstruction,
432 address: u64,
433 text: *mut u8,
434 capacity: usize,
435) -> usize {
436 unsafe {
437 exarmo_aarch32_instruction_tokens(inst, address, text, capacity, std::ptr::null_mut(), 0)
438 }
439 .length
440}