cranelift_codegen/isa/aarch64/inst/
mod.rs

1//! This module defines aarch64-specific machine instruction types.
2
3use crate::binemit::{Addend, CodeOffset, Reloc};
4use crate::ir::types::{F16, F32, F64, F128, I8, I8X16, I16, I32, I64, I128};
5use crate::ir::{MemFlags, Type, types};
6use crate::isa::{CallConv, FunctionAlignment};
7use crate::machinst::*;
8use crate::{CodegenError, CodegenResult, settings};
9
10use crate::machinst::{PrettyPrint, Reg, RegClass, Writable};
11
12use alloc::vec::Vec;
13use core::slice;
14use smallvec::{SmallVec, smallvec};
15use std::fmt::Write;
16use std::string::{String, ToString};
17
18pub(crate) mod regs;
19pub(crate) use self::regs::*;
20pub mod imms;
21pub use self::imms::*;
22pub mod args;
23pub use self::args::*;
24pub mod emit;
25pub(crate) use self::emit::*;
26use crate::isa::aarch64::abi::AArch64MachineDeps;
27
28pub(crate) mod unwind;
29
30#[cfg(test)]
31mod emit_tests;
32
33//=============================================================================
34// Instructions (top level): definition
35
36pub use crate::isa::aarch64::lower::isle::generated_code::{
37    ALUOp, ALUOp3, AMode, APIKey, AtomicRMWLoopOp, AtomicRMWOp, BitOp, BranchTargetType, FPUOp1,
38    FPUOp2, FPUOp3, FpuRoundMode, FpuToIntOp, IntToFpuOp, MInst as Inst, MoveWideOp, VecALUModOp,
39    VecALUOp, VecExtendOp, VecLanesOp, VecMisc2, VecPairOp, VecRRLongOp, VecRRNarrowOp,
40    VecRRPairLongOp, VecRRRLongModOp, VecRRRLongOp, VecShiftImmModOp, VecShiftImmOp,
41};
42
43/// A floating-point unit (FPU) operation with two args, a register and an immediate.
44#[derive(Copy, Clone, Debug)]
45pub enum FPUOpRI {
46    /// Unsigned right shift. Rd = Rn << #imm
47    UShr32(FPURightShiftImm),
48    /// Unsigned right shift. Rd = Rn << #imm
49    UShr64(FPURightShiftImm),
50}
51
52/// A floating-point unit (FPU) operation with two args, a register and
53/// an immediate that modifies its dest (so takes that input value as a
54/// separate virtual register).
55#[derive(Copy, Clone, Debug)]
56pub enum FPUOpRIMod {
57    /// Shift left and insert. Rd |= Rn << #imm
58    Sli32(FPULeftShiftImm),
59    /// Shift left and insert. Rd |= Rn << #imm
60    Sli64(FPULeftShiftImm),
61}
62
63impl BitOp {
64    /// Get the assembly mnemonic for this opcode.
65    pub fn op_str(&self) -> &'static str {
66        match self {
67            BitOp::RBit => "rbit",
68            BitOp::Clz => "clz",
69            BitOp::Cls => "cls",
70            BitOp::Rev16 => "rev16",
71            BitOp::Rev32 => "rev32",
72            BitOp::Rev64 => "rev64",
73        }
74    }
75}
76
77/// Additional information for `return_call[_ind]` instructions, left out of
78/// line to lower the size of the `Inst` enum.
79#[derive(Clone, Debug)]
80pub struct ReturnCallInfo<T> {
81    /// Where this call is going to
82    pub dest: T,
83    /// Arguments to the call instruction.
84    pub uses: CallArgList,
85    /// The size of the new stack frame's stack arguments. This is necessary
86    /// for copying the frame over our current frame. It must already be
87    /// allocated on the stack.
88    pub new_stack_arg_size: u32,
89    /// API key to use to restore the return address, if any.
90    pub key: Option<APIKey>,
91}
92
93fn count_zero_half_words(mut value: u64, num_half_words: u8) -> usize {
94    let mut count = 0;
95    for _ in 0..num_half_words {
96        if value & 0xffff == 0 {
97            count += 1;
98        }
99        value >>= 16;
100    }
101
102    count
103}
104
105impl Inst {
106    /// Create an instruction that loads a constant, using one of several options (MOVZ, MOVN,
107    /// logical immediate, or constant pool).
108    pub fn load_constant(rd: Writable<Reg>, value: u64) -> SmallVec<[Inst; 4]> {
109        // NB: this is duplicated in `lower/isle.rs` and `inst.isle` right now,
110        // if modifications are made here before this is deleted after moving to
111        // ISLE then those locations should be updated as well.
112
113        if let Some(imm) = MoveWideConst::maybe_from_u64(value) {
114            // 16-bit immediate (shifted by 0, 16, 32 or 48 bits) in MOVZ
115            smallvec![Inst::MovWide {
116                op: MoveWideOp::MovZ,
117                rd,
118                imm,
119                size: OperandSize::Size64
120            }]
121        } else if let Some(imm) = MoveWideConst::maybe_from_u64(!value) {
122            // 16-bit immediate (shifted by 0, 16, 32 or 48 bits) in MOVN
123            smallvec![Inst::MovWide {
124                op: MoveWideOp::MovN,
125                rd,
126                imm,
127                size: OperandSize::Size64
128            }]
129        } else if let Some(imml) = ImmLogic::maybe_from_u64(value, I64) {
130            // Weird logical-instruction immediate in ORI using zero register
131            smallvec![Inst::AluRRImmLogic {
132                alu_op: ALUOp::Orr,
133                size: OperandSize::Size64,
134                rd,
135                rn: zero_reg(),
136                imml,
137            }]
138        } else {
139            let mut insts = smallvec![];
140
141            // If the top 32 bits are zero, use 32-bit `mov` operations.
142            let (num_half_words, size, negated) = if value >> 32 == 0 {
143                (2, OperandSize::Size32, (!value << 32) >> 32)
144            } else {
145                (4, OperandSize::Size64, !value)
146            };
147
148            // If the number of 0xffff half words is greater than the number of 0x0000 half words
149            // it is more efficient to use `movn` for the first instruction.
150            let first_is_inverted = count_zero_half_words(negated, num_half_words)
151                > count_zero_half_words(value, num_half_words);
152
153            // Either 0xffff or 0x0000 half words can be skipped, depending on the first
154            // instruction used.
155            let ignored_halfword = if first_is_inverted { 0xffff } else { 0 };
156
157            let halfwords: SmallVec<[_; 4]> = (0..num_half_words)
158                .filter_map(|i| {
159                    let imm16 = (value >> (16 * i)) & 0xffff;
160                    if imm16 == ignored_halfword {
161                        None
162                    } else {
163                        Some((i, imm16))
164                    }
165                })
166                .collect();
167
168            let mut prev_result = None;
169            for (i, imm16) in halfwords {
170                let shift = i * 16;
171
172                if let Some(rn) = prev_result {
173                    let imm = MoveWideConst::maybe_with_shift(imm16 as u16, shift).unwrap();
174                    insts.push(Inst::MovK { rd, rn, imm, size });
175                } else {
176                    if first_is_inverted {
177                        let imm =
178                            MoveWideConst::maybe_with_shift(((!imm16) & 0xffff) as u16, shift)
179                                .unwrap();
180                        insts.push(Inst::MovWide {
181                            op: MoveWideOp::MovN,
182                            rd,
183                            imm,
184                            size,
185                        });
186                    } else {
187                        let imm = MoveWideConst::maybe_with_shift(imm16 as u16, shift).unwrap();
188                        insts.push(Inst::MovWide {
189                            op: MoveWideOp::MovZ,
190                            rd,
191                            imm,
192                            size,
193                        });
194                    }
195                }
196
197                prev_result = Some(rd.to_reg());
198            }
199
200            assert!(prev_result.is_some());
201
202            insts
203        }
204    }
205
206    /// Generic constructor for a load (zero-extending where appropriate).
207    pub fn gen_load(into_reg: Writable<Reg>, mem: AMode, ty: Type, flags: MemFlags) -> Inst {
208        match ty {
209            I8 => Inst::ULoad8 {
210                rd: into_reg,
211                mem,
212                flags,
213            },
214            I16 => Inst::ULoad16 {
215                rd: into_reg,
216                mem,
217                flags,
218            },
219            I32 => Inst::ULoad32 {
220                rd: into_reg,
221                mem,
222                flags,
223            },
224            I64 => Inst::ULoad64 {
225                rd: into_reg,
226                mem,
227                flags,
228            },
229            _ => {
230                if ty.is_vector() || ty.is_float() {
231                    let bits = ty_bits(ty);
232                    let rd = into_reg;
233
234                    match bits {
235                        128 => Inst::FpuLoad128 { rd, mem, flags },
236                        64 => Inst::FpuLoad64 { rd, mem, flags },
237                        32 => Inst::FpuLoad32 { rd, mem, flags },
238                        16 => Inst::FpuLoad16 { rd, mem, flags },
239                        _ => unimplemented!("gen_load({})", ty),
240                    }
241                } else {
242                    unimplemented!("gen_load({})", ty);
243                }
244            }
245        }
246    }
247
248    /// Generic constructor for a store.
249    pub fn gen_store(mem: AMode, from_reg: Reg, ty: Type, flags: MemFlags) -> Inst {
250        match ty {
251            I8 => Inst::Store8 {
252                rd: from_reg,
253                mem,
254                flags,
255            },
256            I16 => Inst::Store16 {
257                rd: from_reg,
258                mem,
259                flags,
260            },
261            I32 => Inst::Store32 {
262                rd: from_reg,
263                mem,
264                flags,
265            },
266            I64 => Inst::Store64 {
267                rd: from_reg,
268                mem,
269                flags,
270            },
271            _ => {
272                if ty.is_vector() || ty.is_float() {
273                    let bits = ty_bits(ty);
274                    let rd = from_reg;
275
276                    match bits {
277                        128 => Inst::FpuStore128 { rd, mem, flags },
278                        64 => Inst::FpuStore64 { rd, mem, flags },
279                        32 => Inst::FpuStore32 { rd, mem, flags },
280                        16 => Inst::FpuStore16 { rd, mem, flags },
281                        _ => unimplemented!("gen_store({})", ty),
282                    }
283                } else {
284                    unimplemented!("gen_store({})", ty);
285                }
286            }
287        }
288    }
289
290    /// What type does this load or store instruction access in memory? When
291    /// uimm12 encoding is used, the size of this type is the amount that
292    /// immediate offsets are scaled by.
293    pub fn mem_type(&self) -> Option<Type> {
294        match self {
295            Inst::ULoad8 { .. } => Some(I8),
296            Inst::SLoad8 { .. } => Some(I8),
297            Inst::ULoad16 { .. } => Some(I16),
298            Inst::SLoad16 { .. } => Some(I16),
299            Inst::ULoad32 { .. } => Some(I32),
300            Inst::SLoad32 { .. } => Some(I32),
301            Inst::ULoad64 { .. } => Some(I64),
302            Inst::FpuLoad16 { .. } => Some(F16),
303            Inst::FpuLoad32 { .. } => Some(F32),
304            Inst::FpuLoad64 { .. } => Some(F64),
305            Inst::FpuLoad128 { .. } => Some(I8X16),
306            Inst::Store8 { .. } => Some(I8),
307            Inst::Store16 { .. } => Some(I16),
308            Inst::Store32 { .. } => Some(I32),
309            Inst::Store64 { .. } => Some(I64),
310            Inst::FpuStore16 { .. } => Some(F16),
311            Inst::FpuStore32 { .. } => Some(F32),
312            Inst::FpuStore64 { .. } => Some(F64),
313            Inst::FpuStore128 { .. } => Some(I8X16),
314            _ => None,
315        }
316    }
317}
318
319//=============================================================================
320// Instructions: get_regs
321
322fn memarg_operands(memarg: &mut AMode, collector: &mut impl OperandVisitor) {
323    match memarg {
324        AMode::Unscaled { rn, .. } | AMode::UnsignedOffset { rn, .. } => {
325            collector.reg_use(rn);
326        }
327        AMode::RegReg { rn, rm, .. }
328        | AMode::RegScaled { rn, rm, .. }
329        | AMode::RegScaledExtended { rn, rm, .. }
330        | AMode::RegExtended { rn, rm, .. } => {
331            collector.reg_use(rn);
332            collector.reg_use(rm);
333        }
334        AMode::Label { .. } => {}
335        AMode::SPPreIndexed { .. } | AMode::SPPostIndexed { .. } => {}
336        AMode::FPOffset { .. } | AMode::IncomingArg { .. } => {}
337        AMode::SPOffset { .. } | AMode::SlotOffset { .. } => {}
338        AMode::RegOffset { rn, .. } => {
339            collector.reg_use(rn);
340        }
341        AMode::Const { .. } => {}
342    }
343}
344
345fn pairmemarg_operands(pairmemarg: &mut PairAMode, collector: &mut impl OperandVisitor) {
346    match pairmemarg {
347        PairAMode::SignedOffset { reg, .. } => {
348            collector.reg_use(reg);
349        }
350        PairAMode::SPPreIndexed { .. } | PairAMode::SPPostIndexed { .. } => {}
351    }
352}
353
354fn aarch64_get_operands(inst: &mut Inst, collector: &mut impl OperandVisitor) {
355    match inst {
356        Inst::AluRRR { rd, rn, rm, .. } => {
357            collector.reg_def(rd);
358            collector.reg_use(rn);
359            collector.reg_use(rm);
360        }
361        Inst::AluRRRR { rd, rn, rm, ra, .. } => {
362            collector.reg_def(rd);
363            collector.reg_use(rn);
364            collector.reg_use(rm);
365            collector.reg_use(ra);
366        }
367        Inst::AluRRImm12 { rd, rn, .. } => {
368            collector.reg_def(rd);
369            collector.reg_use(rn);
370        }
371        Inst::AluRRImmLogic { rd, rn, .. } => {
372            collector.reg_def(rd);
373            collector.reg_use(rn);
374        }
375        Inst::AluRRImmShift { rd, rn, .. } => {
376            collector.reg_def(rd);
377            collector.reg_use(rn);
378        }
379        Inst::AluRRRShift { rd, rn, rm, .. } => {
380            collector.reg_def(rd);
381            collector.reg_use(rn);
382            collector.reg_use(rm);
383        }
384        Inst::AluRRRExtend { rd, rn, rm, .. } => {
385            collector.reg_def(rd);
386            collector.reg_use(rn);
387            collector.reg_use(rm);
388        }
389        Inst::BitRR { rd, rn, .. } => {
390            collector.reg_def(rd);
391            collector.reg_use(rn);
392        }
393        Inst::ULoad8 { rd, mem, .. }
394        | Inst::SLoad8 { rd, mem, .. }
395        | Inst::ULoad16 { rd, mem, .. }
396        | Inst::SLoad16 { rd, mem, .. }
397        | Inst::ULoad32 { rd, mem, .. }
398        | Inst::SLoad32 { rd, mem, .. }
399        | Inst::ULoad64 { rd, mem, .. } => {
400            collector.reg_def(rd);
401            memarg_operands(mem, collector);
402        }
403        Inst::Store8 { rd, mem, .. }
404        | Inst::Store16 { rd, mem, .. }
405        | Inst::Store32 { rd, mem, .. }
406        | Inst::Store64 { rd, mem, .. } => {
407            collector.reg_use(rd);
408            memarg_operands(mem, collector);
409        }
410        Inst::StoreP64 { rt, rt2, mem, .. } => {
411            collector.reg_use(rt);
412            collector.reg_use(rt2);
413            pairmemarg_operands(mem, collector);
414        }
415        Inst::LoadP64 { rt, rt2, mem, .. } => {
416            collector.reg_def(rt);
417            collector.reg_def(rt2);
418            pairmemarg_operands(mem, collector);
419        }
420        Inst::Mov { rd, rm, .. } => {
421            collector.reg_def(rd);
422            collector.reg_use(rm);
423        }
424        Inst::MovFromPReg { rd, rm } => {
425            debug_assert!(rd.to_reg().is_virtual());
426            collector.reg_def(rd);
427            collector.reg_fixed_nonallocatable(*rm);
428        }
429        Inst::MovToPReg { rd, rm } => {
430            debug_assert!(rm.is_virtual());
431            collector.reg_fixed_nonallocatable(*rd);
432            collector.reg_use(rm);
433        }
434        Inst::MovK { rd, rn, .. } => {
435            collector.reg_use(rn);
436            collector.reg_reuse_def(rd, 0); // `rn` == `rd`.
437        }
438        Inst::MovWide { rd, .. } => {
439            collector.reg_def(rd);
440        }
441        Inst::CSel { rd, rn, rm, .. } => {
442            collector.reg_def(rd);
443            collector.reg_use(rn);
444            collector.reg_use(rm);
445        }
446        Inst::CSNeg { rd, rn, rm, .. } => {
447            collector.reg_def(rd);
448            collector.reg_use(rn);
449            collector.reg_use(rm);
450        }
451        Inst::CSet { rd, .. } | Inst::CSetm { rd, .. } => {
452            collector.reg_def(rd);
453        }
454        Inst::CCmp { rn, rm, .. } => {
455            collector.reg_use(rn);
456            collector.reg_use(rm);
457        }
458        Inst::CCmpImm { rn, .. } => {
459            collector.reg_use(rn);
460        }
461        Inst::AtomicRMWLoop {
462            op,
463            addr,
464            operand,
465            oldval,
466            scratch1,
467            scratch2,
468            ..
469        } => {
470            collector.reg_fixed_use(addr, xreg(25));
471            collector.reg_fixed_use(operand, xreg(26));
472            collector.reg_fixed_def(oldval, xreg(27));
473            collector.reg_fixed_def(scratch1, xreg(24));
474            if *op != AtomicRMWLoopOp::Xchg {
475                collector.reg_fixed_def(scratch2, xreg(28));
476            }
477        }
478        Inst::AtomicRMW { rs, rt, rn, .. } => {
479            collector.reg_use(rs);
480            collector.reg_def(rt);
481            collector.reg_use(rn);
482        }
483        Inst::AtomicCAS { rd, rs, rt, rn, .. } => {
484            collector.reg_reuse_def(rd, 1); // reuse `rs`.
485            collector.reg_use(rs);
486            collector.reg_use(rt);
487            collector.reg_use(rn);
488        }
489        Inst::AtomicCASLoop {
490            addr,
491            expected,
492            replacement,
493            oldval,
494            scratch,
495            ..
496        } => {
497            collector.reg_fixed_use(addr, xreg(25));
498            collector.reg_fixed_use(expected, xreg(26));
499            collector.reg_fixed_use(replacement, xreg(28));
500            collector.reg_fixed_def(oldval, xreg(27));
501            collector.reg_fixed_def(scratch, xreg(24));
502        }
503        Inst::LoadAcquire { rt, rn, .. } => {
504            collector.reg_use(rn);
505            collector.reg_def(rt);
506        }
507        Inst::StoreRelease { rt, rn, .. } => {
508            collector.reg_use(rn);
509            collector.reg_use(rt);
510        }
511        Inst::Fence {} | Inst::Csdb {} => {}
512        Inst::FpuMove32 { rd, rn } => {
513            collector.reg_def(rd);
514            collector.reg_use(rn);
515        }
516        Inst::FpuMove64 { rd, rn } => {
517            collector.reg_def(rd);
518            collector.reg_use(rn);
519        }
520        Inst::FpuMove128 { rd, rn } => {
521            collector.reg_def(rd);
522            collector.reg_use(rn);
523        }
524        Inst::FpuMoveFromVec { rd, rn, .. } => {
525            collector.reg_def(rd);
526            collector.reg_use(rn);
527        }
528        Inst::FpuExtend { rd, rn, .. } => {
529            collector.reg_def(rd);
530            collector.reg_use(rn);
531        }
532        Inst::FpuRR { rd, rn, .. } => {
533            collector.reg_def(rd);
534            collector.reg_use(rn);
535        }
536        Inst::FpuRRR { rd, rn, rm, .. } => {
537            collector.reg_def(rd);
538            collector.reg_use(rn);
539            collector.reg_use(rm);
540        }
541        Inst::FpuRRI { rd, rn, .. } => {
542            collector.reg_def(rd);
543            collector.reg_use(rn);
544        }
545        Inst::FpuRRIMod { rd, ri, rn, .. } => {
546            collector.reg_reuse_def(rd, 1); // reuse `ri`.
547            collector.reg_use(ri);
548            collector.reg_use(rn);
549        }
550        Inst::FpuRRRR { rd, rn, rm, ra, .. } => {
551            collector.reg_def(rd);
552            collector.reg_use(rn);
553            collector.reg_use(rm);
554            collector.reg_use(ra);
555        }
556        Inst::VecMisc { rd, rn, .. } => {
557            collector.reg_def(rd);
558            collector.reg_use(rn);
559        }
560
561        Inst::VecLanes { rd, rn, .. } => {
562            collector.reg_def(rd);
563            collector.reg_use(rn);
564        }
565        Inst::VecShiftImm { rd, rn, .. } => {
566            collector.reg_def(rd);
567            collector.reg_use(rn);
568        }
569        Inst::VecShiftImmMod { rd, ri, rn, .. } => {
570            collector.reg_reuse_def(rd, 1); // `rd` == `ri`.
571            collector.reg_use(ri);
572            collector.reg_use(rn);
573        }
574        Inst::VecExtract { rd, rn, rm, .. } => {
575            collector.reg_def(rd);
576            collector.reg_use(rn);
577            collector.reg_use(rm);
578        }
579        Inst::VecTbl { rd, rn, rm } => {
580            collector.reg_use(rn);
581            collector.reg_use(rm);
582            collector.reg_def(rd);
583        }
584        Inst::VecTblExt { rd, ri, rn, rm } => {
585            collector.reg_use(rn);
586            collector.reg_use(rm);
587            collector.reg_reuse_def(rd, 3); // `rd` == `ri`.
588            collector.reg_use(ri);
589        }
590
591        Inst::VecTbl2 { rd, rn, rn2, rm } => {
592            // Constrain to v30 / v31 so that we satisfy the "adjacent
593            // registers" constraint without use of pinned vregs in
594            // lowering.
595            collector.reg_fixed_use(rn, vreg(30));
596            collector.reg_fixed_use(rn2, vreg(31));
597            collector.reg_use(rm);
598            collector.reg_def(rd);
599        }
600        Inst::VecTbl2Ext {
601            rd,
602            ri,
603            rn,
604            rn2,
605            rm,
606        } => {
607            // Constrain to v30 / v31 so that we satisfy the "adjacent
608            // registers" constraint without use of pinned vregs in
609            // lowering.
610            collector.reg_fixed_use(rn, vreg(30));
611            collector.reg_fixed_use(rn2, vreg(31));
612            collector.reg_use(rm);
613            collector.reg_reuse_def(rd, 4); // `rd` == `ri`.
614            collector.reg_use(ri);
615        }
616        Inst::VecLoadReplicate { rd, rn, .. } => {
617            collector.reg_def(rd);
618            collector.reg_use(rn);
619        }
620        Inst::VecCSel { rd, rn, rm, .. } => {
621            collector.reg_def(rd);
622            collector.reg_use(rn);
623            collector.reg_use(rm);
624        }
625        Inst::FpuCmp { rn, rm, .. } => {
626            collector.reg_use(rn);
627            collector.reg_use(rm);
628        }
629        Inst::FpuLoad16 { rd, mem, .. } => {
630            collector.reg_def(rd);
631            memarg_operands(mem, collector);
632        }
633        Inst::FpuLoad32 { rd, mem, .. } => {
634            collector.reg_def(rd);
635            memarg_operands(mem, collector);
636        }
637        Inst::FpuLoad64 { rd, mem, .. } => {
638            collector.reg_def(rd);
639            memarg_operands(mem, collector);
640        }
641        Inst::FpuLoad128 { rd, mem, .. } => {
642            collector.reg_def(rd);
643            memarg_operands(mem, collector);
644        }
645        Inst::FpuStore16 { rd, mem, .. } => {
646            collector.reg_use(rd);
647            memarg_operands(mem, collector);
648        }
649        Inst::FpuStore32 { rd, mem, .. } => {
650            collector.reg_use(rd);
651            memarg_operands(mem, collector);
652        }
653        Inst::FpuStore64 { rd, mem, .. } => {
654            collector.reg_use(rd);
655            memarg_operands(mem, collector);
656        }
657        Inst::FpuStore128 { rd, mem, .. } => {
658            collector.reg_use(rd);
659            memarg_operands(mem, collector);
660        }
661        Inst::FpuLoadP64 { rt, rt2, mem, .. } => {
662            collector.reg_def(rt);
663            collector.reg_def(rt2);
664            pairmemarg_operands(mem, collector);
665        }
666        Inst::FpuStoreP64 { rt, rt2, mem, .. } => {
667            collector.reg_use(rt);
668            collector.reg_use(rt2);
669            pairmemarg_operands(mem, collector);
670        }
671        Inst::FpuLoadP128 { rt, rt2, mem, .. } => {
672            collector.reg_def(rt);
673            collector.reg_def(rt2);
674            pairmemarg_operands(mem, collector);
675        }
676        Inst::FpuStoreP128 { rt, rt2, mem, .. } => {
677            collector.reg_use(rt);
678            collector.reg_use(rt2);
679            pairmemarg_operands(mem, collector);
680        }
681        Inst::FpuToInt { rd, rn, .. } => {
682            collector.reg_def(rd);
683            collector.reg_use(rn);
684        }
685        Inst::IntToFpu { rd, rn, .. } => {
686            collector.reg_def(rd);
687            collector.reg_use(rn);
688        }
689        Inst::FpuCSel16 { rd, rn, rm, .. }
690        | Inst::FpuCSel32 { rd, rn, rm, .. }
691        | Inst::FpuCSel64 { rd, rn, rm, .. } => {
692            collector.reg_def(rd);
693            collector.reg_use(rn);
694            collector.reg_use(rm);
695        }
696        Inst::FpuRound { rd, rn, .. } => {
697            collector.reg_def(rd);
698            collector.reg_use(rn);
699        }
700        Inst::MovToFpu { rd, rn, .. } => {
701            collector.reg_def(rd);
702            collector.reg_use(rn);
703        }
704        Inst::FpuMoveFPImm { rd, .. } => {
705            collector.reg_def(rd);
706        }
707        Inst::MovToVec { rd, ri, rn, .. } => {
708            collector.reg_reuse_def(rd, 1); // `rd` == `ri`.
709            collector.reg_use(ri);
710            collector.reg_use(rn);
711        }
712        Inst::MovFromVec { rd, rn, .. } | Inst::MovFromVecSigned { rd, rn, .. } => {
713            collector.reg_def(rd);
714            collector.reg_use(rn);
715        }
716        Inst::VecDup { rd, rn, .. } => {
717            collector.reg_def(rd);
718            collector.reg_use(rn);
719        }
720        Inst::VecDupFromFpu { rd, rn, .. } => {
721            collector.reg_def(rd);
722            collector.reg_use(rn);
723        }
724        Inst::VecDupFPImm { rd, .. } => {
725            collector.reg_def(rd);
726        }
727        Inst::VecDupImm { rd, .. } => {
728            collector.reg_def(rd);
729        }
730        Inst::VecExtend { rd, rn, .. } => {
731            collector.reg_def(rd);
732            collector.reg_use(rn);
733        }
734        Inst::VecMovElement { rd, ri, rn, .. } => {
735            collector.reg_reuse_def(rd, 1); // `rd` == `ri`.
736            collector.reg_use(ri);
737            collector.reg_use(rn);
738        }
739        Inst::VecRRLong { rd, rn, .. } => {
740            collector.reg_def(rd);
741            collector.reg_use(rn);
742        }
743        Inst::VecRRNarrowLow { rd, rn, .. } => {
744            collector.reg_use(rn);
745            collector.reg_def(rd);
746        }
747        Inst::VecRRNarrowHigh { rd, ri, rn, .. } => {
748            collector.reg_use(rn);
749            collector.reg_reuse_def(rd, 2); // `rd` == `ri`.
750            collector.reg_use(ri);
751        }
752        Inst::VecRRPair { rd, rn, .. } => {
753            collector.reg_def(rd);
754            collector.reg_use(rn);
755        }
756        Inst::VecRRRLong { rd, rn, rm, .. } => {
757            collector.reg_def(rd);
758            collector.reg_use(rn);
759            collector.reg_use(rm);
760        }
761        Inst::VecRRRLongMod { rd, ri, rn, rm, .. } => {
762            collector.reg_reuse_def(rd, 1); // `rd` == `ri`.
763            collector.reg_use(ri);
764            collector.reg_use(rn);
765            collector.reg_use(rm);
766        }
767        Inst::VecRRPairLong { rd, rn, .. } => {
768            collector.reg_def(rd);
769            collector.reg_use(rn);
770        }
771        Inst::VecRRR { rd, rn, rm, .. } => {
772            collector.reg_def(rd);
773            collector.reg_use(rn);
774            collector.reg_use(rm);
775        }
776        Inst::VecRRRMod { rd, ri, rn, rm, .. } | Inst::VecFmlaElem { rd, ri, rn, rm, .. } => {
777            collector.reg_reuse_def(rd, 1); // `rd` == `ri`.
778            collector.reg_use(ri);
779            collector.reg_use(rn);
780            collector.reg_use(rm);
781        }
782        Inst::MovToNZCV { rn } => {
783            collector.reg_use(rn);
784        }
785        Inst::MovFromNZCV { rd } => {
786            collector.reg_def(rd);
787        }
788        Inst::Extend { rd, rn, .. } => {
789            collector.reg_def(rd);
790            collector.reg_use(rn);
791        }
792        Inst::Args { args } => {
793            for ArgPair { vreg, preg } in args {
794                collector.reg_fixed_def(vreg, *preg);
795            }
796        }
797        Inst::Rets { rets } => {
798            for RetPair { vreg, preg } in rets {
799                collector.reg_fixed_use(vreg, *preg);
800            }
801        }
802        Inst::Ret { .. } | Inst::AuthenticatedRet { .. } => {}
803        Inst::Jump { .. } => {}
804        Inst::Call { info, .. } => {
805            let CallInfo { uses, defs, .. } = &mut **info;
806            for CallArgPair { vreg, preg } in uses {
807                collector.reg_fixed_use(vreg, *preg);
808            }
809            for CallRetPair { vreg, location } in defs {
810                match location {
811                    RetLocation::Reg(preg, ..) => collector.reg_fixed_def(vreg, *preg),
812                    RetLocation::Stack(..) => collector.any_def(vreg),
813                }
814            }
815            collector.reg_clobbers(info.clobbers);
816            if let Some(try_call_info) = &mut info.try_call_info {
817                try_call_info.collect_operands(collector);
818            }
819        }
820        Inst::CallInd { info, .. } => {
821            let CallInfo {
822                dest, uses, defs, ..
823            } = &mut **info;
824            collector.reg_use(dest);
825            for CallArgPair { vreg, preg } in uses {
826                collector.reg_fixed_use(vreg, *preg);
827            }
828            for CallRetPair { vreg, location } in defs {
829                match location {
830                    RetLocation::Reg(preg, ..) => collector.reg_fixed_def(vreg, *preg),
831                    RetLocation::Stack(..) => collector.any_def(vreg),
832                }
833            }
834            collector.reg_clobbers(info.clobbers);
835            if let Some(try_call_info) = &mut info.try_call_info {
836                try_call_info.collect_operands(collector);
837            }
838        }
839        Inst::ReturnCall { info } => {
840            for CallArgPair { vreg, preg } in &mut info.uses {
841                collector.reg_fixed_use(vreg, *preg);
842            }
843        }
844        Inst::ReturnCallInd { info } => {
845            // TODO(https://github.com/bytecodealliance/regalloc2/issues/145):
846            // This shouldn't be a fixed register constraint, but it's not clear how to pick a
847            // register that won't be clobbered by the callee-save restore code emitted with a
848            // return_call_indirect.
849            collector.reg_fixed_use(&mut info.dest, xreg(1));
850            for CallArgPair { vreg, preg } in &mut info.uses {
851                collector.reg_fixed_use(vreg, *preg);
852            }
853        }
854        Inst::CondBr { kind, .. } => match kind {
855            CondBrKind::Zero(rt, _) | CondBrKind::NotZero(rt, _) => collector.reg_use(rt),
856            CondBrKind::Cond(_) => {}
857        },
858        Inst::TestBitAndBranch { rn, .. } => {
859            collector.reg_use(rn);
860        }
861        Inst::IndirectBr { rn, .. } => {
862            collector.reg_use(rn);
863        }
864        Inst::Nop0 | Inst::Nop4 => {}
865        Inst::Brk => {}
866        Inst::Udf { .. } => {}
867        Inst::TrapIf { kind, .. } => match kind {
868            CondBrKind::Zero(rt, _) | CondBrKind::NotZero(rt, _) => collector.reg_use(rt),
869            CondBrKind::Cond(_) => {}
870        },
871        Inst::Adr { rd, .. } | Inst::Adrp { rd, .. } => {
872            collector.reg_def(rd);
873        }
874        Inst::Word4 { .. } | Inst::Word8 { .. } => {}
875        Inst::JTSequence {
876            ridx, rtmp1, rtmp2, ..
877        } => {
878            collector.reg_use(ridx);
879            collector.reg_early_def(rtmp1);
880            collector.reg_early_def(rtmp2);
881        }
882        Inst::LoadExtNameGot { rd, .. }
883        | Inst::LoadExtNameNear { rd, .. }
884        | Inst::LoadExtNameFar { rd, .. } => {
885            collector.reg_def(rd);
886        }
887        Inst::LoadAddr { rd, mem } => {
888            collector.reg_def(rd);
889            memarg_operands(mem, collector);
890        }
891        Inst::Paci { .. } | Inst::Xpaclri => {
892            // Neither LR nor SP is an allocatable register, so there is no need
893            // to do anything.
894        }
895        Inst::Bti { .. } => {}
896
897        Inst::ElfTlsGetAddr { rd, tmp, .. } => {
898            // TLSDESC has a very neat calling convention. It is required to preserve
899            // all registers except x0 and x30. X30 is non allocatable in cranelift since
900            // its the link register.
901            //
902            // Additionally we need a second register as a temporary register for the
903            // TLSDESC sequence. This register can be any register other than x0 (and x30).
904            collector.reg_fixed_def(rd, regs::xreg(0));
905            collector.reg_early_def(tmp);
906        }
907        Inst::MachOTlsGetAddr { rd, .. } => {
908            collector.reg_fixed_def(rd, regs::xreg(0));
909            let mut clobbers =
910                AArch64MachineDeps::get_regs_clobbered_by_call(CallConv::AppleAarch64, false);
911            clobbers.remove(regs::xreg_preg(0));
912            collector.reg_clobbers(clobbers);
913        }
914        Inst::Unwind { .. } => {}
915        Inst::EmitIsland { .. } => {}
916        Inst::DummyUse { reg } => {
917            collector.reg_use(reg);
918        }
919        Inst::LabelAddress { dst, .. } => {
920            collector.reg_def(dst);
921        }
922        Inst::StackProbeLoop { start, end, .. } => {
923            collector.reg_early_def(start);
924            collector.reg_use(end);
925        }
926    }
927}
928
929//=============================================================================
930// Instructions: misc functions and external interface
931
932impl MachInst for Inst {
933    type ABIMachineSpec = AArch64MachineDeps;
934    type LabelUse = LabelUse;
935
936    // "CLIF" in hex, to make the trap recognizable during
937    // debugging.
938    const TRAP_OPCODE: &'static [u8] = &0xc11f_u32.to_le_bytes();
939
940    fn get_operands(&mut self, collector: &mut impl OperandVisitor) {
941        aarch64_get_operands(self, collector);
942    }
943
944    fn is_move(&self) -> Option<(Writable<Reg>, Reg)> {
945        match self {
946            &Inst::Mov {
947                size: OperandSize::Size64,
948                rd,
949                rm,
950            } => Some((rd, rm)),
951            &Inst::FpuMove64 { rd, rn } => Some((rd, rn)),
952            &Inst::FpuMove128 { rd, rn } => Some((rd, rn)),
953            _ => None,
954        }
955    }
956
957    fn is_included_in_clobbers(&self) -> bool {
958        let (caller, callee, is_exception) = match self {
959            Inst::Args { .. } => return false,
960            Inst::Call { info } => (
961                info.caller_conv,
962                info.callee_conv,
963                info.try_call_info.is_some(),
964            ),
965            Inst::CallInd { info } => (
966                info.caller_conv,
967                info.callee_conv,
968                info.try_call_info.is_some(),
969            ),
970            _ => return true,
971        };
972
973        // We exclude call instructions from the clobber-set when they are calls
974        // from caller to callee that both clobber the same register (such as
975        // using the same or similar ABIs). Such calls cannot possibly force any
976        // new registers to be saved in the prologue, because anything that the
977        // callee clobbers, the caller is also allowed to clobber. This both
978        // saves work and enables us to more precisely follow the
979        // half-caller-save, half-callee-save SysV ABI for some vector
980        // registers.
981        //
982        // See the note in [crate::isa::aarch64::abi::is_caller_save_reg] for
983        // more information on this ABI-implementation hack.
984        let caller_clobbers = AArch64MachineDeps::get_regs_clobbered_by_call(caller, false);
985        let callee_clobbers = AArch64MachineDeps::get_regs_clobbered_by_call(callee, is_exception);
986
987        let mut all_clobbers = caller_clobbers;
988        all_clobbers.union_from(callee_clobbers);
989        all_clobbers != caller_clobbers
990    }
991
992    fn is_trap(&self) -> bool {
993        match self {
994            Self::Udf { .. } => true,
995            _ => false,
996        }
997    }
998
999    fn is_args(&self) -> bool {
1000        match self {
1001            Self::Args { .. } => true,
1002            _ => false,
1003        }
1004    }
1005
1006    fn call_type(&self) -> CallType {
1007        match self {
1008            Inst::Call { .. }
1009            | Inst::CallInd { .. }
1010            | Inst::ElfTlsGetAddr { .. }
1011            | Inst::MachOTlsGetAddr { .. } => CallType::Regular,
1012
1013            Inst::ReturnCall { .. } | Inst::ReturnCallInd { .. } => CallType::TailCall,
1014
1015            _ => CallType::None,
1016        }
1017    }
1018
1019    fn is_term(&self) -> MachTerminator {
1020        match self {
1021            &Inst::Rets { .. } => MachTerminator::Ret,
1022            &Inst::ReturnCall { .. } | &Inst::ReturnCallInd { .. } => MachTerminator::RetCall,
1023            &Inst::Jump { .. } => MachTerminator::Branch,
1024            &Inst::CondBr { .. } => MachTerminator::Branch,
1025            &Inst::TestBitAndBranch { .. } => MachTerminator::Branch,
1026            &Inst::IndirectBr { .. } => MachTerminator::Branch,
1027            &Inst::JTSequence { .. } => MachTerminator::Branch,
1028            &Inst::Call { ref info } if info.try_call_info.is_some() => MachTerminator::Branch,
1029            &Inst::CallInd { ref info } if info.try_call_info.is_some() => MachTerminator::Branch,
1030            _ => MachTerminator::None,
1031        }
1032    }
1033
1034    fn is_mem_access(&self) -> bool {
1035        match self {
1036            &Inst::ULoad8 { .. }
1037            | &Inst::SLoad8 { .. }
1038            | &Inst::ULoad16 { .. }
1039            | &Inst::SLoad16 { .. }
1040            | &Inst::ULoad32 { .. }
1041            | &Inst::SLoad32 { .. }
1042            | &Inst::ULoad64 { .. }
1043            | &Inst::LoadP64 { .. }
1044            | &Inst::FpuLoad16 { .. }
1045            | &Inst::FpuLoad32 { .. }
1046            | &Inst::FpuLoad64 { .. }
1047            | &Inst::FpuLoad128 { .. }
1048            | &Inst::FpuLoadP64 { .. }
1049            | &Inst::FpuLoadP128 { .. }
1050            | &Inst::Store8 { .. }
1051            | &Inst::Store16 { .. }
1052            | &Inst::Store32 { .. }
1053            | &Inst::Store64 { .. }
1054            | &Inst::StoreP64 { .. }
1055            | &Inst::FpuStore16 { .. }
1056            | &Inst::FpuStore32 { .. }
1057            | &Inst::FpuStore64 { .. }
1058            | &Inst::FpuStore128 { .. } => true,
1059            // TODO: verify this carefully
1060            _ => false,
1061        }
1062    }
1063
1064    fn gen_move(to_reg: Writable<Reg>, from_reg: Reg, ty: Type) -> Inst {
1065        let bits = ty.bits();
1066
1067        assert!(bits <= 128);
1068        assert!(to_reg.to_reg().class() == from_reg.class());
1069        match from_reg.class() {
1070            RegClass::Int => Inst::Mov {
1071                size: OperandSize::Size64,
1072                rd: to_reg,
1073                rm: from_reg,
1074            },
1075            RegClass::Float => {
1076                if bits > 64 {
1077                    Inst::FpuMove128 {
1078                        rd: to_reg,
1079                        rn: from_reg,
1080                    }
1081                } else {
1082                    Inst::FpuMove64 {
1083                        rd: to_reg,
1084                        rn: from_reg,
1085                    }
1086                }
1087            }
1088            RegClass::Vector => unreachable!(),
1089        }
1090    }
1091
1092    fn is_safepoint(&self) -> bool {
1093        match self {
1094            Inst::Call { .. } | Inst::CallInd { .. } => true,
1095            _ => false,
1096        }
1097    }
1098
1099    fn gen_dummy_use(reg: Reg) -> Inst {
1100        Inst::DummyUse { reg }
1101    }
1102
1103    fn gen_nop(preferred_size: usize) -> Inst {
1104        if preferred_size == 0 {
1105            return Inst::Nop0;
1106        }
1107        // We can't give a NOP (or any insn) < 4 bytes.
1108        assert!(preferred_size >= 4);
1109        Inst::Nop4
1110    }
1111
1112    fn rc_for_type(ty: Type) -> CodegenResult<(&'static [RegClass], &'static [Type])> {
1113        match ty {
1114            I8 => Ok((&[RegClass::Int], &[I8])),
1115            I16 => Ok((&[RegClass::Int], &[I16])),
1116            I32 => Ok((&[RegClass::Int], &[I32])),
1117            I64 => Ok((&[RegClass::Int], &[I64])),
1118            F16 => Ok((&[RegClass::Float], &[F16])),
1119            F32 => Ok((&[RegClass::Float], &[F32])),
1120            F64 => Ok((&[RegClass::Float], &[F64])),
1121            F128 => Ok((&[RegClass::Float], &[F128])),
1122            I128 => Ok((&[RegClass::Int, RegClass::Int], &[I64, I64])),
1123            _ if ty.is_vector() && ty.bits() <= 128 => {
1124                let types = &[types::I8X2, types::I8X4, types::I8X8, types::I8X16];
1125                Ok((
1126                    &[RegClass::Float],
1127                    slice::from_ref(&types[ty.bytes().ilog2() as usize - 1]),
1128                ))
1129            }
1130            _ if ty.is_dynamic_vector() => Ok((&[RegClass::Float], &[I8X16])),
1131            _ => Err(CodegenError::Unsupported(format!(
1132                "Unexpected SSA-value type: {ty}"
1133            ))),
1134        }
1135    }
1136
1137    fn canonical_type_for_rc(rc: RegClass) -> Type {
1138        match rc {
1139            RegClass::Float => types::I8X16,
1140            RegClass::Int => types::I64,
1141            RegClass::Vector => unreachable!(),
1142        }
1143    }
1144
1145    fn gen_jump(target: MachLabel) -> Inst {
1146        Inst::Jump {
1147            dest: BranchTarget::Label(target),
1148        }
1149    }
1150
1151    fn worst_case_size() -> CodeOffset {
1152        // The maximum size, in bytes, of any `Inst`'s emitted code. We have at least one case of
1153        // an 8-instruction sequence (saturating int-to-float conversions) with three embedded
1154        // 64-bit f64 constants.
1155        //
1156        // Note that inline jump-tables handle island/pool insertion separately, so we do not need
1157        // to account for them here (otherwise the worst case would be 2^31 * 4, clearly not
1158        // feasible for other reasons).
1159        44
1160    }
1161
1162    fn ref_type_regclass(_: &settings::Flags) -> RegClass {
1163        RegClass::Int
1164    }
1165
1166    fn gen_block_start(
1167        is_indirect_branch_target: bool,
1168        is_forward_edge_cfi_enabled: bool,
1169    ) -> Option<Self> {
1170        if is_indirect_branch_target && is_forward_edge_cfi_enabled {
1171            Some(Inst::Bti {
1172                targets: BranchTargetType::J,
1173            })
1174        } else {
1175            None
1176        }
1177    }
1178
1179    fn function_alignment() -> FunctionAlignment {
1180        // We use 32-byte alignment for performance reasons, but for correctness
1181        // we would only need 4-byte alignment.
1182        FunctionAlignment {
1183            minimum: 4,
1184            preferred: 32,
1185        }
1186    }
1187}
1188
1189//=============================================================================
1190// Pretty-printing of instructions.
1191
1192fn mem_finalize_for_show(mem: &AMode, access_ty: Type, state: &EmitState) -> (String, String) {
1193    let (mem_insts, mem) = mem_finalize(None, mem, access_ty, state);
1194    let mut mem_str = mem_insts
1195        .into_iter()
1196        .map(|inst| inst.print_with_state(&mut EmitState::default()))
1197        .collect::<Vec<_>>()
1198        .join(" ; ");
1199    if !mem_str.is_empty() {
1200        mem_str += " ; ";
1201    }
1202
1203    let mem = mem.pretty_print(access_ty.bytes() as u8);
1204    (mem_str, mem)
1205}
1206
1207fn pretty_print_try_call(info: &TryCallInfo) -> String {
1208    format!(
1209        "; b {:?}; catch [{}]",
1210        info.continuation,
1211        info.pretty_print_dests()
1212    )
1213}
1214
1215impl Inst {
1216    fn print_with_state(&self, state: &mut EmitState) -> String {
1217        fn op_name(alu_op: ALUOp) -> &'static str {
1218            match alu_op {
1219                ALUOp::Add => "add",
1220                ALUOp::Sub => "sub",
1221                ALUOp::Orr => "orr",
1222                ALUOp::And => "and",
1223                ALUOp::AndS => "ands",
1224                ALUOp::Eor => "eor",
1225                ALUOp::AddS => "adds",
1226                ALUOp::SubS => "subs",
1227                ALUOp::SMulH => "smulh",
1228                ALUOp::UMulH => "umulh",
1229                ALUOp::SDiv => "sdiv",
1230                ALUOp::UDiv => "udiv",
1231                ALUOp::AndNot => "bic",
1232                ALUOp::OrrNot => "orn",
1233                ALUOp::EorNot => "eon",
1234                ALUOp::Extr => "extr",
1235                ALUOp::Lsr => "lsr",
1236                ALUOp::Asr => "asr",
1237                ALUOp::Lsl => "lsl",
1238                ALUOp::Adc => "adc",
1239                ALUOp::AdcS => "adcs",
1240                ALUOp::Sbc => "sbc",
1241                ALUOp::SbcS => "sbcs",
1242            }
1243        }
1244
1245        match self {
1246            &Inst::Nop0 => "nop-zero-len".to_string(),
1247            &Inst::Nop4 => "nop".to_string(),
1248            &Inst::AluRRR {
1249                alu_op,
1250                size,
1251                rd,
1252                rn,
1253                rm,
1254            } => {
1255                let op = op_name(alu_op);
1256                let rd = pretty_print_ireg(rd.to_reg(), size);
1257                let rn = pretty_print_ireg(rn, size);
1258                let rm = pretty_print_ireg(rm, size);
1259                format!("{op} {rd}, {rn}, {rm}")
1260            }
1261            &Inst::AluRRRR {
1262                alu_op,
1263                size,
1264                rd,
1265                rn,
1266                rm,
1267                ra,
1268            } => {
1269                let (op, da_size) = match alu_op {
1270                    ALUOp3::MAdd => ("madd", size),
1271                    ALUOp3::MSub => ("msub", size),
1272                    ALUOp3::UMAddL => ("umaddl", OperandSize::Size64),
1273                    ALUOp3::SMAddL => ("smaddl", OperandSize::Size64),
1274                };
1275                let rd = pretty_print_ireg(rd.to_reg(), da_size);
1276                let rn = pretty_print_ireg(rn, size);
1277                let rm = pretty_print_ireg(rm, size);
1278                let ra = pretty_print_ireg(ra, da_size);
1279
1280                format!("{op} {rd}, {rn}, {rm}, {ra}")
1281            }
1282            &Inst::AluRRImm12 {
1283                alu_op,
1284                size,
1285                rd,
1286                rn,
1287                ref imm12,
1288            } => {
1289                let op = op_name(alu_op);
1290                let rd = pretty_print_ireg(rd.to_reg(), size);
1291                let rn = pretty_print_ireg(rn, size);
1292
1293                if imm12.bits == 0 && alu_op == ALUOp::Add && size.is64() {
1294                    // special-case MOV (used for moving into SP).
1295                    format!("mov {rd}, {rn}")
1296                } else {
1297                    let imm12 = imm12.pretty_print(0);
1298                    format!("{op} {rd}, {rn}, {imm12}")
1299                }
1300            }
1301            &Inst::AluRRImmLogic {
1302                alu_op,
1303                size,
1304                rd,
1305                rn,
1306                ref imml,
1307            } => {
1308                let op = op_name(alu_op);
1309                let rd = pretty_print_ireg(rd.to_reg(), size);
1310                let rn = pretty_print_ireg(rn, size);
1311                let imml = imml.pretty_print(0);
1312                format!("{op} {rd}, {rn}, {imml}")
1313            }
1314            &Inst::AluRRImmShift {
1315                alu_op,
1316                size,
1317                rd,
1318                rn,
1319                ref immshift,
1320            } => {
1321                let op = op_name(alu_op);
1322                let rd = pretty_print_ireg(rd.to_reg(), size);
1323                let rn = pretty_print_ireg(rn, size);
1324                let immshift = immshift.pretty_print(0);
1325                format!("{op} {rd}, {rn}, {immshift}")
1326            }
1327            &Inst::AluRRRShift {
1328                alu_op,
1329                size,
1330                rd,
1331                rn,
1332                rm,
1333                ref shiftop,
1334            } => {
1335                let op = op_name(alu_op);
1336                let rd = pretty_print_ireg(rd.to_reg(), size);
1337                let rn = pretty_print_ireg(rn, size);
1338                let rm = pretty_print_ireg(rm, size);
1339                let shiftop = shiftop.pretty_print(0);
1340                format!("{op} {rd}, {rn}, {rm}, {shiftop}")
1341            }
1342            &Inst::AluRRRExtend {
1343                alu_op,
1344                size,
1345                rd,
1346                rn,
1347                rm,
1348                ref extendop,
1349            } => {
1350                let op = op_name(alu_op);
1351                let rd = pretty_print_ireg(rd.to_reg(), size);
1352                let rn = pretty_print_ireg(rn, size);
1353                let rm = pretty_print_ireg(rm, size);
1354                let extendop = extendop.pretty_print(0);
1355                format!("{op} {rd}, {rn}, {rm}, {extendop}")
1356            }
1357            &Inst::BitRR { op, size, rd, rn } => {
1358                let op = op.op_str();
1359                let rd = pretty_print_ireg(rd.to_reg(), size);
1360                let rn = pretty_print_ireg(rn, size);
1361                format!("{op} {rd}, {rn}")
1362            }
1363            &Inst::ULoad8 { rd, ref mem, .. }
1364            | &Inst::SLoad8 { rd, ref mem, .. }
1365            | &Inst::ULoad16 { rd, ref mem, .. }
1366            | &Inst::SLoad16 { rd, ref mem, .. }
1367            | &Inst::ULoad32 { rd, ref mem, .. }
1368            | &Inst::SLoad32 { rd, ref mem, .. }
1369            | &Inst::ULoad64 { rd, ref mem, .. } => {
1370                let is_unscaled = match &mem {
1371                    &AMode::Unscaled { .. } => true,
1372                    _ => false,
1373                };
1374                let (op, size) = match (self, is_unscaled) {
1375                    (&Inst::ULoad8 { .. }, false) => ("ldrb", OperandSize::Size32),
1376                    (&Inst::ULoad8 { .. }, true) => ("ldurb", OperandSize::Size32),
1377                    (&Inst::SLoad8 { .. }, false) => ("ldrsb", OperandSize::Size64),
1378                    (&Inst::SLoad8 { .. }, true) => ("ldursb", OperandSize::Size64),
1379                    (&Inst::ULoad16 { .. }, false) => ("ldrh", OperandSize::Size32),
1380                    (&Inst::ULoad16 { .. }, true) => ("ldurh", OperandSize::Size32),
1381                    (&Inst::SLoad16 { .. }, false) => ("ldrsh", OperandSize::Size64),
1382                    (&Inst::SLoad16 { .. }, true) => ("ldursh", OperandSize::Size64),
1383                    (&Inst::ULoad32 { .. }, false) => ("ldr", OperandSize::Size32),
1384                    (&Inst::ULoad32 { .. }, true) => ("ldur", OperandSize::Size32),
1385                    (&Inst::SLoad32 { .. }, false) => ("ldrsw", OperandSize::Size64),
1386                    (&Inst::SLoad32 { .. }, true) => ("ldursw", OperandSize::Size64),
1387                    (&Inst::ULoad64 { .. }, false) => ("ldr", OperandSize::Size64),
1388                    (&Inst::ULoad64 { .. }, true) => ("ldur", OperandSize::Size64),
1389                    _ => unreachable!(),
1390                };
1391
1392                let rd = pretty_print_ireg(rd.to_reg(), size);
1393                let mem = mem.clone();
1394                let access_ty = self.mem_type().unwrap();
1395                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1396
1397                format!("{mem_str}{op} {rd}, {mem}")
1398            }
1399            &Inst::Store8 { rd, ref mem, .. }
1400            | &Inst::Store16 { rd, ref mem, .. }
1401            | &Inst::Store32 { rd, ref mem, .. }
1402            | &Inst::Store64 { rd, ref mem, .. } => {
1403                let is_unscaled = match &mem {
1404                    &AMode::Unscaled { .. } => true,
1405                    _ => false,
1406                };
1407                let (op, size) = match (self, is_unscaled) {
1408                    (&Inst::Store8 { .. }, false) => ("strb", OperandSize::Size32),
1409                    (&Inst::Store8 { .. }, true) => ("sturb", OperandSize::Size32),
1410                    (&Inst::Store16 { .. }, false) => ("strh", OperandSize::Size32),
1411                    (&Inst::Store16 { .. }, true) => ("sturh", OperandSize::Size32),
1412                    (&Inst::Store32 { .. }, false) => ("str", OperandSize::Size32),
1413                    (&Inst::Store32 { .. }, true) => ("stur", OperandSize::Size32),
1414                    (&Inst::Store64 { .. }, false) => ("str", OperandSize::Size64),
1415                    (&Inst::Store64 { .. }, true) => ("stur", OperandSize::Size64),
1416                    _ => unreachable!(),
1417                };
1418
1419                let rd = pretty_print_ireg(rd, size);
1420                let mem = mem.clone();
1421                let access_ty = self.mem_type().unwrap();
1422                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1423
1424                format!("{mem_str}{op} {rd}, {mem}")
1425            }
1426            &Inst::StoreP64 {
1427                rt, rt2, ref mem, ..
1428            } => {
1429                let rt = pretty_print_ireg(rt, OperandSize::Size64);
1430                let rt2 = pretty_print_ireg(rt2, OperandSize::Size64);
1431                let mem = mem.clone();
1432                let mem = mem.pretty_print_default();
1433                format!("stp {rt}, {rt2}, {mem}")
1434            }
1435            &Inst::LoadP64 {
1436                rt, rt2, ref mem, ..
1437            } => {
1438                let rt = pretty_print_ireg(rt.to_reg(), OperandSize::Size64);
1439                let rt2 = pretty_print_ireg(rt2.to_reg(), OperandSize::Size64);
1440                let mem = mem.clone();
1441                let mem = mem.pretty_print_default();
1442                format!("ldp {rt}, {rt2}, {mem}")
1443            }
1444            &Inst::Mov { size, rd, rm } => {
1445                let rd = pretty_print_ireg(rd.to_reg(), size);
1446                let rm = pretty_print_ireg(rm, size);
1447                format!("mov {rd}, {rm}")
1448            }
1449            &Inst::MovFromPReg { rd, rm } => {
1450                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size64);
1451                let rm = show_ireg_sized(rm.into(), OperandSize::Size64);
1452                format!("mov {rd}, {rm}")
1453            }
1454            &Inst::MovToPReg { rd, rm } => {
1455                let rd = show_ireg_sized(rd.into(), OperandSize::Size64);
1456                let rm = pretty_print_ireg(rm, OperandSize::Size64);
1457                format!("mov {rd}, {rm}")
1458            }
1459            &Inst::MovWide {
1460                op,
1461                rd,
1462                ref imm,
1463                size,
1464            } => {
1465                let op_str = match op {
1466                    MoveWideOp::MovZ => "movz",
1467                    MoveWideOp::MovN => "movn",
1468                };
1469                let rd = pretty_print_ireg(rd.to_reg(), size);
1470                let imm = imm.pretty_print(0);
1471                format!("{op_str} {rd}, {imm}")
1472            }
1473            &Inst::MovK {
1474                rd,
1475                rn,
1476                ref imm,
1477                size,
1478            } => {
1479                let rn = pretty_print_ireg(rn, size);
1480                let rd = pretty_print_ireg(rd.to_reg(), size);
1481                let imm = imm.pretty_print(0);
1482                format!("movk {rd}, {rn}, {imm}")
1483            }
1484            &Inst::CSel { rd, rn, rm, cond } => {
1485                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size64);
1486                let rn = pretty_print_ireg(rn, OperandSize::Size64);
1487                let rm = pretty_print_ireg(rm, OperandSize::Size64);
1488                let cond = cond.pretty_print(0);
1489                format!("csel {rd}, {rn}, {rm}, {cond}")
1490            }
1491            &Inst::CSNeg { rd, rn, rm, cond } => {
1492                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size64);
1493                let rn = pretty_print_ireg(rn, OperandSize::Size64);
1494                let rm = pretty_print_ireg(rm, OperandSize::Size64);
1495                let cond = cond.pretty_print(0);
1496                format!("csneg {rd}, {rn}, {rm}, {cond}")
1497            }
1498            &Inst::CSet { rd, cond } => {
1499                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size64);
1500                let cond = cond.pretty_print(0);
1501                format!("cset {rd}, {cond}")
1502            }
1503            &Inst::CSetm { rd, cond } => {
1504                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size64);
1505                let cond = cond.pretty_print(0);
1506                format!("csetm {rd}, {cond}")
1507            }
1508            &Inst::CCmp {
1509                size,
1510                rn,
1511                rm,
1512                nzcv,
1513                cond,
1514            } => {
1515                let rn = pretty_print_ireg(rn, size);
1516                let rm = pretty_print_ireg(rm, size);
1517                let nzcv = nzcv.pretty_print(0);
1518                let cond = cond.pretty_print(0);
1519                format!("ccmp {rn}, {rm}, {nzcv}, {cond}")
1520            }
1521            &Inst::CCmpImm {
1522                size,
1523                rn,
1524                imm,
1525                nzcv,
1526                cond,
1527            } => {
1528                let rn = pretty_print_ireg(rn, size);
1529                let imm = imm.pretty_print(0);
1530                let nzcv = nzcv.pretty_print(0);
1531                let cond = cond.pretty_print(0);
1532                format!("ccmp {rn}, {imm}, {nzcv}, {cond}")
1533            }
1534            &Inst::AtomicRMW {
1535                rs, rt, rn, ty, op, ..
1536            } => {
1537                let op = match op {
1538                    AtomicRMWOp::Add => "ldaddal",
1539                    AtomicRMWOp::Clr => "ldclral",
1540                    AtomicRMWOp::Eor => "ldeoral",
1541                    AtomicRMWOp::Set => "ldsetal",
1542                    AtomicRMWOp::Smax => "ldsmaxal",
1543                    AtomicRMWOp::Umax => "ldumaxal",
1544                    AtomicRMWOp::Smin => "ldsminal",
1545                    AtomicRMWOp::Umin => "lduminal",
1546                    AtomicRMWOp::Swp => "swpal",
1547                };
1548
1549                let size = OperandSize::from_ty(ty);
1550                let rs = pretty_print_ireg(rs, size);
1551                let rt = pretty_print_ireg(rt.to_reg(), size);
1552                let rn = pretty_print_ireg(rn, OperandSize::Size64);
1553
1554                let ty_suffix = match ty {
1555                    I8 => "b",
1556                    I16 => "h",
1557                    _ => "",
1558                };
1559                format!("{op}{ty_suffix} {rs}, {rt}, [{rn}]")
1560            }
1561            &Inst::AtomicRMWLoop {
1562                ty,
1563                op,
1564                addr,
1565                operand,
1566                oldval,
1567                scratch1,
1568                scratch2,
1569                ..
1570            } => {
1571                let op = match op {
1572                    AtomicRMWLoopOp::Add => "add",
1573                    AtomicRMWLoopOp::Sub => "sub",
1574                    AtomicRMWLoopOp::Eor => "eor",
1575                    AtomicRMWLoopOp::Orr => "orr",
1576                    AtomicRMWLoopOp::And => "and",
1577                    AtomicRMWLoopOp::Nand => "nand",
1578                    AtomicRMWLoopOp::Smin => "smin",
1579                    AtomicRMWLoopOp::Smax => "smax",
1580                    AtomicRMWLoopOp::Umin => "umin",
1581                    AtomicRMWLoopOp::Umax => "umax",
1582                    AtomicRMWLoopOp::Xchg => "xchg",
1583                };
1584                let addr = pretty_print_ireg(addr, OperandSize::Size64);
1585                let operand = pretty_print_ireg(operand, OperandSize::Size64);
1586                let oldval = pretty_print_ireg(oldval.to_reg(), OperandSize::Size64);
1587                let scratch1 = pretty_print_ireg(scratch1.to_reg(), OperandSize::Size64);
1588                let scratch2 = pretty_print_ireg(scratch2.to_reg(), OperandSize::Size64);
1589                format!(
1590                    "atomic_rmw_loop_{}_{} addr={} operand={} oldval={} scratch1={} scratch2={}",
1591                    op,
1592                    ty.bits(),
1593                    addr,
1594                    operand,
1595                    oldval,
1596                    scratch1,
1597                    scratch2,
1598                )
1599            }
1600            &Inst::AtomicCAS {
1601                rd, rs, rt, rn, ty, ..
1602            } => {
1603                let op = match ty {
1604                    I8 => "casalb",
1605                    I16 => "casalh",
1606                    I32 | I64 => "casal",
1607                    _ => panic!("Unsupported type: {ty}"),
1608                };
1609                let size = OperandSize::from_ty(ty);
1610                let rd = pretty_print_ireg(rd.to_reg(), size);
1611                let rs = pretty_print_ireg(rs, size);
1612                let rt = pretty_print_ireg(rt, size);
1613                let rn = pretty_print_ireg(rn, OperandSize::Size64);
1614
1615                format!("{op} {rd}, {rs}, {rt}, [{rn}]")
1616            }
1617            &Inst::AtomicCASLoop {
1618                ty,
1619                addr,
1620                expected,
1621                replacement,
1622                oldval,
1623                scratch,
1624                ..
1625            } => {
1626                let addr = pretty_print_ireg(addr, OperandSize::Size64);
1627                let expected = pretty_print_ireg(expected, OperandSize::Size64);
1628                let replacement = pretty_print_ireg(replacement, OperandSize::Size64);
1629                let oldval = pretty_print_ireg(oldval.to_reg(), OperandSize::Size64);
1630                let scratch = pretty_print_ireg(scratch.to_reg(), OperandSize::Size64);
1631                format!(
1632                    "atomic_cas_loop_{} addr={}, expect={}, replacement={}, oldval={}, scratch={}",
1633                    ty.bits(),
1634                    addr,
1635                    expected,
1636                    replacement,
1637                    oldval,
1638                    scratch,
1639                )
1640            }
1641            &Inst::LoadAcquire {
1642                access_ty, rt, rn, ..
1643            } => {
1644                let (op, ty) = match access_ty {
1645                    I8 => ("ldarb", I32),
1646                    I16 => ("ldarh", I32),
1647                    I32 => ("ldar", I32),
1648                    I64 => ("ldar", I64),
1649                    _ => panic!("Unsupported type: {access_ty}"),
1650                };
1651                let size = OperandSize::from_ty(ty);
1652                let rn = pretty_print_ireg(rn, OperandSize::Size64);
1653                let rt = pretty_print_ireg(rt.to_reg(), size);
1654                format!("{op} {rt}, [{rn}]")
1655            }
1656            &Inst::StoreRelease {
1657                access_ty, rt, rn, ..
1658            } => {
1659                let (op, ty) = match access_ty {
1660                    I8 => ("stlrb", I32),
1661                    I16 => ("stlrh", I32),
1662                    I32 => ("stlr", I32),
1663                    I64 => ("stlr", I64),
1664                    _ => panic!("Unsupported type: {access_ty}"),
1665                };
1666                let size = OperandSize::from_ty(ty);
1667                let rn = pretty_print_ireg(rn, OperandSize::Size64);
1668                let rt = pretty_print_ireg(rt, size);
1669                format!("{op} {rt}, [{rn}]")
1670            }
1671            &Inst::Fence {} => {
1672                format!("dmb ish")
1673            }
1674            &Inst::Csdb {} => {
1675                format!("csdb")
1676            }
1677            &Inst::FpuMove32 { rd, rn } => {
1678                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size32);
1679                let rn = pretty_print_vreg_scalar(rn, ScalarSize::Size32);
1680                format!("fmov {rd}, {rn}")
1681            }
1682            &Inst::FpuMove64 { rd, rn } => {
1683                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size64);
1684                let rn = pretty_print_vreg_scalar(rn, ScalarSize::Size64);
1685                format!("fmov {rd}, {rn}")
1686            }
1687            &Inst::FpuMove128 { rd, rn } => {
1688                let rd = pretty_print_reg(rd.to_reg());
1689                let rn = pretty_print_reg(rn);
1690                format!("mov {rd}.16b, {rn}.16b")
1691            }
1692            &Inst::FpuMoveFromVec { rd, rn, idx, size } => {
1693                let rd = pretty_print_vreg_scalar(rd.to_reg(), size.lane_size());
1694                let rn = pretty_print_vreg_element(rn, idx as usize, size.lane_size());
1695                format!("mov {rd}, {rn}")
1696            }
1697            &Inst::FpuExtend { rd, rn, size } => {
1698                let rd = pretty_print_vreg_scalar(rd.to_reg(), size);
1699                let rn = pretty_print_vreg_scalar(rn, size);
1700                format!("fmov {rd}, {rn}")
1701            }
1702            &Inst::FpuRR {
1703                fpu_op,
1704                size,
1705                rd,
1706                rn,
1707            } => {
1708                let op = match fpu_op {
1709                    FPUOp1::Abs => "fabs",
1710                    FPUOp1::Neg => "fneg",
1711                    FPUOp1::Sqrt => "fsqrt",
1712                    FPUOp1::Cvt32To64 | FPUOp1::Cvt64To32 => "fcvt",
1713                };
1714                let dst_size = match fpu_op {
1715                    FPUOp1::Cvt32To64 => ScalarSize::Size64,
1716                    FPUOp1::Cvt64To32 => ScalarSize::Size32,
1717                    _ => size,
1718                };
1719                let rd = pretty_print_vreg_scalar(rd.to_reg(), dst_size);
1720                let rn = pretty_print_vreg_scalar(rn, size);
1721                format!("{op} {rd}, {rn}")
1722            }
1723            &Inst::FpuRRR {
1724                fpu_op,
1725                size,
1726                rd,
1727                rn,
1728                rm,
1729            } => {
1730                let op = match fpu_op {
1731                    FPUOp2::Add => "fadd",
1732                    FPUOp2::Sub => "fsub",
1733                    FPUOp2::Mul => "fmul",
1734                    FPUOp2::Div => "fdiv",
1735                    FPUOp2::Max => "fmax",
1736                    FPUOp2::Min => "fmin",
1737                };
1738                let rd = pretty_print_vreg_scalar(rd.to_reg(), size);
1739                let rn = pretty_print_vreg_scalar(rn, size);
1740                let rm = pretty_print_vreg_scalar(rm, size);
1741                format!("{op} {rd}, {rn}, {rm}")
1742            }
1743            &Inst::FpuRRI { fpu_op, rd, rn } => {
1744                let (op, imm, vector) = match fpu_op {
1745                    FPUOpRI::UShr32(imm) => ("ushr", imm.pretty_print(0), true),
1746                    FPUOpRI::UShr64(imm) => ("ushr", imm.pretty_print(0), false),
1747                };
1748
1749                let (rd, rn) = if vector {
1750                    (
1751                        pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size32x2),
1752                        pretty_print_vreg_vector(rn, VectorSize::Size32x2),
1753                    )
1754                } else {
1755                    (
1756                        pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size64),
1757                        pretty_print_vreg_scalar(rn, ScalarSize::Size64),
1758                    )
1759                };
1760                format!("{op} {rd}, {rn}, {imm}")
1761            }
1762            &Inst::FpuRRIMod { fpu_op, rd, ri, rn } => {
1763                let (op, imm, vector) = match fpu_op {
1764                    FPUOpRIMod::Sli32(imm) => ("sli", imm.pretty_print(0), true),
1765                    FPUOpRIMod::Sli64(imm) => ("sli", imm.pretty_print(0), false),
1766                };
1767
1768                let (rd, ri, rn) = if vector {
1769                    (
1770                        pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size32x2),
1771                        pretty_print_vreg_vector(ri, VectorSize::Size32x2),
1772                        pretty_print_vreg_vector(rn, VectorSize::Size32x2),
1773                    )
1774                } else {
1775                    (
1776                        pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size64),
1777                        pretty_print_vreg_scalar(ri, ScalarSize::Size64),
1778                        pretty_print_vreg_scalar(rn, ScalarSize::Size64),
1779                    )
1780                };
1781                format!("{op} {rd}, {ri}, {rn}, {imm}")
1782            }
1783            &Inst::FpuRRRR {
1784                fpu_op,
1785                size,
1786                rd,
1787                rn,
1788                rm,
1789                ra,
1790            } => {
1791                let op = match fpu_op {
1792                    FPUOp3::MAdd => "fmadd",
1793                    FPUOp3::MSub => "fmsub",
1794                    FPUOp3::NMAdd => "fnmadd",
1795                    FPUOp3::NMSub => "fnmsub",
1796                };
1797                let rd = pretty_print_vreg_scalar(rd.to_reg(), size);
1798                let rn = pretty_print_vreg_scalar(rn, size);
1799                let rm = pretty_print_vreg_scalar(rm, size);
1800                let ra = pretty_print_vreg_scalar(ra, size);
1801                format!("{op} {rd}, {rn}, {rm}, {ra}")
1802            }
1803            &Inst::FpuCmp { size, rn, rm } => {
1804                let rn = pretty_print_vreg_scalar(rn, size);
1805                let rm = pretty_print_vreg_scalar(rm, size);
1806                format!("fcmp {rn}, {rm}")
1807            }
1808            &Inst::FpuLoad16 { rd, ref mem, .. } => {
1809                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size16);
1810                let mem = mem.clone();
1811                let access_ty = self.mem_type().unwrap();
1812                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1813                format!("{mem_str}ldr {rd}, {mem}")
1814            }
1815            &Inst::FpuLoad32 { rd, ref mem, .. } => {
1816                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size32);
1817                let mem = mem.clone();
1818                let access_ty = self.mem_type().unwrap();
1819                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1820                format!("{mem_str}ldr {rd}, {mem}")
1821            }
1822            &Inst::FpuLoad64 { rd, ref mem, .. } => {
1823                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size64);
1824                let mem = mem.clone();
1825                let access_ty = self.mem_type().unwrap();
1826                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1827                format!("{mem_str}ldr {rd}, {mem}")
1828            }
1829            &Inst::FpuLoad128 { rd, ref mem, .. } => {
1830                let rd = pretty_print_reg(rd.to_reg());
1831                let rd = "q".to_string() + &rd[1..];
1832                let mem = mem.clone();
1833                let access_ty = self.mem_type().unwrap();
1834                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1835                format!("{mem_str}ldr {rd}, {mem}")
1836            }
1837            &Inst::FpuStore16 { rd, ref mem, .. } => {
1838                let rd = pretty_print_vreg_scalar(rd, ScalarSize::Size16);
1839                let mem = mem.clone();
1840                let access_ty = self.mem_type().unwrap();
1841                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1842                format!("{mem_str}str {rd}, {mem}")
1843            }
1844            &Inst::FpuStore32 { rd, ref mem, .. } => {
1845                let rd = pretty_print_vreg_scalar(rd, ScalarSize::Size32);
1846                let mem = mem.clone();
1847                let access_ty = self.mem_type().unwrap();
1848                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1849                format!("{mem_str}str {rd}, {mem}")
1850            }
1851            &Inst::FpuStore64 { rd, ref mem, .. } => {
1852                let rd = pretty_print_vreg_scalar(rd, ScalarSize::Size64);
1853                let mem = mem.clone();
1854                let access_ty = self.mem_type().unwrap();
1855                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1856                format!("{mem_str}str {rd}, {mem}")
1857            }
1858            &Inst::FpuStore128 { rd, ref mem, .. } => {
1859                let rd = pretty_print_reg(rd);
1860                let rd = "q".to_string() + &rd[1..];
1861                let mem = mem.clone();
1862                let access_ty = self.mem_type().unwrap();
1863                let (mem_str, mem) = mem_finalize_for_show(&mem, access_ty, state);
1864                format!("{mem_str}str {rd}, {mem}")
1865            }
1866            &Inst::FpuLoadP64 {
1867                rt, rt2, ref mem, ..
1868            } => {
1869                let rt = pretty_print_vreg_scalar(rt.to_reg(), ScalarSize::Size64);
1870                let rt2 = pretty_print_vreg_scalar(rt2.to_reg(), ScalarSize::Size64);
1871                let mem = mem.clone();
1872                let mem = mem.pretty_print_default();
1873
1874                format!("ldp {rt}, {rt2}, {mem}")
1875            }
1876            &Inst::FpuStoreP64 {
1877                rt, rt2, ref mem, ..
1878            } => {
1879                let rt = pretty_print_vreg_scalar(rt, ScalarSize::Size64);
1880                let rt2 = pretty_print_vreg_scalar(rt2, ScalarSize::Size64);
1881                let mem = mem.clone();
1882                let mem = mem.pretty_print_default();
1883
1884                format!("stp {rt}, {rt2}, {mem}")
1885            }
1886            &Inst::FpuLoadP128 {
1887                rt, rt2, ref mem, ..
1888            } => {
1889                let rt = pretty_print_vreg_scalar(rt.to_reg(), ScalarSize::Size128);
1890                let rt2 = pretty_print_vreg_scalar(rt2.to_reg(), ScalarSize::Size128);
1891                let mem = mem.clone();
1892                let mem = mem.pretty_print_default();
1893
1894                format!("ldp {rt}, {rt2}, {mem}")
1895            }
1896            &Inst::FpuStoreP128 {
1897                rt, rt2, ref mem, ..
1898            } => {
1899                let rt = pretty_print_vreg_scalar(rt, ScalarSize::Size128);
1900                let rt2 = pretty_print_vreg_scalar(rt2, ScalarSize::Size128);
1901                let mem = mem.clone();
1902                let mem = mem.pretty_print_default();
1903
1904                format!("stp {rt}, {rt2}, {mem}")
1905            }
1906            &Inst::FpuToInt { op, rd, rn } => {
1907                let (op, sizesrc, sizedest) = match op {
1908                    FpuToIntOp::F32ToI32 => ("fcvtzs", ScalarSize::Size32, OperandSize::Size32),
1909                    FpuToIntOp::F32ToU32 => ("fcvtzu", ScalarSize::Size32, OperandSize::Size32),
1910                    FpuToIntOp::F32ToI64 => ("fcvtzs", ScalarSize::Size32, OperandSize::Size64),
1911                    FpuToIntOp::F32ToU64 => ("fcvtzu", ScalarSize::Size32, OperandSize::Size64),
1912                    FpuToIntOp::F64ToI32 => ("fcvtzs", ScalarSize::Size64, OperandSize::Size32),
1913                    FpuToIntOp::F64ToU32 => ("fcvtzu", ScalarSize::Size64, OperandSize::Size32),
1914                    FpuToIntOp::F64ToI64 => ("fcvtzs", ScalarSize::Size64, OperandSize::Size64),
1915                    FpuToIntOp::F64ToU64 => ("fcvtzu", ScalarSize::Size64, OperandSize::Size64),
1916                };
1917                let rd = pretty_print_ireg(rd.to_reg(), sizedest);
1918                let rn = pretty_print_vreg_scalar(rn, sizesrc);
1919                format!("{op} {rd}, {rn}")
1920            }
1921            &Inst::IntToFpu { op, rd, rn } => {
1922                let (op, sizesrc, sizedest) = match op {
1923                    IntToFpuOp::I32ToF32 => ("scvtf", OperandSize::Size32, ScalarSize::Size32),
1924                    IntToFpuOp::U32ToF32 => ("ucvtf", OperandSize::Size32, ScalarSize::Size32),
1925                    IntToFpuOp::I64ToF32 => ("scvtf", OperandSize::Size64, ScalarSize::Size32),
1926                    IntToFpuOp::U64ToF32 => ("ucvtf", OperandSize::Size64, ScalarSize::Size32),
1927                    IntToFpuOp::I32ToF64 => ("scvtf", OperandSize::Size32, ScalarSize::Size64),
1928                    IntToFpuOp::U32ToF64 => ("ucvtf", OperandSize::Size32, ScalarSize::Size64),
1929                    IntToFpuOp::I64ToF64 => ("scvtf", OperandSize::Size64, ScalarSize::Size64),
1930                    IntToFpuOp::U64ToF64 => ("ucvtf", OperandSize::Size64, ScalarSize::Size64),
1931                };
1932                let rd = pretty_print_vreg_scalar(rd.to_reg(), sizedest);
1933                let rn = pretty_print_ireg(rn, sizesrc);
1934                format!("{op} {rd}, {rn}")
1935            }
1936            &Inst::FpuCSel16 { rd, rn, rm, cond } => {
1937                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size16);
1938                let rn = pretty_print_vreg_scalar(rn, ScalarSize::Size16);
1939                let rm = pretty_print_vreg_scalar(rm, ScalarSize::Size16);
1940                let cond = cond.pretty_print(0);
1941                format!("fcsel {rd}, {rn}, {rm}, {cond}")
1942            }
1943            &Inst::FpuCSel32 { rd, rn, rm, cond } => {
1944                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size32);
1945                let rn = pretty_print_vreg_scalar(rn, ScalarSize::Size32);
1946                let rm = pretty_print_vreg_scalar(rm, ScalarSize::Size32);
1947                let cond = cond.pretty_print(0);
1948                format!("fcsel {rd}, {rn}, {rm}, {cond}")
1949            }
1950            &Inst::FpuCSel64 { rd, rn, rm, cond } => {
1951                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size64);
1952                let rn = pretty_print_vreg_scalar(rn, ScalarSize::Size64);
1953                let rm = pretty_print_vreg_scalar(rm, ScalarSize::Size64);
1954                let cond = cond.pretty_print(0);
1955                format!("fcsel {rd}, {rn}, {rm}, {cond}")
1956            }
1957            &Inst::FpuRound { op, rd, rn } => {
1958                let (inst, size) = match op {
1959                    FpuRoundMode::Minus32 => ("frintm", ScalarSize::Size32),
1960                    FpuRoundMode::Minus64 => ("frintm", ScalarSize::Size64),
1961                    FpuRoundMode::Plus32 => ("frintp", ScalarSize::Size32),
1962                    FpuRoundMode::Plus64 => ("frintp", ScalarSize::Size64),
1963                    FpuRoundMode::Zero32 => ("frintz", ScalarSize::Size32),
1964                    FpuRoundMode::Zero64 => ("frintz", ScalarSize::Size64),
1965                    FpuRoundMode::Nearest32 => ("frintn", ScalarSize::Size32),
1966                    FpuRoundMode::Nearest64 => ("frintn", ScalarSize::Size64),
1967                };
1968                let rd = pretty_print_vreg_scalar(rd.to_reg(), size);
1969                let rn = pretty_print_vreg_scalar(rn, size);
1970                format!("{inst} {rd}, {rn}")
1971            }
1972            &Inst::MovToFpu { rd, rn, size } => {
1973                let operand_size = size.operand_size();
1974                let rd = pretty_print_vreg_scalar(rd.to_reg(), size);
1975                let rn = pretty_print_ireg(rn, operand_size);
1976                format!("fmov {rd}, {rn}")
1977            }
1978            &Inst::FpuMoveFPImm { rd, imm, size } => {
1979                let imm = imm.pretty_print(0);
1980                let rd = pretty_print_vreg_scalar(rd.to_reg(), size);
1981
1982                format!("fmov {rd}, {imm}")
1983            }
1984            &Inst::MovToVec {
1985                rd,
1986                ri,
1987                rn,
1988                idx,
1989                size,
1990            } => {
1991                let rd = pretty_print_vreg_element(rd.to_reg(), idx as usize, size.lane_size());
1992                let ri = pretty_print_vreg_element(ri, idx as usize, size.lane_size());
1993                let rn = pretty_print_ireg(rn, size.operand_size());
1994                format!("mov {rd}, {ri}, {rn}")
1995            }
1996            &Inst::MovFromVec { rd, rn, idx, size } => {
1997                let op = match size {
1998                    ScalarSize::Size8 => "umov",
1999                    ScalarSize::Size16 => "umov",
2000                    ScalarSize::Size32 => "mov",
2001                    ScalarSize::Size64 => "mov",
2002                    _ => unimplemented!(),
2003                };
2004                let rd = pretty_print_ireg(rd.to_reg(), size.operand_size());
2005                let rn = pretty_print_vreg_element(rn, idx as usize, size);
2006                format!("{op} {rd}, {rn}")
2007            }
2008            &Inst::MovFromVecSigned {
2009                rd,
2010                rn,
2011                idx,
2012                size,
2013                scalar_size,
2014            } => {
2015                let rd = pretty_print_ireg(rd.to_reg(), scalar_size);
2016                let rn = pretty_print_vreg_element(rn, idx as usize, size.lane_size());
2017                format!("smov {rd}, {rn}")
2018            }
2019            &Inst::VecDup { rd, rn, size } => {
2020                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2021                let rn = pretty_print_ireg(rn, size.operand_size());
2022                format!("dup {rd}, {rn}")
2023            }
2024            &Inst::VecDupFromFpu { rd, rn, size, lane } => {
2025                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2026                let rn = pretty_print_vreg_element(rn, lane.into(), size.lane_size());
2027                format!("dup {rd}, {rn}")
2028            }
2029            &Inst::VecDupFPImm { rd, imm, size } => {
2030                let imm = imm.pretty_print(0);
2031                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2032
2033                format!("fmov {rd}, {imm}")
2034            }
2035            &Inst::VecDupImm {
2036                rd,
2037                imm,
2038                invert,
2039                size,
2040            } => {
2041                let imm = imm.pretty_print(0);
2042                let op = if invert { "mvni" } else { "movi" };
2043                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2044
2045                format!("{op} {rd}, {imm}")
2046            }
2047            &Inst::VecExtend {
2048                t,
2049                rd,
2050                rn,
2051                high_half,
2052                lane_size,
2053            } => {
2054                let vec64 = VectorSize::from_lane_size(lane_size.narrow(), false);
2055                let vec128 = VectorSize::from_lane_size(lane_size.narrow(), true);
2056                let rd_size = VectorSize::from_lane_size(lane_size, true);
2057                let (op, rn_size) = match (t, high_half) {
2058                    (VecExtendOp::Sxtl, false) => ("sxtl", vec64),
2059                    (VecExtendOp::Sxtl, true) => ("sxtl2", vec128),
2060                    (VecExtendOp::Uxtl, false) => ("uxtl", vec64),
2061                    (VecExtendOp::Uxtl, true) => ("uxtl2", vec128),
2062                };
2063                let rd = pretty_print_vreg_vector(rd.to_reg(), rd_size);
2064                let rn = pretty_print_vreg_vector(rn, rn_size);
2065                format!("{op} {rd}, {rn}")
2066            }
2067            &Inst::VecMovElement {
2068                rd,
2069                ri,
2070                rn,
2071                dest_idx,
2072                src_idx,
2073                size,
2074            } => {
2075                let rd =
2076                    pretty_print_vreg_element(rd.to_reg(), dest_idx as usize, size.lane_size());
2077                let ri = pretty_print_vreg_element(ri, dest_idx as usize, size.lane_size());
2078                let rn = pretty_print_vreg_element(rn, src_idx as usize, size.lane_size());
2079                format!("mov {rd}, {ri}, {rn}")
2080            }
2081            &Inst::VecRRLong {
2082                op,
2083                rd,
2084                rn,
2085                high_half,
2086            } => {
2087                let (op, rd_size, size, suffix) = match (op, high_half) {
2088                    (VecRRLongOp::Fcvtl16, false) => {
2089                        ("fcvtl", VectorSize::Size32x4, VectorSize::Size16x4, "")
2090                    }
2091                    (VecRRLongOp::Fcvtl16, true) => {
2092                        ("fcvtl2", VectorSize::Size32x4, VectorSize::Size16x8, "")
2093                    }
2094                    (VecRRLongOp::Fcvtl32, false) => {
2095                        ("fcvtl", VectorSize::Size64x2, VectorSize::Size32x2, "")
2096                    }
2097                    (VecRRLongOp::Fcvtl32, true) => {
2098                        ("fcvtl2", VectorSize::Size64x2, VectorSize::Size32x4, "")
2099                    }
2100                    (VecRRLongOp::Shll8, false) => {
2101                        ("shll", VectorSize::Size16x8, VectorSize::Size8x8, ", #8")
2102                    }
2103                    (VecRRLongOp::Shll8, true) => {
2104                        ("shll2", VectorSize::Size16x8, VectorSize::Size8x16, ", #8")
2105                    }
2106                    (VecRRLongOp::Shll16, false) => {
2107                        ("shll", VectorSize::Size32x4, VectorSize::Size16x4, ", #16")
2108                    }
2109                    (VecRRLongOp::Shll16, true) => {
2110                        ("shll2", VectorSize::Size32x4, VectorSize::Size16x8, ", #16")
2111                    }
2112                    (VecRRLongOp::Shll32, false) => {
2113                        ("shll", VectorSize::Size64x2, VectorSize::Size32x2, ", #32")
2114                    }
2115                    (VecRRLongOp::Shll32, true) => {
2116                        ("shll2", VectorSize::Size64x2, VectorSize::Size32x4, ", #32")
2117                    }
2118                };
2119                let rd = pretty_print_vreg_vector(rd.to_reg(), rd_size);
2120                let rn = pretty_print_vreg_vector(rn, size);
2121
2122                format!("{op} {rd}, {rn}{suffix}")
2123            }
2124            &Inst::VecRRNarrowLow {
2125                op,
2126                rd,
2127                rn,
2128                lane_size,
2129                ..
2130            }
2131            | &Inst::VecRRNarrowHigh {
2132                op,
2133                rd,
2134                rn,
2135                lane_size,
2136                ..
2137            } => {
2138                let vec64 = VectorSize::from_lane_size(lane_size, false);
2139                let vec128 = VectorSize::from_lane_size(lane_size, true);
2140                let rn_size = VectorSize::from_lane_size(lane_size.widen(), true);
2141                let high_half = match self {
2142                    &Inst::VecRRNarrowLow { .. } => false,
2143                    &Inst::VecRRNarrowHigh { .. } => true,
2144                    _ => unreachable!(),
2145                };
2146                let (op, rd_size) = match (op, high_half) {
2147                    (VecRRNarrowOp::Xtn, false) => ("xtn", vec64),
2148                    (VecRRNarrowOp::Xtn, true) => ("xtn2", vec128),
2149                    (VecRRNarrowOp::Sqxtn, false) => ("sqxtn", vec64),
2150                    (VecRRNarrowOp::Sqxtn, true) => ("sqxtn2", vec128),
2151                    (VecRRNarrowOp::Sqxtun, false) => ("sqxtun", vec64),
2152                    (VecRRNarrowOp::Sqxtun, true) => ("sqxtun2", vec128),
2153                    (VecRRNarrowOp::Uqxtn, false) => ("uqxtn", vec64),
2154                    (VecRRNarrowOp::Uqxtn, true) => ("uqxtn2", vec128),
2155                    (VecRRNarrowOp::Fcvtn, false) => ("fcvtn", vec64),
2156                    (VecRRNarrowOp::Fcvtn, true) => ("fcvtn2", vec128),
2157                };
2158                let rn = pretty_print_vreg_vector(rn, rn_size);
2159                let rd = pretty_print_vreg_vector(rd.to_reg(), rd_size);
2160                let ri = match self {
2161                    &Inst::VecRRNarrowLow { .. } => "".to_string(),
2162                    &Inst::VecRRNarrowHigh { ri, .. } => {
2163                        format!("{}, ", pretty_print_vreg_vector(ri, rd_size))
2164                    }
2165                    _ => unreachable!(),
2166                };
2167
2168                format!("{op} {rd}, {ri}{rn}")
2169            }
2170            &Inst::VecRRPair { op, rd, rn } => {
2171                let op = match op {
2172                    VecPairOp::Addp => "addp",
2173                };
2174                let rd = pretty_print_vreg_scalar(rd.to_reg(), ScalarSize::Size64);
2175                let rn = pretty_print_vreg_vector(rn, VectorSize::Size64x2);
2176
2177                format!("{op} {rd}, {rn}")
2178            }
2179            &Inst::VecRRPairLong { op, rd, rn } => {
2180                let (op, dest, src) = match op {
2181                    VecRRPairLongOp::Saddlp8 => {
2182                        ("saddlp", VectorSize::Size16x8, VectorSize::Size8x16)
2183                    }
2184                    VecRRPairLongOp::Saddlp16 => {
2185                        ("saddlp", VectorSize::Size32x4, VectorSize::Size16x8)
2186                    }
2187                    VecRRPairLongOp::Uaddlp8 => {
2188                        ("uaddlp", VectorSize::Size16x8, VectorSize::Size8x16)
2189                    }
2190                    VecRRPairLongOp::Uaddlp16 => {
2191                        ("uaddlp", VectorSize::Size32x4, VectorSize::Size16x8)
2192                    }
2193                };
2194                let rd = pretty_print_vreg_vector(rd.to_reg(), dest);
2195                let rn = pretty_print_vreg_vector(rn, src);
2196
2197                format!("{op} {rd}, {rn}")
2198            }
2199            &Inst::VecRRR {
2200                rd,
2201                rn,
2202                rm,
2203                alu_op,
2204                size,
2205            } => {
2206                let (op, size) = match alu_op {
2207                    VecALUOp::Sqadd => ("sqadd", size),
2208                    VecALUOp::Uqadd => ("uqadd", size),
2209                    VecALUOp::Sqsub => ("sqsub", size),
2210                    VecALUOp::Uqsub => ("uqsub", size),
2211                    VecALUOp::Cmeq => ("cmeq", size),
2212                    VecALUOp::Cmge => ("cmge", size),
2213                    VecALUOp::Cmgt => ("cmgt", size),
2214                    VecALUOp::Cmhs => ("cmhs", size),
2215                    VecALUOp::Cmhi => ("cmhi", size),
2216                    VecALUOp::Fcmeq => ("fcmeq", size),
2217                    VecALUOp::Fcmgt => ("fcmgt", size),
2218                    VecALUOp::Fcmge => ("fcmge", size),
2219                    VecALUOp::And => ("and", VectorSize::Size8x16),
2220                    VecALUOp::Bic => ("bic", VectorSize::Size8x16),
2221                    VecALUOp::Orr => ("orr", VectorSize::Size8x16),
2222                    VecALUOp::Eor => ("eor", VectorSize::Size8x16),
2223                    VecALUOp::Umaxp => ("umaxp", size),
2224                    VecALUOp::Add => ("add", size),
2225                    VecALUOp::Sub => ("sub", size),
2226                    VecALUOp::Mul => ("mul", size),
2227                    VecALUOp::Sshl => ("sshl", size),
2228                    VecALUOp::Ushl => ("ushl", size),
2229                    VecALUOp::Umin => ("umin", size),
2230                    VecALUOp::Smin => ("smin", size),
2231                    VecALUOp::Umax => ("umax", size),
2232                    VecALUOp::Smax => ("smax", size),
2233                    VecALUOp::Urhadd => ("urhadd", size),
2234                    VecALUOp::Fadd => ("fadd", size),
2235                    VecALUOp::Fsub => ("fsub", size),
2236                    VecALUOp::Fdiv => ("fdiv", size),
2237                    VecALUOp::Fmax => ("fmax", size),
2238                    VecALUOp::Fmin => ("fmin", size),
2239                    VecALUOp::Fmul => ("fmul", size),
2240                    VecALUOp::Addp => ("addp", size),
2241                    VecALUOp::Zip1 => ("zip1", size),
2242                    VecALUOp::Zip2 => ("zip2", size),
2243                    VecALUOp::Sqrdmulh => ("sqrdmulh", size),
2244                    VecALUOp::Uzp1 => ("uzp1", size),
2245                    VecALUOp::Uzp2 => ("uzp2", size),
2246                    VecALUOp::Trn1 => ("trn1", size),
2247                    VecALUOp::Trn2 => ("trn2", size),
2248                };
2249                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2250                let rn = pretty_print_vreg_vector(rn, size);
2251                let rm = pretty_print_vreg_vector(rm, size);
2252                format!("{op} {rd}, {rn}, {rm}")
2253            }
2254            &Inst::VecRRRMod {
2255                rd,
2256                ri,
2257                rn,
2258                rm,
2259                alu_op,
2260                size,
2261            } => {
2262                let (op, size) = match alu_op {
2263                    VecALUModOp::Bsl => ("bsl", VectorSize::Size8x16),
2264                    VecALUModOp::Fmla => ("fmla", size),
2265                    VecALUModOp::Fmls => ("fmls", size),
2266                };
2267                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2268                let ri = pretty_print_vreg_vector(ri, size);
2269                let rn = pretty_print_vreg_vector(rn, size);
2270                let rm = pretty_print_vreg_vector(rm, size);
2271                format!("{op} {rd}, {ri}, {rn}, {rm}")
2272            }
2273            &Inst::VecFmlaElem {
2274                rd,
2275                ri,
2276                rn,
2277                rm,
2278                alu_op,
2279                size,
2280                idx,
2281            } => {
2282                let (op, size) = match alu_op {
2283                    VecALUModOp::Fmla => ("fmla", size),
2284                    VecALUModOp::Fmls => ("fmls", size),
2285                    _ => unreachable!(),
2286                };
2287                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2288                let ri = pretty_print_vreg_vector(ri, size);
2289                let rn = pretty_print_vreg_vector(rn, size);
2290                let rm = pretty_print_vreg_element(rm, idx.into(), size.lane_size());
2291                format!("{op} {rd}, {ri}, {rn}, {rm}")
2292            }
2293            &Inst::VecRRRLong {
2294                rd,
2295                rn,
2296                rm,
2297                alu_op,
2298                high_half,
2299            } => {
2300                let (op, dest_size, src_size) = match (alu_op, high_half) {
2301                    (VecRRRLongOp::Smull8, false) => {
2302                        ("smull", VectorSize::Size16x8, VectorSize::Size8x8)
2303                    }
2304                    (VecRRRLongOp::Smull8, true) => {
2305                        ("smull2", VectorSize::Size16x8, VectorSize::Size8x16)
2306                    }
2307                    (VecRRRLongOp::Smull16, false) => {
2308                        ("smull", VectorSize::Size32x4, VectorSize::Size16x4)
2309                    }
2310                    (VecRRRLongOp::Smull16, true) => {
2311                        ("smull2", VectorSize::Size32x4, VectorSize::Size16x8)
2312                    }
2313                    (VecRRRLongOp::Smull32, false) => {
2314                        ("smull", VectorSize::Size64x2, VectorSize::Size32x2)
2315                    }
2316                    (VecRRRLongOp::Smull32, true) => {
2317                        ("smull2", VectorSize::Size64x2, VectorSize::Size32x4)
2318                    }
2319                    (VecRRRLongOp::Umull8, false) => {
2320                        ("umull", VectorSize::Size16x8, VectorSize::Size8x8)
2321                    }
2322                    (VecRRRLongOp::Umull8, true) => {
2323                        ("umull2", VectorSize::Size16x8, VectorSize::Size8x16)
2324                    }
2325                    (VecRRRLongOp::Umull16, false) => {
2326                        ("umull", VectorSize::Size32x4, VectorSize::Size16x4)
2327                    }
2328                    (VecRRRLongOp::Umull16, true) => {
2329                        ("umull2", VectorSize::Size32x4, VectorSize::Size16x8)
2330                    }
2331                    (VecRRRLongOp::Umull32, false) => {
2332                        ("umull", VectorSize::Size64x2, VectorSize::Size32x2)
2333                    }
2334                    (VecRRRLongOp::Umull32, true) => {
2335                        ("umull2", VectorSize::Size64x2, VectorSize::Size32x4)
2336                    }
2337                };
2338                let rd = pretty_print_vreg_vector(rd.to_reg(), dest_size);
2339                let rn = pretty_print_vreg_vector(rn, src_size);
2340                let rm = pretty_print_vreg_vector(rm, src_size);
2341                format!("{op} {rd}, {rn}, {rm}")
2342            }
2343            &Inst::VecRRRLongMod {
2344                rd,
2345                ri,
2346                rn,
2347                rm,
2348                alu_op,
2349                high_half,
2350            } => {
2351                let (op, dest_size, src_size) = match (alu_op, high_half) {
2352                    (VecRRRLongModOp::Umlal8, false) => {
2353                        ("umlal", VectorSize::Size16x8, VectorSize::Size8x8)
2354                    }
2355                    (VecRRRLongModOp::Umlal8, true) => {
2356                        ("umlal2", VectorSize::Size16x8, VectorSize::Size8x16)
2357                    }
2358                    (VecRRRLongModOp::Umlal16, false) => {
2359                        ("umlal", VectorSize::Size32x4, VectorSize::Size16x4)
2360                    }
2361                    (VecRRRLongModOp::Umlal16, true) => {
2362                        ("umlal2", VectorSize::Size32x4, VectorSize::Size16x8)
2363                    }
2364                    (VecRRRLongModOp::Umlal32, false) => {
2365                        ("umlal", VectorSize::Size64x2, VectorSize::Size32x2)
2366                    }
2367                    (VecRRRLongModOp::Umlal32, true) => {
2368                        ("umlal2", VectorSize::Size64x2, VectorSize::Size32x4)
2369                    }
2370                };
2371                let rd = pretty_print_vreg_vector(rd.to_reg(), dest_size);
2372                let ri = pretty_print_vreg_vector(ri, dest_size);
2373                let rn = pretty_print_vreg_vector(rn, src_size);
2374                let rm = pretty_print_vreg_vector(rm, src_size);
2375                format!("{op} {rd}, {ri}, {rn}, {rm}")
2376            }
2377            &Inst::VecMisc { op, rd, rn, size } => {
2378                let (op, size, suffix) = match op {
2379                    VecMisc2::Not => (
2380                        "mvn",
2381                        if size.is_128bits() {
2382                            VectorSize::Size8x16
2383                        } else {
2384                            VectorSize::Size8x8
2385                        },
2386                        "",
2387                    ),
2388                    VecMisc2::Neg => ("neg", size, ""),
2389                    VecMisc2::Abs => ("abs", size, ""),
2390                    VecMisc2::Fabs => ("fabs", size, ""),
2391                    VecMisc2::Fneg => ("fneg", size, ""),
2392                    VecMisc2::Fsqrt => ("fsqrt", size, ""),
2393                    VecMisc2::Rev16 => ("rev16", size, ""),
2394                    VecMisc2::Rev32 => ("rev32", size, ""),
2395                    VecMisc2::Rev64 => ("rev64", size, ""),
2396                    VecMisc2::Fcvtzs => ("fcvtzs", size, ""),
2397                    VecMisc2::Fcvtzu => ("fcvtzu", size, ""),
2398                    VecMisc2::Scvtf => ("scvtf", size, ""),
2399                    VecMisc2::Ucvtf => ("ucvtf", size, ""),
2400                    VecMisc2::Frintn => ("frintn", size, ""),
2401                    VecMisc2::Frintz => ("frintz", size, ""),
2402                    VecMisc2::Frintm => ("frintm", size, ""),
2403                    VecMisc2::Frintp => ("frintp", size, ""),
2404                    VecMisc2::Cnt => ("cnt", size, ""),
2405                    VecMisc2::Cmeq0 => ("cmeq", size, ", #0"),
2406                    VecMisc2::Cmge0 => ("cmge", size, ", #0"),
2407                    VecMisc2::Cmgt0 => ("cmgt", size, ", #0"),
2408                    VecMisc2::Cmle0 => ("cmle", size, ", #0"),
2409                    VecMisc2::Cmlt0 => ("cmlt", size, ", #0"),
2410                    VecMisc2::Fcmeq0 => ("fcmeq", size, ", #0.0"),
2411                    VecMisc2::Fcmge0 => ("fcmge", size, ", #0.0"),
2412                    VecMisc2::Fcmgt0 => ("fcmgt", size, ", #0.0"),
2413                    VecMisc2::Fcmle0 => ("fcmle", size, ", #0.0"),
2414                    VecMisc2::Fcmlt0 => ("fcmlt", size, ", #0.0"),
2415                };
2416                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2417                let rn = pretty_print_vreg_vector(rn, size);
2418                format!("{op} {rd}, {rn}{suffix}")
2419            }
2420            &Inst::VecLanes { op, rd, rn, size } => {
2421                let op = match op {
2422                    VecLanesOp::Uminv => "uminv",
2423                    VecLanesOp::Addv => "addv",
2424                };
2425                let rd = pretty_print_vreg_scalar(rd.to_reg(), size.lane_size());
2426                let rn = pretty_print_vreg_vector(rn, size);
2427                format!("{op} {rd}, {rn}")
2428            }
2429            &Inst::VecShiftImm {
2430                op,
2431                rd,
2432                rn,
2433                size,
2434                imm,
2435            } => {
2436                let op = match op {
2437                    VecShiftImmOp::Shl => "shl",
2438                    VecShiftImmOp::Ushr => "ushr",
2439                    VecShiftImmOp::Sshr => "sshr",
2440                };
2441                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2442                let rn = pretty_print_vreg_vector(rn, size);
2443                format!("{op} {rd}, {rn}, #{imm}")
2444            }
2445            &Inst::VecShiftImmMod {
2446                op,
2447                rd,
2448                ri,
2449                rn,
2450                size,
2451                imm,
2452            } => {
2453                let op = match op {
2454                    VecShiftImmModOp::Sli => "sli",
2455                };
2456                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2457                let ri = pretty_print_vreg_vector(ri, size);
2458                let rn = pretty_print_vreg_vector(rn, size);
2459                format!("{op} {rd}, {ri}, {rn}, #{imm}")
2460            }
2461            &Inst::VecExtract { rd, rn, rm, imm4 } => {
2462                let rd = pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size8x16);
2463                let rn = pretty_print_vreg_vector(rn, VectorSize::Size8x16);
2464                let rm = pretty_print_vreg_vector(rm, VectorSize::Size8x16);
2465                format!("ext {rd}, {rn}, {rm}, #{imm4}")
2466            }
2467            &Inst::VecTbl { rd, rn, rm } => {
2468                let rn = pretty_print_vreg_vector(rn, VectorSize::Size8x16);
2469                let rm = pretty_print_vreg_vector(rm, VectorSize::Size8x16);
2470                let rd = pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size8x16);
2471                format!("tbl {rd}, {{ {rn} }}, {rm}")
2472            }
2473            &Inst::VecTblExt { rd, ri, rn, rm } => {
2474                let rn = pretty_print_vreg_vector(rn, VectorSize::Size8x16);
2475                let rm = pretty_print_vreg_vector(rm, VectorSize::Size8x16);
2476                let rd = pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size8x16);
2477                let ri = pretty_print_vreg_vector(ri, VectorSize::Size8x16);
2478                format!("tbx {rd}, {ri}, {{ {rn} }}, {rm}")
2479            }
2480            &Inst::VecTbl2 { rd, rn, rn2, rm } => {
2481                let rn = pretty_print_vreg_vector(rn, VectorSize::Size8x16);
2482                let rn2 = pretty_print_vreg_vector(rn2, VectorSize::Size8x16);
2483                let rm = pretty_print_vreg_vector(rm, VectorSize::Size8x16);
2484                let rd = pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size8x16);
2485                format!("tbl {rd}, {{ {rn}, {rn2} }}, {rm}")
2486            }
2487            &Inst::VecTbl2Ext {
2488                rd,
2489                ri,
2490                rn,
2491                rn2,
2492                rm,
2493            } => {
2494                let rn = pretty_print_vreg_vector(rn, VectorSize::Size8x16);
2495                let rn2 = pretty_print_vreg_vector(rn2, VectorSize::Size8x16);
2496                let rm = pretty_print_vreg_vector(rm, VectorSize::Size8x16);
2497                let rd = pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size8x16);
2498                let ri = pretty_print_vreg_vector(ri, VectorSize::Size8x16);
2499                format!("tbx {rd}, {ri}, {{ {rn}, {rn2} }}, {rm}")
2500            }
2501            &Inst::VecLoadReplicate { rd, rn, size, .. } => {
2502                let rd = pretty_print_vreg_vector(rd.to_reg(), size);
2503                let rn = pretty_print_reg(rn);
2504
2505                format!("ld1r {{ {rd} }}, [{rn}]")
2506            }
2507            &Inst::VecCSel { rd, rn, rm, cond } => {
2508                let rd = pretty_print_vreg_vector(rd.to_reg(), VectorSize::Size8x16);
2509                let rn = pretty_print_vreg_vector(rn, VectorSize::Size8x16);
2510                let rm = pretty_print_vreg_vector(rm, VectorSize::Size8x16);
2511                let cond = cond.pretty_print(0);
2512                format!("vcsel {rd}, {rn}, {rm}, {cond} (if-then-else diamond)")
2513            }
2514            &Inst::MovToNZCV { rn } => {
2515                let rn = pretty_print_reg(rn);
2516                format!("msr nzcv, {rn}")
2517            }
2518            &Inst::MovFromNZCV { rd } => {
2519                let rd = pretty_print_reg(rd.to_reg());
2520                format!("mrs {rd}, nzcv")
2521            }
2522            &Inst::Extend {
2523                rd,
2524                rn,
2525                signed: false,
2526                from_bits: 1,
2527                ..
2528            } => {
2529                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size32);
2530                let rn = pretty_print_ireg(rn, OperandSize::Size32);
2531                format!("and {rd}, {rn}, #1")
2532            }
2533            &Inst::Extend {
2534                rd,
2535                rn,
2536                signed: false,
2537                from_bits: 32,
2538                to_bits: 64,
2539            } => {
2540                // The case of a zero extension from 32 to 64 bits, is implemented
2541                // with a "mov" to a 32-bit (W-reg) dest, because this zeroes
2542                // the top 32 bits.
2543                let rd = pretty_print_ireg(rd.to_reg(), OperandSize::Size32);
2544                let rn = pretty_print_ireg(rn, OperandSize::Size32);
2545                format!("mov {rd}, {rn}")
2546            }
2547            &Inst::Extend {
2548                rd,
2549                rn,
2550                signed,
2551                from_bits,
2552                to_bits,
2553            } => {
2554                assert!(from_bits <= to_bits);
2555                let op = match (signed, from_bits) {
2556                    (false, 8) => "uxtb",
2557                    (true, 8) => "sxtb",
2558                    (false, 16) => "uxth",
2559                    (true, 16) => "sxth",
2560                    (true, 32) => "sxtw",
2561                    (true, _) => "sbfx",
2562                    (false, _) => "ubfx",
2563                };
2564                if op == "sbfx" || op == "ubfx" {
2565                    let dest_size = OperandSize::from_bits(to_bits);
2566                    let rd = pretty_print_ireg(rd.to_reg(), dest_size);
2567                    let rn = pretty_print_ireg(rn, dest_size);
2568                    format!("{op} {rd}, {rn}, #0, #{from_bits}")
2569                } else {
2570                    let dest_size = if signed {
2571                        OperandSize::from_bits(to_bits)
2572                    } else {
2573                        OperandSize::Size32
2574                    };
2575                    let rd = pretty_print_ireg(rd.to_reg(), dest_size);
2576                    let rn = pretty_print_ireg(rn, OperandSize::from_bits(from_bits));
2577                    format!("{op} {rd}, {rn}")
2578                }
2579            }
2580            &Inst::Call { ref info } => {
2581                let try_call = info
2582                    .try_call_info
2583                    .as_ref()
2584                    .map(|tci| pretty_print_try_call(tci))
2585                    .unwrap_or_default();
2586                format!("bl 0{try_call}")
2587            }
2588            &Inst::CallInd { ref info } => {
2589                let rn = pretty_print_reg(info.dest);
2590                let try_call = info
2591                    .try_call_info
2592                    .as_ref()
2593                    .map(|tci| pretty_print_try_call(tci))
2594                    .unwrap_or_default();
2595                format!("blr {rn}{try_call}")
2596            }
2597            &Inst::ReturnCall { ref info } => {
2598                let mut s = format!(
2599                    "return_call {:?} new_stack_arg_size:{}",
2600                    info.dest, info.new_stack_arg_size
2601                );
2602                for ret in &info.uses {
2603                    let preg = pretty_print_reg(ret.preg);
2604                    let vreg = pretty_print_reg(ret.vreg);
2605                    write!(&mut s, " {vreg}={preg}").unwrap();
2606                }
2607                s
2608            }
2609            &Inst::ReturnCallInd { ref info } => {
2610                let callee = pretty_print_reg(info.dest);
2611                let mut s = format!(
2612                    "return_call_ind {callee} new_stack_arg_size:{}",
2613                    info.new_stack_arg_size
2614                );
2615                for ret in &info.uses {
2616                    let preg = pretty_print_reg(ret.preg);
2617                    let vreg = pretty_print_reg(ret.vreg);
2618                    write!(&mut s, " {vreg}={preg}").unwrap();
2619                }
2620                s
2621            }
2622            &Inst::Args { ref args } => {
2623                let mut s = "args".to_string();
2624                for arg in args {
2625                    let preg = pretty_print_reg(arg.preg);
2626                    let def = pretty_print_reg(arg.vreg.to_reg());
2627                    write!(&mut s, " {def}={preg}").unwrap();
2628                }
2629                s
2630            }
2631            &Inst::Rets { ref rets } => {
2632                let mut s = "rets".to_string();
2633                for ret in rets {
2634                    let preg = pretty_print_reg(ret.preg);
2635                    let vreg = pretty_print_reg(ret.vreg);
2636                    write!(&mut s, " {vreg}={preg}").unwrap();
2637                }
2638                s
2639            }
2640            &Inst::Ret {} => "ret".to_string(),
2641            &Inst::AuthenticatedRet { key, is_hint } => {
2642                let key = match key {
2643                    APIKey::AZ => "az",
2644                    APIKey::BZ => "bz",
2645                    APIKey::ASP => "asp",
2646                    APIKey::BSP => "bsp",
2647                };
2648                match is_hint {
2649                    false => format!("reta{key}"),
2650                    true => format!("auti{key} ; ret"),
2651                }
2652            }
2653            &Inst::Jump { ref dest } => {
2654                let dest = dest.pretty_print(0);
2655                format!("b {dest}")
2656            }
2657            &Inst::CondBr {
2658                ref taken,
2659                ref not_taken,
2660                ref kind,
2661            } => {
2662                let taken = taken.pretty_print(0);
2663                let not_taken = not_taken.pretty_print(0);
2664                match kind {
2665                    &CondBrKind::Zero(reg, size) => {
2666                        let reg = pretty_print_reg_sized(reg, size);
2667                        format!("cbz {reg}, {taken} ; b {not_taken}")
2668                    }
2669                    &CondBrKind::NotZero(reg, size) => {
2670                        let reg = pretty_print_reg_sized(reg, size);
2671                        format!("cbnz {reg}, {taken} ; b {not_taken}")
2672                    }
2673                    &CondBrKind::Cond(c) => {
2674                        let c = c.pretty_print(0);
2675                        format!("b.{c} {taken} ; b {not_taken}")
2676                    }
2677                }
2678            }
2679            &Inst::TestBitAndBranch {
2680                kind,
2681                ref taken,
2682                ref not_taken,
2683                rn,
2684                bit,
2685            } => {
2686                let cond = match kind {
2687                    TestBitAndBranchKind::Z => "z",
2688                    TestBitAndBranchKind::NZ => "nz",
2689                };
2690                let taken = taken.pretty_print(0);
2691                let not_taken = not_taken.pretty_print(0);
2692                let rn = pretty_print_reg(rn);
2693                format!("tb{cond} {rn}, #{bit}, {taken} ; b {not_taken}")
2694            }
2695            &Inst::IndirectBr { rn, .. } => {
2696                let rn = pretty_print_reg(rn);
2697                format!("br {rn}")
2698            }
2699            &Inst::Brk => "brk #0xf000".to_string(),
2700            &Inst::Udf { .. } => "udf #0xc11f".to_string(),
2701            &Inst::TrapIf {
2702                ref kind,
2703                trap_code,
2704            } => match kind {
2705                &CondBrKind::Zero(reg, size) => {
2706                    let reg = pretty_print_reg_sized(reg, size);
2707                    format!("cbz {reg}, #trap={trap_code}")
2708                }
2709                &CondBrKind::NotZero(reg, size) => {
2710                    let reg = pretty_print_reg_sized(reg, size);
2711                    format!("cbnz {reg}, #trap={trap_code}")
2712                }
2713                &CondBrKind::Cond(c) => {
2714                    let c = c.pretty_print(0);
2715                    format!("b.{c} #trap={trap_code}")
2716                }
2717            },
2718            &Inst::Adr { rd, off } => {
2719                let rd = pretty_print_reg(rd.to_reg());
2720                format!("adr {rd}, pc+{off}")
2721            }
2722            &Inst::Adrp { rd, off } => {
2723                let rd = pretty_print_reg(rd.to_reg());
2724                // This instruction addresses 4KiB pages, so multiply it by the page size.
2725                let byte_offset = off * 4096;
2726                format!("adrp {rd}, pc+{byte_offset}")
2727            }
2728            &Inst::Word4 { data } => format!("data.i32 {data}"),
2729            &Inst::Word8 { data } => format!("data.i64 {data}"),
2730            &Inst::JTSequence {
2731                default,
2732                ref targets,
2733                ridx,
2734                rtmp1,
2735                rtmp2,
2736                ..
2737            } => {
2738                let ridx = pretty_print_reg(ridx);
2739                let rtmp1 = pretty_print_reg(rtmp1.to_reg());
2740                let rtmp2 = pretty_print_reg(rtmp2.to_reg());
2741                let default_target = BranchTarget::Label(default).pretty_print(0);
2742                format!(
2743                    concat!(
2744                        "b.hs {} ; ",
2745                        "csel {}, xzr, {}, hs ; ",
2746                        "csdb ; ",
2747                        "adr {}, pc+16 ; ",
2748                        "ldrsw {}, [{}, {}, uxtw #2] ; ",
2749                        "add {}, {}, {} ; ",
2750                        "br {} ; ",
2751                        "jt_entries {:?}"
2752                    ),
2753                    default_target,
2754                    rtmp2,
2755                    ridx,
2756                    rtmp1,
2757                    rtmp2,
2758                    rtmp1,
2759                    rtmp2,
2760                    rtmp1,
2761                    rtmp1,
2762                    rtmp2,
2763                    rtmp1,
2764                    targets
2765                )
2766            }
2767            &Inst::LoadExtNameGot { rd, ref name } => {
2768                let rd = pretty_print_reg(rd.to_reg());
2769                format!("load_ext_name_got {rd}, {name:?}")
2770            }
2771            &Inst::LoadExtNameNear {
2772                rd,
2773                ref name,
2774                offset,
2775            } => {
2776                let rd = pretty_print_reg(rd.to_reg());
2777                format!("load_ext_name_near {rd}, {name:?}+{offset}")
2778            }
2779            &Inst::LoadExtNameFar {
2780                rd,
2781                ref name,
2782                offset,
2783            } => {
2784                let rd = pretty_print_reg(rd.to_reg());
2785                format!("load_ext_name_far {rd}, {name:?}+{offset}")
2786            }
2787            &Inst::LoadAddr { rd, ref mem } => {
2788                // TODO: we really should find a better way to avoid duplication of
2789                // this logic between `emit()` and `show_rru()` -- a separate 1-to-N
2790                // expansion stage (i.e., legalization, but without the slow edit-in-place
2791                // of the existing legalization framework).
2792                let mem = mem.clone();
2793                let (mem_insts, mem) = mem_finalize(None, &mem, I8, state);
2794                let mut ret = String::new();
2795                for inst in mem_insts.into_iter() {
2796                    ret.push_str(&inst.print_with_state(&mut EmitState::default()));
2797                }
2798                let (reg, index_reg, offset) = match mem {
2799                    AMode::RegExtended { rn, rm, extendop } => (rn, Some((rm, extendop)), 0),
2800                    AMode::Unscaled { rn, simm9 } => (rn, None, simm9.value()),
2801                    AMode::UnsignedOffset { rn, uimm12 } => (rn, None, uimm12.value() as i32),
2802                    _ => panic!("Unsupported case for LoadAddr: {mem:?}"),
2803                };
2804                let abs_offset = if offset < 0 {
2805                    -offset as u64
2806                } else {
2807                    offset as u64
2808                };
2809                let alu_op = if offset < 0 { ALUOp::Sub } else { ALUOp::Add };
2810
2811                if let Some((idx, extendop)) = index_reg {
2812                    let add = Inst::AluRRRExtend {
2813                        alu_op: ALUOp::Add,
2814                        size: OperandSize::Size64,
2815                        rd,
2816                        rn: reg,
2817                        rm: idx,
2818                        extendop,
2819                    };
2820
2821                    ret.push_str(&add.print_with_state(&mut EmitState::default()));
2822                } else if offset == 0 {
2823                    let mov = Inst::gen_move(rd, reg, I64);
2824                    ret.push_str(&mov.print_with_state(&mut EmitState::default()));
2825                } else if let Some(imm12) = Imm12::maybe_from_u64(abs_offset) {
2826                    let add = Inst::AluRRImm12 {
2827                        alu_op,
2828                        size: OperandSize::Size64,
2829                        rd,
2830                        rn: reg,
2831                        imm12,
2832                    };
2833                    ret.push_str(&add.print_with_state(&mut EmitState::default()));
2834                } else {
2835                    let tmp = writable_spilltmp_reg();
2836                    for inst in Inst::load_constant(tmp, abs_offset).into_iter() {
2837                        ret.push_str(&inst.print_with_state(&mut EmitState::default()));
2838                    }
2839                    let add = Inst::AluRRR {
2840                        alu_op,
2841                        size: OperandSize::Size64,
2842                        rd,
2843                        rn: reg,
2844                        rm: tmp.to_reg(),
2845                    };
2846                    ret.push_str(&add.print_with_state(&mut EmitState::default()));
2847                }
2848                ret
2849            }
2850            &Inst::Paci { key } => {
2851                let key = match key {
2852                    APIKey::AZ => "az",
2853                    APIKey::BZ => "bz",
2854                    APIKey::ASP => "asp",
2855                    APIKey::BSP => "bsp",
2856                };
2857
2858                "paci".to_string() + key
2859            }
2860            &Inst::Xpaclri => "xpaclri".to_string(),
2861            &Inst::Bti { targets } => {
2862                let targets = match targets {
2863                    BranchTargetType::None => "",
2864                    BranchTargetType::C => " c",
2865                    BranchTargetType::J => " j",
2866                    BranchTargetType::JC => " jc",
2867                };
2868
2869                "bti".to_string() + targets
2870            }
2871            &Inst::EmitIsland { needed_space } => format!("emit_island {needed_space}"),
2872
2873            &Inst::ElfTlsGetAddr {
2874                ref symbol,
2875                rd,
2876                tmp,
2877            } => {
2878                let rd = pretty_print_reg(rd.to_reg());
2879                let tmp = pretty_print_reg(tmp.to_reg());
2880                format!("elf_tls_get_addr {}, {}, {}", rd, tmp, symbol.display(None))
2881            }
2882            &Inst::MachOTlsGetAddr { ref symbol, rd } => {
2883                let rd = pretty_print_reg(rd.to_reg());
2884                format!("macho_tls_get_addr {}, {}", rd, symbol.display(None))
2885            }
2886            &Inst::Unwind { ref inst } => {
2887                format!("unwind {inst:?}")
2888            }
2889            &Inst::DummyUse { reg } => {
2890                let reg = pretty_print_reg(reg);
2891                format!("dummy_use {reg}")
2892            }
2893            &Inst::LabelAddress { dst, label } => {
2894                let dst = pretty_print_reg(dst.to_reg());
2895                format!("label_address {dst}, {label:?}")
2896            }
2897            &Inst::StackProbeLoop { start, end, step } => {
2898                let start = pretty_print_reg(start.to_reg());
2899                let end = pretty_print_reg(end);
2900                let step = step.pretty_print(0);
2901                format!("stack_probe_loop {start}, {end}, {step}")
2902            }
2903        }
2904    }
2905}
2906
2907//=============================================================================
2908// Label fixups and jump veneers.
2909
2910/// Different forms of label references for different instruction formats.
2911#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2912pub enum LabelUse {
2913    /// 14-bit branch offset (conditional branches). PC-rel, offset is imm <<
2914    /// 2. Immediate is 14 signed bits, in bits 18:5. Used by tbz and tbnz.
2915    Branch14,
2916    /// 19-bit branch offset (conditional branches). PC-rel, offset is imm << 2. Immediate is 19
2917    /// signed bits, in bits 23:5. Used by cbz, cbnz, b.cond.
2918    Branch19,
2919    /// 26-bit branch offset (unconditional branches). PC-rel, offset is imm << 2. Immediate is 26
2920    /// signed bits, in bits 25:0. Used by b, bl.
2921    Branch26,
2922    /// 19-bit offset for LDR (load literal). PC-rel, offset is imm << 2. Immediate is 19 signed bits,
2923    /// in bits 23:5.
2924    Ldr19,
2925    /// 21-bit offset for ADR (get address of label). PC-rel, offset is not shifted. Immediate is
2926    /// 21 signed bits, with high 19 bits in bits 23:5 and low 2 bits in bits 30:29.
2927    Adr21,
2928    /// 32-bit PC relative constant offset (from address of constant itself),
2929    /// signed. Used in jump tables.
2930    PCRel32,
2931}
2932
2933impl MachInstLabelUse for LabelUse {
2934    /// Alignment for veneer code. Every AArch64 instruction must be 4-byte-aligned.
2935    const ALIGN: CodeOffset = 4;
2936
2937    /// Maximum PC-relative range (positive), inclusive.
2938    fn max_pos_range(self) -> CodeOffset {
2939        match self {
2940            // N-bit immediate, left-shifted by 2, for (N+2) bits of total
2941            // range. Signed, so +2^(N+1) from zero. Likewise for two other
2942            // shifted cases below.
2943            LabelUse::Branch14 => (1 << 15) - 1,
2944            LabelUse::Branch19 => (1 << 20) - 1,
2945            LabelUse::Branch26 => (1 << 27) - 1,
2946            LabelUse::Ldr19 => (1 << 20) - 1,
2947            // Adr does not shift its immediate, so the 21-bit immediate gives 21 bits of total
2948            // range.
2949            LabelUse::Adr21 => (1 << 20) - 1,
2950            LabelUse::PCRel32 => 0x7fffffff,
2951        }
2952    }
2953
2954    /// Maximum PC-relative range (negative).
2955    fn max_neg_range(self) -> CodeOffset {
2956        // All forms are twos-complement signed offsets, so negative limit is one more than
2957        // positive limit.
2958        self.max_pos_range() + 1
2959    }
2960
2961    /// Size of window into code needed to do the patch.
2962    fn patch_size(self) -> CodeOffset {
2963        // Patch is on one instruction only for all of these label reference types.
2964        4
2965    }
2966
2967    /// Perform the patch.
2968    fn patch(self, buffer: &mut [u8], use_offset: CodeOffset, label_offset: CodeOffset) {
2969        let pc_rel = (label_offset as i64) - (use_offset as i64);
2970        debug_assert!(pc_rel <= self.max_pos_range() as i64);
2971        debug_assert!(pc_rel >= -(self.max_neg_range() as i64));
2972        let pc_rel = pc_rel as u32;
2973        let insn_word = u32::from_le_bytes([buffer[0], buffer[1], buffer[2], buffer[3]]);
2974        let mask = match self {
2975            LabelUse::Branch14 => 0x0007ffe0, // bits 18..5 inclusive
2976            LabelUse::Branch19 => 0x00ffffe0, // bits 23..5 inclusive
2977            LabelUse::Branch26 => 0x03ffffff, // bits 25..0 inclusive
2978            LabelUse::Ldr19 => 0x00ffffe0,    // bits 23..5 inclusive
2979            LabelUse::Adr21 => 0x60ffffe0,    // bits 30..29, 25..5 inclusive
2980            LabelUse::PCRel32 => 0xffffffff,
2981        };
2982        let pc_rel_shifted = match self {
2983            LabelUse::Adr21 | LabelUse::PCRel32 => pc_rel,
2984            _ => {
2985                debug_assert!(pc_rel & 3 == 0);
2986                pc_rel >> 2
2987            }
2988        };
2989        let pc_rel_inserted = match self {
2990            LabelUse::Branch14 => (pc_rel_shifted & 0x3fff) << 5,
2991            LabelUse::Branch19 | LabelUse::Ldr19 => (pc_rel_shifted & 0x7ffff) << 5,
2992            LabelUse::Branch26 => pc_rel_shifted & 0x3ffffff,
2993            // Note: the *low* two bits of offset are put in the
2994            // *high* bits (30, 29).
2995            LabelUse::Adr21 => (pc_rel_shifted & 0x1ffffc) << 3 | (pc_rel_shifted & 3) << 29,
2996            LabelUse::PCRel32 => pc_rel_shifted,
2997        };
2998        let is_add = match self {
2999            LabelUse::PCRel32 => true,
3000            _ => false,
3001        };
3002        let insn_word = if is_add {
3003            insn_word.wrapping_add(pc_rel_inserted)
3004        } else {
3005            (insn_word & !mask) | pc_rel_inserted
3006        };
3007        buffer[0..4].clone_from_slice(&u32::to_le_bytes(insn_word));
3008    }
3009
3010    /// Is a veneer supported for this label reference type?
3011    fn supports_veneer(self) -> bool {
3012        match self {
3013            LabelUse::Branch14 | LabelUse::Branch19 => true, // veneer is a Branch26
3014            LabelUse::Branch26 => true,                      // veneer is a PCRel32
3015            _ => false,
3016        }
3017    }
3018
3019    /// How large is the veneer, if supported?
3020    fn veneer_size(self) -> CodeOffset {
3021        match self {
3022            LabelUse::Branch14 | LabelUse::Branch19 => 4,
3023            LabelUse::Branch26 => 20,
3024            _ => unreachable!(),
3025        }
3026    }
3027
3028    fn worst_case_veneer_size() -> CodeOffset {
3029        20
3030    }
3031
3032    /// Generate a veneer into the buffer, given that this veneer is at `veneer_offset`, and return
3033    /// an offset and label-use for the veneer's use of the original label.
3034    fn generate_veneer(
3035        self,
3036        buffer: &mut [u8],
3037        veneer_offset: CodeOffset,
3038    ) -> (CodeOffset, LabelUse) {
3039        match self {
3040            LabelUse::Branch14 | LabelUse::Branch19 => {
3041                // veneer is a Branch26 (unconditional branch). Just encode directly here -- don't
3042                // bother with constructing an Inst.
3043                let insn_word = 0b000101 << 26;
3044                buffer[0..4].clone_from_slice(&u32::to_le_bytes(insn_word));
3045                (veneer_offset, LabelUse::Branch26)
3046            }
3047
3048            // This is promoting a 26-bit call/jump to a 32-bit call/jump to
3049            // get a further range. This jump translates to a jump to a
3050            // relative location based on the address of the constant loaded
3051            // from here.
3052            //
3053            // If this path is taken from a call instruction then caller-saved
3054            // registers are available (minus arguments), so x16/x17 are
3055            // available. Otherwise for intra-function jumps we also reserve
3056            // x16/x17 as spill-style registers. In both cases these are
3057            // available for us to use.
3058            LabelUse::Branch26 => {
3059                let tmp1 = regs::spilltmp_reg();
3060                let tmp1_w = regs::writable_spilltmp_reg();
3061                let tmp2 = regs::tmp2_reg();
3062                let tmp2_w = regs::writable_tmp2_reg();
3063                // ldrsw x16, 16
3064                let ldr = emit::enc_ldst_imm19(0b1001_1000, 16 / 4, tmp1);
3065                // adr x17, 12
3066                let adr = emit::enc_adr(12, tmp2_w);
3067                // add x16, x16, x17
3068                let add = emit::enc_arith_rrr(0b10001011_000, 0, tmp1_w, tmp1, tmp2);
3069                // br x16
3070                let br = emit::enc_br(tmp1);
3071                buffer[0..4].clone_from_slice(&u32::to_le_bytes(ldr));
3072                buffer[4..8].clone_from_slice(&u32::to_le_bytes(adr));
3073                buffer[8..12].clone_from_slice(&u32::to_le_bytes(add));
3074                buffer[12..16].clone_from_slice(&u32::to_le_bytes(br));
3075                // the 4-byte signed immediate we'll load is after these
3076                // instructions, 16-bytes in.
3077                (veneer_offset + 16, LabelUse::PCRel32)
3078            }
3079
3080            _ => panic!("Unsupported label-reference type for veneer generation!"),
3081        }
3082    }
3083
3084    fn from_reloc(reloc: Reloc, addend: Addend) -> Option<LabelUse> {
3085        match (reloc, addend) {
3086            (Reloc::Arm64Call, 0) => Some(LabelUse::Branch26),
3087            _ => None,
3088        }
3089    }
3090}
3091
3092#[cfg(test)]
3093mod tests {
3094    use super::*;
3095
3096    #[test]
3097    fn inst_size_test() {
3098        // This test will help with unintentionally growing the size
3099        // of the Inst enum.
3100        let expected = if cfg!(target_pointer_width = "32") && !cfg!(target_arch = "arm") {
3101            28
3102        } else {
3103            32
3104        };
3105        assert_eq!(expected, std::mem::size_of::<Inst>());
3106    }
3107}