xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision d90a4733)
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 "arm_ldst.h"
26 #include "semihosting/semihost.h"
27 #include "cpregs.h"
28 
29 static TCGv_i64 cpu_X[32];
30 static TCGv_i64 cpu_pc;
31 
32 /* Load/store exclusive handling */
33 static TCGv_i64 cpu_exclusive_high;
34 
35 static const char *regnames[] = {
36     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
37     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
38     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
39     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
40 };
41 
42 enum a64_shift_type {
43     A64_SHIFT_TYPE_LSL = 0,
44     A64_SHIFT_TYPE_LSR = 1,
45     A64_SHIFT_TYPE_ASR = 2,
46     A64_SHIFT_TYPE_ROR = 3
47 };
48 
49 /*
50  * Helpers for extracting complex instruction fields
51  */
52 
53 /*
54  * For load/store with an unsigned 12 bit immediate scaled by the element
55  * size. The input has the immediate field in bits [14:3] and the element
56  * size in [2:0].
57  */
58 static int uimm_scaled(DisasContext *s, int x)
59 {
60     unsigned imm = x >> 3;
61     unsigned scale = extract32(x, 0, 3);
62     return imm << scale;
63 }
64 
65 /* For load/store memory tags: scale offset by LOG2_TAG_GRANULE */
66 static int scale_by_log2_tag_granule(DisasContext *s, int x)
67 {
68     return x << LOG2_TAG_GRANULE;
69 }
70 
71 /*
72  * Include the generated decoders.
73  */
74 
75 #include "decode-sme-fa64.c.inc"
76 #include "decode-a64.c.inc"
77 
78 /* Table based decoder typedefs - used when the relevant bits for decode
79  * are too awkwardly scattered across the instruction (eg SIMD).
80  */
81 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
82 
83 typedef struct AArch64DecodeTable {
84     uint32_t pattern;
85     uint32_t mask;
86     AArch64DecodeFn *disas_fn;
87 } AArch64DecodeTable;
88 
89 /* initialize TCG globals.  */
90 void a64_translate_init(void)
91 {
92     int i;
93 
94     cpu_pc = tcg_global_mem_new_i64(tcg_env,
95                                     offsetof(CPUARMState, pc),
96                                     "pc");
97     for (i = 0; i < 32; i++) {
98         cpu_X[i] = tcg_global_mem_new_i64(tcg_env,
99                                           offsetof(CPUARMState, xregs[i]),
100                                           regnames[i]);
101     }
102 
103     cpu_exclusive_high = tcg_global_mem_new_i64(tcg_env,
104         offsetof(CPUARMState, exclusive_high), "exclusive_high");
105 }
106 
107 /*
108  * Return the core mmu_idx to use for A64 load/store insns which
109  * have a "unprivileged load/store" variant. Those insns access
110  * EL0 if executed from an EL which has control over EL0 (usually
111  * EL1) but behave like normal loads and stores if executed from
112  * elsewhere (eg EL3).
113  *
114  * @unpriv : true for the unprivileged encoding; false for the
115  *           normal encoding (in which case we will return the same
116  *           thing as get_mem_index().
117  */
118 static int get_a64_user_mem_index(DisasContext *s, bool unpriv)
119 {
120     /*
121      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
122      * which is the usual mmu_idx for this cpu state.
123      */
124     ARMMMUIdx useridx = s->mmu_idx;
125 
126     if (unpriv && s->unpriv) {
127         /*
128          * We have pre-computed the condition for AccType_UNPRIV.
129          * Therefore we should never get here with a mmu_idx for
130          * which we do not know the corresponding user mmu_idx.
131          */
132         switch (useridx) {
133         case ARMMMUIdx_E10_1:
134         case ARMMMUIdx_E10_1_PAN:
135             useridx = ARMMMUIdx_E10_0;
136             break;
137         case ARMMMUIdx_E20_2:
138         case ARMMMUIdx_E20_2_PAN:
139             useridx = ARMMMUIdx_E20_0;
140             break;
141         default:
142             g_assert_not_reached();
143         }
144     }
145     return arm_to_core_mmu_idx(useridx);
146 }
147 
148 static void set_btype_raw(int val)
149 {
150     tcg_gen_st_i32(tcg_constant_i32(val), tcg_env,
151                    offsetof(CPUARMState, btype));
152 }
153 
154 static void set_btype(DisasContext *s, int val)
155 {
156     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
157     tcg_debug_assert(val >= 1 && val <= 3);
158     set_btype_raw(val);
159     s->btype = -1;
160 }
161 
162 static void reset_btype(DisasContext *s)
163 {
164     if (s->btype != 0) {
165         set_btype_raw(0);
166         s->btype = 0;
167     }
168 }
169 
170 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
171 {
172     assert(s->pc_save != -1);
173     if (tb_cflags(s->base.tb) & CF_PCREL) {
174         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
175     } else {
176         tcg_gen_movi_i64(dest, s->pc_curr + diff);
177     }
178 }
179 
180 void gen_a64_update_pc(DisasContext *s, target_long diff)
181 {
182     gen_pc_plus_diff(s, cpu_pc, diff);
183     s->pc_save = s->pc_curr + diff;
184 }
185 
186 /*
187  * Handle Top Byte Ignore (TBI) bits.
188  *
189  * If address tagging is enabled via the TCR TBI bits:
190  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
191  *    then the address is zero-extended, clearing bits [63:56]
192  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
193  *    and TBI1 controls addresses with bit 55 == 1.
194  *    If the appropriate TBI bit is set for the address then
195  *    the address is sign-extended from bit 55 into bits [63:56]
196  *
197  * Here We have concatenated TBI{1,0} into tbi.
198  */
199 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
200                                 TCGv_i64 src, int tbi)
201 {
202     if (tbi == 0) {
203         /* Load unmodified address */
204         tcg_gen_mov_i64(dst, src);
205     } else if (!regime_has_2_ranges(s->mmu_idx)) {
206         /* Force tag byte to all zero */
207         tcg_gen_extract_i64(dst, src, 0, 56);
208     } else {
209         /* Sign-extend from bit 55.  */
210         tcg_gen_sextract_i64(dst, src, 0, 56);
211 
212         switch (tbi) {
213         case 1:
214             /* tbi0 but !tbi1: only use the extension if positive */
215             tcg_gen_and_i64(dst, dst, src);
216             break;
217         case 2:
218             /* !tbi0 but tbi1: only use the extension if negative */
219             tcg_gen_or_i64(dst, dst, src);
220             break;
221         case 3:
222             /* tbi0 and tbi1: always use the extension */
223             break;
224         default:
225             g_assert_not_reached();
226         }
227     }
228 }
229 
230 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
231 {
232     /*
233      * If address tagging is enabled for instructions via the TCR TBI bits,
234      * then loading an address into the PC will clear out any tag.
235      */
236     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
237     s->pc_save = -1;
238 }
239 
240 /*
241  * Handle MTE and/or TBI.
242  *
243  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
244  * for the tag to be present in the FAR_ELx register.  But for user-only
245  * mode we do not have a TLB with which to implement this, so we must
246  * remove the top byte now.
247  *
248  * Always return a fresh temporary that we can increment independently
249  * of the write-back address.
250  */
251 
252 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
253 {
254     TCGv_i64 clean = tcg_temp_new_i64();
255 #ifdef CONFIG_USER_ONLY
256     gen_top_byte_ignore(s, clean, addr, s->tbid);
257 #else
258     tcg_gen_mov_i64(clean, addr);
259 #endif
260     return clean;
261 }
262 
263 /* Insert a zero tag into src, with the result at dst. */
264 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
265 {
266     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
267 }
268 
269 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
270                              MMUAccessType acc, int log2_size)
271 {
272     gen_helper_probe_access(tcg_env, ptr,
273                             tcg_constant_i32(acc),
274                             tcg_constant_i32(get_mem_index(s)),
275                             tcg_constant_i32(1 << log2_size));
276 }
277 
278 /*
279  * For MTE, check a single logical or atomic access.  This probes a single
280  * address, the exact one specified.  The size and alignment of the access
281  * is not relevant to MTE, per se, but watchpoints do require the size,
282  * and we want to recognize those before making any other changes to state.
283  */
284 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
285                                       bool is_write, bool tag_checked,
286                                       MemOp memop, bool is_unpriv,
287                                       int core_idx)
288 {
289     if (tag_checked && s->mte_active[is_unpriv]) {
290         TCGv_i64 ret;
291         int desc = 0;
292 
293         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
294         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
295         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
296         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
297         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
298         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
299 
300         ret = tcg_temp_new_i64();
301         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
302 
303         return ret;
304     }
305     return clean_data_tbi(s, addr);
306 }
307 
308 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
309                         bool tag_checked, MemOp memop)
310 {
311     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
312                                  false, get_mem_index(s));
313 }
314 
315 /*
316  * For MTE, check multiple logical sequential accesses.
317  */
318 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
319                         bool tag_checked, int total_size, MemOp single_mop)
320 {
321     if (tag_checked && s->mte_active[0]) {
322         TCGv_i64 ret;
323         int desc = 0;
324 
325         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
326         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
327         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
328         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
329         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
330         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
331 
332         ret = tcg_temp_new_i64();
333         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
334 
335         return ret;
336     }
337     return clean_data_tbi(s, addr);
338 }
339 
340 /*
341  * Generate the special alignment check that applies to AccType_ATOMIC
342  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
343  * naturally aligned, but it must not cross a 16-byte boundary.
344  * See AArch64.CheckAlignment().
345  */
346 static void check_lse2_align(DisasContext *s, int rn, int imm,
347                              bool is_write, MemOp mop)
348 {
349     TCGv_i32 tmp;
350     TCGv_i64 addr;
351     TCGLabel *over_label;
352     MMUAccessType type;
353     int mmu_idx;
354 
355     tmp = tcg_temp_new_i32();
356     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
357     tcg_gen_addi_i32(tmp, tmp, imm & 15);
358     tcg_gen_andi_i32(tmp, tmp, 15);
359     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
360 
361     over_label = gen_new_label();
362     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
363 
364     addr = tcg_temp_new_i64();
365     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
366 
367     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
368     mmu_idx = get_mem_index(s);
369     gen_helper_unaligned_access(tcg_env, addr, tcg_constant_i32(type),
370                                 tcg_constant_i32(mmu_idx));
371 
372     gen_set_label(over_label);
373 
374 }
375 
376 /* Handle the alignment check for AccType_ATOMIC instructions. */
377 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
378 {
379     MemOp size = mop & MO_SIZE;
380 
381     if (size == MO_8) {
382         return mop;
383     }
384 
385     /*
386      * If size == MO_128, this is a LDXP, and the operation is single-copy
387      * atomic for each doubleword, not the entire quadword; it still must
388      * be quadword aligned.
389      */
390     if (size == MO_128) {
391         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
392                                    MO_ATOM_IFALIGN_PAIR);
393     }
394     if (dc_isar_feature(aa64_lse2, s)) {
395         check_lse2_align(s, rn, 0, true, mop);
396     } else {
397         mop |= MO_ALIGN;
398     }
399     return finalize_memop(s, mop);
400 }
401 
402 /* Handle the alignment check for AccType_ORDERED instructions. */
403 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
404                                  bool is_write, MemOp mop)
405 {
406     MemOp size = mop & MO_SIZE;
407 
408     if (size == MO_8) {
409         return mop;
410     }
411     if (size == MO_128) {
412         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
413                                    MO_ATOM_IFALIGN_PAIR);
414     }
415     if (!dc_isar_feature(aa64_lse2, s)) {
416         mop |= MO_ALIGN;
417     } else if (!s->naa) {
418         check_lse2_align(s, rn, imm, is_write, mop);
419     }
420     return finalize_memop(s, mop);
421 }
422 
423 typedef struct DisasCompare64 {
424     TCGCond cond;
425     TCGv_i64 value;
426 } DisasCompare64;
427 
428 static void a64_test_cc(DisasCompare64 *c64, int cc)
429 {
430     DisasCompare c32;
431 
432     arm_test_cc(&c32, cc);
433 
434     /*
435      * Sign-extend the 32-bit value so that the GE/LT comparisons work
436      * properly.  The NE/EQ comparisons are also fine with this choice.
437       */
438     c64->cond = c32.cond;
439     c64->value = tcg_temp_new_i64();
440     tcg_gen_ext_i32_i64(c64->value, c32.value);
441 }
442 
443 static void gen_rebuild_hflags(DisasContext *s)
444 {
445     gen_helper_rebuild_hflags_a64(tcg_env, tcg_constant_i32(s->current_el));
446 }
447 
448 static void gen_exception_internal(int excp)
449 {
450     assert(excp_is_internal(excp));
451     gen_helper_exception_internal(tcg_env, tcg_constant_i32(excp));
452 }
453 
454 static void gen_exception_internal_insn(DisasContext *s, int excp)
455 {
456     gen_a64_update_pc(s, 0);
457     gen_exception_internal(excp);
458     s->base.is_jmp = DISAS_NORETURN;
459 }
460 
461 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
462 {
463     gen_a64_update_pc(s, 0);
464     gen_helper_exception_bkpt_insn(tcg_env, tcg_constant_i32(syndrome));
465     s->base.is_jmp = DISAS_NORETURN;
466 }
467 
468 static void gen_step_complete_exception(DisasContext *s)
469 {
470     /* We just completed step of an insn. Move from Active-not-pending
471      * to Active-pending, and then also take the swstep exception.
472      * This corresponds to making the (IMPDEF) choice to prioritize
473      * swstep exceptions over asynchronous exceptions taken to an exception
474      * level where debug is disabled. This choice has the advantage that
475      * we do not need to maintain internal state corresponding to the
476      * ISV/EX syndrome bits between completion of the step and generation
477      * of the exception, and our syndrome information is always correct.
478      */
479     gen_ss_advance(s);
480     gen_swstep_exception(s, 1, s->is_ldex);
481     s->base.is_jmp = DISAS_NORETURN;
482 }
483 
484 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
485 {
486     if (s->ss_active) {
487         return false;
488     }
489     return translator_use_goto_tb(&s->base, dest);
490 }
491 
492 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
493 {
494     if (use_goto_tb(s, s->pc_curr + diff)) {
495         /*
496          * For pcrel, the pc must always be up-to-date on entry to
497          * the linked TB, so that it can use simple additions for all
498          * further adjustments.  For !pcrel, the linked TB is compiled
499          * to know its full virtual address, so we can delay the
500          * update to pc to the unlinked path.  A long chain of links
501          * can thus avoid many updates to the PC.
502          */
503         if (tb_cflags(s->base.tb) & CF_PCREL) {
504             gen_a64_update_pc(s, diff);
505             tcg_gen_goto_tb(n);
506         } else {
507             tcg_gen_goto_tb(n);
508             gen_a64_update_pc(s, diff);
509         }
510         tcg_gen_exit_tb(s->base.tb, n);
511         s->base.is_jmp = DISAS_NORETURN;
512     } else {
513         gen_a64_update_pc(s, diff);
514         if (s->ss_active) {
515             gen_step_complete_exception(s);
516         } else {
517             tcg_gen_lookup_and_goto_ptr();
518             s->base.is_jmp = DISAS_NORETURN;
519         }
520     }
521 }
522 
523 /*
524  * Register access functions
525  *
526  * These functions are used for directly accessing a register in where
527  * changes to the final register value are likely to be made. If you
528  * need to use a register for temporary calculation (e.g. index type
529  * operations) use the read_* form.
530  *
531  * B1.2.1 Register mappings
532  *
533  * In instruction register encoding 31 can refer to ZR (zero register) or
534  * the SP (stack pointer) depending on context. In QEMU's case we map SP
535  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
536  * This is the point of the _sp forms.
537  */
538 TCGv_i64 cpu_reg(DisasContext *s, int reg)
539 {
540     if (reg == 31) {
541         TCGv_i64 t = tcg_temp_new_i64();
542         tcg_gen_movi_i64(t, 0);
543         return t;
544     } else {
545         return cpu_X[reg];
546     }
547 }
548 
549 /* register access for when 31 == SP */
550 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
551 {
552     return cpu_X[reg];
553 }
554 
555 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
556  * representing the register contents. This TCGv is an auto-freed
557  * temporary so it need not be explicitly freed, and may be modified.
558  */
559 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
560 {
561     TCGv_i64 v = tcg_temp_new_i64();
562     if (reg != 31) {
563         if (sf) {
564             tcg_gen_mov_i64(v, cpu_X[reg]);
565         } else {
566             tcg_gen_ext32u_i64(v, cpu_X[reg]);
567         }
568     } else {
569         tcg_gen_movi_i64(v, 0);
570     }
571     return v;
572 }
573 
574 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
575 {
576     TCGv_i64 v = tcg_temp_new_i64();
577     if (sf) {
578         tcg_gen_mov_i64(v, cpu_X[reg]);
579     } else {
580         tcg_gen_ext32u_i64(v, cpu_X[reg]);
581     }
582     return v;
583 }
584 
585 /* Return the offset into CPUARMState of a slice (from
586  * the least significant end) of FP register Qn (ie
587  * Dn, Sn, Hn or Bn).
588  * (Note that this is not the same mapping as for A32; see cpu.h)
589  */
590 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
591 {
592     return vec_reg_offset(s, regno, 0, size);
593 }
594 
595 /* Offset of the high half of the 128 bit vector Qn */
596 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
597 {
598     return vec_reg_offset(s, regno, 1, MO_64);
599 }
600 
601 /* Convenience accessors for reading and writing single and double
602  * FP registers. Writing clears the upper parts of the associated
603  * 128 bit vector register, as required by the architecture.
604  * Note that unlike the GP register accessors, the values returned
605  * by the read functions must be manually freed.
606  */
607 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
608 {
609     TCGv_i64 v = tcg_temp_new_i64();
610 
611     tcg_gen_ld_i64(v, tcg_env, fp_reg_offset(s, reg, MO_64));
612     return v;
613 }
614 
615 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
616 {
617     TCGv_i32 v = tcg_temp_new_i32();
618 
619     tcg_gen_ld_i32(v, tcg_env, fp_reg_offset(s, reg, MO_32));
620     return v;
621 }
622 
623 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
624 {
625     TCGv_i32 v = tcg_temp_new_i32();
626 
627     tcg_gen_ld16u_i32(v, tcg_env, fp_reg_offset(s, reg, MO_16));
628     return v;
629 }
630 
631 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
632  * If SVE is not enabled, then there are only 128 bits in the vector.
633  */
634 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
635 {
636     unsigned ofs = fp_reg_offset(s, rd, MO_64);
637     unsigned vsz = vec_full_reg_size(s);
638 
639     /* Nop move, with side effect of clearing the tail. */
640     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
641 }
642 
643 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
644 {
645     unsigned ofs = fp_reg_offset(s, reg, MO_64);
646 
647     tcg_gen_st_i64(v, tcg_env, ofs);
648     clear_vec_high(s, false, reg);
649 }
650 
651 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
652 {
653     TCGv_i64 tmp = tcg_temp_new_i64();
654 
655     tcg_gen_extu_i32_i64(tmp, v);
656     write_fp_dreg(s, reg, tmp);
657 }
658 
659 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
660 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
661                          GVecGen2Fn *gvec_fn, int vece)
662 {
663     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
664             is_q ? 16 : 8, vec_full_reg_size(s));
665 }
666 
667 /* Expand a 2-operand + immediate AdvSIMD vector operation using
668  * an expander function.
669  */
670 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
671                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
672 {
673     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674             imm, is_q ? 16 : 8, vec_full_reg_size(s));
675 }
676 
677 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
678 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
679                          GVecGen3Fn *gvec_fn, int vece)
680 {
681     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
682             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
683 }
684 
685 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
686 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
687                          int rx, GVecGen4Fn *gvec_fn, int vece)
688 {
689     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
690             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
691             is_q ? 16 : 8, vec_full_reg_size(s));
692 }
693 
694 /* Expand a 2-operand operation using an out-of-line helper.  */
695 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
696                              int rn, int data, gen_helper_gvec_2 *fn)
697 {
698     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
699                        vec_full_reg_offset(s, rn),
700                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
701 }
702 
703 /* Expand a 3-operand operation using an out-of-line helper.  */
704 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
705                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
706 {
707     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
708                        vec_full_reg_offset(s, rn),
709                        vec_full_reg_offset(s, rm),
710                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
711 }
712 
713 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
714  * an out-of-line helper.
715  */
716 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
717                               int rm, bool is_fp16, int data,
718                               gen_helper_gvec_3_ptr *fn)
719 {
720     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
721     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
722                        vec_full_reg_offset(s, rn),
723                        vec_full_reg_offset(s, rm), fpst,
724                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
725 }
726 
727 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
728 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
729                             int rm, gen_helper_gvec_3_ptr *fn)
730 {
731     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
732 
733     tcg_gen_addi_ptr(qc_ptr, tcg_env, offsetof(CPUARMState, vfp.qc));
734     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
735                        vec_full_reg_offset(s, rn),
736                        vec_full_reg_offset(s, rm), qc_ptr,
737                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
738 }
739 
740 /* Expand a 4-operand operation using an out-of-line helper.  */
741 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
742                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
743 {
744     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
745                        vec_full_reg_offset(s, rn),
746                        vec_full_reg_offset(s, rm),
747                        vec_full_reg_offset(s, ra),
748                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
749 }
750 
751 /*
752  * Expand a 4-operand + fpstatus pointer + simd data value operation using
753  * an out-of-line helper.
754  */
755 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
756                               int rm, int ra, bool is_fp16, int data,
757                               gen_helper_gvec_4_ptr *fn)
758 {
759     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
760     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
761                        vec_full_reg_offset(s, rn),
762                        vec_full_reg_offset(s, rm),
763                        vec_full_reg_offset(s, ra), fpst,
764                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
765 }
766 
767 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
768  * than the 32 bit equivalent.
769  */
770 static inline void gen_set_NZ64(TCGv_i64 result)
771 {
772     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
773     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
774 }
775 
776 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
777 static inline void gen_logic_CC(int sf, TCGv_i64 result)
778 {
779     if (sf) {
780         gen_set_NZ64(result);
781     } else {
782         tcg_gen_extrl_i64_i32(cpu_ZF, result);
783         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
784     }
785     tcg_gen_movi_i32(cpu_CF, 0);
786     tcg_gen_movi_i32(cpu_VF, 0);
787 }
788 
789 /* dest = T0 + T1; compute C, N, V and Z flags */
790 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
791 {
792     TCGv_i64 result, flag, tmp;
793     result = tcg_temp_new_i64();
794     flag = tcg_temp_new_i64();
795     tmp = tcg_temp_new_i64();
796 
797     tcg_gen_movi_i64(tmp, 0);
798     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
799 
800     tcg_gen_extrl_i64_i32(cpu_CF, flag);
801 
802     gen_set_NZ64(result);
803 
804     tcg_gen_xor_i64(flag, result, t0);
805     tcg_gen_xor_i64(tmp, t0, t1);
806     tcg_gen_andc_i64(flag, flag, tmp);
807     tcg_gen_extrh_i64_i32(cpu_VF, flag);
808 
809     tcg_gen_mov_i64(dest, result);
810 }
811 
812 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
813 {
814     TCGv_i32 t0_32 = tcg_temp_new_i32();
815     TCGv_i32 t1_32 = tcg_temp_new_i32();
816     TCGv_i32 tmp = tcg_temp_new_i32();
817 
818     tcg_gen_movi_i32(tmp, 0);
819     tcg_gen_extrl_i64_i32(t0_32, t0);
820     tcg_gen_extrl_i64_i32(t1_32, t1);
821     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
822     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
823     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
824     tcg_gen_xor_i32(tmp, t0_32, t1_32);
825     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
826     tcg_gen_extu_i32_i64(dest, cpu_NF);
827 }
828 
829 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
830 {
831     if (sf) {
832         gen_add64_CC(dest, t0, t1);
833     } else {
834         gen_add32_CC(dest, t0, t1);
835     }
836 }
837 
838 /* dest = T0 - T1; compute C, N, V and Z flags */
839 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
840 {
841     /* 64 bit arithmetic */
842     TCGv_i64 result, flag, tmp;
843 
844     result = tcg_temp_new_i64();
845     flag = tcg_temp_new_i64();
846     tcg_gen_sub_i64(result, t0, t1);
847 
848     gen_set_NZ64(result);
849 
850     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
851     tcg_gen_extrl_i64_i32(cpu_CF, flag);
852 
853     tcg_gen_xor_i64(flag, result, t0);
854     tmp = tcg_temp_new_i64();
855     tcg_gen_xor_i64(tmp, t0, t1);
856     tcg_gen_and_i64(flag, flag, tmp);
857     tcg_gen_extrh_i64_i32(cpu_VF, flag);
858     tcg_gen_mov_i64(dest, result);
859 }
860 
861 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
862 {
863     /* 32 bit arithmetic */
864     TCGv_i32 t0_32 = tcg_temp_new_i32();
865     TCGv_i32 t1_32 = tcg_temp_new_i32();
866     TCGv_i32 tmp;
867 
868     tcg_gen_extrl_i64_i32(t0_32, t0);
869     tcg_gen_extrl_i64_i32(t1_32, t1);
870     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
871     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
872     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
873     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
874     tmp = tcg_temp_new_i32();
875     tcg_gen_xor_i32(tmp, t0_32, t1_32);
876     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
877     tcg_gen_extu_i32_i64(dest, cpu_NF);
878 }
879 
880 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
881 {
882     if (sf) {
883         gen_sub64_CC(dest, t0, t1);
884     } else {
885         gen_sub32_CC(dest, t0, t1);
886     }
887 }
888 
889 /* dest = T0 + T1 + CF; do not compute flags. */
890 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
891 {
892     TCGv_i64 flag = tcg_temp_new_i64();
893     tcg_gen_extu_i32_i64(flag, cpu_CF);
894     tcg_gen_add_i64(dest, t0, t1);
895     tcg_gen_add_i64(dest, dest, flag);
896 
897     if (!sf) {
898         tcg_gen_ext32u_i64(dest, dest);
899     }
900 }
901 
902 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
903 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
904 {
905     if (sf) {
906         TCGv_i64 result = tcg_temp_new_i64();
907         TCGv_i64 cf_64 = tcg_temp_new_i64();
908         TCGv_i64 vf_64 = tcg_temp_new_i64();
909         TCGv_i64 tmp = tcg_temp_new_i64();
910         TCGv_i64 zero = tcg_constant_i64(0);
911 
912         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
913         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
914         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
915         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
916         gen_set_NZ64(result);
917 
918         tcg_gen_xor_i64(vf_64, result, t0);
919         tcg_gen_xor_i64(tmp, t0, t1);
920         tcg_gen_andc_i64(vf_64, vf_64, tmp);
921         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
922 
923         tcg_gen_mov_i64(dest, result);
924     } else {
925         TCGv_i32 t0_32 = tcg_temp_new_i32();
926         TCGv_i32 t1_32 = tcg_temp_new_i32();
927         TCGv_i32 tmp = tcg_temp_new_i32();
928         TCGv_i32 zero = tcg_constant_i32(0);
929 
930         tcg_gen_extrl_i64_i32(t0_32, t0);
931         tcg_gen_extrl_i64_i32(t1_32, t1);
932         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
933         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
934 
935         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
936         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
937         tcg_gen_xor_i32(tmp, t0_32, t1_32);
938         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
939         tcg_gen_extu_i32_i64(dest, cpu_NF);
940     }
941 }
942 
943 /*
944  * Load/Store generators
945  */
946 
947 /*
948  * Store from GPR register to memory.
949  */
950 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
951                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
952                              bool iss_valid,
953                              unsigned int iss_srt,
954                              bool iss_sf, bool iss_ar)
955 {
956     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
957 
958     if (iss_valid) {
959         uint32_t syn;
960 
961         syn = syn_data_abort_with_iss(0,
962                                       (memop & MO_SIZE),
963                                       false,
964                                       iss_srt,
965                                       iss_sf,
966                                       iss_ar,
967                                       0, 0, 0, 0, 0, false);
968         disas_set_insn_syndrome(s, syn);
969     }
970 }
971 
972 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
973                       TCGv_i64 tcg_addr, MemOp memop,
974                       bool iss_valid,
975                       unsigned int iss_srt,
976                       bool iss_sf, bool iss_ar)
977 {
978     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
979                      iss_valid, iss_srt, iss_sf, iss_ar);
980 }
981 
982 /*
983  * Load from memory to GPR register
984  */
985 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
986                              MemOp memop, bool extend, int memidx,
987                              bool iss_valid, unsigned int iss_srt,
988                              bool iss_sf, bool iss_ar)
989 {
990     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
991 
992     if (extend && (memop & MO_SIGN)) {
993         g_assert((memop & MO_SIZE) <= MO_32);
994         tcg_gen_ext32u_i64(dest, dest);
995     }
996 
997     if (iss_valid) {
998         uint32_t syn;
999 
1000         syn = syn_data_abort_with_iss(0,
1001                                       (memop & MO_SIZE),
1002                                       (memop & MO_SIGN) != 0,
1003                                       iss_srt,
1004                                       iss_sf,
1005                                       iss_ar,
1006                                       0, 0, 0, 0, 0, false);
1007         disas_set_insn_syndrome(s, syn);
1008     }
1009 }
1010 
1011 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1012                       MemOp memop, bool extend,
1013                       bool iss_valid, unsigned int iss_srt,
1014                       bool iss_sf, bool iss_ar)
1015 {
1016     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1017                      iss_valid, iss_srt, iss_sf, iss_ar);
1018 }
1019 
1020 /*
1021  * Store from FP register to memory
1022  */
1023 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1024 {
1025     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1026     TCGv_i64 tmplo = tcg_temp_new_i64();
1027 
1028     tcg_gen_ld_i64(tmplo, tcg_env, fp_reg_offset(s, srcidx, MO_64));
1029 
1030     if ((mop & MO_SIZE) < MO_128) {
1031         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1032     } else {
1033         TCGv_i64 tmphi = tcg_temp_new_i64();
1034         TCGv_i128 t16 = tcg_temp_new_i128();
1035 
1036         tcg_gen_ld_i64(tmphi, tcg_env, fp_reg_hi_offset(s, srcidx));
1037         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1038 
1039         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1040     }
1041 }
1042 
1043 /*
1044  * Load from memory to FP register
1045  */
1046 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1047 {
1048     /* This always zero-extends and writes to a full 128 bit wide vector */
1049     TCGv_i64 tmplo = tcg_temp_new_i64();
1050     TCGv_i64 tmphi = NULL;
1051 
1052     if ((mop & MO_SIZE) < MO_128) {
1053         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1054     } else {
1055         TCGv_i128 t16 = tcg_temp_new_i128();
1056 
1057         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1058 
1059         tmphi = tcg_temp_new_i64();
1060         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1061     }
1062 
1063     tcg_gen_st_i64(tmplo, tcg_env, fp_reg_offset(s, destidx, MO_64));
1064 
1065     if (tmphi) {
1066         tcg_gen_st_i64(tmphi, tcg_env, fp_reg_hi_offset(s, destidx));
1067     }
1068     clear_vec_high(s, tmphi != NULL, destidx);
1069 }
1070 
1071 /*
1072  * Vector load/store helpers.
1073  *
1074  * The principal difference between this and a FP load is that we don't
1075  * zero extend as we are filling a partial chunk of the vector register.
1076  * These functions don't support 128 bit loads/stores, which would be
1077  * normal load/store operations.
1078  *
1079  * The _i32 versions are useful when operating on 32 bit quantities
1080  * (eg for floating point single or using Neon helper functions).
1081  */
1082 
1083 /* Get value of an element within a vector register */
1084 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1085                              int element, MemOp memop)
1086 {
1087     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1088     switch ((unsigned)memop) {
1089     case MO_8:
1090         tcg_gen_ld8u_i64(tcg_dest, tcg_env, vect_off);
1091         break;
1092     case MO_16:
1093         tcg_gen_ld16u_i64(tcg_dest, tcg_env, vect_off);
1094         break;
1095     case MO_32:
1096         tcg_gen_ld32u_i64(tcg_dest, tcg_env, vect_off);
1097         break;
1098     case MO_8|MO_SIGN:
1099         tcg_gen_ld8s_i64(tcg_dest, tcg_env, vect_off);
1100         break;
1101     case MO_16|MO_SIGN:
1102         tcg_gen_ld16s_i64(tcg_dest, tcg_env, vect_off);
1103         break;
1104     case MO_32|MO_SIGN:
1105         tcg_gen_ld32s_i64(tcg_dest, tcg_env, vect_off);
1106         break;
1107     case MO_64:
1108     case MO_64|MO_SIGN:
1109         tcg_gen_ld_i64(tcg_dest, tcg_env, vect_off);
1110         break;
1111     default:
1112         g_assert_not_reached();
1113     }
1114 }
1115 
1116 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1117                                  int element, MemOp memop)
1118 {
1119     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1120     switch (memop) {
1121     case MO_8:
1122         tcg_gen_ld8u_i32(tcg_dest, tcg_env, vect_off);
1123         break;
1124     case MO_16:
1125         tcg_gen_ld16u_i32(tcg_dest, tcg_env, vect_off);
1126         break;
1127     case MO_8|MO_SIGN:
1128         tcg_gen_ld8s_i32(tcg_dest, tcg_env, vect_off);
1129         break;
1130     case MO_16|MO_SIGN:
1131         tcg_gen_ld16s_i32(tcg_dest, tcg_env, vect_off);
1132         break;
1133     case MO_32:
1134     case MO_32|MO_SIGN:
1135         tcg_gen_ld_i32(tcg_dest, tcg_env, vect_off);
1136         break;
1137     default:
1138         g_assert_not_reached();
1139     }
1140 }
1141 
1142 /* Set value of an element within a vector register */
1143 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1144                               int element, MemOp memop)
1145 {
1146     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1147     switch (memop) {
1148     case MO_8:
1149         tcg_gen_st8_i64(tcg_src, tcg_env, vect_off);
1150         break;
1151     case MO_16:
1152         tcg_gen_st16_i64(tcg_src, tcg_env, vect_off);
1153         break;
1154     case MO_32:
1155         tcg_gen_st32_i64(tcg_src, tcg_env, vect_off);
1156         break;
1157     case MO_64:
1158         tcg_gen_st_i64(tcg_src, tcg_env, vect_off);
1159         break;
1160     default:
1161         g_assert_not_reached();
1162     }
1163 }
1164 
1165 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1166                                   int destidx, int element, MemOp memop)
1167 {
1168     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1169     switch (memop) {
1170     case MO_8:
1171         tcg_gen_st8_i32(tcg_src, tcg_env, vect_off);
1172         break;
1173     case MO_16:
1174         tcg_gen_st16_i32(tcg_src, tcg_env, vect_off);
1175         break;
1176     case MO_32:
1177         tcg_gen_st_i32(tcg_src, tcg_env, vect_off);
1178         break;
1179     default:
1180         g_assert_not_reached();
1181     }
1182 }
1183 
1184 /* Store from vector register to memory */
1185 static void do_vec_st(DisasContext *s, int srcidx, int element,
1186                       TCGv_i64 tcg_addr, MemOp mop)
1187 {
1188     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1189 
1190     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1191     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1192 }
1193 
1194 /* Load from memory to vector register */
1195 static void do_vec_ld(DisasContext *s, int destidx, int element,
1196                       TCGv_i64 tcg_addr, MemOp mop)
1197 {
1198     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1199 
1200     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1201     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1202 }
1203 
1204 /* Check that FP/Neon access is enabled. If it is, return
1205  * true. If not, emit code to generate an appropriate exception,
1206  * and return false; the caller should not emit any code for
1207  * the instruction. Note that this check must happen after all
1208  * unallocated-encoding checks (otherwise the syndrome information
1209  * for the resulting exception will be incorrect).
1210  */
1211 static bool fp_access_check_only(DisasContext *s)
1212 {
1213     if (s->fp_excp_el) {
1214         assert(!s->fp_access_checked);
1215         s->fp_access_checked = true;
1216 
1217         gen_exception_insn_el(s, 0, EXCP_UDEF,
1218                               syn_fp_access_trap(1, 0xe, false, 0),
1219                               s->fp_excp_el);
1220         return false;
1221     }
1222     s->fp_access_checked = true;
1223     return true;
1224 }
1225 
1226 static bool fp_access_check(DisasContext *s)
1227 {
1228     if (!fp_access_check_only(s)) {
1229         return false;
1230     }
1231     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1232         gen_exception_insn(s, 0, EXCP_UDEF,
1233                            syn_smetrap(SME_ET_Streaming, false));
1234         return false;
1235     }
1236     return true;
1237 }
1238 
1239 /*
1240  * Check that SVE access is enabled.  If it is, return true.
1241  * If not, emit code to generate an appropriate exception and return false.
1242  * This function corresponds to CheckSVEEnabled().
1243  */
1244 bool sve_access_check(DisasContext *s)
1245 {
1246     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1247         assert(dc_isar_feature(aa64_sme, s));
1248         if (!sme_sm_enabled_check(s)) {
1249             goto fail_exit;
1250         }
1251     } else if (s->sve_excp_el) {
1252         gen_exception_insn_el(s, 0, EXCP_UDEF,
1253                               syn_sve_access_trap(), s->sve_excp_el);
1254         goto fail_exit;
1255     }
1256     s->sve_access_checked = true;
1257     return fp_access_check(s);
1258 
1259  fail_exit:
1260     /* Assert that we only raise one exception per instruction. */
1261     assert(!s->sve_access_checked);
1262     s->sve_access_checked = true;
1263     return false;
1264 }
1265 
1266 /*
1267  * Check that SME access is enabled, raise an exception if not.
1268  * Note that this function corresponds to CheckSMEAccess and is
1269  * only used directly for cpregs.
1270  */
1271 static bool sme_access_check(DisasContext *s)
1272 {
1273     if (s->sme_excp_el) {
1274         gen_exception_insn_el(s, 0, EXCP_UDEF,
1275                               syn_smetrap(SME_ET_AccessTrap, false),
1276                               s->sme_excp_el);
1277         return false;
1278     }
1279     return true;
1280 }
1281 
1282 /* This function corresponds to CheckSMEEnabled. */
1283 bool sme_enabled_check(DisasContext *s)
1284 {
1285     /*
1286      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1287      * to be zero when fp_excp_el has priority.  This is because we need
1288      * sme_excp_el by itself for cpregs access checks.
1289      */
1290     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1291         s->fp_access_checked = true;
1292         return sme_access_check(s);
1293     }
1294     return fp_access_check_only(s);
1295 }
1296 
1297 /* Common subroutine for CheckSMEAnd*Enabled. */
1298 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1299 {
1300     if (!sme_enabled_check(s)) {
1301         return false;
1302     }
1303     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1304         gen_exception_insn(s, 0, EXCP_UDEF,
1305                            syn_smetrap(SME_ET_NotStreaming, false));
1306         return false;
1307     }
1308     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1309         gen_exception_insn(s, 0, EXCP_UDEF,
1310                            syn_smetrap(SME_ET_InactiveZA, false));
1311         return false;
1312     }
1313     return true;
1314 }
1315 
1316 /*
1317  * Expanders for AdvSIMD translation functions.
1318  */
1319 
1320 static bool do_gvec_op2_ool(DisasContext *s, arg_qrr_e *a, int data,
1321                             gen_helper_gvec_2 *fn)
1322 {
1323     if (!a->q && a->esz == MO_64) {
1324         return false;
1325     }
1326     if (fp_access_check(s)) {
1327         gen_gvec_op2_ool(s, a->q, a->rd, a->rn, data, fn);
1328     }
1329     return true;
1330 }
1331 
1332 static bool do_gvec_op3_ool(DisasContext *s, arg_qrrr_e *a, int data,
1333                             gen_helper_gvec_3 *fn)
1334 {
1335     if (!a->q && a->esz == MO_64) {
1336         return false;
1337     }
1338     if (fp_access_check(s)) {
1339         gen_gvec_op3_ool(s, a->q, a->rd, a->rn, a->rm, data, fn);
1340     }
1341     return true;
1342 }
1343 
1344 static bool do_gvec_fn3(DisasContext *s, arg_qrrr_e *a, GVecGen3Fn *fn)
1345 {
1346     if (!a->q && a->esz == MO_64) {
1347         return false;
1348     }
1349     if (fp_access_check(s)) {
1350         gen_gvec_fn3(s, a->q, a->rd, a->rn, a->rm, fn, a->esz);
1351     }
1352     return true;
1353 }
1354 
1355 static bool do_gvec_fn4(DisasContext *s, arg_qrrrr_e *a, GVecGen4Fn *fn)
1356 {
1357     if (!a->q && a->esz == MO_64) {
1358         return false;
1359     }
1360     if (fp_access_check(s)) {
1361         gen_gvec_fn4(s, a->q, a->rd, a->rn, a->rm, a->ra, fn, a->esz);
1362     }
1363     return true;
1364 }
1365 
1366 /*
1367  * This utility function is for doing register extension with an
1368  * optional shift. You will likely want to pass a temporary for the
1369  * destination register. See DecodeRegExtend() in the ARM ARM.
1370  */
1371 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1372                               int option, unsigned int shift)
1373 {
1374     int extsize = extract32(option, 0, 2);
1375     bool is_signed = extract32(option, 2, 1);
1376 
1377     tcg_gen_ext_i64(tcg_out, tcg_in, extsize | (is_signed ? MO_SIGN : 0));
1378     tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1379 }
1380 
1381 static inline void gen_check_sp_alignment(DisasContext *s)
1382 {
1383     /* The AArch64 architecture mandates that (if enabled via PSTATE
1384      * or SCTLR bits) there is a check that SP is 16-aligned on every
1385      * SP-relative load or store (with an exception generated if it is not).
1386      * In line with general QEMU practice regarding misaligned accesses,
1387      * we omit these checks for the sake of guest program performance.
1388      * This function is provided as a hook so we can more easily add these
1389      * checks in future (possibly as a "favour catching guest program bugs
1390      * over speed" user selectable option).
1391      */
1392 }
1393 
1394 /*
1395  * This provides a simple table based table lookup decoder. It is
1396  * intended to be used when the relevant bits for decode are too
1397  * awkwardly placed and switch/if based logic would be confusing and
1398  * deeply nested. Since it's a linear search through the table, tables
1399  * should be kept small.
1400  *
1401  * It returns the first handler where insn & mask == pattern, or
1402  * NULL if there is no match.
1403  * The table is terminated by an empty mask (i.e. 0)
1404  */
1405 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1406                                                uint32_t insn)
1407 {
1408     const AArch64DecodeTable *tptr = table;
1409 
1410     while (tptr->mask) {
1411         if ((insn & tptr->mask) == tptr->pattern) {
1412             return tptr->disas_fn;
1413         }
1414         tptr++;
1415     }
1416     return NULL;
1417 }
1418 
1419 /*
1420  * The instruction disassembly implemented here matches
1421  * the instruction encoding classifications in chapter C4
1422  * of the ARM Architecture Reference Manual (DDI0487B_a);
1423  * classification names and decode diagrams here should generally
1424  * match up with those in the manual.
1425  */
1426 
1427 static bool trans_B(DisasContext *s, arg_i *a)
1428 {
1429     reset_btype(s);
1430     gen_goto_tb(s, 0, a->imm);
1431     return true;
1432 }
1433 
1434 static bool trans_BL(DisasContext *s, arg_i *a)
1435 {
1436     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1437     reset_btype(s);
1438     gen_goto_tb(s, 0, a->imm);
1439     return true;
1440 }
1441 
1442 
1443 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1444 {
1445     DisasLabel match;
1446     TCGv_i64 tcg_cmp;
1447 
1448     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1449     reset_btype(s);
1450 
1451     match = gen_disas_label(s);
1452     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1453                         tcg_cmp, 0, match.label);
1454     gen_goto_tb(s, 0, 4);
1455     set_disas_label(s, match);
1456     gen_goto_tb(s, 1, a->imm);
1457     return true;
1458 }
1459 
1460 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1461 {
1462     DisasLabel match;
1463     TCGv_i64 tcg_cmp;
1464 
1465     tcg_cmp = tcg_temp_new_i64();
1466     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1467 
1468     reset_btype(s);
1469 
1470     match = gen_disas_label(s);
1471     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1472                         tcg_cmp, 0, match.label);
1473     gen_goto_tb(s, 0, 4);
1474     set_disas_label(s, match);
1475     gen_goto_tb(s, 1, a->imm);
1476     return true;
1477 }
1478 
1479 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1480 {
1481     /* BC.cond is only present with FEAT_HBC */
1482     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
1483         return false;
1484     }
1485     reset_btype(s);
1486     if (a->cond < 0x0e) {
1487         /* genuinely conditional branches */
1488         DisasLabel match = gen_disas_label(s);
1489         arm_gen_test_cc(a->cond, match.label);
1490         gen_goto_tb(s, 0, 4);
1491         set_disas_label(s, match);
1492         gen_goto_tb(s, 1, a->imm);
1493     } else {
1494         /* 0xe and 0xf are both "always" conditions */
1495         gen_goto_tb(s, 0, a->imm);
1496     }
1497     return true;
1498 }
1499 
1500 static void set_btype_for_br(DisasContext *s, int rn)
1501 {
1502     if (dc_isar_feature(aa64_bti, s)) {
1503         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1504         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1505     }
1506 }
1507 
1508 static void set_btype_for_blr(DisasContext *s)
1509 {
1510     if (dc_isar_feature(aa64_bti, s)) {
1511         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1512         set_btype(s, 2);
1513     }
1514 }
1515 
1516 static bool trans_BR(DisasContext *s, arg_r *a)
1517 {
1518     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1519     set_btype_for_br(s, a->rn);
1520     s->base.is_jmp = DISAS_JUMP;
1521     return true;
1522 }
1523 
1524 static bool trans_BLR(DisasContext *s, arg_r *a)
1525 {
1526     TCGv_i64 dst = cpu_reg(s, a->rn);
1527     TCGv_i64 lr = cpu_reg(s, 30);
1528     if (dst == lr) {
1529         TCGv_i64 tmp = tcg_temp_new_i64();
1530         tcg_gen_mov_i64(tmp, dst);
1531         dst = tmp;
1532     }
1533     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1534     gen_a64_set_pc(s, dst);
1535     set_btype_for_blr(s);
1536     s->base.is_jmp = DISAS_JUMP;
1537     return true;
1538 }
1539 
1540 static bool trans_RET(DisasContext *s, arg_r *a)
1541 {
1542     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1543     s->base.is_jmp = DISAS_JUMP;
1544     return true;
1545 }
1546 
1547 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1548                                    TCGv_i64 modifier, bool use_key_a)
1549 {
1550     TCGv_i64 truedst;
1551     /*
1552      * Return the branch target for a BRAA/RETA/etc, which is either
1553      * just the destination dst, or that value with the pauth check
1554      * done and the code removed from the high bits.
1555      */
1556     if (!s->pauth_active) {
1557         return dst;
1558     }
1559 
1560     truedst = tcg_temp_new_i64();
1561     if (use_key_a) {
1562         gen_helper_autia_combined(truedst, tcg_env, dst, modifier);
1563     } else {
1564         gen_helper_autib_combined(truedst, tcg_env, dst, modifier);
1565     }
1566     return truedst;
1567 }
1568 
1569 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1570 {
1571     TCGv_i64 dst;
1572 
1573     if (!dc_isar_feature(aa64_pauth, s)) {
1574         return false;
1575     }
1576 
1577     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1578     gen_a64_set_pc(s, dst);
1579     set_btype_for_br(s, a->rn);
1580     s->base.is_jmp = DISAS_JUMP;
1581     return true;
1582 }
1583 
1584 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1585 {
1586     TCGv_i64 dst, lr;
1587 
1588     if (!dc_isar_feature(aa64_pauth, s)) {
1589         return false;
1590     }
1591 
1592     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1593     lr = cpu_reg(s, 30);
1594     if (dst == lr) {
1595         TCGv_i64 tmp = tcg_temp_new_i64();
1596         tcg_gen_mov_i64(tmp, dst);
1597         dst = tmp;
1598     }
1599     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1600     gen_a64_set_pc(s, dst);
1601     set_btype_for_blr(s);
1602     s->base.is_jmp = DISAS_JUMP;
1603     return true;
1604 }
1605 
1606 static bool trans_RETA(DisasContext *s, arg_reta *a)
1607 {
1608     TCGv_i64 dst;
1609 
1610     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1611     gen_a64_set_pc(s, dst);
1612     s->base.is_jmp = DISAS_JUMP;
1613     return true;
1614 }
1615 
1616 static bool trans_BRA(DisasContext *s, arg_bra *a)
1617 {
1618     TCGv_i64 dst;
1619 
1620     if (!dc_isar_feature(aa64_pauth, s)) {
1621         return false;
1622     }
1623     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1624     gen_a64_set_pc(s, dst);
1625     set_btype_for_br(s, a->rn);
1626     s->base.is_jmp = DISAS_JUMP;
1627     return true;
1628 }
1629 
1630 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1631 {
1632     TCGv_i64 dst, lr;
1633 
1634     if (!dc_isar_feature(aa64_pauth, s)) {
1635         return false;
1636     }
1637     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1638     lr = cpu_reg(s, 30);
1639     if (dst == lr) {
1640         TCGv_i64 tmp = tcg_temp_new_i64();
1641         tcg_gen_mov_i64(tmp, dst);
1642         dst = tmp;
1643     }
1644     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1645     gen_a64_set_pc(s, dst);
1646     set_btype_for_blr(s);
1647     s->base.is_jmp = DISAS_JUMP;
1648     return true;
1649 }
1650 
1651 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1652 {
1653     TCGv_i64 dst;
1654 
1655     if (s->current_el == 0) {
1656         return false;
1657     }
1658     if (s->trap_eret) {
1659         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(0), 2);
1660         return true;
1661     }
1662     dst = tcg_temp_new_i64();
1663     tcg_gen_ld_i64(dst, tcg_env,
1664                    offsetof(CPUARMState, elr_el[s->current_el]));
1665 
1666     translator_io_start(&s->base);
1667 
1668     gen_helper_exception_return(tcg_env, dst);
1669     /* Must exit loop to check un-masked IRQs */
1670     s->base.is_jmp = DISAS_EXIT;
1671     return true;
1672 }
1673 
1674 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1675 {
1676     TCGv_i64 dst;
1677 
1678     if (!dc_isar_feature(aa64_pauth, s)) {
1679         return false;
1680     }
1681     if (s->current_el == 0) {
1682         return false;
1683     }
1684     /* The FGT trap takes precedence over an auth trap. */
1685     if (s->trap_eret) {
1686         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(a->m ? 3 : 2), 2);
1687         return true;
1688     }
1689     dst = tcg_temp_new_i64();
1690     tcg_gen_ld_i64(dst, tcg_env,
1691                    offsetof(CPUARMState, elr_el[s->current_el]));
1692 
1693     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1694 
1695     translator_io_start(&s->base);
1696 
1697     gen_helper_exception_return(tcg_env, dst);
1698     /* Must exit loop to check un-masked IRQs */
1699     s->base.is_jmp = DISAS_EXIT;
1700     return true;
1701 }
1702 
1703 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1704 {
1705     return true;
1706 }
1707 
1708 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1709 {
1710     /*
1711      * When running in MTTCG we don't generate jumps to the yield and
1712      * WFE helpers as it won't affect the scheduling of other vCPUs.
1713      * If we wanted to more completely model WFE/SEV so we don't busy
1714      * spin unnecessarily we would need to do something more involved.
1715      */
1716     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1717         s->base.is_jmp = DISAS_YIELD;
1718     }
1719     return true;
1720 }
1721 
1722 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1723 {
1724     s->base.is_jmp = DISAS_WFI;
1725     return true;
1726 }
1727 
1728 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1729 {
1730     /*
1731      * When running in MTTCG we don't generate jumps to the yield and
1732      * WFE helpers as it won't affect the scheduling of other vCPUs.
1733      * If we wanted to more completely model WFE/SEV so we don't busy
1734      * spin unnecessarily we would need to do something more involved.
1735      */
1736     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1737         s->base.is_jmp = DISAS_WFE;
1738     }
1739     return true;
1740 }
1741 
1742 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1743 {
1744     if (s->pauth_active) {
1745         gen_helper_xpaci(cpu_X[30], tcg_env, cpu_X[30]);
1746     }
1747     return true;
1748 }
1749 
1750 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1751 {
1752     if (s->pauth_active) {
1753         gen_helper_pacia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1754     }
1755     return true;
1756 }
1757 
1758 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1759 {
1760     if (s->pauth_active) {
1761         gen_helper_pacib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1762     }
1763     return true;
1764 }
1765 
1766 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1767 {
1768     if (s->pauth_active) {
1769         gen_helper_autia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1770     }
1771     return true;
1772 }
1773 
1774 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1775 {
1776     if (s->pauth_active) {
1777         gen_helper_autib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1778     }
1779     return true;
1780 }
1781 
1782 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1783 {
1784     /* Without RAS, we must implement this as NOP. */
1785     if (dc_isar_feature(aa64_ras, s)) {
1786         /*
1787          * QEMU does not have a source of physical SErrors,
1788          * so we are only concerned with virtual SErrors.
1789          * The pseudocode in the ARM for this case is
1790          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1791          *      AArch64.vESBOperation();
1792          * Most of the condition can be evaluated at translation time.
1793          * Test for EL2 present, and defer test for SEL2 to runtime.
1794          */
1795         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1796             gen_helper_vesb(tcg_env);
1797         }
1798     }
1799     return true;
1800 }
1801 
1802 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1803 {
1804     if (s->pauth_active) {
1805         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1806     }
1807     return true;
1808 }
1809 
1810 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1811 {
1812     if (s->pauth_active) {
1813         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1814     }
1815     return true;
1816 }
1817 
1818 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1819 {
1820     if (s->pauth_active) {
1821         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1822     }
1823     return true;
1824 }
1825 
1826 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1827 {
1828     if (s->pauth_active) {
1829         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1830     }
1831     return true;
1832 }
1833 
1834 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1835 {
1836     if (s->pauth_active) {
1837         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1838     }
1839     return true;
1840 }
1841 
1842 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1843 {
1844     if (s->pauth_active) {
1845         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1846     }
1847     return true;
1848 }
1849 
1850 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1851 {
1852     if (s->pauth_active) {
1853         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1854     }
1855     return true;
1856 }
1857 
1858 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1859 {
1860     if (s->pauth_active) {
1861         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1862     }
1863     return true;
1864 }
1865 
1866 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1867 {
1868     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1869     return true;
1870 }
1871 
1872 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1873 {
1874     /* We handle DSB and DMB the same way */
1875     TCGBar bar;
1876 
1877     switch (a->types) {
1878     case 1: /* MBReqTypes_Reads */
1879         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1880         break;
1881     case 2: /* MBReqTypes_Writes */
1882         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1883         break;
1884     default: /* MBReqTypes_All */
1885         bar = TCG_BAR_SC | TCG_MO_ALL;
1886         break;
1887     }
1888     tcg_gen_mb(bar);
1889     return true;
1890 }
1891 
1892 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1893 {
1894     /*
1895      * We need to break the TB after this insn to execute
1896      * self-modifying code correctly and also to take
1897      * any pending interrupts immediately.
1898      */
1899     reset_btype(s);
1900     gen_goto_tb(s, 0, 4);
1901     return true;
1902 }
1903 
1904 static bool trans_SB(DisasContext *s, arg_SB *a)
1905 {
1906     if (!dc_isar_feature(aa64_sb, s)) {
1907         return false;
1908     }
1909     /*
1910      * TODO: There is no speculation barrier opcode for TCG;
1911      * MB and end the TB instead.
1912      */
1913     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1914     gen_goto_tb(s, 0, 4);
1915     return true;
1916 }
1917 
1918 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1919 {
1920     if (!dc_isar_feature(aa64_condm_4, s)) {
1921         return false;
1922     }
1923     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1924     return true;
1925 }
1926 
1927 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1928 {
1929     TCGv_i32 z;
1930 
1931     if (!dc_isar_feature(aa64_condm_5, s)) {
1932         return false;
1933     }
1934 
1935     z = tcg_temp_new_i32();
1936 
1937     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1938 
1939     /*
1940      * (!C & !Z) << 31
1941      * (!(C | Z)) << 31
1942      * ~((C | Z) << 31)
1943      * ~-(C | Z)
1944      * (C | Z) - 1
1945      */
1946     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1947     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1948 
1949     /* !(Z & C) */
1950     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1951     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1952 
1953     /* (!C & Z) << 31 -> -(Z & ~C) */
1954     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1955     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1956 
1957     /* C | Z */
1958     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1959 
1960     return true;
1961 }
1962 
1963 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1964 {
1965     if (!dc_isar_feature(aa64_condm_5, s)) {
1966         return false;
1967     }
1968 
1969     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1970     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1971 
1972     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1973     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1974 
1975     tcg_gen_movi_i32(cpu_NF, 0);
1976     tcg_gen_movi_i32(cpu_VF, 0);
1977 
1978     return true;
1979 }
1980 
1981 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1982 {
1983     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1984         return false;
1985     }
1986     if (a->imm & 1) {
1987         set_pstate_bits(PSTATE_UAO);
1988     } else {
1989         clear_pstate_bits(PSTATE_UAO);
1990     }
1991     gen_rebuild_hflags(s);
1992     s->base.is_jmp = DISAS_TOO_MANY;
1993     return true;
1994 }
1995 
1996 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1997 {
1998     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1999         return false;
2000     }
2001     if (a->imm & 1) {
2002         set_pstate_bits(PSTATE_PAN);
2003     } else {
2004         clear_pstate_bits(PSTATE_PAN);
2005     }
2006     gen_rebuild_hflags(s);
2007     s->base.is_jmp = DISAS_TOO_MANY;
2008     return true;
2009 }
2010 
2011 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
2012 {
2013     if (s->current_el == 0) {
2014         return false;
2015     }
2016     gen_helper_msr_i_spsel(tcg_env, tcg_constant_i32(a->imm & PSTATE_SP));
2017     s->base.is_jmp = DISAS_TOO_MANY;
2018     return true;
2019 }
2020 
2021 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
2022 {
2023     if (!dc_isar_feature(aa64_ssbs, s)) {
2024         return false;
2025     }
2026     if (a->imm & 1) {
2027         set_pstate_bits(PSTATE_SSBS);
2028     } else {
2029         clear_pstate_bits(PSTATE_SSBS);
2030     }
2031     /* Don't need to rebuild hflags since SSBS is a nop */
2032     s->base.is_jmp = DISAS_TOO_MANY;
2033     return true;
2034 }
2035 
2036 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
2037 {
2038     if (!dc_isar_feature(aa64_dit, s)) {
2039         return false;
2040     }
2041     if (a->imm & 1) {
2042         set_pstate_bits(PSTATE_DIT);
2043     } else {
2044         clear_pstate_bits(PSTATE_DIT);
2045     }
2046     /* There's no need to rebuild hflags because DIT is a nop */
2047     s->base.is_jmp = DISAS_TOO_MANY;
2048     return true;
2049 }
2050 
2051 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2052 {
2053     if (dc_isar_feature(aa64_mte, s)) {
2054         /* Full MTE is enabled -- set the TCO bit as directed. */
2055         if (a->imm & 1) {
2056             set_pstate_bits(PSTATE_TCO);
2057         } else {
2058             clear_pstate_bits(PSTATE_TCO);
2059         }
2060         gen_rebuild_hflags(s);
2061         /* Many factors, including TCO, go into MTE_ACTIVE. */
2062         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2063         return true;
2064     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2065         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2066         return true;
2067     } else {
2068         /* Insn not present */
2069         return false;
2070     }
2071 }
2072 
2073 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2074 {
2075     gen_helper_msr_i_daifset(tcg_env, tcg_constant_i32(a->imm));
2076     s->base.is_jmp = DISAS_TOO_MANY;
2077     return true;
2078 }
2079 
2080 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2081 {
2082     gen_helper_msr_i_daifclear(tcg_env, tcg_constant_i32(a->imm));
2083     /* Exit the cpu loop to re-evaluate pending IRQs. */
2084     s->base.is_jmp = DISAS_UPDATE_EXIT;
2085     return true;
2086 }
2087 
2088 static bool trans_MSR_i_ALLINT(DisasContext *s, arg_i *a)
2089 {
2090     if (!dc_isar_feature(aa64_nmi, s) || s->current_el == 0) {
2091         return false;
2092     }
2093 
2094     if (a->imm == 0) {
2095         clear_pstate_bits(PSTATE_ALLINT);
2096     } else if (s->current_el > 1) {
2097         set_pstate_bits(PSTATE_ALLINT);
2098     } else {
2099         gen_helper_msr_set_allint_el1(tcg_env);
2100     }
2101 
2102     /* Exit the cpu loop to re-evaluate pending IRQs. */
2103     s->base.is_jmp = DISAS_UPDATE_EXIT;
2104     return true;
2105 }
2106 
2107 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2108 {
2109     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2110         return false;
2111     }
2112     if (sme_access_check(s)) {
2113         int old = s->pstate_sm | (s->pstate_za << 1);
2114         int new = a->imm * 3;
2115 
2116         if ((old ^ new) & a->mask) {
2117             /* At least one bit changes. */
2118             gen_helper_set_svcr(tcg_env, tcg_constant_i32(new),
2119                                 tcg_constant_i32(a->mask));
2120             s->base.is_jmp = DISAS_TOO_MANY;
2121         }
2122     }
2123     return true;
2124 }
2125 
2126 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2127 {
2128     TCGv_i32 tmp = tcg_temp_new_i32();
2129     TCGv_i32 nzcv = tcg_temp_new_i32();
2130 
2131     /* build bit 31, N */
2132     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2133     /* build bit 30, Z */
2134     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2135     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2136     /* build bit 29, C */
2137     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2138     /* build bit 28, V */
2139     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2140     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2141     /* generate result */
2142     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2143 }
2144 
2145 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2146 {
2147     TCGv_i32 nzcv = tcg_temp_new_i32();
2148 
2149     /* take NZCV from R[t] */
2150     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2151 
2152     /* bit 31, N */
2153     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2154     /* bit 30, Z */
2155     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2156     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2157     /* bit 29, C */
2158     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2159     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2160     /* bit 28, V */
2161     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2162     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2163 }
2164 
2165 static void gen_sysreg_undef(DisasContext *s, bool isread,
2166                              uint8_t op0, uint8_t op1, uint8_t op2,
2167                              uint8_t crn, uint8_t crm, uint8_t rt)
2168 {
2169     /*
2170      * Generate code to emit an UNDEF with correct syndrome
2171      * information for a failed system register access.
2172      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2173      * but if FEAT_IDST is implemented then read accesses to registers
2174      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2175      * syndrome.
2176      */
2177     uint32_t syndrome;
2178 
2179     if (isread && dc_isar_feature(aa64_ids, s) &&
2180         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2181         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2182     } else {
2183         syndrome = syn_uncategorized();
2184     }
2185     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2186 }
2187 
2188 /* MRS - move from system register
2189  * MSR (register) - move to system register
2190  * SYS
2191  * SYSL
2192  * These are all essentially the same insn in 'read' and 'write'
2193  * versions, with varying op0 fields.
2194  */
2195 static void handle_sys(DisasContext *s, bool isread,
2196                        unsigned int op0, unsigned int op1, unsigned int op2,
2197                        unsigned int crn, unsigned int crm, unsigned int rt)
2198 {
2199     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2200                                       crn, crm, op0, op1, op2);
2201     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2202     bool need_exit_tb = false;
2203     bool nv_trap_to_el2 = false;
2204     bool nv_redirect_reg = false;
2205     bool skip_fp_access_checks = false;
2206     bool nv2_mem_redirect = false;
2207     TCGv_ptr tcg_ri = NULL;
2208     TCGv_i64 tcg_rt;
2209     uint32_t syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2210 
2211     if (crn == 11 || crn == 15) {
2212         /*
2213          * Check for TIDCP trap, which must take precedence over
2214          * the UNDEF for "no such register" etc.
2215          */
2216         switch (s->current_el) {
2217         case 0:
2218             if (dc_isar_feature(aa64_tidcp1, s)) {
2219                 gen_helper_tidcp_el0(tcg_env, tcg_constant_i32(syndrome));
2220             }
2221             break;
2222         case 1:
2223             gen_helper_tidcp_el1(tcg_env, tcg_constant_i32(syndrome));
2224             break;
2225         }
2226     }
2227 
2228     if (!ri) {
2229         /* Unknown register; this might be a guest error or a QEMU
2230          * unimplemented feature.
2231          */
2232         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2233                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2234                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2235         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2236         return;
2237     }
2238 
2239     if (s->nv2 && ri->nv2_redirect_offset) {
2240         /*
2241          * Some registers always redirect to memory; some only do so if
2242          * HCR_EL2.NV1 is 0, and some only if NV1 is 1 (these come in
2243          * pairs which share an offset; see the table in R_CSRPQ).
2244          */
2245         if (ri->nv2_redirect_offset & NV2_REDIR_NV1) {
2246             nv2_mem_redirect = s->nv1;
2247         } else if (ri->nv2_redirect_offset & NV2_REDIR_NO_NV1) {
2248             nv2_mem_redirect = !s->nv1;
2249         } else {
2250             nv2_mem_redirect = true;
2251         }
2252     }
2253 
2254     /* Check access permissions */
2255     if (!cp_access_ok(s->current_el, ri, isread)) {
2256         /*
2257          * FEAT_NV/NV2 handling does not do the usual FP access checks
2258          * for registers only accessible at EL2 (though it *does* do them
2259          * for registers accessible at EL1).
2260          */
2261         skip_fp_access_checks = true;
2262         if (s->nv2 && (ri->type & ARM_CP_NV2_REDIRECT)) {
2263             /*
2264              * This is one of the few EL2 registers which should redirect
2265              * to the equivalent EL1 register. We do that after running
2266              * the EL2 register's accessfn.
2267              */
2268             nv_redirect_reg = true;
2269             assert(!nv2_mem_redirect);
2270         } else if (nv2_mem_redirect) {
2271             /*
2272              * NV2 redirect-to-memory takes precedence over trap to EL2 or
2273              * UNDEF to EL1.
2274              */
2275         } else if (s->nv && arm_cpreg_traps_in_nv(ri)) {
2276             /*
2277              * This register / instruction exists and is an EL2 register, so
2278              * we must trap to EL2 if accessed in nested virtualization EL1
2279              * instead of UNDEFing. We'll do that after the usual access checks.
2280              * (This makes a difference only for a couple of registers like
2281              * VSTTBR_EL2 where the "UNDEF if NonSecure" should take priority
2282              * over the trap-to-EL2. Most trapped-by-FEAT_NV registers have
2283              * an accessfn which does nothing when called from EL1, because
2284              * the trap-to-EL3 controls which would apply to that register
2285              * at EL2 don't take priority over the FEAT_NV trap-to-EL2.)
2286              */
2287             nv_trap_to_el2 = true;
2288         } else {
2289             gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2290             return;
2291         }
2292     }
2293 
2294     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2295         /* Emit code to perform further access permissions checks at
2296          * runtime; this may result in an exception.
2297          */
2298         gen_a64_update_pc(s, 0);
2299         tcg_ri = tcg_temp_new_ptr();
2300         gen_helper_access_check_cp_reg(tcg_ri, tcg_env,
2301                                        tcg_constant_i32(key),
2302                                        tcg_constant_i32(syndrome),
2303                                        tcg_constant_i32(isread));
2304     } else if (ri->type & ARM_CP_RAISES_EXC) {
2305         /*
2306          * The readfn or writefn might raise an exception;
2307          * synchronize the CPU state in case it does.
2308          */
2309         gen_a64_update_pc(s, 0);
2310     }
2311 
2312     if (!skip_fp_access_checks) {
2313         if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2314             return;
2315         } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2316             return;
2317         } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2318             return;
2319         }
2320     }
2321 
2322     if (nv_trap_to_el2) {
2323         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2324         return;
2325     }
2326 
2327     if (nv_redirect_reg) {
2328         /*
2329          * FEAT_NV2 redirection of an EL2 register to an EL1 register.
2330          * Conveniently in all cases the encoding of the EL1 register is
2331          * identical to the EL2 register except that opc1 is 0.
2332          * Get the reginfo for the EL1 register to use for the actual access.
2333          * We don't use the EL1 register's access function, and
2334          * fine-grained-traps on EL1 also do not apply here.
2335          */
2336         key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2337                                  crn, crm, op0, 0, op2);
2338         ri = get_arm_cp_reginfo(s->cp_regs, key);
2339         assert(ri);
2340         assert(cp_access_ok(s->current_el, ri, isread));
2341         /*
2342          * We might not have done an update_pc earlier, so check we don't
2343          * need it. We could support this in future if necessary.
2344          */
2345         assert(!(ri->type & ARM_CP_RAISES_EXC));
2346     }
2347 
2348     if (nv2_mem_redirect) {
2349         /*
2350          * This system register is being redirected into an EL2 memory access.
2351          * This means it is not an IO operation, doesn't change hflags,
2352          * and need not end the TB, because it has no side effects.
2353          *
2354          * The access is 64-bit single copy atomic, guaranteed aligned because
2355          * of the definition of VCNR_EL2. Its endianness depends on
2356          * SCTLR_EL2.EE, not on the data endianness of EL1.
2357          * It is done under either the EL2 translation regime or the EL2&0
2358          * translation regime, depending on HCR_EL2.E2H. It behaves as if
2359          * PSTATE.PAN is 0.
2360          */
2361         TCGv_i64 ptr = tcg_temp_new_i64();
2362         MemOp mop = MO_64 | MO_ALIGN | MO_ATOM_IFALIGN;
2363         ARMMMUIdx armmemidx = s->nv2_mem_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
2364         int memidx = arm_to_core_mmu_idx(armmemidx);
2365         uint32_t syn;
2366 
2367         mop |= (s->nv2_mem_be ? MO_BE : MO_LE);
2368 
2369         tcg_gen_ld_i64(ptr, tcg_env, offsetof(CPUARMState, cp15.vncr_el2));
2370         tcg_gen_addi_i64(ptr, ptr,
2371                          (ri->nv2_redirect_offset & ~NV2_REDIR_FLAG_MASK));
2372         tcg_rt = cpu_reg(s, rt);
2373 
2374         syn = syn_data_abort_vncr(0, !isread, 0);
2375         disas_set_insn_syndrome(s, syn);
2376         if (isread) {
2377             tcg_gen_qemu_ld_i64(tcg_rt, ptr, memidx, mop);
2378         } else {
2379             tcg_gen_qemu_st_i64(tcg_rt, ptr, memidx, mop);
2380         }
2381         return;
2382     }
2383 
2384     /* Handle special cases first */
2385     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2386     case 0:
2387         break;
2388     case ARM_CP_NOP:
2389         return;
2390     case ARM_CP_NZCV:
2391         tcg_rt = cpu_reg(s, rt);
2392         if (isread) {
2393             gen_get_nzcv(tcg_rt);
2394         } else {
2395             gen_set_nzcv(tcg_rt);
2396         }
2397         return;
2398     case ARM_CP_CURRENTEL:
2399     {
2400         /*
2401          * Reads as current EL value from pstate, which is
2402          * guaranteed to be constant by the tb flags.
2403          * For nested virt we should report EL2.
2404          */
2405         int el = s->nv ? 2 : s->current_el;
2406         tcg_rt = cpu_reg(s, rt);
2407         tcg_gen_movi_i64(tcg_rt, el << 2);
2408         return;
2409     }
2410     case ARM_CP_DC_ZVA:
2411         /* Writes clear the aligned block of memory which rt points into. */
2412         if (s->mte_active[0]) {
2413             int desc = 0;
2414 
2415             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2416             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2417             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2418 
2419             tcg_rt = tcg_temp_new_i64();
2420             gen_helper_mte_check_zva(tcg_rt, tcg_env,
2421                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2422         } else {
2423             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2424         }
2425         gen_helper_dc_zva(tcg_env, tcg_rt);
2426         return;
2427     case ARM_CP_DC_GVA:
2428         {
2429             TCGv_i64 clean_addr, tag;
2430 
2431             /*
2432              * DC_GVA, like DC_ZVA, requires that we supply the original
2433              * pointer for an invalid page.  Probe that address first.
2434              */
2435             tcg_rt = cpu_reg(s, rt);
2436             clean_addr = clean_data_tbi(s, tcg_rt);
2437             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2438 
2439             if (s->ata[0]) {
2440                 /* Extract the tag from the register to match STZGM.  */
2441                 tag = tcg_temp_new_i64();
2442                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2443                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2444             }
2445         }
2446         return;
2447     case ARM_CP_DC_GZVA:
2448         {
2449             TCGv_i64 clean_addr, tag;
2450 
2451             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2452             tcg_rt = cpu_reg(s, rt);
2453             clean_addr = clean_data_tbi(s, tcg_rt);
2454             gen_helper_dc_zva(tcg_env, clean_addr);
2455 
2456             if (s->ata[0]) {
2457                 /* Extract the tag from the register to match STZGM.  */
2458                 tag = tcg_temp_new_i64();
2459                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2460                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2461             }
2462         }
2463         return;
2464     default:
2465         g_assert_not_reached();
2466     }
2467 
2468     if (ri->type & ARM_CP_IO) {
2469         /* I/O operations must end the TB here (whether read or write) */
2470         need_exit_tb = translator_io_start(&s->base);
2471     }
2472 
2473     tcg_rt = cpu_reg(s, rt);
2474 
2475     if (isread) {
2476         if (ri->type & ARM_CP_CONST) {
2477             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2478         } else if (ri->readfn) {
2479             if (!tcg_ri) {
2480                 tcg_ri = gen_lookup_cp_reg(key);
2481             }
2482             gen_helper_get_cp_reg64(tcg_rt, tcg_env, tcg_ri);
2483         } else {
2484             tcg_gen_ld_i64(tcg_rt, tcg_env, ri->fieldoffset);
2485         }
2486     } else {
2487         if (ri->type & ARM_CP_CONST) {
2488             /* If not forbidden by access permissions, treat as WI */
2489             return;
2490         } else if (ri->writefn) {
2491             if (!tcg_ri) {
2492                 tcg_ri = gen_lookup_cp_reg(key);
2493             }
2494             gen_helper_set_cp_reg64(tcg_env, tcg_ri, tcg_rt);
2495         } else {
2496             tcg_gen_st_i64(tcg_rt, tcg_env, ri->fieldoffset);
2497         }
2498     }
2499 
2500     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2501         /*
2502          * A write to any coprocessor register that ends a TB
2503          * must rebuild the hflags for the next TB.
2504          */
2505         gen_rebuild_hflags(s);
2506         /*
2507          * We default to ending the TB on a coprocessor register write,
2508          * but allow this to be suppressed by the register definition
2509          * (usually only necessary to work around guest bugs).
2510          */
2511         need_exit_tb = true;
2512     }
2513     if (need_exit_tb) {
2514         s->base.is_jmp = DISAS_UPDATE_EXIT;
2515     }
2516 }
2517 
2518 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2519 {
2520     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2521     return true;
2522 }
2523 
2524 static bool trans_SVC(DisasContext *s, arg_i *a)
2525 {
2526     /*
2527      * For SVC, HVC and SMC we advance the single-step state
2528      * machine before taking the exception. This is architecturally
2529      * mandated, to ensure that single-stepping a system call
2530      * instruction works properly.
2531      */
2532     uint32_t syndrome = syn_aa64_svc(a->imm);
2533     if (s->fgt_svc) {
2534         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2535         return true;
2536     }
2537     gen_ss_advance(s);
2538     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2539     return true;
2540 }
2541 
2542 static bool trans_HVC(DisasContext *s, arg_i *a)
2543 {
2544     int target_el = s->current_el == 3 ? 3 : 2;
2545 
2546     if (s->current_el == 0) {
2547         unallocated_encoding(s);
2548         return true;
2549     }
2550     /*
2551      * The pre HVC helper handles cases when HVC gets trapped
2552      * as an undefined insn by runtime configuration.
2553      */
2554     gen_a64_update_pc(s, 0);
2555     gen_helper_pre_hvc(tcg_env);
2556     /* Architecture requires ss advance before we do the actual work */
2557     gen_ss_advance(s);
2558     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), target_el);
2559     return true;
2560 }
2561 
2562 static bool trans_SMC(DisasContext *s, arg_i *a)
2563 {
2564     if (s->current_el == 0) {
2565         unallocated_encoding(s);
2566         return true;
2567     }
2568     gen_a64_update_pc(s, 0);
2569     gen_helper_pre_smc(tcg_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2570     /* Architecture requires ss advance before we do the actual work */
2571     gen_ss_advance(s);
2572     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2573     return true;
2574 }
2575 
2576 static bool trans_BRK(DisasContext *s, arg_i *a)
2577 {
2578     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2579     return true;
2580 }
2581 
2582 static bool trans_HLT(DisasContext *s, arg_i *a)
2583 {
2584     /*
2585      * HLT. This has two purposes.
2586      * Architecturally, it is an external halting debug instruction.
2587      * Since QEMU doesn't implement external debug, we treat this as
2588      * it is required for halting debug disabled: it will UNDEF.
2589      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2590      */
2591     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2592         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2593     } else {
2594         unallocated_encoding(s);
2595     }
2596     return true;
2597 }
2598 
2599 /*
2600  * Load/Store exclusive instructions are implemented by remembering
2601  * the value/address loaded, and seeing if these are the same
2602  * when the store is performed. This is not actually the architecturally
2603  * mandated semantics, but it works for typical guest code sequences
2604  * and avoids having to monitor regular stores.
2605  *
2606  * The store exclusive uses the atomic cmpxchg primitives to avoid
2607  * races in multi-threaded linux-user and when MTTCG softmmu is
2608  * enabled.
2609  */
2610 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2611                                int size, bool is_pair)
2612 {
2613     int idx = get_mem_index(s);
2614     TCGv_i64 dirty_addr, clean_addr;
2615     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2616 
2617     s->is_ldex = true;
2618     dirty_addr = cpu_reg_sp(s, rn);
2619     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2620 
2621     g_assert(size <= 3);
2622     if (is_pair) {
2623         g_assert(size >= 2);
2624         if (size == 2) {
2625             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2626             if (s->be_data == MO_LE) {
2627                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2628                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2629             } else {
2630                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2631                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2632             }
2633         } else {
2634             TCGv_i128 t16 = tcg_temp_new_i128();
2635 
2636             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2637 
2638             if (s->be_data == MO_LE) {
2639                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2640                                       cpu_exclusive_high, t16);
2641             } else {
2642                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2643                                       cpu_exclusive_val, t16);
2644             }
2645             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2646             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2647         }
2648     } else {
2649         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2650         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2651     }
2652     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2653 }
2654 
2655 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2656                                 int rn, int size, int is_pair)
2657 {
2658     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2659      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2660      *     [addr] = {Rt};
2661      *     if (is_pair) {
2662      *         [addr + datasize] = {Rt2};
2663      *     }
2664      *     {Rd} = 0;
2665      * } else {
2666      *     {Rd} = 1;
2667      * }
2668      * env->exclusive_addr = -1;
2669      */
2670     TCGLabel *fail_label = gen_new_label();
2671     TCGLabel *done_label = gen_new_label();
2672     TCGv_i64 tmp, clean_addr;
2673     MemOp memop;
2674 
2675     /*
2676      * FIXME: We are out of spec here.  We have recorded only the address
2677      * from load_exclusive, not the entire range, and we assume that the
2678      * size of the access on both sides match.  The architecture allows the
2679      * store to be smaller than the load, so long as the stored bytes are
2680      * within the range recorded by the load.
2681      */
2682 
2683     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2684     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2685     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2686 
2687     /*
2688      * The write, and any associated faults, only happen if the virtual
2689      * and physical addresses pass the exclusive monitor check.  These
2690      * faults are exceedingly unlikely, because normally the guest uses
2691      * the exact same address register for the load_exclusive, and we
2692      * would have recognized these faults there.
2693      *
2694      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2695      * unaligned 4-byte write within the range of an aligned 8-byte load.
2696      * With LSE2, the store would need to cross a 16-byte boundary when the
2697      * load did not, which would mean the store is outside the range
2698      * recorded for the monitor, which would have failed a corrected monitor
2699      * check above.  For now, we assume no size change and retain the
2700      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2701      *
2702      * It is possible to trigger an MTE fault, by performing the load with
2703      * a virtual address with a valid tag and performing the store with the
2704      * same virtual address and a different invalid tag.
2705      */
2706     memop = size + is_pair;
2707     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2708         memop |= MO_ALIGN;
2709     }
2710     memop = finalize_memop(s, memop);
2711     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2712 
2713     tmp = tcg_temp_new_i64();
2714     if (is_pair) {
2715         if (size == 2) {
2716             if (s->be_data == MO_LE) {
2717                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2718             } else {
2719                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2720             }
2721             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2722                                        cpu_exclusive_val, tmp,
2723                                        get_mem_index(s), memop);
2724             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2725         } else {
2726             TCGv_i128 t16 = tcg_temp_new_i128();
2727             TCGv_i128 c16 = tcg_temp_new_i128();
2728             TCGv_i64 a, b;
2729 
2730             if (s->be_data == MO_LE) {
2731                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2732                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2733                                         cpu_exclusive_high);
2734             } else {
2735                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2736                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2737                                         cpu_exclusive_val);
2738             }
2739 
2740             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2741                                         get_mem_index(s), memop);
2742 
2743             a = tcg_temp_new_i64();
2744             b = tcg_temp_new_i64();
2745             if (s->be_data == MO_LE) {
2746                 tcg_gen_extr_i128_i64(a, b, t16);
2747             } else {
2748                 tcg_gen_extr_i128_i64(b, a, t16);
2749             }
2750 
2751             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2752             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2753             tcg_gen_or_i64(tmp, a, b);
2754 
2755             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2756         }
2757     } else {
2758         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2759                                    cpu_reg(s, rt), get_mem_index(s), memop);
2760         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2761     }
2762     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2763     tcg_gen_br(done_label);
2764 
2765     gen_set_label(fail_label);
2766     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2767     gen_set_label(done_label);
2768     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2769 }
2770 
2771 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2772                                  int rn, int size)
2773 {
2774     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2775     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2776     int memidx = get_mem_index(s);
2777     TCGv_i64 clean_addr;
2778     MemOp memop;
2779 
2780     if (rn == 31) {
2781         gen_check_sp_alignment(s);
2782     }
2783     memop = check_atomic_align(s, rn, size);
2784     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2785     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2786                                memidx, memop);
2787 }
2788 
2789 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2790                                       int rn, int size)
2791 {
2792     TCGv_i64 s1 = cpu_reg(s, rs);
2793     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2794     TCGv_i64 t1 = cpu_reg(s, rt);
2795     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2796     TCGv_i64 clean_addr;
2797     int memidx = get_mem_index(s);
2798     MemOp memop;
2799 
2800     if (rn == 31) {
2801         gen_check_sp_alignment(s);
2802     }
2803 
2804     /* This is a single atomic access, despite the "pair". */
2805     memop = check_atomic_align(s, rn, size + 1);
2806     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2807 
2808     if (size == 2) {
2809         TCGv_i64 cmp = tcg_temp_new_i64();
2810         TCGv_i64 val = tcg_temp_new_i64();
2811 
2812         if (s->be_data == MO_LE) {
2813             tcg_gen_concat32_i64(val, t1, t2);
2814             tcg_gen_concat32_i64(cmp, s1, s2);
2815         } else {
2816             tcg_gen_concat32_i64(val, t2, t1);
2817             tcg_gen_concat32_i64(cmp, s2, s1);
2818         }
2819 
2820         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2821 
2822         if (s->be_data == MO_LE) {
2823             tcg_gen_extr32_i64(s1, s2, cmp);
2824         } else {
2825             tcg_gen_extr32_i64(s2, s1, cmp);
2826         }
2827     } else {
2828         TCGv_i128 cmp = tcg_temp_new_i128();
2829         TCGv_i128 val = tcg_temp_new_i128();
2830 
2831         if (s->be_data == MO_LE) {
2832             tcg_gen_concat_i64_i128(val, t1, t2);
2833             tcg_gen_concat_i64_i128(cmp, s1, s2);
2834         } else {
2835             tcg_gen_concat_i64_i128(val, t2, t1);
2836             tcg_gen_concat_i64_i128(cmp, s2, s1);
2837         }
2838 
2839         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2840 
2841         if (s->be_data == MO_LE) {
2842             tcg_gen_extr_i128_i64(s1, s2, cmp);
2843         } else {
2844             tcg_gen_extr_i128_i64(s2, s1, cmp);
2845         }
2846     }
2847 }
2848 
2849 /*
2850  * Compute the ISS.SF bit for syndrome information if an exception
2851  * is taken on a load or store. This indicates whether the instruction
2852  * is accessing a 32-bit or 64-bit register. This logic is derived
2853  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2854  */
2855 static bool ldst_iss_sf(int size, bool sign, bool ext)
2856 {
2857 
2858     if (sign) {
2859         /*
2860          * Signed loads are 64 bit results if we are not going to
2861          * do a zero-extend from 32 to 64 after the load.
2862          * (For a store, sign and ext are always false.)
2863          */
2864         return !ext;
2865     } else {
2866         /* Unsigned loads/stores work at the specified size */
2867         return size == MO_64;
2868     }
2869 }
2870 
2871 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2872 {
2873     if (a->rn == 31) {
2874         gen_check_sp_alignment(s);
2875     }
2876     if (a->lasr) {
2877         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2878     }
2879     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2880     return true;
2881 }
2882 
2883 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2884 {
2885     if (a->rn == 31) {
2886         gen_check_sp_alignment(s);
2887     }
2888     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2889     if (a->lasr) {
2890         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2891     }
2892     return true;
2893 }
2894 
2895 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2896 {
2897     TCGv_i64 clean_addr;
2898     MemOp memop;
2899     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2900 
2901     /*
2902      * StoreLORelease is the same as Store-Release for QEMU, but
2903      * needs the feature-test.
2904      */
2905     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2906         return false;
2907     }
2908     /* Generate ISS for non-exclusive accesses including LASR.  */
2909     if (a->rn == 31) {
2910         gen_check_sp_alignment(s);
2911     }
2912     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2913     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2914     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2915                                 true, a->rn != 31, memop);
2916     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2917               iss_sf, a->lasr);
2918     return true;
2919 }
2920 
2921 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2922 {
2923     TCGv_i64 clean_addr;
2924     MemOp memop;
2925     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2926 
2927     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2928     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2929         return false;
2930     }
2931     /* Generate ISS for non-exclusive accesses including LASR.  */
2932     if (a->rn == 31) {
2933         gen_check_sp_alignment(s);
2934     }
2935     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2936     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2937                                 false, a->rn != 31, memop);
2938     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2939               a->rt, iss_sf, a->lasr);
2940     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2941     return true;
2942 }
2943 
2944 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2945 {
2946     if (a->rn == 31) {
2947         gen_check_sp_alignment(s);
2948     }
2949     if (a->lasr) {
2950         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2951     }
2952     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2953     return true;
2954 }
2955 
2956 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2957 {
2958     if (a->rn == 31) {
2959         gen_check_sp_alignment(s);
2960     }
2961     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2962     if (a->lasr) {
2963         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2964     }
2965     return true;
2966 }
2967 
2968 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2969 {
2970     if (!dc_isar_feature(aa64_atomics, s)) {
2971         return false;
2972     }
2973     if (((a->rt | a->rs) & 1) != 0) {
2974         return false;
2975     }
2976 
2977     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2978     return true;
2979 }
2980 
2981 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2982 {
2983     if (!dc_isar_feature(aa64_atomics, s)) {
2984         return false;
2985     }
2986     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2987     return true;
2988 }
2989 
2990 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2991 {
2992     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2993     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
2994     TCGv_i64 clean_addr = tcg_temp_new_i64();
2995     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
2996 
2997     gen_pc_plus_diff(s, clean_addr, a->imm);
2998     do_gpr_ld(s, tcg_rt, clean_addr, memop,
2999               false, true, a->rt, iss_sf, false);
3000     return true;
3001 }
3002 
3003 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
3004 {
3005     /* Load register (literal), vector version */
3006     TCGv_i64 clean_addr;
3007     MemOp memop;
3008 
3009     if (!fp_access_check(s)) {
3010         return true;
3011     }
3012     memop = finalize_memop_asimd(s, a->sz);
3013     clean_addr = tcg_temp_new_i64();
3014     gen_pc_plus_diff(s, clean_addr, a->imm);
3015     do_fp_ld(s, a->rt, clean_addr, memop);
3016     return true;
3017 }
3018 
3019 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
3020                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3021                                  uint64_t offset, bool is_store, MemOp mop)
3022 {
3023     if (a->rn == 31) {
3024         gen_check_sp_alignment(s);
3025     }
3026 
3027     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3028     if (!a->p) {
3029         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3030     }
3031 
3032     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
3033                                  (a->w || a->rn != 31), 2 << a->sz, mop);
3034 }
3035 
3036 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
3037                                   TCGv_i64 dirty_addr, uint64_t offset)
3038 {
3039     if (a->w) {
3040         if (a->p) {
3041             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3042         }
3043         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3044     }
3045 }
3046 
3047 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
3048 {
3049     uint64_t offset = a->imm << a->sz;
3050     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3051     MemOp mop = finalize_memop(s, a->sz);
3052 
3053     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3054     tcg_rt = cpu_reg(s, a->rt);
3055     tcg_rt2 = cpu_reg(s, a->rt2);
3056     /*
3057      * We built mop above for the single logical access -- rebuild it
3058      * now for the paired operation.
3059      *
3060      * With LSE2, non-sign-extending pairs are treated atomically if
3061      * aligned, and if unaligned one of the pair will be completely
3062      * within a 16-byte block and that element will be atomic.
3063      * Otherwise each element is separately atomic.
3064      * In all cases, issue one operation with the correct atomicity.
3065      */
3066     mop = a->sz + 1;
3067     if (s->align_mem) {
3068         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3069     }
3070     mop = finalize_memop_pair(s, mop);
3071     if (a->sz == 2) {
3072         TCGv_i64 tmp = tcg_temp_new_i64();
3073 
3074         if (s->be_data == MO_LE) {
3075             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3076         } else {
3077             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3078         }
3079         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3080     } else {
3081         TCGv_i128 tmp = tcg_temp_new_i128();
3082 
3083         if (s->be_data == MO_LE) {
3084             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3085         } else {
3086             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3087         }
3088         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3089     }
3090     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3091     return true;
3092 }
3093 
3094 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
3095 {
3096     uint64_t offset = a->imm << a->sz;
3097     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3098     MemOp mop = finalize_memop(s, a->sz);
3099 
3100     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3101     tcg_rt = cpu_reg(s, a->rt);
3102     tcg_rt2 = cpu_reg(s, a->rt2);
3103 
3104     /*
3105      * We built mop above for the single logical access -- rebuild it
3106      * now for the paired operation.
3107      *
3108      * With LSE2, non-sign-extending pairs are treated atomically if
3109      * aligned, and if unaligned one of the pair will be completely
3110      * within a 16-byte block and that element will be atomic.
3111      * Otherwise each element is separately atomic.
3112      * In all cases, issue one operation with the correct atomicity.
3113      *
3114      * This treats sign-extending loads like zero-extending loads,
3115      * since that reuses the most code below.
3116      */
3117     mop = a->sz + 1;
3118     if (s->align_mem) {
3119         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3120     }
3121     mop = finalize_memop_pair(s, mop);
3122     if (a->sz == 2) {
3123         int o2 = s->be_data == MO_LE ? 32 : 0;
3124         int o1 = o2 ^ 32;
3125 
3126         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3127         if (a->sign) {
3128             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3129             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3130         } else {
3131             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3132             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3133         }
3134     } else {
3135         TCGv_i128 tmp = tcg_temp_new_i128();
3136 
3137         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3138         if (s->be_data == MO_LE) {
3139             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3140         } else {
3141             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3142         }
3143     }
3144     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3145     return true;
3146 }
3147 
3148 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
3149 {
3150     uint64_t offset = a->imm << a->sz;
3151     TCGv_i64 clean_addr, dirty_addr;
3152     MemOp mop;
3153 
3154     if (!fp_access_check(s)) {
3155         return true;
3156     }
3157 
3158     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3159     mop = finalize_memop_asimd(s, a->sz);
3160     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3161     do_fp_st(s, a->rt, clean_addr, mop);
3162     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3163     do_fp_st(s, a->rt2, clean_addr, mop);
3164     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3165     return true;
3166 }
3167 
3168 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
3169 {
3170     uint64_t offset = a->imm << a->sz;
3171     TCGv_i64 clean_addr, dirty_addr;
3172     MemOp mop;
3173 
3174     if (!fp_access_check(s)) {
3175         return true;
3176     }
3177 
3178     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3179     mop = finalize_memop_asimd(s, a->sz);
3180     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3181     do_fp_ld(s, a->rt, clean_addr, mop);
3182     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3183     do_fp_ld(s, a->rt2, clean_addr, mop);
3184     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3185     return true;
3186 }
3187 
3188 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3189 {
3190     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3191     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3192     MemOp mop;
3193     TCGv_i128 tmp;
3194 
3195     /* STGP only comes in one size. */
3196     tcg_debug_assert(a->sz == MO_64);
3197 
3198     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3199         return false;
3200     }
3201 
3202     if (a->rn == 31) {
3203         gen_check_sp_alignment(s);
3204     }
3205 
3206     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3207     if (!a->p) {
3208         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3209     }
3210 
3211     clean_addr = clean_data_tbi(s, dirty_addr);
3212     tcg_rt = cpu_reg(s, a->rt);
3213     tcg_rt2 = cpu_reg(s, a->rt2);
3214 
3215     /*
3216      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3217      * and one tag operation.  We implement it as one single aligned 16-byte
3218      * memory operation for convenience.  Note that the alignment ensures
3219      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3220      */
3221     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3222 
3223     tmp = tcg_temp_new_i128();
3224     if (s->be_data == MO_LE) {
3225         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3226     } else {
3227         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3228     }
3229     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3230 
3231     /* Perform the tag store, if tag access enabled. */
3232     if (s->ata[0]) {
3233         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3234             gen_helper_stg_parallel(tcg_env, dirty_addr, dirty_addr);
3235         } else {
3236             gen_helper_stg(tcg_env, dirty_addr, dirty_addr);
3237         }
3238     }
3239 
3240     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3241     return true;
3242 }
3243 
3244 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3245                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3246                                  uint64_t offset, bool is_store, MemOp mop)
3247 {
3248     int memidx;
3249 
3250     if (a->rn == 31) {
3251         gen_check_sp_alignment(s);
3252     }
3253 
3254     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3255     if (!a->p) {
3256         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3257     }
3258     memidx = get_a64_user_mem_index(s, a->unpriv);
3259     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3260                                         a->w || a->rn != 31,
3261                                         mop, a->unpriv, memidx);
3262 }
3263 
3264 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3265                                   TCGv_i64 dirty_addr, uint64_t offset)
3266 {
3267     if (a->w) {
3268         if (a->p) {
3269             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3270         }
3271         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3272     }
3273 }
3274 
3275 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3276 {
3277     bool iss_sf, iss_valid = !a->w;
3278     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3279     int memidx = get_a64_user_mem_index(s, a->unpriv);
3280     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3281 
3282     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3283 
3284     tcg_rt = cpu_reg(s, a->rt);
3285     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3286 
3287     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3288                      iss_valid, a->rt, iss_sf, false);
3289     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3290     return true;
3291 }
3292 
3293 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3294 {
3295     bool iss_sf, iss_valid = !a->w;
3296     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3297     int memidx = get_a64_user_mem_index(s, a->unpriv);
3298     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3299 
3300     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3301 
3302     tcg_rt = cpu_reg(s, a->rt);
3303     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3304 
3305     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3306                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3307     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3308     return true;
3309 }
3310 
3311 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3312 {
3313     TCGv_i64 clean_addr, dirty_addr;
3314     MemOp mop;
3315 
3316     if (!fp_access_check(s)) {
3317         return true;
3318     }
3319     mop = finalize_memop_asimd(s, a->sz);
3320     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3321     do_fp_st(s, a->rt, clean_addr, mop);
3322     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3323     return true;
3324 }
3325 
3326 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3327 {
3328     TCGv_i64 clean_addr, dirty_addr;
3329     MemOp mop;
3330 
3331     if (!fp_access_check(s)) {
3332         return true;
3333     }
3334     mop = finalize_memop_asimd(s, a->sz);
3335     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3336     do_fp_ld(s, a->rt, clean_addr, mop);
3337     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3338     return true;
3339 }
3340 
3341 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3342                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3343                              bool is_store, MemOp memop)
3344 {
3345     TCGv_i64 tcg_rm;
3346 
3347     if (a->rn == 31) {
3348         gen_check_sp_alignment(s);
3349     }
3350     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3351 
3352     tcg_rm = read_cpu_reg(s, a->rm, 1);
3353     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3354 
3355     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3356     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3357 }
3358 
3359 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3360 {
3361     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3362     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3363     MemOp memop;
3364 
3365     if (extract32(a->opt, 1, 1) == 0) {
3366         return false;
3367     }
3368 
3369     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3370     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3371     tcg_rt = cpu_reg(s, a->rt);
3372     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3373               a->ext, true, a->rt, iss_sf, false);
3374     return true;
3375 }
3376 
3377 static bool trans_STR(DisasContext *s, arg_ldst *a)
3378 {
3379     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3380     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3381     MemOp memop;
3382 
3383     if (extract32(a->opt, 1, 1) == 0) {
3384         return false;
3385     }
3386 
3387     memop = finalize_memop(s, a->sz);
3388     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3389     tcg_rt = cpu_reg(s, a->rt);
3390     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3391     return true;
3392 }
3393 
3394 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3395 {
3396     TCGv_i64 clean_addr, dirty_addr;
3397     MemOp memop;
3398 
3399     if (extract32(a->opt, 1, 1) == 0) {
3400         return false;
3401     }
3402 
3403     if (!fp_access_check(s)) {
3404         return true;
3405     }
3406 
3407     memop = finalize_memop_asimd(s, a->sz);
3408     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3409     do_fp_ld(s, a->rt, clean_addr, memop);
3410     return true;
3411 }
3412 
3413 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3414 {
3415     TCGv_i64 clean_addr, dirty_addr;
3416     MemOp memop;
3417 
3418     if (extract32(a->opt, 1, 1) == 0) {
3419         return false;
3420     }
3421 
3422     if (!fp_access_check(s)) {
3423         return true;
3424     }
3425 
3426     memop = finalize_memop_asimd(s, a->sz);
3427     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3428     do_fp_st(s, a->rt, clean_addr, memop);
3429     return true;
3430 }
3431 
3432 
3433 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3434                          int sign, bool invert)
3435 {
3436     MemOp mop = a->sz | sign;
3437     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3438 
3439     if (a->rn == 31) {
3440         gen_check_sp_alignment(s);
3441     }
3442     mop = check_atomic_align(s, a->rn, mop);
3443     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3444                                 a->rn != 31, mop);
3445     tcg_rs = read_cpu_reg(s, a->rs, true);
3446     tcg_rt = cpu_reg(s, a->rt);
3447     if (invert) {
3448         tcg_gen_not_i64(tcg_rs, tcg_rs);
3449     }
3450     /*
3451      * The tcg atomic primitives are all full barriers.  Therefore we
3452      * can ignore the Acquire and Release bits of this instruction.
3453      */
3454     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3455 
3456     if (mop & MO_SIGN) {
3457         switch (a->sz) {
3458         case MO_8:
3459             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3460             break;
3461         case MO_16:
3462             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3463             break;
3464         case MO_32:
3465             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3466             break;
3467         case MO_64:
3468             break;
3469         default:
3470             g_assert_not_reached();
3471         }
3472     }
3473     return true;
3474 }
3475 
3476 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3477 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3478 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3479 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3480 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3481 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3482 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3483 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3484 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3485 
3486 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3487 {
3488     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3489     TCGv_i64 clean_addr;
3490     MemOp mop;
3491 
3492     if (!dc_isar_feature(aa64_atomics, s) ||
3493         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3494         return false;
3495     }
3496     if (a->rn == 31) {
3497         gen_check_sp_alignment(s);
3498     }
3499     mop = check_atomic_align(s, a->rn, a->sz);
3500     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3501                                 a->rn != 31, mop);
3502     /*
3503      * LDAPR* are a special case because they are a simple load, not a
3504      * fetch-and-do-something op.
3505      * The architectural consistency requirements here are weaker than
3506      * full load-acquire (we only need "load-acquire processor consistent"),
3507      * but we choose to implement them as full LDAQ.
3508      */
3509     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3510               true, a->rt, iss_sf, true);
3511     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3512     return true;
3513 }
3514 
3515 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3516 {
3517     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3518     MemOp memop;
3519 
3520     /* Load with pointer authentication */
3521     if (!dc_isar_feature(aa64_pauth, s)) {
3522         return false;
3523     }
3524 
3525     if (a->rn == 31) {
3526         gen_check_sp_alignment(s);
3527     }
3528     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3529 
3530     if (s->pauth_active) {
3531         if (!a->m) {
3532             gen_helper_autda_combined(dirty_addr, tcg_env, dirty_addr,
3533                                       tcg_constant_i64(0));
3534         } else {
3535             gen_helper_autdb_combined(dirty_addr, tcg_env, dirty_addr,
3536                                       tcg_constant_i64(0));
3537         }
3538     }
3539 
3540     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3541 
3542     memop = finalize_memop(s, MO_64);
3543 
3544     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3545     clean_addr = gen_mte_check1(s, dirty_addr, false,
3546                                 a->w || a->rn != 31, memop);
3547 
3548     tcg_rt = cpu_reg(s, a->rt);
3549     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3550               /* extend */ false, /* iss_valid */ !a->w,
3551               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3552 
3553     if (a->w) {
3554         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3555     }
3556     return true;
3557 }
3558 
3559 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3560 {
3561     TCGv_i64 clean_addr, dirty_addr;
3562     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3563     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3564 
3565     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3566         return false;
3567     }
3568 
3569     if (a->rn == 31) {
3570         gen_check_sp_alignment(s);
3571     }
3572 
3573     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3574     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3575     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3576     clean_addr = clean_data_tbi(s, dirty_addr);
3577 
3578     /*
3579      * Load-AcquirePC semantics; we implement as the slightly more
3580      * restrictive Load-Acquire.
3581      */
3582     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3583               a->rt, iss_sf, true);
3584     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3585     return true;
3586 }
3587 
3588 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3589 {
3590     TCGv_i64 clean_addr, dirty_addr;
3591     MemOp mop = a->sz;
3592     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3593 
3594     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3595         return false;
3596     }
3597 
3598     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3599 
3600     if (a->rn == 31) {
3601         gen_check_sp_alignment(s);
3602     }
3603 
3604     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3605     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3606     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3607     clean_addr = clean_data_tbi(s, dirty_addr);
3608 
3609     /* Store-Release semantics */
3610     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3611     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3612     return true;
3613 }
3614 
3615 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3616 {
3617     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3618     MemOp endian, align, mop;
3619 
3620     int total;    /* total bytes */
3621     int elements; /* elements per vector */
3622     int r;
3623     int size = a->sz;
3624 
3625     if (!a->p && a->rm != 0) {
3626         /* For non-postindexed accesses the Rm field must be 0 */
3627         return false;
3628     }
3629     if (size == 3 && !a->q && a->selem != 1) {
3630         return false;
3631     }
3632     if (!fp_access_check(s)) {
3633         return true;
3634     }
3635 
3636     if (a->rn == 31) {
3637         gen_check_sp_alignment(s);
3638     }
3639 
3640     /* For our purposes, bytes are always little-endian.  */
3641     endian = s->be_data;
3642     if (size == 0) {
3643         endian = MO_LE;
3644     }
3645 
3646     total = a->rpt * a->selem * (a->q ? 16 : 8);
3647     tcg_rn = cpu_reg_sp(s, a->rn);
3648 
3649     /*
3650      * Issue the MTE check vs the logical repeat count, before we
3651      * promote consecutive little-endian elements below.
3652      */
3653     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3654                                 finalize_memop_asimd(s, size));
3655 
3656     /*
3657      * Consecutive little-endian elements from a single register
3658      * can be promoted to a larger little-endian operation.
3659      */
3660     align = MO_ALIGN;
3661     if (a->selem == 1 && endian == MO_LE) {
3662         align = pow2_align(size);
3663         size = 3;
3664     }
3665     if (!s->align_mem) {
3666         align = 0;
3667     }
3668     mop = endian | size | align;
3669 
3670     elements = (a->q ? 16 : 8) >> size;
3671     tcg_ebytes = tcg_constant_i64(1 << size);
3672     for (r = 0; r < a->rpt; r++) {
3673         int e;
3674         for (e = 0; e < elements; e++) {
3675             int xs;
3676             for (xs = 0; xs < a->selem; xs++) {
3677                 int tt = (a->rt + r + xs) % 32;
3678                 do_vec_ld(s, tt, e, clean_addr, mop);
3679                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3680             }
3681         }
3682     }
3683 
3684     /*
3685      * For non-quad operations, setting a slice of the low 64 bits of
3686      * the register clears the high 64 bits (in the ARM ARM pseudocode
3687      * this is implicit in the fact that 'rval' is a 64 bit wide
3688      * variable).  For quad operations, we might still need to zero
3689      * the high bits of SVE.
3690      */
3691     for (r = 0; r < a->rpt * a->selem; r++) {
3692         int tt = (a->rt + r) % 32;
3693         clear_vec_high(s, a->q, tt);
3694     }
3695 
3696     if (a->p) {
3697         if (a->rm == 31) {
3698             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3699         } else {
3700             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3701         }
3702     }
3703     return true;
3704 }
3705 
3706 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3707 {
3708     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3709     MemOp endian, align, mop;
3710 
3711     int total;    /* total bytes */
3712     int elements; /* elements per vector */
3713     int r;
3714     int size = a->sz;
3715 
3716     if (!a->p && a->rm != 0) {
3717         /* For non-postindexed accesses the Rm field must be 0 */
3718         return false;
3719     }
3720     if (size == 3 && !a->q && a->selem != 1) {
3721         return false;
3722     }
3723     if (!fp_access_check(s)) {
3724         return true;
3725     }
3726 
3727     if (a->rn == 31) {
3728         gen_check_sp_alignment(s);
3729     }
3730 
3731     /* For our purposes, bytes are always little-endian.  */
3732     endian = s->be_data;
3733     if (size == 0) {
3734         endian = MO_LE;
3735     }
3736 
3737     total = a->rpt * a->selem * (a->q ? 16 : 8);
3738     tcg_rn = cpu_reg_sp(s, a->rn);
3739 
3740     /*
3741      * Issue the MTE check vs the logical repeat count, before we
3742      * promote consecutive little-endian elements below.
3743      */
3744     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3745                                 finalize_memop_asimd(s, size));
3746 
3747     /*
3748      * Consecutive little-endian elements from a single register
3749      * can be promoted to a larger little-endian operation.
3750      */
3751     align = MO_ALIGN;
3752     if (a->selem == 1 && endian == MO_LE) {
3753         align = pow2_align(size);
3754         size = 3;
3755     }
3756     if (!s->align_mem) {
3757         align = 0;
3758     }
3759     mop = endian | size | align;
3760 
3761     elements = (a->q ? 16 : 8) >> size;
3762     tcg_ebytes = tcg_constant_i64(1 << size);
3763     for (r = 0; r < a->rpt; r++) {
3764         int e;
3765         for (e = 0; e < elements; e++) {
3766             int xs;
3767             for (xs = 0; xs < a->selem; xs++) {
3768                 int tt = (a->rt + r + xs) % 32;
3769                 do_vec_st(s, tt, e, clean_addr, mop);
3770                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3771             }
3772         }
3773     }
3774 
3775     if (a->p) {
3776         if (a->rm == 31) {
3777             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3778         } else {
3779             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3780         }
3781     }
3782     return true;
3783 }
3784 
3785 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3786 {
3787     int xs, total, rt;
3788     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3789     MemOp mop;
3790 
3791     if (!a->p && a->rm != 0) {
3792         return false;
3793     }
3794     if (!fp_access_check(s)) {
3795         return true;
3796     }
3797 
3798     if (a->rn == 31) {
3799         gen_check_sp_alignment(s);
3800     }
3801 
3802     total = a->selem << a->scale;
3803     tcg_rn = cpu_reg_sp(s, a->rn);
3804 
3805     mop = finalize_memop_asimd(s, a->scale);
3806     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3807                                 total, mop);
3808 
3809     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3810     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3811         do_vec_st(s, rt, a->index, clean_addr, mop);
3812         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3813     }
3814 
3815     if (a->p) {
3816         if (a->rm == 31) {
3817             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3818         } else {
3819             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3820         }
3821     }
3822     return true;
3823 }
3824 
3825 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3826 {
3827     int xs, total, rt;
3828     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3829     MemOp mop;
3830 
3831     if (!a->p && a->rm != 0) {
3832         return false;
3833     }
3834     if (!fp_access_check(s)) {
3835         return true;
3836     }
3837 
3838     if (a->rn == 31) {
3839         gen_check_sp_alignment(s);
3840     }
3841 
3842     total = a->selem << a->scale;
3843     tcg_rn = cpu_reg_sp(s, a->rn);
3844 
3845     mop = finalize_memop_asimd(s, a->scale);
3846     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3847                                 total, mop);
3848 
3849     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3850     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3851         do_vec_ld(s, rt, a->index, clean_addr, mop);
3852         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3853     }
3854 
3855     if (a->p) {
3856         if (a->rm == 31) {
3857             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3858         } else {
3859             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3860         }
3861     }
3862     return true;
3863 }
3864 
3865 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3866 {
3867     int xs, total, rt;
3868     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3869     MemOp mop;
3870 
3871     if (!a->p && a->rm != 0) {
3872         return false;
3873     }
3874     if (!fp_access_check(s)) {
3875         return true;
3876     }
3877 
3878     if (a->rn == 31) {
3879         gen_check_sp_alignment(s);
3880     }
3881 
3882     total = a->selem << a->scale;
3883     tcg_rn = cpu_reg_sp(s, a->rn);
3884 
3885     mop = finalize_memop_asimd(s, a->scale);
3886     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3887                                 total, mop);
3888 
3889     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3890     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3891         /* Load and replicate to all elements */
3892         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3893 
3894         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3895         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3896                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3897         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3898     }
3899 
3900     if (a->p) {
3901         if (a->rm == 31) {
3902             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3903         } else {
3904             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3905         }
3906     }
3907     return true;
3908 }
3909 
3910 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3911 {
3912     TCGv_i64 addr, clean_addr, tcg_rt;
3913     int size = 4 << s->dcz_blocksize;
3914 
3915     if (!dc_isar_feature(aa64_mte, s)) {
3916         return false;
3917     }
3918     if (s->current_el == 0) {
3919         return false;
3920     }
3921 
3922     if (a->rn == 31) {
3923         gen_check_sp_alignment(s);
3924     }
3925 
3926     addr = read_cpu_reg_sp(s, a->rn, true);
3927     tcg_gen_addi_i64(addr, addr, a->imm);
3928     tcg_rt = cpu_reg(s, a->rt);
3929 
3930     if (s->ata[0]) {
3931         gen_helper_stzgm_tags(tcg_env, addr, tcg_rt);
3932     }
3933     /*
3934      * The non-tags portion of STZGM is mostly like DC_ZVA,
3935      * except the alignment happens before the access.
3936      */
3937     clean_addr = clean_data_tbi(s, addr);
3938     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3939     gen_helper_dc_zva(tcg_env, clean_addr);
3940     return true;
3941 }
3942 
3943 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3944 {
3945     TCGv_i64 addr, clean_addr, tcg_rt;
3946 
3947     if (!dc_isar_feature(aa64_mte, s)) {
3948         return false;
3949     }
3950     if (s->current_el == 0) {
3951         return false;
3952     }
3953 
3954     if (a->rn == 31) {
3955         gen_check_sp_alignment(s);
3956     }
3957 
3958     addr = read_cpu_reg_sp(s, a->rn, true);
3959     tcg_gen_addi_i64(addr, addr, a->imm);
3960     tcg_rt = cpu_reg(s, a->rt);
3961 
3962     if (s->ata[0]) {
3963         gen_helper_stgm(tcg_env, addr, tcg_rt);
3964     } else {
3965         MMUAccessType acc = MMU_DATA_STORE;
3966         int size = 4 << s->gm_blocksize;
3967 
3968         clean_addr = clean_data_tbi(s, addr);
3969         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3970         gen_probe_access(s, clean_addr, acc, size);
3971     }
3972     return true;
3973 }
3974 
3975 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3976 {
3977     TCGv_i64 addr, clean_addr, tcg_rt;
3978 
3979     if (!dc_isar_feature(aa64_mte, s)) {
3980         return false;
3981     }
3982     if (s->current_el == 0) {
3983         return false;
3984     }
3985 
3986     if (a->rn == 31) {
3987         gen_check_sp_alignment(s);
3988     }
3989 
3990     addr = read_cpu_reg_sp(s, a->rn, true);
3991     tcg_gen_addi_i64(addr, addr, a->imm);
3992     tcg_rt = cpu_reg(s, a->rt);
3993 
3994     if (s->ata[0]) {
3995         gen_helper_ldgm(tcg_rt, tcg_env, addr);
3996     } else {
3997         MMUAccessType acc = MMU_DATA_LOAD;
3998         int size = 4 << s->gm_blocksize;
3999 
4000         clean_addr = clean_data_tbi(s, addr);
4001         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4002         gen_probe_access(s, clean_addr, acc, size);
4003         /* The result tags are zeros.  */
4004         tcg_gen_movi_i64(tcg_rt, 0);
4005     }
4006     return true;
4007 }
4008 
4009 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
4010 {
4011     TCGv_i64 addr, clean_addr, tcg_rt;
4012 
4013     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
4014         return false;
4015     }
4016 
4017     if (a->rn == 31) {
4018         gen_check_sp_alignment(s);
4019     }
4020 
4021     addr = read_cpu_reg_sp(s, a->rn, true);
4022     if (!a->p) {
4023         /* pre-index or signed offset */
4024         tcg_gen_addi_i64(addr, addr, a->imm);
4025     }
4026 
4027     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4028     tcg_rt = cpu_reg(s, a->rt);
4029     if (s->ata[0]) {
4030         gen_helper_ldg(tcg_rt, tcg_env, addr, tcg_rt);
4031     } else {
4032         /*
4033          * Tag access disabled: we must check for aborts on the load
4034          * load from [rn+offset], and then insert a 0 tag into rt.
4035          */
4036         clean_addr = clean_data_tbi(s, addr);
4037         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4038         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4039     }
4040 
4041     if (a->w) {
4042         /* pre-index or post-index */
4043         if (a->p) {
4044             /* post-index */
4045             tcg_gen_addi_i64(addr, addr, a->imm);
4046         }
4047         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
4048     }
4049     return true;
4050 }
4051 
4052 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
4053 {
4054     TCGv_i64 addr, tcg_rt;
4055 
4056     if (a->rn == 31) {
4057         gen_check_sp_alignment(s);
4058     }
4059 
4060     addr = read_cpu_reg_sp(s, a->rn, true);
4061     if (!a->p) {
4062         /* pre-index or signed offset */
4063         tcg_gen_addi_i64(addr, addr, a->imm);
4064     }
4065     tcg_rt = cpu_reg_sp(s, a->rt);
4066     if (!s->ata[0]) {
4067         /*
4068          * For STG and ST2G, we need to check alignment and probe memory.
4069          * TODO: For STZG and STZ2G, we could rely on the stores below,
4070          * at least for system mode; user-only won't enforce alignment.
4071          */
4072         if (is_pair) {
4073             gen_helper_st2g_stub(tcg_env, addr);
4074         } else {
4075             gen_helper_stg_stub(tcg_env, addr);
4076         }
4077     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4078         if (is_pair) {
4079             gen_helper_st2g_parallel(tcg_env, addr, tcg_rt);
4080         } else {
4081             gen_helper_stg_parallel(tcg_env, addr, tcg_rt);
4082         }
4083     } else {
4084         if (is_pair) {
4085             gen_helper_st2g(tcg_env, addr, tcg_rt);
4086         } else {
4087             gen_helper_stg(tcg_env, addr, tcg_rt);
4088         }
4089     }
4090 
4091     if (is_zero) {
4092         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4093         TCGv_i64 zero64 = tcg_constant_i64(0);
4094         TCGv_i128 zero128 = tcg_temp_new_i128();
4095         int mem_index = get_mem_index(s);
4096         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4097 
4098         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4099 
4100         /* This is 1 or 2 atomic 16-byte operations. */
4101         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4102         if (is_pair) {
4103             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4104             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4105         }
4106     }
4107 
4108     if (a->w) {
4109         /* pre-index or post-index */
4110         if (a->p) {
4111             /* post-index */
4112             tcg_gen_addi_i64(addr, addr, a->imm);
4113         }
4114         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
4115     }
4116     return true;
4117 }
4118 
4119 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
4120 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
4121 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
4122 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
4123 
4124 typedef void SetFn(TCGv_env, TCGv_i32, TCGv_i32);
4125 
4126 static bool do_SET(DisasContext *s, arg_set *a, bool is_epilogue,
4127                    bool is_setg, SetFn fn)
4128 {
4129     int memidx;
4130     uint32_t syndrome, desc = 0;
4131 
4132     if (is_setg && !dc_isar_feature(aa64_mte, s)) {
4133         return false;
4134     }
4135 
4136     /*
4137      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4138      * us to pull this check before the CheckMOPSEnabled() test
4139      * (which we do in the helper function)
4140      */
4141     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4142         a->rd == 31 || a->rn == 31) {
4143         return false;
4144     }
4145 
4146     memidx = get_a64_user_mem_index(s, a->unpriv);
4147 
4148     /*
4149      * We pass option_a == true, matching our implementation;
4150      * we pass wrong_option == false: helper function may set that bit.
4151      */
4152     syndrome = syn_mop(true, is_setg, (a->nontemp << 1) | a->unpriv,
4153                        is_epilogue, false, true, a->rd, a->rs, a->rn);
4154 
4155     if (is_setg ? s->ata[a->unpriv] : s->mte_active[a->unpriv]) {
4156         /* We may need to do MTE tag checking, so assemble the descriptor */
4157         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
4158         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
4159         desc = FIELD_DP32(desc, MTEDESC, WRITE, true);
4160         /* SIZEM1 and ALIGN we leave 0 (byte write) */
4161     }
4162     /* The helper function always needs the memidx even with MTE disabled */
4163     desc = FIELD_DP32(desc, MTEDESC, MIDX, memidx);
4164 
4165     /*
4166      * The helper needs the register numbers, but since they're in
4167      * the syndrome anyway, we let it extract them from there rather
4168      * than passing in an extra three integer arguments.
4169      */
4170     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(desc));
4171     return true;
4172 }
4173 
4174 TRANS_FEAT(SETP, aa64_mops, do_SET, a, false, false, gen_helper_setp)
4175 TRANS_FEAT(SETM, aa64_mops, do_SET, a, false, false, gen_helper_setm)
4176 TRANS_FEAT(SETE, aa64_mops, do_SET, a, true, false, gen_helper_sete)
4177 TRANS_FEAT(SETGP, aa64_mops, do_SET, a, false, true, gen_helper_setgp)
4178 TRANS_FEAT(SETGM, aa64_mops, do_SET, a, false, true, gen_helper_setgm)
4179 TRANS_FEAT(SETGE, aa64_mops, do_SET, a, true, true, gen_helper_setge)
4180 
4181 typedef void CpyFn(TCGv_env, TCGv_i32, TCGv_i32, TCGv_i32);
4182 
4183 static bool do_CPY(DisasContext *s, arg_cpy *a, bool is_epilogue, CpyFn fn)
4184 {
4185     int rmemidx, wmemidx;
4186     uint32_t syndrome, rdesc = 0, wdesc = 0;
4187     bool wunpriv = extract32(a->options, 0, 1);
4188     bool runpriv = extract32(a->options, 1, 1);
4189 
4190     /*
4191      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4192      * us to pull this check before the CheckMOPSEnabled() test
4193      * (which we do in the helper function)
4194      */
4195     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4196         a->rd == 31 || a->rs == 31 || a->rn == 31) {
4197         return false;
4198     }
4199 
4200     rmemidx = get_a64_user_mem_index(s, runpriv);
4201     wmemidx = get_a64_user_mem_index(s, wunpriv);
4202 
4203     /*
4204      * We pass option_a == true, matching our implementation;
4205      * we pass wrong_option == false: helper function may set that bit.
4206      */
4207     syndrome = syn_mop(false, false, a->options, is_epilogue,
4208                        false, true, a->rd, a->rs, a->rn);
4209 
4210     /* If we need to do MTE tag checking, assemble the descriptors */
4211     if (s->mte_active[runpriv]) {
4212         rdesc = FIELD_DP32(rdesc, MTEDESC, TBI, s->tbid);
4213         rdesc = FIELD_DP32(rdesc, MTEDESC, TCMA, s->tcma);
4214     }
4215     if (s->mte_active[wunpriv]) {
4216         wdesc = FIELD_DP32(wdesc, MTEDESC, TBI, s->tbid);
4217         wdesc = FIELD_DP32(wdesc, MTEDESC, TCMA, s->tcma);
4218         wdesc = FIELD_DP32(wdesc, MTEDESC, WRITE, true);
4219     }
4220     /* The helper function needs these parts of the descriptor regardless */
4221     rdesc = FIELD_DP32(rdesc, MTEDESC, MIDX, rmemidx);
4222     wdesc = FIELD_DP32(wdesc, MTEDESC, MIDX, wmemidx);
4223 
4224     /*
4225      * The helper needs the register numbers, but since they're in
4226      * the syndrome anyway, we let it extract them from there rather
4227      * than passing in an extra three integer arguments.
4228      */
4229     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(wdesc),
4230        tcg_constant_i32(rdesc));
4231     return true;
4232 }
4233 
4234 TRANS_FEAT(CPYP, aa64_mops, do_CPY, a, false, gen_helper_cpyp)
4235 TRANS_FEAT(CPYM, aa64_mops, do_CPY, a, false, gen_helper_cpym)
4236 TRANS_FEAT(CPYE, aa64_mops, do_CPY, a, true, gen_helper_cpye)
4237 TRANS_FEAT(CPYFP, aa64_mops, do_CPY, a, false, gen_helper_cpyfp)
4238 TRANS_FEAT(CPYFM, aa64_mops, do_CPY, a, false, gen_helper_cpyfm)
4239 TRANS_FEAT(CPYFE, aa64_mops, do_CPY, a, true, gen_helper_cpyfe)
4240 
4241 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4242 
4243 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4244                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4245 {
4246     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4247     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4248     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4249 
4250     fn(tcg_rd, tcg_rn, tcg_imm);
4251     if (!a->sf) {
4252         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4253     }
4254     return true;
4255 }
4256 
4257 /*
4258  * PC-rel. addressing
4259  */
4260 
4261 static bool trans_ADR(DisasContext *s, arg_ri *a)
4262 {
4263     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4264     return true;
4265 }
4266 
4267 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4268 {
4269     int64_t offset = (int64_t)a->imm << 12;
4270 
4271     /* The page offset is ok for CF_PCREL. */
4272     offset -= s->pc_curr & 0xfff;
4273     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4274     return true;
4275 }
4276 
4277 /*
4278  * Add/subtract (immediate)
4279  */
4280 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4281 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4282 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4283 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4284 
4285 /*
4286  * Add/subtract (immediate, with tags)
4287  */
4288 
4289 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4290                                       bool sub_op)
4291 {
4292     TCGv_i64 tcg_rn, tcg_rd;
4293     int imm;
4294 
4295     imm = a->uimm6 << LOG2_TAG_GRANULE;
4296     if (sub_op) {
4297         imm = -imm;
4298     }
4299 
4300     tcg_rn = cpu_reg_sp(s, a->rn);
4301     tcg_rd = cpu_reg_sp(s, a->rd);
4302 
4303     if (s->ata[0]) {
4304         gen_helper_addsubg(tcg_rd, tcg_env, tcg_rn,
4305                            tcg_constant_i32(imm),
4306                            tcg_constant_i32(a->uimm4));
4307     } else {
4308         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4309         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4310     }
4311     return true;
4312 }
4313 
4314 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4315 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4316 
4317 /* The input should be a value in the bottom e bits (with higher
4318  * bits zero); returns that value replicated into every element
4319  * of size e in a 64 bit integer.
4320  */
4321 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4322 {
4323     assert(e != 0);
4324     while (e < 64) {
4325         mask |= mask << e;
4326         e *= 2;
4327     }
4328     return mask;
4329 }
4330 
4331 /*
4332  * Logical (immediate)
4333  */
4334 
4335 /*
4336  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4337  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4338  * value (ie should cause a guest UNDEF exception), and true if they are
4339  * valid, in which case the decoded bit pattern is written to result.
4340  */
4341 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4342                             unsigned int imms, unsigned int immr)
4343 {
4344     uint64_t mask;
4345     unsigned e, levels, s, r;
4346     int len;
4347 
4348     assert(immn < 2 && imms < 64 && immr < 64);
4349 
4350     /* The bit patterns we create here are 64 bit patterns which
4351      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4352      * 64 bits each. Each element contains the same value: a run
4353      * of between 1 and e-1 non-zero bits, rotated within the
4354      * element by between 0 and e-1 bits.
4355      *
4356      * The element size and run length are encoded into immn (1 bit)
4357      * and imms (6 bits) as follows:
4358      * 64 bit elements: immn = 1, imms = <length of run - 1>
4359      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4360      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4361      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4362      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4363      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4364      * Notice that immn = 0, imms = 11111x is the only combination
4365      * not covered by one of the above options; this is reserved.
4366      * Further, <length of run - 1> all-ones is a reserved pattern.
4367      *
4368      * In all cases the rotation is by immr % e (and immr is 6 bits).
4369      */
4370 
4371     /* First determine the element size */
4372     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4373     if (len < 1) {
4374         /* This is the immn == 0, imms == 0x11111x case */
4375         return false;
4376     }
4377     e = 1 << len;
4378 
4379     levels = e - 1;
4380     s = imms & levels;
4381     r = immr & levels;
4382 
4383     if (s == levels) {
4384         /* <length of run - 1> mustn't be all-ones. */
4385         return false;
4386     }
4387 
4388     /* Create the value of one element: s+1 set bits rotated
4389      * by r within the element (which is e bits wide)...
4390      */
4391     mask = MAKE_64BIT_MASK(0, s + 1);
4392     if (r) {
4393         mask = (mask >> r) | (mask << (e - r));
4394         mask &= MAKE_64BIT_MASK(0, e);
4395     }
4396     /* ...then replicate the element over the whole 64 bit value */
4397     mask = bitfield_replicate(mask, e);
4398     *result = mask;
4399     return true;
4400 }
4401 
4402 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4403                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4404 {
4405     TCGv_i64 tcg_rd, tcg_rn;
4406     uint64_t imm;
4407 
4408     /* Some immediate field values are reserved. */
4409     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4410                                 extract32(a->dbm, 0, 6),
4411                                 extract32(a->dbm, 6, 6))) {
4412         return false;
4413     }
4414     if (!a->sf) {
4415         imm &= 0xffffffffull;
4416     }
4417 
4418     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4419     tcg_rn = cpu_reg(s, a->rn);
4420 
4421     fn(tcg_rd, tcg_rn, imm);
4422     if (set_cc) {
4423         gen_logic_CC(a->sf, tcg_rd);
4424     }
4425     if (!a->sf) {
4426         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4427     }
4428     return true;
4429 }
4430 
4431 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4432 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4433 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4434 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4435 
4436 /*
4437  * Move wide (immediate)
4438  */
4439 
4440 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4441 {
4442     int pos = a->hw << 4;
4443     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4444     return true;
4445 }
4446 
4447 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4448 {
4449     int pos = a->hw << 4;
4450     uint64_t imm = a->imm;
4451 
4452     imm = ~(imm << pos);
4453     if (!a->sf) {
4454         imm = (uint32_t)imm;
4455     }
4456     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4457     return true;
4458 }
4459 
4460 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4461 {
4462     int pos = a->hw << 4;
4463     TCGv_i64 tcg_rd, tcg_im;
4464 
4465     tcg_rd = cpu_reg(s, a->rd);
4466     tcg_im = tcg_constant_i64(a->imm);
4467     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4468     if (!a->sf) {
4469         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4470     }
4471     return true;
4472 }
4473 
4474 /*
4475  * Bitfield
4476  */
4477 
4478 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4479 {
4480     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4481     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4482     unsigned int bitsize = a->sf ? 64 : 32;
4483     unsigned int ri = a->immr;
4484     unsigned int si = a->imms;
4485     unsigned int pos, len;
4486 
4487     if (si >= ri) {
4488         /* Wd<s-r:0> = Wn<s:r> */
4489         len = (si - ri) + 1;
4490         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4491         if (!a->sf) {
4492             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4493         }
4494     } else {
4495         /* Wd<32+s-r,32-r> = Wn<s:0> */
4496         len = si + 1;
4497         pos = (bitsize - ri) & (bitsize - 1);
4498 
4499         if (len < ri) {
4500             /*
4501              * Sign extend the destination field from len to fill the
4502              * balance of the word.  Let the deposit below insert all
4503              * of those sign bits.
4504              */
4505             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4506             len = ri;
4507         }
4508 
4509         /*
4510          * We start with zero, and we haven't modified any bits outside
4511          * bitsize, therefore no final zero-extension is unneeded for !sf.
4512          */
4513         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4514     }
4515     return true;
4516 }
4517 
4518 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4519 {
4520     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4521     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4522     unsigned int bitsize = a->sf ? 64 : 32;
4523     unsigned int ri = a->immr;
4524     unsigned int si = a->imms;
4525     unsigned int pos, len;
4526 
4527     tcg_rd = cpu_reg(s, a->rd);
4528     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4529 
4530     if (si >= ri) {
4531         /* Wd<s-r:0> = Wn<s:r> */
4532         len = (si - ri) + 1;
4533         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4534     } else {
4535         /* Wd<32+s-r,32-r> = Wn<s:0> */
4536         len = si + 1;
4537         pos = (bitsize - ri) & (bitsize - 1);
4538         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4539     }
4540     return true;
4541 }
4542 
4543 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4544 {
4545     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4546     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4547     unsigned int bitsize = a->sf ? 64 : 32;
4548     unsigned int ri = a->immr;
4549     unsigned int si = a->imms;
4550     unsigned int pos, len;
4551 
4552     tcg_rd = cpu_reg(s, a->rd);
4553     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4554 
4555     if (si >= ri) {
4556         /* Wd<s-r:0> = Wn<s:r> */
4557         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4558         len = (si - ri) + 1;
4559         pos = 0;
4560     } else {
4561         /* Wd<32+s-r,32-r> = Wn<s:0> */
4562         len = si + 1;
4563         pos = (bitsize - ri) & (bitsize - 1);
4564     }
4565 
4566     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4567     if (!a->sf) {
4568         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4569     }
4570     return true;
4571 }
4572 
4573 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4574 {
4575     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4576 
4577     tcg_rd = cpu_reg(s, a->rd);
4578 
4579     if (unlikely(a->imm == 0)) {
4580         /*
4581          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4582          * so an extract from bit 0 is a special case.
4583          */
4584         if (a->sf) {
4585             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4586         } else {
4587             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4588         }
4589     } else {
4590         tcg_rm = cpu_reg(s, a->rm);
4591         tcg_rn = cpu_reg(s, a->rn);
4592 
4593         if (a->sf) {
4594             /* Specialization to ROR happens in EXTRACT2.  */
4595             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4596         } else {
4597             TCGv_i32 t0 = tcg_temp_new_i32();
4598 
4599             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4600             if (a->rm == a->rn) {
4601                 tcg_gen_rotri_i32(t0, t0, a->imm);
4602             } else {
4603                 TCGv_i32 t1 = tcg_temp_new_i32();
4604                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4605                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4606             }
4607             tcg_gen_extu_i32_i64(tcg_rd, t0);
4608         }
4609     }
4610     return true;
4611 }
4612 
4613 /*
4614  * Cryptographic AES, SHA, SHA512
4615  */
4616 
4617 TRANS_FEAT(AESE, aa64_aes, do_gvec_op3_ool, a, 0, gen_helper_crypto_aese)
4618 TRANS_FEAT(AESD, aa64_aes, do_gvec_op3_ool, a, 0, gen_helper_crypto_aesd)
4619 TRANS_FEAT(AESMC, aa64_aes, do_gvec_op2_ool, a, 0, gen_helper_crypto_aesmc)
4620 TRANS_FEAT(AESIMC, aa64_aes, do_gvec_op2_ool, a, 0, gen_helper_crypto_aesimc)
4621 
4622 TRANS_FEAT(SHA1C, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1c)
4623 TRANS_FEAT(SHA1P, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1p)
4624 TRANS_FEAT(SHA1M, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1m)
4625 TRANS_FEAT(SHA1SU0, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1su0)
4626 
4627 TRANS_FEAT(SHA256H, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256h)
4628 TRANS_FEAT(SHA256H2, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256h2)
4629 TRANS_FEAT(SHA256SU1, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256su1)
4630 
4631 TRANS_FEAT(SHA1H, aa64_sha1, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha1h)
4632 TRANS_FEAT(SHA1SU1, aa64_sha1, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha1su1)
4633 TRANS_FEAT(SHA256SU0, aa64_sha256, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha256su0)
4634 
4635 TRANS_FEAT(SHA512H, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512h)
4636 TRANS_FEAT(SHA512H2, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512h2)
4637 TRANS_FEAT(SHA512SU1, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512su1)
4638 TRANS_FEAT(RAX1, aa64_sha3, do_gvec_fn3, a, gen_gvec_rax1)
4639 TRANS_FEAT(SM3PARTW1, aa64_sm3, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm3partw1)
4640 TRANS_FEAT(SM3PARTW2, aa64_sm3, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm3partw2)
4641 TRANS_FEAT(SM4EKEY, aa64_sm4, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm4ekey)
4642 
4643 TRANS_FEAT(SHA512SU0, aa64_sha512, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha512su0)
4644 TRANS_FEAT(SM4E, aa64_sm4, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm4e)
4645 
4646 TRANS_FEAT(EOR3, aa64_sha3, do_gvec_fn4, a, gen_gvec_eor3)
4647 TRANS_FEAT(BCAX, aa64_sha3, do_gvec_fn4, a, gen_gvec_bcax)
4648 
4649 static bool trans_SM3SS1(DisasContext *s, arg_SM3SS1 *a)
4650 {
4651     if (!dc_isar_feature(aa64_sm3, s)) {
4652         return false;
4653     }
4654     if (fp_access_check(s)) {
4655         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
4656         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
4657         TCGv_i32 tcg_op3 = tcg_temp_new_i32();
4658         TCGv_i32 tcg_res = tcg_temp_new_i32();
4659         unsigned vsz, dofs;
4660 
4661         read_vec_element_i32(s, tcg_op1, a->rn, 3, MO_32);
4662         read_vec_element_i32(s, tcg_op2, a->rm, 3, MO_32);
4663         read_vec_element_i32(s, tcg_op3, a->ra, 3, MO_32);
4664 
4665         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
4666         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
4667         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
4668         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
4669 
4670         /* Clear the whole register first, then store bits [127:96]. */
4671         vsz = vec_full_reg_size(s);
4672         dofs = vec_full_reg_offset(s, a->rd);
4673         tcg_gen_gvec_dup_imm(MO_64, dofs, vsz, vsz, 0);
4674         write_vec_element_i32(s, tcg_res, a->rd, 3, MO_32);
4675     }
4676     return true;
4677 }
4678 
4679 static bool do_crypto3i(DisasContext *s, arg_crypto3i *a, gen_helper_gvec_3 *fn)
4680 {
4681     if (fp_access_check(s)) {
4682         gen_gvec_op3_ool(s, true, a->rd, a->rn, a->rm, a->imm, fn);
4683     }
4684     return true;
4685 }
4686 TRANS_FEAT(SM3TT1A, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt1a)
4687 TRANS_FEAT(SM3TT1B, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt1b)
4688 TRANS_FEAT(SM3TT2A, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt2a)
4689 TRANS_FEAT(SM3TT2B, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt2b)
4690 
4691 static bool trans_XAR(DisasContext *s, arg_XAR *a)
4692 {
4693     if (!dc_isar_feature(aa64_sha3, s)) {
4694         return false;
4695     }
4696     if (fp_access_check(s)) {
4697         gen_gvec_xar(MO_64, vec_full_reg_offset(s, a->rd),
4698                      vec_full_reg_offset(s, a->rn),
4699                      vec_full_reg_offset(s, a->rm), a->imm, 16,
4700                      vec_full_reg_size(s));
4701     }
4702     return true;
4703 }
4704 
4705 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4706  * Note that it is the caller's responsibility to ensure that the
4707  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4708  * mandated semantics for out of range shifts.
4709  */
4710 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4711                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4712 {
4713     switch (shift_type) {
4714     case A64_SHIFT_TYPE_LSL:
4715         tcg_gen_shl_i64(dst, src, shift_amount);
4716         break;
4717     case A64_SHIFT_TYPE_LSR:
4718         tcg_gen_shr_i64(dst, src, shift_amount);
4719         break;
4720     case A64_SHIFT_TYPE_ASR:
4721         if (!sf) {
4722             tcg_gen_ext32s_i64(dst, src);
4723         }
4724         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4725         break;
4726     case A64_SHIFT_TYPE_ROR:
4727         if (sf) {
4728             tcg_gen_rotr_i64(dst, src, shift_amount);
4729         } else {
4730             TCGv_i32 t0, t1;
4731             t0 = tcg_temp_new_i32();
4732             t1 = tcg_temp_new_i32();
4733             tcg_gen_extrl_i64_i32(t0, src);
4734             tcg_gen_extrl_i64_i32(t1, shift_amount);
4735             tcg_gen_rotr_i32(t0, t0, t1);
4736             tcg_gen_extu_i32_i64(dst, t0);
4737         }
4738         break;
4739     default:
4740         assert(FALSE); /* all shift types should be handled */
4741         break;
4742     }
4743 
4744     if (!sf) { /* zero extend final result */
4745         tcg_gen_ext32u_i64(dst, dst);
4746     }
4747 }
4748 
4749 /* Shift a TCGv src by immediate, put result in dst.
4750  * The shift amount must be in range (this should always be true as the
4751  * relevant instructions will UNDEF on bad shift immediates).
4752  */
4753 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4754                           enum a64_shift_type shift_type, unsigned int shift_i)
4755 {
4756     assert(shift_i < (sf ? 64 : 32));
4757 
4758     if (shift_i == 0) {
4759         tcg_gen_mov_i64(dst, src);
4760     } else {
4761         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4762     }
4763 }
4764 
4765 /* Logical (shifted register)
4766  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4767  * +----+-----+-----------+-------+---+------+--------+------+------+
4768  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4769  * +----+-----+-----------+-------+---+------+--------+------+------+
4770  */
4771 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4772 {
4773     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4774     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4775 
4776     sf = extract32(insn, 31, 1);
4777     opc = extract32(insn, 29, 2);
4778     shift_type = extract32(insn, 22, 2);
4779     invert = extract32(insn, 21, 1);
4780     rm = extract32(insn, 16, 5);
4781     shift_amount = extract32(insn, 10, 6);
4782     rn = extract32(insn, 5, 5);
4783     rd = extract32(insn, 0, 5);
4784 
4785     if (!sf && (shift_amount & (1 << 5))) {
4786         unallocated_encoding(s);
4787         return;
4788     }
4789 
4790     tcg_rd = cpu_reg(s, rd);
4791 
4792     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4793         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4794          * register-register MOV and MVN, so it is worth special casing.
4795          */
4796         tcg_rm = cpu_reg(s, rm);
4797         if (invert) {
4798             tcg_gen_not_i64(tcg_rd, tcg_rm);
4799             if (!sf) {
4800                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4801             }
4802         } else {
4803             if (sf) {
4804                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4805             } else {
4806                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4807             }
4808         }
4809         return;
4810     }
4811 
4812     tcg_rm = read_cpu_reg(s, rm, sf);
4813 
4814     if (shift_amount) {
4815         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4816     }
4817 
4818     tcg_rn = cpu_reg(s, rn);
4819 
4820     switch (opc | (invert << 2)) {
4821     case 0: /* AND */
4822     case 3: /* ANDS */
4823         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4824         break;
4825     case 1: /* ORR */
4826         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4827         break;
4828     case 2: /* EOR */
4829         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4830         break;
4831     case 4: /* BIC */
4832     case 7: /* BICS */
4833         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4834         break;
4835     case 5: /* ORN */
4836         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4837         break;
4838     case 6: /* EON */
4839         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4840         break;
4841     default:
4842         assert(FALSE);
4843         break;
4844     }
4845 
4846     if (!sf) {
4847         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4848     }
4849 
4850     if (opc == 3) {
4851         gen_logic_CC(sf, tcg_rd);
4852     }
4853 }
4854 
4855 /*
4856  * Add/subtract (extended register)
4857  *
4858  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4859  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4860  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4861  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4862  *
4863  *  sf: 0 -> 32bit, 1 -> 64bit
4864  *  op: 0 -> add  , 1 -> sub
4865  *   S: 1 -> set flags
4866  * opt: 00
4867  * option: extension type (see DecodeRegExtend)
4868  * imm3: optional shift to Rm
4869  *
4870  * Rd = Rn + LSL(extend(Rm), amount)
4871  */
4872 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4873 {
4874     int rd = extract32(insn, 0, 5);
4875     int rn = extract32(insn, 5, 5);
4876     int imm3 = extract32(insn, 10, 3);
4877     int option = extract32(insn, 13, 3);
4878     int rm = extract32(insn, 16, 5);
4879     int opt = extract32(insn, 22, 2);
4880     bool setflags = extract32(insn, 29, 1);
4881     bool sub_op = extract32(insn, 30, 1);
4882     bool sf = extract32(insn, 31, 1);
4883 
4884     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4885     TCGv_i64 tcg_rd;
4886     TCGv_i64 tcg_result;
4887 
4888     if (imm3 > 4 || opt != 0) {
4889         unallocated_encoding(s);
4890         return;
4891     }
4892 
4893     /* non-flag setting ops may use SP */
4894     if (!setflags) {
4895         tcg_rd = cpu_reg_sp(s, rd);
4896     } else {
4897         tcg_rd = cpu_reg(s, rd);
4898     }
4899     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4900 
4901     tcg_rm = read_cpu_reg(s, rm, sf);
4902     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4903 
4904     tcg_result = tcg_temp_new_i64();
4905 
4906     if (!setflags) {
4907         if (sub_op) {
4908             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4909         } else {
4910             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4911         }
4912     } else {
4913         if (sub_op) {
4914             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4915         } else {
4916             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4917         }
4918     }
4919 
4920     if (sf) {
4921         tcg_gen_mov_i64(tcg_rd, tcg_result);
4922     } else {
4923         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4924     }
4925 }
4926 
4927 /*
4928  * Add/subtract (shifted register)
4929  *
4930  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4931  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4932  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4933  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4934  *
4935  *    sf: 0 -> 32bit, 1 -> 64bit
4936  *    op: 0 -> add  , 1 -> sub
4937  *     S: 1 -> set flags
4938  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4939  *  imm6: Shift amount to apply to Rm before the add/sub
4940  */
4941 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4942 {
4943     int rd = extract32(insn, 0, 5);
4944     int rn = extract32(insn, 5, 5);
4945     int imm6 = extract32(insn, 10, 6);
4946     int rm = extract32(insn, 16, 5);
4947     int shift_type = extract32(insn, 22, 2);
4948     bool setflags = extract32(insn, 29, 1);
4949     bool sub_op = extract32(insn, 30, 1);
4950     bool sf = extract32(insn, 31, 1);
4951 
4952     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4953     TCGv_i64 tcg_rn, tcg_rm;
4954     TCGv_i64 tcg_result;
4955 
4956     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4957         unallocated_encoding(s);
4958         return;
4959     }
4960 
4961     tcg_rn = read_cpu_reg(s, rn, sf);
4962     tcg_rm = read_cpu_reg(s, rm, sf);
4963 
4964     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4965 
4966     tcg_result = tcg_temp_new_i64();
4967 
4968     if (!setflags) {
4969         if (sub_op) {
4970             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4971         } else {
4972             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4973         }
4974     } else {
4975         if (sub_op) {
4976             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4977         } else {
4978             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4979         }
4980     }
4981 
4982     if (sf) {
4983         tcg_gen_mov_i64(tcg_rd, tcg_result);
4984     } else {
4985         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4986     }
4987 }
4988 
4989 /* Data-processing (3 source)
4990  *
4991  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4992  *  +--+------+-----------+------+------+----+------+------+------+
4993  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4994  *  +--+------+-----------+------+------+----+------+------+------+
4995  */
4996 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4997 {
4998     int rd = extract32(insn, 0, 5);
4999     int rn = extract32(insn, 5, 5);
5000     int ra = extract32(insn, 10, 5);
5001     int rm = extract32(insn, 16, 5);
5002     int op_id = (extract32(insn, 29, 3) << 4) |
5003         (extract32(insn, 21, 3) << 1) |
5004         extract32(insn, 15, 1);
5005     bool sf = extract32(insn, 31, 1);
5006     bool is_sub = extract32(op_id, 0, 1);
5007     bool is_high = extract32(op_id, 2, 1);
5008     bool is_signed = false;
5009     TCGv_i64 tcg_op1;
5010     TCGv_i64 tcg_op2;
5011     TCGv_i64 tcg_tmp;
5012 
5013     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5014     switch (op_id) {
5015     case 0x42: /* SMADDL */
5016     case 0x43: /* SMSUBL */
5017     case 0x44: /* SMULH */
5018         is_signed = true;
5019         break;
5020     case 0x0: /* MADD (32bit) */
5021     case 0x1: /* MSUB (32bit) */
5022     case 0x40: /* MADD (64bit) */
5023     case 0x41: /* MSUB (64bit) */
5024     case 0x4a: /* UMADDL */
5025     case 0x4b: /* UMSUBL */
5026     case 0x4c: /* UMULH */
5027         break;
5028     default:
5029         unallocated_encoding(s);
5030         return;
5031     }
5032 
5033     if (is_high) {
5034         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5035         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5036         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5037         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5038 
5039         if (is_signed) {
5040             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5041         } else {
5042             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5043         }
5044         return;
5045     }
5046 
5047     tcg_op1 = tcg_temp_new_i64();
5048     tcg_op2 = tcg_temp_new_i64();
5049     tcg_tmp = tcg_temp_new_i64();
5050 
5051     if (op_id < 0x42) {
5052         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5053         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5054     } else {
5055         if (is_signed) {
5056             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5057             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5058         } else {
5059             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5060             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5061         }
5062     }
5063 
5064     if (ra == 31 && !is_sub) {
5065         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5066         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5067     } else {
5068         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5069         if (is_sub) {
5070             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5071         } else {
5072             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5073         }
5074     }
5075 
5076     if (!sf) {
5077         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5078     }
5079 }
5080 
5081 /* Add/subtract (with carry)
5082  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5083  * +--+--+--+------------------------+------+-------------+------+-----+
5084  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5085  * +--+--+--+------------------------+------+-------------+------+-----+
5086  */
5087 
5088 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5089 {
5090     unsigned int sf, op, setflags, rm, rn, rd;
5091     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5092 
5093     sf = extract32(insn, 31, 1);
5094     op = extract32(insn, 30, 1);
5095     setflags = extract32(insn, 29, 1);
5096     rm = extract32(insn, 16, 5);
5097     rn = extract32(insn, 5, 5);
5098     rd = extract32(insn, 0, 5);
5099 
5100     tcg_rd = cpu_reg(s, rd);
5101     tcg_rn = cpu_reg(s, rn);
5102 
5103     if (op) {
5104         tcg_y = tcg_temp_new_i64();
5105         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5106     } else {
5107         tcg_y = cpu_reg(s, rm);
5108     }
5109 
5110     if (setflags) {
5111         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5112     } else {
5113         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5114     }
5115 }
5116 
5117 /*
5118  * Rotate right into flags
5119  *  31 30 29                21       15          10      5  4      0
5120  * +--+--+--+-----------------+--------+-----------+------+--+------+
5121  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5122  * +--+--+--+-----------------+--------+-----------+------+--+------+
5123  */
5124 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5125 {
5126     int mask = extract32(insn, 0, 4);
5127     int o2 = extract32(insn, 4, 1);
5128     int rn = extract32(insn, 5, 5);
5129     int imm6 = extract32(insn, 15, 6);
5130     int sf_op_s = extract32(insn, 29, 3);
5131     TCGv_i64 tcg_rn;
5132     TCGv_i32 nzcv;
5133 
5134     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5135         unallocated_encoding(s);
5136         return;
5137     }
5138 
5139     tcg_rn = read_cpu_reg(s, rn, 1);
5140     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5141 
5142     nzcv = tcg_temp_new_i32();
5143     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5144 
5145     if (mask & 8) { /* N */
5146         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5147     }
5148     if (mask & 4) { /* Z */
5149         tcg_gen_not_i32(cpu_ZF, nzcv);
5150         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5151     }
5152     if (mask & 2) { /* C */
5153         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5154     }
5155     if (mask & 1) { /* V */
5156         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5157     }
5158 }
5159 
5160 /*
5161  * Evaluate into flags
5162  *  31 30 29                21        15   14        10      5  4      0
5163  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5164  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5165  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5166  */
5167 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5168 {
5169     int o3_mask = extract32(insn, 0, 5);
5170     int rn = extract32(insn, 5, 5);
5171     int o2 = extract32(insn, 15, 6);
5172     int sz = extract32(insn, 14, 1);
5173     int sf_op_s = extract32(insn, 29, 3);
5174     TCGv_i32 tmp;
5175     int shift;
5176 
5177     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5178         !dc_isar_feature(aa64_condm_4, s)) {
5179         unallocated_encoding(s);
5180         return;
5181     }
5182     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5183 
5184     tmp = tcg_temp_new_i32();
5185     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5186     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5187     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5188     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5189     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5190 }
5191 
5192 /* Conditional compare (immediate / register)
5193  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5194  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5195  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5196  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5197  *        [1]                             y                [0]       [0]
5198  */
5199 static void disas_cc(DisasContext *s, uint32_t insn)
5200 {
5201     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5202     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5203     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5204     DisasCompare c;
5205 
5206     if (!extract32(insn, 29, 1)) {
5207         unallocated_encoding(s);
5208         return;
5209     }
5210     if (insn & (1 << 10 | 1 << 4)) {
5211         unallocated_encoding(s);
5212         return;
5213     }
5214     sf = extract32(insn, 31, 1);
5215     op = extract32(insn, 30, 1);
5216     is_imm = extract32(insn, 11, 1);
5217     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5218     cond = extract32(insn, 12, 4);
5219     rn = extract32(insn, 5, 5);
5220     nzcv = extract32(insn, 0, 4);
5221 
5222     /* Set T0 = !COND.  */
5223     tcg_t0 = tcg_temp_new_i32();
5224     arm_test_cc(&c, cond);
5225     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5226 
5227     /* Load the arguments for the new comparison.  */
5228     if (is_imm) {
5229         tcg_y = tcg_temp_new_i64();
5230         tcg_gen_movi_i64(tcg_y, y);
5231     } else {
5232         tcg_y = cpu_reg(s, y);
5233     }
5234     tcg_rn = cpu_reg(s, rn);
5235 
5236     /* Set the flags for the new comparison.  */
5237     tcg_tmp = tcg_temp_new_i64();
5238     if (op) {
5239         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5240     } else {
5241         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5242     }
5243 
5244     /* If COND was false, force the flags to #nzcv.  Compute two masks
5245      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5246      * For tcg hosts that support ANDC, we can make do with just T1.
5247      * In either case, allow the tcg optimizer to delete any unused mask.
5248      */
5249     tcg_t1 = tcg_temp_new_i32();
5250     tcg_t2 = tcg_temp_new_i32();
5251     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5252     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5253 
5254     if (nzcv & 8) { /* N */
5255         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5256     } else {
5257         if (TCG_TARGET_HAS_andc_i32) {
5258             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5259         } else {
5260             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5261         }
5262     }
5263     if (nzcv & 4) { /* Z */
5264         if (TCG_TARGET_HAS_andc_i32) {
5265             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5266         } else {
5267             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5268         }
5269     } else {
5270         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5271     }
5272     if (nzcv & 2) { /* C */
5273         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5274     } else {
5275         if (TCG_TARGET_HAS_andc_i32) {
5276             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5277         } else {
5278             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5279         }
5280     }
5281     if (nzcv & 1) { /* V */
5282         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5283     } else {
5284         if (TCG_TARGET_HAS_andc_i32) {
5285             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5286         } else {
5287             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5288         }
5289     }
5290 }
5291 
5292 /* Conditional select
5293  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5294  * +----+----+---+-----------------+------+------+-----+------+------+
5295  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5296  * +----+----+---+-----------------+------+------+-----+------+------+
5297  */
5298 static void disas_cond_select(DisasContext *s, uint32_t insn)
5299 {
5300     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5301     TCGv_i64 tcg_rd, zero;
5302     DisasCompare64 c;
5303 
5304     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5305         /* S == 1 or op2<1> == 1 */
5306         unallocated_encoding(s);
5307         return;
5308     }
5309     sf = extract32(insn, 31, 1);
5310     else_inv = extract32(insn, 30, 1);
5311     rm = extract32(insn, 16, 5);
5312     cond = extract32(insn, 12, 4);
5313     else_inc = extract32(insn, 10, 1);
5314     rn = extract32(insn, 5, 5);
5315     rd = extract32(insn, 0, 5);
5316 
5317     tcg_rd = cpu_reg(s, rd);
5318 
5319     a64_test_cc(&c, cond);
5320     zero = tcg_constant_i64(0);
5321 
5322     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5323         /* CSET & CSETM.  */
5324         if (else_inv) {
5325             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
5326                                    tcg_rd, c.value, zero);
5327         } else {
5328             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
5329                                 tcg_rd, c.value, zero);
5330         }
5331     } else {
5332         TCGv_i64 t_true = cpu_reg(s, rn);
5333         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5334         if (else_inv && else_inc) {
5335             tcg_gen_neg_i64(t_false, t_false);
5336         } else if (else_inv) {
5337             tcg_gen_not_i64(t_false, t_false);
5338         } else if (else_inc) {
5339             tcg_gen_addi_i64(t_false, t_false, 1);
5340         }
5341         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5342     }
5343 
5344     if (!sf) {
5345         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5346     }
5347 }
5348 
5349 static void handle_clz(DisasContext *s, unsigned int sf,
5350                        unsigned int rn, unsigned int rd)
5351 {
5352     TCGv_i64 tcg_rd, tcg_rn;
5353     tcg_rd = cpu_reg(s, rd);
5354     tcg_rn = cpu_reg(s, rn);
5355 
5356     if (sf) {
5357         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5358     } else {
5359         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5360         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5361         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5362         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5363     }
5364 }
5365 
5366 static void handle_cls(DisasContext *s, unsigned int sf,
5367                        unsigned int rn, unsigned int rd)
5368 {
5369     TCGv_i64 tcg_rd, tcg_rn;
5370     tcg_rd = cpu_reg(s, rd);
5371     tcg_rn = cpu_reg(s, rn);
5372 
5373     if (sf) {
5374         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5375     } else {
5376         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5377         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5378         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5379         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5380     }
5381 }
5382 
5383 static void handle_rbit(DisasContext *s, unsigned int sf,
5384                         unsigned int rn, unsigned int rd)
5385 {
5386     TCGv_i64 tcg_rd, tcg_rn;
5387     tcg_rd = cpu_reg(s, rd);
5388     tcg_rn = cpu_reg(s, rn);
5389 
5390     if (sf) {
5391         gen_helper_rbit64(tcg_rd, tcg_rn);
5392     } else {
5393         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5394         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5395         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5396         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5397     }
5398 }
5399 
5400 /* REV with sf==1, opcode==3 ("REV64") */
5401 static void handle_rev64(DisasContext *s, unsigned int sf,
5402                          unsigned int rn, unsigned int rd)
5403 {
5404     if (!sf) {
5405         unallocated_encoding(s);
5406         return;
5407     }
5408     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5409 }
5410 
5411 /* REV with sf==0, opcode==2
5412  * REV32 (sf==1, opcode==2)
5413  */
5414 static void handle_rev32(DisasContext *s, unsigned int sf,
5415                          unsigned int rn, unsigned int rd)
5416 {
5417     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5418     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5419 
5420     if (sf) {
5421         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5422         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5423     } else {
5424         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5425     }
5426 }
5427 
5428 /* REV16 (opcode==1) */
5429 static void handle_rev16(DisasContext *s, unsigned int sf,
5430                          unsigned int rn, unsigned int rd)
5431 {
5432     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5433     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5434     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5435     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5436 
5437     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5438     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5439     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5440     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5441     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5442 }
5443 
5444 /* Data-processing (1 source)
5445  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5446  * +----+---+---+-----------------+---------+--------+------+------+
5447  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5448  * +----+---+---+-----------------+---------+--------+------+------+
5449  */
5450 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5451 {
5452     unsigned int sf, opcode, opcode2, rn, rd;
5453     TCGv_i64 tcg_rd;
5454 
5455     if (extract32(insn, 29, 1)) {
5456         unallocated_encoding(s);
5457         return;
5458     }
5459 
5460     sf = extract32(insn, 31, 1);
5461     opcode = extract32(insn, 10, 6);
5462     opcode2 = extract32(insn, 16, 5);
5463     rn = extract32(insn, 5, 5);
5464     rd = extract32(insn, 0, 5);
5465 
5466 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5467 
5468     switch (MAP(sf, opcode2, opcode)) {
5469     case MAP(0, 0x00, 0x00): /* RBIT */
5470     case MAP(1, 0x00, 0x00):
5471         handle_rbit(s, sf, rn, rd);
5472         break;
5473     case MAP(0, 0x00, 0x01): /* REV16 */
5474     case MAP(1, 0x00, 0x01):
5475         handle_rev16(s, sf, rn, rd);
5476         break;
5477     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5478     case MAP(1, 0x00, 0x02):
5479         handle_rev32(s, sf, rn, rd);
5480         break;
5481     case MAP(1, 0x00, 0x03): /* REV64 */
5482         handle_rev64(s, sf, rn, rd);
5483         break;
5484     case MAP(0, 0x00, 0x04): /* CLZ */
5485     case MAP(1, 0x00, 0x04):
5486         handle_clz(s, sf, rn, rd);
5487         break;
5488     case MAP(0, 0x00, 0x05): /* CLS */
5489     case MAP(1, 0x00, 0x05):
5490         handle_cls(s, sf, rn, rd);
5491         break;
5492     case MAP(1, 0x01, 0x00): /* PACIA */
5493         if (s->pauth_active) {
5494             tcg_rd = cpu_reg(s, rd);
5495             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5496         } else if (!dc_isar_feature(aa64_pauth, s)) {
5497             goto do_unallocated;
5498         }
5499         break;
5500     case MAP(1, 0x01, 0x01): /* PACIB */
5501         if (s->pauth_active) {
5502             tcg_rd = cpu_reg(s, rd);
5503             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5504         } else if (!dc_isar_feature(aa64_pauth, s)) {
5505             goto do_unallocated;
5506         }
5507         break;
5508     case MAP(1, 0x01, 0x02): /* PACDA */
5509         if (s->pauth_active) {
5510             tcg_rd = cpu_reg(s, rd);
5511             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5512         } else if (!dc_isar_feature(aa64_pauth, s)) {
5513             goto do_unallocated;
5514         }
5515         break;
5516     case MAP(1, 0x01, 0x03): /* PACDB */
5517         if (s->pauth_active) {
5518             tcg_rd = cpu_reg(s, rd);
5519             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5520         } else if (!dc_isar_feature(aa64_pauth, s)) {
5521             goto do_unallocated;
5522         }
5523         break;
5524     case MAP(1, 0x01, 0x04): /* AUTIA */
5525         if (s->pauth_active) {
5526             tcg_rd = cpu_reg(s, rd);
5527             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5528         } else if (!dc_isar_feature(aa64_pauth, s)) {
5529             goto do_unallocated;
5530         }
5531         break;
5532     case MAP(1, 0x01, 0x05): /* AUTIB */
5533         if (s->pauth_active) {
5534             tcg_rd = cpu_reg(s, rd);
5535             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5536         } else if (!dc_isar_feature(aa64_pauth, s)) {
5537             goto do_unallocated;
5538         }
5539         break;
5540     case MAP(1, 0x01, 0x06): /* AUTDA */
5541         if (s->pauth_active) {
5542             tcg_rd = cpu_reg(s, rd);
5543             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5544         } else if (!dc_isar_feature(aa64_pauth, s)) {
5545             goto do_unallocated;
5546         }
5547         break;
5548     case MAP(1, 0x01, 0x07): /* AUTDB */
5549         if (s->pauth_active) {
5550             tcg_rd = cpu_reg(s, rd);
5551             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5552         } else if (!dc_isar_feature(aa64_pauth, s)) {
5553             goto do_unallocated;
5554         }
5555         break;
5556     case MAP(1, 0x01, 0x08): /* PACIZA */
5557         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5558             goto do_unallocated;
5559         } else if (s->pauth_active) {
5560             tcg_rd = cpu_reg(s, rd);
5561             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5562         }
5563         break;
5564     case MAP(1, 0x01, 0x09): /* PACIZB */
5565         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5566             goto do_unallocated;
5567         } else if (s->pauth_active) {
5568             tcg_rd = cpu_reg(s, rd);
5569             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5570         }
5571         break;
5572     case MAP(1, 0x01, 0x0a): /* PACDZA */
5573         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5574             goto do_unallocated;
5575         } else if (s->pauth_active) {
5576             tcg_rd = cpu_reg(s, rd);
5577             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5578         }
5579         break;
5580     case MAP(1, 0x01, 0x0b): /* PACDZB */
5581         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5582             goto do_unallocated;
5583         } else if (s->pauth_active) {
5584             tcg_rd = cpu_reg(s, rd);
5585             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5586         }
5587         break;
5588     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5589         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5590             goto do_unallocated;
5591         } else if (s->pauth_active) {
5592             tcg_rd = cpu_reg(s, rd);
5593             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5594         }
5595         break;
5596     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5597         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5598             goto do_unallocated;
5599         } else if (s->pauth_active) {
5600             tcg_rd = cpu_reg(s, rd);
5601             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5602         }
5603         break;
5604     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5605         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5606             goto do_unallocated;
5607         } else if (s->pauth_active) {
5608             tcg_rd = cpu_reg(s, rd);
5609             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5610         }
5611         break;
5612     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5613         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5614             goto do_unallocated;
5615         } else if (s->pauth_active) {
5616             tcg_rd = cpu_reg(s, rd);
5617             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5618         }
5619         break;
5620     case MAP(1, 0x01, 0x10): /* XPACI */
5621         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5622             goto do_unallocated;
5623         } else if (s->pauth_active) {
5624             tcg_rd = cpu_reg(s, rd);
5625             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
5626         }
5627         break;
5628     case MAP(1, 0x01, 0x11): /* XPACD */
5629         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5630             goto do_unallocated;
5631         } else if (s->pauth_active) {
5632             tcg_rd = cpu_reg(s, rd);
5633             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
5634         }
5635         break;
5636     default:
5637     do_unallocated:
5638         unallocated_encoding(s);
5639         break;
5640     }
5641 
5642 #undef MAP
5643 }
5644 
5645 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5646                        unsigned int rm, unsigned int rn, unsigned int rd)
5647 {
5648     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5649     tcg_rd = cpu_reg(s, rd);
5650 
5651     if (!sf && is_signed) {
5652         tcg_n = tcg_temp_new_i64();
5653         tcg_m = tcg_temp_new_i64();
5654         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5655         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5656     } else {
5657         tcg_n = read_cpu_reg(s, rn, sf);
5658         tcg_m = read_cpu_reg(s, rm, sf);
5659     }
5660 
5661     if (is_signed) {
5662         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5663     } else {
5664         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5665     }
5666 
5667     if (!sf) { /* zero extend final result */
5668         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5669     }
5670 }
5671 
5672 /* LSLV, LSRV, ASRV, RORV */
5673 static void handle_shift_reg(DisasContext *s,
5674                              enum a64_shift_type shift_type, unsigned int sf,
5675                              unsigned int rm, unsigned int rn, unsigned int rd)
5676 {
5677     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5678     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5679     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5680 
5681     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5682     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5683 }
5684 
5685 /* CRC32[BHWX], CRC32C[BHWX] */
5686 static void handle_crc32(DisasContext *s,
5687                          unsigned int sf, unsigned int sz, bool crc32c,
5688                          unsigned int rm, unsigned int rn, unsigned int rd)
5689 {
5690     TCGv_i64 tcg_acc, tcg_val;
5691     TCGv_i32 tcg_bytes;
5692 
5693     if (!dc_isar_feature(aa64_crc32, s)
5694         || (sf == 1 && sz != 3)
5695         || (sf == 0 && sz == 3)) {
5696         unallocated_encoding(s);
5697         return;
5698     }
5699 
5700     if (sz == 3) {
5701         tcg_val = cpu_reg(s, rm);
5702     } else {
5703         uint64_t mask;
5704         switch (sz) {
5705         case 0:
5706             mask = 0xFF;
5707             break;
5708         case 1:
5709             mask = 0xFFFF;
5710             break;
5711         case 2:
5712             mask = 0xFFFFFFFF;
5713             break;
5714         default:
5715             g_assert_not_reached();
5716         }
5717         tcg_val = tcg_temp_new_i64();
5718         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5719     }
5720 
5721     tcg_acc = cpu_reg(s, rn);
5722     tcg_bytes = tcg_constant_i32(1 << sz);
5723 
5724     if (crc32c) {
5725         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5726     } else {
5727         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5728     }
5729 }
5730 
5731 /* Data-processing (2 source)
5732  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5733  * +----+---+---+-----------------+------+--------+------+------+
5734  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5735  * +----+---+---+-----------------+------+--------+------+------+
5736  */
5737 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5738 {
5739     unsigned int sf, rm, opcode, rn, rd, setflag;
5740     sf = extract32(insn, 31, 1);
5741     setflag = extract32(insn, 29, 1);
5742     rm = extract32(insn, 16, 5);
5743     opcode = extract32(insn, 10, 6);
5744     rn = extract32(insn, 5, 5);
5745     rd = extract32(insn, 0, 5);
5746 
5747     if (setflag && opcode != 0) {
5748         unallocated_encoding(s);
5749         return;
5750     }
5751 
5752     switch (opcode) {
5753     case 0: /* SUBP(S) */
5754         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5755             goto do_unallocated;
5756         } else {
5757             TCGv_i64 tcg_n, tcg_m, tcg_d;
5758 
5759             tcg_n = read_cpu_reg_sp(s, rn, true);
5760             tcg_m = read_cpu_reg_sp(s, rm, true);
5761             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5762             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5763             tcg_d = cpu_reg(s, rd);
5764 
5765             if (setflag) {
5766                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5767             } else {
5768                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5769             }
5770         }
5771         break;
5772     case 2: /* UDIV */
5773         handle_div(s, false, sf, rm, rn, rd);
5774         break;
5775     case 3: /* SDIV */
5776         handle_div(s, true, sf, rm, rn, rd);
5777         break;
5778     case 4: /* IRG */
5779         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5780             goto do_unallocated;
5781         }
5782         if (s->ata[0]) {
5783             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
5784                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5785         } else {
5786             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5787                                              cpu_reg_sp(s, rn));
5788         }
5789         break;
5790     case 5: /* GMI */
5791         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5792             goto do_unallocated;
5793         } else {
5794             TCGv_i64 t = tcg_temp_new_i64();
5795 
5796             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5797             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5798             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5799         }
5800         break;
5801     case 8: /* LSLV */
5802         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5803         break;
5804     case 9: /* LSRV */
5805         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5806         break;
5807     case 10: /* ASRV */
5808         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5809         break;
5810     case 11: /* RORV */
5811         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5812         break;
5813     case 12: /* PACGA */
5814         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5815             goto do_unallocated;
5816         }
5817         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
5818                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5819         break;
5820     case 16:
5821     case 17:
5822     case 18:
5823     case 19:
5824     case 20:
5825     case 21:
5826     case 22:
5827     case 23: /* CRC32 */
5828     {
5829         int sz = extract32(opcode, 0, 2);
5830         bool crc32c = extract32(opcode, 2, 1);
5831         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5832         break;
5833     }
5834     default:
5835     do_unallocated:
5836         unallocated_encoding(s);
5837         break;
5838     }
5839 }
5840 
5841 /*
5842  * Data processing - register
5843  *  31  30 29  28      25    21  20  16      10         0
5844  * +--+---+--+---+-------+-----+-------+-------+---------+
5845  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5846  * +--+---+--+---+-------+-----+-------+-------+---------+
5847  */
5848 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5849 {
5850     int op0 = extract32(insn, 30, 1);
5851     int op1 = extract32(insn, 28, 1);
5852     int op2 = extract32(insn, 21, 4);
5853     int op3 = extract32(insn, 10, 6);
5854 
5855     if (!op1) {
5856         if (op2 & 8) {
5857             if (op2 & 1) {
5858                 /* Add/sub (extended register) */
5859                 disas_add_sub_ext_reg(s, insn);
5860             } else {
5861                 /* Add/sub (shifted register) */
5862                 disas_add_sub_reg(s, insn);
5863             }
5864         } else {
5865             /* Logical (shifted register) */
5866             disas_logic_reg(s, insn);
5867         }
5868         return;
5869     }
5870 
5871     switch (op2) {
5872     case 0x0:
5873         switch (op3) {
5874         case 0x00: /* Add/subtract (with carry) */
5875             disas_adc_sbc(s, insn);
5876             break;
5877 
5878         case 0x01: /* Rotate right into flags */
5879         case 0x21:
5880             disas_rotate_right_into_flags(s, insn);
5881             break;
5882 
5883         case 0x02: /* Evaluate into flags */
5884         case 0x12:
5885         case 0x22:
5886         case 0x32:
5887             disas_evaluate_into_flags(s, insn);
5888             break;
5889 
5890         default:
5891             goto do_unallocated;
5892         }
5893         break;
5894 
5895     case 0x2: /* Conditional compare */
5896         disas_cc(s, insn); /* both imm and reg forms */
5897         break;
5898 
5899     case 0x4: /* Conditional select */
5900         disas_cond_select(s, insn);
5901         break;
5902 
5903     case 0x6: /* Data-processing */
5904         if (op0) {    /* (1 source) */
5905             disas_data_proc_1src(s, insn);
5906         } else {      /* (2 source) */
5907             disas_data_proc_2src(s, insn);
5908         }
5909         break;
5910     case 0x8 ... 0xf: /* (3 source) */
5911         disas_data_proc_3src(s, insn);
5912         break;
5913 
5914     default:
5915     do_unallocated:
5916         unallocated_encoding(s);
5917         break;
5918     }
5919 }
5920 
5921 static void handle_fp_compare(DisasContext *s, int size,
5922                               unsigned int rn, unsigned int rm,
5923                               bool cmp_with_zero, bool signal_all_nans)
5924 {
5925     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5926     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5927 
5928     if (size == MO_64) {
5929         TCGv_i64 tcg_vn, tcg_vm;
5930 
5931         tcg_vn = read_fp_dreg(s, rn);
5932         if (cmp_with_zero) {
5933             tcg_vm = tcg_constant_i64(0);
5934         } else {
5935             tcg_vm = read_fp_dreg(s, rm);
5936         }
5937         if (signal_all_nans) {
5938             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5939         } else {
5940             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5941         }
5942     } else {
5943         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5944         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5945 
5946         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5947         if (cmp_with_zero) {
5948             tcg_gen_movi_i32(tcg_vm, 0);
5949         } else {
5950             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5951         }
5952 
5953         switch (size) {
5954         case MO_32:
5955             if (signal_all_nans) {
5956                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5957             } else {
5958                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5959             }
5960             break;
5961         case MO_16:
5962             if (signal_all_nans) {
5963                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5964             } else {
5965                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5966             }
5967             break;
5968         default:
5969             g_assert_not_reached();
5970         }
5971     }
5972 
5973     gen_set_nzcv(tcg_flags);
5974 }
5975 
5976 /* Floating point compare
5977  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5978  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5979  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5980  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5981  */
5982 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5983 {
5984     unsigned int mos, type, rm, op, rn, opc, op2r;
5985     int size;
5986 
5987     mos = extract32(insn, 29, 3);
5988     type = extract32(insn, 22, 2);
5989     rm = extract32(insn, 16, 5);
5990     op = extract32(insn, 14, 2);
5991     rn = extract32(insn, 5, 5);
5992     opc = extract32(insn, 3, 2);
5993     op2r = extract32(insn, 0, 3);
5994 
5995     if (mos || op || op2r) {
5996         unallocated_encoding(s);
5997         return;
5998     }
5999 
6000     switch (type) {
6001     case 0:
6002         size = MO_32;
6003         break;
6004     case 1:
6005         size = MO_64;
6006         break;
6007     case 3:
6008         size = MO_16;
6009         if (dc_isar_feature(aa64_fp16, s)) {
6010             break;
6011         }
6012         /* fallthru */
6013     default:
6014         unallocated_encoding(s);
6015         return;
6016     }
6017 
6018     if (!fp_access_check(s)) {
6019         return;
6020     }
6021 
6022     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6023 }
6024 
6025 /* Floating point conditional compare
6026  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6027  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6028  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6029  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6030  */
6031 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6032 {
6033     unsigned int mos, type, rm, cond, rn, op, nzcv;
6034     TCGLabel *label_continue = NULL;
6035     int size;
6036 
6037     mos = extract32(insn, 29, 3);
6038     type = extract32(insn, 22, 2);
6039     rm = extract32(insn, 16, 5);
6040     cond = extract32(insn, 12, 4);
6041     rn = extract32(insn, 5, 5);
6042     op = extract32(insn, 4, 1);
6043     nzcv = extract32(insn, 0, 4);
6044 
6045     if (mos) {
6046         unallocated_encoding(s);
6047         return;
6048     }
6049 
6050     switch (type) {
6051     case 0:
6052         size = MO_32;
6053         break;
6054     case 1:
6055         size = MO_64;
6056         break;
6057     case 3:
6058         size = MO_16;
6059         if (dc_isar_feature(aa64_fp16, s)) {
6060             break;
6061         }
6062         /* fallthru */
6063     default:
6064         unallocated_encoding(s);
6065         return;
6066     }
6067 
6068     if (!fp_access_check(s)) {
6069         return;
6070     }
6071 
6072     if (cond < 0x0e) { /* not always */
6073         TCGLabel *label_match = gen_new_label();
6074         label_continue = gen_new_label();
6075         arm_gen_test_cc(cond, label_match);
6076         /* nomatch: */
6077         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6078         tcg_gen_br(label_continue);
6079         gen_set_label(label_match);
6080     }
6081 
6082     handle_fp_compare(s, size, rn, rm, false, op);
6083 
6084     if (cond < 0x0e) {
6085         gen_set_label(label_continue);
6086     }
6087 }
6088 
6089 /* Floating point conditional select
6090  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6091  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6092  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6093  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6094  */
6095 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6096 {
6097     unsigned int mos, type, rm, cond, rn, rd;
6098     TCGv_i64 t_true, t_false;
6099     DisasCompare64 c;
6100     MemOp sz;
6101 
6102     mos = extract32(insn, 29, 3);
6103     type = extract32(insn, 22, 2);
6104     rm = extract32(insn, 16, 5);
6105     cond = extract32(insn, 12, 4);
6106     rn = extract32(insn, 5, 5);
6107     rd = extract32(insn, 0, 5);
6108 
6109     if (mos) {
6110         unallocated_encoding(s);
6111         return;
6112     }
6113 
6114     switch (type) {
6115     case 0:
6116         sz = MO_32;
6117         break;
6118     case 1:
6119         sz = MO_64;
6120         break;
6121     case 3:
6122         sz = MO_16;
6123         if (dc_isar_feature(aa64_fp16, s)) {
6124             break;
6125         }
6126         /* fallthru */
6127     default:
6128         unallocated_encoding(s);
6129         return;
6130     }
6131 
6132     if (!fp_access_check(s)) {
6133         return;
6134     }
6135 
6136     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6137     t_true = tcg_temp_new_i64();
6138     t_false = tcg_temp_new_i64();
6139     read_vec_element(s, t_true, rn, 0, sz);
6140     read_vec_element(s, t_false, rm, 0, sz);
6141 
6142     a64_test_cc(&c, cond);
6143     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6144                         t_true, t_false);
6145 
6146     /* Note that sregs & hregs write back zeros to the high bits,
6147        and we've already done the zero-extension.  */
6148     write_fp_dreg(s, rd, t_true);
6149 }
6150 
6151 /* Floating-point data-processing (1 source) - half precision */
6152 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6153 {
6154     TCGv_ptr fpst = NULL;
6155     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6156     TCGv_i32 tcg_res = tcg_temp_new_i32();
6157 
6158     switch (opcode) {
6159     case 0x0: /* FMOV */
6160         tcg_gen_mov_i32(tcg_res, tcg_op);
6161         break;
6162     case 0x1: /* FABS */
6163         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6164         break;
6165     case 0x2: /* FNEG */
6166         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6167         break;
6168     case 0x3: /* FSQRT */
6169         fpst = fpstatus_ptr(FPST_FPCR_F16);
6170         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6171         break;
6172     case 0x8: /* FRINTN */
6173     case 0x9: /* FRINTP */
6174     case 0xa: /* FRINTM */
6175     case 0xb: /* FRINTZ */
6176     case 0xc: /* FRINTA */
6177     {
6178         TCGv_i32 tcg_rmode;
6179 
6180         fpst = fpstatus_ptr(FPST_FPCR_F16);
6181         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6182         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6183         gen_restore_rmode(tcg_rmode, fpst);
6184         break;
6185     }
6186     case 0xe: /* FRINTX */
6187         fpst = fpstatus_ptr(FPST_FPCR_F16);
6188         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6189         break;
6190     case 0xf: /* FRINTI */
6191         fpst = fpstatus_ptr(FPST_FPCR_F16);
6192         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6193         break;
6194     default:
6195         g_assert_not_reached();
6196     }
6197 
6198     write_fp_sreg(s, rd, tcg_res);
6199 }
6200 
6201 /* Floating-point data-processing (1 source) - single precision */
6202 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6203 {
6204     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6205     TCGv_i32 tcg_op, tcg_res;
6206     TCGv_ptr fpst;
6207     int rmode = -1;
6208 
6209     tcg_op = read_fp_sreg(s, rn);
6210     tcg_res = tcg_temp_new_i32();
6211 
6212     switch (opcode) {
6213     case 0x0: /* FMOV */
6214         tcg_gen_mov_i32(tcg_res, tcg_op);
6215         goto done;
6216     case 0x1: /* FABS */
6217         gen_helper_vfp_abss(tcg_res, tcg_op);
6218         goto done;
6219     case 0x2: /* FNEG */
6220         gen_helper_vfp_negs(tcg_res, tcg_op);
6221         goto done;
6222     case 0x3: /* FSQRT */
6223         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
6224         goto done;
6225     case 0x6: /* BFCVT */
6226         gen_fpst = gen_helper_bfcvt;
6227         break;
6228     case 0x8: /* FRINTN */
6229     case 0x9: /* FRINTP */
6230     case 0xa: /* FRINTM */
6231     case 0xb: /* FRINTZ */
6232     case 0xc: /* FRINTA */
6233         rmode = opcode & 7;
6234         gen_fpst = gen_helper_rints;
6235         break;
6236     case 0xe: /* FRINTX */
6237         gen_fpst = gen_helper_rints_exact;
6238         break;
6239     case 0xf: /* FRINTI */
6240         gen_fpst = gen_helper_rints;
6241         break;
6242     case 0x10: /* FRINT32Z */
6243         rmode = FPROUNDING_ZERO;
6244         gen_fpst = gen_helper_frint32_s;
6245         break;
6246     case 0x11: /* FRINT32X */
6247         gen_fpst = gen_helper_frint32_s;
6248         break;
6249     case 0x12: /* FRINT64Z */
6250         rmode = FPROUNDING_ZERO;
6251         gen_fpst = gen_helper_frint64_s;
6252         break;
6253     case 0x13: /* FRINT64X */
6254         gen_fpst = gen_helper_frint64_s;
6255         break;
6256     default:
6257         g_assert_not_reached();
6258     }
6259 
6260     fpst = fpstatus_ptr(FPST_FPCR);
6261     if (rmode >= 0) {
6262         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6263         gen_fpst(tcg_res, tcg_op, fpst);
6264         gen_restore_rmode(tcg_rmode, fpst);
6265     } else {
6266         gen_fpst(tcg_res, tcg_op, fpst);
6267     }
6268 
6269  done:
6270     write_fp_sreg(s, rd, tcg_res);
6271 }
6272 
6273 /* Floating-point data-processing (1 source) - double precision */
6274 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6275 {
6276     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6277     TCGv_i64 tcg_op, tcg_res;
6278     TCGv_ptr fpst;
6279     int rmode = -1;
6280 
6281     switch (opcode) {
6282     case 0x0: /* FMOV */
6283         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6284         return;
6285     }
6286 
6287     tcg_op = read_fp_dreg(s, rn);
6288     tcg_res = tcg_temp_new_i64();
6289 
6290     switch (opcode) {
6291     case 0x1: /* FABS */
6292         gen_helper_vfp_absd(tcg_res, tcg_op);
6293         goto done;
6294     case 0x2: /* FNEG */
6295         gen_helper_vfp_negd(tcg_res, tcg_op);
6296         goto done;
6297     case 0x3: /* FSQRT */
6298         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
6299         goto done;
6300     case 0x8: /* FRINTN */
6301     case 0x9: /* FRINTP */
6302     case 0xa: /* FRINTM */
6303     case 0xb: /* FRINTZ */
6304     case 0xc: /* FRINTA */
6305         rmode = opcode & 7;
6306         gen_fpst = gen_helper_rintd;
6307         break;
6308     case 0xe: /* FRINTX */
6309         gen_fpst = gen_helper_rintd_exact;
6310         break;
6311     case 0xf: /* FRINTI */
6312         gen_fpst = gen_helper_rintd;
6313         break;
6314     case 0x10: /* FRINT32Z */
6315         rmode = FPROUNDING_ZERO;
6316         gen_fpst = gen_helper_frint32_d;
6317         break;
6318     case 0x11: /* FRINT32X */
6319         gen_fpst = gen_helper_frint32_d;
6320         break;
6321     case 0x12: /* FRINT64Z */
6322         rmode = FPROUNDING_ZERO;
6323         gen_fpst = gen_helper_frint64_d;
6324         break;
6325     case 0x13: /* FRINT64X */
6326         gen_fpst = gen_helper_frint64_d;
6327         break;
6328     default:
6329         g_assert_not_reached();
6330     }
6331 
6332     fpst = fpstatus_ptr(FPST_FPCR);
6333     if (rmode >= 0) {
6334         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6335         gen_fpst(tcg_res, tcg_op, fpst);
6336         gen_restore_rmode(tcg_rmode, fpst);
6337     } else {
6338         gen_fpst(tcg_res, tcg_op, fpst);
6339     }
6340 
6341  done:
6342     write_fp_dreg(s, rd, tcg_res);
6343 }
6344 
6345 static void handle_fp_fcvt(DisasContext *s, int opcode,
6346                            int rd, int rn, int dtype, int ntype)
6347 {
6348     switch (ntype) {
6349     case 0x0:
6350     {
6351         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6352         if (dtype == 1) {
6353             /* Single to double */
6354             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6355             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
6356             write_fp_dreg(s, rd, tcg_rd);
6357         } else {
6358             /* Single to half */
6359             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6360             TCGv_i32 ahp = get_ahp_flag();
6361             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6362 
6363             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6364             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6365             write_fp_sreg(s, rd, tcg_rd);
6366         }
6367         break;
6368     }
6369     case 0x1:
6370     {
6371         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6372         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6373         if (dtype == 0) {
6374             /* Double to single */
6375             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
6376         } else {
6377             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6378             TCGv_i32 ahp = get_ahp_flag();
6379             /* Double to half */
6380             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6381             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6382         }
6383         write_fp_sreg(s, rd, tcg_rd);
6384         break;
6385     }
6386     case 0x3:
6387     {
6388         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6389         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6390         TCGv_i32 tcg_ahp = get_ahp_flag();
6391         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6392         if (dtype == 0) {
6393             /* Half to single */
6394             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6395             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6396             write_fp_sreg(s, rd, tcg_rd);
6397         } else {
6398             /* Half to double */
6399             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6400             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6401             write_fp_dreg(s, rd, tcg_rd);
6402         }
6403         break;
6404     }
6405     default:
6406         g_assert_not_reached();
6407     }
6408 }
6409 
6410 /* Floating point data-processing (1 source)
6411  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6412  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6413  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6414  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6415  */
6416 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6417 {
6418     int mos = extract32(insn, 29, 3);
6419     int type = extract32(insn, 22, 2);
6420     int opcode = extract32(insn, 15, 6);
6421     int rn = extract32(insn, 5, 5);
6422     int rd = extract32(insn, 0, 5);
6423 
6424     if (mos) {
6425         goto do_unallocated;
6426     }
6427 
6428     switch (opcode) {
6429     case 0x4: case 0x5: case 0x7:
6430     {
6431         /* FCVT between half, single and double precision */
6432         int dtype = extract32(opcode, 0, 2);
6433         if (type == 2 || dtype == type) {
6434             goto do_unallocated;
6435         }
6436         if (!fp_access_check(s)) {
6437             return;
6438         }
6439 
6440         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6441         break;
6442     }
6443 
6444     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6445         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6446             goto do_unallocated;
6447         }
6448         /* fall through */
6449     case 0x0 ... 0x3:
6450     case 0x8 ... 0xc:
6451     case 0xe ... 0xf:
6452         /* 32-to-32 and 64-to-64 ops */
6453         switch (type) {
6454         case 0:
6455             if (!fp_access_check(s)) {
6456                 return;
6457             }
6458             handle_fp_1src_single(s, opcode, rd, rn);
6459             break;
6460         case 1:
6461             if (!fp_access_check(s)) {
6462                 return;
6463             }
6464             handle_fp_1src_double(s, opcode, rd, rn);
6465             break;
6466         case 3:
6467             if (!dc_isar_feature(aa64_fp16, s)) {
6468                 goto do_unallocated;
6469             }
6470 
6471             if (!fp_access_check(s)) {
6472                 return;
6473             }
6474             handle_fp_1src_half(s, opcode, rd, rn);
6475             break;
6476         default:
6477             goto do_unallocated;
6478         }
6479         break;
6480 
6481     case 0x6:
6482         switch (type) {
6483         case 1: /* BFCVT */
6484             if (!dc_isar_feature(aa64_bf16, s)) {
6485                 goto do_unallocated;
6486             }
6487             if (!fp_access_check(s)) {
6488                 return;
6489             }
6490             handle_fp_1src_single(s, opcode, rd, rn);
6491             break;
6492         default:
6493             goto do_unallocated;
6494         }
6495         break;
6496 
6497     default:
6498     do_unallocated:
6499         unallocated_encoding(s);
6500         break;
6501     }
6502 }
6503 
6504 /* Floating-point data-processing (2 source) - single precision */
6505 static void handle_fp_2src_single(DisasContext *s, int opcode,
6506                                   int rd, int rn, int rm)
6507 {
6508     TCGv_i32 tcg_op1;
6509     TCGv_i32 tcg_op2;
6510     TCGv_i32 tcg_res;
6511     TCGv_ptr fpst;
6512 
6513     tcg_res = tcg_temp_new_i32();
6514     fpst = fpstatus_ptr(FPST_FPCR);
6515     tcg_op1 = read_fp_sreg(s, rn);
6516     tcg_op2 = read_fp_sreg(s, rm);
6517 
6518     switch (opcode) {
6519     case 0x0: /* FMUL */
6520         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6521         break;
6522     case 0x1: /* FDIV */
6523         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6524         break;
6525     case 0x2: /* FADD */
6526         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6527         break;
6528     case 0x3: /* FSUB */
6529         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6530         break;
6531     case 0x4: /* FMAX */
6532         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6533         break;
6534     case 0x5: /* FMIN */
6535         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6536         break;
6537     case 0x6: /* FMAXNM */
6538         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6539         break;
6540     case 0x7: /* FMINNM */
6541         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6542         break;
6543     case 0x8: /* FNMUL */
6544         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6545         gen_helper_vfp_negs(tcg_res, tcg_res);
6546         break;
6547     }
6548 
6549     write_fp_sreg(s, rd, tcg_res);
6550 }
6551 
6552 /* Floating-point data-processing (2 source) - double precision */
6553 static void handle_fp_2src_double(DisasContext *s, int opcode,
6554                                   int rd, int rn, int rm)
6555 {
6556     TCGv_i64 tcg_op1;
6557     TCGv_i64 tcg_op2;
6558     TCGv_i64 tcg_res;
6559     TCGv_ptr fpst;
6560 
6561     tcg_res = tcg_temp_new_i64();
6562     fpst = fpstatus_ptr(FPST_FPCR);
6563     tcg_op1 = read_fp_dreg(s, rn);
6564     tcg_op2 = read_fp_dreg(s, rm);
6565 
6566     switch (opcode) {
6567     case 0x0: /* FMUL */
6568         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6569         break;
6570     case 0x1: /* FDIV */
6571         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6572         break;
6573     case 0x2: /* FADD */
6574         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6575         break;
6576     case 0x3: /* FSUB */
6577         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6578         break;
6579     case 0x4: /* FMAX */
6580         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6581         break;
6582     case 0x5: /* FMIN */
6583         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6584         break;
6585     case 0x6: /* FMAXNM */
6586         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6587         break;
6588     case 0x7: /* FMINNM */
6589         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6590         break;
6591     case 0x8: /* FNMUL */
6592         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6593         gen_helper_vfp_negd(tcg_res, tcg_res);
6594         break;
6595     }
6596 
6597     write_fp_dreg(s, rd, tcg_res);
6598 }
6599 
6600 /* Floating-point data-processing (2 source) - half precision */
6601 static void handle_fp_2src_half(DisasContext *s, int opcode,
6602                                 int rd, int rn, int rm)
6603 {
6604     TCGv_i32 tcg_op1;
6605     TCGv_i32 tcg_op2;
6606     TCGv_i32 tcg_res;
6607     TCGv_ptr fpst;
6608 
6609     tcg_res = tcg_temp_new_i32();
6610     fpst = fpstatus_ptr(FPST_FPCR_F16);
6611     tcg_op1 = read_fp_hreg(s, rn);
6612     tcg_op2 = read_fp_hreg(s, rm);
6613 
6614     switch (opcode) {
6615     case 0x0: /* FMUL */
6616         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6617         break;
6618     case 0x1: /* FDIV */
6619         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6620         break;
6621     case 0x2: /* FADD */
6622         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6623         break;
6624     case 0x3: /* FSUB */
6625         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6626         break;
6627     case 0x4: /* FMAX */
6628         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6629         break;
6630     case 0x5: /* FMIN */
6631         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6632         break;
6633     case 0x6: /* FMAXNM */
6634         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6635         break;
6636     case 0x7: /* FMINNM */
6637         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6638         break;
6639     case 0x8: /* FNMUL */
6640         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6641         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6642         break;
6643     default:
6644         g_assert_not_reached();
6645     }
6646 
6647     write_fp_sreg(s, rd, tcg_res);
6648 }
6649 
6650 /* Floating point data-processing (2 source)
6651  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6652  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6653  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6654  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6655  */
6656 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6657 {
6658     int mos = extract32(insn, 29, 3);
6659     int type = extract32(insn, 22, 2);
6660     int rd = extract32(insn, 0, 5);
6661     int rn = extract32(insn, 5, 5);
6662     int rm = extract32(insn, 16, 5);
6663     int opcode = extract32(insn, 12, 4);
6664 
6665     if (opcode > 8 || mos) {
6666         unallocated_encoding(s);
6667         return;
6668     }
6669 
6670     switch (type) {
6671     case 0:
6672         if (!fp_access_check(s)) {
6673             return;
6674         }
6675         handle_fp_2src_single(s, opcode, rd, rn, rm);
6676         break;
6677     case 1:
6678         if (!fp_access_check(s)) {
6679             return;
6680         }
6681         handle_fp_2src_double(s, opcode, rd, rn, rm);
6682         break;
6683     case 3:
6684         if (!dc_isar_feature(aa64_fp16, s)) {
6685             unallocated_encoding(s);
6686             return;
6687         }
6688         if (!fp_access_check(s)) {
6689             return;
6690         }
6691         handle_fp_2src_half(s, opcode, rd, rn, rm);
6692         break;
6693     default:
6694         unallocated_encoding(s);
6695     }
6696 }
6697 
6698 /* Floating-point data-processing (3 source) - single precision */
6699 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6700                                   int rd, int rn, int rm, int ra)
6701 {
6702     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6703     TCGv_i32 tcg_res = tcg_temp_new_i32();
6704     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6705 
6706     tcg_op1 = read_fp_sreg(s, rn);
6707     tcg_op2 = read_fp_sreg(s, rm);
6708     tcg_op3 = read_fp_sreg(s, ra);
6709 
6710     /* These are fused multiply-add, and must be done as one
6711      * floating point operation with no rounding between the
6712      * multiplication and addition steps.
6713      * NB that doing the negations here as separate steps is
6714      * correct : an input NaN should come out with its sign bit
6715      * flipped if it is a negated-input.
6716      */
6717     if (o1 == true) {
6718         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6719     }
6720 
6721     if (o0 != o1) {
6722         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6723     }
6724 
6725     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6726 
6727     write_fp_sreg(s, rd, tcg_res);
6728 }
6729 
6730 /* Floating-point data-processing (3 source) - double precision */
6731 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6732                                   int rd, int rn, int rm, int ra)
6733 {
6734     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6735     TCGv_i64 tcg_res = tcg_temp_new_i64();
6736     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6737 
6738     tcg_op1 = read_fp_dreg(s, rn);
6739     tcg_op2 = read_fp_dreg(s, rm);
6740     tcg_op3 = read_fp_dreg(s, ra);
6741 
6742     /* These are fused multiply-add, and must be done as one
6743      * floating point operation with no rounding between the
6744      * multiplication and addition steps.
6745      * NB that doing the negations here as separate steps is
6746      * correct : an input NaN should come out with its sign bit
6747      * flipped if it is a negated-input.
6748      */
6749     if (o1 == true) {
6750         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6751     }
6752 
6753     if (o0 != o1) {
6754         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6755     }
6756 
6757     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6758 
6759     write_fp_dreg(s, rd, tcg_res);
6760 }
6761 
6762 /* Floating-point data-processing (3 source) - half precision */
6763 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6764                                 int rd, int rn, int rm, int ra)
6765 {
6766     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6767     TCGv_i32 tcg_res = tcg_temp_new_i32();
6768     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6769 
6770     tcg_op1 = read_fp_hreg(s, rn);
6771     tcg_op2 = read_fp_hreg(s, rm);
6772     tcg_op3 = read_fp_hreg(s, ra);
6773 
6774     /* These are fused multiply-add, and must be done as one
6775      * floating point operation with no rounding between the
6776      * multiplication and addition steps.
6777      * NB that doing the negations here as separate steps is
6778      * correct : an input NaN should come out with its sign bit
6779      * flipped if it is a negated-input.
6780      */
6781     if (o1 == true) {
6782         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6783     }
6784 
6785     if (o0 != o1) {
6786         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6787     }
6788 
6789     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6790 
6791     write_fp_sreg(s, rd, tcg_res);
6792 }
6793 
6794 /* Floating point data-processing (3 source)
6795  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6796  * +---+---+---+-----------+------+----+------+----+------+------+------+
6797  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6798  * +---+---+---+-----------+------+----+------+----+------+------+------+
6799  */
6800 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6801 {
6802     int mos = extract32(insn, 29, 3);
6803     int type = extract32(insn, 22, 2);
6804     int rd = extract32(insn, 0, 5);
6805     int rn = extract32(insn, 5, 5);
6806     int ra = extract32(insn, 10, 5);
6807     int rm = extract32(insn, 16, 5);
6808     bool o0 = extract32(insn, 15, 1);
6809     bool o1 = extract32(insn, 21, 1);
6810 
6811     if (mos) {
6812         unallocated_encoding(s);
6813         return;
6814     }
6815 
6816     switch (type) {
6817     case 0:
6818         if (!fp_access_check(s)) {
6819             return;
6820         }
6821         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6822         break;
6823     case 1:
6824         if (!fp_access_check(s)) {
6825             return;
6826         }
6827         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6828         break;
6829     case 3:
6830         if (!dc_isar_feature(aa64_fp16, s)) {
6831             unallocated_encoding(s);
6832             return;
6833         }
6834         if (!fp_access_check(s)) {
6835             return;
6836         }
6837         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6838         break;
6839     default:
6840         unallocated_encoding(s);
6841     }
6842 }
6843 
6844 /* Floating point immediate
6845  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6846  * +---+---+---+-----------+------+---+------------+-------+------+------+
6847  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6848  * +---+---+---+-----------+------+---+------------+-------+------+------+
6849  */
6850 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6851 {
6852     int rd = extract32(insn, 0, 5);
6853     int imm5 = extract32(insn, 5, 5);
6854     int imm8 = extract32(insn, 13, 8);
6855     int type = extract32(insn, 22, 2);
6856     int mos = extract32(insn, 29, 3);
6857     uint64_t imm;
6858     MemOp sz;
6859 
6860     if (mos || imm5) {
6861         unallocated_encoding(s);
6862         return;
6863     }
6864 
6865     switch (type) {
6866     case 0:
6867         sz = MO_32;
6868         break;
6869     case 1:
6870         sz = MO_64;
6871         break;
6872     case 3:
6873         sz = MO_16;
6874         if (dc_isar_feature(aa64_fp16, s)) {
6875             break;
6876         }
6877         /* fallthru */
6878     default:
6879         unallocated_encoding(s);
6880         return;
6881     }
6882 
6883     if (!fp_access_check(s)) {
6884         return;
6885     }
6886 
6887     imm = vfp_expand_imm(sz, imm8);
6888     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6889 }
6890 
6891 /* Handle floating point <=> fixed point conversions. Note that we can
6892  * also deal with fp <=> integer conversions as a special case (scale == 64)
6893  * OPTME: consider handling that special case specially or at least skipping
6894  * the call to scalbn in the helpers for zero shifts.
6895  */
6896 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6897                            bool itof, int rmode, int scale, int sf, int type)
6898 {
6899     bool is_signed = !(opcode & 1);
6900     TCGv_ptr tcg_fpstatus;
6901     TCGv_i32 tcg_shift, tcg_single;
6902     TCGv_i64 tcg_double;
6903 
6904     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6905 
6906     tcg_shift = tcg_constant_i32(64 - scale);
6907 
6908     if (itof) {
6909         TCGv_i64 tcg_int = cpu_reg(s, rn);
6910         if (!sf) {
6911             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6912 
6913             if (is_signed) {
6914                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6915             } else {
6916                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6917             }
6918 
6919             tcg_int = tcg_extend;
6920         }
6921 
6922         switch (type) {
6923         case 1: /* float64 */
6924             tcg_double = tcg_temp_new_i64();
6925             if (is_signed) {
6926                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6927                                      tcg_shift, tcg_fpstatus);
6928             } else {
6929                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6930                                      tcg_shift, tcg_fpstatus);
6931             }
6932             write_fp_dreg(s, rd, tcg_double);
6933             break;
6934 
6935         case 0: /* float32 */
6936             tcg_single = tcg_temp_new_i32();
6937             if (is_signed) {
6938                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6939                                      tcg_shift, tcg_fpstatus);
6940             } else {
6941                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6942                                      tcg_shift, tcg_fpstatus);
6943             }
6944             write_fp_sreg(s, rd, tcg_single);
6945             break;
6946 
6947         case 3: /* float16 */
6948             tcg_single = tcg_temp_new_i32();
6949             if (is_signed) {
6950                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6951                                      tcg_shift, tcg_fpstatus);
6952             } else {
6953                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6954                                      tcg_shift, tcg_fpstatus);
6955             }
6956             write_fp_sreg(s, rd, tcg_single);
6957             break;
6958 
6959         default:
6960             g_assert_not_reached();
6961         }
6962     } else {
6963         TCGv_i64 tcg_int = cpu_reg(s, rd);
6964         TCGv_i32 tcg_rmode;
6965 
6966         if (extract32(opcode, 2, 1)) {
6967             /* There are too many rounding modes to all fit into rmode,
6968              * so FCVTA[US] is a special case.
6969              */
6970             rmode = FPROUNDING_TIEAWAY;
6971         }
6972 
6973         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6974 
6975         switch (type) {
6976         case 1: /* float64 */
6977             tcg_double = read_fp_dreg(s, rn);
6978             if (is_signed) {
6979                 if (!sf) {
6980                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6981                                          tcg_shift, tcg_fpstatus);
6982                 } else {
6983                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6984                                          tcg_shift, tcg_fpstatus);
6985                 }
6986             } else {
6987                 if (!sf) {
6988                     gen_helper_vfp_tould(tcg_int, tcg_double,
6989                                          tcg_shift, tcg_fpstatus);
6990                 } else {
6991                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6992                                          tcg_shift, tcg_fpstatus);
6993                 }
6994             }
6995             if (!sf) {
6996                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6997             }
6998             break;
6999 
7000         case 0: /* float32 */
7001             tcg_single = read_fp_sreg(s, rn);
7002             if (sf) {
7003                 if (is_signed) {
7004                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
7005                                          tcg_shift, tcg_fpstatus);
7006                 } else {
7007                     gen_helper_vfp_touqs(tcg_int, tcg_single,
7008                                          tcg_shift, tcg_fpstatus);
7009                 }
7010             } else {
7011                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7012                 if (is_signed) {
7013                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7014                                          tcg_shift, tcg_fpstatus);
7015                 } else {
7016                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7017                                          tcg_shift, tcg_fpstatus);
7018                 }
7019                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7020             }
7021             break;
7022 
7023         case 3: /* float16 */
7024             tcg_single = read_fp_sreg(s, rn);
7025             if (sf) {
7026                 if (is_signed) {
7027                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7028                                          tcg_shift, tcg_fpstatus);
7029                 } else {
7030                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7031                                          tcg_shift, tcg_fpstatus);
7032                 }
7033             } else {
7034                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7035                 if (is_signed) {
7036                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7037                                          tcg_shift, tcg_fpstatus);
7038                 } else {
7039                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7040                                          tcg_shift, tcg_fpstatus);
7041                 }
7042                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7043             }
7044             break;
7045 
7046         default:
7047             g_assert_not_reached();
7048         }
7049 
7050         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7051     }
7052 }
7053 
7054 /* Floating point <-> fixed point conversions
7055  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7056  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7057  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7058  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7059  */
7060 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7061 {
7062     int rd = extract32(insn, 0, 5);
7063     int rn = extract32(insn, 5, 5);
7064     int scale = extract32(insn, 10, 6);
7065     int opcode = extract32(insn, 16, 3);
7066     int rmode = extract32(insn, 19, 2);
7067     int type = extract32(insn, 22, 2);
7068     bool sbit = extract32(insn, 29, 1);
7069     bool sf = extract32(insn, 31, 1);
7070     bool itof;
7071 
7072     if (sbit || (!sf && scale < 32)) {
7073         unallocated_encoding(s);
7074         return;
7075     }
7076 
7077     switch (type) {
7078     case 0: /* float32 */
7079     case 1: /* float64 */
7080         break;
7081     case 3: /* float16 */
7082         if (dc_isar_feature(aa64_fp16, s)) {
7083             break;
7084         }
7085         /* fallthru */
7086     default:
7087         unallocated_encoding(s);
7088         return;
7089     }
7090 
7091     switch ((rmode << 3) | opcode) {
7092     case 0x2: /* SCVTF */
7093     case 0x3: /* UCVTF */
7094         itof = true;
7095         break;
7096     case 0x18: /* FCVTZS */
7097     case 0x19: /* FCVTZU */
7098         itof = false;
7099         break;
7100     default:
7101         unallocated_encoding(s);
7102         return;
7103     }
7104 
7105     if (!fp_access_check(s)) {
7106         return;
7107     }
7108 
7109     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7110 }
7111 
7112 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7113 {
7114     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7115      * without conversion.
7116      */
7117 
7118     if (itof) {
7119         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7120         TCGv_i64 tmp;
7121 
7122         switch (type) {
7123         case 0:
7124             /* 32 bit */
7125             tmp = tcg_temp_new_i64();
7126             tcg_gen_ext32u_i64(tmp, tcg_rn);
7127             write_fp_dreg(s, rd, tmp);
7128             break;
7129         case 1:
7130             /* 64 bit */
7131             write_fp_dreg(s, rd, tcg_rn);
7132             break;
7133         case 2:
7134             /* 64 bit to top half. */
7135             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
7136             clear_vec_high(s, true, rd);
7137             break;
7138         case 3:
7139             /* 16 bit */
7140             tmp = tcg_temp_new_i64();
7141             tcg_gen_ext16u_i64(tmp, tcg_rn);
7142             write_fp_dreg(s, rd, tmp);
7143             break;
7144         default:
7145             g_assert_not_reached();
7146         }
7147     } else {
7148         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7149 
7150         switch (type) {
7151         case 0:
7152             /* 32 bit */
7153             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
7154             break;
7155         case 1:
7156             /* 64 bit */
7157             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
7158             break;
7159         case 2:
7160             /* 64 bits from top half */
7161             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
7162             break;
7163         case 3:
7164             /* 16 bit */
7165             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
7166             break;
7167         default:
7168             g_assert_not_reached();
7169         }
7170     }
7171 }
7172 
7173 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7174 {
7175     TCGv_i64 t = read_fp_dreg(s, rn);
7176     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7177 
7178     gen_helper_fjcvtzs(t, t, fpstatus);
7179 
7180     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7181     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7182     tcg_gen_movi_i32(cpu_CF, 0);
7183     tcg_gen_movi_i32(cpu_NF, 0);
7184     tcg_gen_movi_i32(cpu_VF, 0);
7185 }
7186 
7187 /* Floating point <-> integer conversions
7188  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7189  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7190  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7191  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7192  */
7193 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7194 {
7195     int rd = extract32(insn, 0, 5);
7196     int rn = extract32(insn, 5, 5);
7197     int opcode = extract32(insn, 16, 3);
7198     int rmode = extract32(insn, 19, 2);
7199     int type = extract32(insn, 22, 2);
7200     bool sbit = extract32(insn, 29, 1);
7201     bool sf = extract32(insn, 31, 1);
7202     bool itof = false;
7203 
7204     if (sbit) {
7205         goto do_unallocated;
7206     }
7207 
7208     switch (opcode) {
7209     case 2: /* SCVTF */
7210     case 3: /* UCVTF */
7211         itof = true;
7212         /* fallthru */
7213     case 4: /* FCVTAS */
7214     case 5: /* FCVTAU */
7215         if (rmode != 0) {
7216             goto do_unallocated;
7217         }
7218         /* fallthru */
7219     case 0: /* FCVT[NPMZ]S */
7220     case 1: /* FCVT[NPMZ]U */
7221         switch (type) {
7222         case 0: /* float32 */
7223         case 1: /* float64 */
7224             break;
7225         case 3: /* float16 */
7226             if (!dc_isar_feature(aa64_fp16, s)) {
7227                 goto do_unallocated;
7228             }
7229             break;
7230         default:
7231             goto do_unallocated;
7232         }
7233         if (!fp_access_check(s)) {
7234             return;
7235         }
7236         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7237         break;
7238 
7239     default:
7240         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7241         case 0b01100110: /* FMOV half <-> 32-bit int */
7242         case 0b01100111:
7243         case 0b11100110: /* FMOV half <-> 64-bit int */
7244         case 0b11100111:
7245             if (!dc_isar_feature(aa64_fp16, s)) {
7246                 goto do_unallocated;
7247             }
7248             /* fallthru */
7249         case 0b00000110: /* FMOV 32-bit */
7250         case 0b00000111:
7251         case 0b10100110: /* FMOV 64-bit */
7252         case 0b10100111:
7253         case 0b11001110: /* FMOV top half of 128-bit */
7254         case 0b11001111:
7255             if (!fp_access_check(s)) {
7256                 return;
7257             }
7258             itof = opcode & 1;
7259             handle_fmov(s, rd, rn, type, itof);
7260             break;
7261 
7262         case 0b00111110: /* FJCVTZS */
7263             if (!dc_isar_feature(aa64_jscvt, s)) {
7264                 goto do_unallocated;
7265             } else if (fp_access_check(s)) {
7266                 handle_fjcvtzs(s, rd, rn);
7267             }
7268             break;
7269 
7270         default:
7271         do_unallocated:
7272             unallocated_encoding(s);
7273             return;
7274         }
7275         break;
7276     }
7277 }
7278 
7279 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7280  *   31  30  29 28     25 24                          0
7281  * +---+---+---+---------+-----------------------------+
7282  * |   | 0 |   | 1 1 1 1 |                             |
7283  * +---+---+---+---------+-----------------------------+
7284  */
7285 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7286 {
7287     if (extract32(insn, 24, 1)) {
7288         /* Floating point data-processing (3 source) */
7289         disas_fp_3src(s, insn);
7290     } else if (extract32(insn, 21, 1) == 0) {
7291         /* Floating point to fixed point conversions */
7292         disas_fp_fixed_conv(s, insn);
7293     } else {
7294         switch (extract32(insn, 10, 2)) {
7295         case 1:
7296             /* Floating point conditional compare */
7297             disas_fp_ccomp(s, insn);
7298             break;
7299         case 2:
7300             /* Floating point data-processing (2 source) */
7301             disas_fp_2src(s, insn);
7302             break;
7303         case 3:
7304             /* Floating point conditional select */
7305             disas_fp_csel(s, insn);
7306             break;
7307         case 0:
7308             switch (ctz32(extract32(insn, 12, 4))) {
7309             case 0: /* [15:12] == xxx1 */
7310                 /* Floating point immediate */
7311                 disas_fp_imm(s, insn);
7312                 break;
7313             case 1: /* [15:12] == xx10 */
7314                 /* Floating point compare */
7315                 disas_fp_compare(s, insn);
7316                 break;
7317             case 2: /* [15:12] == x100 */
7318                 /* Floating point data-processing (1 source) */
7319                 disas_fp_1src(s, insn);
7320                 break;
7321             case 3: /* [15:12] == 1000 */
7322                 unallocated_encoding(s);
7323                 break;
7324             default: /* [15:12] == 0000 */
7325                 /* Floating point <-> integer conversions */
7326                 disas_fp_int_conv(s, insn);
7327                 break;
7328             }
7329             break;
7330         }
7331     }
7332 }
7333 
7334 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7335                      int pos)
7336 {
7337     /* Extract 64 bits from the middle of two concatenated 64 bit
7338      * vector register slices left:right. The extracted bits start
7339      * at 'pos' bits into the right (least significant) side.
7340      * We return the result in tcg_right, and guarantee not to
7341      * trash tcg_left.
7342      */
7343     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7344     assert(pos > 0 && pos < 64);
7345 
7346     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7347     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7348     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7349 }
7350 
7351 /* EXT
7352  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7353  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7354  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7355  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7356  */
7357 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7358 {
7359     int is_q = extract32(insn, 30, 1);
7360     int op2 = extract32(insn, 22, 2);
7361     int imm4 = extract32(insn, 11, 4);
7362     int rm = extract32(insn, 16, 5);
7363     int rn = extract32(insn, 5, 5);
7364     int rd = extract32(insn, 0, 5);
7365     int pos = imm4 << 3;
7366     TCGv_i64 tcg_resl, tcg_resh;
7367 
7368     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7369         unallocated_encoding(s);
7370         return;
7371     }
7372 
7373     if (!fp_access_check(s)) {
7374         return;
7375     }
7376 
7377     tcg_resh = tcg_temp_new_i64();
7378     tcg_resl = tcg_temp_new_i64();
7379 
7380     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7381      * either extracting 128 bits from a 128:128 concatenation, or
7382      * extracting 64 bits from a 64:64 concatenation.
7383      */
7384     if (!is_q) {
7385         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7386         if (pos != 0) {
7387             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7388             do_ext64(s, tcg_resh, tcg_resl, pos);
7389         }
7390     } else {
7391         TCGv_i64 tcg_hh;
7392         typedef struct {
7393             int reg;
7394             int elt;
7395         } EltPosns;
7396         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7397         EltPosns *elt = eltposns;
7398 
7399         if (pos >= 64) {
7400             elt++;
7401             pos -= 64;
7402         }
7403 
7404         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7405         elt++;
7406         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7407         elt++;
7408         if (pos != 0) {
7409             do_ext64(s, tcg_resh, tcg_resl, pos);
7410             tcg_hh = tcg_temp_new_i64();
7411             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7412             do_ext64(s, tcg_hh, tcg_resh, pos);
7413         }
7414     }
7415 
7416     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7417     if (is_q) {
7418         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7419     }
7420     clear_vec_high(s, is_q, rd);
7421 }
7422 
7423 /* TBL/TBX
7424  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7425  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7426  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7427  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7428  */
7429 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7430 {
7431     int op2 = extract32(insn, 22, 2);
7432     int is_q = extract32(insn, 30, 1);
7433     int rm = extract32(insn, 16, 5);
7434     int rn = extract32(insn, 5, 5);
7435     int rd = extract32(insn, 0, 5);
7436     int is_tbx = extract32(insn, 12, 1);
7437     int len = (extract32(insn, 13, 2) + 1) * 16;
7438 
7439     if (op2 != 0) {
7440         unallocated_encoding(s);
7441         return;
7442     }
7443 
7444     if (!fp_access_check(s)) {
7445         return;
7446     }
7447 
7448     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7449                        vec_full_reg_offset(s, rm), tcg_env,
7450                        is_q ? 16 : 8, vec_full_reg_size(s),
7451                        (len << 6) | (is_tbx << 5) | rn,
7452                        gen_helper_simd_tblx);
7453 }
7454 
7455 /* ZIP/UZP/TRN
7456  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7457  * +---+---+-------------+------+---+------+---+------------------+------+
7458  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7459  * +---+---+-------------+------+---+------+---+------------------+------+
7460  */
7461 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7462 {
7463     int rd = extract32(insn, 0, 5);
7464     int rn = extract32(insn, 5, 5);
7465     int rm = extract32(insn, 16, 5);
7466     int size = extract32(insn, 22, 2);
7467     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7468      * bit 2 indicates 1 vs 2 variant of the insn.
7469      */
7470     int opcode = extract32(insn, 12, 2);
7471     bool part = extract32(insn, 14, 1);
7472     bool is_q = extract32(insn, 30, 1);
7473     int esize = 8 << size;
7474     int i;
7475     int datasize = is_q ? 128 : 64;
7476     int elements = datasize / esize;
7477     TCGv_i64 tcg_res[2], tcg_ele;
7478 
7479     if (opcode == 0 || (size == 3 && !is_q)) {
7480         unallocated_encoding(s);
7481         return;
7482     }
7483 
7484     if (!fp_access_check(s)) {
7485         return;
7486     }
7487 
7488     tcg_res[0] = tcg_temp_new_i64();
7489     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7490     tcg_ele = tcg_temp_new_i64();
7491 
7492     for (i = 0; i < elements; i++) {
7493         int o, w;
7494 
7495         switch (opcode) {
7496         case 1: /* UZP1/2 */
7497         {
7498             int midpoint = elements / 2;
7499             if (i < midpoint) {
7500                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7501             } else {
7502                 read_vec_element(s, tcg_ele, rm,
7503                                  2 * (i - midpoint) + part, size);
7504             }
7505             break;
7506         }
7507         case 2: /* TRN1/2 */
7508             if (i & 1) {
7509                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7510             } else {
7511                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7512             }
7513             break;
7514         case 3: /* ZIP1/2 */
7515         {
7516             int base = part * elements / 2;
7517             if (i & 1) {
7518                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7519             } else {
7520                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7521             }
7522             break;
7523         }
7524         default:
7525             g_assert_not_reached();
7526         }
7527 
7528         w = (i * esize) / 64;
7529         o = (i * esize) % 64;
7530         if (o == 0) {
7531             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7532         } else {
7533             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7534             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7535         }
7536     }
7537 
7538     for (i = 0; i <= is_q; ++i) {
7539         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7540     }
7541     clear_vec_high(s, is_q, rd);
7542 }
7543 
7544 /*
7545  * do_reduction_op helper
7546  *
7547  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7548  * important for correct NaN propagation that we do these
7549  * operations in exactly the order specified by the pseudocode.
7550  *
7551  * This is a recursive function, TCG temps should be freed by the
7552  * calling function once it is done with the values.
7553  */
7554 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7555                                 int esize, int size, int vmap, TCGv_ptr fpst)
7556 {
7557     if (esize == size) {
7558         int element;
7559         MemOp msize = esize == 16 ? MO_16 : MO_32;
7560         TCGv_i32 tcg_elem;
7561 
7562         /* We should have one register left here */
7563         assert(ctpop8(vmap) == 1);
7564         element = ctz32(vmap);
7565         assert(element < 8);
7566 
7567         tcg_elem = tcg_temp_new_i32();
7568         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7569         return tcg_elem;
7570     } else {
7571         int bits = size / 2;
7572         int shift = ctpop8(vmap) / 2;
7573         int vmap_lo = (vmap >> shift) & vmap;
7574         int vmap_hi = (vmap & ~vmap_lo);
7575         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7576 
7577         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7578         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7579         tcg_res = tcg_temp_new_i32();
7580 
7581         switch (fpopcode) {
7582         case 0x0c: /* fmaxnmv half-precision */
7583             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7584             break;
7585         case 0x0f: /* fmaxv half-precision */
7586             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7587             break;
7588         case 0x1c: /* fminnmv half-precision */
7589             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7590             break;
7591         case 0x1f: /* fminv half-precision */
7592             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7593             break;
7594         case 0x2c: /* fmaxnmv */
7595             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7596             break;
7597         case 0x2f: /* fmaxv */
7598             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7599             break;
7600         case 0x3c: /* fminnmv */
7601             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7602             break;
7603         case 0x3f: /* fminv */
7604             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7605             break;
7606         default:
7607             g_assert_not_reached();
7608         }
7609         return tcg_res;
7610     }
7611 }
7612 
7613 /* AdvSIMD across lanes
7614  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7615  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7616  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7617  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7618  */
7619 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7620 {
7621     int rd = extract32(insn, 0, 5);
7622     int rn = extract32(insn, 5, 5);
7623     int size = extract32(insn, 22, 2);
7624     int opcode = extract32(insn, 12, 5);
7625     bool is_q = extract32(insn, 30, 1);
7626     bool is_u = extract32(insn, 29, 1);
7627     bool is_fp = false;
7628     bool is_min = false;
7629     int esize;
7630     int elements;
7631     int i;
7632     TCGv_i64 tcg_res, tcg_elt;
7633 
7634     switch (opcode) {
7635     case 0x1b: /* ADDV */
7636         if (is_u) {
7637             unallocated_encoding(s);
7638             return;
7639         }
7640         /* fall through */
7641     case 0x3: /* SADDLV, UADDLV */
7642     case 0xa: /* SMAXV, UMAXV */
7643     case 0x1a: /* SMINV, UMINV */
7644         if (size == 3 || (size == 2 && !is_q)) {
7645             unallocated_encoding(s);
7646             return;
7647         }
7648         break;
7649     case 0xc: /* FMAXNMV, FMINNMV */
7650     case 0xf: /* FMAXV, FMINV */
7651         /* Bit 1 of size field encodes min vs max and the actual size
7652          * depends on the encoding of the U bit. If not set (and FP16
7653          * enabled) then we do half-precision float instead of single
7654          * precision.
7655          */
7656         is_min = extract32(size, 1, 1);
7657         is_fp = true;
7658         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7659             size = 1;
7660         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7661             unallocated_encoding(s);
7662             return;
7663         } else {
7664             size = 2;
7665         }
7666         break;
7667     default:
7668         unallocated_encoding(s);
7669         return;
7670     }
7671 
7672     if (!fp_access_check(s)) {
7673         return;
7674     }
7675 
7676     esize = 8 << size;
7677     elements = (is_q ? 128 : 64) / esize;
7678 
7679     tcg_res = tcg_temp_new_i64();
7680     tcg_elt = tcg_temp_new_i64();
7681 
7682     /* These instructions operate across all lanes of a vector
7683      * to produce a single result. We can guarantee that a 64
7684      * bit intermediate is sufficient:
7685      *  + for [US]ADDLV the maximum element size is 32 bits, and
7686      *    the result type is 64 bits
7687      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7688      *    same as the element size, which is 32 bits at most
7689      * For the integer operations we can choose to work at 64
7690      * or 32 bits and truncate at the end; for simplicity
7691      * we use 64 bits always. The floating point
7692      * ops do require 32 bit intermediates, though.
7693      */
7694     if (!is_fp) {
7695         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7696 
7697         for (i = 1; i < elements; i++) {
7698             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7699 
7700             switch (opcode) {
7701             case 0x03: /* SADDLV / UADDLV */
7702             case 0x1b: /* ADDV */
7703                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7704                 break;
7705             case 0x0a: /* SMAXV / UMAXV */
7706                 if (is_u) {
7707                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7708                 } else {
7709                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7710                 }
7711                 break;
7712             case 0x1a: /* SMINV / UMINV */
7713                 if (is_u) {
7714                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7715                 } else {
7716                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7717                 }
7718                 break;
7719             default:
7720                 g_assert_not_reached();
7721             }
7722 
7723         }
7724     } else {
7725         /* Floating point vector reduction ops which work across 32
7726          * bit (single) or 16 bit (half-precision) intermediates.
7727          * Note that correct NaN propagation requires that we do these
7728          * operations in exactly the order specified by the pseudocode.
7729          */
7730         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7731         int fpopcode = opcode | is_min << 4 | is_u << 5;
7732         int vmap = (1 << elements) - 1;
7733         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7734                                              (is_q ? 128 : 64), vmap, fpst);
7735         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7736     }
7737 
7738     /* Now truncate the result to the width required for the final output */
7739     if (opcode == 0x03) {
7740         /* SADDLV, UADDLV: result is 2*esize */
7741         size++;
7742     }
7743 
7744     switch (size) {
7745     case 0:
7746         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7747         break;
7748     case 1:
7749         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7750         break;
7751     case 2:
7752         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7753         break;
7754     case 3:
7755         break;
7756     default:
7757         g_assert_not_reached();
7758     }
7759 
7760     write_fp_dreg(s, rd, tcg_res);
7761 }
7762 
7763 /* DUP (Element, Vector)
7764  *
7765  *  31  30   29              21 20    16 15        10  9    5 4    0
7766  * +---+---+-------------------+--------+-------------+------+------+
7767  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7768  * +---+---+-------------------+--------+-------------+------+------+
7769  *
7770  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7771  */
7772 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7773                              int imm5)
7774 {
7775     int size = ctz32(imm5);
7776     int index;
7777 
7778     if (size > 3 || (size == 3 && !is_q)) {
7779         unallocated_encoding(s);
7780         return;
7781     }
7782 
7783     if (!fp_access_check(s)) {
7784         return;
7785     }
7786 
7787     index = imm5 >> (size + 1);
7788     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7789                          vec_reg_offset(s, rn, index, size),
7790                          is_q ? 16 : 8, vec_full_reg_size(s));
7791 }
7792 
7793 /* DUP (element, scalar)
7794  *  31                   21 20    16 15        10  9    5 4    0
7795  * +-----------------------+--------+-------------+------+------+
7796  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7797  * +-----------------------+--------+-------------+------+------+
7798  */
7799 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7800                               int imm5)
7801 {
7802     int size = ctz32(imm5);
7803     int index;
7804     TCGv_i64 tmp;
7805 
7806     if (size > 3) {
7807         unallocated_encoding(s);
7808         return;
7809     }
7810 
7811     if (!fp_access_check(s)) {
7812         return;
7813     }
7814 
7815     index = imm5 >> (size + 1);
7816 
7817     /* This instruction just extracts the specified element and
7818      * zero-extends it into the bottom of the destination register.
7819      */
7820     tmp = tcg_temp_new_i64();
7821     read_vec_element(s, tmp, rn, index, size);
7822     write_fp_dreg(s, rd, tmp);
7823 }
7824 
7825 /* DUP (General)
7826  *
7827  *  31  30   29              21 20    16 15        10  9    5 4    0
7828  * +---+---+-------------------+--------+-------------+------+------+
7829  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7830  * +---+---+-------------------+--------+-------------+------+------+
7831  *
7832  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7833  */
7834 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7835                              int imm5)
7836 {
7837     int size = ctz32(imm5);
7838     uint32_t dofs, oprsz, maxsz;
7839 
7840     if (size > 3 || ((size == 3) && !is_q)) {
7841         unallocated_encoding(s);
7842         return;
7843     }
7844 
7845     if (!fp_access_check(s)) {
7846         return;
7847     }
7848 
7849     dofs = vec_full_reg_offset(s, rd);
7850     oprsz = is_q ? 16 : 8;
7851     maxsz = vec_full_reg_size(s);
7852 
7853     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7854 }
7855 
7856 /* INS (Element)
7857  *
7858  *  31                   21 20    16 15  14    11  10 9    5 4    0
7859  * +-----------------------+--------+------------+---+------+------+
7860  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7861  * +-----------------------+--------+------------+---+------+------+
7862  *
7863  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7864  * index: encoded in imm5<4:size+1>
7865  */
7866 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7867                              int imm4, int imm5)
7868 {
7869     int size = ctz32(imm5);
7870     int src_index, dst_index;
7871     TCGv_i64 tmp;
7872 
7873     if (size > 3) {
7874         unallocated_encoding(s);
7875         return;
7876     }
7877 
7878     if (!fp_access_check(s)) {
7879         return;
7880     }
7881 
7882     dst_index = extract32(imm5, 1+size, 5);
7883     src_index = extract32(imm4, size, 4);
7884 
7885     tmp = tcg_temp_new_i64();
7886 
7887     read_vec_element(s, tmp, rn, src_index, size);
7888     write_vec_element(s, tmp, rd, dst_index, size);
7889 
7890     /* INS is considered a 128-bit write for SVE. */
7891     clear_vec_high(s, true, rd);
7892 }
7893 
7894 
7895 /* INS (General)
7896  *
7897  *  31                   21 20    16 15        10  9    5 4    0
7898  * +-----------------------+--------+-------------+------+------+
7899  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7900  * +-----------------------+--------+-------------+------+------+
7901  *
7902  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7903  * index: encoded in imm5<4:size+1>
7904  */
7905 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7906 {
7907     int size = ctz32(imm5);
7908     int idx;
7909 
7910     if (size > 3) {
7911         unallocated_encoding(s);
7912         return;
7913     }
7914 
7915     if (!fp_access_check(s)) {
7916         return;
7917     }
7918 
7919     idx = extract32(imm5, 1 + size, 4 - size);
7920     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7921 
7922     /* INS is considered a 128-bit write for SVE. */
7923     clear_vec_high(s, true, rd);
7924 }
7925 
7926 /*
7927  * UMOV (General)
7928  * SMOV (General)
7929  *
7930  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7931  * +---+---+-------------------+--------+-------------+------+------+
7932  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7933  * +---+---+-------------------+--------+-------------+------+------+
7934  *
7935  * U: unsigned when set
7936  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7937  */
7938 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7939                                   int rn, int rd, int imm5)
7940 {
7941     int size = ctz32(imm5);
7942     int element;
7943     TCGv_i64 tcg_rd;
7944 
7945     /* Check for UnallocatedEncodings */
7946     if (is_signed) {
7947         if (size > 2 || (size == 2 && !is_q)) {
7948             unallocated_encoding(s);
7949             return;
7950         }
7951     } else {
7952         if (size > 3
7953             || (size < 3 && is_q)
7954             || (size == 3 && !is_q)) {
7955             unallocated_encoding(s);
7956             return;
7957         }
7958     }
7959 
7960     if (!fp_access_check(s)) {
7961         return;
7962     }
7963 
7964     element = extract32(imm5, 1+size, 4);
7965 
7966     tcg_rd = cpu_reg(s, rd);
7967     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7968     if (is_signed && !is_q) {
7969         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7970     }
7971 }
7972 
7973 /* AdvSIMD copy
7974  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7975  * +---+---+----+-----------------+------+---+------+---+------+------+
7976  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7977  * +---+---+----+-----------------+------+---+------+---+------+------+
7978  */
7979 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7980 {
7981     int rd = extract32(insn, 0, 5);
7982     int rn = extract32(insn, 5, 5);
7983     int imm4 = extract32(insn, 11, 4);
7984     int op = extract32(insn, 29, 1);
7985     int is_q = extract32(insn, 30, 1);
7986     int imm5 = extract32(insn, 16, 5);
7987 
7988     if (op) {
7989         if (is_q) {
7990             /* INS (element) */
7991             handle_simd_inse(s, rd, rn, imm4, imm5);
7992         } else {
7993             unallocated_encoding(s);
7994         }
7995     } else {
7996         switch (imm4) {
7997         case 0:
7998             /* DUP (element - vector) */
7999             handle_simd_dupe(s, is_q, rd, rn, imm5);
8000             break;
8001         case 1:
8002             /* DUP (general) */
8003             handle_simd_dupg(s, is_q, rd, rn, imm5);
8004             break;
8005         case 3:
8006             if (is_q) {
8007                 /* INS (general) */
8008                 handle_simd_insg(s, rd, rn, imm5);
8009             } else {
8010                 unallocated_encoding(s);
8011             }
8012             break;
8013         case 5:
8014         case 7:
8015             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8016             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8017             break;
8018         default:
8019             unallocated_encoding(s);
8020             break;
8021         }
8022     }
8023 }
8024 
8025 /* AdvSIMD modified immediate
8026  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8027  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8028  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8029  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8030  *
8031  * There are a number of operations that can be carried out here:
8032  *   MOVI - move (shifted) imm into register
8033  *   MVNI - move inverted (shifted) imm into register
8034  *   ORR  - bitwise OR of (shifted) imm with register
8035  *   BIC  - bitwise clear of (shifted) imm with register
8036  * With ARMv8.2 we also have:
8037  *   FMOV half-precision
8038  */
8039 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8040 {
8041     int rd = extract32(insn, 0, 5);
8042     int cmode = extract32(insn, 12, 4);
8043     int o2 = extract32(insn, 11, 1);
8044     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8045     bool is_neg = extract32(insn, 29, 1);
8046     bool is_q = extract32(insn, 30, 1);
8047     uint64_t imm = 0;
8048 
8049     if (o2) {
8050         if (cmode != 0xf || is_neg) {
8051             unallocated_encoding(s);
8052             return;
8053         }
8054         /* FMOV (vector, immediate) - half-precision */
8055         if (!dc_isar_feature(aa64_fp16, s)) {
8056             unallocated_encoding(s);
8057             return;
8058         }
8059         imm = vfp_expand_imm(MO_16, abcdefgh);
8060         /* now duplicate across the lanes */
8061         imm = dup_const(MO_16, imm);
8062     } else {
8063         if (cmode == 0xf && is_neg && !is_q) {
8064             unallocated_encoding(s);
8065             return;
8066         }
8067         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8068     }
8069 
8070     if (!fp_access_check(s)) {
8071         return;
8072     }
8073 
8074     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8075         /* MOVI or MVNI, with MVNI negation handled above.  */
8076         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8077                              vec_full_reg_size(s), imm);
8078     } else {
8079         /* ORR or BIC, with BIC negation to AND handled above.  */
8080         if (is_neg) {
8081             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8082         } else {
8083             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8084         }
8085     }
8086 }
8087 
8088 /* AdvSIMD scalar copy
8089  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8090  * +-----+----+-----------------+------+---+------+---+------+------+
8091  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8092  * +-----+----+-----------------+------+---+------+---+------+------+
8093  */
8094 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8095 {
8096     int rd = extract32(insn, 0, 5);
8097     int rn = extract32(insn, 5, 5);
8098     int imm4 = extract32(insn, 11, 4);
8099     int imm5 = extract32(insn, 16, 5);
8100     int op = extract32(insn, 29, 1);
8101 
8102     if (op != 0 || imm4 != 0) {
8103         unallocated_encoding(s);
8104         return;
8105     }
8106 
8107     /* DUP (element, scalar) */
8108     handle_simd_dupes(s, rd, rn, imm5);
8109 }
8110 
8111 /* AdvSIMD scalar pairwise
8112  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8113  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8114  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8115  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8116  */
8117 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8118 {
8119     int u = extract32(insn, 29, 1);
8120     int size = extract32(insn, 22, 2);
8121     int opcode = extract32(insn, 12, 5);
8122     int rn = extract32(insn, 5, 5);
8123     int rd = extract32(insn, 0, 5);
8124     TCGv_ptr fpst;
8125 
8126     /* For some ops (the FP ones), size[1] is part of the encoding.
8127      * For ADDP strictly it is not but size[1] is always 1 for valid
8128      * encodings.
8129      */
8130     opcode |= (extract32(size, 1, 1) << 5);
8131 
8132     switch (opcode) {
8133     case 0x3b: /* ADDP */
8134         if (u || size != 3) {
8135             unallocated_encoding(s);
8136             return;
8137         }
8138         if (!fp_access_check(s)) {
8139             return;
8140         }
8141 
8142         fpst = NULL;
8143         break;
8144     case 0xc: /* FMAXNMP */
8145     case 0xd: /* FADDP */
8146     case 0xf: /* FMAXP */
8147     case 0x2c: /* FMINNMP */
8148     case 0x2f: /* FMINP */
8149         /* FP op, size[0] is 32 or 64 bit*/
8150         if (!u) {
8151             if ((size & 1) || !dc_isar_feature(aa64_fp16, s)) {
8152                 unallocated_encoding(s);
8153                 return;
8154             } else {
8155                 size = MO_16;
8156             }
8157         } else {
8158             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8159         }
8160 
8161         if (!fp_access_check(s)) {
8162             return;
8163         }
8164 
8165         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8166         break;
8167     default:
8168         unallocated_encoding(s);
8169         return;
8170     }
8171 
8172     if (size == MO_64) {
8173         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8174         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8175         TCGv_i64 tcg_res = tcg_temp_new_i64();
8176 
8177         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8178         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8179 
8180         switch (opcode) {
8181         case 0x3b: /* ADDP */
8182             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8183             break;
8184         case 0xc: /* FMAXNMP */
8185             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8186             break;
8187         case 0xd: /* FADDP */
8188             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8189             break;
8190         case 0xf: /* FMAXP */
8191             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8192             break;
8193         case 0x2c: /* FMINNMP */
8194             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8195             break;
8196         case 0x2f: /* FMINP */
8197             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8198             break;
8199         default:
8200             g_assert_not_reached();
8201         }
8202 
8203         write_fp_dreg(s, rd, tcg_res);
8204     } else {
8205         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8206         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8207         TCGv_i32 tcg_res = tcg_temp_new_i32();
8208 
8209         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8210         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8211 
8212         if (size == MO_16) {
8213             switch (opcode) {
8214             case 0xc: /* FMAXNMP */
8215                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8216                 break;
8217             case 0xd: /* FADDP */
8218                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8219                 break;
8220             case 0xf: /* FMAXP */
8221                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8222                 break;
8223             case 0x2c: /* FMINNMP */
8224                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8225                 break;
8226             case 0x2f: /* FMINP */
8227                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8228                 break;
8229             default:
8230                 g_assert_not_reached();
8231             }
8232         } else {
8233             switch (opcode) {
8234             case 0xc: /* FMAXNMP */
8235                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8236                 break;
8237             case 0xd: /* FADDP */
8238                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8239                 break;
8240             case 0xf: /* FMAXP */
8241                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8242                 break;
8243             case 0x2c: /* FMINNMP */
8244                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8245                 break;
8246             case 0x2f: /* FMINP */
8247                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8248                 break;
8249             default:
8250                 g_assert_not_reached();
8251             }
8252         }
8253 
8254         write_fp_sreg(s, rd, tcg_res);
8255     }
8256 }
8257 
8258 /*
8259  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8260  *
8261  * This code is handles the common shifting code and is used by both
8262  * the vector and scalar code.
8263  */
8264 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8265                                     TCGv_i64 tcg_rnd, bool accumulate,
8266                                     bool is_u, int size, int shift)
8267 {
8268     bool extended_result = false;
8269     bool round = tcg_rnd != NULL;
8270     int ext_lshift = 0;
8271     TCGv_i64 tcg_src_hi;
8272 
8273     if (round && size == 3) {
8274         extended_result = true;
8275         ext_lshift = 64 - shift;
8276         tcg_src_hi = tcg_temp_new_i64();
8277     } else if (shift == 64) {
8278         if (!accumulate && is_u) {
8279             /* result is zero */
8280             tcg_gen_movi_i64(tcg_res, 0);
8281             return;
8282         }
8283     }
8284 
8285     /* Deal with the rounding step */
8286     if (round) {
8287         if (extended_result) {
8288             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8289             if (!is_u) {
8290                 /* take care of sign extending tcg_res */
8291                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8292                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8293                                  tcg_src, tcg_src_hi,
8294                                  tcg_rnd, tcg_zero);
8295             } else {
8296                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8297                                  tcg_src, tcg_zero,
8298                                  tcg_rnd, tcg_zero);
8299             }
8300         } else {
8301             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8302         }
8303     }
8304 
8305     /* Now do the shift right */
8306     if (round && extended_result) {
8307         /* extended case, >64 bit precision required */
8308         if (ext_lshift == 0) {
8309             /* special case, only high bits matter */
8310             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8311         } else {
8312             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8313             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8314             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8315         }
8316     } else {
8317         if (is_u) {
8318             if (shift == 64) {
8319                 /* essentially shifting in 64 zeros */
8320                 tcg_gen_movi_i64(tcg_src, 0);
8321             } else {
8322                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8323             }
8324         } else {
8325             if (shift == 64) {
8326                 /* effectively extending the sign-bit */
8327                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8328             } else {
8329                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8330             }
8331         }
8332     }
8333 
8334     if (accumulate) {
8335         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8336     } else {
8337         tcg_gen_mov_i64(tcg_res, tcg_src);
8338     }
8339 }
8340 
8341 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8342 static void handle_scalar_simd_shri(DisasContext *s,
8343                                     bool is_u, int immh, int immb,
8344                                     int opcode, int rn, int rd)
8345 {
8346     const int size = 3;
8347     int immhb = immh << 3 | immb;
8348     int shift = 2 * (8 << size) - immhb;
8349     bool accumulate = false;
8350     bool round = false;
8351     bool insert = false;
8352     TCGv_i64 tcg_rn;
8353     TCGv_i64 tcg_rd;
8354     TCGv_i64 tcg_round;
8355 
8356     if (!extract32(immh, 3, 1)) {
8357         unallocated_encoding(s);
8358         return;
8359     }
8360 
8361     if (!fp_access_check(s)) {
8362         return;
8363     }
8364 
8365     switch (opcode) {
8366     case 0x02: /* SSRA / USRA (accumulate) */
8367         accumulate = true;
8368         break;
8369     case 0x04: /* SRSHR / URSHR (rounding) */
8370         round = true;
8371         break;
8372     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8373         accumulate = round = true;
8374         break;
8375     case 0x08: /* SRI */
8376         insert = true;
8377         break;
8378     }
8379 
8380     if (round) {
8381         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8382     } else {
8383         tcg_round = NULL;
8384     }
8385 
8386     tcg_rn = read_fp_dreg(s, rn);
8387     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8388 
8389     if (insert) {
8390         /* shift count same as element size is valid but does nothing;
8391          * special case to avoid potential shift by 64.
8392          */
8393         int esize = 8 << size;
8394         if (shift != esize) {
8395             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8396             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8397         }
8398     } else {
8399         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8400                                 accumulate, is_u, size, shift);
8401     }
8402 
8403     write_fp_dreg(s, rd, tcg_rd);
8404 }
8405 
8406 /* SHL/SLI - Scalar shift left */
8407 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8408                                     int immh, int immb, int opcode,
8409                                     int rn, int rd)
8410 {
8411     int size = 32 - clz32(immh) - 1;
8412     int immhb = immh << 3 | immb;
8413     int shift = immhb - (8 << size);
8414     TCGv_i64 tcg_rn;
8415     TCGv_i64 tcg_rd;
8416 
8417     if (!extract32(immh, 3, 1)) {
8418         unallocated_encoding(s);
8419         return;
8420     }
8421 
8422     if (!fp_access_check(s)) {
8423         return;
8424     }
8425 
8426     tcg_rn = read_fp_dreg(s, rn);
8427     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8428 
8429     if (insert) {
8430         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8431     } else {
8432         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8433     }
8434 
8435     write_fp_dreg(s, rd, tcg_rd);
8436 }
8437 
8438 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8439  * (signed/unsigned) narrowing */
8440 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8441                                    bool is_u_shift, bool is_u_narrow,
8442                                    int immh, int immb, int opcode,
8443                                    int rn, int rd)
8444 {
8445     int immhb = immh << 3 | immb;
8446     int size = 32 - clz32(immh) - 1;
8447     int esize = 8 << size;
8448     int shift = (2 * esize) - immhb;
8449     int elements = is_scalar ? 1 : (64 / esize);
8450     bool round = extract32(opcode, 0, 1);
8451     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8452     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8453     TCGv_i32 tcg_rd_narrowed;
8454     TCGv_i64 tcg_final;
8455 
8456     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8457         { gen_helper_neon_narrow_sat_s8,
8458           gen_helper_neon_unarrow_sat8 },
8459         { gen_helper_neon_narrow_sat_s16,
8460           gen_helper_neon_unarrow_sat16 },
8461         { gen_helper_neon_narrow_sat_s32,
8462           gen_helper_neon_unarrow_sat32 },
8463         { NULL, NULL },
8464     };
8465     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8466         gen_helper_neon_narrow_sat_u8,
8467         gen_helper_neon_narrow_sat_u16,
8468         gen_helper_neon_narrow_sat_u32,
8469         NULL
8470     };
8471     NeonGenNarrowEnvFn *narrowfn;
8472 
8473     int i;
8474 
8475     assert(size < 4);
8476 
8477     if (extract32(immh, 3, 1)) {
8478         unallocated_encoding(s);
8479         return;
8480     }
8481 
8482     if (!fp_access_check(s)) {
8483         return;
8484     }
8485 
8486     if (is_u_shift) {
8487         narrowfn = unsigned_narrow_fns[size];
8488     } else {
8489         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8490     }
8491 
8492     tcg_rn = tcg_temp_new_i64();
8493     tcg_rd = tcg_temp_new_i64();
8494     tcg_rd_narrowed = tcg_temp_new_i32();
8495     tcg_final = tcg_temp_new_i64();
8496 
8497     if (round) {
8498         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8499     } else {
8500         tcg_round = NULL;
8501     }
8502 
8503     for (i = 0; i < elements; i++) {
8504         read_vec_element(s, tcg_rn, rn, i, ldop);
8505         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8506                                 false, is_u_shift, size+1, shift);
8507         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8508         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8509         if (i == 0) {
8510             tcg_gen_extract_i64(tcg_final, tcg_rd, 0, esize);
8511         } else {
8512             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8513         }
8514     }
8515 
8516     if (!is_q) {
8517         write_vec_element(s, tcg_final, rd, 0, MO_64);
8518     } else {
8519         write_vec_element(s, tcg_final, rd, 1, MO_64);
8520     }
8521     clear_vec_high(s, is_q, rd);
8522 }
8523 
8524 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8525 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8526                              bool src_unsigned, bool dst_unsigned,
8527                              int immh, int immb, int rn, int rd)
8528 {
8529     int immhb = immh << 3 | immb;
8530     int size = 32 - clz32(immh) - 1;
8531     int shift = immhb - (8 << size);
8532     int pass;
8533 
8534     assert(immh != 0);
8535     assert(!(scalar && is_q));
8536 
8537     if (!scalar) {
8538         if (!is_q && extract32(immh, 3, 1)) {
8539             unallocated_encoding(s);
8540             return;
8541         }
8542 
8543         /* Since we use the variable-shift helpers we must
8544          * replicate the shift count into each element of
8545          * the tcg_shift value.
8546          */
8547         switch (size) {
8548         case 0:
8549             shift |= shift << 8;
8550             /* fall through */
8551         case 1:
8552             shift |= shift << 16;
8553             break;
8554         case 2:
8555         case 3:
8556             break;
8557         default:
8558             g_assert_not_reached();
8559         }
8560     }
8561 
8562     if (!fp_access_check(s)) {
8563         return;
8564     }
8565 
8566     if (size == 3) {
8567         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8568         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8569             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8570             { NULL, gen_helper_neon_qshl_u64 },
8571         };
8572         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8573         int maxpass = is_q ? 2 : 1;
8574 
8575         for (pass = 0; pass < maxpass; pass++) {
8576             TCGv_i64 tcg_op = tcg_temp_new_i64();
8577 
8578             read_vec_element(s, tcg_op, rn, pass, MO_64);
8579             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8580             write_vec_element(s, tcg_op, rd, pass, MO_64);
8581         }
8582         clear_vec_high(s, is_q, rd);
8583     } else {
8584         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8585         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8586             {
8587                 { gen_helper_neon_qshl_s8,
8588                   gen_helper_neon_qshl_s16,
8589                   gen_helper_neon_qshl_s32 },
8590                 { gen_helper_neon_qshlu_s8,
8591                   gen_helper_neon_qshlu_s16,
8592                   gen_helper_neon_qshlu_s32 }
8593             }, {
8594                 { NULL, NULL, NULL },
8595                 { gen_helper_neon_qshl_u8,
8596                   gen_helper_neon_qshl_u16,
8597                   gen_helper_neon_qshl_u32 }
8598             }
8599         };
8600         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8601         MemOp memop = scalar ? size : MO_32;
8602         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8603 
8604         for (pass = 0; pass < maxpass; pass++) {
8605             TCGv_i32 tcg_op = tcg_temp_new_i32();
8606 
8607             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8608             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8609             if (scalar) {
8610                 switch (size) {
8611                 case 0:
8612                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8613                     break;
8614                 case 1:
8615                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8616                     break;
8617                 case 2:
8618                     break;
8619                 default:
8620                     g_assert_not_reached();
8621                 }
8622                 write_fp_sreg(s, rd, tcg_op);
8623             } else {
8624                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8625             }
8626         }
8627 
8628         if (!scalar) {
8629             clear_vec_high(s, is_q, rd);
8630         }
8631     }
8632 }
8633 
8634 /* Common vector code for handling integer to FP conversion */
8635 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8636                                    int elements, int is_signed,
8637                                    int fracbits, int size)
8638 {
8639     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8640     TCGv_i32 tcg_shift = NULL;
8641 
8642     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8643     int pass;
8644 
8645     if (fracbits || size == MO_64) {
8646         tcg_shift = tcg_constant_i32(fracbits);
8647     }
8648 
8649     if (size == MO_64) {
8650         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8651         TCGv_i64 tcg_double = tcg_temp_new_i64();
8652 
8653         for (pass = 0; pass < elements; pass++) {
8654             read_vec_element(s, tcg_int64, rn, pass, mop);
8655 
8656             if (is_signed) {
8657                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8658                                      tcg_shift, tcg_fpst);
8659             } else {
8660                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8661                                      tcg_shift, tcg_fpst);
8662             }
8663             if (elements == 1) {
8664                 write_fp_dreg(s, rd, tcg_double);
8665             } else {
8666                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8667             }
8668         }
8669     } else {
8670         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8671         TCGv_i32 tcg_float = tcg_temp_new_i32();
8672 
8673         for (pass = 0; pass < elements; pass++) {
8674             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8675 
8676             switch (size) {
8677             case MO_32:
8678                 if (fracbits) {
8679                     if (is_signed) {
8680                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8681                                              tcg_shift, tcg_fpst);
8682                     } else {
8683                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8684                                              tcg_shift, tcg_fpst);
8685                     }
8686                 } else {
8687                     if (is_signed) {
8688                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8689                     } else {
8690                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8691                     }
8692                 }
8693                 break;
8694             case MO_16:
8695                 if (fracbits) {
8696                     if (is_signed) {
8697                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8698                                              tcg_shift, tcg_fpst);
8699                     } else {
8700                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8701                                              tcg_shift, tcg_fpst);
8702                     }
8703                 } else {
8704                     if (is_signed) {
8705                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8706                     } else {
8707                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8708                     }
8709                 }
8710                 break;
8711             default:
8712                 g_assert_not_reached();
8713             }
8714 
8715             if (elements == 1) {
8716                 write_fp_sreg(s, rd, tcg_float);
8717             } else {
8718                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8719             }
8720         }
8721     }
8722 
8723     clear_vec_high(s, elements << size == 16, rd);
8724 }
8725 
8726 /* UCVTF/SCVTF - Integer to FP conversion */
8727 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8728                                          bool is_q, bool is_u,
8729                                          int immh, int immb, int opcode,
8730                                          int rn, int rd)
8731 {
8732     int size, elements, fracbits;
8733     int immhb = immh << 3 | immb;
8734 
8735     if (immh & 8) {
8736         size = MO_64;
8737         if (!is_scalar && !is_q) {
8738             unallocated_encoding(s);
8739             return;
8740         }
8741     } else if (immh & 4) {
8742         size = MO_32;
8743     } else if (immh & 2) {
8744         size = MO_16;
8745         if (!dc_isar_feature(aa64_fp16, s)) {
8746             unallocated_encoding(s);
8747             return;
8748         }
8749     } else {
8750         /* immh == 0 would be a failure of the decode logic */
8751         g_assert(immh == 1);
8752         unallocated_encoding(s);
8753         return;
8754     }
8755 
8756     if (is_scalar) {
8757         elements = 1;
8758     } else {
8759         elements = (8 << is_q) >> size;
8760     }
8761     fracbits = (16 << size) - immhb;
8762 
8763     if (!fp_access_check(s)) {
8764         return;
8765     }
8766 
8767     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8768 }
8769 
8770 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8771 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8772                                          bool is_q, bool is_u,
8773                                          int immh, int immb, int rn, int rd)
8774 {
8775     int immhb = immh << 3 | immb;
8776     int pass, size, fracbits;
8777     TCGv_ptr tcg_fpstatus;
8778     TCGv_i32 tcg_rmode, tcg_shift;
8779 
8780     if (immh & 0x8) {
8781         size = MO_64;
8782         if (!is_scalar && !is_q) {
8783             unallocated_encoding(s);
8784             return;
8785         }
8786     } else if (immh & 0x4) {
8787         size = MO_32;
8788     } else if (immh & 0x2) {
8789         size = MO_16;
8790         if (!dc_isar_feature(aa64_fp16, s)) {
8791             unallocated_encoding(s);
8792             return;
8793         }
8794     } else {
8795         /* Should have split out AdvSIMD modified immediate earlier.  */
8796         assert(immh == 1);
8797         unallocated_encoding(s);
8798         return;
8799     }
8800 
8801     if (!fp_access_check(s)) {
8802         return;
8803     }
8804 
8805     assert(!(is_scalar && is_q));
8806 
8807     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8808     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8809     fracbits = (16 << size) - immhb;
8810     tcg_shift = tcg_constant_i32(fracbits);
8811 
8812     if (size == MO_64) {
8813         int maxpass = is_scalar ? 1 : 2;
8814 
8815         for (pass = 0; pass < maxpass; pass++) {
8816             TCGv_i64 tcg_op = tcg_temp_new_i64();
8817 
8818             read_vec_element(s, tcg_op, rn, pass, MO_64);
8819             if (is_u) {
8820                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8821             } else {
8822                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8823             }
8824             write_vec_element(s, tcg_op, rd, pass, MO_64);
8825         }
8826         clear_vec_high(s, is_q, rd);
8827     } else {
8828         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8829         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8830 
8831         switch (size) {
8832         case MO_16:
8833             if (is_u) {
8834                 fn = gen_helper_vfp_touhh;
8835             } else {
8836                 fn = gen_helper_vfp_toshh;
8837             }
8838             break;
8839         case MO_32:
8840             if (is_u) {
8841                 fn = gen_helper_vfp_touls;
8842             } else {
8843                 fn = gen_helper_vfp_tosls;
8844             }
8845             break;
8846         default:
8847             g_assert_not_reached();
8848         }
8849 
8850         for (pass = 0; pass < maxpass; pass++) {
8851             TCGv_i32 tcg_op = tcg_temp_new_i32();
8852 
8853             read_vec_element_i32(s, tcg_op, rn, pass, size);
8854             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8855             if (is_scalar) {
8856                 if (size == MO_16 && !is_u) {
8857                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8858                 }
8859                 write_fp_sreg(s, rd, tcg_op);
8860             } else {
8861                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8862             }
8863         }
8864         if (!is_scalar) {
8865             clear_vec_high(s, is_q, rd);
8866         }
8867     }
8868 
8869     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8870 }
8871 
8872 /* AdvSIMD scalar shift by immediate
8873  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8874  * +-----+---+-------------+------+------+--------+---+------+------+
8875  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8876  * +-----+---+-------------+------+------+--------+---+------+------+
8877  *
8878  * This is the scalar version so it works on a fixed sized registers
8879  */
8880 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8881 {
8882     int rd = extract32(insn, 0, 5);
8883     int rn = extract32(insn, 5, 5);
8884     int opcode = extract32(insn, 11, 5);
8885     int immb = extract32(insn, 16, 3);
8886     int immh = extract32(insn, 19, 4);
8887     bool is_u = extract32(insn, 29, 1);
8888 
8889     if (immh == 0) {
8890         unallocated_encoding(s);
8891         return;
8892     }
8893 
8894     switch (opcode) {
8895     case 0x08: /* SRI */
8896         if (!is_u) {
8897             unallocated_encoding(s);
8898             return;
8899         }
8900         /* fall through */
8901     case 0x00: /* SSHR / USHR */
8902     case 0x02: /* SSRA / USRA */
8903     case 0x04: /* SRSHR / URSHR */
8904     case 0x06: /* SRSRA / URSRA */
8905         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8906         break;
8907     case 0x0a: /* SHL / SLI */
8908         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8909         break;
8910     case 0x1c: /* SCVTF, UCVTF */
8911         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8912                                      opcode, rn, rd);
8913         break;
8914     case 0x10: /* SQSHRUN, SQSHRUN2 */
8915     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8916         if (!is_u) {
8917             unallocated_encoding(s);
8918             return;
8919         }
8920         handle_vec_simd_sqshrn(s, true, false, false, true,
8921                                immh, immb, opcode, rn, rd);
8922         break;
8923     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8924     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8925         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8926                                immh, immb, opcode, rn, rd);
8927         break;
8928     case 0xc: /* SQSHLU */
8929         if (!is_u) {
8930             unallocated_encoding(s);
8931             return;
8932         }
8933         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8934         break;
8935     case 0xe: /* SQSHL, UQSHL */
8936         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8937         break;
8938     case 0x1f: /* FCVTZS, FCVTZU */
8939         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8940         break;
8941     default:
8942         unallocated_encoding(s);
8943         break;
8944     }
8945 }
8946 
8947 /* AdvSIMD scalar three different
8948  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8949  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8950  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8951  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8952  */
8953 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8954 {
8955     bool is_u = extract32(insn, 29, 1);
8956     int size = extract32(insn, 22, 2);
8957     int opcode = extract32(insn, 12, 4);
8958     int rm = extract32(insn, 16, 5);
8959     int rn = extract32(insn, 5, 5);
8960     int rd = extract32(insn, 0, 5);
8961 
8962     if (is_u) {
8963         unallocated_encoding(s);
8964         return;
8965     }
8966 
8967     switch (opcode) {
8968     case 0x9: /* SQDMLAL, SQDMLAL2 */
8969     case 0xb: /* SQDMLSL, SQDMLSL2 */
8970     case 0xd: /* SQDMULL, SQDMULL2 */
8971         if (size == 0 || size == 3) {
8972             unallocated_encoding(s);
8973             return;
8974         }
8975         break;
8976     default:
8977         unallocated_encoding(s);
8978         return;
8979     }
8980 
8981     if (!fp_access_check(s)) {
8982         return;
8983     }
8984 
8985     if (size == 2) {
8986         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8987         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8988         TCGv_i64 tcg_res = tcg_temp_new_i64();
8989 
8990         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8991         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8992 
8993         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8994         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
8995 
8996         switch (opcode) {
8997         case 0xd: /* SQDMULL, SQDMULL2 */
8998             break;
8999         case 0xb: /* SQDMLSL, SQDMLSL2 */
9000             tcg_gen_neg_i64(tcg_res, tcg_res);
9001             /* fall through */
9002         case 0x9: /* SQDMLAL, SQDMLAL2 */
9003             read_vec_element(s, tcg_op1, rd, 0, MO_64);
9004             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
9005                                               tcg_res, tcg_op1);
9006             break;
9007         default:
9008             g_assert_not_reached();
9009         }
9010 
9011         write_fp_dreg(s, rd, tcg_res);
9012     } else {
9013         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9014         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9015         TCGv_i64 tcg_res = tcg_temp_new_i64();
9016 
9017         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9018         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
9019 
9020         switch (opcode) {
9021         case 0xd: /* SQDMULL, SQDMULL2 */
9022             break;
9023         case 0xb: /* SQDMLSL, SQDMLSL2 */
9024             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9025             /* fall through */
9026         case 0x9: /* SQDMLAL, SQDMLAL2 */
9027         {
9028             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9029             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9030             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
9031                                               tcg_res, tcg_op3);
9032             break;
9033         }
9034         default:
9035             g_assert_not_reached();
9036         }
9037 
9038         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9039         write_fp_dreg(s, rd, tcg_res);
9040     }
9041 }
9042 
9043 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9044                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9045 {
9046     /* Handle 64x64->64 opcodes which are shared between the scalar
9047      * and vector 3-same groups. We cover every opcode where size == 3
9048      * is valid in either the three-reg-same (integer, not pairwise)
9049      * or scalar-three-reg-same groups.
9050      */
9051     TCGCond cond;
9052 
9053     switch (opcode) {
9054     case 0x1: /* SQADD */
9055         if (u) {
9056             gen_helper_neon_qadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9057         } else {
9058             gen_helper_neon_qadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9059         }
9060         break;
9061     case 0x5: /* SQSUB */
9062         if (u) {
9063             gen_helper_neon_qsub_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9064         } else {
9065             gen_helper_neon_qsub_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9066         }
9067         break;
9068     case 0x6: /* CMGT, CMHI */
9069         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9070     do_cmop:
9071         /* 64 bit integer comparison, result = test ? -1 : 0. */
9072         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9073         break;
9074     case 0x7: /* CMGE, CMHS */
9075         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9076         goto do_cmop;
9077     case 0x11: /* CMTST, CMEQ */
9078         if (u) {
9079             cond = TCG_COND_EQ;
9080             goto do_cmop;
9081         }
9082         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9083         break;
9084     case 0x8: /* SSHL, USHL */
9085         if (u) {
9086             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9087         } else {
9088             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9089         }
9090         break;
9091     case 0x9: /* SQSHL, UQSHL */
9092         if (u) {
9093             gen_helper_neon_qshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9094         } else {
9095             gen_helper_neon_qshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9096         }
9097         break;
9098     case 0xa: /* SRSHL, URSHL */
9099         if (u) {
9100             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9101         } else {
9102             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9103         }
9104         break;
9105     case 0xb: /* SQRSHL, UQRSHL */
9106         if (u) {
9107             gen_helper_neon_qrshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9108         } else {
9109             gen_helper_neon_qrshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9110         }
9111         break;
9112     case 0x10: /* ADD, SUB */
9113         if (u) {
9114             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9115         } else {
9116             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9117         }
9118         break;
9119     default:
9120         g_assert_not_reached();
9121     }
9122 }
9123 
9124 /* Handle the 3-same-operands float operations; shared by the scalar
9125  * and vector encodings. The caller must filter out any encodings
9126  * not allocated for the encoding it is dealing with.
9127  */
9128 static void handle_3same_float(DisasContext *s, int size, int elements,
9129                                int fpopcode, int rd, int rn, int rm)
9130 {
9131     int pass;
9132     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9133 
9134     for (pass = 0; pass < elements; pass++) {
9135         if (size) {
9136             /* Double */
9137             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9138             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9139             TCGv_i64 tcg_res = tcg_temp_new_i64();
9140 
9141             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9142             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9143 
9144             switch (fpopcode) {
9145             case 0x39: /* FMLS */
9146                 /* As usual for ARM, separate negation for fused multiply-add */
9147                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9148                 /* fall through */
9149             case 0x19: /* FMLA */
9150                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9151                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9152                                        tcg_res, fpst);
9153                 break;
9154             case 0x18: /* FMAXNM */
9155                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9156                 break;
9157             case 0x1a: /* FADD */
9158                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9159                 break;
9160             case 0x1b: /* FMULX */
9161                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9162                 break;
9163             case 0x1c: /* FCMEQ */
9164                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9165                 break;
9166             case 0x1e: /* FMAX */
9167                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9168                 break;
9169             case 0x1f: /* FRECPS */
9170                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9171                 break;
9172             case 0x38: /* FMINNM */
9173                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9174                 break;
9175             case 0x3a: /* FSUB */
9176                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9177                 break;
9178             case 0x3e: /* FMIN */
9179                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9180                 break;
9181             case 0x3f: /* FRSQRTS */
9182                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9183                 break;
9184             case 0x5b: /* FMUL */
9185                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9186                 break;
9187             case 0x5c: /* FCMGE */
9188                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9189                 break;
9190             case 0x5d: /* FACGE */
9191                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9192                 break;
9193             case 0x5f: /* FDIV */
9194                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9195                 break;
9196             case 0x7a: /* FABD */
9197                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9198                 gen_helper_vfp_absd(tcg_res, tcg_res);
9199                 break;
9200             case 0x7c: /* FCMGT */
9201                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9202                 break;
9203             case 0x7d: /* FACGT */
9204                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9205                 break;
9206             default:
9207                 g_assert_not_reached();
9208             }
9209 
9210             write_vec_element(s, tcg_res, rd, pass, MO_64);
9211         } else {
9212             /* Single */
9213             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9214             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9215             TCGv_i32 tcg_res = tcg_temp_new_i32();
9216 
9217             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9218             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9219 
9220             switch (fpopcode) {
9221             case 0x39: /* FMLS */
9222                 /* As usual for ARM, separate negation for fused multiply-add */
9223                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9224                 /* fall through */
9225             case 0x19: /* FMLA */
9226                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9227                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9228                                        tcg_res, fpst);
9229                 break;
9230             case 0x1a: /* FADD */
9231                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9232                 break;
9233             case 0x1b: /* FMULX */
9234                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9235                 break;
9236             case 0x1c: /* FCMEQ */
9237                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9238                 break;
9239             case 0x1e: /* FMAX */
9240                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9241                 break;
9242             case 0x1f: /* FRECPS */
9243                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9244                 break;
9245             case 0x18: /* FMAXNM */
9246                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9247                 break;
9248             case 0x38: /* FMINNM */
9249                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9250                 break;
9251             case 0x3a: /* FSUB */
9252                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9253                 break;
9254             case 0x3e: /* FMIN */
9255                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9256                 break;
9257             case 0x3f: /* FRSQRTS */
9258                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9259                 break;
9260             case 0x5b: /* FMUL */
9261                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9262                 break;
9263             case 0x5c: /* FCMGE */
9264                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9265                 break;
9266             case 0x5d: /* FACGE */
9267                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9268                 break;
9269             case 0x5f: /* FDIV */
9270                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9271                 break;
9272             case 0x7a: /* FABD */
9273                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9274                 gen_helper_vfp_abss(tcg_res, tcg_res);
9275                 break;
9276             case 0x7c: /* FCMGT */
9277                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9278                 break;
9279             case 0x7d: /* FACGT */
9280                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9281                 break;
9282             default:
9283                 g_assert_not_reached();
9284             }
9285 
9286             if (elements == 1) {
9287                 /* scalar single so clear high part */
9288                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9289 
9290                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9291                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9292             } else {
9293                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9294             }
9295         }
9296     }
9297 
9298     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9299 }
9300 
9301 /* AdvSIMD scalar three same
9302  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9303  * +-----+---+-----------+------+---+------+--------+---+------+------+
9304  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9305  * +-----+---+-----------+------+---+------+--------+---+------+------+
9306  */
9307 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9308 {
9309     int rd = extract32(insn, 0, 5);
9310     int rn = extract32(insn, 5, 5);
9311     int opcode = extract32(insn, 11, 5);
9312     int rm = extract32(insn, 16, 5);
9313     int size = extract32(insn, 22, 2);
9314     bool u = extract32(insn, 29, 1);
9315     TCGv_i64 tcg_rd;
9316 
9317     if (opcode >= 0x18) {
9318         /* Floating point: U, size[1] and opcode indicate operation */
9319         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9320         switch (fpopcode) {
9321         case 0x1b: /* FMULX */
9322         case 0x1f: /* FRECPS */
9323         case 0x3f: /* FRSQRTS */
9324         case 0x5d: /* FACGE */
9325         case 0x7d: /* FACGT */
9326         case 0x1c: /* FCMEQ */
9327         case 0x5c: /* FCMGE */
9328         case 0x7c: /* FCMGT */
9329         case 0x7a: /* FABD */
9330             break;
9331         default:
9332             unallocated_encoding(s);
9333             return;
9334         }
9335 
9336         if (!fp_access_check(s)) {
9337             return;
9338         }
9339 
9340         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9341         return;
9342     }
9343 
9344     switch (opcode) {
9345     case 0x1: /* SQADD, UQADD */
9346     case 0x5: /* SQSUB, UQSUB */
9347     case 0x9: /* SQSHL, UQSHL */
9348     case 0xb: /* SQRSHL, UQRSHL */
9349         break;
9350     case 0x8: /* SSHL, USHL */
9351     case 0xa: /* SRSHL, URSHL */
9352     case 0x6: /* CMGT, CMHI */
9353     case 0x7: /* CMGE, CMHS */
9354     case 0x11: /* CMTST, CMEQ */
9355     case 0x10: /* ADD, SUB (vector) */
9356         if (size != 3) {
9357             unallocated_encoding(s);
9358             return;
9359         }
9360         break;
9361     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9362         if (size != 1 && size != 2) {
9363             unallocated_encoding(s);
9364             return;
9365         }
9366         break;
9367     default:
9368         unallocated_encoding(s);
9369         return;
9370     }
9371 
9372     if (!fp_access_check(s)) {
9373         return;
9374     }
9375 
9376     tcg_rd = tcg_temp_new_i64();
9377 
9378     if (size == 3) {
9379         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9380         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9381 
9382         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9383     } else {
9384         /* Do a single operation on the lowest element in the vector.
9385          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9386          * no side effects for all these operations.
9387          * OPTME: special-purpose helpers would avoid doing some
9388          * unnecessary work in the helper for the 8 and 16 bit cases.
9389          */
9390         NeonGenTwoOpEnvFn *genenvfn;
9391         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9392         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9393         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9394 
9395         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9396         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9397 
9398         switch (opcode) {
9399         case 0x1: /* SQADD, UQADD */
9400         {
9401             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9402                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9403                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9404                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9405             };
9406             genenvfn = fns[size][u];
9407             break;
9408         }
9409         case 0x5: /* SQSUB, UQSUB */
9410         {
9411             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9412                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9413                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9414                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9415             };
9416             genenvfn = fns[size][u];
9417             break;
9418         }
9419         case 0x9: /* SQSHL, UQSHL */
9420         {
9421             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9422                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9423                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9424                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9425             };
9426             genenvfn = fns[size][u];
9427             break;
9428         }
9429         case 0xb: /* SQRSHL, UQRSHL */
9430         {
9431             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9432                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9433                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9434                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9435             };
9436             genenvfn = fns[size][u];
9437             break;
9438         }
9439         case 0x16: /* SQDMULH, SQRDMULH */
9440         {
9441             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9442                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9443                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9444             };
9445             assert(size == 1 || size == 2);
9446             genenvfn = fns[size - 1][u];
9447             break;
9448         }
9449         default:
9450             g_assert_not_reached();
9451         }
9452 
9453         genenvfn(tcg_rd32, tcg_env, tcg_rn, tcg_rm);
9454         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9455     }
9456 
9457     write_fp_dreg(s, rd, tcg_rd);
9458 }
9459 
9460 /* AdvSIMD scalar three same FP16
9461  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9462  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9463  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9464  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9465  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9466  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9467  */
9468 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9469                                                   uint32_t insn)
9470 {
9471     int rd = extract32(insn, 0, 5);
9472     int rn = extract32(insn, 5, 5);
9473     int opcode = extract32(insn, 11, 3);
9474     int rm = extract32(insn, 16, 5);
9475     bool u = extract32(insn, 29, 1);
9476     bool a = extract32(insn, 23, 1);
9477     int fpopcode = opcode | (a << 3) |  (u << 4);
9478     TCGv_ptr fpst;
9479     TCGv_i32 tcg_op1;
9480     TCGv_i32 tcg_op2;
9481     TCGv_i32 tcg_res;
9482 
9483     switch (fpopcode) {
9484     case 0x03: /* FMULX */
9485     case 0x04: /* FCMEQ (reg) */
9486     case 0x07: /* FRECPS */
9487     case 0x0f: /* FRSQRTS */
9488     case 0x14: /* FCMGE (reg) */
9489     case 0x15: /* FACGE */
9490     case 0x1a: /* FABD */
9491     case 0x1c: /* FCMGT (reg) */
9492     case 0x1d: /* FACGT */
9493         break;
9494     default:
9495         unallocated_encoding(s);
9496         return;
9497     }
9498 
9499     if (!dc_isar_feature(aa64_fp16, s)) {
9500         unallocated_encoding(s);
9501     }
9502 
9503     if (!fp_access_check(s)) {
9504         return;
9505     }
9506 
9507     fpst = fpstatus_ptr(FPST_FPCR_F16);
9508 
9509     tcg_op1 = read_fp_hreg(s, rn);
9510     tcg_op2 = read_fp_hreg(s, rm);
9511     tcg_res = tcg_temp_new_i32();
9512 
9513     switch (fpopcode) {
9514     case 0x03: /* FMULX */
9515         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9516         break;
9517     case 0x04: /* FCMEQ (reg) */
9518         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9519         break;
9520     case 0x07: /* FRECPS */
9521         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9522         break;
9523     case 0x0f: /* FRSQRTS */
9524         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9525         break;
9526     case 0x14: /* FCMGE (reg) */
9527         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9528         break;
9529     case 0x15: /* FACGE */
9530         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9531         break;
9532     case 0x1a: /* FABD */
9533         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9534         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9535         break;
9536     case 0x1c: /* FCMGT (reg) */
9537         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9538         break;
9539     case 0x1d: /* FACGT */
9540         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9541         break;
9542     default:
9543         g_assert_not_reached();
9544     }
9545 
9546     write_fp_sreg(s, rd, tcg_res);
9547 }
9548 
9549 /* AdvSIMD scalar three same extra
9550  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9551  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9552  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9553  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9554  */
9555 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9556                                                    uint32_t insn)
9557 {
9558     int rd = extract32(insn, 0, 5);
9559     int rn = extract32(insn, 5, 5);
9560     int opcode = extract32(insn, 11, 4);
9561     int rm = extract32(insn, 16, 5);
9562     int size = extract32(insn, 22, 2);
9563     bool u = extract32(insn, 29, 1);
9564     TCGv_i32 ele1, ele2, ele3;
9565     TCGv_i64 res;
9566     bool feature;
9567 
9568     switch (u * 16 + opcode) {
9569     case 0x10: /* SQRDMLAH (vector) */
9570     case 0x11: /* SQRDMLSH (vector) */
9571         if (size != 1 && size != 2) {
9572             unallocated_encoding(s);
9573             return;
9574         }
9575         feature = dc_isar_feature(aa64_rdm, s);
9576         break;
9577     default:
9578         unallocated_encoding(s);
9579         return;
9580     }
9581     if (!feature) {
9582         unallocated_encoding(s);
9583         return;
9584     }
9585     if (!fp_access_check(s)) {
9586         return;
9587     }
9588 
9589     /* Do a single operation on the lowest element in the vector.
9590      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9591      * with no side effects for all these operations.
9592      * OPTME: special-purpose helpers would avoid doing some
9593      * unnecessary work in the helper for the 16 bit cases.
9594      */
9595     ele1 = tcg_temp_new_i32();
9596     ele2 = tcg_temp_new_i32();
9597     ele3 = tcg_temp_new_i32();
9598 
9599     read_vec_element_i32(s, ele1, rn, 0, size);
9600     read_vec_element_i32(s, ele2, rm, 0, size);
9601     read_vec_element_i32(s, ele3, rd, 0, size);
9602 
9603     switch (opcode) {
9604     case 0x0: /* SQRDMLAH */
9605         if (size == 1) {
9606             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9607         } else {
9608             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9609         }
9610         break;
9611     case 0x1: /* SQRDMLSH */
9612         if (size == 1) {
9613             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9614         } else {
9615             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9616         }
9617         break;
9618     default:
9619         g_assert_not_reached();
9620     }
9621 
9622     res = tcg_temp_new_i64();
9623     tcg_gen_extu_i32_i64(res, ele3);
9624     write_fp_dreg(s, rd, res);
9625 }
9626 
9627 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9628                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9629                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9630 {
9631     /* Handle 64->64 opcodes which are shared between the scalar and
9632      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9633      * is valid in either group and also the double-precision fp ops.
9634      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9635      * requires them.
9636      */
9637     TCGCond cond;
9638 
9639     switch (opcode) {
9640     case 0x4: /* CLS, CLZ */
9641         if (u) {
9642             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9643         } else {
9644             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9645         }
9646         break;
9647     case 0x5: /* NOT */
9648         /* This opcode is shared with CNT and RBIT but we have earlier
9649          * enforced that size == 3 if and only if this is the NOT insn.
9650          */
9651         tcg_gen_not_i64(tcg_rd, tcg_rn);
9652         break;
9653     case 0x7: /* SQABS, SQNEG */
9654         if (u) {
9655             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9656         } else {
9657             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9658         }
9659         break;
9660     case 0xa: /* CMLT */
9661         cond = TCG_COND_LT;
9662     do_cmop:
9663         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9664         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9665         break;
9666     case 0x8: /* CMGT, CMGE */
9667         cond = u ? TCG_COND_GE : TCG_COND_GT;
9668         goto do_cmop;
9669     case 0x9: /* CMEQ, CMLE */
9670         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9671         goto do_cmop;
9672     case 0xb: /* ABS, NEG */
9673         if (u) {
9674             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9675         } else {
9676             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9677         }
9678         break;
9679     case 0x2f: /* FABS */
9680         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9681         break;
9682     case 0x6f: /* FNEG */
9683         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9684         break;
9685     case 0x7f: /* FSQRT */
9686         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9687         break;
9688     case 0x1a: /* FCVTNS */
9689     case 0x1b: /* FCVTMS */
9690     case 0x1c: /* FCVTAS */
9691     case 0x3a: /* FCVTPS */
9692     case 0x3b: /* FCVTZS */
9693         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9694         break;
9695     case 0x5a: /* FCVTNU */
9696     case 0x5b: /* FCVTMU */
9697     case 0x5c: /* FCVTAU */
9698     case 0x7a: /* FCVTPU */
9699     case 0x7b: /* FCVTZU */
9700         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9701         break;
9702     case 0x18: /* FRINTN */
9703     case 0x19: /* FRINTM */
9704     case 0x38: /* FRINTP */
9705     case 0x39: /* FRINTZ */
9706     case 0x58: /* FRINTA */
9707     case 0x79: /* FRINTI */
9708         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9709         break;
9710     case 0x59: /* FRINTX */
9711         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9712         break;
9713     case 0x1e: /* FRINT32Z */
9714     case 0x5e: /* FRINT32X */
9715         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9716         break;
9717     case 0x1f: /* FRINT64Z */
9718     case 0x5f: /* FRINT64X */
9719         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9720         break;
9721     default:
9722         g_assert_not_reached();
9723     }
9724 }
9725 
9726 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9727                                    bool is_scalar, bool is_u, bool is_q,
9728                                    int size, int rn, int rd)
9729 {
9730     bool is_double = (size == MO_64);
9731     TCGv_ptr fpst;
9732 
9733     if (!fp_access_check(s)) {
9734         return;
9735     }
9736 
9737     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9738 
9739     if (is_double) {
9740         TCGv_i64 tcg_op = tcg_temp_new_i64();
9741         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9742         TCGv_i64 tcg_res = tcg_temp_new_i64();
9743         NeonGenTwoDoubleOpFn *genfn;
9744         bool swap = false;
9745         int pass;
9746 
9747         switch (opcode) {
9748         case 0x2e: /* FCMLT (zero) */
9749             swap = true;
9750             /* fallthrough */
9751         case 0x2c: /* FCMGT (zero) */
9752             genfn = gen_helper_neon_cgt_f64;
9753             break;
9754         case 0x2d: /* FCMEQ (zero) */
9755             genfn = gen_helper_neon_ceq_f64;
9756             break;
9757         case 0x6d: /* FCMLE (zero) */
9758             swap = true;
9759             /* fall through */
9760         case 0x6c: /* FCMGE (zero) */
9761             genfn = gen_helper_neon_cge_f64;
9762             break;
9763         default:
9764             g_assert_not_reached();
9765         }
9766 
9767         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9768             read_vec_element(s, tcg_op, rn, pass, MO_64);
9769             if (swap) {
9770                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9771             } else {
9772                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9773             }
9774             write_vec_element(s, tcg_res, rd, pass, MO_64);
9775         }
9776 
9777         clear_vec_high(s, !is_scalar, rd);
9778     } else {
9779         TCGv_i32 tcg_op = tcg_temp_new_i32();
9780         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9781         TCGv_i32 tcg_res = tcg_temp_new_i32();
9782         NeonGenTwoSingleOpFn *genfn;
9783         bool swap = false;
9784         int pass, maxpasses;
9785 
9786         if (size == MO_16) {
9787             switch (opcode) {
9788             case 0x2e: /* FCMLT (zero) */
9789                 swap = true;
9790                 /* fall through */
9791             case 0x2c: /* FCMGT (zero) */
9792                 genfn = gen_helper_advsimd_cgt_f16;
9793                 break;
9794             case 0x2d: /* FCMEQ (zero) */
9795                 genfn = gen_helper_advsimd_ceq_f16;
9796                 break;
9797             case 0x6d: /* FCMLE (zero) */
9798                 swap = true;
9799                 /* fall through */
9800             case 0x6c: /* FCMGE (zero) */
9801                 genfn = gen_helper_advsimd_cge_f16;
9802                 break;
9803             default:
9804                 g_assert_not_reached();
9805             }
9806         } else {
9807             switch (opcode) {
9808             case 0x2e: /* FCMLT (zero) */
9809                 swap = true;
9810                 /* fall through */
9811             case 0x2c: /* FCMGT (zero) */
9812                 genfn = gen_helper_neon_cgt_f32;
9813                 break;
9814             case 0x2d: /* FCMEQ (zero) */
9815                 genfn = gen_helper_neon_ceq_f32;
9816                 break;
9817             case 0x6d: /* FCMLE (zero) */
9818                 swap = true;
9819                 /* fall through */
9820             case 0x6c: /* FCMGE (zero) */
9821                 genfn = gen_helper_neon_cge_f32;
9822                 break;
9823             default:
9824                 g_assert_not_reached();
9825             }
9826         }
9827 
9828         if (is_scalar) {
9829             maxpasses = 1;
9830         } else {
9831             int vector_size = 8 << is_q;
9832             maxpasses = vector_size >> size;
9833         }
9834 
9835         for (pass = 0; pass < maxpasses; pass++) {
9836             read_vec_element_i32(s, tcg_op, rn, pass, size);
9837             if (swap) {
9838                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9839             } else {
9840                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9841             }
9842             if (is_scalar) {
9843                 write_fp_sreg(s, rd, tcg_res);
9844             } else {
9845                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9846             }
9847         }
9848 
9849         if (!is_scalar) {
9850             clear_vec_high(s, is_q, rd);
9851         }
9852     }
9853 }
9854 
9855 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9856                                     bool is_scalar, bool is_u, bool is_q,
9857                                     int size, int rn, int rd)
9858 {
9859     bool is_double = (size == 3);
9860     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9861 
9862     if (is_double) {
9863         TCGv_i64 tcg_op = tcg_temp_new_i64();
9864         TCGv_i64 tcg_res = tcg_temp_new_i64();
9865         int pass;
9866 
9867         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9868             read_vec_element(s, tcg_op, rn, pass, MO_64);
9869             switch (opcode) {
9870             case 0x3d: /* FRECPE */
9871                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9872                 break;
9873             case 0x3f: /* FRECPX */
9874                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9875                 break;
9876             case 0x7d: /* FRSQRTE */
9877                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9878                 break;
9879             default:
9880                 g_assert_not_reached();
9881             }
9882             write_vec_element(s, tcg_res, rd, pass, MO_64);
9883         }
9884         clear_vec_high(s, !is_scalar, rd);
9885     } else {
9886         TCGv_i32 tcg_op = tcg_temp_new_i32();
9887         TCGv_i32 tcg_res = tcg_temp_new_i32();
9888         int pass, maxpasses;
9889 
9890         if (is_scalar) {
9891             maxpasses = 1;
9892         } else {
9893             maxpasses = is_q ? 4 : 2;
9894         }
9895 
9896         for (pass = 0; pass < maxpasses; pass++) {
9897             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9898 
9899             switch (opcode) {
9900             case 0x3c: /* URECPE */
9901                 gen_helper_recpe_u32(tcg_res, tcg_op);
9902                 break;
9903             case 0x3d: /* FRECPE */
9904                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9905                 break;
9906             case 0x3f: /* FRECPX */
9907                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9908                 break;
9909             case 0x7d: /* FRSQRTE */
9910                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9911                 break;
9912             default:
9913                 g_assert_not_reached();
9914             }
9915 
9916             if (is_scalar) {
9917                 write_fp_sreg(s, rd, tcg_res);
9918             } else {
9919                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9920             }
9921         }
9922         if (!is_scalar) {
9923             clear_vec_high(s, is_q, rd);
9924         }
9925     }
9926 }
9927 
9928 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9929                                 int opcode, bool u, bool is_q,
9930                                 int size, int rn, int rd)
9931 {
9932     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9933      * in the source becomes a size element in the destination).
9934      */
9935     int pass;
9936     TCGv_i32 tcg_res[2];
9937     int destelt = is_q ? 2 : 0;
9938     int passes = scalar ? 1 : 2;
9939 
9940     if (scalar) {
9941         tcg_res[1] = tcg_constant_i32(0);
9942     }
9943 
9944     for (pass = 0; pass < passes; pass++) {
9945         TCGv_i64 tcg_op = tcg_temp_new_i64();
9946         NeonGenNarrowFn *genfn = NULL;
9947         NeonGenNarrowEnvFn *genenvfn = NULL;
9948 
9949         if (scalar) {
9950             read_vec_element(s, tcg_op, rn, pass, size + 1);
9951         } else {
9952             read_vec_element(s, tcg_op, rn, pass, MO_64);
9953         }
9954         tcg_res[pass] = tcg_temp_new_i32();
9955 
9956         switch (opcode) {
9957         case 0x12: /* XTN, SQXTUN */
9958         {
9959             static NeonGenNarrowFn * const xtnfns[3] = {
9960                 gen_helper_neon_narrow_u8,
9961                 gen_helper_neon_narrow_u16,
9962                 tcg_gen_extrl_i64_i32,
9963             };
9964             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9965                 gen_helper_neon_unarrow_sat8,
9966                 gen_helper_neon_unarrow_sat16,
9967                 gen_helper_neon_unarrow_sat32,
9968             };
9969             if (u) {
9970                 genenvfn = sqxtunfns[size];
9971             } else {
9972                 genfn = xtnfns[size];
9973             }
9974             break;
9975         }
9976         case 0x14: /* SQXTN, UQXTN */
9977         {
9978             static NeonGenNarrowEnvFn * const fns[3][2] = {
9979                 { gen_helper_neon_narrow_sat_s8,
9980                   gen_helper_neon_narrow_sat_u8 },
9981                 { gen_helper_neon_narrow_sat_s16,
9982                   gen_helper_neon_narrow_sat_u16 },
9983                 { gen_helper_neon_narrow_sat_s32,
9984                   gen_helper_neon_narrow_sat_u32 },
9985             };
9986             genenvfn = fns[size][u];
9987             break;
9988         }
9989         case 0x16: /* FCVTN, FCVTN2 */
9990             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9991             if (size == 2) {
9992                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9993             } else {
9994                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9995                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9996                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9997                 TCGv_i32 ahp = get_ahp_flag();
9998 
9999                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10000                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10001                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10002                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10003             }
10004             break;
10005         case 0x36: /* BFCVTN, BFCVTN2 */
10006             {
10007                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10008                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
10009             }
10010             break;
10011         case 0x56:  /* FCVTXN, FCVTXN2 */
10012             /* 64 bit to 32 bit float conversion
10013              * with von Neumann rounding (round to odd)
10014              */
10015             assert(size == 2);
10016             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
10017             break;
10018         default:
10019             g_assert_not_reached();
10020         }
10021 
10022         if (genfn) {
10023             genfn(tcg_res[pass], tcg_op);
10024         } else if (genenvfn) {
10025             genenvfn(tcg_res[pass], tcg_env, tcg_op);
10026         }
10027     }
10028 
10029     for (pass = 0; pass < 2; pass++) {
10030         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10031     }
10032     clear_vec_high(s, is_q, rd);
10033 }
10034 
10035 /* Remaining saturating accumulating ops */
10036 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10037                                 bool is_q, int size, int rn, int rd)
10038 {
10039     bool is_double = (size == 3);
10040 
10041     if (is_double) {
10042         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10043         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10044         int pass;
10045 
10046         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10047             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10048             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10049 
10050             if (is_u) { /* USQADD */
10051                 gen_helper_neon_uqadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10052             } else { /* SUQADD */
10053                 gen_helper_neon_sqadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10054             }
10055             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10056         }
10057         clear_vec_high(s, !is_scalar, rd);
10058     } else {
10059         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10060         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10061         int pass, maxpasses;
10062 
10063         if (is_scalar) {
10064             maxpasses = 1;
10065         } else {
10066             maxpasses = is_q ? 4 : 2;
10067         }
10068 
10069         for (pass = 0; pass < maxpasses; pass++) {
10070             if (is_scalar) {
10071                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10072                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10073             } else {
10074                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10075                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10076             }
10077 
10078             if (is_u) { /* USQADD */
10079                 switch (size) {
10080                 case 0:
10081                     gen_helper_neon_uqadd_s8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10082                     break;
10083                 case 1:
10084                     gen_helper_neon_uqadd_s16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10085                     break;
10086                 case 2:
10087                     gen_helper_neon_uqadd_s32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10088                     break;
10089                 default:
10090                     g_assert_not_reached();
10091                 }
10092             } else { /* SUQADD */
10093                 switch (size) {
10094                 case 0:
10095                     gen_helper_neon_sqadd_u8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10096                     break;
10097                 case 1:
10098                     gen_helper_neon_sqadd_u16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10099                     break;
10100                 case 2:
10101                     gen_helper_neon_sqadd_u32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
10102                     break;
10103                 default:
10104                     g_assert_not_reached();
10105                 }
10106             }
10107 
10108             if (is_scalar) {
10109                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10110             }
10111             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10112         }
10113         clear_vec_high(s, is_q, rd);
10114     }
10115 }
10116 
10117 /* AdvSIMD scalar two reg misc
10118  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10119  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10120  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10121  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10122  */
10123 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10124 {
10125     int rd = extract32(insn, 0, 5);
10126     int rn = extract32(insn, 5, 5);
10127     int opcode = extract32(insn, 12, 5);
10128     int size = extract32(insn, 22, 2);
10129     bool u = extract32(insn, 29, 1);
10130     bool is_fcvt = false;
10131     int rmode;
10132     TCGv_i32 tcg_rmode;
10133     TCGv_ptr tcg_fpstatus;
10134 
10135     switch (opcode) {
10136     case 0x3: /* USQADD / SUQADD*/
10137         if (!fp_access_check(s)) {
10138             return;
10139         }
10140         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10141         return;
10142     case 0x7: /* SQABS / SQNEG */
10143         break;
10144     case 0xa: /* CMLT */
10145         if (u) {
10146             unallocated_encoding(s);
10147             return;
10148         }
10149         /* fall through */
10150     case 0x8: /* CMGT, CMGE */
10151     case 0x9: /* CMEQ, CMLE */
10152     case 0xb: /* ABS, NEG */
10153         if (size != 3) {
10154             unallocated_encoding(s);
10155             return;
10156         }
10157         break;
10158     case 0x12: /* SQXTUN */
10159         if (!u) {
10160             unallocated_encoding(s);
10161             return;
10162         }
10163         /* fall through */
10164     case 0x14: /* SQXTN, UQXTN */
10165         if (size == 3) {
10166             unallocated_encoding(s);
10167             return;
10168         }
10169         if (!fp_access_check(s)) {
10170             return;
10171         }
10172         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10173         return;
10174     case 0xc ... 0xf:
10175     case 0x16 ... 0x1d:
10176     case 0x1f:
10177         /* Floating point: U, size[1] and opcode indicate operation;
10178          * size[0] indicates single or double precision.
10179          */
10180         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10181         size = extract32(size, 0, 1) ? 3 : 2;
10182         switch (opcode) {
10183         case 0x2c: /* FCMGT (zero) */
10184         case 0x2d: /* FCMEQ (zero) */
10185         case 0x2e: /* FCMLT (zero) */
10186         case 0x6c: /* FCMGE (zero) */
10187         case 0x6d: /* FCMLE (zero) */
10188             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10189             return;
10190         case 0x1d: /* SCVTF */
10191         case 0x5d: /* UCVTF */
10192         {
10193             bool is_signed = (opcode == 0x1d);
10194             if (!fp_access_check(s)) {
10195                 return;
10196             }
10197             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10198             return;
10199         }
10200         case 0x3d: /* FRECPE */
10201         case 0x3f: /* FRECPX */
10202         case 0x7d: /* FRSQRTE */
10203             if (!fp_access_check(s)) {
10204                 return;
10205             }
10206             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10207             return;
10208         case 0x1a: /* FCVTNS */
10209         case 0x1b: /* FCVTMS */
10210         case 0x3a: /* FCVTPS */
10211         case 0x3b: /* FCVTZS */
10212         case 0x5a: /* FCVTNU */
10213         case 0x5b: /* FCVTMU */
10214         case 0x7a: /* FCVTPU */
10215         case 0x7b: /* FCVTZU */
10216             is_fcvt = true;
10217             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10218             break;
10219         case 0x1c: /* FCVTAS */
10220         case 0x5c: /* FCVTAU */
10221             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10222             is_fcvt = true;
10223             rmode = FPROUNDING_TIEAWAY;
10224             break;
10225         case 0x56: /* FCVTXN, FCVTXN2 */
10226             if (size == 2) {
10227                 unallocated_encoding(s);
10228                 return;
10229             }
10230             if (!fp_access_check(s)) {
10231                 return;
10232             }
10233             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10234             return;
10235         default:
10236             unallocated_encoding(s);
10237             return;
10238         }
10239         break;
10240     default:
10241         unallocated_encoding(s);
10242         return;
10243     }
10244 
10245     if (!fp_access_check(s)) {
10246         return;
10247     }
10248 
10249     if (is_fcvt) {
10250         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10251         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10252     } else {
10253         tcg_fpstatus = NULL;
10254         tcg_rmode = NULL;
10255     }
10256 
10257     if (size == 3) {
10258         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10259         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10260 
10261         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10262         write_fp_dreg(s, rd, tcg_rd);
10263     } else {
10264         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10265         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10266 
10267         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10268 
10269         switch (opcode) {
10270         case 0x7: /* SQABS, SQNEG */
10271         {
10272             NeonGenOneOpEnvFn *genfn;
10273             static NeonGenOneOpEnvFn * const fns[3][2] = {
10274                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10275                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10276                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10277             };
10278             genfn = fns[size][u];
10279             genfn(tcg_rd, tcg_env, tcg_rn);
10280             break;
10281         }
10282         case 0x1a: /* FCVTNS */
10283         case 0x1b: /* FCVTMS */
10284         case 0x1c: /* FCVTAS */
10285         case 0x3a: /* FCVTPS */
10286         case 0x3b: /* FCVTZS */
10287             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10288                                  tcg_fpstatus);
10289             break;
10290         case 0x5a: /* FCVTNU */
10291         case 0x5b: /* FCVTMU */
10292         case 0x5c: /* FCVTAU */
10293         case 0x7a: /* FCVTPU */
10294         case 0x7b: /* FCVTZU */
10295             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10296                                  tcg_fpstatus);
10297             break;
10298         default:
10299             g_assert_not_reached();
10300         }
10301 
10302         write_fp_sreg(s, rd, tcg_rd);
10303     }
10304 
10305     if (is_fcvt) {
10306         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10307     }
10308 }
10309 
10310 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10311 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10312                                  int immh, int immb, int opcode, int rn, int rd)
10313 {
10314     int size = 32 - clz32(immh) - 1;
10315     int immhb = immh << 3 | immb;
10316     int shift = 2 * (8 << size) - immhb;
10317     GVecGen2iFn *gvec_fn;
10318 
10319     if (extract32(immh, 3, 1) && !is_q) {
10320         unallocated_encoding(s);
10321         return;
10322     }
10323     tcg_debug_assert(size <= 3);
10324 
10325     if (!fp_access_check(s)) {
10326         return;
10327     }
10328 
10329     switch (opcode) {
10330     case 0x02: /* SSRA / USRA (accumulate) */
10331         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10332         break;
10333 
10334     case 0x08: /* SRI */
10335         gvec_fn = gen_gvec_sri;
10336         break;
10337 
10338     case 0x00: /* SSHR / USHR */
10339         if (is_u) {
10340             if (shift == 8 << size) {
10341                 /* Shift count the same size as element size produces zero.  */
10342                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10343                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10344                 return;
10345             }
10346             gvec_fn = tcg_gen_gvec_shri;
10347         } else {
10348             /* Shift count the same size as element size produces all sign.  */
10349             if (shift == 8 << size) {
10350                 shift -= 1;
10351             }
10352             gvec_fn = tcg_gen_gvec_sari;
10353         }
10354         break;
10355 
10356     case 0x04: /* SRSHR / URSHR (rounding) */
10357         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10358         break;
10359 
10360     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10361         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10362         break;
10363 
10364     default:
10365         g_assert_not_reached();
10366     }
10367 
10368     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10369 }
10370 
10371 /* SHL/SLI - Vector shift left */
10372 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10373                                  int immh, int immb, int opcode, int rn, int rd)
10374 {
10375     int size = 32 - clz32(immh) - 1;
10376     int immhb = immh << 3 | immb;
10377     int shift = immhb - (8 << size);
10378 
10379     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10380     assert(size >= 0 && size <= 3);
10381 
10382     if (extract32(immh, 3, 1) && !is_q) {
10383         unallocated_encoding(s);
10384         return;
10385     }
10386 
10387     if (!fp_access_check(s)) {
10388         return;
10389     }
10390 
10391     if (insert) {
10392         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10393     } else {
10394         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10395     }
10396 }
10397 
10398 /* USHLL/SHLL - Vector shift left with widening */
10399 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10400                                  int immh, int immb, int opcode, int rn, int rd)
10401 {
10402     int size = 32 - clz32(immh) - 1;
10403     int immhb = immh << 3 | immb;
10404     int shift = immhb - (8 << size);
10405     int dsize = 64;
10406     int esize = 8 << size;
10407     int elements = dsize/esize;
10408     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10409     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10410     int i;
10411 
10412     if (size >= 3) {
10413         unallocated_encoding(s);
10414         return;
10415     }
10416 
10417     if (!fp_access_check(s)) {
10418         return;
10419     }
10420 
10421     /* For the LL variants the store is larger than the load,
10422      * so if rd == rn we would overwrite parts of our input.
10423      * So load everything right now and use shifts in the main loop.
10424      */
10425     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10426 
10427     for (i = 0; i < elements; i++) {
10428         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10429         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10430         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10431         write_vec_element(s, tcg_rd, rd, i, size + 1);
10432     }
10433 }
10434 
10435 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10436 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10437                                  int immh, int immb, int opcode, int rn, int rd)
10438 {
10439     int immhb = immh << 3 | immb;
10440     int size = 32 - clz32(immh) - 1;
10441     int dsize = 64;
10442     int esize = 8 << size;
10443     int elements = dsize/esize;
10444     int shift = (2 * esize) - immhb;
10445     bool round = extract32(opcode, 0, 1);
10446     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10447     TCGv_i64 tcg_round;
10448     int i;
10449 
10450     if (extract32(immh, 3, 1)) {
10451         unallocated_encoding(s);
10452         return;
10453     }
10454 
10455     if (!fp_access_check(s)) {
10456         return;
10457     }
10458 
10459     tcg_rn = tcg_temp_new_i64();
10460     tcg_rd = tcg_temp_new_i64();
10461     tcg_final = tcg_temp_new_i64();
10462     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10463 
10464     if (round) {
10465         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10466     } else {
10467         tcg_round = NULL;
10468     }
10469 
10470     for (i = 0; i < elements; i++) {
10471         read_vec_element(s, tcg_rn, rn, i, size+1);
10472         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10473                                 false, true, size+1, shift);
10474 
10475         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10476     }
10477 
10478     if (!is_q) {
10479         write_vec_element(s, tcg_final, rd, 0, MO_64);
10480     } else {
10481         write_vec_element(s, tcg_final, rd, 1, MO_64);
10482     }
10483 
10484     clear_vec_high(s, is_q, rd);
10485 }
10486 
10487 
10488 /* AdvSIMD shift by immediate
10489  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10490  * +---+---+---+-------------+------+------+--------+---+------+------+
10491  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10492  * +---+---+---+-------------+------+------+--------+---+------+------+
10493  */
10494 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10495 {
10496     int rd = extract32(insn, 0, 5);
10497     int rn = extract32(insn, 5, 5);
10498     int opcode = extract32(insn, 11, 5);
10499     int immb = extract32(insn, 16, 3);
10500     int immh = extract32(insn, 19, 4);
10501     bool is_u = extract32(insn, 29, 1);
10502     bool is_q = extract32(insn, 30, 1);
10503 
10504     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10505     assert(immh != 0);
10506 
10507     switch (opcode) {
10508     case 0x08: /* SRI */
10509         if (!is_u) {
10510             unallocated_encoding(s);
10511             return;
10512         }
10513         /* fall through */
10514     case 0x00: /* SSHR / USHR */
10515     case 0x02: /* SSRA / USRA (accumulate) */
10516     case 0x04: /* SRSHR / URSHR (rounding) */
10517     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10518         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10519         break;
10520     case 0x0a: /* SHL / SLI */
10521         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10522         break;
10523     case 0x10: /* SHRN */
10524     case 0x11: /* RSHRN / SQRSHRUN */
10525         if (is_u) {
10526             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10527                                    opcode, rn, rd);
10528         } else {
10529             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10530         }
10531         break;
10532     case 0x12: /* SQSHRN / UQSHRN */
10533     case 0x13: /* SQRSHRN / UQRSHRN */
10534         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10535                                opcode, rn, rd);
10536         break;
10537     case 0x14: /* SSHLL / USHLL */
10538         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10539         break;
10540     case 0x1c: /* SCVTF / UCVTF */
10541         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10542                                      opcode, rn, rd);
10543         break;
10544     case 0xc: /* SQSHLU */
10545         if (!is_u) {
10546             unallocated_encoding(s);
10547             return;
10548         }
10549         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10550         break;
10551     case 0xe: /* SQSHL, UQSHL */
10552         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10553         break;
10554     case 0x1f: /* FCVTZS/ FCVTZU */
10555         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10556         return;
10557     default:
10558         unallocated_encoding(s);
10559         return;
10560     }
10561 }
10562 
10563 /* Generate code to do a "long" addition or subtraction, ie one done in
10564  * TCGv_i64 on vector lanes twice the width specified by size.
10565  */
10566 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10567                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10568 {
10569     static NeonGenTwo64OpFn * const fns[3][2] = {
10570         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10571         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10572         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10573     };
10574     NeonGenTwo64OpFn *genfn;
10575     assert(size < 3);
10576 
10577     genfn = fns[size][is_sub];
10578     genfn(tcg_res, tcg_op1, tcg_op2);
10579 }
10580 
10581 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10582                                 int opcode, int rd, int rn, int rm)
10583 {
10584     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10585     TCGv_i64 tcg_res[2];
10586     int pass, accop;
10587 
10588     tcg_res[0] = tcg_temp_new_i64();
10589     tcg_res[1] = tcg_temp_new_i64();
10590 
10591     /* Does this op do an adding accumulate, a subtracting accumulate,
10592      * or no accumulate at all?
10593      */
10594     switch (opcode) {
10595     case 5:
10596     case 8:
10597     case 9:
10598         accop = 1;
10599         break;
10600     case 10:
10601     case 11:
10602         accop = -1;
10603         break;
10604     default:
10605         accop = 0;
10606         break;
10607     }
10608 
10609     if (accop != 0) {
10610         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10611         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10612     }
10613 
10614     /* size == 2 means two 32x32->64 operations; this is worth special
10615      * casing because we can generally handle it inline.
10616      */
10617     if (size == 2) {
10618         for (pass = 0; pass < 2; pass++) {
10619             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10620             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10621             TCGv_i64 tcg_passres;
10622             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10623 
10624             int elt = pass + is_q * 2;
10625 
10626             read_vec_element(s, tcg_op1, rn, elt, memop);
10627             read_vec_element(s, tcg_op2, rm, elt, memop);
10628 
10629             if (accop == 0) {
10630                 tcg_passres = tcg_res[pass];
10631             } else {
10632                 tcg_passres = tcg_temp_new_i64();
10633             }
10634 
10635             switch (opcode) {
10636             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10637                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10638                 break;
10639             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10640                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10641                 break;
10642             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10643             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10644             {
10645                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10646                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10647 
10648                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10649                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10650                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10651                                     tcg_passres,
10652                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10653                 break;
10654             }
10655             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10656             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10657             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10658                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10659                 break;
10660             case 9: /* SQDMLAL, SQDMLAL2 */
10661             case 11: /* SQDMLSL, SQDMLSL2 */
10662             case 13: /* SQDMULL, SQDMULL2 */
10663                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10664                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10665                                                   tcg_passres, tcg_passres);
10666                 break;
10667             default:
10668                 g_assert_not_reached();
10669             }
10670 
10671             if (opcode == 9 || opcode == 11) {
10672                 /* saturating accumulate ops */
10673                 if (accop < 0) {
10674                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10675                 }
10676                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10677                                                   tcg_res[pass], tcg_passres);
10678             } else if (accop > 0) {
10679                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10680             } else if (accop < 0) {
10681                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10682             }
10683         }
10684     } else {
10685         /* size 0 or 1, generally helper functions */
10686         for (pass = 0; pass < 2; pass++) {
10687             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10688             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10689             TCGv_i64 tcg_passres;
10690             int elt = pass + is_q * 2;
10691 
10692             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10693             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10694 
10695             if (accop == 0) {
10696                 tcg_passres = tcg_res[pass];
10697             } else {
10698                 tcg_passres = tcg_temp_new_i64();
10699             }
10700 
10701             switch (opcode) {
10702             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10703             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10704             {
10705                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10706                 static NeonGenWidenFn * const widenfns[2][2] = {
10707                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10708                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10709                 };
10710                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10711 
10712                 widenfn(tcg_op2_64, tcg_op2);
10713                 widenfn(tcg_passres, tcg_op1);
10714                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10715                               tcg_passres, tcg_op2_64);
10716                 break;
10717             }
10718             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10719             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10720                 if (size == 0) {
10721                     if (is_u) {
10722                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10723                     } else {
10724                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10725                     }
10726                 } else {
10727                     if (is_u) {
10728                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10729                     } else {
10730                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10731                     }
10732                 }
10733                 break;
10734             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10735             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10736             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10737                 if (size == 0) {
10738                     if (is_u) {
10739                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10740                     } else {
10741                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10742                     }
10743                 } else {
10744                     if (is_u) {
10745                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10746                     } else {
10747                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10748                     }
10749                 }
10750                 break;
10751             case 9: /* SQDMLAL, SQDMLAL2 */
10752             case 11: /* SQDMLSL, SQDMLSL2 */
10753             case 13: /* SQDMULL, SQDMULL2 */
10754                 assert(size == 1);
10755                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10756                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10757                                                   tcg_passres, tcg_passres);
10758                 break;
10759             default:
10760                 g_assert_not_reached();
10761             }
10762 
10763             if (accop != 0) {
10764                 if (opcode == 9 || opcode == 11) {
10765                     /* saturating accumulate ops */
10766                     if (accop < 0) {
10767                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10768                     }
10769                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10770                                                       tcg_res[pass],
10771                                                       tcg_passres);
10772                 } else {
10773                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10774                                   tcg_res[pass], tcg_passres);
10775                 }
10776             }
10777         }
10778     }
10779 
10780     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10781     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10782 }
10783 
10784 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10785                             int opcode, int rd, int rn, int rm)
10786 {
10787     TCGv_i64 tcg_res[2];
10788     int part = is_q ? 2 : 0;
10789     int pass;
10790 
10791     for (pass = 0; pass < 2; pass++) {
10792         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10793         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10794         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10795         static NeonGenWidenFn * const widenfns[3][2] = {
10796             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10797             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10798             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10799         };
10800         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10801 
10802         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10803         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10804         widenfn(tcg_op2_wide, tcg_op2);
10805         tcg_res[pass] = tcg_temp_new_i64();
10806         gen_neon_addl(size, (opcode == 3),
10807                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10808     }
10809 
10810     for (pass = 0; pass < 2; pass++) {
10811         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10812     }
10813 }
10814 
10815 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10816 {
10817     tcg_gen_addi_i64(in, in, 1U << 31);
10818     tcg_gen_extrh_i64_i32(res, in);
10819 }
10820 
10821 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10822                                  int opcode, int rd, int rn, int rm)
10823 {
10824     TCGv_i32 tcg_res[2];
10825     int part = is_q ? 2 : 0;
10826     int pass;
10827 
10828     for (pass = 0; pass < 2; pass++) {
10829         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10830         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10831         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10832         static NeonGenNarrowFn * const narrowfns[3][2] = {
10833             { gen_helper_neon_narrow_high_u8,
10834               gen_helper_neon_narrow_round_high_u8 },
10835             { gen_helper_neon_narrow_high_u16,
10836               gen_helper_neon_narrow_round_high_u16 },
10837             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10838         };
10839         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10840 
10841         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10842         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10843 
10844         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10845 
10846         tcg_res[pass] = tcg_temp_new_i32();
10847         gennarrow(tcg_res[pass], tcg_wideres);
10848     }
10849 
10850     for (pass = 0; pass < 2; pass++) {
10851         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10852     }
10853     clear_vec_high(s, is_q, rd);
10854 }
10855 
10856 /* AdvSIMD three different
10857  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10858  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10859  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10860  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10861  */
10862 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10863 {
10864     /* Instructions in this group fall into three basic classes
10865      * (in each case with the operation working on each element in
10866      * the input vectors):
10867      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10868      *     128 bit input)
10869      * (2) wide 64 x 128 -> 128
10870      * (3) narrowing 128 x 128 -> 64
10871      * Here we do initial decode, catch unallocated cases and
10872      * dispatch to separate functions for each class.
10873      */
10874     int is_q = extract32(insn, 30, 1);
10875     int is_u = extract32(insn, 29, 1);
10876     int size = extract32(insn, 22, 2);
10877     int opcode = extract32(insn, 12, 4);
10878     int rm = extract32(insn, 16, 5);
10879     int rn = extract32(insn, 5, 5);
10880     int rd = extract32(insn, 0, 5);
10881 
10882     switch (opcode) {
10883     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10884     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10885         /* 64 x 128 -> 128 */
10886         if (size == 3) {
10887             unallocated_encoding(s);
10888             return;
10889         }
10890         if (!fp_access_check(s)) {
10891             return;
10892         }
10893         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10894         break;
10895     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10896     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10897         /* 128 x 128 -> 64 */
10898         if (size == 3) {
10899             unallocated_encoding(s);
10900             return;
10901         }
10902         if (!fp_access_check(s)) {
10903             return;
10904         }
10905         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10906         break;
10907     case 14: /* PMULL, PMULL2 */
10908         if (is_u) {
10909             unallocated_encoding(s);
10910             return;
10911         }
10912         switch (size) {
10913         case 0: /* PMULL.P8 */
10914             if (!fp_access_check(s)) {
10915                 return;
10916             }
10917             /* The Q field specifies lo/hi half input for this insn.  */
10918             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10919                              gen_helper_neon_pmull_h);
10920             break;
10921 
10922         case 3: /* PMULL.P64 */
10923             if (!dc_isar_feature(aa64_pmull, s)) {
10924                 unallocated_encoding(s);
10925                 return;
10926             }
10927             if (!fp_access_check(s)) {
10928                 return;
10929             }
10930             /* The Q field specifies lo/hi half input for this insn.  */
10931             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10932                              gen_helper_gvec_pmull_q);
10933             break;
10934 
10935         default:
10936             unallocated_encoding(s);
10937             break;
10938         }
10939         return;
10940     case 9: /* SQDMLAL, SQDMLAL2 */
10941     case 11: /* SQDMLSL, SQDMLSL2 */
10942     case 13: /* SQDMULL, SQDMULL2 */
10943         if (is_u || size == 0) {
10944             unallocated_encoding(s);
10945             return;
10946         }
10947         /* fall through */
10948     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10949     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10950     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10951     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10952     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10953     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10954     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10955         /* 64 x 64 -> 128 */
10956         if (size == 3) {
10957             unallocated_encoding(s);
10958             return;
10959         }
10960         if (!fp_access_check(s)) {
10961             return;
10962         }
10963 
10964         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10965         break;
10966     default:
10967         /* opcode 15 not allocated */
10968         unallocated_encoding(s);
10969         break;
10970     }
10971 }
10972 
10973 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10974 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10975 {
10976     int rd = extract32(insn, 0, 5);
10977     int rn = extract32(insn, 5, 5);
10978     int rm = extract32(insn, 16, 5);
10979     int size = extract32(insn, 22, 2);
10980     bool is_u = extract32(insn, 29, 1);
10981     bool is_q = extract32(insn, 30, 1);
10982 
10983     if (!fp_access_check(s)) {
10984         return;
10985     }
10986 
10987     switch (size + 4 * is_u) {
10988     case 0: /* AND */
10989         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10990         return;
10991     case 1: /* BIC */
10992         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10993         return;
10994     case 2: /* ORR */
10995         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10996         return;
10997     case 3: /* ORN */
10998         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10999         return;
11000     case 4: /* EOR */
11001         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
11002         return;
11003 
11004     case 5: /* BSL bitwise select */
11005         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
11006         return;
11007     case 6: /* BIT, bitwise insert if true */
11008         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
11009         return;
11010     case 7: /* BIF, bitwise insert if false */
11011         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11012         return;
11013 
11014     default:
11015         g_assert_not_reached();
11016     }
11017 }
11018 
11019 /* Pairwise op subgroup of C3.6.16.
11020  *
11021  * This is called directly or via the handle_3same_float for float pairwise
11022  * operations where the opcode and size are calculated differently.
11023  */
11024 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11025                                    int size, int rn, int rm, int rd)
11026 {
11027     TCGv_ptr fpst;
11028     int pass;
11029 
11030     /* Floating point operations need fpst */
11031     if (opcode >= 0x58) {
11032         fpst = fpstatus_ptr(FPST_FPCR);
11033     } else {
11034         fpst = NULL;
11035     }
11036 
11037     if (!fp_access_check(s)) {
11038         return;
11039     }
11040 
11041     /* These operations work on the concatenated rm:rn, with each pair of
11042      * adjacent elements being operated on to produce an element in the result.
11043      */
11044     if (size == 3) {
11045         TCGv_i64 tcg_res[2];
11046 
11047         for (pass = 0; pass < 2; pass++) {
11048             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11049             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11050             int passreg = (pass == 0) ? rn : rm;
11051 
11052             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11053             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11054             tcg_res[pass] = tcg_temp_new_i64();
11055 
11056             switch (opcode) {
11057             case 0x17: /* ADDP */
11058                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11059                 break;
11060             case 0x58: /* FMAXNMP */
11061                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11062                 break;
11063             case 0x5a: /* FADDP */
11064                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11065                 break;
11066             case 0x5e: /* FMAXP */
11067                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11068                 break;
11069             case 0x78: /* FMINNMP */
11070                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11071                 break;
11072             case 0x7e: /* FMINP */
11073                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11074                 break;
11075             default:
11076                 g_assert_not_reached();
11077             }
11078         }
11079 
11080         for (pass = 0; pass < 2; pass++) {
11081             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11082         }
11083     } else {
11084         int maxpass = is_q ? 4 : 2;
11085         TCGv_i32 tcg_res[4];
11086 
11087         for (pass = 0; pass < maxpass; pass++) {
11088             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11089             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11090             NeonGenTwoOpFn *genfn = NULL;
11091             int passreg = pass < (maxpass / 2) ? rn : rm;
11092             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11093 
11094             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11095             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11096             tcg_res[pass] = tcg_temp_new_i32();
11097 
11098             switch (opcode) {
11099             case 0x17: /* ADDP */
11100             {
11101                 static NeonGenTwoOpFn * const fns[3] = {
11102                     gen_helper_neon_padd_u8,
11103                     gen_helper_neon_padd_u16,
11104                     tcg_gen_add_i32,
11105                 };
11106                 genfn = fns[size];
11107                 break;
11108             }
11109             case 0x14: /* SMAXP, UMAXP */
11110             {
11111                 static NeonGenTwoOpFn * const fns[3][2] = {
11112                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11113                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11114                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11115                 };
11116                 genfn = fns[size][u];
11117                 break;
11118             }
11119             case 0x15: /* SMINP, UMINP */
11120             {
11121                 static NeonGenTwoOpFn * const fns[3][2] = {
11122                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11123                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11124                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11125                 };
11126                 genfn = fns[size][u];
11127                 break;
11128             }
11129             /* The FP operations are all on single floats (32 bit) */
11130             case 0x58: /* FMAXNMP */
11131                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11132                 break;
11133             case 0x5a: /* FADDP */
11134                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11135                 break;
11136             case 0x5e: /* FMAXP */
11137                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11138                 break;
11139             case 0x78: /* FMINNMP */
11140                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11141                 break;
11142             case 0x7e: /* FMINP */
11143                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11144                 break;
11145             default:
11146                 g_assert_not_reached();
11147             }
11148 
11149             /* FP ops called directly, otherwise call now */
11150             if (genfn) {
11151                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11152             }
11153         }
11154 
11155         for (pass = 0; pass < maxpass; pass++) {
11156             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11157         }
11158         clear_vec_high(s, is_q, rd);
11159     }
11160 }
11161 
11162 /* Floating point op subgroup of C3.6.16. */
11163 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11164 {
11165     /* For floating point ops, the U, size[1] and opcode bits
11166      * together indicate the operation. size[0] indicates single
11167      * or double.
11168      */
11169     int fpopcode = extract32(insn, 11, 5)
11170         | (extract32(insn, 23, 1) << 5)
11171         | (extract32(insn, 29, 1) << 6);
11172     int is_q = extract32(insn, 30, 1);
11173     int size = extract32(insn, 22, 1);
11174     int rm = extract32(insn, 16, 5);
11175     int rn = extract32(insn, 5, 5);
11176     int rd = extract32(insn, 0, 5);
11177 
11178     int datasize = is_q ? 128 : 64;
11179     int esize = 32 << size;
11180     int elements = datasize / esize;
11181 
11182     if (size == 1 && !is_q) {
11183         unallocated_encoding(s);
11184         return;
11185     }
11186 
11187     switch (fpopcode) {
11188     case 0x58: /* FMAXNMP */
11189     case 0x5a: /* FADDP */
11190     case 0x5e: /* FMAXP */
11191     case 0x78: /* FMINNMP */
11192     case 0x7e: /* FMINP */
11193         if (size && !is_q) {
11194             unallocated_encoding(s);
11195             return;
11196         }
11197         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11198                                rn, rm, rd);
11199         return;
11200     case 0x1b: /* FMULX */
11201     case 0x1f: /* FRECPS */
11202     case 0x3f: /* FRSQRTS */
11203     case 0x5d: /* FACGE */
11204     case 0x7d: /* FACGT */
11205     case 0x19: /* FMLA */
11206     case 0x39: /* FMLS */
11207     case 0x18: /* FMAXNM */
11208     case 0x1a: /* FADD */
11209     case 0x1c: /* FCMEQ */
11210     case 0x1e: /* FMAX */
11211     case 0x38: /* FMINNM */
11212     case 0x3a: /* FSUB */
11213     case 0x3e: /* FMIN */
11214     case 0x5b: /* FMUL */
11215     case 0x5c: /* FCMGE */
11216     case 0x5f: /* FDIV */
11217     case 0x7a: /* FABD */
11218     case 0x7c: /* FCMGT */
11219         if (!fp_access_check(s)) {
11220             return;
11221         }
11222         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11223         return;
11224 
11225     case 0x1d: /* FMLAL  */
11226     case 0x3d: /* FMLSL  */
11227     case 0x59: /* FMLAL2 */
11228     case 0x79: /* FMLSL2 */
11229         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11230             unallocated_encoding(s);
11231             return;
11232         }
11233         if (fp_access_check(s)) {
11234             int is_s = extract32(insn, 23, 1);
11235             int is_2 = extract32(insn, 29, 1);
11236             int data = (is_2 << 1) | is_s;
11237             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11238                                vec_full_reg_offset(s, rn),
11239                                vec_full_reg_offset(s, rm), tcg_env,
11240                                is_q ? 16 : 8, vec_full_reg_size(s),
11241                                data, gen_helper_gvec_fmlal_a64);
11242         }
11243         return;
11244 
11245     default:
11246         unallocated_encoding(s);
11247         return;
11248     }
11249 }
11250 
11251 /* Integer op subgroup of C3.6.16. */
11252 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11253 {
11254     int is_q = extract32(insn, 30, 1);
11255     int u = extract32(insn, 29, 1);
11256     int size = extract32(insn, 22, 2);
11257     int opcode = extract32(insn, 11, 5);
11258     int rm = extract32(insn, 16, 5);
11259     int rn = extract32(insn, 5, 5);
11260     int rd = extract32(insn, 0, 5);
11261     int pass;
11262     TCGCond cond;
11263 
11264     switch (opcode) {
11265     case 0x13: /* MUL, PMUL */
11266         if (u && size != 0) {
11267             unallocated_encoding(s);
11268             return;
11269         }
11270         /* fall through */
11271     case 0x0: /* SHADD, UHADD */
11272     case 0x2: /* SRHADD, URHADD */
11273     case 0x4: /* SHSUB, UHSUB */
11274     case 0xc: /* SMAX, UMAX */
11275     case 0xd: /* SMIN, UMIN */
11276     case 0xe: /* SABD, UABD */
11277     case 0xf: /* SABA, UABA */
11278     case 0x12: /* MLA, MLS */
11279         if (size == 3) {
11280             unallocated_encoding(s);
11281             return;
11282         }
11283         break;
11284     case 0x16: /* SQDMULH, SQRDMULH */
11285         if (size == 0 || size == 3) {
11286             unallocated_encoding(s);
11287             return;
11288         }
11289         break;
11290     default:
11291         if (size == 3 && !is_q) {
11292             unallocated_encoding(s);
11293             return;
11294         }
11295         break;
11296     }
11297 
11298     if (!fp_access_check(s)) {
11299         return;
11300     }
11301 
11302     switch (opcode) {
11303     case 0x01: /* SQADD, UQADD */
11304         if (u) {
11305             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11306         } else {
11307             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11308         }
11309         return;
11310     case 0x05: /* SQSUB, UQSUB */
11311         if (u) {
11312             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11313         } else {
11314             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11315         }
11316         return;
11317     case 0x08: /* SSHL, USHL */
11318         if (u) {
11319             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11320         } else {
11321             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11322         }
11323         return;
11324     case 0x0c: /* SMAX, UMAX */
11325         if (u) {
11326             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11327         } else {
11328             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11329         }
11330         return;
11331     case 0x0d: /* SMIN, UMIN */
11332         if (u) {
11333             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11334         } else {
11335             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11336         }
11337         return;
11338     case 0xe: /* SABD, UABD */
11339         if (u) {
11340             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11341         } else {
11342             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11343         }
11344         return;
11345     case 0xf: /* SABA, UABA */
11346         if (u) {
11347             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11348         } else {
11349             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11350         }
11351         return;
11352     case 0x10: /* ADD, SUB */
11353         if (u) {
11354             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11355         } else {
11356             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11357         }
11358         return;
11359     case 0x13: /* MUL, PMUL */
11360         if (!u) { /* MUL */
11361             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11362         } else {  /* PMUL */
11363             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11364         }
11365         return;
11366     case 0x12: /* MLA, MLS */
11367         if (u) {
11368             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11369         } else {
11370             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11371         }
11372         return;
11373     case 0x16: /* SQDMULH, SQRDMULH */
11374         {
11375             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11376                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11377                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11378             };
11379             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11380         }
11381         return;
11382     case 0x11:
11383         if (!u) { /* CMTST */
11384             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11385             return;
11386         }
11387         /* else CMEQ */
11388         cond = TCG_COND_EQ;
11389         goto do_gvec_cmp;
11390     case 0x06: /* CMGT, CMHI */
11391         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11392         goto do_gvec_cmp;
11393     case 0x07: /* CMGE, CMHS */
11394         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11395     do_gvec_cmp:
11396         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11397                          vec_full_reg_offset(s, rn),
11398                          vec_full_reg_offset(s, rm),
11399                          is_q ? 16 : 8, vec_full_reg_size(s));
11400         return;
11401     }
11402 
11403     if (size == 3) {
11404         assert(is_q);
11405         for (pass = 0; pass < 2; pass++) {
11406             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11407             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11408             TCGv_i64 tcg_res = tcg_temp_new_i64();
11409 
11410             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11411             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11412 
11413             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11414 
11415             write_vec_element(s, tcg_res, rd, pass, MO_64);
11416         }
11417     } else {
11418         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11419             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11420             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11421             TCGv_i32 tcg_res = tcg_temp_new_i32();
11422             NeonGenTwoOpFn *genfn = NULL;
11423             NeonGenTwoOpEnvFn *genenvfn = NULL;
11424 
11425             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11426             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11427 
11428             switch (opcode) {
11429             case 0x0: /* SHADD, UHADD */
11430             {
11431                 static NeonGenTwoOpFn * const fns[3][2] = {
11432                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11433                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11434                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11435                 };
11436                 genfn = fns[size][u];
11437                 break;
11438             }
11439             case 0x2: /* SRHADD, URHADD */
11440             {
11441                 static NeonGenTwoOpFn * const fns[3][2] = {
11442                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11443                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11444                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11445                 };
11446                 genfn = fns[size][u];
11447                 break;
11448             }
11449             case 0x4: /* SHSUB, UHSUB */
11450             {
11451                 static NeonGenTwoOpFn * const fns[3][2] = {
11452                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11453                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11454                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11455                 };
11456                 genfn = fns[size][u];
11457                 break;
11458             }
11459             case 0x9: /* SQSHL, UQSHL */
11460             {
11461                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11462                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11463                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11464                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11465                 };
11466                 genenvfn = fns[size][u];
11467                 break;
11468             }
11469             case 0xa: /* SRSHL, URSHL */
11470             {
11471                 static NeonGenTwoOpFn * const fns[3][2] = {
11472                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11473                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11474                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11475                 };
11476                 genfn = fns[size][u];
11477                 break;
11478             }
11479             case 0xb: /* SQRSHL, UQRSHL */
11480             {
11481                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11482                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11483                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11484                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11485                 };
11486                 genenvfn = fns[size][u];
11487                 break;
11488             }
11489             default:
11490                 g_assert_not_reached();
11491             }
11492 
11493             if (genenvfn) {
11494                 genenvfn(tcg_res, tcg_env, tcg_op1, tcg_op2);
11495             } else {
11496                 genfn(tcg_res, tcg_op1, tcg_op2);
11497             }
11498 
11499             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11500         }
11501     }
11502     clear_vec_high(s, is_q, rd);
11503 }
11504 
11505 /* AdvSIMD three same
11506  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11507  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11508  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11509  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11510  */
11511 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11512 {
11513     int opcode = extract32(insn, 11, 5);
11514 
11515     switch (opcode) {
11516     case 0x3: /* logic ops */
11517         disas_simd_3same_logic(s, insn);
11518         break;
11519     case 0x17: /* ADDP */
11520     case 0x14: /* SMAXP, UMAXP */
11521     case 0x15: /* SMINP, UMINP */
11522     {
11523         /* Pairwise operations */
11524         int is_q = extract32(insn, 30, 1);
11525         int u = extract32(insn, 29, 1);
11526         int size = extract32(insn, 22, 2);
11527         int rm = extract32(insn, 16, 5);
11528         int rn = extract32(insn, 5, 5);
11529         int rd = extract32(insn, 0, 5);
11530         if (opcode == 0x17) {
11531             if (u || (size == 3 && !is_q)) {
11532                 unallocated_encoding(s);
11533                 return;
11534             }
11535         } else {
11536             if (size == 3) {
11537                 unallocated_encoding(s);
11538                 return;
11539             }
11540         }
11541         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11542         break;
11543     }
11544     case 0x18 ... 0x31:
11545         /* floating point ops, sz[1] and U are part of opcode */
11546         disas_simd_3same_float(s, insn);
11547         break;
11548     default:
11549         disas_simd_3same_int(s, insn);
11550         break;
11551     }
11552 }
11553 
11554 /*
11555  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11556  *
11557  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11558  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11559  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11560  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11561  *
11562  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11563  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11564  *
11565  */
11566 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11567 {
11568     int opcode = extract32(insn, 11, 3);
11569     int u = extract32(insn, 29, 1);
11570     int a = extract32(insn, 23, 1);
11571     int is_q = extract32(insn, 30, 1);
11572     int rm = extract32(insn, 16, 5);
11573     int rn = extract32(insn, 5, 5);
11574     int rd = extract32(insn, 0, 5);
11575     /*
11576      * For these floating point ops, the U, a and opcode bits
11577      * together indicate the operation.
11578      */
11579     int fpopcode = opcode | (a << 3) | (u << 4);
11580     int datasize = is_q ? 128 : 64;
11581     int elements = datasize / 16;
11582     bool pairwise;
11583     TCGv_ptr fpst;
11584     int pass;
11585 
11586     switch (fpopcode) {
11587     case 0x0: /* FMAXNM */
11588     case 0x1: /* FMLA */
11589     case 0x2: /* FADD */
11590     case 0x3: /* FMULX */
11591     case 0x4: /* FCMEQ */
11592     case 0x6: /* FMAX */
11593     case 0x7: /* FRECPS */
11594     case 0x8: /* FMINNM */
11595     case 0x9: /* FMLS */
11596     case 0xa: /* FSUB */
11597     case 0xe: /* FMIN */
11598     case 0xf: /* FRSQRTS */
11599     case 0x13: /* FMUL */
11600     case 0x14: /* FCMGE */
11601     case 0x15: /* FACGE */
11602     case 0x17: /* FDIV */
11603     case 0x1a: /* FABD */
11604     case 0x1c: /* FCMGT */
11605     case 0x1d: /* FACGT */
11606         pairwise = false;
11607         break;
11608     case 0x10: /* FMAXNMP */
11609     case 0x12: /* FADDP */
11610     case 0x16: /* FMAXP */
11611     case 0x18: /* FMINNMP */
11612     case 0x1e: /* FMINP */
11613         pairwise = true;
11614         break;
11615     default:
11616         unallocated_encoding(s);
11617         return;
11618     }
11619 
11620     if (!dc_isar_feature(aa64_fp16, s)) {
11621         unallocated_encoding(s);
11622         return;
11623     }
11624 
11625     if (!fp_access_check(s)) {
11626         return;
11627     }
11628 
11629     fpst = fpstatus_ptr(FPST_FPCR_F16);
11630 
11631     if (pairwise) {
11632         int maxpass = is_q ? 8 : 4;
11633         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11634         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11635         TCGv_i32 tcg_res[8];
11636 
11637         for (pass = 0; pass < maxpass; pass++) {
11638             int passreg = pass < (maxpass / 2) ? rn : rm;
11639             int passelt = (pass << 1) & (maxpass - 1);
11640 
11641             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11642             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11643             tcg_res[pass] = tcg_temp_new_i32();
11644 
11645             switch (fpopcode) {
11646             case 0x10: /* FMAXNMP */
11647                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11648                                            fpst);
11649                 break;
11650             case 0x12: /* FADDP */
11651                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11652                 break;
11653             case 0x16: /* FMAXP */
11654                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11655                 break;
11656             case 0x18: /* FMINNMP */
11657                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11658                                            fpst);
11659                 break;
11660             case 0x1e: /* FMINP */
11661                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11662                 break;
11663             default:
11664                 g_assert_not_reached();
11665             }
11666         }
11667 
11668         for (pass = 0; pass < maxpass; pass++) {
11669             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11670         }
11671     } else {
11672         for (pass = 0; pass < elements; pass++) {
11673             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11674             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11675             TCGv_i32 tcg_res = tcg_temp_new_i32();
11676 
11677             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11678             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11679 
11680             switch (fpopcode) {
11681             case 0x0: /* FMAXNM */
11682                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11683                 break;
11684             case 0x1: /* FMLA */
11685                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11686                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11687                                            fpst);
11688                 break;
11689             case 0x2: /* FADD */
11690                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11691                 break;
11692             case 0x3: /* FMULX */
11693                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11694                 break;
11695             case 0x4: /* FCMEQ */
11696                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11697                 break;
11698             case 0x6: /* FMAX */
11699                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11700                 break;
11701             case 0x7: /* FRECPS */
11702                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11703                 break;
11704             case 0x8: /* FMINNM */
11705                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11706                 break;
11707             case 0x9: /* FMLS */
11708                 /* As usual for ARM, separate negation for fused multiply-add */
11709                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11710                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11711                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11712                                            fpst);
11713                 break;
11714             case 0xa: /* FSUB */
11715                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11716                 break;
11717             case 0xe: /* FMIN */
11718                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11719                 break;
11720             case 0xf: /* FRSQRTS */
11721                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11722                 break;
11723             case 0x13: /* FMUL */
11724                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11725                 break;
11726             case 0x14: /* FCMGE */
11727                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11728                 break;
11729             case 0x15: /* FACGE */
11730                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11731                 break;
11732             case 0x17: /* FDIV */
11733                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11734                 break;
11735             case 0x1a: /* FABD */
11736                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11737                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11738                 break;
11739             case 0x1c: /* FCMGT */
11740                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11741                 break;
11742             case 0x1d: /* FACGT */
11743                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11744                 break;
11745             default:
11746                 g_assert_not_reached();
11747             }
11748 
11749             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11750         }
11751     }
11752 
11753     clear_vec_high(s, is_q, rd);
11754 }
11755 
11756 /* AdvSIMD three same extra
11757  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11758  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11759  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11760  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11761  */
11762 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11763 {
11764     int rd = extract32(insn, 0, 5);
11765     int rn = extract32(insn, 5, 5);
11766     int opcode = extract32(insn, 11, 4);
11767     int rm = extract32(insn, 16, 5);
11768     int size = extract32(insn, 22, 2);
11769     bool u = extract32(insn, 29, 1);
11770     bool is_q = extract32(insn, 30, 1);
11771     bool feature;
11772     int rot;
11773 
11774     switch (u * 16 + opcode) {
11775     case 0x10: /* SQRDMLAH (vector) */
11776     case 0x11: /* SQRDMLSH (vector) */
11777         if (size != 1 && size != 2) {
11778             unallocated_encoding(s);
11779             return;
11780         }
11781         feature = dc_isar_feature(aa64_rdm, s);
11782         break;
11783     case 0x02: /* SDOT (vector) */
11784     case 0x12: /* UDOT (vector) */
11785         if (size != MO_32) {
11786             unallocated_encoding(s);
11787             return;
11788         }
11789         feature = dc_isar_feature(aa64_dp, s);
11790         break;
11791     case 0x03: /* USDOT */
11792         if (size != MO_32) {
11793             unallocated_encoding(s);
11794             return;
11795         }
11796         feature = dc_isar_feature(aa64_i8mm, s);
11797         break;
11798     case 0x04: /* SMMLA */
11799     case 0x14: /* UMMLA */
11800     case 0x05: /* USMMLA */
11801         if (!is_q || size != MO_32) {
11802             unallocated_encoding(s);
11803             return;
11804         }
11805         feature = dc_isar_feature(aa64_i8mm, s);
11806         break;
11807     case 0x18: /* FCMLA, #0 */
11808     case 0x19: /* FCMLA, #90 */
11809     case 0x1a: /* FCMLA, #180 */
11810     case 0x1b: /* FCMLA, #270 */
11811     case 0x1c: /* FCADD, #90 */
11812     case 0x1e: /* FCADD, #270 */
11813         if (size == 0
11814             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11815             || (size == 3 && !is_q)) {
11816             unallocated_encoding(s);
11817             return;
11818         }
11819         feature = dc_isar_feature(aa64_fcma, s);
11820         break;
11821     case 0x1d: /* BFMMLA */
11822         if (size != MO_16 || !is_q) {
11823             unallocated_encoding(s);
11824             return;
11825         }
11826         feature = dc_isar_feature(aa64_bf16, s);
11827         break;
11828     case 0x1f:
11829         switch (size) {
11830         case 1: /* BFDOT */
11831         case 3: /* BFMLAL{B,T} */
11832             feature = dc_isar_feature(aa64_bf16, s);
11833             break;
11834         default:
11835             unallocated_encoding(s);
11836             return;
11837         }
11838         break;
11839     default:
11840         unallocated_encoding(s);
11841         return;
11842     }
11843     if (!feature) {
11844         unallocated_encoding(s);
11845         return;
11846     }
11847     if (!fp_access_check(s)) {
11848         return;
11849     }
11850 
11851     switch (opcode) {
11852     case 0x0: /* SQRDMLAH (vector) */
11853         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11854         return;
11855 
11856     case 0x1: /* SQRDMLSH (vector) */
11857         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11858         return;
11859 
11860     case 0x2: /* SDOT / UDOT */
11861         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11862                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11863         return;
11864 
11865     case 0x3: /* USDOT */
11866         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11867         return;
11868 
11869     case 0x04: /* SMMLA, UMMLA */
11870         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11871                          u ? gen_helper_gvec_ummla_b
11872                          : gen_helper_gvec_smmla_b);
11873         return;
11874     case 0x05: /* USMMLA */
11875         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11876         return;
11877 
11878     case 0x8: /* FCMLA, #0 */
11879     case 0x9: /* FCMLA, #90 */
11880     case 0xa: /* FCMLA, #180 */
11881     case 0xb: /* FCMLA, #270 */
11882         rot = extract32(opcode, 0, 2);
11883         switch (size) {
11884         case 1:
11885             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11886                               gen_helper_gvec_fcmlah);
11887             break;
11888         case 2:
11889             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11890                               gen_helper_gvec_fcmlas);
11891             break;
11892         case 3:
11893             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11894                               gen_helper_gvec_fcmlad);
11895             break;
11896         default:
11897             g_assert_not_reached();
11898         }
11899         return;
11900 
11901     case 0xc: /* FCADD, #90 */
11902     case 0xe: /* FCADD, #270 */
11903         rot = extract32(opcode, 1, 1);
11904         switch (size) {
11905         case 1:
11906             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11907                               gen_helper_gvec_fcaddh);
11908             break;
11909         case 2:
11910             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11911                               gen_helper_gvec_fcadds);
11912             break;
11913         case 3:
11914             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11915                               gen_helper_gvec_fcaddd);
11916             break;
11917         default:
11918             g_assert_not_reached();
11919         }
11920         return;
11921 
11922     case 0xd: /* BFMMLA */
11923         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11924         return;
11925     case 0xf:
11926         switch (size) {
11927         case 1: /* BFDOT */
11928             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11929             break;
11930         case 3: /* BFMLAL{B,T} */
11931             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11932                               gen_helper_gvec_bfmlal);
11933             break;
11934         default:
11935             g_assert_not_reached();
11936         }
11937         return;
11938 
11939     default:
11940         g_assert_not_reached();
11941     }
11942 }
11943 
11944 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11945                                   int size, int rn, int rd)
11946 {
11947     /* Handle 2-reg-misc ops which are widening (so each size element
11948      * in the source becomes a 2*size element in the destination.
11949      * The only instruction like this is FCVTL.
11950      */
11951     int pass;
11952 
11953     if (size == 3) {
11954         /* 32 -> 64 bit fp conversion */
11955         TCGv_i64 tcg_res[2];
11956         int srcelt = is_q ? 2 : 0;
11957 
11958         for (pass = 0; pass < 2; pass++) {
11959             TCGv_i32 tcg_op = tcg_temp_new_i32();
11960             tcg_res[pass] = tcg_temp_new_i64();
11961 
11962             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11963             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11964         }
11965         for (pass = 0; pass < 2; pass++) {
11966             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11967         }
11968     } else {
11969         /* 16 -> 32 bit fp conversion */
11970         int srcelt = is_q ? 4 : 0;
11971         TCGv_i32 tcg_res[4];
11972         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11973         TCGv_i32 ahp = get_ahp_flag();
11974 
11975         for (pass = 0; pass < 4; pass++) {
11976             tcg_res[pass] = tcg_temp_new_i32();
11977 
11978             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11979             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11980                                            fpst, ahp);
11981         }
11982         for (pass = 0; pass < 4; pass++) {
11983             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11984         }
11985     }
11986 }
11987 
11988 static void handle_rev(DisasContext *s, int opcode, bool u,
11989                        bool is_q, int size, int rn, int rd)
11990 {
11991     int op = (opcode << 1) | u;
11992     int opsz = op + size;
11993     int grp_size = 3 - opsz;
11994     int dsize = is_q ? 128 : 64;
11995     int i;
11996 
11997     if (opsz >= 3) {
11998         unallocated_encoding(s);
11999         return;
12000     }
12001 
12002     if (!fp_access_check(s)) {
12003         return;
12004     }
12005 
12006     if (size == 0) {
12007         /* Special case bytes, use bswap op on each group of elements */
12008         int groups = dsize / (8 << grp_size);
12009 
12010         for (i = 0; i < groups; i++) {
12011             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12012 
12013             read_vec_element(s, tcg_tmp, rn, i, grp_size);
12014             switch (grp_size) {
12015             case MO_16:
12016                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12017                 break;
12018             case MO_32:
12019                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12020                 break;
12021             case MO_64:
12022                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12023                 break;
12024             default:
12025                 g_assert_not_reached();
12026             }
12027             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12028         }
12029         clear_vec_high(s, is_q, rd);
12030     } else {
12031         int revmask = (1 << grp_size) - 1;
12032         int esize = 8 << size;
12033         int elements = dsize / esize;
12034         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12035         TCGv_i64 tcg_rd[2];
12036 
12037         for (i = 0; i < 2; i++) {
12038             tcg_rd[i] = tcg_temp_new_i64();
12039             tcg_gen_movi_i64(tcg_rd[i], 0);
12040         }
12041 
12042         for (i = 0; i < elements; i++) {
12043             int e_rev = (i & 0xf) ^ revmask;
12044             int w = (e_rev * esize) / 64;
12045             int o = (e_rev * esize) % 64;
12046 
12047             read_vec_element(s, tcg_rn, rn, i, size);
12048             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12049         }
12050 
12051         for (i = 0; i < 2; i++) {
12052             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12053         }
12054         clear_vec_high(s, true, rd);
12055     }
12056 }
12057 
12058 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12059                                   bool is_q, int size, int rn, int rd)
12060 {
12061     /* Implement the pairwise operations from 2-misc:
12062      * SADDLP, UADDLP, SADALP, UADALP.
12063      * These all add pairs of elements in the input to produce a
12064      * double-width result element in the output (possibly accumulating).
12065      */
12066     bool accum = (opcode == 0x6);
12067     int maxpass = is_q ? 2 : 1;
12068     int pass;
12069     TCGv_i64 tcg_res[2];
12070 
12071     if (size == 2) {
12072         /* 32 + 32 -> 64 op */
12073         MemOp memop = size + (u ? 0 : MO_SIGN);
12074 
12075         for (pass = 0; pass < maxpass; pass++) {
12076             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12077             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12078 
12079             tcg_res[pass] = tcg_temp_new_i64();
12080 
12081             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12082             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12083             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12084             if (accum) {
12085                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12086                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12087             }
12088         }
12089     } else {
12090         for (pass = 0; pass < maxpass; pass++) {
12091             TCGv_i64 tcg_op = tcg_temp_new_i64();
12092             NeonGenOne64OpFn *genfn;
12093             static NeonGenOne64OpFn * const fns[2][2] = {
12094                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12095                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12096             };
12097 
12098             genfn = fns[size][u];
12099 
12100             tcg_res[pass] = tcg_temp_new_i64();
12101 
12102             read_vec_element(s, tcg_op, rn, pass, MO_64);
12103             genfn(tcg_res[pass], tcg_op);
12104 
12105             if (accum) {
12106                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12107                 if (size == 0) {
12108                     gen_helper_neon_addl_u16(tcg_res[pass],
12109                                              tcg_res[pass], tcg_op);
12110                 } else {
12111                     gen_helper_neon_addl_u32(tcg_res[pass],
12112                                              tcg_res[pass], tcg_op);
12113                 }
12114             }
12115         }
12116     }
12117     if (!is_q) {
12118         tcg_res[1] = tcg_constant_i64(0);
12119     }
12120     for (pass = 0; pass < 2; pass++) {
12121         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12122     }
12123 }
12124 
12125 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12126 {
12127     /* Implement SHLL and SHLL2 */
12128     int pass;
12129     int part = is_q ? 2 : 0;
12130     TCGv_i64 tcg_res[2];
12131 
12132     for (pass = 0; pass < 2; pass++) {
12133         static NeonGenWidenFn * const widenfns[3] = {
12134             gen_helper_neon_widen_u8,
12135             gen_helper_neon_widen_u16,
12136             tcg_gen_extu_i32_i64,
12137         };
12138         NeonGenWidenFn *widenfn = widenfns[size];
12139         TCGv_i32 tcg_op = tcg_temp_new_i32();
12140 
12141         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12142         tcg_res[pass] = tcg_temp_new_i64();
12143         widenfn(tcg_res[pass], tcg_op);
12144         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12145     }
12146 
12147     for (pass = 0; pass < 2; pass++) {
12148         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12149     }
12150 }
12151 
12152 /* AdvSIMD two reg misc
12153  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12154  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12155  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12156  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12157  */
12158 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12159 {
12160     int size = extract32(insn, 22, 2);
12161     int opcode = extract32(insn, 12, 5);
12162     bool u = extract32(insn, 29, 1);
12163     bool is_q = extract32(insn, 30, 1);
12164     int rn = extract32(insn, 5, 5);
12165     int rd = extract32(insn, 0, 5);
12166     bool need_fpstatus = false;
12167     int rmode = -1;
12168     TCGv_i32 tcg_rmode;
12169     TCGv_ptr tcg_fpstatus;
12170 
12171     switch (opcode) {
12172     case 0x0: /* REV64, REV32 */
12173     case 0x1: /* REV16 */
12174         handle_rev(s, opcode, u, is_q, size, rn, rd);
12175         return;
12176     case 0x5: /* CNT, NOT, RBIT */
12177         if (u && size == 0) {
12178             /* NOT */
12179             break;
12180         } else if (u && size == 1) {
12181             /* RBIT */
12182             break;
12183         } else if (!u && size == 0) {
12184             /* CNT */
12185             break;
12186         }
12187         unallocated_encoding(s);
12188         return;
12189     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12190     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12191         if (size == 3) {
12192             unallocated_encoding(s);
12193             return;
12194         }
12195         if (!fp_access_check(s)) {
12196             return;
12197         }
12198 
12199         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12200         return;
12201     case 0x4: /* CLS, CLZ */
12202         if (size == 3) {
12203             unallocated_encoding(s);
12204             return;
12205         }
12206         break;
12207     case 0x2: /* SADDLP, UADDLP */
12208     case 0x6: /* SADALP, UADALP */
12209         if (size == 3) {
12210             unallocated_encoding(s);
12211             return;
12212         }
12213         if (!fp_access_check(s)) {
12214             return;
12215         }
12216         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12217         return;
12218     case 0x13: /* SHLL, SHLL2 */
12219         if (u == 0 || size == 3) {
12220             unallocated_encoding(s);
12221             return;
12222         }
12223         if (!fp_access_check(s)) {
12224             return;
12225         }
12226         handle_shll(s, is_q, size, rn, rd);
12227         return;
12228     case 0xa: /* CMLT */
12229         if (u == 1) {
12230             unallocated_encoding(s);
12231             return;
12232         }
12233         /* fall through */
12234     case 0x8: /* CMGT, CMGE */
12235     case 0x9: /* CMEQ, CMLE */
12236     case 0xb: /* ABS, NEG */
12237         if (size == 3 && !is_q) {
12238             unallocated_encoding(s);
12239             return;
12240         }
12241         break;
12242     case 0x3: /* SUQADD, USQADD */
12243         if (size == 3 && !is_q) {
12244             unallocated_encoding(s);
12245             return;
12246         }
12247         if (!fp_access_check(s)) {
12248             return;
12249         }
12250         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12251         return;
12252     case 0x7: /* SQABS, SQNEG */
12253         if (size == 3 && !is_q) {
12254             unallocated_encoding(s);
12255             return;
12256         }
12257         break;
12258     case 0xc ... 0xf:
12259     case 0x16 ... 0x1f:
12260     {
12261         /* Floating point: U, size[1] and opcode indicate operation;
12262          * size[0] indicates single or double precision.
12263          */
12264         int is_double = extract32(size, 0, 1);
12265         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12266         size = is_double ? 3 : 2;
12267         switch (opcode) {
12268         case 0x2f: /* FABS */
12269         case 0x6f: /* FNEG */
12270             if (size == 3 && !is_q) {
12271                 unallocated_encoding(s);
12272                 return;
12273             }
12274             break;
12275         case 0x1d: /* SCVTF */
12276         case 0x5d: /* UCVTF */
12277         {
12278             bool is_signed = (opcode == 0x1d) ? true : false;
12279             int elements = is_double ? 2 : is_q ? 4 : 2;
12280             if (is_double && !is_q) {
12281                 unallocated_encoding(s);
12282                 return;
12283             }
12284             if (!fp_access_check(s)) {
12285                 return;
12286             }
12287             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12288             return;
12289         }
12290         case 0x2c: /* FCMGT (zero) */
12291         case 0x2d: /* FCMEQ (zero) */
12292         case 0x2e: /* FCMLT (zero) */
12293         case 0x6c: /* FCMGE (zero) */
12294         case 0x6d: /* FCMLE (zero) */
12295             if (size == 3 && !is_q) {
12296                 unallocated_encoding(s);
12297                 return;
12298             }
12299             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12300             return;
12301         case 0x7f: /* FSQRT */
12302             if (size == 3 && !is_q) {
12303                 unallocated_encoding(s);
12304                 return;
12305             }
12306             break;
12307         case 0x1a: /* FCVTNS */
12308         case 0x1b: /* FCVTMS */
12309         case 0x3a: /* FCVTPS */
12310         case 0x3b: /* FCVTZS */
12311         case 0x5a: /* FCVTNU */
12312         case 0x5b: /* FCVTMU */
12313         case 0x7a: /* FCVTPU */
12314         case 0x7b: /* FCVTZU */
12315             need_fpstatus = true;
12316             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12317             if (size == 3 && !is_q) {
12318                 unallocated_encoding(s);
12319                 return;
12320             }
12321             break;
12322         case 0x5c: /* FCVTAU */
12323         case 0x1c: /* FCVTAS */
12324             need_fpstatus = true;
12325             rmode = FPROUNDING_TIEAWAY;
12326             if (size == 3 && !is_q) {
12327                 unallocated_encoding(s);
12328                 return;
12329             }
12330             break;
12331         case 0x3c: /* URECPE */
12332             if (size == 3) {
12333                 unallocated_encoding(s);
12334                 return;
12335             }
12336             /* fall through */
12337         case 0x3d: /* FRECPE */
12338         case 0x7d: /* FRSQRTE */
12339             if (size == 3 && !is_q) {
12340                 unallocated_encoding(s);
12341                 return;
12342             }
12343             if (!fp_access_check(s)) {
12344                 return;
12345             }
12346             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12347             return;
12348         case 0x56: /* FCVTXN, FCVTXN2 */
12349             if (size == 2) {
12350                 unallocated_encoding(s);
12351                 return;
12352             }
12353             /* fall through */
12354         case 0x16: /* FCVTN, FCVTN2 */
12355             /* handle_2misc_narrow does a 2*size -> size operation, but these
12356              * instructions encode the source size rather than dest size.
12357              */
12358             if (!fp_access_check(s)) {
12359                 return;
12360             }
12361             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12362             return;
12363         case 0x36: /* BFCVTN, BFCVTN2 */
12364             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12365                 unallocated_encoding(s);
12366                 return;
12367             }
12368             if (!fp_access_check(s)) {
12369                 return;
12370             }
12371             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12372             return;
12373         case 0x17: /* FCVTL, FCVTL2 */
12374             if (!fp_access_check(s)) {
12375                 return;
12376             }
12377             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12378             return;
12379         case 0x18: /* FRINTN */
12380         case 0x19: /* FRINTM */
12381         case 0x38: /* FRINTP */
12382         case 0x39: /* FRINTZ */
12383             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12384             /* fall through */
12385         case 0x59: /* FRINTX */
12386         case 0x79: /* FRINTI */
12387             need_fpstatus = true;
12388             if (size == 3 && !is_q) {
12389                 unallocated_encoding(s);
12390                 return;
12391             }
12392             break;
12393         case 0x58: /* FRINTA */
12394             rmode = FPROUNDING_TIEAWAY;
12395             need_fpstatus = true;
12396             if (size == 3 && !is_q) {
12397                 unallocated_encoding(s);
12398                 return;
12399             }
12400             break;
12401         case 0x7c: /* URSQRTE */
12402             if (size == 3) {
12403                 unallocated_encoding(s);
12404                 return;
12405             }
12406             break;
12407         case 0x1e: /* FRINT32Z */
12408         case 0x1f: /* FRINT64Z */
12409             rmode = FPROUNDING_ZERO;
12410             /* fall through */
12411         case 0x5e: /* FRINT32X */
12412         case 0x5f: /* FRINT64X */
12413             need_fpstatus = true;
12414             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12415                 unallocated_encoding(s);
12416                 return;
12417             }
12418             break;
12419         default:
12420             unallocated_encoding(s);
12421             return;
12422         }
12423         break;
12424     }
12425     default:
12426         unallocated_encoding(s);
12427         return;
12428     }
12429 
12430     if (!fp_access_check(s)) {
12431         return;
12432     }
12433 
12434     if (need_fpstatus || rmode >= 0) {
12435         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12436     } else {
12437         tcg_fpstatus = NULL;
12438     }
12439     if (rmode >= 0) {
12440         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12441     } else {
12442         tcg_rmode = NULL;
12443     }
12444 
12445     switch (opcode) {
12446     case 0x5:
12447         if (u && size == 0) { /* NOT */
12448             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12449             return;
12450         }
12451         break;
12452     case 0x8: /* CMGT, CMGE */
12453         if (u) {
12454             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12455         } else {
12456             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12457         }
12458         return;
12459     case 0x9: /* CMEQ, CMLE */
12460         if (u) {
12461             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12462         } else {
12463             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12464         }
12465         return;
12466     case 0xa: /* CMLT */
12467         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12468         return;
12469     case 0xb:
12470         if (u) { /* ABS, NEG */
12471             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12472         } else {
12473             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12474         }
12475         return;
12476     }
12477 
12478     if (size == 3) {
12479         /* All 64-bit element operations can be shared with scalar 2misc */
12480         int pass;
12481 
12482         /* Coverity claims (size == 3 && !is_q) has been eliminated
12483          * from all paths leading to here.
12484          */
12485         tcg_debug_assert(is_q);
12486         for (pass = 0; pass < 2; pass++) {
12487             TCGv_i64 tcg_op = tcg_temp_new_i64();
12488             TCGv_i64 tcg_res = tcg_temp_new_i64();
12489 
12490             read_vec_element(s, tcg_op, rn, pass, MO_64);
12491 
12492             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12493                             tcg_rmode, tcg_fpstatus);
12494 
12495             write_vec_element(s, tcg_res, rd, pass, MO_64);
12496         }
12497     } else {
12498         int pass;
12499 
12500         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12501             TCGv_i32 tcg_op = tcg_temp_new_i32();
12502             TCGv_i32 tcg_res = tcg_temp_new_i32();
12503 
12504             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12505 
12506             if (size == 2) {
12507                 /* Special cases for 32 bit elements */
12508                 switch (opcode) {
12509                 case 0x4: /* CLS */
12510                     if (u) {
12511                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12512                     } else {
12513                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12514                     }
12515                     break;
12516                 case 0x7: /* SQABS, SQNEG */
12517                     if (u) {
12518                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
12519                     } else {
12520                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
12521                     }
12522                     break;
12523                 case 0x2f: /* FABS */
12524                     gen_helper_vfp_abss(tcg_res, tcg_op);
12525                     break;
12526                 case 0x6f: /* FNEG */
12527                     gen_helper_vfp_negs(tcg_res, tcg_op);
12528                     break;
12529                 case 0x7f: /* FSQRT */
12530                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
12531                     break;
12532                 case 0x1a: /* FCVTNS */
12533                 case 0x1b: /* FCVTMS */
12534                 case 0x1c: /* FCVTAS */
12535                 case 0x3a: /* FCVTPS */
12536                 case 0x3b: /* FCVTZS */
12537                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12538                                          tcg_constant_i32(0), tcg_fpstatus);
12539                     break;
12540                 case 0x5a: /* FCVTNU */
12541                 case 0x5b: /* FCVTMU */
12542                 case 0x5c: /* FCVTAU */
12543                 case 0x7a: /* FCVTPU */
12544                 case 0x7b: /* FCVTZU */
12545                     gen_helper_vfp_touls(tcg_res, tcg_op,
12546                                          tcg_constant_i32(0), tcg_fpstatus);
12547                     break;
12548                 case 0x18: /* FRINTN */
12549                 case 0x19: /* FRINTM */
12550                 case 0x38: /* FRINTP */
12551                 case 0x39: /* FRINTZ */
12552                 case 0x58: /* FRINTA */
12553                 case 0x79: /* FRINTI */
12554                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12555                     break;
12556                 case 0x59: /* FRINTX */
12557                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12558                     break;
12559                 case 0x7c: /* URSQRTE */
12560                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12561                     break;
12562                 case 0x1e: /* FRINT32Z */
12563                 case 0x5e: /* FRINT32X */
12564                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12565                     break;
12566                 case 0x1f: /* FRINT64Z */
12567                 case 0x5f: /* FRINT64X */
12568                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12569                     break;
12570                 default:
12571                     g_assert_not_reached();
12572                 }
12573             } else {
12574                 /* Use helpers for 8 and 16 bit elements */
12575                 switch (opcode) {
12576                 case 0x5: /* CNT, RBIT */
12577                     /* For these two insns size is part of the opcode specifier
12578                      * (handled earlier); they always operate on byte elements.
12579                      */
12580                     if (u) {
12581                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12582                     } else {
12583                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12584                     }
12585                     break;
12586                 case 0x7: /* SQABS, SQNEG */
12587                 {
12588                     NeonGenOneOpEnvFn *genfn;
12589                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12590                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12591                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12592                     };
12593                     genfn = fns[size][u];
12594                     genfn(tcg_res, tcg_env, tcg_op);
12595                     break;
12596                 }
12597                 case 0x4: /* CLS, CLZ */
12598                     if (u) {
12599                         if (size == 0) {
12600                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12601                         } else {
12602                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12603                         }
12604                     } else {
12605                         if (size == 0) {
12606                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12607                         } else {
12608                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12609                         }
12610                     }
12611                     break;
12612                 default:
12613                     g_assert_not_reached();
12614                 }
12615             }
12616 
12617             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12618         }
12619     }
12620     clear_vec_high(s, is_q, rd);
12621 
12622     if (tcg_rmode) {
12623         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12624     }
12625 }
12626 
12627 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12628  *
12629  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12630  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12631  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12632  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12633  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12634  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12635  *
12636  * This actually covers two groups where scalar access is governed by
12637  * bit 28. A bunch of the instructions (float to integral) only exist
12638  * in the vector form and are un-allocated for the scalar decode. Also
12639  * in the scalar decode Q is always 1.
12640  */
12641 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12642 {
12643     int fpop, opcode, a, u;
12644     int rn, rd;
12645     bool is_q;
12646     bool is_scalar;
12647     bool only_in_vector = false;
12648 
12649     int pass;
12650     TCGv_i32 tcg_rmode = NULL;
12651     TCGv_ptr tcg_fpstatus = NULL;
12652     bool need_fpst = true;
12653     int rmode = -1;
12654 
12655     if (!dc_isar_feature(aa64_fp16, s)) {
12656         unallocated_encoding(s);
12657         return;
12658     }
12659 
12660     rd = extract32(insn, 0, 5);
12661     rn = extract32(insn, 5, 5);
12662 
12663     a = extract32(insn, 23, 1);
12664     u = extract32(insn, 29, 1);
12665     is_scalar = extract32(insn, 28, 1);
12666     is_q = extract32(insn, 30, 1);
12667 
12668     opcode = extract32(insn, 12, 5);
12669     fpop = deposit32(opcode, 5, 1, a);
12670     fpop = deposit32(fpop, 6, 1, u);
12671 
12672     switch (fpop) {
12673     case 0x1d: /* SCVTF */
12674     case 0x5d: /* UCVTF */
12675     {
12676         int elements;
12677 
12678         if (is_scalar) {
12679             elements = 1;
12680         } else {
12681             elements = (is_q ? 8 : 4);
12682         }
12683 
12684         if (!fp_access_check(s)) {
12685             return;
12686         }
12687         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12688         return;
12689     }
12690     break;
12691     case 0x2c: /* FCMGT (zero) */
12692     case 0x2d: /* FCMEQ (zero) */
12693     case 0x2e: /* FCMLT (zero) */
12694     case 0x6c: /* FCMGE (zero) */
12695     case 0x6d: /* FCMLE (zero) */
12696         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12697         return;
12698     case 0x3d: /* FRECPE */
12699     case 0x3f: /* FRECPX */
12700         break;
12701     case 0x18: /* FRINTN */
12702         only_in_vector = true;
12703         rmode = FPROUNDING_TIEEVEN;
12704         break;
12705     case 0x19: /* FRINTM */
12706         only_in_vector = true;
12707         rmode = FPROUNDING_NEGINF;
12708         break;
12709     case 0x38: /* FRINTP */
12710         only_in_vector = true;
12711         rmode = FPROUNDING_POSINF;
12712         break;
12713     case 0x39: /* FRINTZ */
12714         only_in_vector = true;
12715         rmode = FPROUNDING_ZERO;
12716         break;
12717     case 0x58: /* FRINTA */
12718         only_in_vector = true;
12719         rmode = FPROUNDING_TIEAWAY;
12720         break;
12721     case 0x59: /* FRINTX */
12722     case 0x79: /* FRINTI */
12723         only_in_vector = true;
12724         /* current rounding mode */
12725         break;
12726     case 0x1a: /* FCVTNS */
12727         rmode = FPROUNDING_TIEEVEN;
12728         break;
12729     case 0x1b: /* FCVTMS */
12730         rmode = FPROUNDING_NEGINF;
12731         break;
12732     case 0x1c: /* FCVTAS */
12733         rmode = FPROUNDING_TIEAWAY;
12734         break;
12735     case 0x3a: /* FCVTPS */
12736         rmode = FPROUNDING_POSINF;
12737         break;
12738     case 0x3b: /* FCVTZS */
12739         rmode = FPROUNDING_ZERO;
12740         break;
12741     case 0x5a: /* FCVTNU */
12742         rmode = FPROUNDING_TIEEVEN;
12743         break;
12744     case 0x5b: /* FCVTMU */
12745         rmode = FPROUNDING_NEGINF;
12746         break;
12747     case 0x5c: /* FCVTAU */
12748         rmode = FPROUNDING_TIEAWAY;
12749         break;
12750     case 0x7a: /* FCVTPU */
12751         rmode = FPROUNDING_POSINF;
12752         break;
12753     case 0x7b: /* FCVTZU */
12754         rmode = FPROUNDING_ZERO;
12755         break;
12756     case 0x2f: /* FABS */
12757     case 0x6f: /* FNEG */
12758         need_fpst = false;
12759         break;
12760     case 0x7d: /* FRSQRTE */
12761     case 0x7f: /* FSQRT (vector) */
12762         break;
12763     default:
12764         unallocated_encoding(s);
12765         return;
12766     }
12767 
12768 
12769     /* Check additional constraints for the scalar encoding */
12770     if (is_scalar) {
12771         if (!is_q) {
12772             unallocated_encoding(s);
12773             return;
12774         }
12775         /* FRINTxx is only in the vector form */
12776         if (only_in_vector) {
12777             unallocated_encoding(s);
12778             return;
12779         }
12780     }
12781 
12782     if (!fp_access_check(s)) {
12783         return;
12784     }
12785 
12786     if (rmode >= 0 || need_fpst) {
12787         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12788     }
12789 
12790     if (rmode >= 0) {
12791         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12792     }
12793 
12794     if (is_scalar) {
12795         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12796         TCGv_i32 tcg_res = tcg_temp_new_i32();
12797 
12798         switch (fpop) {
12799         case 0x1a: /* FCVTNS */
12800         case 0x1b: /* FCVTMS */
12801         case 0x1c: /* FCVTAS */
12802         case 0x3a: /* FCVTPS */
12803         case 0x3b: /* FCVTZS */
12804             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12805             break;
12806         case 0x3d: /* FRECPE */
12807             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12808             break;
12809         case 0x3f: /* FRECPX */
12810             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12811             break;
12812         case 0x5a: /* FCVTNU */
12813         case 0x5b: /* FCVTMU */
12814         case 0x5c: /* FCVTAU */
12815         case 0x7a: /* FCVTPU */
12816         case 0x7b: /* FCVTZU */
12817             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12818             break;
12819         case 0x6f: /* FNEG */
12820             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12821             break;
12822         case 0x7d: /* FRSQRTE */
12823             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12824             break;
12825         default:
12826             g_assert_not_reached();
12827         }
12828 
12829         /* limit any sign extension going on */
12830         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12831         write_fp_sreg(s, rd, tcg_res);
12832     } else {
12833         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12834             TCGv_i32 tcg_op = tcg_temp_new_i32();
12835             TCGv_i32 tcg_res = tcg_temp_new_i32();
12836 
12837             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12838 
12839             switch (fpop) {
12840             case 0x1a: /* FCVTNS */
12841             case 0x1b: /* FCVTMS */
12842             case 0x1c: /* FCVTAS */
12843             case 0x3a: /* FCVTPS */
12844             case 0x3b: /* FCVTZS */
12845                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12846                 break;
12847             case 0x3d: /* FRECPE */
12848                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12849                 break;
12850             case 0x5a: /* FCVTNU */
12851             case 0x5b: /* FCVTMU */
12852             case 0x5c: /* FCVTAU */
12853             case 0x7a: /* FCVTPU */
12854             case 0x7b: /* FCVTZU */
12855                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12856                 break;
12857             case 0x18: /* FRINTN */
12858             case 0x19: /* FRINTM */
12859             case 0x38: /* FRINTP */
12860             case 0x39: /* FRINTZ */
12861             case 0x58: /* FRINTA */
12862             case 0x79: /* FRINTI */
12863                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12864                 break;
12865             case 0x59: /* FRINTX */
12866                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12867                 break;
12868             case 0x2f: /* FABS */
12869                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12870                 break;
12871             case 0x6f: /* FNEG */
12872                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12873                 break;
12874             case 0x7d: /* FRSQRTE */
12875                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12876                 break;
12877             case 0x7f: /* FSQRT */
12878                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12879                 break;
12880             default:
12881                 g_assert_not_reached();
12882             }
12883 
12884             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12885         }
12886 
12887         clear_vec_high(s, is_q, rd);
12888     }
12889 
12890     if (tcg_rmode) {
12891         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12892     }
12893 }
12894 
12895 /* AdvSIMD scalar x indexed element
12896  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12897  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12898  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12899  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12900  * AdvSIMD vector x indexed element
12901  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12902  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12903  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12904  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12905  */
12906 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12907 {
12908     /* This encoding has two kinds of instruction:
12909      *  normal, where we perform elt x idxelt => elt for each
12910      *     element in the vector
12911      *  long, where we perform elt x idxelt and generate a result of
12912      *     double the width of the input element
12913      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12914      */
12915     bool is_scalar = extract32(insn, 28, 1);
12916     bool is_q = extract32(insn, 30, 1);
12917     bool u = extract32(insn, 29, 1);
12918     int size = extract32(insn, 22, 2);
12919     int l = extract32(insn, 21, 1);
12920     int m = extract32(insn, 20, 1);
12921     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12922     int rm = extract32(insn, 16, 4);
12923     int opcode = extract32(insn, 12, 4);
12924     int h = extract32(insn, 11, 1);
12925     int rn = extract32(insn, 5, 5);
12926     int rd = extract32(insn, 0, 5);
12927     bool is_long = false;
12928     int is_fp = 0;
12929     bool is_fp16 = false;
12930     int index;
12931     TCGv_ptr fpst;
12932 
12933     switch (16 * u + opcode) {
12934     case 0x08: /* MUL */
12935     case 0x10: /* MLA */
12936     case 0x14: /* MLS */
12937         if (is_scalar) {
12938             unallocated_encoding(s);
12939             return;
12940         }
12941         break;
12942     case 0x02: /* SMLAL, SMLAL2 */
12943     case 0x12: /* UMLAL, UMLAL2 */
12944     case 0x06: /* SMLSL, SMLSL2 */
12945     case 0x16: /* UMLSL, UMLSL2 */
12946     case 0x0a: /* SMULL, SMULL2 */
12947     case 0x1a: /* UMULL, UMULL2 */
12948         if (is_scalar) {
12949             unallocated_encoding(s);
12950             return;
12951         }
12952         is_long = true;
12953         break;
12954     case 0x03: /* SQDMLAL, SQDMLAL2 */
12955     case 0x07: /* SQDMLSL, SQDMLSL2 */
12956     case 0x0b: /* SQDMULL, SQDMULL2 */
12957         is_long = true;
12958         break;
12959     case 0x0c: /* SQDMULH */
12960     case 0x0d: /* SQRDMULH */
12961         break;
12962     case 0x01: /* FMLA */
12963     case 0x05: /* FMLS */
12964     case 0x09: /* FMUL */
12965     case 0x19: /* FMULX */
12966         is_fp = 1;
12967         break;
12968     case 0x1d: /* SQRDMLAH */
12969     case 0x1f: /* SQRDMLSH */
12970         if (!dc_isar_feature(aa64_rdm, s)) {
12971             unallocated_encoding(s);
12972             return;
12973         }
12974         break;
12975     case 0x0e: /* SDOT */
12976     case 0x1e: /* UDOT */
12977         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12978             unallocated_encoding(s);
12979             return;
12980         }
12981         break;
12982     case 0x0f:
12983         switch (size) {
12984         case 0: /* SUDOT */
12985         case 2: /* USDOT */
12986             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12987                 unallocated_encoding(s);
12988                 return;
12989             }
12990             size = MO_32;
12991             break;
12992         case 1: /* BFDOT */
12993             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12994                 unallocated_encoding(s);
12995                 return;
12996             }
12997             size = MO_32;
12998             break;
12999         case 3: /* BFMLAL{B,T} */
13000             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13001                 unallocated_encoding(s);
13002                 return;
13003             }
13004             /* can't set is_fp without other incorrect size checks */
13005             size = MO_16;
13006             break;
13007         default:
13008             unallocated_encoding(s);
13009             return;
13010         }
13011         break;
13012     case 0x11: /* FCMLA #0 */
13013     case 0x13: /* FCMLA #90 */
13014     case 0x15: /* FCMLA #180 */
13015     case 0x17: /* FCMLA #270 */
13016         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13017             unallocated_encoding(s);
13018             return;
13019         }
13020         is_fp = 2;
13021         break;
13022     case 0x00: /* FMLAL */
13023     case 0x04: /* FMLSL */
13024     case 0x18: /* FMLAL2 */
13025     case 0x1c: /* FMLSL2 */
13026         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13027             unallocated_encoding(s);
13028             return;
13029         }
13030         size = MO_16;
13031         /* is_fp, but we pass tcg_env not fp_status.  */
13032         break;
13033     default:
13034         unallocated_encoding(s);
13035         return;
13036     }
13037 
13038     switch (is_fp) {
13039     case 1: /* normal fp */
13040         /* convert insn encoded size to MemOp size */
13041         switch (size) {
13042         case 0: /* half-precision */
13043             size = MO_16;
13044             is_fp16 = true;
13045             break;
13046         case MO_32: /* single precision */
13047         case MO_64: /* double precision */
13048             break;
13049         default:
13050             unallocated_encoding(s);
13051             return;
13052         }
13053         break;
13054 
13055     case 2: /* complex fp */
13056         /* Each indexable element is a complex pair.  */
13057         size += 1;
13058         switch (size) {
13059         case MO_32:
13060             if (h && !is_q) {
13061                 unallocated_encoding(s);
13062                 return;
13063             }
13064             is_fp16 = true;
13065             break;
13066         case MO_64:
13067             break;
13068         default:
13069             unallocated_encoding(s);
13070             return;
13071         }
13072         break;
13073 
13074     default: /* integer */
13075         switch (size) {
13076         case MO_8:
13077         case MO_64:
13078             unallocated_encoding(s);
13079             return;
13080         }
13081         break;
13082     }
13083     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13084         unallocated_encoding(s);
13085         return;
13086     }
13087 
13088     /* Given MemOp size, adjust register and indexing.  */
13089     switch (size) {
13090     case MO_16:
13091         index = h << 2 | l << 1 | m;
13092         break;
13093     case MO_32:
13094         index = h << 1 | l;
13095         rm |= m << 4;
13096         break;
13097     case MO_64:
13098         if (l || !is_q) {
13099             unallocated_encoding(s);
13100             return;
13101         }
13102         index = h;
13103         rm |= m << 4;
13104         break;
13105     default:
13106         g_assert_not_reached();
13107     }
13108 
13109     if (!fp_access_check(s)) {
13110         return;
13111     }
13112 
13113     if (is_fp) {
13114         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13115     } else {
13116         fpst = NULL;
13117     }
13118 
13119     switch (16 * u + opcode) {
13120     case 0x0e: /* SDOT */
13121     case 0x1e: /* UDOT */
13122         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13123                          u ? gen_helper_gvec_udot_idx_b
13124                          : gen_helper_gvec_sdot_idx_b);
13125         return;
13126     case 0x0f:
13127         switch (extract32(insn, 22, 2)) {
13128         case 0: /* SUDOT */
13129             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13130                              gen_helper_gvec_sudot_idx_b);
13131             return;
13132         case 1: /* BFDOT */
13133             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13134                              gen_helper_gvec_bfdot_idx);
13135             return;
13136         case 2: /* USDOT */
13137             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13138                              gen_helper_gvec_usdot_idx_b);
13139             return;
13140         case 3: /* BFMLAL{B,T} */
13141             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13142                               gen_helper_gvec_bfmlal_idx);
13143             return;
13144         }
13145         g_assert_not_reached();
13146     case 0x11: /* FCMLA #0 */
13147     case 0x13: /* FCMLA #90 */
13148     case 0x15: /* FCMLA #180 */
13149     case 0x17: /* FCMLA #270 */
13150         {
13151             int rot = extract32(insn, 13, 2);
13152             int data = (index << 2) | rot;
13153             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13154                                vec_full_reg_offset(s, rn),
13155                                vec_full_reg_offset(s, rm),
13156                                vec_full_reg_offset(s, rd), fpst,
13157                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13158                                size == MO_64
13159                                ? gen_helper_gvec_fcmlas_idx
13160                                : gen_helper_gvec_fcmlah_idx);
13161         }
13162         return;
13163 
13164     case 0x00: /* FMLAL */
13165     case 0x04: /* FMLSL */
13166     case 0x18: /* FMLAL2 */
13167     case 0x1c: /* FMLSL2 */
13168         {
13169             int is_s = extract32(opcode, 2, 1);
13170             int is_2 = u;
13171             int data = (index << 2) | (is_2 << 1) | is_s;
13172             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13173                                vec_full_reg_offset(s, rn),
13174                                vec_full_reg_offset(s, rm), tcg_env,
13175                                is_q ? 16 : 8, vec_full_reg_size(s),
13176                                data, gen_helper_gvec_fmlal_idx_a64);
13177         }
13178         return;
13179 
13180     case 0x08: /* MUL */
13181         if (!is_long && !is_scalar) {
13182             static gen_helper_gvec_3 * const fns[3] = {
13183                 gen_helper_gvec_mul_idx_h,
13184                 gen_helper_gvec_mul_idx_s,
13185                 gen_helper_gvec_mul_idx_d,
13186             };
13187             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13188                                vec_full_reg_offset(s, rn),
13189                                vec_full_reg_offset(s, rm),
13190                                is_q ? 16 : 8, vec_full_reg_size(s),
13191                                index, fns[size - 1]);
13192             return;
13193         }
13194         break;
13195 
13196     case 0x10: /* MLA */
13197         if (!is_long && !is_scalar) {
13198             static gen_helper_gvec_4 * const fns[3] = {
13199                 gen_helper_gvec_mla_idx_h,
13200                 gen_helper_gvec_mla_idx_s,
13201                 gen_helper_gvec_mla_idx_d,
13202             };
13203             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13204                                vec_full_reg_offset(s, rn),
13205                                vec_full_reg_offset(s, rm),
13206                                vec_full_reg_offset(s, rd),
13207                                is_q ? 16 : 8, vec_full_reg_size(s),
13208                                index, fns[size - 1]);
13209             return;
13210         }
13211         break;
13212 
13213     case 0x14: /* MLS */
13214         if (!is_long && !is_scalar) {
13215             static gen_helper_gvec_4 * const fns[3] = {
13216                 gen_helper_gvec_mls_idx_h,
13217                 gen_helper_gvec_mls_idx_s,
13218                 gen_helper_gvec_mls_idx_d,
13219             };
13220             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13221                                vec_full_reg_offset(s, rn),
13222                                vec_full_reg_offset(s, rm),
13223                                vec_full_reg_offset(s, rd),
13224                                is_q ? 16 : 8, vec_full_reg_size(s),
13225                                index, fns[size - 1]);
13226             return;
13227         }
13228         break;
13229     }
13230 
13231     if (size == 3) {
13232         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13233         int pass;
13234 
13235         assert(is_fp && is_q && !is_long);
13236 
13237         read_vec_element(s, tcg_idx, rm, index, MO_64);
13238 
13239         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13240             TCGv_i64 tcg_op = tcg_temp_new_i64();
13241             TCGv_i64 tcg_res = tcg_temp_new_i64();
13242 
13243             read_vec_element(s, tcg_op, rn, pass, MO_64);
13244 
13245             switch (16 * u + opcode) {
13246             case 0x05: /* FMLS */
13247                 /* As usual for ARM, separate negation for fused multiply-add */
13248                 gen_helper_vfp_negd(tcg_op, tcg_op);
13249                 /* fall through */
13250             case 0x01: /* FMLA */
13251                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13252                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13253                 break;
13254             case 0x09: /* FMUL */
13255                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13256                 break;
13257             case 0x19: /* FMULX */
13258                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13259                 break;
13260             default:
13261                 g_assert_not_reached();
13262             }
13263 
13264             write_vec_element(s, tcg_res, rd, pass, MO_64);
13265         }
13266 
13267         clear_vec_high(s, !is_scalar, rd);
13268     } else if (!is_long) {
13269         /* 32 bit floating point, or 16 or 32 bit integer.
13270          * For the 16 bit scalar case we use the usual Neon helpers and
13271          * rely on the fact that 0 op 0 == 0 with no side effects.
13272          */
13273         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13274         int pass, maxpasses;
13275 
13276         if (is_scalar) {
13277             maxpasses = 1;
13278         } else {
13279             maxpasses = is_q ? 4 : 2;
13280         }
13281 
13282         read_vec_element_i32(s, tcg_idx, rm, index, size);
13283 
13284         if (size == 1 && !is_scalar) {
13285             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13286              * the index into both halves of the 32 bit tcg_idx and then use
13287              * the usual Neon helpers.
13288              */
13289             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13290         }
13291 
13292         for (pass = 0; pass < maxpasses; pass++) {
13293             TCGv_i32 tcg_op = tcg_temp_new_i32();
13294             TCGv_i32 tcg_res = tcg_temp_new_i32();
13295 
13296             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13297 
13298             switch (16 * u + opcode) {
13299             case 0x08: /* MUL */
13300             case 0x10: /* MLA */
13301             case 0x14: /* MLS */
13302             {
13303                 static NeonGenTwoOpFn * const fns[2][2] = {
13304                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13305                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13306                 };
13307                 NeonGenTwoOpFn *genfn;
13308                 bool is_sub = opcode == 0x4;
13309 
13310                 if (size == 1) {
13311                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13312                 } else {
13313                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13314                 }
13315                 if (opcode == 0x8) {
13316                     break;
13317                 }
13318                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13319                 genfn = fns[size - 1][is_sub];
13320                 genfn(tcg_res, tcg_op, tcg_res);
13321                 break;
13322             }
13323             case 0x05: /* FMLS */
13324             case 0x01: /* FMLA */
13325                 read_vec_element_i32(s, tcg_res, rd, pass,
13326                                      is_scalar ? size : MO_32);
13327                 switch (size) {
13328                 case 1:
13329                     if (opcode == 0x5) {
13330                         /* As usual for ARM, separate negation for fused
13331                          * multiply-add */
13332                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13333                     }
13334                     if (is_scalar) {
13335                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13336                                                    tcg_res, fpst);
13337                     } else {
13338                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13339                                                     tcg_res, fpst);
13340                     }
13341                     break;
13342                 case 2:
13343                     if (opcode == 0x5) {
13344                         /* As usual for ARM, separate negation for
13345                          * fused multiply-add */
13346                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13347                     }
13348                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13349                                            tcg_res, fpst);
13350                     break;
13351                 default:
13352                     g_assert_not_reached();
13353                 }
13354                 break;
13355             case 0x09: /* FMUL */
13356                 switch (size) {
13357                 case 1:
13358                     if (is_scalar) {
13359                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13360                                                 tcg_idx, fpst);
13361                     } else {
13362                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13363                                                  tcg_idx, fpst);
13364                     }
13365                     break;
13366                 case 2:
13367                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13368                     break;
13369                 default:
13370                     g_assert_not_reached();
13371                 }
13372                 break;
13373             case 0x19: /* FMULX */
13374                 switch (size) {
13375                 case 1:
13376                     if (is_scalar) {
13377                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13378                                                  tcg_idx, fpst);
13379                     } else {
13380                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13381                                                   tcg_idx, fpst);
13382                     }
13383                     break;
13384                 case 2:
13385                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13386                     break;
13387                 default:
13388                     g_assert_not_reached();
13389                 }
13390                 break;
13391             case 0x0c: /* SQDMULH */
13392                 if (size == 1) {
13393                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
13394                                                tcg_op, tcg_idx);
13395                 } else {
13396                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
13397                                                tcg_op, tcg_idx);
13398                 }
13399                 break;
13400             case 0x0d: /* SQRDMULH */
13401                 if (size == 1) {
13402                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
13403                                                 tcg_op, tcg_idx);
13404                 } else {
13405                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
13406                                                 tcg_op, tcg_idx);
13407                 }
13408                 break;
13409             case 0x1d: /* SQRDMLAH */
13410                 read_vec_element_i32(s, tcg_res, rd, pass,
13411                                      is_scalar ? size : MO_32);
13412                 if (size == 1) {
13413                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
13414                                                 tcg_op, tcg_idx, tcg_res);
13415                 } else {
13416                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
13417                                                 tcg_op, tcg_idx, tcg_res);
13418                 }
13419                 break;
13420             case 0x1f: /* SQRDMLSH */
13421                 read_vec_element_i32(s, tcg_res, rd, pass,
13422                                      is_scalar ? size : MO_32);
13423                 if (size == 1) {
13424                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
13425                                                 tcg_op, tcg_idx, tcg_res);
13426                 } else {
13427                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
13428                                                 tcg_op, tcg_idx, tcg_res);
13429                 }
13430                 break;
13431             default:
13432                 g_assert_not_reached();
13433             }
13434 
13435             if (is_scalar) {
13436                 write_fp_sreg(s, rd, tcg_res);
13437             } else {
13438                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13439             }
13440         }
13441 
13442         clear_vec_high(s, is_q, rd);
13443     } else {
13444         /* long ops: 16x16->32 or 32x32->64 */
13445         TCGv_i64 tcg_res[2];
13446         int pass;
13447         bool satop = extract32(opcode, 0, 1);
13448         MemOp memop = MO_32;
13449 
13450         if (satop || !u) {
13451             memop |= MO_SIGN;
13452         }
13453 
13454         if (size == 2) {
13455             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13456 
13457             read_vec_element(s, tcg_idx, rm, index, memop);
13458 
13459             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13460                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13461                 TCGv_i64 tcg_passres;
13462                 int passelt;
13463 
13464                 if (is_scalar) {
13465                     passelt = 0;
13466                 } else {
13467                     passelt = pass + (is_q * 2);
13468                 }
13469 
13470                 read_vec_element(s, tcg_op, rn, passelt, memop);
13471 
13472                 tcg_res[pass] = tcg_temp_new_i64();
13473 
13474                 if (opcode == 0xa || opcode == 0xb) {
13475                     /* Non-accumulating ops */
13476                     tcg_passres = tcg_res[pass];
13477                 } else {
13478                     tcg_passres = tcg_temp_new_i64();
13479                 }
13480 
13481                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13482 
13483                 if (satop) {
13484                     /* saturating, doubling */
13485                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
13486                                                       tcg_passres, tcg_passres);
13487                 }
13488 
13489                 if (opcode == 0xa || opcode == 0xb) {
13490                     continue;
13491                 }
13492 
13493                 /* Accumulating op: handle accumulate step */
13494                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13495 
13496                 switch (opcode) {
13497                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13498                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13499                     break;
13500                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13501                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13502                     break;
13503                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13504                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13505                     /* fall through */
13506                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13507                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
13508                                                       tcg_res[pass],
13509                                                       tcg_passres);
13510                     break;
13511                 default:
13512                     g_assert_not_reached();
13513                 }
13514             }
13515 
13516             clear_vec_high(s, !is_scalar, rd);
13517         } else {
13518             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13519 
13520             assert(size == 1);
13521             read_vec_element_i32(s, tcg_idx, rm, index, size);
13522 
13523             if (!is_scalar) {
13524                 /* The simplest way to handle the 16x16 indexed ops is to
13525                  * duplicate the index into both halves of the 32 bit tcg_idx
13526                  * and then use the usual Neon helpers.
13527                  */
13528                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13529             }
13530 
13531             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13532                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13533                 TCGv_i64 tcg_passres;
13534 
13535                 if (is_scalar) {
13536                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13537                 } else {
13538                     read_vec_element_i32(s, tcg_op, rn,
13539                                          pass + (is_q * 2), MO_32);
13540                 }
13541 
13542                 tcg_res[pass] = tcg_temp_new_i64();
13543 
13544                 if (opcode == 0xa || opcode == 0xb) {
13545                     /* Non-accumulating ops */
13546                     tcg_passres = tcg_res[pass];
13547                 } else {
13548                     tcg_passres = tcg_temp_new_i64();
13549                 }
13550 
13551                 if (memop & MO_SIGN) {
13552                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13553                 } else {
13554                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13555                 }
13556                 if (satop) {
13557                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
13558                                                       tcg_passres, tcg_passres);
13559                 }
13560 
13561                 if (opcode == 0xa || opcode == 0xb) {
13562                     continue;
13563                 }
13564 
13565                 /* Accumulating op: handle accumulate step */
13566                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13567 
13568                 switch (opcode) {
13569                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13570                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13571                                              tcg_passres);
13572                     break;
13573                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13574                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13575                                              tcg_passres);
13576                     break;
13577                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13578                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13579                     /* fall through */
13580                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13581                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
13582                                                       tcg_res[pass],
13583                                                       tcg_passres);
13584                     break;
13585                 default:
13586                     g_assert_not_reached();
13587                 }
13588             }
13589 
13590             if (is_scalar) {
13591                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13592             }
13593         }
13594 
13595         if (is_scalar) {
13596             tcg_res[1] = tcg_constant_i64(0);
13597         }
13598 
13599         for (pass = 0; pass < 2; pass++) {
13600             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13601         }
13602     }
13603 }
13604 
13605 /* C3.6 Data processing - SIMD, inc Crypto
13606  *
13607  * As the decode gets a little complex we are using a table based
13608  * approach for this part of the decode.
13609  */
13610 static const AArch64DecodeTable data_proc_simd[] = {
13611     /* pattern  ,  mask     ,  fn                        */
13612     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13613     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13614     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13615     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13616     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13617     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13618     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13619     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13620     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13621     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13622     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13623     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13624     { 0x2e000000, 0xbf208400, disas_simd_ext },
13625     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13626     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13627     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13628     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13629     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13630     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13631     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13632     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13633     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13634     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13635     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13636     { 0x00000000, 0x00000000, NULL }
13637 };
13638 
13639 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13640 {
13641     /* Note that this is called with all non-FP cases from
13642      * table C3-6 so it must UNDEF for entries not specifically
13643      * allocated to instructions in that table.
13644      */
13645     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13646     if (fn) {
13647         fn(s, insn);
13648     } else {
13649         unallocated_encoding(s);
13650     }
13651 }
13652 
13653 /* C3.6 Data processing - SIMD and floating point */
13654 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13655 {
13656     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13657         disas_data_proc_fp(s, insn);
13658     } else {
13659         /* SIMD, including crypto */
13660         disas_data_proc_simd(s, insn);
13661     }
13662 }
13663 
13664 static bool trans_OK(DisasContext *s, arg_OK *a)
13665 {
13666     return true;
13667 }
13668 
13669 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13670 {
13671     s->is_nonstreaming = true;
13672     return true;
13673 }
13674 
13675 /**
13676  * is_guarded_page:
13677  * @env: The cpu environment
13678  * @s: The DisasContext
13679  *
13680  * Return true if the page is guarded.
13681  */
13682 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13683 {
13684     uint64_t addr = s->base.pc_first;
13685 #ifdef CONFIG_USER_ONLY
13686     return page_get_flags(addr) & PAGE_BTI;
13687 #else
13688     CPUTLBEntryFull *full;
13689     void *host;
13690     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13691     int flags;
13692 
13693     /*
13694      * We test this immediately after reading an insn, which means
13695      * that the TLB entry must be present and valid, and thus this
13696      * access will never raise an exception.
13697      */
13698     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13699                               false, &host, &full, 0);
13700     assert(!(flags & TLB_INVALID_MASK));
13701 
13702     return full->extra.arm.guarded;
13703 #endif
13704 }
13705 
13706 /**
13707  * btype_destination_ok:
13708  * @insn: The instruction at the branch destination
13709  * @bt: SCTLR_ELx.BT
13710  * @btype: PSTATE.BTYPE, and is non-zero
13711  *
13712  * On a guarded page, there are a limited number of insns
13713  * that may be present at the branch target:
13714  *   - branch target identifiers,
13715  *   - paciasp, pacibsp,
13716  *   - BRK insn
13717  *   - HLT insn
13718  * Anything else causes a Branch Target Exception.
13719  *
13720  * Return true if the branch is compatible, false to raise BTITRAP.
13721  */
13722 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13723 {
13724     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13725         /* HINT space */
13726         switch (extract32(insn, 5, 7)) {
13727         case 0b011001: /* PACIASP */
13728         case 0b011011: /* PACIBSP */
13729             /*
13730              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13731              * with btype == 3.  Otherwise all btype are ok.
13732              */
13733             return !bt || btype != 3;
13734         case 0b100000: /* BTI */
13735             /* Not compatible with any btype.  */
13736             return false;
13737         case 0b100010: /* BTI c */
13738             /* Not compatible with btype == 3 */
13739             return btype != 3;
13740         case 0b100100: /* BTI j */
13741             /* Not compatible with btype == 2 */
13742             return btype != 2;
13743         case 0b100110: /* BTI jc */
13744             /* Compatible with any btype.  */
13745             return true;
13746         }
13747     } else {
13748         switch (insn & 0xffe0001fu) {
13749         case 0xd4200000u: /* BRK */
13750         case 0xd4400000u: /* HLT */
13751             /* Give priority to the breakpoint exception.  */
13752             return true;
13753         }
13754     }
13755     return false;
13756 }
13757 
13758 /* C3.1 A64 instruction index by encoding */
13759 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13760 {
13761     switch (extract32(insn, 25, 4)) {
13762     case 0x5:
13763     case 0xd:      /* Data processing - register */
13764         disas_data_proc_reg(s, insn);
13765         break;
13766     case 0x7:
13767     case 0xf:      /* Data processing - SIMD and floating point */
13768         disas_data_proc_simd_fp(s, insn);
13769         break;
13770     default:
13771         unallocated_encoding(s);
13772         break;
13773     }
13774 }
13775 
13776 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13777                                           CPUState *cpu)
13778 {
13779     DisasContext *dc = container_of(dcbase, DisasContext, base);
13780     CPUARMState *env = cpu_env(cpu);
13781     ARMCPU *arm_cpu = env_archcpu(env);
13782     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13783     int bound, core_mmu_idx;
13784 
13785     dc->isar = &arm_cpu->isar;
13786     dc->condjmp = 0;
13787     dc->pc_save = dc->base.pc_first;
13788     dc->aarch64 = true;
13789     dc->thumb = false;
13790     dc->sctlr_b = 0;
13791     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13792     dc->condexec_mask = 0;
13793     dc->condexec_cond = 0;
13794     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
13795     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
13796     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
13797     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
13798     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
13799     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
13800 #if !defined(CONFIG_USER_ONLY)
13801     dc->user = (dc->current_el == 0);
13802 #endif
13803     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
13804     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
13805     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
13806     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
13807     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
13808     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
13809     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
13810     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
13811     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
13812     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
13813     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
13814     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
13815     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
13816     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
13817     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
13818     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
13819     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
13820     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
13821     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
13822     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
13823     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
13824     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
13825     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
13826     dc->nv1 = EX_TBFLAG_A64(tb_flags, NV1);
13827     dc->nv2 = EX_TBFLAG_A64(tb_flags, NV2);
13828     dc->nv2_mem_e20 = EX_TBFLAG_A64(tb_flags, NV2_MEM_E20);
13829     dc->nv2_mem_be = EX_TBFLAG_A64(tb_flags, NV2_MEM_BE);
13830     dc->vec_len = 0;
13831     dc->vec_stride = 0;
13832     dc->cp_regs = arm_cpu->cp_regs;
13833     dc->features = env->features;
13834     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
13835     dc->gm_blocksize = arm_cpu->gm_blocksize;
13836 
13837 #ifdef CONFIG_USER_ONLY
13838     /* In sve_probe_page, we assume TBI is enabled. */
13839     tcg_debug_assert(dc->tbid & 1);
13840 #endif
13841 
13842     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
13843 
13844     /* Single step state. The code-generation logic here is:
13845      *  SS_ACTIVE == 0:
13846      *   generate code with no special handling for single-stepping (except
13847      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
13848      *   this happens anyway because those changes are all system register or
13849      *   PSTATE writes).
13850      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
13851      *   emit code for one insn
13852      *   emit code to clear PSTATE.SS
13853      *   emit code to generate software step exception for completed step
13854      *   end TB (as usual for having generated an exception)
13855      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
13856      *   emit code to generate a software step exception
13857      *   end the TB
13858      */
13859     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
13860     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
13861     dc->is_ldex = false;
13862 
13863     /* Bound the number of insns to execute to those left on the page.  */
13864     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
13865 
13866     /* If architectural single step active, limit to 1.  */
13867     if (dc->ss_active) {
13868         bound = 1;
13869     }
13870     dc->base.max_insns = MIN(dc->base.max_insns, bound);
13871 }
13872 
13873 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
13874 {
13875 }
13876 
13877 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
13878 {
13879     DisasContext *dc = container_of(dcbase, DisasContext, base);
13880     target_ulong pc_arg = dc->base.pc_next;
13881 
13882     if (tb_cflags(dcbase->tb) & CF_PCREL) {
13883         pc_arg &= ~TARGET_PAGE_MASK;
13884     }
13885     tcg_gen_insn_start(pc_arg, 0, 0);
13886     dc->insn_start_updated = false;
13887 }
13888 
13889 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
13890 {
13891     DisasContext *s = container_of(dcbase, DisasContext, base);
13892     CPUARMState *env = cpu_env(cpu);
13893     uint64_t pc = s->base.pc_next;
13894     uint32_t insn;
13895 
13896     /* Singlestep exceptions have the highest priority. */
13897     if (s->ss_active && !s->pstate_ss) {
13898         /* Singlestep state is Active-pending.
13899          * If we're in this state at the start of a TB then either
13900          *  a) we just took an exception to an EL which is being debugged
13901          *     and this is the first insn in the exception handler
13902          *  b) debug exceptions were masked and we just unmasked them
13903          *     without changing EL (eg by clearing PSTATE.D)
13904          * In either case we're going to take a swstep exception in the
13905          * "did not step an insn" case, and so the syndrome ISV and EX
13906          * bits should be zero.
13907          */
13908         assert(s->base.num_insns == 1);
13909         gen_swstep_exception(s, 0, 0);
13910         s->base.is_jmp = DISAS_NORETURN;
13911         s->base.pc_next = pc + 4;
13912         return;
13913     }
13914 
13915     if (pc & 3) {
13916         /*
13917          * PC alignment fault.  This has priority over the instruction abort
13918          * that we would receive from a translation fault via arm_ldl_code.
13919          * This should only be possible after an indirect branch, at the
13920          * start of the TB.
13921          */
13922         assert(s->base.num_insns == 1);
13923         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
13924         s->base.is_jmp = DISAS_NORETURN;
13925         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
13926         return;
13927     }
13928 
13929     s->pc_curr = pc;
13930     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
13931     s->insn = insn;
13932     s->base.pc_next = pc + 4;
13933 
13934     s->fp_access_checked = false;
13935     s->sve_access_checked = false;
13936 
13937     if (s->pstate_il) {
13938         /*
13939          * Illegal execution state. This has priority over BTI
13940          * exceptions, but comes after instruction abort exceptions.
13941          */
13942         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
13943         return;
13944     }
13945 
13946     if (dc_isar_feature(aa64_bti, s)) {
13947         if (s->base.num_insns == 1) {
13948             /*
13949              * At the first insn of the TB, compute s->guarded_page.
13950              * We delayed computing this until successfully reading
13951              * the first insn of the TB, above.  This (mostly) ensures
13952              * that the softmmu tlb entry has been populated, and the
13953              * page table GP bit is available.
13954              *
13955              * Note that we need to compute this even if btype == 0,
13956              * because this value is used for BR instructions later
13957              * where ENV is not available.
13958              */
13959             s->guarded_page = is_guarded_page(env, s);
13960 
13961             /* First insn can have btype set to non-zero.  */
13962             tcg_debug_assert(s->btype >= 0);
13963 
13964             /*
13965              * Note that the Branch Target Exception has fairly high
13966              * priority -- below debugging exceptions but above most
13967              * everything else.  This allows us to handle this now
13968              * instead of waiting until the insn is otherwise decoded.
13969              */
13970             if (s->btype != 0
13971                 && s->guarded_page
13972                 && !btype_destination_ok(insn, s->bt, s->btype)) {
13973                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
13974                 return;
13975             }
13976         } else {
13977             /* Not the first insn: btype must be 0.  */
13978             tcg_debug_assert(s->btype == 0);
13979         }
13980     }
13981 
13982     s->is_nonstreaming = false;
13983     if (s->sme_trap_nonstreaming) {
13984         disas_sme_fa64(s, insn);
13985     }
13986 
13987     if (!disas_a64(s, insn) &&
13988         !disas_sme(s, insn) &&
13989         !disas_sve(s, insn)) {
13990         disas_a64_legacy(s, insn);
13991     }
13992 
13993     /*
13994      * After execution of most insns, btype is reset to 0.
13995      * Note that we set btype == -1 when the insn sets btype.
13996      */
13997     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
13998         reset_btype(s);
13999     }
14000 }
14001 
14002 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14003 {
14004     DisasContext *dc = container_of(dcbase, DisasContext, base);
14005 
14006     if (unlikely(dc->ss_active)) {
14007         /* Note that this means single stepping WFI doesn't halt the CPU.
14008          * For conditional branch insns this is harmless unreachable code as
14009          * gen_goto_tb() has already handled emitting the debug exception
14010          * (and thus a tb-jump is not possible when singlestepping).
14011          */
14012         switch (dc->base.is_jmp) {
14013         default:
14014             gen_a64_update_pc(dc, 4);
14015             /* fall through */
14016         case DISAS_EXIT:
14017         case DISAS_JUMP:
14018             gen_step_complete_exception(dc);
14019             break;
14020         case DISAS_NORETURN:
14021             break;
14022         }
14023     } else {
14024         switch (dc->base.is_jmp) {
14025         case DISAS_NEXT:
14026         case DISAS_TOO_MANY:
14027             gen_goto_tb(dc, 1, 4);
14028             break;
14029         default:
14030         case DISAS_UPDATE_EXIT:
14031             gen_a64_update_pc(dc, 4);
14032             /* fall through */
14033         case DISAS_EXIT:
14034             tcg_gen_exit_tb(NULL, 0);
14035             break;
14036         case DISAS_UPDATE_NOCHAIN:
14037             gen_a64_update_pc(dc, 4);
14038             /* fall through */
14039         case DISAS_JUMP:
14040             tcg_gen_lookup_and_goto_ptr();
14041             break;
14042         case DISAS_NORETURN:
14043         case DISAS_SWI:
14044             break;
14045         case DISAS_WFE:
14046             gen_a64_update_pc(dc, 4);
14047             gen_helper_wfe(tcg_env);
14048             break;
14049         case DISAS_YIELD:
14050             gen_a64_update_pc(dc, 4);
14051             gen_helper_yield(tcg_env);
14052             break;
14053         case DISAS_WFI:
14054             /*
14055              * This is a special case because we don't want to just halt
14056              * the CPU if trying to debug across a WFI.
14057              */
14058             gen_a64_update_pc(dc, 4);
14059             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
14060             /*
14061              * The helper doesn't necessarily throw an exception, but we
14062              * must go back to the main loop to check for interrupts anyway.
14063              */
14064             tcg_gen_exit_tb(NULL, 0);
14065             break;
14066         }
14067     }
14068 }
14069 
14070 const TranslatorOps aarch64_translator_ops = {
14071     .init_disas_context = aarch64_tr_init_disas_context,
14072     .tb_start           = aarch64_tr_tb_start,
14073     .insn_start         = aarch64_tr_insn_start,
14074     .translate_insn     = aarch64_tr_translate_insn,
14075     .tb_stop            = aarch64_tr_tb_stop,
14076 };
14077