xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision d6edf915)
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 /*
4706  * Advanced SIMD copy
4707  */
4708 
4709 static bool decode_esz_idx(int imm, MemOp *pesz, unsigned *pidx)
4710 {
4711     unsigned esz = ctz32(imm);
4712     if (esz <= MO_64) {
4713         *pesz = esz;
4714         *pidx = imm >> (esz + 1);
4715         return true;
4716     }
4717     return false;
4718 }
4719 
4720 static bool trans_DUP_element_s(DisasContext *s, arg_DUP_element_s *a)
4721 {
4722     MemOp esz;
4723     unsigned idx;
4724 
4725     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4726         return false;
4727     }
4728     if (fp_access_check(s)) {
4729         /*
4730          * This instruction just extracts the specified element and
4731          * zero-extends it into the bottom of the destination register.
4732          */
4733         TCGv_i64 tmp = tcg_temp_new_i64();
4734         read_vec_element(s, tmp, a->rn, idx, esz);
4735         write_fp_dreg(s, a->rd, tmp);
4736     }
4737     return true;
4738 }
4739 
4740 static bool trans_DUP_element_v(DisasContext *s, arg_DUP_element_v *a)
4741 {
4742     MemOp esz;
4743     unsigned idx;
4744 
4745     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4746         return false;
4747     }
4748     if (esz == MO_64 && !a->q) {
4749         return false;
4750     }
4751     if (fp_access_check(s)) {
4752         tcg_gen_gvec_dup_mem(esz, vec_full_reg_offset(s, a->rd),
4753                              vec_reg_offset(s, a->rn, idx, esz),
4754                              a->q ? 16 : 8, vec_full_reg_size(s));
4755     }
4756     return true;
4757 }
4758 
4759 static bool trans_DUP_general(DisasContext *s, arg_DUP_general *a)
4760 {
4761     MemOp esz;
4762     unsigned idx;
4763 
4764     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4765         return false;
4766     }
4767     if (esz == MO_64 && !a->q) {
4768         return false;
4769     }
4770     if (fp_access_check(s)) {
4771         tcg_gen_gvec_dup_i64(esz, vec_full_reg_offset(s, a->rd),
4772                              a->q ? 16 : 8, vec_full_reg_size(s),
4773                              cpu_reg(s, a->rn));
4774     }
4775     return true;
4776 }
4777 
4778 static bool do_smov_umov(DisasContext *s, arg_SMOV *a, MemOp is_signed)
4779 {
4780     MemOp esz;
4781     unsigned idx;
4782 
4783     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4784         return false;
4785     }
4786     if (is_signed) {
4787         if (esz == MO_64 || (esz == MO_32 && !a->q)) {
4788             return false;
4789         }
4790     } else {
4791         if (esz == MO_64 ? !a->q : a->q) {
4792             return false;
4793         }
4794     }
4795     if (fp_access_check(s)) {
4796         TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4797         read_vec_element(s, tcg_rd, a->rn, idx, esz | is_signed);
4798         if (is_signed && !a->q) {
4799             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4800         }
4801     }
4802     return true;
4803 }
4804 
4805 TRANS(SMOV, do_smov_umov, a, MO_SIGN)
4806 TRANS(UMOV, do_smov_umov, a, 0)
4807 
4808 static bool trans_INS_general(DisasContext *s, arg_INS_general *a)
4809 {
4810     MemOp esz;
4811     unsigned idx;
4812 
4813     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4814         return false;
4815     }
4816     if (fp_access_check(s)) {
4817         write_vec_element(s, cpu_reg(s, a->rn), a->rd, idx, esz);
4818         clear_vec_high(s, true, a->rd);
4819     }
4820     return true;
4821 }
4822 
4823 static bool trans_INS_element(DisasContext *s, arg_INS_element *a)
4824 {
4825     MemOp esz;
4826     unsigned didx, sidx;
4827 
4828     if (!decode_esz_idx(a->di, &esz, &didx)) {
4829         return false;
4830     }
4831     sidx = a->si >> esz;
4832     if (fp_access_check(s)) {
4833         TCGv_i64 tmp = tcg_temp_new_i64();
4834 
4835         read_vec_element(s, tmp, a->rn, sidx, esz);
4836         write_vec_element(s, tmp, a->rd, didx, esz);
4837 
4838         /* INS is considered a 128-bit write for SVE. */
4839         clear_vec_high(s, true, a->rd);
4840     }
4841     return true;
4842 }
4843 
4844 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4845  * Note that it is the caller's responsibility to ensure that the
4846  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4847  * mandated semantics for out of range shifts.
4848  */
4849 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4850                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4851 {
4852     switch (shift_type) {
4853     case A64_SHIFT_TYPE_LSL:
4854         tcg_gen_shl_i64(dst, src, shift_amount);
4855         break;
4856     case A64_SHIFT_TYPE_LSR:
4857         tcg_gen_shr_i64(dst, src, shift_amount);
4858         break;
4859     case A64_SHIFT_TYPE_ASR:
4860         if (!sf) {
4861             tcg_gen_ext32s_i64(dst, src);
4862         }
4863         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4864         break;
4865     case A64_SHIFT_TYPE_ROR:
4866         if (sf) {
4867             tcg_gen_rotr_i64(dst, src, shift_amount);
4868         } else {
4869             TCGv_i32 t0, t1;
4870             t0 = tcg_temp_new_i32();
4871             t1 = tcg_temp_new_i32();
4872             tcg_gen_extrl_i64_i32(t0, src);
4873             tcg_gen_extrl_i64_i32(t1, shift_amount);
4874             tcg_gen_rotr_i32(t0, t0, t1);
4875             tcg_gen_extu_i32_i64(dst, t0);
4876         }
4877         break;
4878     default:
4879         assert(FALSE); /* all shift types should be handled */
4880         break;
4881     }
4882 
4883     if (!sf) { /* zero extend final result */
4884         tcg_gen_ext32u_i64(dst, dst);
4885     }
4886 }
4887 
4888 /* Shift a TCGv src by immediate, put result in dst.
4889  * The shift amount must be in range (this should always be true as the
4890  * relevant instructions will UNDEF on bad shift immediates).
4891  */
4892 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4893                           enum a64_shift_type shift_type, unsigned int shift_i)
4894 {
4895     assert(shift_i < (sf ? 64 : 32));
4896 
4897     if (shift_i == 0) {
4898         tcg_gen_mov_i64(dst, src);
4899     } else {
4900         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4901     }
4902 }
4903 
4904 /* Logical (shifted register)
4905  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4906  * +----+-----+-----------+-------+---+------+--------+------+------+
4907  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4908  * +----+-----+-----------+-------+---+------+--------+------+------+
4909  */
4910 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4911 {
4912     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4913     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4914 
4915     sf = extract32(insn, 31, 1);
4916     opc = extract32(insn, 29, 2);
4917     shift_type = extract32(insn, 22, 2);
4918     invert = extract32(insn, 21, 1);
4919     rm = extract32(insn, 16, 5);
4920     shift_amount = extract32(insn, 10, 6);
4921     rn = extract32(insn, 5, 5);
4922     rd = extract32(insn, 0, 5);
4923 
4924     if (!sf && (shift_amount & (1 << 5))) {
4925         unallocated_encoding(s);
4926         return;
4927     }
4928 
4929     tcg_rd = cpu_reg(s, rd);
4930 
4931     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4932         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4933          * register-register MOV and MVN, so it is worth special casing.
4934          */
4935         tcg_rm = cpu_reg(s, rm);
4936         if (invert) {
4937             tcg_gen_not_i64(tcg_rd, tcg_rm);
4938             if (!sf) {
4939                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4940             }
4941         } else {
4942             if (sf) {
4943                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4944             } else {
4945                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4946             }
4947         }
4948         return;
4949     }
4950 
4951     tcg_rm = read_cpu_reg(s, rm, sf);
4952 
4953     if (shift_amount) {
4954         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4955     }
4956 
4957     tcg_rn = cpu_reg(s, rn);
4958 
4959     switch (opc | (invert << 2)) {
4960     case 0: /* AND */
4961     case 3: /* ANDS */
4962         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4963         break;
4964     case 1: /* ORR */
4965         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4966         break;
4967     case 2: /* EOR */
4968         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4969         break;
4970     case 4: /* BIC */
4971     case 7: /* BICS */
4972         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4973         break;
4974     case 5: /* ORN */
4975         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4976         break;
4977     case 6: /* EON */
4978         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4979         break;
4980     default:
4981         assert(FALSE);
4982         break;
4983     }
4984 
4985     if (!sf) {
4986         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4987     }
4988 
4989     if (opc == 3) {
4990         gen_logic_CC(sf, tcg_rd);
4991     }
4992 }
4993 
4994 /*
4995  * Add/subtract (extended register)
4996  *
4997  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4998  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4999  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
5000  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
5001  *
5002  *  sf: 0 -> 32bit, 1 -> 64bit
5003  *  op: 0 -> add  , 1 -> sub
5004  *   S: 1 -> set flags
5005  * opt: 00
5006  * option: extension type (see DecodeRegExtend)
5007  * imm3: optional shift to Rm
5008  *
5009  * Rd = Rn + LSL(extend(Rm), amount)
5010  */
5011 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
5012 {
5013     int rd = extract32(insn, 0, 5);
5014     int rn = extract32(insn, 5, 5);
5015     int imm3 = extract32(insn, 10, 3);
5016     int option = extract32(insn, 13, 3);
5017     int rm = extract32(insn, 16, 5);
5018     int opt = extract32(insn, 22, 2);
5019     bool setflags = extract32(insn, 29, 1);
5020     bool sub_op = extract32(insn, 30, 1);
5021     bool sf = extract32(insn, 31, 1);
5022 
5023     TCGv_i64 tcg_rm, tcg_rn; /* temps */
5024     TCGv_i64 tcg_rd;
5025     TCGv_i64 tcg_result;
5026 
5027     if (imm3 > 4 || opt != 0) {
5028         unallocated_encoding(s);
5029         return;
5030     }
5031 
5032     /* non-flag setting ops may use SP */
5033     if (!setflags) {
5034         tcg_rd = cpu_reg_sp(s, rd);
5035     } else {
5036         tcg_rd = cpu_reg(s, rd);
5037     }
5038     tcg_rn = read_cpu_reg_sp(s, rn, sf);
5039 
5040     tcg_rm = read_cpu_reg(s, rm, sf);
5041     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
5042 
5043     tcg_result = tcg_temp_new_i64();
5044 
5045     if (!setflags) {
5046         if (sub_op) {
5047             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
5048         } else {
5049             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
5050         }
5051     } else {
5052         if (sub_op) {
5053             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
5054         } else {
5055             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
5056         }
5057     }
5058 
5059     if (sf) {
5060         tcg_gen_mov_i64(tcg_rd, tcg_result);
5061     } else {
5062         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
5063     }
5064 }
5065 
5066 /*
5067  * Add/subtract (shifted register)
5068  *
5069  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
5070  * +--+--+--+-----------+-----+--+-------+---------+------+------+
5071  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
5072  * +--+--+--+-----------+-----+--+-------+---------+------+------+
5073  *
5074  *    sf: 0 -> 32bit, 1 -> 64bit
5075  *    op: 0 -> add  , 1 -> sub
5076  *     S: 1 -> set flags
5077  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
5078  *  imm6: Shift amount to apply to Rm before the add/sub
5079  */
5080 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
5081 {
5082     int rd = extract32(insn, 0, 5);
5083     int rn = extract32(insn, 5, 5);
5084     int imm6 = extract32(insn, 10, 6);
5085     int rm = extract32(insn, 16, 5);
5086     int shift_type = extract32(insn, 22, 2);
5087     bool setflags = extract32(insn, 29, 1);
5088     bool sub_op = extract32(insn, 30, 1);
5089     bool sf = extract32(insn, 31, 1);
5090 
5091     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5092     TCGv_i64 tcg_rn, tcg_rm;
5093     TCGv_i64 tcg_result;
5094 
5095     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
5096         unallocated_encoding(s);
5097         return;
5098     }
5099 
5100     tcg_rn = read_cpu_reg(s, rn, sf);
5101     tcg_rm = read_cpu_reg(s, rm, sf);
5102 
5103     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
5104 
5105     tcg_result = tcg_temp_new_i64();
5106 
5107     if (!setflags) {
5108         if (sub_op) {
5109             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
5110         } else {
5111             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
5112         }
5113     } else {
5114         if (sub_op) {
5115             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
5116         } else {
5117             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
5118         }
5119     }
5120 
5121     if (sf) {
5122         tcg_gen_mov_i64(tcg_rd, tcg_result);
5123     } else {
5124         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
5125     }
5126 }
5127 
5128 /* Data-processing (3 source)
5129  *
5130  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
5131  *  +--+------+-----------+------+------+----+------+------+------+
5132  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
5133  *  +--+------+-----------+------+------+----+------+------+------+
5134  */
5135 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
5136 {
5137     int rd = extract32(insn, 0, 5);
5138     int rn = extract32(insn, 5, 5);
5139     int ra = extract32(insn, 10, 5);
5140     int rm = extract32(insn, 16, 5);
5141     int op_id = (extract32(insn, 29, 3) << 4) |
5142         (extract32(insn, 21, 3) << 1) |
5143         extract32(insn, 15, 1);
5144     bool sf = extract32(insn, 31, 1);
5145     bool is_sub = extract32(op_id, 0, 1);
5146     bool is_high = extract32(op_id, 2, 1);
5147     bool is_signed = false;
5148     TCGv_i64 tcg_op1;
5149     TCGv_i64 tcg_op2;
5150     TCGv_i64 tcg_tmp;
5151 
5152     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5153     switch (op_id) {
5154     case 0x42: /* SMADDL */
5155     case 0x43: /* SMSUBL */
5156     case 0x44: /* SMULH */
5157         is_signed = true;
5158         break;
5159     case 0x0: /* MADD (32bit) */
5160     case 0x1: /* MSUB (32bit) */
5161     case 0x40: /* MADD (64bit) */
5162     case 0x41: /* MSUB (64bit) */
5163     case 0x4a: /* UMADDL */
5164     case 0x4b: /* UMSUBL */
5165     case 0x4c: /* UMULH */
5166         break;
5167     default:
5168         unallocated_encoding(s);
5169         return;
5170     }
5171 
5172     if (is_high) {
5173         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5174         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5175         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5176         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5177 
5178         if (is_signed) {
5179             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5180         } else {
5181             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5182         }
5183         return;
5184     }
5185 
5186     tcg_op1 = tcg_temp_new_i64();
5187     tcg_op2 = tcg_temp_new_i64();
5188     tcg_tmp = tcg_temp_new_i64();
5189 
5190     if (op_id < 0x42) {
5191         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5192         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5193     } else {
5194         if (is_signed) {
5195             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5196             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5197         } else {
5198             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5199             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5200         }
5201     }
5202 
5203     if (ra == 31 && !is_sub) {
5204         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5205         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5206     } else {
5207         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5208         if (is_sub) {
5209             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5210         } else {
5211             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5212         }
5213     }
5214 
5215     if (!sf) {
5216         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5217     }
5218 }
5219 
5220 /* Add/subtract (with carry)
5221  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5222  * +--+--+--+------------------------+------+-------------+------+-----+
5223  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5224  * +--+--+--+------------------------+------+-------------+------+-----+
5225  */
5226 
5227 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5228 {
5229     unsigned int sf, op, setflags, rm, rn, rd;
5230     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5231 
5232     sf = extract32(insn, 31, 1);
5233     op = extract32(insn, 30, 1);
5234     setflags = extract32(insn, 29, 1);
5235     rm = extract32(insn, 16, 5);
5236     rn = extract32(insn, 5, 5);
5237     rd = extract32(insn, 0, 5);
5238 
5239     tcg_rd = cpu_reg(s, rd);
5240     tcg_rn = cpu_reg(s, rn);
5241 
5242     if (op) {
5243         tcg_y = tcg_temp_new_i64();
5244         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5245     } else {
5246         tcg_y = cpu_reg(s, rm);
5247     }
5248 
5249     if (setflags) {
5250         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5251     } else {
5252         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5253     }
5254 }
5255 
5256 /*
5257  * Rotate right into flags
5258  *  31 30 29                21       15          10      5  4      0
5259  * +--+--+--+-----------------+--------+-----------+------+--+------+
5260  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5261  * +--+--+--+-----------------+--------+-----------+------+--+------+
5262  */
5263 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5264 {
5265     int mask = extract32(insn, 0, 4);
5266     int o2 = extract32(insn, 4, 1);
5267     int rn = extract32(insn, 5, 5);
5268     int imm6 = extract32(insn, 15, 6);
5269     int sf_op_s = extract32(insn, 29, 3);
5270     TCGv_i64 tcg_rn;
5271     TCGv_i32 nzcv;
5272 
5273     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5274         unallocated_encoding(s);
5275         return;
5276     }
5277 
5278     tcg_rn = read_cpu_reg(s, rn, 1);
5279     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5280 
5281     nzcv = tcg_temp_new_i32();
5282     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5283 
5284     if (mask & 8) { /* N */
5285         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5286     }
5287     if (mask & 4) { /* Z */
5288         tcg_gen_not_i32(cpu_ZF, nzcv);
5289         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5290     }
5291     if (mask & 2) { /* C */
5292         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5293     }
5294     if (mask & 1) { /* V */
5295         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5296     }
5297 }
5298 
5299 /*
5300  * Evaluate into flags
5301  *  31 30 29                21        15   14        10      5  4      0
5302  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5303  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5304  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5305  */
5306 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5307 {
5308     int o3_mask = extract32(insn, 0, 5);
5309     int rn = extract32(insn, 5, 5);
5310     int o2 = extract32(insn, 15, 6);
5311     int sz = extract32(insn, 14, 1);
5312     int sf_op_s = extract32(insn, 29, 3);
5313     TCGv_i32 tmp;
5314     int shift;
5315 
5316     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5317         !dc_isar_feature(aa64_condm_4, s)) {
5318         unallocated_encoding(s);
5319         return;
5320     }
5321     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5322 
5323     tmp = tcg_temp_new_i32();
5324     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5325     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5326     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5327     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5328     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5329 }
5330 
5331 /* Conditional compare (immediate / register)
5332  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5333  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5334  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5335  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5336  *        [1]                             y                [0]       [0]
5337  */
5338 static void disas_cc(DisasContext *s, uint32_t insn)
5339 {
5340     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5341     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5342     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5343     DisasCompare c;
5344 
5345     if (!extract32(insn, 29, 1)) {
5346         unallocated_encoding(s);
5347         return;
5348     }
5349     if (insn & (1 << 10 | 1 << 4)) {
5350         unallocated_encoding(s);
5351         return;
5352     }
5353     sf = extract32(insn, 31, 1);
5354     op = extract32(insn, 30, 1);
5355     is_imm = extract32(insn, 11, 1);
5356     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5357     cond = extract32(insn, 12, 4);
5358     rn = extract32(insn, 5, 5);
5359     nzcv = extract32(insn, 0, 4);
5360 
5361     /* Set T0 = !COND.  */
5362     tcg_t0 = tcg_temp_new_i32();
5363     arm_test_cc(&c, cond);
5364     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5365 
5366     /* Load the arguments for the new comparison.  */
5367     if (is_imm) {
5368         tcg_y = tcg_temp_new_i64();
5369         tcg_gen_movi_i64(tcg_y, y);
5370     } else {
5371         tcg_y = cpu_reg(s, y);
5372     }
5373     tcg_rn = cpu_reg(s, rn);
5374 
5375     /* Set the flags for the new comparison.  */
5376     tcg_tmp = tcg_temp_new_i64();
5377     if (op) {
5378         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5379     } else {
5380         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5381     }
5382 
5383     /* If COND was false, force the flags to #nzcv.  Compute two masks
5384      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5385      * For tcg hosts that support ANDC, we can make do with just T1.
5386      * In either case, allow the tcg optimizer to delete any unused mask.
5387      */
5388     tcg_t1 = tcg_temp_new_i32();
5389     tcg_t2 = tcg_temp_new_i32();
5390     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5391     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5392 
5393     if (nzcv & 8) { /* N */
5394         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5395     } else {
5396         if (TCG_TARGET_HAS_andc_i32) {
5397             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5398         } else {
5399             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5400         }
5401     }
5402     if (nzcv & 4) { /* Z */
5403         if (TCG_TARGET_HAS_andc_i32) {
5404             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5405         } else {
5406             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5407         }
5408     } else {
5409         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5410     }
5411     if (nzcv & 2) { /* C */
5412         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5413     } else {
5414         if (TCG_TARGET_HAS_andc_i32) {
5415             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5416         } else {
5417             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5418         }
5419     }
5420     if (nzcv & 1) { /* V */
5421         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5422     } else {
5423         if (TCG_TARGET_HAS_andc_i32) {
5424             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5425         } else {
5426             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5427         }
5428     }
5429 }
5430 
5431 /* Conditional select
5432  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5433  * +----+----+---+-----------------+------+------+-----+------+------+
5434  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5435  * +----+----+---+-----------------+------+------+-----+------+------+
5436  */
5437 static void disas_cond_select(DisasContext *s, uint32_t insn)
5438 {
5439     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5440     TCGv_i64 tcg_rd, zero;
5441     DisasCompare64 c;
5442 
5443     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5444         /* S == 1 or op2<1> == 1 */
5445         unallocated_encoding(s);
5446         return;
5447     }
5448     sf = extract32(insn, 31, 1);
5449     else_inv = extract32(insn, 30, 1);
5450     rm = extract32(insn, 16, 5);
5451     cond = extract32(insn, 12, 4);
5452     else_inc = extract32(insn, 10, 1);
5453     rn = extract32(insn, 5, 5);
5454     rd = extract32(insn, 0, 5);
5455 
5456     tcg_rd = cpu_reg(s, rd);
5457 
5458     a64_test_cc(&c, cond);
5459     zero = tcg_constant_i64(0);
5460 
5461     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5462         /* CSET & CSETM.  */
5463         if (else_inv) {
5464             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
5465                                    tcg_rd, c.value, zero);
5466         } else {
5467             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
5468                                 tcg_rd, c.value, zero);
5469         }
5470     } else {
5471         TCGv_i64 t_true = cpu_reg(s, rn);
5472         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5473         if (else_inv && else_inc) {
5474             tcg_gen_neg_i64(t_false, t_false);
5475         } else if (else_inv) {
5476             tcg_gen_not_i64(t_false, t_false);
5477         } else if (else_inc) {
5478             tcg_gen_addi_i64(t_false, t_false, 1);
5479         }
5480         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5481     }
5482 
5483     if (!sf) {
5484         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5485     }
5486 }
5487 
5488 static void handle_clz(DisasContext *s, unsigned int sf,
5489                        unsigned int rn, unsigned int rd)
5490 {
5491     TCGv_i64 tcg_rd, tcg_rn;
5492     tcg_rd = cpu_reg(s, rd);
5493     tcg_rn = cpu_reg(s, rn);
5494 
5495     if (sf) {
5496         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5497     } else {
5498         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5499         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5500         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5501         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5502     }
5503 }
5504 
5505 static void handle_cls(DisasContext *s, unsigned int sf,
5506                        unsigned int rn, unsigned int rd)
5507 {
5508     TCGv_i64 tcg_rd, tcg_rn;
5509     tcg_rd = cpu_reg(s, rd);
5510     tcg_rn = cpu_reg(s, rn);
5511 
5512     if (sf) {
5513         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5514     } else {
5515         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5516         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5517         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5518         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5519     }
5520 }
5521 
5522 static void handle_rbit(DisasContext *s, unsigned int sf,
5523                         unsigned int rn, unsigned int rd)
5524 {
5525     TCGv_i64 tcg_rd, tcg_rn;
5526     tcg_rd = cpu_reg(s, rd);
5527     tcg_rn = cpu_reg(s, rn);
5528 
5529     if (sf) {
5530         gen_helper_rbit64(tcg_rd, tcg_rn);
5531     } else {
5532         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5533         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5534         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5535         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5536     }
5537 }
5538 
5539 /* REV with sf==1, opcode==3 ("REV64") */
5540 static void handle_rev64(DisasContext *s, unsigned int sf,
5541                          unsigned int rn, unsigned int rd)
5542 {
5543     if (!sf) {
5544         unallocated_encoding(s);
5545         return;
5546     }
5547     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5548 }
5549 
5550 /* REV with sf==0, opcode==2
5551  * REV32 (sf==1, opcode==2)
5552  */
5553 static void handle_rev32(DisasContext *s, unsigned int sf,
5554                          unsigned int rn, unsigned int rd)
5555 {
5556     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5557     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5558 
5559     if (sf) {
5560         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5561         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5562     } else {
5563         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5564     }
5565 }
5566 
5567 /* REV16 (opcode==1) */
5568 static void handle_rev16(DisasContext *s, unsigned int sf,
5569                          unsigned int rn, unsigned int rd)
5570 {
5571     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5572     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5573     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5574     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5575 
5576     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5577     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5578     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5579     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5580     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5581 }
5582 
5583 /* Data-processing (1 source)
5584  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5585  * +----+---+---+-----------------+---------+--------+------+------+
5586  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5587  * +----+---+---+-----------------+---------+--------+------+------+
5588  */
5589 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5590 {
5591     unsigned int sf, opcode, opcode2, rn, rd;
5592     TCGv_i64 tcg_rd;
5593 
5594     if (extract32(insn, 29, 1)) {
5595         unallocated_encoding(s);
5596         return;
5597     }
5598 
5599     sf = extract32(insn, 31, 1);
5600     opcode = extract32(insn, 10, 6);
5601     opcode2 = extract32(insn, 16, 5);
5602     rn = extract32(insn, 5, 5);
5603     rd = extract32(insn, 0, 5);
5604 
5605 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5606 
5607     switch (MAP(sf, opcode2, opcode)) {
5608     case MAP(0, 0x00, 0x00): /* RBIT */
5609     case MAP(1, 0x00, 0x00):
5610         handle_rbit(s, sf, rn, rd);
5611         break;
5612     case MAP(0, 0x00, 0x01): /* REV16 */
5613     case MAP(1, 0x00, 0x01):
5614         handle_rev16(s, sf, rn, rd);
5615         break;
5616     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5617     case MAP(1, 0x00, 0x02):
5618         handle_rev32(s, sf, rn, rd);
5619         break;
5620     case MAP(1, 0x00, 0x03): /* REV64 */
5621         handle_rev64(s, sf, rn, rd);
5622         break;
5623     case MAP(0, 0x00, 0x04): /* CLZ */
5624     case MAP(1, 0x00, 0x04):
5625         handle_clz(s, sf, rn, rd);
5626         break;
5627     case MAP(0, 0x00, 0x05): /* CLS */
5628     case MAP(1, 0x00, 0x05):
5629         handle_cls(s, sf, rn, rd);
5630         break;
5631     case MAP(1, 0x01, 0x00): /* PACIA */
5632         if (s->pauth_active) {
5633             tcg_rd = cpu_reg(s, rd);
5634             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5635         } else if (!dc_isar_feature(aa64_pauth, s)) {
5636             goto do_unallocated;
5637         }
5638         break;
5639     case MAP(1, 0x01, 0x01): /* PACIB */
5640         if (s->pauth_active) {
5641             tcg_rd = cpu_reg(s, rd);
5642             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5643         } else if (!dc_isar_feature(aa64_pauth, s)) {
5644             goto do_unallocated;
5645         }
5646         break;
5647     case MAP(1, 0x01, 0x02): /* PACDA */
5648         if (s->pauth_active) {
5649             tcg_rd = cpu_reg(s, rd);
5650             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5651         } else if (!dc_isar_feature(aa64_pauth, s)) {
5652             goto do_unallocated;
5653         }
5654         break;
5655     case MAP(1, 0x01, 0x03): /* PACDB */
5656         if (s->pauth_active) {
5657             tcg_rd = cpu_reg(s, rd);
5658             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5659         } else if (!dc_isar_feature(aa64_pauth, s)) {
5660             goto do_unallocated;
5661         }
5662         break;
5663     case MAP(1, 0x01, 0x04): /* AUTIA */
5664         if (s->pauth_active) {
5665             tcg_rd = cpu_reg(s, rd);
5666             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5667         } else if (!dc_isar_feature(aa64_pauth, s)) {
5668             goto do_unallocated;
5669         }
5670         break;
5671     case MAP(1, 0x01, 0x05): /* AUTIB */
5672         if (s->pauth_active) {
5673             tcg_rd = cpu_reg(s, rd);
5674             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5675         } else if (!dc_isar_feature(aa64_pauth, s)) {
5676             goto do_unallocated;
5677         }
5678         break;
5679     case MAP(1, 0x01, 0x06): /* AUTDA */
5680         if (s->pauth_active) {
5681             tcg_rd = cpu_reg(s, rd);
5682             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5683         } else if (!dc_isar_feature(aa64_pauth, s)) {
5684             goto do_unallocated;
5685         }
5686         break;
5687     case MAP(1, 0x01, 0x07): /* AUTDB */
5688         if (s->pauth_active) {
5689             tcg_rd = cpu_reg(s, rd);
5690             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5691         } else if (!dc_isar_feature(aa64_pauth, s)) {
5692             goto do_unallocated;
5693         }
5694         break;
5695     case MAP(1, 0x01, 0x08): /* PACIZA */
5696         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5697             goto do_unallocated;
5698         } else if (s->pauth_active) {
5699             tcg_rd = cpu_reg(s, rd);
5700             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5701         }
5702         break;
5703     case MAP(1, 0x01, 0x09): /* PACIZB */
5704         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5705             goto do_unallocated;
5706         } else if (s->pauth_active) {
5707             tcg_rd = cpu_reg(s, rd);
5708             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5709         }
5710         break;
5711     case MAP(1, 0x01, 0x0a): /* PACDZA */
5712         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5713             goto do_unallocated;
5714         } else if (s->pauth_active) {
5715             tcg_rd = cpu_reg(s, rd);
5716             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5717         }
5718         break;
5719     case MAP(1, 0x01, 0x0b): /* PACDZB */
5720         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5721             goto do_unallocated;
5722         } else if (s->pauth_active) {
5723             tcg_rd = cpu_reg(s, rd);
5724             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5725         }
5726         break;
5727     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5728         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5729             goto do_unallocated;
5730         } else if (s->pauth_active) {
5731             tcg_rd = cpu_reg(s, rd);
5732             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5733         }
5734         break;
5735     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5736         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5737             goto do_unallocated;
5738         } else if (s->pauth_active) {
5739             tcg_rd = cpu_reg(s, rd);
5740             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5741         }
5742         break;
5743     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5744         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5745             goto do_unallocated;
5746         } else if (s->pauth_active) {
5747             tcg_rd = cpu_reg(s, rd);
5748             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5749         }
5750         break;
5751     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5752         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5753             goto do_unallocated;
5754         } else if (s->pauth_active) {
5755             tcg_rd = cpu_reg(s, rd);
5756             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5757         }
5758         break;
5759     case MAP(1, 0x01, 0x10): /* XPACI */
5760         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5761             goto do_unallocated;
5762         } else if (s->pauth_active) {
5763             tcg_rd = cpu_reg(s, rd);
5764             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
5765         }
5766         break;
5767     case MAP(1, 0x01, 0x11): /* XPACD */
5768         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5769             goto do_unallocated;
5770         } else if (s->pauth_active) {
5771             tcg_rd = cpu_reg(s, rd);
5772             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
5773         }
5774         break;
5775     default:
5776     do_unallocated:
5777         unallocated_encoding(s);
5778         break;
5779     }
5780 
5781 #undef MAP
5782 }
5783 
5784 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5785                        unsigned int rm, unsigned int rn, unsigned int rd)
5786 {
5787     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5788     tcg_rd = cpu_reg(s, rd);
5789 
5790     if (!sf && is_signed) {
5791         tcg_n = tcg_temp_new_i64();
5792         tcg_m = tcg_temp_new_i64();
5793         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5794         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5795     } else {
5796         tcg_n = read_cpu_reg(s, rn, sf);
5797         tcg_m = read_cpu_reg(s, rm, sf);
5798     }
5799 
5800     if (is_signed) {
5801         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5802     } else {
5803         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5804     }
5805 
5806     if (!sf) { /* zero extend final result */
5807         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5808     }
5809 }
5810 
5811 /* LSLV, LSRV, ASRV, RORV */
5812 static void handle_shift_reg(DisasContext *s,
5813                              enum a64_shift_type shift_type, unsigned int sf,
5814                              unsigned int rm, unsigned int rn, unsigned int rd)
5815 {
5816     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5817     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5818     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5819 
5820     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5821     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5822 }
5823 
5824 /* CRC32[BHWX], CRC32C[BHWX] */
5825 static void handle_crc32(DisasContext *s,
5826                          unsigned int sf, unsigned int sz, bool crc32c,
5827                          unsigned int rm, unsigned int rn, unsigned int rd)
5828 {
5829     TCGv_i64 tcg_acc, tcg_val;
5830     TCGv_i32 tcg_bytes;
5831 
5832     if (!dc_isar_feature(aa64_crc32, s)
5833         || (sf == 1 && sz != 3)
5834         || (sf == 0 && sz == 3)) {
5835         unallocated_encoding(s);
5836         return;
5837     }
5838 
5839     if (sz == 3) {
5840         tcg_val = cpu_reg(s, rm);
5841     } else {
5842         uint64_t mask;
5843         switch (sz) {
5844         case 0:
5845             mask = 0xFF;
5846             break;
5847         case 1:
5848             mask = 0xFFFF;
5849             break;
5850         case 2:
5851             mask = 0xFFFFFFFF;
5852             break;
5853         default:
5854             g_assert_not_reached();
5855         }
5856         tcg_val = tcg_temp_new_i64();
5857         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5858     }
5859 
5860     tcg_acc = cpu_reg(s, rn);
5861     tcg_bytes = tcg_constant_i32(1 << sz);
5862 
5863     if (crc32c) {
5864         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5865     } else {
5866         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5867     }
5868 }
5869 
5870 /* Data-processing (2 source)
5871  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5872  * +----+---+---+-----------------+------+--------+------+------+
5873  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5874  * +----+---+---+-----------------+------+--------+------+------+
5875  */
5876 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5877 {
5878     unsigned int sf, rm, opcode, rn, rd, setflag;
5879     sf = extract32(insn, 31, 1);
5880     setflag = extract32(insn, 29, 1);
5881     rm = extract32(insn, 16, 5);
5882     opcode = extract32(insn, 10, 6);
5883     rn = extract32(insn, 5, 5);
5884     rd = extract32(insn, 0, 5);
5885 
5886     if (setflag && opcode != 0) {
5887         unallocated_encoding(s);
5888         return;
5889     }
5890 
5891     switch (opcode) {
5892     case 0: /* SUBP(S) */
5893         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5894             goto do_unallocated;
5895         } else {
5896             TCGv_i64 tcg_n, tcg_m, tcg_d;
5897 
5898             tcg_n = read_cpu_reg_sp(s, rn, true);
5899             tcg_m = read_cpu_reg_sp(s, rm, true);
5900             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5901             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5902             tcg_d = cpu_reg(s, rd);
5903 
5904             if (setflag) {
5905                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5906             } else {
5907                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5908             }
5909         }
5910         break;
5911     case 2: /* UDIV */
5912         handle_div(s, false, sf, rm, rn, rd);
5913         break;
5914     case 3: /* SDIV */
5915         handle_div(s, true, sf, rm, rn, rd);
5916         break;
5917     case 4: /* IRG */
5918         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5919             goto do_unallocated;
5920         }
5921         if (s->ata[0]) {
5922             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
5923                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5924         } else {
5925             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5926                                              cpu_reg_sp(s, rn));
5927         }
5928         break;
5929     case 5: /* GMI */
5930         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5931             goto do_unallocated;
5932         } else {
5933             TCGv_i64 t = tcg_temp_new_i64();
5934 
5935             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5936             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5937             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5938         }
5939         break;
5940     case 8: /* LSLV */
5941         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5942         break;
5943     case 9: /* LSRV */
5944         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5945         break;
5946     case 10: /* ASRV */
5947         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5948         break;
5949     case 11: /* RORV */
5950         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5951         break;
5952     case 12: /* PACGA */
5953         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5954             goto do_unallocated;
5955         }
5956         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
5957                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5958         break;
5959     case 16:
5960     case 17:
5961     case 18:
5962     case 19:
5963     case 20:
5964     case 21:
5965     case 22:
5966     case 23: /* CRC32 */
5967     {
5968         int sz = extract32(opcode, 0, 2);
5969         bool crc32c = extract32(opcode, 2, 1);
5970         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5971         break;
5972     }
5973     default:
5974     do_unallocated:
5975         unallocated_encoding(s);
5976         break;
5977     }
5978 }
5979 
5980 /*
5981  * Data processing - register
5982  *  31  30 29  28      25    21  20  16      10         0
5983  * +--+---+--+---+-------+-----+-------+-------+---------+
5984  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5985  * +--+---+--+---+-------+-----+-------+-------+---------+
5986  */
5987 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5988 {
5989     int op0 = extract32(insn, 30, 1);
5990     int op1 = extract32(insn, 28, 1);
5991     int op2 = extract32(insn, 21, 4);
5992     int op3 = extract32(insn, 10, 6);
5993 
5994     if (!op1) {
5995         if (op2 & 8) {
5996             if (op2 & 1) {
5997                 /* Add/sub (extended register) */
5998                 disas_add_sub_ext_reg(s, insn);
5999             } else {
6000                 /* Add/sub (shifted register) */
6001                 disas_add_sub_reg(s, insn);
6002             }
6003         } else {
6004             /* Logical (shifted register) */
6005             disas_logic_reg(s, insn);
6006         }
6007         return;
6008     }
6009 
6010     switch (op2) {
6011     case 0x0:
6012         switch (op3) {
6013         case 0x00: /* Add/subtract (with carry) */
6014             disas_adc_sbc(s, insn);
6015             break;
6016 
6017         case 0x01: /* Rotate right into flags */
6018         case 0x21:
6019             disas_rotate_right_into_flags(s, insn);
6020             break;
6021 
6022         case 0x02: /* Evaluate into flags */
6023         case 0x12:
6024         case 0x22:
6025         case 0x32:
6026             disas_evaluate_into_flags(s, insn);
6027             break;
6028 
6029         default:
6030             goto do_unallocated;
6031         }
6032         break;
6033 
6034     case 0x2: /* Conditional compare */
6035         disas_cc(s, insn); /* both imm and reg forms */
6036         break;
6037 
6038     case 0x4: /* Conditional select */
6039         disas_cond_select(s, insn);
6040         break;
6041 
6042     case 0x6: /* Data-processing */
6043         if (op0) {    /* (1 source) */
6044             disas_data_proc_1src(s, insn);
6045         } else {      /* (2 source) */
6046             disas_data_proc_2src(s, insn);
6047         }
6048         break;
6049     case 0x8 ... 0xf: /* (3 source) */
6050         disas_data_proc_3src(s, insn);
6051         break;
6052 
6053     default:
6054     do_unallocated:
6055         unallocated_encoding(s);
6056         break;
6057     }
6058 }
6059 
6060 static void handle_fp_compare(DisasContext *s, int size,
6061                               unsigned int rn, unsigned int rm,
6062                               bool cmp_with_zero, bool signal_all_nans)
6063 {
6064     TCGv_i64 tcg_flags = tcg_temp_new_i64();
6065     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
6066 
6067     if (size == MO_64) {
6068         TCGv_i64 tcg_vn, tcg_vm;
6069 
6070         tcg_vn = read_fp_dreg(s, rn);
6071         if (cmp_with_zero) {
6072             tcg_vm = tcg_constant_i64(0);
6073         } else {
6074             tcg_vm = read_fp_dreg(s, rm);
6075         }
6076         if (signal_all_nans) {
6077             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6078         } else {
6079             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6080         }
6081     } else {
6082         TCGv_i32 tcg_vn = tcg_temp_new_i32();
6083         TCGv_i32 tcg_vm = tcg_temp_new_i32();
6084 
6085         read_vec_element_i32(s, tcg_vn, rn, 0, size);
6086         if (cmp_with_zero) {
6087             tcg_gen_movi_i32(tcg_vm, 0);
6088         } else {
6089             read_vec_element_i32(s, tcg_vm, rm, 0, size);
6090         }
6091 
6092         switch (size) {
6093         case MO_32:
6094             if (signal_all_nans) {
6095                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6096             } else {
6097                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6098             }
6099             break;
6100         case MO_16:
6101             if (signal_all_nans) {
6102                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6103             } else {
6104                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6105             }
6106             break;
6107         default:
6108             g_assert_not_reached();
6109         }
6110     }
6111 
6112     gen_set_nzcv(tcg_flags);
6113 }
6114 
6115 /* Floating point compare
6116  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
6117  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6118  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
6119  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6120  */
6121 static void disas_fp_compare(DisasContext *s, uint32_t insn)
6122 {
6123     unsigned int mos, type, rm, op, rn, opc, op2r;
6124     int size;
6125 
6126     mos = extract32(insn, 29, 3);
6127     type = extract32(insn, 22, 2);
6128     rm = extract32(insn, 16, 5);
6129     op = extract32(insn, 14, 2);
6130     rn = extract32(insn, 5, 5);
6131     opc = extract32(insn, 3, 2);
6132     op2r = extract32(insn, 0, 3);
6133 
6134     if (mos || op || op2r) {
6135         unallocated_encoding(s);
6136         return;
6137     }
6138 
6139     switch (type) {
6140     case 0:
6141         size = MO_32;
6142         break;
6143     case 1:
6144         size = MO_64;
6145         break;
6146     case 3:
6147         size = MO_16;
6148         if (dc_isar_feature(aa64_fp16, s)) {
6149             break;
6150         }
6151         /* fallthru */
6152     default:
6153         unallocated_encoding(s);
6154         return;
6155     }
6156 
6157     if (!fp_access_check(s)) {
6158         return;
6159     }
6160 
6161     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6162 }
6163 
6164 /* Floating point conditional compare
6165  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6166  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6167  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6168  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6169  */
6170 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6171 {
6172     unsigned int mos, type, rm, cond, rn, op, nzcv;
6173     TCGLabel *label_continue = NULL;
6174     int size;
6175 
6176     mos = extract32(insn, 29, 3);
6177     type = extract32(insn, 22, 2);
6178     rm = extract32(insn, 16, 5);
6179     cond = extract32(insn, 12, 4);
6180     rn = extract32(insn, 5, 5);
6181     op = extract32(insn, 4, 1);
6182     nzcv = extract32(insn, 0, 4);
6183 
6184     if (mos) {
6185         unallocated_encoding(s);
6186         return;
6187     }
6188 
6189     switch (type) {
6190     case 0:
6191         size = MO_32;
6192         break;
6193     case 1:
6194         size = MO_64;
6195         break;
6196     case 3:
6197         size = MO_16;
6198         if (dc_isar_feature(aa64_fp16, s)) {
6199             break;
6200         }
6201         /* fallthru */
6202     default:
6203         unallocated_encoding(s);
6204         return;
6205     }
6206 
6207     if (!fp_access_check(s)) {
6208         return;
6209     }
6210 
6211     if (cond < 0x0e) { /* not always */
6212         TCGLabel *label_match = gen_new_label();
6213         label_continue = gen_new_label();
6214         arm_gen_test_cc(cond, label_match);
6215         /* nomatch: */
6216         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6217         tcg_gen_br(label_continue);
6218         gen_set_label(label_match);
6219     }
6220 
6221     handle_fp_compare(s, size, rn, rm, false, op);
6222 
6223     if (cond < 0x0e) {
6224         gen_set_label(label_continue);
6225     }
6226 }
6227 
6228 /* Floating point conditional select
6229  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6230  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6231  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6232  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6233  */
6234 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6235 {
6236     unsigned int mos, type, rm, cond, rn, rd;
6237     TCGv_i64 t_true, t_false;
6238     DisasCompare64 c;
6239     MemOp sz;
6240 
6241     mos = extract32(insn, 29, 3);
6242     type = extract32(insn, 22, 2);
6243     rm = extract32(insn, 16, 5);
6244     cond = extract32(insn, 12, 4);
6245     rn = extract32(insn, 5, 5);
6246     rd = extract32(insn, 0, 5);
6247 
6248     if (mos) {
6249         unallocated_encoding(s);
6250         return;
6251     }
6252 
6253     switch (type) {
6254     case 0:
6255         sz = MO_32;
6256         break;
6257     case 1:
6258         sz = MO_64;
6259         break;
6260     case 3:
6261         sz = MO_16;
6262         if (dc_isar_feature(aa64_fp16, s)) {
6263             break;
6264         }
6265         /* fallthru */
6266     default:
6267         unallocated_encoding(s);
6268         return;
6269     }
6270 
6271     if (!fp_access_check(s)) {
6272         return;
6273     }
6274 
6275     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6276     t_true = tcg_temp_new_i64();
6277     t_false = tcg_temp_new_i64();
6278     read_vec_element(s, t_true, rn, 0, sz);
6279     read_vec_element(s, t_false, rm, 0, sz);
6280 
6281     a64_test_cc(&c, cond);
6282     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6283                         t_true, t_false);
6284 
6285     /* Note that sregs & hregs write back zeros to the high bits,
6286        and we've already done the zero-extension.  */
6287     write_fp_dreg(s, rd, t_true);
6288 }
6289 
6290 /* Floating-point data-processing (1 source) - half precision */
6291 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6292 {
6293     TCGv_ptr fpst = NULL;
6294     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6295     TCGv_i32 tcg_res = tcg_temp_new_i32();
6296 
6297     switch (opcode) {
6298     case 0x0: /* FMOV */
6299         tcg_gen_mov_i32(tcg_res, tcg_op);
6300         break;
6301     case 0x1: /* FABS */
6302         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6303         break;
6304     case 0x2: /* FNEG */
6305         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6306         break;
6307     case 0x3: /* FSQRT */
6308         fpst = fpstatus_ptr(FPST_FPCR_F16);
6309         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6310         break;
6311     case 0x8: /* FRINTN */
6312     case 0x9: /* FRINTP */
6313     case 0xa: /* FRINTM */
6314     case 0xb: /* FRINTZ */
6315     case 0xc: /* FRINTA */
6316     {
6317         TCGv_i32 tcg_rmode;
6318 
6319         fpst = fpstatus_ptr(FPST_FPCR_F16);
6320         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6321         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6322         gen_restore_rmode(tcg_rmode, fpst);
6323         break;
6324     }
6325     case 0xe: /* FRINTX */
6326         fpst = fpstatus_ptr(FPST_FPCR_F16);
6327         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6328         break;
6329     case 0xf: /* FRINTI */
6330         fpst = fpstatus_ptr(FPST_FPCR_F16);
6331         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6332         break;
6333     default:
6334         g_assert_not_reached();
6335     }
6336 
6337     write_fp_sreg(s, rd, tcg_res);
6338 }
6339 
6340 /* Floating-point data-processing (1 source) - single precision */
6341 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6342 {
6343     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6344     TCGv_i32 tcg_op, tcg_res;
6345     TCGv_ptr fpst;
6346     int rmode = -1;
6347 
6348     tcg_op = read_fp_sreg(s, rn);
6349     tcg_res = tcg_temp_new_i32();
6350 
6351     switch (opcode) {
6352     case 0x0: /* FMOV */
6353         tcg_gen_mov_i32(tcg_res, tcg_op);
6354         goto done;
6355     case 0x1: /* FABS */
6356         gen_helper_vfp_abss(tcg_res, tcg_op);
6357         goto done;
6358     case 0x2: /* FNEG */
6359         gen_helper_vfp_negs(tcg_res, tcg_op);
6360         goto done;
6361     case 0x3: /* FSQRT */
6362         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
6363         goto done;
6364     case 0x6: /* BFCVT */
6365         gen_fpst = gen_helper_bfcvt;
6366         break;
6367     case 0x8: /* FRINTN */
6368     case 0x9: /* FRINTP */
6369     case 0xa: /* FRINTM */
6370     case 0xb: /* FRINTZ */
6371     case 0xc: /* FRINTA */
6372         rmode = opcode & 7;
6373         gen_fpst = gen_helper_rints;
6374         break;
6375     case 0xe: /* FRINTX */
6376         gen_fpst = gen_helper_rints_exact;
6377         break;
6378     case 0xf: /* FRINTI */
6379         gen_fpst = gen_helper_rints;
6380         break;
6381     case 0x10: /* FRINT32Z */
6382         rmode = FPROUNDING_ZERO;
6383         gen_fpst = gen_helper_frint32_s;
6384         break;
6385     case 0x11: /* FRINT32X */
6386         gen_fpst = gen_helper_frint32_s;
6387         break;
6388     case 0x12: /* FRINT64Z */
6389         rmode = FPROUNDING_ZERO;
6390         gen_fpst = gen_helper_frint64_s;
6391         break;
6392     case 0x13: /* FRINT64X */
6393         gen_fpst = gen_helper_frint64_s;
6394         break;
6395     default:
6396         g_assert_not_reached();
6397     }
6398 
6399     fpst = fpstatus_ptr(FPST_FPCR);
6400     if (rmode >= 0) {
6401         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6402         gen_fpst(tcg_res, tcg_op, fpst);
6403         gen_restore_rmode(tcg_rmode, fpst);
6404     } else {
6405         gen_fpst(tcg_res, tcg_op, fpst);
6406     }
6407 
6408  done:
6409     write_fp_sreg(s, rd, tcg_res);
6410 }
6411 
6412 /* Floating-point data-processing (1 source) - double precision */
6413 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6414 {
6415     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6416     TCGv_i64 tcg_op, tcg_res;
6417     TCGv_ptr fpst;
6418     int rmode = -1;
6419 
6420     switch (opcode) {
6421     case 0x0: /* FMOV */
6422         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6423         return;
6424     }
6425 
6426     tcg_op = read_fp_dreg(s, rn);
6427     tcg_res = tcg_temp_new_i64();
6428 
6429     switch (opcode) {
6430     case 0x1: /* FABS */
6431         gen_helper_vfp_absd(tcg_res, tcg_op);
6432         goto done;
6433     case 0x2: /* FNEG */
6434         gen_helper_vfp_negd(tcg_res, tcg_op);
6435         goto done;
6436     case 0x3: /* FSQRT */
6437         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
6438         goto done;
6439     case 0x8: /* FRINTN */
6440     case 0x9: /* FRINTP */
6441     case 0xa: /* FRINTM */
6442     case 0xb: /* FRINTZ */
6443     case 0xc: /* FRINTA */
6444         rmode = opcode & 7;
6445         gen_fpst = gen_helper_rintd;
6446         break;
6447     case 0xe: /* FRINTX */
6448         gen_fpst = gen_helper_rintd_exact;
6449         break;
6450     case 0xf: /* FRINTI */
6451         gen_fpst = gen_helper_rintd;
6452         break;
6453     case 0x10: /* FRINT32Z */
6454         rmode = FPROUNDING_ZERO;
6455         gen_fpst = gen_helper_frint32_d;
6456         break;
6457     case 0x11: /* FRINT32X */
6458         gen_fpst = gen_helper_frint32_d;
6459         break;
6460     case 0x12: /* FRINT64Z */
6461         rmode = FPROUNDING_ZERO;
6462         gen_fpst = gen_helper_frint64_d;
6463         break;
6464     case 0x13: /* FRINT64X */
6465         gen_fpst = gen_helper_frint64_d;
6466         break;
6467     default:
6468         g_assert_not_reached();
6469     }
6470 
6471     fpst = fpstatus_ptr(FPST_FPCR);
6472     if (rmode >= 0) {
6473         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6474         gen_fpst(tcg_res, tcg_op, fpst);
6475         gen_restore_rmode(tcg_rmode, fpst);
6476     } else {
6477         gen_fpst(tcg_res, tcg_op, fpst);
6478     }
6479 
6480  done:
6481     write_fp_dreg(s, rd, tcg_res);
6482 }
6483 
6484 static void handle_fp_fcvt(DisasContext *s, int opcode,
6485                            int rd, int rn, int dtype, int ntype)
6486 {
6487     switch (ntype) {
6488     case 0x0:
6489     {
6490         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6491         if (dtype == 1) {
6492             /* Single to double */
6493             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6494             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
6495             write_fp_dreg(s, rd, tcg_rd);
6496         } else {
6497             /* Single to half */
6498             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6499             TCGv_i32 ahp = get_ahp_flag();
6500             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6501 
6502             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6503             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6504             write_fp_sreg(s, rd, tcg_rd);
6505         }
6506         break;
6507     }
6508     case 0x1:
6509     {
6510         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6511         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6512         if (dtype == 0) {
6513             /* Double to single */
6514             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
6515         } else {
6516             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6517             TCGv_i32 ahp = get_ahp_flag();
6518             /* Double to half */
6519             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6520             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6521         }
6522         write_fp_sreg(s, rd, tcg_rd);
6523         break;
6524     }
6525     case 0x3:
6526     {
6527         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6528         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6529         TCGv_i32 tcg_ahp = get_ahp_flag();
6530         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6531         if (dtype == 0) {
6532             /* Half to single */
6533             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6534             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6535             write_fp_sreg(s, rd, tcg_rd);
6536         } else {
6537             /* Half to double */
6538             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6539             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6540             write_fp_dreg(s, rd, tcg_rd);
6541         }
6542         break;
6543     }
6544     default:
6545         g_assert_not_reached();
6546     }
6547 }
6548 
6549 /* Floating point data-processing (1 source)
6550  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6551  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6552  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6553  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6554  */
6555 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6556 {
6557     int mos = extract32(insn, 29, 3);
6558     int type = extract32(insn, 22, 2);
6559     int opcode = extract32(insn, 15, 6);
6560     int rn = extract32(insn, 5, 5);
6561     int rd = extract32(insn, 0, 5);
6562 
6563     if (mos) {
6564         goto do_unallocated;
6565     }
6566 
6567     switch (opcode) {
6568     case 0x4: case 0x5: case 0x7:
6569     {
6570         /* FCVT between half, single and double precision */
6571         int dtype = extract32(opcode, 0, 2);
6572         if (type == 2 || dtype == type) {
6573             goto do_unallocated;
6574         }
6575         if (!fp_access_check(s)) {
6576             return;
6577         }
6578 
6579         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6580         break;
6581     }
6582 
6583     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6584         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6585             goto do_unallocated;
6586         }
6587         /* fall through */
6588     case 0x0 ... 0x3:
6589     case 0x8 ... 0xc:
6590     case 0xe ... 0xf:
6591         /* 32-to-32 and 64-to-64 ops */
6592         switch (type) {
6593         case 0:
6594             if (!fp_access_check(s)) {
6595                 return;
6596             }
6597             handle_fp_1src_single(s, opcode, rd, rn);
6598             break;
6599         case 1:
6600             if (!fp_access_check(s)) {
6601                 return;
6602             }
6603             handle_fp_1src_double(s, opcode, rd, rn);
6604             break;
6605         case 3:
6606             if (!dc_isar_feature(aa64_fp16, s)) {
6607                 goto do_unallocated;
6608             }
6609 
6610             if (!fp_access_check(s)) {
6611                 return;
6612             }
6613             handle_fp_1src_half(s, opcode, rd, rn);
6614             break;
6615         default:
6616             goto do_unallocated;
6617         }
6618         break;
6619 
6620     case 0x6:
6621         switch (type) {
6622         case 1: /* BFCVT */
6623             if (!dc_isar_feature(aa64_bf16, s)) {
6624                 goto do_unallocated;
6625             }
6626             if (!fp_access_check(s)) {
6627                 return;
6628             }
6629             handle_fp_1src_single(s, opcode, rd, rn);
6630             break;
6631         default:
6632             goto do_unallocated;
6633         }
6634         break;
6635 
6636     default:
6637     do_unallocated:
6638         unallocated_encoding(s);
6639         break;
6640     }
6641 }
6642 
6643 /* Floating-point data-processing (2 source) - single precision */
6644 static void handle_fp_2src_single(DisasContext *s, int opcode,
6645                                   int rd, int rn, int rm)
6646 {
6647     TCGv_i32 tcg_op1;
6648     TCGv_i32 tcg_op2;
6649     TCGv_i32 tcg_res;
6650     TCGv_ptr fpst;
6651 
6652     tcg_res = tcg_temp_new_i32();
6653     fpst = fpstatus_ptr(FPST_FPCR);
6654     tcg_op1 = read_fp_sreg(s, rn);
6655     tcg_op2 = read_fp_sreg(s, rm);
6656 
6657     switch (opcode) {
6658     case 0x0: /* FMUL */
6659         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6660         break;
6661     case 0x1: /* FDIV */
6662         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6663         break;
6664     case 0x2: /* FADD */
6665         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6666         break;
6667     case 0x3: /* FSUB */
6668         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6669         break;
6670     case 0x4: /* FMAX */
6671         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6672         break;
6673     case 0x5: /* FMIN */
6674         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6675         break;
6676     case 0x6: /* FMAXNM */
6677         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6678         break;
6679     case 0x7: /* FMINNM */
6680         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6681         break;
6682     case 0x8: /* FNMUL */
6683         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6684         gen_helper_vfp_negs(tcg_res, tcg_res);
6685         break;
6686     }
6687 
6688     write_fp_sreg(s, rd, tcg_res);
6689 }
6690 
6691 /* Floating-point data-processing (2 source) - double precision */
6692 static void handle_fp_2src_double(DisasContext *s, int opcode,
6693                                   int rd, int rn, int rm)
6694 {
6695     TCGv_i64 tcg_op1;
6696     TCGv_i64 tcg_op2;
6697     TCGv_i64 tcg_res;
6698     TCGv_ptr fpst;
6699 
6700     tcg_res = tcg_temp_new_i64();
6701     fpst = fpstatus_ptr(FPST_FPCR);
6702     tcg_op1 = read_fp_dreg(s, rn);
6703     tcg_op2 = read_fp_dreg(s, rm);
6704 
6705     switch (opcode) {
6706     case 0x0: /* FMUL */
6707         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6708         break;
6709     case 0x1: /* FDIV */
6710         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6711         break;
6712     case 0x2: /* FADD */
6713         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6714         break;
6715     case 0x3: /* FSUB */
6716         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6717         break;
6718     case 0x4: /* FMAX */
6719         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6720         break;
6721     case 0x5: /* FMIN */
6722         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6723         break;
6724     case 0x6: /* FMAXNM */
6725         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6726         break;
6727     case 0x7: /* FMINNM */
6728         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6729         break;
6730     case 0x8: /* FNMUL */
6731         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6732         gen_helper_vfp_negd(tcg_res, tcg_res);
6733         break;
6734     }
6735 
6736     write_fp_dreg(s, rd, tcg_res);
6737 }
6738 
6739 /* Floating-point data-processing (2 source) - half precision */
6740 static void handle_fp_2src_half(DisasContext *s, int opcode,
6741                                 int rd, int rn, int rm)
6742 {
6743     TCGv_i32 tcg_op1;
6744     TCGv_i32 tcg_op2;
6745     TCGv_i32 tcg_res;
6746     TCGv_ptr fpst;
6747 
6748     tcg_res = tcg_temp_new_i32();
6749     fpst = fpstatus_ptr(FPST_FPCR_F16);
6750     tcg_op1 = read_fp_hreg(s, rn);
6751     tcg_op2 = read_fp_hreg(s, rm);
6752 
6753     switch (opcode) {
6754     case 0x0: /* FMUL */
6755         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6756         break;
6757     case 0x1: /* FDIV */
6758         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6759         break;
6760     case 0x2: /* FADD */
6761         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6762         break;
6763     case 0x3: /* FSUB */
6764         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6765         break;
6766     case 0x4: /* FMAX */
6767         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6768         break;
6769     case 0x5: /* FMIN */
6770         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6771         break;
6772     case 0x6: /* FMAXNM */
6773         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6774         break;
6775     case 0x7: /* FMINNM */
6776         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6777         break;
6778     case 0x8: /* FNMUL */
6779         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6780         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6781         break;
6782     default:
6783         g_assert_not_reached();
6784     }
6785 
6786     write_fp_sreg(s, rd, tcg_res);
6787 }
6788 
6789 /* Floating point data-processing (2 source)
6790  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6791  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6792  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6793  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6794  */
6795 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6796 {
6797     int mos = extract32(insn, 29, 3);
6798     int type = extract32(insn, 22, 2);
6799     int rd = extract32(insn, 0, 5);
6800     int rn = extract32(insn, 5, 5);
6801     int rm = extract32(insn, 16, 5);
6802     int opcode = extract32(insn, 12, 4);
6803 
6804     if (opcode > 8 || mos) {
6805         unallocated_encoding(s);
6806         return;
6807     }
6808 
6809     switch (type) {
6810     case 0:
6811         if (!fp_access_check(s)) {
6812             return;
6813         }
6814         handle_fp_2src_single(s, opcode, rd, rn, rm);
6815         break;
6816     case 1:
6817         if (!fp_access_check(s)) {
6818             return;
6819         }
6820         handle_fp_2src_double(s, opcode, rd, rn, rm);
6821         break;
6822     case 3:
6823         if (!dc_isar_feature(aa64_fp16, s)) {
6824             unallocated_encoding(s);
6825             return;
6826         }
6827         if (!fp_access_check(s)) {
6828             return;
6829         }
6830         handle_fp_2src_half(s, opcode, rd, rn, rm);
6831         break;
6832     default:
6833         unallocated_encoding(s);
6834     }
6835 }
6836 
6837 /* Floating-point data-processing (3 source) - single precision */
6838 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6839                                   int rd, int rn, int rm, int ra)
6840 {
6841     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6842     TCGv_i32 tcg_res = tcg_temp_new_i32();
6843     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6844 
6845     tcg_op1 = read_fp_sreg(s, rn);
6846     tcg_op2 = read_fp_sreg(s, rm);
6847     tcg_op3 = read_fp_sreg(s, ra);
6848 
6849     /* These are fused multiply-add, and must be done as one
6850      * floating point operation with no rounding between the
6851      * multiplication and addition steps.
6852      * NB that doing the negations here as separate steps is
6853      * correct : an input NaN should come out with its sign bit
6854      * flipped if it is a negated-input.
6855      */
6856     if (o1 == true) {
6857         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6858     }
6859 
6860     if (o0 != o1) {
6861         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6862     }
6863 
6864     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6865 
6866     write_fp_sreg(s, rd, tcg_res);
6867 }
6868 
6869 /* Floating-point data-processing (3 source) - double precision */
6870 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6871                                   int rd, int rn, int rm, int ra)
6872 {
6873     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6874     TCGv_i64 tcg_res = tcg_temp_new_i64();
6875     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6876 
6877     tcg_op1 = read_fp_dreg(s, rn);
6878     tcg_op2 = read_fp_dreg(s, rm);
6879     tcg_op3 = read_fp_dreg(s, ra);
6880 
6881     /* These are fused multiply-add, and must be done as one
6882      * floating point operation with no rounding between the
6883      * multiplication and addition steps.
6884      * NB that doing the negations here as separate steps is
6885      * correct : an input NaN should come out with its sign bit
6886      * flipped if it is a negated-input.
6887      */
6888     if (o1 == true) {
6889         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6890     }
6891 
6892     if (o0 != o1) {
6893         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6894     }
6895 
6896     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6897 
6898     write_fp_dreg(s, rd, tcg_res);
6899 }
6900 
6901 /* Floating-point data-processing (3 source) - half precision */
6902 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6903                                 int rd, int rn, int rm, int ra)
6904 {
6905     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6906     TCGv_i32 tcg_res = tcg_temp_new_i32();
6907     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6908 
6909     tcg_op1 = read_fp_hreg(s, rn);
6910     tcg_op2 = read_fp_hreg(s, rm);
6911     tcg_op3 = read_fp_hreg(s, ra);
6912 
6913     /* These are fused multiply-add, and must be done as one
6914      * floating point operation with no rounding between the
6915      * multiplication and addition steps.
6916      * NB that doing the negations here as separate steps is
6917      * correct : an input NaN should come out with its sign bit
6918      * flipped if it is a negated-input.
6919      */
6920     if (o1 == true) {
6921         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6922     }
6923 
6924     if (o0 != o1) {
6925         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6926     }
6927 
6928     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6929 
6930     write_fp_sreg(s, rd, tcg_res);
6931 }
6932 
6933 /* Floating point data-processing (3 source)
6934  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6935  * +---+---+---+-----------+------+----+------+----+------+------+------+
6936  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6937  * +---+---+---+-----------+------+----+------+----+------+------+------+
6938  */
6939 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6940 {
6941     int mos = extract32(insn, 29, 3);
6942     int type = extract32(insn, 22, 2);
6943     int rd = extract32(insn, 0, 5);
6944     int rn = extract32(insn, 5, 5);
6945     int ra = extract32(insn, 10, 5);
6946     int rm = extract32(insn, 16, 5);
6947     bool o0 = extract32(insn, 15, 1);
6948     bool o1 = extract32(insn, 21, 1);
6949 
6950     if (mos) {
6951         unallocated_encoding(s);
6952         return;
6953     }
6954 
6955     switch (type) {
6956     case 0:
6957         if (!fp_access_check(s)) {
6958             return;
6959         }
6960         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6961         break;
6962     case 1:
6963         if (!fp_access_check(s)) {
6964             return;
6965         }
6966         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6967         break;
6968     case 3:
6969         if (!dc_isar_feature(aa64_fp16, s)) {
6970             unallocated_encoding(s);
6971             return;
6972         }
6973         if (!fp_access_check(s)) {
6974             return;
6975         }
6976         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6977         break;
6978     default:
6979         unallocated_encoding(s);
6980     }
6981 }
6982 
6983 /* Floating point immediate
6984  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6985  * +---+---+---+-----------+------+---+------------+-------+------+------+
6986  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6987  * +---+---+---+-----------+------+---+------------+-------+------+------+
6988  */
6989 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6990 {
6991     int rd = extract32(insn, 0, 5);
6992     int imm5 = extract32(insn, 5, 5);
6993     int imm8 = extract32(insn, 13, 8);
6994     int type = extract32(insn, 22, 2);
6995     int mos = extract32(insn, 29, 3);
6996     uint64_t imm;
6997     MemOp sz;
6998 
6999     if (mos || imm5) {
7000         unallocated_encoding(s);
7001         return;
7002     }
7003 
7004     switch (type) {
7005     case 0:
7006         sz = MO_32;
7007         break;
7008     case 1:
7009         sz = MO_64;
7010         break;
7011     case 3:
7012         sz = MO_16;
7013         if (dc_isar_feature(aa64_fp16, s)) {
7014             break;
7015         }
7016         /* fallthru */
7017     default:
7018         unallocated_encoding(s);
7019         return;
7020     }
7021 
7022     if (!fp_access_check(s)) {
7023         return;
7024     }
7025 
7026     imm = vfp_expand_imm(sz, imm8);
7027     write_fp_dreg(s, rd, tcg_constant_i64(imm));
7028 }
7029 
7030 /* Handle floating point <=> fixed point conversions. Note that we can
7031  * also deal with fp <=> integer conversions as a special case (scale == 64)
7032  * OPTME: consider handling that special case specially or at least skipping
7033  * the call to scalbn in the helpers for zero shifts.
7034  */
7035 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
7036                            bool itof, int rmode, int scale, int sf, int type)
7037 {
7038     bool is_signed = !(opcode & 1);
7039     TCGv_ptr tcg_fpstatus;
7040     TCGv_i32 tcg_shift, tcg_single;
7041     TCGv_i64 tcg_double;
7042 
7043     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
7044 
7045     tcg_shift = tcg_constant_i32(64 - scale);
7046 
7047     if (itof) {
7048         TCGv_i64 tcg_int = cpu_reg(s, rn);
7049         if (!sf) {
7050             TCGv_i64 tcg_extend = tcg_temp_new_i64();
7051 
7052             if (is_signed) {
7053                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
7054             } else {
7055                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
7056             }
7057 
7058             tcg_int = tcg_extend;
7059         }
7060 
7061         switch (type) {
7062         case 1: /* float64 */
7063             tcg_double = tcg_temp_new_i64();
7064             if (is_signed) {
7065                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
7066                                      tcg_shift, tcg_fpstatus);
7067             } else {
7068                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
7069                                      tcg_shift, tcg_fpstatus);
7070             }
7071             write_fp_dreg(s, rd, tcg_double);
7072             break;
7073 
7074         case 0: /* float32 */
7075             tcg_single = tcg_temp_new_i32();
7076             if (is_signed) {
7077                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7078                                      tcg_shift, tcg_fpstatus);
7079             } else {
7080                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7081                                      tcg_shift, tcg_fpstatus);
7082             }
7083             write_fp_sreg(s, rd, tcg_single);
7084             break;
7085 
7086         case 3: /* float16 */
7087             tcg_single = tcg_temp_new_i32();
7088             if (is_signed) {
7089                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7090                                      tcg_shift, tcg_fpstatus);
7091             } else {
7092                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7093                                      tcg_shift, tcg_fpstatus);
7094             }
7095             write_fp_sreg(s, rd, tcg_single);
7096             break;
7097 
7098         default:
7099             g_assert_not_reached();
7100         }
7101     } else {
7102         TCGv_i64 tcg_int = cpu_reg(s, rd);
7103         TCGv_i32 tcg_rmode;
7104 
7105         if (extract32(opcode, 2, 1)) {
7106             /* There are too many rounding modes to all fit into rmode,
7107              * so FCVTA[US] is a special case.
7108              */
7109             rmode = FPROUNDING_TIEAWAY;
7110         }
7111 
7112         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
7113 
7114         switch (type) {
7115         case 1: /* float64 */
7116             tcg_double = read_fp_dreg(s, rn);
7117             if (is_signed) {
7118                 if (!sf) {
7119                     gen_helper_vfp_tosld(tcg_int, tcg_double,
7120                                          tcg_shift, tcg_fpstatus);
7121                 } else {
7122                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
7123                                          tcg_shift, tcg_fpstatus);
7124                 }
7125             } else {
7126                 if (!sf) {
7127                     gen_helper_vfp_tould(tcg_int, tcg_double,
7128                                          tcg_shift, tcg_fpstatus);
7129                 } else {
7130                     gen_helper_vfp_touqd(tcg_int, tcg_double,
7131                                          tcg_shift, tcg_fpstatus);
7132                 }
7133             }
7134             if (!sf) {
7135                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7136             }
7137             break;
7138 
7139         case 0: /* float32 */
7140             tcg_single = read_fp_sreg(s, rn);
7141             if (sf) {
7142                 if (is_signed) {
7143                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
7144                                          tcg_shift, tcg_fpstatus);
7145                 } else {
7146                     gen_helper_vfp_touqs(tcg_int, tcg_single,
7147                                          tcg_shift, tcg_fpstatus);
7148                 }
7149             } else {
7150                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7151                 if (is_signed) {
7152                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7153                                          tcg_shift, tcg_fpstatus);
7154                 } else {
7155                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7156                                          tcg_shift, tcg_fpstatus);
7157                 }
7158                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7159             }
7160             break;
7161 
7162         case 3: /* float16 */
7163             tcg_single = read_fp_sreg(s, rn);
7164             if (sf) {
7165                 if (is_signed) {
7166                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7167                                          tcg_shift, tcg_fpstatus);
7168                 } else {
7169                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7170                                          tcg_shift, tcg_fpstatus);
7171                 }
7172             } else {
7173                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7174                 if (is_signed) {
7175                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7176                                          tcg_shift, tcg_fpstatus);
7177                 } else {
7178                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7179                                          tcg_shift, tcg_fpstatus);
7180                 }
7181                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7182             }
7183             break;
7184 
7185         default:
7186             g_assert_not_reached();
7187         }
7188 
7189         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7190     }
7191 }
7192 
7193 /* Floating point <-> fixed point conversions
7194  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7195  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7196  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7197  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7198  */
7199 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7200 {
7201     int rd = extract32(insn, 0, 5);
7202     int rn = extract32(insn, 5, 5);
7203     int scale = extract32(insn, 10, 6);
7204     int opcode = extract32(insn, 16, 3);
7205     int rmode = extract32(insn, 19, 2);
7206     int type = extract32(insn, 22, 2);
7207     bool sbit = extract32(insn, 29, 1);
7208     bool sf = extract32(insn, 31, 1);
7209     bool itof;
7210 
7211     if (sbit || (!sf && scale < 32)) {
7212         unallocated_encoding(s);
7213         return;
7214     }
7215 
7216     switch (type) {
7217     case 0: /* float32 */
7218     case 1: /* float64 */
7219         break;
7220     case 3: /* float16 */
7221         if (dc_isar_feature(aa64_fp16, s)) {
7222             break;
7223         }
7224         /* fallthru */
7225     default:
7226         unallocated_encoding(s);
7227         return;
7228     }
7229 
7230     switch ((rmode << 3) | opcode) {
7231     case 0x2: /* SCVTF */
7232     case 0x3: /* UCVTF */
7233         itof = true;
7234         break;
7235     case 0x18: /* FCVTZS */
7236     case 0x19: /* FCVTZU */
7237         itof = false;
7238         break;
7239     default:
7240         unallocated_encoding(s);
7241         return;
7242     }
7243 
7244     if (!fp_access_check(s)) {
7245         return;
7246     }
7247 
7248     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7249 }
7250 
7251 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7252 {
7253     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7254      * without conversion.
7255      */
7256 
7257     if (itof) {
7258         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7259         TCGv_i64 tmp;
7260 
7261         switch (type) {
7262         case 0:
7263             /* 32 bit */
7264             tmp = tcg_temp_new_i64();
7265             tcg_gen_ext32u_i64(tmp, tcg_rn);
7266             write_fp_dreg(s, rd, tmp);
7267             break;
7268         case 1:
7269             /* 64 bit */
7270             write_fp_dreg(s, rd, tcg_rn);
7271             break;
7272         case 2:
7273             /* 64 bit to top half. */
7274             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
7275             clear_vec_high(s, true, rd);
7276             break;
7277         case 3:
7278             /* 16 bit */
7279             tmp = tcg_temp_new_i64();
7280             tcg_gen_ext16u_i64(tmp, tcg_rn);
7281             write_fp_dreg(s, rd, tmp);
7282             break;
7283         default:
7284             g_assert_not_reached();
7285         }
7286     } else {
7287         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7288 
7289         switch (type) {
7290         case 0:
7291             /* 32 bit */
7292             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
7293             break;
7294         case 1:
7295             /* 64 bit */
7296             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
7297             break;
7298         case 2:
7299             /* 64 bits from top half */
7300             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
7301             break;
7302         case 3:
7303             /* 16 bit */
7304             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
7305             break;
7306         default:
7307             g_assert_not_reached();
7308         }
7309     }
7310 }
7311 
7312 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7313 {
7314     TCGv_i64 t = read_fp_dreg(s, rn);
7315     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7316 
7317     gen_helper_fjcvtzs(t, t, fpstatus);
7318 
7319     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7320     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7321     tcg_gen_movi_i32(cpu_CF, 0);
7322     tcg_gen_movi_i32(cpu_NF, 0);
7323     tcg_gen_movi_i32(cpu_VF, 0);
7324 }
7325 
7326 /* Floating point <-> integer conversions
7327  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7328  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7329  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7330  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7331  */
7332 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7333 {
7334     int rd = extract32(insn, 0, 5);
7335     int rn = extract32(insn, 5, 5);
7336     int opcode = extract32(insn, 16, 3);
7337     int rmode = extract32(insn, 19, 2);
7338     int type = extract32(insn, 22, 2);
7339     bool sbit = extract32(insn, 29, 1);
7340     bool sf = extract32(insn, 31, 1);
7341     bool itof = false;
7342 
7343     if (sbit) {
7344         goto do_unallocated;
7345     }
7346 
7347     switch (opcode) {
7348     case 2: /* SCVTF */
7349     case 3: /* UCVTF */
7350         itof = true;
7351         /* fallthru */
7352     case 4: /* FCVTAS */
7353     case 5: /* FCVTAU */
7354         if (rmode != 0) {
7355             goto do_unallocated;
7356         }
7357         /* fallthru */
7358     case 0: /* FCVT[NPMZ]S */
7359     case 1: /* FCVT[NPMZ]U */
7360         switch (type) {
7361         case 0: /* float32 */
7362         case 1: /* float64 */
7363             break;
7364         case 3: /* float16 */
7365             if (!dc_isar_feature(aa64_fp16, s)) {
7366                 goto do_unallocated;
7367             }
7368             break;
7369         default:
7370             goto do_unallocated;
7371         }
7372         if (!fp_access_check(s)) {
7373             return;
7374         }
7375         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7376         break;
7377 
7378     default:
7379         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7380         case 0b01100110: /* FMOV half <-> 32-bit int */
7381         case 0b01100111:
7382         case 0b11100110: /* FMOV half <-> 64-bit int */
7383         case 0b11100111:
7384             if (!dc_isar_feature(aa64_fp16, s)) {
7385                 goto do_unallocated;
7386             }
7387             /* fallthru */
7388         case 0b00000110: /* FMOV 32-bit */
7389         case 0b00000111:
7390         case 0b10100110: /* FMOV 64-bit */
7391         case 0b10100111:
7392         case 0b11001110: /* FMOV top half of 128-bit */
7393         case 0b11001111:
7394             if (!fp_access_check(s)) {
7395                 return;
7396             }
7397             itof = opcode & 1;
7398             handle_fmov(s, rd, rn, type, itof);
7399             break;
7400 
7401         case 0b00111110: /* FJCVTZS */
7402             if (!dc_isar_feature(aa64_jscvt, s)) {
7403                 goto do_unallocated;
7404             } else if (fp_access_check(s)) {
7405                 handle_fjcvtzs(s, rd, rn);
7406             }
7407             break;
7408 
7409         default:
7410         do_unallocated:
7411             unallocated_encoding(s);
7412             return;
7413         }
7414         break;
7415     }
7416 }
7417 
7418 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7419  *   31  30  29 28     25 24                          0
7420  * +---+---+---+---------+-----------------------------+
7421  * |   | 0 |   | 1 1 1 1 |                             |
7422  * +---+---+---+---------+-----------------------------+
7423  */
7424 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7425 {
7426     if (extract32(insn, 24, 1)) {
7427         /* Floating point data-processing (3 source) */
7428         disas_fp_3src(s, insn);
7429     } else if (extract32(insn, 21, 1) == 0) {
7430         /* Floating point to fixed point conversions */
7431         disas_fp_fixed_conv(s, insn);
7432     } else {
7433         switch (extract32(insn, 10, 2)) {
7434         case 1:
7435             /* Floating point conditional compare */
7436             disas_fp_ccomp(s, insn);
7437             break;
7438         case 2:
7439             /* Floating point data-processing (2 source) */
7440             disas_fp_2src(s, insn);
7441             break;
7442         case 3:
7443             /* Floating point conditional select */
7444             disas_fp_csel(s, insn);
7445             break;
7446         case 0:
7447             switch (ctz32(extract32(insn, 12, 4))) {
7448             case 0: /* [15:12] == xxx1 */
7449                 /* Floating point immediate */
7450                 disas_fp_imm(s, insn);
7451                 break;
7452             case 1: /* [15:12] == xx10 */
7453                 /* Floating point compare */
7454                 disas_fp_compare(s, insn);
7455                 break;
7456             case 2: /* [15:12] == x100 */
7457                 /* Floating point data-processing (1 source) */
7458                 disas_fp_1src(s, insn);
7459                 break;
7460             case 3: /* [15:12] == 1000 */
7461                 unallocated_encoding(s);
7462                 break;
7463             default: /* [15:12] == 0000 */
7464                 /* Floating point <-> integer conversions */
7465                 disas_fp_int_conv(s, insn);
7466                 break;
7467             }
7468             break;
7469         }
7470     }
7471 }
7472 
7473 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7474                      int pos)
7475 {
7476     /* Extract 64 bits from the middle of two concatenated 64 bit
7477      * vector register slices left:right. The extracted bits start
7478      * at 'pos' bits into the right (least significant) side.
7479      * We return the result in tcg_right, and guarantee not to
7480      * trash tcg_left.
7481      */
7482     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7483     assert(pos > 0 && pos < 64);
7484 
7485     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7486     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7487     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7488 }
7489 
7490 /* EXT
7491  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7492  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7493  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7494  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7495  */
7496 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7497 {
7498     int is_q = extract32(insn, 30, 1);
7499     int op2 = extract32(insn, 22, 2);
7500     int imm4 = extract32(insn, 11, 4);
7501     int rm = extract32(insn, 16, 5);
7502     int rn = extract32(insn, 5, 5);
7503     int rd = extract32(insn, 0, 5);
7504     int pos = imm4 << 3;
7505     TCGv_i64 tcg_resl, tcg_resh;
7506 
7507     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7508         unallocated_encoding(s);
7509         return;
7510     }
7511 
7512     if (!fp_access_check(s)) {
7513         return;
7514     }
7515 
7516     tcg_resh = tcg_temp_new_i64();
7517     tcg_resl = tcg_temp_new_i64();
7518 
7519     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7520      * either extracting 128 bits from a 128:128 concatenation, or
7521      * extracting 64 bits from a 64:64 concatenation.
7522      */
7523     if (!is_q) {
7524         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7525         if (pos != 0) {
7526             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7527             do_ext64(s, tcg_resh, tcg_resl, pos);
7528         }
7529     } else {
7530         TCGv_i64 tcg_hh;
7531         typedef struct {
7532             int reg;
7533             int elt;
7534         } EltPosns;
7535         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7536         EltPosns *elt = eltposns;
7537 
7538         if (pos >= 64) {
7539             elt++;
7540             pos -= 64;
7541         }
7542 
7543         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7544         elt++;
7545         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7546         elt++;
7547         if (pos != 0) {
7548             do_ext64(s, tcg_resh, tcg_resl, pos);
7549             tcg_hh = tcg_temp_new_i64();
7550             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7551             do_ext64(s, tcg_hh, tcg_resh, pos);
7552         }
7553     }
7554 
7555     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7556     if (is_q) {
7557         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7558     }
7559     clear_vec_high(s, is_q, rd);
7560 }
7561 
7562 /* TBL/TBX
7563  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7564  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7565  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7566  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7567  */
7568 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7569 {
7570     int op2 = extract32(insn, 22, 2);
7571     int is_q = extract32(insn, 30, 1);
7572     int rm = extract32(insn, 16, 5);
7573     int rn = extract32(insn, 5, 5);
7574     int rd = extract32(insn, 0, 5);
7575     int is_tbx = extract32(insn, 12, 1);
7576     int len = (extract32(insn, 13, 2) + 1) * 16;
7577 
7578     if (op2 != 0) {
7579         unallocated_encoding(s);
7580         return;
7581     }
7582 
7583     if (!fp_access_check(s)) {
7584         return;
7585     }
7586 
7587     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7588                        vec_full_reg_offset(s, rm), tcg_env,
7589                        is_q ? 16 : 8, vec_full_reg_size(s),
7590                        (len << 6) | (is_tbx << 5) | rn,
7591                        gen_helper_simd_tblx);
7592 }
7593 
7594 /* ZIP/UZP/TRN
7595  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7596  * +---+---+-------------+------+---+------+---+------------------+------+
7597  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7598  * +---+---+-------------+------+---+------+---+------------------+------+
7599  */
7600 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7601 {
7602     int rd = extract32(insn, 0, 5);
7603     int rn = extract32(insn, 5, 5);
7604     int rm = extract32(insn, 16, 5);
7605     int size = extract32(insn, 22, 2);
7606     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7607      * bit 2 indicates 1 vs 2 variant of the insn.
7608      */
7609     int opcode = extract32(insn, 12, 2);
7610     bool part = extract32(insn, 14, 1);
7611     bool is_q = extract32(insn, 30, 1);
7612     int esize = 8 << size;
7613     int i;
7614     int datasize = is_q ? 128 : 64;
7615     int elements = datasize / esize;
7616     TCGv_i64 tcg_res[2], tcg_ele;
7617 
7618     if (opcode == 0 || (size == 3 && !is_q)) {
7619         unallocated_encoding(s);
7620         return;
7621     }
7622 
7623     if (!fp_access_check(s)) {
7624         return;
7625     }
7626 
7627     tcg_res[0] = tcg_temp_new_i64();
7628     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7629     tcg_ele = tcg_temp_new_i64();
7630 
7631     for (i = 0; i < elements; i++) {
7632         int o, w;
7633 
7634         switch (opcode) {
7635         case 1: /* UZP1/2 */
7636         {
7637             int midpoint = elements / 2;
7638             if (i < midpoint) {
7639                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7640             } else {
7641                 read_vec_element(s, tcg_ele, rm,
7642                                  2 * (i - midpoint) + part, size);
7643             }
7644             break;
7645         }
7646         case 2: /* TRN1/2 */
7647             if (i & 1) {
7648                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7649             } else {
7650                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7651             }
7652             break;
7653         case 3: /* ZIP1/2 */
7654         {
7655             int base = part * elements / 2;
7656             if (i & 1) {
7657                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7658             } else {
7659                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7660             }
7661             break;
7662         }
7663         default:
7664             g_assert_not_reached();
7665         }
7666 
7667         w = (i * esize) / 64;
7668         o = (i * esize) % 64;
7669         if (o == 0) {
7670             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7671         } else {
7672             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7673             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7674         }
7675     }
7676 
7677     for (i = 0; i <= is_q; ++i) {
7678         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7679     }
7680     clear_vec_high(s, is_q, rd);
7681 }
7682 
7683 /*
7684  * do_reduction_op helper
7685  *
7686  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7687  * important for correct NaN propagation that we do these
7688  * operations in exactly the order specified by the pseudocode.
7689  *
7690  * This is a recursive function, TCG temps should be freed by the
7691  * calling function once it is done with the values.
7692  */
7693 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7694                                 int esize, int size, int vmap, TCGv_ptr fpst)
7695 {
7696     if (esize == size) {
7697         int element;
7698         MemOp msize = esize == 16 ? MO_16 : MO_32;
7699         TCGv_i32 tcg_elem;
7700 
7701         /* We should have one register left here */
7702         assert(ctpop8(vmap) == 1);
7703         element = ctz32(vmap);
7704         assert(element < 8);
7705 
7706         tcg_elem = tcg_temp_new_i32();
7707         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7708         return tcg_elem;
7709     } else {
7710         int bits = size / 2;
7711         int shift = ctpop8(vmap) / 2;
7712         int vmap_lo = (vmap >> shift) & vmap;
7713         int vmap_hi = (vmap & ~vmap_lo);
7714         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7715 
7716         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7717         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7718         tcg_res = tcg_temp_new_i32();
7719 
7720         switch (fpopcode) {
7721         case 0x0c: /* fmaxnmv half-precision */
7722             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7723             break;
7724         case 0x0f: /* fmaxv half-precision */
7725             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7726             break;
7727         case 0x1c: /* fminnmv half-precision */
7728             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7729             break;
7730         case 0x1f: /* fminv half-precision */
7731             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7732             break;
7733         case 0x2c: /* fmaxnmv */
7734             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7735             break;
7736         case 0x2f: /* fmaxv */
7737             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7738             break;
7739         case 0x3c: /* fminnmv */
7740             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7741             break;
7742         case 0x3f: /* fminv */
7743             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7744             break;
7745         default:
7746             g_assert_not_reached();
7747         }
7748         return tcg_res;
7749     }
7750 }
7751 
7752 /* AdvSIMD across lanes
7753  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7754  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7755  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7756  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7757  */
7758 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7759 {
7760     int rd = extract32(insn, 0, 5);
7761     int rn = extract32(insn, 5, 5);
7762     int size = extract32(insn, 22, 2);
7763     int opcode = extract32(insn, 12, 5);
7764     bool is_q = extract32(insn, 30, 1);
7765     bool is_u = extract32(insn, 29, 1);
7766     bool is_fp = false;
7767     bool is_min = false;
7768     int esize;
7769     int elements;
7770     int i;
7771     TCGv_i64 tcg_res, tcg_elt;
7772 
7773     switch (opcode) {
7774     case 0x1b: /* ADDV */
7775         if (is_u) {
7776             unallocated_encoding(s);
7777             return;
7778         }
7779         /* fall through */
7780     case 0x3: /* SADDLV, UADDLV */
7781     case 0xa: /* SMAXV, UMAXV */
7782     case 0x1a: /* SMINV, UMINV */
7783         if (size == 3 || (size == 2 && !is_q)) {
7784             unallocated_encoding(s);
7785             return;
7786         }
7787         break;
7788     case 0xc: /* FMAXNMV, FMINNMV */
7789     case 0xf: /* FMAXV, FMINV */
7790         /* Bit 1 of size field encodes min vs max and the actual size
7791          * depends on the encoding of the U bit. If not set (and FP16
7792          * enabled) then we do half-precision float instead of single
7793          * precision.
7794          */
7795         is_min = extract32(size, 1, 1);
7796         is_fp = true;
7797         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7798             size = 1;
7799         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7800             unallocated_encoding(s);
7801             return;
7802         } else {
7803             size = 2;
7804         }
7805         break;
7806     default:
7807         unallocated_encoding(s);
7808         return;
7809     }
7810 
7811     if (!fp_access_check(s)) {
7812         return;
7813     }
7814 
7815     esize = 8 << size;
7816     elements = (is_q ? 128 : 64) / esize;
7817 
7818     tcg_res = tcg_temp_new_i64();
7819     tcg_elt = tcg_temp_new_i64();
7820 
7821     /* These instructions operate across all lanes of a vector
7822      * to produce a single result. We can guarantee that a 64
7823      * bit intermediate is sufficient:
7824      *  + for [US]ADDLV the maximum element size is 32 bits, and
7825      *    the result type is 64 bits
7826      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7827      *    same as the element size, which is 32 bits at most
7828      * For the integer operations we can choose to work at 64
7829      * or 32 bits and truncate at the end; for simplicity
7830      * we use 64 bits always. The floating point
7831      * ops do require 32 bit intermediates, though.
7832      */
7833     if (!is_fp) {
7834         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7835 
7836         for (i = 1; i < elements; i++) {
7837             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7838 
7839             switch (opcode) {
7840             case 0x03: /* SADDLV / UADDLV */
7841             case 0x1b: /* ADDV */
7842                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7843                 break;
7844             case 0x0a: /* SMAXV / UMAXV */
7845                 if (is_u) {
7846                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7847                 } else {
7848                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7849                 }
7850                 break;
7851             case 0x1a: /* SMINV / UMINV */
7852                 if (is_u) {
7853                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7854                 } else {
7855                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7856                 }
7857                 break;
7858             default:
7859                 g_assert_not_reached();
7860             }
7861 
7862         }
7863     } else {
7864         /* Floating point vector reduction ops which work across 32
7865          * bit (single) or 16 bit (half-precision) intermediates.
7866          * Note that correct NaN propagation requires that we do these
7867          * operations in exactly the order specified by the pseudocode.
7868          */
7869         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7870         int fpopcode = opcode | is_min << 4 | is_u << 5;
7871         int vmap = (1 << elements) - 1;
7872         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7873                                              (is_q ? 128 : 64), vmap, fpst);
7874         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7875     }
7876 
7877     /* Now truncate the result to the width required for the final output */
7878     if (opcode == 0x03) {
7879         /* SADDLV, UADDLV: result is 2*esize */
7880         size++;
7881     }
7882 
7883     switch (size) {
7884     case 0:
7885         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7886         break;
7887     case 1:
7888         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7889         break;
7890     case 2:
7891         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7892         break;
7893     case 3:
7894         break;
7895     default:
7896         g_assert_not_reached();
7897     }
7898 
7899     write_fp_dreg(s, rd, tcg_res);
7900 }
7901 
7902 /* AdvSIMD modified immediate
7903  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7904  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7905  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7906  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7907  *
7908  * There are a number of operations that can be carried out here:
7909  *   MOVI - move (shifted) imm into register
7910  *   MVNI - move inverted (shifted) imm into register
7911  *   ORR  - bitwise OR of (shifted) imm with register
7912  *   BIC  - bitwise clear of (shifted) imm with register
7913  * With ARMv8.2 we also have:
7914  *   FMOV half-precision
7915  */
7916 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7917 {
7918     int rd = extract32(insn, 0, 5);
7919     int cmode = extract32(insn, 12, 4);
7920     int o2 = extract32(insn, 11, 1);
7921     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7922     bool is_neg = extract32(insn, 29, 1);
7923     bool is_q = extract32(insn, 30, 1);
7924     uint64_t imm = 0;
7925 
7926     if (o2) {
7927         if (cmode != 0xf || is_neg) {
7928             unallocated_encoding(s);
7929             return;
7930         }
7931         /* FMOV (vector, immediate) - half-precision */
7932         if (!dc_isar_feature(aa64_fp16, s)) {
7933             unallocated_encoding(s);
7934             return;
7935         }
7936         imm = vfp_expand_imm(MO_16, abcdefgh);
7937         /* now duplicate across the lanes */
7938         imm = dup_const(MO_16, imm);
7939     } else {
7940         if (cmode == 0xf && is_neg && !is_q) {
7941             unallocated_encoding(s);
7942             return;
7943         }
7944         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7945     }
7946 
7947     if (!fp_access_check(s)) {
7948         return;
7949     }
7950 
7951     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7952         /* MOVI or MVNI, with MVNI negation handled above.  */
7953         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7954                              vec_full_reg_size(s), imm);
7955     } else {
7956         /* ORR or BIC, with BIC negation to AND handled above.  */
7957         if (is_neg) {
7958             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7959         } else {
7960             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7961         }
7962     }
7963 }
7964 
7965 /* AdvSIMD scalar pairwise
7966  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7967  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7968  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7969  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7970  */
7971 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7972 {
7973     int u = extract32(insn, 29, 1);
7974     int size = extract32(insn, 22, 2);
7975     int opcode = extract32(insn, 12, 5);
7976     int rn = extract32(insn, 5, 5);
7977     int rd = extract32(insn, 0, 5);
7978     TCGv_ptr fpst;
7979 
7980     /* For some ops (the FP ones), size[1] is part of the encoding.
7981      * For ADDP strictly it is not but size[1] is always 1 for valid
7982      * encodings.
7983      */
7984     opcode |= (extract32(size, 1, 1) << 5);
7985 
7986     switch (opcode) {
7987     case 0x3b: /* ADDP */
7988         if (u || size != 3) {
7989             unallocated_encoding(s);
7990             return;
7991         }
7992         if (!fp_access_check(s)) {
7993             return;
7994         }
7995 
7996         fpst = NULL;
7997         break;
7998     case 0xc: /* FMAXNMP */
7999     case 0xd: /* FADDP */
8000     case 0xf: /* FMAXP */
8001     case 0x2c: /* FMINNMP */
8002     case 0x2f: /* FMINP */
8003         /* FP op, size[0] is 32 or 64 bit*/
8004         if (!u) {
8005             if ((size & 1) || !dc_isar_feature(aa64_fp16, s)) {
8006                 unallocated_encoding(s);
8007                 return;
8008             } else {
8009                 size = MO_16;
8010             }
8011         } else {
8012             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8013         }
8014 
8015         if (!fp_access_check(s)) {
8016             return;
8017         }
8018 
8019         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8020         break;
8021     default:
8022         unallocated_encoding(s);
8023         return;
8024     }
8025 
8026     if (size == MO_64) {
8027         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8028         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8029         TCGv_i64 tcg_res = tcg_temp_new_i64();
8030 
8031         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8032         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8033 
8034         switch (opcode) {
8035         case 0x3b: /* ADDP */
8036             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8037             break;
8038         case 0xc: /* FMAXNMP */
8039             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8040             break;
8041         case 0xd: /* FADDP */
8042             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8043             break;
8044         case 0xf: /* FMAXP */
8045             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8046             break;
8047         case 0x2c: /* FMINNMP */
8048             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8049             break;
8050         case 0x2f: /* FMINP */
8051             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8052             break;
8053         default:
8054             g_assert_not_reached();
8055         }
8056 
8057         write_fp_dreg(s, rd, tcg_res);
8058     } else {
8059         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8060         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8061         TCGv_i32 tcg_res = tcg_temp_new_i32();
8062 
8063         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8064         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8065 
8066         if (size == MO_16) {
8067             switch (opcode) {
8068             case 0xc: /* FMAXNMP */
8069                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8070                 break;
8071             case 0xd: /* FADDP */
8072                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8073                 break;
8074             case 0xf: /* FMAXP */
8075                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8076                 break;
8077             case 0x2c: /* FMINNMP */
8078                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8079                 break;
8080             case 0x2f: /* FMINP */
8081                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8082                 break;
8083             default:
8084                 g_assert_not_reached();
8085             }
8086         } else {
8087             switch (opcode) {
8088             case 0xc: /* FMAXNMP */
8089                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8090                 break;
8091             case 0xd: /* FADDP */
8092                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8093                 break;
8094             case 0xf: /* FMAXP */
8095                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8096                 break;
8097             case 0x2c: /* FMINNMP */
8098                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8099                 break;
8100             case 0x2f: /* FMINP */
8101                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8102                 break;
8103             default:
8104                 g_assert_not_reached();
8105             }
8106         }
8107 
8108         write_fp_sreg(s, rd, tcg_res);
8109     }
8110 }
8111 
8112 /*
8113  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8114  *
8115  * This code is handles the common shifting code and is used by both
8116  * the vector and scalar code.
8117  */
8118 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8119                                     TCGv_i64 tcg_rnd, bool accumulate,
8120                                     bool is_u, int size, int shift)
8121 {
8122     bool extended_result = false;
8123     bool round = tcg_rnd != NULL;
8124     int ext_lshift = 0;
8125     TCGv_i64 tcg_src_hi;
8126 
8127     if (round && size == 3) {
8128         extended_result = true;
8129         ext_lshift = 64 - shift;
8130         tcg_src_hi = tcg_temp_new_i64();
8131     } else if (shift == 64) {
8132         if (!accumulate && is_u) {
8133             /* result is zero */
8134             tcg_gen_movi_i64(tcg_res, 0);
8135             return;
8136         }
8137     }
8138 
8139     /* Deal with the rounding step */
8140     if (round) {
8141         if (extended_result) {
8142             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8143             if (!is_u) {
8144                 /* take care of sign extending tcg_res */
8145                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8146                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8147                                  tcg_src, tcg_src_hi,
8148                                  tcg_rnd, tcg_zero);
8149             } else {
8150                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8151                                  tcg_src, tcg_zero,
8152                                  tcg_rnd, tcg_zero);
8153             }
8154         } else {
8155             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8156         }
8157     }
8158 
8159     /* Now do the shift right */
8160     if (round && extended_result) {
8161         /* extended case, >64 bit precision required */
8162         if (ext_lshift == 0) {
8163             /* special case, only high bits matter */
8164             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8165         } else {
8166             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8167             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8168             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8169         }
8170     } else {
8171         if (is_u) {
8172             if (shift == 64) {
8173                 /* essentially shifting in 64 zeros */
8174                 tcg_gen_movi_i64(tcg_src, 0);
8175             } else {
8176                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8177             }
8178         } else {
8179             if (shift == 64) {
8180                 /* effectively extending the sign-bit */
8181                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8182             } else {
8183                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8184             }
8185         }
8186     }
8187 
8188     if (accumulate) {
8189         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8190     } else {
8191         tcg_gen_mov_i64(tcg_res, tcg_src);
8192     }
8193 }
8194 
8195 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8196 static void handle_scalar_simd_shri(DisasContext *s,
8197                                     bool is_u, int immh, int immb,
8198                                     int opcode, int rn, int rd)
8199 {
8200     const int size = 3;
8201     int immhb = immh << 3 | immb;
8202     int shift = 2 * (8 << size) - immhb;
8203     bool accumulate = false;
8204     bool round = false;
8205     bool insert = false;
8206     TCGv_i64 tcg_rn;
8207     TCGv_i64 tcg_rd;
8208     TCGv_i64 tcg_round;
8209 
8210     if (!extract32(immh, 3, 1)) {
8211         unallocated_encoding(s);
8212         return;
8213     }
8214 
8215     if (!fp_access_check(s)) {
8216         return;
8217     }
8218 
8219     switch (opcode) {
8220     case 0x02: /* SSRA / USRA (accumulate) */
8221         accumulate = true;
8222         break;
8223     case 0x04: /* SRSHR / URSHR (rounding) */
8224         round = true;
8225         break;
8226     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8227         accumulate = round = true;
8228         break;
8229     case 0x08: /* SRI */
8230         insert = true;
8231         break;
8232     }
8233 
8234     if (round) {
8235         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8236     } else {
8237         tcg_round = NULL;
8238     }
8239 
8240     tcg_rn = read_fp_dreg(s, rn);
8241     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8242 
8243     if (insert) {
8244         /* shift count same as element size is valid but does nothing;
8245          * special case to avoid potential shift by 64.
8246          */
8247         int esize = 8 << size;
8248         if (shift != esize) {
8249             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8250             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8251         }
8252     } else {
8253         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8254                                 accumulate, is_u, size, shift);
8255     }
8256 
8257     write_fp_dreg(s, rd, tcg_rd);
8258 }
8259 
8260 /* SHL/SLI - Scalar shift left */
8261 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8262                                     int immh, int immb, int opcode,
8263                                     int rn, int rd)
8264 {
8265     int size = 32 - clz32(immh) - 1;
8266     int immhb = immh << 3 | immb;
8267     int shift = immhb - (8 << size);
8268     TCGv_i64 tcg_rn;
8269     TCGv_i64 tcg_rd;
8270 
8271     if (!extract32(immh, 3, 1)) {
8272         unallocated_encoding(s);
8273         return;
8274     }
8275 
8276     if (!fp_access_check(s)) {
8277         return;
8278     }
8279 
8280     tcg_rn = read_fp_dreg(s, rn);
8281     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8282 
8283     if (insert) {
8284         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8285     } else {
8286         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8287     }
8288 
8289     write_fp_dreg(s, rd, tcg_rd);
8290 }
8291 
8292 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8293  * (signed/unsigned) narrowing */
8294 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8295                                    bool is_u_shift, bool is_u_narrow,
8296                                    int immh, int immb, int opcode,
8297                                    int rn, int rd)
8298 {
8299     int immhb = immh << 3 | immb;
8300     int size = 32 - clz32(immh) - 1;
8301     int esize = 8 << size;
8302     int shift = (2 * esize) - immhb;
8303     int elements = is_scalar ? 1 : (64 / esize);
8304     bool round = extract32(opcode, 0, 1);
8305     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8306     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8307     TCGv_i32 tcg_rd_narrowed;
8308     TCGv_i64 tcg_final;
8309 
8310     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8311         { gen_helper_neon_narrow_sat_s8,
8312           gen_helper_neon_unarrow_sat8 },
8313         { gen_helper_neon_narrow_sat_s16,
8314           gen_helper_neon_unarrow_sat16 },
8315         { gen_helper_neon_narrow_sat_s32,
8316           gen_helper_neon_unarrow_sat32 },
8317         { NULL, NULL },
8318     };
8319     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8320         gen_helper_neon_narrow_sat_u8,
8321         gen_helper_neon_narrow_sat_u16,
8322         gen_helper_neon_narrow_sat_u32,
8323         NULL
8324     };
8325     NeonGenNarrowEnvFn *narrowfn;
8326 
8327     int i;
8328 
8329     assert(size < 4);
8330 
8331     if (extract32(immh, 3, 1)) {
8332         unallocated_encoding(s);
8333         return;
8334     }
8335 
8336     if (!fp_access_check(s)) {
8337         return;
8338     }
8339 
8340     if (is_u_shift) {
8341         narrowfn = unsigned_narrow_fns[size];
8342     } else {
8343         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8344     }
8345 
8346     tcg_rn = tcg_temp_new_i64();
8347     tcg_rd = tcg_temp_new_i64();
8348     tcg_rd_narrowed = tcg_temp_new_i32();
8349     tcg_final = tcg_temp_new_i64();
8350 
8351     if (round) {
8352         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8353     } else {
8354         tcg_round = NULL;
8355     }
8356 
8357     for (i = 0; i < elements; i++) {
8358         read_vec_element(s, tcg_rn, rn, i, ldop);
8359         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8360                                 false, is_u_shift, size+1, shift);
8361         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8362         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8363         if (i == 0) {
8364             tcg_gen_extract_i64(tcg_final, tcg_rd, 0, esize);
8365         } else {
8366             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8367         }
8368     }
8369 
8370     if (!is_q) {
8371         write_vec_element(s, tcg_final, rd, 0, MO_64);
8372     } else {
8373         write_vec_element(s, tcg_final, rd, 1, MO_64);
8374     }
8375     clear_vec_high(s, is_q, rd);
8376 }
8377 
8378 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8379 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8380                              bool src_unsigned, bool dst_unsigned,
8381                              int immh, int immb, int rn, int rd)
8382 {
8383     int immhb = immh << 3 | immb;
8384     int size = 32 - clz32(immh) - 1;
8385     int shift = immhb - (8 << size);
8386     int pass;
8387 
8388     assert(immh != 0);
8389     assert(!(scalar && is_q));
8390 
8391     if (!scalar) {
8392         if (!is_q && extract32(immh, 3, 1)) {
8393             unallocated_encoding(s);
8394             return;
8395         }
8396 
8397         /* Since we use the variable-shift helpers we must
8398          * replicate the shift count into each element of
8399          * the tcg_shift value.
8400          */
8401         switch (size) {
8402         case 0:
8403             shift |= shift << 8;
8404             /* fall through */
8405         case 1:
8406             shift |= shift << 16;
8407             break;
8408         case 2:
8409         case 3:
8410             break;
8411         default:
8412             g_assert_not_reached();
8413         }
8414     }
8415 
8416     if (!fp_access_check(s)) {
8417         return;
8418     }
8419 
8420     if (size == 3) {
8421         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8422         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8423             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8424             { NULL, gen_helper_neon_qshl_u64 },
8425         };
8426         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8427         int maxpass = is_q ? 2 : 1;
8428 
8429         for (pass = 0; pass < maxpass; pass++) {
8430             TCGv_i64 tcg_op = tcg_temp_new_i64();
8431 
8432             read_vec_element(s, tcg_op, rn, pass, MO_64);
8433             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8434             write_vec_element(s, tcg_op, rd, pass, MO_64);
8435         }
8436         clear_vec_high(s, is_q, rd);
8437     } else {
8438         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8439         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8440             {
8441                 { gen_helper_neon_qshl_s8,
8442                   gen_helper_neon_qshl_s16,
8443                   gen_helper_neon_qshl_s32 },
8444                 { gen_helper_neon_qshlu_s8,
8445                   gen_helper_neon_qshlu_s16,
8446                   gen_helper_neon_qshlu_s32 }
8447             }, {
8448                 { NULL, NULL, NULL },
8449                 { gen_helper_neon_qshl_u8,
8450                   gen_helper_neon_qshl_u16,
8451                   gen_helper_neon_qshl_u32 }
8452             }
8453         };
8454         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8455         MemOp memop = scalar ? size : MO_32;
8456         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8457 
8458         for (pass = 0; pass < maxpass; pass++) {
8459             TCGv_i32 tcg_op = tcg_temp_new_i32();
8460 
8461             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8462             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8463             if (scalar) {
8464                 switch (size) {
8465                 case 0:
8466                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8467                     break;
8468                 case 1:
8469                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8470                     break;
8471                 case 2:
8472                     break;
8473                 default:
8474                     g_assert_not_reached();
8475                 }
8476                 write_fp_sreg(s, rd, tcg_op);
8477             } else {
8478                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8479             }
8480         }
8481 
8482         if (!scalar) {
8483             clear_vec_high(s, is_q, rd);
8484         }
8485     }
8486 }
8487 
8488 /* Common vector code for handling integer to FP conversion */
8489 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8490                                    int elements, int is_signed,
8491                                    int fracbits, int size)
8492 {
8493     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8494     TCGv_i32 tcg_shift = NULL;
8495 
8496     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8497     int pass;
8498 
8499     if (fracbits || size == MO_64) {
8500         tcg_shift = tcg_constant_i32(fracbits);
8501     }
8502 
8503     if (size == MO_64) {
8504         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8505         TCGv_i64 tcg_double = tcg_temp_new_i64();
8506 
8507         for (pass = 0; pass < elements; pass++) {
8508             read_vec_element(s, tcg_int64, rn, pass, mop);
8509 
8510             if (is_signed) {
8511                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8512                                      tcg_shift, tcg_fpst);
8513             } else {
8514                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8515                                      tcg_shift, tcg_fpst);
8516             }
8517             if (elements == 1) {
8518                 write_fp_dreg(s, rd, tcg_double);
8519             } else {
8520                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8521             }
8522         }
8523     } else {
8524         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8525         TCGv_i32 tcg_float = tcg_temp_new_i32();
8526 
8527         for (pass = 0; pass < elements; pass++) {
8528             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8529 
8530             switch (size) {
8531             case MO_32:
8532                 if (fracbits) {
8533                     if (is_signed) {
8534                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8535                                              tcg_shift, tcg_fpst);
8536                     } else {
8537                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8538                                              tcg_shift, tcg_fpst);
8539                     }
8540                 } else {
8541                     if (is_signed) {
8542                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8543                     } else {
8544                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8545                     }
8546                 }
8547                 break;
8548             case MO_16:
8549                 if (fracbits) {
8550                     if (is_signed) {
8551                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8552                                              tcg_shift, tcg_fpst);
8553                     } else {
8554                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8555                                              tcg_shift, tcg_fpst);
8556                     }
8557                 } else {
8558                     if (is_signed) {
8559                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8560                     } else {
8561                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8562                     }
8563                 }
8564                 break;
8565             default:
8566                 g_assert_not_reached();
8567             }
8568 
8569             if (elements == 1) {
8570                 write_fp_sreg(s, rd, tcg_float);
8571             } else {
8572                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8573             }
8574         }
8575     }
8576 
8577     clear_vec_high(s, elements << size == 16, rd);
8578 }
8579 
8580 /* UCVTF/SCVTF - Integer to FP conversion */
8581 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8582                                          bool is_q, bool is_u,
8583                                          int immh, int immb, int opcode,
8584                                          int rn, int rd)
8585 {
8586     int size, elements, fracbits;
8587     int immhb = immh << 3 | immb;
8588 
8589     if (immh & 8) {
8590         size = MO_64;
8591         if (!is_scalar && !is_q) {
8592             unallocated_encoding(s);
8593             return;
8594         }
8595     } else if (immh & 4) {
8596         size = MO_32;
8597     } else if (immh & 2) {
8598         size = MO_16;
8599         if (!dc_isar_feature(aa64_fp16, s)) {
8600             unallocated_encoding(s);
8601             return;
8602         }
8603     } else {
8604         /* immh == 0 would be a failure of the decode logic */
8605         g_assert(immh == 1);
8606         unallocated_encoding(s);
8607         return;
8608     }
8609 
8610     if (is_scalar) {
8611         elements = 1;
8612     } else {
8613         elements = (8 << is_q) >> size;
8614     }
8615     fracbits = (16 << size) - immhb;
8616 
8617     if (!fp_access_check(s)) {
8618         return;
8619     }
8620 
8621     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8622 }
8623 
8624 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8625 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8626                                          bool is_q, bool is_u,
8627                                          int immh, int immb, int rn, int rd)
8628 {
8629     int immhb = immh << 3 | immb;
8630     int pass, size, fracbits;
8631     TCGv_ptr tcg_fpstatus;
8632     TCGv_i32 tcg_rmode, tcg_shift;
8633 
8634     if (immh & 0x8) {
8635         size = MO_64;
8636         if (!is_scalar && !is_q) {
8637             unallocated_encoding(s);
8638             return;
8639         }
8640     } else if (immh & 0x4) {
8641         size = MO_32;
8642     } else if (immh & 0x2) {
8643         size = MO_16;
8644         if (!dc_isar_feature(aa64_fp16, s)) {
8645             unallocated_encoding(s);
8646             return;
8647         }
8648     } else {
8649         /* Should have split out AdvSIMD modified immediate earlier.  */
8650         assert(immh == 1);
8651         unallocated_encoding(s);
8652         return;
8653     }
8654 
8655     if (!fp_access_check(s)) {
8656         return;
8657     }
8658 
8659     assert(!(is_scalar && is_q));
8660 
8661     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8662     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8663     fracbits = (16 << size) - immhb;
8664     tcg_shift = tcg_constant_i32(fracbits);
8665 
8666     if (size == MO_64) {
8667         int maxpass = is_scalar ? 1 : 2;
8668 
8669         for (pass = 0; pass < maxpass; pass++) {
8670             TCGv_i64 tcg_op = tcg_temp_new_i64();
8671 
8672             read_vec_element(s, tcg_op, rn, pass, MO_64);
8673             if (is_u) {
8674                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8675             } else {
8676                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8677             }
8678             write_vec_element(s, tcg_op, rd, pass, MO_64);
8679         }
8680         clear_vec_high(s, is_q, rd);
8681     } else {
8682         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8683         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8684 
8685         switch (size) {
8686         case MO_16:
8687             if (is_u) {
8688                 fn = gen_helper_vfp_touhh;
8689             } else {
8690                 fn = gen_helper_vfp_toshh;
8691             }
8692             break;
8693         case MO_32:
8694             if (is_u) {
8695                 fn = gen_helper_vfp_touls;
8696             } else {
8697                 fn = gen_helper_vfp_tosls;
8698             }
8699             break;
8700         default:
8701             g_assert_not_reached();
8702         }
8703 
8704         for (pass = 0; pass < maxpass; pass++) {
8705             TCGv_i32 tcg_op = tcg_temp_new_i32();
8706 
8707             read_vec_element_i32(s, tcg_op, rn, pass, size);
8708             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8709             if (is_scalar) {
8710                 if (size == MO_16 && !is_u) {
8711                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8712                 }
8713                 write_fp_sreg(s, rd, tcg_op);
8714             } else {
8715                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8716             }
8717         }
8718         if (!is_scalar) {
8719             clear_vec_high(s, is_q, rd);
8720         }
8721     }
8722 
8723     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8724 }
8725 
8726 /* AdvSIMD scalar shift by immediate
8727  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8728  * +-----+---+-------------+------+------+--------+---+------+------+
8729  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8730  * +-----+---+-------------+------+------+--------+---+------+------+
8731  *
8732  * This is the scalar version so it works on a fixed sized registers
8733  */
8734 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8735 {
8736     int rd = extract32(insn, 0, 5);
8737     int rn = extract32(insn, 5, 5);
8738     int opcode = extract32(insn, 11, 5);
8739     int immb = extract32(insn, 16, 3);
8740     int immh = extract32(insn, 19, 4);
8741     bool is_u = extract32(insn, 29, 1);
8742 
8743     if (immh == 0) {
8744         unallocated_encoding(s);
8745         return;
8746     }
8747 
8748     switch (opcode) {
8749     case 0x08: /* SRI */
8750         if (!is_u) {
8751             unallocated_encoding(s);
8752             return;
8753         }
8754         /* fall through */
8755     case 0x00: /* SSHR / USHR */
8756     case 0x02: /* SSRA / USRA */
8757     case 0x04: /* SRSHR / URSHR */
8758     case 0x06: /* SRSRA / URSRA */
8759         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8760         break;
8761     case 0x0a: /* SHL / SLI */
8762         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8763         break;
8764     case 0x1c: /* SCVTF, UCVTF */
8765         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8766                                      opcode, rn, rd);
8767         break;
8768     case 0x10: /* SQSHRUN, SQSHRUN2 */
8769     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8770         if (!is_u) {
8771             unallocated_encoding(s);
8772             return;
8773         }
8774         handle_vec_simd_sqshrn(s, true, false, false, true,
8775                                immh, immb, opcode, rn, rd);
8776         break;
8777     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8778     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8779         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8780                                immh, immb, opcode, rn, rd);
8781         break;
8782     case 0xc: /* SQSHLU */
8783         if (!is_u) {
8784             unallocated_encoding(s);
8785             return;
8786         }
8787         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8788         break;
8789     case 0xe: /* SQSHL, UQSHL */
8790         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8791         break;
8792     case 0x1f: /* FCVTZS, FCVTZU */
8793         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8794         break;
8795     default:
8796         unallocated_encoding(s);
8797         break;
8798     }
8799 }
8800 
8801 /* AdvSIMD scalar three different
8802  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8803  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8804  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8805  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8806  */
8807 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8808 {
8809     bool is_u = extract32(insn, 29, 1);
8810     int size = extract32(insn, 22, 2);
8811     int opcode = extract32(insn, 12, 4);
8812     int rm = extract32(insn, 16, 5);
8813     int rn = extract32(insn, 5, 5);
8814     int rd = extract32(insn, 0, 5);
8815 
8816     if (is_u) {
8817         unallocated_encoding(s);
8818         return;
8819     }
8820 
8821     switch (opcode) {
8822     case 0x9: /* SQDMLAL, SQDMLAL2 */
8823     case 0xb: /* SQDMLSL, SQDMLSL2 */
8824     case 0xd: /* SQDMULL, SQDMULL2 */
8825         if (size == 0 || size == 3) {
8826             unallocated_encoding(s);
8827             return;
8828         }
8829         break;
8830     default:
8831         unallocated_encoding(s);
8832         return;
8833     }
8834 
8835     if (!fp_access_check(s)) {
8836         return;
8837     }
8838 
8839     if (size == 2) {
8840         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8841         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8842         TCGv_i64 tcg_res = tcg_temp_new_i64();
8843 
8844         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8845         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8846 
8847         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8848         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
8849 
8850         switch (opcode) {
8851         case 0xd: /* SQDMULL, SQDMULL2 */
8852             break;
8853         case 0xb: /* SQDMLSL, SQDMLSL2 */
8854             tcg_gen_neg_i64(tcg_res, tcg_res);
8855             /* fall through */
8856         case 0x9: /* SQDMLAL, SQDMLAL2 */
8857             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8858             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
8859                                               tcg_res, tcg_op1);
8860             break;
8861         default:
8862             g_assert_not_reached();
8863         }
8864 
8865         write_fp_dreg(s, rd, tcg_res);
8866     } else {
8867         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8868         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8869         TCGv_i64 tcg_res = tcg_temp_new_i64();
8870 
8871         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8872         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
8873 
8874         switch (opcode) {
8875         case 0xd: /* SQDMULL, SQDMULL2 */
8876             break;
8877         case 0xb: /* SQDMLSL, SQDMLSL2 */
8878             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8879             /* fall through */
8880         case 0x9: /* SQDMLAL, SQDMLAL2 */
8881         {
8882             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8883             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8884             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
8885                                               tcg_res, tcg_op3);
8886             break;
8887         }
8888         default:
8889             g_assert_not_reached();
8890         }
8891 
8892         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8893         write_fp_dreg(s, rd, tcg_res);
8894     }
8895 }
8896 
8897 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8898                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8899 {
8900     /* Handle 64x64->64 opcodes which are shared between the scalar
8901      * and vector 3-same groups. We cover every opcode where size == 3
8902      * is valid in either the three-reg-same (integer, not pairwise)
8903      * or scalar-three-reg-same groups.
8904      */
8905     TCGCond cond;
8906 
8907     switch (opcode) {
8908     case 0x1: /* SQADD */
8909         if (u) {
8910             gen_helper_neon_qadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8911         } else {
8912             gen_helper_neon_qadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8913         }
8914         break;
8915     case 0x5: /* SQSUB */
8916         if (u) {
8917             gen_helper_neon_qsub_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8918         } else {
8919             gen_helper_neon_qsub_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8920         }
8921         break;
8922     case 0x6: /* CMGT, CMHI */
8923         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8924     do_cmop:
8925         /* 64 bit integer comparison, result = test ? -1 : 0. */
8926         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8927         break;
8928     case 0x7: /* CMGE, CMHS */
8929         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8930         goto do_cmop;
8931     case 0x11: /* CMTST, CMEQ */
8932         if (u) {
8933             cond = TCG_COND_EQ;
8934             goto do_cmop;
8935         }
8936         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8937         break;
8938     case 0x8: /* SSHL, USHL */
8939         if (u) {
8940             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8941         } else {
8942             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8943         }
8944         break;
8945     case 0x9: /* SQSHL, UQSHL */
8946         if (u) {
8947             gen_helper_neon_qshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8948         } else {
8949             gen_helper_neon_qshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8950         }
8951         break;
8952     case 0xa: /* SRSHL, URSHL */
8953         if (u) {
8954             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8955         } else {
8956             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8957         }
8958         break;
8959     case 0xb: /* SQRSHL, UQRSHL */
8960         if (u) {
8961             gen_helper_neon_qrshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8962         } else {
8963             gen_helper_neon_qrshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8964         }
8965         break;
8966     case 0x10: /* ADD, SUB */
8967         if (u) {
8968             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8969         } else {
8970             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8971         }
8972         break;
8973     default:
8974         g_assert_not_reached();
8975     }
8976 }
8977 
8978 /* Handle the 3-same-operands float operations; shared by the scalar
8979  * and vector encodings. The caller must filter out any encodings
8980  * not allocated for the encoding it is dealing with.
8981  */
8982 static void handle_3same_float(DisasContext *s, int size, int elements,
8983                                int fpopcode, int rd, int rn, int rm)
8984 {
8985     int pass;
8986     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8987 
8988     for (pass = 0; pass < elements; pass++) {
8989         if (size) {
8990             /* Double */
8991             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8992             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8993             TCGv_i64 tcg_res = tcg_temp_new_i64();
8994 
8995             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8996             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8997 
8998             switch (fpopcode) {
8999             case 0x39: /* FMLS */
9000                 /* As usual for ARM, separate negation for fused multiply-add */
9001                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9002                 /* fall through */
9003             case 0x19: /* FMLA */
9004                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9005                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9006                                        tcg_res, fpst);
9007                 break;
9008             case 0x18: /* FMAXNM */
9009                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9010                 break;
9011             case 0x1a: /* FADD */
9012                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9013                 break;
9014             case 0x1b: /* FMULX */
9015                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9016                 break;
9017             case 0x1c: /* FCMEQ */
9018                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9019                 break;
9020             case 0x1e: /* FMAX */
9021                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9022                 break;
9023             case 0x1f: /* FRECPS */
9024                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9025                 break;
9026             case 0x38: /* FMINNM */
9027                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9028                 break;
9029             case 0x3a: /* FSUB */
9030                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9031                 break;
9032             case 0x3e: /* FMIN */
9033                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9034                 break;
9035             case 0x3f: /* FRSQRTS */
9036                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9037                 break;
9038             case 0x5b: /* FMUL */
9039                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9040                 break;
9041             case 0x5c: /* FCMGE */
9042                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9043                 break;
9044             case 0x5d: /* FACGE */
9045                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9046                 break;
9047             case 0x5f: /* FDIV */
9048                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9049                 break;
9050             case 0x7a: /* FABD */
9051                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9052                 gen_helper_vfp_absd(tcg_res, tcg_res);
9053                 break;
9054             case 0x7c: /* FCMGT */
9055                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9056                 break;
9057             case 0x7d: /* FACGT */
9058                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9059                 break;
9060             default:
9061                 g_assert_not_reached();
9062             }
9063 
9064             write_vec_element(s, tcg_res, rd, pass, MO_64);
9065         } else {
9066             /* Single */
9067             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9068             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9069             TCGv_i32 tcg_res = tcg_temp_new_i32();
9070 
9071             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9072             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9073 
9074             switch (fpopcode) {
9075             case 0x39: /* FMLS */
9076                 /* As usual for ARM, separate negation for fused multiply-add */
9077                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9078                 /* fall through */
9079             case 0x19: /* FMLA */
9080                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9081                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9082                                        tcg_res, fpst);
9083                 break;
9084             case 0x1a: /* FADD */
9085                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9086                 break;
9087             case 0x1b: /* FMULX */
9088                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9089                 break;
9090             case 0x1c: /* FCMEQ */
9091                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9092                 break;
9093             case 0x1e: /* FMAX */
9094                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9095                 break;
9096             case 0x1f: /* FRECPS */
9097                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9098                 break;
9099             case 0x18: /* FMAXNM */
9100                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9101                 break;
9102             case 0x38: /* FMINNM */
9103                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9104                 break;
9105             case 0x3a: /* FSUB */
9106                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9107                 break;
9108             case 0x3e: /* FMIN */
9109                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9110                 break;
9111             case 0x3f: /* FRSQRTS */
9112                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9113                 break;
9114             case 0x5b: /* FMUL */
9115                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9116                 break;
9117             case 0x5c: /* FCMGE */
9118                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9119                 break;
9120             case 0x5d: /* FACGE */
9121                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9122                 break;
9123             case 0x5f: /* FDIV */
9124                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9125                 break;
9126             case 0x7a: /* FABD */
9127                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9128                 gen_helper_vfp_abss(tcg_res, tcg_res);
9129                 break;
9130             case 0x7c: /* FCMGT */
9131                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9132                 break;
9133             case 0x7d: /* FACGT */
9134                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9135                 break;
9136             default:
9137                 g_assert_not_reached();
9138             }
9139 
9140             if (elements == 1) {
9141                 /* scalar single so clear high part */
9142                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9143 
9144                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9145                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9146             } else {
9147                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9148             }
9149         }
9150     }
9151 
9152     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9153 }
9154 
9155 /* AdvSIMD scalar three same
9156  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9157  * +-----+---+-----------+------+---+------+--------+---+------+------+
9158  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9159  * +-----+---+-----------+------+---+------+--------+---+------+------+
9160  */
9161 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9162 {
9163     int rd = extract32(insn, 0, 5);
9164     int rn = extract32(insn, 5, 5);
9165     int opcode = extract32(insn, 11, 5);
9166     int rm = extract32(insn, 16, 5);
9167     int size = extract32(insn, 22, 2);
9168     bool u = extract32(insn, 29, 1);
9169     TCGv_i64 tcg_rd;
9170 
9171     if (opcode >= 0x18) {
9172         /* Floating point: U, size[1] and opcode indicate operation */
9173         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9174         switch (fpopcode) {
9175         case 0x1b: /* FMULX */
9176         case 0x1f: /* FRECPS */
9177         case 0x3f: /* FRSQRTS */
9178         case 0x5d: /* FACGE */
9179         case 0x7d: /* FACGT */
9180         case 0x1c: /* FCMEQ */
9181         case 0x5c: /* FCMGE */
9182         case 0x7c: /* FCMGT */
9183         case 0x7a: /* FABD */
9184             break;
9185         default:
9186             unallocated_encoding(s);
9187             return;
9188         }
9189 
9190         if (!fp_access_check(s)) {
9191             return;
9192         }
9193 
9194         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9195         return;
9196     }
9197 
9198     switch (opcode) {
9199     case 0x1: /* SQADD, UQADD */
9200     case 0x5: /* SQSUB, UQSUB */
9201     case 0x9: /* SQSHL, UQSHL */
9202     case 0xb: /* SQRSHL, UQRSHL */
9203         break;
9204     case 0x8: /* SSHL, USHL */
9205     case 0xa: /* SRSHL, URSHL */
9206     case 0x6: /* CMGT, CMHI */
9207     case 0x7: /* CMGE, CMHS */
9208     case 0x11: /* CMTST, CMEQ */
9209     case 0x10: /* ADD, SUB (vector) */
9210         if (size != 3) {
9211             unallocated_encoding(s);
9212             return;
9213         }
9214         break;
9215     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9216         if (size != 1 && size != 2) {
9217             unallocated_encoding(s);
9218             return;
9219         }
9220         break;
9221     default:
9222         unallocated_encoding(s);
9223         return;
9224     }
9225 
9226     if (!fp_access_check(s)) {
9227         return;
9228     }
9229 
9230     tcg_rd = tcg_temp_new_i64();
9231 
9232     if (size == 3) {
9233         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9234         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9235 
9236         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9237     } else {
9238         /* Do a single operation on the lowest element in the vector.
9239          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9240          * no side effects for all these operations.
9241          * OPTME: special-purpose helpers would avoid doing some
9242          * unnecessary work in the helper for the 8 and 16 bit cases.
9243          */
9244         NeonGenTwoOpEnvFn *genenvfn;
9245         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9246         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9247         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9248 
9249         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9250         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9251 
9252         switch (opcode) {
9253         case 0x1: /* SQADD, UQADD */
9254         {
9255             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9256                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9257                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9258                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9259             };
9260             genenvfn = fns[size][u];
9261             break;
9262         }
9263         case 0x5: /* SQSUB, UQSUB */
9264         {
9265             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9266                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9267                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9268                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9269             };
9270             genenvfn = fns[size][u];
9271             break;
9272         }
9273         case 0x9: /* SQSHL, UQSHL */
9274         {
9275             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9276                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9277                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9278                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9279             };
9280             genenvfn = fns[size][u];
9281             break;
9282         }
9283         case 0xb: /* SQRSHL, UQRSHL */
9284         {
9285             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9286                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9287                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9288                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9289             };
9290             genenvfn = fns[size][u];
9291             break;
9292         }
9293         case 0x16: /* SQDMULH, SQRDMULH */
9294         {
9295             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9296                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9297                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9298             };
9299             assert(size == 1 || size == 2);
9300             genenvfn = fns[size - 1][u];
9301             break;
9302         }
9303         default:
9304             g_assert_not_reached();
9305         }
9306 
9307         genenvfn(tcg_rd32, tcg_env, tcg_rn, tcg_rm);
9308         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9309     }
9310 
9311     write_fp_dreg(s, rd, tcg_rd);
9312 }
9313 
9314 /* AdvSIMD scalar three same FP16
9315  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9316  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9317  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9318  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9319  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9320  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9321  */
9322 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9323                                                   uint32_t insn)
9324 {
9325     int rd = extract32(insn, 0, 5);
9326     int rn = extract32(insn, 5, 5);
9327     int opcode = extract32(insn, 11, 3);
9328     int rm = extract32(insn, 16, 5);
9329     bool u = extract32(insn, 29, 1);
9330     bool a = extract32(insn, 23, 1);
9331     int fpopcode = opcode | (a << 3) |  (u << 4);
9332     TCGv_ptr fpst;
9333     TCGv_i32 tcg_op1;
9334     TCGv_i32 tcg_op2;
9335     TCGv_i32 tcg_res;
9336 
9337     switch (fpopcode) {
9338     case 0x03: /* FMULX */
9339     case 0x04: /* FCMEQ (reg) */
9340     case 0x07: /* FRECPS */
9341     case 0x0f: /* FRSQRTS */
9342     case 0x14: /* FCMGE (reg) */
9343     case 0x15: /* FACGE */
9344     case 0x1a: /* FABD */
9345     case 0x1c: /* FCMGT (reg) */
9346     case 0x1d: /* FACGT */
9347         break;
9348     default:
9349         unallocated_encoding(s);
9350         return;
9351     }
9352 
9353     if (!dc_isar_feature(aa64_fp16, s)) {
9354         unallocated_encoding(s);
9355     }
9356 
9357     if (!fp_access_check(s)) {
9358         return;
9359     }
9360 
9361     fpst = fpstatus_ptr(FPST_FPCR_F16);
9362 
9363     tcg_op1 = read_fp_hreg(s, rn);
9364     tcg_op2 = read_fp_hreg(s, rm);
9365     tcg_res = tcg_temp_new_i32();
9366 
9367     switch (fpopcode) {
9368     case 0x03: /* FMULX */
9369         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9370         break;
9371     case 0x04: /* FCMEQ (reg) */
9372         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9373         break;
9374     case 0x07: /* FRECPS */
9375         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9376         break;
9377     case 0x0f: /* FRSQRTS */
9378         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9379         break;
9380     case 0x14: /* FCMGE (reg) */
9381         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9382         break;
9383     case 0x15: /* FACGE */
9384         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9385         break;
9386     case 0x1a: /* FABD */
9387         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9388         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9389         break;
9390     case 0x1c: /* FCMGT (reg) */
9391         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9392         break;
9393     case 0x1d: /* FACGT */
9394         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9395         break;
9396     default:
9397         g_assert_not_reached();
9398     }
9399 
9400     write_fp_sreg(s, rd, tcg_res);
9401 }
9402 
9403 /* AdvSIMD scalar three same extra
9404  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9405  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9406  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9407  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9408  */
9409 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9410                                                    uint32_t insn)
9411 {
9412     int rd = extract32(insn, 0, 5);
9413     int rn = extract32(insn, 5, 5);
9414     int opcode = extract32(insn, 11, 4);
9415     int rm = extract32(insn, 16, 5);
9416     int size = extract32(insn, 22, 2);
9417     bool u = extract32(insn, 29, 1);
9418     TCGv_i32 ele1, ele2, ele3;
9419     TCGv_i64 res;
9420     bool feature;
9421 
9422     switch (u * 16 + opcode) {
9423     case 0x10: /* SQRDMLAH (vector) */
9424     case 0x11: /* SQRDMLSH (vector) */
9425         if (size != 1 && size != 2) {
9426             unallocated_encoding(s);
9427             return;
9428         }
9429         feature = dc_isar_feature(aa64_rdm, s);
9430         break;
9431     default:
9432         unallocated_encoding(s);
9433         return;
9434     }
9435     if (!feature) {
9436         unallocated_encoding(s);
9437         return;
9438     }
9439     if (!fp_access_check(s)) {
9440         return;
9441     }
9442 
9443     /* Do a single operation on the lowest element in the vector.
9444      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9445      * with no side effects for all these operations.
9446      * OPTME: special-purpose helpers would avoid doing some
9447      * unnecessary work in the helper for the 16 bit cases.
9448      */
9449     ele1 = tcg_temp_new_i32();
9450     ele2 = tcg_temp_new_i32();
9451     ele3 = tcg_temp_new_i32();
9452 
9453     read_vec_element_i32(s, ele1, rn, 0, size);
9454     read_vec_element_i32(s, ele2, rm, 0, size);
9455     read_vec_element_i32(s, ele3, rd, 0, size);
9456 
9457     switch (opcode) {
9458     case 0x0: /* SQRDMLAH */
9459         if (size == 1) {
9460             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9461         } else {
9462             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9463         }
9464         break;
9465     case 0x1: /* SQRDMLSH */
9466         if (size == 1) {
9467             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9468         } else {
9469             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9470         }
9471         break;
9472     default:
9473         g_assert_not_reached();
9474     }
9475 
9476     res = tcg_temp_new_i64();
9477     tcg_gen_extu_i32_i64(res, ele3);
9478     write_fp_dreg(s, rd, res);
9479 }
9480 
9481 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9482                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9483                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9484 {
9485     /* Handle 64->64 opcodes which are shared between the scalar and
9486      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9487      * is valid in either group and also the double-precision fp ops.
9488      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9489      * requires them.
9490      */
9491     TCGCond cond;
9492 
9493     switch (opcode) {
9494     case 0x4: /* CLS, CLZ */
9495         if (u) {
9496             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9497         } else {
9498             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9499         }
9500         break;
9501     case 0x5: /* NOT */
9502         /* This opcode is shared with CNT and RBIT but we have earlier
9503          * enforced that size == 3 if and only if this is the NOT insn.
9504          */
9505         tcg_gen_not_i64(tcg_rd, tcg_rn);
9506         break;
9507     case 0x7: /* SQABS, SQNEG */
9508         if (u) {
9509             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9510         } else {
9511             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9512         }
9513         break;
9514     case 0xa: /* CMLT */
9515         cond = TCG_COND_LT;
9516     do_cmop:
9517         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9518         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9519         break;
9520     case 0x8: /* CMGT, CMGE */
9521         cond = u ? TCG_COND_GE : TCG_COND_GT;
9522         goto do_cmop;
9523     case 0x9: /* CMEQ, CMLE */
9524         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9525         goto do_cmop;
9526     case 0xb: /* ABS, NEG */
9527         if (u) {
9528             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9529         } else {
9530             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9531         }
9532         break;
9533     case 0x2f: /* FABS */
9534         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9535         break;
9536     case 0x6f: /* FNEG */
9537         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9538         break;
9539     case 0x7f: /* FSQRT */
9540         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9541         break;
9542     case 0x1a: /* FCVTNS */
9543     case 0x1b: /* FCVTMS */
9544     case 0x1c: /* FCVTAS */
9545     case 0x3a: /* FCVTPS */
9546     case 0x3b: /* FCVTZS */
9547         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9548         break;
9549     case 0x5a: /* FCVTNU */
9550     case 0x5b: /* FCVTMU */
9551     case 0x5c: /* FCVTAU */
9552     case 0x7a: /* FCVTPU */
9553     case 0x7b: /* FCVTZU */
9554         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9555         break;
9556     case 0x18: /* FRINTN */
9557     case 0x19: /* FRINTM */
9558     case 0x38: /* FRINTP */
9559     case 0x39: /* FRINTZ */
9560     case 0x58: /* FRINTA */
9561     case 0x79: /* FRINTI */
9562         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9563         break;
9564     case 0x59: /* FRINTX */
9565         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9566         break;
9567     case 0x1e: /* FRINT32Z */
9568     case 0x5e: /* FRINT32X */
9569         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9570         break;
9571     case 0x1f: /* FRINT64Z */
9572     case 0x5f: /* FRINT64X */
9573         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9574         break;
9575     default:
9576         g_assert_not_reached();
9577     }
9578 }
9579 
9580 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9581                                    bool is_scalar, bool is_u, bool is_q,
9582                                    int size, int rn, int rd)
9583 {
9584     bool is_double = (size == MO_64);
9585     TCGv_ptr fpst;
9586 
9587     if (!fp_access_check(s)) {
9588         return;
9589     }
9590 
9591     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9592 
9593     if (is_double) {
9594         TCGv_i64 tcg_op = tcg_temp_new_i64();
9595         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9596         TCGv_i64 tcg_res = tcg_temp_new_i64();
9597         NeonGenTwoDoubleOpFn *genfn;
9598         bool swap = false;
9599         int pass;
9600 
9601         switch (opcode) {
9602         case 0x2e: /* FCMLT (zero) */
9603             swap = true;
9604             /* fallthrough */
9605         case 0x2c: /* FCMGT (zero) */
9606             genfn = gen_helper_neon_cgt_f64;
9607             break;
9608         case 0x2d: /* FCMEQ (zero) */
9609             genfn = gen_helper_neon_ceq_f64;
9610             break;
9611         case 0x6d: /* FCMLE (zero) */
9612             swap = true;
9613             /* fall through */
9614         case 0x6c: /* FCMGE (zero) */
9615             genfn = gen_helper_neon_cge_f64;
9616             break;
9617         default:
9618             g_assert_not_reached();
9619         }
9620 
9621         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9622             read_vec_element(s, tcg_op, rn, pass, MO_64);
9623             if (swap) {
9624                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9625             } else {
9626                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9627             }
9628             write_vec_element(s, tcg_res, rd, pass, MO_64);
9629         }
9630 
9631         clear_vec_high(s, !is_scalar, rd);
9632     } else {
9633         TCGv_i32 tcg_op = tcg_temp_new_i32();
9634         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9635         TCGv_i32 tcg_res = tcg_temp_new_i32();
9636         NeonGenTwoSingleOpFn *genfn;
9637         bool swap = false;
9638         int pass, maxpasses;
9639 
9640         if (size == MO_16) {
9641             switch (opcode) {
9642             case 0x2e: /* FCMLT (zero) */
9643                 swap = true;
9644                 /* fall through */
9645             case 0x2c: /* FCMGT (zero) */
9646                 genfn = gen_helper_advsimd_cgt_f16;
9647                 break;
9648             case 0x2d: /* FCMEQ (zero) */
9649                 genfn = gen_helper_advsimd_ceq_f16;
9650                 break;
9651             case 0x6d: /* FCMLE (zero) */
9652                 swap = true;
9653                 /* fall through */
9654             case 0x6c: /* FCMGE (zero) */
9655                 genfn = gen_helper_advsimd_cge_f16;
9656                 break;
9657             default:
9658                 g_assert_not_reached();
9659             }
9660         } else {
9661             switch (opcode) {
9662             case 0x2e: /* FCMLT (zero) */
9663                 swap = true;
9664                 /* fall through */
9665             case 0x2c: /* FCMGT (zero) */
9666                 genfn = gen_helper_neon_cgt_f32;
9667                 break;
9668             case 0x2d: /* FCMEQ (zero) */
9669                 genfn = gen_helper_neon_ceq_f32;
9670                 break;
9671             case 0x6d: /* FCMLE (zero) */
9672                 swap = true;
9673                 /* fall through */
9674             case 0x6c: /* FCMGE (zero) */
9675                 genfn = gen_helper_neon_cge_f32;
9676                 break;
9677             default:
9678                 g_assert_not_reached();
9679             }
9680         }
9681 
9682         if (is_scalar) {
9683             maxpasses = 1;
9684         } else {
9685             int vector_size = 8 << is_q;
9686             maxpasses = vector_size >> size;
9687         }
9688 
9689         for (pass = 0; pass < maxpasses; pass++) {
9690             read_vec_element_i32(s, tcg_op, rn, pass, size);
9691             if (swap) {
9692                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9693             } else {
9694                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9695             }
9696             if (is_scalar) {
9697                 write_fp_sreg(s, rd, tcg_res);
9698             } else {
9699                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9700             }
9701         }
9702 
9703         if (!is_scalar) {
9704             clear_vec_high(s, is_q, rd);
9705         }
9706     }
9707 }
9708 
9709 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9710                                     bool is_scalar, bool is_u, bool is_q,
9711                                     int size, int rn, int rd)
9712 {
9713     bool is_double = (size == 3);
9714     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9715 
9716     if (is_double) {
9717         TCGv_i64 tcg_op = tcg_temp_new_i64();
9718         TCGv_i64 tcg_res = tcg_temp_new_i64();
9719         int pass;
9720 
9721         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9722             read_vec_element(s, tcg_op, rn, pass, MO_64);
9723             switch (opcode) {
9724             case 0x3d: /* FRECPE */
9725                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9726                 break;
9727             case 0x3f: /* FRECPX */
9728                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9729                 break;
9730             case 0x7d: /* FRSQRTE */
9731                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9732                 break;
9733             default:
9734                 g_assert_not_reached();
9735             }
9736             write_vec_element(s, tcg_res, rd, pass, MO_64);
9737         }
9738         clear_vec_high(s, !is_scalar, rd);
9739     } else {
9740         TCGv_i32 tcg_op = tcg_temp_new_i32();
9741         TCGv_i32 tcg_res = tcg_temp_new_i32();
9742         int pass, maxpasses;
9743 
9744         if (is_scalar) {
9745             maxpasses = 1;
9746         } else {
9747             maxpasses = is_q ? 4 : 2;
9748         }
9749 
9750         for (pass = 0; pass < maxpasses; pass++) {
9751             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9752 
9753             switch (opcode) {
9754             case 0x3c: /* URECPE */
9755                 gen_helper_recpe_u32(tcg_res, tcg_op);
9756                 break;
9757             case 0x3d: /* FRECPE */
9758                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9759                 break;
9760             case 0x3f: /* FRECPX */
9761                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9762                 break;
9763             case 0x7d: /* FRSQRTE */
9764                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9765                 break;
9766             default:
9767                 g_assert_not_reached();
9768             }
9769 
9770             if (is_scalar) {
9771                 write_fp_sreg(s, rd, tcg_res);
9772             } else {
9773                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9774             }
9775         }
9776         if (!is_scalar) {
9777             clear_vec_high(s, is_q, rd);
9778         }
9779     }
9780 }
9781 
9782 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9783                                 int opcode, bool u, bool is_q,
9784                                 int size, int rn, int rd)
9785 {
9786     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9787      * in the source becomes a size element in the destination).
9788      */
9789     int pass;
9790     TCGv_i32 tcg_res[2];
9791     int destelt = is_q ? 2 : 0;
9792     int passes = scalar ? 1 : 2;
9793 
9794     if (scalar) {
9795         tcg_res[1] = tcg_constant_i32(0);
9796     }
9797 
9798     for (pass = 0; pass < passes; pass++) {
9799         TCGv_i64 tcg_op = tcg_temp_new_i64();
9800         NeonGenNarrowFn *genfn = NULL;
9801         NeonGenNarrowEnvFn *genenvfn = NULL;
9802 
9803         if (scalar) {
9804             read_vec_element(s, tcg_op, rn, pass, size + 1);
9805         } else {
9806             read_vec_element(s, tcg_op, rn, pass, MO_64);
9807         }
9808         tcg_res[pass] = tcg_temp_new_i32();
9809 
9810         switch (opcode) {
9811         case 0x12: /* XTN, SQXTUN */
9812         {
9813             static NeonGenNarrowFn * const xtnfns[3] = {
9814                 gen_helper_neon_narrow_u8,
9815                 gen_helper_neon_narrow_u16,
9816                 tcg_gen_extrl_i64_i32,
9817             };
9818             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9819                 gen_helper_neon_unarrow_sat8,
9820                 gen_helper_neon_unarrow_sat16,
9821                 gen_helper_neon_unarrow_sat32,
9822             };
9823             if (u) {
9824                 genenvfn = sqxtunfns[size];
9825             } else {
9826                 genfn = xtnfns[size];
9827             }
9828             break;
9829         }
9830         case 0x14: /* SQXTN, UQXTN */
9831         {
9832             static NeonGenNarrowEnvFn * const fns[3][2] = {
9833                 { gen_helper_neon_narrow_sat_s8,
9834                   gen_helper_neon_narrow_sat_u8 },
9835                 { gen_helper_neon_narrow_sat_s16,
9836                   gen_helper_neon_narrow_sat_u16 },
9837                 { gen_helper_neon_narrow_sat_s32,
9838                   gen_helper_neon_narrow_sat_u32 },
9839             };
9840             genenvfn = fns[size][u];
9841             break;
9842         }
9843         case 0x16: /* FCVTN, FCVTN2 */
9844             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9845             if (size == 2) {
9846                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9847             } else {
9848                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9849                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9850                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9851                 TCGv_i32 ahp = get_ahp_flag();
9852 
9853                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9854                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9855                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9856                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9857             }
9858             break;
9859         case 0x36: /* BFCVTN, BFCVTN2 */
9860             {
9861                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9862                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9863             }
9864             break;
9865         case 0x56:  /* FCVTXN, FCVTXN2 */
9866             /* 64 bit to 32 bit float conversion
9867              * with von Neumann rounding (round to odd)
9868              */
9869             assert(size == 2);
9870             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
9871             break;
9872         default:
9873             g_assert_not_reached();
9874         }
9875 
9876         if (genfn) {
9877             genfn(tcg_res[pass], tcg_op);
9878         } else if (genenvfn) {
9879             genenvfn(tcg_res[pass], tcg_env, tcg_op);
9880         }
9881     }
9882 
9883     for (pass = 0; pass < 2; pass++) {
9884         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9885     }
9886     clear_vec_high(s, is_q, rd);
9887 }
9888 
9889 /* Remaining saturating accumulating ops */
9890 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9891                                 bool is_q, int size, int rn, int rd)
9892 {
9893     bool is_double = (size == 3);
9894 
9895     if (is_double) {
9896         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9897         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9898         int pass;
9899 
9900         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9901             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9902             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9903 
9904             if (is_u) { /* USQADD */
9905                 gen_helper_neon_uqadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9906             } else { /* SUQADD */
9907                 gen_helper_neon_sqadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9908             }
9909             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9910         }
9911         clear_vec_high(s, !is_scalar, rd);
9912     } else {
9913         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9914         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9915         int pass, maxpasses;
9916 
9917         if (is_scalar) {
9918             maxpasses = 1;
9919         } else {
9920             maxpasses = is_q ? 4 : 2;
9921         }
9922 
9923         for (pass = 0; pass < maxpasses; pass++) {
9924             if (is_scalar) {
9925                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9926                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9927             } else {
9928                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9929                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9930             }
9931 
9932             if (is_u) { /* USQADD */
9933                 switch (size) {
9934                 case 0:
9935                     gen_helper_neon_uqadd_s8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9936                     break;
9937                 case 1:
9938                     gen_helper_neon_uqadd_s16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9939                     break;
9940                 case 2:
9941                     gen_helper_neon_uqadd_s32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9942                     break;
9943                 default:
9944                     g_assert_not_reached();
9945                 }
9946             } else { /* SUQADD */
9947                 switch (size) {
9948                 case 0:
9949                     gen_helper_neon_sqadd_u8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9950                     break;
9951                 case 1:
9952                     gen_helper_neon_sqadd_u16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9953                     break;
9954                 case 2:
9955                     gen_helper_neon_sqadd_u32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9956                     break;
9957                 default:
9958                     g_assert_not_reached();
9959                 }
9960             }
9961 
9962             if (is_scalar) {
9963                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9964             }
9965             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9966         }
9967         clear_vec_high(s, is_q, rd);
9968     }
9969 }
9970 
9971 /* AdvSIMD scalar two reg misc
9972  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9973  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9974  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9975  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9976  */
9977 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9978 {
9979     int rd = extract32(insn, 0, 5);
9980     int rn = extract32(insn, 5, 5);
9981     int opcode = extract32(insn, 12, 5);
9982     int size = extract32(insn, 22, 2);
9983     bool u = extract32(insn, 29, 1);
9984     bool is_fcvt = false;
9985     int rmode;
9986     TCGv_i32 tcg_rmode;
9987     TCGv_ptr tcg_fpstatus;
9988 
9989     switch (opcode) {
9990     case 0x3: /* USQADD / SUQADD*/
9991         if (!fp_access_check(s)) {
9992             return;
9993         }
9994         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9995         return;
9996     case 0x7: /* SQABS / SQNEG */
9997         break;
9998     case 0xa: /* CMLT */
9999         if (u) {
10000             unallocated_encoding(s);
10001             return;
10002         }
10003         /* fall through */
10004     case 0x8: /* CMGT, CMGE */
10005     case 0x9: /* CMEQ, CMLE */
10006     case 0xb: /* ABS, NEG */
10007         if (size != 3) {
10008             unallocated_encoding(s);
10009             return;
10010         }
10011         break;
10012     case 0x12: /* SQXTUN */
10013         if (!u) {
10014             unallocated_encoding(s);
10015             return;
10016         }
10017         /* fall through */
10018     case 0x14: /* SQXTN, UQXTN */
10019         if (size == 3) {
10020             unallocated_encoding(s);
10021             return;
10022         }
10023         if (!fp_access_check(s)) {
10024             return;
10025         }
10026         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10027         return;
10028     case 0xc ... 0xf:
10029     case 0x16 ... 0x1d:
10030     case 0x1f:
10031         /* Floating point: U, size[1] and opcode indicate operation;
10032          * size[0] indicates single or double precision.
10033          */
10034         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10035         size = extract32(size, 0, 1) ? 3 : 2;
10036         switch (opcode) {
10037         case 0x2c: /* FCMGT (zero) */
10038         case 0x2d: /* FCMEQ (zero) */
10039         case 0x2e: /* FCMLT (zero) */
10040         case 0x6c: /* FCMGE (zero) */
10041         case 0x6d: /* FCMLE (zero) */
10042             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10043             return;
10044         case 0x1d: /* SCVTF */
10045         case 0x5d: /* UCVTF */
10046         {
10047             bool is_signed = (opcode == 0x1d);
10048             if (!fp_access_check(s)) {
10049                 return;
10050             }
10051             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10052             return;
10053         }
10054         case 0x3d: /* FRECPE */
10055         case 0x3f: /* FRECPX */
10056         case 0x7d: /* FRSQRTE */
10057             if (!fp_access_check(s)) {
10058                 return;
10059             }
10060             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10061             return;
10062         case 0x1a: /* FCVTNS */
10063         case 0x1b: /* FCVTMS */
10064         case 0x3a: /* FCVTPS */
10065         case 0x3b: /* FCVTZS */
10066         case 0x5a: /* FCVTNU */
10067         case 0x5b: /* FCVTMU */
10068         case 0x7a: /* FCVTPU */
10069         case 0x7b: /* FCVTZU */
10070             is_fcvt = true;
10071             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10072             break;
10073         case 0x1c: /* FCVTAS */
10074         case 0x5c: /* FCVTAU */
10075             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10076             is_fcvt = true;
10077             rmode = FPROUNDING_TIEAWAY;
10078             break;
10079         case 0x56: /* FCVTXN, FCVTXN2 */
10080             if (size == 2) {
10081                 unallocated_encoding(s);
10082                 return;
10083             }
10084             if (!fp_access_check(s)) {
10085                 return;
10086             }
10087             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10088             return;
10089         default:
10090             unallocated_encoding(s);
10091             return;
10092         }
10093         break;
10094     default:
10095         unallocated_encoding(s);
10096         return;
10097     }
10098 
10099     if (!fp_access_check(s)) {
10100         return;
10101     }
10102 
10103     if (is_fcvt) {
10104         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10105         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10106     } else {
10107         tcg_fpstatus = NULL;
10108         tcg_rmode = NULL;
10109     }
10110 
10111     if (size == 3) {
10112         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10113         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10114 
10115         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10116         write_fp_dreg(s, rd, tcg_rd);
10117     } else {
10118         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10119         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10120 
10121         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10122 
10123         switch (opcode) {
10124         case 0x7: /* SQABS, SQNEG */
10125         {
10126             NeonGenOneOpEnvFn *genfn;
10127             static NeonGenOneOpEnvFn * const fns[3][2] = {
10128                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10129                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10130                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10131             };
10132             genfn = fns[size][u];
10133             genfn(tcg_rd, tcg_env, tcg_rn);
10134             break;
10135         }
10136         case 0x1a: /* FCVTNS */
10137         case 0x1b: /* FCVTMS */
10138         case 0x1c: /* FCVTAS */
10139         case 0x3a: /* FCVTPS */
10140         case 0x3b: /* FCVTZS */
10141             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10142                                  tcg_fpstatus);
10143             break;
10144         case 0x5a: /* FCVTNU */
10145         case 0x5b: /* FCVTMU */
10146         case 0x5c: /* FCVTAU */
10147         case 0x7a: /* FCVTPU */
10148         case 0x7b: /* FCVTZU */
10149             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10150                                  tcg_fpstatus);
10151             break;
10152         default:
10153             g_assert_not_reached();
10154         }
10155 
10156         write_fp_sreg(s, rd, tcg_rd);
10157     }
10158 
10159     if (is_fcvt) {
10160         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10161     }
10162 }
10163 
10164 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10165 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10166                                  int immh, int immb, int opcode, int rn, int rd)
10167 {
10168     int size = 32 - clz32(immh) - 1;
10169     int immhb = immh << 3 | immb;
10170     int shift = 2 * (8 << size) - immhb;
10171     GVecGen2iFn *gvec_fn;
10172 
10173     if (extract32(immh, 3, 1) && !is_q) {
10174         unallocated_encoding(s);
10175         return;
10176     }
10177     tcg_debug_assert(size <= 3);
10178 
10179     if (!fp_access_check(s)) {
10180         return;
10181     }
10182 
10183     switch (opcode) {
10184     case 0x02: /* SSRA / USRA (accumulate) */
10185         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10186         break;
10187 
10188     case 0x08: /* SRI */
10189         gvec_fn = gen_gvec_sri;
10190         break;
10191 
10192     case 0x00: /* SSHR / USHR */
10193         if (is_u) {
10194             if (shift == 8 << size) {
10195                 /* Shift count the same size as element size produces zero.  */
10196                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10197                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10198                 return;
10199             }
10200             gvec_fn = tcg_gen_gvec_shri;
10201         } else {
10202             /* Shift count the same size as element size produces all sign.  */
10203             if (shift == 8 << size) {
10204                 shift -= 1;
10205             }
10206             gvec_fn = tcg_gen_gvec_sari;
10207         }
10208         break;
10209 
10210     case 0x04: /* SRSHR / URSHR (rounding) */
10211         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10212         break;
10213 
10214     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10215         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10216         break;
10217 
10218     default:
10219         g_assert_not_reached();
10220     }
10221 
10222     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10223 }
10224 
10225 /* SHL/SLI - Vector shift left */
10226 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10227                                  int immh, int immb, int opcode, int rn, int rd)
10228 {
10229     int size = 32 - clz32(immh) - 1;
10230     int immhb = immh << 3 | immb;
10231     int shift = immhb - (8 << size);
10232 
10233     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10234     assert(size >= 0 && size <= 3);
10235 
10236     if (extract32(immh, 3, 1) && !is_q) {
10237         unallocated_encoding(s);
10238         return;
10239     }
10240 
10241     if (!fp_access_check(s)) {
10242         return;
10243     }
10244 
10245     if (insert) {
10246         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10247     } else {
10248         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10249     }
10250 }
10251 
10252 /* USHLL/SHLL - Vector shift left with widening */
10253 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10254                                  int immh, int immb, int opcode, int rn, int rd)
10255 {
10256     int size = 32 - clz32(immh) - 1;
10257     int immhb = immh << 3 | immb;
10258     int shift = immhb - (8 << size);
10259     int dsize = 64;
10260     int esize = 8 << size;
10261     int elements = dsize/esize;
10262     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10263     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10264     int i;
10265 
10266     if (size >= 3) {
10267         unallocated_encoding(s);
10268         return;
10269     }
10270 
10271     if (!fp_access_check(s)) {
10272         return;
10273     }
10274 
10275     /* For the LL variants the store is larger than the load,
10276      * so if rd == rn we would overwrite parts of our input.
10277      * So load everything right now and use shifts in the main loop.
10278      */
10279     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10280 
10281     for (i = 0; i < elements; i++) {
10282         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10283         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10284         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10285         write_vec_element(s, tcg_rd, rd, i, size + 1);
10286     }
10287 }
10288 
10289 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10290 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10291                                  int immh, int immb, int opcode, int rn, int rd)
10292 {
10293     int immhb = immh << 3 | immb;
10294     int size = 32 - clz32(immh) - 1;
10295     int dsize = 64;
10296     int esize = 8 << size;
10297     int elements = dsize/esize;
10298     int shift = (2 * esize) - immhb;
10299     bool round = extract32(opcode, 0, 1);
10300     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10301     TCGv_i64 tcg_round;
10302     int i;
10303 
10304     if (extract32(immh, 3, 1)) {
10305         unallocated_encoding(s);
10306         return;
10307     }
10308 
10309     if (!fp_access_check(s)) {
10310         return;
10311     }
10312 
10313     tcg_rn = tcg_temp_new_i64();
10314     tcg_rd = tcg_temp_new_i64();
10315     tcg_final = tcg_temp_new_i64();
10316     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10317 
10318     if (round) {
10319         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10320     } else {
10321         tcg_round = NULL;
10322     }
10323 
10324     for (i = 0; i < elements; i++) {
10325         read_vec_element(s, tcg_rn, rn, i, size+1);
10326         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10327                                 false, true, size+1, shift);
10328 
10329         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10330     }
10331 
10332     if (!is_q) {
10333         write_vec_element(s, tcg_final, rd, 0, MO_64);
10334     } else {
10335         write_vec_element(s, tcg_final, rd, 1, MO_64);
10336     }
10337 
10338     clear_vec_high(s, is_q, rd);
10339 }
10340 
10341 
10342 /* AdvSIMD shift by immediate
10343  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10344  * +---+---+---+-------------+------+------+--------+---+------+------+
10345  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10346  * +---+---+---+-------------+------+------+--------+---+------+------+
10347  */
10348 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10349 {
10350     int rd = extract32(insn, 0, 5);
10351     int rn = extract32(insn, 5, 5);
10352     int opcode = extract32(insn, 11, 5);
10353     int immb = extract32(insn, 16, 3);
10354     int immh = extract32(insn, 19, 4);
10355     bool is_u = extract32(insn, 29, 1);
10356     bool is_q = extract32(insn, 30, 1);
10357 
10358     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10359     assert(immh != 0);
10360 
10361     switch (opcode) {
10362     case 0x08: /* SRI */
10363         if (!is_u) {
10364             unallocated_encoding(s);
10365             return;
10366         }
10367         /* fall through */
10368     case 0x00: /* SSHR / USHR */
10369     case 0x02: /* SSRA / USRA (accumulate) */
10370     case 0x04: /* SRSHR / URSHR (rounding) */
10371     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10372         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10373         break;
10374     case 0x0a: /* SHL / SLI */
10375         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10376         break;
10377     case 0x10: /* SHRN */
10378     case 0x11: /* RSHRN / SQRSHRUN */
10379         if (is_u) {
10380             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10381                                    opcode, rn, rd);
10382         } else {
10383             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10384         }
10385         break;
10386     case 0x12: /* SQSHRN / UQSHRN */
10387     case 0x13: /* SQRSHRN / UQRSHRN */
10388         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10389                                opcode, rn, rd);
10390         break;
10391     case 0x14: /* SSHLL / USHLL */
10392         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10393         break;
10394     case 0x1c: /* SCVTF / UCVTF */
10395         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10396                                      opcode, rn, rd);
10397         break;
10398     case 0xc: /* SQSHLU */
10399         if (!is_u) {
10400             unallocated_encoding(s);
10401             return;
10402         }
10403         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10404         break;
10405     case 0xe: /* SQSHL, UQSHL */
10406         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10407         break;
10408     case 0x1f: /* FCVTZS/ FCVTZU */
10409         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10410         return;
10411     default:
10412         unallocated_encoding(s);
10413         return;
10414     }
10415 }
10416 
10417 /* Generate code to do a "long" addition or subtraction, ie one done in
10418  * TCGv_i64 on vector lanes twice the width specified by size.
10419  */
10420 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10421                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10422 {
10423     static NeonGenTwo64OpFn * const fns[3][2] = {
10424         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10425         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10426         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10427     };
10428     NeonGenTwo64OpFn *genfn;
10429     assert(size < 3);
10430 
10431     genfn = fns[size][is_sub];
10432     genfn(tcg_res, tcg_op1, tcg_op2);
10433 }
10434 
10435 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10436                                 int opcode, int rd, int rn, int rm)
10437 {
10438     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10439     TCGv_i64 tcg_res[2];
10440     int pass, accop;
10441 
10442     tcg_res[0] = tcg_temp_new_i64();
10443     tcg_res[1] = tcg_temp_new_i64();
10444 
10445     /* Does this op do an adding accumulate, a subtracting accumulate,
10446      * or no accumulate at all?
10447      */
10448     switch (opcode) {
10449     case 5:
10450     case 8:
10451     case 9:
10452         accop = 1;
10453         break;
10454     case 10:
10455     case 11:
10456         accop = -1;
10457         break;
10458     default:
10459         accop = 0;
10460         break;
10461     }
10462 
10463     if (accop != 0) {
10464         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10465         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10466     }
10467 
10468     /* size == 2 means two 32x32->64 operations; this is worth special
10469      * casing because we can generally handle it inline.
10470      */
10471     if (size == 2) {
10472         for (pass = 0; pass < 2; pass++) {
10473             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10474             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10475             TCGv_i64 tcg_passres;
10476             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10477 
10478             int elt = pass + is_q * 2;
10479 
10480             read_vec_element(s, tcg_op1, rn, elt, memop);
10481             read_vec_element(s, tcg_op2, rm, elt, memop);
10482 
10483             if (accop == 0) {
10484                 tcg_passres = tcg_res[pass];
10485             } else {
10486                 tcg_passres = tcg_temp_new_i64();
10487             }
10488 
10489             switch (opcode) {
10490             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10491                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10492                 break;
10493             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10494                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10495                 break;
10496             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10497             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10498             {
10499                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10500                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10501 
10502                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10503                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10504                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10505                                     tcg_passres,
10506                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10507                 break;
10508             }
10509             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10510             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10511             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10512                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10513                 break;
10514             case 9: /* SQDMLAL, SQDMLAL2 */
10515             case 11: /* SQDMLSL, SQDMLSL2 */
10516             case 13: /* SQDMULL, SQDMULL2 */
10517                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10518                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10519                                                   tcg_passres, tcg_passres);
10520                 break;
10521             default:
10522                 g_assert_not_reached();
10523             }
10524 
10525             if (opcode == 9 || opcode == 11) {
10526                 /* saturating accumulate ops */
10527                 if (accop < 0) {
10528                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10529                 }
10530                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10531                                                   tcg_res[pass], tcg_passres);
10532             } else if (accop > 0) {
10533                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10534             } else if (accop < 0) {
10535                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10536             }
10537         }
10538     } else {
10539         /* size 0 or 1, generally helper functions */
10540         for (pass = 0; pass < 2; pass++) {
10541             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10542             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10543             TCGv_i64 tcg_passres;
10544             int elt = pass + is_q * 2;
10545 
10546             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10547             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10548 
10549             if (accop == 0) {
10550                 tcg_passres = tcg_res[pass];
10551             } else {
10552                 tcg_passres = tcg_temp_new_i64();
10553             }
10554 
10555             switch (opcode) {
10556             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10557             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10558             {
10559                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10560                 static NeonGenWidenFn * const widenfns[2][2] = {
10561                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10562                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10563                 };
10564                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10565 
10566                 widenfn(tcg_op2_64, tcg_op2);
10567                 widenfn(tcg_passres, tcg_op1);
10568                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10569                               tcg_passres, tcg_op2_64);
10570                 break;
10571             }
10572             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10573             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10574                 if (size == 0) {
10575                     if (is_u) {
10576                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10577                     } else {
10578                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10579                     }
10580                 } else {
10581                     if (is_u) {
10582                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10583                     } else {
10584                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10585                     }
10586                 }
10587                 break;
10588             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10589             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10590             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10591                 if (size == 0) {
10592                     if (is_u) {
10593                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10594                     } else {
10595                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10596                     }
10597                 } else {
10598                     if (is_u) {
10599                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10600                     } else {
10601                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10602                     }
10603                 }
10604                 break;
10605             case 9: /* SQDMLAL, SQDMLAL2 */
10606             case 11: /* SQDMLSL, SQDMLSL2 */
10607             case 13: /* SQDMULL, SQDMULL2 */
10608                 assert(size == 1);
10609                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10610                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10611                                                   tcg_passres, tcg_passres);
10612                 break;
10613             default:
10614                 g_assert_not_reached();
10615             }
10616 
10617             if (accop != 0) {
10618                 if (opcode == 9 || opcode == 11) {
10619                     /* saturating accumulate ops */
10620                     if (accop < 0) {
10621                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10622                     }
10623                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10624                                                       tcg_res[pass],
10625                                                       tcg_passres);
10626                 } else {
10627                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10628                                   tcg_res[pass], tcg_passres);
10629                 }
10630             }
10631         }
10632     }
10633 
10634     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10635     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10636 }
10637 
10638 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10639                             int opcode, int rd, int rn, int rm)
10640 {
10641     TCGv_i64 tcg_res[2];
10642     int part = is_q ? 2 : 0;
10643     int pass;
10644 
10645     for (pass = 0; pass < 2; pass++) {
10646         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10647         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10648         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10649         static NeonGenWidenFn * const widenfns[3][2] = {
10650             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10651             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10652             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10653         };
10654         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10655 
10656         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10657         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10658         widenfn(tcg_op2_wide, tcg_op2);
10659         tcg_res[pass] = tcg_temp_new_i64();
10660         gen_neon_addl(size, (opcode == 3),
10661                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10662     }
10663 
10664     for (pass = 0; pass < 2; pass++) {
10665         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10666     }
10667 }
10668 
10669 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10670 {
10671     tcg_gen_addi_i64(in, in, 1U << 31);
10672     tcg_gen_extrh_i64_i32(res, in);
10673 }
10674 
10675 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10676                                  int opcode, int rd, int rn, int rm)
10677 {
10678     TCGv_i32 tcg_res[2];
10679     int part = is_q ? 2 : 0;
10680     int pass;
10681 
10682     for (pass = 0; pass < 2; pass++) {
10683         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10684         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10685         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10686         static NeonGenNarrowFn * const narrowfns[3][2] = {
10687             { gen_helper_neon_narrow_high_u8,
10688               gen_helper_neon_narrow_round_high_u8 },
10689             { gen_helper_neon_narrow_high_u16,
10690               gen_helper_neon_narrow_round_high_u16 },
10691             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10692         };
10693         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10694 
10695         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10696         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10697 
10698         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10699 
10700         tcg_res[pass] = tcg_temp_new_i32();
10701         gennarrow(tcg_res[pass], tcg_wideres);
10702     }
10703 
10704     for (pass = 0; pass < 2; pass++) {
10705         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10706     }
10707     clear_vec_high(s, is_q, rd);
10708 }
10709 
10710 /* AdvSIMD three different
10711  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10712  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10713  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10714  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10715  */
10716 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10717 {
10718     /* Instructions in this group fall into three basic classes
10719      * (in each case with the operation working on each element in
10720      * the input vectors):
10721      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10722      *     128 bit input)
10723      * (2) wide 64 x 128 -> 128
10724      * (3) narrowing 128 x 128 -> 64
10725      * Here we do initial decode, catch unallocated cases and
10726      * dispatch to separate functions for each class.
10727      */
10728     int is_q = extract32(insn, 30, 1);
10729     int is_u = extract32(insn, 29, 1);
10730     int size = extract32(insn, 22, 2);
10731     int opcode = extract32(insn, 12, 4);
10732     int rm = extract32(insn, 16, 5);
10733     int rn = extract32(insn, 5, 5);
10734     int rd = extract32(insn, 0, 5);
10735 
10736     switch (opcode) {
10737     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10738     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10739         /* 64 x 128 -> 128 */
10740         if (size == 3) {
10741             unallocated_encoding(s);
10742             return;
10743         }
10744         if (!fp_access_check(s)) {
10745             return;
10746         }
10747         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10748         break;
10749     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10750     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10751         /* 128 x 128 -> 64 */
10752         if (size == 3) {
10753             unallocated_encoding(s);
10754             return;
10755         }
10756         if (!fp_access_check(s)) {
10757             return;
10758         }
10759         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10760         break;
10761     case 14: /* PMULL, PMULL2 */
10762         if (is_u) {
10763             unallocated_encoding(s);
10764             return;
10765         }
10766         switch (size) {
10767         case 0: /* PMULL.P8 */
10768             if (!fp_access_check(s)) {
10769                 return;
10770             }
10771             /* The Q field specifies lo/hi half input for this insn.  */
10772             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10773                              gen_helper_neon_pmull_h);
10774             break;
10775 
10776         case 3: /* PMULL.P64 */
10777             if (!dc_isar_feature(aa64_pmull, s)) {
10778                 unallocated_encoding(s);
10779                 return;
10780             }
10781             if (!fp_access_check(s)) {
10782                 return;
10783             }
10784             /* The Q field specifies lo/hi half input for this insn.  */
10785             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10786                              gen_helper_gvec_pmull_q);
10787             break;
10788 
10789         default:
10790             unallocated_encoding(s);
10791             break;
10792         }
10793         return;
10794     case 9: /* SQDMLAL, SQDMLAL2 */
10795     case 11: /* SQDMLSL, SQDMLSL2 */
10796     case 13: /* SQDMULL, SQDMULL2 */
10797         if (is_u || size == 0) {
10798             unallocated_encoding(s);
10799             return;
10800         }
10801         /* fall through */
10802     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10803     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10804     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10805     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10806     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10807     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10808     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10809         /* 64 x 64 -> 128 */
10810         if (size == 3) {
10811             unallocated_encoding(s);
10812             return;
10813         }
10814         if (!fp_access_check(s)) {
10815             return;
10816         }
10817 
10818         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10819         break;
10820     default:
10821         /* opcode 15 not allocated */
10822         unallocated_encoding(s);
10823         break;
10824     }
10825 }
10826 
10827 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10828 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10829 {
10830     int rd = extract32(insn, 0, 5);
10831     int rn = extract32(insn, 5, 5);
10832     int rm = extract32(insn, 16, 5);
10833     int size = extract32(insn, 22, 2);
10834     bool is_u = extract32(insn, 29, 1);
10835     bool is_q = extract32(insn, 30, 1);
10836 
10837     if (!fp_access_check(s)) {
10838         return;
10839     }
10840 
10841     switch (size + 4 * is_u) {
10842     case 0: /* AND */
10843         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10844         return;
10845     case 1: /* BIC */
10846         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10847         return;
10848     case 2: /* ORR */
10849         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10850         return;
10851     case 3: /* ORN */
10852         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10853         return;
10854     case 4: /* EOR */
10855         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10856         return;
10857 
10858     case 5: /* BSL bitwise select */
10859         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10860         return;
10861     case 6: /* BIT, bitwise insert if true */
10862         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10863         return;
10864     case 7: /* BIF, bitwise insert if false */
10865         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10866         return;
10867 
10868     default:
10869         g_assert_not_reached();
10870     }
10871 }
10872 
10873 /* Pairwise op subgroup of C3.6.16.
10874  *
10875  * This is called directly or via the handle_3same_float for float pairwise
10876  * operations where the opcode and size are calculated differently.
10877  */
10878 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10879                                    int size, int rn, int rm, int rd)
10880 {
10881     TCGv_ptr fpst;
10882     int pass;
10883 
10884     /* Floating point operations need fpst */
10885     if (opcode >= 0x58) {
10886         fpst = fpstatus_ptr(FPST_FPCR);
10887     } else {
10888         fpst = NULL;
10889     }
10890 
10891     if (!fp_access_check(s)) {
10892         return;
10893     }
10894 
10895     /* These operations work on the concatenated rm:rn, with each pair of
10896      * adjacent elements being operated on to produce an element in the result.
10897      */
10898     if (size == 3) {
10899         TCGv_i64 tcg_res[2];
10900 
10901         for (pass = 0; pass < 2; pass++) {
10902             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10903             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10904             int passreg = (pass == 0) ? rn : rm;
10905 
10906             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10907             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10908             tcg_res[pass] = tcg_temp_new_i64();
10909 
10910             switch (opcode) {
10911             case 0x17: /* ADDP */
10912                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10913                 break;
10914             case 0x58: /* FMAXNMP */
10915                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10916                 break;
10917             case 0x5a: /* FADDP */
10918                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10919                 break;
10920             case 0x5e: /* FMAXP */
10921                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10922                 break;
10923             case 0x78: /* FMINNMP */
10924                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10925                 break;
10926             case 0x7e: /* FMINP */
10927                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10928                 break;
10929             default:
10930                 g_assert_not_reached();
10931             }
10932         }
10933 
10934         for (pass = 0; pass < 2; pass++) {
10935             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10936         }
10937     } else {
10938         int maxpass = is_q ? 4 : 2;
10939         TCGv_i32 tcg_res[4];
10940 
10941         for (pass = 0; pass < maxpass; pass++) {
10942             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10943             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10944             NeonGenTwoOpFn *genfn = NULL;
10945             int passreg = pass < (maxpass / 2) ? rn : rm;
10946             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10947 
10948             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10949             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10950             tcg_res[pass] = tcg_temp_new_i32();
10951 
10952             switch (opcode) {
10953             case 0x17: /* ADDP */
10954             {
10955                 static NeonGenTwoOpFn * const fns[3] = {
10956                     gen_helper_neon_padd_u8,
10957                     gen_helper_neon_padd_u16,
10958                     tcg_gen_add_i32,
10959                 };
10960                 genfn = fns[size];
10961                 break;
10962             }
10963             case 0x14: /* SMAXP, UMAXP */
10964             {
10965                 static NeonGenTwoOpFn * const fns[3][2] = {
10966                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10967                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10968                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10969                 };
10970                 genfn = fns[size][u];
10971                 break;
10972             }
10973             case 0x15: /* SMINP, UMINP */
10974             {
10975                 static NeonGenTwoOpFn * const fns[3][2] = {
10976                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10977                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10978                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10979                 };
10980                 genfn = fns[size][u];
10981                 break;
10982             }
10983             /* The FP operations are all on single floats (32 bit) */
10984             case 0x58: /* FMAXNMP */
10985                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10986                 break;
10987             case 0x5a: /* FADDP */
10988                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10989                 break;
10990             case 0x5e: /* FMAXP */
10991                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10992                 break;
10993             case 0x78: /* FMINNMP */
10994                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10995                 break;
10996             case 0x7e: /* FMINP */
10997                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10998                 break;
10999             default:
11000                 g_assert_not_reached();
11001             }
11002 
11003             /* FP ops called directly, otherwise call now */
11004             if (genfn) {
11005                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11006             }
11007         }
11008 
11009         for (pass = 0; pass < maxpass; pass++) {
11010             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11011         }
11012         clear_vec_high(s, is_q, rd);
11013     }
11014 }
11015 
11016 /* Floating point op subgroup of C3.6.16. */
11017 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11018 {
11019     /* For floating point ops, the U, size[1] and opcode bits
11020      * together indicate the operation. size[0] indicates single
11021      * or double.
11022      */
11023     int fpopcode = extract32(insn, 11, 5)
11024         | (extract32(insn, 23, 1) << 5)
11025         | (extract32(insn, 29, 1) << 6);
11026     int is_q = extract32(insn, 30, 1);
11027     int size = extract32(insn, 22, 1);
11028     int rm = extract32(insn, 16, 5);
11029     int rn = extract32(insn, 5, 5);
11030     int rd = extract32(insn, 0, 5);
11031 
11032     int datasize = is_q ? 128 : 64;
11033     int esize = 32 << size;
11034     int elements = datasize / esize;
11035 
11036     if (size == 1 && !is_q) {
11037         unallocated_encoding(s);
11038         return;
11039     }
11040 
11041     switch (fpopcode) {
11042     case 0x58: /* FMAXNMP */
11043     case 0x5a: /* FADDP */
11044     case 0x5e: /* FMAXP */
11045     case 0x78: /* FMINNMP */
11046     case 0x7e: /* FMINP */
11047         if (size && !is_q) {
11048             unallocated_encoding(s);
11049             return;
11050         }
11051         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11052                                rn, rm, rd);
11053         return;
11054     case 0x1b: /* FMULX */
11055     case 0x1f: /* FRECPS */
11056     case 0x3f: /* FRSQRTS */
11057     case 0x5d: /* FACGE */
11058     case 0x7d: /* FACGT */
11059     case 0x19: /* FMLA */
11060     case 0x39: /* FMLS */
11061     case 0x18: /* FMAXNM */
11062     case 0x1a: /* FADD */
11063     case 0x1c: /* FCMEQ */
11064     case 0x1e: /* FMAX */
11065     case 0x38: /* FMINNM */
11066     case 0x3a: /* FSUB */
11067     case 0x3e: /* FMIN */
11068     case 0x5b: /* FMUL */
11069     case 0x5c: /* FCMGE */
11070     case 0x5f: /* FDIV */
11071     case 0x7a: /* FABD */
11072     case 0x7c: /* FCMGT */
11073         if (!fp_access_check(s)) {
11074             return;
11075         }
11076         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11077         return;
11078 
11079     case 0x1d: /* FMLAL  */
11080     case 0x3d: /* FMLSL  */
11081     case 0x59: /* FMLAL2 */
11082     case 0x79: /* FMLSL2 */
11083         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11084             unallocated_encoding(s);
11085             return;
11086         }
11087         if (fp_access_check(s)) {
11088             int is_s = extract32(insn, 23, 1);
11089             int is_2 = extract32(insn, 29, 1);
11090             int data = (is_2 << 1) | is_s;
11091             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11092                                vec_full_reg_offset(s, rn),
11093                                vec_full_reg_offset(s, rm), tcg_env,
11094                                is_q ? 16 : 8, vec_full_reg_size(s),
11095                                data, gen_helper_gvec_fmlal_a64);
11096         }
11097         return;
11098 
11099     default:
11100         unallocated_encoding(s);
11101         return;
11102     }
11103 }
11104 
11105 /* Integer op subgroup of C3.6.16. */
11106 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11107 {
11108     int is_q = extract32(insn, 30, 1);
11109     int u = extract32(insn, 29, 1);
11110     int size = extract32(insn, 22, 2);
11111     int opcode = extract32(insn, 11, 5);
11112     int rm = extract32(insn, 16, 5);
11113     int rn = extract32(insn, 5, 5);
11114     int rd = extract32(insn, 0, 5);
11115     int pass;
11116     TCGCond cond;
11117 
11118     switch (opcode) {
11119     case 0x13: /* MUL, PMUL */
11120         if (u && size != 0) {
11121             unallocated_encoding(s);
11122             return;
11123         }
11124         /* fall through */
11125     case 0x0: /* SHADD, UHADD */
11126     case 0x2: /* SRHADD, URHADD */
11127     case 0x4: /* SHSUB, UHSUB */
11128     case 0xc: /* SMAX, UMAX */
11129     case 0xd: /* SMIN, UMIN */
11130     case 0xe: /* SABD, UABD */
11131     case 0xf: /* SABA, UABA */
11132     case 0x12: /* MLA, MLS */
11133         if (size == 3) {
11134             unallocated_encoding(s);
11135             return;
11136         }
11137         break;
11138     case 0x16: /* SQDMULH, SQRDMULH */
11139         if (size == 0 || size == 3) {
11140             unallocated_encoding(s);
11141             return;
11142         }
11143         break;
11144     default:
11145         if (size == 3 && !is_q) {
11146             unallocated_encoding(s);
11147             return;
11148         }
11149         break;
11150     }
11151 
11152     if (!fp_access_check(s)) {
11153         return;
11154     }
11155 
11156     switch (opcode) {
11157     case 0x01: /* SQADD, UQADD */
11158         if (u) {
11159             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11160         } else {
11161             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11162         }
11163         return;
11164     case 0x05: /* SQSUB, UQSUB */
11165         if (u) {
11166             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11167         } else {
11168             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11169         }
11170         return;
11171     case 0x08: /* SSHL, USHL */
11172         if (u) {
11173             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11174         } else {
11175             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11176         }
11177         return;
11178     case 0x0c: /* SMAX, UMAX */
11179         if (u) {
11180             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11181         } else {
11182             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11183         }
11184         return;
11185     case 0x0d: /* SMIN, UMIN */
11186         if (u) {
11187             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11188         } else {
11189             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11190         }
11191         return;
11192     case 0xe: /* SABD, UABD */
11193         if (u) {
11194             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11195         } else {
11196             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11197         }
11198         return;
11199     case 0xf: /* SABA, UABA */
11200         if (u) {
11201             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11202         } else {
11203             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11204         }
11205         return;
11206     case 0x10: /* ADD, SUB */
11207         if (u) {
11208             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11209         } else {
11210             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11211         }
11212         return;
11213     case 0x13: /* MUL, PMUL */
11214         if (!u) { /* MUL */
11215             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11216         } else {  /* PMUL */
11217             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11218         }
11219         return;
11220     case 0x12: /* MLA, MLS */
11221         if (u) {
11222             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11223         } else {
11224             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11225         }
11226         return;
11227     case 0x16: /* SQDMULH, SQRDMULH */
11228         {
11229             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11230                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11231                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11232             };
11233             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11234         }
11235         return;
11236     case 0x11:
11237         if (!u) { /* CMTST */
11238             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11239             return;
11240         }
11241         /* else CMEQ */
11242         cond = TCG_COND_EQ;
11243         goto do_gvec_cmp;
11244     case 0x06: /* CMGT, CMHI */
11245         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11246         goto do_gvec_cmp;
11247     case 0x07: /* CMGE, CMHS */
11248         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11249     do_gvec_cmp:
11250         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11251                          vec_full_reg_offset(s, rn),
11252                          vec_full_reg_offset(s, rm),
11253                          is_q ? 16 : 8, vec_full_reg_size(s));
11254         return;
11255     }
11256 
11257     if (size == 3) {
11258         assert(is_q);
11259         for (pass = 0; pass < 2; pass++) {
11260             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11261             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11262             TCGv_i64 tcg_res = tcg_temp_new_i64();
11263 
11264             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11265             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11266 
11267             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11268 
11269             write_vec_element(s, tcg_res, rd, pass, MO_64);
11270         }
11271     } else {
11272         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11273             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11274             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11275             TCGv_i32 tcg_res = tcg_temp_new_i32();
11276             NeonGenTwoOpFn *genfn = NULL;
11277             NeonGenTwoOpEnvFn *genenvfn = NULL;
11278 
11279             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11280             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11281 
11282             switch (opcode) {
11283             case 0x0: /* SHADD, UHADD */
11284             {
11285                 static NeonGenTwoOpFn * const fns[3][2] = {
11286                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11287                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11288                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11289                 };
11290                 genfn = fns[size][u];
11291                 break;
11292             }
11293             case 0x2: /* SRHADD, URHADD */
11294             {
11295                 static NeonGenTwoOpFn * const fns[3][2] = {
11296                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11297                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11298                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11299                 };
11300                 genfn = fns[size][u];
11301                 break;
11302             }
11303             case 0x4: /* SHSUB, UHSUB */
11304             {
11305                 static NeonGenTwoOpFn * const fns[3][2] = {
11306                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11307                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11308                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11309                 };
11310                 genfn = fns[size][u];
11311                 break;
11312             }
11313             case 0x9: /* SQSHL, UQSHL */
11314             {
11315                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11316                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11317                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11318                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11319                 };
11320                 genenvfn = fns[size][u];
11321                 break;
11322             }
11323             case 0xa: /* SRSHL, URSHL */
11324             {
11325                 static NeonGenTwoOpFn * const fns[3][2] = {
11326                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11327                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11328                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11329                 };
11330                 genfn = fns[size][u];
11331                 break;
11332             }
11333             case 0xb: /* SQRSHL, UQRSHL */
11334             {
11335                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11336                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11337                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11338                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11339                 };
11340                 genenvfn = fns[size][u];
11341                 break;
11342             }
11343             default:
11344                 g_assert_not_reached();
11345             }
11346 
11347             if (genenvfn) {
11348                 genenvfn(tcg_res, tcg_env, tcg_op1, tcg_op2);
11349             } else {
11350                 genfn(tcg_res, tcg_op1, tcg_op2);
11351             }
11352 
11353             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11354         }
11355     }
11356     clear_vec_high(s, is_q, rd);
11357 }
11358 
11359 /* AdvSIMD three same
11360  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11361  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11362  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11363  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11364  */
11365 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11366 {
11367     int opcode = extract32(insn, 11, 5);
11368 
11369     switch (opcode) {
11370     case 0x3: /* logic ops */
11371         disas_simd_3same_logic(s, insn);
11372         break;
11373     case 0x17: /* ADDP */
11374     case 0x14: /* SMAXP, UMAXP */
11375     case 0x15: /* SMINP, UMINP */
11376     {
11377         /* Pairwise operations */
11378         int is_q = extract32(insn, 30, 1);
11379         int u = extract32(insn, 29, 1);
11380         int size = extract32(insn, 22, 2);
11381         int rm = extract32(insn, 16, 5);
11382         int rn = extract32(insn, 5, 5);
11383         int rd = extract32(insn, 0, 5);
11384         if (opcode == 0x17) {
11385             if (u || (size == 3 && !is_q)) {
11386                 unallocated_encoding(s);
11387                 return;
11388             }
11389         } else {
11390             if (size == 3) {
11391                 unallocated_encoding(s);
11392                 return;
11393             }
11394         }
11395         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11396         break;
11397     }
11398     case 0x18 ... 0x31:
11399         /* floating point ops, sz[1] and U are part of opcode */
11400         disas_simd_3same_float(s, insn);
11401         break;
11402     default:
11403         disas_simd_3same_int(s, insn);
11404         break;
11405     }
11406 }
11407 
11408 /*
11409  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11410  *
11411  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11412  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11413  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11414  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11415  *
11416  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11417  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11418  *
11419  */
11420 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11421 {
11422     int opcode = extract32(insn, 11, 3);
11423     int u = extract32(insn, 29, 1);
11424     int a = extract32(insn, 23, 1);
11425     int is_q = extract32(insn, 30, 1);
11426     int rm = extract32(insn, 16, 5);
11427     int rn = extract32(insn, 5, 5);
11428     int rd = extract32(insn, 0, 5);
11429     /*
11430      * For these floating point ops, the U, a and opcode bits
11431      * together indicate the operation.
11432      */
11433     int fpopcode = opcode | (a << 3) | (u << 4);
11434     int datasize = is_q ? 128 : 64;
11435     int elements = datasize / 16;
11436     bool pairwise;
11437     TCGv_ptr fpst;
11438     int pass;
11439 
11440     switch (fpopcode) {
11441     case 0x0: /* FMAXNM */
11442     case 0x1: /* FMLA */
11443     case 0x2: /* FADD */
11444     case 0x3: /* FMULX */
11445     case 0x4: /* FCMEQ */
11446     case 0x6: /* FMAX */
11447     case 0x7: /* FRECPS */
11448     case 0x8: /* FMINNM */
11449     case 0x9: /* FMLS */
11450     case 0xa: /* FSUB */
11451     case 0xe: /* FMIN */
11452     case 0xf: /* FRSQRTS */
11453     case 0x13: /* FMUL */
11454     case 0x14: /* FCMGE */
11455     case 0x15: /* FACGE */
11456     case 0x17: /* FDIV */
11457     case 0x1a: /* FABD */
11458     case 0x1c: /* FCMGT */
11459     case 0x1d: /* FACGT */
11460         pairwise = false;
11461         break;
11462     case 0x10: /* FMAXNMP */
11463     case 0x12: /* FADDP */
11464     case 0x16: /* FMAXP */
11465     case 0x18: /* FMINNMP */
11466     case 0x1e: /* FMINP */
11467         pairwise = true;
11468         break;
11469     default:
11470         unallocated_encoding(s);
11471         return;
11472     }
11473 
11474     if (!dc_isar_feature(aa64_fp16, s)) {
11475         unallocated_encoding(s);
11476         return;
11477     }
11478 
11479     if (!fp_access_check(s)) {
11480         return;
11481     }
11482 
11483     fpst = fpstatus_ptr(FPST_FPCR_F16);
11484 
11485     if (pairwise) {
11486         int maxpass = is_q ? 8 : 4;
11487         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11488         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11489         TCGv_i32 tcg_res[8];
11490 
11491         for (pass = 0; pass < maxpass; pass++) {
11492             int passreg = pass < (maxpass / 2) ? rn : rm;
11493             int passelt = (pass << 1) & (maxpass - 1);
11494 
11495             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11496             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11497             tcg_res[pass] = tcg_temp_new_i32();
11498 
11499             switch (fpopcode) {
11500             case 0x10: /* FMAXNMP */
11501                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11502                                            fpst);
11503                 break;
11504             case 0x12: /* FADDP */
11505                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11506                 break;
11507             case 0x16: /* FMAXP */
11508                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11509                 break;
11510             case 0x18: /* FMINNMP */
11511                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11512                                            fpst);
11513                 break;
11514             case 0x1e: /* FMINP */
11515                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11516                 break;
11517             default:
11518                 g_assert_not_reached();
11519             }
11520         }
11521 
11522         for (pass = 0; pass < maxpass; pass++) {
11523             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11524         }
11525     } else {
11526         for (pass = 0; pass < elements; pass++) {
11527             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11528             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11529             TCGv_i32 tcg_res = tcg_temp_new_i32();
11530 
11531             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11532             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11533 
11534             switch (fpopcode) {
11535             case 0x0: /* FMAXNM */
11536                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11537                 break;
11538             case 0x1: /* FMLA */
11539                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11540                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11541                                            fpst);
11542                 break;
11543             case 0x2: /* FADD */
11544                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11545                 break;
11546             case 0x3: /* FMULX */
11547                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11548                 break;
11549             case 0x4: /* FCMEQ */
11550                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11551                 break;
11552             case 0x6: /* FMAX */
11553                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11554                 break;
11555             case 0x7: /* FRECPS */
11556                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11557                 break;
11558             case 0x8: /* FMINNM */
11559                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11560                 break;
11561             case 0x9: /* FMLS */
11562                 /* As usual for ARM, separate negation for fused multiply-add */
11563                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11564                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11565                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11566                                            fpst);
11567                 break;
11568             case 0xa: /* FSUB */
11569                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11570                 break;
11571             case 0xe: /* FMIN */
11572                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11573                 break;
11574             case 0xf: /* FRSQRTS */
11575                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11576                 break;
11577             case 0x13: /* FMUL */
11578                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11579                 break;
11580             case 0x14: /* FCMGE */
11581                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11582                 break;
11583             case 0x15: /* FACGE */
11584                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11585                 break;
11586             case 0x17: /* FDIV */
11587                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11588                 break;
11589             case 0x1a: /* FABD */
11590                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11591                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11592                 break;
11593             case 0x1c: /* FCMGT */
11594                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11595                 break;
11596             case 0x1d: /* FACGT */
11597                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11598                 break;
11599             default:
11600                 g_assert_not_reached();
11601             }
11602 
11603             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11604         }
11605     }
11606 
11607     clear_vec_high(s, is_q, rd);
11608 }
11609 
11610 /* AdvSIMD three same extra
11611  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11612  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11613  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11614  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11615  */
11616 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11617 {
11618     int rd = extract32(insn, 0, 5);
11619     int rn = extract32(insn, 5, 5);
11620     int opcode = extract32(insn, 11, 4);
11621     int rm = extract32(insn, 16, 5);
11622     int size = extract32(insn, 22, 2);
11623     bool u = extract32(insn, 29, 1);
11624     bool is_q = extract32(insn, 30, 1);
11625     bool feature;
11626     int rot;
11627 
11628     switch (u * 16 + opcode) {
11629     case 0x10: /* SQRDMLAH (vector) */
11630     case 0x11: /* SQRDMLSH (vector) */
11631         if (size != 1 && size != 2) {
11632             unallocated_encoding(s);
11633             return;
11634         }
11635         feature = dc_isar_feature(aa64_rdm, s);
11636         break;
11637     case 0x02: /* SDOT (vector) */
11638     case 0x12: /* UDOT (vector) */
11639         if (size != MO_32) {
11640             unallocated_encoding(s);
11641             return;
11642         }
11643         feature = dc_isar_feature(aa64_dp, s);
11644         break;
11645     case 0x03: /* USDOT */
11646         if (size != MO_32) {
11647             unallocated_encoding(s);
11648             return;
11649         }
11650         feature = dc_isar_feature(aa64_i8mm, s);
11651         break;
11652     case 0x04: /* SMMLA */
11653     case 0x14: /* UMMLA */
11654     case 0x05: /* USMMLA */
11655         if (!is_q || size != MO_32) {
11656             unallocated_encoding(s);
11657             return;
11658         }
11659         feature = dc_isar_feature(aa64_i8mm, s);
11660         break;
11661     case 0x18: /* FCMLA, #0 */
11662     case 0x19: /* FCMLA, #90 */
11663     case 0x1a: /* FCMLA, #180 */
11664     case 0x1b: /* FCMLA, #270 */
11665     case 0x1c: /* FCADD, #90 */
11666     case 0x1e: /* FCADD, #270 */
11667         if (size == 0
11668             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11669             || (size == 3 && !is_q)) {
11670             unallocated_encoding(s);
11671             return;
11672         }
11673         feature = dc_isar_feature(aa64_fcma, s);
11674         break;
11675     case 0x1d: /* BFMMLA */
11676         if (size != MO_16 || !is_q) {
11677             unallocated_encoding(s);
11678             return;
11679         }
11680         feature = dc_isar_feature(aa64_bf16, s);
11681         break;
11682     case 0x1f:
11683         switch (size) {
11684         case 1: /* BFDOT */
11685         case 3: /* BFMLAL{B,T} */
11686             feature = dc_isar_feature(aa64_bf16, s);
11687             break;
11688         default:
11689             unallocated_encoding(s);
11690             return;
11691         }
11692         break;
11693     default:
11694         unallocated_encoding(s);
11695         return;
11696     }
11697     if (!feature) {
11698         unallocated_encoding(s);
11699         return;
11700     }
11701     if (!fp_access_check(s)) {
11702         return;
11703     }
11704 
11705     switch (opcode) {
11706     case 0x0: /* SQRDMLAH (vector) */
11707         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11708         return;
11709 
11710     case 0x1: /* SQRDMLSH (vector) */
11711         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11712         return;
11713 
11714     case 0x2: /* SDOT / UDOT */
11715         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11716                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11717         return;
11718 
11719     case 0x3: /* USDOT */
11720         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11721         return;
11722 
11723     case 0x04: /* SMMLA, UMMLA */
11724         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11725                          u ? gen_helper_gvec_ummla_b
11726                          : gen_helper_gvec_smmla_b);
11727         return;
11728     case 0x05: /* USMMLA */
11729         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11730         return;
11731 
11732     case 0x8: /* FCMLA, #0 */
11733     case 0x9: /* FCMLA, #90 */
11734     case 0xa: /* FCMLA, #180 */
11735     case 0xb: /* FCMLA, #270 */
11736         rot = extract32(opcode, 0, 2);
11737         switch (size) {
11738         case 1:
11739             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11740                               gen_helper_gvec_fcmlah);
11741             break;
11742         case 2:
11743             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11744                               gen_helper_gvec_fcmlas);
11745             break;
11746         case 3:
11747             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11748                               gen_helper_gvec_fcmlad);
11749             break;
11750         default:
11751             g_assert_not_reached();
11752         }
11753         return;
11754 
11755     case 0xc: /* FCADD, #90 */
11756     case 0xe: /* FCADD, #270 */
11757         rot = extract32(opcode, 1, 1);
11758         switch (size) {
11759         case 1:
11760             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11761                               gen_helper_gvec_fcaddh);
11762             break;
11763         case 2:
11764             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11765                               gen_helper_gvec_fcadds);
11766             break;
11767         case 3:
11768             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11769                               gen_helper_gvec_fcaddd);
11770             break;
11771         default:
11772             g_assert_not_reached();
11773         }
11774         return;
11775 
11776     case 0xd: /* BFMMLA */
11777         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11778         return;
11779     case 0xf:
11780         switch (size) {
11781         case 1: /* BFDOT */
11782             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11783             break;
11784         case 3: /* BFMLAL{B,T} */
11785             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11786                               gen_helper_gvec_bfmlal);
11787             break;
11788         default:
11789             g_assert_not_reached();
11790         }
11791         return;
11792 
11793     default:
11794         g_assert_not_reached();
11795     }
11796 }
11797 
11798 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11799                                   int size, int rn, int rd)
11800 {
11801     /* Handle 2-reg-misc ops which are widening (so each size element
11802      * in the source becomes a 2*size element in the destination.
11803      * The only instruction like this is FCVTL.
11804      */
11805     int pass;
11806 
11807     if (size == 3) {
11808         /* 32 -> 64 bit fp conversion */
11809         TCGv_i64 tcg_res[2];
11810         int srcelt = is_q ? 2 : 0;
11811 
11812         for (pass = 0; pass < 2; pass++) {
11813             TCGv_i32 tcg_op = tcg_temp_new_i32();
11814             tcg_res[pass] = tcg_temp_new_i64();
11815 
11816             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11817             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11818         }
11819         for (pass = 0; pass < 2; pass++) {
11820             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11821         }
11822     } else {
11823         /* 16 -> 32 bit fp conversion */
11824         int srcelt = is_q ? 4 : 0;
11825         TCGv_i32 tcg_res[4];
11826         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11827         TCGv_i32 ahp = get_ahp_flag();
11828 
11829         for (pass = 0; pass < 4; pass++) {
11830             tcg_res[pass] = tcg_temp_new_i32();
11831 
11832             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11833             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11834                                            fpst, ahp);
11835         }
11836         for (pass = 0; pass < 4; pass++) {
11837             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11838         }
11839     }
11840 }
11841 
11842 static void handle_rev(DisasContext *s, int opcode, bool u,
11843                        bool is_q, int size, int rn, int rd)
11844 {
11845     int op = (opcode << 1) | u;
11846     int opsz = op + size;
11847     int grp_size = 3 - opsz;
11848     int dsize = is_q ? 128 : 64;
11849     int i;
11850 
11851     if (opsz >= 3) {
11852         unallocated_encoding(s);
11853         return;
11854     }
11855 
11856     if (!fp_access_check(s)) {
11857         return;
11858     }
11859 
11860     if (size == 0) {
11861         /* Special case bytes, use bswap op on each group of elements */
11862         int groups = dsize / (8 << grp_size);
11863 
11864         for (i = 0; i < groups; i++) {
11865             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11866 
11867             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11868             switch (grp_size) {
11869             case MO_16:
11870                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11871                 break;
11872             case MO_32:
11873                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11874                 break;
11875             case MO_64:
11876                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11877                 break;
11878             default:
11879                 g_assert_not_reached();
11880             }
11881             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11882         }
11883         clear_vec_high(s, is_q, rd);
11884     } else {
11885         int revmask = (1 << grp_size) - 1;
11886         int esize = 8 << size;
11887         int elements = dsize / esize;
11888         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11889         TCGv_i64 tcg_rd[2];
11890 
11891         for (i = 0; i < 2; i++) {
11892             tcg_rd[i] = tcg_temp_new_i64();
11893             tcg_gen_movi_i64(tcg_rd[i], 0);
11894         }
11895 
11896         for (i = 0; i < elements; i++) {
11897             int e_rev = (i & 0xf) ^ revmask;
11898             int w = (e_rev * esize) / 64;
11899             int o = (e_rev * esize) % 64;
11900 
11901             read_vec_element(s, tcg_rn, rn, i, size);
11902             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11903         }
11904 
11905         for (i = 0; i < 2; i++) {
11906             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11907         }
11908         clear_vec_high(s, true, rd);
11909     }
11910 }
11911 
11912 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11913                                   bool is_q, int size, int rn, int rd)
11914 {
11915     /* Implement the pairwise operations from 2-misc:
11916      * SADDLP, UADDLP, SADALP, UADALP.
11917      * These all add pairs of elements in the input to produce a
11918      * double-width result element in the output (possibly accumulating).
11919      */
11920     bool accum = (opcode == 0x6);
11921     int maxpass = is_q ? 2 : 1;
11922     int pass;
11923     TCGv_i64 tcg_res[2];
11924 
11925     if (size == 2) {
11926         /* 32 + 32 -> 64 op */
11927         MemOp memop = size + (u ? 0 : MO_SIGN);
11928 
11929         for (pass = 0; pass < maxpass; pass++) {
11930             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11931             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11932 
11933             tcg_res[pass] = tcg_temp_new_i64();
11934 
11935             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11936             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11937             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11938             if (accum) {
11939                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11940                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11941             }
11942         }
11943     } else {
11944         for (pass = 0; pass < maxpass; pass++) {
11945             TCGv_i64 tcg_op = tcg_temp_new_i64();
11946             NeonGenOne64OpFn *genfn;
11947             static NeonGenOne64OpFn * const fns[2][2] = {
11948                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11949                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11950             };
11951 
11952             genfn = fns[size][u];
11953 
11954             tcg_res[pass] = tcg_temp_new_i64();
11955 
11956             read_vec_element(s, tcg_op, rn, pass, MO_64);
11957             genfn(tcg_res[pass], tcg_op);
11958 
11959             if (accum) {
11960                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11961                 if (size == 0) {
11962                     gen_helper_neon_addl_u16(tcg_res[pass],
11963                                              tcg_res[pass], tcg_op);
11964                 } else {
11965                     gen_helper_neon_addl_u32(tcg_res[pass],
11966                                              tcg_res[pass], tcg_op);
11967                 }
11968             }
11969         }
11970     }
11971     if (!is_q) {
11972         tcg_res[1] = tcg_constant_i64(0);
11973     }
11974     for (pass = 0; pass < 2; pass++) {
11975         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11976     }
11977 }
11978 
11979 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11980 {
11981     /* Implement SHLL and SHLL2 */
11982     int pass;
11983     int part = is_q ? 2 : 0;
11984     TCGv_i64 tcg_res[2];
11985 
11986     for (pass = 0; pass < 2; pass++) {
11987         static NeonGenWidenFn * const widenfns[3] = {
11988             gen_helper_neon_widen_u8,
11989             gen_helper_neon_widen_u16,
11990             tcg_gen_extu_i32_i64,
11991         };
11992         NeonGenWidenFn *widenfn = widenfns[size];
11993         TCGv_i32 tcg_op = tcg_temp_new_i32();
11994 
11995         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11996         tcg_res[pass] = tcg_temp_new_i64();
11997         widenfn(tcg_res[pass], tcg_op);
11998         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11999     }
12000 
12001     for (pass = 0; pass < 2; pass++) {
12002         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12003     }
12004 }
12005 
12006 /* AdvSIMD two reg misc
12007  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12008  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12009  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12010  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12011  */
12012 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12013 {
12014     int size = extract32(insn, 22, 2);
12015     int opcode = extract32(insn, 12, 5);
12016     bool u = extract32(insn, 29, 1);
12017     bool is_q = extract32(insn, 30, 1);
12018     int rn = extract32(insn, 5, 5);
12019     int rd = extract32(insn, 0, 5);
12020     bool need_fpstatus = false;
12021     int rmode = -1;
12022     TCGv_i32 tcg_rmode;
12023     TCGv_ptr tcg_fpstatus;
12024 
12025     switch (opcode) {
12026     case 0x0: /* REV64, REV32 */
12027     case 0x1: /* REV16 */
12028         handle_rev(s, opcode, u, is_q, size, rn, rd);
12029         return;
12030     case 0x5: /* CNT, NOT, RBIT */
12031         if (u && size == 0) {
12032             /* NOT */
12033             break;
12034         } else if (u && size == 1) {
12035             /* RBIT */
12036             break;
12037         } else if (!u && size == 0) {
12038             /* CNT */
12039             break;
12040         }
12041         unallocated_encoding(s);
12042         return;
12043     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12044     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12045         if (size == 3) {
12046             unallocated_encoding(s);
12047             return;
12048         }
12049         if (!fp_access_check(s)) {
12050             return;
12051         }
12052 
12053         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12054         return;
12055     case 0x4: /* CLS, CLZ */
12056         if (size == 3) {
12057             unallocated_encoding(s);
12058             return;
12059         }
12060         break;
12061     case 0x2: /* SADDLP, UADDLP */
12062     case 0x6: /* SADALP, UADALP */
12063         if (size == 3) {
12064             unallocated_encoding(s);
12065             return;
12066         }
12067         if (!fp_access_check(s)) {
12068             return;
12069         }
12070         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12071         return;
12072     case 0x13: /* SHLL, SHLL2 */
12073         if (u == 0 || size == 3) {
12074             unallocated_encoding(s);
12075             return;
12076         }
12077         if (!fp_access_check(s)) {
12078             return;
12079         }
12080         handle_shll(s, is_q, size, rn, rd);
12081         return;
12082     case 0xa: /* CMLT */
12083         if (u == 1) {
12084             unallocated_encoding(s);
12085             return;
12086         }
12087         /* fall through */
12088     case 0x8: /* CMGT, CMGE */
12089     case 0x9: /* CMEQ, CMLE */
12090     case 0xb: /* ABS, NEG */
12091         if (size == 3 && !is_q) {
12092             unallocated_encoding(s);
12093             return;
12094         }
12095         break;
12096     case 0x3: /* SUQADD, USQADD */
12097         if (size == 3 && !is_q) {
12098             unallocated_encoding(s);
12099             return;
12100         }
12101         if (!fp_access_check(s)) {
12102             return;
12103         }
12104         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12105         return;
12106     case 0x7: /* SQABS, SQNEG */
12107         if (size == 3 && !is_q) {
12108             unallocated_encoding(s);
12109             return;
12110         }
12111         break;
12112     case 0xc ... 0xf:
12113     case 0x16 ... 0x1f:
12114     {
12115         /* Floating point: U, size[1] and opcode indicate operation;
12116          * size[0] indicates single or double precision.
12117          */
12118         int is_double = extract32(size, 0, 1);
12119         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12120         size = is_double ? 3 : 2;
12121         switch (opcode) {
12122         case 0x2f: /* FABS */
12123         case 0x6f: /* FNEG */
12124             if (size == 3 && !is_q) {
12125                 unallocated_encoding(s);
12126                 return;
12127             }
12128             break;
12129         case 0x1d: /* SCVTF */
12130         case 0x5d: /* UCVTF */
12131         {
12132             bool is_signed = (opcode == 0x1d) ? true : false;
12133             int elements = is_double ? 2 : is_q ? 4 : 2;
12134             if (is_double && !is_q) {
12135                 unallocated_encoding(s);
12136                 return;
12137             }
12138             if (!fp_access_check(s)) {
12139                 return;
12140             }
12141             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12142             return;
12143         }
12144         case 0x2c: /* FCMGT (zero) */
12145         case 0x2d: /* FCMEQ (zero) */
12146         case 0x2e: /* FCMLT (zero) */
12147         case 0x6c: /* FCMGE (zero) */
12148         case 0x6d: /* FCMLE (zero) */
12149             if (size == 3 && !is_q) {
12150                 unallocated_encoding(s);
12151                 return;
12152             }
12153             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12154             return;
12155         case 0x7f: /* FSQRT */
12156             if (size == 3 && !is_q) {
12157                 unallocated_encoding(s);
12158                 return;
12159             }
12160             break;
12161         case 0x1a: /* FCVTNS */
12162         case 0x1b: /* FCVTMS */
12163         case 0x3a: /* FCVTPS */
12164         case 0x3b: /* FCVTZS */
12165         case 0x5a: /* FCVTNU */
12166         case 0x5b: /* FCVTMU */
12167         case 0x7a: /* FCVTPU */
12168         case 0x7b: /* FCVTZU */
12169             need_fpstatus = true;
12170             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12171             if (size == 3 && !is_q) {
12172                 unallocated_encoding(s);
12173                 return;
12174             }
12175             break;
12176         case 0x5c: /* FCVTAU */
12177         case 0x1c: /* FCVTAS */
12178             need_fpstatus = true;
12179             rmode = FPROUNDING_TIEAWAY;
12180             if (size == 3 && !is_q) {
12181                 unallocated_encoding(s);
12182                 return;
12183             }
12184             break;
12185         case 0x3c: /* URECPE */
12186             if (size == 3) {
12187                 unallocated_encoding(s);
12188                 return;
12189             }
12190             /* fall through */
12191         case 0x3d: /* FRECPE */
12192         case 0x7d: /* FRSQRTE */
12193             if (size == 3 && !is_q) {
12194                 unallocated_encoding(s);
12195                 return;
12196             }
12197             if (!fp_access_check(s)) {
12198                 return;
12199             }
12200             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12201             return;
12202         case 0x56: /* FCVTXN, FCVTXN2 */
12203             if (size == 2) {
12204                 unallocated_encoding(s);
12205                 return;
12206             }
12207             /* fall through */
12208         case 0x16: /* FCVTN, FCVTN2 */
12209             /* handle_2misc_narrow does a 2*size -> size operation, but these
12210              * instructions encode the source size rather than dest size.
12211              */
12212             if (!fp_access_check(s)) {
12213                 return;
12214             }
12215             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12216             return;
12217         case 0x36: /* BFCVTN, BFCVTN2 */
12218             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12219                 unallocated_encoding(s);
12220                 return;
12221             }
12222             if (!fp_access_check(s)) {
12223                 return;
12224             }
12225             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12226             return;
12227         case 0x17: /* FCVTL, FCVTL2 */
12228             if (!fp_access_check(s)) {
12229                 return;
12230             }
12231             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12232             return;
12233         case 0x18: /* FRINTN */
12234         case 0x19: /* FRINTM */
12235         case 0x38: /* FRINTP */
12236         case 0x39: /* FRINTZ */
12237             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12238             /* fall through */
12239         case 0x59: /* FRINTX */
12240         case 0x79: /* FRINTI */
12241             need_fpstatus = true;
12242             if (size == 3 && !is_q) {
12243                 unallocated_encoding(s);
12244                 return;
12245             }
12246             break;
12247         case 0x58: /* FRINTA */
12248             rmode = FPROUNDING_TIEAWAY;
12249             need_fpstatus = true;
12250             if (size == 3 && !is_q) {
12251                 unallocated_encoding(s);
12252                 return;
12253             }
12254             break;
12255         case 0x7c: /* URSQRTE */
12256             if (size == 3) {
12257                 unallocated_encoding(s);
12258                 return;
12259             }
12260             break;
12261         case 0x1e: /* FRINT32Z */
12262         case 0x1f: /* FRINT64Z */
12263             rmode = FPROUNDING_ZERO;
12264             /* fall through */
12265         case 0x5e: /* FRINT32X */
12266         case 0x5f: /* FRINT64X */
12267             need_fpstatus = true;
12268             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12269                 unallocated_encoding(s);
12270                 return;
12271             }
12272             break;
12273         default:
12274             unallocated_encoding(s);
12275             return;
12276         }
12277         break;
12278     }
12279     default:
12280         unallocated_encoding(s);
12281         return;
12282     }
12283 
12284     if (!fp_access_check(s)) {
12285         return;
12286     }
12287 
12288     if (need_fpstatus || rmode >= 0) {
12289         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12290     } else {
12291         tcg_fpstatus = NULL;
12292     }
12293     if (rmode >= 0) {
12294         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12295     } else {
12296         tcg_rmode = NULL;
12297     }
12298 
12299     switch (opcode) {
12300     case 0x5:
12301         if (u && size == 0) { /* NOT */
12302             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12303             return;
12304         }
12305         break;
12306     case 0x8: /* CMGT, CMGE */
12307         if (u) {
12308             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12309         } else {
12310             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12311         }
12312         return;
12313     case 0x9: /* CMEQ, CMLE */
12314         if (u) {
12315             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12316         } else {
12317             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12318         }
12319         return;
12320     case 0xa: /* CMLT */
12321         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12322         return;
12323     case 0xb:
12324         if (u) { /* ABS, NEG */
12325             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12326         } else {
12327             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12328         }
12329         return;
12330     }
12331 
12332     if (size == 3) {
12333         /* All 64-bit element operations can be shared with scalar 2misc */
12334         int pass;
12335 
12336         /* Coverity claims (size == 3 && !is_q) has been eliminated
12337          * from all paths leading to here.
12338          */
12339         tcg_debug_assert(is_q);
12340         for (pass = 0; pass < 2; pass++) {
12341             TCGv_i64 tcg_op = tcg_temp_new_i64();
12342             TCGv_i64 tcg_res = tcg_temp_new_i64();
12343 
12344             read_vec_element(s, tcg_op, rn, pass, MO_64);
12345 
12346             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12347                             tcg_rmode, tcg_fpstatus);
12348 
12349             write_vec_element(s, tcg_res, rd, pass, MO_64);
12350         }
12351     } else {
12352         int pass;
12353 
12354         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12355             TCGv_i32 tcg_op = tcg_temp_new_i32();
12356             TCGv_i32 tcg_res = tcg_temp_new_i32();
12357 
12358             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12359 
12360             if (size == 2) {
12361                 /* Special cases for 32 bit elements */
12362                 switch (opcode) {
12363                 case 0x4: /* CLS */
12364                     if (u) {
12365                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12366                     } else {
12367                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12368                     }
12369                     break;
12370                 case 0x7: /* SQABS, SQNEG */
12371                     if (u) {
12372                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
12373                     } else {
12374                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
12375                     }
12376                     break;
12377                 case 0x2f: /* FABS */
12378                     gen_helper_vfp_abss(tcg_res, tcg_op);
12379                     break;
12380                 case 0x6f: /* FNEG */
12381                     gen_helper_vfp_negs(tcg_res, tcg_op);
12382                     break;
12383                 case 0x7f: /* FSQRT */
12384                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
12385                     break;
12386                 case 0x1a: /* FCVTNS */
12387                 case 0x1b: /* FCVTMS */
12388                 case 0x1c: /* FCVTAS */
12389                 case 0x3a: /* FCVTPS */
12390                 case 0x3b: /* FCVTZS */
12391                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12392                                          tcg_constant_i32(0), tcg_fpstatus);
12393                     break;
12394                 case 0x5a: /* FCVTNU */
12395                 case 0x5b: /* FCVTMU */
12396                 case 0x5c: /* FCVTAU */
12397                 case 0x7a: /* FCVTPU */
12398                 case 0x7b: /* FCVTZU */
12399                     gen_helper_vfp_touls(tcg_res, tcg_op,
12400                                          tcg_constant_i32(0), tcg_fpstatus);
12401                     break;
12402                 case 0x18: /* FRINTN */
12403                 case 0x19: /* FRINTM */
12404                 case 0x38: /* FRINTP */
12405                 case 0x39: /* FRINTZ */
12406                 case 0x58: /* FRINTA */
12407                 case 0x79: /* FRINTI */
12408                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12409                     break;
12410                 case 0x59: /* FRINTX */
12411                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12412                     break;
12413                 case 0x7c: /* URSQRTE */
12414                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12415                     break;
12416                 case 0x1e: /* FRINT32Z */
12417                 case 0x5e: /* FRINT32X */
12418                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12419                     break;
12420                 case 0x1f: /* FRINT64Z */
12421                 case 0x5f: /* FRINT64X */
12422                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12423                     break;
12424                 default:
12425                     g_assert_not_reached();
12426                 }
12427             } else {
12428                 /* Use helpers for 8 and 16 bit elements */
12429                 switch (opcode) {
12430                 case 0x5: /* CNT, RBIT */
12431                     /* For these two insns size is part of the opcode specifier
12432                      * (handled earlier); they always operate on byte elements.
12433                      */
12434                     if (u) {
12435                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12436                     } else {
12437                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12438                     }
12439                     break;
12440                 case 0x7: /* SQABS, SQNEG */
12441                 {
12442                     NeonGenOneOpEnvFn *genfn;
12443                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12444                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12445                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12446                     };
12447                     genfn = fns[size][u];
12448                     genfn(tcg_res, tcg_env, tcg_op);
12449                     break;
12450                 }
12451                 case 0x4: /* CLS, CLZ */
12452                     if (u) {
12453                         if (size == 0) {
12454                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12455                         } else {
12456                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12457                         }
12458                     } else {
12459                         if (size == 0) {
12460                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12461                         } else {
12462                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12463                         }
12464                     }
12465                     break;
12466                 default:
12467                     g_assert_not_reached();
12468                 }
12469             }
12470 
12471             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12472         }
12473     }
12474     clear_vec_high(s, is_q, rd);
12475 
12476     if (tcg_rmode) {
12477         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12478     }
12479 }
12480 
12481 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12482  *
12483  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12484  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12485  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12486  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12487  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12488  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12489  *
12490  * This actually covers two groups where scalar access is governed by
12491  * bit 28. A bunch of the instructions (float to integral) only exist
12492  * in the vector form and are un-allocated for the scalar decode. Also
12493  * in the scalar decode Q is always 1.
12494  */
12495 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12496 {
12497     int fpop, opcode, a, u;
12498     int rn, rd;
12499     bool is_q;
12500     bool is_scalar;
12501     bool only_in_vector = false;
12502 
12503     int pass;
12504     TCGv_i32 tcg_rmode = NULL;
12505     TCGv_ptr tcg_fpstatus = NULL;
12506     bool need_fpst = true;
12507     int rmode = -1;
12508 
12509     if (!dc_isar_feature(aa64_fp16, s)) {
12510         unallocated_encoding(s);
12511         return;
12512     }
12513 
12514     rd = extract32(insn, 0, 5);
12515     rn = extract32(insn, 5, 5);
12516 
12517     a = extract32(insn, 23, 1);
12518     u = extract32(insn, 29, 1);
12519     is_scalar = extract32(insn, 28, 1);
12520     is_q = extract32(insn, 30, 1);
12521 
12522     opcode = extract32(insn, 12, 5);
12523     fpop = deposit32(opcode, 5, 1, a);
12524     fpop = deposit32(fpop, 6, 1, u);
12525 
12526     switch (fpop) {
12527     case 0x1d: /* SCVTF */
12528     case 0x5d: /* UCVTF */
12529     {
12530         int elements;
12531 
12532         if (is_scalar) {
12533             elements = 1;
12534         } else {
12535             elements = (is_q ? 8 : 4);
12536         }
12537 
12538         if (!fp_access_check(s)) {
12539             return;
12540         }
12541         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12542         return;
12543     }
12544     break;
12545     case 0x2c: /* FCMGT (zero) */
12546     case 0x2d: /* FCMEQ (zero) */
12547     case 0x2e: /* FCMLT (zero) */
12548     case 0x6c: /* FCMGE (zero) */
12549     case 0x6d: /* FCMLE (zero) */
12550         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12551         return;
12552     case 0x3d: /* FRECPE */
12553     case 0x3f: /* FRECPX */
12554         break;
12555     case 0x18: /* FRINTN */
12556         only_in_vector = true;
12557         rmode = FPROUNDING_TIEEVEN;
12558         break;
12559     case 0x19: /* FRINTM */
12560         only_in_vector = true;
12561         rmode = FPROUNDING_NEGINF;
12562         break;
12563     case 0x38: /* FRINTP */
12564         only_in_vector = true;
12565         rmode = FPROUNDING_POSINF;
12566         break;
12567     case 0x39: /* FRINTZ */
12568         only_in_vector = true;
12569         rmode = FPROUNDING_ZERO;
12570         break;
12571     case 0x58: /* FRINTA */
12572         only_in_vector = true;
12573         rmode = FPROUNDING_TIEAWAY;
12574         break;
12575     case 0x59: /* FRINTX */
12576     case 0x79: /* FRINTI */
12577         only_in_vector = true;
12578         /* current rounding mode */
12579         break;
12580     case 0x1a: /* FCVTNS */
12581         rmode = FPROUNDING_TIEEVEN;
12582         break;
12583     case 0x1b: /* FCVTMS */
12584         rmode = FPROUNDING_NEGINF;
12585         break;
12586     case 0x1c: /* FCVTAS */
12587         rmode = FPROUNDING_TIEAWAY;
12588         break;
12589     case 0x3a: /* FCVTPS */
12590         rmode = FPROUNDING_POSINF;
12591         break;
12592     case 0x3b: /* FCVTZS */
12593         rmode = FPROUNDING_ZERO;
12594         break;
12595     case 0x5a: /* FCVTNU */
12596         rmode = FPROUNDING_TIEEVEN;
12597         break;
12598     case 0x5b: /* FCVTMU */
12599         rmode = FPROUNDING_NEGINF;
12600         break;
12601     case 0x5c: /* FCVTAU */
12602         rmode = FPROUNDING_TIEAWAY;
12603         break;
12604     case 0x7a: /* FCVTPU */
12605         rmode = FPROUNDING_POSINF;
12606         break;
12607     case 0x7b: /* FCVTZU */
12608         rmode = FPROUNDING_ZERO;
12609         break;
12610     case 0x2f: /* FABS */
12611     case 0x6f: /* FNEG */
12612         need_fpst = false;
12613         break;
12614     case 0x7d: /* FRSQRTE */
12615     case 0x7f: /* FSQRT (vector) */
12616         break;
12617     default:
12618         unallocated_encoding(s);
12619         return;
12620     }
12621 
12622 
12623     /* Check additional constraints for the scalar encoding */
12624     if (is_scalar) {
12625         if (!is_q) {
12626             unallocated_encoding(s);
12627             return;
12628         }
12629         /* FRINTxx is only in the vector form */
12630         if (only_in_vector) {
12631             unallocated_encoding(s);
12632             return;
12633         }
12634     }
12635 
12636     if (!fp_access_check(s)) {
12637         return;
12638     }
12639 
12640     if (rmode >= 0 || need_fpst) {
12641         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12642     }
12643 
12644     if (rmode >= 0) {
12645         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12646     }
12647 
12648     if (is_scalar) {
12649         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12650         TCGv_i32 tcg_res = tcg_temp_new_i32();
12651 
12652         switch (fpop) {
12653         case 0x1a: /* FCVTNS */
12654         case 0x1b: /* FCVTMS */
12655         case 0x1c: /* FCVTAS */
12656         case 0x3a: /* FCVTPS */
12657         case 0x3b: /* FCVTZS */
12658             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12659             break;
12660         case 0x3d: /* FRECPE */
12661             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12662             break;
12663         case 0x3f: /* FRECPX */
12664             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12665             break;
12666         case 0x5a: /* FCVTNU */
12667         case 0x5b: /* FCVTMU */
12668         case 0x5c: /* FCVTAU */
12669         case 0x7a: /* FCVTPU */
12670         case 0x7b: /* FCVTZU */
12671             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12672             break;
12673         case 0x6f: /* FNEG */
12674             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12675             break;
12676         case 0x7d: /* FRSQRTE */
12677             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12678             break;
12679         default:
12680             g_assert_not_reached();
12681         }
12682 
12683         /* limit any sign extension going on */
12684         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12685         write_fp_sreg(s, rd, tcg_res);
12686     } else {
12687         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12688             TCGv_i32 tcg_op = tcg_temp_new_i32();
12689             TCGv_i32 tcg_res = tcg_temp_new_i32();
12690 
12691             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12692 
12693             switch (fpop) {
12694             case 0x1a: /* FCVTNS */
12695             case 0x1b: /* FCVTMS */
12696             case 0x1c: /* FCVTAS */
12697             case 0x3a: /* FCVTPS */
12698             case 0x3b: /* FCVTZS */
12699                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12700                 break;
12701             case 0x3d: /* FRECPE */
12702                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12703                 break;
12704             case 0x5a: /* FCVTNU */
12705             case 0x5b: /* FCVTMU */
12706             case 0x5c: /* FCVTAU */
12707             case 0x7a: /* FCVTPU */
12708             case 0x7b: /* FCVTZU */
12709                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12710                 break;
12711             case 0x18: /* FRINTN */
12712             case 0x19: /* FRINTM */
12713             case 0x38: /* FRINTP */
12714             case 0x39: /* FRINTZ */
12715             case 0x58: /* FRINTA */
12716             case 0x79: /* FRINTI */
12717                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12718                 break;
12719             case 0x59: /* FRINTX */
12720                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12721                 break;
12722             case 0x2f: /* FABS */
12723                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12724                 break;
12725             case 0x6f: /* FNEG */
12726                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12727                 break;
12728             case 0x7d: /* FRSQRTE */
12729                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12730                 break;
12731             case 0x7f: /* FSQRT */
12732                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12733                 break;
12734             default:
12735                 g_assert_not_reached();
12736             }
12737 
12738             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12739         }
12740 
12741         clear_vec_high(s, is_q, rd);
12742     }
12743 
12744     if (tcg_rmode) {
12745         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12746     }
12747 }
12748 
12749 /* AdvSIMD scalar x indexed element
12750  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12751  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12752  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12753  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12754  * AdvSIMD vector x indexed element
12755  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12756  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12757  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12758  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12759  */
12760 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12761 {
12762     /* This encoding has two kinds of instruction:
12763      *  normal, where we perform elt x idxelt => elt for each
12764      *     element in the vector
12765      *  long, where we perform elt x idxelt and generate a result of
12766      *     double the width of the input element
12767      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12768      */
12769     bool is_scalar = extract32(insn, 28, 1);
12770     bool is_q = extract32(insn, 30, 1);
12771     bool u = extract32(insn, 29, 1);
12772     int size = extract32(insn, 22, 2);
12773     int l = extract32(insn, 21, 1);
12774     int m = extract32(insn, 20, 1);
12775     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12776     int rm = extract32(insn, 16, 4);
12777     int opcode = extract32(insn, 12, 4);
12778     int h = extract32(insn, 11, 1);
12779     int rn = extract32(insn, 5, 5);
12780     int rd = extract32(insn, 0, 5);
12781     bool is_long = false;
12782     int is_fp = 0;
12783     bool is_fp16 = false;
12784     int index;
12785     TCGv_ptr fpst;
12786 
12787     switch (16 * u + opcode) {
12788     case 0x08: /* MUL */
12789     case 0x10: /* MLA */
12790     case 0x14: /* MLS */
12791         if (is_scalar) {
12792             unallocated_encoding(s);
12793             return;
12794         }
12795         break;
12796     case 0x02: /* SMLAL, SMLAL2 */
12797     case 0x12: /* UMLAL, UMLAL2 */
12798     case 0x06: /* SMLSL, SMLSL2 */
12799     case 0x16: /* UMLSL, UMLSL2 */
12800     case 0x0a: /* SMULL, SMULL2 */
12801     case 0x1a: /* UMULL, UMULL2 */
12802         if (is_scalar) {
12803             unallocated_encoding(s);
12804             return;
12805         }
12806         is_long = true;
12807         break;
12808     case 0x03: /* SQDMLAL, SQDMLAL2 */
12809     case 0x07: /* SQDMLSL, SQDMLSL2 */
12810     case 0x0b: /* SQDMULL, SQDMULL2 */
12811         is_long = true;
12812         break;
12813     case 0x0c: /* SQDMULH */
12814     case 0x0d: /* SQRDMULH */
12815         break;
12816     case 0x01: /* FMLA */
12817     case 0x05: /* FMLS */
12818     case 0x09: /* FMUL */
12819     case 0x19: /* FMULX */
12820         is_fp = 1;
12821         break;
12822     case 0x1d: /* SQRDMLAH */
12823     case 0x1f: /* SQRDMLSH */
12824         if (!dc_isar_feature(aa64_rdm, s)) {
12825             unallocated_encoding(s);
12826             return;
12827         }
12828         break;
12829     case 0x0e: /* SDOT */
12830     case 0x1e: /* UDOT */
12831         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12832             unallocated_encoding(s);
12833             return;
12834         }
12835         break;
12836     case 0x0f:
12837         switch (size) {
12838         case 0: /* SUDOT */
12839         case 2: /* USDOT */
12840             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12841                 unallocated_encoding(s);
12842                 return;
12843             }
12844             size = MO_32;
12845             break;
12846         case 1: /* BFDOT */
12847             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12848                 unallocated_encoding(s);
12849                 return;
12850             }
12851             size = MO_32;
12852             break;
12853         case 3: /* BFMLAL{B,T} */
12854             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12855                 unallocated_encoding(s);
12856                 return;
12857             }
12858             /* can't set is_fp without other incorrect size checks */
12859             size = MO_16;
12860             break;
12861         default:
12862             unallocated_encoding(s);
12863             return;
12864         }
12865         break;
12866     case 0x11: /* FCMLA #0 */
12867     case 0x13: /* FCMLA #90 */
12868     case 0x15: /* FCMLA #180 */
12869     case 0x17: /* FCMLA #270 */
12870         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12871             unallocated_encoding(s);
12872             return;
12873         }
12874         is_fp = 2;
12875         break;
12876     case 0x00: /* FMLAL */
12877     case 0x04: /* FMLSL */
12878     case 0x18: /* FMLAL2 */
12879     case 0x1c: /* FMLSL2 */
12880         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12881             unallocated_encoding(s);
12882             return;
12883         }
12884         size = MO_16;
12885         /* is_fp, but we pass tcg_env not fp_status.  */
12886         break;
12887     default:
12888         unallocated_encoding(s);
12889         return;
12890     }
12891 
12892     switch (is_fp) {
12893     case 1: /* normal fp */
12894         /* convert insn encoded size to MemOp size */
12895         switch (size) {
12896         case 0: /* half-precision */
12897             size = MO_16;
12898             is_fp16 = true;
12899             break;
12900         case MO_32: /* single precision */
12901         case MO_64: /* double precision */
12902             break;
12903         default:
12904             unallocated_encoding(s);
12905             return;
12906         }
12907         break;
12908 
12909     case 2: /* complex fp */
12910         /* Each indexable element is a complex pair.  */
12911         size += 1;
12912         switch (size) {
12913         case MO_32:
12914             if (h && !is_q) {
12915                 unallocated_encoding(s);
12916                 return;
12917             }
12918             is_fp16 = true;
12919             break;
12920         case MO_64:
12921             break;
12922         default:
12923             unallocated_encoding(s);
12924             return;
12925         }
12926         break;
12927 
12928     default: /* integer */
12929         switch (size) {
12930         case MO_8:
12931         case MO_64:
12932             unallocated_encoding(s);
12933             return;
12934         }
12935         break;
12936     }
12937     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12938         unallocated_encoding(s);
12939         return;
12940     }
12941 
12942     /* Given MemOp size, adjust register and indexing.  */
12943     switch (size) {
12944     case MO_16:
12945         index = h << 2 | l << 1 | m;
12946         break;
12947     case MO_32:
12948         index = h << 1 | l;
12949         rm |= m << 4;
12950         break;
12951     case MO_64:
12952         if (l || !is_q) {
12953             unallocated_encoding(s);
12954             return;
12955         }
12956         index = h;
12957         rm |= m << 4;
12958         break;
12959     default:
12960         g_assert_not_reached();
12961     }
12962 
12963     if (!fp_access_check(s)) {
12964         return;
12965     }
12966 
12967     if (is_fp) {
12968         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12969     } else {
12970         fpst = NULL;
12971     }
12972 
12973     switch (16 * u + opcode) {
12974     case 0x0e: /* SDOT */
12975     case 0x1e: /* UDOT */
12976         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12977                          u ? gen_helper_gvec_udot_idx_b
12978                          : gen_helper_gvec_sdot_idx_b);
12979         return;
12980     case 0x0f:
12981         switch (extract32(insn, 22, 2)) {
12982         case 0: /* SUDOT */
12983             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12984                              gen_helper_gvec_sudot_idx_b);
12985             return;
12986         case 1: /* BFDOT */
12987             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12988                              gen_helper_gvec_bfdot_idx);
12989             return;
12990         case 2: /* USDOT */
12991             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12992                              gen_helper_gvec_usdot_idx_b);
12993             return;
12994         case 3: /* BFMLAL{B,T} */
12995             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12996                               gen_helper_gvec_bfmlal_idx);
12997             return;
12998         }
12999         g_assert_not_reached();
13000     case 0x11: /* FCMLA #0 */
13001     case 0x13: /* FCMLA #90 */
13002     case 0x15: /* FCMLA #180 */
13003     case 0x17: /* FCMLA #270 */
13004         {
13005             int rot = extract32(insn, 13, 2);
13006             int data = (index << 2) | rot;
13007             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13008                                vec_full_reg_offset(s, rn),
13009                                vec_full_reg_offset(s, rm),
13010                                vec_full_reg_offset(s, rd), fpst,
13011                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13012                                size == MO_64
13013                                ? gen_helper_gvec_fcmlas_idx
13014                                : gen_helper_gvec_fcmlah_idx);
13015         }
13016         return;
13017 
13018     case 0x00: /* FMLAL */
13019     case 0x04: /* FMLSL */
13020     case 0x18: /* FMLAL2 */
13021     case 0x1c: /* FMLSL2 */
13022         {
13023             int is_s = extract32(opcode, 2, 1);
13024             int is_2 = u;
13025             int data = (index << 2) | (is_2 << 1) | is_s;
13026             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13027                                vec_full_reg_offset(s, rn),
13028                                vec_full_reg_offset(s, rm), tcg_env,
13029                                is_q ? 16 : 8, vec_full_reg_size(s),
13030                                data, gen_helper_gvec_fmlal_idx_a64);
13031         }
13032         return;
13033 
13034     case 0x08: /* MUL */
13035         if (!is_long && !is_scalar) {
13036             static gen_helper_gvec_3 * const fns[3] = {
13037                 gen_helper_gvec_mul_idx_h,
13038                 gen_helper_gvec_mul_idx_s,
13039                 gen_helper_gvec_mul_idx_d,
13040             };
13041             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13042                                vec_full_reg_offset(s, rn),
13043                                vec_full_reg_offset(s, rm),
13044                                is_q ? 16 : 8, vec_full_reg_size(s),
13045                                index, fns[size - 1]);
13046             return;
13047         }
13048         break;
13049 
13050     case 0x10: /* MLA */
13051         if (!is_long && !is_scalar) {
13052             static gen_helper_gvec_4 * const fns[3] = {
13053                 gen_helper_gvec_mla_idx_h,
13054                 gen_helper_gvec_mla_idx_s,
13055                 gen_helper_gvec_mla_idx_d,
13056             };
13057             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13058                                vec_full_reg_offset(s, rn),
13059                                vec_full_reg_offset(s, rm),
13060                                vec_full_reg_offset(s, rd),
13061                                is_q ? 16 : 8, vec_full_reg_size(s),
13062                                index, fns[size - 1]);
13063             return;
13064         }
13065         break;
13066 
13067     case 0x14: /* MLS */
13068         if (!is_long && !is_scalar) {
13069             static gen_helper_gvec_4 * const fns[3] = {
13070                 gen_helper_gvec_mls_idx_h,
13071                 gen_helper_gvec_mls_idx_s,
13072                 gen_helper_gvec_mls_idx_d,
13073             };
13074             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13075                                vec_full_reg_offset(s, rn),
13076                                vec_full_reg_offset(s, rm),
13077                                vec_full_reg_offset(s, rd),
13078                                is_q ? 16 : 8, vec_full_reg_size(s),
13079                                index, fns[size - 1]);
13080             return;
13081         }
13082         break;
13083     }
13084 
13085     if (size == 3) {
13086         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13087         int pass;
13088 
13089         assert(is_fp && is_q && !is_long);
13090 
13091         read_vec_element(s, tcg_idx, rm, index, MO_64);
13092 
13093         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13094             TCGv_i64 tcg_op = tcg_temp_new_i64();
13095             TCGv_i64 tcg_res = tcg_temp_new_i64();
13096 
13097             read_vec_element(s, tcg_op, rn, pass, MO_64);
13098 
13099             switch (16 * u + opcode) {
13100             case 0x05: /* FMLS */
13101                 /* As usual for ARM, separate negation for fused multiply-add */
13102                 gen_helper_vfp_negd(tcg_op, tcg_op);
13103                 /* fall through */
13104             case 0x01: /* FMLA */
13105                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13106                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13107                 break;
13108             case 0x09: /* FMUL */
13109                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13110                 break;
13111             case 0x19: /* FMULX */
13112                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13113                 break;
13114             default:
13115                 g_assert_not_reached();
13116             }
13117 
13118             write_vec_element(s, tcg_res, rd, pass, MO_64);
13119         }
13120 
13121         clear_vec_high(s, !is_scalar, rd);
13122     } else if (!is_long) {
13123         /* 32 bit floating point, or 16 or 32 bit integer.
13124          * For the 16 bit scalar case we use the usual Neon helpers and
13125          * rely on the fact that 0 op 0 == 0 with no side effects.
13126          */
13127         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13128         int pass, maxpasses;
13129 
13130         if (is_scalar) {
13131             maxpasses = 1;
13132         } else {
13133             maxpasses = is_q ? 4 : 2;
13134         }
13135 
13136         read_vec_element_i32(s, tcg_idx, rm, index, size);
13137 
13138         if (size == 1 && !is_scalar) {
13139             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13140              * the index into both halves of the 32 bit tcg_idx and then use
13141              * the usual Neon helpers.
13142              */
13143             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13144         }
13145 
13146         for (pass = 0; pass < maxpasses; pass++) {
13147             TCGv_i32 tcg_op = tcg_temp_new_i32();
13148             TCGv_i32 tcg_res = tcg_temp_new_i32();
13149 
13150             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13151 
13152             switch (16 * u + opcode) {
13153             case 0x08: /* MUL */
13154             case 0x10: /* MLA */
13155             case 0x14: /* MLS */
13156             {
13157                 static NeonGenTwoOpFn * const fns[2][2] = {
13158                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13159                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13160                 };
13161                 NeonGenTwoOpFn *genfn;
13162                 bool is_sub = opcode == 0x4;
13163 
13164                 if (size == 1) {
13165                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13166                 } else {
13167                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13168                 }
13169                 if (opcode == 0x8) {
13170                     break;
13171                 }
13172                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13173                 genfn = fns[size - 1][is_sub];
13174                 genfn(tcg_res, tcg_op, tcg_res);
13175                 break;
13176             }
13177             case 0x05: /* FMLS */
13178             case 0x01: /* FMLA */
13179                 read_vec_element_i32(s, tcg_res, rd, pass,
13180                                      is_scalar ? size : MO_32);
13181                 switch (size) {
13182                 case 1:
13183                     if (opcode == 0x5) {
13184                         /* As usual for ARM, separate negation for fused
13185                          * multiply-add */
13186                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13187                     }
13188                     if (is_scalar) {
13189                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13190                                                    tcg_res, fpst);
13191                     } else {
13192                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13193                                                     tcg_res, fpst);
13194                     }
13195                     break;
13196                 case 2:
13197                     if (opcode == 0x5) {
13198                         /* As usual for ARM, separate negation for
13199                          * fused multiply-add */
13200                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13201                     }
13202                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13203                                            tcg_res, fpst);
13204                     break;
13205                 default:
13206                     g_assert_not_reached();
13207                 }
13208                 break;
13209             case 0x09: /* FMUL */
13210                 switch (size) {
13211                 case 1:
13212                     if (is_scalar) {
13213                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13214                                                 tcg_idx, fpst);
13215                     } else {
13216                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13217                                                  tcg_idx, fpst);
13218                     }
13219                     break;
13220                 case 2:
13221                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13222                     break;
13223                 default:
13224                     g_assert_not_reached();
13225                 }
13226                 break;
13227             case 0x19: /* FMULX */
13228                 switch (size) {
13229                 case 1:
13230                     if (is_scalar) {
13231                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13232                                                  tcg_idx, fpst);
13233                     } else {
13234                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13235                                                   tcg_idx, fpst);
13236                     }
13237                     break;
13238                 case 2:
13239                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13240                     break;
13241                 default:
13242                     g_assert_not_reached();
13243                 }
13244                 break;
13245             case 0x0c: /* SQDMULH */
13246                 if (size == 1) {
13247                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
13248                                                tcg_op, tcg_idx);
13249                 } else {
13250                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
13251                                                tcg_op, tcg_idx);
13252                 }
13253                 break;
13254             case 0x0d: /* SQRDMULH */
13255                 if (size == 1) {
13256                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
13257                                                 tcg_op, tcg_idx);
13258                 } else {
13259                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
13260                                                 tcg_op, tcg_idx);
13261                 }
13262                 break;
13263             case 0x1d: /* SQRDMLAH */
13264                 read_vec_element_i32(s, tcg_res, rd, pass,
13265                                      is_scalar ? size : MO_32);
13266                 if (size == 1) {
13267                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
13268                                                 tcg_op, tcg_idx, tcg_res);
13269                 } else {
13270                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
13271                                                 tcg_op, tcg_idx, tcg_res);
13272                 }
13273                 break;
13274             case 0x1f: /* SQRDMLSH */
13275                 read_vec_element_i32(s, tcg_res, rd, pass,
13276                                      is_scalar ? size : MO_32);
13277                 if (size == 1) {
13278                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
13279                                                 tcg_op, tcg_idx, tcg_res);
13280                 } else {
13281                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
13282                                                 tcg_op, tcg_idx, tcg_res);
13283                 }
13284                 break;
13285             default:
13286                 g_assert_not_reached();
13287             }
13288 
13289             if (is_scalar) {
13290                 write_fp_sreg(s, rd, tcg_res);
13291             } else {
13292                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13293             }
13294         }
13295 
13296         clear_vec_high(s, is_q, rd);
13297     } else {
13298         /* long ops: 16x16->32 or 32x32->64 */
13299         TCGv_i64 tcg_res[2];
13300         int pass;
13301         bool satop = extract32(opcode, 0, 1);
13302         MemOp memop = MO_32;
13303 
13304         if (satop || !u) {
13305             memop |= MO_SIGN;
13306         }
13307 
13308         if (size == 2) {
13309             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13310 
13311             read_vec_element(s, tcg_idx, rm, index, memop);
13312 
13313             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13314                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13315                 TCGv_i64 tcg_passres;
13316                 int passelt;
13317 
13318                 if (is_scalar) {
13319                     passelt = 0;
13320                 } else {
13321                     passelt = pass + (is_q * 2);
13322                 }
13323 
13324                 read_vec_element(s, tcg_op, rn, passelt, memop);
13325 
13326                 tcg_res[pass] = tcg_temp_new_i64();
13327 
13328                 if (opcode == 0xa || opcode == 0xb) {
13329                     /* Non-accumulating ops */
13330                     tcg_passres = tcg_res[pass];
13331                 } else {
13332                     tcg_passres = tcg_temp_new_i64();
13333                 }
13334 
13335                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13336 
13337                 if (satop) {
13338                     /* saturating, doubling */
13339                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
13340                                                       tcg_passres, tcg_passres);
13341                 }
13342 
13343                 if (opcode == 0xa || opcode == 0xb) {
13344                     continue;
13345                 }
13346 
13347                 /* Accumulating op: handle accumulate step */
13348                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13349 
13350                 switch (opcode) {
13351                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13352                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13353                     break;
13354                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13355                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13356                     break;
13357                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13358                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13359                     /* fall through */
13360                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13361                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
13362                                                       tcg_res[pass],
13363                                                       tcg_passres);
13364                     break;
13365                 default:
13366                     g_assert_not_reached();
13367                 }
13368             }
13369 
13370             clear_vec_high(s, !is_scalar, rd);
13371         } else {
13372             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13373 
13374             assert(size == 1);
13375             read_vec_element_i32(s, tcg_idx, rm, index, size);
13376 
13377             if (!is_scalar) {
13378                 /* The simplest way to handle the 16x16 indexed ops is to
13379                  * duplicate the index into both halves of the 32 bit tcg_idx
13380                  * and then use the usual Neon helpers.
13381                  */
13382                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13383             }
13384 
13385             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13386                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13387                 TCGv_i64 tcg_passres;
13388 
13389                 if (is_scalar) {
13390                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13391                 } else {
13392                     read_vec_element_i32(s, tcg_op, rn,
13393                                          pass + (is_q * 2), MO_32);
13394                 }
13395 
13396                 tcg_res[pass] = tcg_temp_new_i64();
13397 
13398                 if (opcode == 0xa || opcode == 0xb) {
13399                     /* Non-accumulating ops */
13400                     tcg_passres = tcg_res[pass];
13401                 } else {
13402                     tcg_passres = tcg_temp_new_i64();
13403                 }
13404 
13405                 if (memop & MO_SIGN) {
13406                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13407                 } else {
13408                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13409                 }
13410                 if (satop) {
13411                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
13412                                                       tcg_passres, tcg_passres);
13413                 }
13414 
13415                 if (opcode == 0xa || opcode == 0xb) {
13416                     continue;
13417                 }
13418 
13419                 /* Accumulating op: handle accumulate step */
13420                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13421 
13422                 switch (opcode) {
13423                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13424                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13425                                              tcg_passres);
13426                     break;
13427                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13428                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13429                                              tcg_passres);
13430                     break;
13431                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13432                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13433                     /* fall through */
13434                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13435                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
13436                                                       tcg_res[pass],
13437                                                       tcg_passres);
13438                     break;
13439                 default:
13440                     g_assert_not_reached();
13441                 }
13442             }
13443 
13444             if (is_scalar) {
13445                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13446             }
13447         }
13448 
13449         if (is_scalar) {
13450             tcg_res[1] = tcg_constant_i64(0);
13451         }
13452 
13453         for (pass = 0; pass < 2; pass++) {
13454             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13455         }
13456     }
13457 }
13458 
13459 /* C3.6 Data processing - SIMD, inc Crypto
13460  *
13461  * As the decode gets a little complex we are using a table based
13462  * approach for this part of the decode.
13463  */
13464 static const AArch64DecodeTable data_proc_simd[] = {
13465     /* pattern  ,  mask     ,  fn                        */
13466     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13467     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13468     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13469     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13470     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13471     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13472     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13473     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13474     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13475     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13476     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13477     { 0x2e000000, 0xbf208400, disas_simd_ext },
13478     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13479     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13480     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13481     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13482     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13483     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13484     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13485     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13486     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13487     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13488     { 0x00000000, 0x00000000, NULL }
13489 };
13490 
13491 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13492 {
13493     /* Note that this is called with all non-FP cases from
13494      * table C3-6 so it must UNDEF for entries not specifically
13495      * allocated to instructions in that table.
13496      */
13497     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13498     if (fn) {
13499         fn(s, insn);
13500     } else {
13501         unallocated_encoding(s);
13502     }
13503 }
13504 
13505 /* C3.6 Data processing - SIMD and floating point */
13506 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13507 {
13508     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13509         disas_data_proc_fp(s, insn);
13510     } else {
13511         /* SIMD, including crypto */
13512         disas_data_proc_simd(s, insn);
13513     }
13514 }
13515 
13516 static bool trans_OK(DisasContext *s, arg_OK *a)
13517 {
13518     return true;
13519 }
13520 
13521 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13522 {
13523     s->is_nonstreaming = true;
13524     return true;
13525 }
13526 
13527 /**
13528  * is_guarded_page:
13529  * @env: The cpu environment
13530  * @s: The DisasContext
13531  *
13532  * Return true if the page is guarded.
13533  */
13534 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13535 {
13536     uint64_t addr = s->base.pc_first;
13537 #ifdef CONFIG_USER_ONLY
13538     return page_get_flags(addr) & PAGE_BTI;
13539 #else
13540     CPUTLBEntryFull *full;
13541     void *host;
13542     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13543     int flags;
13544 
13545     /*
13546      * We test this immediately after reading an insn, which means
13547      * that the TLB entry must be present and valid, and thus this
13548      * access will never raise an exception.
13549      */
13550     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13551                               false, &host, &full, 0);
13552     assert(!(flags & TLB_INVALID_MASK));
13553 
13554     return full->extra.arm.guarded;
13555 #endif
13556 }
13557 
13558 /**
13559  * btype_destination_ok:
13560  * @insn: The instruction at the branch destination
13561  * @bt: SCTLR_ELx.BT
13562  * @btype: PSTATE.BTYPE, and is non-zero
13563  *
13564  * On a guarded page, there are a limited number of insns
13565  * that may be present at the branch target:
13566  *   - branch target identifiers,
13567  *   - paciasp, pacibsp,
13568  *   - BRK insn
13569  *   - HLT insn
13570  * Anything else causes a Branch Target Exception.
13571  *
13572  * Return true if the branch is compatible, false to raise BTITRAP.
13573  */
13574 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13575 {
13576     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13577         /* HINT space */
13578         switch (extract32(insn, 5, 7)) {
13579         case 0b011001: /* PACIASP */
13580         case 0b011011: /* PACIBSP */
13581             /*
13582              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13583              * with btype == 3.  Otherwise all btype are ok.
13584              */
13585             return !bt || btype != 3;
13586         case 0b100000: /* BTI */
13587             /* Not compatible with any btype.  */
13588             return false;
13589         case 0b100010: /* BTI c */
13590             /* Not compatible with btype == 3 */
13591             return btype != 3;
13592         case 0b100100: /* BTI j */
13593             /* Not compatible with btype == 2 */
13594             return btype != 2;
13595         case 0b100110: /* BTI jc */
13596             /* Compatible with any btype.  */
13597             return true;
13598         }
13599     } else {
13600         switch (insn & 0xffe0001fu) {
13601         case 0xd4200000u: /* BRK */
13602         case 0xd4400000u: /* HLT */
13603             /* Give priority to the breakpoint exception.  */
13604             return true;
13605         }
13606     }
13607     return false;
13608 }
13609 
13610 /* C3.1 A64 instruction index by encoding */
13611 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13612 {
13613     switch (extract32(insn, 25, 4)) {
13614     case 0x5:
13615     case 0xd:      /* Data processing - register */
13616         disas_data_proc_reg(s, insn);
13617         break;
13618     case 0x7:
13619     case 0xf:      /* Data processing - SIMD and floating point */
13620         disas_data_proc_simd_fp(s, insn);
13621         break;
13622     default:
13623         unallocated_encoding(s);
13624         break;
13625     }
13626 }
13627 
13628 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13629                                           CPUState *cpu)
13630 {
13631     DisasContext *dc = container_of(dcbase, DisasContext, base);
13632     CPUARMState *env = cpu_env(cpu);
13633     ARMCPU *arm_cpu = env_archcpu(env);
13634     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13635     int bound, core_mmu_idx;
13636 
13637     dc->isar = &arm_cpu->isar;
13638     dc->condjmp = 0;
13639     dc->pc_save = dc->base.pc_first;
13640     dc->aarch64 = true;
13641     dc->thumb = false;
13642     dc->sctlr_b = 0;
13643     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13644     dc->condexec_mask = 0;
13645     dc->condexec_cond = 0;
13646     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
13647     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
13648     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
13649     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
13650     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
13651     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
13652 #if !defined(CONFIG_USER_ONLY)
13653     dc->user = (dc->current_el == 0);
13654 #endif
13655     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
13656     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
13657     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
13658     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
13659     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
13660     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
13661     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
13662     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
13663     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
13664     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
13665     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
13666     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
13667     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
13668     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
13669     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
13670     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
13671     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
13672     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
13673     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
13674     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
13675     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
13676     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
13677     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
13678     dc->nv1 = EX_TBFLAG_A64(tb_flags, NV1);
13679     dc->nv2 = EX_TBFLAG_A64(tb_flags, NV2);
13680     dc->nv2_mem_e20 = EX_TBFLAG_A64(tb_flags, NV2_MEM_E20);
13681     dc->nv2_mem_be = EX_TBFLAG_A64(tb_flags, NV2_MEM_BE);
13682     dc->vec_len = 0;
13683     dc->vec_stride = 0;
13684     dc->cp_regs = arm_cpu->cp_regs;
13685     dc->features = env->features;
13686     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
13687     dc->gm_blocksize = arm_cpu->gm_blocksize;
13688 
13689 #ifdef CONFIG_USER_ONLY
13690     /* In sve_probe_page, we assume TBI is enabled. */
13691     tcg_debug_assert(dc->tbid & 1);
13692 #endif
13693 
13694     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
13695 
13696     /* Single step state. The code-generation logic here is:
13697      *  SS_ACTIVE == 0:
13698      *   generate code with no special handling for single-stepping (except
13699      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
13700      *   this happens anyway because those changes are all system register or
13701      *   PSTATE writes).
13702      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
13703      *   emit code for one insn
13704      *   emit code to clear PSTATE.SS
13705      *   emit code to generate software step exception for completed step
13706      *   end TB (as usual for having generated an exception)
13707      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
13708      *   emit code to generate a software step exception
13709      *   end the TB
13710      */
13711     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
13712     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
13713     dc->is_ldex = false;
13714 
13715     /* Bound the number of insns to execute to those left on the page.  */
13716     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
13717 
13718     /* If architectural single step active, limit to 1.  */
13719     if (dc->ss_active) {
13720         bound = 1;
13721     }
13722     dc->base.max_insns = MIN(dc->base.max_insns, bound);
13723 }
13724 
13725 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
13726 {
13727 }
13728 
13729 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
13730 {
13731     DisasContext *dc = container_of(dcbase, DisasContext, base);
13732     target_ulong pc_arg = dc->base.pc_next;
13733 
13734     if (tb_cflags(dcbase->tb) & CF_PCREL) {
13735         pc_arg &= ~TARGET_PAGE_MASK;
13736     }
13737     tcg_gen_insn_start(pc_arg, 0, 0);
13738     dc->insn_start_updated = false;
13739 }
13740 
13741 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
13742 {
13743     DisasContext *s = container_of(dcbase, DisasContext, base);
13744     CPUARMState *env = cpu_env(cpu);
13745     uint64_t pc = s->base.pc_next;
13746     uint32_t insn;
13747 
13748     /* Singlestep exceptions have the highest priority. */
13749     if (s->ss_active && !s->pstate_ss) {
13750         /* Singlestep state is Active-pending.
13751          * If we're in this state at the start of a TB then either
13752          *  a) we just took an exception to an EL which is being debugged
13753          *     and this is the first insn in the exception handler
13754          *  b) debug exceptions were masked and we just unmasked them
13755          *     without changing EL (eg by clearing PSTATE.D)
13756          * In either case we're going to take a swstep exception in the
13757          * "did not step an insn" case, and so the syndrome ISV and EX
13758          * bits should be zero.
13759          */
13760         assert(s->base.num_insns == 1);
13761         gen_swstep_exception(s, 0, 0);
13762         s->base.is_jmp = DISAS_NORETURN;
13763         s->base.pc_next = pc + 4;
13764         return;
13765     }
13766 
13767     if (pc & 3) {
13768         /*
13769          * PC alignment fault.  This has priority over the instruction abort
13770          * that we would receive from a translation fault via arm_ldl_code.
13771          * This should only be possible after an indirect branch, at the
13772          * start of the TB.
13773          */
13774         assert(s->base.num_insns == 1);
13775         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
13776         s->base.is_jmp = DISAS_NORETURN;
13777         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
13778         return;
13779     }
13780 
13781     s->pc_curr = pc;
13782     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
13783     s->insn = insn;
13784     s->base.pc_next = pc + 4;
13785 
13786     s->fp_access_checked = false;
13787     s->sve_access_checked = false;
13788 
13789     if (s->pstate_il) {
13790         /*
13791          * Illegal execution state. This has priority over BTI
13792          * exceptions, but comes after instruction abort exceptions.
13793          */
13794         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
13795         return;
13796     }
13797 
13798     if (dc_isar_feature(aa64_bti, s)) {
13799         if (s->base.num_insns == 1) {
13800             /*
13801              * At the first insn of the TB, compute s->guarded_page.
13802              * We delayed computing this until successfully reading
13803              * the first insn of the TB, above.  This (mostly) ensures
13804              * that the softmmu tlb entry has been populated, and the
13805              * page table GP bit is available.
13806              *
13807              * Note that we need to compute this even if btype == 0,
13808              * because this value is used for BR instructions later
13809              * where ENV is not available.
13810              */
13811             s->guarded_page = is_guarded_page(env, s);
13812 
13813             /* First insn can have btype set to non-zero.  */
13814             tcg_debug_assert(s->btype >= 0);
13815 
13816             /*
13817              * Note that the Branch Target Exception has fairly high
13818              * priority -- below debugging exceptions but above most
13819              * everything else.  This allows us to handle this now
13820              * instead of waiting until the insn is otherwise decoded.
13821              */
13822             if (s->btype != 0
13823                 && s->guarded_page
13824                 && !btype_destination_ok(insn, s->bt, s->btype)) {
13825                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
13826                 return;
13827             }
13828         } else {
13829             /* Not the first insn: btype must be 0.  */
13830             tcg_debug_assert(s->btype == 0);
13831         }
13832     }
13833 
13834     s->is_nonstreaming = false;
13835     if (s->sme_trap_nonstreaming) {
13836         disas_sme_fa64(s, insn);
13837     }
13838 
13839     if (!disas_a64(s, insn) &&
13840         !disas_sme(s, insn) &&
13841         !disas_sve(s, insn)) {
13842         disas_a64_legacy(s, insn);
13843     }
13844 
13845     /*
13846      * After execution of most insns, btype is reset to 0.
13847      * Note that we set btype == -1 when the insn sets btype.
13848      */
13849     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
13850         reset_btype(s);
13851     }
13852 }
13853 
13854 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
13855 {
13856     DisasContext *dc = container_of(dcbase, DisasContext, base);
13857 
13858     if (unlikely(dc->ss_active)) {
13859         /* Note that this means single stepping WFI doesn't halt the CPU.
13860          * For conditional branch insns this is harmless unreachable code as
13861          * gen_goto_tb() has already handled emitting the debug exception
13862          * (and thus a tb-jump is not possible when singlestepping).
13863          */
13864         switch (dc->base.is_jmp) {
13865         default:
13866             gen_a64_update_pc(dc, 4);
13867             /* fall through */
13868         case DISAS_EXIT:
13869         case DISAS_JUMP:
13870             gen_step_complete_exception(dc);
13871             break;
13872         case DISAS_NORETURN:
13873             break;
13874         }
13875     } else {
13876         switch (dc->base.is_jmp) {
13877         case DISAS_NEXT:
13878         case DISAS_TOO_MANY:
13879             gen_goto_tb(dc, 1, 4);
13880             break;
13881         default:
13882         case DISAS_UPDATE_EXIT:
13883             gen_a64_update_pc(dc, 4);
13884             /* fall through */
13885         case DISAS_EXIT:
13886             tcg_gen_exit_tb(NULL, 0);
13887             break;
13888         case DISAS_UPDATE_NOCHAIN:
13889             gen_a64_update_pc(dc, 4);
13890             /* fall through */
13891         case DISAS_JUMP:
13892             tcg_gen_lookup_and_goto_ptr();
13893             break;
13894         case DISAS_NORETURN:
13895         case DISAS_SWI:
13896             break;
13897         case DISAS_WFE:
13898             gen_a64_update_pc(dc, 4);
13899             gen_helper_wfe(tcg_env);
13900             break;
13901         case DISAS_YIELD:
13902             gen_a64_update_pc(dc, 4);
13903             gen_helper_yield(tcg_env);
13904             break;
13905         case DISAS_WFI:
13906             /*
13907              * This is a special case because we don't want to just halt
13908              * the CPU if trying to debug across a WFI.
13909              */
13910             gen_a64_update_pc(dc, 4);
13911             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
13912             /*
13913              * The helper doesn't necessarily throw an exception, but we
13914              * must go back to the main loop to check for interrupts anyway.
13915              */
13916             tcg_gen_exit_tb(NULL, 0);
13917             break;
13918         }
13919     }
13920 }
13921 
13922 const TranslatorOps aarch64_translator_ops = {
13923     .init_disas_context = aarch64_tr_init_disas_context,
13924     .tb_start           = aarch64_tr_tb_start,
13925     .insn_start         = aarch64_tr_insn_start,
13926     .translate_insn     = aarch64_tr_translate_insn,
13927     .tb_stop            = aarch64_tr_tb_stop,
13928 };
13929