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