xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 67d10fc4)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "exec/exec-all.h"
22 #include "translate.h"
23 #include "translate-a64.h"
24 #include "qemu/log.h"
25 #include "disas/disas.h"
26 #include "arm_ldst.h"
27 #include "semihosting/semihost.h"
28 #include "cpregs.h"
29 
30 static TCGv_i64 cpu_X[32];
31 static TCGv_i64 cpu_pc;
32 
33 /* Load/store exclusive handling */
34 static TCGv_i64 cpu_exclusive_high;
35 
36 static const char *regnames[] = {
37     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
38     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
39     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
40     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
41 };
42 
43 enum a64_shift_type {
44     A64_SHIFT_TYPE_LSL = 0,
45     A64_SHIFT_TYPE_LSR = 1,
46     A64_SHIFT_TYPE_ASR = 2,
47     A64_SHIFT_TYPE_ROR = 3
48 };
49 
50 /*
51  * Helpers for extracting complex instruction fields
52  */
53 
54 /*
55  * For load/store with an unsigned 12 bit immediate scaled by the element
56  * size. The input has the immediate field in bits [14:3] and the element
57  * size in [2:0].
58  */
59 static int uimm_scaled(DisasContext *s, int x)
60 {
61     unsigned imm = x >> 3;
62     unsigned scale = extract32(x, 0, 3);
63     return imm << scale;
64 }
65 
66 /* For load/store memory tags: scale offset by LOG2_TAG_GRANULE */
67 static int scale_by_log2_tag_granule(DisasContext *s, int x)
68 {
69     return x << LOG2_TAG_GRANULE;
70 }
71 
72 /*
73  * Include the generated decoders.
74  */
75 
76 #include "decode-sme-fa64.c.inc"
77 #include "decode-a64.c.inc"
78 
79 /* Table based decoder typedefs - used when the relevant bits for decode
80  * are too awkwardly scattered across the instruction (eg SIMD).
81  */
82 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
83 
84 typedef struct AArch64DecodeTable {
85     uint32_t pattern;
86     uint32_t mask;
87     AArch64DecodeFn *disas_fn;
88 } AArch64DecodeTable;
89 
90 /* initialize TCG globals.  */
91 void a64_translate_init(void)
92 {
93     int i;
94 
95     cpu_pc = tcg_global_mem_new_i64(tcg_env,
96                                     offsetof(CPUARMState, pc),
97                                     "pc");
98     for (i = 0; i < 32; i++) {
99         cpu_X[i] = tcg_global_mem_new_i64(tcg_env,
100                                           offsetof(CPUARMState, xregs[i]),
101                                           regnames[i]);
102     }
103 
104     cpu_exclusive_high = tcg_global_mem_new_i64(tcg_env,
105         offsetof(CPUARMState, exclusive_high), "exclusive_high");
106 }
107 
108 /*
109  * Return the core mmu_idx to use for A64 load/store insns which
110  * have a "unprivileged load/store" variant. Those insns access
111  * EL0 if executed from an EL which has control over EL0 (usually
112  * EL1) but behave like normal loads and stores if executed from
113  * elsewhere (eg EL3).
114  *
115  * @unpriv : true for the unprivileged encoding; false for the
116  *           normal encoding (in which case we will return the same
117  *           thing as get_mem_index().
118  */
119 static int get_a64_user_mem_index(DisasContext *s, bool unpriv)
120 {
121     /*
122      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
123      * which is the usual mmu_idx for this cpu state.
124      */
125     ARMMMUIdx useridx = s->mmu_idx;
126 
127     if (unpriv && s->unpriv) {
128         /*
129          * We have pre-computed the condition for AccType_UNPRIV.
130          * Therefore we should never get here with a mmu_idx for
131          * which we do not know the corresponding user mmu_idx.
132          */
133         switch (useridx) {
134         case ARMMMUIdx_E10_1:
135         case ARMMMUIdx_E10_1_PAN:
136             useridx = ARMMMUIdx_E10_0;
137             break;
138         case ARMMMUIdx_E20_2:
139         case ARMMMUIdx_E20_2_PAN:
140             useridx = ARMMMUIdx_E20_0;
141             break;
142         default:
143             g_assert_not_reached();
144         }
145     }
146     return arm_to_core_mmu_idx(useridx);
147 }
148 
149 static void set_btype_raw(int val)
150 {
151     tcg_gen_st_i32(tcg_constant_i32(val), tcg_env,
152                    offsetof(CPUARMState, btype));
153 }
154 
155 static void set_btype(DisasContext *s, int val)
156 {
157     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
158     tcg_debug_assert(val >= 1 && val <= 3);
159     set_btype_raw(val);
160     s->btype = -1;
161 }
162 
163 static void reset_btype(DisasContext *s)
164 {
165     if (s->btype != 0) {
166         set_btype_raw(0);
167         s->btype = 0;
168     }
169 }
170 
171 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
172 {
173     assert(s->pc_save != -1);
174     if (tb_cflags(s->base.tb) & CF_PCREL) {
175         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
176     } else {
177         tcg_gen_movi_i64(dest, s->pc_curr + diff);
178     }
179 }
180 
181 void gen_a64_update_pc(DisasContext *s, target_long diff)
182 {
183     gen_pc_plus_diff(s, cpu_pc, diff);
184     s->pc_save = s->pc_curr + diff;
185 }
186 
187 /*
188  * Handle Top Byte Ignore (TBI) bits.
189  *
190  * If address tagging is enabled via the TCR TBI bits:
191  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
192  *    then the address is zero-extended, clearing bits [63:56]
193  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
194  *    and TBI1 controls addresses with bit 55 == 1.
195  *    If the appropriate TBI bit is set for the address then
196  *    the address is sign-extended from bit 55 into bits [63:56]
197  *
198  * Here We have concatenated TBI{1,0} into tbi.
199  */
200 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
201                                 TCGv_i64 src, int tbi)
202 {
203     if (tbi == 0) {
204         /* Load unmodified address */
205         tcg_gen_mov_i64(dst, src);
206     } else if (!regime_has_2_ranges(s->mmu_idx)) {
207         /* Force tag byte to all zero */
208         tcg_gen_extract_i64(dst, src, 0, 56);
209     } else {
210         /* Sign-extend from bit 55.  */
211         tcg_gen_sextract_i64(dst, src, 0, 56);
212 
213         switch (tbi) {
214         case 1:
215             /* tbi0 but !tbi1: only use the extension if positive */
216             tcg_gen_and_i64(dst, dst, src);
217             break;
218         case 2:
219             /* !tbi0 but tbi1: only use the extension if negative */
220             tcg_gen_or_i64(dst, dst, src);
221             break;
222         case 3:
223             /* tbi0 and tbi1: always use the extension */
224             break;
225         default:
226             g_assert_not_reached();
227         }
228     }
229 }
230 
231 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
232 {
233     /*
234      * If address tagging is enabled for instructions via the TCR TBI bits,
235      * then loading an address into the PC will clear out any tag.
236      */
237     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
238     s->pc_save = -1;
239 }
240 
241 /*
242  * Handle MTE and/or TBI.
243  *
244  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
245  * for the tag to be present in the FAR_ELx register.  But for user-only
246  * mode we do not have a TLB with which to implement this, so we must
247  * remove the top byte now.
248  *
249  * Always return a fresh temporary that we can increment independently
250  * of the write-back address.
251  */
252 
253 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
254 {
255     TCGv_i64 clean = tcg_temp_new_i64();
256 #ifdef CONFIG_USER_ONLY
257     gen_top_byte_ignore(s, clean, addr, s->tbid);
258 #else
259     tcg_gen_mov_i64(clean, addr);
260 #endif
261     return clean;
262 }
263 
264 /* Insert a zero tag into src, with the result at dst. */
265 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
266 {
267     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
268 }
269 
270 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
271                              MMUAccessType acc, int log2_size)
272 {
273     gen_helper_probe_access(tcg_env, ptr,
274                             tcg_constant_i32(acc),
275                             tcg_constant_i32(get_mem_index(s)),
276                             tcg_constant_i32(1 << log2_size));
277 }
278 
279 /*
280  * For MTE, check a single logical or atomic access.  This probes a single
281  * address, the exact one specified.  The size and alignment of the access
282  * is not relevant to MTE, per se, but watchpoints do require the size,
283  * and we want to recognize those before making any other changes to state.
284  */
285 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
286                                       bool is_write, bool tag_checked,
287                                       MemOp memop, bool is_unpriv,
288                                       int core_idx)
289 {
290     if (tag_checked && s->mte_active[is_unpriv]) {
291         TCGv_i64 ret;
292         int desc = 0;
293 
294         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
295         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
296         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
297         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
298         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
299         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
300 
301         ret = tcg_temp_new_i64();
302         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
303 
304         return ret;
305     }
306     return clean_data_tbi(s, addr);
307 }
308 
309 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
310                         bool tag_checked, MemOp memop)
311 {
312     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
313                                  false, get_mem_index(s));
314 }
315 
316 /*
317  * For MTE, check multiple logical sequential accesses.
318  */
319 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
320                         bool tag_checked, int total_size, MemOp single_mop)
321 {
322     if (tag_checked && s->mte_active[0]) {
323         TCGv_i64 ret;
324         int desc = 0;
325 
326         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
327         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
328         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
329         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
330         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
331         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
332 
333         ret = tcg_temp_new_i64();
334         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
335 
336         return ret;
337     }
338     return clean_data_tbi(s, addr);
339 }
340 
341 /*
342  * Generate the special alignment check that applies to AccType_ATOMIC
343  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
344  * naturally aligned, but it must not cross a 16-byte boundary.
345  * See AArch64.CheckAlignment().
346  */
347 static void check_lse2_align(DisasContext *s, int rn, int imm,
348                              bool is_write, MemOp mop)
349 {
350     TCGv_i32 tmp;
351     TCGv_i64 addr;
352     TCGLabel *over_label;
353     MMUAccessType type;
354     int mmu_idx;
355 
356     tmp = tcg_temp_new_i32();
357     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
358     tcg_gen_addi_i32(tmp, tmp, imm & 15);
359     tcg_gen_andi_i32(tmp, tmp, 15);
360     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
361 
362     over_label = gen_new_label();
363     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
364 
365     addr = tcg_temp_new_i64();
366     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
367 
368     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
369     mmu_idx = get_mem_index(s);
370     gen_helper_unaligned_access(tcg_env, addr, tcg_constant_i32(type),
371                                 tcg_constant_i32(mmu_idx));
372 
373     gen_set_label(over_label);
374 
375 }
376 
377 /* Handle the alignment check for AccType_ATOMIC instructions. */
378 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
379 {
380     MemOp size = mop & MO_SIZE;
381 
382     if (size == MO_8) {
383         return mop;
384     }
385 
386     /*
387      * If size == MO_128, this is a LDXP, and the operation is single-copy
388      * atomic for each doubleword, not the entire quadword; it still must
389      * be quadword aligned.
390      */
391     if (size == MO_128) {
392         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
393                                    MO_ATOM_IFALIGN_PAIR);
394     }
395     if (dc_isar_feature(aa64_lse2, s)) {
396         check_lse2_align(s, rn, 0, true, mop);
397     } else {
398         mop |= MO_ALIGN;
399     }
400     return finalize_memop(s, mop);
401 }
402 
403 /* Handle the alignment check for AccType_ORDERED instructions. */
404 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
405                                  bool is_write, MemOp mop)
406 {
407     MemOp size = mop & MO_SIZE;
408 
409     if (size == MO_8) {
410         return mop;
411     }
412     if (size == MO_128) {
413         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
414                                    MO_ATOM_IFALIGN_PAIR);
415     }
416     if (!dc_isar_feature(aa64_lse2, s)) {
417         mop |= MO_ALIGN;
418     } else if (!s->naa) {
419         check_lse2_align(s, rn, imm, is_write, mop);
420     }
421     return finalize_memop(s, mop);
422 }
423 
424 typedef struct DisasCompare64 {
425     TCGCond cond;
426     TCGv_i64 value;
427 } DisasCompare64;
428 
429 static void a64_test_cc(DisasCompare64 *c64, int cc)
430 {
431     DisasCompare c32;
432 
433     arm_test_cc(&c32, cc);
434 
435     /*
436      * Sign-extend the 32-bit value so that the GE/LT comparisons work
437      * properly.  The NE/EQ comparisons are also fine with this choice.
438       */
439     c64->cond = c32.cond;
440     c64->value = tcg_temp_new_i64();
441     tcg_gen_ext_i32_i64(c64->value, c32.value);
442 }
443 
444 static void gen_rebuild_hflags(DisasContext *s)
445 {
446     gen_helper_rebuild_hflags_a64(tcg_env, tcg_constant_i32(s->current_el));
447 }
448 
449 static void gen_exception_internal(int excp)
450 {
451     assert(excp_is_internal(excp));
452     gen_helper_exception_internal(tcg_env, tcg_constant_i32(excp));
453 }
454 
455 static void gen_exception_internal_insn(DisasContext *s, int excp)
456 {
457     gen_a64_update_pc(s, 0);
458     gen_exception_internal(excp);
459     s->base.is_jmp = DISAS_NORETURN;
460 }
461 
462 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
463 {
464     gen_a64_update_pc(s, 0);
465     gen_helper_exception_bkpt_insn(tcg_env, tcg_constant_i32(syndrome));
466     s->base.is_jmp = DISAS_NORETURN;
467 }
468 
469 static void gen_step_complete_exception(DisasContext *s)
470 {
471     /* We just completed step of an insn. Move from Active-not-pending
472      * to Active-pending, and then also take the swstep exception.
473      * This corresponds to making the (IMPDEF) choice to prioritize
474      * swstep exceptions over asynchronous exceptions taken to an exception
475      * level where debug is disabled. This choice has the advantage that
476      * we do not need to maintain internal state corresponding to the
477      * ISV/EX syndrome bits between completion of the step and generation
478      * of the exception, and our syndrome information is always correct.
479      */
480     gen_ss_advance(s);
481     gen_swstep_exception(s, 1, s->is_ldex);
482     s->base.is_jmp = DISAS_NORETURN;
483 }
484 
485 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
486 {
487     if (s->ss_active) {
488         return false;
489     }
490     return translator_use_goto_tb(&s->base, dest);
491 }
492 
493 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
494 {
495     if (use_goto_tb(s, s->pc_curr + diff)) {
496         /*
497          * For pcrel, the pc must always be up-to-date on entry to
498          * the linked TB, so that it can use simple additions for all
499          * further adjustments.  For !pcrel, the linked TB is compiled
500          * to know its full virtual address, so we can delay the
501          * update to pc to the unlinked path.  A long chain of links
502          * can thus avoid many updates to the PC.
503          */
504         if (tb_cflags(s->base.tb) & CF_PCREL) {
505             gen_a64_update_pc(s, diff);
506             tcg_gen_goto_tb(n);
507         } else {
508             tcg_gen_goto_tb(n);
509             gen_a64_update_pc(s, diff);
510         }
511         tcg_gen_exit_tb(s->base.tb, n);
512         s->base.is_jmp = DISAS_NORETURN;
513     } else {
514         gen_a64_update_pc(s, diff);
515         if (s->ss_active) {
516             gen_step_complete_exception(s);
517         } else {
518             tcg_gen_lookup_and_goto_ptr();
519             s->base.is_jmp = DISAS_NORETURN;
520         }
521     }
522 }
523 
524 /*
525  * Register access functions
526  *
527  * These functions are used for directly accessing a register in where
528  * changes to the final register value are likely to be made. If you
529  * need to use a register for temporary calculation (e.g. index type
530  * operations) use the read_* form.
531  *
532  * B1.2.1 Register mappings
533  *
534  * In instruction register encoding 31 can refer to ZR (zero register) or
535  * the SP (stack pointer) depending on context. In QEMU's case we map SP
536  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
537  * This is the point of the _sp forms.
538  */
539 TCGv_i64 cpu_reg(DisasContext *s, int reg)
540 {
541     if (reg == 31) {
542         TCGv_i64 t = tcg_temp_new_i64();
543         tcg_gen_movi_i64(t, 0);
544         return t;
545     } else {
546         return cpu_X[reg];
547     }
548 }
549 
550 /* register access for when 31 == SP */
551 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
552 {
553     return cpu_X[reg];
554 }
555 
556 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
557  * representing the register contents. This TCGv is an auto-freed
558  * temporary so it need not be explicitly freed, and may be modified.
559  */
560 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
561 {
562     TCGv_i64 v = tcg_temp_new_i64();
563     if (reg != 31) {
564         if (sf) {
565             tcg_gen_mov_i64(v, cpu_X[reg]);
566         } else {
567             tcg_gen_ext32u_i64(v, cpu_X[reg]);
568         }
569     } else {
570         tcg_gen_movi_i64(v, 0);
571     }
572     return v;
573 }
574 
575 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
576 {
577     TCGv_i64 v = tcg_temp_new_i64();
578     if (sf) {
579         tcg_gen_mov_i64(v, cpu_X[reg]);
580     } else {
581         tcg_gen_ext32u_i64(v, cpu_X[reg]);
582     }
583     return v;
584 }
585 
586 /* Return the offset into CPUARMState of a slice (from
587  * the least significant end) of FP register Qn (ie
588  * Dn, Sn, Hn or Bn).
589  * (Note that this is not the same mapping as for A32; see cpu.h)
590  */
591 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
592 {
593     return vec_reg_offset(s, regno, 0, size);
594 }
595 
596 /* Offset of the high half of the 128 bit vector Qn */
597 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
598 {
599     return vec_reg_offset(s, regno, 1, MO_64);
600 }
601 
602 /* Convenience accessors for reading and writing single and double
603  * FP registers. Writing clears the upper parts of the associated
604  * 128 bit vector register, as required by the architecture.
605  * Note that unlike the GP register accessors, the values returned
606  * by the read functions must be manually freed.
607  */
608 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
609 {
610     TCGv_i64 v = tcg_temp_new_i64();
611 
612     tcg_gen_ld_i64(v, tcg_env, fp_reg_offset(s, reg, MO_64));
613     return v;
614 }
615 
616 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
617 {
618     TCGv_i32 v = tcg_temp_new_i32();
619 
620     tcg_gen_ld_i32(v, tcg_env, fp_reg_offset(s, reg, MO_32));
621     return v;
622 }
623 
624 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
625 {
626     TCGv_i32 v = tcg_temp_new_i32();
627 
628     tcg_gen_ld16u_i32(v, tcg_env, fp_reg_offset(s, reg, MO_16));
629     return v;
630 }
631 
632 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
633  * If SVE is not enabled, then there are only 128 bits in the vector.
634  */
635 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
636 {
637     unsigned ofs = fp_reg_offset(s, rd, MO_64);
638     unsigned vsz = vec_full_reg_size(s);
639 
640     /* Nop move, with side effect of clearing the tail. */
641     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
642 }
643 
644 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
645 {
646     unsigned ofs = fp_reg_offset(s, reg, MO_64);
647 
648     tcg_gen_st_i64(v, tcg_env, ofs);
649     clear_vec_high(s, false, reg);
650 }
651 
652 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
653 {
654     TCGv_i64 tmp = tcg_temp_new_i64();
655 
656     tcg_gen_extu_i32_i64(tmp, v);
657     write_fp_dreg(s, reg, tmp);
658 }
659 
660 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
661 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
662                          GVecGen2Fn *gvec_fn, int vece)
663 {
664     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
665             is_q ? 16 : 8, vec_full_reg_size(s));
666 }
667 
668 /* Expand a 2-operand + immediate AdvSIMD vector operation using
669  * an expander function.
670  */
671 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
672                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
673 {
674     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
675             imm, is_q ? 16 : 8, vec_full_reg_size(s));
676 }
677 
678 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
679 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
680                          GVecGen3Fn *gvec_fn, int vece)
681 {
682     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
683             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
684 }
685 
686 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
687 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
688                          int rx, GVecGen4Fn *gvec_fn, int vece)
689 {
690     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
691             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
692             is_q ? 16 : 8, vec_full_reg_size(s));
693 }
694 
695 /* Expand a 2-operand operation using an out-of-line helper.  */
696 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
697                              int rn, int data, gen_helper_gvec_2 *fn)
698 {
699     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
700                        vec_full_reg_offset(s, rn),
701                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
702 }
703 
704 /* Expand a 3-operand operation using an out-of-line helper.  */
705 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
706                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
707 {
708     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
709                        vec_full_reg_offset(s, rn),
710                        vec_full_reg_offset(s, rm),
711                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
712 }
713 
714 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
715  * an out-of-line helper.
716  */
717 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
718                               int rm, bool is_fp16, int data,
719                               gen_helper_gvec_3_ptr *fn)
720 {
721     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
722     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
723                        vec_full_reg_offset(s, rn),
724                        vec_full_reg_offset(s, rm), fpst,
725                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
726 }
727 
728 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
729 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
730                             int rm, gen_helper_gvec_3_ptr *fn)
731 {
732     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
733 
734     tcg_gen_addi_ptr(qc_ptr, tcg_env, offsetof(CPUARMState, vfp.qc));
735     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
736                        vec_full_reg_offset(s, rn),
737                        vec_full_reg_offset(s, rm), qc_ptr,
738                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
739 }
740 
741 /* Expand a 4-operand operation using an out-of-line helper.  */
742 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
743                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
744 {
745     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
746                        vec_full_reg_offset(s, rn),
747                        vec_full_reg_offset(s, rm),
748                        vec_full_reg_offset(s, ra),
749                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
750 }
751 
752 /*
753  * Expand a 4-operand + fpstatus pointer + simd data value operation using
754  * an out-of-line helper.
755  */
756 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
757                               int rm, int ra, bool is_fp16, int data,
758                               gen_helper_gvec_4_ptr *fn)
759 {
760     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
761     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
762                        vec_full_reg_offset(s, rn),
763                        vec_full_reg_offset(s, rm),
764                        vec_full_reg_offset(s, ra), fpst,
765                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
766 }
767 
768 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
769  * than the 32 bit equivalent.
770  */
771 static inline void gen_set_NZ64(TCGv_i64 result)
772 {
773     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
774     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
775 }
776 
777 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
778 static inline void gen_logic_CC(int sf, TCGv_i64 result)
779 {
780     if (sf) {
781         gen_set_NZ64(result);
782     } else {
783         tcg_gen_extrl_i64_i32(cpu_ZF, result);
784         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
785     }
786     tcg_gen_movi_i32(cpu_CF, 0);
787     tcg_gen_movi_i32(cpu_VF, 0);
788 }
789 
790 /* dest = T0 + T1; compute C, N, V and Z flags */
791 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
792 {
793     TCGv_i64 result, flag, tmp;
794     result = tcg_temp_new_i64();
795     flag = tcg_temp_new_i64();
796     tmp = tcg_temp_new_i64();
797 
798     tcg_gen_movi_i64(tmp, 0);
799     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
800 
801     tcg_gen_extrl_i64_i32(cpu_CF, flag);
802 
803     gen_set_NZ64(result);
804 
805     tcg_gen_xor_i64(flag, result, t0);
806     tcg_gen_xor_i64(tmp, t0, t1);
807     tcg_gen_andc_i64(flag, flag, tmp);
808     tcg_gen_extrh_i64_i32(cpu_VF, flag);
809 
810     tcg_gen_mov_i64(dest, result);
811 }
812 
813 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
814 {
815     TCGv_i32 t0_32 = tcg_temp_new_i32();
816     TCGv_i32 t1_32 = tcg_temp_new_i32();
817     TCGv_i32 tmp = tcg_temp_new_i32();
818 
819     tcg_gen_movi_i32(tmp, 0);
820     tcg_gen_extrl_i64_i32(t0_32, t0);
821     tcg_gen_extrl_i64_i32(t1_32, t1);
822     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
823     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
824     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
825     tcg_gen_xor_i32(tmp, t0_32, t1_32);
826     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
827     tcg_gen_extu_i32_i64(dest, cpu_NF);
828 }
829 
830 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
831 {
832     if (sf) {
833         gen_add64_CC(dest, t0, t1);
834     } else {
835         gen_add32_CC(dest, t0, t1);
836     }
837 }
838 
839 /* dest = T0 - T1; compute C, N, V and Z flags */
840 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
841 {
842     /* 64 bit arithmetic */
843     TCGv_i64 result, flag, tmp;
844 
845     result = tcg_temp_new_i64();
846     flag = tcg_temp_new_i64();
847     tcg_gen_sub_i64(result, t0, t1);
848 
849     gen_set_NZ64(result);
850 
851     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
852     tcg_gen_extrl_i64_i32(cpu_CF, flag);
853 
854     tcg_gen_xor_i64(flag, result, t0);
855     tmp = tcg_temp_new_i64();
856     tcg_gen_xor_i64(tmp, t0, t1);
857     tcg_gen_and_i64(flag, flag, tmp);
858     tcg_gen_extrh_i64_i32(cpu_VF, flag);
859     tcg_gen_mov_i64(dest, result);
860 }
861 
862 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
863 {
864     /* 32 bit arithmetic */
865     TCGv_i32 t0_32 = tcg_temp_new_i32();
866     TCGv_i32 t1_32 = tcg_temp_new_i32();
867     TCGv_i32 tmp;
868 
869     tcg_gen_extrl_i64_i32(t0_32, t0);
870     tcg_gen_extrl_i64_i32(t1_32, t1);
871     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
872     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
873     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
874     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
875     tmp = tcg_temp_new_i32();
876     tcg_gen_xor_i32(tmp, t0_32, t1_32);
877     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
878     tcg_gen_extu_i32_i64(dest, cpu_NF);
879 }
880 
881 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
882 {
883     if (sf) {
884         gen_sub64_CC(dest, t0, t1);
885     } else {
886         gen_sub32_CC(dest, t0, t1);
887     }
888 }
889 
890 /* dest = T0 + T1 + CF; do not compute flags. */
891 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
892 {
893     TCGv_i64 flag = tcg_temp_new_i64();
894     tcg_gen_extu_i32_i64(flag, cpu_CF);
895     tcg_gen_add_i64(dest, t0, t1);
896     tcg_gen_add_i64(dest, dest, flag);
897 
898     if (!sf) {
899         tcg_gen_ext32u_i64(dest, dest);
900     }
901 }
902 
903 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
904 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
905 {
906     if (sf) {
907         TCGv_i64 result = tcg_temp_new_i64();
908         TCGv_i64 cf_64 = tcg_temp_new_i64();
909         TCGv_i64 vf_64 = tcg_temp_new_i64();
910         TCGv_i64 tmp = tcg_temp_new_i64();
911         TCGv_i64 zero = tcg_constant_i64(0);
912 
913         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
914         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
915         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
916         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
917         gen_set_NZ64(result);
918 
919         tcg_gen_xor_i64(vf_64, result, t0);
920         tcg_gen_xor_i64(tmp, t0, t1);
921         tcg_gen_andc_i64(vf_64, vf_64, tmp);
922         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
923 
924         tcg_gen_mov_i64(dest, result);
925     } else {
926         TCGv_i32 t0_32 = tcg_temp_new_i32();
927         TCGv_i32 t1_32 = tcg_temp_new_i32();
928         TCGv_i32 tmp = tcg_temp_new_i32();
929         TCGv_i32 zero = tcg_constant_i32(0);
930 
931         tcg_gen_extrl_i64_i32(t0_32, t0);
932         tcg_gen_extrl_i64_i32(t1_32, t1);
933         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
934         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
935 
936         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
937         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
938         tcg_gen_xor_i32(tmp, t0_32, t1_32);
939         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
940         tcg_gen_extu_i32_i64(dest, cpu_NF);
941     }
942 }
943 
944 /*
945  * Load/Store generators
946  */
947 
948 /*
949  * Store from GPR register to memory.
950  */
951 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
952                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
953                              bool iss_valid,
954                              unsigned int iss_srt,
955                              bool iss_sf, bool iss_ar)
956 {
957     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
958 
959     if (iss_valid) {
960         uint32_t syn;
961 
962         syn = syn_data_abort_with_iss(0,
963                                       (memop & MO_SIZE),
964                                       false,
965                                       iss_srt,
966                                       iss_sf,
967                                       iss_ar,
968                                       0, 0, 0, 0, 0, false);
969         disas_set_insn_syndrome(s, syn);
970     }
971 }
972 
973 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
974                       TCGv_i64 tcg_addr, MemOp memop,
975                       bool iss_valid,
976                       unsigned int iss_srt,
977                       bool iss_sf, bool iss_ar)
978 {
979     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
980                      iss_valid, iss_srt, iss_sf, iss_ar);
981 }
982 
983 /*
984  * Load from memory to GPR register
985  */
986 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
987                              MemOp memop, bool extend, int memidx,
988                              bool iss_valid, unsigned int iss_srt,
989                              bool iss_sf, bool iss_ar)
990 {
991     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
992 
993     if (extend && (memop & MO_SIGN)) {
994         g_assert((memop & MO_SIZE) <= MO_32);
995         tcg_gen_ext32u_i64(dest, dest);
996     }
997 
998     if (iss_valid) {
999         uint32_t syn;
1000 
1001         syn = syn_data_abort_with_iss(0,
1002                                       (memop & MO_SIZE),
1003                                       (memop & MO_SIGN) != 0,
1004                                       iss_srt,
1005                                       iss_sf,
1006                                       iss_ar,
1007                                       0, 0, 0, 0, 0, false);
1008         disas_set_insn_syndrome(s, syn);
1009     }
1010 }
1011 
1012 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1013                       MemOp memop, bool extend,
1014                       bool iss_valid, unsigned int iss_srt,
1015                       bool iss_sf, bool iss_ar)
1016 {
1017     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1018                      iss_valid, iss_srt, iss_sf, iss_ar);
1019 }
1020 
1021 /*
1022  * Store from FP register to memory
1023  */
1024 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1025 {
1026     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1027     TCGv_i64 tmplo = tcg_temp_new_i64();
1028 
1029     tcg_gen_ld_i64(tmplo, tcg_env, fp_reg_offset(s, srcidx, MO_64));
1030 
1031     if ((mop & MO_SIZE) < MO_128) {
1032         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1033     } else {
1034         TCGv_i64 tmphi = tcg_temp_new_i64();
1035         TCGv_i128 t16 = tcg_temp_new_i128();
1036 
1037         tcg_gen_ld_i64(tmphi, tcg_env, fp_reg_hi_offset(s, srcidx));
1038         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1039 
1040         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1041     }
1042 }
1043 
1044 /*
1045  * Load from memory to FP register
1046  */
1047 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1048 {
1049     /* This always zero-extends and writes to a full 128 bit wide vector */
1050     TCGv_i64 tmplo = tcg_temp_new_i64();
1051     TCGv_i64 tmphi = NULL;
1052 
1053     if ((mop & MO_SIZE) < MO_128) {
1054         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1055     } else {
1056         TCGv_i128 t16 = tcg_temp_new_i128();
1057 
1058         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1059 
1060         tmphi = tcg_temp_new_i64();
1061         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1062     }
1063 
1064     tcg_gen_st_i64(tmplo, tcg_env, fp_reg_offset(s, destidx, MO_64));
1065 
1066     if (tmphi) {
1067         tcg_gen_st_i64(tmphi, tcg_env, fp_reg_hi_offset(s, destidx));
1068     }
1069     clear_vec_high(s, tmphi != NULL, destidx);
1070 }
1071 
1072 /*
1073  * Vector load/store helpers.
1074  *
1075  * The principal difference between this and a FP load is that we don't
1076  * zero extend as we are filling a partial chunk of the vector register.
1077  * These functions don't support 128 bit loads/stores, which would be
1078  * normal load/store operations.
1079  *
1080  * The _i32 versions are useful when operating on 32 bit quantities
1081  * (eg for floating point single or using Neon helper functions).
1082  */
1083 
1084 /* Get value of an element within a vector register */
1085 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1086                              int element, MemOp memop)
1087 {
1088     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1089     switch ((unsigned)memop) {
1090     case MO_8:
1091         tcg_gen_ld8u_i64(tcg_dest, tcg_env, vect_off);
1092         break;
1093     case MO_16:
1094         tcg_gen_ld16u_i64(tcg_dest, tcg_env, vect_off);
1095         break;
1096     case MO_32:
1097         tcg_gen_ld32u_i64(tcg_dest, tcg_env, vect_off);
1098         break;
1099     case MO_8|MO_SIGN:
1100         tcg_gen_ld8s_i64(tcg_dest, tcg_env, vect_off);
1101         break;
1102     case MO_16|MO_SIGN:
1103         tcg_gen_ld16s_i64(tcg_dest, tcg_env, vect_off);
1104         break;
1105     case MO_32|MO_SIGN:
1106         tcg_gen_ld32s_i64(tcg_dest, tcg_env, vect_off);
1107         break;
1108     case MO_64:
1109     case MO_64|MO_SIGN:
1110         tcg_gen_ld_i64(tcg_dest, tcg_env, vect_off);
1111         break;
1112     default:
1113         g_assert_not_reached();
1114     }
1115 }
1116 
1117 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1118                                  int element, MemOp memop)
1119 {
1120     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1121     switch (memop) {
1122     case MO_8:
1123         tcg_gen_ld8u_i32(tcg_dest, tcg_env, vect_off);
1124         break;
1125     case MO_16:
1126         tcg_gen_ld16u_i32(tcg_dest, tcg_env, vect_off);
1127         break;
1128     case MO_8|MO_SIGN:
1129         tcg_gen_ld8s_i32(tcg_dest, tcg_env, vect_off);
1130         break;
1131     case MO_16|MO_SIGN:
1132         tcg_gen_ld16s_i32(tcg_dest, tcg_env, vect_off);
1133         break;
1134     case MO_32:
1135     case MO_32|MO_SIGN:
1136         tcg_gen_ld_i32(tcg_dest, tcg_env, vect_off);
1137         break;
1138     default:
1139         g_assert_not_reached();
1140     }
1141 }
1142 
1143 /* Set value of an element within a vector register */
1144 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1145                               int element, MemOp memop)
1146 {
1147     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1148     switch (memop) {
1149     case MO_8:
1150         tcg_gen_st8_i64(tcg_src, tcg_env, vect_off);
1151         break;
1152     case MO_16:
1153         tcg_gen_st16_i64(tcg_src, tcg_env, vect_off);
1154         break;
1155     case MO_32:
1156         tcg_gen_st32_i64(tcg_src, tcg_env, vect_off);
1157         break;
1158     case MO_64:
1159         tcg_gen_st_i64(tcg_src, tcg_env, vect_off);
1160         break;
1161     default:
1162         g_assert_not_reached();
1163     }
1164 }
1165 
1166 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1167                                   int destidx, int element, MemOp memop)
1168 {
1169     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1170     switch (memop) {
1171     case MO_8:
1172         tcg_gen_st8_i32(tcg_src, tcg_env, vect_off);
1173         break;
1174     case MO_16:
1175         tcg_gen_st16_i32(tcg_src, tcg_env, vect_off);
1176         break;
1177     case MO_32:
1178         tcg_gen_st_i32(tcg_src, tcg_env, vect_off);
1179         break;
1180     default:
1181         g_assert_not_reached();
1182     }
1183 }
1184 
1185 /* Store from vector register to memory */
1186 static void do_vec_st(DisasContext *s, int srcidx, int element,
1187                       TCGv_i64 tcg_addr, MemOp mop)
1188 {
1189     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1190 
1191     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1192     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1193 }
1194 
1195 /* Load from memory to vector register */
1196 static void do_vec_ld(DisasContext *s, int destidx, int element,
1197                       TCGv_i64 tcg_addr, MemOp mop)
1198 {
1199     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1200 
1201     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1202     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1203 }
1204 
1205 /* Check that FP/Neon access is enabled. If it is, return
1206  * true. If not, emit code to generate an appropriate exception,
1207  * and return false; the caller should not emit any code for
1208  * the instruction. Note that this check must happen after all
1209  * unallocated-encoding checks (otherwise the syndrome information
1210  * for the resulting exception will be incorrect).
1211  */
1212 static bool fp_access_check_only(DisasContext *s)
1213 {
1214     if (s->fp_excp_el) {
1215         assert(!s->fp_access_checked);
1216         s->fp_access_checked = true;
1217 
1218         gen_exception_insn_el(s, 0, EXCP_UDEF,
1219                               syn_fp_access_trap(1, 0xe, false, 0),
1220                               s->fp_excp_el);
1221         return false;
1222     }
1223     s->fp_access_checked = true;
1224     return true;
1225 }
1226 
1227 static bool fp_access_check(DisasContext *s)
1228 {
1229     if (!fp_access_check_only(s)) {
1230         return false;
1231     }
1232     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1233         gen_exception_insn(s, 0, EXCP_UDEF,
1234                            syn_smetrap(SME_ET_Streaming, false));
1235         return false;
1236     }
1237     return true;
1238 }
1239 
1240 /*
1241  * Check that SVE access is enabled.  If it is, return true.
1242  * If not, emit code to generate an appropriate exception and return false.
1243  * This function corresponds to CheckSVEEnabled().
1244  */
1245 bool sve_access_check(DisasContext *s)
1246 {
1247     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1248         assert(dc_isar_feature(aa64_sme, s));
1249         if (!sme_sm_enabled_check(s)) {
1250             goto fail_exit;
1251         }
1252     } else if (s->sve_excp_el) {
1253         gen_exception_insn_el(s, 0, EXCP_UDEF,
1254                               syn_sve_access_trap(), s->sve_excp_el);
1255         goto fail_exit;
1256     }
1257     s->sve_access_checked = true;
1258     return fp_access_check(s);
1259 
1260  fail_exit:
1261     /* Assert that we only raise one exception per instruction. */
1262     assert(!s->sve_access_checked);
1263     s->sve_access_checked = true;
1264     return false;
1265 }
1266 
1267 /*
1268  * Check that SME access is enabled, raise an exception if not.
1269  * Note that this function corresponds to CheckSMEAccess and is
1270  * only used directly for cpregs.
1271  */
1272 static bool sme_access_check(DisasContext *s)
1273 {
1274     if (s->sme_excp_el) {
1275         gen_exception_insn_el(s, 0, EXCP_UDEF,
1276                               syn_smetrap(SME_ET_AccessTrap, false),
1277                               s->sme_excp_el);
1278         return false;
1279     }
1280     return true;
1281 }
1282 
1283 /* This function corresponds to CheckSMEEnabled. */
1284 bool sme_enabled_check(DisasContext *s)
1285 {
1286     /*
1287      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1288      * to be zero when fp_excp_el has priority.  This is because we need
1289      * sme_excp_el by itself for cpregs access checks.
1290      */
1291     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1292         s->fp_access_checked = true;
1293         return sme_access_check(s);
1294     }
1295     return fp_access_check_only(s);
1296 }
1297 
1298 /* Common subroutine for CheckSMEAnd*Enabled. */
1299 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1300 {
1301     if (!sme_enabled_check(s)) {
1302         return false;
1303     }
1304     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1305         gen_exception_insn(s, 0, EXCP_UDEF,
1306                            syn_smetrap(SME_ET_NotStreaming, false));
1307         return false;
1308     }
1309     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1310         gen_exception_insn(s, 0, EXCP_UDEF,
1311                            syn_smetrap(SME_ET_InactiveZA, false));
1312         return false;
1313     }
1314     return true;
1315 }
1316 
1317 /*
1318  * This utility function is for doing register extension with an
1319  * optional shift. You will likely want to pass a temporary for the
1320  * destination register. See DecodeRegExtend() in the ARM ARM.
1321  */
1322 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1323                               int option, unsigned int shift)
1324 {
1325     int extsize = extract32(option, 0, 2);
1326     bool is_signed = extract32(option, 2, 1);
1327 
1328     tcg_gen_ext_i64(tcg_out, tcg_in, extsize | (is_signed ? MO_SIGN : 0));
1329     tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1330 }
1331 
1332 static inline void gen_check_sp_alignment(DisasContext *s)
1333 {
1334     /* The AArch64 architecture mandates that (if enabled via PSTATE
1335      * or SCTLR bits) there is a check that SP is 16-aligned on every
1336      * SP-relative load or store (with an exception generated if it is not).
1337      * In line with general QEMU practice regarding misaligned accesses,
1338      * we omit these checks for the sake of guest program performance.
1339      * This function is provided as a hook so we can more easily add these
1340      * checks in future (possibly as a "favour catching guest program bugs
1341      * over speed" user selectable option).
1342      */
1343 }
1344 
1345 /*
1346  * This provides a simple table based table lookup decoder. It is
1347  * intended to be used when the relevant bits for decode are too
1348  * awkwardly placed and switch/if based logic would be confusing and
1349  * deeply nested. Since it's a linear search through the table, tables
1350  * should be kept small.
1351  *
1352  * It returns the first handler where insn & mask == pattern, or
1353  * NULL if there is no match.
1354  * The table is terminated by an empty mask (i.e. 0)
1355  */
1356 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1357                                                uint32_t insn)
1358 {
1359     const AArch64DecodeTable *tptr = table;
1360 
1361     while (tptr->mask) {
1362         if ((insn & tptr->mask) == tptr->pattern) {
1363             return tptr->disas_fn;
1364         }
1365         tptr++;
1366     }
1367     return NULL;
1368 }
1369 
1370 /*
1371  * The instruction disassembly implemented here matches
1372  * the instruction encoding classifications in chapter C4
1373  * of the ARM Architecture Reference Manual (DDI0487B_a);
1374  * classification names and decode diagrams here should generally
1375  * match up with those in the manual.
1376  */
1377 
1378 static bool trans_B(DisasContext *s, arg_i *a)
1379 {
1380     reset_btype(s);
1381     gen_goto_tb(s, 0, a->imm);
1382     return true;
1383 }
1384 
1385 static bool trans_BL(DisasContext *s, arg_i *a)
1386 {
1387     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1388     reset_btype(s);
1389     gen_goto_tb(s, 0, a->imm);
1390     return true;
1391 }
1392 
1393 
1394 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1395 {
1396     DisasLabel match;
1397     TCGv_i64 tcg_cmp;
1398 
1399     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1400     reset_btype(s);
1401 
1402     match = gen_disas_label(s);
1403     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1404                         tcg_cmp, 0, match.label);
1405     gen_goto_tb(s, 0, 4);
1406     set_disas_label(s, match);
1407     gen_goto_tb(s, 1, a->imm);
1408     return true;
1409 }
1410 
1411 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1412 {
1413     DisasLabel match;
1414     TCGv_i64 tcg_cmp;
1415 
1416     tcg_cmp = tcg_temp_new_i64();
1417     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1418 
1419     reset_btype(s);
1420 
1421     match = gen_disas_label(s);
1422     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1423                         tcg_cmp, 0, match.label);
1424     gen_goto_tb(s, 0, 4);
1425     set_disas_label(s, match);
1426     gen_goto_tb(s, 1, a->imm);
1427     return true;
1428 }
1429 
1430 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1431 {
1432     /* BC.cond is only present with FEAT_HBC */
1433     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
1434         return false;
1435     }
1436     reset_btype(s);
1437     if (a->cond < 0x0e) {
1438         /* genuinely conditional branches */
1439         DisasLabel match = gen_disas_label(s);
1440         arm_gen_test_cc(a->cond, match.label);
1441         gen_goto_tb(s, 0, 4);
1442         set_disas_label(s, match);
1443         gen_goto_tb(s, 1, a->imm);
1444     } else {
1445         /* 0xe and 0xf are both "always" conditions */
1446         gen_goto_tb(s, 0, a->imm);
1447     }
1448     return true;
1449 }
1450 
1451 static void set_btype_for_br(DisasContext *s, int rn)
1452 {
1453     if (dc_isar_feature(aa64_bti, s)) {
1454         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1455         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1456     }
1457 }
1458 
1459 static void set_btype_for_blr(DisasContext *s)
1460 {
1461     if (dc_isar_feature(aa64_bti, s)) {
1462         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1463         set_btype(s, 2);
1464     }
1465 }
1466 
1467 static bool trans_BR(DisasContext *s, arg_r *a)
1468 {
1469     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1470     set_btype_for_br(s, a->rn);
1471     s->base.is_jmp = DISAS_JUMP;
1472     return true;
1473 }
1474 
1475 static bool trans_BLR(DisasContext *s, arg_r *a)
1476 {
1477     TCGv_i64 dst = cpu_reg(s, a->rn);
1478     TCGv_i64 lr = cpu_reg(s, 30);
1479     if (dst == lr) {
1480         TCGv_i64 tmp = tcg_temp_new_i64();
1481         tcg_gen_mov_i64(tmp, dst);
1482         dst = tmp;
1483     }
1484     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1485     gen_a64_set_pc(s, dst);
1486     set_btype_for_blr(s);
1487     s->base.is_jmp = DISAS_JUMP;
1488     return true;
1489 }
1490 
1491 static bool trans_RET(DisasContext *s, arg_r *a)
1492 {
1493     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1494     s->base.is_jmp = DISAS_JUMP;
1495     return true;
1496 }
1497 
1498 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1499                                    TCGv_i64 modifier, bool use_key_a)
1500 {
1501     TCGv_i64 truedst;
1502     /*
1503      * Return the branch target for a BRAA/RETA/etc, which is either
1504      * just the destination dst, or that value with the pauth check
1505      * done and the code removed from the high bits.
1506      */
1507     if (!s->pauth_active) {
1508         return dst;
1509     }
1510 
1511     truedst = tcg_temp_new_i64();
1512     if (use_key_a) {
1513         gen_helper_autia_combined(truedst, tcg_env, dst, modifier);
1514     } else {
1515         gen_helper_autib_combined(truedst, tcg_env, dst, modifier);
1516     }
1517     return truedst;
1518 }
1519 
1520 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1521 {
1522     TCGv_i64 dst;
1523 
1524     if (!dc_isar_feature(aa64_pauth, s)) {
1525         return false;
1526     }
1527 
1528     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1529     gen_a64_set_pc(s, dst);
1530     set_btype_for_br(s, a->rn);
1531     s->base.is_jmp = DISAS_JUMP;
1532     return true;
1533 }
1534 
1535 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1536 {
1537     TCGv_i64 dst, lr;
1538 
1539     if (!dc_isar_feature(aa64_pauth, s)) {
1540         return false;
1541     }
1542 
1543     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1544     lr = cpu_reg(s, 30);
1545     if (dst == lr) {
1546         TCGv_i64 tmp = tcg_temp_new_i64();
1547         tcg_gen_mov_i64(tmp, dst);
1548         dst = tmp;
1549     }
1550     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1551     gen_a64_set_pc(s, dst);
1552     set_btype_for_blr(s);
1553     s->base.is_jmp = DISAS_JUMP;
1554     return true;
1555 }
1556 
1557 static bool trans_RETA(DisasContext *s, arg_reta *a)
1558 {
1559     TCGv_i64 dst;
1560 
1561     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1562     gen_a64_set_pc(s, dst);
1563     s->base.is_jmp = DISAS_JUMP;
1564     return true;
1565 }
1566 
1567 static bool trans_BRA(DisasContext *s, arg_bra *a)
1568 {
1569     TCGv_i64 dst;
1570 
1571     if (!dc_isar_feature(aa64_pauth, s)) {
1572         return false;
1573     }
1574     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1575     gen_a64_set_pc(s, dst);
1576     set_btype_for_br(s, a->rn);
1577     s->base.is_jmp = DISAS_JUMP;
1578     return true;
1579 }
1580 
1581 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1582 {
1583     TCGv_i64 dst, lr;
1584 
1585     if (!dc_isar_feature(aa64_pauth, s)) {
1586         return false;
1587     }
1588     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1589     lr = cpu_reg(s, 30);
1590     if (dst == lr) {
1591         TCGv_i64 tmp = tcg_temp_new_i64();
1592         tcg_gen_mov_i64(tmp, dst);
1593         dst = tmp;
1594     }
1595     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1596     gen_a64_set_pc(s, dst);
1597     set_btype_for_blr(s);
1598     s->base.is_jmp = DISAS_JUMP;
1599     return true;
1600 }
1601 
1602 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1603 {
1604     TCGv_i64 dst;
1605 
1606     if (s->current_el == 0) {
1607         return false;
1608     }
1609     if (s->trap_eret) {
1610         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(0), 2);
1611         return true;
1612     }
1613     dst = tcg_temp_new_i64();
1614     tcg_gen_ld_i64(dst, tcg_env,
1615                    offsetof(CPUARMState, elr_el[s->current_el]));
1616 
1617     translator_io_start(&s->base);
1618 
1619     gen_helper_exception_return(tcg_env, dst);
1620     /* Must exit loop to check un-masked IRQs */
1621     s->base.is_jmp = DISAS_EXIT;
1622     return true;
1623 }
1624 
1625 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1626 {
1627     TCGv_i64 dst;
1628 
1629     if (!dc_isar_feature(aa64_pauth, s)) {
1630         return false;
1631     }
1632     if (s->current_el == 0) {
1633         return false;
1634     }
1635     /* The FGT trap takes precedence over an auth trap. */
1636     if (s->trap_eret) {
1637         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(a->m ? 3 : 2), 2);
1638         return true;
1639     }
1640     dst = tcg_temp_new_i64();
1641     tcg_gen_ld_i64(dst, tcg_env,
1642                    offsetof(CPUARMState, elr_el[s->current_el]));
1643 
1644     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1645 
1646     translator_io_start(&s->base);
1647 
1648     gen_helper_exception_return(tcg_env, dst);
1649     /* Must exit loop to check un-masked IRQs */
1650     s->base.is_jmp = DISAS_EXIT;
1651     return true;
1652 }
1653 
1654 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1655 {
1656     return true;
1657 }
1658 
1659 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1660 {
1661     /*
1662      * When running in MTTCG we don't generate jumps to the yield and
1663      * WFE helpers as it won't affect the scheduling of other vCPUs.
1664      * If we wanted to more completely model WFE/SEV so we don't busy
1665      * spin unnecessarily we would need to do something more involved.
1666      */
1667     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1668         s->base.is_jmp = DISAS_YIELD;
1669     }
1670     return true;
1671 }
1672 
1673 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1674 {
1675     s->base.is_jmp = DISAS_WFI;
1676     return true;
1677 }
1678 
1679 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1680 {
1681     /*
1682      * When running in MTTCG we don't generate jumps to the yield and
1683      * WFE helpers as it won't affect the scheduling of other vCPUs.
1684      * If we wanted to more completely model WFE/SEV so we don't busy
1685      * spin unnecessarily we would need to do something more involved.
1686      */
1687     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1688         s->base.is_jmp = DISAS_WFE;
1689     }
1690     return true;
1691 }
1692 
1693 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1694 {
1695     if (s->pauth_active) {
1696         gen_helper_xpaci(cpu_X[30], tcg_env, cpu_X[30]);
1697     }
1698     return true;
1699 }
1700 
1701 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1702 {
1703     if (s->pauth_active) {
1704         gen_helper_pacia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1705     }
1706     return true;
1707 }
1708 
1709 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1710 {
1711     if (s->pauth_active) {
1712         gen_helper_pacib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1713     }
1714     return true;
1715 }
1716 
1717 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1718 {
1719     if (s->pauth_active) {
1720         gen_helper_autia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1721     }
1722     return true;
1723 }
1724 
1725 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1726 {
1727     if (s->pauth_active) {
1728         gen_helper_autib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1729     }
1730     return true;
1731 }
1732 
1733 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1734 {
1735     /* Without RAS, we must implement this as NOP. */
1736     if (dc_isar_feature(aa64_ras, s)) {
1737         /*
1738          * QEMU does not have a source of physical SErrors,
1739          * so we are only concerned with virtual SErrors.
1740          * The pseudocode in the ARM for this case is
1741          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1742          *      AArch64.vESBOperation();
1743          * Most of the condition can be evaluated at translation time.
1744          * Test for EL2 present, and defer test for SEL2 to runtime.
1745          */
1746         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1747             gen_helper_vesb(tcg_env);
1748         }
1749     }
1750     return true;
1751 }
1752 
1753 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1754 {
1755     if (s->pauth_active) {
1756         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1757     }
1758     return true;
1759 }
1760 
1761 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1762 {
1763     if (s->pauth_active) {
1764         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1765     }
1766     return true;
1767 }
1768 
1769 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1770 {
1771     if (s->pauth_active) {
1772         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1773     }
1774     return true;
1775 }
1776 
1777 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1778 {
1779     if (s->pauth_active) {
1780         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1781     }
1782     return true;
1783 }
1784 
1785 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1786 {
1787     if (s->pauth_active) {
1788         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1789     }
1790     return true;
1791 }
1792 
1793 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1794 {
1795     if (s->pauth_active) {
1796         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1797     }
1798     return true;
1799 }
1800 
1801 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1802 {
1803     if (s->pauth_active) {
1804         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1805     }
1806     return true;
1807 }
1808 
1809 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1810 {
1811     if (s->pauth_active) {
1812         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1813     }
1814     return true;
1815 }
1816 
1817 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1818 {
1819     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1820     return true;
1821 }
1822 
1823 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1824 {
1825     /* We handle DSB and DMB the same way */
1826     TCGBar bar;
1827 
1828     switch (a->types) {
1829     case 1: /* MBReqTypes_Reads */
1830         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1831         break;
1832     case 2: /* MBReqTypes_Writes */
1833         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1834         break;
1835     default: /* MBReqTypes_All */
1836         bar = TCG_BAR_SC | TCG_MO_ALL;
1837         break;
1838     }
1839     tcg_gen_mb(bar);
1840     return true;
1841 }
1842 
1843 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1844 {
1845     /*
1846      * We need to break the TB after this insn to execute
1847      * self-modifying code correctly and also to take
1848      * any pending interrupts immediately.
1849      */
1850     reset_btype(s);
1851     gen_goto_tb(s, 0, 4);
1852     return true;
1853 }
1854 
1855 static bool trans_SB(DisasContext *s, arg_SB *a)
1856 {
1857     if (!dc_isar_feature(aa64_sb, s)) {
1858         return false;
1859     }
1860     /*
1861      * TODO: There is no speculation barrier opcode for TCG;
1862      * MB and end the TB instead.
1863      */
1864     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1865     gen_goto_tb(s, 0, 4);
1866     return true;
1867 }
1868 
1869 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1870 {
1871     if (!dc_isar_feature(aa64_condm_4, s)) {
1872         return false;
1873     }
1874     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1875     return true;
1876 }
1877 
1878 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1879 {
1880     TCGv_i32 z;
1881 
1882     if (!dc_isar_feature(aa64_condm_5, s)) {
1883         return false;
1884     }
1885 
1886     z = tcg_temp_new_i32();
1887 
1888     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1889 
1890     /*
1891      * (!C & !Z) << 31
1892      * (!(C | Z)) << 31
1893      * ~((C | Z) << 31)
1894      * ~-(C | Z)
1895      * (C | Z) - 1
1896      */
1897     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1898     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1899 
1900     /* !(Z & C) */
1901     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1902     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1903 
1904     /* (!C & Z) << 31 -> -(Z & ~C) */
1905     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1906     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1907 
1908     /* C | Z */
1909     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1910 
1911     return true;
1912 }
1913 
1914 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1915 {
1916     if (!dc_isar_feature(aa64_condm_5, s)) {
1917         return false;
1918     }
1919 
1920     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1921     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1922 
1923     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1924     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1925 
1926     tcg_gen_movi_i32(cpu_NF, 0);
1927     tcg_gen_movi_i32(cpu_VF, 0);
1928 
1929     return true;
1930 }
1931 
1932 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1933 {
1934     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1935         return false;
1936     }
1937     if (a->imm & 1) {
1938         set_pstate_bits(PSTATE_UAO);
1939     } else {
1940         clear_pstate_bits(PSTATE_UAO);
1941     }
1942     gen_rebuild_hflags(s);
1943     s->base.is_jmp = DISAS_TOO_MANY;
1944     return true;
1945 }
1946 
1947 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1948 {
1949     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1950         return false;
1951     }
1952     if (a->imm & 1) {
1953         set_pstate_bits(PSTATE_PAN);
1954     } else {
1955         clear_pstate_bits(PSTATE_PAN);
1956     }
1957     gen_rebuild_hflags(s);
1958     s->base.is_jmp = DISAS_TOO_MANY;
1959     return true;
1960 }
1961 
1962 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1963 {
1964     if (s->current_el == 0) {
1965         return false;
1966     }
1967     gen_helper_msr_i_spsel(tcg_env, tcg_constant_i32(a->imm & PSTATE_SP));
1968     s->base.is_jmp = DISAS_TOO_MANY;
1969     return true;
1970 }
1971 
1972 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
1973 {
1974     if (!dc_isar_feature(aa64_ssbs, s)) {
1975         return false;
1976     }
1977     if (a->imm & 1) {
1978         set_pstate_bits(PSTATE_SSBS);
1979     } else {
1980         clear_pstate_bits(PSTATE_SSBS);
1981     }
1982     /* Don't need to rebuild hflags since SSBS is a nop */
1983     s->base.is_jmp = DISAS_TOO_MANY;
1984     return true;
1985 }
1986 
1987 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
1988 {
1989     if (!dc_isar_feature(aa64_dit, s)) {
1990         return false;
1991     }
1992     if (a->imm & 1) {
1993         set_pstate_bits(PSTATE_DIT);
1994     } else {
1995         clear_pstate_bits(PSTATE_DIT);
1996     }
1997     /* There's no need to rebuild hflags because DIT is a nop */
1998     s->base.is_jmp = DISAS_TOO_MANY;
1999     return true;
2000 }
2001 
2002 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2003 {
2004     if (dc_isar_feature(aa64_mte, s)) {
2005         /* Full MTE is enabled -- set the TCO bit as directed. */
2006         if (a->imm & 1) {
2007             set_pstate_bits(PSTATE_TCO);
2008         } else {
2009             clear_pstate_bits(PSTATE_TCO);
2010         }
2011         gen_rebuild_hflags(s);
2012         /* Many factors, including TCO, go into MTE_ACTIVE. */
2013         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2014         return true;
2015     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2016         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2017         return true;
2018     } else {
2019         /* Insn not present */
2020         return false;
2021     }
2022 }
2023 
2024 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2025 {
2026     gen_helper_msr_i_daifset(tcg_env, tcg_constant_i32(a->imm));
2027     s->base.is_jmp = DISAS_TOO_MANY;
2028     return true;
2029 }
2030 
2031 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2032 {
2033     gen_helper_msr_i_daifclear(tcg_env, tcg_constant_i32(a->imm));
2034     /* Exit the cpu loop to re-evaluate pending IRQs. */
2035     s->base.is_jmp = DISAS_UPDATE_EXIT;
2036     return true;
2037 }
2038 
2039 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2040 {
2041     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2042         return false;
2043     }
2044     if (sme_access_check(s)) {
2045         int old = s->pstate_sm | (s->pstate_za << 1);
2046         int new = a->imm * 3;
2047 
2048         if ((old ^ new) & a->mask) {
2049             /* At least one bit changes. */
2050             gen_helper_set_svcr(tcg_env, tcg_constant_i32(new),
2051                                 tcg_constant_i32(a->mask));
2052             s->base.is_jmp = DISAS_TOO_MANY;
2053         }
2054     }
2055     return true;
2056 }
2057 
2058 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2059 {
2060     TCGv_i32 tmp = tcg_temp_new_i32();
2061     TCGv_i32 nzcv = tcg_temp_new_i32();
2062 
2063     /* build bit 31, N */
2064     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2065     /* build bit 30, Z */
2066     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2067     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2068     /* build bit 29, C */
2069     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2070     /* build bit 28, V */
2071     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2072     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2073     /* generate result */
2074     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2075 }
2076 
2077 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2078 {
2079     TCGv_i32 nzcv = tcg_temp_new_i32();
2080 
2081     /* take NZCV from R[t] */
2082     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2083 
2084     /* bit 31, N */
2085     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2086     /* bit 30, Z */
2087     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2088     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2089     /* bit 29, C */
2090     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2091     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2092     /* bit 28, V */
2093     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2094     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2095 }
2096 
2097 static void gen_sysreg_undef(DisasContext *s, bool isread,
2098                              uint8_t op0, uint8_t op1, uint8_t op2,
2099                              uint8_t crn, uint8_t crm, uint8_t rt)
2100 {
2101     /*
2102      * Generate code to emit an UNDEF with correct syndrome
2103      * information for a failed system register access.
2104      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2105      * but if FEAT_IDST is implemented then read accesses to registers
2106      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2107      * syndrome.
2108      */
2109     uint32_t syndrome;
2110 
2111     if (isread && dc_isar_feature(aa64_ids, s) &&
2112         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2113         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2114     } else {
2115         syndrome = syn_uncategorized();
2116     }
2117     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2118 }
2119 
2120 /* MRS - move from system register
2121  * MSR (register) - move to system register
2122  * SYS
2123  * SYSL
2124  * These are all essentially the same insn in 'read' and 'write'
2125  * versions, with varying op0 fields.
2126  */
2127 static void handle_sys(DisasContext *s, bool isread,
2128                        unsigned int op0, unsigned int op1, unsigned int op2,
2129                        unsigned int crn, unsigned int crm, unsigned int rt)
2130 {
2131     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2132                                       crn, crm, op0, op1, op2);
2133     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2134     bool need_exit_tb = false;
2135     bool nv_trap_to_el2 = false;
2136     bool skip_fp_access_checks = false;
2137     TCGv_ptr tcg_ri = NULL;
2138     TCGv_i64 tcg_rt;
2139     uint32_t syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2140 
2141     if (crn == 11 || crn == 15) {
2142         /*
2143          * Check for TIDCP trap, which must take precedence over
2144          * the UNDEF for "no such register" etc.
2145          */
2146         switch (s->current_el) {
2147         case 0:
2148             if (dc_isar_feature(aa64_tidcp1, s)) {
2149                 gen_helper_tidcp_el0(tcg_env, tcg_constant_i32(syndrome));
2150             }
2151             break;
2152         case 1:
2153             gen_helper_tidcp_el1(tcg_env, tcg_constant_i32(syndrome));
2154             break;
2155         }
2156     }
2157 
2158     if (!ri) {
2159         /* Unknown register; this might be a guest error or a QEMU
2160          * unimplemented feature.
2161          */
2162         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2163                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2164                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2165         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2166         return;
2167     }
2168 
2169     /* Check access permissions */
2170     if (!cp_access_ok(s->current_el, ri, isread)) {
2171         /*
2172          * FEAT_NV/NV2 handling does not do the usual FP access checks
2173          * for registers only accessible at EL2 (though it *does* do them
2174          * for registers accessible at EL1).
2175          */
2176         skip_fp_access_checks = true;
2177         if (s->nv && arm_cpreg_traps_in_nv(ri)) {
2178             /*
2179              * This register / instruction exists and is an EL2 register, so
2180              * we must trap to EL2 if accessed in nested virtualization EL1
2181              * instead of UNDEFing. We'll do that after the usual access checks.
2182              * (This makes a difference only for a couple of registers like
2183              * VSTTBR_EL2 where the "UNDEF if NonSecure" should take priority
2184              * over the trap-to-EL2. Most trapped-by-FEAT_NV registers have
2185              * an accessfn which does nothing when called from EL1, because
2186              * the trap-to-EL3 controls which would apply to that register
2187              * at EL2 don't take priority over the FEAT_NV trap-to-EL2.)
2188              */
2189             nv_trap_to_el2 = true;
2190         } else {
2191             gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2192             return;
2193         }
2194     }
2195 
2196     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2197         /* Emit code to perform further access permissions checks at
2198          * runtime; this may result in an exception.
2199          */
2200         gen_a64_update_pc(s, 0);
2201         tcg_ri = tcg_temp_new_ptr();
2202         gen_helper_access_check_cp_reg(tcg_ri, tcg_env,
2203                                        tcg_constant_i32(key),
2204                                        tcg_constant_i32(syndrome),
2205                                        tcg_constant_i32(isread));
2206     } else if (ri->type & ARM_CP_RAISES_EXC) {
2207         /*
2208          * The readfn or writefn might raise an exception;
2209          * synchronize the CPU state in case it does.
2210          */
2211         gen_a64_update_pc(s, 0);
2212     }
2213 
2214     if (!skip_fp_access_checks) {
2215         if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2216             return;
2217         } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2218             return;
2219         } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2220             return;
2221         }
2222     }
2223 
2224     if (nv_trap_to_el2) {
2225         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2226         return;
2227     }
2228 
2229     /* Handle special cases first */
2230     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2231     case 0:
2232         break;
2233     case ARM_CP_NOP:
2234         return;
2235     case ARM_CP_NZCV:
2236         tcg_rt = cpu_reg(s, rt);
2237         if (isread) {
2238             gen_get_nzcv(tcg_rt);
2239         } else {
2240             gen_set_nzcv(tcg_rt);
2241         }
2242         return;
2243     case ARM_CP_CURRENTEL:
2244         /* Reads as current EL value from pstate, which is
2245          * guaranteed to be constant by the tb flags.
2246          */
2247         tcg_rt = cpu_reg(s, rt);
2248         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2249         return;
2250     case ARM_CP_DC_ZVA:
2251         /* Writes clear the aligned block of memory which rt points into. */
2252         if (s->mte_active[0]) {
2253             int desc = 0;
2254 
2255             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2256             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2257             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2258 
2259             tcg_rt = tcg_temp_new_i64();
2260             gen_helper_mte_check_zva(tcg_rt, tcg_env,
2261                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2262         } else {
2263             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2264         }
2265         gen_helper_dc_zva(tcg_env, tcg_rt);
2266         return;
2267     case ARM_CP_DC_GVA:
2268         {
2269             TCGv_i64 clean_addr, tag;
2270 
2271             /*
2272              * DC_GVA, like DC_ZVA, requires that we supply the original
2273              * pointer for an invalid page.  Probe that address first.
2274              */
2275             tcg_rt = cpu_reg(s, rt);
2276             clean_addr = clean_data_tbi(s, tcg_rt);
2277             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2278 
2279             if (s->ata[0]) {
2280                 /* Extract the tag from the register to match STZGM.  */
2281                 tag = tcg_temp_new_i64();
2282                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2283                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2284             }
2285         }
2286         return;
2287     case ARM_CP_DC_GZVA:
2288         {
2289             TCGv_i64 clean_addr, tag;
2290 
2291             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2292             tcg_rt = cpu_reg(s, rt);
2293             clean_addr = clean_data_tbi(s, tcg_rt);
2294             gen_helper_dc_zva(tcg_env, clean_addr);
2295 
2296             if (s->ata[0]) {
2297                 /* Extract the tag from the register to match STZGM.  */
2298                 tag = tcg_temp_new_i64();
2299                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2300                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2301             }
2302         }
2303         return;
2304     default:
2305         g_assert_not_reached();
2306     }
2307 
2308     if (ri->type & ARM_CP_IO) {
2309         /* I/O operations must end the TB here (whether read or write) */
2310         need_exit_tb = translator_io_start(&s->base);
2311     }
2312 
2313     tcg_rt = cpu_reg(s, rt);
2314 
2315     if (isread) {
2316         if (ri->type & ARM_CP_CONST) {
2317             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2318         } else if (ri->readfn) {
2319             if (!tcg_ri) {
2320                 tcg_ri = gen_lookup_cp_reg(key);
2321             }
2322             gen_helper_get_cp_reg64(tcg_rt, tcg_env, tcg_ri);
2323         } else {
2324             tcg_gen_ld_i64(tcg_rt, tcg_env, ri->fieldoffset);
2325         }
2326     } else {
2327         if (ri->type & ARM_CP_CONST) {
2328             /* If not forbidden by access permissions, treat as WI */
2329             return;
2330         } else if (ri->writefn) {
2331             if (!tcg_ri) {
2332                 tcg_ri = gen_lookup_cp_reg(key);
2333             }
2334             gen_helper_set_cp_reg64(tcg_env, tcg_ri, tcg_rt);
2335         } else {
2336             tcg_gen_st_i64(tcg_rt, tcg_env, ri->fieldoffset);
2337         }
2338     }
2339 
2340     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2341         /*
2342          * A write to any coprocessor register that ends a TB
2343          * must rebuild the hflags for the next TB.
2344          */
2345         gen_rebuild_hflags(s);
2346         /*
2347          * We default to ending the TB on a coprocessor register write,
2348          * but allow this to be suppressed by the register definition
2349          * (usually only necessary to work around guest bugs).
2350          */
2351         need_exit_tb = true;
2352     }
2353     if (need_exit_tb) {
2354         s->base.is_jmp = DISAS_UPDATE_EXIT;
2355     }
2356 }
2357 
2358 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2359 {
2360     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2361     return true;
2362 }
2363 
2364 static bool trans_SVC(DisasContext *s, arg_i *a)
2365 {
2366     /*
2367      * For SVC, HVC and SMC we advance the single-step state
2368      * machine before taking the exception. This is architecturally
2369      * mandated, to ensure that single-stepping a system call
2370      * instruction works properly.
2371      */
2372     uint32_t syndrome = syn_aa64_svc(a->imm);
2373     if (s->fgt_svc) {
2374         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2375         return true;
2376     }
2377     gen_ss_advance(s);
2378     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2379     return true;
2380 }
2381 
2382 static bool trans_HVC(DisasContext *s, arg_i *a)
2383 {
2384     int target_el = s->current_el == 3 ? 3 : 2;
2385 
2386     if (s->current_el == 0) {
2387         unallocated_encoding(s);
2388         return true;
2389     }
2390     /*
2391      * The pre HVC helper handles cases when HVC gets trapped
2392      * as an undefined insn by runtime configuration.
2393      */
2394     gen_a64_update_pc(s, 0);
2395     gen_helper_pre_hvc(tcg_env);
2396     /* Architecture requires ss advance before we do the actual work */
2397     gen_ss_advance(s);
2398     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), target_el);
2399     return true;
2400 }
2401 
2402 static bool trans_SMC(DisasContext *s, arg_i *a)
2403 {
2404     if (s->current_el == 0) {
2405         unallocated_encoding(s);
2406         return true;
2407     }
2408     gen_a64_update_pc(s, 0);
2409     gen_helper_pre_smc(tcg_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2410     /* Architecture requires ss advance before we do the actual work */
2411     gen_ss_advance(s);
2412     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2413     return true;
2414 }
2415 
2416 static bool trans_BRK(DisasContext *s, arg_i *a)
2417 {
2418     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2419     return true;
2420 }
2421 
2422 static bool trans_HLT(DisasContext *s, arg_i *a)
2423 {
2424     /*
2425      * HLT. This has two purposes.
2426      * Architecturally, it is an external halting debug instruction.
2427      * Since QEMU doesn't implement external debug, we treat this as
2428      * it is required for halting debug disabled: it will UNDEF.
2429      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2430      */
2431     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2432         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2433     } else {
2434         unallocated_encoding(s);
2435     }
2436     return true;
2437 }
2438 
2439 /*
2440  * Load/Store exclusive instructions are implemented by remembering
2441  * the value/address loaded, and seeing if these are the same
2442  * when the store is performed. This is not actually the architecturally
2443  * mandated semantics, but it works for typical guest code sequences
2444  * and avoids having to monitor regular stores.
2445  *
2446  * The store exclusive uses the atomic cmpxchg primitives to avoid
2447  * races in multi-threaded linux-user and when MTTCG softmmu is
2448  * enabled.
2449  */
2450 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2451                                int size, bool is_pair)
2452 {
2453     int idx = get_mem_index(s);
2454     TCGv_i64 dirty_addr, clean_addr;
2455     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2456 
2457     s->is_ldex = true;
2458     dirty_addr = cpu_reg_sp(s, rn);
2459     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2460 
2461     g_assert(size <= 3);
2462     if (is_pair) {
2463         g_assert(size >= 2);
2464         if (size == 2) {
2465             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2466             if (s->be_data == MO_LE) {
2467                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2468                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2469             } else {
2470                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2471                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2472             }
2473         } else {
2474             TCGv_i128 t16 = tcg_temp_new_i128();
2475 
2476             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2477 
2478             if (s->be_data == MO_LE) {
2479                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2480                                       cpu_exclusive_high, t16);
2481             } else {
2482                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2483                                       cpu_exclusive_val, t16);
2484             }
2485             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2486             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2487         }
2488     } else {
2489         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2490         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2491     }
2492     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2493 }
2494 
2495 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2496                                 int rn, int size, int is_pair)
2497 {
2498     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2499      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2500      *     [addr] = {Rt};
2501      *     if (is_pair) {
2502      *         [addr + datasize] = {Rt2};
2503      *     }
2504      *     {Rd} = 0;
2505      * } else {
2506      *     {Rd} = 1;
2507      * }
2508      * env->exclusive_addr = -1;
2509      */
2510     TCGLabel *fail_label = gen_new_label();
2511     TCGLabel *done_label = gen_new_label();
2512     TCGv_i64 tmp, clean_addr;
2513     MemOp memop;
2514 
2515     /*
2516      * FIXME: We are out of spec here.  We have recorded only the address
2517      * from load_exclusive, not the entire range, and we assume that the
2518      * size of the access on both sides match.  The architecture allows the
2519      * store to be smaller than the load, so long as the stored bytes are
2520      * within the range recorded by the load.
2521      */
2522 
2523     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2524     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2525     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2526 
2527     /*
2528      * The write, and any associated faults, only happen if the virtual
2529      * and physical addresses pass the exclusive monitor check.  These
2530      * faults are exceedingly unlikely, because normally the guest uses
2531      * the exact same address register for the load_exclusive, and we
2532      * would have recognized these faults there.
2533      *
2534      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2535      * unaligned 4-byte write within the range of an aligned 8-byte load.
2536      * With LSE2, the store would need to cross a 16-byte boundary when the
2537      * load did not, which would mean the store is outside the range
2538      * recorded for the monitor, which would have failed a corrected monitor
2539      * check above.  For now, we assume no size change and retain the
2540      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2541      *
2542      * It is possible to trigger an MTE fault, by performing the load with
2543      * a virtual address with a valid tag and performing the store with the
2544      * same virtual address and a different invalid tag.
2545      */
2546     memop = size + is_pair;
2547     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2548         memop |= MO_ALIGN;
2549     }
2550     memop = finalize_memop(s, memop);
2551     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2552 
2553     tmp = tcg_temp_new_i64();
2554     if (is_pair) {
2555         if (size == 2) {
2556             if (s->be_data == MO_LE) {
2557                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2558             } else {
2559                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2560             }
2561             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2562                                        cpu_exclusive_val, tmp,
2563                                        get_mem_index(s), memop);
2564             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2565         } else {
2566             TCGv_i128 t16 = tcg_temp_new_i128();
2567             TCGv_i128 c16 = tcg_temp_new_i128();
2568             TCGv_i64 a, b;
2569 
2570             if (s->be_data == MO_LE) {
2571                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2572                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2573                                         cpu_exclusive_high);
2574             } else {
2575                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2576                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2577                                         cpu_exclusive_val);
2578             }
2579 
2580             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2581                                         get_mem_index(s), memop);
2582 
2583             a = tcg_temp_new_i64();
2584             b = tcg_temp_new_i64();
2585             if (s->be_data == MO_LE) {
2586                 tcg_gen_extr_i128_i64(a, b, t16);
2587             } else {
2588                 tcg_gen_extr_i128_i64(b, a, t16);
2589             }
2590 
2591             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2592             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2593             tcg_gen_or_i64(tmp, a, b);
2594 
2595             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2596         }
2597     } else {
2598         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2599                                    cpu_reg(s, rt), get_mem_index(s), memop);
2600         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2601     }
2602     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2603     tcg_gen_br(done_label);
2604 
2605     gen_set_label(fail_label);
2606     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2607     gen_set_label(done_label);
2608     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2609 }
2610 
2611 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2612                                  int rn, int size)
2613 {
2614     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2615     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2616     int memidx = get_mem_index(s);
2617     TCGv_i64 clean_addr;
2618     MemOp memop;
2619 
2620     if (rn == 31) {
2621         gen_check_sp_alignment(s);
2622     }
2623     memop = check_atomic_align(s, rn, size);
2624     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2625     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2626                                memidx, memop);
2627 }
2628 
2629 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2630                                       int rn, int size)
2631 {
2632     TCGv_i64 s1 = cpu_reg(s, rs);
2633     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2634     TCGv_i64 t1 = cpu_reg(s, rt);
2635     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2636     TCGv_i64 clean_addr;
2637     int memidx = get_mem_index(s);
2638     MemOp memop;
2639 
2640     if (rn == 31) {
2641         gen_check_sp_alignment(s);
2642     }
2643 
2644     /* This is a single atomic access, despite the "pair". */
2645     memop = check_atomic_align(s, rn, size + 1);
2646     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2647 
2648     if (size == 2) {
2649         TCGv_i64 cmp = tcg_temp_new_i64();
2650         TCGv_i64 val = tcg_temp_new_i64();
2651 
2652         if (s->be_data == MO_LE) {
2653             tcg_gen_concat32_i64(val, t1, t2);
2654             tcg_gen_concat32_i64(cmp, s1, s2);
2655         } else {
2656             tcg_gen_concat32_i64(val, t2, t1);
2657             tcg_gen_concat32_i64(cmp, s2, s1);
2658         }
2659 
2660         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2661 
2662         if (s->be_data == MO_LE) {
2663             tcg_gen_extr32_i64(s1, s2, cmp);
2664         } else {
2665             tcg_gen_extr32_i64(s2, s1, cmp);
2666         }
2667     } else {
2668         TCGv_i128 cmp = tcg_temp_new_i128();
2669         TCGv_i128 val = tcg_temp_new_i128();
2670 
2671         if (s->be_data == MO_LE) {
2672             tcg_gen_concat_i64_i128(val, t1, t2);
2673             tcg_gen_concat_i64_i128(cmp, s1, s2);
2674         } else {
2675             tcg_gen_concat_i64_i128(val, t2, t1);
2676             tcg_gen_concat_i64_i128(cmp, s2, s1);
2677         }
2678 
2679         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2680 
2681         if (s->be_data == MO_LE) {
2682             tcg_gen_extr_i128_i64(s1, s2, cmp);
2683         } else {
2684             tcg_gen_extr_i128_i64(s2, s1, cmp);
2685         }
2686     }
2687 }
2688 
2689 /*
2690  * Compute the ISS.SF bit for syndrome information if an exception
2691  * is taken on a load or store. This indicates whether the instruction
2692  * is accessing a 32-bit or 64-bit register. This logic is derived
2693  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2694  */
2695 static bool ldst_iss_sf(int size, bool sign, bool ext)
2696 {
2697 
2698     if (sign) {
2699         /*
2700          * Signed loads are 64 bit results if we are not going to
2701          * do a zero-extend from 32 to 64 after the load.
2702          * (For a store, sign and ext are always false.)
2703          */
2704         return !ext;
2705     } else {
2706         /* Unsigned loads/stores work at the specified size */
2707         return size == MO_64;
2708     }
2709 }
2710 
2711 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2712 {
2713     if (a->rn == 31) {
2714         gen_check_sp_alignment(s);
2715     }
2716     if (a->lasr) {
2717         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2718     }
2719     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2720     return true;
2721 }
2722 
2723 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2724 {
2725     if (a->rn == 31) {
2726         gen_check_sp_alignment(s);
2727     }
2728     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2729     if (a->lasr) {
2730         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2731     }
2732     return true;
2733 }
2734 
2735 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2736 {
2737     TCGv_i64 clean_addr;
2738     MemOp memop;
2739     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2740 
2741     /*
2742      * StoreLORelease is the same as Store-Release for QEMU, but
2743      * needs the feature-test.
2744      */
2745     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2746         return false;
2747     }
2748     /* Generate ISS for non-exclusive accesses including LASR.  */
2749     if (a->rn == 31) {
2750         gen_check_sp_alignment(s);
2751     }
2752     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2753     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2754     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2755                                 true, a->rn != 31, memop);
2756     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2757               iss_sf, a->lasr);
2758     return true;
2759 }
2760 
2761 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2762 {
2763     TCGv_i64 clean_addr;
2764     MemOp memop;
2765     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2766 
2767     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2768     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2769         return false;
2770     }
2771     /* Generate ISS for non-exclusive accesses including LASR.  */
2772     if (a->rn == 31) {
2773         gen_check_sp_alignment(s);
2774     }
2775     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2776     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2777                                 false, a->rn != 31, memop);
2778     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2779               a->rt, iss_sf, a->lasr);
2780     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2781     return true;
2782 }
2783 
2784 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2785 {
2786     if (a->rn == 31) {
2787         gen_check_sp_alignment(s);
2788     }
2789     if (a->lasr) {
2790         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2791     }
2792     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2793     return true;
2794 }
2795 
2796 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2797 {
2798     if (a->rn == 31) {
2799         gen_check_sp_alignment(s);
2800     }
2801     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2802     if (a->lasr) {
2803         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2804     }
2805     return true;
2806 }
2807 
2808 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2809 {
2810     if (!dc_isar_feature(aa64_atomics, s)) {
2811         return false;
2812     }
2813     if (((a->rt | a->rs) & 1) != 0) {
2814         return false;
2815     }
2816 
2817     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2818     return true;
2819 }
2820 
2821 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2822 {
2823     if (!dc_isar_feature(aa64_atomics, s)) {
2824         return false;
2825     }
2826     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2827     return true;
2828 }
2829 
2830 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2831 {
2832     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2833     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
2834     TCGv_i64 clean_addr = tcg_temp_new_i64();
2835     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
2836 
2837     gen_pc_plus_diff(s, clean_addr, a->imm);
2838     do_gpr_ld(s, tcg_rt, clean_addr, memop,
2839               false, true, a->rt, iss_sf, false);
2840     return true;
2841 }
2842 
2843 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
2844 {
2845     /* Load register (literal), vector version */
2846     TCGv_i64 clean_addr;
2847     MemOp memop;
2848 
2849     if (!fp_access_check(s)) {
2850         return true;
2851     }
2852     memop = finalize_memop_asimd(s, a->sz);
2853     clean_addr = tcg_temp_new_i64();
2854     gen_pc_plus_diff(s, clean_addr, a->imm);
2855     do_fp_ld(s, a->rt, clean_addr, memop);
2856     return true;
2857 }
2858 
2859 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
2860                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
2861                                  uint64_t offset, bool is_store, MemOp mop)
2862 {
2863     if (a->rn == 31) {
2864         gen_check_sp_alignment(s);
2865     }
2866 
2867     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
2868     if (!a->p) {
2869         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
2870     }
2871 
2872     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
2873                                  (a->w || a->rn != 31), 2 << a->sz, mop);
2874 }
2875 
2876 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
2877                                   TCGv_i64 dirty_addr, uint64_t offset)
2878 {
2879     if (a->w) {
2880         if (a->p) {
2881             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2882         }
2883         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
2884     }
2885 }
2886 
2887 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
2888 {
2889     uint64_t offset = a->imm << a->sz;
2890     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2891     MemOp mop = finalize_memop(s, a->sz);
2892 
2893     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2894     tcg_rt = cpu_reg(s, a->rt);
2895     tcg_rt2 = cpu_reg(s, a->rt2);
2896     /*
2897      * We built mop above for the single logical access -- rebuild it
2898      * now for the paired operation.
2899      *
2900      * With LSE2, non-sign-extending pairs are treated atomically if
2901      * aligned, and if unaligned one of the pair will be completely
2902      * within a 16-byte block and that element will be atomic.
2903      * Otherwise each element is separately atomic.
2904      * In all cases, issue one operation with the correct atomicity.
2905      */
2906     mop = a->sz + 1;
2907     if (s->align_mem) {
2908         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2909     }
2910     mop = finalize_memop_pair(s, mop);
2911     if (a->sz == 2) {
2912         TCGv_i64 tmp = tcg_temp_new_i64();
2913 
2914         if (s->be_data == MO_LE) {
2915             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2916         } else {
2917             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2918         }
2919         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2920     } else {
2921         TCGv_i128 tmp = tcg_temp_new_i128();
2922 
2923         if (s->be_data == MO_LE) {
2924             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
2925         } else {
2926             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
2927         }
2928         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
2929     }
2930     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2931     return true;
2932 }
2933 
2934 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
2935 {
2936     uint64_t offset = a->imm << a->sz;
2937     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2938     MemOp mop = finalize_memop(s, a->sz);
2939 
2940     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2941     tcg_rt = cpu_reg(s, a->rt);
2942     tcg_rt2 = cpu_reg(s, a->rt2);
2943 
2944     /*
2945      * We built mop above for the single logical access -- rebuild it
2946      * now for the paired operation.
2947      *
2948      * With LSE2, non-sign-extending pairs are treated atomically if
2949      * aligned, and if unaligned one of the pair will be completely
2950      * within a 16-byte block and that element will be atomic.
2951      * Otherwise each element is separately atomic.
2952      * In all cases, issue one operation with the correct atomicity.
2953      *
2954      * This treats sign-extending loads like zero-extending loads,
2955      * since that reuses the most code below.
2956      */
2957     mop = a->sz + 1;
2958     if (s->align_mem) {
2959         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2960     }
2961     mop = finalize_memop_pair(s, mop);
2962     if (a->sz == 2) {
2963         int o2 = s->be_data == MO_LE ? 32 : 0;
2964         int o1 = o2 ^ 32;
2965 
2966         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2967         if (a->sign) {
2968             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2969             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2970         } else {
2971             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2972             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2973         }
2974     } else {
2975         TCGv_i128 tmp = tcg_temp_new_i128();
2976 
2977         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2978         if (s->be_data == MO_LE) {
2979             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2980         } else {
2981             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2982         }
2983     }
2984     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2985     return true;
2986 }
2987 
2988 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
2989 {
2990     uint64_t offset = a->imm << a->sz;
2991     TCGv_i64 clean_addr, dirty_addr;
2992     MemOp mop;
2993 
2994     if (!fp_access_check(s)) {
2995         return true;
2996     }
2997 
2998     /* LSE2 does not merge FP pairs; leave these as separate operations. */
2999     mop = finalize_memop_asimd(s, a->sz);
3000     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3001     do_fp_st(s, a->rt, clean_addr, mop);
3002     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3003     do_fp_st(s, a->rt2, clean_addr, mop);
3004     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3005     return true;
3006 }
3007 
3008 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
3009 {
3010     uint64_t offset = a->imm << a->sz;
3011     TCGv_i64 clean_addr, dirty_addr;
3012     MemOp mop;
3013 
3014     if (!fp_access_check(s)) {
3015         return true;
3016     }
3017 
3018     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3019     mop = finalize_memop_asimd(s, a->sz);
3020     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3021     do_fp_ld(s, a->rt, clean_addr, mop);
3022     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3023     do_fp_ld(s, a->rt2, clean_addr, mop);
3024     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3025     return true;
3026 }
3027 
3028 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3029 {
3030     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3031     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3032     MemOp mop;
3033     TCGv_i128 tmp;
3034 
3035     /* STGP only comes in one size. */
3036     tcg_debug_assert(a->sz == MO_64);
3037 
3038     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3039         return false;
3040     }
3041 
3042     if (a->rn == 31) {
3043         gen_check_sp_alignment(s);
3044     }
3045 
3046     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3047     if (!a->p) {
3048         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3049     }
3050 
3051     clean_addr = clean_data_tbi(s, dirty_addr);
3052     tcg_rt = cpu_reg(s, a->rt);
3053     tcg_rt2 = cpu_reg(s, a->rt2);
3054 
3055     /*
3056      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3057      * and one tag operation.  We implement it as one single aligned 16-byte
3058      * memory operation for convenience.  Note that the alignment ensures
3059      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3060      */
3061     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3062 
3063     tmp = tcg_temp_new_i128();
3064     if (s->be_data == MO_LE) {
3065         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3066     } else {
3067         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3068     }
3069     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3070 
3071     /* Perform the tag store, if tag access enabled. */
3072     if (s->ata[0]) {
3073         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3074             gen_helper_stg_parallel(tcg_env, dirty_addr, dirty_addr);
3075         } else {
3076             gen_helper_stg(tcg_env, dirty_addr, dirty_addr);
3077         }
3078     }
3079 
3080     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3081     return true;
3082 }
3083 
3084 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3085                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3086                                  uint64_t offset, bool is_store, MemOp mop)
3087 {
3088     int memidx;
3089 
3090     if (a->rn == 31) {
3091         gen_check_sp_alignment(s);
3092     }
3093 
3094     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3095     if (!a->p) {
3096         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3097     }
3098     memidx = get_a64_user_mem_index(s, a->unpriv);
3099     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3100                                         a->w || a->rn != 31,
3101                                         mop, a->unpriv, memidx);
3102 }
3103 
3104 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3105                                   TCGv_i64 dirty_addr, uint64_t offset)
3106 {
3107     if (a->w) {
3108         if (a->p) {
3109             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3110         }
3111         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3112     }
3113 }
3114 
3115 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3116 {
3117     bool iss_sf, iss_valid = !a->w;
3118     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3119     int memidx = get_a64_user_mem_index(s, a->unpriv);
3120     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3121 
3122     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3123 
3124     tcg_rt = cpu_reg(s, a->rt);
3125     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3126 
3127     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3128                      iss_valid, a->rt, iss_sf, false);
3129     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3130     return true;
3131 }
3132 
3133 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3134 {
3135     bool iss_sf, iss_valid = !a->w;
3136     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3137     int memidx = get_a64_user_mem_index(s, a->unpriv);
3138     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3139 
3140     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3141 
3142     tcg_rt = cpu_reg(s, a->rt);
3143     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3144 
3145     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3146                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3147     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3148     return true;
3149 }
3150 
3151 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3152 {
3153     TCGv_i64 clean_addr, dirty_addr;
3154     MemOp mop;
3155 
3156     if (!fp_access_check(s)) {
3157         return true;
3158     }
3159     mop = finalize_memop_asimd(s, a->sz);
3160     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3161     do_fp_st(s, a->rt, clean_addr, mop);
3162     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3163     return true;
3164 }
3165 
3166 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3167 {
3168     TCGv_i64 clean_addr, dirty_addr;
3169     MemOp mop;
3170 
3171     if (!fp_access_check(s)) {
3172         return true;
3173     }
3174     mop = finalize_memop_asimd(s, a->sz);
3175     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3176     do_fp_ld(s, a->rt, clean_addr, mop);
3177     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3178     return true;
3179 }
3180 
3181 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3182                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3183                              bool is_store, MemOp memop)
3184 {
3185     TCGv_i64 tcg_rm;
3186 
3187     if (a->rn == 31) {
3188         gen_check_sp_alignment(s);
3189     }
3190     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3191 
3192     tcg_rm = read_cpu_reg(s, a->rm, 1);
3193     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3194 
3195     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3196     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3197 }
3198 
3199 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3200 {
3201     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3202     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3203     MemOp memop;
3204 
3205     if (extract32(a->opt, 1, 1) == 0) {
3206         return false;
3207     }
3208 
3209     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3210     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3211     tcg_rt = cpu_reg(s, a->rt);
3212     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3213               a->ext, true, a->rt, iss_sf, false);
3214     return true;
3215 }
3216 
3217 static bool trans_STR(DisasContext *s, arg_ldst *a)
3218 {
3219     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3220     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3221     MemOp memop;
3222 
3223     if (extract32(a->opt, 1, 1) == 0) {
3224         return false;
3225     }
3226 
3227     memop = finalize_memop(s, a->sz);
3228     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3229     tcg_rt = cpu_reg(s, a->rt);
3230     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3231     return true;
3232 }
3233 
3234 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3235 {
3236     TCGv_i64 clean_addr, dirty_addr;
3237     MemOp memop;
3238 
3239     if (extract32(a->opt, 1, 1) == 0) {
3240         return false;
3241     }
3242 
3243     if (!fp_access_check(s)) {
3244         return true;
3245     }
3246 
3247     memop = finalize_memop_asimd(s, a->sz);
3248     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3249     do_fp_ld(s, a->rt, clean_addr, memop);
3250     return true;
3251 }
3252 
3253 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3254 {
3255     TCGv_i64 clean_addr, dirty_addr;
3256     MemOp memop;
3257 
3258     if (extract32(a->opt, 1, 1) == 0) {
3259         return false;
3260     }
3261 
3262     if (!fp_access_check(s)) {
3263         return true;
3264     }
3265 
3266     memop = finalize_memop_asimd(s, a->sz);
3267     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3268     do_fp_st(s, a->rt, clean_addr, memop);
3269     return true;
3270 }
3271 
3272 
3273 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3274                          int sign, bool invert)
3275 {
3276     MemOp mop = a->sz | sign;
3277     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3278 
3279     if (a->rn == 31) {
3280         gen_check_sp_alignment(s);
3281     }
3282     mop = check_atomic_align(s, a->rn, mop);
3283     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3284                                 a->rn != 31, mop);
3285     tcg_rs = read_cpu_reg(s, a->rs, true);
3286     tcg_rt = cpu_reg(s, a->rt);
3287     if (invert) {
3288         tcg_gen_not_i64(tcg_rs, tcg_rs);
3289     }
3290     /*
3291      * The tcg atomic primitives are all full barriers.  Therefore we
3292      * can ignore the Acquire and Release bits of this instruction.
3293      */
3294     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3295 
3296     if (mop & MO_SIGN) {
3297         switch (a->sz) {
3298         case MO_8:
3299             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3300             break;
3301         case MO_16:
3302             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3303             break;
3304         case MO_32:
3305             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3306             break;
3307         case MO_64:
3308             break;
3309         default:
3310             g_assert_not_reached();
3311         }
3312     }
3313     return true;
3314 }
3315 
3316 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3317 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3318 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3319 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3320 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3321 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3322 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3323 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3324 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3325 
3326 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3327 {
3328     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3329     TCGv_i64 clean_addr;
3330     MemOp mop;
3331 
3332     if (!dc_isar_feature(aa64_atomics, s) ||
3333         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3334         return false;
3335     }
3336     if (a->rn == 31) {
3337         gen_check_sp_alignment(s);
3338     }
3339     mop = check_atomic_align(s, a->rn, a->sz);
3340     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3341                                 a->rn != 31, mop);
3342     /*
3343      * LDAPR* are a special case because they are a simple load, not a
3344      * fetch-and-do-something op.
3345      * The architectural consistency requirements here are weaker than
3346      * full load-acquire (we only need "load-acquire processor consistent"),
3347      * but we choose to implement them as full LDAQ.
3348      */
3349     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3350               true, a->rt, iss_sf, true);
3351     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3352     return true;
3353 }
3354 
3355 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3356 {
3357     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3358     MemOp memop;
3359 
3360     /* Load with pointer authentication */
3361     if (!dc_isar_feature(aa64_pauth, s)) {
3362         return false;
3363     }
3364 
3365     if (a->rn == 31) {
3366         gen_check_sp_alignment(s);
3367     }
3368     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3369 
3370     if (s->pauth_active) {
3371         if (!a->m) {
3372             gen_helper_autda_combined(dirty_addr, tcg_env, dirty_addr,
3373                                       tcg_constant_i64(0));
3374         } else {
3375             gen_helper_autdb_combined(dirty_addr, tcg_env, dirty_addr,
3376                                       tcg_constant_i64(0));
3377         }
3378     }
3379 
3380     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3381 
3382     memop = finalize_memop(s, MO_64);
3383 
3384     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3385     clean_addr = gen_mte_check1(s, dirty_addr, false,
3386                                 a->w || a->rn != 31, memop);
3387 
3388     tcg_rt = cpu_reg(s, a->rt);
3389     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3390               /* extend */ false, /* iss_valid */ !a->w,
3391               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3392 
3393     if (a->w) {
3394         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3395     }
3396     return true;
3397 }
3398 
3399 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3400 {
3401     TCGv_i64 clean_addr, dirty_addr;
3402     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3403     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3404 
3405     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3406         return false;
3407     }
3408 
3409     if (a->rn == 31) {
3410         gen_check_sp_alignment(s);
3411     }
3412 
3413     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3414     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3415     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3416     clean_addr = clean_data_tbi(s, dirty_addr);
3417 
3418     /*
3419      * Load-AcquirePC semantics; we implement as the slightly more
3420      * restrictive Load-Acquire.
3421      */
3422     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3423               a->rt, iss_sf, true);
3424     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3425     return true;
3426 }
3427 
3428 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3429 {
3430     TCGv_i64 clean_addr, dirty_addr;
3431     MemOp mop = a->sz;
3432     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3433 
3434     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3435         return false;
3436     }
3437 
3438     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3439 
3440     if (a->rn == 31) {
3441         gen_check_sp_alignment(s);
3442     }
3443 
3444     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3445     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3446     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3447     clean_addr = clean_data_tbi(s, dirty_addr);
3448 
3449     /* Store-Release semantics */
3450     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3451     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3452     return true;
3453 }
3454 
3455 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3456 {
3457     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3458     MemOp endian, align, mop;
3459 
3460     int total;    /* total bytes */
3461     int elements; /* elements per vector */
3462     int r;
3463     int size = a->sz;
3464 
3465     if (!a->p && a->rm != 0) {
3466         /* For non-postindexed accesses the Rm field must be 0 */
3467         return false;
3468     }
3469     if (size == 3 && !a->q && a->selem != 1) {
3470         return false;
3471     }
3472     if (!fp_access_check(s)) {
3473         return true;
3474     }
3475 
3476     if (a->rn == 31) {
3477         gen_check_sp_alignment(s);
3478     }
3479 
3480     /* For our purposes, bytes are always little-endian.  */
3481     endian = s->be_data;
3482     if (size == 0) {
3483         endian = MO_LE;
3484     }
3485 
3486     total = a->rpt * a->selem * (a->q ? 16 : 8);
3487     tcg_rn = cpu_reg_sp(s, a->rn);
3488 
3489     /*
3490      * Issue the MTE check vs the logical repeat count, before we
3491      * promote consecutive little-endian elements below.
3492      */
3493     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3494                                 finalize_memop_asimd(s, size));
3495 
3496     /*
3497      * Consecutive little-endian elements from a single register
3498      * can be promoted to a larger little-endian operation.
3499      */
3500     align = MO_ALIGN;
3501     if (a->selem == 1 && endian == MO_LE) {
3502         align = pow2_align(size);
3503         size = 3;
3504     }
3505     if (!s->align_mem) {
3506         align = 0;
3507     }
3508     mop = endian | size | align;
3509 
3510     elements = (a->q ? 16 : 8) >> size;
3511     tcg_ebytes = tcg_constant_i64(1 << size);
3512     for (r = 0; r < a->rpt; r++) {
3513         int e;
3514         for (e = 0; e < elements; e++) {
3515             int xs;
3516             for (xs = 0; xs < a->selem; xs++) {
3517                 int tt = (a->rt + r + xs) % 32;
3518                 do_vec_ld(s, tt, e, clean_addr, mop);
3519                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3520             }
3521         }
3522     }
3523 
3524     /*
3525      * For non-quad operations, setting a slice of the low 64 bits of
3526      * the register clears the high 64 bits (in the ARM ARM pseudocode
3527      * this is implicit in the fact that 'rval' is a 64 bit wide
3528      * variable).  For quad operations, we might still need to zero
3529      * the high bits of SVE.
3530      */
3531     for (r = 0; r < a->rpt * a->selem; r++) {
3532         int tt = (a->rt + r) % 32;
3533         clear_vec_high(s, a->q, tt);
3534     }
3535 
3536     if (a->p) {
3537         if (a->rm == 31) {
3538             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3539         } else {
3540             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3541         }
3542     }
3543     return true;
3544 }
3545 
3546 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3547 {
3548     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3549     MemOp endian, align, mop;
3550 
3551     int total;    /* total bytes */
3552     int elements; /* elements per vector */
3553     int r;
3554     int size = a->sz;
3555 
3556     if (!a->p && a->rm != 0) {
3557         /* For non-postindexed accesses the Rm field must be 0 */
3558         return false;
3559     }
3560     if (size == 3 && !a->q && a->selem != 1) {
3561         return false;
3562     }
3563     if (!fp_access_check(s)) {
3564         return true;
3565     }
3566 
3567     if (a->rn == 31) {
3568         gen_check_sp_alignment(s);
3569     }
3570 
3571     /* For our purposes, bytes are always little-endian.  */
3572     endian = s->be_data;
3573     if (size == 0) {
3574         endian = MO_LE;
3575     }
3576 
3577     total = a->rpt * a->selem * (a->q ? 16 : 8);
3578     tcg_rn = cpu_reg_sp(s, a->rn);
3579 
3580     /*
3581      * Issue the MTE check vs the logical repeat count, before we
3582      * promote consecutive little-endian elements below.
3583      */
3584     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3585                                 finalize_memop_asimd(s, size));
3586 
3587     /*
3588      * Consecutive little-endian elements from a single register
3589      * can be promoted to a larger little-endian operation.
3590      */
3591     align = MO_ALIGN;
3592     if (a->selem == 1 && endian == MO_LE) {
3593         align = pow2_align(size);
3594         size = 3;
3595     }
3596     if (!s->align_mem) {
3597         align = 0;
3598     }
3599     mop = endian | size | align;
3600 
3601     elements = (a->q ? 16 : 8) >> size;
3602     tcg_ebytes = tcg_constant_i64(1 << size);
3603     for (r = 0; r < a->rpt; r++) {
3604         int e;
3605         for (e = 0; e < elements; e++) {
3606             int xs;
3607             for (xs = 0; xs < a->selem; xs++) {
3608                 int tt = (a->rt + r + xs) % 32;
3609                 do_vec_st(s, tt, e, clean_addr, mop);
3610                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3611             }
3612         }
3613     }
3614 
3615     if (a->p) {
3616         if (a->rm == 31) {
3617             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3618         } else {
3619             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3620         }
3621     }
3622     return true;
3623 }
3624 
3625 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3626 {
3627     int xs, total, rt;
3628     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3629     MemOp mop;
3630 
3631     if (!a->p && a->rm != 0) {
3632         return false;
3633     }
3634     if (!fp_access_check(s)) {
3635         return true;
3636     }
3637 
3638     if (a->rn == 31) {
3639         gen_check_sp_alignment(s);
3640     }
3641 
3642     total = a->selem << a->scale;
3643     tcg_rn = cpu_reg_sp(s, a->rn);
3644 
3645     mop = finalize_memop_asimd(s, a->scale);
3646     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3647                                 total, mop);
3648 
3649     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3650     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3651         do_vec_st(s, rt, a->index, clean_addr, mop);
3652         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3653     }
3654 
3655     if (a->p) {
3656         if (a->rm == 31) {
3657             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3658         } else {
3659             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3660         }
3661     }
3662     return true;
3663 }
3664 
3665 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3666 {
3667     int xs, total, rt;
3668     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3669     MemOp mop;
3670 
3671     if (!a->p && a->rm != 0) {
3672         return false;
3673     }
3674     if (!fp_access_check(s)) {
3675         return true;
3676     }
3677 
3678     if (a->rn == 31) {
3679         gen_check_sp_alignment(s);
3680     }
3681 
3682     total = a->selem << a->scale;
3683     tcg_rn = cpu_reg_sp(s, a->rn);
3684 
3685     mop = finalize_memop_asimd(s, a->scale);
3686     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3687                                 total, mop);
3688 
3689     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3690     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3691         do_vec_ld(s, rt, a->index, clean_addr, mop);
3692         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3693     }
3694 
3695     if (a->p) {
3696         if (a->rm == 31) {
3697             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3698         } else {
3699             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3700         }
3701     }
3702     return true;
3703 }
3704 
3705 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3706 {
3707     int xs, total, rt;
3708     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3709     MemOp mop;
3710 
3711     if (!a->p && a->rm != 0) {
3712         return false;
3713     }
3714     if (!fp_access_check(s)) {
3715         return true;
3716     }
3717 
3718     if (a->rn == 31) {
3719         gen_check_sp_alignment(s);
3720     }
3721 
3722     total = a->selem << a->scale;
3723     tcg_rn = cpu_reg_sp(s, a->rn);
3724 
3725     mop = finalize_memop_asimd(s, a->scale);
3726     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3727                                 total, mop);
3728 
3729     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3730     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3731         /* Load and replicate to all elements */
3732         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3733 
3734         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3735         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3736                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3737         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3738     }
3739 
3740     if (a->p) {
3741         if (a->rm == 31) {
3742             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3743         } else {
3744             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3745         }
3746     }
3747     return true;
3748 }
3749 
3750 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3751 {
3752     TCGv_i64 addr, clean_addr, tcg_rt;
3753     int size = 4 << s->dcz_blocksize;
3754 
3755     if (!dc_isar_feature(aa64_mte, s)) {
3756         return false;
3757     }
3758     if (s->current_el == 0) {
3759         return false;
3760     }
3761 
3762     if (a->rn == 31) {
3763         gen_check_sp_alignment(s);
3764     }
3765 
3766     addr = read_cpu_reg_sp(s, a->rn, true);
3767     tcg_gen_addi_i64(addr, addr, a->imm);
3768     tcg_rt = cpu_reg(s, a->rt);
3769 
3770     if (s->ata[0]) {
3771         gen_helper_stzgm_tags(tcg_env, addr, tcg_rt);
3772     }
3773     /*
3774      * The non-tags portion of STZGM is mostly like DC_ZVA,
3775      * except the alignment happens before the access.
3776      */
3777     clean_addr = clean_data_tbi(s, addr);
3778     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3779     gen_helper_dc_zva(tcg_env, clean_addr);
3780     return true;
3781 }
3782 
3783 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3784 {
3785     TCGv_i64 addr, clean_addr, tcg_rt;
3786 
3787     if (!dc_isar_feature(aa64_mte, s)) {
3788         return false;
3789     }
3790     if (s->current_el == 0) {
3791         return false;
3792     }
3793 
3794     if (a->rn == 31) {
3795         gen_check_sp_alignment(s);
3796     }
3797 
3798     addr = read_cpu_reg_sp(s, a->rn, true);
3799     tcg_gen_addi_i64(addr, addr, a->imm);
3800     tcg_rt = cpu_reg(s, a->rt);
3801 
3802     if (s->ata[0]) {
3803         gen_helper_stgm(tcg_env, addr, tcg_rt);
3804     } else {
3805         MMUAccessType acc = MMU_DATA_STORE;
3806         int size = 4 << s->gm_blocksize;
3807 
3808         clean_addr = clean_data_tbi(s, addr);
3809         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3810         gen_probe_access(s, clean_addr, acc, size);
3811     }
3812     return true;
3813 }
3814 
3815 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3816 {
3817     TCGv_i64 addr, clean_addr, tcg_rt;
3818 
3819     if (!dc_isar_feature(aa64_mte, s)) {
3820         return false;
3821     }
3822     if (s->current_el == 0) {
3823         return false;
3824     }
3825 
3826     if (a->rn == 31) {
3827         gen_check_sp_alignment(s);
3828     }
3829 
3830     addr = read_cpu_reg_sp(s, a->rn, true);
3831     tcg_gen_addi_i64(addr, addr, a->imm);
3832     tcg_rt = cpu_reg(s, a->rt);
3833 
3834     if (s->ata[0]) {
3835         gen_helper_ldgm(tcg_rt, tcg_env, addr);
3836     } else {
3837         MMUAccessType acc = MMU_DATA_LOAD;
3838         int size = 4 << s->gm_blocksize;
3839 
3840         clean_addr = clean_data_tbi(s, addr);
3841         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3842         gen_probe_access(s, clean_addr, acc, size);
3843         /* The result tags are zeros.  */
3844         tcg_gen_movi_i64(tcg_rt, 0);
3845     }
3846     return true;
3847 }
3848 
3849 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
3850 {
3851     TCGv_i64 addr, clean_addr, tcg_rt;
3852 
3853     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3854         return false;
3855     }
3856 
3857     if (a->rn == 31) {
3858         gen_check_sp_alignment(s);
3859     }
3860 
3861     addr = read_cpu_reg_sp(s, a->rn, true);
3862     if (!a->p) {
3863         /* pre-index or signed offset */
3864         tcg_gen_addi_i64(addr, addr, a->imm);
3865     }
3866 
3867     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
3868     tcg_rt = cpu_reg(s, a->rt);
3869     if (s->ata[0]) {
3870         gen_helper_ldg(tcg_rt, tcg_env, addr, tcg_rt);
3871     } else {
3872         /*
3873          * Tag access disabled: we must check for aborts on the load
3874          * load from [rn+offset], and then insert a 0 tag into rt.
3875          */
3876         clean_addr = clean_data_tbi(s, addr);
3877         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
3878         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
3879     }
3880 
3881     if (a->w) {
3882         /* pre-index or post-index */
3883         if (a->p) {
3884             /* post-index */
3885             tcg_gen_addi_i64(addr, addr, a->imm);
3886         }
3887         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3888     }
3889     return true;
3890 }
3891 
3892 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
3893 {
3894     TCGv_i64 addr, tcg_rt;
3895 
3896     if (a->rn == 31) {
3897         gen_check_sp_alignment(s);
3898     }
3899 
3900     addr = read_cpu_reg_sp(s, a->rn, true);
3901     if (!a->p) {
3902         /* pre-index or signed offset */
3903         tcg_gen_addi_i64(addr, addr, a->imm);
3904     }
3905     tcg_rt = cpu_reg_sp(s, a->rt);
3906     if (!s->ata[0]) {
3907         /*
3908          * For STG and ST2G, we need to check alignment and probe memory.
3909          * TODO: For STZG and STZ2G, we could rely on the stores below,
3910          * at least for system mode; user-only won't enforce alignment.
3911          */
3912         if (is_pair) {
3913             gen_helper_st2g_stub(tcg_env, addr);
3914         } else {
3915             gen_helper_stg_stub(tcg_env, addr);
3916         }
3917     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3918         if (is_pair) {
3919             gen_helper_st2g_parallel(tcg_env, addr, tcg_rt);
3920         } else {
3921             gen_helper_stg_parallel(tcg_env, addr, tcg_rt);
3922         }
3923     } else {
3924         if (is_pair) {
3925             gen_helper_st2g(tcg_env, addr, tcg_rt);
3926         } else {
3927             gen_helper_stg(tcg_env, addr, tcg_rt);
3928         }
3929     }
3930 
3931     if (is_zero) {
3932         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
3933         TCGv_i64 zero64 = tcg_constant_i64(0);
3934         TCGv_i128 zero128 = tcg_temp_new_i128();
3935         int mem_index = get_mem_index(s);
3936         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
3937 
3938         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
3939 
3940         /* This is 1 or 2 atomic 16-byte operations. */
3941         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3942         if (is_pair) {
3943             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
3944             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3945         }
3946     }
3947 
3948     if (a->w) {
3949         /* pre-index or post-index */
3950         if (a->p) {
3951             /* post-index */
3952             tcg_gen_addi_i64(addr, addr, a->imm);
3953         }
3954         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3955     }
3956     return true;
3957 }
3958 
3959 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
3960 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
3961 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
3962 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
3963 
3964 typedef void SetFn(TCGv_env, TCGv_i32, TCGv_i32);
3965 
3966 static bool do_SET(DisasContext *s, arg_set *a, bool is_epilogue,
3967                    bool is_setg, SetFn fn)
3968 {
3969     int memidx;
3970     uint32_t syndrome, desc = 0;
3971 
3972     if (is_setg && !dc_isar_feature(aa64_mte, s)) {
3973         return false;
3974     }
3975 
3976     /*
3977      * UNPREDICTABLE cases: we choose to UNDEF, which allows
3978      * us to pull this check before the CheckMOPSEnabled() test
3979      * (which we do in the helper function)
3980      */
3981     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
3982         a->rd == 31 || a->rn == 31) {
3983         return false;
3984     }
3985 
3986     memidx = get_a64_user_mem_index(s, a->unpriv);
3987 
3988     /*
3989      * We pass option_a == true, matching our implementation;
3990      * we pass wrong_option == false: helper function may set that bit.
3991      */
3992     syndrome = syn_mop(true, is_setg, (a->nontemp << 1) | a->unpriv,
3993                        is_epilogue, false, true, a->rd, a->rs, a->rn);
3994 
3995     if (is_setg ? s->ata[a->unpriv] : s->mte_active[a->unpriv]) {
3996         /* We may need to do MTE tag checking, so assemble the descriptor */
3997         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
3998         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
3999         desc = FIELD_DP32(desc, MTEDESC, WRITE, true);
4000         /* SIZEM1 and ALIGN we leave 0 (byte write) */
4001     }
4002     /* The helper function always needs the memidx even with MTE disabled */
4003     desc = FIELD_DP32(desc, MTEDESC, MIDX, memidx);
4004 
4005     /*
4006      * The helper needs the register numbers, but since they're in
4007      * the syndrome anyway, we let it extract them from there rather
4008      * than passing in an extra three integer arguments.
4009      */
4010     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(desc));
4011     return true;
4012 }
4013 
4014 TRANS_FEAT(SETP, aa64_mops, do_SET, a, false, false, gen_helper_setp)
4015 TRANS_FEAT(SETM, aa64_mops, do_SET, a, false, false, gen_helper_setm)
4016 TRANS_FEAT(SETE, aa64_mops, do_SET, a, true, false, gen_helper_sete)
4017 TRANS_FEAT(SETGP, aa64_mops, do_SET, a, false, true, gen_helper_setgp)
4018 TRANS_FEAT(SETGM, aa64_mops, do_SET, a, false, true, gen_helper_setgm)
4019 TRANS_FEAT(SETGE, aa64_mops, do_SET, a, true, true, gen_helper_setge)
4020 
4021 typedef void CpyFn(TCGv_env, TCGv_i32, TCGv_i32, TCGv_i32);
4022 
4023 static bool do_CPY(DisasContext *s, arg_cpy *a, bool is_epilogue, CpyFn fn)
4024 {
4025     int rmemidx, wmemidx;
4026     uint32_t syndrome, rdesc = 0, wdesc = 0;
4027     bool wunpriv = extract32(a->options, 0, 1);
4028     bool runpriv = extract32(a->options, 1, 1);
4029 
4030     /*
4031      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4032      * us to pull this check before the CheckMOPSEnabled() test
4033      * (which we do in the helper function)
4034      */
4035     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4036         a->rd == 31 || a->rs == 31 || a->rn == 31) {
4037         return false;
4038     }
4039 
4040     rmemidx = get_a64_user_mem_index(s, runpriv);
4041     wmemidx = get_a64_user_mem_index(s, wunpriv);
4042 
4043     /*
4044      * We pass option_a == true, matching our implementation;
4045      * we pass wrong_option == false: helper function may set that bit.
4046      */
4047     syndrome = syn_mop(false, false, a->options, is_epilogue,
4048                        false, true, a->rd, a->rs, a->rn);
4049 
4050     /* If we need to do MTE tag checking, assemble the descriptors */
4051     if (s->mte_active[runpriv]) {
4052         rdesc = FIELD_DP32(rdesc, MTEDESC, TBI, s->tbid);
4053         rdesc = FIELD_DP32(rdesc, MTEDESC, TCMA, s->tcma);
4054     }
4055     if (s->mte_active[wunpriv]) {
4056         wdesc = FIELD_DP32(wdesc, MTEDESC, TBI, s->tbid);
4057         wdesc = FIELD_DP32(wdesc, MTEDESC, TCMA, s->tcma);
4058         wdesc = FIELD_DP32(wdesc, MTEDESC, WRITE, true);
4059     }
4060     /* The helper function needs these parts of the descriptor regardless */
4061     rdesc = FIELD_DP32(rdesc, MTEDESC, MIDX, rmemidx);
4062     wdesc = FIELD_DP32(wdesc, MTEDESC, MIDX, wmemidx);
4063 
4064     /*
4065      * The helper needs the register numbers, but since they're in
4066      * the syndrome anyway, we let it extract them from there rather
4067      * than passing in an extra three integer arguments.
4068      */
4069     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(wdesc),
4070        tcg_constant_i32(rdesc));
4071     return true;
4072 }
4073 
4074 TRANS_FEAT(CPYP, aa64_mops, do_CPY, a, false, gen_helper_cpyp)
4075 TRANS_FEAT(CPYM, aa64_mops, do_CPY, a, false, gen_helper_cpym)
4076 TRANS_FEAT(CPYE, aa64_mops, do_CPY, a, true, gen_helper_cpye)
4077 TRANS_FEAT(CPYFP, aa64_mops, do_CPY, a, false, gen_helper_cpyfp)
4078 TRANS_FEAT(CPYFM, aa64_mops, do_CPY, a, false, gen_helper_cpyfm)
4079 TRANS_FEAT(CPYFE, aa64_mops, do_CPY, a, true, gen_helper_cpyfe)
4080 
4081 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4082 
4083 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4084                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4085 {
4086     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4087     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4088     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4089 
4090     fn(tcg_rd, tcg_rn, tcg_imm);
4091     if (!a->sf) {
4092         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4093     }
4094     return true;
4095 }
4096 
4097 /*
4098  * PC-rel. addressing
4099  */
4100 
4101 static bool trans_ADR(DisasContext *s, arg_ri *a)
4102 {
4103     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4104     return true;
4105 }
4106 
4107 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4108 {
4109     int64_t offset = (int64_t)a->imm << 12;
4110 
4111     /* The page offset is ok for CF_PCREL. */
4112     offset -= s->pc_curr & 0xfff;
4113     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4114     return true;
4115 }
4116 
4117 /*
4118  * Add/subtract (immediate)
4119  */
4120 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4121 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4122 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4123 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4124 
4125 /*
4126  * Add/subtract (immediate, with tags)
4127  */
4128 
4129 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4130                                       bool sub_op)
4131 {
4132     TCGv_i64 tcg_rn, tcg_rd;
4133     int imm;
4134 
4135     imm = a->uimm6 << LOG2_TAG_GRANULE;
4136     if (sub_op) {
4137         imm = -imm;
4138     }
4139 
4140     tcg_rn = cpu_reg_sp(s, a->rn);
4141     tcg_rd = cpu_reg_sp(s, a->rd);
4142 
4143     if (s->ata[0]) {
4144         gen_helper_addsubg(tcg_rd, tcg_env, tcg_rn,
4145                            tcg_constant_i32(imm),
4146                            tcg_constant_i32(a->uimm4));
4147     } else {
4148         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4149         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4150     }
4151     return true;
4152 }
4153 
4154 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4155 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4156 
4157 /* The input should be a value in the bottom e bits (with higher
4158  * bits zero); returns that value replicated into every element
4159  * of size e in a 64 bit integer.
4160  */
4161 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4162 {
4163     assert(e != 0);
4164     while (e < 64) {
4165         mask |= mask << e;
4166         e *= 2;
4167     }
4168     return mask;
4169 }
4170 
4171 /*
4172  * Logical (immediate)
4173  */
4174 
4175 /*
4176  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4177  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4178  * value (ie should cause a guest UNDEF exception), and true if they are
4179  * valid, in which case the decoded bit pattern is written to result.
4180  */
4181 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4182                             unsigned int imms, unsigned int immr)
4183 {
4184     uint64_t mask;
4185     unsigned e, levels, s, r;
4186     int len;
4187 
4188     assert(immn < 2 && imms < 64 && immr < 64);
4189 
4190     /* The bit patterns we create here are 64 bit patterns which
4191      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4192      * 64 bits each. Each element contains the same value: a run
4193      * of between 1 and e-1 non-zero bits, rotated within the
4194      * element by between 0 and e-1 bits.
4195      *
4196      * The element size and run length are encoded into immn (1 bit)
4197      * and imms (6 bits) as follows:
4198      * 64 bit elements: immn = 1, imms = <length of run - 1>
4199      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4200      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4201      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4202      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4203      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4204      * Notice that immn = 0, imms = 11111x is the only combination
4205      * not covered by one of the above options; this is reserved.
4206      * Further, <length of run - 1> all-ones is a reserved pattern.
4207      *
4208      * In all cases the rotation is by immr % e (and immr is 6 bits).
4209      */
4210 
4211     /* First determine the element size */
4212     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4213     if (len < 1) {
4214         /* This is the immn == 0, imms == 0x11111x case */
4215         return false;
4216     }
4217     e = 1 << len;
4218 
4219     levels = e - 1;
4220     s = imms & levels;
4221     r = immr & levels;
4222 
4223     if (s == levels) {
4224         /* <length of run - 1> mustn't be all-ones. */
4225         return false;
4226     }
4227 
4228     /* Create the value of one element: s+1 set bits rotated
4229      * by r within the element (which is e bits wide)...
4230      */
4231     mask = MAKE_64BIT_MASK(0, s + 1);
4232     if (r) {
4233         mask = (mask >> r) | (mask << (e - r));
4234         mask &= MAKE_64BIT_MASK(0, e);
4235     }
4236     /* ...then replicate the element over the whole 64 bit value */
4237     mask = bitfield_replicate(mask, e);
4238     *result = mask;
4239     return true;
4240 }
4241 
4242 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4243                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4244 {
4245     TCGv_i64 tcg_rd, tcg_rn;
4246     uint64_t imm;
4247 
4248     /* Some immediate field values are reserved. */
4249     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4250                                 extract32(a->dbm, 0, 6),
4251                                 extract32(a->dbm, 6, 6))) {
4252         return false;
4253     }
4254     if (!a->sf) {
4255         imm &= 0xffffffffull;
4256     }
4257 
4258     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4259     tcg_rn = cpu_reg(s, a->rn);
4260 
4261     fn(tcg_rd, tcg_rn, imm);
4262     if (set_cc) {
4263         gen_logic_CC(a->sf, tcg_rd);
4264     }
4265     if (!a->sf) {
4266         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4267     }
4268     return true;
4269 }
4270 
4271 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4272 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4273 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4274 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4275 
4276 /*
4277  * Move wide (immediate)
4278  */
4279 
4280 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4281 {
4282     int pos = a->hw << 4;
4283     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4284     return true;
4285 }
4286 
4287 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4288 {
4289     int pos = a->hw << 4;
4290     uint64_t imm = a->imm;
4291 
4292     imm = ~(imm << pos);
4293     if (!a->sf) {
4294         imm = (uint32_t)imm;
4295     }
4296     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4297     return true;
4298 }
4299 
4300 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4301 {
4302     int pos = a->hw << 4;
4303     TCGv_i64 tcg_rd, tcg_im;
4304 
4305     tcg_rd = cpu_reg(s, a->rd);
4306     tcg_im = tcg_constant_i64(a->imm);
4307     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4308     if (!a->sf) {
4309         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4310     }
4311     return true;
4312 }
4313 
4314 /*
4315  * Bitfield
4316  */
4317 
4318 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4319 {
4320     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4321     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4322     unsigned int bitsize = a->sf ? 64 : 32;
4323     unsigned int ri = a->immr;
4324     unsigned int si = a->imms;
4325     unsigned int pos, len;
4326 
4327     if (si >= ri) {
4328         /* Wd<s-r:0> = Wn<s:r> */
4329         len = (si - ri) + 1;
4330         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4331         if (!a->sf) {
4332             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4333         }
4334     } else {
4335         /* Wd<32+s-r,32-r> = Wn<s:0> */
4336         len = si + 1;
4337         pos = (bitsize - ri) & (bitsize - 1);
4338 
4339         if (len < ri) {
4340             /*
4341              * Sign extend the destination field from len to fill the
4342              * balance of the word.  Let the deposit below insert all
4343              * of those sign bits.
4344              */
4345             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4346             len = ri;
4347         }
4348 
4349         /*
4350          * We start with zero, and we haven't modified any bits outside
4351          * bitsize, therefore no final zero-extension is unneeded for !sf.
4352          */
4353         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4354     }
4355     return true;
4356 }
4357 
4358 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4359 {
4360     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4361     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4362     unsigned int bitsize = a->sf ? 64 : 32;
4363     unsigned int ri = a->immr;
4364     unsigned int si = a->imms;
4365     unsigned int pos, len;
4366 
4367     tcg_rd = cpu_reg(s, a->rd);
4368     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4369 
4370     if (si >= ri) {
4371         /* Wd<s-r:0> = Wn<s:r> */
4372         len = (si - ri) + 1;
4373         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4374     } else {
4375         /* Wd<32+s-r,32-r> = Wn<s:0> */
4376         len = si + 1;
4377         pos = (bitsize - ri) & (bitsize - 1);
4378         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4379     }
4380     return true;
4381 }
4382 
4383 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4384 {
4385     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4386     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4387     unsigned int bitsize = a->sf ? 64 : 32;
4388     unsigned int ri = a->immr;
4389     unsigned int si = a->imms;
4390     unsigned int pos, len;
4391 
4392     tcg_rd = cpu_reg(s, a->rd);
4393     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4394 
4395     if (si >= ri) {
4396         /* Wd<s-r:0> = Wn<s:r> */
4397         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4398         len = (si - ri) + 1;
4399         pos = 0;
4400     } else {
4401         /* Wd<32+s-r,32-r> = Wn<s:0> */
4402         len = si + 1;
4403         pos = (bitsize - ri) & (bitsize - 1);
4404     }
4405 
4406     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4407     if (!a->sf) {
4408         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4409     }
4410     return true;
4411 }
4412 
4413 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4414 {
4415     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4416 
4417     tcg_rd = cpu_reg(s, a->rd);
4418 
4419     if (unlikely(a->imm == 0)) {
4420         /*
4421          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4422          * so an extract from bit 0 is a special case.
4423          */
4424         if (a->sf) {
4425             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4426         } else {
4427             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4428         }
4429     } else {
4430         tcg_rm = cpu_reg(s, a->rm);
4431         tcg_rn = cpu_reg(s, a->rn);
4432 
4433         if (a->sf) {
4434             /* Specialization to ROR happens in EXTRACT2.  */
4435             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4436         } else {
4437             TCGv_i32 t0 = tcg_temp_new_i32();
4438 
4439             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4440             if (a->rm == a->rn) {
4441                 tcg_gen_rotri_i32(t0, t0, a->imm);
4442             } else {
4443                 TCGv_i32 t1 = tcg_temp_new_i32();
4444                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4445                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4446             }
4447             tcg_gen_extu_i32_i64(tcg_rd, t0);
4448         }
4449     }
4450     return true;
4451 }
4452 
4453 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4454  * Note that it is the caller's responsibility to ensure that the
4455  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4456  * mandated semantics for out of range shifts.
4457  */
4458 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4459                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4460 {
4461     switch (shift_type) {
4462     case A64_SHIFT_TYPE_LSL:
4463         tcg_gen_shl_i64(dst, src, shift_amount);
4464         break;
4465     case A64_SHIFT_TYPE_LSR:
4466         tcg_gen_shr_i64(dst, src, shift_amount);
4467         break;
4468     case A64_SHIFT_TYPE_ASR:
4469         if (!sf) {
4470             tcg_gen_ext32s_i64(dst, src);
4471         }
4472         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4473         break;
4474     case A64_SHIFT_TYPE_ROR:
4475         if (sf) {
4476             tcg_gen_rotr_i64(dst, src, shift_amount);
4477         } else {
4478             TCGv_i32 t0, t1;
4479             t0 = tcg_temp_new_i32();
4480             t1 = tcg_temp_new_i32();
4481             tcg_gen_extrl_i64_i32(t0, src);
4482             tcg_gen_extrl_i64_i32(t1, shift_amount);
4483             tcg_gen_rotr_i32(t0, t0, t1);
4484             tcg_gen_extu_i32_i64(dst, t0);
4485         }
4486         break;
4487     default:
4488         assert(FALSE); /* all shift types should be handled */
4489         break;
4490     }
4491 
4492     if (!sf) { /* zero extend final result */
4493         tcg_gen_ext32u_i64(dst, dst);
4494     }
4495 }
4496 
4497 /* Shift a TCGv src by immediate, put result in dst.
4498  * The shift amount must be in range (this should always be true as the
4499  * relevant instructions will UNDEF on bad shift immediates).
4500  */
4501 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4502                           enum a64_shift_type shift_type, unsigned int shift_i)
4503 {
4504     assert(shift_i < (sf ? 64 : 32));
4505 
4506     if (shift_i == 0) {
4507         tcg_gen_mov_i64(dst, src);
4508     } else {
4509         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4510     }
4511 }
4512 
4513 /* Logical (shifted register)
4514  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4515  * +----+-----+-----------+-------+---+------+--------+------+------+
4516  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4517  * +----+-----+-----------+-------+---+------+--------+------+------+
4518  */
4519 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4520 {
4521     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4522     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4523 
4524     sf = extract32(insn, 31, 1);
4525     opc = extract32(insn, 29, 2);
4526     shift_type = extract32(insn, 22, 2);
4527     invert = extract32(insn, 21, 1);
4528     rm = extract32(insn, 16, 5);
4529     shift_amount = extract32(insn, 10, 6);
4530     rn = extract32(insn, 5, 5);
4531     rd = extract32(insn, 0, 5);
4532 
4533     if (!sf && (shift_amount & (1 << 5))) {
4534         unallocated_encoding(s);
4535         return;
4536     }
4537 
4538     tcg_rd = cpu_reg(s, rd);
4539 
4540     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4541         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4542          * register-register MOV and MVN, so it is worth special casing.
4543          */
4544         tcg_rm = cpu_reg(s, rm);
4545         if (invert) {
4546             tcg_gen_not_i64(tcg_rd, tcg_rm);
4547             if (!sf) {
4548                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4549             }
4550         } else {
4551             if (sf) {
4552                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4553             } else {
4554                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4555             }
4556         }
4557         return;
4558     }
4559 
4560     tcg_rm = read_cpu_reg(s, rm, sf);
4561 
4562     if (shift_amount) {
4563         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4564     }
4565 
4566     tcg_rn = cpu_reg(s, rn);
4567 
4568     switch (opc | (invert << 2)) {
4569     case 0: /* AND */
4570     case 3: /* ANDS */
4571         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4572         break;
4573     case 1: /* ORR */
4574         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4575         break;
4576     case 2: /* EOR */
4577         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4578         break;
4579     case 4: /* BIC */
4580     case 7: /* BICS */
4581         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4582         break;
4583     case 5: /* ORN */
4584         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4585         break;
4586     case 6: /* EON */
4587         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4588         break;
4589     default:
4590         assert(FALSE);
4591         break;
4592     }
4593 
4594     if (!sf) {
4595         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4596     }
4597 
4598     if (opc == 3) {
4599         gen_logic_CC(sf, tcg_rd);
4600     }
4601 }
4602 
4603 /*
4604  * Add/subtract (extended register)
4605  *
4606  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4607  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4608  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4609  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4610  *
4611  *  sf: 0 -> 32bit, 1 -> 64bit
4612  *  op: 0 -> add  , 1 -> sub
4613  *   S: 1 -> set flags
4614  * opt: 00
4615  * option: extension type (see DecodeRegExtend)
4616  * imm3: optional shift to Rm
4617  *
4618  * Rd = Rn + LSL(extend(Rm), amount)
4619  */
4620 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4621 {
4622     int rd = extract32(insn, 0, 5);
4623     int rn = extract32(insn, 5, 5);
4624     int imm3 = extract32(insn, 10, 3);
4625     int option = extract32(insn, 13, 3);
4626     int rm = extract32(insn, 16, 5);
4627     int opt = extract32(insn, 22, 2);
4628     bool setflags = extract32(insn, 29, 1);
4629     bool sub_op = extract32(insn, 30, 1);
4630     bool sf = extract32(insn, 31, 1);
4631 
4632     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4633     TCGv_i64 tcg_rd;
4634     TCGv_i64 tcg_result;
4635 
4636     if (imm3 > 4 || opt != 0) {
4637         unallocated_encoding(s);
4638         return;
4639     }
4640 
4641     /* non-flag setting ops may use SP */
4642     if (!setflags) {
4643         tcg_rd = cpu_reg_sp(s, rd);
4644     } else {
4645         tcg_rd = cpu_reg(s, rd);
4646     }
4647     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4648 
4649     tcg_rm = read_cpu_reg(s, rm, sf);
4650     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4651 
4652     tcg_result = tcg_temp_new_i64();
4653 
4654     if (!setflags) {
4655         if (sub_op) {
4656             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4657         } else {
4658             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4659         }
4660     } else {
4661         if (sub_op) {
4662             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4663         } else {
4664             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4665         }
4666     }
4667 
4668     if (sf) {
4669         tcg_gen_mov_i64(tcg_rd, tcg_result);
4670     } else {
4671         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4672     }
4673 }
4674 
4675 /*
4676  * Add/subtract (shifted register)
4677  *
4678  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4679  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4680  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4681  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4682  *
4683  *    sf: 0 -> 32bit, 1 -> 64bit
4684  *    op: 0 -> add  , 1 -> sub
4685  *     S: 1 -> set flags
4686  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4687  *  imm6: Shift amount to apply to Rm before the add/sub
4688  */
4689 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4690 {
4691     int rd = extract32(insn, 0, 5);
4692     int rn = extract32(insn, 5, 5);
4693     int imm6 = extract32(insn, 10, 6);
4694     int rm = extract32(insn, 16, 5);
4695     int shift_type = extract32(insn, 22, 2);
4696     bool setflags = extract32(insn, 29, 1);
4697     bool sub_op = extract32(insn, 30, 1);
4698     bool sf = extract32(insn, 31, 1);
4699 
4700     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4701     TCGv_i64 tcg_rn, tcg_rm;
4702     TCGv_i64 tcg_result;
4703 
4704     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4705         unallocated_encoding(s);
4706         return;
4707     }
4708 
4709     tcg_rn = read_cpu_reg(s, rn, sf);
4710     tcg_rm = read_cpu_reg(s, rm, sf);
4711 
4712     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4713 
4714     tcg_result = tcg_temp_new_i64();
4715 
4716     if (!setflags) {
4717         if (sub_op) {
4718             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4719         } else {
4720             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4721         }
4722     } else {
4723         if (sub_op) {
4724             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4725         } else {
4726             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4727         }
4728     }
4729 
4730     if (sf) {
4731         tcg_gen_mov_i64(tcg_rd, tcg_result);
4732     } else {
4733         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4734     }
4735 }
4736 
4737 /* Data-processing (3 source)
4738  *
4739  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4740  *  +--+------+-----------+------+------+----+------+------+------+
4741  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4742  *  +--+------+-----------+------+------+----+------+------+------+
4743  */
4744 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4745 {
4746     int rd = extract32(insn, 0, 5);
4747     int rn = extract32(insn, 5, 5);
4748     int ra = extract32(insn, 10, 5);
4749     int rm = extract32(insn, 16, 5);
4750     int op_id = (extract32(insn, 29, 3) << 4) |
4751         (extract32(insn, 21, 3) << 1) |
4752         extract32(insn, 15, 1);
4753     bool sf = extract32(insn, 31, 1);
4754     bool is_sub = extract32(op_id, 0, 1);
4755     bool is_high = extract32(op_id, 2, 1);
4756     bool is_signed = false;
4757     TCGv_i64 tcg_op1;
4758     TCGv_i64 tcg_op2;
4759     TCGv_i64 tcg_tmp;
4760 
4761     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4762     switch (op_id) {
4763     case 0x42: /* SMADDL */
4764     case 0x43: /* SMSUBL */
4765     case 0x44: /* SMULH */
4766         is_signed = true;
4767         break;
4768     case 0x0: /* MADD (32bit) */
4769     case 0x1: /* MSUB (32bit) */
4770     case 0x40: /* MADD (64bit) */
4771     case 0x41: /* MSUB (64bit) */
4772     case 0x4a: /* UMADDL */
4773     case 0x4b: /* UMSUBL */
4774     case 0x4c: /* UMULH */
4775         break;
4776     default:
4777         unallocated_encoding(s);
4778         return;
4779     }
4780 
4781     if (is_high) {
4782         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4783         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4784         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4785         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4786 
4787         if (is_signed) {
4788             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4789         } else {
4790             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4791         }
4792         return;
4793     }
4794 
4795     tcg_op1 = tcg_temp_new_i64();
4796     tcg_op2 = tcg_temp_new_i64();
4797     tcg_tmp = tcg_temp_new_i64();
4798 
4799     if (op_id < 0x42) {
4800         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4801         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4802     } else {
4803         if (is_signed) {
4804             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4805             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4806         } else {
4807             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4808             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4809         }
4810     }
4811 
4812     if (ra == 31 && !is_sub) {
4813         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4814         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4815     } else {
4816         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4817         if (is_sub) {
4818             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4819         } else {
4820             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4821         }
4822     }
4823 
4824     if (!sf) {
4825         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4826     }
4827 }
4828 
4829 /* Add/subtract (with carry)
4830  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4831  * +--+--+--+------------------------+------+-------------+------+-----+
4832  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4833  * +--+--+--+------------------------+------+-------------+------+-----+
4834  */
4835 
4836 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4837 {
4838     unsigned int sf, op, setflags, rm, rn, rd;
4839     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4840 
4841     sf = extract32(insn, 31, 1);
4842     op = extract32(insn, 30, 1);
4843     setflags = extract32(insn, 29, 1);
4844     rm = extract32(insn, 16, 5);
4845     rn = extract32(insn, 5, 5);
4846     rd = extract32(insn, 0, 5);
4847 
4848     tcg_rd = cpu_reg(s, rd);
4849     tcg_rn = cpu_reg(s, rn);
4850 
4851     if (op) {
4852         tcg_y = tcg_temp_new_i64();
4853         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4854     } else {
4855         tcg_y = cpu_reg(s, rm);
4856     }
4857 
4858     if (setflags) {
4859         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4860     } else {
4861         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4862     }
4863 }
4864 
4865 /*
4866  * Rotate right into flags
4867  *  31 30 29                21       15          10      5  4      0
4868  * +--+--+--+-----------------+--------+-----------+------+--+------+
4869  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4870  * +--+--+--+-----------------+--------+-----------+------+--+------+
4871  */
4872 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4873 {
4874     int mask = extract32(insn, 0, 4);
4875     int o2 = extract32(insn, 4, 1);
4876     int rn = extract32(insn, 5, 5);
4877     int imm6 = extract32(insn, 15, 6);
4878     int sf_op_s = extract32(insn, 29, 3);
4879     TCGv_i64 tcg_rn;
4880     TCGv_i32 nzcv;
4881 
4882     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4883         unallocated_encoding(s);
4884         return;
4885     }
4886 
4887     tcg_rn = read_cpu_reg(s, rn, 1);
4888     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4889 
4890     nzcv = tcg_temp_new_i32();
4891     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4892 
4893     if (mask & 8) { /* N */
4894         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4895     }
4896     if (mask & 4) { /* Z */
4897         tcg_gen_not_i32(cpu_ZF, nzcv);
4898         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4899     }
4900     if (mask & 2) { /* C */
4901         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4902     }
4903     if (mask & 1) { /* V */
4904         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4905     }
4906 }
4907 
4908 /*
4909  * Evaluate into flags
4910  *  31 30 29                21        15   14        10      5  4      0
4911  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4912  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
4913  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4914  */
4915 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4916 {
4917     int o3_mask = extract32(insn, 0, 5);
4918     int rn = extract32(insn, 5, 5);
4919     int o2 = extract32(insn, 15, 6);
4920     int sz = extract32(insn, 14, 1);
4921     int sf_op_s = extract32(insn, 29, 3);
4922     TCGv_i32 tmp;
4923     int shift;
4924 
4925     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4926         !dc_isar_feature(aa64_condm_4, s)) {
4927         unallocated_encoding(s);
4928         return;
4929     }
4930     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
4931 
4932     tmp = tcg_temp_new_i32();
4933     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4934     tcg_gen_shli_i32(cpu_NF, tmp, shift);
4935     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4936     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4937     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4938 }
4939 
4940 /* Conditional compare (immediate / register)
4941  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
4942  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4943  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
4944  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4945  *        [1]                             y                [0]       [0]
4946  */
4947 static void disas_cc(DisasContext *s, uint32_t insn)
4948 {
4949     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4950     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4951     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4952     DisasCompare c;
4953 
4954     if (!extract32(insn, 29, 1)) {
4955         unallocated_encoding(s);
4956         return;
4957     }
4958     if (insn & (1 << 10 | 1 << 4)) {
4959         unallocated_encoding(s);
4960         return;
4961     }
4962     sf = extract32(insn, 31, 1);
4963     op = extract32(insn, 30, 1);
4964     is_imm = extract32(insn, 11, 1);
4965     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4966     cond = extract32(insn, 12, 4);
4967     rn = extract32(insn, 5, 5);
4968     nzcv = extract32(insn, 0, 4);
4969 
4970     /* Set T0 = !COND.  */
4971     tcg_t0 = tcg_temp_new_i32();
4972     arm_test_cc(&c, cond);
4973     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4974 
4975     /* Load the arguments for the new comparison.  */
4976     if (is_imm) {
4977         tcg_y = tcg_temp_new_i64();
4978         tcg_gen_movi_i64(tcg_y, y);
4979     } else {
4980         tcg_y = cpu_reg(s, y);
4981     }
4982     tcg_rn = cpu_reg(s, rn);
4983 
4984     /* Set the flags for the new comparison.  */
4985     tcg_tmp = tcg_temp_new_i64();
4986     if (op) {
4987         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4988     } else {
4989         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4990     }
4991 
4992     /* If COND was false, force the flags to #nzcv.  Compute two masks
4993      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4994      * For tcg hosts that support ANDC, we can make do with just T1.
4995      * In either case, allow the tcg optimizer to delete any unused mask.
4996      */
4997     tcg_t1 = tcg_temp_new_i32();
4998     tcg_t2 = tcg_temp_new_i32();
4999     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5000     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5001 
5002     if (nzcv & 8) { /* N */
5003         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5004     } else {
5005         if (TCG_TARGET_HAS_andc_i32) {
5006             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5007         } else {
5008             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5009         }
5010     }
5011     if (nzcv & 4) { /* Z */
5012         if (TCG_TARGET_HAS_andc_i32) {
5013             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5014         } else {
5015             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5016         }
5017     } else {
5018         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5019     }
5020     if (nzcv & 2) { /* C */
5021         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5022     } else {
5023         if (TCG_TARGET_HAS_andc_i32) {
5024             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5025         } else {
5026             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5027         }
5028     }
5029     if (nzcv & 1) { /* V */
5030         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5031     } else {
5032         if (TCG_TARGET_HAS_andc_i32) {
5033             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5034         } else {
5035             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5036         }
5037     }
5038 }
5039 
5040 /* Conditional select
5041  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5042  * +----+----+---+-----------------+------+------+-----+------+------+
5043  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5044  * +----+----+---+-----------------+------+------+-----+------+------+
5045  */
5046 static void disas_cond_select(DisasContext *s, uint32_t insn)
5047 {
5048     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5049     TCGv_i64 tcg_rd, zero;
5050     DisasCompare64 c;
5051 
5052     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5053         /* S == 1 or op2<1> == 1 */
5054         unallocated_encoding(s);
5055         return;
5056     }
5057     sf = extract32(insn, 31, 1);
5058     else_inv = extract32(insn, 30, 1);
5059     rm = extract32(insn, 16, 5);
5060     cond = extract32(insn, 12, 4);
5061     else_inc = extract32(insn, 10, 1);
5062     rn = extract32(insn, 5, 5);
5063     rd = extract32(insn, 0, 5);
5064 
5065     tcg_rd = cpu_reg(s, rd);
5066 
5067     a64_test_cc(&c, cond);
5068     zero = tcg_constant_i64(0);
5069 
5070     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5071         /* CSET & CSETM.  */
5072         if (else_inv) {
5073             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
5074                                    tcg_rd, c.value, zero);
5075         } else {
5076             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
5077                                 tcg_rd, c.value, zero);
5078         }
5079     } else {
5080         TCGv_i64 t_true = cpu_reg(s, rn);
5081         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5082         if (else_inv && else_inc) {
5083             tcg_gen_neg_i64(t_false, t_false);
5084         } else if (else_inv) {
5085             tcg_gen_not_i64(t_false, t_false);
5086         } else if (else_inc) {
5087             tcg_gen_addi_i64(t_false, t_false, 1);
5088         }
5089         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5090     }
5091 
5092     if (!sf) {
5093         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5094     }
5095 }
5096 
5097 static void handle_clz(DisasContext *s, unsigned int sf,
5098                        unsigned int rn, unsigned int rd)
5099 {
5100     TCGv_i64 tcg_rd, tcg_rn;
5101     tcg_rd = cpu_reg(s, rd);
5102     tcg_rn = cpu_reg(s, rn);
5103 
5104     if (sf) {
5105         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5106     } else {
5107         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5108         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5109         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5110         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5111     }
5112 }
5113 
5114 static void handle_cls(DisasContext *s, unsigned int sf,
5115                        unsigned int rn, unsigned int rd)
5116 {
5117     TCGv_i64 tcg_rd, tcg_rn;
5118     tcg_rd = cpu_reg(s, rd);
5119     tcg_rn = cpu_reg(s, rn);
5120 
5121     if (sf) {
5122         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5123     } else {
5124         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5125         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5126         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5127         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5128     }
5129 }
5130 
5131 static void handle_rbit(DisasContext *s, unsigned int sf,
5132                         unsigned int rn, unsigned int rd)
5133 {
5134     TCGv_i64 tcg_rd, tcg_rn;
5135     tcg_rd = cpu_reg(s, rd);
5136     tcg_rn = cpu_reg(s, rn);
5137 
5138     if (sf) {
5139         gen_helper_rbit64(tcg_rd, tcg_rn);
5140     } else {
5141         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5142         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5143         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5144         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5145     }
5146 }
5147 
5148 /* REV with sf==1, opcode==3 ("REV64") */
5149 static void handle_rev64(DisasContext *s, unsigned int sf,
5150                          unsigned int rn, unsigned int rd)
5151 {
5152     if (!sf) {
5153         unallocated_encoding(s);
5154         return;
5155     }
5156     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5157 }
5158 
5159 /* REV with sf==0, opcode==2
5160  * REV32 (sf==1, opcode==2)
5161  */
5162 static void handle_rev32(DisasContext *s, unsigned int sf,
5163                          unsigned int rn, unsigned int rd)
5164 {
5165     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5166     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5167 
5168     if (sf) {
5169         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5170         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5171     } else {
5172         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5173     }
5174 }
5175 
5176 /* REV16 (opcode==1) */
5177 static void handle_rev16(DisasContext *s, unsigned int sf,
5178                          unsigned int rn, unsigned int rd)
5179 {
5180     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5181     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5182     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5183     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5184 
5185     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5186     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5187     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5188     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5189     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5190 }
5191 
5192 /* Data-processing (1 source)
5193  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5194  * +----+---+---+-----------------+---------+--------+------+------+
5195  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5196  * +----+---+---+-----------------+---------+--------+------+------+
5197  */
5198 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5199 {
5200     unsigned int sf, opcode, opcode2, rn, rd;
5201     TCGv_i64 tcg_rd;
5202 
5203     if (extract32(insn, 29, 1)) {
5204         unallocated_encoding(s);
5205         return;
5206     }
5207 
5208     sf = extract32(insn, 31, 1);
5209     opcode = extract32(insn, 10, 6);
5210     opcode2 = extract32(insn, 16, 5);
5211     rn = extract32(insn, 5, 5);
5212     rd = extract32(insn, 0, 5);
5213 
5214 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5215 
5216     switch (MAP(sf, opcode2, opcode)) {
5217     case MAP(0, 0x00, 0x00): /* RBIT */
5218     case MAP(1, 0x00, 0x00):
5219         handle_rbit(s, sf, rn, rd);
5220         break;
5221     case MAP(0, 0x00, 0x01): /* REV16 */
5222     case MAP(1, 0x00, 0x01):
5223         handle_rev16(s, sf, rn, rd);
5224         break;
5225     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5226     case MAP(1, 0x00, 0x02):
5227         handle_rev32(s, sf, rn, rd);
5228         break;
5229     case MAP(1, 0x00, 0x03): /* REV64 */
5230         handle_rev64(s, sf, rn, rd);
5231         break;
5232     case MAP(0, 0x00, 0x04): /* CLZ */
5233     case MAP(1, 0x00, 0x04):
5234         handle_clz(s, sf, rn, rd);
5235         break;
5236     case MAP(0, 0x00, 0x05): /* CLS */
5237     case MAP(1, 0x00, 0x05):
5238         handle_cls(s, sf, rn, rd);
5239         break;
5240     case MAP(1, 0x01, 0x00): /* PACIA */
5241         if (s->pauth_active) {
5242             tcg_rd = cpu_reg(s, rd);
5243             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5244         } else if (!dc_isar_feature(aa64_pauth, s)) {
5245             goto do_unallocated;
5246         }
5247         break;
5248     case MAP(1, 0x01, 0x01): /* PACIB */
5249         if (s->pauth_active) {
5250             tcg_rd = cpu_reg(s, rd);
5251             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5252         } else if (!dc_isar_feature(aa64_pauth, s)) {
5253             goto do_unallocated;
5254         }
5255         break;
5256     case MAP(1, 0x01, 0x02): /* PACDA */
5257         if (s->pauth_active) {
5258             tcg_rd = cpu_reg(s, rd);
5259             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5260         } else if (!dc_isar_feature(aa64_pauth, s)) {
5261             goto do_unallocated;
5262         }
5263         break;
5264     case MAP(1, 0x01, 0x03): /* PACDB */
5265         if (s->pauth_active) {
5266             tcg_rd = cpu_reg(s, rd);
5267             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5268         } else if (!dc_isar_feature(aa64_pauth, s)) {
5269             goto do_unallocated;
5270         }
5271         break;
5272     case MAP(1, 0x01, 0x04): /* AUTIA */
5273         if (s->pauth_active) {
5274             tcg_rd = cpu_reg(s, rd);
5275             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5276         } else if (!dc_isar_feature(aa64_pauth, s)) {
5277             goto do_unallocated;
5278         }
5279         break;
5280     case MAP(1, 0x01, 0x05): /* AUTIB */
5281         if (s->pauth_active) {
5282             tcg_rd = cpu_reg(s, rd);
5283             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5284         } else if (!dc_isar_feature(aa64_pauth, s)) {
5285             goto do_unallocated;
5286         }
5287         break;
5288     case MAP(1, 0x01, 0x06): /* AUTDA */
5289         if (s->pauth_active) {
5290             tcg_rd = cpu_reg(s, rd);
5291             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5292         } else if (!dc_isar_feature(aa64_pauth, s)) {
5293             goto do_unallocated;
5294         }
5295         break;
5296     case MAP(1, 0x01, 0x07): /* AUTDB */
5297         if (s->pauth_active) {
5298             tcg_rd = cpu_reg(s, rd);
5299             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5300         } else if (!dc_isar_feature(aa64_pauth, s)) {
5301             goto do_unallocated;
5302         }
5303         break;
5304     case MAP(1, 0x01, 0x08): /* PACIZA */
5305         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5306             goto do_unallocated;
5307         } else if (s->pauth_active) {
5308             tcg_rd = cpu_reg(s, rd);
5309             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5310         }
5311         break;
5312     case MAP(1, 0x01, 0x09): /* PACIZB */
5313         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5314             goto do_unallocated;
5315         } else if (s->pauth_active) {
5316             tcg_rd = cpu_reg(s, rd);
5317             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5318         }
5319         break;
5320     case MAP(1, 0x01, 0x0a): /* PACDZA */
5321         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5322             goto do_unallocated;
5323         } else if (s->pauth_active) {
5324             tcg_rd = cpu_reg(s, rd);
5325             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5326         }
5327         break;
5328     case MAP(1, 0x01, 0x0b): /* PACDZB */
5329         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5330             goto do_unallocated;
5331         } else if (s->pauth_active) {
5332             tcg_rd = cpu_reg(s, rd);
5333             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5334         }
5335         break;
5336     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5337         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5338             goto do_unallocated;
5339         } else if (s->pauth_active) {
5340             tcg_rd = cpu_reg(s, rd);
5341             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5342         }
5343         break;
5344     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5345         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5346             goto do_unallocated;
5347         } else if (s->pauth_active) {
5348             tcg_rd = cpu_reg(s, rd);
5349             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5350         }
5351         break;
5352     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5353         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5354             goto do_unallocated;
5355         } else if (s->pauth_active) {
5356             tcg_rd = cpu_reg(s, rd);
5357             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5358         }
5359         break;
5360     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5361         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5362             goto do_unallocated;
5363         } else if (s->pauth_active) {
5364             tcg_rd = cpu_reg(s, rd);
5365             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5366         }
5367         break;
5368     case MAP(1, 0x01, 0x10): /* XPACI */
5369         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5370             goto do_unallocated;
5371         } else if (s->pauth_active) {
5372             tcg_rd = cpu_reg(s, rd);
5373             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
5374         }
5375         break;
5376     case MAP(1, 0x01, 0x11): /* XPACD */
5377         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5378             goto do_unallocated;
5379         } else if (s->pauth_active) {
5380             tcg_rd = cpu_reg(s, rd);
5381             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
5382         }
5383         break;
5384     default:
5385     do_unallocated:
5386         unallocated_encoding(s);
5387         break;
5388     }
5389 
5390 #undef MAP
5391 }
5392 
5393 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5394                        unsigned int rm, unsigned int rn, unsigned int rd)
5395 {
5396     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5397     tcg_rd = cpu_reg(s, rd);
5398 
5399     if (!sf && is_signed) {
5400         tcg_n = tcg_temp_new_i64();
5401         tcg_m = tcg_temp_new_i64();
5402         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5403         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5404     } else {
5405         tcg_n = read_cpu_reg(s, rn, sf);
5406         tcg_m = read_cpu_reg(s, rm, sf);
5407     }
5408 
5409     if (is_signed) {
5410         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5411     } else {
5412         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5413     }
5414 
5415     if (!sf) { /* zero extend final result */
5416         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5417     }
5418 }
5419 
5420 /* LSLV, LSRV, ASRV, RORV */
5421 static void handle_shift_reg(DisasContext *s,
5422                              enum a64_shift_type shift_type, unsigned int sf,
5423                              unsigned int rm, unsigned int rn, unsigned int rd)
5424 {
5425     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5426     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5427     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5428 
5429     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5430     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5431 }
5432 
5433 /* CRC32[BHWX], CRC32C[BHWX] */
5434 static void handle_crc32(DisasContext *s,
5435                          unsigned int sf, unsigned int sz, bool crc32c,
5436                          unsigned int rm, unsigned int rn, unsigned int rd)
5437 {
5438     TCGv_i64 tcg_acc, tcg_val;
5439     TCGv_i32 tcg_bytes;
5440 
5441     if (!dc_isar_feature(aa64_crc32, s)
5442         || (sf == 1 && sz != 3)
5443         || (sf == 0 && sz == 3)) {
5444         unallocated_encoding(s);
5445         return;
5446     }
5447 
5448     if (sz == 3) {
5449         tcg_val = cpu_reg(s, rm);
5450     } else {
5451         uint64_t mask;
5452         switch (sz) {
5453         case 0:
5454             mask = 0xFF;
5455             break;
5456         case 1:
5457             mask = 0xFFFF;
5458             break;
5459         case 2:
5460             mask = 0xFFFFFFFF;
5461             break;
5462         default:
5463             g_assert_not_reached();
5464         }
5465         tcg_val = tcg_temp_new_i64();
5466         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5467     }
5468 
5469     tcg_acc = cpu_reg(s, rn);
5470     tcg_bytes = tcg_constant_i32(1 << sz);
5471 
5472     if (crc32c) {
5473         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5474     } else {
5475         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5476     }
5477 }
5478 
5479 /* Data-processing (2 source)
5480  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5481  * +----+---+---+-----------------+------+--------+------+------+
5482  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5483  * +----+---+---+-----------------+------+--------+------+------+
5484  */
5485 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5486 {
5487     unsigned int sf, rm, opcode, rn, rd, setflag;
5488     sf = extract32(insn, 31, 1);
5489     setflag = extract32(insn, 29, 1);
5490     rm = extract32(insn, 16, 5);
5491     opcode = extract32(insn, 10, 6);
5492     rn = extract32(insn, 5, 5);
5493     rd = extract32(insn, 0, 5);
5494 
5495     if (setflag && opcode != 0) {
5496         unallocated_encoding(s);
5497         return;
5498     }
5499 
5500     switch (opcode) {
5501     case 0: /* SUBP(S) */
5502         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5503             goto do_unallocated;
5504         } else {
5505             TCGv_i64 tcg_n, tcg_m, tcg_d;
5506 
5507             tcg_n = read_cpu_reg_sp(s, rn, true);
5508             tcg_m = read_cpu_reg_sp(s, rm, true);
5509             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5510             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5511             tcg_d = cpu_reg(s, rd);
5512 
5513             if (setflag) {
5514                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5515             } else {
5516                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5517             }
5518         }
5519         break;
5520     case 2: /* UDIV */
5521         handle_div(s, false, sf, rm, rn, rd);
5522         break;
5523     case 3: /* SDIV */
5524         handle_div(s, true, sf, rm, rn, rd);
5525         break;
5526     case 4: /* IRG */
5527         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5528             goto do_unallocated;
5529         }
5530         if (s->ata[0]) {
5531             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
5532                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5533         } else {
5534             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5535                                              cpu_reg_sp(s, rn));
5536         }
5537         break;
5538     case 5: /* GMI */
5539         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5540             goto do_unallocated;
5541         } else {
5542             TCGv_i64 t = tcg_temp_new_i64();
5543 
5544             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5545             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5546             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5547         }
5548         break;
5549     case 8: /* LSLV */
5550         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5551         break;
5552     case 9: /* LSRV */
5553         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5554         break;
5555     case 10: /* ASRV */
5556         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5557         break;
5558     case 11: /* RORV */
5559         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5560         break;
5561     case 12: /* PACGA */
5562         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5563             goto do_unallocated;
5564         }
5565         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
5566                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5567         break;
5568     case 16:
5569     case 17:
5570     case 18:
5571     case 19:
5572     case 20:
5573     case 21:
5574     case 22:
5575     case 23: /* CRC32 */
5576     {
5577         int sz = extract32(opcode, 0, 2);
5578         bool crc32c = extract32(opcode, 2, 1);
5579         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5580         break;
5581     }
5582     default:
5583     do_unallocated:
5584         unallocated_encoding(s);
5585         break;
5586     }
5587 }
5588 
5589 /*
5590  * Data processing - register
5591  *  31  30 29  28      25    21  20  16      10         0
5592  * +--+---+--+---+-------+-----+-------+-------+---------+
5593  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5594  * +--+---+--+---+-------+-----+-------+-------+---------+
5595  */
5596 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5597 {
5598     int op0 = extract32(insn, 30, 1);
5599     int op1 = extract32(insn, 28, 1);
5600     int op2 = extract32(insn, 21, 4);
5601     int op3 = extract32(insn, 10, 6);
5602 
5603     if (!op1) {
5604         if (op2 & 8) {
5605             if (op2 & 1) {
5606                 /* Add/sub (extended register) */
5607                 disas_add_sub_ext_reg(s, insn);
5608             } else {
5609                 /* Add/sub (shifted register) */
5610                 disas_add_sub_reg(s, insn);
5611             }
5612         } else {
5613             /* Logical (shifted register) */
5614             disas_logic_reg(s, insn);
5615         }
5616         return;
5617     }
5618 
5619     switch (op2) {
5620     case 0x0:
5621         switch (op3) {
5622         case 0x00: /* Add/subtract (with carry) */
5623             disas_adc_sbc(s, insn);
5624             break;
5625 
5626         case 0x01: /* Rotate right into flags */
5627         case 0x21:
5628             disas_rotate_right_into_flags(s, insn);
5629             break;
5630 
5631         case 0x02: /* Evaluate into flags */
5632         case 0x12:
5633         case 0x22:
5634         case 0x32:
5635             disas_evaluate_into_flags(s, insn);
5636             break;
5637 
5638         default:
5639             goto do_unallocated;
5640         }
5641         break;
5642 
5643     case 0x2: /* Conditional compare */
5644         disas_cc(s, insn); /* both imm and reg forms */
5645         break;
5646 
5647     case 0x4: /* Conditional select */
5648         disas_cond_select(s, insn);
5649         break;
5650 
5651     case 0x6: /* Data-processing */
5652         if (op0) {    /* (1 source) */
5653             disas_data_proc_1src(s, insn);
5654         } else {      /* (2 source) */
5655             disas_data_proc_2src(s, insn);
5656         }
5657         break;
5658     case 0x8 ... 0xf: /* (3 source) */
5659         disas_data_proc_3src(s, insn);
5660         break;
5661 
5662     default:
5663     do_unallocated:
5664         unallocated_encoding(s);
5665         break;
5666     }
5667 }
5668 
5669 static void handle_fp_compare(DisasContext *s, int size,
5670                               unsigned int rn, unsigned int rm,
5671                               bool cmp_with_zero, bool signal_all_nans)
5672 {
5673     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5674     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5675 
5676     if (size == MO_64) {
5677         TCGv_i64 tcg_vn, tcg_vm;
5678 
5679         tcg_vn = read_fp_dreg(s, rn);
5680         if (cmp_with_zero) {
5681             tcg_vm = tcg_constant_i64(0);
5682         } else {
5683             tcg_vm = read_fp_dreg(s, rm);
5684         }
5685         if (signal_all_nans) {
5686             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5687         } else {
5688             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5689         }
5690     } else {
5691         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5692         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5693 
5694         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5695         if (cmp_with_zero) {
5696             tcg_gen_movi_i32(tcg_vm, 0);
5697         } else {
5698             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5699         }
5700 
5701         switch (size) {
5702         case MO_32:
5703             if (signal_all_nans) {
5704                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5705             } else {
5706                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5707             }
5708             break;
5709         case MO_16:
5710             if (signal_all_nans) {
5711                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5712             } else {
5713                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5714             }
5715             break;
5716         default:
5717             g_assert_not_reached();
5718         }
5719     }
5720 
5721     gen_set_nzcv(tcg_flags);
5722 }
5723 
5724 /* Floating point compare
5725  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5726  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5727  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5728  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5729  */
5730 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5731 {
5732     unsigned int mos, type, rm, op, rn, opc, op2r;
5733     int size;
5734 
5735     mos = extract32(insn, 29, 3);
5736     type = extract32(insn, 22, 2);
5737     rm = extract32(insn, 16, 5);
5738     op = extract32(insn, 14, 2);
5739     rn = extract32(insn, 5, 5);
5740     opc = extract32(insn, 3, 2);
5741     op2r = extract32(insn, 0, 3);
5742 
5743     if (mos || op || op2r) {
5744         unallocated_encoding(s);
5745         return;
5746     }
5747 
5748     switch (type) {
5749     case 0:
5750         size = MO_32;
5751         break;
5752     case 1:
5753         size = MO_64;
5754         break;
5755     case 3:
5756         size = MO_16;
5757         if (dc_isar_feature(aa64_fp16, s)) {
5758             break;
5759         }
5760         /* fallthru */
5761     default:
5762         unallocated_encoding(s);
5763         return;
5764     }
5765 
5766     if (!fp_access_check(s)) {
5767         return;
5768     }
5769 
5770     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5771 }
5772 
5773 /* Floating point conditional compare
5774  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5775  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5776  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5777  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5778  */
5779 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5780 {
5781     unsigned int mos, type, rm, cond, rn, op, nzcv;
5782     TCGLabel *label_continue = NULL;
5783     int size;
5784 
5785     mos = extract32(insn, 29, 3);
5786     type = extract32(insn, 22, 2);
5787     rm = extract32(insn, 16, 5);
5788     cond = extract32(insn, 12, 4);
5789     rn = extract32(insn, 5, 5);
5790     op = extract32(insn, 4, 1);
5791     nzcv = extract32(insn, 0, 4);
5792 
5793     if (mos) {
5794         unallocated_encoding(s);
5795         return;
5796     }
5797 
5798     switch (type) {
5799     case 0:
5800         size = MO_32;
5801         break;
5802     case 1:
5803         size = MO_64;
5804         break;
5805     case 3:
5806         size = MO_16;
5807         if (dc_isar_feature(aa64_fp16, s)) {
5808             break;
5809         }
5810         /* fallthru */
5811     default:
5812         unallocated_encoding(s);
5813         return;
5814     }
5815 
5816     if (!fp_access_check(s)) {
5817         return;
5818     }
5819 
5820     if (cond < 0x0e) { /* not always */
5821         TCGLabel *label_match = gen_new_label();
5822         label_continue = gen_new_label();
5823         arm_gen_test_cc(cond, label_match);
5824         /* nomatch: */
5825         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5826         tcg_gen_br(label_continue);
5827         gen_set_label(label_match);
5828     }
5829 
5830     handle_fp_compare(s, size, rn, rm, false, op);
5831 
5832     if (cond < 0x0e) {
5833         gen_set_label(label_continue);
5834     }
5835 }
5836 
5837 /* Floating point conditional select
5838  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5839  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5840  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5841  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5842  */
5843 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5844 {
5845     unsigned int mos, type, rm, cond, rn, rd;
5846     TCGv_i64 t_true, t_false;
5847     DisasCompare64 c;
5848     MemOp sz;
5849 
5850     mos = extract32(insn, 29, 3);
5851     type = extract32(insn, 22, 2);
5852     rm = extract32(insn, 16, 5);
5853     cond = extract32(insn, 12, 4);
5854     rn = extract32(insn, 5, 5);
5855     rd = extract32(insn, 0, 5);
5856 
5857     if (mos) {
5858         unallocated_encoding(s);
5859         return;
5860     }
5861 
5862     switch (type) {
5863     case 0:
5864         sz = MO_32;
5865         break;
5866     case 1:
5867         sz = MO_64;
5868         break;
5869     case 3:
5870         sz = MO_16;
5871         if (dc_isar_feature(aa64_fp16, s)) {
5872             break;
5873         }
5874         /* fallthru */
5875     default:
5876         unallocated_encoding(s);
5877         return;
5878     }
5879 
5880     if (!fp_access_check(s)) {
5881         return;
5882     }
5883 
5884     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5885     t_true = tcg_temp_new_i64();
5886     t_false = tcg_temp_new_i64();
5887     read_vec_element(s, t_true, rn, 0, sz);
5888     read_vec_element(s, t_false, rm, 0, sz);
5889 
5890     a64_test_cc(&c, cond);
5891     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5892                         t_true, t_false);
5893 
5894     /* Note that sregs & hregs write back zeros to the high bits,
5895        and we've already done the zero-extension.  */
5896     write_fp_dreg(s, rd, t_true);
5897 }
5898 
5899 /* Floating-point data-processing (1 source) - half precision */
5900 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5901 {
5902     TCGv_ptr fpst = NULL;
5903     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5904     TCGv_i32 tcg_res = tcg_temp_new_i32();
5905 
5906     switch (opcode) {
5907     case 0x0: /* FMOV */
5908         tcg_gen_mov_i32(tcg_res, tcg_op);
5909         break;
5910     case 0x1: /* FABS */
5911         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5912         break;
5913     case 0x2: /* FNEG */
5914         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5915         break;
5916     case 0x3: /* FSQRT */
5917         fpst = fpstatus_ptr(FPST_FPCR_F16);
5918         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5919         break;
5920     case 0x8: /* FRINTN */
5921     case 0x9: /* FRINTP */
5922     case 0xa: /* FRINTM */
5923     case 0xb: /* FRINTZ */
5924     case 0xc: /* FRINTA */
5925     {
5926         TCGv_i32 tcg_rmode;
5927 
5928         fpst = fpstatus_ptr(FPST_FPCR_F16);
5929         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
5930         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5931         gen_restore_rmode(tcg_rmode, fpst);
5932         break;
5933     }
5934     case 0xe: /* FRINTX */
5935         fpst = fpstatus_ptr(FPST_FPCR_F16);
5936         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5937         break;
5938     case 0xf: /* FRINTI */
5939         fpst = fpstatus_ptr(FPST_FPCR_F16);
5940         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5941         break;
5942     default:
5943         g_assert_not_reached();
5944     }
5945 
5946     write_fp_sreg(s, rd, tcg_res);
5947 }
5948 
5949 /* Floating-point data-processing (1 source) - single precision */
5950 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5951 {
5952     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5953     TCGv_i32 tcg_op, tcg_res;
5954     TCGv_ptr fpst;
5955     int rmode = -1;
5956 
5957     tcg_op = read_fp_sreg(s, rn);
5958     tcg_res = tcg_temp_new_i32();
5959 
5960     switch (opcode) {
5961     case 0x0: /* FMOV */
5962         tcg_gen_mov_i32(tcg_res, tcg_op);
5963         goto done;
5964     case 0x1: /* FABS */
5965         gen_helper_vfp_abss(tcg_res, tcg_op);
5966         goto done;
5967     case 0x2: /* FNEG */
5968         gen_helper_vfp_negs(tcg_res, tcg_op);
5969         goto done;
5970     case 0x3: /* FSQRT */
5971         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
5972         goto done;
5973     case 0x6: /* BFCVT */
5974         gen_fpst = gen_helper_bfcvt;
5975         break;
5976     case 0x8: /* FRINTN */
5977     case 0x9: /* FRINTP */
5978     case 0xa: /* FRINTM */
5979     case 0xb: /* FRINTZ */
5980     case 0xc: /* FRINTA */
5981         rmode = opcode & 7;
5982         gen_fpst = gen_helper_rints;
5983         break;
5984     case 0xe: /* FRINTX */
5985         gen_fpst = gen_helper_rints_exact;
5986         break;
5987     case 0xf: /* FRINTI */
5988         gen_fpst = gen_helper_rints;
5989         break;
5990     case 0x10: /* FRINT32Z */
5991         rmode = FPROUNDING_ZERO;
5992         gen_fpst = gen_helper_frint32_s;
5993         break;
5994     case 0x11: /* FRINT32X */
5995         gen_fpst = gen_helper_frint32_s;
5996         break;
5997     case 0x12: /* FRINT64Z */
5998         rmode = FPROUNDING_ZERO;
5999         gen_fpst = gen_helper_frint64_s;
6000         break;
6001     case 0x13: /* FRINT64X */
6002         gen_fpst = gen_helper_frint64_s;
6003         break;
6004     default:
6005         g_assert_not_reached();
6006     }
6007 
6008     fpst = fpstatus_ptr(FPST_FPCR);
6009     if (rmode >= 0) {
6010         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6011         gen_fpst(tcg_res, tcg_op, fpst);
6012         gen_restore_rmode(tcg_rmode, fpst);
6013     } else {
6014         gen_fpst(tcg_res, tcg_op, fpst);
6015     }
6016 
6017  done:
6018     write_fp_sreg(s, rd, tcg_res);
6019 }
6020 
6021 /* Floating-point data-processing (1 source) - double precision */
6022 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6023 {
6024     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6025     TCGv_i64 tcg_op, tcg_res;
6026     TCGv_ptr fpst;
6027     int rmode = -1;
6028 
6029     switch (opcode) {
6030     case 0x0: /* FMOV */
6031         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6032         return;
6033     }
6034 
6035     tcg_op = read_fp_dreg(s, rn);
6036     tcg_res = tcg_temp_new_i64();
6037 
6038     switch (opcode) {
6039     case 0x1: /* FABS */
6040         gen_helper_vfp_absd(tcg_res, tcg_op);
6041         goto done;
6042     case 0x2: /* FNEG */
6043         gen_helper_vfp_negd(tcg_res, tcg_op);
6044         goto done;
6045     case 0x3: /* FSQRT */
6046         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
6047         goto done;
6048     case 0x8: /* FRINTN */
6049     case 0x9: /* FRINTP */
6050     case 0xa: /* FRINTM */
6051     case 0xb: /* FRINTZ */
6052     case 0xc: /* FRINTA */
6053         rmode = opcode & 7;
6054         gen_fpst = gen_helper_rintd;
6055         break;
6056     case 0xe: /* FRINTX */
6057         gen_fpst = gen_helper_rintd_exact;
6058         break;
6059     case 0xf: /* FRINTI */
6060         gen_fpst = gen_helper_rintd;
6061         break;
6062     case 0x10: /* FRINT32Z */
6063         rmode = FPROUNDING_ZERO;
6064         gen_fpst = gen_helper_frint32_d;
6065         break;
6066     case 0x11: /* FRINT32X */
6067         gen_fpst = gen_helper_frint32_d;
6068         break;
6069     case 0x12: /* FRINT64Z */
6070         rmode = FPROUNDING_ZERO;
6071         gen_fpst = gen_helper_frint64_d;
6072         break;
6073     case 0x13: /* FRINT64X */
6074         gen_fpst = gen_helper_frint64_d;
6075         break;
6076     default:
6077         g_assert_not_reached();
6078     }
6079 
6080     fpst = fpstatus_ptr(FPST_FPCR);
6081     if (rmode >= 0) {
6082         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6083         gen_fpst(tcg_res, tcg_op, fpst);
6084         gen_restore_rmode(tcg_rmode, fpst);
6085     } else {
6086         gen_fpst(tcg_res, tcg_op, fpst);
6087     }
6088 
6089  done:
6090     write_fp_dreg(s, rd, tcg_res);
6091 }
6092 
6093 static void handle_fp_fcvt(DisasContext *s, int opcode,
6094                            int rd, int rn, int dtype, int ntype)
6095 {
6096     switch (ntype) {
6097     case 0x0:
6098     {
6099         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6100         if (dtype == 1) {
6101             /* Single to double */
6102             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6103             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
6104             write_fp_dreg(s, rd, tcg_rd);
6105         } else {
6106             /* Single to half */
6107             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6108             TCGv_i32 ahp = get_ahp_flag();
6109             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6110 
6111             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6112             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6113             write_fp_sreg(s, rd, tcg_rd);
6114         }
6115         break;
6116     }
6117     case 0x1:
6118     {
6119         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6120         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6121         if (dtype == 0) {
6122             /* Double to single */
6123             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
6124         } else {
6125             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6126             TCGv_i32 ahp = get_ahp_flag();
6127             /* Double to half */
6128             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6129             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6130         }
6131         write_fp_sreg(s, rd, tcg_rd);
6132         break;
6133     }
6134     case 0x3:
6135     {
6136         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6137         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6138         TCGv_i32 tcg_ahp = get_ahp_flag();
6139         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6140         if (dtype == 0) {
6141             /* Half to single */
6142             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6143             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6144             write_fp_sreg(s, rd, tcg_rd);
6145         } else {
6146             /* Half to double */
6147             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6148             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6149             write_fp_dreg(s, rd, tcg_rd);
6150         }
6151         break;
6152     }
6153     default:
6154         g_assert_not_reached();
6155     }
6156 }
6157 
6158 /* Floating point data-processing (1 source)
6159  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6160  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6161  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6162  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6163  */
6164 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6165 {
6166     int mos = extract32(insn, 29, 3);
6167     int type = extract32(insn, 22, 2);
6168     int opcode = extract32(insn, 15, 6);
6169     int rn = extract32(insn, 5, 5);
6170     int rd = extract32(insn, 0, 5);
6171 
6172     if (mos) {
6173         goto do_unallocated;
6174     }
6175 
6176     switch (opcode) {
6177     case 0x4: case 0x5: case 0x7:
6178     {
6179         /* FCVT between half, single and double precision */
6180         int dtype = extract32(opcode, 0, 2);
6181         if (type == 2 || dtype == type) {
6182             goto do_unallocated;
6183         }
6184         if (!fp_access_check(s)) {
6185             return;
6186         }
6187 
6188         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6189         break;
6190     }
6191 
6192     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6193         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6194             goto do_unallocated;
6195         }
6196         /* fall through */
6197     case 0x0 ... 0x3:
6198     case 0x8 ... 0xc:
6199     case 0xe ... 0xf:
6200         /* 32-to-32 and 64-to-64 ops */
6201         switch (type) {
6202         case 0:
6203             if (!fp_access_check(s)) {
6204                 return;
6205             }
6206             handle_fp_1src_single(s, opcode, rd, rn);
6207             break;
6208         case 1:
6209             if (!fp_access_check(s)) {
6210                 return;
6211             }
6212             handle_fp_1src_double(s, opcode, rd, rn);
6213             break;
6214         case 3:
6215             if (!dc_isar_feature(aa64_fp16, s)) {
6216                 goto do_unallocated;
6217             }
6218 
6219             if (!fp_access_check(s)) {
6220                 return;
6221             }
6222             handle_fp_1src_half(s, opcode, rd, rn);
6223             break;
6224         default:
6225             goto do_unallocated;
6226         }
6227         break;
6228 
6229     case 0x6:
6230         switch (type) {
6231         case 1: /* BFCVT */
6232             if (!dc_isar_feature(aa64_bf16, s)) {
6233                 goto do_unallocated;
6234             }
6235             if (!fp_access_check(s)) {
6236                 return;
6237             }
6238             handle_fp_1src_single(s, opcode, rd, rn);
6239             break;
6240         default:
6241             goto do_unallocated;
6242         }
6243         break;
6244 
6245     default:
6246     do_unallocated:
6247         unallocated_encoding(s);
6248         break;
6249     }
6250 }
6251 
6252 /* Floating-point data-processing (2 source) - single precision */
6253 static void handle_fp_2src_single(DisasContext *s, int opcode,
6254                                   int rd, int rn, int rm)
6255 {
6256     TCGv_i32 tcg_op1;
6257     TCGv_i32 tcg_op2;
6258     TCGv_i32 tcg_res;
6259     TCGv_ptr fpst;
6260 
6261     tcg_res = tcg_temp_new_i32();
6262     fpst = fpstatus_ptr(FPST_FPCR);
6263     tcg_op1 = read_fp_sreg(s, rn);
6264     tcg_op2 = read_fp_sreg(s, rm);
6265 
6266     switch (opcode) {
6267     case 0x0: /* FMUL */
6268         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6269         break;
6270     case 0x1: /* FDIV */
6271         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6272         break;
6273     case 0x2: /* FADD */
6274         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6275         break;
6276     case 0x3: /* FSUB */
6277         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6278         break;
6279     case 0x4: /* FMAX */
6280         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6281         break;
6282     case 0x5: /* FMIN */
6283         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6284         break;
6285     case 0x6: /* FMAXNM */
6286         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6287         break;
6288     case 0x7: /* FMINNM */
6289         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6290         break;
6291     case 0x8: /* FNMUL */
6292         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6293         gen_helper_vfp_negs(tcg_res, tcg_res);
6294         break;
6295     }
6296 
6297     write_fp_sreg(s, rd, tcg_res);
6298 }
6299 
6300 /* Floating-point data-processing (2 source) - double precision */
6301 static void handle_fp_2src_double(DisasContext *s, int opcode,
6302                                   int rd, int rn, int rm)
6303 {
6304     TCGv_i64 tcg_op1;
6305     TCGv_i64 tcg_op2;
6306     TCGv_i64 tcg_res;
6307     TCGv_ptr fpst;
6308 
6309     tcg_res = tcg_temp_new_i64();
6310     fpst = fpstatus_ptr(FPST_FPCR);
6311     tcg_op1 = read_fp_dreg(s, rn);
6312     tcg_op2 = read_fp_dreg(s, rm);
6313 
6314     switch (opcode) {
6315     case 0x0: /* FMUL */
6316         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6317         break;
6318     case 0x1: /* FDIV */
6319         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6320         break;
6321     case 0x2: /* FADD */
6322         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6323         break;
6324     case 0x3: /* FSUB */
6325         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6326         break;
6327     case 0x4: /* FMAX */
6328         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6329         break;
6330     case 0x5: /* FMIN */
6331         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6332         break;
6333     case 0x6: /* FMAXNM */
6334         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6335         break;
6336     case 0x7: /* FMINNM */
6337         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6338         break;
6339     case 0x8: /* FNMUL */
6340         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6341         gen_helper_vfp_negd(tcg_res, tcg_res);
6342         break;
6343     }
6344 
6345     write_fp_dreg(s, rd, tcg_res);
6346 }
6347 
6348 /* Floating-point data-processing (2 source) - half precision */
6349 static void handle_fp_2src_half(DisasContext *s, int opcode,
6350                                 int rd, int rn, int rm)
6351 {
6352     TCGv_i32 tcg_op1;
6353     TCGv_i32 tcg_op2;
6354     TCGv_i32 tcg_res;
6355     TCGv_ptr fpst;
6356 
6357     tcg_res = tcg_temp_new_i32();
6358     fpst = fpstatus_ptr(FPST_FPCR_F16);
6359     tcg_op1 = read_fp_hreg(s, rn);
6360     tcg_op2 = read_fp_hreg(s, rm);
6361 
6362     switch (opcode) {
6363     case 0x0: /* FMUL */
6364         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6365         break;
6366     case 0x1: /* FDIV */
6367         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6368         break;
6369     case 0x2: /* FADD */
6370         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6371         break;
6372     case 0x3: /* FSUB */
6373         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6374         break;
6375     case 0x4: /* FMAX */
6376         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6377         break;
6378     case 0x5: /* FMIN */
6379         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6380         break;
6381     case 0x6: /* FMAXNM */
6382         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6383         break;
6384     case 0x7: /* FMINNM */
6385         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6386         break;
6387     case 0x8: /* FNMUL */
6388         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6389         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6390         break;
6391     default:
6392         g_assert_not_reached();
6393     }
6394 
6395     write_fp_sreg(s, rd, tcg_res);
6396 }
6397 
6398 /* Floating point data-processing (2 source)
6399  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6400  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6401  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6402  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6403  */
6404 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6405 {
6406     int mos = extract32(insn, 29, 3);
6407     int type = extract32(insn, 22, 2);
6408     int rd = extract32(insn, 0, 5);
6409     int rn = extract32(insn, 5, 5);
6410     int rm = extract32(insn, 16, 5);
6411     int opcode = extract32(insn, 12, 4);
6412 
6413     if (opcode > 8 || mos) {
6414         unallocated_encoding(s);
6415         return;
6416     }
6417 
6418     switch (type) {
6419     case 0:
6420         if (!fp_access_check(s)) {
6421             return;
6422         }
6423         handle_fp_2src_single(s, opcode, rd, rn, rm);
6424         break;
6425     case 1:
6426         if (!fp_access_check(s)) {
6427             return;
6428         }
6429         handle_fp_2src_double(s, opcode, rd, rn, rm);
6430         break;
6431     case 3:
6432         if (!dc_isar_feature(aa64_fp16, s)) {
6433             unallocated_encoding(s);
6434             return;
6435         }
6436         if (!fp_access_check(s)) {
6437             return;
6438         }
6439         handle_fp_2src_half(s, opcode, rd, rn, rm);
6440         break;
6441     default:
6442         unallocated_encoding(s);
6443     }
6444 }
6445 
6446 /* Floating-point data-processing (3 source) - single precision */
6447 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6448                                   int rd, int rn, int rm, int ra)
6449 {
6450     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6451     TCGv_i32 tcg_res = tcg_temp_new_i32();
6452     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6453 
6454     tcg_op1 = read_fp_sreg(s, rn);
6455     tcg_op2 = read_fp_sreg(s, rm);
6456     tcg_op3 = read_fp_sreg(s, ra);
6457 
6458     /* These are fused multiply-add, and must be done as one
6459      * floating point operation with no rounding between the
6460      * multiplication and addition steps.
6461      * NB that doing the negations here as separate steps is
6462      * correct : an input NaN should come out with its sign bit
6463      * flipped if it is a negated-input.
6464      */
6465     if (o1 == true) {
6466         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6467     }
6468 
6469     if (o0 != o1) {
6470         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6471     }
6472 
6473     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6474 
6475     write_fp_sreg(s, rd, tcg_res);
6476 }
6477 
6478 /* Floating-point data-processing (3 source) - double precision */
6479 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6480                                   int rd, int rn, int rm, int ra)
6481 {
6482     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6483     TCGv_i64 tcg_res = tcg_temp_new_i64();
6484     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6485 
6486     tcg_op1 = read_fp_dreg(s, rn);
6487     tcg_op2 = read_fp_dreg(s, rm);
6488     tcg_op3 = read_fp_dreg(s, ra);
6489 
6490     /* These are fused multiply-add, and must be done as one
6491      * floating point operation with no rounding between the
6492      * multiplication and addition steps.
6493      * NB that doing the negations here as separate steps is
6494      * correct : an input NaN should come out with its sign bit
6495      * flipped if it is a negated-input.
6496      */
6497     if (o1 == true) {
6498         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6499     }
6500 
6501     if (o0 != o1) {
6502         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6503     }
6504 
6505     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6506 
6507     write_fp_dreg(s, rd, tcg_res);
6508 }
6509 
6510 /* Floating-point data-processing (3 source) - half precision */
6511 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6512                                 int rd, int rn, int rm, int ra)
6513 {
6514     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6515     TCGv_i32 tcg_res = tcg_temp_new_i32();
6516     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6517 
6518     tcg_op1 = read_fp_hreg(s, rn);
6519     tcg_op2 = read_fp_hreg(s, rm);
6520     tcg_op3 = read_fp_hreg(s, ra);
6521 
6522     /* These are fused multiply-add, and must be done as one
6523      * floating point operation with no rounding between the
6524      * multiplication and addition steps.
6525      * NB that doing the negations here as separate steps is
6526      * correct : an input NaN should come out with its sign bit
6527      * flipped if it is a negated-input.
6528      */
6529     if (o1 == true) {
6530         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6531     }
6532 
6533     if (o0 != o1) {
6534         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6535     }
6536 
6537     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6538 
6539     write_fp_sreg(s, rd, tcg_res);
6540 }
6541 
6542 /* Floating point data-processing (3 source)
6543  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6544  * +---+---+---+-----------+------+----+------+----+------+------+------+
6545  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6546  * +---+---+---+-----------+------+----+------+----+------+------+------+
6547  */
6548 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6549 {
6550     int mos = extract32(insn, 29, 3);
6551     int type = extract32(insn, 22, 2);
6552     int rd = extract32(insn, 0, 5);
6553     int rn = extract32(insn, 5, 5);
6554     int ra = extract32(insn, 10, 5);
6555     int rm = extract32(insn, 16, 5);
6556     bool o0 = extract32(insn, 15, 1);
6557     bool o1 = extract32(insn, 21, 1);
6558 
6559     if (mos) {
6560         unallocated_encoding(s);
6561         return;
6562     }
6563 
6564     switch (type) {
6565     case 0:
6566         if (!fp_access_check(s)) {
6567             return;
6568         }
6569         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6570         break;
6571     case 1:
6572         if (!fp_access_check(s)) {
6573             return;
6574         }
6575         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6576         break;
6577     case 3:
6578         if (!dc_isar_feature(aa64_fp16, s)) {
6579             unallocated_encoding(s);
6580             return;
6581         }
6582         if (!fp_access_check(s)) {
6583             return;
6584         }
6585         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6586         break;
6587     default:
6588         unallocated_encoding(s);
6589     }
6590 }
6591 
6592 /* Floating point immediate
6593  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6594  * +---+---+---+-----------+------+---+------------+-------+------+------+
6595  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6596  * +---+---+---+-----------+------+---+------------+-------+------+------+
6597  */
6598 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6599 {
6600     int rd = extract32(insn, 0, 5);
6601     int imm5 = extract32(insn, 5, 5);
6602     int imm8 = extract32(insn, 13, 8);
6603     int type = extract32(insn, 22, 2);
6604     int mos = extract32(insn, 29, 3);
6605     uint64_t imm;
6606     MemOp sz;
6607 
6608     if (mos || imm5) {
6609         unallocated_encoding(s);
6610         return;
6611     }
6612 
6613     switch (type) {
6614     case 0:
6615         sz = MO_32;
6616         break;
6617     case 1:
6618         sz = MO_64;
6619         break;
6620     case 3:
6621         sz = MO_16;
6622         if (dc_isar_feature(aa64_fp16, s)) {
6623             break;
6624         }
6625         /* fallthru */
6626     default:
6627         unallocated_encoding(s);
6628         return;
6629     }
6630 
6631     if (!fp_access_check(s)) {
6632         return;
6633     }
6634 
6635     imm = vfp_expand_imm(sz, imm8);
6636     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6637 }
6638 
6639 /* Handle floating point <=> fixed point conversions. Note that we can
6640  * also deal with fp <=> integer conversions as a special case (scale == 64)
6641  * OPTME: consider handling that special case specially or at least skipping
6642  * the call to scalbn in the helpers for zero shifts.
6643  */
6644 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6645                            bool itof, int rmode, int scale, int sf, int type)
6646 {
6647     bool is_signed = !(opcode & 1);
6648     TCGv_ptr tcg_fpstatus;
6649     TCGv_i32 tcg_shift, tcg_single;
6650     TCGv_i64 tcg_double;
6651 
6652     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6653 
6654     tcg_shift = tcg_constant_i32(64 - scale);
6655 
6656     if (itof) {
6657         TCGv_i64 tcg_int = cpu_reg(s, rn);
6658         if (!sf) {
6659             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6660 
6661             if (is_signed) {
6662                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6663             } else {
6664                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6665             }
6666 
6667             tcg_int = tcg_extend;
6668         }
6669 
6670         switch (type) {
6671         case 1: /* float64 */
6672             tcg_double = tcg_temp_new_i64();
6673             if (is_signed) {
6674                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6675                                      tcg_shift, tcg_fpstatus);
6676             } else {
6677                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6678                                      tcg_shift, tcg_fpstatus);
6679             }
6680             write_fp_dreg(s, rd, tcg_double);
6681             break;
6682 
6683         case 0: /* float32 */
6684             tcg_single = tcg_temp_new_i32();
6685             if (is_signed) {
6686                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6687                                      tcg_shift, tcg_fpstatus);
6688             } else {
6689                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6690                                      tcg_shift, tcg_fpstatus);
6691             }
6692             write_fp_sreg(s, rd, tcg_single);
6693             break;
6694 
6695         case 3: /* float16 */
6696             tcg_single = tcg_temp_new_i32();
6697             if (is_signed) {
6698                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6699                                      tcg_shift, tcg_fpstatus);
6700             } else {
6701                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6702                                      tcg_shift, tcg_fpstatus);
6703             }
6704             write_fp_sreg(s, rd, tcg_single);
6705             break;
6706 
6707         default:
6708             g_assert_not_reached();
6709         }
6710     } else {
6711         TCGv_i64 tcg_int = cpu_reg(s, rd);
6712         TCGv_i32 tcg_rmode;
6713 
6714         if (extract32(opcode, 2, 1)) {
6715             /* There are too many rounding modes to all fit into rmode,
6716              * so FCVTA[US] is a special case.
6717              */
6718             rmode = FPROUNDING_TIEAWAY;
6719         }
6720 
6721         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6722 
6723         switch (type) {
6724         case 1: /* float64 */
6725             tcg_double = read_fp_dreg(s, rn);
6726             if (is_signed) {
6727                 if (!sf) {
6728                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6729                                          tcg_shift, tcg_fpstatus);
6730                 } else {
6731                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6732                                          tcg_shift, tcg_fpstatus);
6733                 }
6734             } else {
6735                 if (!sf) {
6736                     gen_helper_vfp_tould(tcg_int, tcg_double,
6737                                          tcg_shift, tcg_fpstatus);
6738                 } else {
6739                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6740                                          tcg_shift, tcg_fpstatus);
6741                 }
6742             }
6743             if (!sf) {
6744                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6745             }
6746             break;
6747 
6748         case 0: /* float32 */
6749             tcg_single = read_fp_sreg(s, rn);
6750             if (sf) {
6751                 if (is_signed) {
6752                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6753                                          tcg_shift, tcg_fpstatus);
6754                 } else {
6755                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6756                                          tcg_shift, tcg_fpstatus);
6757                 }
6758             } else {
6759                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6760                 if (is_signed) {
6761                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6762                                          tcg_shift, tcg_fpstatus);
6763                 } else {
6764                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6765                                          tcg_shift, tcg_fpstatus);
6766                 }
6767                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6768             }
6769             break;
6770 
6771         case 3: /* float16 */
6772             tcg_single = read_fp_sreg(s, rn);
6773             if (sf) {
6774                 if (is_signed) {
6775                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6776                                          tcg_shift, tcg_fpstatus);
6777                 } else {
6778                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6779                                          tcg_shift, tcg_fpstatus);
6780                 }
6781             } else {
6782                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6783                 if (is_signed) {
6784                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6785                                          tcg_shift, tcg_fpstatus);
6786                 } else {
6787                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6788                                          tcg_shift, tcg_fpstatus);
6789                 }
6790                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6791             }
6792             break;
6793 
6794         default:
6795             g_assert_not_reached();
6796         }
6797 
6798         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6799     }
6800 }
6801 
6802 /* Floating point <-> fixed point conversions
6803  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6804  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6805  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6806  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6807  */
6808 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6809 {
6810     int rd = extract32(insn, 0, 5);
6811     int rn = extract32(insn, 5, 5);
6812     int scale = extract32(insn, 10, 6);
6813     int opcode = extract32(insn, 16, 3);
6814     int rmode = extract32(insn, 19, 2);
6815     int type = extract32(insn, 22, 2);
6816     bool sbit = extract32(insn, 29, 1);
6817     bool sf = extract32(insn, 31, 1);
6818     bool itof;
6819 
6820     if (sbit || (!sf && scale < 32)) {
6821         unallocated_encoding(s);
6822         return;
6823     }
6824 
6825     switch (type) {
6826     case 0: /* float32 */
6827     case 1: /* float64 */
6828         break;
6829     case 3: /* float16 */
6830         if (dc_isar_feature(aa64_fp16, s)) {
6831             break;
6832         }
6833         /* fallthru */
6834     default:
6835         unallocated_encoding(s);
6836         return;
6837     }
6838 
6839     switch ((rmode << 3) | opcode) {
6840     case 0x2: /* SCVTF */
6841     case 0x3: /* UCVTF */
6842         itof = true;
6843         break;
6844     case 0x18: /* FCVTZS */
6845     case 0x19: /* FCVTZU */
6846         itof = false;
6847         break;
6848     default:
6849         unallocated_encoding(s);
6850         return;
6851     }
6852 
6853     if (!fp_access_check(s)) {
6854         return;
6855     }
6856 
6857     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6858 }
6859 
6860 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6861 {
6862     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6863      * without conversion.
6864      */
6865 
6866     if (itof) {
6867         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6868         TCGv_i64 tmp;
6869 
6870         switch (type) {
6871         case 0:
6872             /* 32 bit */
6873             tmp = tcg_temp_new_i64();
6874             tcg_gen_ext32u_i64(tmp, tcg_rn);
6875             write_fp_dreg(s, rd, tmp);
6876             break;
6877         case 1:
6878             /* 64 bit */
6879             write_fp_dreg(s, rd, tcg_rn);
6880             break;
6881         case 2:
6882             /* 64 bit to top half. */
6883             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
6884             clear_vec_high(s, true, rd);
6885             break;
6886         case 3:
6887             /* 16 bit */
6888             tmp = tcg_temp_new_i64();
6889             tcg_gen_ext16u_i64(tmp, tcg_rn);
6890             write_fp_dreg(s, rd, tmp);
6891             break;
6892         default:
6893             g_assert_not_reached();
6894         }
6895     } else {
6896         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6897 
6898         switch (type) {
6899         case 0:
6900             /* 32 bit */
6901             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
6902             break;
6903         case 1:
6904             /* 64 bit */
6905             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
6906             break;
6907         case 2:
6908             /* 64 bits from top half */
6909             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
6910             break;
6911         case 3:
6912             /* 16 bit */
6913             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
6914             break;
6915         default:
6916             g_assert_not_reached();
6917         }
6918     }
6919 }
6920 
6921 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6922 {
6923     TCGv_i64 t = read_fp_dreg(s, rn);
6924     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
6925 
6926     gen_helper_fjcvtzs(t, t, fpstatus);
6927 
6928     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6929     tcg_gen_extrh_i64_i32(cpu_ZF, t);
6930     tcg_gen_movi_i32(cpu_CF, 0);
6931     tcg_gen_movi_i32(cpu_NF, 0);
6932     tcg_gen_movi_i32(cpu_VF, 0);
6933 }
6934 
6935 /* Floating point <-> integer conversions
6936  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
6937  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6938  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6939  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6940  */
6941 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6942 {
6943     int rd = extract32(insn, 0, 5);
6944     int rn = extract32(insn, 5, 5);
6945     int opcode = extract32(insn, 16, 3);
6946     int rmode = extract32(insn, 19, 2);
6947     int type = extract32(insn, 22, 2);
6948     bool sbit = extract32(insn, 29, 1);
6949     bool sf = extract32(insn, 31, 1);
6950     bool itof = false;
6951 
6952     if (sbit) {
6953         goto do_unallocated;
6954     }
6955 
6956     switch (opcode) {
6957     case 2: /* SCVTF */
6958     case 3: /* UCVTF */
6959         itof = true;
6960         /* fallthru */
6961     case 4: /* FCVTAS */
6962     case 5: /* FCVTAU */
6963         if (rmode != 0) {
6964             goto do_unallocated;
6965         }
6966         /* fallthru */
6967     case 0: /* FCVT[NPMZ]S */
6968     case 1: /* FCVT[NPMZ]U */
6969         switch (type) {
6970         case 0: /* float32 */
6971         case 1: /* float64 */
6972             break;
6973         case 3: /* float16 */
6974             if (!dc_isar_feature(aa64_fp16, s)) {
6975                 goto do_unallocated;
6976             }
6977             break;
6978         default:
6979             goto do_unallocated;
6980         }
6981         if (!fp_access_check(s)) {
6982             return;
6983         }
6984         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6985         break;
6986 
6987     default:
6988         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6989         case 0b01100110: /* FMOV half <-> 32-bit int */
6990         case 0b01100111:
6991         case 0b11100110: /* FMOV half <-> 64-bit int */
6992         case 0b11100111:
6993             if (!dc_isar_feature(aa64_fp16, s)) {
6994                 goto do_unallocated;
6995             }
6996             /* fallthru */
6997         case 0b00000110: /* FMOV 32-bit */
6998         case 0b00000111:
6999         case 0b10100110: /* FMOV 64-bit */
7000         case 0b10100111:
7001         case 0b11001110: /* FMOV top half of 128-bit */
7002         case 0b11001111:
7003             if (!fp_access_check(s)) {
7004                 return;
7005             }
7006             itof = opcode & 1;
7007             handle_fmov(s, rd, rn, type, itof);
7008             break;
7009 
7010         case 0b00111110: /* FJCVTZS */
7011             if (!dc_isar_feature(aa64_jscvt, s)) {
7012                 goto do_unallocated;
7013             } else if (fp_access_check(s)) {
7014                 handle_fjcvtzs(s, rd, rn);
7015             }
7016             break;
7017 
7018         default:
7019         do_unallocated:
7020             unallocated_encoding(s);
7021             return;
7022         }
7023         break;
7024     }
7025 }
7026 
7027 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7028  *   31  30  29 28     25 24                          0
7029  * +---+---+---+---------+-----------------------------+
7030  * |   | 0 |   | 1 1 1 1 |                             |
7031  * +---+---+---+---------+-----------------------------+
7032  */
7033 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7034 {
7035     if (extract32(insn, 24, 1)) {
7036         /* Floating point data-processing (3 source) */
7037         disas_fp_3src(s, insn);
7038     } else if (extract32(insn, 21, 1) == 0) {
7039         /* Floating point to fixed point conversions */
7040         disas_fp_fixed_conv(s, insn);
7041     } else {
7042         switch (extract32(insn, 10, 2)) {
7043         case 1:
7044             /* Floating point conditional compare */
7045             disas_fp_ccomp(s, insn);
7046             break;
7047         case 2:
7048             /* Floating point data-processing (2 source) */
7049             disas_fp_2src(s, insn);
7050             break;
7051         case 3:
7052             /* Floating point conditional select */
7053             disas_fp_csel(s, insn);
7054             break;
7055         case 0:
7056             switch (ctz32(extract32(insn, 12, 4))) {
7057             case 0: /* [15:12] == xxx1 */
7058                 /* Floating point immediate */
7059                 disas_fp_imm(s, insn);
7060                 break;
7061             case 1: /* [15:12] == xx10 */
7062                 /* Floating point compare */
7063                 disas_fp_compare(s, insn);
7064                 break;
7065             case 2: /* [15:12] == x100 */
7066                 /* Floating point data-processing (1 source) */
7067                 disas_fp_1src(s, insn);
7068                 break;
7069             case 3: /* [15:12] == 1000 */
7070                 unallocated_encoding(s);
7071                 break;
7072             default: /* [15:12] == 0000 */
7073                 /* Floating point <-> integer conversions */
7074                 disas_fp_int_conv(s, insn);
7075                 break;
7076             }
7077             break;
7078         }
7079     }
7080 }
7081 
7082 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7083                      int pos)
7084 {
7085     /* Extract 64 bits from the middle of two concatenated 64 bit
7086      * vector register slices left:right. The extracted bits start
7087      * at 'pos' bits into the right (least significant) side.
7088      * We return the result in tcg_right, and guarantee not to
7089      * trash tcg_left.
7090      */
7091     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7092     assert(pos > 0 && pos < 64);
7093 
7094     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7095     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7096     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7097 }
7098 
7099 /* EXT
7100  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7101  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7102  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7103  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7104  */
7105 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7106 {
7107     int is_q = extract32(insn, 30, 1);
7108     int op2 = extract32(insn, 22, 2);
7109     int imm4 = extract32(insn, 11, 4);
7110     int rm = extract32(insn, 16, 5);
7111     int rn = extract32(insn, 5, 5);
7112     int rd = extract32(insn, 0, 5);
7113     int pos = imm4 << 3;
7114     TCGv_i64 tcg_resl, tcg_resh;
7115 
7116     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7117         unallocated_encoding(s);
7118         return;
7119     }
7120 
7121     if (!fp_access_check(s)) {
7122         return;
7123     }
7124 
7125     tcg_resh = tcg_temp_new_i64();
7126     tcg_resl = tcg_temp_new_i64();
7127 
7128     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7129      * either extracting 128 bits from a 128:128 concatenation, or
7130      * extracting 64 bits from a 64:64 concatenation.
7131      */
7132     if (!is_q) {
7133         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7134         if (pos != 0) {
7135             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7136             do_ext64(s, tcg_resh, tcg_resl, pos);
7137         }
7138     } else {
7139         TCGv_i64 tcg_hh;
7140         typedef struct {
7141             int reg;
7142             int elt;
7143         } EltPosns;
7144         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7145         EltPosns *elt = eltposns;
7146 
7147         if (pos >= 64) {
7148             elt++;
7149             pos -= 64;
7150         }
7151 
7152         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7153         elt++;
7154         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7155         elt++;
7156         if (pos != 0) {
7157             do_ext64(s, tcg_resh, tcg_resl, pos);
7158             tcg_hh = tcg_temp_new_i64();
7159             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7160             do_ext64(s, tcg_hh, tcg_resh, pos);
7161         }
7162     }
7163 
7164     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7165     if (is_q) {
7166         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7167     }
7168     clear_vec_high(s, is_q, rd);
7169 }
7170 
7171 /* TBL/TBX
7172  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7173  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7174  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7175  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7176  */
7177 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7178 {
7179     int op2 = extract32(insn, 22, 2);
7180     int is_q = extract32(insn, 30, 1);
7181     int rm = extract32(insn, 16, 5);
7182     int rn = extract32(insn, 5, 5);
7183     int rd = extract32(insn, 0, 5);
7184     int is_tbx = extract32(insn, 12, 1);
7185     int len = (extract32(insn, 13, 2) + 1) * 16;
7186 
7187     if (op2 != 0) {
7188         unallocated_encoding(s);
7189         return;
7190     }
7191 
7192     if (!fp_access_check(s)) {
7193         return;
7194     }
7195 
7196     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7197                        vec_full_reg_offset(s, rm), tcg_env,
7198                        is_q ? 16 : 8, vec_full_reg_size(s),
7199                        (len << 6) | (is_tbx << 5) | rn,
7200                        gen_helper_simd_tblx);
7201 }
7202 
7203 /* ZIP/UZP/TRN
7204  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7205  * +---+---+-------------+------+---+------+---+------------------+------+
7206  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7207  * +---+---+-------------+------+---+------+---+------------------+------+
7208  */
7209 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7210 {
7211     int rd = extract32(insn, 0, 5);
7212     int rn = extract32(insn, 5, 5);
7213     int rm = extract32(insn, 16, 5);
7214     int size = extract32(insn, 22, 2);
7215     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7216      * bit 2 indicates 1 vs 2 variant of the insn.
7217      */
7218     int opcode = extract32(insn, 12, 2);
7219     bool part = extract32(insn, 14, 1);
7220     bool is_q = extract32(insn, 30, 1);
7221     int esize = 8 << size;
7222     int i;
7223     int datasize = is_q ? 128 : 64;
7224     int elements = datasize / esize;
7225     TCGv_i64 tcg_res[2], tcg_ele;
7226 
7227     if (opcode == 0 || (size == 3 && !is_q)) {
7228         unallocated_encoding(s);
7229         return;
7230     }
7231 
7232     if (!fp_access_check(s)) {
7233         return;
7234     }
7235 
7236     tcg_res[0] = tcg_temp_new_i64();
7237     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7238     tcg_ele = tcg_temp_new_i64();
7239 
7240     for (i = 0; i < elements; i++) {
7241         int o, w;
7242 
7243         switch (opcode) {
7244         case 1: /* UZP1/2 */
7245         {
7246             int midpoint = elements / 2;
7247             if (i < midpoint) {
7248                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7249             } else {
7250                 read_vec_element(s, tcg_ele, rm,
7251                                  2 * (i - midpoint) + part, size);
7252             }
7253             break;
7254         }
7255         case 2: /* TRN1/2 */
7256             if (i & 1) {
7257                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7258             } else {
7259                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7260             }
7261             break;
7262         case 3: /* ZIP1/2 */
7263         {
7264             int base = part * elements / 2;
7265             if (i & 1) {
7266                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7267             } else {
7268                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7269             }
7270             break;
7271         }
7272         default:
7273             g_assert_not_reached();
7274         }
7275 
7276         w = (i * esize) / 64;
7277         o = (i * esize) % 64;
7278         if (o == 0) {
7279             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7280         } else {
7281             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7282             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7283         }
7284     }
7285 
7286     for (i = 0; i <= is_q; ++i) {
7287         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7288     }
7289     clear_vec_high(s, is_q, rd);
7290 }
7291 
7292 /*
7293  * do_reduction_op helper
7294  *
7295  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7296  * important for correct NaN propagation that we do these
7297  * operations in exactly the order specified by the pseudocode.
7298  *
7299  * This is a recursive function, TCG temps should be freed by the
7300  * calling function once it is done with the values.
7301  */
7302 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7303                                 int esize, int size, int vmap, TCGv_ptr fpst)
7304 {
7305     if (esize == size) {
7306         int element;
7307         MemOp msize = esize == 16 ? MO_16 : MO_32;
7308         TCGv_i32 tcg_elem;
7309 
7310         /* We should have one register left here */
7311         assert(ctpop8(vmap) == 1);
7312         element = ctz32(vmap);
7313         assert(element < 8);
7314 
7315         tcg_elem = tcg_temp_new_i32();
7316         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7317         return tcg_elem;
7318     } else {
7319         int bits = size / 2;
7320         int shift = ctpop8(vmap) / 2;
7321         int vmap_lo = (vmap >> shift) & vmap;
7322         int vmap_hi = (vmap & ~vmap_lo);
7323         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7324 
7325         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7326         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7327         tcg_res = tcg_temp_new_i32();
7328 
7329         switch (fpopcode) {
7330         case 0x0c: /* fmaxnmv half-precision */
7331             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7332             break;
7333         case 0x0f: /* fmaxv half-precision */
7334             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7335             break;
7336         case 0x1c: /* fminnmv half-precision */
7337             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7338             break;
7339         case 0x1f: /* fminv half-precision */
7340             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7341             break;
7342         case 0x2c: /* fmaxnmv */
7343             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7344             break;
7345         case 0x2f: /* fmaxv */
7346             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7347             break;
7348         case 0x3c: /* fminnmv */
7349             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7350             break;
7351         case 0x3f: /* fminv */
7352             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7353             break;
7354         default:
7355             g_assert_not_reached();
7356         }
7357         return tcg_res;
7358     }
7359 }
7360 
7361 /* AdvSIMD across lanes
7362  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7363  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7364  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7365  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7366  */
7367 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7368 {
7369     int rd = extract32(insn, 0, 5);
7370     int rn = extract32(insn, 5, 5);
7371     int size = extract32(insn, 22, 2);
7372     int opcode = extract32(insn, 12, 5);
7373     bool is_q = extract32(insn, 30, 1);
7374     bool is_u = extract32(insn, 29, 1);
7375     bool is_fp = false;
7376     bool is_min = false;
7377     int esize;
7378     int elements;
7379     int i;
7380     TCGv_i64 tcg_res, tcg_elt;
7381 
7382     switch (opcode) {
7383     case 0x1b: /* ADDV */
7384         if (is_u) {
7385             unallocated_encoding(s);
7386             return;
7387         }
7388         /* fall through */
7389     case 0x3: /* SADDLV, UADDLV */
7390     case 0xa: /* SMAXV, UMAXV */
7391     case 0x1a: /* SMINV, UMINV */
7392         if (size == 3 || (size == 2 && !is_q)) {
7393             unallocated_encoding(s);
7394             return;
7395         }
7396         break;
7397     case 0xc: /* FMAXNMV, FMINNMV */
7398     case 0xf: /* FMAXV, FMINV */
7399         /* Bit 1 of size field encodes min vs max and the actual size
7400          * depends on the encoding of the U bit. If not set (and FP16
7401          * enabled) then we do half-precision float instead of single
7402          * precision.
7403          */
7404         is_min = extract32(size, 1, 1);
7405         is_fp = true;
7406         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7407             size = 1;
7408         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7409             unallocated_encoding(s);
7410             return;
7411         } else {
7412             size = 2;
7413         }
7414         break;
7415     default:
7416         unallocated_encoding(s);
7417         return;
7418     }
7419 
7420     if (!fp_access_check(s)) {
7421         return;
7422     }
7423 
7424     esize = 8 << size;
7425     elements = (is_q ? 128 : 64) / esize;
7426 
7427     tcg_res = tcg_temp_new_i64();
7428     tcg_elt = tcg_temp_new_i64();
7429 
7430     /* These instructions operate across all lanes of a vector
7431      * to produce a single result. We can guarantee that a 64
7432      * bit intermediate is sufficient:
7433      *  + for [US]ADDLV the maximum element size is 32 bits, and
7434      *    the result type is 64 bits
7435      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7436      *    same as the element size, which is 32 bits at most
7437      * For the integer operations we can choose to work at 64
7438      * or 32 bits and truncate at the end; for simplicity
7439      * we use 64 bits always. The floating point
7440      * ops do require 32 bit intermediates, though.
7441      */
7442     if (!is_fp) {
7443         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7444 
7445         for (i = 1; i < elements; i++) {
7446             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7447 
7448             switch (opcode) {
7449             case 0x03: /* SADDLV / UADDLV */
7450             case 0x1b: /* ADDV */
7451                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7452                 break;
7453             case 0x0a: /* SMAXV / UMAXV */
7454                 if (is_u) {
7455                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7456                 } else {
7457                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7458                 }
7459                 break;
7460             case 0x1a: /* SMINV / UMINV */
7461                 if (is_u) {
7462                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7463                 } else {
7464                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7465                 }
7466                 break;
7467             default:
7468                 g_assert_not_reached();
7469             }
7470 
7471         }
7472     } else {
7473         /* Floating point vector reduction ops which work across 32
7474          * bit (single) or 16 bit (half-precision) intermediates.
7475          * Note that correct NaN propagation requires that we do these
7476          * operations in exactly the order specified by the pseudocode.
7477          */
7478         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7479         int fpopcode = opcode | is_min << 4 | is_u << 5;
7480         int vmap = (1 << elements) - 1;
7481         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7482                                              (is_q ? 128 : 64), vmap, fpst);
7483         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7484     }
7485 
7486     /* Now truncate the result to the width required for the final output */
7487     if (opcode == 0x03) {
7488         /* SADDLV, UADDLV: result is 2*esize */
7489         size++;
7490     }
7491 
7492     switch (size) {
7493     case 0:
7494         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7495         break;
7496     case 1:
7497         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7498         break;
7499     case 2:
7500         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7501         break;
7502     case 3:
7503         break;
7504     default:
7505         g_assert_not_reached();
7506     }
7507 
7508     write_fp_dreg(s, rd, tcg_res);
7509 }
7510 
7511 /* DUP (Element, Vector)
7512  *
7513  *  31  30   29              21 20    16 15        10  9    5 4    0
7514  * +---+---+-------------------+--------+-------------+------+------+
7515  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7516  * +---+---+-------------------+--------+-------------+------+------+
7517  *
7518  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7519  */
7520 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7521                              int imm5)
7522 {
7523     int size = ctz32(imm5);
7524     int index;
7525 
7526     if (size > 3 || (size == 3 && !is_q)) {
7527         unallocated_encoding(s);
7528         return;
7529     }
7530 
7531     if (!fp_access_check(s)) {
7532         return;
7533     }
7534 
7535     index = imm5 >> (size + 1);
7536     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7537                          vec_reg_offset(s, rn, index, size),
7538                          is_q ? 16 : 8, vec_full_reg_size(s));
7539 }
7540 
7541 /* DUP (element, scalar)
7542  *  31                   21 20    16 15        10  9    5 4    0
7543  * +-----------------------+--------+-------------+------+------+
7544  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7545  * +-----------------------+--------+-------------+------+------+
7546  */
7547 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7548                               int imm5)
7549 {
7550     int size = ctz32(imm5);
7551     int index;
7552     TCGv_i64 tmp;
7553 
7554     if (size > 3) {
7555         unallocated_encoding(s);
7556         return;
7557     }
7558 
7559     if (!fp_access_check(s)) {
7560         return;
7561     }
7562 
7563     index = imm5 >> (size + 1);
7564 
7565     /* This instruction just extracts the specified element and
7566      * zero-extends it into the bottom of the destination register.
7567      */
7568     tmp = tcg_temp_new_i64();
7569     read_vec_element(s, tmp, rn, index, size);
7570     write_fp_dreg(s, rd, tmp);
7571 }
7572 
7573 /* DUP (General)
7574  *
7575  *  31  30   29              21 20    16 15        10  9    5 4    0
7576  * +---+---+-------------------+--------+-------------+------+------+
7577  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7578  * +---+---+-------------------+--------+-------------+------+------+
7579  *
7580  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7581  */
7582 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7583                              int imm5)
7584 {
7585     int size = ctz32(imm5);
7586     uint32_t dofs, oprsz, maxsz;
7587 
7588     if (size > 3 || ((size == 3) && !is_q)) {
7589         unallocated_encoding(s);
7590         return;
7591     }
7592 
7593     if (!fp_access_check(s)) {
7594         return;
7595     }
7596 
7597     dofs = vec_full_reg_offset(s, rd);
7598     oprsz = is_q ? 16 : 8;
7599     maxsz = vec_full_reg_size(s);
7600 
7601     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7602 }
7603 
7604 /* INS (Element)
7605  *
7606  *  31                   21 20    16 15  14    11  10 9    5 4    0
7607  * +-----------------------+--------+------------+---+------+------+
7608  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7609  * +-----------------------+--------+------------+---+------+------+
7610  *
7611  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7612  * index: encoded in imm5<4:size+1>
7613  */
7614 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7615                              int imm4, int imm5)
7616 {
7617     int size = ctz32(imm5);
7618     int src_index, dst_index;
7619     TCGv_i64 tmp;
7620 
7621     if (size > 3) {
7622         unallocated_encoding(s);
7623         return;
7624     }
7625 
7626     if (!fp_access_check(s)) {
7627         return;
7628     }
7629 
7630     dst_index = extract32(imm5, 1+size, 5);
7631     src_index = extract32(imm4, size, 4);
7632 
7633     tmp = tcg_temp_new_i64();
7634 
7635     read_vec_element(s, tmp, rn, src_index, size);
7636     write_vec_element(s, tmp, rd, dst_index, size);
7637 
7638     /* INS is considered a 128-bit write for SVE. */
7639     clear_vec_high(s, true, rd);
7640 }
7641 
7642 
7643 /* INS (General)
7644  *
7645  *  31                   21 20    16 15        10  9    5 4    0
7646  * +-----------------------+--------+-------------+------+------+
7647  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7648  * +-----------------------+--------+-------------+------+------+
7649  *
7650  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7651  * index: encoded in imm5<4:size+1>
7652  */
7653 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7654 {
7655     int size = ctz32(imm5);
7656     int idx;
7657 
7658     if (size > 3) {
7659         unallocated_encoding(s);
7660         return;
7661     }
7662 
7663     if (!fp_access_check(s)) {
7664         return;
7665     }
7666 
7667     idx = extract32(imm5, 1 + size, 4 - size);
7668     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7669 
7670     /* INS is considered a 128-bit write for SVE. */
7671     clear_vec_high(s, true, rd);
7672 }
7673 
7674 /*
7675  * UMOV (General)
7676  * SMOV (General)
7677  *
7678  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7679  * +---+---+-------------------+--------+-------------+------+------+
7680  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7681  * +---+---+-------------------+--------+-------------+------+------+
7682  *
7683  * U: unsigned when set
7684  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7685  */
7686 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7687                                   int rn, int rd, int imm5)
7688 {
7689     int size = ctz32(imm5);
7690     int element;
7691     TCGv_i64 tcg_rd;
7692 
7693     /* Check for UnallocatedEncodings */
7694     if (is_signed) {
7695         if (size > 2 || (size == 2 && !is_q)) {
7696             unallocated_encoding(s);
7697             return;
7698         }
7699     } else {
7700         if (size > 3
7701             || (size < 3 && is_q)
7702             || (size == 3 && !is_q)) {
7703             unallocated_encoding(s);
7704             return;
7705         }
7706     }
7707 
7708     if (!fp_access_check(s)) {
7709         return;
7710     }
7711 
7712     element = extract32(imm5, 1+size, 4);
7713 
7714     tcg_rd = cpu_reg(s, rd);
7715     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7716     if (is_signed && !is_q) {
7717         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7718     }
7719 }
7720 
7721 /* AdvSIMD copy
7722  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7723  * +---+---+----+-----------------+------+---+------+---+------+------+
7724  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7725  * +---+---+----+-----------------+------+---+------+---+------+------+
7726  */
7727 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7728 {
7729     int rd = extract32(insn, 0, 5);
7730     int rn = extract32(insn, 5, 5);
7731     int imm4 = extract32(insn, 11, 4);
7732     int op = extract32(insn, 29, 1);
7733     int is_q = extract32(insn, 30, 1);
7734     int imm5 = extract32(insn, 16, 5);
7735 
7736     if (op) {
7737         if (is_q) {
7738             /* INS (element) */
7739             handle_simd_inse(s, rd, rn, imm4, imm5);
7740         } else {
7741             unallocated_encoding(s);
7742         }
7743     } else {
7744         switch (imm4) {
7745         case 0:
7746             /* DUP (element - vector) */
7747             handle_simd_dupe(s, is_q, rd, rn, imm5);
7748             break;
7749         case 1:
7750             /* DUP (general) */
7751             handle_simd_dupg(s, is_q, rd, rn, imm5);
7752             break;
7753         case 3:
7754             if (is_q) {
7755                 /* INS (general) */
7756                 handle_simd_insg(s, rd, rn, imm5);
7757             } else {
7758                 unallocated_encoding(s);
7759             }
7760             break;
7761         case 5:
7762         case 7:
7763             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7764             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7765             break;
7766         default:
7767             unallocated_encoding(s);
7768             break;
7769         }
7770     }
7771 }
7772 
7773 /* AdvSIMD modified immediate
7774  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7775  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7776  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7777  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7778  *
7779  * There are a number of operations that can be carried out here:
7780  *   MOVI - move (shifted) imm into register
7781  *   MVNI - move inverted (shifted) imm into register
7782  *   ORR  - bitwise OR of (shifted) imm with register
7783  *   BIC  - bitwise clear of (shifted) imm with register
7784  * With ARMv8.2 we also have:
7785  *   FMOV half-precision
7786  */
7787 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7788 {
7789     int rd = extract32(insn, 0, 5);
7790     int cmode = extract32(insn, 12, 4);
7791     int o2 = extract32(insn, 11, 1);
7792     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7793     bool is_neg = extract32(insn, 29, 1);
7794     bool is_q = extract32(insn, 30, 1);
7795     uint64_t imm = 0;
7796 
7797     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7798         /* Check for FMOV (vector, immediate) - half-precision */
7799         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7800             unallocated_encoding(s);
7801             return;
7802         }
7803     }
7804 
7805     if (!fp_access_check(s)) {
7806         return;
7807     }
7808 
7809     if (cmode == 15 && o2 && !is_neg) {
7810         /* FMOV (vector, immediate) - half-precision */
7811         imm = vfp_expand_imm(MO_16, abcdefgh);
7812         /* now duplicate across the lanes */
7813         imm = dup_const(MO_16, imm);
7814     } else {
7815         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7816     }
7817 
7818     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7819         /* MOVI or MVNI, with MVNI negation handled above.  */
7820         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7821                              vec_full_reg_size(s), imm);
7822     } else {
7823         /* ORR or BIC, with BIC negation to AND handled above.  */
7824         if (is_neg) {
7825             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7826         } else {
7827             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7828         }
7829     }
7830 }
7831 
7832 /* AdvSIMD scalar copy
7833  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7834  * +-----+----+-----------------+------+---+------+---+------+------+
7835  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7836  * +-----+----+-----------------+------+---+------+---+------+------+
7837  */
7838 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7839 {
7840     int rd = extract32(insn, 0, 5);
7841     int rn = extract32(insn, 5, 5);
7842     int imm4 = extract32(insn, 11, 4);
7843     int imm5 = extract32(insn, 16, 5);
7844     int op = extract32(insn, 29, 1);
7845 
7846     if (op != 0 || imm4 != 0) {
7847         unallocated_encoding(s);
7848         return;
7849     }
7850 
7851     /* DUP (element, scalar) */
7852     handle_simd_dupes(s, rd, rn, imm5);
7853 }
7854 
7855 /* AdvSIMD scalar pairwise
7856  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7857  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7858  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7859  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7860  */
7861 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7862 {
7863     int u = extract32(insn, 29, 1);
7864     int size = extract32(insn, 22, 2);
7865     int opcode = extract32(insn, 12, 5);
7866     int rn = extract32(insn, 5, 5);
7867     int rd = extract32(insn, 0, 5);
7868     TCGv_ptr fpst;
7869 
7870     /* For some ops (the FP ones), size[1] is part of the encoding.
7871      * For ADDP strictly it is not but size[1] is always 1 for valid
7872      * encodings.
7873      */
7874     opcode |= (extract32(size, 1, 1) << 5);
7875 
7876     switch (opcode) {
7877     case 0x3b: /* ADDP */
7878         if (u || size != 3) {
7879             unallocated_encoding(s);
7880             return;
7881         }
7882         if (!fp_access_check(s)) {
7883             return;
7884         }
7885 
7886         fpst = NULL;
7887         break;
7888     case 0xc: /* FMAXNMP */
7889     case 0xd: /* FADDP */
7890     case 0xf: /* FMAXP */
7891     case 0x2c: /* FMINNMP */
7892     case 0x2f: /* FMINP */
7893         /* FP op, size[0] is 32 or 64 bit*/
7894         if (!u) {
7895             if (!dc_isar_feature(aa64_fp16, s)) {
7896                 unallocated_encoding(s);
7897                 return;
7898             } else {
7899                 size = MO_16;
7900             }
7901         } else {
7902             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7903         }
7904 
7905         if (!fp_access_check(s)) {
7906             return;
7907         }
7908 
7909         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7910         break;
7911     default:
7912         unallocated_encoding(s);
7913         return;
7914     }
7915 
7916     if (size == MO_64) {
7917         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7918         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7919         TCGv_i64 tcg_res = tcg_temp_new_i64();
7920 
7921         read_vec_element(s, tcg_op1, rn, 0, MO_64);
7922         read_vec_element(s, tcg_op2, rn, 1, MO_64);
7923 
7924         switch (opcode) {
7925         case 0x3b: /* ADDP */
7926             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7927             break;
7928         case 0xc: /* FMAXNMP */
7929             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7930             break;
7931         case 0xd: /* FADDP */
7932             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7933             break;
7934         case 0xf: /* FMAXP */
7935             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7936             break;
7937         case 0x2c: /* FMINNMP */
7938             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7939             break;
7940         case 0x2f: /* FMINP */
7941             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7942             break;
7943         default:
7944             g_assert_not_reached();
7945         }
7946 
7947         write_fp_dreg(s, rd, tcg_res);
7948     } else {
7949         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7950         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7951         TCGv_i32 tcg_res = tcg_temp_new_i32();
7952 
7953         read_vec_element_i32(s, tcg_op1, rn, 0, size);
7954         read_vec_element_i32(s, tcg_op2, rn, 1, size);
7955 
7956         if (size == MO_16) {
7957             switch (opcode) {
7958             case 0xc: /* FMAXNMP */
7959                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7960                 break;
7961             case 0xd: /* FADDP */
7962                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7963                 break;
7964             case 0xf: /* FMAXP */
7965                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7966                 break;
7967             case 0x2c: /* FMINNMP */
7968                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7969                 break;
7970             case 0x2f: /* FMINP */
7971                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7972                 break;
7973             default:
7974                 g_assert_not_reached();
7975             }
7976         } else {
7977             switch (opcode) {
7978             case 0xc: /* FMAXNMP */
7979                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7980                 break;
7981             case 0xd: /* FADDP */
7982                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7983                 break;
7984             case 0xf: /* FMAXP */
7985                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7986                 break;
7987             case 0x2c: /* FMINNMP */
7988                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7989                 break;
7990             case 0x2f: /* FMINP */
7991                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7992                 break;
7993             default:
7994                 g_assert_not_reached();
7995             }
7996         }
7997 
7998         write_fp_sreg(s, rd, tcg_res);
7999     }
8000 }
8001 
8002 /*
8003  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8004  *
8005  * This code is handles the common shifting code and is used by both
8006  * the vector and scalar code.
8007  */
8008 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8009                                     TCGv_i64 tcg_rnd, bool accumulate,
8010                                     bool is_u, int size, int shift)
8011 {
8012     bool extended_result = false;
8013     bool round = tcg_rnd != NULL;
8014     int ext_lshift = 0;
8015     TCGv_i64 tcg_src_hi;
8016 
8017     if (round && size == 3) {
8018         extended_result = true;
8019         ext_lshift = 64 - shift;
8020         tcg_src_hi = tcg_temp_new_i64();
8021     } else if (shift == 64) {
8022         if (!accumulate && is_u) {
8023             /* result is zero */
8024             tcg_gen_movi_i64(tcg_res, 0);
8025             return;
8026         }
8027     }
8028 
8029     /* Deal with the rounding step */
8030     if (round) {
8031         if (extended_result) {
8032             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8033             if (!is_u) {
8034                 /* take care of sign extending tcg_res */
8035                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8036                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8037                                  tcg_src, tcg_src_hi,
8038                                  tcg_rnd, tcg_zero);
8039             } else {
8040                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8041                                  tcg_src, tcg_zero,
8042                                  tcg_rnd, tcg_zero);
8043             }
8044         } else {
8045             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8046         }
8047     }
8048 
8049     /* Now do the shift right */
8050     if (round && extended_result) {
8051         /* extended case, >64 bit precision required */
8052         if (ext_lshift == 0) {
8053             /* special case, only high bits matter */
8054             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8055         } else {
8056             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8057             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8058             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8059         }
8060     } else {
8061         if (is_u) {
8062             if (shift == 64) {
8063                 /* essentially shifting in 64 zeros */
8064                 tcg_gen_movi_i64(tcg_src, 0);
8065             } else {
8066                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8067             }
8068         } else {
8069             if (shift == 64) {
8070                 /* effectively extending the sign-bit */
8071                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8072             } else {
8073                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8074             }
8075         }
8076     }
8077 
8078     if (accumulate) {
8079         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8080     } else {
8081         tcg_gen_mov_i64(tcg_res, tcg_src);
8082     }
8083 }
8084 
8085 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8086 static void handle_scalar_simd_shri(DisasContext *s,
8087                                     bool is_u, int immh, int immb,
8088                                     int opcode, int rn, int rd)
8089 {
8090     const int size = 3;
8091     int immhb = immh << 3 | immb;
8092     int shift = 2 * (8 << size) - immhb;
8093     bool accumulate = false;
8094     bool round = false;
8095     bool insert = false;
8096     TCGv_i64 tcg_rn;
8097     TCGv_i64 tcg_rd;
8098     TCGv_i64 tcg_round;
8099 
8100     if (!extract32(immh, 3, 1)) {
8101         unallocated_encoding(s);
8102         return;
8103     }
8104 
8105     if (!fp_access_check(s)) {
8106         return;
8107     }
8108 
8109     switch (opcode) {
8110     case 0x02: /* SSRA / USRA (accumulate) */
8111         accumulate = true;
8112         break;
8113     case 0x04: /* SRSHR / URSHR (rounding) */
8114         round = true;
8115         break;
8116     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8117         accumulate = round = true;
8118         break;
8119     case 0x08: /* SRI */
8120         insert = true;
8121         break;
8122     }
8123 
8124     if (round) {
8125         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8126     } else {
8127         tcg_round = NULL;
8128     }
8129 
8130     tcg_rn = read_fp_dreg(s, rn);
8131     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8132 
8133     if (insert) {
8134         /* shift count same as element size is valid but does nothing;
8135          * special case to avoid potential shift by 64.
8136          */
8137         int esize = 8 << size;
8138         if (shift != esize) {
8139             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8140             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8141         }
8142     } else {
8143         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8144                                 accumulate, is_u, size, shift);
8145     }
8146 
8147     write_fp_dreg(s, rd, tcg_rd);
8148 }
8149 
8150 /* SHL/SLI - Scalar shift left */
8151 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8152                                     int immh, int immb, int opcode,
8153                                     int rn, int rd)
8154 {
8155     int size = 32 - clz32(immh) - 1;
8156     int immhb = immh << 3 | immb;
8157     int shift = immhb - (8 << size);
8158     TCGv_i64 tcg_rn;
8159     TCGv_i64 tcg_rd;
8160 
8161     if (!extract32(immh, 3, 1)) {
8162         unallocated_encoding(s);
8163         return;
8164     }
8165 
8166     if (!fp_access_check(s)) {
8167         return;
8168     }
8169 
8170     tcg_rn = read_fp_dreg(s, rn);
8171     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8172 
8173     if (insert) {
8174         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8175     } else {
8176         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8177     }
8178 
8179     write_fp_dreg(s, rd, tcg_rd);
8180 }
8181 
8182 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8183  * (signed/unsigned) narrowing */
8184 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8185                                    bool is_u_shift, bool is_u_narrow,
8186                                    int immh, int immb, int opcode,
8187                                    int rn, int rd)
8188 {
8189     int immhb = immh << 3 | immb;
8190     int size = 32 - clz32(immh) - 1;
8191     int esize = 8 << size;
8192     int shift = (2 * esize) - immhb;
8193     int elements = is_scalar ? 1 : (64 / esize);
8194     bool round = extract32(opcode, 0, 1);
8195     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8196     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8197     TCGv_i32 tcg_rd_narrowed;
8198     TCGv_i64 tcg_final;
8199 
8200     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8201         { gen_helper_neon_narrow_sat_s8,
8202           gen_helper_neon_unarrow_sat8 },
8203         { gen_helper_neon_narrow_sat_s16,
8204           gen_helper_neon_unarrow_sat16 },
8205         { gen_helper_neon_narrow_sat_s32,
8206           gen_helper_neon_unarrow_sat32 },
8207         { NULL, NULL },
8208     };
8209     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8210         gen_helper_neon_narrow_sat_u8,
8211         gen_helper_neon_narrow_sat_u16,
8212         gen_helper_neon_narrow_sat_u32,
8213         NULL
8214     };
8215     NeonGenNarrowEnvFn *narrowfn;
8216 
8217     int i;
8218 
8219     assert(size < 4);
8220 
8221     if (extract32(immh, 3, 1)) {
8222         unallocated_encoding(s);
8223         return;
8224     }
8225 
8226     if (!fp_access_check(s)) {
8227         return;
8228     }
8229 
8230     if (is_u_shift) {
8231         narrowfn = unsigned_narrow_fns[size];
8232     } else {
8233         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8234     }
8235 
8236     tcg_rn = tcg_temp_new_i64();
8237     tcg_rd = tcg_temp_new_i64();
8238     tcg_rd_narrowed = tcg_temp_new_i32();
8239     tcg_final = tcg_temp_new_i64();
8240 
8241     if (round) {
8242         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8243     } else {
8244         tcg_round = NULL;
8245     }
8246 
8247     for (i = 0; i < elements; i++) {
8248         read_vec_element(s, tcg_rn, rn, i, ldop);
8249         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8250                                 false, is_u_shift, size+1, shift);
8251         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8252         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8253         if (i == 0) {
8254             tcg_gen_mov_i64(tcg_final, tcg_rd);
8255         } else {
8256             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8257         }
8258     }
8259 
8260     if (!is_q) {
8261         write_vec_element(s, tcg_final, rd, 0, MO_64);
8262     } else {
8263         write_vec_element(s, tcg_final, rd, 1, MO_64);
8264     }
8265     clear_vec_high(s, is_q, rd);
8266 }
8267 
8268 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8269 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8270                              bool src_unsigned, bool dst_unsigned,
8271                              int immh, int immb, int rn, int rd)
8272 {
8273     int immhb = immh << 3 | immb;
8274     int size = 32 - clz32(immh) - 1;
8275     int shift = immhb - (8 << size);
8276     int pass;
8277 
8278     assert(immh != 0);
8279     assert(!(scalar && is_q));
8280 
8281     if (!scalar) {
8282         if (!is_q && extract32(immh, 3, 1)) {
8283             unallocated_encoding(s);
8284             return;
8285         }
8286 
8287         /* Since we use the variable-shift helpers we must
8288          * replicate the shift count into each element of
8289          * the tcg_shift value.
8290          */
8291         switch (size) {
8292         case 0:
8293             shift |= shift << 8;
8294             /* fall through */
8295         case 1:
8296             shift |= shift << 16;
8297             break;
8298         case 2:
8299         case 3:
8300             break;
8301         default:
8302             g_assert_not_reached();
8303         }
8304     }
8305 
8306     if (!fp_access_check(s)) {
8307         return;
8308     }
8309 
8310     if (size == 3) {
8311         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8312         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8313             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8314             { NULL, gen_helper_neon_qshl_u64 },
8315         };
8316         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8317         int maxpass = is_q ? 2 : 1;
8318 
8319         for (pass = 0; pass < maxpass; pass++) {
8320             TCGv_i64 tcg_op = tcg_temp_new_i64();
8321 
8322             read_vec_element(s, tcg_op, rn, pass, MO_64);
8323             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8324             write_vec_element(s, tcg_op, rd, pass, MO_64);
8325         }
8326         clear_vec_high(s, is_q, rd);
8327     } else {
8328         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8329         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8330             {
8331                 { gen_helper_neon_qshl_s8,
8332                   gen_helper_neon_qshl_s16,
8333                   gen_helper_neon_qshl_s32 },
8334                 { gen_helper_neon_qshlu_s8,
8335                   gen_helper_neon_qshlu_s16,
8336                   gen_helper_neon_qshlu_s32 }
8337             }, {
8338                 { NULL, NULL, NULL },
8339                 { gen_helper_neon_qshl_u8,
8340                   gen_helper_neon_qshl_u16,
8341                   gen_helper_neon_qshl_u32 }
8342             }
8343         };
8344         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8345         MemOp memop = scalar ? size : MO_32;
8346         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8347 
8348         for (pass = 0; pass < maxpass; pass++) {
8349             TCGv_i32 tcg_op = tcg_temp_new_i32();
8350 
8351             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8352             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8353             if (scalar) {
8354                 switch (size) {
8355                 case 0:
8356                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8357                     break;
8358                 case 1:
8359                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8360                     break;
8361                 case 2:
8362                     break;
8363                 default:
8364                     g_assert_not_reached();
8365                 }
8366                 write_fp_sreg(s, rd, tcg_op);
8367             } else {
8368                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8369             }
8370         }
8371 
8372         if (!scalar) {
8373             clear_vec_high(s, is_q, rd);
8374         }
8375     }
8376 }
8377 
8378 /* Common vector code for handling integer to FP conversion */
8379 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8380                                    int elements, int is_signed,
8381                                    int fracbits, int size)
8382 {
8383     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8384     TCGv_i32 tcg_shift = NULL;
8385 
8386     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8387     int pass;
8388 
8389     if (fracbits || size == MO_64) {
8390         tcg_shift = tcg_constant_i32(fracbits);
8391     }
8392 
8393     if (size == MO_64) {
8394         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8395         TCGv_i64 tcg_double = tcg_temp_new_i64();
8396 
8397         for (pass = 0; pass < elements; pass++) {
8398             read_vec_element(s, tcg_int64, rn, pass, mop);
8399 
8400             if (is_signed) {
8401                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8402                                      tcg_shift, tcg_fpst);
8403             } else {
8404                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8405                                      tcg_shift, tcg_fpst);
8406             }
8407             if (elements == 1) {
8408                 write_fp_dreg(s, rd, tcg_double);
8409             } else {
8410                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8411             }
8412         }
8413     } else {
8414         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8415         TCGv_i32 tcg_float = tcg_temp_new_i32();
8416 
8417         for (pass = 0; pass < elements; pass++) {
8418             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8419 
8420             switch (size) {
8421             case MO_32:
8422                 if (fracbits) {
8423                     if (is_signed) {
8424                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8425                                              tcg_shift, tcg_fpst);
8426                     } else {
8427                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8428                                              tcg_shift, tcg_fpst);
8429                     }
8430                 } else {
8431                     if (is_signed) {
8432                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8433                     } else {
8434                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8435                     }
8436                 }
8437                 break;
8438             case MO_16:
8439                 if (fracbits) {
8440                     if (is_signed) {
8441                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8442                                              tcg_shift, tcg_fpst);
8443                     } else {
8444                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8445                                              tcg_shift, tcg_fpst);
8446                     }
8447                 } else {
8448                     if (is_signed) {
8449                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8450                     } else {
8451                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8452                     }
8453                 }
8454                 break;
8455             default:
8456                 g_assert_not_reached();
8457             }
8458 
8459             if (elements == 1) {
8460                 write_fp_sreg(s, rd, tcg_float);
8461             } else {
8462                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8463             }
8464         }
8465     }
8466 
8467     clear_vec_high(s, elements << size == 16, rd);
8468 }
8469 
8470 /* UCVTF/SCVTF - Integer to FP conversion */
8471 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8472                                          bool is_q, bool is_u,
8473                                          int immh, int immb, int opcode,
8474                                          int rn, int rd)
8475 {
8476     int size, elements, fracbits;
8477     int immhb = immh << 3 | immb;
8478 
8479     if (immh & 8) {
8480         size = MO_64;
8481         if (!is_scalar && !is_q) {
8482             unallocated_encoding(s);
8483             return;
8484         }
8485     } else if (immh & 4) {
8486         size = MO_32;
8487     } else if (immh & 2) {
8488         size = MO_16;
8489         if (!dc_isar_feature(aa64_fp16, s)) {
8490             unallocated_encoding(s);
8491             return;
8492         }
8493     } else {
8494         /* immh == 0 would be a failure of the decode logic */
8495         g_assert(immh == 1);
8496         unallocated_encoding(s);
8497         return;
8498     }
8499 
8500     if (is_scalar) {
8501         elements = 1;
8502     } else {
8503         elements = (8 << is_q) >> size;
8504     }
8505     fracbits = (16 << size) - immhb;
8506 
8507     if (!fp_access_check(s)) {
8508         return;
8509     }
8510 
8511     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8512 }
8513 
8514 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8515 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8516                                          bool is_q, bool is_u,
8517                                          int immh, int immb, int rn, int rd)
8518 {
8519     int immhb = immh << 3 | immb;
8520     int pass, size, fracbits;
8521     TCGv_ptr tcg_fpstatus;
8522     TCGv_i32 tcg_rmode, tcg_shift;
8523 
8524     if (immh & 0x8) {
8525         size = MO_64;
8526         if (!is_scalar && !is_q) {
8527             unallocated_encoding(s);
8528             return;
8529         }
8530     } else if (immh & 0x4) {
8531         size = MO_32;
8532     } else if (immh & 0x2) {
8533         size = MO_16;
8534         if (!dc_isar_feature(aa64_fp16, s)) {
8535             unallocated_encoding(s);
8536             return;
8537         }
8538     } else {
8539         /* Should have split out AdvSIMD modified immediate earlier.  */
8540         assert(immh == 1);
8541         unallocated_encoding(s);
8542         return;
8543     }
8544 
8545     if (!fp_access_check(s)) {
8546         return;
8547     }
8548 
8549     assert(!(is_scalar && is_q));
8550 
8551     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8552     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8553     fracbits = (16 << size) - immhb;
8554     tcg_shift = tcg_constant_i32(fracbits);
8555 
8556     if (size == MO_64) {
8557         int maxpass = is_scalar ? 1 : 2;
8558 
8559         for (pass = 0; pass < maxpass; pass++) {
8560             TCGv_i64 tcg_op = tcg_temp_new_i64();
8561 
8562             read_vec_element(s, tcg_op, rn, pass, MO_64);
8563             if (is_u) {
8564                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8565             } else {
8566                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8567             }
8568             write_vec_element(s, tcg_op, rd, pass, MO_64);
8569         }
8570         clear_vec_high(s, is_q, rd);
8571     } else {
8572         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8573         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8574 
8575         switch (size) {
8576         case MO_16:
8577             if (is_u) {
8578                 fn = gen_helper_vfp_touhh;
8579             } else {
8580                 fn = gen_helper_vfp_toshh;
8581             }
8582             break;
8583         case MO_32:
8584             if (is_u) {
8585                 fn = gen_helper_vfp_touls;
8586             } else {
8587                 fn = gen_helper_vfp_tosls;
8588             }
8589             break;
8590         default:
8591             g_assert_not_reached();
8592         }
8593 
8594         for (pass = 0; pass < maxpass; pass++) {
8595             TCGv_i32 tcg_op = tcg_temp_new_i32();
8596 
8597             read_vec_element_i32(s, tcg_op, rn, pass, size);
8598             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8599             if (is_scalar) {
8600                 write_fp_sreg(s, rd, tcg_op);
8601             } else {
8602                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8603             }
8604         }
8605         if (!is_scalar) {
8606             clear_vec_high(s, is_q, rd);
8607         }
8608     }
8609 
8610     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8611 }
8612 
8613 /* AdvSIMD scalar shift by immediate
8614  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8615  * +-----+---+-------------+------+------+--------+---+------+------+
8616  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8617  * +-----+---+-------------+------+------+--------+---+------+------+
8618  *
8619  * This is the scalar version so it works on a fixed sized registers
8620  */
8621 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8622 {
8623     int rd = extract32(insn, 0, 5);
8624     int rn = extract32(insn, 5, 5);
8625     int opcode = extract32(insn, 11, 5);
8626     int immb = extract32(insn, 16, 3);
8627     int immh = extract32(insn, 19, 4);
8628     bool is_u = extract32(insn, 29, 1);
8629 
8630     if (immh == 0) {
8631         unallocated_encoding(s);
8632         return;
8633     }
8634 
8635     switch (opcode) {
8636     case 0x08: /* SRI */
8637         if (!is_u) {
8638             unallocated_encoding(s);
8639             return;
8640         }
8641         /* fall through */
8642     case 0x00: /* SSHR / USHR */
8643     case 0x02: /* SSRA / USRA */
8644     case 0x04: /* SRSHR / URSHR */
8645     case 0x06: /* SRSRA / URSRA */
8646         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8647         break;
8648     case 0x0a: /* SHL / SLI */
8649         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8650         break;
8651     case 0x1c: /* SCVTF, UCVTF */
8652         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8653                                      opcode, rn, rd);
8654         break;
8655     case 0x10: /* SQSHRUN, SQSHRUN2 */
8656     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8657         if (!is_u) {
8658             unallocated_encoding(s);
8659             return;
8660         }
8661         handle_vec_simd_sqshrn(s, true, false, false, true,
8662                                immh, immb, opcode, rn, rd);
8663         break;
8664     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8665     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8666         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8667                                immh, immb, opcode, rn, rd);
8668         break;
8669     case 0xc: /* SQSHLU */
8670         if (!is_u) {
8671             unallocated_encoding(s);
8672             return;
8673         }
8674         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8675         break;
8676     case 0xe: /* SQSHL, UQSHL */
8677         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8678         break;
8679     case 0x1f: /* FCVTZS, FCVTZU */
8680         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8681         break;
8682     default:
8683         unallocated_encoding(s);
8684         break;
8685     }
8686 }
8687 
8688 /* AdvSIMD scalar three different
8689  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8690  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8691  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8692  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8693  */
8694 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8695 {
8696     bool is_u = extract32(insn, 29, 1);
8697     int size = extract32(insn, 22, 2);
8698     int opcode = extract32(insn, 12, 4);
8699     int rm = extract32(insn, 16, 5);
8700     int rn = extract32(insn, 5, 5);
8701     int rd = extract32(insn, 0, 5);
8702 
8703     if (is_u) {
8704         unallocated_encoding(s);
8705         return;
8706     }
8707 
8708     switch (opcode) {
8709     case 0x9: /* SQDMLAL, SQDMLAL2 */
8710     case 0xb: /* SQDMLSL, SQDMLSL2 */
8711     case 0xd: /* SQDMULL, SQDMULL2 */
8712         if (size == 0 || size == 3) {
8713             unallocated_encoding(s);
8714             return;
8715         }
8716         break;
8717     default:
8718         unallocated_encoding(s);
8719         return;
8720     }
8721 
8722     if (!fp_access_check(s)) {
8723         return;
8724     }
8725 
8726     if (size == 2) {
8727         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8728         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8729         TCGv_i64 tcg_res = tcg_temp_new_i64();
8730 
8731         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8732         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8733 
8734         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8735         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
8736 
8737         switch (opcode) {
8738         case 0xd: /* SQDMULL, SQDMULL2 */
8739             break;
8740         case 0xb: /* SQDMLSL, SQDMLSL2 */
8741             tcg_gen_neg_i64(tcg_res, tcg_res);
8742             /* fall through */
8743         case 0x9: /* SQDMLAL, SQDMLAL2 */
8744             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8745             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
8746                                               tcg_res, tcg_op1);
8747             break;
8748         default:
8749             g_assert_not_reached();
8750         }
8751 
8752         write_fp_dreg(s, rd, tcg_res);
8753     } else {
8754         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8755         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8756         TCGv_i64 tcg_res = tcg_temp_new_i64();
8757 
8758         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8759         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
8760 
8761         switch (opcode) {
8762         case 0xd: /* SQDMULL, SQDMULL2 */
8763             break;
8764         case 0xb: /* SQDMLSL, SQDMLSL2 */
8765             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8766             /* fall through */
8767         case 0x9: /* SQDMLAL, SQDMLAL2 */
8768         {
8769             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8770             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8771             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
8772                                               tcg_res, tcg_op3);
8773             break;
8774         }
8775         default:
8776             g_assert_not_reached();
8777         }
8778 
8779         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8780         write_fp_dreg(s, rd, tcg_res);
8781     }
8782 }
8783 
8784 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8785                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8786 {
8787     /* Handle 64x64->64 opcodes which are shared between the scalar
8788      * and vector 3-same groups. We cover every opcode where size == 3
8789      * is valid in either the three-reg-same (integer, not pairwise)
8790      * or scalar-three-reg-same groups.
8791      */
8792     TCGCond cond;
8793 
8794     switch (opcode) {
8795     case 0x1: /* SQADD */
8796         if (u) {
8797             gen_helper_neon_qadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8798         } else {
8799             gen_helper_neon_qadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8800         }
8801         break;
8802     case 0x5: /* SQSUB */
8803         if (u) {
8804             gen_helper_neon_qsub_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8805         } else {
8806             gen_helper_neon_qsub_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8807         }
8808         break;
8809     case 0x6: /* CMGT, CMHI */
8810         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8811     do_cmop:
8812         /* 64 bit integer comparison, result = test ? -1 : 0. */
8813         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8814         break;
8815     case 0x7: /* CMGE, CMHS */
8816         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8817         goto do_cmop;
8818     case 0x11: /* CMTST, CMEQ */
8819         if (u) {
8820             cond = TCG_COND_EQ;
8821             goto do_cmop;
8822         }
8823         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8824         break;
8825     case 0x8: /* SSHL, USHL */
8826         if (u) {
8827             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8828         } else {
8829             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8830         }
8831         break;
8832     case 0x9: /* SQSHL, UQSHL */
8833         if (u) {
8834             gen_helper_neon_qshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8835         } else {
8836             gen_helper_neon_qshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8837         }
8838         break;
8839     case 0xa: /* SRSHL, URSHL */
8840         if (u) {
8841             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8842         } else {
8843             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8844         }
8845         break;
8846     case 0xb: /* SQRSHL, UQRSHL */
8847         if (u) {
8848             gen_helper_neon_qrshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8849         } else {
8850             gen_helper_neon_qrshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8851         }
8852         break;
8853     case 0x10: /* ADD, SUB */
8854         if (u) {
8855             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8856         } else {
8857             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8858         }
8859         break;
8860     default:
8861         g_assert_not_reached();
8862     }
8863 }
8864 
8865 /* Handle the 3-same-operands float operations; shared by the scalar
8866  * and vector encodings. The caller must filter out any encodings
8867  * not allocated for the encoding it is dealing with.
8868  */
8869 static void handle_3same_float(DisasContext *s, int size, int elements,
8870                                int fpopcode, int rd, int rn, int rm)
8871 {
8872     int pass;
8873     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8874 
8875     for (pass = 0; pass < elements; pass++) {
8876         if (size) {
8877             /* Double */
8878             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8879             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8880             TCGv_i64 tcg_res = tcg_temp_new_i64();
8881 
8882             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8883             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8884 
8885             switch (fpopcode) {
8886             case 0x39: /* FMLS */
8887                 /* As usual for ARM, separate negation for fused multiply-add */
8888                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8889                 /* fall through */
8890             case 0x19: /* FMLA */
8891                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8892                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8893                                        tcg_res, fpst);
8894                 break;
8895             case 0x18: /* FMAXNM */
8896                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8897                 break;
8898             case 0x1a: /* FADD */
8899                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8900                 break;
8901             case 0x1b: /* FMULX */
8902                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8903                 break;
8904             case 0x1c: /* FCMEQ */
8905                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8906                 break;
8907             case 0x1e: /* FMAX */
8908                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8909                 break;
8910             case 0x1f: /* FRECPS */
8911                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8912                 break;
8913             case 0x38: /* FMINNM */
8914                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8915                 break;
8916             case 0x3a: /* FSUB */
8917                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8918                 break;
8919             case 0x3e: /* FMIN */
8920                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8921                 break;
8922             case 0x3f: /* FRSQRTS */
8923                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8924                 break;
8925             case 0x5b: /* FMUL */
8926                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8927                 break;
8928             case 0x5c: /* FCMGE */
8929                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8930                 break;
8931             case 0x5d: /* FACGE */
8932                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8933                 break;
8934             case 0x5f: /* FDIV */
8935                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8936                 break;
8937             case 0x7a: /* FABD */
8938                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8939                 gen_helper_vfp_absd(tcg_res, tcg_res);
8940                 break;
8941             case 0x7c: /* FCMGT */
8942                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8943                 break;
8944             case 0x7d: /* FACGT */
8945                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8946                 break;
8947             default:
8948                 g_assert_not_reached();
8949             }
8950 
8951             write_vec_element(s, tcg_res, rd, pass, MO_64);
8952         } else {
8953             /* Single */
8954             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8955             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8956             TCGv_i32 tcg_res = tcg_temp_new_i32();
8957 
8958             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8959             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8960 
8961             switch (fpopcode) {
8962             case 0x39: /* FMLS */
8963                 /* As usual for ARM, separate negation for fused multiply-add */
8964                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8965                 /* fall through */
8966             case 0x19: /* FMLA */
8967                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8968                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8969                                        tcg_res, fpst);
8970                 break;
8971             case 0x1a: /* FADD */
8972                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8973                 break;
8974             case 0x1b: /* FMULX */
8975                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8976                 break;
8977             case 0x1c: /* FCMEQ */
8978                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8979                 break;
8980             case 0x1e: /* FMAX */
8981                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8982                 break;
8983             case 0x1f: /* FRECPS */
8984                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8985                 break;
8986             case 0x18: /* FMAXNM */
8987                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8988                 break;
8989             case 0x38: /* FMINNM */
8990                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8991                 break;
8992             case 0x3a: /* FSUB */
8993                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8994                 break;
8995             case 0x3e: /* FMIN */
8996                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8997                 break;
8998             case 0x3f: /* FRSQRTS */
8999                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9000                 break;
9001             case 0x5b: /* FMUL */
9002                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9003                 break;
9004             case 0x5c: /* FCMGE */
9005                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9006                 break;
9007             case 0x5d: /* FACGE */
9008                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9009                 break;
9010             case 0x5f: /* FDIV */
9011                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9012                 break;
9013             case 0x7a: /* FABD */
9014                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9015                 gen_helper_vfp_abss(tcg_res, tcg_res);
9016                 break;
9017             case 0x7c: /* FCMGT */
9018                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9019                 break;
9020             case 0x7d: /* FACGT */
9021                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9022                 break;
9023             default:
9024                 g_assert_not_reached();
9025             }
9026 
9027             if (elements == 1) {
9028                 /* scalar single so clear high part */
9029                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9030 
9031                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9032                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9033             } else {
9034                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9035             }
9036         }
9037     }
9038 
9039     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9040 }
9041 
9042 /* AdvSIMD scalar three same
9043  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9044  * +-----+---+-----------+------+---+------+--------+---+------+------+
9045  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9046  * +-----+---+-----------+------+---+------+--------+---+------+------+
9047  */
9048 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9049 {
9050     int rd = extract32(insn, 0, 5);
9051     int rn = extract32(insn, 5, 5);
9052     int opcode = extract32(insn, 11, 5);
9053     int rm = extract32(insn, 16, 5);
9054     int size = extract32(insn, 22, 2);
9055     bool u = extract32(insn, 29, 1);
9056     TCGv_i64 tcg_rd;
9057 
9058     if (opcode >= 0x18) {
9059         /* Floating point: U, size[1] and opcode indicate operation */
9060         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9061         switch (fpopcode) {
9062         case 0x1b: /* FMULX */
9063         case 0x1f: /* FRECPS */
9064         case 0x3f: /* FRSQRTS */
9065         case 0x5d: /* FACGE */
9066         case 0x7d: /* FACGT */
9067         case 0x1c: /* FCMEQ */
9068         case 0x5c: /* FCMGE */
9069         case 0x7c: /* FCMGT */
9070         case 0x7a: /* FABD */
9071             break;
9072         default:
9073             unallocated_encoding(s);
9074             return;
9075         }
9076 
9077         if (!fp_access_check(s)) {
9078             return;
9079         }
9080 
9081         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9082         return;
9083     }
9084 
9085     switch (opcode) {
9086     case 0x1: /* SQADD, UQADD */
9087     case 0x5: /* SQSUB, UQSUB */
9088     case 0x9: /* SQSHL, UQSHL */
9089     case 0xb: /* SQRSHL, UQRSHL */
9090         break;
9091     case 0x8: /* SSHL, USHL */
9092     case 0xa: /* SRSHL, URSHL */
9093     case 0x6: /* CMGT, CMHI */
9094     case 0x7: /* CMGE, CMHS */
9095     case 0x11: /* CMTST, CMEQ */
9096     case 0x10: /* ADD, SUB (vector) */
9097         if (size != 3) {
9098             unallocated_encoding(s);
9099             return;
9100         }
9101         break;
9102     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9103         if (size != 1 && size != 2) {
9104             unallocated_encoding(s);
9105             return;
9106         }
9107         break;
9108     default:
9109         unallocated_encoding(s);
9110         return;
9111     }
9112 
9113     if (!fp_access_check(s)) {
9114         return;
9115     }
9116 
9117     tcg_rd = tcg_temp_new_i64();
9118 
9119     if (size == 3) {
9120         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9121         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9122 
9123         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9124     } else {
9125         /* Do a single operation on the lowest element in the vector.
9126          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9127          * no side effects for all these operations.
9128          * OPTME: special-purpose helpers would avoid doing some
9129          * unnecessary work in the helper for the 8 and 16 bit cases.
9130          */
9131         NeonGenTwoOpEnvFn *genenvfn;
9132         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9133         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9134         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9135 
9136         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9137         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9138 
9139         switch (opcode) {
9140         case 0x1: /* SQADD, UQADD */
9141         {
9142             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9143                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9144                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9145                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9146             };
9147             genenvfn = fns[size][u];
9148             break;
9149         }
9150         case 0x5: /* SQSUB, UQSUB */
9151         {
9152             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9153                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9154                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9155                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9156             };
9157             genenvfn = fns[size][u];
9158             break;
9159         }
9160         case 0x9: /* SQSHL, UQSHL */
9161         {
9162             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9163                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9164                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9165                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9166             };
9167             genenvfn = fns[size][u];
9168             break;
9169         }
9170         case 0xb: /* SQRSHL, UQRSHL */
9171         {
9172             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9173                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9174                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9175                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9176             };
9177             genenvfn = fns[size][u];
9178             break;
9179         }
9180         case 0x16: /* SQDMULH, SQRDMULH */
9181         {
9182             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9183                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9184                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9185             };
9186             assert(size == 1 || size == 2);
9187             genenvfn = fns[size - 1][u];
9188             break;
9189         }
9190         default:
9191             g_assert_not_reached();
9192         }
9193 
9194         genenvfn(tcg_rd32, tcg_env, tcg_rn, tcg_rm);
9195         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9196     }
9197 
9198     write_fp_dreg(s, rd, tcg_rd);
9199 }
9200 
9201 /* AdvSIMD scalar three same FP16
9202  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9203  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9204  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9205  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9206  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9207  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9208  */
9209 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9210                                                   uint32_t insn)
9211 {
9212     int rd = extract32(insn, 0, 5);
9213     int rn = extract32(insn, 5, 5);
9214     int opcode = extract32(insn, 11, 3);
9215     int rm = extract32(insn, 16, 5);
9216     bool u = extract32(insn, 29, 1);
9217     bool a = extract32(insn, 23, 1);
9218     int fpopcode = opcode | (a << 3) |  (u << 4);
9219     TCGv_ptr fpst;
9220     TCGv_i32 tcg_op1;
9221     TCGv_i32 tcg_op2;
9222     TCGv_i32 tcg_res;
9223 
9224     switch (fpopcode) {
9225     case 0x03: /* FMULX */
9226     case 0x04: /* FCMEQ (reg) */
9227     case 0x07: /* FRECPS */
9228     case 0x0f: /* FRSQRTS */
9229     case 0x14: /* FCMGE (reg) */
9230     case 0x15: /* FACGE */
9231     case 0x1a: /* FABD */
9232     case 0x1c: /* FCMGT (reg) */
9233     case 0x1d: /* FACGT */
9234         break;
9235     default:
9236         unallocated_encoding(s);
9237         return;
9238     }
9239 
9240     if (!dc_isar_feature(aa64_fp16, s)) {
9241         unallocated_encoding(s);
9242     }
9243 
9244     if (!fp_access_check(s)) {
9245         return;
9246     }
9247 
9248     fpst = fpstatus_ptr(FPST_FPCR_F16);
9249 
9250     tcg_op1 = read_fp_hreg(s, rn);
9251     tcg_op2 = read_fp_hreg(s, rm);
9252     tcg_res = tcg_temp_new_i32();
9253 
9254     switch (fpopcode) {
9255     case 0x03: /* FMULX */
9256         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9257         break;
9258     case 0x04: /* FCMEQ (reg) */
9259         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9260         break;
9261     case 0x07: /* FRECPS */
9262         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9263         break;
9264     case 0x0f: /* FRSQRTS */
9265         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9266         break;
9267     case 0x14: /* FCMGE (reg) */
9268         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9269         break;
9270     case 0x15: /* FACGE */
9271         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9272         break;
9273     case 0x1a: /* FABD */
9274         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9275         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9276         break;
9277     case 0x1c: /* FCMGT (reg) */
9278         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9279         break;
9280     case 0x1d: /* FACGT */
9281         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9282         break;
9283     default:
9284         g_assert_not_reached();
9285     }
9286 
9287     write_fp_sreg(s, rd, tcg_res);
9288 }
9289 
9290 /* AdvSIMD scalar three same extra
9291  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9292  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9293  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9294  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9295  */
9296 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9297                                                    uint32_t insn)
9298 {
9299     int rd = extract32(insn, 0, 5);
9300     int rn = extract32(insn, 5, 5);
9301     int opcode = extract32(insn, 11, 4);
9302     int rm = extract32(insn, 16, 5);
9303     int size = extract32(insn, 22, 2);
9304     bool u = extract32(insn, 29, 1);
9305     TCGv_i32 ele1, ele2, ele3;
9306     TCGv_i64 res;
9307     bool feature;
9308 
9309     switch (u * 16 + opcode) {
9310     case 0x10: /* SQRDMLAH (vector) */
9311     case 0x11: /* SQRDMLSH (vector) */
9312         if (size != 1 && size != 2) {
9313             unallocated_encoding(s);
9314             return;
9315         }
9316         feature = dc_isar_feature(aa64_rdm, s);
9317         break;
9318     default:
9319         unallocated_encoding(s);
9320         return;
9321     }
9322     if (!feature) {
9323         unallocated_encoding(s);
9324         return;
9325     }
9326     if (!fp_access_check(s)) {
9327         return;
9328     }
9329 
9330     /* Do a single operation on the lowest element in the vector.
9331      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9332      * with no side effects for all these operations.
9333      * OPTME: special-purpose helpers would avoid doing some
9334      * unnecessary work in the helper for the 16 bit cases.
9335      */
9336     ele1 = tcg_temp_new_i32();
9337     ele2 = tcg_temp_new_i32();
9338     ele3 = tcg_temp_new_i32();
9339 
9340     read_vec_element_i32(s, ele1, rn, 0, size);
9341     read_vec_element_i32(s, ele2, rm, 0, size);
9342     read_vec_element_i32(s, ele3, rd, 0, size);
9343 
9344     switch (opcode) {
9345     case 0x0: /* SQRDMLAH */
9346         if (size == 1) {
9347             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9348         } else {
9349             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9350         }
9351         break;
9352     case 0x1: /* SQRDMLSH */
9353         if (size == 1) {
9354             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9355         } else {
9356             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9357         }
9358         break;
9359     default:
9360         g_assert_not_reached();
9361     }
9362 
9363     res = tcg_temp_new_i64();
9364     tcg_gen_extu_i32_i64(res, ele3);
9365     write_fp_dreg(s, rd, res);
9366 }
9367 
9368 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9369                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9370                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9371 {
9372     /* Handle 64->64 opcodes which are shared between the scalar and
9373      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9374      * is valid in either group and also the double-precision fp ops.
9375      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9376      * requires them.
9377      */
9378     TCGCond cond;
9379 
9380     switch (opcode) {
9381     case 0x4: /* CLS, CLZ */
9382         if (u) {
9383             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9384         } else {
9385             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9386         }
9387         break;
9388     case 0x5: /* NOT */
9389         /* This opcode is shared with CNT and RBIT but we have earlier
9390          * enforced that size == 3 if and only if this is the NOT insn.
9391          */
9392         tcg_gen_not_i64(tcg_rd, tcg_rn);
9393         break;
9394     case 0x7: /* SQABS, SQNEG */
9395         if (u) {
9396             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9397         } else {
9398             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9399         }
9400         break;
9401     case 0xa: /* CMLT */
9402         cond = TCG_COND_LT;
9403     do_cmop:
9404         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9405         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9406         break;
9407     case 0x8: /* CMGT, CMGE */
9408         cond = u ? TCG_COND_GE : TCG_COND_GT;
9409         goto do_cmop;
9410     case 0x9: /* CMEQ, CMLE */
9411         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9412         goto do_cmop;
9413     case 0xb: /* ABS, NEG */
9414         if (u) {
9415             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9416         } else {
9417             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9418         }
9419         break;
9420     case 0x2f: /* FABS */
9421         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9422         break;
9423     case 0x6f: /* FNEG */
9424         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9425         break;
9426     case 0x7f: /* FSQRT */
9427         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9428         break;
9429     case 0x1a: /* FCVTNS */
9430     case 0x1b: /* FCVTMS */
9431     case 0x1c: /* FCVTAS */
9432     case 0x3a: /* FCVTPS */
9433     case 0x3b: /* FCVTZS */
9434         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9435         break;
9436     case 0x5a: /* FCVTNU */
9437     case 0x5b: /* FCVTMU */
9438     case 0x5c: /* FCVTAU */
9439     case 0x7a: /* FCVTPU */
9440     case 0x7b: /* FCVTZU */
9441         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9442         break;
9443     case 0x18: /* FRINTN */
9444     case 0x19: /* FRINTM */
9445     case 0x38: /* FRINTP */
9446     case 0x39: /* FRINTZ */
9447     case 0x58: /* FRINTA */
9448     case 0x79: /* FRINTI */
9449         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9450         break;
9451     case 0x59: /* FRINTX */
9452         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9453         break;
9454     case 0x1e: /* FRINT32Z */
9455     case 0x5e: /* FRINT32X */
9456         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9457         break;
9458     case 0x1f: /* FRINT64Z */
9459     case 0x5f: /* FRINT64X */
9460         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9461         break;
9462     default:
9463         g_assert_not_reached();
9464     }
9465 }
9466 
9467 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9468                                    bool is_scalar, bool is_u, bool is_q,
9469                                    int size, int rn, int rd)
9470 {
9471     bool is_double = (size == MO_64);
9472     TCGv_ptr fpst;
9473 
9474     if (!fp_access_check(s)) {
9475         return;
9476     }
9477 
9478     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9479 
9480     if (is_double) {
9481         TCGv_i64 tcg_op = tcg_temp_new_i64();
9482         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9483         TCGv_i64 tcg_res = tcg_temp_new_i64();
9484         NeonGenTwoDoubleOpFn *genfn;
9485         bool swap = false;
9486         int pass;
9487 
9488         switch (opcode) {
9489         case 0x2e: /* FCMLT (zero) */
9490             swap = true;
9491             /* fallthrough */
9492         case 0x2c: /* FCMGT (zero) */
9493             genfn = gen_helper_neon_cgt_f64;
9494             break;
9495         case 0x2d: /* FCMEQ (zero) */
9496             genfn = gen_helper_neon_ceq_f64;
9497             break;
9498         case 0x6d: /* FCMLE (zero) */
9499             swap = true;
9500             /* fall through */
9501         case 0x6c: /* FCMGE (zero) */
9502             genfn = gen_helper_neon_cge_f64;
9503             break;
9504         default:
9505             g_assert_not_reached();
9506         }
9507 
9508         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9509             read_vec_element(s, tcg_op, rn, pass, MO_64);
9510             if (swap) {
9511                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9512             } else {
9513                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9514             }
9515             write_vec_element(s, tcg_res, rd, pass, MO_64);
9516         }
9517 
9518         clear_vec_high(s, !is_scalar, rd);
9519     } else {
9520         TCGv_i32 tcg_op = tcg_temp_new_i32();
9521         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9522         TCGv_i32 tcg_res = tcg_temp_new_i32();
9523         NeonGenTwoSingleOpFn *genfn;
9524         bool swap = false;
9525         int pass, maxpasses;
9526 
9527         if (size == MO_16) {
9528             switch (opcode) {
9529             case 0x2e: /* FCMLT (zero) */
9530                 swap = true;
9531                 /* fall through */
9532             case 0x2c: /* FCMGT (zero) */
9533                 genfn = gen_helper_advsimd_cgt_f16;
9534                 break;
9535             case 0x2d: /* FCMEQ (zero) */
9536                 genfn = gen_helper_advsimd_ceq_f16;
9537                 break;
9538             case 0x6d: /* FCMLE (zero) */
9539                 swap = true;
9540                 /* fall through */
9541             case 0x6c: /* FCMGE (zero) */
9542                 genfn = gen_helper_advsimd_cge_f16;
9543                 break;
9544             default:
9545                 g_assert_not_reached();
9546             }
9547         } else {
9548             switch (opcode) {
9549             case 0x2e: /* FCMLT (zero) */
9550                 swap = true;
9551                 /* fall through */
9552             case 0x2c: /* FCMGT (zero) */
9553                 genfn = gen_helper_neon_cgt_f32;
9554                 break;
9555             case 0x2d: /* FCMEQ (zero) */
9556                 genfn = gen_helper_neon_ceq_f32;
9557                 break;
9558             case 0x6d: /* FCMLE (zero) */
9559                 swap = true;
9560                 /* fall through */
9561             case 0x6c: /* FCMGE (zero) */
9562                 genfn = gen_helper_neon_cge_f32;
9563                 break;
9564             default:
9565                 g_assert_not_reached();
9566             }
9567         }
9568 
9569         if (is_scalar) {
9570             maxpasses = 1;
9571         } else {
9572             int vector_size = 8 << is_q;
9573             maxpasses = vector_size >> size;
9574         }
9575 
9576         for (pass = 0; pass < maxpasses; pass++) {
9577             read_vec_element_i32(s, tcg_op, rn, pass, size);
9578             if (swap) {
9579                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9580             } else {
9581                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9582             }
9583             if (is_scalar) {
9584                 write_fp_sreg(s, rd, tcg_res);
9585             } else {
9586                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9587             }
9588         }
9589 
9590         if (!is_scalar) {
9591             clear_vec_high(s, is_q, rd);
9592         }
9593     }
9594 }
9595 
9596 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9597                                     bool is_scalar, bool is_u, bool is_q,
9598                                     int size, int rn, int rd)
9599 {
9600     bool is_double = (size == 3);
9601     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9602 
9603     if (is_double) {
9604         TCGv_i64 tcg_op = tcg_temp_new_i64();
9605         TCGv_i64 tcg_res = tcg_temp_new_i64();
9606         int pass;
9607 
9608         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9609             read_vec_element(s, tcg_op, rn, pass, MO_64);
9610             switch (opcode) {
9611             case 0x3d: /* FRECPE */
9612                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9613                 break;
9614             case 0x3f: /* FRECPX */
9615                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9616                 break;
9617             case 0x7d: /* FRSQRTE */
9618                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9619                 break;
9620             default:
9621                 g_assert_not_reached();
9622             }
9623             write_vec_element(s, tcg_res, rd, pass, MO_64);
9624         }
9625         clear_vec_high(s, !is_scalar, rd);
9626     } else {
9627         TCGv_i32 tcg_op = tcg_temp_new_i32();
9628         TCGv_i32 tcg_res = tcg_temp_new_i32();
9629         int pass, maxpasses;
9630 
9631         if (is_scalar) {
9632             maxpasses = 1;
9633         } else {
9634             maxpasses = is_q ? 4 : 2;
9635         }
9636 
9637         for (pass = 0; pass < maxpasses; pass++) {
9638             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9639 
9640             switch (opcode) {
9641             case 0x3c: /* URECPE */
9642                 gen_helper_recpe_u32(tcg_res, tcg_op);
9643                 break;
9644             case 0x3d: /* FRECPE */
9645                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9646                 break;
9647             case 0x3f: /* FRECPX */
9648                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9649                 break;
9650             case 0x7d: /* FRSQRTE */
9651                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9652                 break;
9653             default:
9654                 g_assert_not_reached();
9655             }
9656 
9657             if (is_scalar) {
9658                 write_fp_sreg(s, rd, tcg_res);
9659             } else {
9660                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9661             }
9662         }
9663         if (!is_scalar) {
9664             clear_vec_high(s, is_q, rd);
9665         }
9666     }
9667 }
9668 
9669 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9670                                 int opcode, bool u, bool is_q,
9671                                 int size, int rn, int rd)
9672 {
9673     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9674      * in the source becomes a size element in the destination).
9675      */
9676     int pass;
9677     TCGv_i32 tcg_res[2];
9678     int destelt = is_q ? 2 : 0;
9679     int passes = scalar ? 1 : 2;
9680 
9681     if (scalar) {
9682         tcg_res[1] = tcg_constant_i32(0);
9683     }
9684 
9685     for (pass = 0; pass < passes; pass++) {
9686         TCGv_i64 tcg_op = tcg_temp_new_i64();
9687         NeonGenNarrowFn *genfn = NULL;
9688         NeonGenNarrowEnvFn *genenvfn = NULL;
9689 
9690         if (scalar) {
9691             read_vec_element(s, tcg_op, rn, pass, size + 1);
9692         } else {
9693             read_vec_element(s, tcg_op, rn, pass, MO_64);
9694         }
9695         tcg_res[pass] = tcg_temp_new_i32();
9696 
9697         switch (opcode) {
9698         case 0x12: /* XTN, SQXTUN */
9699         {
9700             static NeonGenNarrowFn * const xtnfns[3] = {
9701                 gen_helper_neon_narrow_u8,
9702                 gen_helper_neon_narrow_u16,
9703                 tcg_gen_extrl_i64_i32,
9704             };
9705             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9706                 gen_helper_neon_unarrow_sat8,
9707                 gen_helper_neon_unarrow_sat16,
9708                 gen_helper_neon_unarrow_sat32,
9709             };
9710             if (u) {
9711                 genenvfn = sqxtunfns[size];
9712             } else {
9713                 genfn = xtnfns[size];
9714             }
9715             break;
9716         }
9717         case 0x14: /* SQXTN, UQXTN */
9718         {
9719             static NeonGenNarrowEnvFn * const fns[3][2] = {
9720                 { gen_helper_neon_narrow_sat_s8,
9721                   gen_helper_neon_narrow_sat_u8 },
9722                 { gen_helper_neon_narrow_sat_s16,
9723                   gen_helper_neon_narrow_sat_u16 },
9724                 { gen_helper_neon_narrow_sat_s32,
9725                   gen_helper_neon_narrow_sat_u32 },
9726             };
9727             genenvfn = fns[size][u];
9728             break;
9729         }
9730         case 0x16: /* FCVTN, FCVTN2 */
9731             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9732             if (size == 2) {
9733                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9734             } else {
9735                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9736                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9737                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9738                 TCGv_i32 ahp = get_ahp_flag();
9739 
9740                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9741                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9742                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9743                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9744             }
9745             break;
9746         case 0x36: /* BFCVTN, BFCVTN2 */
9747             {
9748                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9749                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9750             }
9751             break;
9752         case 0x56:  /* FCVTXN, FCVTXN2 */
9753             /* 64 bit to 32 bit float conversion
9754              * with von Neumann rounding (round to odd)
9755              */
9756             assert(size == 2);
9757             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
9758             break;
9759         default:
9760             g_assert_not_reached();
9761         }
9762 
9763         if (genfn) {
9764             genfn(tcg_res[pass], tcg_op);
9765         } else if (genenvfn) {
9766             genenvfn(tcg_res[pass], tcg_env, tcg_op);
9767         }
9768     }
9769 
9770     for (pass = 0; pass < 2; pass++) {
9771         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9772     }
9773     clear_vec_high(s, is_q, rd);
9774 }
9775 
9776 /* Remaining saturating accumulating ops */
9777 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9778                                 bool is_q, int size, int rn, int rd)
9779 {
9780     bool is_double = (size == 3);
9781 
9782     if (is_double) {
9783         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9784         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9785         int pass;
9786 
9787         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9788             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9789             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9790 
9791             if (is_u) { /* USQADD */
9792                 gen_helper_neon_uqadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9793             } else { /* SUQADD */
9794                 gen_helper_neon_sqadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9795             }
9796             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9797         }
9798         clear_vec_high(s, !is_scalar, rd);
9799     } else {
9800         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9801         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9802         int pass, maxpasses;
9803 
9804         if (is_scalar) {
9805             maxpasses = 1;
9806         } else {
9807             maxpasses = is_q ? 4 : 2;
9808         }
9809 
9810         for (pass = 0; pass < maxpasses; pass++) {
9811             if (is_scalar) {
9812                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9813                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9814             } else {
9815                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9816                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9817             }
9818 
9819             if (is_u) { /* USQADD */
9820                 switch (size) {
9821                 case 0:
9822                     gen_helper_neon_uqadd_s8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9823                     break;
9824                 case 1:
9825                     gen_helper_neon_uqadd_s16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9826                     break;
9827                 case 2:
9828                     gen_helper_neon_uqadd_s32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9829                     break;
9830                 default:
9831                     g_assert_not_reached();
9832                 }
9833             } else { /* SUQADD */
9834                 switch (size) {
9835                 case 0:
9836                     gen_helper_neon_sqadd_u8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9837                     break;
9838                 case 1:
9839                     gen_helper_neon_sqadd_u16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9840                     break;
9841                 case 2:
9842                     gen_helper_neon_sqadd_u32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9843                     break;
9844                 default:
9845                     g_assert_not_reached();
9846                 }
9847             }
9848 
9849             if (is_scalar) {
9850                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9851             }
9852             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9853         }
9854         clear_vec_high(s, is_q, rd);
9855     }
9856 }
9857 
9858 /* AdvSIMD scalar two reg misc
9859  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9860  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9861  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9862  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9863  */
9864 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9865 {
9866     int rd = extract32(insn, 0, 5);
9867     int rn = extract32(insn, 5, 5);
9868     int opcode = extract32(insn, 12, 5);
9869     int size = extract32(insn, 22, 2);
9870     bool u = extract32(insn, 29, 1);
9871     bool is_fcvt = false;
9872     int rmode;
9873     TCGv_i32 tcg_rmode;
9874     TCGv_ptr tcg_fpstatus;
9875 
9876     switch (opcode) {
9877     case 0x3: /* USQADD / SUQADD*/
9878         if (!fp_access_check(s)) {
9879             return;
9880         }
9881         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9882         return;
9883     case 0x7: /* SQABS / SQNEG */
9884         break;
9885     case 0xa: /* CMLT */
9886         if (u) {
9887             unallocated_encoding(s);
9888             return;
9889         }
9890         /* fall through */
9891     case 0x8: /* CMGT, CMGE */
9892     case 0x9: /* CMEQ, CMLE */
9893     case 0xb: /* ABS, NEG */
9894         if (size != 3) {
9895             unallocated_encoding(s);
9896             return;
9897         }
9898         break;
9899     case 0x12: /* SQXTUN */
9900         if (!u) {
9901             unallocated_encoding(s);
9902             return;
9903         }
9904         /* fall through */
9905     case 0x14: /* SQXTN, UQXTN */
9906         if (size == 3) {
9907             unallocated_encoding(s);
9908             return;
9909         }
9910         if (!fp_access_check(s)) {
9911             return;
9912         }
9913         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9914         return;
9915     case 0xc ... 0xf:
9916     case 0x16 ... 0x1d:
9917     case 0x1f:
9918         /* Floating point: U, size[1] and opcode indicate operation;
9919          * size[0] indicates single or double precision.
9920          */
9921         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9922         size = extract32(size, 0, 1) ? 3 : 2;
9923         switch (opcode) {
9924         case 0x2c: /* FCMGT (zero) */
9925         case 0x2d: /* FCMEQ (zero) */
9926         case 0x2e: /* FCMLT (zero) */
9927         case 0x6c: /* FCMGE (zero) */
9928         case 0x6d: /* FCMLE (zero) */
9929             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9930             return;
9931         case 0x1d: /* SCVTF */
9932         case 0x5d: /* UCVTF */
9933         {
9934             bool is_signed = (opcode == 0x1d);
9935             if (!fp_access_check(s)) {
9936                 return;
9937             }
9938             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9939             return;
9940         }
9941         case 0x3d: /* FRECPE */
9942         case 0x3f: /* FRECPX */
9943         case 0x7d: /* FRSQRTE */
9944             if (!fp_access_check(s)) {
9945                 return;
9946             }
9947             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9948             return;
9949         case 0x1a: /* FCVTNS */
9950         case 0x1b: /* FCVTMS */
9951         case 0x3a: /* FCVTPS */
9952         case 0x3b: /* FCVTZS */
9953         case 0x5a: /* FCVTNU */
9954         case 0x5b: /* FCVTMU */
9955         case 0x7a: /* FCVTPU */
9956         case 0x7b: /* FCVTZU */
9957             is_fcvt = true;
9958             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9959             break;
9960         case 0x1c: /* FCVTAS */
9961         case 0x5c: /* FCVTAU */
9962             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9963             is_fcvt = true;
9964             rmode = FPROUNDING_TIEAWAY;
9965             break;
9966         case 0x56: /* FCVTXN, FCVTXN2 */
9967             if (size == 2) {
9968                 unallocated_encoding(s);
9969                 return;
9970             }
9971             if (!fp_access_check(s)) {
9972                 return;
9973             }
9974             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9975             return;
9976         default:
9977             unallocated_encoding(s);
9978             return;
9979         }
9980         break;
9981     default:
9982         unallocated_encoding(s);
9983         return;
9984     }
9985 
9986     if (!fp_access_check(s)) {
9987         return;
9988     }
9989 
9990     if (is_fcvt) {
9991         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
9992         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
9993     } else {
9994         tcg_fpstatus = NULL;
9995         tcg_rmode = NULL;
9996     }
9997 
9998     if (size == 3) {
9999         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10000         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10001 
10002         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10003         write_fp_dreg(s, rd, tcg_rd);
10004     } else {
10005         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10006         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10007 
10008         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10009 
10010         switch (opcode) {
10011         case 0x7: /* SQABS, SQNEG */
10012         {
10013             NeonGenOneOpEnvFn *genfn;
10014             static NeonGenOneOpEnvFn * const fns[3][2] = {
10015                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10016                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10017                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10018             };
10019             genfn = fns[size][u];
10020             genfn(tcg_rd, tcg_env, tcg_rn);
10021             break;
10022         }
10023         case 0x1a: /* FCVTNS */
10024         case 0x1b: /* FCVTMS */
10025         case 0x1c: /* FCVTAS */
10026         case 0x3a: /* FCVTPS */
10027         case 0x3b: /* FCVTZS */
10028             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10029                                  tcg_fpstatus);
10030             break;
10031         case 0x5a: /* FCVTNU */
10032         case 0x5b: /* FCVTMU */
10033         case 0x5c: /* FCVTAU */
10034         case 0x7a: /* FCVTPU */
10035         case 0x7b: /* FCVTZU */
10036             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10037                                  tcg_fpstatus);
10038             break;
10039         default:
10040             g_assert_not_reached();
10041         }
10042 
10043         write_fp_sreg(s, rd, tcg_rd);
10044     }
10045 
10046     if (is_fcvt) {
10047         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10048     }
10049 }
10050 
10051 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10052 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10053                                  int immh, int immb, int opcode, int rn, int rd)
10054 {
10055     int size = 32 - clz32(immh) - 1;
10056     int immhb = immh << 3 | immb;
10057     int shift = 2 * (8 << size) - immhb;
10058     GVecGen2iFn *gvec_fn;
10059 
10060     if (extract32(immh, 3, 1) && !is_q) {
10061         unallocated_encoding(s);
10062         return;
10063     }
10064     tcg_debug_assert(size <= 3);
10065 
10066     if (!fp_access_check(s)) {
10067         return;
10068     }
10069 
10070     switch (opcode) {
10071     case 0x02: /* SSRA / USRA (accumulate) */
10072         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10073         break;
10074 
10075     case 0x08: /* SRI */
10076         gvec_fn = gen_gvec_sri;
10077         break;
10078 
10079     case 0x00: /* SSHR / USHR */
10080         if (is_u) {
10081             if (shift == 8 << size) {
10082                 /* Shift count the same size as element size produces zero.  */
10083                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10084                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10085                 return;
10086             }
10087             gvec_fn = tcg_gen_gvec_shri;
10088         } else {
10089             /* Shift count the same size as element size produces all sign.  */
10090             if (shift == 8 << size) {
10091                 shift -= 1;
10092             }
10093             gvec_fn = tcg_gen_gvec_sari;
10094         }
10095         break;
10096 
10097     case 0x04: /* SRSHR / URSHR (rounding) */
10098         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10099         break;
10100 
10101     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10102         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10103         break;
10104 
10105     default:
10106         g_assert_not_reached();
10107     }
10108 
10109     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10110 }
10111 
10112 /* SHL/SLI - Vector shift left */
10113 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10114                                  int immh, int immb, int opcode, int rn, int rd)
10115 {
10116     int size = 32 - clz32(immh) - 1;
10117     int immhb = immh << 3 | immb;
10118     int shift = immhb - (8 << size);
10119 
10120     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10121     assert(size >= 0 && size <= 3);
10122 
10123     if (extract32(immh, 3, 1) && !is_q) {
10124         unallocated_encoding(s);
10125         return;
10126     }
10127 
10128     if (!fp_access_check(s)) {
10129         return;
10130     }
10131 
10132     if (insert) {
10133         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10134     } else {
10135         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10136     }
10137 }
10138 
10139 /* USHLL/SHLL - Vector shift left with widening */
10140 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10141                                  int immh, int immb, int opcode, int rn, int rd)
10142 {
10143     int size = 32 - clz32(immh) - 1;
10144     int immhb = immh << 3 | immb;
10145     int shift = immhb - (8 << size);
10146     int dsize = 64;
10147     int esize = 8 << size;
10148     int elements = dsize/esize;
10149     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10150     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10151     int i;
10152 
10153     if (size >= 3) {
10154         unallocated_encoding(s);
10155         return;
10156     }
10157 
10158     if (!fp_access_check(s)) {
10159         return;
10160     }
10161 
10162     /* For the LL variants the store is larger than the load,
10163      * so if rd == rn we would overwrite parts of our input.
10164      * So load everything right now and use shifts in the main loop.
10165      */
10166     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10167 
10168     for (i = 0; i < elements; i++) {
10169         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10170         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10171         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10172         write_vec_element(s, tcg_rd, rd, i, size + 1);
10173     }
10174 }
10175 
10176 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10177 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10178                                  int immh, int immb, int opcode, int rn, int rd)
10179 {
10180     int immhb = immh << 3 | immb;
10181     int size = 32 - clz32(immh) - 1;
10182     int dsize = 64;
10183     int esize = 8 << size;
10184     int elements = dsize/esize;
10185     int shift = (2 * esize) - immhb;
10186     bool round = extract32(opcode, 0, 1);
10187     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10188     TCGv_i64 tcg_round;
10189     int i;
10190 
10191     if (extract32(immh, 3, 1)) {
10192         unallocated_encoding(s);
10193         return;
10194     }
10195 
10196     if (!fp_access_check(s)) {
10197         return;
10198     }
10199 
10200     tcg_rn = tcg_temp_new_i64();
10201     tcg_rd = tcg_temp_new_i64();
10202     tcg_final = tcg_temp_new_i64();
10203     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10204 
10205     if (round) {
10206         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10207     } else {
10208         tcg_round = NULL;
10209     }
10210 
10211     for (i = 0; i < elements; i++) {
10212         read_vec_element(s, tcg_rn, rn, i, size+1);
10213         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10214                                 false, true, size+1, shift);
10215 
10216         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10217     }
10218 
10219     if (!is_q) {
10220         write_vec_element(s, tcg_final, rd, 0, MO_64);
10221     } else {
10222         write_vec_element(s, tcg_final, rd, 1, MO_64);
10223     }
10224 
10225     clear_vec_high(s, is_q, rd);
10226 }
10227 
10228 
10229 /* AdvSIMD shift by immediate
10230  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10231  * +---+---+---+-------------+------+------+--------+---+------+------+
10232  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10233  * +---+---+---+-------------+------+------+--------+---+------+------+
10234  */
10235 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10236 {
10237     int rd = extract32(insn, 0, 5);
10238     int rn = extract32(insn, 5, 5);
10239     int opcode = extract32(insn, 11, 5);
10240     int immb = extract32(insn, 16, 3);
10241     int immh = extract32(insn, 19, 4);
10242     bool is_u = extract32(insn, 29, 1);
10243     bool is_q = extract32(insn, 30, 1);
10244 
10245     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10246     assert(immh != 0);
10247 
10248     switch (opcode) {
10249     case 0x08: /* SRI */
10250         if (!is_u) {
10251             unallocated_encoding(s);
10252             return;
10253         }
10254         /* fall through */
10255     case 0x00: /* SSHR / USHR */
10256     case 0x02: /* SSRA / USRA (accumulate) */
10257     case 0x04: /* SRSHR / URSHR (rounding) */
10258     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10259         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10260         break;
10261     case 0x0a: /* SHL / SLI */
10262         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10263         break;
10264     case 0x10: /* SHRN */
10265     case 0x11: /* RSHRN / SQRSHRUN */
10266         if (is_u) {
10267             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10268                                    opcode, rn, rd);
10269         } else {
10270             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10271         }
10272         break;
10273     case 0x12: /* SQSHRN / UQSHRN */
10274     case 0x13: /* SQRSHRN / UQRSHRN */
10275         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10276                                opcode, rn, rd);
10277         break;
10278     case 0x14: /* SSHLL / USHLL */
10279         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10280         break;
10281     case 0x1c: /* SCVTF / UCVTF */
10282         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10283                                      opcode, rn, rd);
10284         break;
10285     case 0xc: /* SQSHLU */
10286         if (!is_u) {
10287             unallocated_encoding(s);
10288             return;
10289         }
10290         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10291         break;
10292     case 0xe: /* SQSHL, UQSHL */
10293         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10294         break;
10295     case 0x1f: /* FCVTZS/ FCVTZU */
10296         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10297         return;
10298     default:
10299         unallocated_encoding(s);
10300         return;
10301     }
10302 }
10303 
10304 /* Generate code to do a "long" addition or subtraction, ie one done in
10305  * TCGv_i64 on vector lanes twice the width specified by size.
10306  */
10307 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10308                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10309 {
10310     static NeonGenTwo64OpFn * const fns[3][2] = {
10311         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10312         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10313         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10314     };
10315     NeonGenTwo64OpFn *genfn;
10316     assert(size < 3);
10317 
10318     genfn = fns[size][is_sub];
10319     genfn(tcg_res, tcg_op1, tcg_op2);
10320 }
10321 
10322 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10323                                 int opcode, int rd, int rn, int rm)
10324 {
10325     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10326     TCGv_i64 tcg_res[2];
10327     int pass, accop;
10328 
10329     tcg_res[0] = tcg_temp_new_i64();
10330     tcg_res[1] = tcg_temp_new_i64();
10331 
10332     /* Does this op do an adding accumulate, a subtracting accumulate,
10333      * or no accumulate at all?
10334      */
10335     switch (opcode) {
10336     case 5:
10337     case 8:
10338     case 9:
10339         accop = 1;
10340         break;
10341     case 10:
10342     case 11:
10343         accop = -1;
10344         break;
10345     default:
10346         accop = 0;
10347         break;
10348     }
10349 
10350     if (accop != 0) {
10351         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10352         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10353     }
10354 
10355     /* size == 2 means two 32x32->64 operations; this is worth special
10356      * casing because we can generally handle it inline.
10357      */
10358     if (size == 2) {
10359         for (pass = 0; pass < 2; pass++) {
10360             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10361             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10362             TCGv_i64 tcg_passres;
10363             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10364 
10365             int elt = pass + is_q * 2;
10366 
10367             read_vec_element(s, tcg_op1, rn, elt, memop);
10368             read_vec_element(s, tcg_op2, rm, elt, memop);
10369 
10370             if (accop == 0) {
10371                 tcg_passres = tcg_res[pass];
10372             } else {
10373                 tcg_passres = tcg_temp_new_i64();
10374             }
10375 
10376             switch (opcode) {
10377             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10378                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10379                 break;
10380             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10381                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10382                 break;
10383             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10384             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10385             {
10386                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10387                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10388 
10389                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10390                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10391                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10392                                     tcg_passres,
10393                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10394                 break;
10395             }
10396             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10397             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10398             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10399                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10400                 break;
10401             case 9: /* SQDMLAL, SQDMLAL2 */
10402             case 11: /* SQDMLSL, SQDMLSL2 */
10403             case 13: /* SQDMULL, SQDMULL2 */
10404                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10405                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10406                                                   tcg_passres, tcg_passres);
10407                 break;
10408             default:
10409                 g_assert_not_reached();
10410             }
10411 
10412             if (opcode == 9 || opcode == 11) {
10413                 /* saturating accumulate ops */
10414                 if (accop < 0) {
10415                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10416                 }
10417                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10418                                                   tcg_res[pass], tcg_passres);
10419             } else if (accop > 0) {
10420                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10421             } else if (accop < 0) {
10422                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10423             }
10424         }
10425     } else {
10426         /* size 0 or 1, generally helper functions */
10427         for (pass = 0; pass < 2; pass++) {
10428             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10429             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10430             TCGv_i64 tcg_passres;
10431             int elt = pass + is_q * 2;
10432 
10433             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10434             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10435 
10436             if (accop == 0) {
10437                 tcg_passres = tcg_res[pass];
10438             } else {
10439                 tcg_passres = tcg_temp_new_i64();
10440             }
10441 
10442             switch (opcode) {
10443             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10444             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10445             {
10446                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10447                 static NeonGenWidenFn * const widenfns[2][2] = {
10448                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10449                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10450                 };
10451                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10452 
10453                 widenfn(tcg_op2_64, tcg_op2);
10454                 widenfn(tcg_passres, tcg_op1);
10455                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10456                               tcg_passres, tcg_op2_64);
10457                 break;
10458             }
10459             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10460             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10461                 if (size == 0) {
10462                     if (is_u) {
10463                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10464                     } else {
10465                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10466                     }
10467                 } else {
10468                     if (is_u) {
10469                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10470                     } else {
10471                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10472                     }
10473                 }
10474                 break;
10475             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10476             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10477             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10478                 if (size == 0) {
10479                     if (is_u) {
10480                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10481                     } else {
10482                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10483                     }
10484                 } else {
10485                     if (is_u) {
10486                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10487                     } else {
10488                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10489                     }
10490                 }
10491                 break;
10492             case 9: /* SQDMLAL, SQDMLAL2 */
10493             case 11: /* SQDMLSL, SQDMLSL2 */
10494             case 13: /* SQDMULL, SQDMULL2 */
10495                 assert(size == 1);
10496                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10497                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10498                                                   tcg_passres, tcg_passres);
10499                 break;
10500             default:
10501                 g_assert_not_reached();
10502             }
10503 
10504             if (accop != 0) {
10505                 if (opcode == 9 || opcode == 11) {
10506                     /* saturating accumulate ops */
10507                     if (accop < 0) {
10508                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10509                     }
10510                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10511                                                       tcg_res[pass],
10512                                                       tcg_passres);
10513                 } else {
10514                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10515                                   tcg_res[pass], tcg_passres);
10516                 }
10517             }
10518         }
10519     }
10520 
10521     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10522     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10523 }
10524 
10525 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10526                             int opcode, int rd, int rn, int rm)
10527 {
10528     TCGv_i64 tcg_res[2];
10529     int part = is_q ? 2 : 0;
10530     int pass;
10531 
10532     for (pass = 0; pass < 2; pass++) {
10533         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10534         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10535         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10536         static NeonGenWidenFn * const widenfns[3][2] = {
10537             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10538             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10539             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10540         };
10541         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10542 
10543         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10544         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10545         widenfn(tcg_op2_wide, tcg_op2);
10546         tcg_res[pass] = tcg_temp_new_i64();
10547         gen_neon_addl(size, (opcode == 3),
10548                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10549     }
10550 
10551     for (pass = 0; pass < 2; pass++) {
10552         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10553     }
10554 }
10555 
10556 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10557 {
10558     tcg_gen_addi_i64(in, in, 1U << 31);
10559     tcg_gen_extrh_i64_i32(res, in);
10560 }
10561 
10562 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10563                                  int opcode, int rd, int rn, int rm)
10564 {
10565     TCGv_i32 tcg_res[2];
10566     int part = is_q ? 2 : 0;
10567     int pass;
10568 
10569     for (pass = 0; pass < 2; pass++) {
10570         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10571         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10572         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10573         static NeonGenNarrowFn * const narrowfns[3][2] = {
10574             { gen_helper_neon_narrow_high_u8,
10575               gen_helper_neon_narrow_round_high_u8 },
10576             { gen_helper_neon_narrow_high_u16,
10577               gen_helper_neon_narrow_round_high_u16 },
10578             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10579         };
10580         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10581 
10582         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10583         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10584 
10585         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10586 
10587         tcg_res[pass] = tcg_temp_new_i32();
10588         gennarrow(tcg_res[pass], tcg_wideres);
10589     }
10590 
10591     for (pass = 0; pass < 2; pass++) {
10592         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10593     }
10594     clear_vec_high(s, is_q, rd);
10595 }
10596 
10597 /* AdvSIMD three different
10598  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10599  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10600  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10601  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10602  */
10603 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10604 {
10605     /* Instructions in this group fall into three basic classes
10606      * (in each case with the operation working on each element in
10607      * the input vectors):
10608      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10609      *     128 bit input)
10610      * (2) wide 64 x 128 -> 128
10611      * (3) narrowing 128 x 128 -> 64
10612      * Here we do initial decode, catch unallocated cases and
10613      * dispatch to separate functions for each class.
10614      */
10615     int is_q = extract32(insn, 30, 1);
10616     int is_u = extract32(insn, 29, 1);
10617     int size = extract32(insn, 22, 2);
10618     int opcode = extract32(insn, 12, 4);
10619     int rm = extract32(insn, 16, 5);
10620     int rn = extract32(insn, 5, 5);
10621     int rd = extract32(insn, 0, 5);
10622 
10623     switch (opcode) {
10624     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10625     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10626         /* 64 x 128 -> 128 */
10627         if (size == 3) {
10628             unallocated_encoding(s);
10629             return;
10630         }
10631         if (!fp_access_check(s)) {
10632             return;
10633         }
10634         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10635         break;
10636     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10637     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10638         /* 128 x 128 -> 64 */
10639         if (size == 3) {
10640             unallocated_encoding(s);
10641             return;
10642         }
10643         if (!fp_access_check(s)) {
10644             return;
10645         }
10646         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10647         break;
10648     case 14: /* PMULL, PMULL2 */
10649         if (is_u) {
10650             unallocated_encoding(s);
10651             return;
10652         }
10653         switch (size) {
10654         case 0: /* PMULL.P8 */
10655             if (!fp_access_check(s)) {
10656                 return;
10657             }
10658             /* The Q field specifies lo/hi half input for this insn.  */
10659             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10660                              gen_helper_neon_pmull_h);
10661             break;
10662 
10663         case 3: /* PMULL.P64 */
10664             if (!dc_isar_feature(aa64_pmull, s)) {
10665                 unallocated_encoding(s);
10666                 return;
10667             }
10668             if (!fp_access_check(s)) {
10669                 return;
10670             }
10671             /* The Q field specifies lo/hi half input for this insn.  */
10672             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10673                              gen_helper_gvec_pmull_q);
10674             break;
10675 
10676         default:
10677             unallocated_encoding(s);
10678             break;
10679         }
10680         return;
10681     case 9: /* SQDMLAL, SQDMLAL2 */
10682     case 11: /* SQDMLSL, SQDMLSL2 */
10683     case 13: /* SQDMULL, SQDMULL2 */
10684         if (is_u || size == 0) {
10685             unallocated_encoding(s);
10686             return;
10687         }
10688         /* fall through */
10689     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10690     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10691     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10692     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10693     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10694     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10695     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10696         /* 64 x 64 -> 128 */
10697         if (size == 3) {
10698             unallocated_encoding(s);
10699             return;
10700         }
10701         if (!fp_access_check(s)) {
10702             return;
10703         }
10704 
10705         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10706         break;
10707     default:
10708         /* opcode 15 not allocated */
10709         unallocated_encoding(s);
10710         break;
10711     }
10712 }
10713 
10714 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10715 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10716 {
10717     int rd = extract32(insn, 0, 5);
10718     int rn = extract32(insn, 5, 5);
10719     int rm = extract32(insn, 16, 5);
10720     int size = extract32(insn, 22, 2);
10721     bool is_u = extract32(insn, 29, 1);
10722     bool is_q = extract32(insn, 30, 1);
10723 
10724     if (!fp_access_check(s)) {
10725         return;
10726     }
10727 
10728     switch (size + 4 * is_u) {
10729     case 0: /* AND */
10730         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10731         return;
10732     case 1: /* BIC */
10733         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10734         return;
10735     case 2: /* ORR */
10736         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10737         return;
10738     case 3: /* ORN */
10739         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10740         return;
10741     case 4: /* EOR */
10742         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10743         return;
10744 
10745     case 5: /* BSL bitwise select */
10746         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10747         return;
10748     case 6: /* BIT, bitwise insert if true */
10749         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10750         return;
10751     case 7: /* BIF, bitwise insert if false */
10752         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10753         return;
10754 
10755     default:
10756         g_assert_not_reached();
10757     }
10758 }
10759 
10760 /* Pairwise op subgroup of C3.6.16.
10761  *
10762  * This is called directly or via the handle_3same_float for float pairwise
10763  * operations where the opcode and size are calculated differently.
10764  */
10765 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10766                                    int size, int rn, int rm, int rd)
10767 {
10768     TCGv_ptr fpst;
10769     int pass;
10770 
10771     /* Floating point operations need fpst */
10772     if (opcode >= 0x58) {
10773         fpst = fpstatus_ptr(FPST_FPCR);
10774     } else {
10775         fpst = NULL;
10776     }
10777 
10778     if (!fp_access_check(s)) {
10779         return;
10780     }
10781 
10782     /* These operations work on the concatenated rm:rn, with each pair of
10783      * adjacent elements being operated on to produce an element in the result.
10784      */
10785     if (size == 3) {
10786         TCGv_i64 tcg_res[2];
10787 
10788         for (pass = 0; pass < 2; pass++) {
10789             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10790             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10791             int passreg = (pass == 0) ? rn : rm;
10792 
10793             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10794             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10795             tcg_res[pass] = tcg_temp_new_i64();
10796 
10797             switch (opcode) {
10798             case 0x17: /* ADDP */
10799                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10800                 break;
10801             case 0x58: /* FMAXNMP */
10802                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10803                 break;
10804             case 0x5a: /* FADDP */
10805                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10806                 break;
10807             case 0x5e: /* FMAXP */
10808                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10809                 break;
10810             case 0x78: /* FMINNMP */
10811                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10812                 break;
10813             case 0x7e: /* FMINP */
10814                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10815                 break;
10816             default:
10817                 g_assert_not_reached();
10818             }
10819         }
10820 
10821         for (pass = 0; pass < 2; pass++) {
10822             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10823         }
10824     } else {
10825         int maxpass = is_q ? 4 : 2;
10826         TCGv_i32 tcg_res[4];
10827 
10828         for (pass = 0; pass < maxpass; pass++) {
10829             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10830             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10831             NeonGenTwoOpFn *genfn = NULL;
10832             int passreg = pass < (maxpass / 2) ? rn : rm;
10833             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10834 
10835             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10836             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10837             tcg_res[pass] = tcg_temp_new_i32();
10838 
10839             switch (opcode) {
10840             case 0x17: /* ADDP */
10841             {
10842                 static NeonGenTwoOpFn * const fns[3] = {
10843                     gen_helper_neon_padd_u8,
10844                     gen_helper_neon_padd_u16,
10845                     tcg_gen_add_i32,
10846                 };
10847                 genfn = fns[size];
10848                 break;
10849             }
10850             case 0x14: /* SMAXP, UMAXP */
10851             {
10852                 static NeonGenTwoOpFn * const fns[3][2] = {
10853                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10854                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10855                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10856                 };
10857                 genfn = fns[size][u];
10858                 break;
10859             }
10860             case 0x15: /* SMINP, UMINP */
10861             {
10862                 static NeonGenTwoOpFn * const fns[3][2] = {
10863                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10864                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10865                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10866                 };
10867                 genfn = fns[size][u];
10868                 break;
10869             }
10870             /* The FP operations are all on single floats (32 bit) */
10871             case 0x58: /* FMAXNMP */
10872                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10873                 break;
10874             case 0x5a: /* FADDP */
10875                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10876                 break;
10877             case 0x5e: /* FMAXP */
10878                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10879                 break;
10880             case 0x78: /* FMINNMP */
10881                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10882                 break;
10883             case 0x7e: /* FMINP */
10884                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10885                 break;
10886             default:
10887                 g_assert_not_reached();
10888             }
10889 
10890             /* FP ops called directly, otherwise call now */
10891             if (genfn) {
10892                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10893             }
10894         }
10895 
10896         for (pass = 0; pass < maxpass; pass++) {
10897             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10898         }
10899         clear_vec_high(s, is_q, rd);
10900     }
10901 }
10902 
10903 /* Floating point op subgroup of C3.6.16. */
10904 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10905 {
10906     /* For floating point ops, the U, size[1] and opcode bits
10907      * together indicate the operation. size[0] indicates single
10908      * or double.
10909      */
10910     int fpopcode = extract32(insn, 11, 5)
10911         | (extract32(insn, 23, 1) << 5)
10912         | (extract32(insn, 29, 1) << 6);
10913     int is_q = extract32(insn, 30, 1);
10914     int size = extract32(insn, 22, 1);
10915     int rm = extract32(insn, 16, 5);
10916     int rn = extract32(insn, 5, 5);
10917     int rd = extract32(insn, 0, 5);
10918 
10919     int datasize = is_q ? 128 : 64;
10920     int esize = 32 << size;
10921     int elements = datasize / esize;
10922 
10923     if (size == 1 && !is_q) {
10924         unallocated_encoding(s);
10925         return;
10926     }
10927 
10928     switch (fpopcode) {
10929     case 0x58: /* FMAXNMP */
10930     case 0x5a: /* FADDP */
10931     case 0x5e: /* FMAXP */
10932     case 0x78: /* FMINNMP */
10933     case 0x7e: /* FMINP */
10934         if (size && !is_q) {
10935             unallocated_encoding(s);
10936             return;
10937         }
10938         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10939                                rn, rm, rd);
10940         return;
10941     case 0x1b: /* FMULX */
10942     case 0x1f: /* FRECPS */
10943     case 0x3f: /* FRSQRTS */
10944     case 0x5d: /* FACGE */
10945     case 0x7d: /* FACGT */
10946     case 0x19: /* FMLA */
10947     case 0x39: /* FMLS */
10948     case 0x18: /* FMAXNM */
10949     case 0x1a: /* FADD */
10950     case 0x1c: /* FCMEQ */
10951     case 0x1e: /* FMAX */
10952     case 0x38: /* FMINNM */
10953     case 0x3a: /* FSUB */
10954     case 0x3e: /* FMIN */
10955     case 0x5b: /* FMUL */
10956     case 0x5c: /* FCMGE */
10957     case 0x5f: /* FDIV */
10958     case 0x7a: /* FABD */
10959     case 0x7c: /* FCMGT */
10960         if (!fp_access_check(s)) {
10961             return;
10962         }
10963         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10964         return;
10965 
10966     case 0x1d: /* FMLAL  */
10967     case 0x3d: /* FMLSL  */
10968     case 0x59: /* FMLAL2 */
10969     case 0x79: /* FMLSL2 */
10970         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10971             unallocated_encoding(s);
10972             return;
10973         }
10974         if (fp_access_check(s)) {
10975             int is_s = extract32(insn, 23, 1);
10976             int is_2 = extract32(insn, 29, 1);
10977             int data = (is_2 << 1) | is_s;
10978             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10979                                vec_full_reg_offset(s, rn),
10980                                vec_full_reg_offset(s, rm), tcg_env,
10981                                is_q ? 16 : 8, vec_full_reg_size(s),
10982                                data, gen_helper_gvec_fmlal_a64);
10983         }
10984         return;
10985 
10986     default:
10987         unallocated_encoding(s);
10988         return;
10989     }
10990 }
10991 
10992 /* Integer op subgroup of C3.6.16. */
10993 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10994 {
10995     int is_q = extract32(insn, 30, 1);
10996     int u = extract32(insn, 29, 1);
10997     int size = extract32(insn, 22, 2);
10998     int opcode = extract32(insn, 11, 5);
10999     int rm = extract32(insn, 16, 5);
11000     int rn = extract32(insn, 5, 5);
11001     int rd = extract32(insn, 0, 5);
11002     int pass;
11003     TCGCond cond;
11004 
11005     switch (opcode) {
11006     case 0x13: /* MUL, PMUL */
11007         if (u && size != 0) {
11008             unallocated_encoding(s);
11009             return;
11010         }
11011         /* fall through */
11012     case 0x0: /* SHADD, UHADD */
11013     case 0x2: /* SRHADD, URHADD */
11014     case 0x4: /* SHSUB, UHSUB */
11015     case 0xc: /* SMAX, UMAX */
11016     case 0xd: /* SMIN, UMIN */
11017     case 0xe: /* SABD, UABD */
11018     case 0xf: /* SABA, UABA */
11019     case 0x12: /* MLA, MLS */
11020         if (size == 3) {
11021             unallocated_encoding(s);
11022             return;
11023         }
11024         break;
11025     case 0x16: /* SQDMULH, SQRDMULH */
11026         if (size == 0 || size == 3) {
11027             unallocated_encoding(s);
11028             return;
11029         }
11030         break;
11031     default:
11032         if (size == 3 && !is_q) {
11033             unallocated_encoding(s);
11034             return;
11035         }
11036         break;
11037     }
11038 
11039     if (!fp_access_check(s)) {
11040         return;
11041     }
11042 
11043     switch (opcode) {
11044     case 0x01: /* SQADD, UQADD */
11045         if (u) {
11046             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11047         } else {
11048             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11049         }
11050         return;
11051     case 0x05: /* SQSUB, UQSUB */
11052         if (u) {
11053             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11054         } else {
11055             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11056         }
11057         return;
11058     case 0x08: /* SSHL, USHL */
11059         if (u) {
11060             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11061         } else {
11062             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11063         }
11064         return;
11065     case 0x0c: /* SMAX, UMAX */
11066         if (u) {
11067             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11068         } else {
11069             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11070         }
11071         return;
11072     case 0x0d: /* SMIN, UMIN */
11073         if (u) {
11074             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11075         } else {
11076             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11077         }
11078         return;
11079     case 0xe: /* SABD, UABD */
11080         if (u) {
11081             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11082         } else {
11083             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11084         }
11085         return;
11086     case 0xf: /* SABA, UABA */
11087         if (u) {
11088             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11089         } else {
11090             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11091         }
11092         return;
11093     case 0x10: /* ADD, SUB */
11094         if (u) {
11095             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11096         } else {
11097             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11098         }
11099         return;
11100     case 0x13: /* MUL, PMUL */
11101         if (!u) { /* MUL */
11102             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11103         } else {  /* PMUL */
11104             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11105         }
11106         return;
11107     case 0x12: /* MLA, MLS */
11108         if (u) {
11109             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11110         } else {
11111             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11112         }
11113         return;
11114     case 0x16: /* SQDMULH, SQRDMULH */
11115         {
11116             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11117                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11118                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11119             };
11120             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11121         }
11122         return;
11123     case 0x11:
11124         if (!u) { /* CMTST */
11125             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11126             return;
11127         }
11128         /* else CMEQ */
11129         cond = TCG_COND_EQ;
11130         goto do_gvec_cmp;
11131     case 0x06: /* CMGT, CMHI */
11132         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11133         goto do_gvec_cmp;
11134     case 0x07: /* CMGE, CMHS */
11135         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11136     do_gvec_cmp:
11137         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11138                          vec_full_reg_offset(s, rn),
11139                          vec_full_reg_offset(s, rm),
11140                          is_q ? 16 : 8, vec_full_reg_size(s));
11141         return;
11142     }
11143 
11144     if (size == 3) {
11145         assert(is_q);
11146         for (pass = 0; pass < 2; pass++) {
11147             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11148             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11149             TCGv_i64 tcg_res = tcg_temp_new_i64();
11150 
11151             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11152             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11153 
11154             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11155 
11156             write_vec_element(s, tcg_res, rd, pass, MO_64);
11157         }
11158     } else {
11159         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11160             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11161             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11162             TCGv_i32 tcg_res = tcg_temp_new_i32();
11163             NeonGenTwoOpFn *genfn = NULL;
11164             NeonGenTwoOpEnvFn *genenvfn = NULL;
11165 
11166             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11167             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11168 
11169             switch (opcode) {
11170             case 0x0: /* SHADD, UHADD */
11171             {
11172                 static NeonGenTwoOpFn * const fns[3][2] = {
11173                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11174                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11175                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11176                 };
11177                 genfn = fns[size][u];
11178                 break;
11179             }
11180             case 0x2: /* SRHADD, URHADD */
11181             {
11182                 static NeonGenTwoOpFn * const fns[3][2] = {
11183                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11184                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11185                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11186                 };
11187                 genfn = fns[size][u];
11188                 break;
11189             }
11190             case 0x4: /* SHSUB, UHSUB */
11191             {
11192                 static NeonGenTwoOpFn * const fns[3][2] = {
11193                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11194                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11195                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11196                 };
11197                 genfn = fns[size][u];
11198                 break;
11199             }
11200             case 0x9: /* SQSHL, UQSHL */
11201             {
11202                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11203                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11204                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11205                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11206                 };
11207                 genenvfn = fns[size][u];
11208                 break;
11209             }
11210             case 0xa: /* SRSHL, URSHL */
11211             {
11212                 static NeonGenTwoOpFn * const fns[3][2] = {
11213                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11214                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11215                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11216                 };
11217                 genfn = fns[size][u];
11218                 break;
11219             }
11220             case 0xb: /* SQRSHL, UQRSHL */
11221             {
11222                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11223                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11224                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11225                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11226                 };
11227                 genenvfn = fns[size][u];
11228                 break;
11229             }
11230             default:
11231                 g_assert_not_reached();
11232             }
11233 
11234             if (genenvfn) {
11235                 genenvfn(tcg_res, tcg_env, tcg_op1, tcg_op2);
11236             } else {
11237                 genfn(tcg_res, tcg_op1, tcg_op2);
11238             }
11239 
11240             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11241         }
11242     }
11243     clear_vec_high(s, is_q, rd);
11244 }
11245 
11246 /* AdvSIMD three same
11247  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11248  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11249  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11250  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11251  */
11252 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11253 {
11254     int opcode = extract32(insn, 11, 5);
11255 
11256     switch (opcode) {
11257     case 0x3: /* logic ops */
11258         disas_simd_3same_logic(s, insn);
11259         break;
11260     case 0x17: /* ADDP */
11261     case 0x14: /* SMAXP, UMAXP */
11262     case 0x15: /* SMINP, UMINP */
11263     {
11264         /* Pairwise operations */
11265         int is_q = extract32(insn, 30, 1);
11266         int u = extract32(insn, 29, 1);
11267         int size = extract32(insn, 22, 2);
11268         int rm = extract32(insn, 16, 5);
11269         int rn = extract32(insn, 5, 5);
11270         int rd = extract32(insn, 0, 5);
11271         if (opcode == 0x17) {
11272             if (u || (size == 3 && !is_q)) {
11273                 unallocated_encoding(s);
11274                 return;
11275             }
11276         } else {
11277             if (size == 3) {
11278                 unallocated_encoding(s);
11279                 return;
11280             }
11281         }
11282         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11283         break;
11284     }
11285     case 0x18 ... 0x31:
11286         /* floating point ops, sz[1] and U are part of opcode */
11287         disas_simd_3same_float(s, insn);
11288         break;
11289     default:
11290         disas_simd_3same_int(s, insn);
11291         break;
11292     }
11293 }
11294 
11295 /*
11296  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11297  *
11298  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11299  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11300  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11301  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11302  *
11303  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11304  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11305  *
11306  */
11307 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11308 {
11309     int opcode = extract32(insn, 11, 3);
11310     int u = extract32(insn, 29, 1);
11311     int a = extract32(insn, 23, 1);
11312     int is_q = extract32(insn, 30, 1);
11313     int rm = extract32(insn, 16, 5);
11314     int rn = extract32(insn, 5, 5);
11315     int rd = extract32(insn, 0, 5);
11316     /*
11317      * For these floating point ops, the U, a and opcode bits
11318      * together indicate the operation.
11319      */
11320     int fpopcode = opcode | (a << 3) | (u << 4);
11321     int datasize = is_q ? 128 : 64;
11322     int elements = datasize / 16;
11323     bool pairwise;
11324     TCGv_ptr fpst;
11325     int pass;
11326 
11327     switch (fpopcode) {
11328     case 0x0: /* FMAXNM */
11329     case 0x1: /* FMLA */
11330     case 0x2: /* FADD */
11331     case 0x3: /* FMULX */
11332     case 0x4: /* FCMEQ */
11333     case 0x6: /* FMAX */
11334     case 0x7: /* FRECPS */
11335     case 0x8: /* FMINNM */
11336     case 0x9: /* FMLS */
11337     case 0xa: /* FSUB */
11338     case 0xe: /* FMIN */
11339     case 0xf: /* FRSQRTS */
11340     case 0x13: /* FMUL */
11341     case 0x14: /* FCMGE */
11342     case 0x15: /* FACGE */
11343     case 0x17: /* FDIV */
11344     case 0x1a: /* FABD */
11345     case 0x1c: /* FCMGT */
11346     case 0x1d: /* FACGT */
11347         pairwise = false;
11348         break;
11349     case 0x10: /* FMAXNMP */
11350     case 0x12: /* FADDP */
11351     case 0x16: /* FMAXP */
11352     case 0x18: /* FMINNMP */
11353     case 0x1e: /* FMINP */
11354         pairwise = true;
11355         break;
11356     default:
11357         unallocated_encoding(s);
11358         return;
11359     }
11360 
11361     if (!dc_isar_feature(aa64_fp16, s)) {
11362         unallocated_encoding(s);
11363         return;
11364     }
11365 
11366     if (!fp_access_check(s)) {
11367         return;
11368     }
11369 
11370     fpst = fpstatus_ptr(FPST_FPCR_F16);
11371 
11372     if (pairwise) {
11373         int maxpass = is_q ? 8 : 4;
11374         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11375         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11376         TCGv_i32 tcg_res[8];
11377 
11378         for (pass = 0; pass < maxpass; pass++) {
11379             int passreg = pass < (maxpass / 2) ? rn : rm;
11380             int passelt = (pass << 1) & (maxpass - 1);
11381 
11382             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11383             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11384             tcg_res[pass] = tcg_temp_new_i32();
11385 
11386             switch (fpopcode) {
11387             case 0x10: /* FMAXNMP */
11388                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11389                                            fpst);
11390                 break;
11391             case 0x12: /* FADDP */
11392                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11393                 break;
11394             case 0x16: /* FMAXP */
11395                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11396                 break;
11397             case 0x18: /* FMINNMP */
11398                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11399                                            fpst);
11400                 break;
11401             case 0x1e: /* FMINP */
11402                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11403                 break;
11404             default:
11405                 g_assert_not_reached();
11406             }
11407         }
11408 
11409         for (pass = 0; pass < maxpass; pass++) {
11410             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11411         }
11412     } else {
11413         for (pass = 0; pass < elements; pass++) {
11414             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11415             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11416             TCGv_i32 tcg_res = tcg_temp_new_i32();
11417 
11418             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11419             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11420 
11421             switch (fpopcode) {
11422             case 0x0: /* FMAXNM */
11423                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11424                 break;
11425             case 0x1: /* FMLA */
11426                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11427                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11428                                            fpst);
11429                 break;
11430             case 0x2: /* FADD */
11431                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11432                 break;
11433             case 0x3: /* FMULX */
11434                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11435                 break;
11436             case 0x4: /* FCMEQ */
11437                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11438                 break;
11439             case 0x6: /* FMAX */
11440                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11441                 break;
11442             case 0x7: /* FRECPS */
11443                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11444                 break;
11445             case 0x8: /* FMINNM */
11446                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11447                 break;
11448             case 0x9: /* FMLS */
11449                 /* As usual for ARM, separate negation for fused multiply-add */
11450                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11451                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11452                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11453                                            fpst);
11454                 break;
11455             case 0xa: /* FSUB */
11456                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11457                 break;
11458             case 0xe: /* FMIN */
11459                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11460                 break;
11461             case 0xf: /* FRSQRTS */
11462                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11463                 break;
11464             case 0x13: /* FMUL */
11465                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11466                 break;
11467             case 0x14: /* FCMGE */
11468                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11469                 break;
11470             case 0x15: /* FACGE */
11471                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11472                 break;
11473             case 0x17: /* FDIV */
11474                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11475                 break;
11476             case 0x1a: /* FABD */
11477                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11478                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11479                 break;
11480             case 0x1c: /* FCMGT */
11481                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11482                 break;
11483             case 0x1d: /* FACGT */
11484                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11485                 break;
11486             default:
11487                 g_assert_not_reached();
11488             }
11489 
11490             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11491         }
11492     }
11493 
11494     clear_vec_high(s, is_q, rd);
11495 }
11496 
11497 /* AdvSIMD three same extra
11498  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11499  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11500  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11501  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11502  */
11503 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11504 {
11505     int rd = extract32(insn, 0, 5);
11506     int rn = extract32(insn, 5, 5);
11507     int opcode = extract32(insn, 11, 4);
11508     int rm = extract32(insn, 16, 5);
11509     int size = extract32(insn, 22, 2);
11510     bool u = extract32(insn, 29, 1);
11511     bool is_q = extract32(insn, 30, 1);
11512     bool feature;
11513     int rot;
11514 
11515     switch (u * 16 + opcode) {
11516     case 0x10: /* SQRDMLAH (vector) */
11517     case 0x11: /* SQRDMLSH (vector) */
11518         if (size != 1 && size != 2) {
11519             unallocated_encoding(s);
11520             return;
11521         }
11522         feature = dc_isar_feature(aa64_rdm, s);
11523         break;
11524     case 0x02: /* SDOT (vector) */
11525     case 0x12: /* UDOT (vector) */
11526         if (size != MO_32) {
11527             unallocated_encoding(s);
11528             return;
11529         }
11530         feature = dc_isar_feature(aa64_dp, s);
11531         break;
11532     case 0x03: /* USDOT */
11533         if (size != MO_32) {
11534             unallocated_encoding(s);
11535             return;
11536         }
11537         feature = dc_isar_feature(aa64_i8mm, s);
11538         break;
11539     case 0x04: /* SMMLA */
11540     case 0x14: /* UMMLA */
11541     case 0x05: /* USMMLA */
11542         if (!is_q || size != MO_32) {
11543             unallocated_encoding(s);
11544             return;
11545         }
11546         feature = dc_isar_feature(aa64_i8mm, s);
11547         break;
11548     case 0x18: /* FCMLA, #0 */
11549     case 0x19: /* FCMLA, #90 */
11550     case 0x1a: /* FCMLA, #180 */
11551     case 0x1b: /* FCMLA, #270 */
11552     case 0x1c: /* FCADD, #90 */
11553     case 0x1e: /* FCADD, #270 */
11554         if (size == 0
11555             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11556             || (size == 3 && !is_q)) {
11557             unallocated_encoding(s);
11558             return;
11559         }
11560         feature = dc_isar_feature(aa64_fcma, s);
11561         break;
11562     case 0x1d: /* BFMMLA */
11563         if (size != MO_16 || !is_q) {
11564             unallocated_encoding(s);
11565             return;
11566         }
11567         feature = dc_isar_feature(aa64_bf16, s);
11568         break;
11569     case 0x1f:
11570         switch (size) {
11571         case 1: /* BFDOT */
11572         case 3: /* BFMLAL{B,T} */
11573             feature = dc_isar_feature(aa64_bf16, s);
11574             break;
11575         default:
11576             unallocated_encoding(s);
11577             return;
11578         }
11579         break;
11580     default:
11581         unallocated_encoding(s);
11582         return;
11583     }
11584     if (!feature) {
11585         unallocated_encoding(s);
11586         return;
11587     }
11588     if (!fp_access_check(s)) {
11589         return;
11590     }
11591 
11592     switch (opcode) {
11593     case 0x0: /* SQRDMLAH (vector) */
11594         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11595         return;
11596 
11597     case 0x1: /* SQRDMLSH (vector) */
11598         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11599         return;
11600 
11601     case 0x2: /* SDOT / UDOT */
11602         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11603                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11604         return;
11605 
11606     case 0x3: /* USDOT */
11607         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11608         return;
11609 
11610     case 0x04: /* SMMLA, UMMLA */
11611         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11612                          u ? gen_helper_gvec_ummla_b
11613                          : gen_helper_gvec_smmla_b);
11614         return;
11615     case 0x05: /* USMMLA */
11616         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11617         return;
11618 
11619     case 0x8: /* FCMLA, #0 */
11620     case 0x9: /* FCMLA, #90 */
11621     case 0xa: /* FCMLA, #180 */
11622     case 0xb: /* FCMLA, #270 */
11623         rot = extract32(opcode, 0, 2);
11624         switch (size) {
11625         case 1:
11626             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11627                               gen_helper_gvec_fcmlah);
11628             break;
11629         case 2:
11630             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11631                               gen_helper_gvec_fcmlas);
11632             break;
11633         case 3:
11634             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11635                               gen_helper_gvec_fcmlad);
11636             break;
11637         default:
11638             g_assert_not_reached();
11639         }
11640         return;
11641 
11642     case 0xc: /* FCADD, #90 */
11643     case 0xe: /* FCADD, #270 */
11644         rot = extract32(opcode, 1, 1);
11645         switch (size) {
11646         case 1:
11647             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11648                               gen_helper_gvec_fcaddh);
11649             break;
11650         case 2:
11651             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11652                               gen_helper_gvec_fcadds);
11653             break;
11654         case 3:
11655             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11656                               gen_helper_gvec_fcaddd);
11657             break;
11658         default:
11659             g_assert_not_reached();
11660         }
11661         return;
11662 
11663     case 0xd: /* BFMMLA */
11664         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11665         return;
11666     case 0xf:
11667         switch (size) {
11668         case 1: /* BFDOT */
11669             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11670             break;
11671         case 3: /* BFMLAL{B,T} */
11672             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11673                               gen_helper_gvec_bfmlal);
11674             break;
11675         default:
11676             g_assert_not_reached();
11677         }
11678         return;
11679 
11680     default:
11681         g_assert_not_reached();
11682     }
11683 }
11684 
11685 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11686                                   int size, int rn, int rd)
11687 {
11688     /* Handle 2-reg-misc ops which are widening (so each size element
11689      * in the source becomes a 2*size element in the destination.
11690      * The only instruction like this is FCVTL.
11691      */
11692     int pass;
11693 
11694     if (size == 3) {
11695         /* 32 -> 64 bit fp conversion */
11696         TCGv_i64 tcg_res[2];
11697         int srcelt = is_q ? 2 : 0;
11698 
11699         for (pass = 0; pass < 2; pass++) {
11700             TCGv_i32 tcg_op = tcg_temp_new_i32();
11701             tcg_res[pass] = tcg_temp_new_i64();
11702 
11703             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11704             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11705         }
11706         for (pass = 0; pass < 2; pass++) {
11707             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11708         }
11709     } else {
11710         /* 16 -> 32 bit fp conversion */
11711         int srcelt = is_q ? 4 : 0;
11712         TCGv_i32 tcg_res[4];
11713         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11714         TCGv_i32 ahp = get_ahp_flag();
11715 
11716         for (pass = 0; pass < 4; pass++) {
11717             tcg_res[pass] = tcg_temp_new_i32();
11718 
11719             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11720             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11721                                            fpst, ahp);
11722         }
11723         for (pass = 0; pass < 4; pass++) {
11724             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11725         }
11726     }
11727 }
11728 
11729 static void handle_rev(DisasContext *s, int opcode, bool u,
11730                        bool is_q, int size, int rn, int rd)
11731 {
11732     int op = (opcode << 1) | u;
11733     int opsz = op + size;
11734     int grp_size = 3 - opsz;
11735     int dsize = is_q ? 128 : 64;
11736     int i;
11737 
11738     if (opsz >= 3) {
11739         unallocated_encoding(s);
11740         return;
11741     }
11742 
11743     if (!fp_access_check(s)) {
11744         return;
11745     }
11746 
11747     if (size == 0) {
11748         /* Special case bytes, use bswap op on each group of elements */
11749         int groups = dsize / (8 << grp_size);
11750 
11751         for (i = 0; i < groups; i++) {
11752             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11753 
11754             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11755             switch (grp_size) {
11756             case MO_16:
11757                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11758                 break;
11759             case MO_32:
11760                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11761                 break;
11762             case MO_64:
11763                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11764                 break;
11765             default:
11766                 g_assert_not_reached();
11767             }
11768             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11769         }
11770         clear_vec_high(s, is_q, rd);
11771     } else {
11772         int revmask = (1 << grp_size) - 1;
11773         int esize = 8 << size;
11774         int elements = dsize / esize;
11775         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11776         TCGv_i64 tcg_rd[2];
11777 
11778         for (i = 0; i < 2; i++) {
11779             tcg_rd[i] = tcg_temp_new_i64();
11780             tcg_gen_movi_i64(tcg_rd[i], 0);
11781         }
11782 
11783         for (i = 0; i < elements; i++) {
11784             int e_rev = (i & 0xf) ^ revmask;
11785             int w = (e_rev * esize) / 64;
11786             int o = (e_rev * esize) % 64;
11787 
11788             read_vec_element(s, tcg_rn, rn, i, size);
11789             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11790         }
11791 
11792         for (i = 0; i < 2; i++) {
11793             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11794         }
11795         clear_vec_high(s, true, rd);
11796     }
11797 }
11798 
11799 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11800                                   bool is_q, int size, int rn, int rd)
11801 {
11802     /* Implement the pairwise operations from 2-misc:
11803      * SADDLP, UADDLP, SADALP, UADALP.
11804      * These all add pairs of elements in the input to produce a
11805      * double-width result element in the output (possibly accumulating).
11806      */
11807     bool accum = (opcode == 0x6);
11808     int maxpass = is_q ? 2 : 1;
11809     int pass;
11810     TCGv_i64 tcg_res[2];
11811 
11812     if (size == 2) {
11813         /* 32 + 32 -> 64 op */
11814         MemOp memop = size + (u ? 0 : MO_SIGN);
11815 
11816         for (pass = 0; pass < maxpass; pass++) {
11817             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11818             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11819 
11820             tcg_res[pass] = tcg_temp_new_i64();
11821 
11822             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11823             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11824             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11825             if (accum) {
11826                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11827                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11828             }
11829         }
11830     } else {
11831         for (pass = 0; pass < maxpass; pass++) {
11832             TCGv_i64 tcg_op = tcg_temp_new_i64();
11833             NeonGenOne64OpFn *genfn;
11834             static NeonGenOne64OpFn * const fns[2][2] = {
11835                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11836                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11837             };
11838 
11839             genfn = fns[size][u];
11840 
11841             tcg_res[pass] = tcg_temp_new_i64();
11842 
11843             read_vec_element(s, tcg_op, rn, pass, MO_64);
11844             genfn(tcg_res[pass], tcg_op);
11845 
11846             if (accum) {
11847                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11848                 if (size == 0) {
11849                     gen_helper_neon_addl_u16(tcg_res[pass],
11850                                              tcg_res[pass], tcg_op);
11851                 } else {
11852                     gen_helper_neon_addl_u32(tcg_res[pass],
11853                                              tcg_res[pass], tcg_op);
11854                 }
11855             }
11856         }
11857     }
11858     if (!is_q) {
11859         tcg_res[1] = tcg_constant_i64(0);
11860     }
11861     for (pass = 0; pass < 2; pass++) {
11862         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11863     }
11864 }
11865 
11866 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11867 {
11868     /* Implement SHLL and SHLL2 */
11869     int pass;
11870     int part = is_q ? 2 : 0;
11871     TCGv_i64 tcg_res[2];
11872 
11873     for (pass = 0; pass < 2; pass++) {
11874         static NeonGenWidenFn * const widenfns[3] = {
11875             gen_helper_neon_widen_u8,
11876             gen_helper_neon_widen_u16,
11877             tcg_gen_extu_i32_i64,
11878         };
11879         NeonGenWidenFn *widenfn = widenfns[size];
11880         TCGv_i32 tcg_op = tcg_temp_new_i32();
11881 
11882         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11883         tcg_res[pass] = tcg_temp_new_i64();
11884         widenfn(tcg_res[pass], tcg_op);
11885         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11886     }
11887 
11888     for (pass = 0; pass < 2; pass++) {
11889         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11890     }
11891 }
11892 
11893 /* AdvSIMD two reg misc
11894  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11895  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11896  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11897  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11898  */
11899 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11900 {
11901     int size = extract32(insn, 22, 2);
11902     int opcode = extract32(insn, 12, 5);
11903     bool u = extract32(insn, 29, 1);
11904     bool is_q = extract32(insn, 30, 1);
11905     int rn = extract32(insn, 5, 5);
11906     int rd = extract32(insn, 0, 5);
11907     bool need_fpstatus = false;
11908     int rmode = -1;
11909     TCGv_i32 tcg_rmode;
11910     TCGv_ptr tcg_fpstatus;
11911 
11912     switch (opcode) {
11913     case 0x0: /* REV64, REV32 */
11914     case 0x1: /* REV16 */
11915         handle_rev(s, opcode, u, is_q, size, rn, rd);
11916         return;
11917     case 0x5: /* CNT, NOT, RBIT */
11918         if (u && size == 0) {
11919             /* NOT */
11920             break;
11921         } else if (u && size == 1) {
11922             /* RBIT */
11923             break;
11924         } else if (!u && size == 0) {
11925             /* CNT */
11926             break;
11927         }
11928         unallocated_encoding(s);
11929         return;
11930     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11931     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11932         if (size == 3) {
11933             unallocated_encoding(s);
11934             return;
11935         }
11936         if (!fp_access_check(s)) {
11937             return;
11938         }
11939 
11940         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11941         return;
11942     case 0x4: /* CLS, CLZ */
11943         if (size == 3) {
11944             unallocated_encoding(s);
11945             return;
11946         }
11947         break;
11948     case 0x2: /* SADDLP, UADDLP */
11949     case 0x6: /* SADALP, UADALP */
11950         if (size == 3) {
11951             unallocated_encoding(s);
11952             return;
11953         }
11954         if (!fp_access_check(s)) {
11955             return;
11956         }
11957         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11958         return;
11959     case 0x13: /* SHLL, SHLL2 */
11960         if (u == 0 || size == 3) {
11961             unallocated_encoding(s);
11962             return;
11963         }
11964         if (!fp_access_check(s)) {
11965             return;
11966         }
11967         handle_shll(s, is_q, size, rn, rd);
11968         return;
11969     case 0xa: /* CMLT */
11970         if (u == 1) {
11971             unallocated_encoding(s);
11972             return;
11973         }
11974         /* fall through */
11975     case 0x8: /* CMGT, CMGE */
11976     case 0x9: /* CMEQ, CMLE */
11977     case 0xb: /* ABS, NEG */
11978         if (size == 3 && !is_q) {
11979             unallocated_encoding(s);
11980             return;
11981         }
11982         break;
11983     case 0x3: /* SUQADD, USQADD */
11984         if (size == 3 && !is_q) {
11985             unallocated_encoding(s);
11986             return;
11987         }
11988         if (!fp_access_check(s)) {
11989             return;
11990         }
11991         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11992         return;
11993     case 0x7: /* SQABS, SQNEG */
11994         if (size == 3 && !is_q) {
11995             unallocated_encoding(s);
11996             return;
11997         }
11998         break;
11999     case 0xc ... 0xf:
12000     case 0x16 ... 0x1f:
12001     {
12002         /* Floating point: U, size[1] and opcode indicate operation;
12003          * size[0] indicates single or double precision.
12004          */
12005         int is_double = extract32(size, 0, 1);
12006         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12007         size = is_double ? 3 : 2;
12008         switch (opcode) {
12009         case 0x2f: /* FABS */
12010         case 0x6f: /* FNEG */
12011             if (size == 3 && !is_q) {
12012                 unallocated_encoding(s);
12013                 return;
12014             }
12015             break;
12016         case 0x1d: /* SCVTF */
12017         case 0x5d: /* UCVTF */
12018         {
12019             bool is_signed = (opcode == 0x1d) ? true : false;
12020             int elements = is_double ? 2 : is_q ? 4 : 2;
12021             if (is_double && !is_q) {
12022                 unallocated_encoding(s);
12023                 return;
12024             }
12025             if (!fp_access_check(s)) {
12026                 return;
12027             }
12028             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12029             return;
12030         }
12031         case 0x2c: /* FCMGT (zero) */
12032         case 0x2d: /* FCMEQ (zero) */
12033         case 0x2e: /* FCMLT (zero) */
12034         case 0x6c: /* FCMGE (zero) */
12035         case 0x6d: /* FCMLE (zero) */
12036             if (size == 3 && !is_q) {
12037                 unallocated_encoding(s);
12038                 return;
12039             }
12040             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12041             return;
12042         case 0x7f: /* FSQRT */
12043             if (size == 3 && !is_q) {
12044                 unallocated_encoding(s);
12045                 return;
12046             }
12047             break;
12048         case 0x1a: /* FCVTNS */
12049         case 0x1b: /* FCVTMS */
12050         case 0x3a: /* FCVTPS */
12051         case 0x3b: /* FCVTZS */
12052         case 0x5a: /* FCVTNU */
12053         case 0x5b: /* FCVTMU */
12054         case 0x7a: /* FCVTPU */
12055         case 0x7b: /* FCVTZU */
12056             need_fpstatus = true;
12057             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12058             if (size == 3 && !is_q) {
12059                 unallocated_encoding(s);
12060                 return;
12061             }
12062             break;
12063         case 0x5c: /* FCVTAU */
12064         case 0x1c: /* FCVTAS */
12065             need_fpstatus = true;
12066             rmode = FPROUNDING_TIEAWAY;
12067             if (size == 3 && !is_q) {
12068                 unallocated_encoding(s);
12069                 return;
12070             }
12071             break;
12072         case 0x3c: /* URECPE */
12073             if (size == 3) {
12074                 unallocated_encoding(s);
12075                 return;
12076             }
12077             /* fall through */
12078         case 0x3d: /* FRECPE */
12079         case 0x7d: /* FRSQRTE */
12080             if (size == 3 && !is_q) {
12081                 unallocated_encoding(s);
12082                 return;
12083             }
12084             if (!fp_access_check(s)) {
12085                 return;
12086             }
12087             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12088             return;
12089         case 0x56: /* FCVTXN, FCVTXN2 */
12090             if (size == 2) {
12091                 unallocated_encoding(s);
12092                 return;
12093             }
12094             /* fall through */
12095         case 0x16: /* FCVTN, FCVTN2 */
12096             /* handle_2misc_narrow does a 2*size -> size operation, but these
12097              * instructions encode the source size rather than dest size.
12098              */
12099             if (!fp_access_check(s)) {
12100                 return;
12101             }
12102             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12103             return;
12104         case 0x36: /* BFCVTN, BFCVTN2 */
12105             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12106                 unallocated_encoding(s);
12107                 return;
12108             }
12109             if (!fp_access_check(s)) {
12110                 return;
12111             }
12112             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12113             return;
12114         case 0x17: /* FCVTL, FCVTL2 */
12115             if (!fp_access_check(s)) {
12116                 return;
12117             }
12118             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12119             return;
12120         case 0x18: /* FRINTN */
12121         case 0x19: /* FRINTM */
12122         case 0x38: /* FRINTP */
12123         case 0x39: /* FRINTZ */
12124             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12125             /* fall through */
12126         case 0x59: /* FRINTX */
12127         case 0x79: /* FRINTI */
12128             need_fpstatus = true;
12129             if (size == 3 && !is_q) {
12130                 unallocated_encoding(s);
12131                 return;
12132             }
12133             break;
12134         case 0x58: /* FRINTA */
12135             rmode = FPROUNDING_TIEAWAY;
12136             need_fpstatus = true;
12137             if (size == 3 && !is_q) {
12138                 unallocated_encoding(s);
12139                 return;
12140             }
12141             break;
12142         case 0x7c: /* URSQRTE */
12143             if (size == 3) {
12144                 unallocated_encoding(s);
12145                 return;
12146             }
12147             break;
12148         case 0x1e: /* FRINT32Z */
12149         case 0x1f: /* FRINT64Z */
12150             rmode = FPROUNDING_ZERO;
12151             /* fall through */
12152         case 0x5e: /* FRINT32X */
12153         case 0x5f: /* FRINT64X */
12154             need_fpstatus = true;
12155             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12156                 unallocated_encoding(s);
12157                 return;
12158             }
12159             break;
12160         default:
12161             unallocated_encoding(s);
12162             return;
12163         }
12164         break;
12165     }
12166     default:
12167         unallocated_encoding(s);
12168         return;
12169     }
12170 
12171     if (!fp_access_check(s)) {
12172         return;
12173     }
12174 
12175     if (need_fpstatus || rmode >= 0) {
12176         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12177     } else {
12178         tcg_fpstatus = NULL;
12179     }
12180     if (rmode >= 0) {
12181         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12182     } else {
12183         tcg_rmode = NULL;
12184     }
12185 
12186     switch (opcode) {
12187     case 0x5:
12188         if (u && size == 0) { /* NOT */
12189             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12190             return;
12191         }
12192         break;
12193     case 0x8: /* CMGT, CMGE */
12194         if (u) {
12195             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12196         } else {
12197             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12198         }
12199         return;
12200     case 0x9: /* CMEQ, CMLE */
12201         if (u) {
12202             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12203         } else {
12204             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12205         }
12206         return;
12207     case 0xa: /* CMLT */
12208         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12209         return;
12210     case 0xb:
12211         if (u) { /* ABS, NEG */
12212             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12213         } else {
12214             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12215         }
12216         return;
12217     }
12218 
12219     if (size == 3) {
12220         /* All 64-bit element operations can be shared with scalar 2misc */
12221         int pass;
12222 
12223         /* Coverity claims (size == 3 && !is_q) has been eliminated
12224          * from all paths leading to here.
12225          */
12226         tcg_debug_assert(is_q);
12227         for (pass = 0; pass < 2; pass++) {
12228             TCGv_i64 tcg_op = tcg_temp_new_i64();
12229             TCGv_i64 tcg_res = tcg_temp_new_i64();
12230 
12231             read_vec_element(s, tcg_op, rn, pass, MO_64);
12232 
12233             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12234                             tcg_rmode, tcg_fpstatus);
12235 
12236             write_vec_element(s, tcg_res, rd, pass, MO_64);
12237         }
12238     } else {
12239         int pass;
12240 
12241         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12242             TCGv_i32 tcg_op = tcg_temp_new_i32();
12243             TCGv_i32 tcg_res = tcg_temp_new_i32();
12244 
12245             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12246 
12247             if (size == 2) {
12248                 /* Special cases for 32 bit elements */
12249                 switch (opcode) {
12250                 case 0x4: /* CLS */
12251                     if (u) {
12252                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12253                     } else {
12254                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12255                     }
12256                     break;
12257                 case 0x7: /* SQABS, SQNEG */
12258                     if (u) {
12259                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
12260                     } else {
12261                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
12262                     }
12263                     break;
12264                 case 0x2f: /* FABS */
12265                     gen_helper_vfp_abss(tcg_res, tcg_op);
12266                     break;
12267                 case 0x6f: /* FNEG */
12268                     gen_helper_vfp_negs(tcg_res, tcg_op);
12269                     break;
12270                 case 0x7f: /* FSQRT */
12271                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
12272                     break;
12273                 case 0x1a: /* FCVTNS */
12274                 case 0x1b: /* FCVTMS */
12275                 case 0x1c: /* FCVTAS */
12276                 case 0x3a: /* FCVTPS */
12277                 case 0x3b: /* FCVTZS */
12278                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12279                                          tcg_constant_i32(0), tcg_fpstatus);
12280                     break;
12281                 case 0x5a: /* FCVTNU */
12282                 case 0x5b: /* FCVTMU */
12283                 case 0x5c: /* FCVTAU */
12284                 case 0x7a: /* FCVTPU */
12285                 case 0x7b: /* FCVTZU */
12286                     gen_helper_vfp_touls(tcg_res, tcg_op,
12287                                          tcg_constant_i32(0), tcg_fpstatus);
12288                     break;
12289                 case 0x18: /* FRINTN */
12290                 case 0x19: /* FRINTM */
12291                 case 0x38: /* FRINTP */
12292                 case 0x39: /* FRINTZ */
12293                 case 0x58: /* FRINTA */
12294                 case 0x79: /* FRINTI */
12295                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12296                     break;
12297                 case 0x59: /* FRINTX */
12298                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12299                     break;
12300                 case 0x7c: /* URSQRTE */
12301                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12302                     break;
12303                 case 0x1e: /* FRINT32Z */
12304                 case 0x5e: /* FRINT32X */
12305                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12306                     break;
12307                 case 0x1f: /* FRINT64Z */
12308                 case 0x5f: /* FRINT64X */
12309                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12310                     break;
12311                 default:
12312                     g_assert_not_reached();
12313                 }
12314             } else {
12315                 /* Use helpers for 8 and 16 bit elements */
12316                 switch (opcode) {
12317                 case 0x5: /* CNT, RBIT */
12318                     /* For these two insns size is part of the opcode specifier
12319                      * (handled earlier); they always operate on byte elements.
12320                      */
12321                     if (u) {
12322                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12323                     } else {
12324                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12325                     }
12326                     break;
12327                 case 0x7: /* SQABS, SQNEG */
12328                 {
12329                     NeonGenOneOpEnvFn *genfn;
12330                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12331                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12332                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12333                     };
12334                     genfn = fns[size][u];
12335                     genfn(tcg_res, tcg_env, tcg_op);
12336                     break;
12337                 }
12338                 case 0x4: /* CLS, CLZ */
12339                     if (u) {
12340                         if (size == 0) {
12341                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12342                         } else {
12343                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12344                         }
12345                     } else {
12346                         if (size == 0) {
12347                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12348                         } else {
12349                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12350                         }
12351                     }
12352                     break;
12353                 default:
12354                     g_assert_not_reached();
12355                 }
12356             }
12357 
12358             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12359         }
12360     }
12361     clear_vec_high(s, is_q, rd);
12362 
12363     if (tcg_rmode) {
12364         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12365     }
12366 }
12367 
12368 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12369  *
12370  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12371  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12372  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12373  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12374  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12375  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12376  *
12377  * This actually covers two groups where scalar access is governed by
12378  * bit 28. A bunch of the instructions (float to integral) only exist
12379  * in the vector form and are un-allocated for the scalar decode. Also
12380  * in the scalar decode Q is always 1.
12381  */
12382 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12383 {
12384     int fpop, opcode, a, u;
12385     int rn, rd;
12386     bool is_q;
12387     bool is_scalar;
12388     bool only_in_vector = false;
12389 
12390     int pass;
12391     TCGv_i32 tcg_rmode = NULL;
12392     TCGv_ptr tcg_fpstatus = NULL;
12393     bool need_fpst = true;
12394     int rmode = -1;
12395 
12396     if (!dc_isar_feature(aa64_fp16, s)) {
12397         unallocated_encoding(s);
12398         return;
12399     }
12400 
12401     rd = extract32(insn, 0, 5);
12402     rn = extract32(insn, 5, 5);
12403 
12404     a = extract32(insn, 23, 1);
12405     u = extract32(insn, 29, 1);
12406     is_scalar = extract32(insn, 28, 1);
12407     is_q = extract32(insn, 30, 1);
12408 
12409     opcode = extract32(insn, 12, 5);
12410     fpop = deposit32(opcode, 5, 1, a);
12411     fpop = deposit32(fpop, 6, 1, u);
12412 
12413     switch (fpop) {
12414     case 0x1d: /* SCVTF */
12415     case 0x5d: /* UCVTF */
12416     {
12417         int elements;
12418 
12419         if (is_scalar) {
12420             elements = 1;
12421         } else {
12422             elements = (is_q ? 8 : 4);
12423         }
12424 
12425         if (!fp_access_check(s)) {
12426             return;
12427         }
12428         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12429         return;
12430     }
12431     break;
12432     case 0x2c: /* FCMGT (zero) */
12433     case 0x2d: /* FCMEQ (zero) */
12434     case 0x2e: /* FCMLT (zero) */
12435     case 0x6c: /* FCMGE (zero) */
12436     case 0x6d: /* FCMLE (zero) */
12437         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12438         return;
12439     case 0x3d: /* FRECPE */
12440     case 0x3f: /* FRECPX */
12441         break;
12442     case 0x18: /* FRINTN */
12443         only_in_vector = true;
12444         rmode = FPROUNDING_TIEEVEN;
12445         break;
12446     case 0x19: /* FRINTM */
12447         only_in_vector = true;
12448         rmode = FPROUNDING_NEGINF;
12449         break;
12450     case 0x38: /* FRINTP */
12451         only_in_vector = true;
12452         rmode = FPROUNDING_POSINF;
12453         break;
12454     case 0x39: /* FRINTZ */
12455         only_in_vector = true;
12456         rmode = FPROUNDING_ZERO;
12457         break;
12458     case 0x58: /* FRINTA */
12459         only_in_vector = true;
12460         rmode = FPROUNDING_TIEAWAY;
12461         break;
12462     case 0x59: /* FRINTX */
12463     case 0x79: /* FRINTI */
12464         only_in_vector = true;
12465         /* current rounding mode */
12466         break;
12467     case 0x1a: /* FCVTNS */
12468         rmode = FPROUNDING_TIEEVEN;
12469         break;
12470     case 0x1b: /* FCVTMS */
12471         rmode = FPROUNDING_NEGINF;
12472         break;
12473     case 0x1c: /* FCVTAS */
12474         rmode = FPROUNDING_TIEAWAY;
12475         break;
12476     case 0x3a: /* FCVTPS */
12477         rmode = FPROUNDING_POSINF;
12478         break;
12479     case 0x3b: /* FCVTZS */
12480         rmode = FPROUNDING_ZERO;
12481         break;
12482     case 0x5a: /* FCVTNU */
12483         rmode = FPROUNDING_TIEEVEN;
12484         break;
12485     case 0x5b: /* FCVTMU */
12486         rmode = FPROUNDING_NEGINF;
12487         break;
12488     case 0x5c: /* FCVTAU */
12489         rmode = FPROUNDING_TIEAWAY;
12490         break;
12491     case 0x7a: /* FCVTPU */
12492         rmode = FPROUNDING_POSINF;
12493         break;
12494     case 0x7b: /* FCVTZU */
12495         rmode = FPROUNDING_ZERO;
12496         break;
12497     case 0x2f: /* FABS */
12498     case 0x6f: /* FNEG */
12499         need_fpst = false;
12500         break;
12501     case 0x7d: /* FRSQRTE */
12502     case 0x7f: /* FSQRT (vector) */
12503         break;
12504     default:
12505         unallocated_encoding(s);
12506         return;
12507     }
12508 
12509 
12510     /* Check additional constraints for the scalar encoding */
12511     if (is_scalar) {
12512         if (!is_q) {
12513             unallocated_encoding(s);
12514             return;
12515         }
12516         /* FRINTxx is only in the vector form */
12517         if (only_in_vector) {
12518             unallocated_encoding(s);
12519             return;
12520         }
12521     }
12522 
12523     if (!fp_access_check(s)) {
12524         return;
12525     }
12526 
12527     if (rmode >= 0 || need_fpst) {
12528         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12529     }
12530 
12531     if (rmode >= 0) {
12532         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12533     }
12534 
12535     if (is_scalar) {
12536         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12537         TCGv_i32 tcg_res = tcg_temp_new_i32();
12538 
12539         switch (fpop) {
12540         case 0x1a: /* FCVTNS */
12541         case 0x1b: /* FCVTMS */
12542         case 0x1c: /* FCVTAS */
12543         case 0x3a: /* FCVTPS */
12544         case 0x3b: /* FCVTZS */
12545             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12546             break;
12547         case 0x3d: /* FRECPE */
12548             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12549             break;
12550         case 0x3f: /* FRECPX */
12551             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12552             break;
12553         case 0x5a: /* FCVTNU */
12554         case 0x5b: /* FCVTMU */
12555         case 0x5c: /* FCVTAU */
12556         case 0x7a: /* FCVTPU */
12557         case 0x7b: /* FCVTZU */
12558             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12559             break;
12560         case 0x6f: /* FNEG */
12561             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12562             break;
12563         case 0x7d: /* FRSQRTE */
12564             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12565             break;
12566         default:
12567             g_assert_not_reached();
12568         }
12569 
12570         /* limit any sign extension going on */
12571         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12572         write_fp_sreg(s, rd, tcg_res);
12573     } else {
12574         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12575             TCGv_i32 tcg_op = tcg_temp_new_i32();
12576             TCGv_i32 tcg_res = tcg_temp_new_i32();
12577 
12578             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12579 
12580             switch (fpop) {
12581             case 0x1a: /* FCVTNS */
12582             case 0x1b: /* FCVTMS */
12583             case 0x1c: /* FCVTAS */
12584             case 0x3a: /* FCVTPS */
12585             case 0x3b: /* FCVTZS */
12586                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12587                 break;
12588             case 0x3d: /* FRECPE */
12589                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12590                 break;
12591             case 0x5a: /* FCVTNU */
12592             case 0x5b: /* FCVTMU */
12593             case 0x5c: /* FCVTAU */
12594             case 0x7a: /* FCVTPU */
12595             case 0x7b: /* FCVTZU */
12596                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12597                 break;
12598             case 0x18: /* FRINTN */
12599             case 0x19: /* FRINTM */
12600             case 0x38: /* FRINTP */
12601             case 0x39: /* FRINTZ */
12602             case 0x58: /* FRINTA */
12603             case 0x79: /* FRINTI */
12604                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12605                 break;
12606             case 0x59: /* FRINTX */
12607                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12608                 break;
12609             case 0x2f: /* FABS */
12610                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12611                 break;
12612             case 0x6f: /* FNEG */
12613                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12614                 break;
12615             case 0x7d: /* FRSQRTE */
12616                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12617                 break;
12618             case 0x7f: /* FSQRT */
12619                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12620                 break;
12621             default:
12622                 g_assert_not_reached();
12623             }
12624 
12625             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12626         }
12627 
12628         clear_vec_high(s, is_q, rd);
12629     }
12630 
12631     if (tcg_rmode) {
12632         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12633     }
12634 }
12635 
12636 /* AdvSIMD scalar x indexed element
12637  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12638  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12639  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12640  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12641  * AdvSIMD vector x indexed element
12642  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12643  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12644  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12645  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12646  */
12647 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12648 {
12649     /* This encoding has two kinds of instruction:
12650      *  normal, where we perform elt x idxelt => elt for each
12651      *     element in the vector
12652      *  long, where we perform elt x idxelt and generate a result of
12653      *     double the width of the input element
12654      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12655      */
12656     bool is_scalar = extract32(insn, 28, 1);
12657     bool is_q = extract32(insn, 30, 1);
12658     bool u = extract32(insn, 29, 1);
12659     int size = extract32(insn, 22, 2);
12660     int l = extract32(insn, 21, 1);
12661     int m = extract32(insn, 20, 1);
12662     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12663     int rm = extract32(insn, 16, 4);
12664     int opcode = extract32(insn, 12, 4);
12665     int h = extract32(insn, 11, 1);
12666     int rn = extract32(insn, 5, 5);
12667     int rd = extract32(insn, 0, 5);
12668     bool is_long = false;
12669     int is_fp = 0;
12670     bool is_fp16 = false;
12671     int index;
12672     TCGv_ptr fpst;
12673 
12674     switch (16 * u + opcode) {
12675     case 0x08: /* MUL */
12676     case 0x10: /* MLA */
12677     case 0x14: /* MLS */
12678         if (is_scalar) {
12679             unallocated_encoding(s);
12680             return;
12681         }
12682         break;
12683     case 0x02: /* SMLAL, SMLAL2 */
12684     case 0x12: /* UMLAL, UMLAL2 */
12685     case 0x06: /* SMLSL, SMLSL2 */
12686     case 0x16: /* UMLSL, UMLSL2 */
12687     case 0x0a: /* SMULL, SMULL2 */
12688     case 0x1a: /* UMULL, UMULL2 */
12689         if (is_scalar) {
12690             unallocated_encoding(s);
12691             return;
12692         }
12693         is_long = true;
12694         break;
12695     case 0x03: /* SQDMLAL, SQDMLAL2 */
12696     case 0x07: /* SQDMLSL, SQDMLSL2 */
12697     case 0x0b: /* SQDMULL, SQDMULL2 */
12698         is_long = true;
12699         break;
12700     case 0x0c: /* SQDMULH */
12701     case 0x0d: /* SQRDMULH */
12702         break;
12703     case 0x01: /* FMLA */
12704     case 0x05: /* FMLS */
12705     case 0x09: /* FMUL */
12706     case 0x19: /* FMULX */
12707         is_fp = 1;
12708         break;
12709     case 0x1d: /* SQRDMLAH */
12710     case 0x1f: /* SQRDMLSH */
12711         if (!dc_isar_feature(aa64_rdm, s)) {
12712             unallocated_encoding(s);
12713             return;
12714         }
12715         break;
12716     case 0x0e: /* SDOT */
12717     case 0x1e: /* UDOT */
12718         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12719             unallocated_encoding(s);
12720             return;
12721         }
12722         break;
12723     case 0x0f:
12724         switch (size) {
12725         case 0: /* SUDOT */
12726         case 2: /* USDOT */
12727             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12728                 unallocated_encoding(s);
12729                 return;
12730             }
12731             size = MO_32;
12732             break;
12733         case 1: /* BFDOT */
12734             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12735                 unallocated_encoding(s);
12736                 return;
12737             }
12738             size = MO_32;
12739             break;
12740         case 3: /* BFMLAL{B,T} */
12741             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12742                 unallocated_encoding(s);
12743                 return;
12744             }
12745             /* can't set is_fp without other incorrect size checks */
12746             size = MO_16;
12747             break;
12748         default:
12749             unallocated_encoding(s);
12750             return;
12751         }
12752         break;
12753     case 0x11: /* FCMLA #0 */
12754     case 0x13: /* FCMLA #90 */
12755     case 0x15: /* FCMLA #180 */
12756     case 0x17: /* FCMLA #270 */
12757         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12758             unallocated_encoding(s);
12759             return;
12760         }
12761         is_fp = 2;
12762         break;
12763     case 0x00: /* FMLAL */
12764     case 0x04: /* FMLSL */
12765     case 0x18: /* FMLAL2 */
12766     case 0x1c: /* FMLSL2 */
12767         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12768             unallocated_encoding(s);
12769             return;
12770         }
12771         size = MO_16;
12772         /* is_fp, but we pass tcg_env not fp_status.  */
12773         break;
12774     default:
12775         unallocated_encoding(s);
12776         return;
12777     }
12778 
12779     switch (is_fp) {
12780     case 1: /* normal fp */
12781         /* convert insn encoded size to MemOp size */
12782         switch (size) {
12783         case 0: /* half-precision */
12784             size = MO_16;
12785             is_fp16 = true;
12786             break;
12787         case MO_32: /* single precision */
12788         case MO_64: /* double precision */
12789             break;
12790         default:
12791             unallocated_encoding(s);
12792             return;
12793         }
12794         break;
12795 
12796     case 2: /* complex fp */
12797         /* Each indexable element is a complex pair.  */
12798         size += 1;
12799         switch (size) {
12800         case MO_32:
12801             if (h && !is_q) {
12802                 unallocated_encoding(s);
12803                 return;
12804             }
12805             is_fp16 = true;
12806             break;
12807         case MO_64:
12808             break;
12809         default:
12810             unallocated_encoding(s);
12811             return;
12812         }
12813         break;
12814 
12815     default: /* integer */
12816         switch (size) {
12817         case MO_8:
12818         case MO_64:
12819             unallocated_encoding(s);
12820             return;
12821         }
12822         break;
12823     }
12824     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12825         unallocated_encoding(s);
12826         return;
12827     }
12828 
12829     /* Given MemOp size, adjust register and indexing.  */
12830     switch (size) {
12831     case MO_16:
12832         index = h << 2 | l << 1 | m;
12833         break;
12834     case MO_32:
12835         index = h << 1 | l;
12836         rm |= m << 4;
12837         break;
12838     case MO_64:
12839         if (l || !is_q) {
12840             unallocated_encoding(s);
12841             return;
12842         }
12843         index = h;
12844         rm |= m << 4;
12845         break;
12846     default:
12847         g_assert_not_reached();
12848     }
12849 
12850     if (!fp_access_check(s)) {
12851         return;
12852     }
12853 
12854     if (is_fp) {
12855         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12856     } else {
12857         fpst = NULL;
12858     }
12859 
12860     switch (16 * u + opcode) {
12861     case 0x0e: /* SDOT */
12862     case 0x1e: /* UDOT */
12863         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12864                          u ? gen_helper_gvec_udot_idx_b
12865                          : gen_helper_gvec_sdot_idx_b);
12866         return;
12867     case 0x0f:
12868         switch (extract32(insn, 22, 2)) {
12869         case 0: /* SUDOT */
12870             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12871                              gen_helper_gvec_sudot_idx_b);
12872             return;
12873         case 1: /* BFDOT */
12874             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12875                              gen_helper_gvec_bfdot_idx);
12876             return;
12877         case 2: /* USDOT */
12878             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12879                              gen_helper_gvec_usdot_idx_b);
12880             return;
12881         case 3: /* BFMLAL{B,T} */
12882             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12883                               gen_helper_gvec_bfmlal_idx);
12884             return;
12885         }
12886         g_assert_not_reached();
12887     case 0x11: /* FCMLA #0 */
12888     case 0x13: /* FCMLA #90 */
12889     case 0x15: /* FCMLA #180 */
12890     case 0x17: /* FCMLA #270 */
12891         {
12892             int rot = extract32(insn, 13, 2);
12893             int data = (index << 2) | rot;
12894             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12895                                vec_full_reg_offset(s, rn),
12896                                vec_full_reg_offset(s, rm),
12897                                vec_full_reg_offset(s, rd), fpst,
12898                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12899                                size == MO_64
12900                                ? gen_helper_gvec_fcmlas_idx
12901                                : gen_helper_gvec_fcmlah_idx);
12902         }
12903         return;
12904 
12905     case 0x00: /* FMLAL */
12906     case 0x04: /* FMLSL */
12907     case 0x18: /* FMLAL2 */
12908     case 0x1c: /* FMLSL2 */
12909         {
12910             int is_s = extract32(opcode, 2, 1);
12911             int is_2 = u;
12912             int data = (index << 2) | (is_2 << 1) | is_s;
12913             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12914                                vec_full_reg_offset(s, rn),
12915                                vec_full_reg_offset(s, rm), tcg_env,
12916                                is_q ? 16 : 8, vec_full_reg_size(s),
12917                                data, gen_helper_gvec_fmlal_idx_a64);
12918         }
12919         return;
12920 
12921     case 0x08: /* MUL */
12922         if (!is_long && !is_scalar) {
12923             static gen_helper_gvec_3 * const fns[3] = {
12924                 gen_helper_gvec_mul_idx_h,
12925                 gen_helper_gvec_mul_idx_s,
12926                 gen_helper_gvec_mul_idx_d,
12927             };
12928             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
12929                                vec_full_reg_offset(s, rn),
12930                                vec_full_reg_offset(s, rm),
12931                                is_q ? 16 : 8, vec_full_reg_size(s),
12932                                index, fns[size - 1]);
12933             return;
12934         }
12935         break;
12936 
12937     case 0x10: /* MLA */
12938         if (!is_long && !is_scalar) {
12939             static gen_helper_gvec_4 * const fns[3] = {
12940                 gen_helper_gvec_mla_idx_h,
12941                 gen_helper_gvec_mla_idx_s,
12942                 gen_helper_gvec_mla_idx_d,
12943             };
12944             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12945                                vec_full_reg_offset(s, rn),
12946                                vec_full_reg_offset(s, rm),
12947                                vec_full_reg_offset(s, rd),
12948                                is_q ? 16 : 8, vec_full_reg_size(s),
12949                                index, fns[size - 1]);
12950             return;
12951         }
12952         break;
12953 
12954     case 0x14: /* MLS */
12955         if (!is_long && !is_scalar) {
12956             static gen_helper_gvec_4 * const fns[3] = {
12957                 gen_helper_gvec_mls_idx_h,
12958                 gen_helper_gvec_mls_idx_s,
12959                 gen_helper_gvec_mls_idx_d,
12960             };
12961             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12962                                vec_full_reg_offset(s, rn),
12963                                vec_full_reg_offset(s, rm),
12964                                vec_full_reg_offset(s, rd),
12965                                is_q ? 16 : 8, vec_full_reg_size(s),
12966                                index, fns[size - 1]);
12967             return;
12968         }
12969         break;
12970     }
12971 
12972     if (size == 3) {
12973         TCGv_i64 tcg_idx = tcg_temp_new_i64();
12974         int pass;
12975 
12976         assert(is_fp && is_q && !is_long);
12977 
12978         read_vec_element(s, tcg_idx, rm, index, MO_64);
12979 
12980         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12981             TCGv_i64 tcg_op = tcg_temp_new_i64();
12982             TCGv_i64 tcg_res = tcg_temp_new_i64();
12983 
12984             read_vec_element(s, tcg_op, rn, pass, MO_64);
12985 
12986             switch (16 * u + opcode) {
12987             case 0x05: /* FMLS */
12988                 /* As usual for ARM, separate negation for fused multiply-add */
12989                 gen_helper_vfp_negd(tcg_op, tcg_op);
12990                 /* fall through */
12991             case 0x01: /* FMLA */
12992                 read_vec_element(s, tcg_res, rd, pass, MO_64);
12993                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12994                 break;
12995             case 0x09: /* FMUL */
12996                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
12997                 break;
12998             case 0x19: /* FMULX */
12999                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13000                 break;
13001             default:
13002                 g_assert_not_reached();
13003             }
13004 
13005             write_vec_element(s, tcg_res, rd, pass, MO_64);
13006         }
13007 
13008         clear_vec_high(s, !is_scalar, rd);
13009     } else if (!is_long) {
13010         /* 32 bit floating point, or 16 or 32 bit integer.
13011          * For the 16 bit scalar case we use the usual Neon helpers and
13012          * rely on the fact that 0 op 0 == 0 with no side effects.
13013          */
13014         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13015         int pass, maxpasses;
13016 
13017         if (is_scalar) {
13018             maxpasses = 1;
13019         } else {
13020             maxpasses = is_q ? 4 : 2;
13021         }
13022 
13023         read_vec_element_i32(s, tcg_idx, rm, index, size);
13024 
13025         if (size == 1 && !is_scalar) {
13026             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13027              * the index into both halves of the 32 bit tcg_idx and then use
13028              * the usual Neon helpers.
13029              */
13030             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13031         }
13032 
13033         for (pass = 0; pass < maxpasses; pass++) {
13034             TCGv_i32 tcg_op = tcg_temp_new_i32();
13035             TCGv_i32 tcg_res = tcg_temp_new_i32();
13036 
13037             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13038 
13039             switch (16 * u + opcode) {
13040             case 0x08: /* MUL */
13041             case 0x10: /* MLA */
13042             case 0x14: /* MLS */
13043             {
13044                 static NeonGenTwoOpFn * const fns[2][2] = {
13045                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13046                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13047                 };
13048                 NeonGenTwoOpFn *genfn;
13049                 bool is_sub = opcode == 0x4;
13050 
13051                 if (size == 1) {
13052                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13053                 } else {
13054                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13055                 }
13056                 if (opcode == 0x8) {
13057                     break;
13058                 }
13059                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13060                 genfn = fns[size - 1][is_sub];
13061                 genfn(tcg_res, tcg_op, tcg_res);
13062                 break;
13063             }
13064             case 0x05: /* FMLS */
13065             case 0x01: /* FMLA */
13066                 read_vec_element_i32(s, tcg_res, rd, pass,
13067                                      is_scalar ? size : MO_32);
13068                 switch (size) {
13069                 case 1:
13070                     if (opcode == 0x5) {
13071                         /* As usual for ARM, separate negation for fused
13072                          * multiply-add */
13073                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13074                     }
13075                     if (is_scalar) {
13076                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13077                                                    tcg_res, fpst);
13078                     } else {
13079                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13080                                                     tcg_res, fpst);
13081                     }
13082                     break;
13083                 case 2:
13084                     if (opcode == 0x5) {
13085                         /* As usual for ARM, separate negation for
13086                          * fused multiply-add */
13087                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13088                     }
13089                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13090                                            tcg_res, fpst);
13091                     break;
13092                 default:
13093                     g_assert_not_reached();
13094                 }
13095                 break;
13096             case 0x09: /* FMUL */
13097                 switch (size) {
13098                 case 1:
13099                     if (is_scalar) {
13100                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13101                                                 tcg_idx, fpst);
13102                     } else {
13103                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13104                                                  tcg_idx, fpst);
13105                     }
13106                     break;
13107                 case 2:
13108                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13109                     break;
13110                 default:
13111                     g_assert_not_reached();
13112                 }
13113                 break;
13114             case 0x19: /* FMULX */
13115                 switch (size) {
13116                 case 1:
13117                     if (is_scalar) {
13118                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13119                                                  tcg_idx, fpst);
13120                     } else {
13121                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13122                                                   tcg_idx, fpst);
13123                     }
13124                     break;
13125                 case 2:
13126                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13127                     break;
13128                 default:
13129                     g_assert_not_reached();
13130                 }
13131                 break;
13132             case 0x0c: /* SQDMULH */
13133                 if (size == 1) {
13134                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
13135                                                tcg_op, tcg_idx);
13136                 } else {
13137                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
13138                                                tcg_op, tcg_idx);
13139                 }
13140                 break;
13141             case 0x0d: /* SQRDMULH */
13142                 if (size == 1) {
13143                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
13144                                                 tcg_op, tcg_idx);
13145                 } else {
13146                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
13147                                                 tcg_op, tcg_idx);
13148                 }
13149                 break;
13150             case 0x1d: /* SQRDMLAH */
13151                 read_vec_element_i32(s, tcg_res, rd, pass,
13152                                      is_scalar ? size : MO_32);
13153                 if (size == 1) {
13154                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
13155                                                 tcg_op, tcg_idx, tcg_res);
13156                 } else {
13157                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
13158                                                 tcg_op, tcg_idx, tcg_res);
13159                 }
13160                 break;
13161             case 0x1f: /* SQRDMLSH */
13162                 read_vec_element_i32(s, tcg_res, rd, pass,
13163                                      is_scalar ? size : MO_32);
13164                 if (size == 1) {
13165                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
13166                                                 tcg_op, tcg_idx, tcg_res);
13167                 } else {
13168                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
13169                                                 tcg_op, tcg_idx, tcg_res);
13170                 }
13171                 break;
13172             default:
13173                 g_assert_not_reached();
13174             }
13175 
13176             if (is_scalar) {
13177                 write_fp_sreg(s, rd, tcg_res);
13178             } else {
13179                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13180             }
13181         }
13182 
13183         clear_vec_high(s, is_q, rd);
13184     } else {
13185         /* long ops: 16x16->32 or 32x32->64 */
13186         TCGv_i64 tcg_res[2];
13187         int pass;
13188         bool satop = extract32(opcode, 0, 1);
13189         MemOp memop = MO_32;
13190 
13191         if (satop || !u) {
13192             memop |= MO_SIGN;
13193         }
13194 
13195         if (size == 2) {
13196             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13197 
13198             read_vec_element(s, tcg_idx, rm, index, memop);
13199 
13200             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13201                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13202                 TCGv_i64 tcg_passres;
13203                 int passelt;
13204 
13205                 if (is_scalar) {
13206                     passelt = 0;
13207                 } else {
13208                     passelt = pass + (is_q * 2);
13209                 }
13210 
13211                 read_vec_element(s, tcg_op, rn, passelt, memop);
13212 
13213                 tcg_res[pass] = tcg_temp_new_i64();
13214 
13215                 if (opcode == 0xa || opcode == 0xb) {
13216                     /* Non-accumulating ops */
13217                     tcg_passres = tcg_res[pass];
13218                 } else {
13219                     tcg_passres = tcg_temp_new_i64();
13220                 }
13221 
13222                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13223 
13224                 if (satop) {
13225                     /* saturating, doubling */
13226                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
13227                                                       tcg_passres, tcg_passres);
13228                 }
13229 
13230                 if (opcode == 0xa || opcode == 0xb) {
13231                     continue;
13232                 }
13233 
13234                 /* Accumulating op: handle accumulate step */
13235                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13236 
13237                 switch (opcode) {
13238                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13239                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13240                     break;
13241                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13242                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13243                     break;
13244                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13245                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13246                     /* fall through */
13247                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13248                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
13249                                                       tcg_res[pass],
13250                                                       tcg_passres);
13251                     break;
13252                 default:
13253                     g_assert_not_reached();
13254                 }
13255             }
13256 
13257             clear_vec_high(s, !is_scalar, rd);
13258         } else {
13259             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13260 
13261             assert(size == 1);
13262             read_vec_element_i32(s, tcg_idx, rm, index, size);
13263 
13264             if (!is_scalar) {
13265                 /* The simplest way to handle the 16x16 indexed ops is to
13266                  * duplicate the index into both halves of the 32 bit tcg_idx
13267                  * and then use the usual Neon helpers.
13268                  */
13269                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13270             }
13271 
13272             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13273                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13274                 TCGv_i64 tcg_passres;
13275 
13276                 if (is_scalar) {
13277                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13278                 } else {
13279                     read_vec_element_i32(s, tcg_op, rn,
13280                                          pass + (is_q * 2), MO_32);
13281                 }
13282 
13283                 tcg_res[pass] = tcg_temp_new_i64();
13284 
13285                 if (opcode == 0xa || opcode == 0xb) {
13286                     /* Non-accumulating ops */
13287                     tcg_passres = tcg_res[pass];
13288                 } else {
13289                     tcg_passres = tcg_temp_new_i64();
13290                 }
13291 
13292                 if (memop & MO_SIGN) {
13293                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13294                 } else {
13295                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13296                 }
13297                 if (satop) {
13298                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
13299                                                       tcg_passres, tcg_passres);
13300                 }
13301 
13302                 if (opcode == 0xa || opcode == 0xb) {
13303                     continue;
13304                 }
13305 
13306                 /* Accumulating op: handle accumulate step */
13307                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13308 
13309                 switch (opcode) {
13310                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13311                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13312                                              tcg_passres);
13313                     break;
13314                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13315                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13316                                              tcg_passres);
13317                     break;
13318                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13319                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13320                     /* fall through */
13321                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13322                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
13323                                                       tcg_res[pass],
13324                                                       tcg_passres);
13325                     break;
13326                 default:
13327                     g_assert_not_reached();
13328                 }
13329             }
13330 
13331             if (is_scalar) {
13332                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13333             }
13334         }
13335 
13336         if (is_scalar) {
13337             tcg_res[1] = tcg_constant_i64(0);
13338         }
13339 
13340         for (pass = 0; pass < 2; pass++) {
13341             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13342         }
13343     }
13344 }
13345 
13346 /* Crypto AES
13347  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13348  * +-----------------+------+-----------+--------+-----+------+------+
13349  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13350  * +-----------------+------+-----------+--------+-----+------+------+
13351  */
13352 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13353 {
13354     int size = extract32(insn, 22, 2);
13355     int opcode = extract32(insn, 12, 5);
13356     int rn = extract32(insn, 5, 5);
13357     int rd = extract32(insn, 0, 5);
13358     gen_helper_gvec_2 *genfn2 = NULL;
13359     gen_helper_gvec_3 *genfn3 = NULL;
13360 
13361     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13362         unallocated_encoding(s);
13363         return;
13364     }
13365 
13366     switch (opcode) {
13367     case 0x4: /* AESE */
13368         genfn3 = gen_helper_crypto_aese;
13369         break;
13370     case 0x6: /* AESMC */
13371         genfn2 = gen_helper_crypto_aesmc;
13372         break;
13373     case 0x5: /* AESD */
13374         genfn3 = gen_helper_crypto_aesd;
13375         break;
13376     case 0x7: /* AESIMC */
13377         genfn2 = gen_helper_crypto_aesimc;
13378         break;
13379     default:
13380         unallocated_encoding(s);
13381         return;
13382     }
13383 
13384     if (!fp_access_check(s)) {
13385         return;
13386     }
13387     if (genfn2) {
13388         gen_gvec_op2_ool(s, true, rd, rn, 0, genfn2);
13389     } else {
13390         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, genfn3);
13391     }
13392 }
13393 
13394 /* Crypto three-reg SHA
13395  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13396  * +-----------------+------+---+------+---+--------+-----+------+------+
13397  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13398  * +-----------------+------+---+------+---+--------+-----+------+------+
13399  */
13400 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13401 {
13402     int size = extract32(insn, 22, 2);
13403     int opcode = extract32(insn, 12, 3);
13404     int rm = extract32(insn, 16, 5);
13405     int rn = extract32(insn, 5, 5);
13406     int rd = extract32(insn, 0, 5);
13407     gen_helper_gvec_3 *genfn;
13408     bool feature;
13409 
13410     if (size != 0) {
13411         unallocated_encoding(s);
13412         return;
13413     }
13414 
13415     switch (opcode) {
13416     case 0: /* SHA1C */
13417         genfn = gen_helper_crypto_sha1c;
13418         feature = dc_isar_feature(aa64_sha1, s);
13419         break;
13420     case 1: /* SHA1P */
13421         genfn = gen_helper_crypto_sha1p;
13422         feature = dc_isar_feature(aa64_sha1, s);
13423         break;
13424     case 2: /* SHA1M */
13425         genfn = gen_helper_crypto_sha1m;
13426         feature = dc_isar_feature(aa64_sha1, s);
13427         break;
13428     case 3: /* SHA1SU0 */
13429         genfn = gen_helper_crypto_sha1su0;
13430         feature = dc_isar_feature(aa64_sha1, s);
13431         break;
13432     case 4: /* SHA256H */
13433         genfn = gen_helper_crypto_sha256h;
13434         feature = dc_isar_feature(aa64_sha256, s);
13435         break;
13436     case 5: /* SHA256H2 */
13437         genfn = gen_helper_crypto_sha256h2;
13438         feature = dc_isar_feature(aa64_sha256, s);
13439         break;
13440     case 6: /* SHA256SU1 */
13441         genfn = gen_helper_crypto_sha256su1;
13442         feature = dc_isar_feature(aa64_sha256, s);
13443         break;
13444     default:
13445         unallocated_encoding(s);
13446         return;
13447     }
13448 
13449     if (!feature) {
13450         unallocated_encoding(s);
13451         return;
13452     }
13453 
13454     if (!fp_access_check(s)) {
13455         return;
13456     }
13457     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13458 }
13459 
13460 /* Crypto two-reg SHA
13461  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13462  * +-----------------+------+-----------+--------+-----+------+------+
13463  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13464  * +-----------------+------+-----------+--------+-----+------+------+
13465  */
13466 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13467 {
13468     int size = extract32(insn, 22, 2);
13469     int opcode = extract32(insn, 12, 5);
13470     int rn = extract32(insn, 5, 5);
13471     int rd = extract32(insn, 0, 5);
13472     gen_helper_gvec_2 *genfn;
13473     bool feature;
13474 
13475     if (size != 0) {
13476         unallocated_encoding(s);
13477         return;
13478     }
13479 
13480     switch (opcode) {
13481     case 0: /* SHA1H */
13482         feature = dc_isar_feature(aa64_sha1, s);
13483         genfn = gen_helper_crypto_sha1h;
13484         break;
13485     case 1: /* SHA1SU1 */
13486         feature = dc_isar_feature(aa64_sha1, s);
13487         genfn = gen_helper_crypto_sha1su1;
13488         break;
13489     case 2: /* SHA256SU0 */
13490         feature = dc_isar_feature(aa64_sha256, s);
13491         genfn = gen_helper_crypto_sha256su0;
13492         break;
13493     default:
13494         unallocated_encoding(s);
13495         return;
13496     }
13497 
13498     if (!feature) {
13499         unallocated_encoding(s);
13500         return;
13501     }
13502 
13503     if (!fp_access_check(s)) {
13504         return;
13505     }
13506     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13507 }
13508 
13509 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13510 {
13511     tcg_gen_rotli_i64(d, m, 1);
13512     tcg_gen_xor_i64(d, d, n);
13513 }
13514 
13515 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13516 {
13517     tcg_gen_rotli_vec(vece, d, m, 1);
13518     tcg_gen_xor_vec(vece, d, d, n);
13519 }
13520 
13521 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13522                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13523 {
13524     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13525     static const GVecGen3 op = {
13526         .fni8 = gen_rax1_i64,
13527         .fniv = gen_rax1_vec,
13528         .opt_opc = vecop_list,
13529         .fno = gen_helper_crypto_rax1,
13530         .vece = MO_64,
13531     };
13532     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13533 }
13534 
13535 /* Crypto three-reg SHA512
13536  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13537  * +-----------------------+------+---+---+-----+--------+------+------+
13538  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13539  * +-----------------------+------+---+---+-----+--------+------+------+
13540  */
13541 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13542 {
13543     int opcode = extract32(insn, 10, 2);
13544     int o =  extract32(insn, 14, 1);
13545     int rm = extract32(insn, 16, 5);
13546     int rn = extract32(insn, 5, 5);
13547     int rd = extract32(insn, 0, 5);
13548     bool feature;
13549     gen_helper_gvec_3 *oolfn = NULL;
13550     GVecGen3Fn *gvecfn = NULL;
13551 
13552     if (o == 0) {
13553         switch (opcode) {
13554         case 0: /* SHA512H */
13555             feature = dc_isar_feature(aa64_sha512, s);
13556             oolfn = gen_helper_crypto_sha512h;
13557             break;
13558         case 1: /* SHA512H2 */
13559             feature = dc_isar_feature(aa64_sha512, s);
13560             oolfn = gen_helper_crypto_sha512h2;
13561             break;
13562         case 2: /* SHA512SU1 */
13563             feature = dc_isar_feature(aa64_sha512, s);
13564             oolfn = gen_helper_crypto_sha512su1;
13565             break;
13566         case 3: /* RAX1 */
13567             feature = dc_isar_feature(aa64_sha3, s);
13568             gvecfn = gen_gvec_rax1;
13569             break;
13570         default:
13571             g_assert_not_reached();
13572         }
13573     } else {
13574         switch (opcode) {
13575         case 0: /* SM3PARTW1 */
13576             feature = dc_isar_feature(aa64_sm3, s);
13577             oolfn = gen_helper_crypto_sm3partw1;
13578             break;
13579         case 1: /* SM3PARTW2 */
13580             feature = dc_isar_feature(aa64_sm3, s);
13581             oolfn = gen_helper_crypto_sm3partw2;
13582             break;
13583         case 2: /* SM4EKEY */
13584             feature = dc_isar_feature(aa64_sm4, s);
13585             oolfn = gen_helper_crypto_sm4ekey;
13586             break;
13587         default:
13588             unallocated_encoding(s);
13589             return;
13590         }
13591     }
13592 
13593     if (!feature) {
13594         unallocated_encoding(s);
13595         return;
13596     }
13597 
13598     if (!fp_access_check(s)) {
13599         return;
13600     }
13601 
13602     if (oolfn) {
13603         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13604     } else {
13605         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13606     }
13607 }
13608 
13609 /* Crypto two-reg SHA512
13610  *  31                                     12  11  10  9    5 4    0
13611  * +-----------------------------------------+--------+------+------+
13612  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13613  * +-----------------------------------------+--------+------+------+
13614  */
13615 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13616 {
13617     int opcode = extract32(insn, 10, 2);
13618     int rn = extract32(insn, 5, 5);
13619     int rd = extract32(insn, 0, 5);
13620     bool feature;
13621 
13622     switch (opcode) {
13623     case 0: /* SHA512SU0 */
13624         feature = dc_isar_feature(aa64_sha512, s);
13625         break;
13626     case 1: /* SM4E */
13627         feature = dc_isar_feature(aa64_sm4, s);
13628         break;
13629     default:
13630         unallocated_encoding(s);
13631         return;
13632     }
13633 
13634     if (!feature) {
13635         unallocated_encoding(s);
13636         return;
13637     }
13638 
13639     if (!fp_access_check(s)) {
13640         return;
13641     }
13642 
13643     switch (opcode) {
13644     case 0: /* SHA512SU0 */
13645         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13646         break;
13647     case 1: /* SM4E */
13648         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13649         break;
13650     default:
13651         g_assert_not_reached();
13652     }
13653 }
13654 
13655 /* Crypto four-register
13656  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13657  * +-------------------+-----+------+---+------+------+------+
13658  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13659  * +-------------------+-----+------+---+------+------+------+
13660  */
13661 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13662 {
13663     int op0 = extract32(insn, 21, 2);
13664     int rm = extract32(insn, 16, 5);
13665     int ra = extract32(insn, 10, 5);
13666     int rn = extract32(insn, 5, 5);
13667     int rd = extract32(insn, 0, 5);
13668     bool feature;
13669 
13670     switch (op0) {
13671     case 0: /* EOR3 */
13672     case 1: /* BCAX */
13673         feature = dc_isar_feature(aa64_sha3, s);
13674         break;
13675     case 2: /* SM3SS1 */
13676         feature = dc_isar_feature(aa64_sm3, s);
13677         break;
13678     default:
13679         unallocated_encoding(s);
13680         return;
13681     }
13682 
13683     if (!feature) {
13684         unallocated_encoding(s);
13685         return;
13686     }
13687 
13688     if (!fp_access_check(s)) {
13689         return;
13690     }
13691 
13692     if (op0 < 2) {
13693         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13694         int pass;
13695 
13696         tcg_op1 = tcg_temp_new_i64();
13697         tcg_op2 = tcg_temp_new_i64();
13698         tcg_op3 = tcg_temp_new_i64();
13699         tcg_res[0] = tcg_temp_new_i64();
13700         tcg_res[1] = tcg_temp_new_i64();
13701 
13702         for (pass = 0; pass < 2; pass++) {
13703             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13704             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13705             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13706 
13707             if (op0 == 0) {
13708                 /* EOR3 */
13709                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13710             } else {
13711                 /* BCAX */
13712                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13713             }
13714             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13715         }
13716         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13717         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13718     } else {
13719         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13720 
13721         tcg_op1 = tcg_temp_new_i32();
13722         tcg_op2 = tcg_temp_new_i32();
13723         tcg_op3 = tcg_temp_new_i32();
13724         tcg_res = tcg_temp_new_i32();
13725         tcg_zero = tcg_constant_i32(0);
13726 
13727         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13728         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13729         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13730 
13731         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13732         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13733         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13734         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13735 
13736         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13737         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13738         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13739         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13740     }
13741 }
13742 
13743 /* Crypto XAR
13744  *  31                   21 20  16 15    10 9    5 4    0
13745  * +-----------------------+------+--------+------+------+
13746  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13747  * +-----------------------+------+--------+------+------+
13748  */
13749 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13750 {
13751     int rm = extract32(insn, 16, 5);
13752     int imm6 = extract32(insn, 10, 6);
13753     int rn = extract32(insn, 5, 5);
13754     int rd = extract32(insn, 0, 5);
13755 
13756     if (!dc_isar_feature(aa64_sha3, s)) {
13757         unallocated_encoding(s);
13758         return;
13759     }
13760 
13761     if (!fp_access_check(s)) {
13762         return;
13763     }
13764 
13765     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13766                  vec_full_reg_offset(s, rn),
13767                  vec_full_reg_offset(s, rm), imm6, 16,
13768                  vec_full_reg_size(s));
13769 }
13770 
13771 /* Crypto three-reg imm2
13772  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13773  * +-----------------------+------+-----+------+--------+------+------+
13774  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13775  * +-----------------------+------+-----+------+--------+------+------+
13776  */
13777 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13778 {
13779     static gen_helper_gvec_3 * const fns[4] = {
13780         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13781         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13782     };
13783     int opcode = extract32(insn, 10, 2);
13784     int imm2 = extract32(insn, 12, 2);
13785     int rm = extract32(insn, 16, 5);
13786     int rn = extract32(insn, 5, 5);
13787     int rd = extract32(insn, 0, 5);
13788 
13789     if (!dc_isar_feature(aa64_sm3, s)) {
13790         unallocated_encoding(s);
13791         return;
13792     }
13793 
13794     if (!fp_access_check(s)) {
13795         return;
13796     }
13797 
13798     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13799 }
13800 
13801 /* C3.6 Data processing - SIMD, inc Crypto
13802  *
13803  * As the decode gets a little complex we are using a table based
13804  * approach for this part of the decode.
13805  */
13806 static const AArch64DecodeTable data_proc_simd[] = {
13807     /* pattern  ,  mask     ,  fn                        */
13808     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13809     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13810     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13811     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13812     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13813     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13814     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13815     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13816     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13817     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13818     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13819     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13820     { 0x2e000000, 0xbf208400, disas_simd_ext },
13821     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13822     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13823     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13824     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13825     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13826     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13827     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13828     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13829     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13830     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13831     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13832     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13833     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13834     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13835     { 0xce800000, 0xffe00000, disas_crypto_xar },
13836     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13837     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13838     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13839     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13840     { 0x00000000, 0x00000000, NULL }
13841 };
13842 
13843 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13844 {
13845     /* Note that this is called with all non-FP cases from
13846      * table C3-6 so it must UNDEF for entries not specifically
13847      * allocated to instructions in that table.
13848      */
13849     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13850     if (fn) {
13851         fn(s, insn);
13852     } else {
13853         unallocated_encoding(s);
13854     }
13855 }
13856 
13857 /* C3.6 Data processing - SIMD and floating point */
13858 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13859 {
13860     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13861         disas_data_proc_fp(s, insn);
13862     } else {
13863         /* SIMD, including crypto */
13864         disas_data_proc_simd(s, insn);
13865     }
13866 }
13867 
13868 static bool trans_OK(DisasContext *s, arg_OK *a)
13869 {
13870     return true;
13871 }
13872 
13873 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13874 {
13875     s->is_nonstreaming = true;
13876     return true;
13877 }
13878 
13879 /**
13880  * is_guarded_page:
13881  * @env: The cpu environment
13882  * @s: The DisasContext
13883  *
13884  * Return true if the page is guarded.
13885  */
13886 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13887 {
13888     uint64_t addr = s->base.pc_first;
13889 #ifdef CONFIG_USER_ONLY
13890     return page_get_flags(addr) & PAGE_BTI;
13891 #else
13892     CPUTLBEntryFull *full;
13893     void *host;
13894     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13895     int flags;
13896 
13897     /*
13898      * We test this immediately after reading an insn, which means
13899      * that the TLB entry must be present and valid, and thus this
13900      * access will never raise an exception.
13901      */
13902     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13903                               false, &host, &full, 0);
13904     assert(!(flags & TLB_INVALID_MASK));
13905 
13906     return full->extra.arm.guarded;
13907 #endif
13908 }
13909 
13910 /**
13911  * btype_destination_ok:
13912  * @insn: The instruction at the branch destination
13913  * @bt: SCTLR_ELx.BT
13914  * @btype: PSTATE.BTYPE, and is non-zero
13915  *
13916  * On a guarded page, there are a limited number of insns
13917  * that may be present at the branch target:
13918  *   - branch target identifiers,
13919  *   - paciasp, pacibsp,
13920  *   - BRK insn
13921  *   - HLT insn
13922  * Anything else causes a Branch Target Exception.
13923  *
13924  * Return true if the branch is compatible, false to raise BTITRAP.
13925  */
13926 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13927 {
13928     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13929         /* HINT space */
13930         switch (extract32(insn, 5, 7)) {
13931         case 0b011001: /* PACIASP */
13932         case 0b011011: /* PACIBSP */
13933             /*
13934              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13935              * with btype == 3.  Otherwise all btype are ok.
13936              */
13937             return !bt || btype != 3;
13938         case 0b100000: /* BTI */
13939             /* Not compatible with any btype.  */
13940             return false;
13941         case 0b100010: /* BTI c */
13942             /* Not compatible with btype == 3 */
13943             return btype != 3;
13944         case 0b100100: /* BTI j */
13945             /* Not compatible with btype == 2 */
13946             return btype != 2;
13947         case 0b100110: /* BTI jc */
13948             /* Compatible with any btype.  */
13949             return true;
13950         }
13951     } else {
13952         switch (insn & 0xffe0001fu) {
13953         case 0xd4200000u: /* BRK */
13954         case 0xd4400000u: /* HLT */
13955             /* Give priority to the breakpoint exception.  */
13956             return true;
13957         }
13958     }
13959     return false;
13960 }
13961 
13962 /* C3.1 A64 instruction index by encoding */
13963 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13964 {
13965     switch (extract32(insn, 25, 4)) {
13966     case 0x5:
13967     case 0xd:      /* Data processing - register */
13968         disas_data_proc_reg(s, insn);
13969         break;
13970     case 0x7:
13971     case 0xf:      /* Data processing - SIMD and floating point */
13972         disas_data_proc_simd_fp(s, insn);
13973         break;
13974     default:
13975         unallocated_encoding(s);
13976         break;
13977     }
13978 }
13979 
13980 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13981                                           CPUState *cpu)
13982 {
13983     DisasContext *dc = container_of(dcbase, DisasContext, base);
13984     CPUARMState *env = cpu_env(cpu);
13985     ARMCPU *arm_cpu = env_archcpu(env);
13986     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13987     int bound, core_mmu_idx;
13988 
13989     dc->isar = &arm_cpu->isar;
13990     dc->condjmp = 0;
13991     dc->pc_save = dc->base.pc_first;
13992     dc->aarch64 = true;
13993     dc->thumb = false;
13994     dc->sctlr_b = 0;
13995     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13996     dc->condexec_mask = 0;
13997     dc->condexec_cond = 0;
13998     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
13999     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14000     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14001     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14002     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14003     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14004 #if !defined(CONFIG_USER_ONLY)
14005     dc->user = (dc->current_el == 0);
14006 #endif
14007     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14008     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14009     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14010     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14011     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14012     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
14013     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14014     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14015     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14016     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14017     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14018     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14019     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14020     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14021     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
14022     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
14023     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14024     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14025     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14026     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14027     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14028     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14029     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
14030     dc->vec_len = 0;
14031     dc->vec_stride = 0;
14032     dc->cp_regs = arm_cpu->cp_regs;
14033     dc->features = env->features;
14034     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14035     dc->gm_blocksize = arm_cpu->gm_blocksize;
14036 
14037 #ifdef CONFIG_USER_ONLY
14038     /* In sve_probe_page, we assume TBI is enabled. */
14039     tcg_debug_assert(dc->tbid & 1);
14040 #endif
14041 
14042     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14043 
14044     /* Single step state. The code-generation logic here is:
14045      *  SS_ACTIVE == 0:
14046      *   generate code with no special handling for single-stepping (except
14047      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14048      *   this happens anyway because those changes are all system register or
14049      *   PSTATE writes).
14050      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14051      *   emit code for one insn
14052      *   emit code to clear PSTATE.SS
14053      *   emit code to generate software step exception for completed step
14054      *   end TB (as usual for having generated an exception)
14055      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14056      *   emit code to generate a software step exception
14057      *   end the TB
14058      */
14059     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14060     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14061     dc->is_ldex = false;
14062 
14063     /* Bound the number of insns to execute to those left on the page.  */
14064     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14065 
14066     /* If architectural single step active, limit to 1.  */
14067     if (dc->ss_active) {
14068         bound = 1;
14069     }
14070     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14071 }
14072 
14073 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14074 {
14075 }
14076 
14077 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14078 {
14079     DisasContext *dc = container_of(dcbase, DisasContext, base);
14080     target_ulong pc_arg = dc->base.pc_next;
14081 
14082     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14083         pc_arg &= ~TARGET_PAGE_MASK;
14084     }
14085     tcg_gen_insn_start(pc_arg, 0, 0);
14086     dc->insn_start = tcg_last_op();
14087 }
14088 
14089 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14090 {
14091     DisasContext *s = container_of(dcbase, DisasContext, base);
14092     CPUARMState *env = cpu_env(cpu);
14093     uint64_t pc = s->base.pc_next;
14094     uint32_t insn;
14095 
14096     /* Singlestep exceptions have the highest priority. */
14097     if (s->ss_active && !s->pstate_ss) {
14098         /* Singlestep state is Active-pending.
14099          * If we're in this state at the start of a TB then either
14100          *  a) we just took an exception to an EL which is being debugged
14101          *     and this is the first insn in the exception handler
14102          *  b) debug exceptions were masked and we just unmasked them
14103          *     without changing EL (eg by clearing PSTATE.D)
14104          * In either case we're going to take a swstep exception in the
14105          * "did not step an insn" case, and so the syndrome ISV and EX
14106          * bits should be zero.
14107          */
14108         assert(s->base.num_insns == 1);
14109         gen_swstep_exception(s, 0, 0);
14110         s->base.is_jmp = DISAS_NORETURN;
14111         s->base.pc_next = pc + 4;
14112         return;
14113     }
14114 
14115     if (pc & 3) {
14116         /*
14117          * PC alignment fault.  This has priority over the instruction abort
14118          * that we would receive from a translation fault via arm_ldl_code.
14119          * This should only be possible after an indirect branch, at the
14120          * start of the TB.
14121          */
14122         assert(s->base.num_insns == 1);
14123         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
14124         s->base.is_jmp = DISAS_NORETURN;
14125         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14126         return;
14127     }
14128 
14129     s->pc_curr = pc;
14130     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14131     s->insn = insn;
14132     s->base.pc_next = pc + 4;
14133 
14134     s->fp_access_checked = false;
14135     s->sve_access_checked = false;
14136 
14137     if (s->pstate_il) {
14138         /*
14139          * Illegal execution state. This has priority over BTI
14140          * exceptions, but comes after instruction abort exceptions.
14141          */
14142         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14143         return;
14144     }
14145 
14146     if (dc_isar_feature(aa64_bti, s)) {
14147         if (s->base.num_insns == 1) {
14148             /*
14149              * At the first insn of the TB, compute s->guarded_page.
14150              * We delayed computing this until successfully reading
14151              * the first insn of the TB, above.  This (mostly) ensures
14152              * that the softmmu tlb entry has been populated, and the
14153              * page table GP bit is available.
14154              *
14155              * Note that we need to compute this even if btype == 0,
14156              * because this value is used for BR instructions later
14157              * where ENV is not available.
14158              */
14159             s->guarded_page = is_guarded_page(env, s);
14160 
14161             /* First insn can have btype set to non-zero.  */
14162             tcg_debug_assert(s->btype >= 0);
14163 
14164             /*
14165              * Note that the Branch Target Exception has fairly high
14166              * priority -- below debugging exceptions but above most
14167              * everything else.  This allows us to handle this now
14168              * instead of waiting until the insn is otherwise decoded.
14169              */
14170             if (s->btype != 0
14171                 && s->guarded_page
14172                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14173                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14174                 return;
14175             }
14176         } else {
14177             /* Not the first insn: btype must be 0.  */
14178             tcg_debug_assert(s->btype == 0);
14179         }
14180     }
14181 
14182     s->is_nonstreaming = false;
14183     if (s->sme_trap_nonstreaming) {
14184         disas_sme_fa64(s, insn);
14185     }
14186 
14187     if (!disas_a64(s, insn) &&
14188         !disas_sme(s, insn) &&
14189         !disas_sve(s, insn)) {
14190         disas_a64_legacy(s, insn);
14191     }
14192 
14193     /*
14194      * After execution of most insns, btype is reset to 0.
14195      * Note that we set btype == -1 when the insn sets btype.
14196      */
14197     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14198         reset_btype(s);
14199     }
14200 }
14201 
14202 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14203 {
14204     DisasContext *dc = container_of(dcbase, DisasContext, base);
14205 
14206     if (unlikely(dc->ss_active)) {
14207         /* Note that this means single stepping WFI doesn't halt the CPU.
14208          * For conditional branch insns this is harmless unreachable code as
14209          * gen_goto_tb() has already handled emitting the debug exception
14210          * (and thus a tb-jump is not possible when singlestepping).
14211          */
14212         switch (dc->base.is_jmp) {
14213         default:
14214             gen_a64_update_pc(dc, 4);
14215             /* fall through */
14216         case DISAS_EXIT:
14217         case DISAS_JUMP:
14218             gen_step_complete_exception(dc);
14219             break;
14220         case DISAS_NORETURN:
14221             break;
14222         }
14223     } else {
14224         switch (dc->base.is_jmp) {
14225         case DISAS_NEXT:
14226         case DISAS_TOO_MANY:
14227             gen_goto_tb(dc, 1, 4);
14228             break;
14229         default:
14230         case DISAS_UPDATE_EXIT:
14231             gen_a64_update_pc(dc, 4);
14232             /* fall through */
14233         case DISAS_EXIT:
14234             tcg_gen_exit_tb(NULL, 0);
14235             break;
14236         case DISAS_UPDATE_NOCHAIN:
14237             gen_a64_update_pc(dc, 4);
14238             /* fall through */
14239         case DISAS_JUMP:
14240             tcg_gen_lookup_and_goto_ptr();
14241             break;
14242         case DISAS_NORETURN:
14243         case DISAS_SWI:
14244             break;
14245         case DISAS_WFE:
14246             gen_a64_update_pc(dc, 4);
14247             gen_helper_wfe(tcg_env);
14248             break;
14249         case DISAS_YIELD:
14250             gen_a64_update_pc(dc, 4);
14251             gen_helper_yield(tcg_env);
14252             break;
14253         case DISAS_WFI:
14254             /*
14255              * This is a special case because we don't want to just halt
14256              * the CPU if trying to debug across a WFI.
14257              */
14258             gen_a64_update_pc(dc, 4);
14259             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
14260             /*
14261              * The helper doesn't necessarily throw an exception, but we
14262              * must go back to the main loop to check for interrupts anyway.
14263              */
14264             tcg_gen_exit_tb(NULL, 0);
14265             break;
14266         }
14267     }
14268 }
14269 
14270 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14271                                  CPUState *cpu, FILE *logfile)
14272 {
14273     DisasContext *dc = container_of(dcbase, DisasContext, base);
14274 
14275     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14276     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14277 }
14278 
14279 const TranslatorOps aarch64_translator_ops = {
14280     .init_disas_context = aarch64_tr_init_disas_context,
14281     .tb_start           = aarch64_tr_tb_start,
14282     .insn_start         = aarch64_tr_insn_start,
14283     .translate_insn     = aarch64_tr_translate_insn,
14284     .tb_stop            = aarch64_tr_tb_stop,
14285     .disas_log          = aarch64_tr_disas_log,
14286 };
14287