xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision f80701cb)
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 4-operand operation using an out-of-line helper.  */
728 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
729                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
730 {
731     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
732                        vec_full_reg_offset(s, rn),
733                        vec_full_reg_offset(s, rm),
734                        vec_full_reg_offset(s, ra),
735                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
736 }
737 
738 /*
739  * Expand a 4-operand + fpstatus pointer + simd data value operation using
740  * an out-of-line helper.
741  */
742 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
743                               int rm, int ra, bool is_fp16, int data,
744                               gen_helper_gvec_4_ptr *fn)
745 {
746     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
747     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
748                        vec_full_reg_offset(s, rn),
749                        vec_full_reg_offset(s, rm),
750                        vec_full_reg_offset(s, ra), fpst,
751                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
752 }
753 
754 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
755  * than the 32 bit equivalent.
756  */
757 static inline void gen_set_NZ64(TCGv_i64 result)
758 {
759     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
760     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
761 }
762 
763 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
764 static inline void gen_logic_CC(int sf, TCGv_i64 result)
765 {
766     if (sf) {
767         gen_set_NZ64(result);
768     } else {
769         tcg_gen_extrl_i64_i32(cpu_ZF, result);
770         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
771     }
772     tcg_gen_movi_i32(cpu_CF, 0);
773     tcg_gen_movi_i32(cpu_VF, 0);
774 }
775 
776 /* dest = T0 + T1; compute C, N, V and Z flags */
777 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
778 {
779     TCGv_i64 result, flag, tmp;
780     result = tcg_temp_new_i64();
781     flag = tcg_temp_new_i64();
782     tmp = tcg_temp_new_i64();
783 
784     tcg_gen_movi_i64(tmp, 0);
785     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
786 
787     tcg_gen_extrl_i64_i32(cpu_CF, flag);
788 
789     gen_set_NZ64(result);
790 
791     tcg_gen_xor_i64(flag, result, t0);
792     tcg_gen_xor_i64(tmp, t0, t1);
793     tcg_gen_andc_i64(flag, flag, tmp);
794     tcg_gen_extrh_i64_i32(cpu_VF, flag);
795 
796     tcg_gen_mov_i64(dest, result);
797 }
798 
799 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
800 {
801     TCGv_i32 t0_32 = tcg_temp_new_i32();
802     TCGv_i32 t1_32 = tcg_temp_new_i32();
803     TCGv_i32 tmp = tcg_temp_new_i32();
804 
805     tcg_gen_movi_i32(tmp, 0);
806     tcg_gen_extrl_i64_i32(t0_32, t0);
807     tcg_gen_extrl_i64_i32(t1_32, t1);
808     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
809     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
810     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
811     tcg_gen_xor_i32(tmp, t0_32, t1_32);
812     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
813     tcg_gen_extu_i32_i64(dest, cpu_NF);
814 }
815 
816 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
817 {
818     if (sf) {
819         gen_add64_CC(dest, t0, t1);
820     } else {
821         gen_add32_CC(dest, t0, t1);
822     }
823 }
824 
825 /* dest = T0 - T1; compute C, N, V and Z flags */
826 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
827 {
828     /* 64 bit arithmetic */
829     TCGv_i64 result, flag, tmp;
830 
831     result = tcg_temp_new_i64();
832     flag = tcg_temp_new_i64();
833     tcg_gen_sub_i64(result, t0, t1);
834 
835     gen_set_NZ64(result);
836 
837     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
838     tcg_gen_extrl_i64_i32(cpu_CF, flag);
839 
840     tcg_gen_xor_i64(flag, result, t0);
841     tmp = tcg_temp_new_i64();
842     tcg_gen_xor_i64(tmp, t0, t1);
843     tcg_gen_and_i64(flag, flag, tmp);
844     tcg_gen_extrh_i64_i32(cpu_VF, flag);
845     tcg_gen_mov_i64(dest, result);
846 }
847 
848 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
849 {
850     /* 32 bit arithmetic */
851     TCGv_i32 t0_32 = tcg_temp_new_i32();
852     TCGv_i32 t1_32 = tcg_temp_new_i32();
853     TCGv_i32 tmp;
854 
855     tcg_gen_extrl_i64_i32(t0_32, t0);
856     tcg_gen_extrl_i64_i32(t1_32, t1);
857     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
858     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
859     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
860     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
861     tmp = tcg_temp_new_i32();
862     tcg_gen_xor_i32(tmp, t0_32, t1_32);
863     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
864     tcg_gen_extu_i32_i64(dest, cpu_NF);
865 }
866 
867 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
868 {
869     if (sf) {
870         gen_sub64_CC(dest, t0, t1);
871     } else {
872         gen_sub32_CC(dest, t0, t1);
873     }
874 }
875 
876 /* dest = T0 + T1 + CF; do not compute flags. */
877 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
878 {
879     TCGv_i64 flag = tcg_temp_new_i64();
880     tcg_gen_extu_i32_i64(flag, cpu_CF);
881     tcg_gen_add_i64(dest, t0, t1);
882     tcg_gen_add_i64(dest, dest, flag);
883 
884     if (!sf) {
885         tcg_gen_ext32u_i64(dest, dest);
886     }
887 }
888 
889 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
890 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
891 {
892     if (sf) {
893         TCGv_i64 result = tcg_temp_new_i64();
894         TCGv_i64 cf_64 = tcg_temp_new_i64();
895         TCGv_i64 vf_64 = tcg_temp_new_i64();
896         TCGv_i64 tmp = tcg_temp_new_i64();
897         TCGv_i64 zero = tcg_constant_i64(0);
898 
899         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
900         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
901         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
902         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
903         gen_set_NZ64(result);
904 
905         tcg_gen_xor_i64(vf_64, result, t0);
906         tcg_gen_xor_i64(tmp, t0, t1);
907         tcg_gen_andc_i64(vf_64, vf_64, tmp);
908         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
909 
910         tcg_gen_mov_i64(dest, result);
911     } else {
912         TCGv_i32 t0_32 = tcg_temp_new_i32();
913         TCGv_i32 t1_32 = tcg_temp_new_i32();
914         TCGv_i32 tmp = tcg_temp_new_i32();
915         TCGv_i32 zero = tcg_constant_i32(0);
916 
917         tcg_gen_extrl_i64_i32(t0_32, t0);
918         tcg_gen_extrl_i64_i32(t1_32, t1);
919         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
920         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
921 
922         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
923         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
924         tcg_gen_xor_i32(tmp, t0_32, t1_32);
925         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
926         tcg_gen_extu_i32_i64(dest, cpu_NF);
927     }
928 }
929 
930 /*
931  * Load/Store generators
932  */
933 
934 /*
935  * Store from GPR register to memory.
936  */
937 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
938                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
939                              bool iss_valid,
940                              unsigned int iss_srt,
941                              bool iss_sf, bool iss_ar)
942 {
943     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
944 
945     if (iss_valid) {
946         uint32_t syn;
947 
948         syn = syn_data_abort_with_iss(0,
949                                       (memop & MO_SIZE),
950                                       false,
951                                       iss_srt,
952                                       iss_sf,
953                                       iss_ar,
954                                       0, 0, 0, 0, 0, false);
955         disas_set_insn_syndrome(s, syn);
956     }
957 }
958 
959 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
960                       TCGv_i64 tcg_addr, MemOp memop,
961                       bool iss_valid,
962                       unsigned int iss_srt,
963                       bool iss_sf, bool iss_ar)
964 {
965     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
966                      iss_valid, iss_srt, iss_sf, iss_ar);
967 }
968 
969 /*
970  * Load from memory to GPR register
971  */
972 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
973                              MemOp memop, bool extend, int memidx,
974                              bool iss_valid, unsigned int iss_srt,
975                              bool iss_sf, bool iss_ar)
976 {
977     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
978 
979     if (extend && (memop & MO_SIGN)) {
980         g_assert((memop & MO_SIZE) <= MO_32);
981         tcg_gen_ext32u_i64(dest, dest);
982     }
983 
984     if (iss_valid) {
985         uint32_t syn;
986 
987         syn = syn_data_abort_with_iss(0,
988                                       (memop & MO_SIZE),
989                                       (memop & MO_SIGN) != 0,
990                                       iss_srt,
991                                       iss_sf,
992                                       iss_ar,
993                                       0, 0, 0, 0, 0, false);
994         disas_set_insn_syndrome(s, syn);
995     }
996 }
997 
998 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
999                       MemOp memop, bool extend,
1000                       bool iss_valid, unsigned int iss_srt,
1001                       bool iss_sf, bool iss_ar)
1002 {
1003     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1004                      iss_valid, iss_srt, iss_sf, iss_ar);
1005 }
1006 
1007 /*
1008  * Store from FP register to memory
1009  */
1010 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1011 {
1012     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1013     TCGv_i64 tmplo = tcg_temp_new_i64();
1014 
1015     tcg_gen_ld_i64(tmplo, tcg_env, fp_reg_offset(s, srcidx, MO_64));
1016 
1017     if ((mop & MO_SIZE) < MO_128) {
1018         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1019     } else {
1020         TCGv_i64 tmphi = tcg_temp_new_i64();
1021         TCGv_i128 t16 = tcg_temp_new_i128();
1022 
1023         tcg_gen_ld_i64(tmphi, tcg_env, fp_reg_hi_offset(s, srcidx));
1024         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1025 
1026         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1027     }
1028 }
1029 
1030 /*
1031  * Load from memory to FP register
1032  */
1033 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1034 {
1035     /* This always zero-extends and writes to a full 128 bit wide vector */
1036     TCGv_i64 tmplo = tcg_temp_new_i64();
1037     TCGv_i64 tmphi = NULL;
1038 
1039     if ((mop & MO_SIZE) < MO_128) {
1040         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1041     } else {
1042         TCGv_i128 t16 = tcg_temp_new_i128();
1043 
1044         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1045 
1046         tmphi = tcg_temp_new_i64();
1047         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1048     }
1049 
1050     tcg_gen_st_i64(tmplo, tcg_env, fp_reg_offset(s, destidx, MO_64));
1051 
1052     if (tmphi) {
1053         tcg_gen_st_i64(tmphi, tcg_env, fp_reg_hi_offset(s, destidx));
1054     }
1055     clear_vec_high(s, tmphi != NULL, destidx);
1056 }
1057 
1058 /*
1059  * Vector load/store helpers.
1060  *
1061  * The principal difference between this and a FP load is that we don't
1062  * zero extend as we are filling a partial chunk of the vector register.
1063  * These functions don't support 128 bit loads/stores, which would be
1064  * normal load/store operations.
1065  *
1066  * The _i32 versions are useful when operating on 32 bit quantities
1067  * (eg for floating point single or using Neon helper functions).
1068  */
1069 
1070 /* Get value of an element within a vector register */
1071 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1072                              int element, MemOp memop)
1073 {
1074     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1075     switch ((unsigned)memop) {
1076     case MO_8:
1077         tcg_gen_ld8u_i64(tcg_dest, tcg_env, vect_off);
1078         break;
1079     case MO_16:
1080         tcg_gen_ld16u_i64(tcg_dest, tcg_env, vect_off);
1081         break;
1082     case MO_32:
1083         tcg_gen_ld32u_i64(tcg_dest, tcg_env, vect_off);
1084         break;
1085     case MO_8|MO_SIGN:
1086         tcg_gen_ld8s_i64(tcg_dest, tcg_env, vect_off);
1087         break;
1088     case MO_16|MO_SIGN:
1089         tcg_gen_ld16s_i64(tcg_dest, tcg_env, vect_off);
1090         break;
1091     case MO_32|MO_SIGN:
1092         tcg_gen_ld32s_i64(tcg_dest, tcg_env, vect_off);
1093         break;
1094     case MO_64:
1095     case MO_64|MO_SIGN:
1096         tcg_gen_ld_i64(tcg_dest, tcg_env, vect_off);
1097         break;
1098     default:
1099         g_assert_not_reached();
1100     }
1101 }
1102 
1103 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1104                                  int element, MemOp memop)
1105 {
1106     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1107     switch (memop) {
1108     case MO_8:
1109         tcg_gen_ld8u_i32(tcg_dest, tcg_env, vect_off);
1110         break;
1111     case MO_16:
1112         tcg_gen_ld16u_i32(tcg_dest, tcg_env, vect_off);
1113         break;
1114     case MO_8|MO_SIGN:
1115         tcg_gen_ld8s_i32(tcg_dest, tcg_env, vect_off);
1116         break;
1117     case MO_16|MO_SIGN:
1118         tcg_gen_ld16s_i32(tcg_dest, tcg_env, vect_off);
1119         break;
1120     case MO_32:
1121     case MO_32|MO_SIGN:
1122         tcg_gen_ld_i32(tcg_dest, tcg_env, vect_off);
1123         break;
1124     default:
1125         g_assert_not_reached();
1126     }
1127 }
1128 
1129 /* Set value of an element within a vector register */
1130 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1131                               int element, MemOp memop)
1132 {
1133     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1134     switch (memop) {
1135     case MO_8:
1136         tcg_gen_st8_i64(tcg_src, tcg_env, vect_off);
1137         break;
1138     case MO_16:
1139         tcg_gen_st16_i64(tcg_src, tcg_env, vect_off);
1140         break;
1141     case MO_32:
1142         tcg_gen_st32_i64(tcg_src, tcg_env, vect_off);
1143         break;
1144     case MO_64:
1145         tcg_gen_st_i64(tcg_src, tcg_env, vect_off);
1146         break;
1147     default:
1148         g_assert_not_reached();
1149     }
1150 }
1151 
1152 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1153                                   int destidx, int element, MemOp memop)
1154 {
1155     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1156     switch (memop) {
1157     case MO_8:
1158         tcg_gen_st8_i32(tcg_src, tcg_env, vect_off);
1159         break;
1160     case MO_16:
1161         tcg_gen_st16_i32(tcg_src, tcg_env, vect_off);
1162         break;
1163     case MO_32:
1164         tcg_gen_st_i32(tcg_src, tcg_env, vect_off);
1165         break;
1166     default:
1167         g_assert_not_reached();
1168     }
1169 }
1170 
1171 /* Store from vector register to memory */
1172 static void do_vec_st(DisasContext *s, int srcidx, int element,
1173                       TCGv_i64 tcg_addr, MemOp mop)
1174 {
1175     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1176 
1177     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1178     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1179 }
1180 
1181 /* Load from memory to vector register */
1182 static void do_vec_ld(DisasContext *s, int destidx, int element,
1183                       TCGv_i64 tcg_addr, MemOp mop)
1184 {
1185     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1186 
1187     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1188     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1189 }
1190 
1191 /* Check that FP/Neon access is enabled. If it is, return
1192  * true. If not, emit code to generate an appropriate exception,
1193  * and return false; the caller should not emit any code for
1194  * the instruction. Note that this check must happen after all
1195  * unallocated-encoding checks (otherwise the syndrome information
1196  * for the resulting exception will be incorrect).
1197  */
1198 static bool fp_access_check_only(DisasContext *s)
1199 {
1200     if (s->fp_excp_el) {
1201         assert(!s->fp_access_checked);
1202         s->fp_access_checked = true;
1203 
1204         gen_exception_insn_el(s, 0, EXCP_UDEF,
1205                               syn_fp_access_trap(1, 0xe, false, 0),
1206                               s->fp_excp_el);
1207         return false;
1208     }
1209     s->fp_access_checked = true;
1210     return true;
1211 }
1212 
1213 static bool fp_access_check(DisasContext *s)
1214 {
1215     if (!fp_access_check_only(s)) {
1216         return false;
1217     }
1218     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1219         gen_exception_insn(s, 0, EXCP_UDEF,
1220                            syn_smetrap(SME_ET_Streaming, false));
1221         return false;
1222     }
1223     return true;
1224 }
1225 
1226 /*
1227  * Check that SVE access is enabled.  If it is, return true.
1228  * If not, emit code to generate an appropriate exception and return false.
1229  * This function corresponds to CheckSVEEnabled().
1230  */
1231 bool sve_access_check(DisasContext *s)
1232 {
1233     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1234         assert(dc_isar_feature(aa64_sme, s));
1235         if (!sme_sm_enabled_check(s)) {
1236             goto fail_exit;
1237         }
1238     } else if (s->sve_excp_el) {
1239         gen_exception_insn_el(s, 0, EXCP_UDEF,
1240                               syn_sve_access_trap(), s->sve_excp_el);
1241         goto fail_exit;
1242     }
1243     s->sve_access_checked = true;
1244     return fp_access_check(s);
1245 
1246  fail_exit:
1247     /* Assert that we only raise one exception per instruction. */
1248     assert(!s->sve_access_checked);
1249     s->sve_access_checked = true;
1250     return false;
1251 }
1252 
1253 /*
1254  * Check that SME access is enabled, raise an exception if not.
1255  * Note that this function corresponds to CheckSMEAccess and is
1256  * only used directly for cpregs.
1257  */
1258 static bool sme_access_check(DisasContext *s)
1259 {
1260     if (s->sme_excp_el) {
1261         gen_exception_insn_el(s, 0, EXCP_UDEF,
1262                               syn_smetrap(SME_ET_AccessTrap, false),
1263                               s->sme_excp_el);
1264         return false;
1265     }
1266     return true;
1267 }
1268 
1269 /* This function corresponds to CheckSMEEnabled. */
1270 bool sme_enabled_check(DisasContext *s)
1271 {
1272     /*
1273      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1274      * to be zero when fp_excp_el has priority.  This is because we need
1275      * sme_excp_el by itself for cpregs access checks.
1276      */
1277     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1278         s->fp_access_checked = true;
1279         return sme_access_check(s);
1280     }
1281     return fp_access_check_only(s);
1282 }
1283 
1284 /* Common subroutine for CheckSMEAnd*Enabled. */
1285 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1286 {
1287     if (!sme_enabled_check(s)) {
1288         return false;
1289     }
1290     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1291         gen_exception_insn(s, 0, EXCP_UDEF,
1292                            syn_smetrap(SME_ET_NotStreaming, false));
1293         return false;
1294     }
1295     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1296         gen_exception_insn(s, 0, EXCP_UDEF,
1297                            syn_smetrap(SME_ET_InactiveZA, false));
1298         return false;
1299     }
1300     return true;
1301 }
1302 
1303 /*
1304  * Expanders for AdvSIMD translation functions.
1305  */
1306 
1307 static bool do_gvec_op2_ool(DisasContext *s, arg_qrr_e *a, int data,
1308                             gen_helper_gvec_2 *fn)
1309 {
1310     if (!a->q && a->esz == MO_64) {
1311         return false;
1312     }
1313     if (fp_access_check(s)) {
1314         gen_gvec_op2_ool(s, a->q, a->rd, a->rn, data, fn);
1315     }
1316     return true;
1317 }
1318 
1319 static bool do_gvec_op3_ool(DisasContext *s, arg_qrrr_e *a, int data,
1320                             gen_helper_gvec_3 *fn)
1321 {
1322     if (!a->q && a->esz == MO_64) {
1323         return false;
1324     }
1325     if (fp_access_check(s)) {
1326         gen_gvec_op3_ool(s, a->q, a->rd, a->rn, a->rm, data, fn);
1327     }
1328     return true;
1329 }
1330 
1331 static bool do_gvec_fn3(DisasContext *s, arg_qrrr_e *a, GVecGen3Fn *fn)
1332 {
1333     if (!a->q && a->esz == MO_64) {
1334         return false;
1335     }
1336     if (fp_access_check(s)) {
1337         gen_gvec_fn3(s, a->q, a->rd, a->rn, a->rm, fn, a->esz);
1338     }
1339     return true;
1340 }
1341 
1342 static bool do_gvec_fn3_no64(DisasContext *s, arg_qrrr_e *a, GVecGen3Fn *fn)
1343 {
1344     if (a->esz == MO_64) {
1345         return false;
1346     }
1347     if (fp_access_check(s)) {
1348         gen_gvec_fn3(s, a->q, a->rd, a->rn, a->rm, fn, a->esz);
1349     }
1350     return true;
1351 }
1352 
1353 static bool do_gvec_fn3_no8_no64(DisasContext *s, arg_qrrr_e *a, GVecGen3Fn *fn)
1354 {
1355     if (a->esz == MO_8) {
1356         return false;
1357     }
1358     return do_gvec_fn3_no64(s, a, fn);
1359 }
1360 
1361 static bool do_gvec_fn4(DisasContext *s, arg_qrrrr_e *a, GVecGen4Fn *fn)
1362 {
1363     if (!a->q && a->esz == MO_64) {
1364         return false;
1365     }
1366     if (fp_access_check(s)) {
1367         gen_gvec_fn4(s, a->q, a->rd, a->rn, a->rm, a->ra, fn, a->esz);
1368     }
1369     return true;
1370 }
1371 
1372 /*
1373  * This utility function is for doing register extension with an
1374  * optional shift. You will likely want to pass a temporary for the
1375  * destination register. See DecodeRegExtend() in the ARM ARM.
1376  */
1377 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1378                               int option, unsigned int shift)
1379 {
1380     int extsize = extract32(option, 0, 2);
1381     bool is_signed = extract32(option, 2, 1);
1382 
1383     tcg_gen_ext_i64(tcg_out, tcg_in, extsize | (is_signed ? MO_SIGN : 0));
1384     tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1385 }
1386 
1387 static inline void gen_check_sp_alignment(DisasContext *s)
1388 {
1389     /* The AArch64 architecture mandates that (if enabled via PSTATE
1390      * or SCTLR bits) there is a check that SP is 16-aligned on every
1391      * SP-relative load or store (with an exception generated if it is not).
1392      * In line with general QEMU practice regarding misaligned accesses,
1393      * we omit these checks for the sake of guest program performance.
1394      * This function is provided as a hook so we can more easily add these
1395      * checks in future (possibly as a "favour catching guest program bugs
1396      * over speed" user selectable option).
1397      */
1398 }
1399 
1400 /*
1401  * This provides a simple table based table lookup decoder. It is
1402  * intended to be used when the relevant bits for decode are too
1403  * awkwardly placed and switch/if based logic would be confusing and
1404  * deeply nested. Since it's a linear search through the table, tables
1405  * should be kept small.
1406  *
1407  * It returns the first handler where insn & mask == pattern, or
1408  * NULL if there is no match.
1409  * The table is terminated by an empty mask (i.e. 0)
1410  */
1411 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1412                                                uint32_t insn)
1413 {
1414     const AArch64DecodeTable *tptr = table;
1415 
1416     while (tptr->mask) {
1417         if ((insn & tptr->mask) == tptr->pattern) {
1418             return tptr->disas_fn;
1419         }
1420         tptr++;
1421     }
1422     return NULL;
1423 }
1424 
1425 /*
1426  * The instruction disassembly implemented here matches
1427  * the instruction encoding classifications in chapter C4
1428  * of the ARM Architecture Reference Manual (DDI0487B_a);
1429  * classification names and decode diagrams here should generally
1430  * match up with those in the manual.
1431  */
1432 
1433 static bool trans_B(DisasContext *s, arg_i *a)
1434 {
1435     reset_btype(s);
1436     gen_goto_tb(s, 0, a->imm);
1437     return true;
1438 }
1439 
1440 static bool trans_BL(DisasContext *s, arg_i *a)
1441 {
1442     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1443     reset_btype(s);
1444     gen_goto_tb(s, 0, a->imm);
1445     return true;
1446 }
1447 
1448 
1449 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1450 {
1451     DisasLabel match;
1452     TCGv_i64 tcg_cmp;
1453 
1454     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1455     reset_btype(s);
1456 
1457     match = gen_disas_label(s);
1458     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1459                         tcg_cmp, 0, match.label);
1460     gen_goto_tb(s, 0, 4);
1461     set_disas_label(s, match);
1462     gen_goto_tb(s, 1, a->imm);
1463     return true;
1464 }
1465 
1466 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1467 {
1468     DisasLabel match;
1469     TCGv_i64 tcg_cmp;
1470 
1471     tcg_cmp = tcg_temp_new_i64();
1472     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1473 
1474     reset_btype(s);
1475 
1476     match = gen_disas_label(s);
1477     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1478                         tcg_cmp, 0, match.label);
1479     gen_goto_tb(s, 0, 4);
1480     set_disas_label(s, match);
1481     gen_goto_tb(s, 1, a->imm);
1482     return true;
1483 }
1484 
1485 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1486 {
1487     /* BC.cond is only present with FEAT_HBC */
1488     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
1489         return false;
1490     }
1491     reset_btype(s);
1492     if (a->cond < 0x0e) {
1493         /* genuinely conditional branches */
1494         DisasLabel match = gen_disas_label(s);
1495         arm_gen_test_cc(a->cond, match.label);
1496         gen_goto_tb(s, 0, 4);
1497         set_disas_label(s, match);
1498         gen_goto_tb(s, 1, a->imm);
1499     } else {
1500         /* 0xe and 0xf are both "always" conditions */
1501         gen_goto_tb(s, 0, a->imm);
1502     }
1503     return true;
1504 }
1505 
1506 static void set_btype_for_br(DisasContext *s, int rn)
1507 {
1508     if (dc_isar_feature(aa64_bti, s)) {
1509         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1510         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1511     }
1512 }
1513 
1514 static void set_btype_for_blr(DisasContext *s)
1515 {
1516     if (dc_isar_feature(aa64_bti, s)) {
1517         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1518         set_btype(s, 2);
1519     }
1520 }
1521 
1522 static bool trans_BR(DisasContext *s, arg_r *a)
1523 {
1524     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1525     set_btype_for_br(s, a->rn);
1526     s->base.is_jmp = DISAS_JUMP;
1527     return true;
1528 }
1529 
1530 static bool trans_BLR(DisasContext *s, arg_r *a)
1531 {
1532     TCGv_i64 dst = cpu_reg(s, a->rn);
1533     TCGv_i64 lr = cpu_reg(s, 30);
1534     if (dst == lr) {
1535         TCGv_i64 tmp = tcg_temp_new_i64();
1536         tcg_gen_mov_i64(tmp, dst);
1537         dst = tmp;
1538     }
1539     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1540     gen_a64_set_pc(s, dst);
1541     set_btype_for_blr(s);
1542     s->base.is_jmp = DISAS_JUMP;
1543     return true;
1544 }
1545 
1546 static bool trans_RET(DisasContext *s, arg_r *a)
1547 {
1548     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1549     s->base.is_jmp = DISAS_JUMP;
1550     return true;
1551 }
1552 
1553 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1554                                    TCGv_i64 modifier, bool use_key_a)
1555 {
1556     TCGv_i64 truedst;
1557     /*
1558      * Return the branch target for a BRAA/RETA/etc, which is either
1559      * just the destination dst, or that value with the pauth check
1560      * done and the code removed from the high bits.
1561      */
1562     if (!s->pauth_active) {
1563         return dst;
1564     }
1565 
1566     truedst = tcg_temp_new_i64();
1567     if (use_key_a) {
1568         gen_helper_autia_combined(truedst, tcg_env, dst, modifier);
1569     } else {
1570         gen_helper_autib_combined(truedst, tcg_env, dst, modifier);
1571     }
1572     return truedst;
1573 }
1574 
1575 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1576 {
1577     TCGv_i64 dst;
1578 
1579     if (!dc_isar_feature(aa64_pauth, s)) {
1580         return false;
1581     }
1582 
1583     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1584     gen_a64_set_pc(s, dst);
1585     set_btype_for_br(s, a->rn);
1586     s->base.is_jmp = DISAS_JUMP;
1587     return true;
1588 }
1589 
1590 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1591 {
1592     TCGv_i64 dst, lr;
1593 
1594     if (!dc_isar_feature(aa64_pauth, s)) {
1595         return false;
1596     }
1597 
1598     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1599     lr = cpu_reg(s, 30);
1600     if (dst == lr) {
1601         TCGv_i64 tmp = tcg_temp_new_i64();
1602         tcg_gen_mov_i64(tmp, dst);
1603         dst = tmp;
1604     }
1605     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1606     gen_a64_set_pc(s, dst);
1607     set_btype_for_blr(s);
1608     s->base.is_jmp = DISAS_JUMP;
1609     return true;
1610 }
1611 
1612 static bool trans_RETA(DisasContext *s, arg_reta *a)
1613 {
1614     TCGv_i64 dst;
1615 
1616     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1617     gen_a64_set_pc(s, dst);
1618     s->base.is_jmp = DISAS_JUMP;
1619     return true;
1620 }
1621 
1622 static bool trans_BRA(DisasContext *s, arg_bra *a)
1623 {
1624     TCGv_i64 dst;
1625 
1626     if (!dc_isar_feature(aa64_pauth, s)) {
1627         return false;
1628     }
1629     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1630     gen_a64_set_pc(s, dst);
1631     set_btype_for_br(s, a->rn);
1632     s->base.is_jmp = DISAS_JUMP;
1633     return true;
1634 }
1635 
1636 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1637 {
1638     TCGv_i64 dst, lr;
1639 
1640     if (!dc_isar_feature(aa64_pauth, s)) {
1641         return false;
1642     }
1643     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1644     lr = cpu_reg(s, 30);
1645     if (dst == lr) {
1646         TCGv_i64 tmp = tcg_temp_new_i64();
1647         tcg_gen_mov_i64(tmp, dst);
1648         dst = tmp;
1649     }
1650     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1651     gen_a64_set_pc(s, dst);
1652     set_btype_for_blr(s);
1653     s->base.is_jmp = DISAS_JUMP;
1654     return true;
1655 }
1656 
1657 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1658 {
1659     TCGv_i64 dst;
1660 
1661     if (s->current_el == 0) {
1662         return false;
1663     }
1664     if (s->trap_eret) {
1665         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(0), 2);
1666         return true;
1667     }
1668     dst = tcg_temp_new_i64();
1669     tcg_gen_ld_i64(dst, tcg_env,
1670                    offsetof(CPUARMState, elr_el[s->current_el]));
1671 
1672     translator_io_start(&s->base);
1673 
1674     gen_helper_exception_return(tcg_env, dst);
1675     /* Must exit loop to check un-masked IRQs */
1676     s->base.is_jmp = DISAS_EXIT;
1677     return true;
1678 }
1679 
1680 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1681 {
1682     TCGv_i64 dst;
1683 
1684     if (!dc_isar_feature(aa64_pauth, s)) {
1685         return false;
1686     }
1687     if (s->current_el == 0) {
1688         return false;
1689     }
1690     /* The FGT trap takes precedence over an auth trap. */
1691     if (s->trap_eret) {
1692         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(a->m ? 3 : 2), 2);
1693         return true;
1694     }
1695     dst = tcg_temp_new_i64();
1696     tcg_gen_ld_i64(dst, tcg_env,
1697                    offsetof(CPUARMState, elr_el[s->current_el]));
1698 
1699     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1700 
1701     translator_io_start(&s->base);
1702 
1703     gen_helper_exception_return(tcg_env, dst);
1704     /* Must exit loop to check un-masked IRQs */
1705     s->base.is_jmp = DISAS_EXIT;
1706     return true;
1707 }
1708 
1709 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1710 {
1711     return true;
1712 }
1713 
1714 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1715 {
1716     /*
1717      * When running in MTTCG we don't generate jumps to the yield and
1718      * WFE helpers as it won't affect the scheduling of other vCPUs.
1719      * If we wanted to more completely model WFE/SEV so we don't busy
1720      * spin unnecessarily we would need to do something more involved.
1721      */
1722     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1723         s->base.is_jmp = DISAS_YIELD;
1724     }
1725     return true;
1726 }
1727 
1728 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1729 {
1730     s->base.is_jmp = DISAS_WFI;
1731     return true;
1732 }
1733 
1734 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1735 {
1736     /*
1737      * When running in MTTCG we don't generate jumps to the yield and
1738      * WFE helpers as it won't affect the scheduling of other vCPUs.
1739      * If we wanted to more completely model WFE/SEV so we don't busy
1740      * spin unnecessarily we would need to do something more involved.
1741      */
1742     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1743         s->base.is_jmp = DISAS_WFE;
1744     }
1745     return true;
1746 }
1747 
1748 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1749 {
1750     if (s->pauth_active) {
1751         gen_helper_xpaci(cpu_X[30], tcg_env, cpu_X[30]);
1752     }
1753     return true;
1754 }
1755 
1756 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1757 {
1758     if (s->pauth_active) {
1759         gen_helper_pacia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1760     }
1761     return true;
1762 }
1763 
1764 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1765 {
1766     if (s->pauth_active) {
1767         gen_helper_pacib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1768     }
1769     return true;
1770 }
1771 
1772 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1773 {
1774     if (s->pauth_active) {
1775         gen_helper_autia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1776     }
1777     return true;
1778 }
1779 
1780 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1781 {
1782     if (s->pauth_active) {
1783         gen_helper_autib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1784     }
1785     return true;
1786 }
1787 
1788 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1789 {
1790     /* Without RAS, we must implement this as NOP. */
1791     if (dc_isar_feature(aa64_ras, s)) {
1792         /*
1793          * QEMU does not have a source of physical SErrors,
1794          * so we are only concerned with virtual SErrors.
1795          * The pseudocode in the ARM for this case is
1796          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1797          *      AArch64.vESBOperation();
1798          * Most of the condition can be evaluated at translation time.
1799          * Test for EL2 present, and defer test for SEL2 to runtime.
1800          */
1801         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1802             gen_helper_vesb(tcg_env);
1803         }
1804     }
1805     return true;
1806 }
1807 
1808 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1809 {
1810     if (s->pauth_active) {
1811         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1812     }
1813     return true;
1814 }
1815 
1816 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1817 {
1818     if (s->pauth_active) {
1819         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1820     }
1821     return true;
1822 }
1823 
1824 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1825 {
1826     if (s->pauth_active) {
1827         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1828     }
1829     return true;
1830 }
1831 
1832 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1833 {
1834     if (s->pauth_active) {
1835         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1836     }
1837     return true;
1838 }
1839 
1840 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1841 {
1842     if (s->pauth_active) {
1843         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1844     }
1845     return true;
1846 }
1847 
1848 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1849 {
1850     if (s->pauth_active) {
1851         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1852     }
1853     return true;
1854 }
1855 
1856 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1857 {
1858     if (s->pauth_active) {
1859         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1860     }
1861     return true;
1862 }
1863 
1864 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1865 {
1866     if (s->pauth_active) {
1867         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1868     }
1869     return true;
1870 }
1871 
1872 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1873 {
1874     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1875     return true;
1876 }
1877 
1878 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1879 {
1880     /* We handle DSB and DMB the same way */
1881     TCGBar bar;
1882 
1883     switch (a->types) {
1884     case 1: /* MBReqTypes_Reads */
1885         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1886         break;
1887     case 2: /* MBReqTypes_Writes */
1888         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1889         break;
1890     default: /* MBReqTypes_All */
1891         bar = TCG_BAR_SC | TCG_MO_ALL;
1892         break;
1893     }
1894     tcg_gen_mb(bar);
1895     return true;
1896 }
1897 
1898 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1899 {
1900     /*
1901      * We need to break the TB after this insn to execute
1902      * self-modifying code correctly and also to take
1903      * any pending interrupts immediately.
1904      */
1905     reset_btype(s);
1906     gen_goto_tb(s, 0, 4);
1907     return true;
1908 }
1909 
1910 static bool trans_SB(DisasContext *s, arg_SB *a)
1911 {
1912     if (!dc_isar_feature(aa64_sb, s)) {
1913         return false;
1914     }
1915     /*
1916      * TODO: There is no speculation barrier opcode for TCG;
1917      * MB and end the TB instead.
1918      */
1919     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1920     gen_goto_tb(s, 0, 4);
1921     return true;
1922 }
1923 
1924 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1925 {
1926     if (!dc_isar_feature(aa64_condm_4, s)) {
1927         return false;
1928     }
1929     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1930     return true;
1931 }
1932 
1933 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1934 {
1935     TCGv_i32 z;
1936 
1937     if (!dc_isar_feature(aa64_condm_5, s)) {
1938         return false;
1939     }
1940 
1941     z = tcg_temp_new_i32();
1942 
1943     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1944 
1945     /*
1946      * (!C & !Z) << 31
1947      * (!(C | Z)) << 31
1948      * ~((C | Z) << 31)
1949      * ~-(C | Z)
1950      * (C | Z) - 1
1951      */
1952     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1953     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1954 
1955     /* !(Z & C) */
1956     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1957     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1958 
1959     /* (!C & Z) << 31 -> -(Z & ~C) */
1960     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1961     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1962 
1963     /* C | Z */
1964     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1965 
1966     return true;
1967 }
1968 
1969 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1970 {
1971     if (!dc_isar_feature(aa64_condm_5, s)) {
1972         return false;
1973     }
1974 
1975     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1976     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1977 
1978     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1979     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1980 
1981     tcg_gen_movi_i32(cpu_NF, 0);
1982     tcg_gen_movi_i32(cpu_VF, 0);
1983 
1984     return true;
1985 }
1986 
1987 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1988 {
1989     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1990         return false;
1991     }
1992     if (a->imm & 1) {
1993         set_pstate_bits(PSTATE_UAO);
1994     } else {
1995         clear_pstate_bits(PSTATE_UAO);
1996     }
1997     gen_rebuild_hflags(s);
1998     s->base.is_jmp = DISAS_TOO_MANY;
1999     return true;
2000 }
2001 
2002 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
2003 {
2004     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
2005         return false;
2006     }
2007     if (a->imm & 1) {
2008         set_pstate_bits(PSTATE_PAN);
2009     } else {
2010         clear_pstate_bits(PSTATE_PAN);
2011     }
2012     gen_rebuild_hflags(s);
2013     s->base.is_jmp = DISAS_TOO_MANY;
2014     return true;
2015 }
2016 
2017 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
2018 {
2019     if (s->current_el == 0) {
2020         return false;
2021     }
2022     gen_helper_msr_i_spsel(tcg_env, tcg_constant_i32(a->imm & PSTATE_SP));
2023     s->base.is_jmp = DISAS_TOO_MANY;
2024     return true;
2025 }
2026 
2027 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
2028 {
2029     if (!dc_isar_feature(aa64_ssbs, s)) {
2030         return false;
2031     }
2032     if (a->imm & 1) {
2033         set_pstate_bits(PSTATE_SSBS);
2034     } else {
2035         clear_pstate_bits(PSTATE_SSBS);
2036     }
2037     /* Don't need to rebuild hflags since SSBS is a nop */
2038     s->base.is_jmp = DISAS_TOO_MANY;
2039     return true;
2040 }
2041 
2042 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
2043 {
2044     if (!dc_isar_feature(aa64_dit, s)) {
2045         return false;
2046     }
2047     if (a->imm & 1) {
2048         set_pstate_bits(PSTATE_DIT);
2049     } else {
2050         clear_pstate_bits(PSTATE_DIT);
2051     }
2052     /* There's no need to rebuild hflags because DIT is a nop */
2053     s->base.is_jmp = DISAS_TOO_MANY;
2054     return true;
2055 }
2056 
2057 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2058 {
2059     if (dc_isar_feature(aa64_mte, s)) {
2060         /* Full MTE is enabled -- set the TCO bit as directed. */
2061         if (a->imm & 1) {
2062             set_pstate_bits(PSTATE_TCO);
2063         } else {
2064             clear_pstate_bits(PSTATE_TCO);
2065         }
2066         gen_rebuild_hflags(s);
2067         /* Many factors, including TCO, go into MTE_ACTIVE. */
2068         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2069         return true;
2070     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2071         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2072         return true;
2073     } else {
2074         /* Insn not present */
2075         return false;
2076     }
2077 }
2078 
2079 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2080 {
2081     gen_helper_msr_i_daifset(tcg_env, tcg_constant_i32(a->imm));
2082     s->base.is_jmp = DISAS_TOO_MANY;
2083     return true;
2084 }
2085 
2086 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2087 {
2088     gen_helper_msr_i_daifclear(tcg_env, tcg_constant_i32(a->imm));
2089     /* Exit the cpu loop to re-evaluate pending IRQs. */
2090     s->base.is_jmp = DISAS_UPDATE_EXIT;
2091     return true;
2092 }
2093 
2094 static bool trans_MSR_i_ALLINT(DisasContext *s, arg_i *a)
2095 {
2096     if (!dc_isar_feature(aa64_nmi, s) || s->current_el == 0) {
2097         return false;
2098     }
2099 
2100     if (a->imm == 0) {
2101         clear_pstate_bits(PSTATE_ALLINT);
2102     } else if (s->current_el > 1) {
2103         set_pstate_bits(PSTATE_ALLINT);
2104     } else {
2105         gen_helper_msr_set_allint_el1(tcg_env);
2106     }
2107 
2108     /* Exit the cpu loop to re-evaluate pending IRQs. */
2109     s->base.is_jmp = DISAS_UPDATE_EXIT;
2110     return true;
2111 }
2112 
2113 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2114 {
2115     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2116         return false;
2117     }
2118     if (sme_access_check(s)) {
2119         int old = s->pstate_sm | (s->pstate_za << 1);
2120         int new = a->imm * 3;
2121 
2122         if ((old ^ new) & a->mask) {
2123             /* At least one bit changes. */
2124             gen_helper_set_svcr(tcg_env, tcg_constant_i32(new),
2125                                 tcg_constant_i32(a->mask));
2126             s->base.is_jmp = DISAS_TOO_MANY;
2127         }
2128     }
2129     return true;
2130 }
2131 
2132 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2133 {
2134     TCGv_i32 tmp = tcg_temp_new_i32();
2135     TCGv_i32 nzcv = tcg_temp_new_i32();
2136 
2137     /* build bit 31, N */
2138     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2139     /* build bit 30, Z */
2140     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2141     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2142     /* build bit 29, C */
2143     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2144     /* build bit 28, V */
2145     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2146     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2147     /* generate result */
2148     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2149 }
2150 
2151 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2152 {
2153     TCGv_i32 nzcv = tcg_temp_new_i32();
2154 
2155     /* take NZCV from R[t] */
2156     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2157 
2158     /* bit 31, N */
2159     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2160     /* bit 30, Z */
2161     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2162     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2163     /* bit 29, C */
2164     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2165     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2166     /* bit 28, V */
2167     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2168     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2169 }
2170 
2171 static void gen_sysreg_undef(DisasContext *s, bool isread,
2172                              uint8_t op0, uint8_t op1, uint8_t op2,
2173                              uint8_t crn, uint8_t crm, uint8_t rt)
2174 {
2175     /*
2176      * Generate code to emit an UNDEF with correct syndrome
2177      * information for a failed system register access.
2178      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2179      * but if FEAT_IDST is implemented then read accesses to registers
2180      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2181      * syndrome.
2182      */
2183     uint32_t syndrome;
2184 
2185     if (isread && dc_isar_feature(aa64_ids, s) &&
2186         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2187         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2188     } else {
2189         syndrome = syn_uncategorized();
2190     }
2191     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2192 }
2193 
2194 /* MRS - move from system register
2195  * MSR (register) - move to system register
2196  * SYS
2197  * SYSL
2198  * These are all essentially the same insn in 'read' and 'write'
2199  * versions, with varying op0 fields.
2200  */
2201 static void handle_sys(DisasContext *s, bool isread,
2202                        unsigned int op0, unsigned int op1, unsigned int op2,
2203                        unsigned int crn, unsigned int crm, unsigned int rt)
2204 {
2205     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2206                                       crn, crm, op0, op1, op2);
2207     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2208     bool need_exit_tb = false;
2209     bool nv_trap_to_el2 = false;
2210     bool nv_redirect_reg = false;
2211     bool skip_fp_access_checks = false;
2212     bool nv2_mem_redirect = false;
2213     TCGv_ptr tcg_ri = NULL;
2214     TCGv_i64 tcg_rt;
2215     uint32_t syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2216 
2217     if (crn == 11 || crn == 15) {
2218         /*
2219          * Check for TIDCP trap, which must take precedence over
2220          * the UNDEF for "no such register" etc.
2221          */
2222         switch (s->current_el) {
2223         case 0:
2224             if (dc_isar_feature(aa64_tidcp1, s)) {
2225                 gen_helper_tidcp_el0(tcg_env, tcg_constant_i32(syndrome));
2226             }
2227             break;
2228         case 1:
2229             gen_helper_tidcp_el1(tcg_env, tcg_constant_i32(syndrome));
2230             break;
2231         }
2232     }
2233 
2234     if (!ri) {
2235         /* Unknown register; this might be a guest error or a QEMU
2236          * unimplemented feature.
2237          */
2238         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2239                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2240                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2241         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2242         return;
2243     }
2244 
2245     if (s->nv2 && ri->nv2_redirect_offset) {
2246         /*
2247          * Some registers always redirect to memory; some only do so if
2248          * HCR_EL2.NV1 is 0, and some only if NV1 is 1 (these come in
2249          * pairs which share an offset; see the table in R_CSRPQ).
2250          */
2251         if (ri->nv2_redirect_offset & NV2_REDIR_NV1) {
2252             nv2_mem_redirect = s->nv1;
2253         } else if (ri->nv2_redirect_offset & NV2_REDIR_NO_NV1) {
2254             nv2_mem_redirect = !s->nv1;
2255         } else {
2256             nv2_mem_redirect = true;
2257         }
2258     }
2259 
2260     /* Check access permissions */
2261     if (!cp_access_ok(s->current_el, ri, isread)) {
2262         /*
2263          * FEAT_NV/NV2 handling does not do the usual FP access checks
2264          * for registers only accessible at EL2 (though it *does* do them
2265          * for registers accessible at EL1).
2266          */
2267         skip_fp_access_checks = true;
2268         if (s->nv2 && (ri->type & ARM_CP_NV2_REDIRECT)) {
2269             /*
2270              * This is one of the few EL2 registers which should redirect
2271              * to the equivalent EL1 register. We do that after running
2272              * the EL2 register's accessfn.
2273              */
2274             nv_redirect_reg = true;
2275             assert(!nv2_mem_redirect);
2276         } else if (nv2_mem_redirect) {
2277             /*
2278              * NV2 redirect-to-memory takes precedence over trap to EL2 or
2279              * UNDEF to EL1.
2280              */
2281         } else if (s->nv && arm_cpreg_traps_in_nv(ri)) {
2282             /*
2283              * This register / instruction exists and is an EL2 register, so
2284              * we must trap to EL2 if accessed in nested virtualization EL1
2285              * instead of UNDEFing. We'll do that after the usual access checks.
2286              * (This makes a difference only for a couple of registers like
2287              * VSTTBR_EL2 where the "UNDEF if NonSecure" should take priority
2288              * over the trap-to-EL2. Most trapped-by-FEAT_NV registers have
2289              * an accessfn which does nothing when called from EL1, because
2290              * the trap-to-EL3 controls which would apply to that register
2291              * at EL2 don't take priority over the FEAT_NV trap-to-EL2.)
2292              */
2293             nv_trap_to_el2 = true;
2294         } else {
2295             gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2296             return;
2297         }
2298     }
2299 
2300     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2301         /* Emit code to perform further access permissions checks at
2302          * runtime; this may result in an exception.
2303          */
2304         gen_a64_update_pc(s, 0);
2305         tcg_ri = tcg_temp_new_ptr();
2306         gen_helper_access_check_cp_reg(tcg_ri, tcg_env,
2307                                        tcg_constant_i32(key),
2308                                        tcg_constant_i32(syndrome),
2309                                        tcg_constant_i32(isread));
2310     } else if (ri->type & ARM_CP_RAISES_EXC) {
2311         /*
2312          * The readfn or writefn might raise an exception;
2313          * synchronize the CPU state in case it does.
2314          */
2315         gen_a64_update_pc(s, 0);
2316     }
2317 
2318     if (!skip_fp_access_checks) {
2319         if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2320             return;
2321         } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2322             return;
2323         } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2324             return;
2325         }
2326     }
2327 
2328     if (nv_trap_to_el2) {
2329         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2330         return;
2331     }
2332 
2333     if (nv_redirect_reg) {
2334         /*
2335          * FEAT_NV2 redirection of an EL2 register to an EL1 register.
2336          * Conveniently in all cases the encoding of the EL1 register is
2337          * identical to the EL2 register except that opc1 is 0.
2338          * Get the reginfo for the EL1 register to use for the actual access.
2339          * We don't use the EL1 register's access function, and
2340          * fine-grained-traps on EL1 also do not apply here.
2341          */
2342         key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2343                                  crn, crm, op0, 0, op2);
2344         ri = get_arm_cp_reginfo(s->cp_regs, key);
2345         assert(ri);
2346         assert(cp_access_ok(s->current_el, ri, isread));
2347         /*
2348          * We might not have done an update_pc earlier, so check we don't
2349          * need it. We could support this in future if necessary.
2350          */
2351         assert(!(ri->type & ARM_CP_RAISES_EXC));
2352     }
2353 
2354     if (nv2_mem_redirect) {
2355         /*
2356          * This system register is being redirected into an EL2 memory access.
2357          * This means it is not an IO operation, doesn't change hflags,
2358          * and need not end the TB, because it has no side effects.
2359          *
2360          * The access is 64-bit single copy atomic, guaranteed aligned because
2361          * of the definition of VCNR_EL2. Its endianness depends on
2362          * SCTLR_EL2.EE, not on the data endianness of EL1.
2363          * It is done under either the EL2 translation regime or the EL2&0
2364          * translation regime, depending on HCR_EL2.E2H. It behaves as if
2365          * PSTATE.PAN is 0.
2366          */
2367         TCGv_i64 ptr = tcg_temp_new_i64();
2368         MemOp mop = MO_64 | MO_ALIGN | MO_ATOM_IFALIGN;
2369         ARMMMUIdx armmemidx = s->nv2_mem_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
2370         int memidx = arm_to_core_mmu_idx(armmemidx);
2371         uint32_t syn;
2372 
2373         mop |= (s->nv2_mem_be ? MO_BE : MO_LE);
2374 
2375         tcg_gen_ld_i64(ptr, tcg_env, offsetof(CPUARMState, cp15.vncr_el2));
2376         tcg_gen_addi_i64(ptr, ptr,
2377                          (ri->nv2_redirect_offset & ~NV2_REDIR_FLAG_MASK));
2378         tcg_rt = cpu_reg(s, rt);
2379 
2380         syn = syn_data_abort_vncr(0, !isread, 0);
2381         disas_set_insn_syndrome(s, syn);
2382         if (isread) {
2383             tcg_gen_qemu_ld_i64(tcg_rt, ptr, memidx, mop);
2384         } else {
2385             tcg_gen_qemu_st_i64(tcg_rt, ptr, memidx, mop);
2386         }
2387         return;
2388     }
2389 
2390     /* Handle special cases first */
2391     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2392     case 0:
2393         break;
2394     case ARM_CP_NOP:
2395         return;
2396     case ARM_CP_NZCV:
2397         tcg_rt = cpu_reg(s, rt);
2398         if (isread) {
2399             gen_get_nzcv(tcg_rt);
2400         } else {
2401             gen_set_nzcv(tcg_rt);
2402         }
2403         return;
2404     case ARM_CP_CURRENTEL:
2405     {
2406         /*
2407          * Reads as current EL value from pstate, which is
2408          * guaranteed to be constant by the tb flags.
2409          * For nested virt we should report EL2.
2410          */
2411         int el = s->nv ? 2 : s->current_el;
2412         tcg_rt = cpu_reg(s, rt);
2413         tcg_gen_movi_i64(tcg_rt, el << 2);
2414         return;
2415     }
2416     case ARM_CP_DC_ZVA:
2417         /* Writes clear the aligned block of memory which rt points into. */
2418         if (s->mte_active[0]) {
2419             int desc = 0;
2420 
2421             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2422             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2423             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2424 
2425             tcg_rt = tcg_temp_new_i64();
2426             gen_helper_mte_check_zva(tcg_rt, tcg_env,
2427                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2428         } else {
2429             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2430         }
2431         gen_helper_dc_zva(tcg_env, tcg_rt);
2432         return;
2433     case ARM_CP_DC_GVA:
2434         {
2435             TCGv_i64 clean_addr, tag;
2436 
2437             /*
2438              * DC_GVA, like DC_ZVA, requires that we supply the original
2439              * pointer for an invalid page.  Probe that address first.
2440              */
2441             tcg_rt = cpu_reg(s, rt);
2442             clean_addr = clean_data_tbi(s, tcg_rt);
2443             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2444 
2445             if (s->ata[0]) {
2446                 /* Extract the tag from the register to match STZGM.  */
2447                 tag = tcg_temp_new_i64();
2448                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2449                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2450             }
2451         }
2452         return;
2453     case ARM_CP_DC_GZVA:
2454         {
2455             TCGv_i64 clean_addr, tag;
2456 
2457             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2458             tcg_rt = cpu_reg(s, rt);
2459             clean_addr = clean_data_tbi(s, tcg_rt);
2460             gen_helper_dc_zva(tcg_env, clean_addr);
2461 
2462             if (s->ata[0]) {
2463                 /* Extract the tag from the register to match STZGM.  */
2464                 tag = tcg_temp_new_i64();
2465                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2466                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2467             }
2468         }
2469         return;
2470     default:
2471         g_assert_not_reached();
2472     }
2473 
2474     if (ri->type & ARM_CP_IO) {
2475         /* I/O operations must end the TB here (whether read or write) */
2476         need_exit_tb = translator_io_start(&s->base);
2477     }
2478 
2479     tcg_rt = cpu_reg(s, rt);
2480 
2481     if (isread) {
2482         if (ri->type & ARM_CP_CONST) {
2483             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2484         } else if (ri->readfn) {
2485             if (!tcg_ri) {
2486                 tcg_ri = gen_lookup_cp_reg(key);
2487             }
2488             gen_helper_get_cp_reg64(tcg_rt, tcg_env, tcg_ri);
2489         } else {
2490             tcg_gen_ld_i64(tcg_rt, tcg_env, ri->fieldoffset);
2491         }
2492     } else {
2493         if (ri->type & ARM_CP_CONST) {
2494             /* If not forbidden by access permissions, treat as WI */
2495             return;
2496         } else if (ri->writefn) {
2497             if (!tcg_ri) {
2498                 tcg_ri = gen_lookup_cp_reg(key);
2499             }
2500             gen_helper_set_cp_reg64(tcg_env, tcg_ri, tcg_rt);
2501         } else {
2502             tcg_gen_st_i64(tcg_rt, tcg_env, ri->fieldoffset);
2503         }
2504     }
2505 
2506     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2507         /*
2508          * A write to any coprocessor register that ends a TB
2509          * must rebuild the hflags for the next TB.
2510          */
2511         gen_rebuild_hflags(s);
2512         /*
2513          * We default to ending the TB on a coprocessor register write,
2514          * but allow this to be suppressed by the register definition
2515          * (usually only necessary to work around guest bugs).
2516          */
2517         need_exit_tb = true;
2518     }
2519     if (need_exit_tb) {
2520         s->base.is_jmp = DISAS_UPDATE_EXIT;
2521     }
2522 }
2523 
2524 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2525 {
2526     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2527     return true;
2528 }
2529 
2530 static bool trans_SVC(DisasContext *s, arg_i *a)
2531 {
2532     /*
2533      * For SVC, HVC and SMC we advance the single-step state
2534      * machine before taking the exception. This is architecturally
2535      * mandated, to ensure that single-stepping a system call
2536      * instruction works properly.
2537      */
2538     uint32_t syndrome = syn_aa64_svc(a->imm);
2539     if (s->fgt_svc) {
2540         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2541         return true;
2542     }
2543     gen_ss_advance(s);
2544     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2545     return true;
2546 }
2547 
2548 static bool trans_HVC(DisasContext *s, arg_i *a)
2549 {
2550     int target_el = s->current_el == 3 ? 3 : 2;
2551 
2552     if (s->current_el == 0) {
2553         unallocated_encoding(s);
2554         return true;
2555     }
2556     /*
2557      * The pre HVC helper handles cases when HVC gets trapped
2558      * as an undefined insn by runtime configuration.
2559      */
2560     gen_a64_update_pc(s, 0);
2561     gen_helper_pre_hvc(tcg_env);
2562     /* Architecture requires ss advance before we do the actual work */
2563     gen_ss_advance(s);
2564     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), target_el);
2565     return true;
2566 }
2567 
2568 static bool trans_SMC(DisasContext *s, arg_i *a)
2569 {
2570     if (s->current_el == 0) {
2571         unallocated_encoding(s);
2572         return true;
2573     }
2574     gen_a64_update_pc(s, 0);
2575     gen_helper_pre_smc(tcg_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2576     /* Architecture requires ss advance before we do the actual work */
2577     gen_ss_advance(s);
2578     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2579     return true;
2580 }
2581 
2582 static bool trans_BRK(DisasContext *s, arg_i *a)
2583 {
2584     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2585     return true;
2586 }
2587 
2588 static bool trans_HLT(DisasContext *s, arg_i *a)
2589 {
2590     /*
2591      * HLT. This has two purposes.
2592      * Architecturally, it is an external halting debug instruction.
2593      * Since QEMU doesn't implement external debug, we treat this as
2594      * it is required for halting debug disabled: it will UNDEF.
2595      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2596      */
2597     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2598         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2599     } else {
2600         unallocated_encoding(s);
2601     }
2602     return true;
2603 }
2604 
2605 /*
2606  * Load/Store exclusive instructions are implemented by remembering
2607  * the value/address loaded, and seeing if these are the same
2608  * when the store is performed. This is not actually the architecturally
2609  * mandated semantics, but it works for typical guest code sequences
2610  * and avoids having to monitor regular stores.
2611  *
2612  * The store exclusive uses the atomic cmpxchg primitives to avoid
2613  * races in multi-threaded linux-user and when MTTCG softmmu is
2614  * enabled.
2615  */
2616 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2617                                int size, bool is_pair)
2618 {
2619     int idx = get_mem_index(s);
2620     TCGv_i64 dirty_addr, clean_addr;
2621     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2622 
2623     s->is_ldex = true;
2624     dirty_addr = cpu_reg_sp(s, rn);
2625     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2626 
2627     g_assert(size <= 3);
2628     if (is_pair) {
2629         g_assert(size >= 2);
2630         if (size == 2) {
2631             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2632             if (s->be_data == MO_LE) {
2633                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2634                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2635             } else {
2636                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2637                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2638             }
2639         } else {
2640             TCGv_i128 t16 = tcg_temp_new_i128();
2641 
2642             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2643 
2644             if (s->be_data == MO_LE) {
2645                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2646                                       cpu_exclusive_high, t16);
2647             } else {
2648                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2649                                       cpu_exclusive_val, t16);
2650             }
2651             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2652             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2653         }
2654     } else {
2655         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2656         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2657     }
2658     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2659 }
2660 
2661 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2662                                 int rn, int size, int is_pair)
2663 {
2664     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2665      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2666      *     [addr] = {Rt};
2667      *     if (is_pair) {
2668      *         [addr + datasize] = {Rt2};
2669      *     }
2670      *     {Rd} = 0;
2671      * } else {
2672      *     {Rd} = 1;
2673      * }
2674      * env->exclusive_addr = -1;
2675      */
2676     TCGLabel *fail_label = gen_new_label();
2677     TCGLabel *done_label = gen_new_label();
2678     TCGv_i64 tmp, clean_addr;
2679     MemOp memop;
2680 
2681     /*
2682      * FIXME: We are out of spec here.  We have recorded only the address
2683      * from load_exclusive, not the entire range, and we assume that the
2684      * size of the access on both sides match.  The architecture allows the
2685      * store to be smaller than the load, so long as the stored bytes are
2686      * within the range recorded by the load.
2687      */
2688 
2689     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2690     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2691     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2692 
2693     /*
2694      * The write, and any associated faults, only happen if the virtual
2695      * and physical addresses pass the exclusive monitor check.  These
2696      * faults are exceedingly unlikely, because normally the guest uses
2697      * the exact same address register for the load_exclusive, and we
2698      * would have recognized these faults there.
2699      *
2700      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2701      * unaligned 4-byte write within the range of an aligned 8-byte load.
2702      * With LSE2, the store would need to cross a 16-byte boundary when the
2703      * load did not, which would mean the store is outside the range
2704      * recorded for the monitor, which would have failed a corrected monitor
2705      * check above.  For now, we assume no size change and retain the
2706      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2707      *
2708      * It is possible to trigger an MTE fault, by performing the load with
2709      * a virtual address with a valid tag and performing the store with the
2710      * same virtual address and a different invalid tag.
2711      */
2712     memop = size + is_pair;
2713     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2714         memop |= MO_ALIGN;
2715     }
2716     memop = finalize_memop(s, memop);
2717     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2718 
2719     tmp = tcg_temp_new_i64();
2720     if (is_pair) {
2721         if (size == 2) {
2722             if (s->be_data == MO_LE) {
2723                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2724             } else {
2725                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2726             }
2727             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2728                                        cpu_exclusive_val, tmp,
2729                                        get_mem_index(s), memop);
2730             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2731         } else {
2732             TCGv_i128 t16 = tcg_temp_new_i128();
2733             TCGv_i128 c16 = tcg_temp_new_i128();
2734             TCGv_i64 a, b;
2735 
2736             if (s->be_data == MO_LE) {
2737                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2738                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2739                                         cpu_exclusive_high);
2740             } else {
2741                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2742                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2743                                         cpu_exclusive_val);
2744             }
2745 
2746             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2747                                         get_mem_index(s), memop);
2748 
2749             a = tcg_temp_new_i64();
2750             b = tcg_temp_new_i64();
2751             if (s->be_data == MO_LE) {
2752                 tcg_gen_extr_i128_i64(a, b, t16);
2753             } else {
2754                 tcg_gen_extr_i128_i64(b, a, t16);
2755             }
2756 
2757             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2758             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2759             tcg_gen_or_i64(tmp, a, b);
2760 
2761             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2762         }
2763     } else {
2764         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2765                                    cpu_reg(s, rt), get_mem_index(s), memop);
2766         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2767     }
2768     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2769     tcg_gen_br(done_label);
2770 
2771     gen_set_label(fail_label);
2772     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2773     gen_set_label(done_label);
2774     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2775 }
2776 
2777 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2778                                  int rn, int size)
2779 {
2780     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2781     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2782     int memidx = get_mem_index(s);
2783     TCGv_i64 clean_addr;
2784     MemOp memop;
2785 
2786     if (rn == 31) {
2787         gen_check_sp_alignment(s);
2788     }
2789     memop = check_atomic_align(s, rn, size);
2790     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2791     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2792                                memidx, memop);
2793 }
2794 
2795 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2796                                       int rn, int size)
2797 {
2798     TCGv_i64 s1 = cpu_reg(s, rs);
2799     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2800     TCGv_i64 t1 = cpu_reg(s, rt);
2801     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2802     TCGv_i64 clean_addr;
2803     int memidx = get_mem_index(s);
2804     MemOp memop;
2805 
2806     if (rn == 31) {
2807         gen_check_sp_alignment(s);
2808     }
2809 
2810     /* This is a single atomic access, despite the "pair". */
2811     memop = check_atomic_align(s, rn, size + 1);
2812     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2813 
2814     if (size == 2) {
2815         TCGv_i64 cmp = tcg_temp_new_i64();
2816         TCGv_i64 val = tcg_temp_new_i64();
2817 
2818         if (s->be_data == MO_LE) {
2819             tcg_gen_concat32_i64(val, t1, t2);
2820             tcg_gen_concat32_i64(cmp, s1, s2);
2821         } else {
2822             tcg_gen_concat32_i64(val, t2, t1);
2823             tcg_gen_concat32_i64(cmp, s2, s1);
2824         }
2825 
2826         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2827 
2828         if (s->be_data == MO_LE) {
2829             tcg_gen_extr32_i64(s1, s2, cmp);
2830         } else {
2831             tcg_gen_extr32_i64(s2, s1, cmp);
2832         }
2833     } else {
2834         TCGv_i128 cmp = tcg_temp_new_i128();
2835         TCGv_i128 val = tcg_temp_new_i128();
2836 
2837         if (s->be_data == MO_LE) {
2838             tcg_gen_concat_i64_i128(val, t1, t2);
2839             tcg_gen_concat_i64_i128(cmp, s1, s2);
2840         } else {
2841             tcg_gen_concat_i64_i128(val, t2, t1);
2842             tcg_gen_concat_i64_i128(cmp, s2, s1);
2843         }
2844 
2845         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2846 
2847         if (s->be_data == MO_LE) {
2848             tcg_gen_extr_i128_i64(s1, s2, cmp);
2849         } else {
2850             tcg_gen_extr_i128_i64(s2, s1, cmp);
2851         }
2852     }
2853 }
2854 
2855 /*
2856  * Compute the ISS.SF bit for syndrome information if an exception
2857  * is taken on a load or store. This indicates whether the instruction
2858  * is accessing a 32-bit or 64-bit register. This logic is derived
2859  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2860  */
2861 static bool ldst_iss_sf(int size, bool sign, bool ext)
2862 {
2863 
2864     if (sign) {
2865         /*
2866          * Signed loads are 64 bit results if we are not going to
2867          * do a zero-extend from 32 to 64 after the load.
2868          * (For a store, sign and ext are always false.)
2869          */
2870         return !ext;
2871     } else {
2872         /* Unsigned loads/stores work at the specified size */
2873         return size == MO_64;
2874     }
2875 }
2876 
2877 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2878 {
2879     if (a->rn == 31) {
2880         gen_check_sp_alignment(s);
2881     }
2882     if (a->lasr) {
2883         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2884     }
2885     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2886     return true;
2887 }
2888 
2889 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2890 {
2891     if (a->rn == 31) {
2892         gen_check_sp_alignment(s);
2893     }
2894     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2895     if (a->lasr) {
2896         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2897     }
2898     return true;
2899 }
2900 
2901 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2902 {
2903     TCGv_i64 clean_addr;
2904     MemOp memop;
2905     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2906 
2907     /*
2908      * StoreLORelease is the same as Store-Release for QEMU, but
2909      * needs the feature-test.
2910      */
2911     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2912         return false;
2913     }
2914     /* Generate ISS for non-exclusive accesses including LASR.  */
2915     if (a->rn == 31) {
2916         gen_check_sp_alignment(s);
2917     }
2918     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2919     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2920     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2921                                 true, a->rn != 31, memop);
2922     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2923               iss_sf, a->lasr);
2924     return true;
2925 }
2926 
2927 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2928 {
2929     TCGv_i64 clean_addr;
2930     MemOp memop;
2931     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2932 
2933     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2934     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2935         return false;
2936     }
2937     /* Generate ISS for non-exclusive accesses including LASR.  */
2938     if (a->rn == 31) {
2939         gen_check_sp_alignment(s);
2940     }
2941     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2942     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2943                                 false, a->rn != 31, memop);
2944     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2945               a->rt, iss_sf, a->lasr);
2946     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2947     return true;
2948 }
2949 
2950 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2951 {
2952     if (a->rn == 31) {
2953         gen_check_sp_alignment(s);
2954     }
2955     if (a->lasr) {
2956         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2957     }
2958     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2959     return true;
2960 }
2961 
2962 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2963 {
2964     if (a->rn == 31) {
2965         gen_check_sp_alignment(s);
2966     }
2967     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2968     if (a->lasr) {
2969         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2970     }
2971     return true;
2972 }
2973 
2974 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2975 {
2976     if (!dc_isar_feature(aa64_atomics, s)) {
2977         return false;
2978     }
2979     if (((a->rt | a->rs) & 1) != 0) {
2980         return false;
2981     }
2982 
2983     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2984     return true;
2985 }
2986 
2987 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2988 {
2989     if (!dc_isar_feature(aa64_atomics, s)) {
2990         return false;
2991     }
2992     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2993     return true;
2994 }
2995 
2996 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2997 {
2998     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2999     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
3000     TCGv_i64 clean_addr = tcg_temp_new_i64();
3001     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3002 
3003     gen_pc_plus_diff(s, clean_addr, a->imm);
3004     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3005               false, true, a->rt, iss_sf, false);
3006     return true;
3007 }
3008 
3009 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
3010 {
3011     /* Load register (literal), vector version */
3012     TCGv_i64 clean_addr;
3013     MemOp memop;
3014 
3015     if (!fp_access_check(s)) {
3016         return true;
3017     }
3018     memop = finalize_memop_asimd(s, a->sz);
3019     clean_addr = tcg_temp_new_i64();
3020     gen_pc_plus_diff(s, clean_addr, a->imm);
3021     do_fp_ld(s, a->rt, clean_addr, memop);
3022     return true;
3023 }
3024 
3025 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
3026                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3027                                  uint64_t offset, bool is_store, MemOp mop)
3028 {
3029     if (a->rn == 31) {
3030         gen_check_sp_alignment(s);
3031     }
3032 
3033     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3034     if (!a->p) {
3035         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3036     }
3037 
3038     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
3039                                  (a->w || a->rn != 31), 2 << a->sz, mop);
3040 }
3041 
3042 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
3043                                   TCGv_i64 dirty_addr, uint64_t offset)
3044 {
3045     if (a->w) {
3046         if (a->p) {
3047             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3048         }
3049         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3050     }
3051 }
3052 
3053 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
3054 {
3055     uint64_t offset = a->imm << a->sz;
3056     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3057     MemOp mop = finalize_memop(s, a->sz);
3058 
3059     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3060     tcg_rt = cpu_reg(s, a->rt);
3061     tcg_rt2 = cpu_reg(s, a->rt2);
3062     /*
3063      * We built mop above for the single logical access -- rebuild it
3064      * now for the paired operation.
3065      *
3066      * With LSE2, non-sign-extending pairs are treated atomically if
3067      * aligned, and if unaligned one of the pair will be completely
3068      * within a 16-byte block and that element will be atomic.
3069      * Otherwise each element is separately atomic.
3070      * In all cases, issue one operation with the correct atomicity.
3071      */
3072     mop = a->sz + 1;
3073     if (s->align_mem) {
3074         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3075     }
3076     mop = finalize_memop_pair(s, mop);
3077     if (a->sz == 2) {
3078         TCGv_i64 tmp = tcg_temp_new_i64();
3079 
3080         if (s->be_data == MO_LE) {
3081             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3082         } else {
3083             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3084         }
3085         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3086     } else {
3087         TCGv_i128 tmp = tcg_temp_new_i128();
3088 
3089         if (s->be_data == MO_LE) {
3090             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3091         } else {
3092             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3093         }
3094         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3095     }
3096     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3097     return true;
3098 }
3099 
3100 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
3101 {
3102     uint64_t offset = a->imm << a->sz;
3103     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3104     MemOp mop = finalize_memop(s, a->sz);
3105 
3106     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3107     tcg_rt = cpu_reg(s, a->rt);
3108     tcg_rt2 = cpu_reg(s, a->rt2);
3109 
3110     /*
3111      * We built mop above for the single logical access -- rebuild it
3112      * now for the paired operation.
3113      *
3114      * With LSE2, non-sign-extending pairs are treated atomically if
3115      * aligned, and if unaligned one of the pair will be completely
3116      * within a 16-byte block and that element will be atomic.
3117      * Otherwise each element is separately atomic.
3118      * In all cases, issue one operation with the correct atomicity.
3119      *
3120      * This treats sign-extending loads like zero-extending loads,
3121      * since that reuses the most code below.
3122      */
3123     mop = a->sz + 1;
3124     if (s->align_mem) {
3125         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3126     }
3127     mop = finalize_memop_pair(s, mop);
3128     if (a->sz == 2) {
3129         int o2 = s->be_data == MO_LE ? 32 : 0;
3130         int o1 = o2 ^ 32;
3131 
3132         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3133         if (a->sign) {
3134             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3135             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3136         } else {
3137             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3138             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3139         }
3140     } else {
3141         TCGv_i128 tmp = tcg_temp_new_i128();
3142 
3143         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3144         if (s->be_data == MO_LE) {
3145             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3146         } else {
3147             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3148         }
3149     }
3150     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3151     return true;
3152 }
3153 
3154 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
3155 {
3156     uint64_t offset = a->imm << a->sz;
3157     TCGv_i64 clean_addr, dirty_addr;
3158     MemOp mop;
3159 
3160     if (!fp_access_check(s)) {
3161         return true;
3162     }
3163 
3164     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3165     mop = finalize_memop_asimd(s, a->sz);
3166     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3167     do_fp_st(s, a->rt, clean_addr, mop);
3168     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3169     do_fp_st(s, a->rt2, clean_addr, mop);
3170     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3171     return true;
3172 }
3173 
3174 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
3175 {
3176     uint64_t offset = a->imm << a->sz;
3177     TCGv_i64 clean_addr, dirty_addr;
3178     MemOp mop;
3179 
3180     if (!fp_access_check(s)) {
3181         return true;
3182     }
3183 
3184     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3185     mop = finalize_memop_asimd(s, a->sz);
3186     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3187     do_fp_ld(s, a->rt, clean_addr, mop);
3188     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3189     do_fp_ld(s, a->rt2, clean_addr, mop);
3190     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3191     return true;
3192 }
3193 
3194 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3195 {
3196     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3197     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3198     MemOp mop;
3199     TCGv_i128 tmp;
3200 
3201     /* STGP only comes in one size. */
3202     tcg_debug_assert(a->sz == MO_64);
3203 
3204     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3205         return false;
3206     }
3207 
3208     if (a->rn == 31) {
3209         gen_check_sp_alignment(s);
3210     }
3211 
3212     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3213     if (!a->p) {
3214         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3215     }
3216 
3217     clean_addr = clean_data_tbi(s, dirty_addr);
3218     tcg_rt = cpu_reg(s, a->rt);
3219     tcg_rt2 = cpu_reg(s, a->rt2);
3220 
3221     /*
3222      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3223      * and one tag operation.  We implement it as one single aligned 16-byte
3224      * memory operation for convenience.  Note that the alignment ensures
3225      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3226      */
3227     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3228 
3229     tmp = tcg_temp_new_i128();
3230     if (s->be_data == MO_LE) {
3231         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3232     } else {
3233         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3234     }
3235     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3236 
3237     /* Perform the tag store, if tag access enabled. */
3238     if (s->ata[0]) {
3239         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3240             gen_helper_stg_parallel(tcg_env, dirty_addr, dirty_addr);
3241         } else {
3242             gen_helper_stg(tcg_env, dirty_addr, dirty_addr);
3243         }
3244     }
3245 
3246     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3247     return true;
3248 }
3249 
3250 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3251                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3252                                  uint64_t offset, bool is_store, MemOp mop)
3253 {
3254     int memidx;
3255 
3256     if (a->rn == 31) {
3257         gen_check_sp_alignment(s);
3258     }
3259 
3260     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3261     if (!a->p) {
3262         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3263     }
3264     memidx = get_a64_user_mem_index(s, a->unpriv);
3265     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3266                                         a->w || a->rn != 31,
3267                                         mop, a->unpriv, memidx);
3268 }
3269 
3270 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3271                                   TCGv_i64 dirty_addr, uint64_t offset)
3272 {
3273     if (a->w) {
3274         if (a->p) {
3275             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3276         }
3277         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3278     }
3279 }
3280 
3281 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3282 {
3283     bool iss_sf, iss_valid = !a->w;
3284     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3285     int memidx = get_a64_user_mem_index(s, a->unpriv);
3286     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3287 
3288     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3289 
3290     tcg_rt = cpu_reg(s, a->rt);
3291     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3292 
3293     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3294                      iss_valid, a->rt, iss_sf, false);
3295     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3296     return true;
3297 }
3298 
3299 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3300 {
3301     bool iss_sf, iss_valid = !a->w;
3302     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3303     int memidx = get_a64_user_mem_index(s, a->unpriv);
3304     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3305 
3306     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3307 
3308     tcg_rt = cpu_reg(s, a->rt);
3309     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3310 
3311     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3312                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3313     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3314     return true;
3315 }
3316 
3317 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3318 {
3319     TCGv_i64 clean_addr, dirty_addr;
3320     MemOp mop;
3321 
3322     if (!fp_access_check(s)) {
3323         return true;
3324     }
3325     mop = finalize_memop_asimd(s, a->sz);
3326     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3327     do_fp_st(s, a->rt, clean_addr, mop);
3328     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3329     return true;
3330 }
3331 
3332 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3333 {
3334     TCGv_i64 clean_addr, dirty_addr;
3335     MemOp mop;
3336 
3337     if (!fp_access_check(s)) {
3338         return true;
3339     }
3340     mop = finalize_memop_asimd(s, a->sz);
3341     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3342     do_fp_ld(s, a->rt, clean_addr, mop);
3343     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3344     return true;
3345 }
3346 
3347 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3348                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3349                              bool is_store, MemOp memop)
3350 {
3351     TCGv_i64 tcg_rm;
3352 
3353     if (a->rn == 31) {
3354         gen_check_sp_alignment(s);
3355     }
3356     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3357 
3358     tcg_rm = read_cpu_reg(s, a->rm, 1);
3359     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3360 
3361     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3362     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3363 }
3364 
3365 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3366 {
3367     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3368     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3369     MemOp memop;
3370 
3371     if (extract32(a->opt, 1, 1) == 0) {
3372         return false;
3373     }
3374 
3375     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3376     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3377     tcg_rt = cpu_reg(s, a->rt);
3378     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3379               a->ext, true, a->rt, iss_sf, false);
3380     return true;
3381 }
3382 
3383 static bool trans_STR(DisasContext *s, arg_ldst *a)
3384 {
3385     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3386     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3387     MemOp memop;
3388 
3389     if (extract32(a->opt, 1, 1) == 0) {
3390         return false;
3391     }
3392 
3393     memop = finalize_memop(s, a->sz);
3394     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3395     tcg_rt = cpu_reg(s, a->rt);
3396     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3397     return true;
3398 }
3399 
3400 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3401 {
3402     TCGv_i64 clean_addr, dirty_addr;
3403     MemOp memop;
3404 
3405     if (extract32(a->opt, 1, 1) == 0) {
3406         return false;
3407     }
3408 
3409     if (!fp_access_check(s)) {
3410         return true;
3411     }
3412 
3413     memop = finalize_memop_asimd(s, a->sz);
3414     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3415     do_fp_ld(s, a->rt, clean_addr, memop);
3416     return true;
3417 }
3418 
3419 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3420 {
3421     TCGv_i64 clean_addr, dirty_addr;
3422     MemOp memop;
3423 
3424     if (extract32(a->opt, 1, 1) == 0) {
3425         return false;
3426     }
3427 
3428     if (!fp_access_check(s)) {
3429         return true;
3430     }
3431 
3432     memop = finalize_memop_asimd(s, a->sz);
3433     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3434     do_fp_st(s, a->rt, clean_addr, memop);
3435     return true;
3436 }
3437 
3438 
3439 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3440                          int sign, bool invert)
3441 {
3442     MemOp mop = a->sz | sign;
3443     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3444 
3445     if (a->rn == 31) {
3446         gen_check_sp_alignment(s);
3447     }
3448     mop = check_atomic_align(s, a->rn, mop);
3449     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3450                                 a->rn != 31, mop);
3451     tcg_rs = read_cpu_reg(s, a->rs, true);
3452     tcg_rt = cpu_reg(s, a->rt);
3453     if (invert) {
3454         tcg_gen_not_i64(tcg_rs, tcg_rs);
3455     }
3456     /*
3457      * The tcg atomic primitives are all full barriers.  Therefore we
3458      * can ignore the Acquire and Release bits of this instruction.
3459      */
3460     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3461 
3462     if (mop & MO_SIGN) {
3463         switch (a->sz) {
3464         case MO_8:
3465             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3466             break;
3467         case MO_16:
3468             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3469             break;
3470         case MO_32:
3471             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3472             break;
3473         case MO_64:
3474             break;
3475         default:
3476             g_assert_not_reached();
3477         }
3478     }
3479     return true;
3480 }
3481 
3482 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3483 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3484 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3485 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3486 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3487 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3488 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3489 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3490 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3491 
3492 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3493 {
3494     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3495     TCGv_i64 clean_addr;
3496     MemOp mop;
3497 
3498     if (!dc_isar_feature(aa64_atomics, s) ||
3499         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3500         return false;
3501     }
3502     if (a->rn == 31) {
3503         gen_check_sp_alignment(s);
3504     }
3505     mop = check_atomic_align(s, a->rn, a->sz);
3506     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3507                                 a->rn != 31, mop);
3508     /*
3509      * LDAPR* are a special case because they are a simple load, not a
3510      * fetch-and-do-something op.
3511      * The architectural consistency requirements here are weaker than
3512      * full load-acquire (we only need "load-acquire processor consistent"),
3513      * but we choose to implement them as full LDAQ.
3514      */
3515     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3516               true, a->rt, iss_sf, true);
3517     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3518     return true;
3519 }
3520 
3521 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3522 {
3523     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3524     MemOp memop;
3525 
3526     /* Load with pointer authentication */
3527     if (!dc_isar_feature(aa64_pauth, s)) {
3528         return false;
3529     }
3530 
3531     if (a->rn == 31) {
3532         gen_check_sp_alignment(s);
3533     }
3534     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3535 
3536     if (s->pauth_active) {
3537         if (!a->m) {
3538             gen_helper_autda_combined(dirty_addr, tcg_env, dirty_addr,
3539                                       tcg_constant_i64(0));
3540         } else {
3541             gen_helper_autdb_combined(dirty_addr, tcg_env, dirty_addr,
3542                                       tcg_constant_i64(0));
3543         }
3544     }
3545 
3546     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3547 
3548     memop = finalize_memop(s, MO_64);
3549 
3550     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3551     clean_addr = gen_mte_check1(s, dirty_addr, false,
3552                                 a->w || a->rn != 31, memop);
3553 
3554     tcg_rt = cpu_reg(s, a->rt);
3555     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3556               /* extend */ false, /* iss_valid */ !a->w,
3557               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3558 
3559     if (a->w) {
3560         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3561     }
3562     return true;
3563 }
3564 
3565 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3566 {
3567     TCGv_i64 clean_addr, dirty_addr;
3568     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3569     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3570 
3571     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3572         return false;
3573     }
3574 
3575     if (a->rn == 31) {
3576         gen_check_sp_alignment(s);
3577     }
3578 
3579     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3580     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3581     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3582     clean_addr = clean_data_tbi(s, dirty_addr);
3583 
3584     /*
3585      * Load-AcquirePC semantics; we implement as the slightly more
3586      * restrictive Load-Acquire.
3587      */
3588     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3589               a->rt, iss_sf, true);
3590     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3591     return true;
3592 }
3593 
3594 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3595 {
3596     TCGv_i64 clean_addr, dirty_addr;
3597     MemOp mop = a->sz;
3598     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3599 
3600     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3601         return false;
3602     }
3603 
3604     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3605 
3606     if (a->rn == 31) {
3607         gen_check_sp_alignment(s);
3608     }
3609 
3610     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3611     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3612     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3613     clean_addr = clean_data_tbi(s, dirty_addr);
3614 
3615     /* Store-Release semantics */
3616     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3617     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3618     return true;
3619 }
3620 
3621 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3622 {
3623     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3624     MemOp endian, align, mop;
3625 
3626     int total;    /* total bytes */
3627     int elements; /* elements per vector */
3628     int r;
3629     int size = a->sz;
3630 
3631     if (!a->p && a->rm != 0) {
3632         /* For non-postindexed accesses the Rm field must be 0 */
3633         return false;
3634     }
3635     if (size == 3 && !a->q && a->selem != 1) {
3636         return false;
3637     }
3638     if (!fp_access_check(s)) {
3639         return true;
3640     }
3641 
3642     if (a->rn == 31) {
3643         gen_check_sp_alignment(s);
3644     }
3645 
3646     /* For our purposes, bytes are always little-endian.  */
3647     endian = s->be_data;
3648     if (size == 0) {
3649         endian = MO_LE;
3650     }
3651 
3652     total = a->rpt * a->selem * (a->q ? 16 : 8);
3653     tcg_rn = cpu_reg_sp(s, a->rn);
3654 
3655     /*
3656      * Issue the MTE check vs the logical repeat count, before we
3657      * promote consecutive little-endian elements below.
3658      */
3659     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3660                                 finalize_memop_asimd(s, size));
3661 
3662     /*
3663      * Consecutive little-endian elements from a single register
3664      * can be promoted to a larger little-endian operation.
3665      */
3666     align = MO_ALIGN;
3667     if (a->selem == 1 && endian == MO_LE) {
3668         align = pow2_align(size);
3669         size = 3;
3670     }
3671     if (!s->align_mem) {
3672         align = 0;
3673     }
3674     mop = endian | size | align;
3675 
3676     elements = (a->q ? 16 : 8) >> size;
3677     tcg_ebytes = tcg_constant_i64(1 << size);
3678     for (r = 0; r < a->rpt; r++) {
3679         int e;
3680         for (e = 0; e < elements; e++) {
3681             int xs;
3682             for (xs = 0; xs < a->selem; xs++) {
3683                 int tt = (a->rt + r + xs) % 32;
3684                 do_vec_ld(s, tt, e, clean_addr, mop);
3685                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3686             }
3687         }
3688     }
3689 
3690     /*
3691      * For non-quad operations, setting a slice of the low 64 bits of
3692      * the register clears the high 64 bits (in the ARM ARM pseudocode
3693      * this is implicit in the fact that 'rval' is a 64 bit wide
3694      * variable).  For quad operations, we might still need to zero
3695      * the high bits of SVE.
3696      */
3697     for (r = 0; r < a->rpt * a->selem; r++) {
3698         int tt = (a->rt + r) % 32;
3699         clear_vec_high(s, a->q, tt);
3700     }
3701 
3702     if (a->p) {
3703         if (a->rm == 31) {
3704             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3705         } else {
3706             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3707         }
3708     }
3709     return true;
3710 }
3711 
3712 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3713 {
3714     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3715     MemOp endian, align, mop;
3716 
3717     int total;    /* total bytes */
3718     int elements; /* elements per vector */
3719     int r;
3720     int size = a->sz;
3721 
3722     if (!a->p && a->rm != 0) {
3723         /* For non-postindexed accesses the Rm field must be 0 */
3724         return false;
3725     }
3726     if (size == 3 && !a->q && a->selem != 1) {
3727         return false;
3728     }
3729     if (!fp_access_check(s)) {
3730         return true;
3731     }
3732 
3733     if (a->rn == 31) {
3734         gen_check_sp_alignment(s);
3735     }
3736 
3737     /* For our purposes, bytes are always little-endian.  */
3738     endian = s->be_data;
3739     if (size == 0) {
3740         endian = MO_LE;
3741     }
3742 
3743     total = a->rpt * a->selem * (a->q ? 16 : 8);
3744     tcg_rn = cpu_reg_sp(s, a->rn);
3745 
3746     /*
3747      * Issue the MTE check vs the logical repeat count, before we
3748      * promote consecutive little-endian elements below.
3749      */
3750     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3751                                 finalize_memop_asimd(s, size));
3752 
3753     /*
3754      * Consecutive little-endian elements from a single register
3755      * can be promoted to a larger little-endian operation.
3756      */
3757     align = MO_ALIGN;
3758     if (a->selem == 1 && endian == MO_LE) {
3759         align = pow2_align(size);
3760         size = 3;
3761     }
3762     if (!s->align_mem) {
3763         align = 0;
3764     }
3765     mop = endian | size | align;
3766 
3767     elements = (a->q ? 16 : 8) >> size;
3768     tcg_ebytes = tcg_constant_i64(1 << size);
3769     for (r = 0; r < a->rpt; r++) {
3770         int e;
3771         for (e = 0; e < elements; e++) {
3772             int xs;
3773             for (xs = 0; xs < a->selem; xs++) {
3774                 int tt = (a->rt + r + xs) % 32;
3775                 do_vec_st(s, tt, e, clean_addr, mop);
3776                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3777             }
3778         }
3779     }
3780 
3781     if (a->p) {
3782         if (a->rm == 31) {
3783             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3784         } else {
3785             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3786         }
3787     }
3788     return true;
3789 }
3790 
3791 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3792 {
3793     int xs, total, rt;
3794     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3795     MemOp mop;
3796 
3797     if (!a->p && a->rm != 0) {
3798         return false;
3799     }
3800     if (!fp_access_check(s)) {
3801         return true;
3802     }
3803 
3804     if (a->rn == 31) {
3805         gen_check_sp_alignment(s);
3806     }
3807 
3808     total = a->selem << a->scale;
3809     tcg_rn = cpu_reg_sp(s, a->rn);
3810 
3811     mop = finalize_memop_asimd(s, a->scale);
3812     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3813                                 total, mop);
3814 
3815     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3816     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3817         do_vec_st(s, rt, a->index, clean_addr, mop);
3818         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3819     }
3820 
3821     if (a->p) {
3822         if (a->rm == 31) {
3823             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3824         } else {
3825             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3826         }
3827     }
3828     return true;
3829 }
3830 
3831 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3832 {
3833     int xs, total, rt;
3834     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3835     MemOp mop;
3836 
3837     if (!a->p && a->rm != 0) {
3838         return false;
3839     }
3840     if (!fp_access_check(s)) {
3841         return true;
3842     }
3843 
3844     if (a->rn == 31) {
3845         gen_check_sp_alignment(s);
3846     }
3847 
3848     total = a->selem << a->scale;
3849     tcg_rn = cpu_reg_sp(s, a->rn);
3850 
3851     mop = finalize_memop_asimd(s, a->scale);
3852     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3853                                 total, mop);
3854 
3855     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3856     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3857         do_vec_ld(s, rt, a->index, clean_addr, mop);
3858         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3859     }
3860 
3861     if (a->p) {
3862         if (a->rm == 31) {
3863             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3864         } else {
3865             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3866         }
3867     }
3868     return true;
3869 }
3870 
3871 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3872 {
3873     int xs, total, rt;
3874     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3875     MemOp mop;
3876 
3877     if (!a->p && a->rm != 0) {
3878         return false;
3879     }
3880     if (!fp_access_check(s)) {
3881         return true;
3882     }
3883 
3884     if (a->rn == 31) {
3885         gen_check_sp_alignment(s);
3886     }
3887 
3888     total = a->selem << a->scale;
3889     tcg_rn = cpu_reg_sp(s, a->rn);
3890 
3891     mop = finalize_memop_asimd(s, a->scale);
3892     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3893                                 total, mop);
3894 
3895     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3896     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3897         /* Load and replicate to all elements */
3898         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3899 
3900         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3901         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3902                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3903         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3904     }
3905 
3906     if (a->p) {
3907         if (a->rm == 31) {
3908             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3909         } else {
3910             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3911         }
3912     }
3913     return true;
3914 }
3915 
3916 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3917 {
3918     TCGv_i64 addr, clean_addr, tcg_rt;
3919     int size = 4 << s->dcz_blocksize;
3920 
3921     if (!dc_isar_feature(aa64_mte, s)) {
3922         return false;
3923     }
3924     if (s->current_el == 0) {
3925         return false;
3926     }
3927 
3928     if (a->rn == 31) {
3929         gen_check_sp_alignment(s);
3930     }
3931 
3932     addr = read_cpu_reg_sp(s, a->rn, true);
3933     tcg_gen_addi_i64(addr, addr, a->imm);
3934     tcg_rt = cpu_reg(s, a->rt);
3935 
3936     if (s->ata[0]) {
3937         gen_helper_stzgm_tags(tcg_env, addr, tcg_rt);
3938     }
3939     /*
3940      * The non-tags portion of STZGM is mostly like DC_ZVA,
3941      * except the alignment happens before the access.
3942      */
3943     clean_addr = clean_data_tbi(s, addr);
3944     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3945     gen_helper_dc_zva(tcg_env, clean_addr);
3946     return true;
3947 }
3948 
3949 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3950 {
3951     TCGv_i64 addr, clean_addr, tcg_rt;
3952 
3953     if (!dc_isar_feature(aa64_mte, s)) {
3954         return false;
3955     }
3956     if (s->current_el == 0) {
3957         return false;
3958     }
3959 
3960     if (a->rn == 31) {
3961         gen_check_sp_alignment(s);
3962     }
3963 
3964     addr = read_cpu_reg_sp(s, a->rn, true);
3965     tcg_gen_addi_i64(addr, addr, a->imm);
3966     tcg_rt = cpu_reg(s, a->rt);
3967 
3968     if (s->ata[0]) {
3969         gen_helper_stgm(tcg_env, addr, tcg_rt);
3970     } else {
3971         MMUAccessType acc = MMU_DATA_STORE;
3972         int size = 4 << s->gm_blocksize;
3973 
3974         clean_addr = clean_data_tbi(s, addr);
3975         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3976         gen_probe_access(s, clean_addr, acc, size);
3977     }
3978     return true;
3979 }
3980 
3981 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3982 {
3983     TCGv_i64 addr, clean_addr, tcg_rt;
3984 
3985     if (!dc_isar_feature(aa64_mte, s)) {
3986         return false;
3987     }
3988     if (s->current_el == 0) {
3989         return false;
3990     }
3991 
3992     if (a->rn == 31) {
3993         gen_check_sp_alignment(s);
3994     }
3995 
3996     addr = read_cpu_reg_sp(s, a->rn, true);
3997     tcg_gen_addi_i64(addr, addr, a->imm);
3998     tcg_rt = cpu_reg(s, a->rt);
3999 
4000     if (s->ata[0]) {
4001         gen_helper_ldgm(tcg_rt, tcg_env, addr);
4002     } else {
4003         MMUAccessType acc = MMU_DATA_LOAD;
4004         int size = 4 << s->gm_blocksize;
4005 
4006         clean_addr = clean_data_tbi(s, addr);
4007         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4008         gen_probe_access(s, clean_addr, acc, size);
4009         /* The result tags are zeros.  */
4010         tcg_gen_movi_i64(tcg_rt, 0);
4011     }
4012     return true;
4013 }
4014 
4015 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
4016 {
4017     TCGv_i64 addr, clean_addr, tcg_rt;
4018 
4019     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
4020         return false;
4021     }
4022 
4023     if (a->rn == 31) {
4024         gen_check_sp_alignment(s);
4025     }
4026 
4027     addr = read_cpu_reg_sp(s, a->rn, true);
4028     if (!a->p) {
4029         /* pre-index or signed offset */
4030         tcg_gen_addi_i64(addr, addr, a->imm);
4031     }
4032 
4033     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4034     tcg_rt = cpu_reg(s, a->rt);
4035     if (s->ata[0]) {
4036         gen_helper_ldg(tcg_rt, tcg_env, addr, tcg_rt);
4037     } else {
4038         /*
4039          * Tag access disabled: we must check for aborts on the load
4040          * load from [rn+offset], and then insert a 0 tag into rt.
4041          */
4042         clean_addr = clean_data_tbi(s, addr);
4043         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4044         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4045     }
4046 
4047     if (a->w) {
4048         /* pre-index or post-index */
4049         if (a->p) {
4050             /* post-index */
4051             tcg_gen_addi_i64(addr, addr, a->imm);
4052         }
4053         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
4054     }
4055     return true;
4056 }
4057 
4058 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
4059 {
4060     TCGv_i64 addr, tcg_rt;
4061 
4062     if (a->rn == 31) {
4063         gen_check_sp_alignment(s);
4064     }
4065 
4066     addr = read_cpu_reg_sp(s, a->rn, true);
4067     if (!a->p) {
4068         /* pre-index or signed offset */
4069         tcg_gen_addi_i64(addr, addr, a->imm);
4070     }
4071     tcg_rt = cpu_reg_sp(s, a->rt);
4072     if (!s->ata[0]) {
4073         /*
4074          * For STG and ST2G, we need to check alignment and probe memory.
4075          * TODO: For STZG and STZ2G, we could rely on the stores below,
4076          * at least for system mode; user-only won't enforce alignment.
4077          */
4078         if (is_pair) {
4079             gen_helper_st2g_stub(tcg_env, addr);
4080         } else {
4081             gen_helper_stg_stub(tcg_env, addr);
4082         }
4083     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4084         if (is_pair) {
4085             gen_helper_st2g_parallel(tcg_env, addr, tcg_rt);
4086         } else {
4087             gen_helper_stg_parallel(tcg_env, addr, tcg_rt);
4088         }
4089     } else {
4090         if (is_pair) {
4091             gen_helper_st2g(tcg_env, addr, tcg_rt);
4092         } else {
4093             gen_helper_stg(tcg_env, addr, tcg_rt);
4094         }
4095     }
4096 
4097     if (is_zero) {
4098         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4099         TCGv_i64 zero64 = tcg_constant_i64(0);
4100         TCGv_i128 zero128 = tcg_temp_new_i128();
4101         int mem_index = get_mem_index(s);
4102         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4103 
4104         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4105 
4106         /* This is 1 or 2 atomic 16-byte operations. */
4107         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4108         if (is_pair) {
4109             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4110             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4111         }
4112     }
4113 
4114     if (a->w) {
4115         /* pre-index or post-index */
4116         if (a->p) {
4117             /* post-index */
4118             tcg_gen_addi_i64(addr, addr, a->imm);
4119         }
4120         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
4121     }
4122     return true;
4123 }
4124 
4125 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
4126 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
4127 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
4128 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
4129 
4130 typedef void SetFn(TCGv_env, TCGv_i32, TCGv_i32);
4131 
4132 static bool do_SET(DisasContext *s, arg_set *a, bool is_epilogue,
4133                    bool is_setg, SetFn fn)
4134 {
4135     int memidx;
4136     uint32_t syndrome, desc = 0;
4137 
4138     if (is_setg && !dc_isar_feature(aa64_mte, s)) {
4139         return false;
4140     }
4141 
4142     /*
4143      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4144      * us to pull this check before the CheckMOPSEnabled() test
4145      * (which we do in the helper function)
4146      */
4147     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4148         a->rd == 31 || a->rn == 31) {
4149         return false;
4150     }
4151 
4152     memidx = get_a64_user_mem_index(s, a->unpriv);
4153 
4154     /*
4155      * We pass option_a == true, matching our implementation;
4156      * we pass wrong_option == false: helper function may set that bit.
4157      */
4158     syndrome = syn_mop(true, is_setg, (a->nontemp << 1) | a->unpriv,
4159                        is_epilogue, false, true, a->rd, a->rs, a->rn);
4160 
4161     if (is_setg ? s->ata[a->unpriv] : s->mte_active[a->unpriv]) {
4162         /* We may need to do MTE tag checking, so assemble the descriptor */
4163         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
4164         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
4165         desc = FIELD_DP32(desc, MTEDESC, WRITE, true);
4166         /* SIZEM1 and ALIGN we leave 0 (byte write) */
4167     }
4168     /* The helper function always needs the memidx even with MTE disabled */
4169     desc = FIELD_DP32(desc, MTEDESC, MIDX, memidx);
4170 
4171     /*
4172      * The helper needs the register numbers, but since they're in
4173      * the syndrome anyway, we let it extract them from there rather
4174      * than passing in an extra three integer arguments.
4175      */
4176     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(desc));
4177     return true;
4178 }
4179 
4180 TRANS_FEAT(SETP, aa64_mops, do_SET, a, false, false, gen_helper_setp)
4181 TRANS_FEAT(SETM, aa64_mops, do_SET, a, false, false, gen_helper_setm)
4182 TRANS_FEAT(SETE, aa64_mops, do_SET, a, true, false, gen_helper_sete)
4183 TRANS_FEAT(SETGP, aa64_mops, do_SET, a, false, true, gen_helper_setgp)
4184 TRANS_FEAT(SETGM, aa64_mops, do_SET, a, false, true, gen_helper_setgm)
4185 TRANS_FEAT(SETGE, aa64_mops, do_SET, a, true, true, gen_helper_setge)
4186 
4187 typedef void CpyFn(TCGv_env, TCGv_i32, TCGv_i32, TCGv_i32);
4188 
4189 static bool do_CPY(DisasContext *s, arg_cpy *a, bool is_epilogue, CpyFn fn)
4190 {
4191     int rmemidx, wmemidx;
4192     uint32_t syndrome, rdesc = 0, wdesc = 0;
4193     bool wunpriv = extract32(a->options, 0, 1);
4194     bool runpriv = extract32(a->options, 1, 1);
4195 
4196     /*
4197      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4198      * us to pull this check before the CheckMOPSEnabled() test
4199      * (which we do in the helper function)
4200      */
4201     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4202         a->rd == 31 || a->rs == 31 || a->rn == 31) {
4203         return false;
4204     }
4205 
4206     rmemidx = get_a64_user_mem_index(s, runpriv);
4207     wmemidx = get_a64_user_mem_index(s, wunpriv);
4208 
4209     /*
4210      * We pass option_a == true, matching our implementation;
4211      * we pass wrong_option == false: helper function may set that bit.
4212      */
4213     syndrome = syn_mop(false, false, a->options, is_epilogue,
4214                        false, true, a->rd, a->rs, a->rn);
4215 
4216     /* If we need to do MTE tag checking, assemble the descriptors */
4217     if (s->mte_active[runpriv]) {
4218         rdesc = FIELD_DP32(rdesc, MTEDESC, TBI, s->tbid);
4219         rdesc = FIELD_DP32(rdesc, MTEDESC, TCMA, s->tcma);
4220     }
4221     if (s->mte_active[wunpriv]) {
4222         wdesc = FIELD_DP32(wdesc, MTEDESC, TBI, s->tbid);
4223         wdesc = FIELD_DP32(wdesc, MTEDESC, TCMA, s->tcma);
4224         wdesc = FIELD_DP32(wdesc, MTEDESC, WRITE, true);
4225     }
4226     /* The helper function needs these parts of the descriptor regardless */
4227     rdesc = FIELD_DP32(rdesc, MTEDESC, MIDX, rmemidx);
4228     wdesc = FIELD_DP32(wdesc, MTEDESC, MIDX, wmemidx);
4229 
4230     /*
4231      * The helper needs the register numbers, but since they're in
4232      * the syndrome anyway, we let it extract them from there rather
4233      * than passing in an extra three integer arguments.
4234      */
4235     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(wdesc),
4236        tcg_constant_i32(rdesc));
4237     return true;
4238 }
4239 
4240 TRANS_FEAT(CPYP, aa64_mops, do_CPY, a, false, gen_helper_cpyp)
4241 TRANS_FEAT(CPYM, aa64_mops, do_CPY, a, false, gen_helper_cpym)
4242 TRANS_FEAT(CPYE, aa64_mops, do_CPY, a, true, gen_helper_cpye)
4243 TRANS_FEAT(CPYFP, aa64_mops, do_CPY, a, false, gen_helper_cpyfp)
4244 TRANS_FEAT(CPYFM, aa64_mops, do_CPY, a, false, gen_helper_cpyfm)
4245 TRANS_FEAT(CPYFE, aa64_mops, do_CPY, a, true, gen_helper_cpyfe)
4246 
4247 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4248 
4249 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4250                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4251 {
4252     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4253     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4254     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4255 
4256     fn(tcg_rd, tcg_rn, tcg_imm);
4257     if (!a->sf) {
4258         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4259     }
4260     return true;
4261 }
4262 
4263 /*
4264  * PC-rel. addressing
4265  */
4266 
4267 static bool trans_ADR(DisasContext *s, arg_ri *a)
4268 {
4269     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4270     return true;
4271 }
4272 
4273 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4274 {
4275     int64_t offset = (int64_t)a->imm << 12;
4276 
4277     /* The page offset is ok for CF_PCREL. */
4278     offset -= s->pc_curr & 0xfff;
4279     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4280     return true;
4281 }
4282 
4283 /*
4284  * Add/subtract (immediate)
4285  */
4286 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4287 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4288 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4289 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4290 
4291 /*
4292  * Add/subtract (immediate, with tags)
4293  */
4294 
4295 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4296                                       bool sub_op)
4297 {
4298     TCGv_i64 tcg_rn, tcg_rd;
4299     int imm;
4300 
4301     imm = a->uimm6 << LOG2_TAG_GRANULE;
4302     if (sub_op) {
4303         imm = -imm;
4304     }
4305 
4306     tcg_rn = cpu_reg_sp(s, a->rn);
4307     tcg_rd = cpu_reg_sp(s, a->rd);
4308 
4309     if (s->ata[0]) {
4310         gen_helper_addsubg(tcg_rd, tcg_env, tcg_rn,
4311                            tcg_constant_i32(imm),
4312                            tcg_constant_i32(a->uimm4));
4313     } else {
4314         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4315         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4316     }
4317     return true;
4318 }
4319 
4320 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4321 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4322 
4323 /* The input should be a value in the bottom e bits (with higher
4324  * bits zero); returns that value replicated into every element
4325  * of size e in a 64 bit integer.
4326  */
4327 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4328 {
4329     assert(e != 0);
4330     while (e < 64) {
4331         mask |= mask << e;
4332         e *= 2;
4333     }
4334     return mask;
4335 }
4336 
4337 /*
4338  * Logical (immediate)
4339  */
4340 
4341 /*
4342  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4343  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4344  * value (ie should cause a guest UNDEF exception), and true if they are
4345  * valid, in which case the decoded bit pattern is written to result.
4346  */
4347 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4348                             unsigned int imms, unsigned int immr)
4349 {
4350     uint64_t mask;
4351     unsigned e, levels, s, r;
4352     int len;
4353 
4354     assert(immn < 2 && imms < 64 && immr < 64);
4355 
4356     /* The bit patterns we create here are 64 bit patterns which
4357      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4358      * 64 bits each. Each element contains the same value: a run
4359      * of between 1 and e-1 non-zero bits, rotated within the
4360      * element by between 0 and e-1 bits.
4361      *
4362      * The element size and run length are encoded into immn (1 bit)
4363      * and imms (6 bits) as follows:
4364      * 64 bit elements: immn = 1, imms = <length of run - 1>
4365      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4366      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4367      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4368      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4369      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4370      * Notice that immn = 0, imms = 11111x is the only combination
4371      * not covered by one of the above options; this is reserved.
4372      * Further, <length of run - 1> all-ones is a reserved pattern.
4373      *
4374      * In all cases the rotation is by immr % e (and immr is 6 bits).
4375      */
4376 
4377     /* First determine the element size */
4378     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4379     if (len < 1) {
4380         /* This is the immn == 0, imms == 0x11111x case */
4381         return false;
4382     }
4383     e = 1 << len;
4384 
4385     levels = e - 1;
4386     s = imms & levels;
4387     r = immr & levels;
4388 
4389     if (s == levels) {
4390         /* <length of run - 1> mustn't be all-ones. */
4391         return false;
4392     }
4393 
4394     /* Create the value of one element: s+1 set bits rotated
4395      * by r within the element (which is e bits wide)...
4396      */
4397     mask = MAKE_64BIT_MASK(0, s + 1);
4398     if (r) {
4399         mask = (mask >> r) | (mask << (e - r));
4400         mask &= MAKE_64BIT_MASK(0, e);
4401     }
4402     /* ...then replicate the element over the whole 64 bit value */
4403     mask = bitfield_replicate(mask, e);
4404     *result = mask;
4405     return true;
4406 }
4407 
4408 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4409                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4410 {
4411     TCGv_i64 tcg_rd, tcg_rn;
4412     uint64_t imm;
4413 
4414     /* Some immediate field values are reserved. */
4415     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4416                                 extract32(a->dbm, 0, 6),
4417                                 extract32(a->dbm, 6, 6))) {
4418         return false;
4419     }
4420     if (!a->sf) {
4421         imm &= 0xffffffffull;
4422     }
4423 
4424     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4425     tcg_rn = cpu_reg(s, a->rn);
4426 
4427     fn(tcg_rd, tcg_rn, imm);
4428     if (set_cc) {
4429         gen_logic_CC(a->sf, tcg_rd);
4430     }
4431     if (!a->sf) {
4432         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4433     }
4434     return true;
4435 }
4436 
4437 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4438 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4439 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4440 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4441 
4442 /*
4443  * Move wide (immediate)
4444  */
4445 
4446 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4447 {
4448     int pos = a->hw << 4;
4449     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4450     return true;
4451 }
4452 
4453 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4454 {
4455     int pos = a->hw << 4;
4456     uint64_t imm = a->imm;
4457 
4458     imm = ~(imm << pos);
4459     if (!a->sf) {
4460         imm = (uint32_t)imm;
4461     }
4462     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4463     return true;
4464 }
4465 
4466 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4467 {
4468     int pos = a->hw << 4;
4469     TCGv_i64 tcg_rd, tcg_im;
4470 
4471     tcg_rd = cpu_reg(s, a->rd);
4472     tcg_im = tcg_constant_i64(a->imm);
4473     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4474     if (!a->sf) {
4475         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4476     }
4477     return true;
4478 }
4479 
4480 /*
4481  * Bitfield
4482  */
4483 
4484 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4485 {
4486     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4487     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4488     unsigned int bitsize = a->sf ? 64 : 32;
4489     unsigned int ri = a->immr;
4490     unsigned int si = a->imms;
4491     unsigned int pos, len;
4492 
4493     if (si >= ri) {
4494         /* Wd<s-r:0> = Wn<s:r> */
4495         len = (si - ri) + 1;
4496         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4497         if (!a->sf) {
4498             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4499         }
4500     } else {
4501         /* Wd<32+s-r,32-r> = Wn<s:0> */
4502         len = si + 1;
4503         pos = (bitsize - ri) & (bitsize - 1);
4504 
4505         if (len < ri) {
4506             /*
4507              * Sign extend the destination field from len to fill the
4508              * balance of the word.  Let the deposit below insert all
4509              * of those sign bits.
4510              */
4511             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4512             len = ri;
4513         }
4514 
4515         /*
4516          * We start with zero, and we haven't modified any bits outside
4517          * bitsize, therefore no final zero-extension is unneeded for !sf.
4518          */
4519         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4520     }
4521     return true;
4522 }
4523 
4524 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4525 {
4526     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4527     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4528     unsigned int bitsize = a->sf ? 64 : 32;
4529     unsigned int ri = a->immr;
4530     unsigned int si = a->imms;
4531     unsigned int pos, len;
4532 
4533     tcg_rd = cpu_reg(s, a->rd);
4534     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4535 
4536     if (si >= ri) {
4537         /* Wd<s-r:0> = Wn<s:r> */
4538         len = (si - ri) + 1;
4539         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4540     } else {
4541         /* Wd<32+s-r,32-r> = Wn<s:0> */
4542         len = si + 1;
4543         pos = (bitsize - ri) & (bitsize - 1);
4544         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4545     }
4546     return true;
4547 }
4548 
4549 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4550 {
4551     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4552     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4553     unsigned int bitsize = a->sf ? 64 : 32;
4554     unsigned int ri = a->immr;
4555     unsigned int si = a->imms;
4556     unsigned int pos, len;
4557 
4558     tcg_rd = cpu_reg(s, a->rd);
4559     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4560 
4561     if (si >= ri) {
4562         /* Wd<s-r:0> = Wn<s:r> */
4563         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4564         len = (si - ri) + 1;
4565         pos = 0;
4566     } else {
4567         /* Wd<32+s-r,32-r> = Wn<s:0> */
4568         len = si + 1;
4569         pos = (bitsize - ri) & (bitsize - 1);
4570     }
4571 
4572     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4573     if (!a->sf) {
4574         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4575     }
4576     return true;
4577 }
4578 
4579 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4580 {
4581     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4582 
4583     tcg_rd = cpu_reg(s, a->rd);
4584 
4585     if (unlikely(a->imm == 0)) {
4586         /*
4587          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4588          * so an extract from bit 0 is a special case.
4589          */
4590         if (a->sf) {
4591             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4592         } else {
4593             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4594         }
4595     } else {
4596         tcg_rm = cpu_reg(s, a->rm);
4597         tcg_rn = cpu_reg(s, a->rn);
4598 
4599         if (a->sf) {
4600             /* Specialization to ROR happens in EXTRACT2.  */
4601             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4602         } else {
4603             TCGv_i32 t0 = tcg_temp_new_i32();
4604 
4605             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4606             if (a->rm == a->rn) {
4607                 tcg_gen_rotri_i32(t0, t0, a->imm);
4608             } else {
4609                 TCGv_i32 t1 = tcg_temp_new_i32();
4610                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4611                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4612             }
4613             tcg_gen_extu_i32_i64(tcg_rd, t0);
4614         }
4615     }
4616     return true;
4617 }
4618 
4619 /*
4620  * Cryptographic AES, SHA, SHA512
4621  */
4622 
4623 TRANS_FEAT(AESE, aa64_aes, do_gvec_op3_ool, a, 0, gen_helper_crypto_aese)
4624 TRANS_FEAT(AESD, aa64_aes, do_gvec_op3_ool, a, 0, gen_helper_crypto_aesd)
4625 TRANS_FEAT(AESMC, aa64_aes, do_gvec_op2_ool, a, 0, gen_helper_crypto_aesmc)
4626 TRANS_FEAT(AESIMC, aa64_aes, do_gvec_op2_ool, a, 0, gen_helper_crypto_aesimc)
4627 
4628 TRANS_FEAT(SHA1C, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1c)
4629 TRANS_FEAT(SHA1P, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1p)
4630 TRANS_FEAT(SHA1M, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1m)
4631 TRANS_FEAT(SHA1SU0, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1su0)
4632 
4633 TRANS_FEAT(SHA256H, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256h)
4634 TRANS_FEAT(SHA256H2, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256h2)
4635 TRANS_FEAT(SHA256SU1, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256su1)
4636 
4637 TRANS_FEAT(SHA1H, aa64_sha1, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha1h)
4638 TRANS_FEAT(SHA1SU1, aa64_sha1, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha1su1)
4639 TRANS_FEAT(SHA256SU0, aa64_sha256, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha256su0)
4640 
4641 TRANS_FEAT(SHA512H, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512h)
4642 TRANS_FEAT(SHA512H2, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512h2)
4643 TRANS_FEAT(SHA512SU1, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512su1)
4644 TRANS_FEAT(RAX1, aa64_sha3, do_gvec_fn3, a, gen_gvec_rax1)
4645 TRANS_FEAT(SM3PARTW1, aa64_sm3, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm3partw1)
4646 TRANS_FEAT(SM3PARTW2, aa64_sm3, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm3partw2)
4647 TRANS_FEAT(SM4EKEY, aa64_sm4, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm4ekey)
4648 
4649 TRANS_FEAT(SHA512SU0, aa64_sha512, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha512su0)
4650 TRANS_FEAT(SM4E, aa64_sm4, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm4e)
4651 
4652 TRANS_FEAT(EOR3, aa64_sha3, do_gvec_fn4, a, gen_gvec_eor3)
4653 TRANS_FEAT(BCAX, aa64_sha3, do_gvec_fn4, a, gen_gvec_bcax)
4654 
4655 static bool trans_SM3SS1(DisasContext *s, arg_SM3SS1 *a)
4656 {
4657     if (!dc_isar_feature(aa64_sm3, s)) {
4658         return false;
4659     }
4660     if (fp_access_check(s)) {
4661         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
4662         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
4663         TCGv_i32 tcg_op3 = tcg_temp_new_i32();
4664         TCGv_i32 tcg_res = tcg_temp_new_i32();
4665         unsigned vsz, dofs;
4666 
4667         read_vec_element_i32(s, tcg_op1, a->rn, 3, MO_32);
4668         read_vec_element_i32(s, tcg_op2, a->rm, 3, MO_32);
4669         read_vec_element_i32(s, tcg_op3, a->ra, 3, MO_32);
4670 
4671         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
4672         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
4673         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
4674         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
4675 
4676         /* Clear the whole register first, then store bits [127:96]. */
4677         vsz = vec_full_reg_size(s);
4678         dofs = vec_full_reg_offset(s, a->rd);
4679         tcg_gen_gvec_dup_imm(MO_64, dofs, vsz, vsz, 0);
4680         write_vec_element_i32(s, tcg_res, a->rd, 3, MO_32);
4681     }
4682     return true;
4683 }
4684 
4685 static bool do_crypto3i(DisasContext *s, arg_crypto3i *a, gen_helper_gvec_3 *fn)
4686 {
4687     if (fp_access_check(s)) {
4688         gen_gvec_op3_ool(s, true, a->rd, a->rn, a->rm, a->imm, fn);
4689     }
4690     return true;
4691 }
4692 TRANS_FEAT(SM3TT1A, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt1a)
4693 TRANS_FEAT(SM3TT1B, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt1b)
4694 TRANS_FEAT(SM3TT2A, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt2a)
4695 TRANS_FEAT(SM3TT2B, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt2b)
4696 
4697 static bool trans_XAR(DisasContext *s, arg_XAR *a)
4698 {
4699     if (!dc_isar_feature(aa64_sha3, s)) {
4700         return false;
4701     }
4702     if (fp_access_check(s)) {
4703         gen_gvec_xar(MO_64, vec_full_reg_offset(s, a->rd),
4704                      vec_full_reg_offset(s, a->rn),
4705                      vec_full_reg_offset(s, a->rm), a->imm, 16,
4706                      vec_full_reg_size(s));
4707     }
4708     return true;
4709 }
4710 
4711 /*
4712  * Advanced SIMD copy
4713  */
4714 
4715 static bool decode_esz_idx(int imm, MemOp *pesz, unsigned *pidx)
4716 {
4717     unsigned esz = ctz32(imm);
4718     if (esz <= MO_64) {
4719         *pesz = esz;
4720         *pidx = imm >> (esz + 1);
4721         return true;
4722     }
4723     return false;
4724 }
4725 
4726 static bool trans_DUP_element_s(DisasContext *s, arg_DUP_element_s *a)
4727 {
4728     MemOp esz;
4729     unsigned idx;
4730 
4731     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4732         return false;
4733     }
4734     if (fp_access_check(s)) {
4735         /*
4736          * This instruction just extracts the specified element and
4737          * zero-extends it into the bottom of the destination register.
4738          */
4739         TCGv_i64 tmp = tcg_temp_new_i64();
4740         read_vec_element(s, tmp, a->rn, idx, esz);
4741         write_fp_dreg(s, a->rd, tmp);
4742     }
4743     return true;
4744 }
4745 
4746 static bool trans_DUP_element_v(DisasContext *s, arg_DUP_element_v *a)
4747 {
4748     MemOp esz;
4749     unsigned idx;
4750 
4751     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4752         return false;
4753     }
4754     if (esz == MO_64 && !a->q) {
4755         return false;
4756     }
4757     if (fp_access_check(s)) {
4758         tcg_gen_gvec_dup_mem(esz, vec_full_reg_offset(s, a->rd),
4759                              vec_reg_offset(s, a->rn, idx, esz),
4760                              a->q ? 16 : 8, vec_full_reg_size(s));
4761     }
4762     return true;
4763 }
4764 
4765 static bool trans_DUP_general(DisasContext *s, arg_DUP_general *a)
4766 {
4767     MemOp esz;
4768     unsigned idx;
4769 
4770     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4771         return false;
4772     }
4773     if (esz == MO_64 && !a->q) {
4774         return false;
4775     }
4776     if (fp_access_check(s)) {
4777         tcg_gen_gvec_dup_i64(esz, vec_full_reg_offset(s, a->rd),
4778                              a->q ? 16 : 8, vec_full_reg_size(s),
4779                              cpu_reg(s, a->rn));
4780     }
4781     return true;
4782 }
4783 
4784 static bool do_smov_umov(DisasContext *s, arg_SMOV *a, MemOp is_signed)
4785 {
4786     MemOp esz;
4787     unsigned idx;
4788 
4789     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4790         return false;
4791     }
4792     if (is_signed) {
4793         if (esz == MO_64 || (esz == MO_32 && !a->q)) {
4794             return false;
4795         }
4796     } else {
4797         if (esz == MO_64 ? !a->q : a->q) {
4798             return false;
4799         }
4800     }
4801     if (fp_access_check(s)) {
4802         TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4803         read_vec_element(s, tcg_rd, a->rn, idx, esz | is_signed);
4804         if (is_signed && !a->q) {
4805             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4806         }
4807     }
4808     return true;
4809 }
4810 
4811 TRANS(SMOV, do_smov_umov, a, MO_SIGN)
4812 TRANS(UMOV, do_smov_umov, a, 0)
4813 
4814 static bool trans_INS_general(DisasContext *s, arg_INS_general *a)
4815 {
4816     MemOp esz;
4817     unsigned idx;
4818 
4819     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4820         return false;
4821     }
4822     if (fp_access_check(s)) {
4823         write_vec_element(s, cpu_reg(s, a->rn), a->rd, idx, esz);
4824         clear_vec_high(s, true, a->rd);
4825     }
4826     return true;
4827 }
4828 
4829 static bool trans_INS_element(DisasContext *s, arg_INS_element *a)
4830 {
4831     MemOp esz;
4832     unsigned didx, sidx;
4833 
4834     if (!decode_esz_idx(a->di, &esz, &didx)) {
4835         return false;
4836     }
4837     sidx = a->si >> esz;
4838     if (fp_access_check(s)) {
4839         TCGv_i64 tmp = tcg_temp_new_i64();
4840 
4841         read_vec_element(s, tmp, a->rn, sidx, esz);
4842         write_vec_element(s, tmp, a->rd, didx, esz);
4843 
4844         /* INS is considered a 128-bit write for SVE. */
4845         clear_vec_high(s, true, a->rd);
4846     }
4847     return true;
4848 }
4849 
4850 /*
4851  * Advanced SIMD three same
4852  */
4853 
4854 typedef struct FPScalar {
4855     void (*gen_h)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
4856     void (*gen_s)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
4857     void (*gen_d)(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
4858 } FPScalar;
4859 
4860 static bool do_fp3_scalar(DisasContext *s, arg_rrr_e *a, const FPScalar *f)
4861 {
4862     switch (a->esz) {
4863     case MO_64:
4864         if (fp_access_check(s)) {
4865             TCGv_i64 t0 = read_fp_dreg(s, a->rn);
4866             TCGv_i64 t1 = read_fp_dreg(s, a->rm);
4867             f->gen_d(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
4868             write_fp_dreg(s, a->rd, t0);
4869         }
4870         break;
4871     case MO_32:
4872         if (fp_access_check(s)) {
4873             TCGv_i32 t0 = read_fp_sreg(s, a->rn);
4874             TCGv_i32 t1 = read_fp_sreg(s, a->rm);
4875             f->gen_s(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
4876             write_fp_sreg(s, a->rd, t0);
4877         }
4878         break;
4879     case MO_16:
4880         if (!dc_isar_feature(aa64_fp16, s)) {
4881             return false;
4882         }
4883         if (fp_access_check(s)) {
4884             TCGv_i32 t0 = read_fp_hreg(s, a->rn);
4885             TCGv_i32 t1 = read_fp_hreg(s, a->rm);
4886             f->gen_h(t0, t0, t1, fpstatus_ptr(FPST_FPCR_F16));
4887             write_fp_sreg(s, a->rd, t0);
4888         }
4889         break;
4890     default:
4891         return false;
4892     }
4893     return true;
4894 }
4895 
4896 static const FPScalar f_scalar_fadd = {
4897     gen_helper_vfp_addh,
4898     gen_helper_vfp_adds,
4899     gen_helper_vfp_addd,
4900 };
4901 TRANS(FADD_s, do_fp3_scalar, a, &f_scalar_fadd)
4902 
4903 static const FPScalar f_scalar_fsub = {
4904     gen_helper_vfp_subh,
4905     gen_helper_vfp_subs,
4906     gen_helper_vfp_subd,
4907 };
4908 TRANS(FSUB_s, do_fp3_scalar, a, &f_scalar_fsub)
4909 
4910 static const FPScalar f_scalar_fdiv = {
4911     gen_helper_vfp_divh,
4912     gen_helper_vfp_divs,
4913     gen_helper_vfp_divd,
4914 };
4915 TRANS(FDIV_s, do_fp3_scalar, a, &f_scalar_fdiv)
4916 
4917 static const FPScalar f_scalar_fmul = {
4918     gen_helper_vfp_mulh,
4919     gen_helper_vfp_muls,
4920     gen_helper_vfp_muld,
4921 };
4922 TRANS(FMUL_s, do_fp3_scalar, a, &f_scalar_fmul)
4923 
4924 static const FPScalar f_scalar_fmax = {
4925     gen_helper_advsimd_maxh,
4926     gen_helper_vfp_maxs,
4927     gen_helper_vfp_maxd,
4928 };
4929 TRANS(FMAX_s, do_fp3_scalar, a, &f_scalar_fmax)
4930 
4931 static const FPScalar f_scalar_fmin = {
4932     gen_helper_advsimd_minh,
4933     gen_helper_vfp_mins,
4934     gen_helper_vfp_mind,
4935 };
4936 TRANS(FMIN_s, do_fp3_scalar, a, &f_scalar_fmin)
4937 
4938 static const FPScalar f_scalar_fmaxnm = {
4939     gen_helper_advsimd_maxnumh,
4940     gen_helper_vfp_maxnums,
4941     gen_helper_vfp_maxnumd,
4942 };
4943 TRANS(FMAXNM_s, do_fp3_scalar, a, &f_scalar_fmaxnm)
4944 
4945 static const FPScalar f_scalar_fminnm = {
4946     gen_helper_advsimd_minnumh,
4947     gen_helper_vfp_minnums,
4948     gen_helper_vfp_minnumd,
4949 };
4950 TRANS(FMINNM_s, do_fp3_scalar, a, &f_scalar_fminnm)
4951 
4952 static const FPScalar f_scalar_fmulx = {
4953     gen_helper_advsimd_mulxh,
4954     gen_helper_vfp_mulxs,
4955     gen_helper_vfp_mulxd,
4956 };
4957 TRANS(FMULX_s, do_fp3_scalar, a, &f_scalar_fmulx)
4958 
4959 static void gen_fnmul_h(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
4960 {
4961     gen_helper_vfp_mulh(d, n, m, s);
4962     gen_vfp_negh(d, d);
4963 }
4964 
4965 static void gen_fnmul_s(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
4966 {
4967     gen_helper_vfp_muls(d, n, m, s);
4968     gen_vfp_negs(d, d);
4969 }
4970 
4971 static void gen_fnmul_d(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_ptr s)
4972 {
4973     gen_helper_vfp_muld(d, n, m, s);
4974     gen_vfp_negd(d, d);
4975 }
4976 
4977 static const FPScalar f_scalar_fnmul = {
4978     gen_fnmul_h,
4979     gen_fnmul_s,
4980     gen_fnmul_d,
4981 };
4982 TRANS(FNMUL_s, do_fp3_scalar, a, &f_scalar_fnmul)
4983 
4984 static const FPScalar f_scalar_fcmeq = {
4985     gen_helper_advsimd_ceq_f16,
4986     gen_helper_neon_ceq_f32,
4987     gen_helper_neon_ceq_f64,
4988 };
4989 TRANS(FCMEQ_s, do_fp3_scalar, a, &f_scalar_fcmeq)
4990 
4991 static const FPScalar f_scalar_fcmge = {
4992     gen_helper_advsimd_cge_f16,
4993     gen_helper_neon_cge_f32,
4994     gen_helper_neon_cge_f64,
4995 };
4996 TRANS(FCMGE_s, do_fp3_scalar, a, &f_scalar_fcmge)
4997 
4998 static const FPScalar f_scalar_fcmgt = {
4999     gen_helper_advsimd_cgt_f16,
5000     gen_helper_neon_cgt_f32,
5001     gen_helper_neon_cgt_f64,
5002 };
5003 TRANS(FCMGT_s, do_fp3_scalar, a, &f_scalar_fcmgt)
5004 
5005 static const FPScalar f_scalar_facge = {
5006     gen_helper_advsimd_acge_f16,
5007     gen_helper_neon_acge_f32,
5008     gen_helper_neon_acge_f64,
5009 };
5010 TRANS(FACGE_s, do_fp3_scalar, a, &f_scalar_facge)
5011 
5012 static const FPScalar f_scalar_facgt = {
5013     gen_helper_advsimd_acgt_f16,
5014     gen_helper_neon_acgt_f32,
5015     gen_helper_neon_acgt_f64,
5016 };
5017 TRANS(FACGT_s, do_fp3_scalar, a, &f_scalar_facgt)
5018 
5019 static void gen_fabd_h(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
5020 {
5021     gen_helper_vfp_subh(d, n, m, s);
5022     gen_vfp_absh(d, d);
5023 }
5024 
5025 static void gen_fabd_s(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
5026 {
5027     gen_helper_vfp_subs(d, n, m, s);
5028     gen_vfp_abss(d, d);
5029 }
5030 
5031 static void gen_fabd_d(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_ptr s)
5032 {
5033     gen_helper_vfp_subd(d, n, m, s);
5034     gen_vfp_absd(d, d);
5035 }
5036 
5037 static const FPScalar f_scalar_fabd = {
5038     gen_fabd_h,
5039     gen_fabd_s,
5040     gen_fabd_d,
5041 };
5042 TRANS(FABD_s, do_fp3_scalar, a, &f_scalar_fabd)
5043 
5044 static const FPScalar f_scalar_frecps = {
5045     gen_helper_recpsf_f16,
5046     gen_helper_recpsf_f32,
5047     gen_helper_recpsf_f64,
5048 };
5049 TRANS(FRECPS_s, do_fp3_scalar, a, &f_scalar_frecps)
5050 
5051 static const FPScalar f_scalar_frsqrts = {
5052     gen_helper_rsqrtsf_f16,
5053     gen_helper_rsqrtsf_f32,
5054     gen_helper_rsqrtsf_f64,
5055 };
5056 TRANS(FRSQRTS_s, do_fp3_scalar, a, &f_scalar_frsqrts)
5057 
5058 static bool do_satacc_s(DisasContext *s, arg_rrr_e *a,
5059                 MemOp sgn_n, MemOp sgn_m,
5060                 void (*gen_bhs)(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_i64, MemOp),
5061                 void (*gen_d)(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_i64))
5062 {
5063     TCGv_i64 t0, t1, t2, qc;
5064     MemOp esz = a->esz;
5065 
5066     if (!fp_access_check(s)) {
5067         return true;
5068     }
5069 
5070     t0 = tcg_temp_new_i64();
5071     t1 = tcg_temp_new_i64();
5072     t2 = tcg_temp_new_i64();
5073     qc = tcg_temp_new_i64();
5074     read_vec_element(s, t1, a->rn, 0, esz | sgn_n);
5075     read_vec_element(s, t2, a->rm, 0, esz | sgn_m);
5076     tcg_gen_ld_i64(qc, tcg_env, offsetof(CPUARMState, vfp.qc));
5077 
5078     if (esz == MO_64) {
5079         gen_d(t0, qc, t1, t2);
5080     } else {
5081         gen_bhs(t0, qc, t1, t2, esz);
5082         tcg_gen_ext_i64(t0, t0, esz);
5083     }
5084 
5085     write_fp_dreg(s, a->rd, t0);
5086     tcg_gen_st_i64(qc, tcg_env, offsetof(CPUARMState, vfp.qc));
5087     return true;
5088 }
5089 
5090 TRANS(SQADD_s, do_satacc_s, a, MO_SIGN, MO_SIGN, gen_sqadd_bhs, gen_sqadd_d)
5091 TRANS(SQSUB_s, do_satacc_s, a, MO_SIGN, MO_SIGN, gen_sqsub_bhs, gen_sqsub_d)
5092 TRANS(UQADD_s, do_satacc_s, a, 0, 0, gen_uqadd_bhs, gen_uqadd_d)
5093 TRANS(UQSUB_s, do_satacc_s, a, 0, 0, gen_uqsub_bhs, gen_uqsub_d)
5094 TRANS(SUQADD_s, do_satacc_s, a, MO_SIGN, 0, gen_suqadd_bhs, gen_suqadd_d)
5095 TRANS(USQADD_s, do_satacc_s, a, 0, MO_SIGN, gen_usqadd_bhs, gen_usqadd_d)
5096 
5097 static bool do_int3_scalar_d(DisasContext *s, arg_rrr_e *a,
5098                              void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
5099 {
5100     if (fp_access_check(s)) {
5101         TCGv_i64 t0 = tcg_temp_new_i64();
5102         TCGv_i64 t1 = tcg_temp_new_i64();
5103 
5104         read_vec_element(s, t0, a->rn, 0, MO_64);
5105         read_vec_element(s, t1, a->rm, 0, MO_64);
5106         fn(t0, t0, t1);
5107         write_fp_dreg(s, a->rd, t0);
5108     }
5109     return true;
5110 }
5111 
5112 TRANS(SSHL_s, do_int3_scalar_d, a, gen_sshl_i64)
5113 TRANS(USHL_s, do_int3_scalar_d, a, gen_ushl_i64)
5114 TRANS(SRSHL_s, do_int3_scalar_d, a, gen_helper_neon_rshl_s64)
5115 TRANS(URSHL_s, do_int3_scalar_d, a, gen_helper_neon_rshl_u64)
5116 TRANS(ADD_s, do_int3_scalar_d, a, tcg_gen_add_i64)
5117 TRANS(SUB_s, do_int3_scalar_d, a, tcg_gen_sub_i64)
5118 
5119 typedef struct ENVScalar2 {
5120     NeonGenTwoOpEnvFn *gen_bhs[3];
5121     NeonGenTwo64OpEnvFn *gen_d;
5122 } ENVScalar2;
5123 
5124 static bool do_env_scalar2(DisasContext *s, arg_rrr_e *a, const ENVScalar2 *f)
5125 {
5126     if (!fp_access_check(s)) {
5127         return true;
5128     }
5129     if (a->esz == MO_64) {
5130         TCGv_i64 t0 = read_fp_dreg(s, a->rn);
5131         TCGv_i64 t1 = read_fp_dreg(s, a->rm);
5132         f->gen_d(t0, tcg_env, t0, t1);
5133         write_fp_dreg(s, a->rd, t0);
5134     } else {
5135         TCGv_i32 t0 = tcg_temp_new_i32();
5136         TCGv_i32 t1 = tcg_temp_new_i32();
5137 
5138         read_vec_element_i32(s, t0, a->rn, 0, a->esz);
5139         read_vec_element_i32(s, t1, a->rm, 0, a->esz);
5140         f->gen_bhs[a->esz](t0, tcg_env, t0, t1);
5141         write_fp_sreg(s, a->rd, t0);
5142     }
5143     return true;
5144 }
5145 
5146 static const ENVScalar2 f_scalar_sqshl = {
5147     { gen_helper_neon_qshl_s8,
5148       gen_helper_neon_qshl_s16,
5149       gen_helper_neon_qshl_s32 },
5150     gen_helper_neon_qshl_s64,
5151 };
5152 TRANS(SQSHL_s, do_env_scalar2, a, &f_scalar_sqshl)
5153 
5154 static const ENVScalar2 f_scalar_uqshl = {
5155     { gen_helper_neon_qshl_u8,
5156       gen_helper_neon_qshl_u16,
5157       gen_helper_neon_qshl_u32 },
5158     gen_helper_neon_qshl_u64,
5159 };
5160 TRANS(UQSHL_s, do_env_scalar2, a, &f_scalar_uqshl)
5161 
5162 static const ENVScalar2 f_scalar_sqrshl = {
5163     { gen_helper_neon_qrshl_s8,
5164       gen_helper_neon_qrshl_s16,
5165       gen_helper_neon_qrshl_s32 },
5166     gen_helper_neon_qrshl_s64,
5167 };
5168 TRANS(SQRSHL_s, do_env_scalar2, a, &f_scalar_sqrshl)
5169 
5170 static const ENVScalar2 f_scalar_uqrshl = {
5171     { gen_helper_neon_qrshl_u8,
5172       gen_helper_neon_qrshl_u16,
5173       gen_helper_neon_qrshl_u32 },
5174     gen_helper_neon_qrshl_u64,
5175 };
5176 TRANS(UQRSHL_s, do_env_scalar2, a, &f_scalar_uqrshl)
5177 
5178 static bool do_env_scalar2_hs(DisasContext *s, arg_rrr_e *a,
5179                               const ENVScalar2 *f)
5180 {
5181     if (a->esz == MO_16 || a->esz == MO_32) {
5182         return do_env_scalar2(s, a, f);
5183     }
5184     return false;
5185 }
5186 
5187 static const ENVScalar2 f_scalar_sqdmulh = {
5188     { NULL, gen_helper_neon_qdmulh_s16, gen_helper_neon_qdmulh_s32 }
5189 };
5190 TRANS(SQDMULH_s, do_env_scalar2_hs, a, &f_scalar_sqdmulh)
5191 
5192 static const ENVScalar2 f_scalar_sqrdmulh = {
5193     { NULL, gen_helper_neon_qrdmulh_s16, gen_helper_neon_qrdmulh_s32 }
5194 };
5195 TRANS(SQRDMULH_s, do_env_scalar2_hs, a, &f_scalar_sqrdmulh)
5196 
5197 static bool do_cmop_d(DisasContext *s, arg_rrr_e *a, TCGCond cond)
5198 {
5199     if (fp_access_check(s)) {
5200         TCGv_i64 t0 = read_fp_dreg(s, a->rn);
5201         TCGv_i64 t1 = read_fp_dreg(s, a->rm);
5202         tcg_gen_negsetcond_i64(cond, t0, t0, t1);
5203         write_fp_dreg(s, a->rd, t0);
5204     }
5205     return true;
5206 }
5207 
5208 TRANS(CMGT_s, do_cmop_d, a, TCG_COND_GT)
5209 TRANS(CMHI_s, do_cmop_d, a, TCG_COND_GTU)
5210 TRANS(CMGE_s, do_cmop_d, a, TCG_COND_GE)
5211 TRANS(CMHS_s, do_cmop_d, a, TCG_COND_GEU)
5212 TRANS(CMEQ_s, do_cmop_d, a, TCG_COND_EQ)
5213 TRANS(CMTST_s, do_cmop_d, a, TCG_COND_TSTNE)
5214 
5215 static bool do_fp3_vector(DisasContext *s, arg_qrrr_e *a,
5216                           gen_helper_gvec_3_ptr * const fns[3])
5217 {
5218     MemOp esz = a->esz;
5219 
5220     switch (esz) {
5221     case MO_64:
5222         if (!a->q) {
5223             return false;
5224         }
5225         break;
5226     case MO_32:
5227         break;
5228     case MO_16:
5229         if (!dc_isar_feature(aa64_fp16, s)) {
5230             return false;
5231         }
5232         break;
5233     default:
5234         return false;
5235     }
5236     if (fp_access_check(s)) {
5237         gen_gvec_op3_fpst(s, a->q, a->rd, a->rn, a->rm,
5238                           esz == MO_16, 0, fns[esz - 1]);
5239     }
5240     return true;
5241 }
5242 
5243 static gen_helper_gvec_3_ptr * const f_vector_fadd[3] = {
5244     gen_helper_gvec_fadd_h,
5245     gen_helper_gvec_fadd_s,
5246     gen_helper_gvec_fadd_d,
5247 };
5248 TRANS(FADD_v, do_fp3_vector, a, f_vector_fadd)
5249 
5250 static gen_helper_gvec_3_ptr * const f_vector_fsub[3] = {
5251     gen_helper_gvec_fsub_h,
5252     gen_helper_gvec_fsub_s,
5253     gen_helper_gvec_fsub_d,
5254 };
5255 TRANS(FSUB_v, do_fp3_vector, a, f_vector_fsub)
5256 
5257 static gen_helper_gvec_3_ptr * const f_vector_fdiv[3] = {
5258     gen_helper_gvec_fdiv_h,
5259     gen_helper_gvec_fdiv_s,
5260     gen_helper_gvec_fdiv_d,
5261 };
5262 TRANS(FDIV_v, do_fp3_vector, a, f_vector_fdiv)
5263 
5264 static gen_helper_gvec_3_ptr * const f_vector_fmul[3] = {
5265     gen_helper_gvec_fmul_h,
5266     gen_helper_gvec_fmul_s,
5267     gen_helper_gvec_fmul_d,
5268 };
5269 TRANS(FMUL_v, do_fp3_vector, a, f_vector_fmul)
5270 
5271 static gen_helper_gvec_3_ptr * const f_vector_fmax[3] = {
5272     gen_helper_gvec_fmax_h,
5273     gen_helper_gvec_fmax_s,
5274     gen_helper_gvec_fmax_d,
5275 };
5276 TRANS(FMAX_v, do_fp3_vector, a, f_vector_fmax)
5277 
5278 static gen_helper_gvec_3_ptr * const f_vector_fmin[3] = {
5279     gen_helper_gvec_fmin_h,
5280     gen_helper_gvec_fmin_s,
5281     gen_helper_gvec_fmin_d,
5282 };
5283 TRANS(FMIN_v, do_fp3_vector, a, f_vector_fmin)
5284 
5285 static gen_helper_gvec_3_ptr * const f_vector_fmaxnm[3] = {
5286     gen_helper_gvec_fmaxnum_h,
5287     gen_helper_gvec_fmaxnum_s,
5288     gen_helper_gvec_fmaxnum_d,
5289 };
5290 TRANS(FMAXNM_v, do_fp3_vector, a, f_vector_fmaxnm)
5291 
5292 static gen_helper_gvec_3_ptr * const f_vector_fminnm[3] = {
5293     gen_helper_gvec_fminnum_h,
5294     gen_helper_gvec_fminnum_s,
5295     gen_helper_gvec_fminnum_d,
5296 };
5297 TRANS(FMINNM_v, do_fp3_vector, a, f_vector_fminnm)
5298 
5299 static gen_helper_gvec_3_ptr * const f_vector_fmulx[3] = {
5300     gen_helper_gvec_fmulx_h,
5301     gen_helper_gvec_fmulx_s,
5302     gen_helper_gvec_fmulx_d,
5303 };
5304 TRANS(FMULX_v, do_fp3_vector, a, f_vector_fmulx)
5305 
5306 static gen_helper_gvec_3_ptr * const f_vector_fmla[3] = {
5307     gen_helper_gvec_vfma_h,
5308     gen_helper_gvec_vfma_s,
5309     gen_helper_gvec_vfma_d,
5310 };
5311 TRANS(FMLA_v, do_fp3_vector, a, f_vector_fmla)
5312 
5313 static gen_helper_gvec_3_ptr * const f_vector_fmls[3] = {
5314     gen_helper_gvec_vfms_h,
5315     gen_helper_gvec_vfms_s,
5316     gen_helper_gvec_vfms_d,
5317 };
5318 TRANS(FMLS_v, do_fp3_vector, a, f_vector_fmls)
5319 
5320 static gen_helper_gvec_3_ptr * const f_vector_fcmeq[3] = {
5321     gen_helper_gvec_fceq_h,
5322     gen_helper_gvec_fceq_s,
5323     gen_helper_gvec_fceq_d,
5324 };
5325 TRANS(FCMEQ_v, do_fp3_vector, a, f_vector_fcmeq)
5326 
5327 static gen_helper_gvec_3_ptr * const f_vector_fcmge[3] = {
5328     gen_helper_gvec_fcge_h,
5329     gen_helper_gvec_fcge_s,
5330     gen_helper_gvec_fcge_d,
5331 };
5332 TRANS(FCMGE_v, do_fp3_vector, a, f_vector_fcmge)
5333 
5334 static gen_helper_gvec_3_ptr * const f_vector_fcmgt[3] = {
5335     gen_helper_gvec_fcgt_h,
5336     gen_helper_gvec_fcgt_s,
5337     gen_helper_gvec_fcgt_d,
5338 };
5339 TRANS(FCMGT_v, do_fp3_vector, a, f_vector_fcmgt)
5340 
5341 static gen_helper_gvec_3_ptr * const f_vector_facge[3] = {
5342     gen_helper_gvec_facge_h,
5343     gen_helper_gvec_facge_s,
5344     gen_helper_gvec_facge_d,
5345 };
5346 TRANS(FACGE_v, do_fp3_vector, a, f_vector_facge)
5347 
5348 static gen_helper_gvec_3_ptr * const f_vector_facgt[3] = {
5349     gen_helper_gvec_facgt_h,
5350     gen_helper_gvec_facgt_s,
5351     gen_helper_gvec_facgt_d,
5352 };
5353 TRANS(FACGT_v, do_fp3_vector, a, f_vector_facgt)
5354 
5355 static gen_helper_gvec_3_ptr * const f_vector_fabd[3] = {
5356     gen_helper_gvec_fabd_h,
5357     gen_helper_gvec_fabd_s,
5358     gen_helper_gvec_fabd_d,
5359 };
5360 TRANS(FABD_v, do_fp3_vector, a, f_vector_fabd)
5361 
5362 static gen_helper_gvec_3_ptr * const f_vector_frecps[3] = {
5363     gen_helper_gvec_recps_h,
5364     gen_helper_gvec_recps_s,
5365     gen_helper_gvec_recps_d,
5366 };
5367 TRANS(FRECPS_v, do_fp3_vector, a, f_vector_frecps)
5368 
5369 static gen_helper_gvec_3_ptr * const f_vector_frsqrts[3] = {
5370     gen_helper_gvec_rsqrts_h,
5371     gen_helper_gvec_rsqrts_s,
5372     gen_helper_gvec_rsqrts_d,
5373 };
5374 TRANS(FRSQRTS_v, do_fp3_vector, a, f_vector_frsqrts)
5375 
5376 static gen_helper_gvec_3_ptr * const f_vector_faddp[3] = {
5377     gen_helper_gvec_faddp_h,
5378     gen_helper_gvec_faddp_s,
5379     gen_helper_gvec_faddp_d,
5380 };
5381 TRANS(FADDP_v, do_fp3_vector, a, f_vector_faddp)
5382 
5383 static gen_helper_gvec_3_ptr * const f_vector_fmaxp[3] = {
5384     gen_helper_gvec_fmaxp_h,
5385     gen_helper_gvec_fmaxp_s,
5386     gen_helper_gvec_fmaxp_d,
5387 };
5388 TRANS(FMAXP_v, do_fp3_vector, a, f_vector_fmaxp)
5389 
5390 static gen_helper_gvec_3_ptr * const f_vector_fminp[3] = {
5391     gen_helper_gvec_fminp_h,
5392     gen_helper_gvec_fminp_s,
5393     gen_helper_gvec_fminp_d,
5394 };
5395 TRANS(FMINP_v, do_fp3_vector, a, f_vector_fminp)
5396 
5397 static gen_helper_gvec_3_ptr * const f_vector_fmaxnmp[3] = {
5398     gen_helper_gvec_fmaxnump_h,
5399     gen_helper_gvec_fmaxnump_s,
5400     gen_helper_gvec_fmaxnump_d,
5401 };
5402 TRANS(FMAXNMP_v, do_fp3_vector, a, f_vector_fmaxnmp)
5403 
5404 static gen_helper_gvec_3_ptr * const f_vector_fminnmp[3] = {
5405     gen_helper_gvec_fminnump_h,
5406     gen_helper_gvec_fminnump_s,
5407     gen_helper_gvec_fminnump_d,
5408 };
5409 TRANS(FMINNMP_v, do_fp3_vector, a, f_vector_fminnmp)
5410 
5411 static bool do_fmlal(DisasContext *s, arg_qrrr_e *a, bool is_s, bool is_2)
5412 {
5413     if (fp_access_check(s)) {
5414         int data = (is_2 << 1) | is_s;
5415         tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
5416                            vec_full_reg_offset(s, a->rn),
5417                            vec_full_reg_offset(s, a->rm), tcg_env,
5418                            a->q ? 16 : 8, vec_full_reg_size(s),
5419                            data, gen_helper_gvec_fmlal_a64);
5420     }
5421     return true;
5422 }
5423 
5424 TRANS_FEAT(FMLAL_v, aa64_fhm, do_fmlal, a, false, false)
5425 TRANS_FEAT(FMLSL_v, aa64_fhm, do_fmlal, a, true, false)
5426 TRANS_FEAT(FMLAL2_v, aa64_fhm, do_fmlal, a, false, true)
5427 TRANS_FEAT(FMLSL2_v, aa64_fhm, do_fmlal, a, true, true)
5428 
5429 TRANS(ADDP_v, do_gvec_fn3, a, gen_gvec_addp)
5430 TRANS(SMAXP_v, do_gvec_fn3_no64, a, gen_gvec_smaxp)
5431 TRANS(SMINP_v, do_gvec_fn3_no64, a, gen_gvec_sminp)
5432 TRANS(UMAXP_v, do_gvec_fn3_no64, a, gen_gvec_umaxp)
5433 TRANS(UMINP_v, do_gvec_fn3_no64, a, gen_gvec_uminp)
5434 
5435 TRANS(AND_v, do_gvec_fn3, a, tcg_gen_gvec_and)
5436 TRANS(BIC_v, do_gvec_fn3, a, tcg_gen_gvec_andc)
5437 TRANS(ORR_v, do_gvec_fn3, a, tcg_gen_gvec_or)
5438 TRANS(ORN_v, do_gvec_fn3, a, tcg_gen_gvec_orc)
5439 TRANS(EOR_v, do_gvec_fn3, a, tcg_gen_gvec_xor)
5440 
5441 static bool do_bitsel(DisasContext *s, bool is_q, int d, int a, int b, int c)
5442 {
5443     if (fp_access_check(s)) {
5444         gen_gvec_fn4(s, is_q, d, a, b, c, tcg_gen_gvec_bitsel, 0);
5445     }
5446     return true;
5447 }
5448 
5449 TRANS(BSL_v, do_bitsel, a->q, a->rd, a->rd, a->rn, a->rm)
5450 TRANS(BIT_v, do_bitsel, a->q, a->rd, a->rm, a->rn, a->rd)
5451 TRANS(BIF_v, do_bitsel, a->q, a->rd, a->rm, a->rd, a->rn)
5452 
5453 TRANS(SQADD_v, do_gvec_fn3, a, gen_gvec_sqadd_qc)
5454 TRANS(UQADD_v, do_gvec_fn3, a, gen_gvec_uqadd_qc)
5455 TRANS(SQSUB_v, do_gvec_fn3, a, gen_gvec_sqsub_qc)
5456 TRANS(UQSUB_v, do_gvec_fn3, a, gen_gvec_uqsub_qc)
5457 TRANS(SUQADD_v, do_gvec_fn3, a, gen_gvec_suqadd_qc)
5458 TRANS(USQADD_v, do_gvec_fn3, a, gen_gvec_usqadd_qc)
5459 
5460 TRANS(SSHL_v, do_gvec_fn3, a, gen_gvec_sshl)
5461 TRANS(USHL_v, do_gvec_fn3, a, gen_gvec_ushl)
5462 TRANS(SRSHL_v, do_gvec_fn3, a, gen_gvec_srshl)
5463 TRANS(URSHL_v, do_gvec_fn3, a, gen_gvec_urshl)
5464 TRANS(SQSHL_v, do_gvec_fn3, a, gen_neon_sqshl)
5465 TRANS(UQSHL_v, do_gvec_fn3, a, gen_neon_uqshl)
5466 TRANS(SQRSHL_v, do_gvec_fn3, a, gen_neon_sqrshl)
5467 TRANS(UQRSHL_v, do_gvec_fn3, a, gen_neon_uqrshl)
5468 
5469 TRANS(ADD_v, do_gvec_fn3, a, tcg_gen_gvec_add)
5470 TRANS(SUB_v, do_gvec_fn3, a, tcg_gen_gvec_sub)
5471 TRANS(SHADD_v, do_gvec_fn3_no64, a, gen_gvec_shadd)
5472 TRANS(UHADD_v, do_gvec_fn3_no64, a, gen_gvec_uhadd)
5473 TRANS(SHSUB_v, do_gvec_fn3_no64, a, gen_gvec_shsub)
5474 TRANS(UHSUB_v, do_gvec_fn3_no64, a, gen_gvec_uhsub)
5475 TRANS(SRHADD_v, do_gvec_fn3_no64, a, gen_gvec_srhadd)
5476 TRANS(URHADD_v, do_gvec_fn3_no64, a, gen_gvec_urhadd)
5477 TRANS(SMAX_v, do_gvec_fn3_no64, a, tcg_gen_gvec_smax)
5478 TRANS(UMAX_v, do_gvec_fn3_no64, a, tcg_gen_gvec_umax)
5479 TRANS(SMIN_v, do_gvec_fn3_no64, a, tcg_gen_gvec_smin)
5480 TRANS(UMIN_v, do_gvec_fn3_no64, a, tcg_gen_gvec_umin)
5481 TRANS(SABA_v, do_gvec_fn3_no64, a, gen_gvec_saba)
5482 TRANS(UABA_v, do_gvec_fn3_no64, a, gen_gvec_uaba)
5483 TRANS(SABD_v, do_gvec_fn3_no64, a, gen_gvec_sabd)
5484 TRANS(UABD_v, do_gvec_fn3_no64, a, gen_gvec_uabd)
5485 TRANS(MUL_v, do_gvec_fn3_no64, a, tcg_gen_gvec_mul)
5486 TRANS(PMUL_v, do_gvec_op3_ool, a, 0, gen_helper_gvec_pmul_b)
5487 TRANS(MLA_v, do_gvec_fn3_no64, a, gen_gvec_mla)
5488 TRANS(MLS_v, do_gvec_fn3_no64, a, gen_gvec_mls)
5489 
5490 static bool do_cmop_v(DisasContext *s, arg_qrrr_e *a, TCGCond cond)
5491 {
5492     if (a->esz == MO_64 && !a->q) {
5493         return false;
5494     }
5495     if (fp_access_check(s)) {
5496         tcg_gen_gvec_cmp(cond, a->esz,
5497                          vec_full_reg_offset(s, a->rd),
5498                          vec_full_reg_offset(s, a->rn),
5499                          vec_full_reg_offset(s, a->rm),
5500                          a->q ? 16 : 8, vec_full_reg_size(s));
5501     }
5502     return true;
5503 }
5504 
5505 TRANS(CMGT_v, do_cmop_v, a, TCG_COND_GT)
5506 TRANS(CMHI_v, do_cmop_v, a, TCG_COND_GTU)
5507 TRANS(CMGE_v, do_cmop_v, a, TCG_COND_GE)
5508 TRANS(CMHS_v, do_cmop_v, a, TCG_COND_GEU)
5509 TRANS(CMEQ_v, do_cmop_v, a, TCG_COND_EQ)
5510 TRANS(CMTST_v, do_gvec_fn3, a, gen_gvec_cmtst)
5511 
5512 TRANS(SQDMULH_v, do_gvec_fn3_no8_no64, a, gen_gvec_sqdmulh_qc)
5513 TRANS(SQRDMULH_v, do_gvec_fn3_no8_no64, a, gen_gvec_sqrdmulh_qc)
5514 
5515 /*
5516  * Advanced SIMD scalar/vector x indexed element
5517  */
5518 
5519 static bool do_fp3_scalar_idx(DisasContext *s, arg_rrx_e *a, const FPScalar *f)
5520 {
5521     switch (a->esz) {
5522     case MO_64:
5523         if (fp_access_check(s)) {
5524             TCGv_i64 t0 = read_fp_dreg(s, a->rn);
5525             TCGv_i64 t1 = tcg_temp_new_i64();
5526 
5527             read_vec_element(s, t1, a->rm, a->idx, MO_64);
5528             f->gen_d(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5529             write_fp_dreg(s, a->rd, t0);
5530         }
5531         break;
5532     case MO_32:
5533         if (fp_access_check(s)) {
5534             TCGv_i32 t0 = read_fp_sreg(s, a->rn);
5535             TCGv_i32 t1 = tcg_temp_new_i32();
5536 
5537             read_vec_element_i32(s, t1, a->rm, a->idx, MO_32);
5538             f->gen_s(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5539             write_fp_sreg(s, a->rd, t0);
5540         }
5541         break;
5542     case MO_16:
5543         if (!dc_isar_feature(aa64_fp16, s)) {
5544             return false;
5545         }
5546         if (fp_access_check(s)) {
5547             TCGv_i32 t0 = read_fp_hreg(s, a->rn);
5548             TCGv_i32 t1 = tcg_temp_new_i32();
5549 
5550             read_vec_element_i32(s, t1, a->rm, a->idx, MO_16);
5551             f->gen_h(t0, t0, t1, fpstatus_ptr(FPST_FPCR_F16));
5552             write_fp_sreg(s, a->rd, t0);
5553         }
5554         break;
5555     default:
5556         g_assert_not_reached();
5557     }
5558     return true;
5559 }
5560 
5561 TRANS(FMUL_si, do_fp3_scalar_idx, a, &f_scalar_fmul)
5562 TRANS(FMULX_si, do_fp3_scalar_idx, a, &f_scalar_fmulx)
5563 
5564 static bool do_fmla_scalar_idx(DisasContext *s, arg_rrx_e *a, bool neg)
5565 {
5566     switch (a->esz) {
5567     case MO_64:
5568         if (fp_access_check(s)) {
5569             TCGv_i64 t0 = read_fp_dreg(s, a->rd);
5570             TCGv_i64 t1 = read_fp_dreg(s, a->rn);
5571             TCGv_i64 t2 = tcg_temp_new_i64();
5572 
5573             read_vec_element(s, t2, a->rm, a->idx, MO_64);
5574             if (neg) {
5575                 gen_vfp_negd(t1, t1);
5576             }
5577             gen_helper_vfp_muladdd(t0, t1, t2, t0, fpstatus_ptr(FPST_FPCR));
5578             write_fp_dreg(s, a->rd, t0);
5579         }
5580         break;
5581     case MO_32:
5582         if (fp_access_check(s)) {
5583             TCGv_i32 t0 = read_fp_sreg(s, a->rd);
5584             TCGv_i32 t1 = read_fp_sreg(s, a->rn);
5585             TCGv_i32 t2 = tcg_temp_new_i32();
5586 
5587             read_vec_element_i32(s, t2, a->rm, a->idx, MO_32);
5588             if (neg) {
5589                 gen_vfp_negs(t1, t1);
5590             }
5591             gen_helper_vfp_muladds(t0, t1, t2, t0, fpstatus_ptr(FPST_FPCR));
5592             write_fp_sreg(s, a->rd, t0);
5593         }
5594         break;
5595     case MO_16:
5596         if (!dc_isar_feature(aa64_fp16, s)) {
5597             return false;
5598         }
5599         if (fp_access_check(s)) {
5600             TCGv_i32 t0 = read_fp_hreg(s, a->rd);
5601             TCGv_i32 t1 = read_fp_hreg(s, a->rn);
5602             TCGv_i32 t2 = tcg_temp_new_i32();
5603 
5604             read_vec_element_i32(s, t2, a->rm, a->idx, MO_16);
5605             if (neg) {
5606                 gen_vfp_negh(t1, t1);
5607             }
5608             gen_helper_advsimd_muladdh(t0, t1, t2, t0,
5609                                        fpstatus_ptr(FPST_FPCR_F16));
5610             write_fp_sreg(s, a->rd, t0);
5611         }
5612         break;
5613     default:
5614         g_assert_not_reached();
5615     }
5616     return true;
5617 }
5618 
5619 TRANS(FMLA_si, do_fmla_scalar_idx, a, false)
5620 TRANS(FMLS_si, do_fmla_scalar_idx, a, true)
5621 
5622 static bool do_env_scalar2_idx_hs(DisasContext *s, arg_rrx_e *a,
5623                                   const ENVScalar2 *f)
5624 {
5625     if (a->esz < MO_16 || a->esz > MO_32) {
5626         return false;
5627     }
5628     if (fp_access_check(s)) {
5629         TCGv_i32 t0 = tcg_temp_new_i32();
5630         TCGv_i32 t1 = tcg_temp_new_i32();
5631 
5632         read_vec_element_i32(s, t0, a->rn, 0, a->esz);
5633         read_vec_element_i32(s, t1, a->rm, a->idx, a->esz);
5634         f->gen_bhs[a->esz](t0, tcg_env, t0, t1);
5635         write_fp_sreg(s, a->rd, t0);
5636     }
5637     return true;
5638 }
5639 
5640 TRANS(SQDMULH_si, do_env_scalar2_idx_hs, a, &f_scalar_sqdmulh)
5641 TRANS(SQRDMULH_si, do_env_scalar2_idx_hs, a, &f_scalar_sqrdmulh)
5642 
5643 static bool do_fp3_vector_idx(DisasContext *s, arg_qrrx_e *a,
5644                               gen_helper_gvec_3_ptr * const fns[3])
5645 {
5646     MemOp esz = a->esz;
5647 
5648     switch (esz) {
5649     case MO_64:
5650         if (!a->q) {
5651             return false;
5652         }
5653         break;
5654     case MO_32:
5655         break;
5656     case MO_16:
5657         if (!dc_isar_feature(aa64_fp16, s)) {
5658             return false;
5659         }
5660         break;
5661     default:
5662         g_assert_not_reached();
5663     }
5664     if (fp_access_check(s)) {
5665         gen_gvec_op3_fpst(s, a->q, a->rd, a->rn, a->rm,
5666                           esz == MO_16, a->idx, fns[esz - 1]);
5667     }
5668     return true;
5669 }
5670 
5671 static gen_helper_gvec_3_ptr * const f_vector_idx_fmul[3] = {
5672     gen_helper_gvec_fmul_idx_h,
5673     gen_helper_gvec_fmul_idx_s,
5674     gen_helper_gvec_fmul_idx_d,
5675 };
5676 TRANS(FMUL_vi, do_fp3_vector_idx, a, f_vector_idx_fmul)
5677 
5678 static gen_helper_gvec_3_ptr * const f_vector_idx_fmulx[3] = {
5679     gen_helper_gvec_fmulx_idx_h,
5680     gen_helper_gvec_fmulx_idx_s,
5681     gen_helper_gvec_fmulx_idx_d,
5682 };
5683 TRANS(FMULX_vi, do_fp3_vector_idx, a, f_vector_idx_fmulx)
5684 
5685 static bool do_fmla_vector_idx(DisasContext *s, arg_qrrx_e *a, bool neg)
5686 {
5687     static gen_helper_gvec_4_ptr * const fns[3] = {
5688         gen_helper_gvec_fmla_idx_h,
5689         gen_helper_gvec_fmla_idx_s,
5690         gen_helper_gvec_fmla_idx_d,
5691     };
5692     MemOp esz = a->esz;
5693 
5694     switch (esz) {
5695     case MO_64:
5696         if (!a->q) {
5697             return false;
5698         }
5699         break;
5700     case MO_32:
5701         break;
5702     case MO_16:
5703         if (!dc_isar_feature(aa64_fp16, s)) {
5704             return false;
5705         }
5706         break;
5707     default:
5708         g_assert_not_reached();
5709     }
5710     if (fp_access_check(s)) {
5711         gen_gvec_op4_fpst(s, a->q, a->rd, a->rn, a->rm, a->rd,
5712                           esz == MO_16, (a->idx << 1) | neg,
5713                           fns[esz - 1]);
5714     }
5715     return true;
5716 }
5717 
5718 TRANS(FMLA_vi, do_fmla_vector_idx, a, false)
5719 TRANS(FMLS_vi, do_fmla_vector_idx, a, true)
5720 
5721 static bool do_fmlal_idx(DisasContext *s, arg_qrrx_e *a, bool is_s, bool is_2)
5722 {
5723     if (fp_access_check(s)) {
5724         int data = (a->idx << 2) | (is_2 << 1) | is_s;
5725         tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
5726                            vec_full_reg_offset(s, a->rn),
5727                            vec_full_reg_offset(s, a->rm), tcg_env,
5728                            a->q ? 16 : 8, vec_full_reg_size(s),
5729                            data, gen_helper_gvec_fmlal_idx_a64);
5730     }
5731     return true;
5732 }
5733 
5734 TRANS_FEAT(FMLAL_vi, aa64_fhm, do_fmlal_idx, a, false, false)
5735 TRANS_FEAT(FMLSL_vi, aa64_fhm, do_fmlal_idx, a, true, false)
5736 TRANS_FEAT(FMLAL2_vi, aa64_fhm, do_fmlal_idx, a, false, true)
5737 TRANS_FEAT(FMLSL2_vi, aa64_fhm, do_fmlal_idx, a, true, true)
5738 
5739 static bool do_int3_vector_idx(DisasContext *s, arg_qrrx_e *a,
5740                                gen_helper_gvec_3 * const fns[2])
5741 {
5742     assert(a->esz == MO_16 || a->esz == MO_32);
5743     if (fp_access_check(s)) {
5744         gen_gvec_op3_ool(s, a->q, a->rd, a->rn, a->rm, a->idx, fns[a->esz - 1]);
5745     }
5746     return true;
5747 }
5748 
5749 static gen_helper_gvec_3 * const f_vector_idx_mul[2] = {
5750     gen_helper_gvec_mul_idx_h,
5751     gen_helper_gvec_mul_idx_s,
5752 };
5753 TRANS(MUL_vi, do_int3_vector_idx, a, f_vector_idx_mul)
5754 
5755 static bool do_mla_vector_idx(DisasContext *s, arg_qrrx_e *a, bool sub)
5756 {
5757     static gen_helper_gvec_4 * const fns[2][2] = {
5758         { gen_helper_gvec_mla_idx_h, gen_helper_gvec_mls_idx_h },
5759         { gen_helper_gvec_mla_idx_s, gen_helper_gvec_mls_idx_s },
5760     };
5761 
5762     assert(a->esz == MO_16 || a->esz == MO_32);
5763     if (fp_access_check(s)) {
5764         gen_gvec_op4_ool(s, a->q, a->rd, a->rn, a->rm, a->rd,
5765                          a->idx, fns[a->esz - 1][sub]);
5766     }
5767     return true;
5768 }
5769 
5770 TRANS(MLA_vi, do_mla_vector_idx, a, false)
5771 TRANS(MLS_vi, do_mla_vector_idx, a, true)
5772 
5773 static bool do_int3_qc_vector_idx(DisasContext *s, arg_qrrx_e *a,
5774                                   gen_helper_gvec_4 * const fns[2])
5775 {
5776     assert(a->esz == MO_16 || a->esz == MO_32);
5777     if (fp_access_check(s)) {
5778         tcg_gen_gvec_4_ool(vec_full_reg_offset(s, a->rd),
5779                            vec_full_reg_offset(s, a->rn),
5780                            vec_full_reg_offset(s, a->rm),
5781                            offsetof(CPUARMState, vfp.qc),
5782                            a->q ? 16 : 8, vec_full_reg_size(s),
5783                            a->idx, fns[a->esz - 1]);
5784     }
5785     return true;
5786 }
5787 
5788 static gen_helper_gvec_4 * const f_vector_idx_sqdmulh[2] = {
5789     gen_helper_neon_sqdmulh_idx_h,
5790     gen_helper_neon_sqdmulh_idx_s,
5791 };
5792 TRANS(SQDMULH_vi, do_int3_qc_vector_idx, a, f_vector_idx_sqdmulh)
5793 
5794 static gen_helper_gvec_4 * const f_vector_idx_sqrdmulh[2] = {
5795     gen_helper_neon_sqrdmulh_idx_h,
5796     gen_helper_neon_sqrdmulh_idx_s,
5797 };
5798 TRANS(SQRDMULH_vi, do_int3_qc_vector_idx, a, f_vector_idx_sqrdmulh)
5799 
5800 /*
5801  * Advanced SIMD scalar pairwise
5802  */
5803 
5804 static bool do_fp3_scalar_pair(DisasContext *s, arg_rr_e *a, const FPScalar *f)
5805 {
5806     switch (a->esz) {
5807     case MO_64:
5808         if (fp_access_check(s)) {
5809             TCGv_i64 t0 = tcg_temp_new_i64();
5810             TCGv_i64 t1 = tcg_temp_new_i64();
5811 
5812             read_vec_element(s, t0, a->rn, 0, MO_64);
5813             read_vec_element(s, t1, a->rn, 1, MO_64);
5814             f->gen_d(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5815             write_fp_dreg(s, a->rd, t0);
5816         }
5817         break;
5818     case MO_32:
5819         if (fp_access_check(s)) {
5820             TCGv_i32 t0 = tcg_temp_new_i32();
5821             TCGv_i32 t1 = tcg_temp_new_i32();
5822 
5823             read_vec_element_i32(s, t0, a->rn, 0, MO_32);
5824             read_vec_element_i32(s, t1, a->rn, 1, MO_32);
5825             f->gen_s(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5826             write_fp_sreg(s, a->rd, t0);
5827         }
5828         break;
5829     case MO_16:
5830         if (!dc_isar_feature(aa64_fp16, s)) {
5831             return false;
5832         }
5833         if (fp_access_check(s)) {
5834             TCGv_i32 t0 = tcg_temp_new_i32();
5835             TCGv_i32 t1 = tcg_temp_new_i32();
5836 
5837             read_vec_element_i32(s, t0, a->rn, 0, MO_16);
5838             read_vec_element_i32(s, t1, a->rn, 1, MO_16);
5839             f->gen_h(t0, t0, t1, fpstatus_ptr(FPST_FPCR_F16));
5840             write_fp_sreg(s, a->rd, t0);
5841         }
5842         break;
5843     default:
5844         g_assert_not_reached();
5845     }
5846     return true;
5847 }
5848 
5849 TRANS(FADDP_s, do_fp3_scalar_pair, a, &f_scalar_fadd)
5850 TRANS(FMAXP_s, do_fp3_scalar_pair, a, &f_scalar_fmax)
5851 TRANS(FMINP_s, do_fp3_scalar_pair, a, &f_scalar_fmin)
5852 TRANS(FMAXNMP_s, do_fp3_scalar_pair, a, &f_scalar_fmaxnm)
5853 TRANS(FMINNMP_s, do_fp3_scalar_pair, a, &f_scalar_fminnm)
5854 
5855 static bool trans_ADDP_s(DisasContext *s, arg_rr_e *a)
5856 {
5857     if (fp_access_check(s)) {
5858         TCGv_i64 t0 = tcg_temp_new_i64();
5859         TCGv_i64 t1 = tcg_temp_new_i64();
5860 
5861         read_vec_element(s, t0, a->rn, 0, MO_64);
5862         read_vec_element(s, t1, a->rn, 1, MO_64);
5863         tcg_gen_add_i64(t0, t0, t1);
5864         write_fp_dreg(s, a->rd, t0);
5865     }
5866     return true;
5867 }
5868 
5869 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
5870  * Note that it is the caller's responsibility to ensure that the
5871  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
5872  * mandated semantics for out of range shifts.
5873  */
5874 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
5875                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
5876 {
5877     switch (shift_type) {
5878     case A64_SHIFT_TYPE_LSL:
5879         tcg_gen_shl_i64(dst, src, shift_amount);
5880         break;
5881     case A64_SHIFT_TYPE_LSR:
5882         tcg_gen_shr_i64(dst, src, shift_amount);
5883         break;
5884     case A64_SHIFT_TYPE_ASR:
5885         if (!sf) {
5886             tcg_gen_ext32s_i64(dst, src);
5887         }
5888         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
5889         break;
5890     case A64_SHIFT_TYPE_ROR:
5891         if (sf) {
5892             tcg_gen_rotr_i64(dst, src, shift_amount);
5893         } else {
5894             TCGv_i32 t0, t1;
5895             t0 = tcg_temp_new_i32();
5896             t1 = tcg_temp_new_i32();
5897             tcg_gen_extrl_i64_i32(t0, src);
5898             tcg_gen_extrl_i64_i32(t1, shift_amount);
5899             tcg_gen_rotr_i32(t0, t0, t1);
5900             tcg_gen_extu_i32_i64(dst, t0);
5901         }
5902         break;
5903     default:
5904         assert(FALSE); /* all shift types should be handled */
5905         break;
5906     }
5907 
5908     if (!sf) { /* zero extend final result */
5909         tcg_gen_ext32u_i64(dst, dst);
5910     }
5911 }
5912 
5913 /* Shift a TCGv src by immediate, put result in dst.
5914  * The shift amount must be in range (this should always be true as the
5915  * relevant instructions will UNDEF on bad shift immediates).
5916  */
5917 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
5918                           enum a64_shift_type shift_type, unsigned int shift_i)
5919 {
5920     assert(shift_i < (sf ? 64 : 32));
5921 
5922     if (shift_i == 0) {
5923         tcg_gen_mov_i64(dst, src);
5924     } else {
5925         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
5926     }
5927 }
5928 
5929 /* Logical (shifted register)
5930  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
5931  * +----+-----+-----------+-------+---+------+--------+------+------+
5932  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
5933  * +----+-----+-----------+-------+---+------+--------+------+------+
5934  */
5935 static void disas_logic_reg(DisasContext *s, uint32_t insn)
5936 {
5937     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
5938     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
5939 
5940     sf = extract32(insn, 31, 1);
5941     opc = extract32(insn, 29, 2);
5942     shift_type = extract32(insn, 22, 2);
5943     invert = extract32(insn, 21, 1);
5944     rm = extract32(insn, 16, 5);
5945     shift_amount = extract32(insn, 10, 6);
5946     rn = extract32(insn, 5, 5);
5947     rd = extract32(insn, 0, 5);
5948 
5949     if (!sf && (shift_amount & (1 << 5))) {
5950         unallocated_encoding(s);
5951         return;
5952     }
5953 
5954     tcg_rd = cpu_reg(s, rd);
5955 
5956     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
5957         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
5958          * register-register MOV and MVN, so it is worth special casing.
5959          */
5960         tcg_rm = cpu_reg(s, rm);
5961         if (invert) {
5962             tcg_gen_not_i64(tcg_rd, tcg_rm);
5963             if (!sf) {
5964                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5965             }
5966         } else {
5967             if (sf) {
5968                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
5969             } else {
5970                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
5971             }
5972         }
5973         return;
5974     }
5975 
5976     tcg_rm = read_cpu_reg(s, rm, sf);
5977 
5978     if (shift_amount) {
5979         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
5980     }
5981 
5982     tcg_rn = cpu_reg(s, rn);
5983 
5984     switch (opc | (invert << 2)) {
5985     case 0: /* AND */
5986     case 3: /* ANDS */
5987         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
5988         break;
5989     case 1: /* ORR */
5990         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
5991         break;
5992     case 2: /* EOR */
5993         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
5994         break;
5995     case 4: /* BIC */
5996     case 7: /* BICS */
5997         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
5998         break;
5999     case 5: /* ORN */
6000         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
6001         break;
6002     case 6: /* EON */
6003         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
6004         break;
6005     default:
6006         assert(FALSE);
6007         break;
6008     }
6009 
6010     if (!sf) {
6011         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6012     }
6013 
6014     if (opc == 3) {
6015         gen_logic_CC(sf, tcg_rd);
6016     }
6017 }
6018 
6019 /*
6020  * Add/subtract (extended register)
6021  *
6022  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
6023  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
6024  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
6025  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
6026  *
6027  *  sf: 0 -> 32bit, 1 -> 64bit
6028  *  op: 0 -> add  , 1 -> sub
6029  *   S: 1 -> set flags
6030  * opt: 00
6031  * option: extension type (see DecodeRegExtend)
6032  * imm3: optional shift to Rm
6033  *
6034  * Rd = Rn + LSL(extend(Rm), amount)
6035  */
6036 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
6037 {
6038     int rd = extract32(insn, 0, 5);
6039     int rn = extract32(insn, 5, 5);
6040     int imm3 = extract32(insn, 10, 3);
6041     int option = extract32(insn, 13, 3);
6042     int rm = extract32(insn, 16, 5);
6043     int opt = extract32(insn, 22, 2);
6044     bool setflags = extract32(insn, 29, 1);
6045     bool sub_op = extract32(insn, 30, 1);
6046     bool sf = extract32(insn, 31, 1);
6047 
6048     TCGv_i64 tcg_rm, tcg_rn; /* temps */
6049     TCGv_i64 tcg_rd;
6050     TCGv_i64 tcg_result;
6051 
6052     if (imm3 > 4 || opt != 0) {
6053         unallocated_encoding(s);
6054         return;
6055     }
6056 
6057     /* non-flag setting ops may use SP */
6058     if (!setflags) {
6059         tcg_rd = cpu_reg_sp(s, rd);
6060     } else {
6061         tcg_rd = cpu_reg(s, rd);
6062     }
6063     tcg_rn = read_cpu_reg_sp(s, rn, sf);
6064 
6065     tcg_rm = read_cpu_reg(s, rm, sf);
6066     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
6067 
6068     tcg_result = tcg_temp_new_i64();
6069 
6070     if (!setflags) {
6071         if (sub_op) {
6072             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
6073         } else {
6074             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
6075         }
6076     } else {
6077         if (sub_op) {
6078             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
6079         } else {
6080             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
6081         }
6082     }
6083 
6084     if (sf) {
6085         tcg_gen_mov_i64(tcg_rd, tcg_result);
6086     } else {
6087         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
6088     }
6089 }
6090 
6091 /*
6092  * Add/subtract (shifted register)
6093  *
6094  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
6095  * +--+--+--+-----------+-----+--+-------+---------+------+------+
6096  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
6097  * +--+--+--+-----------+-----+--+-------+---------+------+------+
6098  *
6099  *    sf: 0 -> 32bit, 1 -> 64bit
6100  *    op: 0 -> add  , 1 -> sub
6101  *     S: 1 -> set flags
6102  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
6103  *  imm6: Shift amount to apply to Rm before the add/sub
6104  */
6105 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
6106 {
6107     int rd = extract32(insn, 0, 5);
6108     int rn = extract32(insn, 5, 5);
6109     int imm6 = extract32(insn, 10, 6);
6110     int rm = extract32(insn, 16, 5);
6111     int shift_type = extract32(insn, 22, 2);
6112     bool setflags = extract32(insn, 29, 1);
6113     bool sub_op = extract32(insn, 30, 1);
6114     bool sf = extract32(insn, 31, 1);
6115 
6116     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6117     TCGv_i64 tcg_rn, tcg_rm;
6118     TCGv_i64 tcg_result;
6119 
6120     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
6121         unallocated_encoding(s);
6122         return;
6123     }
6124 
6125     tcg_rn = read_cpu_reg(s, rn, sf);
6126     tcg_rm = read_cpu_reg(s, rm, sf);
6127 
6128     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
6129 
6130     tcg_result = tcg_temp_new_i64();
6131 
6132     if (!setflags) {
6133         if (sub_op) {
6134             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
6135         } else {
6136             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
6137         }
6138     } else {
6139         if (sub_op) {
6140             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
6141         } else {
6142             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
6143         }
6144     }
6145 
6146     if (sf) {
6147         tcg_gen_mov_i64(tcg_rd, tcg_result);
6148     } else {
6149         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
6150     }
6151 }
6152 
6153 /* Data-processing (3 source)
6154  *
6155  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
6156  *  +--+------+-----------+------+------+----+------+------+------+
6157  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6158  *  +--+------+-----------+------+------+----+------+------+------+
6159  */
6160 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
6161 {
6162     int rd = extract32(insn, 0, 5);
6163     int rn = extract32(insn, 5, 5);
6164     int ra = extract32(insn, 10, 5);
6165     int rm = extract32(insn, 16, 5);
6166     int op_id = (extract32(insn, 29, 3) << 4) |
6167         (extract32(insn, 21, 3) << 1) |
6168         extract32(insn, 15, 1);
6169     bool sf = extract32(insn, 31, 1);
6170     bool is_sub = extract32(op_id, 0, 1);
6171     bool is_high = extract32(op_id, 2, 1);
6172     bool is_signed = false;
6173     TCGv_i64 tcg_op1;
6174     TCGv_i64 tcg_op2;
6175     TCGv_i64 tcg_tmp;
6176 
6177     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
6178     switch (op_id) {
6179     case 0x42: /* SMADDL */
6180     case 0x43: /* SMSUBL */
6181     case 0x44: /* SMULH */
6182         is_signed = true;
6183         break;
6184     case 0x0: /* MADD (32bit) */
6185     case 0x1: /* MSUB (32bit) */
6186     case 0x40: /* MADD (64bit) */
6187     case 0x41: /* MSUB (64bit) */
6188     case 0x4a: /* UMADDL */
6189     case 0x4b: /* UMSUBL */
6190     case 0x4c: /* UMULH */
6191         break;
6192     default:
6193         unallocated_encoding(s);
6194         return;
6195     }
6196 
6197     if (is_high) {
6198         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
6199         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6200         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6201         TCGv_i64 tcg_rm = cpu_reg(s, rm);
6202 
6203         if (is_signed) {
6204             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
6205         } else {
6206             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
6207         }
6208         return;
6209     }
6210 
6211     tcg_op1 = tcg_temp_new_i64();
6212     tcg_op2 = tcg_temp_new_i64();
6213     tcg_tmp = tcg_temp_new_i64();
6214 
6215     if (op_id < 0x42) {
6216         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
6217         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
6218     } else {
6219         if (is_signed) {
6220             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
6221             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
6222         } else {
6223             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
6224             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
6225         }
6226     }
6227 
6228     if (ra == 31 && !is_sub) {
6229         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
6230         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
6231     } else {
6232         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
6233         if (is_sub) {
6234             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
6235         } else {
6236             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
6237         }
6238     }
6239 
6240     if (!sf) {
6241         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
6242     }
6243 }
6244 
6245 /* Add/subtract (with carry)
6246  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
6247  * +--+--+--+------------------------+------+-------------+------+-----+
6248  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
6249  * +--+--+--+------------------------+------+-------------+------+-----+
6250  */
6251 
6252 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
6253 {
6254     unsigned int sf, op, setflags, rm, rn, rd;
6255     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
6256 
6257     sf = extract32(insn, 31, 1);
6258     op = extract32(insn, 30, 1);
6259     setflags = extract32(insn, 29, 1);
6260     rm = extract32(insn, 16, 5);
6261     rn = extract32(insn, 5, 5);
6262     rd = extract32(insn, 0, 5);
6263 
6264     tcg_rd = cpu_reg(s, rd);
6265     tcg_rn = cpu_reg(s, rn);
6266 
6267     if (op) {
6268         tcg_y = tcg_temp_new_i64();
6269         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
6270     } else {
6271         tcg_y = cpu_reg(s, rm);
6272     }
6273 
6274     if (setflags) {
6275         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
6276     } else {
6277         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
6278     }
6279 }
6280 
6281 /*
6282  * Rotate right into flags
6283  *  31 30 29                21       15          10      5  4      0
6284  * +--+--+--+-----------------+--------+-----------+------+--+------+
6285  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
6286  * +--+--+--+-----------------+--------+-----------+------+--+------+
6287  */
6288 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
6289 {
6290     int mask = extract32(insn, 0, 4);
6291     int o2 = extract32(insn, 4, 1);
6292     int rn = extract32(insn, 5, 5);
6293     int imm6 = extract32(insn, 15, 6);
6294     int sf_op_s = extract32(insn, 29, 3);
6295     TCGv_i64 tcg_rn;
6296     TCGv_i32 nzcv;
6297 
6298     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
6299         unallocated_encoding(s);
6300         return;
6301     }
6302 
6303     tcg_rn = read_cpu_reg(s, rn, 1);
6304     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
6305 
6306     nzcv = tcg_temp_new_i32();
6307     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
6308 
6309     if (mask & 8) { /* N */
6310         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
6311     }
6312     if (mask & 4) { /* Z */
6313         tcg_gen_not_i32(cpu_ZF, nzcv);
6314         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
6315     }
6316     if (mask & 2) { /* C */
6317         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
6318     }
6319     if (mask & 1) { /* V */
6320         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
6321     }
6322 }
6323 
6324 /*
6325  * Evaluate into flags
6326  *  31 30 29                21        15   14        10      5  4      0
6327  * +--+--+--+-----------------+---------+----+---------+------+--+------+
6328  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
6329  * +--+--+--+-----------------+---------+----+---------+------+--+------+
6330  */
6331 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
6332 {
6333     int o3_mask = extract32(insn, 0, 5);
6334     int rn = extract32(insn, 5, 5);
6335     int o2 = extract32(insn, 15, 6);
6336     int sz = extract32(insn, 14, 1);
6337     int sf_op_s = extract32(insn, 29, 3);
6338     TCGv_i32 tmp;
6339     int shift;
6340 
6341     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
6342         !dc_isar_feature(aa64_condm_4, s)) {
6343         unallocated_encoding(s);
6344         return;
6345     }
6346     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
6347 
6348     tmp = tcg_temp_new_i32();
6349     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
6350     tcg_gen_shli_i32(cpu_NF, tmp, shift);
6351     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
6352     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
6353     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
6354 }
6355 
6356 /* Conditional compare (immediate / register)
6357  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
6358  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
6359  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
6360  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
6361  *        [1]                             y                [0]       [0]
6362  */
6363 static void disas_cc(DisasContext *s, uint32_t insn)
6364 {
6365     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
6366     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
6367     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
6368     DisasCompare c;
6369 
6370     if (!extract32(insn, 29, 1)) {
6371         unallocated_encoding(s);
6372         return;
6373     }
6374     if (insn & (1 << 10 | 1 << 4)) {
6375         unallocated_encoding(s);
6376         return;
6377     }
6378     sf = extract32(insn, 31, 1);
6379     op = extract32(insn, 30, 1);
6380     is_imm = extract32(insn, 11, 1);
6381     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
6382     cond = extract32(insn, 12, 4);
6383     rn = extract32(insn, 5, 5);
6384     nzcv = extract32(insn, 0, 4);
6385 
6386     /* Set T0 = !COND.  */
6387     tcg_t0 = tcg_temp_new_i32();
6388     arm_test_cc(&c, cond);
6389     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
6390 
6391     /* Load the arguments for the new comparison.  */
6392     if (is_imm) {
6393         tcg_y = tcg_temp_new_i64();
6394         tcg_gen_movi_i64(tcg_y, y);
6395     } else {
6396         tcg_y = cpu_reg(s, y);
6397     }
6398     tcg_rn = cpu_reg(s, rn);
6399 
6400     /* Set the flags for the new comparison.  */
6401     tcg_tmp = tcg_temp_new_i64();
6402     if (op) {
6403         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
6404     } else {
6405         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
6406     }
6407 
6408     /* If COND was false, force the flags to #nzcv.  Compute two masks
6409      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
6410      * For tcg hosts that support ANDC, we can make do with just T1.
6411      * In either case, allow the tcg optimizer to delete any unused mask.
6412      */
6413     tcg_t1 = tcg_temp_new_i32();
6414     tcg_t2 = tcg_temp_new_i32();
6415     tcg_gen_neg_i32(tcg_t1, tcg_t0);
6416     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
6417 
6418     if (nzcv & 8) { /* N */
6419         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
6420     } else {
6421         if (TCG_TARGET_HAS_andc_i32) {
6422             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
6423         } else {
6424             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
6425         }
6426     }
6427     if (nzcv & 4) { /* Z */
6428         if (TCG_TARGET_HAS_andc_i32) {
6429             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
6430         } else {
6431             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
6432         }
6433     } else {
6434         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
6435     }
6436     if (nzcv & 2) { /* C */
6437         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
6438     } else {
6439         if (TCG_TARGET_HAS_andc_i32) {
6440             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
6441         } else {
6442             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
6443         }
6444     }
6445     if (nzcv & 1) { /* V */
6446         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
6447     } else {
6448         if (TCG_TARGET_HAS_andc_i32) {
6449             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
6450         } else {
6451             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
6452         }
6453     }
6454 }
6455 
6456 /* Conditional select
6457  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
6458  * +----+----+---+-----------------+------+------+-----+------+------+
6459  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
6460  * +----+----+---+-----------------+------+------+-----+------+------+
6461  */
6462 static void disas_cond_select(DisasContext *s, uint32_t insn)
6463 {
6464     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
6465     TCGv_i64 tcg_rd, zero;
6466     DisasCompare64 c;
6467 
6468     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
6469         /* S == 1 or op2<1> == 1 */
6470         unallocated_encoding(s);
6471         return;
6472     }
6473     sf = extract32(insn, 31, 1);
6474     else_inv = extract32(insn, 30, 1);
6475     rm = extract32(insn, 16, 5);
6476     cond = extract32(insn, 12, 4);
6477     else_inc = extract32(insn, 10, 1);
6478     rn = extract32(insn, 5, 5);
6479     rd = extract32(insn, 0, 5);
6480 
6481     tcg_rd = cpu_reg(s, rd);
6482 
6483     a64_test_cc(&c, cond);
6484     zero = tcg_constant_i64(0);
6485 
6486     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
6487         /* CSET & CSETM.  */
6488         if (else_inv) {
6489             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
6490                                    tcg_rd, c.value, zero);
6491         } else {
6492             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
6493                                 tcg_rd, c.value, zero);
6494         }
6495     } else {
6496         TCGv_i64 t_true = cpu_reg(s, rn);
6497         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
6498         if (else_inv && else_inc) {
6499             tcg_gen_neg_i64(t_false, t_false);
6500         } else if (else_inv) {
6501             tcg_gen_not_i64(t_false, t_false);
6502         } else if (else_inc) {
6503             tcg_gen_addi_i64(t_false, t_false, 1);
6504         }
6505         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
6506     }
6507 
6508     if (!sf) {
6509         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6510     }
6511 }
6512 
6513 static void handle_clz(DisasContext *s, unsigned int sf,
6514                        unsigned int rn, unsigned int rd)
6515 {
6516     TCGv_i64 tcg_rd, tcg_rn;
6517     tcg_rd = cpu_reg(s, rd);
6518     tcg_rn = cpu_reg(s, rn);
6519 
6520     if (sf) {
6521         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
6522     } else {
6523         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6524         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6525         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
6526         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6527     }
6528 }
6529 
6530 static void handle_cls(DisasContext *s, unsigned int sf,
6531                        unsigned int rn, unsigned int rd)
6532 {
6533     TCGv_i64 tcg_rd, tcg_rn;
6534     tcg_rd = cpu_reg(s, rd);
6535     tcg_rn = cpu_reg(s, rn);
6536 
6537     if (sf) {
6538         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
6539     } else {
6540         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6541         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6542         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
6543         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6544     }
6545 }
6546 
6547 static void handle_rbit(DisasContext *s, unsigned int sf,
6548                         unsigned int rn, unsigned int rd)
6549 {
6550     TCGv_i64 tcg_rd, tcg_rn;
6551     tcg_rd = cpu_reg(s, rd);
6552     tcg_rn = cpu_reg(s, rn);
6553 
6554     if (sf) {
6555         gen_helper_rbit64(tcg_rd, tcg_rn);
6556     } else {
6557         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6558         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6559         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
6560         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6561     }
6562 }
6563 
6564 /* REV with sf==1, opcode==3 ("REV64") */
6565 static void handle_rev64(DisasContext *s, unsigned int sf,
6566                          unsigned int rn, unsigned int rd)
6567 {
6568     if (!sf) {
6569         unallocated_encoding(s);
6570         return;
6571     }
6572     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
6573 }
6574 
6575 /* REV with sf==0, opcode==2
6576  * REV32 (sf==1, opcode==2)
6577  */
6578 static void handle_rev32(DisasContext *s, unsigned int sf,
6579                          unsigned int rn, unsigned int rd)
6580 {
6581     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6582     TCGv_i64 tcg_rn = cpu_reg(s, rn);
6583 
6584     if (sf) {
6585         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
6586         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
6587     } else {
6588         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
6589     }
6590 }
6591 
6592 /* REV16 (opcode==1) */
6593 static void handle_rev16(DisasContext *s, unsigned int sf,
6594                          unsigned int rn, unsigned int rd)
6595 {
6596     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6597     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6598     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
6599     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
6600 
6601     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
6602     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
6603     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
6604     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
6605     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
6606 }
6607 
6608 /* Data-processing (1 source)
6609  *   31  30  29  28             21 20     16 15    10 9    5 4    0
6610  * +----+---+---+-----------------+---------+--------+------+------+
6611  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
6612  * +----+---+---+-----------------+---------+--------+------+------+
6613  */
6614 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
6615 {
6616     unsigned int sf, opcode, opcode2, rn, rd;
6617     TCGv_i64 tcg_rd;
6618 
6619     if (extract32(insn, 29, 1)) {
6620         unallocated_encoding(s);
6621         return;
6622     }
6623 
6624     sf = extract32(insn, 31, 1);
6625     opcode = extract32(insn, 10, 6);
6626     opcode2 = extract32(insn, 16, 5);
6627     rn = extract32(insn, 5, 5);
6628     rd = extract32(insn, 0, 5);
6629 
6630 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
6631 
6632     switch (MAP(sf, opcode2, opcode)) {
6633     case MAP(0, 0x00, 0x00): /* RBIT */
6634     case MAP(1, 0x00, 0x00):
6635         handle_rbit(s, sf, rn, rd);
6636         break;
6637     case MAP(0, 0x00, 0x01): /* REV16 */
6638     case MAP(1, 0x00, 0x01):
6639         handle_rev16(s, sf, rn, rd);
6640         break;
6641     case MAP(0, 0x00, 0x02): /* REV/REV32 */
6642     case MAP(1, 0x00, 0x02):
6643         handle_rev32(s, sf, rn, rd);
6644         break;
6645     case MAP(1, 0x00, 0x03): /* REV64 */
6646         handle_rev64(s, sf, rn, rd);
6647         break;
6648     case MAP(0, 0x00, 0x04): /* CLZ */
6649     case MAP(1, 0x00, 0x04):
6650         handle_clz(s, sf, rn, rd);
6651         break;
6652     case MAP(0, 0x00, 0x05): /* CLS */
6653     case MAP(1, 0x00, 0x05):
6654         handle_cls(s, sf, rn, rd);
6655         break;
6656     case MAP(1, 0x01, 0x00): /* PACIA */
6657         if (s->pauth_active) {
6658             tcg_rd = cpu_reg(s, rd);
6659             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6660         } else if (!dc_isar_feature(aa64_pauth, s)) {
6661             goto do_unallocated;
6662         }
6663         break;
6664     case MAP(1, 0x01, 0x01): /* PACIB */
6665         if (s->pauth_active) {
6666             tcg_rd = cpu_reg(s, rd);
6667             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6668         } else if (!dc_isar_feature(aa64_pauth, s)) {
6669             goto do_unallocated;
6670         }
6671         break;
6672     case MAP(1, 0x01, 0x02): /* PACDA */
6673         if (s->pauth_active) {
6674             tcg_rd = cpu_reg(s, rd);
6675             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6676         } else if (!dc_isar_feature(aa64_pauth, s)) {
6677             goto do_unallocated;
6678         }
6679         break;
6680     case MAP(1, 0x01, 0x03): /* PACDB */
6681         if (s->pauth_active) {
6682             tcg_rd = cpu_reg(s, rd);
6683             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6684         } else if (!dc_isar_feature(aa64_pauth, s)) {
6685             goto do_unallocated;
6686         }
6687         break;
6688     case MAP(1, 0x01, 0x04): /* AUTIA */
6689         if (s->pauth_active) {
6690             tcg_rd = cpu_reg(s, rd);
6691             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6692         } else if (!dc_isar_feature(aa64_pauth, s)) {
6693             goto do_unallocated;
6694         }
6695         break;
6696     case MAP(1, 0x01, 0x05): /* AUTIB */
6697         if (s->pauth_active) {
6698             tcg_rd = cpu_reg(s, rd);
6699             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6700         } else if (!dc_isar_feature(aa64_pauth, s)) {
6701             goto do_unallocated;
6702         }
6703         break;
6704     case MAP(1, 0x01, 0x06): /* AUTDA */
6705         if (s->pauth_active) {
6706             tcg_rd = cpu_reg(s, rd);
6707             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6708         } else if (!dc_isar_feature(aa64_pauth, s)) {
6709             goto do_unallocated;
6710         }
6711         break;
6712     case MAP(1, 0x01, 0x07): /* AUTDB */
6713         if (s->pauth_active) {
6714             tcg_rd = cpu_reg(s, rd);
6715             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6716         } else if (!dc_isar_feature(aa64_pauth, s)) {
6717             goto do_unallocated;
6718         }
6719         break;
6720     case MAP(1, 0x01, 0x08): /* PACIZA */
6721         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6722             goto do_unallocated;
6723         } else if (s->pauth_active) {
6724             tcg_rd = cpu_reg(s, rd);
6725             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6726         }
6727         break;
6728     case MAP(1, 0x01, 0x09): /* PACIZB */
6729         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6730             goto do_unallocated;
6731         } else if (s->pauth_active) {
6732             tcg_rd = cpu_reg(s, rd);
6733             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6734         }
6735         break;
6736     case MAP(1, 0x01, 0x0a): /* PACDZA */
6737         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6738             goto do_unallocated;
6739         } else if (s->pauth_active) {
6740             tcg_rd = cpu_reg(s, rd);
6741             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6742         }
6743         break;
6744     case MAP(1, 0x01, 0x0b): /* PACDZB */
6745         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6746             goto do_unallocated;
6747         } else if (s->pauth_active) {
6748             tcg_rd = cpu_reg(s, rd);
6749             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6750         }
6751         break;
6752     case MAP(1, 0x01, 0x0c): /* AUTIZA */
6753         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6754             goto do_unallocated;
6755         } else if (s->pauth_active) {
6756             tcg_rd = cpu_reg(s, rd);
6757             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6758         }
6759         break;
6760     case MAP(1, 0x01, 0x0d): /* AUTIZB */
6761         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6762             goto do_unallocated;
6763         } else if (s->pauth_active) {
6764             tcg_rd = cpu_reg(s, rd);
6765             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6766         }
6767         break;
6768     case MAP(1, 0x01, 0x0e): /* AUTDZA */
6769         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6770             goto do_unallocated;
6771         } else if (s->pauth_active) {
6772             tcg_rd = cpu_reg(s, rd);
6773             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6774         }
6775         break;
6776     case MAP(1, 0x01, 0x0f): /* AUTDZB */
6777         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6778             goto do_unallocated;
6779         } else if (s->pauth_active) {
6780             tcg_rd = cpu_reg(s, rd);
6781             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6782         }
6783         break;
6784     case MAP(1, 0x01, 0x10): /* XPACI */
6785         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6786             goto do_unallocated;
6787         } else if (s->pauth_active) {
6788             tcg_rd = cpu_reg(s, rd);
6789             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
6790         }
6791         break;
6792     case MAP(1, 0x01, 0x11): /* XPACD */
6793         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6794             goto do_unallocated;
6795         } else if (s->pauth_active) {
6796             tcg_rd = cpu_reg(s, rd);
6797             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
6798         }
6799         break;
6800     default:
6801     do_unallocated:
6802         unallocated_encoding(s);
6803         break;
6804     }
6805 
6806 #undef MAP
6807 }
6808 
6809 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
6810                        unsigned int rm, unsigned int rn, unsigned int rd)
6811 {
6812     TCGv_i64 tcg_n, tcg_m, tcg_rd;
6813     tcg_rd = cpu_reg(s, rd);
6814 
6815     if (!sf && is_signed) {
6816         tcg_n = tcg_temp_new_i64();
6817         tcg_m = tcg_temp_new_i64();
6818         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
6819         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
6820     } else {
6821         tcg_n = read_cpu_reg(s, rn, sf);
6822         tcg_m = read_cpu_reg(s, rm, sf);
6823     }
6824 
6825     if (is_signed) {
6826         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
6827     } else {
6828         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
6829     }
6830 
6831     if (!sf) { /* zero extend final result */
6832         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6833     }
6834 }
6835 
6836 /* LSLV, LSRV, ASRV, RORV */
6837 static void handle_shift_reg(DisasContext *s,
6838                              enum a64_shift_type shift_type, unsigned int sf,
6839                              unsigned int rm, unsigned int rn, unsigned int rd)
6840 {
6841     TCGv_i64 tcg_shift = tcg_temp_new_i64();
6842     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6843     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
6844 
6845     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
6846     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
6847 }
6848 
6849 /* CRC32[BHWX], CRC32C[BHWX] */
6850 static void handle_crc32(DisasContext *s,
6851                          unsigned int sf, unsigned int sz, bool crc32c,
6852                          unsigned int rm, unsigned int rn, unsigned int rd)
6853 {
6854     TCGv_i64 tcg_acc, tcg_val;
6855     TCGv_i32 tcg_bytes;
6856 
6857     if (!dc_isar_feature(aa64_crc32, s)
6858         || (sf == 1 && sz != 3)
6859         || (sf == 0 && sz == 3)) {
6860         unallocated_encoding(s);
6861         return;
6862     }
6863 
6864     if (sz == 3) {
6865         tcg_val = cpu_reg(s, rm);
6866     } else {
6867         uint64_t mask;
6868         switch (sz) {
6869         case 0:
6870             mask = 0xFF;
6871             break;
6872         case 1:
6873             mask = 0xFFFF;
6874             break;
6875         case 2:
6876             mask = 0xFFFFFFFF;
6877             break;
6878         default:
6879             g_assert_not_reached();
6880         }
6881         tcg_val = tcg_temp_new_i64();
6882         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
6883     }
6884 
6885     tcg_acc = cpu_reg(s, rn);
6886     tcg_bytes = tcg_constant_i32(1 << sz);
6887 
6888     if (crc32c) {
6889         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
6890     } else {
6891         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
6892     }
6893 }
6894 
6895 /* Data-processing (2 source)
6896  *   31   30  29 28             21 20  16 15    10 9    5 4    0
6897  * +----+---+---+-----------------+------+--------+------+------+
6898  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
6899  * +----+---+---+-----------------+------+--------+------+------+
6900  */
6901 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
6902 {
6903     unsigned int sf, rm, opcode, rn, rd, setflag;
6904     sf = extract32(insn, 31, 1);
6905     setflag = extract32(insn, 29, 1);
6906     rm = extract32(insn, 16, 5);
6907     opcode = extract32(insn, 10, 6);
6908     rn = extract32(insn, 5, 5);
6909     rd = extract32(insn, 0, 5);
6910 
6911     if (setflag && opcode != 0) {
6912         unallocated_encoding(s);
6913         return;
6914     }
6915 
6916     switch (opcode) {
6917     case 0: /* SUBP(S) */
6918         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
6919             goto do_unallocated;
6920         } else {
6921             TCGv_i64 tcg_n, tcg_m, tcg_d;
6922 
6923             tcg_n = read_cpu_reg_sp(s, rn, true);
6924             tcg_m = read_cpu_reg_sp(s, rm, true);
6925             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
6926             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
6927             tcg_d = cpu_reg(s, rd);
6928 
6929             if (setflag) {
6930                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
6931             } else {
6932                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
6933             }
6934         }
6935         break;
6936     case 2: /* UDIV */
6937         handle_div(s, false, sf, rm, rn, rd);
6938         break;
6939     case 3: /* SDIV */
6940         handle_div(s, true, sf, rm, rn, rd);
6941         break;
6942     case 4: /* IRG */
6943         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
6944             goto do_unallocated;
6945         }
6946         if (s->ata[0]) {
6947             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
6948                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
6949         } else {
6950             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
6951                                              cpu_reg_sp(s, rn));
6952         }
6953         break;
6954     case 5: /* GMI */
6955         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
6956             goto do_unallocated;
6957         } else {
6958             TCGv_i64 t = tcg_temp_new_i64();
6959 
6960             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
6961             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
6962             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
6963         }
6964         break;
6965     case 8: /* LSLV */
6966         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
6967         break;
6968     case 9: /* LSRV */
6969         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
6970         break;
6971     case 10: /* ASRV */
6972         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
6973         break;
6974     case 11: /* RORV */
6975         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
6976         break;
6977     case 12: /* PACGA */
6978         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
6979             goto do_unallocated;
6980         }
6981         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
6982                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
6983         break;
6984     case 16:
6985     case 17:
6986     case 18:
6987     case 19:
6988     case 20:
6989     case 21:
6990     case 22:
6991     case 23: /* CRC32 */
6992     {
6993         int sz = extract32(opcode, 0, 2);
6994         bool crc32c = extract32(opcode, 2, 1);
6995         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
6996         break;
6997     }
6998     default:
6999     do_unallocated:
7000         unallocated_encoding(s);
7001         break;
7002     }
7003 }
7004 
7005 /*
7006  * Data processing - register
7007  *  31  30 29  28      25    21  20  16      10         0
7008  * +--+---+--+---+-------+-----+-------+-------+---------+
7009  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
7010  * +--+---+--+---+-------+-----+-------+-------+---------+
7011  */
7012 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
7013 {
7014     int op0 = extract32(insn, 30, 1);
7015     int op1 = extract32(insn, 28, 1);
7016     int op2 = extract32(insn, 21, 4);
7017     int op3 = extract32(insn, 10, 6);
7018 
7019     if (!op1) {
7020         if (op2 & 8) {
7021             if (op2 & 1) {
7022                 /* Add/sub (extended register) */
7023                 disas_add_sub_ext_reg(s, insn);
7024             } else {
7025                 /* Add/sub (shifted register) */
7026                 disas_add_sub_reg(s, insn);
7027             }
7028         } else {
7029             /* Logical (shifted register) */
7030             disas_logic_reg(s, insn);
7031         }
7032         return;
7033     }
7034 
7035     switch (op2) {
7036     case 0x0:
7037         switch (op3) {
7038         case 0x00: /* Add/subtract (with carry) */
7039             disas_adc_sbc(s, insn);
7040             break;
7041 
7042         case 0x01: /* Rotate right into flags */
7043         case 0x21:
7044             disas_rotate_right_into_flags(s, insn);
7045             break;
7046 
7047         case 0x02: /* Evaluate into flags */
7048         case 0x12:
7049         case 0x22:
7050         case 0x32:
7051             disas_evaluate_into_flags(s, insn);
7052             break;
7053 
7054         default:
7055             goto do_unallocated;
7056         }
7057         break;
7058 
7059     case 0x2: /* Conditional compare */
7060         disas_cc(s, insn); /* both imm and reg forms */
7061         break;
7062 
7063     case 0x4: /* Conditional select */
7064         disas_cond_select(s, insn);
7065         break;
7066 
7067     case 0x6: /* Data-processing */
7068         if (op0) {    /* (1 source) */
7069             disas_data_proc_1src(s, insn);
7070         } else {      /* (2 source) */
7071             disas_data_proc_2src(s, insn);
7072         }
7073         break;
7074     case 0x8 ... 0xf: /* (3 source) */
7075         disas_data_proc_3src(s, insn);
7076         break;
7077 
7078     default:
7079     do_unallocated:
7080         unallocated_encoding(s);
7081         break;
7082     }
7083 }
7084 
7085 static void handle_fp_compare(DisasContext *s, int size,
7086                               unsigned int rn, unsigned int rm,
7087                               bool cmp_with_zero, bool signal_all_nans)
7088 {
7089     TCGv_i64 tcg_flags = tcg_temp_new_i64();
7090     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7091 
7092     if (size == MO_64) {
7093         TCGv_i64 tcg_vn, tcg_vm;
7094 
7095         tcg_vn = read_fp_dreg(s, rn);
7096         if (cmp_with_zero) {
7097             tcg_vm = tcg_constant_i64(0);
7098         } else {
7099             tcg_vm = read_fp_dreg(s, rm);
7100         }
7101         if (signal_all_nans) {
7102             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7103         } else {
7104             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7105         }
7106     } else {
7107         TCGv_i32 tcg_vn = tcg_temp_new_i32();
7108         TCGv_i32 tcg_vm = tcg_temp_new_i32();
7109 
7110         read_vec_element_i32(s, tcg_vn, rn, 0, size);
7111         if (cmp_with_zero) {
7112             tcg_gen_movi_i32(tcg_vm, 0);
7113         } else {
7114             read_vec_element_i32(s, tcg_vm, rm, 0, size);
7115         }
7116 
7117         switch (size) {
7118         case MO_32:
7119             if (signal_all_nans) {
7120                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7121             } else {
7122                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7123             }
7124             break;
7125         case MO_16:
7126             if (signal_all_nans) {
7127                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7128             } else {
7129                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7130             }
7131             break;
7132         default:
7133             g_assert_not_reached();
7134         }
7135     }
7136 
7137     gen_set_nzcv(tcg_flags);
7138 }
7139 
7140 /* Floating point compare
7141  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
7142  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
7143  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
7144  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
7145  */
7146 static void disas_fp_compare(DisasContext *s, uint32_t insn)
7147 {
7148     unsigned int mos, type, rm, op, rn, opc, op2r;
7149     int size;
7150 
7151     mos = extract32(insn, 29, 3);
7152     type = extract32(insn, 22, 2);
7153     rm = extract32(insn, 16, 5);
7154     op = extract32(insn, 14, 2);
7155     rn = extract32(insn, 5, 5);
7156     opc = extract32(insn, 3, 2);
7157     op2r = extract32(insn, 0, 3);
7158 
7159     if (mos || op || op2r) {
7160         unallocated_encoding(s);
7161         return;
7162     }
7163 
7164     switch (type) {
7165     case 0:
7166         size = MO_32;
7167         break;
7168     case 1:
7169         size = MO_64;
7170         break;
7171     case 3:
7172         size = MO_16;
7173         if (dc_isar_feature(aa64_fp16, s)) {
7174             break;
7175         }
7176         /* fallthru */
7177     default:
7178         unallocated_encoding(s);
7179         return;
7180     }
7181 
7182     if (!fp_access_check(s)) {
7183         return;
7184     }
7185 
7186     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
7187 }
7188 
7189 /* Floating point conditional compare
7190  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
7191  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
7192  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
7193  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
7194  */
7195 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
7196 {
7197     unsigned int mos, type, rm, cond, rn, op, nzcv;
7198     TCGLabel *label_continue = NULL;
7199     int size;
7200 
7201     mos = extract32(insn, 29, 3);
7202     type = extract32(insn, 22, 2);
7203     rm = extract32(insn, 16, 5);
7204     cond = extract32(insn, 12, 4);
7205     rn = extract32(insn, 5, 5);
7206     op = extract32(insn, 4, 1);
7207     nzcv = extract32(insn, 0, 4);
7208 
7209     if (mos) {
7210         unallocated_encoding(s);
7211         return;
7212     }
7213 
7214     switch (type) {
7215     case 0:
7216         size = MO_32;
7217         break;
7218     case 1:
7219         size = MO_64;
7220         break;
7221     case 3:
7222         size = MO_16;
7223         if (dc_isar_feature(aa64_fp16, s)) {
7224             break;
7225         }
7226         /* fallthru */
7227     default:
7228         unallocated_encoding(s);
7229         return;
7230     }
7231 
7232     if (!fp_access_check(s)) {
7233         return;
7234     }
7235 
7236     if (cond < 0x0e) { /* not always */
7237         TCGLabel *label_match = gen_new_label();
7238         label_continue = gen_new_label();
7239         arm_gen_test_cc(cond, label_match);
7240         /* nomatch: */
7241         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
7242         tcg_gen_br(label_continue);
7243         gen_set_label(label_match);
7244     }
7245 
7246     handle_fp_compare(s, size, rn, rm, false, op);
7247 
7248     if (cond < 0x0e) {
7249         gen_set_label(label_continue);
7250     }
7251 }
7252 
7253 /* Floating point conditional select
7254  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
7255  * +---+---+---+-----------+------+---+------+------+-----+------+------+
7256  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
7257  * +---+---+---+-----------+------+---+------+------+-----+------+------+
7258  */
7259 static void disas_fp_csel(DisasContext *s, uint32_t insn)
7260 {
7261     unsigned int mos, type, rm, cond, rn, rd;
7262     TCGv_i64 t_true, t_false;
7263     DisasCompare64 c;
7264     MemOp sz;
7265 
7266     mos = extract32(insn, 29, 3);
7267     type = extract32(insn, 22, 2);
7268     rm = extract32(insn, 16, 5);
7269     cond = extract32(insn, 12, 4);
7270     rn = extract32(insn, 5, 5);
7271     rd = extract32(insn, 0, 5);
7272 
7273     if (mos) {
7274         unallocated_encoding(s);
7275         return;
7276     }
7277 
7278     switch (type) {
7279     case 0:
7280         sz = MO_32;
7281         break;
7282     case 1:
7283         sz = MO_64;
7284         break;
7285     case 3:
7286         sz = MO_16;
7287         if (dc_isar_feature(aa64_fp16, s)) {
7288             break;
7289         }
7290         /* fallthru */
7291     default:
7292         unallocated_encoding(s);
7293         return;
7294     }
7295 
7296     if (!fp_access_check(s)) {
7297         return;
7298     }
7299 
7300     /* Zero extend sreg & hreg inputs to 64 bits now.  */
7301     t_true = tcg_temp_new_i64();
7302     t_false = tcg_temp_new_i64();
7303     read_vec_element(s, t_true, rn, 0, sz);
7304     read_vec_element(s, t_false, rm, 0, sz);
7305 
7306     a64_test_cc(&c, cond);
7307     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
7308                         t_true, t_false);
7309 
7310     /* Note that sregs & hregs write back zeros to the high bits,
7311        and we've already done the zero-extension.  */
7312     write_fp_dreg(s, rd, t_true);
7313 }
7314 
7315 /* Floating-point data-processing (1 source) - half precision */
7316 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
7317 {
7318     TCGv_ptr fpst = NULL;
7319     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
7320     TCGv_i32 tcg_res = tcg_temp_new_i32();
7321 
7322     switch (opcode) {
7323     case 0x0: /* FMOV */
7324         tcg_gen_mov_i32(tcg_res, tcg_op);
7325         break;
7326     case 0x1: /* FABS */
7327         gen_vfp_absh(tcg_res, tcg_op);
7328         break;
7329     case 0x2: /* FNEG */
7330         gen_vfp_negh(tcg_res, tcg_op);
7331         break;
7332     case 0x3: /* FSQRT */
7333         fpst = fpstatus_ptr(FPST_FPCR_F16);
7334         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
7335         break;
7336     case 0x8: /* FRINTN */
7337     case 0x9: /* FRINTP */
7338     case 0xa: /* FRINTM */
7339     case 0xb: /* FRINTZ */
7340     case 0xc: /* FRINTA */
7341     {
7342         TCGv_i32 tcg_rmode;
7343 
7344         fpst = fpstatus_ptr(FPST_FPCR_F16);
7345         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
7346         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
7347         gen_restore_rmode(tcg_rmode, fpst);
7348         break;
7349     }
7350     case 0xe: /* FRINTX */
7351         fpst = fpstatus_ptr(FPST_FPCR_F16);
7352         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
7353         break;
7354     case 0xf: /* FRINTI */
7355         fpst = fpstatus_ptr(FPST_FPCR_F16);
7356         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
7357         break;
7358     default:
7359         g_assert_not_reached();
7360     }
7361 
7362     write_fp_sreg(s, rd, tcg_res);
7363 }
7364 
7365 /* Floating-point data-processing (1 source) - single precision */
7366 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
7367 {
7368     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
7369     TCGv_i32 tcg_op, tcg_res;
7370     TCGv_ptr fpst;
7371     int rmode = -1;
7372 
7373     tcg_op = read_fp_sreg(s, rn);
7374     tcg_res = tcg_temp_new_i32();
7375 
7376     switch (opcode) {
7377     case 0x0: /* FMOV */
7378         tcg_gen_mov_i32(tcg_res, tcg_op);
7379         goto done;
7380     case 0x1: /* FABS */
7381         gen_vfp_abss(tcg_res, tcg_op);
7382         goto done;
7383     case 0x2: /* FNEG */
7384         gen_vfp_negs(tcg_res, tcg_op);
7385         goto done;
7386     case 0x3: /* FSQRT */
7387         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
7388         goto done;
7389     case 0x6: /* BFCVT */
7390         gen_fpst = gen_helper_bfcvt;
7391         break;
7392     case 0x8: /* FRINTN */
7393     case 0x9: /* FRINTP */
7394     case 0xa: /* FRINTM */
7395     case 0xb: /* FRINTZ */
7396     case 0xc: /* FRINTA */
7397         rmode = opcode & 7;
7398         gen_fpst = gen_helper_rints;
7399         break;
7400     case 0xe: /* FRINTX */
7401         gen_fpst = gen_helper_rints_exact;
7402         break;
7403     case 0xf: /* FRINTI */
7404         gen_fpst = gen_helper_rints;
7405         break;
7406     case 0x10: /* FRINT32Z */
7407         rmode = FPROUNDING_ZERO;
7408         gen_fpst = gen_helper_frint32_s;
7409         break;
7410     case 0x11: /* FRINT32X */
7411         gen_fpst = gen_helper_frint32_s;
7412         break;
7413     case 0x12: /* FRINT64Z */
7414         rmode = FPROUNDING_ZERO;
7415         gen_fpst = gen_helper_frint64_s;
7416         break;
7417     case 0x13: /* FRINT64X */
7418         gen_fpst = gen_helper_frint64_s;
7419         break;
7420     default:
7421         g_assert_not_reached();
7422     }
7423 
7424     fpst = fpstatus_ptr(FPST_FPCR);
7425     if (rmode >= 0) {
7426         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
7427         gen_fpst(tcg_res, tcg_op, fpst);
7428         gen_restore_rmode(tcg_rmode, fpst);
7429     } else {
7430         gen_fpst(tcg_res, tcg_op, fpst);
7431     }
7432 
7433  done:
7434     write_fp_sreg(s, rd, tcg_res);
7435 }
7436 
7437 /* Floating-point data-processing (1 source) - double precision */
7438 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
7439 {
7440     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
7441     TCGv_i64 tcg_op, tcg_res;
7442     TCGv_ptr fpst;
7443     int rmode = -1;
7444 
7445     switch (opcode) {
7446     case 0x0: /* FMOV */
7447         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
7448         return;
7449     }
7450 
7451     tcg_op = read_fp_dreg(s, rn);
7452     tcg_res = tcg_temp_new_i64();
7453 
7454     switch (opcode) {
7455     case 0x1: /* FABS */
7456         gen_vfp_absd(tcg_res, tcg_op);
7457         goto done;
7458     case 0x2: /* FNEG */
7459         gen_vfp_negd(tcg_res, tcg_op);
7460         goto done;
7461     case 0x3: /* FSQRT */
7462         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
7463         goto done;
7464     case 0x8: /* FRINTN */
7465     case 0x9: /* FRINTP */
7466     case 0xa: /* FRINTM */
7467     case 0xb: /* FRINTZ */
7468     case 0xc: /* FRINTA */
7469         rmode = opcode & 7;
7470         gen_fpst = gen_helper_rintd;
7471         break;
7472     case 0xe: /* FRINTX */
7473         gen_fpst = gen_helper_rintd_exact;
7474         break;
7475     case 0xf: /* FRINTI */
7476         gen_fpst = gen_helper_rintd;
7477         break;
7478     case 0x10: /* FRINT32Z */
7479         rmode = FPROUNDING_ZERO;
7480         gen_fpst = gen_helper_frint32_d;
7481         break;
7482     case 0x11: /* FRINT32X */
7483         gen_fpst = gen_helper_frint32_d;
7484         break;
7485     case 0x12: /* FRINT64Z */
7486         rmode = FPROUNDING_ZERO;
7487         gen_fpst = gen_helper_frint64_d;
7488         break;
7489     case 0x13: /* FRINT64X */
7490         gen_fpst = gen_helper_frint64_d;
7491         break;
7492     default:
7493         g_assert_not_reached();
7494     }
7495 
7496     fpst = fpstatus_ptr(FPST_FPCR);
7497     if (rmode >= 0) {
7498         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
7499         gen_fpst(tcg_res, tcg_op, fpst);
7500         gen_restore_rmode(tcg_rmode, fpst);
7501     } else {
7502         gen_fpst(tcg_res, tcg_op, fpst);
7503     }
7504 
7505  done:
7506     write_fp_dreg(s, rd, tcg_res);
7507 }
7508 
7509 static void handle_fp_fcvt(DisasContext *s, int opcode,
7510                            int rd, int rn, int dtype, int ntype)
7511 {
7512     switch (ntype) {
7513     case 0x0:
7514     {
7515         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
7516         if (dtype == 1) {
7517             /* Single to double */
7518             TCGv_i64 tcg_rd = tcg_temp_new_i64();
7519             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
7520             write_fp_dreg(s, rd, tcg_rd);
7521         } else {
7522             /* Single to half */
7523             TCGv_i32 tcg_rd = tcg_temp_new_i32();
7524             TCGv_i32 ahp = get_ahp_flag();
7525             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7526 
7527             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
7528             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
7529             write_fp_sreg(s, rd, tcg_rd);
7530         }
7531         break;
7532     }
7533     case 0x1:
7534     {
7535         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7536         TCGv_i32 tcg_rd = tcg_temp_new_i32();
7537         if (dtype == 0) {
7538             /* Double to single */
7539             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
7540         } else {
7541             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7542             TCGv_i32 ahp = get_ahp_flag();
7543             /* Double to half */
7544             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
7545             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
7546         }
7547         write_fp_sreg(s, rd, tcg_rd);
7548         break;
7549     }
7550     case 0x3:
7551     {
7552         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
7553         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
7554         TCGv_i32 tcg_ahp = get_ahp_flag();
7555         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
7556         if (dtype == 0) {
7557             /* Half to single */
7558             TCGv_i32 tcg_rd = tcg_temp_new_i32();
7559             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
7560             write_fp_sreg(s, rd, tcg_rd);
7561         } else {
7562             /* Half to double */
7563             TCGv_i64 tcg_rd = tcg_temp_new_i64();
7564             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
7565             write_fp_dreg(s, rd, tcg_rd);
7566         }
7567         break;
7568     }
7569     default:
7570         g_assert_not_reached();
7571     }
7572 }
7573 
7574 /* Floating point data-processing (1 source)
7575  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
7576  * +---+---+---+-----------+------+---+--------+-----------+------+------+
7577  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
7578  * +---+---+---+-----------+------+---+--------+-----------+------+------+
7579  */
7580 static void disas_fp_1src(DisasContext *s, uint32_t insn)
7581 {
7582     int mos = extract32(insn, 29, 3);
7583     int type = extract32(insn, 22, 2);
7584     int opcode = extract32(insn, 15, 6);
7585     int rn = extract32(insn, 5, 5);
7586     int rd = extract32(insn, 0, 5);
7587 
7588     if (mos) {
7589         goto do_unallocated;
7590     }
7591 
7592     switch (opcode) {
7593     case 0x4: case 0x5: case 0x7:
7594     {
7595         /* FCVT between half, single and double precision */
7596         int dtype = extract32(opcode, 0, 2);
7597         if (type == 2 || dtype == type) {
7598             goto do_unallocated;
7599         }
7600         if (!fp_access_check(s)) {
7601             return;
7602         }
7603 
7604         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
7605         break;
7606     }
7607 
7608     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
7609         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
7610             goto do_unallocated;
7611         }
7612         /* fall through */
7613     case 0x0 ... 0x3:
7614     case 0x8 ... 0xc:
7615     case 0xe ... 0xf:
7616         /* 32-to-32 and 64-to-64 ops */
7617         switch (type) {
7618         case 0:
7619             if (!fp_access_check(s)) {
7620                 return;
7621             }
7622             handle_fp_1src_single(s, opcode, rd, rn);
7623             break;
7624         case 1:
7625             if (!fp_access_check(s)) {
7626                 return;
7627             }
7628             handle_fp_1src_double(s, opcode, rd, rn);
7629             break;
7630         case 3:
7631             if (!dc_isar_feature(aa64_fp16, s)) {
7632                 goto do_unallocated;
7633             }
7634 
7635             if (!fp_access_check(s)) {
7636                 return;
7637             }
7638             handle_fp_1src_half(s, opcode, rd, rn);
7639             break;
7640         default:
7641             goto do_unallocated;
7642         }
7643         break;
7644 
7645     case 0x6:
7646         switch (type) {
7647         case 1: /* BFCVT */
7648             if (!dc_isar_feature(aa64_bf16, s)) {
7649                 goto do_unallocated;
7650             }
7651             if (!fp_access_check(s)) {
7652                 return;
7653             }
7654             handle_fp_1src_single(s, opcode, rd, rn);
7655             break;
7656         default:
7657             goto do_unallocated;
7658         }
7659         break;
7660 
7661     default:
7662     do_unallocated:
7663         unallocated_encoding(s);
7664         break;
7665     }
7666 }
7667 
7668 /* Floating-point data-processing (3 source) - single precision */
7669 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
7670                                   int rd, int rn, int rm, int ra)
7671 {
7672     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
7673     TCGv_i32 tcg_res = tcg_temp_new_i32();
7674     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7675 
7676     tcg_op1 = read_fp_sreg(s, rn);
7677     tcg_op2 = read_fp_sreg(s, rm);
7678     tcg_op3 = read_fp_sreg(s, ra);
7679 
7680     /* These are fused multiply-add, and must be done as one
7681      * floating point operation with no rounding between the
7682      * multiplication and addition steps.
7683      * NB that doing the negations here as separate steps is
7684      * correct : an input NaN should come out with its sign bit
7685      * flipped if it is a negated-input.
7686      */
7687     if (o1 == true) {
7688         gen_vfp_negs(tcg_op3, tcg_op3);
7689     }
7690 
7691     if (o0 != o1) {
7692         gen_vfp_negs(tcg_op1, tcg_op1);
7693     }
7694 
7695     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
7696 
7697     write_fp_sreg(s, rd, tcg_res);
7698 }
7699 
7700 /* Floating-point data-processing (3 source) - double precision */
7701 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
7702                                   int rd, int rn, int rm, int ra)
7703 {
7704     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
7705     TCGv_i64 tcg_res = tcg_temp_new_i64();
7706     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7707 
7708     tcg_op1 = read_fp_dreg(s, rn);
7709     tcg_op2 = read_fp_dreg(s, rm);
7710     tcg_op3 = read_fp_dreg(s, ra);
7711 
7712     /* These are fused multiply-add, and must be done as one
7713      * floating point operation with no rounding between the
7714      * multiplication and addition steps.
7715      * NB that doing the negations here as separate steps is
7716      * correct : an input NaN should come out with its sign bit
7717      * flipped if it is a negated-input.
7718      */
7719     if (o1 == true) {
7720         gen_vfp_negd(tcg_op3, tcg_op3);
7721     }
7722 
7723     if (o0 != o1) {
7724         gen_vfp_negd(tcg_op1, tcg_op1);
7725     }
7726 
7727     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
7728 
7729     write_fp_dreg(s, rd, tcg_res);
7730 }
7731 
7732 /* Floating-point data-processing (3 source) - half precision */
7733 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
7734                                 int rd, int rn, int rm, int ra)
7735 {
7736     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
7737     TCGv_i32 tcg_res = tcg_temp_new_i32();
7738     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
7739 
7740     tcg_op1 = read_fp_hreg(s, rn);
7741     tcg_op2 = read_fp_hreg(s, rm);
7742     tcg_op3 = read_fp_hreg(s, ra);
7743 
7744     /* These are fused multiply-add, and must be done as one
7745      * floating point operation with no rounding between the
7746      * multiplication and addition steps.
7747      * NB that doing the negations here as separate steps is
7748      * correct : an input NaN should come out with its sign bit
7749      * flipped if it is a negated-input.
7750      */
7751     if (o1 == true) {
7752         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
7753     }
7754 
7755     if (o0 != o1) {
7756         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
7757     }
7758 
7759     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
7760 
7761     write_fp_sreg(s, rd, tcg_res);
7762 }
7763 
7764 /* Floating point data-processing (3 source)
7765  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
7766  * +---+---+---+-----------+------+----+------+----+------+------+------+
7767  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
7768  * +---+---+---+-----------+------+----+------+----+------+------+------+
7769  */
7770 static void disas_fp_3src(DisasContext *s, uint32_t insn)
7771 {
7772     int mos = extract32(insn, 29, 3);
7773     int type = extract32(insn, 22, 2);
7774     int rd = extract32(insn, 0, 5);
7775     int rn = extract32(insn, 5, 5);
7776     int ra = extract32(insn, 10, 5);
7777     int rm = extract32(insn, 16, 5);
7778     bool o0 = extract32(insn, 15, 1);
7779     bool o1 = extract32(insn, 21, 1);
7780 
7781     if (mos) {
7782         unallocated_encoding(s);
7783         return;
7784     }
7785 
7786     switch (type) {
7787     case 0:
7788         if (!fp_access_check(s)) {
7789             return;
7790         }
7791         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
7792         break;
7793     case 1:
7794         if (!fp_access_check(s)) {
7795             return;
7796         }
7797         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
7798         break;
7799     case 3:
7800         if (!dc_isar_feature(aa64_fp16, s)) {
7801             unallocated_encoding(s);
7802             return;
7803         }
7804         if (!fp_access_check(s)) {
7805             return;
7806         }
7807         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
7808         break;
7809     default:
7810         unallocated_encoding(s);
7811     }
7812 }
7813 
7814 /* Floating point immediate
7815  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
7816  * +---+---+---+-----------+------+---+------------+-------+------+------+
7817  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
7818  * +---+---+---+-----------+------+---+------------+-------+------+------+
7819  */
7820 static void disas_fp_imm(DisasContext *s, uint32_t insn)
7821 {
7822     int rd = extract32(insn, 0, 5);
7823     int imm5 = extract32(insn, 5, 5);
7824     int imm8 = extract32(insn, 13, 8);
7825     int type = extract32(insn, 22, 2);
7826     int mos = extract32(insn, 29, 3);
7827     uint64_t imm;
7828     MemOp sz;
7829 
7830     if (mos || imm5) {
7831         unallocated_encoding(s);
7832         return;
7833     }
7834 
7835     switch (type) {
7836     case 0:
7837         sz = MO_32;
7838         break;
7839     case 1:
7840         sz = MO_64;
7841         break;
7842     case 3:
7843         sz = MO_16;
7844         if (dc_isar_feature(aa64_fp16, s)) {
7845             break;
7846         }
7847         /* fallthru */
7848     default:
7849         unallocated_encoding(s);
7850         return;
7851     }
7852 
7853     if (!fp_access_check(s)) {
7854         return;
7855     }
7856 
7857     imm = vfp_expand_imm(sz, imm8);
7858     write_fp_dreg(s, rd, tcg_constant_i64(imm));
7859 }
7860 
7861 /* Handle floating point <=> fixed point conversions. Note that we can
7862  * also deal with fp <=> integer conversions as a special case (scale == 64)
7863  * OPTME: consider handling that special case specially or at least skipping
7864  * the call to scalbn in the helpers for zero shifts.
7865  */
7866 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
7867                            bool itof, int rmode, int scale, int sf, int type)
7868 {
7869     bool is_signed = !(opcode & 1);
7870     TCGv_ptr tcg_fpstatus;
7871     TCGv_i32 tcg_shift, tcg_single;
7872     TCGv_i64 tcg_double;
7873 
7874     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
7875 
7876     tcg_shift = tcg_constant_i32(64 - scale);
7877 
7878     if (itof) {
7879         TCGv_i64 tcg_int = cpu_reg(s, rn);
7880         if (!sf) {
7881             TCGv_i64 tcg_extend = tcg_temp_new_i64();
7882 
7883             if (is_signed) {
7884                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
7885             } else {
7886                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
7887             }
7888 
7889             tcg_int = tcg_extend;
7890         }
7891 
7892         switch (type) {
7893         case 1: /* float64 */
7894             tcg_double = tcg_temp_new_i64();
7895             if (is_signed) {
7896                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
7897                                      tcg_shift, tcg_fpstatus);
7898             } else {
7899                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
7900                                      tcg_shift, tcg_fpstatus);
7901             }
7902             write_fp_dreg(s, rd, tcg_double);
7903             break;
7904 
7905         case 0: /* float32 */
7906             tcg_single = tcg_temp_new_i32();
7907             if (is_signed) {
7908                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7909                                      tcg_shift, tcg_fpstatus);
7910             } else {
7911                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7912                                      tcg_shift, tcg_fpstatus);
7913             }
7914             write_fp_sreg(s, rd, tcg_single);
7915             break;
7916 
7917         case 3: /* float16 */
7918             tcg_single = tcg_temp_new_i32();
7919             if (is_signed) {
7920                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7921                                      tcg_shift, tcg_fpstatus);
7922             } else {
7923                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7924                                      tcg_shift, tcg_fpstatus);
7925             }
7926             write_fp_sreg(s, rd, tcg_single);
7927             break;
7928 
7929         default:
7930             g_assert_not_reached();
7931         }
7932     } else {
7933         TCGv_i64 tcg_int = cpu_reg(s, rd);
7934         TCGv_i32 tcg_rmode;
7935 
7936         if (extract32(opcode, 2, 1)) {
7937             /* There are too many rounding modes to all fit into rmode,
7938              * so FCVTA[US] is a special case.
7939              */
7940             rmode = FPROUNDING_TIEAWAY;
7941         }
7942 
7943         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
7944 
7945         switch (type) {
7946         case 1: /* float64 */
7947             tcg_double = read_fp_dreg(s, rn);
7948             if (is_signed) {
7949                 if (!sf) {
7950                     gen_helper_vfp_tosld(tcg_int, tcg_double,
7951                                          tcg_shift, tcg_fpstatus);
7952                 } else {
7953                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
7954                                          tcg_shift, tcg_fpstatus);
7955                 }
7956             } else {
7957                 if (!sf) {
7958                     gen_helper_vfp_tould(tcg_int, tcg_double,
7959                                          tcg_shift, tcg_fpstatus);
7960                 } else {
7961                     gen_helper_vfp_touqd(tcg_int, tcg_double,
7962                                          tcg_shift, tcg_fpstatus);
7963                 }
7964             }
7965             if (!sf) {
7966                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7967             }
7968             break;
7969 
7970         case 0: /* float32 */
7971             tcg_single = read_fp_sreg(s, rn);
7972             if (sf) {
7973                 if (is_signed) {
7974                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
7975                                          tcg_shift, tcg_fpstatus);
7976                 } else {
7977                     gen_helper_vfp_touqs(tcg_int, tcg_single,
7978                                          tcg_shift, tcg_fpstatus);
7979                 }
7980             } else {
7981                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7982                 if (is_signed) {
7983                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7984                                          tcg_shift, tcg_fpstatus);
7985                 } else {
7986                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7987                                          tcg_shift, tcg_fpstatus);
7988                 }
7989                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7990             }
7991             break;
7992 
7993         case 3: /* float16 */
7994             tcg_single = read_fp_sreg(s, rn);
7995             if (sf) {
7996                 if (is_signed) {
7997                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7998                                          tcg_shift, tcg_fpstatus);
7999                 } else {
8000                     gen_helper_vfp_touqh(tcg_int, tcg_single,
8001                                          tcg_shift, tcg_fpstatus);
8002                 }
8003             } else {
8004                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
8005                 if (is_signed) {
8006                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
8007                                          tcg_shift, tcg_fpstatus);
8008                 } else {
8009                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
8010                                          tcg_shift, tcg_fpstatus);
8011                 }
8012                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
8013             }
8014             break;
8015 
8016         default:
8017             g_assert_not_reached();
8018         }
8019 
8020         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8021     }
8022 }
8023 
8024 /* Floating point <-> fixed point conversions
8025  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
8026  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
8027  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
8028  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
8029  */
8030 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
8031 {
8032     int rd = extract32(insn, 0, 5);
8033     int rn = extract32(insn, 5, 5);
8034     int scale = extract32(insn, 10, 6);
8035     int opcode = extract32(insn, 16, 3);
8036     int rmode = extract32(insn, 19, 2);
8037     int type = extract32(insn, 22, 2);
8038     bool sbit = extract32(insn, 29, 1);
8039     bool sf = extract32(insn, 31, 1);
8040     bool itof;
8041 
8042     if (sbit || (!sf && scale < 32)) {
8043         unallocated_encoding(s);
8044         return;
8045     }
8046 
8047     switch (type) {
8048     case 0: /* float32 */
8049     case 1: /* float64 */
8050         break;
8051     case 3: /* float16 */
8052         if (dc_isar_feature(aa64_fp16, s)) {
8053             break;
8054         }
8055         /* fallthru */
8056     default:
8057         unallocated_encoding(s);
8058         return;
8059     }
8060 
8061     switch ((rmode << 3) | opcode) {
8062     case 0x2: /* SCVTF */
8063     case 0x3: /* UCVTF */
8064         itof = true;
8065         break;
8066     case 0x18: /* FCVTZS */
8067     case 0x19: /* FCVTZU */
8068         itof = false;
8069         break;
8070     default:
8071         unallocated_encoding(s);
8072         return;
8073     }
8074 
8075     if (!fp_access_check(s)) {
8076         return;
8077     }
8078 
8079     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
8080 }
8081 
8082 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
8083 {
8084     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
8085      * without conversion.
8086      */
8087 
8088     if (itof) {
8089         TCGv_i64 tcg_rn = cpu_reg(s, rn);
8090         TCGv_i64 tmp;
8091 
8092         switch (type) {
8093         case 0:
8094             /* 32 bit */
8095             tmp = tcg_temp_new_i64();
8096             tcg_gen_ext32u_i64(tmp, tcg_rn);
8097             write_fp_dreg(s, rd, tmp);
8098             break;
8099         case 1:
8100             /* 64 bit */
8101             write_fp_dreg(s, rd, tcg_rn);
8102             break;
8103         case 2:
8104             /* 64 bit to top half. */
8105             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
8106             clear_vec_high(s, true, rd);
8107             break;
8108         case 3:
8109             /* 16 bit */
8110             tmp = tcg_temp_new_i64();
8111             tcg_gen_ext16u_i64(tmp, tcg_rn);
8112             write_fp_dreg(s, rd, tmp);
8113             break;
8114         default:
8115             g_assert_not_reached();
8116         }
8117     } else {
8118         TCGv_i64 tcg_rd = cpu_reg(s, rd);
8119 
8120         switch (type) {
8121         case 0:
8122             /* 32 bit */
8123             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
8124             break;
8125         case 1:
8126             /* 64 bit */
8127             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
8128             break;
8129         case 2:
8130             /* 64 bits from top half */
8131             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
8132             break;
8133         case 3:
8134             /* 16 bit */
8135             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
8136             break;
8137         default:
8138             g_assert_not_reached();
8139         }
8140     }
8141 }
8142 
8143 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
8144 {
8145     TCGv_i64 t = read_fp_dreg(s, rn);
8146     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
8147 
8148     gen_helper_fjcvtzs(t, t, fpstatus);
8149 
8150     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
8151     tcg_gen_extrh_i64_i32(cpu_ZF, t);
8152     tcg_gen_movi_i32(cpu_CF, 0);
8153     tcg_gen_movi_i32(cpu_NF, 0);
8154     tcg_gen_movi_i32(cpu_VF, 0);
8155 }
8156 
8157 /* Floating point <-> integer conversions
8158  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
8159  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
8160  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
8161  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
8162  */
8163 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
8164 {
8165     int rd = extract32(insn, 0, 5);
8166     int rn = extract32(insn, 5, 5);
8167     int opcode = extract32(insn, 16, 3);
8168     int rmode = extract32(insn, 19, 2);
8169     int type = extract32(insn, 22, 2);
8170     bool sbit = extract32(insn, 29, 1);
8171     bool sf = extract32(insn, 31, 1);
8172     bool itof = false;
8173 
8174     if (sbit) {
8175         goto do_unallocated;
8176     }
8177 
8178     switch (opcode) {
8179     case 2: /* SCVTF */
8180     case 3: /* UCVTF */
8181         itof = true;
8182         /* fallthru */
8183     case 4: /* FCVTAS */
8184     case 5: /* FCVTAU */
8185         if (rmode != 0) {
8186             goto do_unallocated;
8187         }
8188         /* fallthru */
8189     case 0: /* FCVT[NPMZ]S */
8190     case 1: /* FCVT[NPMZ]U */
8191         switch (type) {
8192         case 0: /* float32 */
8193         case 1: /* float64 */
8194             break;
8195         case 3: /* float16 */
8196             if (!dc_isar_feature(aa64_fp16, s)) {
8197                 goto do_unallocated;
8198             }
8199             break;
8200         default:
8201             goto do_unallocated;
8202         }
8203         if (!fp_access_check(s)) {
8204             return;
8205         }
8206         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
8207         break;
8208 
8209     default:
8210         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
8211         case 0b01100110: /* FMOV half <-> 32-bit int */
8212         case 0b01100111:
8213         case 0b11100110: /* FMOV half <-> 64-bit int */
8214         case 0b11100111:
8215             if (!dc_isar_feature(aa64_fp16, s)) {
8216                 goto do_unallocated;
8217             }
8218             /* fallthru */
8219         case 0b00000110: /* FMOV 32-bit */
8220         case 0b00000111:
8221         case 0b10100110: /* FMOV 64-bit */
8222         case 0b10100111:
8223         case 0b11001110: /* FMOV top half of 128-bit */
8224         case 0b11001111:
8225             if (!fp_access_check(s)) {
8226                 return;
8227             }
8228             itof = opcode & 1;
8229             handle_fmov(s, rd, rn, type, itof);
8230             break;
8231 
8232         case 0b00111110: /* FJCVTZS */
8233             if (!dc_isar_feature(aa64_jscvt, s)) {
8234                 goto do_unallocated;
8235             } else if (fp_access_check(s)) {
8236                 handle_fjcvtzs(s, rd, rn);
8237             }
8238             break;
8239 
8240         default:
8241         do_unallocated:
8242             unallocated_encoding(s);
8243             return;
8244         }
8245         break;
8246     }
8247 }
8248 
8249 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
8250  *   31  30  29 28     25 24                          0
8251  * +---+---+---+---------+-----------------------------+
8252  * |   | 0 |   | 1 1 1 1 |                             |
8253  * +---+---+---+---------+-----------------------------+
8254  */
8255 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
8256 {
8257     if (extract32(insn, 24, 1)) {
8258         /* Floating point data-processing (3 source) */
8259         disas_fp_3src(s, insn);
8260     } else if (extract32(insn, 21, 1) == 0) {
8261         /* Floating point to fixed point conversions */
8262         disas_fp_fixed_conv(s, insn);
8263     } else {
8264         switch (extract32(insn, 10, 2)) {
8265         case 1:
8266             /* Floating point conditional compare */
8267             disas_fp_ccomp(s, insn);
8268             break;
8269         case 2:
8270             /* Floating point data-processing (2 source) */
8271             unallocated_encoding(s); /* in decodetree */
8272             break;
8273         case 3:
8274             /* Floating point conditional select */
8275             disas_fp_csel(s, insn);
8276             break;
8277         case 0:
8278             switch (ctz32(extract32(insn, 12, 4))) {
8279             case 0: /* [15:12] == xxx1 */
8280                 /* Floating point immediate */
8281                 disas_fp_imm(s, insn);
8282                 break;
8283             case 1: /* [15:12] == xx10 */
8284                 /* Floating point compare */
8285                 disas_fp_compare(s, insn);
8286                 break;
8287             case 2: /* [15:12] == x100 */
8288                 /* Floating point data-processing (1 source) */
8289                 disas_fp_1src(s, insn);
8290                 break;
8291             case 3: /* [15:12] == 1000 */
8292                 unallocated_encoding(s);
8293                 break;
8294             default: /* [15:12] == 0000 */
8295                 /* Floating point <-> integer conversions */
8296                 disas_fp_int_conv(s, insn);
8297                 break;
8298             }
8299             break;
8300         }
8301     }
8302 }
8303 
8304 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
8305                      int pos)
8306 {
8307     /* Extract 64 bits from the middle of two concatenated 64 bit
8308      * vector register slices left:right. The extracted bits start
8309      * at 'pos' bits into the right (least significant) side.
8310      * We return the result in tcg_right, and guarantee not to
8311      * trash tcg_left.
8312      */
8313     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8314     assert(pos > 0 && pos < 64);
8315 
8316     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
8317     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
8318     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
8319 }
8320 
8321 /* EXT
8322  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
8323  * +---+---+-------------+-----+---+------+---+------+---+------+------+
8324  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
8325  * +---+---+-------------+-----+---+------+---+------+---+------+------+
8326  */
8327 static void disas_simd_ext(DisasContext *s, uint32_t insn)
8328 {
8329     int is_q = extract32(insn, 30, 1);
8330     int op2 = extract32(insn, 22, 2);
8331     int imm4 = extract32(insn, 11, 4);
8332     int rm = extract32(insn, 16, 5);
8333     int rn = extract32(insn, 5, 5);
8334     int rd = extract32(insn, 0, 5);
8335     int pos = imm4 << 3;
8336     TCGv_i64 tcg_resl, tcg_resh;
8337 
8338     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
8339         unallocated_encoding(s);
8340         return;
8341     }
8342 
8343     if (!fp_access_check(s)) {
8344         return;
8345     }
8346 
8347     tcg_resh = tcg_temp_new_i64();
8348     tcg_resl = tcg_temp_new_i64();
8349 
8350     /* Vd gets bits starting at pos bits into Vm:Vn. This is
8351      * either extracting 128 bits from a 128:128 concatenation, or
8352      * extracting 64 bits from a 64:64 concatenation.
8353      */
8354     if (!is_q) {
8355         read_vec_element(s, tcg_resl, rn, 0, MO_64);
8356         if (pos != 0) {
8357             read_vec_element(s, tcg_resh, rm, 0, MO_64);
8358             do_ext64(s, tcg_resh, tcg_resl, pos);
8359         }
8360     } else {
8361         TCGv_i64 tcg_hh;
8362         typedef struct {
8363             int reg;
8364             int elt;
8365         } EltPosns;
8366         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
8367         EltPosns *elt = eltposns;
8368 
8369         if (pos >= 64) {
8370             elt++;
8371             pos -= 64;
8372         }
8373 
8374         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
8375         elt++;
8376         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
8377         elt++;
8378         if (pos != 0) {
8379             do_ext64(s, tcg_resh, tcg_resl, pos);
8380             tcg_hh = tcg_temp_new_i64();
8381             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
8382             do_ext64(s, tcg_hh, tcg_resh, pos);
8383         }
8384     }
8385 
8386     write_vec_element(s, tcg_resl, rd, 0, MO_64);
8387     if (is_q) {
8388         write_vec_element(s, tcg_resh, rd, 1, MO_64);
8389     }
8390     clear_vec_high(s, is_q, rd);
8391 }
8392 
8393 /* TBL/TBX
8394  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
8395  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
8396  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
8397  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
8398  */
8399 static void disas_simd_tb(DisasContext *s, uint32_t insn)
8400 {
8401     int op2 = extract32(insn, 22, 2);
8402     int is_q = extract32(insn, 30, 1);
8403     int rm = extract32(insn, 16, 5);
8404     int rn = extract32(insn, 5, 5);
8405     int rd = extract32(insn, 0, 5);
8406     int is_tbx = extract32(insn, 12, 1);
8407     int len = (extract32(insn, 13, 2) + 1) * 16;
8408 
8409     if (op2 != 0) {
8410         unallocated_encoding(s);
8411         return;
8412     }
8413 
8414     if (!fp_access_check(s)) {
8415         return;
8416     }
8417 
8418     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
8419                        vec_full_reg_offset(s, rm), tcg_env,
8420                        is_q ? 16 : 8, vec_full_reg_size(s),
8421                        (len << 6) | (is_tbx << 5) | rn,
8422                        gen_helper_simd_tblx);
8423 }
8424 
8425 /* ZIP/UZP/TRN
8426  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
8427  * +---+---+-------------+------+---+------+---+------------------+------+
8428  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
8429  * +---+---+-------------+------+---+------+---+------------------+------+
8430  */
8431 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
8432 {
8433     int rd = extract32(insn, 0, 5);
8434     int rn = extract32(insn, 5, 5);
8435     int rm = extract32(insn, 16, 5);
8436     int size = extract32(insn, 22, 2);
8437     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
8438      * bit 2 indicates 1 vs 2 variant of the insn.
8439      */
8440     int opcode = extract32(insn, 12, 2);
8441     bool part = extract32(insn, 14, 1);
8442     bool is_q = extract32(insn, 30, 1);
8443     int esize = 8 << size;
8444     int i;
8445     int datasize = is_q ? 128 : 64;
8446     int elements = datasize / esize;
8447     TCGv_i64 tcg_res[2], tcg_ele;
8448 
8449     if (opcode == 0 || (size == 3 && !is_q)) {
8450         unallocated_encoding(s);
8451         return;
8452     }
8453 
8454     if (!fp_access_check(s)) {
8455         return;
8456     }
8457 
8458     tcg_res[0] = tcg_temp_new_i64();
8459     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
8460     tcg_ele = tcg_temp_new_i64();
8461 
8462     for (i = 0; i < elements; i++) {
8463         int o, w;
8464 
8465         switch (opcode) {
8466         case 1: /* UZP1/2 */
8467         {
8468             int midpoint = elements / 2;
8469             if (i < midpoint) {
8470                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
8471             } else {
8472                 read_vec_element(s, tcg_ele, rm,
8473                                  2 * (i - midpoint) + part, size);
8474             }
8475             break;
8476         }
8477         case 2: /* TRN1/2 */
8478             if (i & 1) {
8479                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
8480             } else {
8481                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
8482             }
8483             break;
8484         case 3: /* ZIP1/2 */
8485         {
8486             int base = part * elements / 2;
8487             if (i & 1) {
8488                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
8489             } else {
8490                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
8491             }
8492             break;
8493         }
8494         default:
8495             g_assert_not_reached();
8496         }
8497 
8498         w = (i * esize) / 64;
8499         o = (i * esize) % 64;
8500         if (o == 0) {
8501             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
8502         } else {
8503             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
8504             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
8505         }
8506     }
8507 
8508     for (i = 0; i <= is_q; ++i) {
8509         write_vec_element(s, tcg_res[i], rd, i, MO_64);
8510     }
8511     clear_vec_high(s, is_q, rd);
8512 }
8513 
8514 /*
8515  * do_reduction_op helper
8516  *
8517  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
8518  * important for correct NaN propagation that we do these
8519  * operations in exactly the order specified by the pseudocode.
8520  *
8521  * This is a recursive function, TCG temps should be freed by the
8522  * calling function once it is done with the values.
8523  */
8524 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
8525                                 int esize, int size, int vmap, TCGv_ptr fpst)
8526 {
8527     if (esize == size) {
8528         int element;
8529         MemOp msize = esize == 16 ? MO_16 : MO_32;
8530         TCGv_i32 tcg_elem;
8531 
8532         /* We should have one register left here */
8533         assert(ctpop8(vmap) == 1);
8534         element = ctz32(vmap);
8535         assert(element < 8);
8536 
8537         tcg_elem = tcg_temp_new_i32();
8538         read_vec_element_i32(s, tcg_elem, rn, element, msize);
8539         return tcg_elem;
8540     } else {
8541         int bits = size / 2;
8542         int shift = ctpop8(vmap) / 2;
8543         int vmap_lo = (vmap >> shift) & vmap;
8544         int vmap_hi = (vmap & ~vmap_lo);
8545         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
8546 
8547         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
8548         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
8549         tcg_res = tcg_temp_new_i32();
8550 
8551         switch (fpopcode) {
8552         case 0x0c: /* fmaxnmv half-precision */
8553             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
8554             break;
8555         case 0x0f: /* fmaxv half-precision */
8556             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
8557             break;
8558         case 0x1c: /* fminnmv half-precision */
8559             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
8560             break;
8561         case 0x1f: /* fminv half-precision */
8562             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
8563             break;
8564         case 0x2c: /* fmaxnmv */
8565             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
8566             break;
8567         case 0x2f: /* fmaxv */
8568             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
8569             break;
8570         case 0x3c: /* fminnmv */
8571             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
8572             break;
8573         case 0x3f: /* fminv */
8574             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
8575             break;
8576         default:
8577             g_assert_not_reached();
8578         }
8579         return tcg_res;
8580     }
8581 }
8582 
8583 /* AdvSIMD across lanes
8584  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8585  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8586  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8587  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8588  */
8589 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
8590 {
8591     int rd = extract32(insn, 0, 5);
8592     int rn = extract32(insn, 5, 5);
8593     int size = extract32(insn, 22, 2);
8594     int opcode = extract32(insn, 12, 5);
8595     bool is_q = extract32(insn, 30, 1);
8596     bool is_u = extract32(insn, 29, 1);
8597     bool is_fp = false;
8598     bool is_min = false;
8599     int esize;
8600     int elements;
8601     int i;
8602     TCGv_i64 tcg_res, tcg_elt;
8603 
8604     switch (opcode) {
8605     case 0x1b: /* ADDV */
8606         if (is_u) {
8607             unallocated_encoding(s);
8608             return;
8609         }
8610         /* fall through */
8611     case 0x3: /* SADDLV, UADDLV */
8612     case 0xa: /* SMAXV, UMAXV */
8613     case 0x1a: /* SMINV, UMINV */
8614         if (size == 3 || (size == 2 && !is_q)) {
8615             unallocated_encoding(s);
8616             return;
8617         }
8618         break;
8619     case 0xc: /* FMAXNMV, FMINNMV */
8620     case 0xf: /* FMAXV, FMINV */
8621         /* Bit 1 of size field encodes min vs max and the actual size
8622          * depends on the encoding of the U bit. If not set (and FP16
8623          * enabled) then we do half-precision float instead of single
8624          * precision.
8625          */
8626         is_min = extract32(size, 1, 1);
8627         is_fp = true;
8628         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
8629             size = 1;
8630         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
8631             unallocated_encoding(s);
8632             return;
8633         } else {
8634             size = 2;
8635         }
8636         break;
8637     default:
8638         unallocated_encoding(s);
8639         return;
8640     }
8641 
8642     if (!fp_access_check(s)) {
8643         return;
8644     }
8645 
8646     esize = 8 << size;
8647     elements = (is_q ? 128 : 64) / esize;
8648 
8649     tcg_res = tcg_temp_new_i64();
8650     tcg_elt = tcg_temp_new_i64();
8651 
8652     /* These instructions operate across all lanes of a vector
8653      * to produce a single result. We can guarantee that a 64
8654      * bit intermediate is sufficient:
8655      *  + for [US]ADDLV the maximum element size is 32 bits, and
8656      *    the result type is 64 bits
8657      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
8658      *    same as the element size, which is 32 bits at most
8659      * For the integer operations we can choose to work at 64
8660      * or 32 bits and truncate at the end; for simplicity
8661      * we use 64 bits always. The floating point
8662      * ops do require 32 bit intermediates, though.
8663      */
8664     if (!is_fp) {
8665         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
8666 
8667         for (i = 1; i < elements; i++) {
8668             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
8669 
8670             switch (opcode) {
8671             case 0x03: /* SADDLV / UADDLV */
8672             case 0x1b: /* ADDV */
8673                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
8674                 break;
8675             case 0x0a: /* SMAXV / UMAXV */
8676                 if (is_u) {
8677                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
8678                 } else {
8679                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
8680                 }
8681                 break;
8682             case 0x1a: /* SMINV / UMINV */
8683                 if (is_u) {
8684                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
8685                 } else {
8686                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
8687                 }
8688                 break;
8689             default:
8690                 g_assert_not_reached();
8691             }
8692 
8693         }
8694     } else {
8695         /* Floating point vector reduction ops which work across 32
8696          * bit (single) or 16 bit (half-precision) intermediates.
8697          * Note that correct NaN propagation requires that we do these
8698          * operations in exactly the order specified by the pseudocode.
8699          */
8700         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8701         int fpopcode = opcode | is_min << 4 | is_u << 5;
8702         int vmap = (1 << elements) - 1;
8703         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
8704                                              (is_q ? 128 : 64), vmap, fpst);
8705         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
8706     }
8707 
8708     /* Now truncate the result to the width required for the final output */
8709     if (opcode == 0x03) {
8710         /* SADDLV, UADDLV: result is 2*esize */
8711         size++;
8712     }
8713 
8714     switch (size) {
8715     case 0:
8716         tcg_gen_ext8u_i64(tcg_res, tcg_res);
8717         break;
8718     case 1:
8719         tcg_gen_ext16u_i64(tcg_res, tcg_res);
8720         break;
8721     case 2:
8722         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8723         break;
8724     case 3:
8725         break;
8726     default:
8727         g_assert_not_reached();
8728     }
8729 
8730     write_fp_dreg(s, rd, tcg_res);
8731 }
8732 
8733 /* AdvSIMD modified immediate
8734  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8735  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8736  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8737  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8738  *
8739  * There are a number of operations that can be carried out here:
8740  *   MOVI - move (shifted) imm into register
8741  *   MVNI - move inverted (shifted) imm into register
8742  *   ORR  - bitwise OR of (shifted) imm with register
8743  *   BIC  - bitwise clear of (shifted) imm with register
8744  * With ARMv8.2 we also have:
8745  *   FMOV half-precision
8746  */
8747 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8748 {
8749     int rd = extract32(insn, 0, 5);
8750     int cmode = extract32(insn, 12, 4);
8751     int o2 = extract32(insn, 11, 1);
8752     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8753     bool is_neg = extract32(insn, 29, 1);
8754     bool is_q = extract32(insn, 30, 1);
8755     uint64_t imm = 0;
8756 
8757     if (o2) {
8758         if (cmode != 0xf || is_neg) {
8759             unallocated_encoding(s);
8760             return;
8761         }
8762         /* FMOV (vector, immediate) - half-precision */
8763         if (!dc_isar_feature(aa64_fp16, s)) {
8764             unallocated_encoding(s);
8765             return;
8766         }
8767         imm = vfp_expand_imm(MO_16, abcdefgh);
8768         /* now duplicate across the lanes */
8769         imm = dup_const(MO_16, imm);
8770     } else {
8771         if (cmode == 0xf && is_neg && !is_q) {
8772             unallocated_encoding(s);
8773             return;
8774         }
8775         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8776     }
8777 
8778     if (!fp_access_check(s)) {
8779         return;
8780     }
8781 
8782     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8783         /* MOVI or MVNI, with MVNI negation handled above.  */
8784         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8785                              vec_full_reg_size(s), imm);
8786     } else {
8787         /* ORR or BIC, with BIC negation to AND handled above.  */
8788         if (is_neg) {
8789             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8790         } else {
8791             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8792         }
8793     }
8794 }
8795 
8796 /*
8797  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8798  *
8799  * This code is handles the common shifting code and is used by both
8800  * the vector and scalar code.
8801  */
8802 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8803                                     TCGv_i64 tcg_rnd, bool accumulate,
8804                                     bool is_u, int size, int shift)
8805 {
8806     bool extended_result = false;
8807     bool round = tcg_rnd != NULL;
8808     int ext_lshift = 0;
8809     TCGv_i64 tcg_src_hi;
8810 
8811     if (round && size == 3) {
8812         extended_result = true;
8813         ext_lshift = 64 - shift;
8814         tcg_src_hi = tcg_temp_new_i64();
8815     } else if (shift == 64) {
8816         if (!accumulate && is_u) {
8817             /* result is zero */
8818             tcg_gen_movi_i64(tcg_res, 0);
8819             return;
8820         }
8821     }
8822 
8823     /* Deal with the rounding step */
8824     if (round) {
8825         if (extended_result) {
8826             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8827             if (!is_u) {
8828                 /* take care of sign extending tcg_res */
8829                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8830                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8831                                  tcg_src, tcg_src_hi,
8832                                  tcg_rnd, tcg_zero);
8833             } else {
8834                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8835                                  tcg_src, tcg_zero,
8836                                  tcg_rnd, tcg_zero);
8837             }
8838         } else {
8839             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8840         }
8841     }
8842 
8843     /* Now do the shift right */
8844     if (round && extended_result) {
8845         /* extended case, >64 bit precision required */
8846         if (ext_lshift == 0) {
8847             /* special case, only high bits matter */
8848             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8849         } else {
8850             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8851             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8852             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8853         }
8854     } else {
8855         if (is_u) {
8856             if (shift == 64) {
8857                 /* essentially shifting in 64 zeros */
8858                 tcg_gen_movi_i64(tcg_src, 0);
8859             } else {
8860                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8861             }
8862         } else {
8863             if (shift == 64) {
8864                 /* effectively extending the sign-bit */
8865                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8866             } else {
8867                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8868             }
8869         }
8870     }
8871 
8872     if (accumulate) {
8873         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8874     } else {
8875         tcg_gen_mov_i64(tcg_res, tcg_src);
8876     }
8877 }
8878 
8879 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8880 static void handle_scalar_simd_shri(DisasContext *s,
8881                                     bool is_u, int immh, int immb,
8882                                     int opcode, int rn, int rd)
8883 {
8884     const int size = 3;
8885     int immhb = immh << 3 | immb;
8886     int shift = 2 * (8 << size) - immhb;
8887     bool accumulate = false;
8888     bool round = false;
8889     bool insert = false;
8890     TCGv_i64 tcg_rn;
8891     TCGv_i64 tcg_rd;
8892     TCGv_i64 tcg_round;
8893 
8894     if (!extract32(immh, 3, 1)) {
8895         unallocated_encoding(s);
8896         return;
8897     }
8898 
8899     if (!fp_access_check(s)) {
8900         return;
8901     }
8902 
8903     switch (opcode) {
8904     case 0x02: /* SSRA / USRA (accumulate) */
8905         accumulate = true;
8906         break;
8907     case 0x04: /* SRSHR / URSHR (rounding) */
8908         round = true;
8909         break;
8910     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8911         accumulate = round = true;
8912         break;
8913     case 0x08: /* SRI */
8914         insert = true;
8915         break;
8916     }
8917 
8918     if (round) {
8919         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8920     } else {
8921         tcg_round = NULL;
8922     }
8923 
8924     tcg_rn = read_fp_dreg(s, rn);
8925     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8926 
8927     if (insert) {
8928         /* shift count same as element size is valid but does nothing;
8929          * special case to avoid potential shift by 64.
8930          */
8931         int esize = 8 << size;
8932         if (shift != esize) {
8933             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8934             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8935         }
8936     } else {
8937         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8938                                 accumulate, is_u, size, shift);
8939     }
8940 
8941     write_fp_dreg(s, rd, tcg_rd);
8942 }
8943 
8944 /* SHL/SLI - Scalar shift left */
8945 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8946                                     int immh, int immb, int opcode,
8947                                     int rn, int rd)
8948 {
8949     int size = 32 - clz32(immh) - 1;
8950     int immhb = immh << 3 | immb;
8951     int shift = immhb - (8 << size);
8952     TCGv_i64 tcg_rn;
8953     TCGv_i64 tcg_rd;
8954 
8955     if (!extract32(immh, 3, 1)) {
8956         unallocated_encoding(s);
8957         return;
8958     }
8959 
8960     if (!fp_access_check(s)) {
8961         return;
8962     }
8963 
8964     tcg_rn = read_fp_dreg(s, rn);
8965     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8966 
8967     if (insert) {
8968         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8969     } else {
8970         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8971     }
8972 
8973     write_fp_dreg(s, rd, tcg_rd);
8974 }
8975 
8976 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8977  * (signed/unsigned) narrowing */
8978 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8979                                    bool is_u_shift, bool is_u_narrow,
8980                                    int immh, int immb, int opcode,
8981                                    int rn, int rd)
8982 {
8983     int immhb = immh << 3 | immb;
8984     int size = 32 - clz32(immh) - 1;
8985     int esize = 8 << size;
8986     int shift = (2 * esize) - immhb;
8987     int elements = is_scalar ? 1 : (64 / esize);
8988     bool round = extract32(opcode, 0, 1);
8989     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8990     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8991     TCGv_i32 tcg_rd_narrowed;
8992     TCGv_i64 tcg_final;
8993 
8994     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8995         { gen_helper_neon_narrow_sat_s8,
8996           gen_helper_neon_unarrow_sat8 },
8997         { gen_helper_neon_narrow_sat_s16,
8998           gen_helper_neon_unarrow_sat16 },
8999         { gen_helper_neon_narrow_sat_s32,
9000           gen_helper_neon_unarrow_sat32 },
9001         { NULL, NULL },
9002     };
9003     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
9004         gen_helper_neon_narrow_sat_u8,
9005         gen_helper_neon_narrow_sat_u16,
9006         gen_helper_neon_narrow_sat_u32,
9007         NULL
9008     };
9009     NeonGenNarrowEnvFn *narrowfn;
9010 
9011     int i;
9012 
9013     assert(size < 4);
9014 
9015     if (extract32(immh, 3, 1)) {
9016         unallocated_encoding(s);
9017         return;
9018     }
9019 
9020     if (!fp_access_check(s)) {
9021         return;
9022     }
9023 
9024     if (is_u_shift) {
9025         narrowfn = unsigned_narrow_fns[size];
9026     } else {
9027         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
9028     }
9029 
9030     tcg_rn = tcg_temp_new_i64();
9031     tcg_rd = tcg_temp_new_i64();
9032     tcg_rd_narrowed = tcg_temp_new_i32();
9033     tcg_final = tcg_temp_new_i64();
9034 
9035     if (round) {
9036         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
9037     } else {
9038         tcg_round = NULL;
9039     }
9040 
9041     for (i = 0; i < elements; i++) {
9042         read_vec_element(s, tcg_rn, rn, i, ldop);
9043         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
9044                                 false, is_u_shift, size+1, shift);
9045         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
9046         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
9047         if (i == 0) {
9048             tcg_gen_extract_i64(tcg_final, tcg_rd, 0, esize);
9049         } else {
9050             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
9051         }
9052     }
9053 
9054     if (!is_q) {
9055         write_vec_element(s, tcg_final, rd, 0, MO_64);
9056     } else {
9057         write_vec_element(s, tcg_final, rd, 1, MO_64);
9058     }
9059     clear_vec_high(s, is_q, rd);
9060 }
9061 
9062 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
9063 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
9064                              bool src_unsigned, bool dst_unsigned,
9065                              int immh, int immb, int rn, int rd)
9066 {
9067     int immhb = immh << 3 | immb;
9068     int size = 32 - clz32(immh) - 1;
9069     int shift = immhb - (8 << size);
9070     int pass;
9071 
9072     assert(immh != 0);
9073     assert(!(scalar && is_q));
9074 
9075     if (!scalar) {
9076         if (!is_q && extract32(immh, 3, 1)) {
9077             unallocated_encoding(s);
9078             return;
9079         }
9080 
9081         /* Since we use the variable-shift helpers we must
9082          * replicate the shift count into each element of
9083          * the tcg_shift value.
9084          */
9085         switch (size) {
9086         case 0:
9087             shift |= shift << 8;
9088             /* fall through */
9089         case 1:
9090             shift |= shift << 16;
9091             break;
9092         case 2:
9093         case 3:
9094             break;
9095         default:
9096             g_assert_not_reached();
9097         }
9098     }
9099 
9100     if (!fp_access_check(s)) {
9101         return;
9102     }
9103 
9104     if (size == 3) {
9105         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
9106         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
9107             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
9108             { NULL, gen_helper_neon_qshl_u64 },
9109         };
9110         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
9111         int maxpass = is_q ? 2 : 1;
9112 
9113         for (pass = 0; pass < maxpass; pass++) {
9114             TCGv_i64 tcg_op = tcg_temp_new_i64();
9115 
9116             read_vec_element(s, tcg_op, rn, pass, MO_64);
9117             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
9118             write_vec_element(s, tcg_op, rd, pass, MO_64);
9119         }
9120         clear_vec_high(s, is_q, rd);
9121     } else {
9122         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
9123         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
9124             {
9125                 { gen_helper_neon_qshl_s8,
9126                   gen_helper_neon_qshl_s16,
9127                   gen_helper_neon_qshl_s32 },
9128                 { gen_helper_neon_qshlu_s8,
9129                   gen_helper_neon_qshlu_s16,
9130                   gen_helper_neon_qshlu_s32 }
9131             }, {
9132                 { NULL, NULL, NULL },
9133                 { gen_helper_neon_qshl_u8,
9134                   gen_helper_neon_qshl_u16,
9135                   gen_helper_neon_qshl_u32 }
9136             }
9137         };
9138         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
9139         MemOp memop = scalar ? size : MO_32;
9140         int maxpass = scalar ? 1 : is_q ? 4 : 2;
9141 
9142         for (pass = 0; pass < maxpass; pass++) {
9143             TCGv_i32 tcg_op = tcg_temp_new_i32();
9144 
9145             read_vec_element_i32(s, tcg_op, rn, pass, memop);
9146             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
9147             if (scalar) {
9148                 switch (size) {
9149                 case 0:
9150                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
9151                     break;
9152                 case 1:
9153                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
9154                     break;
9155                 case 2:
9156                     break;
9157                 default:
9158                     g_assert_not_reached();
9159                 }
9160                 write_fp_sreg(s, rd, tcg_op);
9161             } else {
9162                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
9163             }
9164         }
9165 
9166         if (!scalar) {
9167             clear_vec_high(s, is_q, rd);
9168         }
9169     }
9170 }
9171 
9172 /* Common vector code for handling integer to FP conversion */
9173 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
9174                                    int elements, int is_signed,
9175                                    int fracbits, int size)
9176 {
9177     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9178     TCGv_i32 tcg_shift = NULL;
9179 
9180     MemOp mop = size | (is_signed ? MO_SIGN : 0);
9181     int pass;
9182 
9183     if (fracbits || size == MO_64) {
9184         tcg_shift = tcg_constant_i32(fracbits);
9185     }
9186 
9187     if (size == MO_64) {
9188         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
9189         TCGv_i64 tcg_double = tcg_temp_new_i64();
9190 
9191         for (pass = 0; pass < elements; pass++) {
9192             read_vec_element(s, tcg_int64, rn, pass, mop);
9193 
9194             if (is_signed) {
9195                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
9196                                      tcg_shift, tcg_fpst);
9197             } else {
9198                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
9199                                      tcg_shift, tcg_fpst);
9200             }
9201             if (elements == 1) {
9202                 write_fp_dreg(s, rd, tcg_double);
9203             } else {
9204                 write_vec_element(s, tcg_double, rd, pass, MO_64);
9205             }
9206         }
9207     } else {
9208         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
9209         TCGv_i32 tcg_float = tcg_temp_new_i32();
9210 
9211         for (pass = 0; pass < elements; pass++) {
9212             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
9213 
9214             switch (size) {
9215             case MO_32:
9216                 if (fracbits) {
9217                     if (is_signed) {
9218                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
9219                                              tcg_shift, tcg_fpst);
9220                     } else {
9221                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
9222                                              tcg_shift, tcg_fpst);
9223                     }
9224                 } else {
9225                     if (is_signed) {
9226                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
9227                     } else {
9228                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
9229                     }
9230                 }
9231                 break;
9232             case MO_16:
9233                 if (fracbits) {
9234                     if (is_signed) {
9235                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
9236                                              tcg_shift, tcg_fpst);
9237                     } else {
9238                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
9239                                              tcg_shift, tcg_fpst);
9240                     }
9241                 } else {
9242                     if (is_signed) {
9243                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
9244                     } else {
9245                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
9246                     }
9247                 }
9248                 break;
9249             default:
9250                 g_assert_not_reached();
9251             }
9252 
9253             if (elements == 1) {
9254                 write_fp_sreg(s, rd, tcg_float);
9255             } else {
9256                 write_vec_element_i32(s, tcg_float, rd, pass, size);
9257             }
9258         }
9259     }
9260 
9261     clear_vec_high(s, elements << size == 16, rd);
9262 }
9263 
9264 /* UCVTF/SCVTF - Integer to FP conversion */
9265 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
9266                                          bool is_q, bool is_u,
9267                                          int immh, int immb, int opcode,
9268                                          int rn, int rd)
9269 {
9270     int size, elements, fracbits;
9271     int immhb = immh << 3 | immb;
9272 
9273     if (immh & 8) {
9274         size = MO_64;
9275         if (!is_scalar && !is_q) {
9276             unallocated_encoding(s);
9277             return;
9278         }
9279     } else if (immh & 4) {
9280         size = MO_32;
9281     } else if (immh & 2) {
9282         size = MO_16;
9283         if (!dc_isar_feature(aa64_fp16, s)) {
9284             unallocated_encoding(s);
9285             return;
9286         }
9287     } else {
9288         /* immh == 0 would be a failure of the decode logic */
9289         g_assert(immh == 1);
9290         unallocated_encoding(s);
9291         return;
9292     }
9293 
9294     if (is_scalar) {
9295         elements = 1;
9296     } else {
9297         elements = (8 << is_q) >> size;
9298     }
9299     fracbits = (16 << size) - immhb;
9300 
9301     if (!fp_access_check(s)) {
9302         return;
9303     }
9304 
9305     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
9306 }
9307 
9308 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
9309 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
9310                                          bool is_q, bool is_u,
9311                                          int immh, int immb, int rn, int rd)
9312 {
9313     int immhb = immh << 3 | immb;
9314     int pass, size, fracbits;
9315     TCGv_ptr tcg_fpstatus;
9316     TCGv_i32 tcg_rmode, tcg_shift;
9317 
9318     if (immh & 0x8) {
9319         size = MO_64;
9320         if (!is_scalar && !is_q) {
9321             unallocated_encoding(s);
9322             return;
9323         }
9324     } else if (immh & 0x4) {
9325         size = MO_32;
9326     } else if (immh & 0x2) {
9327         size = MO_16;
9328         if (!dc_isar_feature(aa64_fp16, s)) {
9329             unallocated_encoding(s);
9330             return;
9331         }
9332     } else {
9333         /* Should have split out AdvSIMD modified immediate earlier.  */
9334         assert(immh == 1);
9335         unallocated_encoding(s);
9336         return;
9337     }
9338 
9339     if (!fp_access_check(s)) {
9340         return;
9341     }
9342 
9343     assert(!(is_scalar && is_q));
9344 
9345     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9346     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
9347     fracbits = (16 << size) - immhb;
9348     tcg_shift = tcg_constant_i32(fracbits);
9349 
9350     if (size == MO_64) {
9351         int maxpass = is_scalar ? 1 : 2;
9352 
9353         for (pass = 0; pass < maxpass; pass++) {
9354             TCGv_i64 tcg_op = tcg_temp_new_i64();
9355 
9356             read_vec_element(s, tcg_op, rn, pass, MO_64);
9357             if (is_u) {
9358                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9359             } else {
9360                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9361             }
9362             write_vec_element(s, tcg_op, rd, pass, MO_64);
9363         }
9364         clear_vec_high(s, is_q, rd);
9365     } else {
9366         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
9367         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
9368 
9369         switch (size) {
9370         case MO_16:
9371             if (is_u) {
9372                 fn = gen_helper_vfp_touhh;
9373             } else {
9374                 fn = gen_helper_vfp_toshh;
9375             }
9376             break;
9377         case MO_32:
9378             if (is_u) {
9379                 fn = gen_helper_vfp_touls;
9380             } else {
9381                 fn = gen_helper_vfp_tosls;
9382             }
9383             break;
9384         default:
9385             g_assert_not_reached();
9386         }
9387 
9388         for (pass = 0; pass < maxpass; pass++) {
9389             TCGv_i32 tcg_op = tcg_temp_new_i32();
9390 
9391             read_vec_element_i32(s, tcg_op, rn, pass, size);
9392             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9393             if (is_scalar) {
9394                 if (size == MO_16 && !is_u) {
9395                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
9396                 }
9397                 write_fp_sreg(s, rd, tcg_op);
9398             } else {
9399                 write_vec_element_i32(s, tcg_op, rd, pass, size);
9400             }
9401         }
9402         if (!is_scalar) {
9403             clear_vec_high(s, is_q, rd);
9404         }
9405     }
9406 
9407     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
9408 }
9409 
9410 /* AdvSIMD scalar shift by immediate
9411  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
9412  * +-----+---+-------------+------+------+--------+---+------+------+
9413  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
9414  * +-----+---+-------------+------+------+--------+---+------+------+
9415  *
9416  * This is the scalar version so it works on a fixed sized registers
9417  */
9418 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9419 {
9420     int rd = extract32(insn, 0, 5);
9421     int rn = extract32(insn, 5, 5);
9422     int opcode = extract32(insn, 11, 5);
9423     int immb = extract32(insn, 16, 3);
9424     int immh = extract32(insn, 19, 4);
9425     bool is_u = extract32(insn, 29, 1);
9426 
9427     if (immh == 0) {
9428         unallocated_encoding(s);
9429         return;
9430     }
9431 
9432     switch (opcode) {
9433     case 0x08: /* SRI */
9434         if (!is_u) {
9435             unallocated_encoding(s);
9436             return;
9437         }
9438         /* fall through */
9439     case 0x00: /* SSHR / USHR */
9440     case 0x02: /* SSRA / USRA */
9441     case 0x04: /* SRSHR / URSHR */
9442     case 0x06: /* SRSRA / URSRA */
9443         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9444         break;
9445     case 0x0a: /* SHL / SLI */
9446         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9447         break;
9448     case 0x1c: /* SCVTF, UCVTF */
9449         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9450                                      opcode, rn, rd);
9451         break;
9452     case 0x10: /* SQSHRUN, SQSHRUN2 */
9453     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9454         if (!is_u) {
9455             unallocated_encoding(s);
9456             return;
9457         }
9458         handle_vec_simd_sqshrn(s, true, false, false, true,
9459                                immh, immb, opcode, rn, rd);
9460         break;
9461     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9462     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9463         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9464                                immh, immb, opcode, rn, rd);
9465         break;
9466     case 0xc: /* SQSHLU */
9467         if (!is_u) {
9468             unallocated_encoding(s);
9469             return;
9470         }
9471         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9472         break;
9473     case 0xe: /* SQSHL, UQSHL */
9474         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9475         break;
9476     case 0x1f: /* FCVTZS, FCVTZU */
9477         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9478         break;
9479     default:
9480         unallocated_encoding(s);
9481         break;
9482     }
9483 }
9484 
9485 /* AdvSIMD scalar three different
9486  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
9487  * +-----+---+-----------+------+---+------+--------+-----+------+------+
9488  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
9489  * +-----+---+-----------+------+---+------+--------+-----+------+------+
9490  */
9491 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9492 {
9493     bool is_u = extract32(insn, 29, 1);
9494     int size = extract32(insn, 22, 2);
9495     int opcode = extract32(insn, 12, 4);
9496     int rm = extract32(insn, 16, 5);
9497     int rn = extract32(insn, 5, 5);
9498     int rd = extract32(insn, 0, 5);
9499 
9500     if (is_u) {
9501         unallocated_encoding(s);
9502         return;
9503     }
9504 
9505     switch (opcode) {
9506     case 0x9: /* SQDMLAL, SQDMLAL2 */
9507     case 0xb: /* SQDMLSL, SQDMLSL2 */
9508     case 0xd: /* SQDMULL, SQDMULL2 */
9509         if (size == 0 || size == 3) {
9510             unallocated_encoding(s);
9511             return;
9512         }
9513         break;
9514     default:
9515         unallocated_encoding(s);
9516         return;
9517     }
9518 
9519     if (!fp_access_check(s)) {
9520         return;
9521     }
9522 
9523     if (size == 2) {
9524         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9525         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9526         TCGv_i64 tcg_res = tcg_temp_new_i64();
9527 
9528         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9529         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9530 
9531         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9532         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
9533 
9534         switch (opcode) {
9535         case 0xd: /* SQDMULL, SQDMULL2 */
9536             break;
9537         case 0xb: /* SQDMLSL, SQDMLSL2 */
9538             tcg_gen_neg_i64(tcg_res, tcg_res);
9539             /* fall through */
9540         case 0x9: /* SQDMLAL, SQDMLAL2 */
9541             read_vec_element(s, tcg_op1, rd, 0, MO_64);
9542             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
9543                                               tcg_res, tcg_op1);
9544             break;
9545         default:
9546             g_assert_not_reached();
9547         }
9548 
9549         write_fp_dreg(s, rd, tcg_res);
9550     } else {
9551         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9552         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9553         TCGv_i64 tcg_res = tcg_temp_new_i64();
9554 
9555         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9556         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
9557 
9558         switch (opcode) {
9559         case 0xd: /* SQDMULL, SQDMULL2 */
9560             break;
9561         case 0xb: /* SQDMLSL, SQDMLSL2 */
9562             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9563             /* fall through */
9564         case 0x9: /* SQDMLAL, SQDMLAL2 */
9565         {
9566             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9567             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9568             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
9569                                               tcg_res, tcg_op3);
9570             break;
9571         }
9572         default:
9573             g_assert_not_reached();
9574         }
9575 
9576         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9577         write_fp_dreg(s, rd, tcg_res);
9578     }
9579 }
9580 
9581 /* AdvSIMD scalar three same extra
9582  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9583  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9584  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9585  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9586  */
9587 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9588                                                    uint32_t insn)
9589 {
9590     int rd = extract32(insn, 0, 5);
9591     int rn = extract32(insn, 5, 5);
9592     int opcode = extract32(insn, 11, 4);
9593     int rm = extract32(insn, 16, 5);
9594     int size = extract32(insn, 22, 2);
9595     bool u = extract32(insn, 29, 1);
9596     TCGv_i32 ele1, ele2, ele3;
9597     TCGv_i64 res;
9598     bool feature;
9599 
9600     switch (u * 16 + opcode) {
9601     case 0x10: /* SQRDMLAH (vector) */
9602     case 0x11: /* SQRDMLSH (vector) */
9603         if (size != 1 && size != 2) {
9604             unallocated_encoding(s);
9605             return;
9606         }
9607         feature = dc_isar_feature(aa64_rdm, s);
9608         break;
9609     default:
9610         unallocated_encoding(s);
9611         return;
9612     }
9613     if (!feature) {
9614         unallocated_encoding(s);
9615         return;
9616     }
9617     if (!fp_access_check(s)) {
9618         return;
9619     }
9620 
9621     /* Do a single operation on the lowest element in the vector.
9622      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9623      * with no side effects for all these operations.
9624      * OPTME: special-purpose helpers would avoid doing some
9625      * unnecessary work in the helper for the 16 bit cases.
9626      */
9627     ele1 = tcg_temp_new_i32();
9628     ele2 = tcg_temp_new_i32();
9629     ele3 = tcg_temp_new_i32();
9630 
9631     read_vec_element_i32(s, ele1, rn, 0, size);
9632     read_vec_element_i32(s, ele2, rm, 0, size);
9633     read_vec_element_i32(s, ele3, rd, 0, size);
9634 
9635     switch (opcode) {
9636     case 0x0: /* SQRDMLAH */
9637         if (size == 1) {
9638             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9639         } else {
9640             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9641         }
9642         break;
9643     case 0x1: /* SQRDMLSH */
9644         if (size == 1) {
9645             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9646         } else {
9647             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9648         }
9649         break;
9650     default:
9651         g_assert_not_reached();
9652     }
9653 
9654     res = tcg_temp_new_i64();
9655     tcg_gen_extu_i32_i64(res, ele3);
9656     write_fp_dreg(s, rd, res);
9657 }
9658 
9659 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9660                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9661                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9662 {
9663     /* Handle 64->64 opcodes which are shared between the scalar and
9664      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9665      * is valid in either group and also the double-precision fp ops.
9666      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9667      * requires them.
9668      */
9669     TCGCond cond;
9670 
9671     switch (opcode) {
9672     case 0x4: /* CLS, CLZ */
9673         if (u) {
9674             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9675         } else {
9676             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9677         }
9678         break;
9679     case 0x5: /* NOT */
9680         /* This opcode is shared with CNT and RBIT but we have earlier
9681          * enforced that size == 3 if and only if this is the NOT insn.
9682          */
9683         tcg_gen_not_i64(tcg_rd, tcg_rn);
9684         break;
9685     case 0x7: /* SQABS, SQNEG */
9686         if (u) {
9687             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9688         } else {
9689             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9690         }
9691         break;
9692     case 0xa: /* CMLT */
9693         cond = TCG_COND_LT;
9694     do_cmop:
9695         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9696         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9697         break;
9698     case 0x8: /* CMGT, CMGE */
9699         cond = u ? TCG_COND_GE : TCG_COND_GT;
9700         goto do_cmop;
9701     case 0x9: /* CMEQ, CMLE */
9702         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9703         goto do_cmop;
9704     case 0xb: /* ABS, NEG */
9705         if (u) {
9706             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9707         } else {
9708             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9709         }
9710         break;
9711     case 0x2f: /* FABS */
9712         gen_vfp_absd(tcg_rd, tcg_rn);
9713         break;
9714     case 0x6f: /* FNEG */
9715         gen_vfp_negd(tcg_rd, tcg_rn);
9716         break;
9717     case 0x7f: /* FSQRT */
9718         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9719         break;
9720     case 0x1a: /* FCVTNS */
9721     case 0x1b: /* FCVTMS */
9722     case 0x1c: /* FCVTAS */
9723     case 0x3a: /* FCVTPS */
9724     case 0x3b: /* FCVTZS */
9725         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9726         break;
9727     case 0x5a: /* FCVTNU */
9728     case 0x5b: /* FCVTMU */
9729     case 0x5c: /* FCVTAU */
9730     case 0x7a: /* FCVTPU */
9731     case 0x7b: /* FCVTZU */
9732         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9733         break;
9734     case 0x18: /* FRINTN */
9735     case 0x19: /* FRINTM */
9736     case 0x38: /* FRINTP */
9737     case 0x39: /* FRINTZ */
9738     case 0x58: /* FRINTA */
9739     case 0x79: /* FRINTI */
9740         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9741         break;
9742     case 0x59: /* FRINTX */
9743         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9744         break;
9745     case 0x1e: /* FRINT32Z */
9746     case 0x5e: /* FRINT32X */
9747         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9748         break;
9749     case 0x1f: /* FRINT64Z */
9750     case 0x5f: /* FRINT64X */
9751         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9752         break;
9753     default:
9754         g_assert_not_reached();
9755     }
9756 }
9757 
9758 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9759                                    bool is_scalar, bool is_u, bool is_q,
9760                                    int size, int rn, int rd)
9761 {
9762     bool is_double = (size == MO_64);
9763     TCGv_ptr fpst;
9764 
9765     if (!fp_access_check(s)) {
9766         return;
9767     }
9768 
9769     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9770 
9771     if (is_double) {
9772         TCGv_i64 tcg_op = tcg_temp_new_i64();
9773         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9774         TCGv_i64 tcg_res = tcg_temp_new_i64();
9775         NeonGenTwoDoubleOpFn *genfn;
9776         bool swap = false;
9777         int pass;
9778 
9779         switch (opcode) {
9780         case 0x2e: /* FCMLT (zero) */
9781             swap = true;
9782             /* fallthrough */
9783         case 0x2c: /* FCMGT (zero) */
9784             genfn = gen_helper_neon_cgt_f64;
9785             break;
9786         case 0x2d: /* FCMEQ (zero) */
9787             genfn = gen_helper_neon_ceq_f64;
9788             break;
9789         case 0x6d: /* FCMLE (zero) */
9790             swap = true;
9791             /* fall through */
9792         case 0x6c: /* FCMGE (zero) */
9793             genfn = gen_helper_neon_cge_f64;
9794             break;
9795         default:
9796             g_assert_not_reached();
9797         }
9798 
9799         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9800             read_vec_element(s, tcg_op, rn, pass, MO_64);
9801             if (swap) {
9802                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9803             } else {
9804                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9805             }
9806             write_vec_element(s, tcg_res, rd, pass, MO_64);
9807         }
9808 
9809         clear_vec_high(s, !is_scalar, rd);
9810     } else {
9811         TCGv_i32 tcg_op = tcg_temp_new_i32();
9812         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9813         TCGv_i32 tcg_res = tcg_temp_new_i32();
9814         NeonGenTwoSingleOpFn *genfn;
9815         bool swap = false;
9816         int pass, maxpasses;
9817 
9818         if (size == MO_16) {
9819             switch (opcode) {
9820             case 0x2e: /* FCMLT (zero) */
9821                 swap = true;
9822                 /* fall through */
9823             case 0x2c: /* FCMGT (zero) */
9824                 genfn = gen_helper_advsimd_cgt_f16;
9825                 break;
9826             case 0x2d: /* FCMEQ (zero) */
9827                 genfn = gen_helper_advsimd_ceq_f16;
9828                 break;
9829             case 0x6d: /* FCMLE (zero) */
9830                 swap = true;
9831                 /* fall through */
9832             case 0x6c: /* FCMGE (zero) */
9833                 genfn = gen_helper_advsimd_cge_f16;
9834                 break;
9835             default:
9836                 g_assert_not_reached();
9837             }
9838         } else {
9839             switch (opcode) {
9840             case 0x2e: /* FCMLT (zero) */
9841                 swap = true;
9842                 /* fall through */
9843             case 0x2c: /* FCMGT (zero) */
9844                 genfn = gen_helper_neon_cgt_f32;
9845                 break;
9846             case 0x2d: /* FCMEQ (zero) */
9847                 genfn = gen_helper_neon_ceq_f32;
9848                 break;
9849             case 0x6d: /* FCMLE (zero) */
9850                 swap = true;
9851                 /* fall through */
9852             case 0x6c: /* FCMGE (zero) */
9853                 genfn = gen_helper_neon_cge_f32;
9854                 break;
9855             default:
9856                 g_assert_not_reached();
9857             }
9858         }
9859 
9860         if (is_scalar) {
9861             maxpasses = 1;
9862         } else {
9863             int vector_size = 8 << is_q;
9864             maxpasses = vector_size >> size;
9865         }
9866 
9867         for (pass = 0; pass < maxpasses; pass++) {
9868             read_vec_element_i32(s, tcg_op, rn, pass, size);
9869             if (swap) {
9870                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9871             } else {
9872                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9873             }
9874             if (is_scalar) {
9875                 write_fp_sreg(s, rd, tcg_res);
9876             } else {
9877                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9878             }
9879         }
9880 
9881         if (!is_scalar) {
9882             clear_vec_high(s, is_q, rd);
9883         }
9884     }
9885 }
9886 
9887 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9888                                     bool is_scalar, bool is_u, bool is_q,
9889                                     int size, int rn, int rd)
9890 {
9891     bool is_double = (size == 3);
9892     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9893 
9894     if (is_double) {
9895         TCGv_i64 tcg_op = tcg_temp_new_i64();
9896         TCGv_i64 tcg_res = tcg_temp_new_i64();
9897         int pass;
9898 
9899         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9900             read_vec_element(s, tcg_op, rn, pass, MO_64);
9901             switch (opcode) {
9902             case 0x3d: /* FRECPE */
9903                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9904                 break;
9905             case 0x3f: /* FRECPX */
9906                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9907                 break;
9908             case 0x7d: /* FRSQRTE */
9909                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9910                 break;
9911             default:
9912                 g_assert_not_reached();
9913             }
9914             write_vec_element(s, tcg_res, rd, pass, MO_64);
9915         }
9916         clear_vec_high(s, !is_scalar, rd);
9917     } else {
9918         TCGv_i32 tcg_op = tcg_temp_new_i32();
9919         TCGv_i32 tcg_res = tcg_temp_new_i32();
9920         int pass, maxpasses;
9921 
9922         if (is_scalar) {
9923             maxpasses = 1;
9924         } else {
9925             maxpasses = is_q ? 4 : 2;
9926         }
9927 
9928         for (pass = 0; pass < maxpasses; pass++) {
9929             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9930 
9931             switch (opcode) {
9932             case 0x3c: /* URECPE */
9933                 gen_helper_recpe_u32(tcg_res, tcg_op);
9934                 break;
9935             case 0x3d: /* FRECPE */
9936                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9937                 break;
9938             case 0x3f: /* FRECPX */
9939                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9940                 break;
9941             case 0x7d: /* FRSQRTE */
9942                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9943                 break;
9944             default:
9945                 g_assert_not_reached();
9946             }
9947 
9948             if (is_scalar) {
9949                 write_fp_sreg(s, rd, tcg_res);
9950             } else {
9951                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9952             }
9953         }
9954         if (!is_scalar) {
9955             clear_vec_high(s, is_q, rd);
9956         }
9957     }
9958 }
9959 
9960 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9961                                 int opcode, bool u, bool is_q,
9962                                 int size, int rn, int rd)
9963 {
9964     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9965      * in the source becomes a size element in the destination).
9966      */
9967     int pass;
9968     TCGv_i32 tcg_res[2];
9969     int destelt = is_q ? 2 : 0;
9970     int passes = scalar ? 1 : 2;
9971 
9972     if (scalar) {
9973         tcg_res[1] = tcg_constant_i32(0);
9974     }
9975 
9976     for (pass = 0; pass < passes; pass++) {
9977         TCGv_i64 tcg_op = tcg_temp_new_i64();
9978         NeonGenNarrowFn *genfn = NULL;
9979         NeonGenNarrowEnvFn *genenvfn = NULL;
9980 
9981         if (scalar) {
9982             read_vec_element(s, tcg_op, rn, pass, size + 1);
9983         } else {
9984             read_vec_element(s, tcg_op, rn, pass, MO_64);
9985         }
9986         tcg_res[pass] = tcg_temp_new_i32();
9987 
9988         switch (opcode) {
9989         case 0x12: /* XTN, SQXTUN */
9990         {
9991             static NeonGenNarrowFn * const xtnfns[3] = {
9992                 gen_helper_neon_narrow_u8,
9993                 gen_helper_neon_narrow_u16,
9994                 tcg_gen_extrl_i64_i32,
9995             };
9996             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9997                 gen_helper_neon_unarrow_sat8,
9998                 gen_helper_neon_unarrow_sat16,
9999                 gen_helper_neon_unarrow_sat32,
10000             };
10001             if (u) {
10002                 genenvfn = sqxtunfns[size];
10003             } else {
10004                 genfn = xtnfns[size];
10005             }
10006             break;
10007         }
10008         case 0x14: /* SQXTN, UQXTN */
10009         {
10010             static NeonGenNarrowEnvFn * const fns[3][2] = {
10011                 { gen_helper_neon_narrow_sat_s8,
10012                   gen_helper_neon_narrow_sat_u8 },
10013                 { gen_helper_neon_narrow_sat_s16,
10014                   gen_helper_neon_narrow_sat_u16 },
10015                 { gen_helper_neon_narrow_sat_s32,
10016                   gen_helper_neon_narrow_sat_u32 },
10017             };
10018             genenvfn = fns[size][u];
10019             break;
10020         }
10021         case 0x16: /* FCVTN, FCVTN2 */
10022             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
10023             if (size == 2) {
10024                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
10025             } else {
10026                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
10027                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
10028                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10029                 TCGv_i32 ahp = get_ahp_flag();
10030 
10031                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10032                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10033                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10034                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10035             }
10036             break;
10037         case 0x36: /* BFCVTN, BFCVTN2 */
10038             {
10039                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10040                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
10041             }
10042             break;
10043         case 0x56:  /* FCVTXN, FCVTXN2 */
10044             /* 64 bit to 32 bit float conversion
10045              * with von Neumann rounding (round to odd)
10046              */
10047             assert(size == 2);
10048             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
10049             break;
10050         default:
10051             g_assert_not_reached();
10052         }
10053 
10054         if (genfn) {
10055             genfn(tcg_res[pass], tcg_op);
10056         } else if (genenvfn) {
10057             genenvfn(tcg_res[pass], tcg_env, tcg_op);
10058         }
10059     }
10060 
10061     for (pass = 0; pass < 2; pass++) {
10062         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10063     }
10064     clear_vec_high(s, is_q, rd);
10065 }
10066 
10067 /* AdvSIMD scalar two reg misc
10068  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10069  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10070  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10071  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10072  */
10073 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10074 {
10075     int rd = extract32(insn, 0, 5);
10076     int rn = extract32(insn, 5, 5);
10077     int opcode = extract32(insn, 12, 5);
10078     int size = extract32(insn, 22, 2);
10079     bool u = extract32(insn, 29, 1);
10080     bool is_fcvt = false;
10081     int rmode;
10082     TCGv_i32 tcg_rmode;
10083     TCGv_ptr tcg_fpstatus;
10084 
10085     switch (opcode) {
10086     case 0x7: /* SQABS / SQNEG */
10087         break;
10088     case 0xa: /* CMLT */
10089         if (u) {
10090             unallocated_encoding(s);
10091             return;
10092         }
10093         /* fall through */
10094     case 0x8: /* CMGT, CMGE */
10095     case 0x9: /* CMEQ, CMLE */
10096     case 0xb: /* ABS, NEG */
10097         if (size != 3) {
10098             unallocated_encoding(s);
10099             return;
10100         }
10101         break;
10102     case 0x12: /* SQXTUN */
10103         if (!u) {
10104             unallocated_encoding(s);
10105             return;
10106         }
10107         /* fall through */
10108     case 0x14: /* SQXTN, UQXTN */
10109         if (size == 3) {
10110             unallocated_encoding(s);
10111             return;
10112         }
10113         if (!fp_access_check(s)) {
10114             return;
10115         }
10116         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10117         return;
10118     case 0xc ... 0xf:
10119     case 0x16 ... 0x1d:
10120     case 0x1f:
10121         /* Floating point: U, size[1] and opcode indicate operation;
10122          * size[0] indicates single or double precision.
10123          */
10124         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10125         size = extract32(size, 0, 1) ? 3 : 2;
10126         switch (opcode) {
10127         case 0x2c: /* FCMGT (zero) */
10128         case 0x2d: /* FCMEQ (zero) */
10129         case 0x2e: /* FCMLT (zero) */
10130         case 0x6c: /* FCMGE (zero) */
10131         case 0x6d: /* FCMLE (zero) */
10132             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10133             return;
10134         case 0x1d: /* SCVTF */
10135         case 0x5d: /* UCVTF */
10136         {
10137             bool is_signed = (opcode == 0x1d);
10138             if (!fp_access_check(s)) {
10139                 return;
10140             }
10141             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10142             return;
10143         }
10144         case 0x3d: /* FRECPE */
10145         case 0x3f: /* FRECPX */
10146         case 0x7d: /* FRSQRTE */
10147             if (!fp_access_check(s)) {
10148                 return;
10149             }
10150             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10151             return;
10152         case 0x1a: /* FCVTNS */
10153         case 0x1b: /* FCVTMS */
10154         case 0x3a: /* FCVTPS */
10155         case 0x3b: /* FCVTZS */
10156         case 0x5a: /* FCVTNU */
10157         case 0x5b: /* FCVTMU */
10158         case 0x7a: /* FCVTPU */
10159         case 0x7b: /* FCVTZU */
10160             is_fcvt = true;
10161             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10162             break;
10163         case 0x1c: /* FCVTAS */
10164         case 0x5c: /* FCVTAU */
10165             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10166             is_fcvt = true;
10167             rmode = FPROUNDING_TIEAWAY;
10168             break;
10169         case 0x56: /* FCVTXN, FCVTXN2 */
10170             if (size == 2) {
10171                 unallocated_encoding(s);
10172                 return;
10173             }
10174             if (!fp_access_check(s)) {
10175                 return;
10176             }
10177             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10178             return;
10179         default:
10180             unallocated_encoding(s);
10181             return;
10182         }
10183         break;
10184     default:
10185     case 0x3: /* USQADD / SUQADD */
10186         unallocated_encoding(s);
10187         return;
10188     }
10189 
10190     if (!fp_access_check(s)) {
10191         return;
10192     }
10193 
10194     if (is_fcvt) {
10195         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10196         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10197     } else {
10198         tcg_fpstatus = NULL;
10199         tcg_rmode = NULL;
10200     }
10201 
10202     if (size == 3) {
10203         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10204         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10205 
10206         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10207         write_fp_dreg(s, rd, tcg_rd);
10208     } else {
10209         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10210         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10211 
10212         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10213 
10214         switch (opcode) {
10215         case 0x7: /* SQABS, SQNEG */
10216         {
10217             NeonGenOneOpEnvFn *genfn;
10218             static NeonGenOneOpEnvFn * const fns[3][2] = {
10219                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10220                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10221                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10222             };
10223             genfn = fns[size][u];
10224             genfn(tcg_rd, tcg_env, tcg_rn);
10225             break;
10226         }
10227         case 0x1a: /* FCVTNS */
10228         case 0x1b: /* FCVTMS */
10229         case 0x1c: /* FCVTAS */
10230         case 0x3a: /* FCVTPS */
10231         case 0x3b: /* FCVTZS */
10232             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10233                                  tcg_fpstatus);
10234             break;
10235         case 0x5a: /* FCVTNU */
10236         case 0x5b: /* FCVTMU */
10237         case 0x5c: /* FCVTAU */
10238         case 0x7a: /* FCVTPU */
10239         case 0x7b: /* FCVTZU */
10240             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10241                                  tcg_fpstatus);
10242             break;
10243         default:
10244             g_assert_not_reached();
10245         }
10246 
10247         write_fp_sreg(s, rd, tcg_rd);
10248     }
10249 
10250     if (is_fcvt) {
10251         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10252     }
10253 }
10254 
10255 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10256 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10257                                  int immh, int immb, int opcode, int rn, int rd)
10258 {
10259     int size = 32 - clz32(immh) - 1;
10260     int immhb = immh << 3 | immb;
10261     int shift = 2 * (8 << size) - immhb;
10262     GVecGen2iFn *gvec_fn;
10263 
10264     if (extract32(immh, 3, 1) && !is_q) {
10265         unallocated_encoding(s);
10266         return;
10267     }
10268     tcg_debug_assert(size <= 3);
10269 
10270     if (!fp_access_check(s)) {
10271         return;
10272     }
10273 
10274     switch (opcode) {
10275     case 0x02: /* SSRA / USRA (accumulate) */
10276         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10277         break;
10278 
10279     case 0x08: /* SRI */
10280         gvec_fn = gen_gvec_sri;
10281         break;
10282 
10283     case 0x00: /* SSHR / USHR */
10284         if (is_u) {
10285             if (shift == 8 << size) {
10286                 /* Shift count the same size as element size produces zero.  */
10287                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10288                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10289                 return;
10290             }
10291             gvec_fn = tcg_gen_gvec_shri;
10292         } else {
10293             /* Shift count the same size as element size produces all sign.  */
10294             if (shift == 8 << size) {
10295                 shift -= 1;
10296             }
10297             gvec_fn = tcg_gen_gvec_sari;
10298         }
10299         break;
10300 
10301     case 0x04: /* SRSHR / URSHR (rounding) */
10302         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10303         break;
10304 
10305     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10306         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10307         break;
10308 
10309     default:
10310         g_assert_not_reached();
10311     }
10312 
10313     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10314 }
10315 
10316 /* SHL/SLI - Vector shift left */
10317 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10318                                  int immh, int immb, int opcode, int rn, int rd)
10319 {
10320     int size = 32 - clz32(immh) - 1;
10321     int immhb = immh << 3 | immb;
10322     int shift = immhb - (8 << size);
10323 
10324     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10325     assert(size >= 0 && size <= 3);
10326 
10327     if (extract32(immh, 3, 1) && !is_q) {
10328         unallocated_encoding(s);
10329         return;
10330     }
10331 
10332     if (!fp_access_check(s)) {
10333         return;
10334     }
10335 
10336     if (insert) {
10337         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10338     } else {
10339         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10340     }
10341 }
10342 
10343 /* USHLL/SHLL - Vector shift left with widening */
10344 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10345                                  int immh, int immb, int opcode, int rn, int rd)
10346 {
10347     int size = 32 - clz32(immh) - 1;
10348     int immhb = immh << 3 | immb;
10349     int shift = immhb - (8 << size);
10350     int dsize = 64;
10351     int esize = 8 << size;
10352     int elements = dsize/esize;
10353     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10354     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10355     int i;
10356 
10357     if (size >= 3) {
10358         unallocated_encoding(s);
10359         return;
10360     }
10361 
10362     if (!fp_access_check(s)) {
10363         return;
10364     }
10365 
10366     /* For the LL variants the store is larger than the load,
10367      * so if rd == rn we would overwrite parts of our input.
10368      * So load everything right now and use shifts in the main loop.
10369      */
10370     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10371 
10372     for (i = 0; i < elements; i++) {
10373         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10374         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10375         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10376         write_vec_element(s, tcg_rd, rd, i, size + 1);
10377     }
10378 }
10379 
10380 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10381 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10382                                  int immh, int immb, int opcode, int rn, int rd)
10383 {
10384     int immhb = immh << 3 | immb;
10385     int size = 32 - clz32(immh) - 1;
10386     int dsize = 64;
10387     int esize = 8 << size;
10388     int elements = dsize/esize;
10389     int shift = (2 * esize) - immhb;
10390     bool round = extract32(opcode, 0, 1);
10391     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10392     TCGv_i64 tcg_round;
10393     int i;
10394 
10395     if (extract32(immh, 3, 1)) {
10396         unallocated_encoding(s);
10397         return;
10398     }
10399 
10400     if (!fp_access_check(s)) {
10401         return;
10402     }
10403 
10404     tcg_rn = tcg_temp_new_i64();
10405     tcg_rd = tcg_temp_new_i64();
10406     tcg_final = tcg_temp_new_i64();
10407     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10408 
10409     if (round) {
10410         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10411     } else {
10412         tcg_round = NULL;
10413     }
10414 
10415     for (i = 0; i < elements; i++) {
10416         read_vec_element(s, tcg_rn, rn, i, size+1);
10417         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10418                                 false, true, size+1, shift);
10419 
10420         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10421     }
10422 
10423     if (!is_q) {
10424         write_vec_element(s, tcg_final, rd, 0, MO_64);
10425     } else {
10426         write_vec_element(s, tcg_final, rd, 1, MO_64);
10427     }
10428 
10429     clear_vec_high(s, is_q, rd);
10430 }
10431 
10432 
10433 /* AdvSIMD shift by immediate
10434  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10435  * +---+---+---+-------------+------+------+--------+---+------+------+
10436  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10437  * +---+---+---+-------------+------+------+--------+---+------+------+
10438  */
10439 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10440 {
10441     int rd = extract32(insn, 0, 5);
10442     int rn = extract32(insn, 5, 5);
10443     int opcode = extract32(insn, 11, 5);
10444     int immb = extract32(insn, 16, 3);
10445     int immh = extract32(insn, 19, 4);
10446     bool is_u = extract32(insn, 29, 1);
10447     bool is_q = extract32(insn, 30, 1);
10448 
10449     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10450     assert(immh != 0);
10451 
10452     switch (opcode) {
10453     case 0x08: /* SRI */
10454         if (!is_u) {
10455             unallocated_encoding(s);
10456             return;
10457         }
10458         /* fall through */
10459     case 0x00: /* SSHR / USHR */
10460     case 0x02: /* SSRA / USRA (accumulate) */
10461     case 0x04: /* SRSHR / URSHR (rounding) */
10462     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10463         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10464         break;
10465     case 0x0a: /* SHL / SLI */
10466         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10467         break;
10468     case 0x10: /* SHRN */
10469     case 0x11: /* RSHRN / SQRSHRUN */
10470         if (is_u) {
10471             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10472                                    opcode, rn, rd);
10473         } else {
10474             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10475         }
10476         break;
10477     case 0x12: /* SQSHRN / UQSHRN */
10478     case 0x13: /* SQRSHRN / UQRSHRN */
10479         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10480                                opcode, rn, rd);
10481         break;
10482     case 0x14: /* SSHLL / USHLL */
10483         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10484         break;
10485     case 0x1c: /* SCVTF / UCVTF */
10486         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10487                                      opcode, rn, rd);
10488         break;
10489     case 0xc: /* SQSHLU */
10490         if (!is_u) {
10491             unallocated_encoding(s);
10492             return;
10493         }
10494         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10495         break;
10496     case 0xe: /* SQSHL, UQSHL */
10497         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10498         break;
10499     case 0x1f: /* FCVTZS/ FCVTZU */
10500         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10501         return;
10502     default:
10503         unallocated_encoding(s);
10504         return;
10505     }
10506 }
10507 
10508 /* Generate code to do a "long" addition or subtraction, ie one done in
10509  * TCGv_i64 on vector lanes twice the width specified by size.
10510  */
10511 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10512                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10513 {
10514     static NeonGenTwo64OpFn * const fns[3][2] = {
10515         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10516         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10517         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10518     };
10519     NeonGenTwo64OpFn *genfn;
10520     assert(size < 3);
10521 
10522     genfn = fns[size][is_sub];
10523     genfn(tcg_res, tcg_op1, tcg_op2);
10524 }
10525 
10526 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10527                                 int opcode, int rd, int rn, int rm)
10528 {
10529     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10530     TCGv_i64 tcg_res[2];
10531     int pass, accop;
10532 
10533     tcg_res[0] = tcg_temp_new_i64();
10534     tcg_res[1] = tcg_temp_new_i64();
10535 
10536     /* Does this op do an adding accumulate, a subtracting accumulate,
10537      * or no accumulate at all?
10538      */
10539     switch (opcode) {
10540     case 5:
10541     case 8:
10542     case 9:
10543         accop = 1;
10544         break;
10545     case 10:
10546     case 11:
10547         accop = -1;
10548         break;
10549     default:
10550         accop = 0;
10551         break;
10552     }
10553 
10554     if (accop != 0) {
10555         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10556         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10557     }
10558 
10559     /* size == 2 means two 32x32->64 operations; this is worth special
10560      * casing because we can generally handle it inline.
10561      */
10562     if (size == 2) {
10563         for (pass = 0; pass < 2; pass++) {
10564             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10565             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10566             TCGv_i64 tcg_passres;
10567             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10568 
10569             int elt = pass + is_q * 2;
10570 
10571             read_vec_element(s, tcg_op1, rn, elt, memop);
10572             read_vec_element(s, tcg_op2, rm, elt, memop);
10573 
10574             if (accop == 0) {
10575                 tcg_passres = tcg_res[pass];
10576             } else {
10577                 tcg_passres = tcg_temp_new_i64();
10578             }
10579 
10580             switch (opcode) {
10581             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10582                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10583                 break;
10584             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10585                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10586                 break;
10587             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10588             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10589             {
10590                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10591                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10592 
10593                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10594                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10595                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10596                                     tcg_passres,
10597                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10598                 break;
10599             }
10600             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10601             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10602             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10603                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10604                 break;
10605             case 9: /* SQDMLAL, SQDMLAL2 */
10606             case 11: /* SQDMLSL, SQDMLSL2 */
10607             case 13: /* SQDMULL, SQDMULL2 */
10608                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10609                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10610                                                   tcg_passres, tcg_passres);
10611                 break;
10612             default:
10613                 g_assert_not_reached();
10614             }
10615 
10616             if (opcode == 9 || opcode == 11) {
10617                 /* saturating accumulate ops */
10618                 if (accop < 0) {
10619                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10620                 }
10621                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10622                                                   tcg_res[pass], tcg_passres);
10623             } else if (accop > 0) {
10624                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10625             } else if (accop < 0) {
10626                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10627             }
10628         }
10629     } else {
10630         /* size 0 or 1, generally helper functions */
10631         for (pass = 0; pass < 2; pass++) {
10632             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10633             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10634             TCGv_i64 tcg_passres;
10635             int elt = pass + is_q * 2;
10636 
10637             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10638             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10639 
10640             if (accop == 0) {
10641                 tcg_passres = tcg_res[pass];
10642             } else {
10643                 tcg_passres = tcg_temp_new_i64();
10644             }
10645 
10646             switch (opcode) {
10647             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10648             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10649             {
10650                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10651                 static NeonGenWidenFn * const widenfns[2][2] = {
10652                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10653                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10654                 };
10655                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10656 
10657                 widenfn(tcg_op2_64, tcg_op2);
10658                 widenfn(tcg_passres, tcg_op1);
10659                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10660                               tcg_passres, tcg_op2_64);
10661                 break;
10662             }
10663             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10664             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10665                 if (size == 0) {
10666                     if (is_u) {
10667                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10668                     } else {
10669                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10670                     }
10671                 } else {
10672                     if (is_u) {
10673                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10674                     } else {
10675                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10676                     }
10677                 }
10678                 break;
10679             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10680             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10681             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10682                 if (size == 0) {
10683                     if (is_u) {
10684                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10685                     } else {
10686                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10687                     }
10688                 } else {
10689                     if (is_u) {
10690                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10691                     } else {
10692                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10693                     }
10694                 }
10695                 break;
10696             case 9: /* SQDMLAL, SQDMLAL2 */
10697             case 11: /* SQDMLSL, SQDMLSL2 */
10698             case 13: /* SQDMULL, SQDMULL2 */
10699                 assert(size == 1);
10700                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10701                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10702                                                   tcg_passres, tcg_passres);
10703                 break;
10704             default:
10705                 g_assert_not_reached();
10706             }
10707 
10708             if (accop != 0) {
10709                 if (opcode == 9 || opcode == 11) {
10710                     /* saturating accumulate ops */
10711                     if (accop < 0) {
10712                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10713                     }
10714                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10715                                                       tcg_res[pass],
10716                                                       tcg_passres);
10717                 } else {
10718                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10719                                   tcg_res[pass], tcg_passres);
10720                 }
10721             }
10722         }
10723     }
10724 
10725     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10726     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10727 }
10728 
10729 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10730                             int opcode, int rd, int rn, int rm)
10731 {
10732     TCGv_i64 tcg_res[2];
10733     int part = is_q ? 2 : 0;
10734     int pass;
10735 
10736     for (pass = 0; pass < 2; pass++) {
10737         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10738         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10739         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10740         static NeonGenWidenFn * const widenfns[3][2] = {
10741             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10742             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10743             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10744         };
10745         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10746 
10747         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10748         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10749         widenfn(tcg_op2_wide, tcg_op2);
10750         tcg_res[pass] = tcg_temp_new_i64();
10751         gen_neon_addl(size, (opcode == 3),
10752                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10753     }
10754 
10755     for (pass = 0; pass < 2; pass++) {
10756         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10757     }
10758 }
10759 
10760 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10761 {
10762     tcg_gen_addi_i64(in, in, 1U << 31);
10763     tcg_gen_extrh_i64_i32(res, in);
10764 }
10765 
10766 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10767                                  int opcode, int rd, int rn, int rm)
10768 {
10769     TCGv_i32 tcg_res[2];
10770     int part = is_q ? 2 : 0;
10771     int pass;
10772 
10773     for (pass = 0; pass < 2; pass++) {
10774         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10775         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10776         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10777         static NeonGenNarrowFn * const narrowfns[3][2] = {
10778             { gen_helper_neon_narrow_high_u8,
10779               gen_helper_neon_narrow_round_high_u8 },
10780             { gen_helper_neon_narrow_high_u16,
10781               gen_helper_neon_narrow_round_high_u16 },
10782             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10783         };
10784         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10785 
10786         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10787         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10788 
10789         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10790 
10791         tcg_res[pass] = tcg_temp_new_i32();
10792         gennarrow(tcg_res[pass], tcg_wideres);
10793     }
10794 
10795     for (pass = 0; pass < 2; pass++) {
10796         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10797     }
10798     clear_vec_high(s, is_q, rd);
10799 }
10800 
10801 /* AdvSIMD three different
10802  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10803  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10804  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10805  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10806  */
10807 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10808 {
10809     /* Instructions in this group fall into three basic classes
10810      * (in each case with the operation working on each element in
10811      * the input vectors):
10812      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10813      *     128 bit input)
10814      * (2) wide 64 x 128 -> 128
10815      * (3) narrowing 128 x 128 -> 64
10816      * Here we do initial decode, catch unallocated cases and
10817      * dispatch to separate functions for each class.
10818      */
10819     int is_q = extract32(insn, 30, 1);
10820     int is_u = extract32(insn, 29, 1);
10821     int size = extract32(insn, 22, 2);
10822     int opcode = extract32(insn, 12, 4);
10823     int rm = extract32(insn, 16, 5);
10824     int rn = extract32(insn, 5, 5);
10825     int rd = extract32(insn, 0, 5);
10826 
10827     switch (opcode) {
10828     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10829     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10830         /* 64 x 128 -> 128 */
10831         if (size == 3) {
10832             unallocated_encoding(s);
10833             return;
10834         }
10835         if (!fp_access_check(s)) {
10836             return;
10837         }
10838         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10839         break;
10840     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10841     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10842         /* 128 x 128 -> 64 */
10843         if (size == 3) {
10844             unallocated_encoding(s);
10845             return;
10846         }
10847         if (!fp_access_check(s)) {
10848             return;
10849         }
10850         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10851         break;
10852     case 14: /* PMULL, PMULL2 */
10853         if (is_u) {
10854             unallocated_encoding(s);
10855             return;
10856         }
10857         switch (size) {
10858         case 0: /* PMULL.P8 */
10859             if (!fp_access_check(s)) {
10860                 return;
10861             }
10862             /* The Q field specifies lo/hi half input for this insn.  */
10863             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10864                              gen_helper_neon_pmull_h);
10865             break;
10866 
10867         case 3: /* PMULL.P64 */
10868             if (!dc_isar_feature(aa64_pmull, s)) {
10869                 unallocated_encoding(s);
10870                 return;
10871             }
10872             if (!fp_access_check(s)) {
10873                 return;
10874             }
10875             /* The Q field specifies lo/hi half input for this insn.  */
10876             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10877                              gen_helper_gvec_pmull_q);
10878             break;
10879 
10880         default:
10881             unallocated_encoding(s);
10882             break;
10883         }
10884         return;
10885     case 9: /* SQDMLAL, SQDMLAL2 */
10886     case 11: /* SQDMLSL, SQDMLSL2 */
10887     case 13: /* SQDMULL, SQDMULL2 */
10888         if (is_u || size == 0) {
10889             unallocated_encoding(s);
10890             return;
10891         }
10892         /* fall through */
10893     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10894     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10895     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10896     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10897     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10898     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10899     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10900         /* 64 x 64 -> 128 */
10901         if (size == 3) {
10902             unallocated_encoding(s);
10903             return;
10904         }
10905         if (!fp_access_check(s)) {
10906             return;
10907         }
10908 
10909         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10910         break;
10911     default:
10912         /* opcode 15 not allocated */
10913         unallocated_encoding(s);
10914         break;
10915     }
10916 }
10917 
10918 /* AdvSIMD three same extra
10919  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
10920  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
10921  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
10922  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
10923  */
10924 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
10925 {
10926     int rd = extract32(insn, 0, 5);
10927     int rn = extract32(insn, 5, 5);
10928     int opcode = extract32(insn, 11, 4);
10929     int rm = extract32(insn, 16, 5);
10930     int size = extract32(insn, 22, 2);
10931     bool u = extract32(insn, 29, 1);
10932     bool is_q = extract32(insn, 30, 1);
10933     bool feature;
10934     int rot;
10935 
10936     switch (u * 16 + opcode) {
10937     case 0x10: /* SQRDMLAH (vector) */
10938     case 0x11: /* SQRDMLSH (vector) */
10939         if (size != 1 && size != 2) {
10940             unallocated_encoding(s);
10941             return;
10942         }
10943         feature = dc_isar_feature(aa64_rdm, s);
10944         break;
10945     case 0x02: /* SDOT (vector) */
10946     case 0x12: /* UDOT (vector) */
10947         if (size != MO_32) {
10948             unallocated_encoding(s);
10949             return;
10950         }
10951         feature = dc_isar_feature(aa64_dp, s);
10952         break;
10953     case 0x03: /* USDOT */
10954         if (size != MO_32) {
10955             unallocated_encoding(s);
10956             return;
10957         }
10958         feature = dc_isar_feature(aa64_i8mm, s);
10959         break;
10960     case 0x04: /* SMMLA */
10961     case 0x14: /* UMMLA */
10962     case 0x05: /* USMMLA */
10963         if (!is_q || size != MO_32) {
10964             unallocated_encoding(s);
10965             return;
10966         }
10967         feature = dc_isar_feature(aa64_i8mm, s);
10968         break;
10969     case 0x18: /* FCMLA, #0 */
10970     case 0x19: /* FCMLA, #90 */
10971     case 0x1a: /* FCMLA, #180 */
10972     case 0x1b: /* FCMLA, #270 */
10973     case 0x1c: /* FCADD, #90 */
10974     case 0x1e: /* FCADD, #270 */
10975         if (size == 0
10976             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
10977             || (size == 3 && !is_q)) {
10978             unallocated_encoding(s);
10979             return;
10980         }
10981         feature = dc_isar_feature(aa64_fcma, s);
10982         break;
10983     case 0x1d: /* BFMMLA */
10984         if (size != MO_16 || !is_q) {
10985             unallocated_encoding(s);
10986             return;
10987         }
10988         feature = dc_isar_feature(aa64_bf16, s);
10989         break;
10990     case 0x1f:
10991         switch (size) {
10992         case 1: /* BFDOT */
10993         case 3: /* BFMLAL{B,T} */
10994             feature = dc_isar_feature(aa64_bf16, s);
10995             break;
10996         default:
10997             unallocated_encoding(s);
10998             return;
10999         }
11000         break;
11001     default:
11002         unallocated_encoding(s);
11003         return;
11004     }
11005     if (!feature) {
11006         unallocated_encoding(s);
11007         return;
11008     }
11009     if (!fp_access_check(s)) {
11010         return;
11011     }
11012 
11013     switch (opcode) {
11014     case 0x0: /* SQRDMLAH (vector) */
11015         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11016         return;
11017 
11018     case 0x1: /* SQRDMLSH (vector) */
11019         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11020         return;
11021 
11022     case 0x2: /* SDOT / UDOT */
11023         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11024                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11025         return;
11026 
11027     case 0x3: /* USDOT */
11028         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11029         return;
11030 
11031     case 0x04: /* SMMLA, UMMLA */
11032         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11033                          u ? gen_helper_gvec_ummla_b
11034                          : gen_helper_gvec_smmla_b);
11035         return;
11036     case 0x05: /* USMMLA */
11037         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11038         return;
11039 
11040     case 0x8: /* FCMLA, #0 */
11041     case 0x9: /* FCMLA, #90 */
11042     case 0xa: /* FCMLA, #180 */
11043     case 0xb: /* FCMLA, #270 */
11044         rot = extract32(opcode, 0, 2);
11045         switch (size) {
11046         case 1:
11047             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11048                               gen_helper_gvec_fcmlah);
11049             break;
11050         case 2:
11051             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11052                               gen_helper_gvec_fcmlas);
11053             break;
11054         case 3:
11055             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11056                               gen_helper_gvec_fcmlad);
11057             break;
11058         default:
11059             g_assert_not_reached();
11060         }
11061         return;
11062 
11063     case 0xc: /* FCADD, #90 */
11064     case 0xe: /* FCADD, #270 */
11065         rot = extract32(opcode, 1, 1);
11066         switch (size) {
11067         case 1:
11068             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11069                               gen_helper_gvec_fcaddh);
11070             break;
11071         case 2:
11072             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11073                               gen_helper_gvec_fcadds);
11074             break;
11075         case 3:
11076             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11077                               gen_helper_gvec_fcaddd);
11078             break;
11079         default:
11080             g_assert_not_reached();
11081         }
11082         return;
11083 
11084     case 0xd: /* BFMMLA */
11085         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11086         return;
11087     case 0xf:
11088         switch (size) {
11089         case 1: /* BFDOT */
11090             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11091             break;
11092         case 3: /* BFMLAL{B,T} */
11093             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11094                               gen_helper_gvec_bfmlal);
11095             break;
11096         default:
11097             g_assert_not_reached();
11098         }
11099         return;
11100 
11101     default:
11102         g_assert_not_reached();
11103     }
11104 }
11105 
11106 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11107                                   int size, int rn, int rd)
11108 {
11109     /* Handle 2-reg-misc ops which are widening (so each size element
11110      * in the source becomes a 2*size element in the destination.
11111      * The only instruction like this is FCVTL.
11112      */
11113     int pass;
11114 
11115     if (size == 3) {
11116         /* 32 -> 64 bit fp conversion */
11117         TCGv_i64 tcg_res[2];
11118         int srcelt = is_q ? 2 : 0;
11119 
11120         for (pass = 0; pass < 2; pass++) {
11121             TCGv_i32 tcg_op = tcg_temp_new_i32();
11122             tcg_res[pass] = tcg_temp_new_i64();
11123 
11124             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11125             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11126         }
11127         for (pass = 0; pass < 2; pass++) {
11128             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11129         }
11130     } else {
11131         /* 16 -> 32 bit fp conversion */
11132         int srcelt = is_q ? 4 : 0;
11133         TCGv_i32 tcg_res[4];
11134         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11135         TCGv_i32 ahp = get_ahp_flag();
11136 
11137         for (pass = 0; pass < 4; pass++) {
11138             tcg_res[pass] = tcg_temp_new_i32();
11139 
11140             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11141             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11142                                            fpst, ahp);
11143         }
11144         for (pass = 0; pass < 4; pass++) {
11145             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11146         }
11147     }
11148 }
11149 
11150 static void handle_rev(DisasContext *s, int opcode, bool u,
11151                        bool is_q, int size, int rn, int rd)
11152 {
11153     int op = (opcode << 1) | u;
11154     int opsz = op + size;
11155     int grp_size = 3 - opsz;
11156     int dsize = is_q ? 128 : 64;
11157     int i;
11158 
11159     if (opsz >= 3) {
11160         unallocated_encoding(s);
11161         return;
11162     }
11163 
11164     if (!fp_access_check(s)) {
11165         return;
11166     }
11167 
11168     if (size == 0) {
11169         /* Special case bytes, use bswap op on each group of elements */
11170         int groups = dsize / (8 << grp_size);
11171 
11172         for (i = 0; i < groups; i++) {
11173             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11174 
11175             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11176             switch (grp_size) {
11177             case MO_16:
11178                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11179                 break;
11180             case MO_32:
11181                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11182                 break;
11183             case MO_64:
11184                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11185                 break;
11186             default:
11187                 g_assert_not_reached();
11188             }
11189             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11190         }
11191         clear_vec_high(s, is_q, rd);
11192     } else {
11193         int revmask = (1 << grp_size) - 1;
11194         int esize = 8 << size;
11195         int elements = dsize / esize;
11196         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11197         TCGv_i64 tcg_rd[2];
11198 
11199         for (i = 0; i < 2; i++) {
11200             tcg_rd[i] = tcg_temp_new_i64();
11201             tcg_gen_movi_i64(tcg_rd[i], 0);
11202         }
11203 
11204         for (i = 0; i < elements; i++) {
11205             int e_rev = (i & 0xf) ^ revmask;
11206             int w = (e_rev * esize) / 64;
11207             int o = (e_rev * esize) % 64;
11208 
11209             read_vec_element(s, tcg_rn, rn, i, size);
11210             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11211         }
11212 
11213         for (i = 0; i < 2; i++) {
11214             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11215         }
11216         clear_vec_high(s, true, rd);
11217     }
11218 }
11219 
11220 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11221                                   bool is_q, int size, int rn, int rd)
11222 {
11223     /* Implement the pairwise operations from 2-misc:
11224      * SADDLP, UADDLP, SADALP, UADALP.
11225      * These all add pairs of elements in the input to produce a
11226      * double-width result element in the output (possibly accumulating).
11227      */
11228     bool accum = (opcode == 0x6);
11229     int maxpass = is_q ? 2 : 1;
11230     int pass;
11231     TCGv_i64 tcg_res[2];
11232 
11233     if (size == 2) {
11234         /* 32 + 32 -> 64 op */
11235         MemOp memop = size + (u ? 0 : MO_SIGN);
11236 
11237         for (pass = 0; pass < maxpass; pass++) {
11238             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11239             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11240 
11241             tcg_res[pass] = tcg_temp_new_i64();
11242 
11243             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11244             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11245             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11246             if (accum) {
11247                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11248                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11249             }
11250         }
11251     } else {
11252         for (pass = 0; pass < maxpass; pass++) {
11253             TCGv_i64 tcg_op = tcg_temp_new_i64();
11254             NeonGenOne64OpFn *genfn;
11255             static NeonGenOne64OpFn * const fns[2][2] = {
11256                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11257                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11258             };
11259 
11260             genfn = fns[size][u];
11261 
11262             tcg_res[pass] = tcg_temp_new_i64();
11263 
11264             read_vec_element(s, tcg_op, rn, pass, MO_64);
11265             genfn(tcg_res[pass], tcg_op);
11266 
11267             if (accum) {
11268                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11269                 if (size == 0) {
11270                     gen_helper_neon_addl_u16(tcg_res[pass],
11271                                              tcg_res[pass], tcg_op);
11272                 } else {
11273                     gen_helper_neon_addl_u32(tcg_res[pass],
11274                                              tcg_res[pass], tcg_op);
11275                 }
11276             }
11277         }
11278     }
11279     if (!is_q) {
11280         tcg_res[1] = tcg_constant_i64(0);
11281     }
11282     for (pass = 0; pass < 2; pass++) {
11283         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11284     }
11285 }
11286 
11287 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11288 {
11289     /* Implement SHLL and SHLL2 */
11290     int pass;
11291     int part = is_q ? 2 : 0;
11292     TCGv_i64 tcg_res[2];
11293 
11294     for (pass = 0; pass < 2; pass++) {
11295         static NeonGenWidenFn * const widenfns[3] = {
11296             gen_helper_neon_widen_u8,
11297             gen_helper_neon_widen_u16,
11298             tcg_gen_extu_i32_i64,
11299         };
11300         NeonGenWidenFn *widenfn = widenfns[size];
11301         TCGv_i32 tcg_op = tcg_temp_new_i32();
11302 
11303         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11304         tcg_res[pass] = tcg_temp_new_i64();
11305         widenfn(tcg_res[pass], tcg_op);
11306         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11307     }
11308 
11309     for (pass = 0; pass < 2; pass++) {
11310         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11311     }
11312 }
11313 
11314 /* AdvSIMD two reg misc
11315  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11316  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11317  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11318  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11319  */
11320 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11321 {
11322     int size = extract32(insn, 22, 2);
11323     int opcode = extract32(insn, 12, 5);
11324     bool u = extract32(insn, 29, 1);
11325     bool is_q = extract32(insn, 30, 1);
11326     int rn = extract32(insn, 5, 5);
11327     int rd = extract32(insn, 0, 5);
11328     bool need_fpstatus = false;
11329     int rmode = -1;
11330     TCGv_i32 tcg_rmode;
11331     TCGv_ptr tcg_fpstatus;
11332 
11333     switch (opcode) {
11334     case 0x0: /* REV64, REV32 */
11335     case 0x1: /* REV16 */
11336         handle_rev(s, opcode, u, is_q, size, rn, rd);
11337         return;
11338     case 0x5: /* CNT, NOT, RBIT */
11339         if (u && size == 0) {
11340             /* NOT */
11341             break;
11342         } else if (u && size == 1) {
11343             /* RBIT */
11344             break;
11345         } else if (!u && size == 0) {
11346             /* CNT */
11347             break;
11348         }
11349         unallocated_encoding(s);
11350         return;
11351     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11352     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11353         if (size == 3) {
11354             unallocated_encoding(s);
11355             return;
11356         }
11357         if (!fp_access_check(s)) {
11358             return;
11359         }
11360 
11361         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11362         return;
11363     case 0x4: /* CLS, CLZ */
11364         if (size == 3) {
11365             unallocated_encoding(s);
11366             return;
11367         }
11368         break;
11369     case 0x2: /* SADDLP, UADDLP */
11370     case 0x6: /* SADALP, UADALP */
11371         if (size == 3) {
11372             unallocated_encoding(s);
11373             return;
11374         }
11375         if (!fp_access_check(s)) {
11376             return;
11377         }
11378         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11379         return;
11380     case 0x13: /* SHLL, SHLL2 */
11381         if (u == 0 || size == 3) {
11382             unallocated_encoding(s);
11383             return;
11384         }
11385         if (!fp_access_check(s)) {
11386             return;
11387         }
11388         handle_shll(s, is_q, size, rn, rd);
11389         return;
11390     case 0xa: /* CMLT */
11391         if (u == 1) {
11392             unallocated_encoding(s);
11393             return;
11394         }
11395         /* fall through */
11396     case 0x8: /* CMGT, CMGE */
11397     case 0x9: /* CMEQ, CMLE */
11398     case 0xb: /* ABS, NEG */
11399         if (size == 3 && !is_q) {
11400             unallocated_encoding(s);
11401             return;
11402         }
11403         break;
11404     case 0x7: /* SQABS, SQNEG */
11405         if (size == 3 && !is_q) {
11406             unallocated_encoding(s);
11407             return;
11408         }
11409         break;
11410     case 0xc ... 0xf:
11411     case 0x16 ... 0x1f:
11412     {
11413         /* Floating point: U, size[1] and opcode indicate operation;
11414          * size[0] indicates single or double precision.
11415          */
11416         int is_double = extract32(size, 0, 1);
11417         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11418         size = is_double ? 3 : 2;
11419         switch (opcode) {
11420         case 0x2f: /* FABS */
11421         case 0x6f: /* FNEG */
11422             if (size == 3 && !is_q) {
11423                 unallocated_encoding(s);
11424                 return;
11425             }
11426             break;
11427         case 0x1d: /* SCVTF */
11428         case 0x5d: /* UCVTF */
11429         {
11430             bool is_signed = (opcode == 0x1d) ? true : false;
11431             int elements = is_double ? 2 : is_q ? 4 : 2;
11432             if (is_double && !is_q) {
11433                 unallocated_encoding(s);
11434                 return;
11435             }
11436             if (!fp_access_check(s)) {
11437                 return;
11438             }
11439             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11440             return;
11441         }
11442         case 0x2c: /* FCMGT (zero) */
11443         case 0x2d: /* FCMEQ (zero) */
11444         case 0x2e: /* FCMLT (zero) */
11445         case 0x6c: /* FCMGE (zero) */
11446         case 0x6d: /* FCMLE (zero) */
11447             if (size == 3 && !is_q) {
11448                 unallocated_encoding(s);
11449                 return;
11450             }
11451             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11452             return;
11453         case 0x7f: /* FSQRT */
11454             if (size == 3 && !is_q) {
11455                 unallocated_encoding(s);
11456                 return;
11457             }
11458             break;
11459         case 0x1a: /* FCVTNS */
11460         case 0x1b: /* FCVTMS */
11461         case 0x3a: /* FCVTPS */
11462         case 0x3b: /* FCVTZS */
11463         case 0x5a: /* FCVTNU */
11464         case 0x5b: /* FCVTMU */
11465         case 0x7a: /* FCVTPU */
11466         case 0x7b: /* FCVTZU */
11467             need_fpstatus = true;
11468             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11469             if (size == 3 && !is_q) {
11470                 unallocated_encoding(s);
11471                 return;
11472             }
11473             break;
11474         case 0x5c: /* FCVTAU */
11475         case 0x1c: /* FCVTAS */
11476             need_fpstatus = true;
11477             rmode = FPROUNDING_TIEAWAY;
11478             if (size == 3 && !is_q) {
11479                 unallocated_encoding(s);
11480                 return;
11481             }
11482             break;
11483         case 0x3c: /* URECPE */
11484             if (size == 3) {
11485                 unallocated_encoding(s);
11486                 return;
11487             }
11488             /* fall through */
11489         case 0x3d: /* FRECPE */
11490         case 0x7d: /* FRSQRTE */
11491             if (size == 3 && !is_q) {
11492                 unallocated_encoding(s);
11493                 return;
11494             }
11495             if (!fp_access_check(s)) {
11496                 return;
11497             }
11498             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
11499             return;
11500         case 0x56: /* FCVTXN, FCVTXN2 */
11501             if (size == 2) {
11502                 unallocated_encoding(s);
11503                 return;
11504             }
11505             /* fall through */
11506         case 0x16: /* FCVTN, FCVTN2 */
11507             /* handle_2misc_narrow does a 2*size -> size operation, but these
11508              * instructions encode the source size rather than dest size.
11509              */
11510             if (!fp_access_check(s)) {
11511                 return;
11512             }
11513             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11514             return;
11515         case 0x36: /* BFCVTN, BFCVTN2 */
11516             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
11517                 unallocated_encoding(s);
11518                 return;
11519             }
11520             if (!fp_access_check(s)) {
11521                 return;
11522             }
11523             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11524             return;
11525         case 0x17: /* FCVTL, FCVTL2 */
11526             if (!fp_access_check(s)) {
11527                 return;
11528             }
11529             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
11530             return;
11531         case 0x18: /* FRINTN */
11532         case 0x19: /* FRINTM */
11533         case 0x38: /* FRINTP */
11534         case 0x39: /* FRINTZ */
11535             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11536             /* fall through */
11537         case 0x59: /* FRINTX */
11538         case 0x79: /* FRINTI */
11539             need_fpstatus = true;
11540             if (size == 3 && !is_q) {
11541                 unallocated_encoding(s);
11542                 return;
11543             }
11544             break;
11545         case 0x58: /* FRINTA */
11546             rmode = FPROUNDING_TIEAWAY;
11547             need_fpstatus = true;
11548             if (size == 3 && !is_q) {
11549                 unallocated_encoding(s);
11550                 return;
11551             }
11552             break;
11553         case 0x7c: /* URSQRTE */
11554             if (size == 3) {
11555                 unallocated_encoding(s);
11556                 return;
11557             }
11558             break;
11559         case 0x1e: /* FRINT32Z */
11560         case 0x1f: /* FRINT64Z */
11561             rmode = FPROUNDING_ZERO;
11562             /* fall through */
11563         case 0x5e: /* FRINT32X */
11564         case 0x5f: /* FRINT64X */
11565             need_fpstatus = true;
11566             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
11567                 unallocated_encoding(s);
11568                 return;
11569             }
11570             break;
11571         default:
11572             unallocated_encoding(s);
11573             return;
11574         }
11575         break;
11576     }
11577     default:
11578     case 0x3: /* SUQADD, USQADD */
11579         unallocated_encoding(s);
11580         return;
11581     }
11582 
11583     if (!fp_access_check(s)) {
11584         return;
11585     }
11586 
11587     if (need_fpstatus || rmode >= 0) {
11588         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
11589     } else {
11590         tcg_fpstatus = NULL;
11591     }
11592     if (rmode >= 0) {
11593         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
11594     } else {
11595         tcg_rmode = NULL;
11596     }
11597 
11598     switch (opcode) {
11599     case 0x5:
11600         if (u && size == 0) { /* NOT */
11601             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
11602             return;
11603         }
11604         break;
11605     case 0x8: /* CMGT, CMGE */
11606         if (u) {
11607             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
11608         } else {
11609             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
11610         }
11611         return;
11612     case 0x9: /* CMEQ, CMLE */
11613         if (u) {
11614             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
11615         } else {
11616             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
11617         }
11618         return;
11619     case 0xa: /* CMLT */
11620         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
11621         return;
11622     case 0xb:
11623         if (u) { /* ABS, NEG */
11624             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
11625         } else {
11626             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
11627         }
11628         return;
11629     }
11630 
11631     if (size == 3) {
11632         /* All 64-bit element operations can be shared with scalar 2misc */
11633         int pass;
11634 
11635         /* Coverity claims (size == 3 && !is_q) has been eliminated
11636          * from all paths leading to here.
11637          */
11638         tcg_debug_assert(is_q);
11639         for (pass = 0; pass < 2; pass++) {
11640             TCGv_i64 tcg_op = tcg_temp_new_i64();
11641             TCGv_i64 tcg_res = tcg_temp_new_i64();
11642 
11643             read_vec_element(s, tcg_op, rn, pass, MO_64);
11644 
11645             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
11646                             tcg_rmode, tcg_fpstatus);
11647 
11648             write_vec_element(s, tcg_res, rd, pass, MO_64);
11649         }
11650     } else {
11651         int pass;
11652 
11653         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11654             TCGv_i32 tcg_op = tcg_temp_new_i32();
11655             TCGv_i32 tcg_res = tcg_temp_new_i32();
11656 
11657             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
11658 
11659             if (size == 2) {
11660                 /* Special cases for 32 bit elements */
11661                 switch (opcode) {
11662                 case 0x4: /* CLS */
11663                     if (u) {
11664                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
11665                     } else {
11666                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
11667                     }
11668                     break;
11669                 case 0x7: /* SQABS, SQNEG */
11670                     if (u) {
11671                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
11672                     } else {
11673                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
11674                     }
11675                     break;
11676                 case 0x2f: /* FABS */
11677                     gen_vfp_abss(tcg_res, tcg_op);
11678                     break;
11679                 case 0x6f: /* FNEG */
11680                     gen_vfp_negs(tcg_res, tcg_op);
11681                     break;
11682                 case 0x7f: /* FSQRT */
11683                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
11684                     break;
11685                 case 0x1a: /* FCVTNS */
11686                 case 0x1b: /* FCVTMS */
11687                 case 0x1c: /* FCVTAS */
11688                 case 0x3a: /* FCVTPS */
11689                 case 0x3b: /* FCVTZS */
11690                     gen_helper_vfp_tosls(tcg_res, tcg_op,
11691                                          tcg_constant_i32(0), tcg_fpstatus);
11692                     break;
11693                 case 0x5a: /* FCVTNU */
11694                 case 0x5b: /* FCVTMU */
11695                 case 0x5c: /* FCVTAU */
11696                 case 0x7a: /* FCVTPU */
11697                 case 0x7b: /* FCVTZU */
11698                     gen_helper_vfp_touls(tcg_res, tcg_op,
11699                                          tcg_constant_i32(0), tcg_fpstatus);
11700                     break;
11701                 case 0x18: /* FRINTN */
11702                 case 0x19: /* FRINTM */
11703                 case 0x38: /* FRINTP */
11704                 case 0x39: /* FRINTZ */
11705                 case 0x58: /* FRINTA */
11706                 case 0x79: /* FRINTI */
11707                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
11708                     break;
11709                 case 0x59: /* FRINTX */
11710                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
11711                     break;
11712                 case 0x7c: /* URSQRTE */
11713                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
11714                     break;
11715                 case 0x1e: /* FRINT32Z */
11716                 case 0x5e: /* FRINT32X */
11717                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
11718                     break;
11719                 case 0x1f: /* FRINT64Z */
11720                 case 0x5f: /* FRINT64X */
11721                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
11722                     break;
11723                 default:
11724                     g_assert_not_reached();
11725                 }
11726             } else {
11727                 /* Use helpers for 8 and 16 bit elements */
11728                 switch (opcode) {
11729                 case 0x5: /* CNT, RBIT */
11730                     /* For these two insns size is part of the opcode specifier
11731                      * (handled earlier); they always operate on byte elements.
11732                      */
11733                     if (u) {
11734                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
11735                     } else {
11736                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
11737                     }
11738                     break;
11739                 case 0x7: /* SQABS, SQNEG */
11740                 {
11741                     NeonGenOneOpEnvFn *genfn;
11742                     static NeonGenOneOpEnvFn * const fns[2][2] = {
11743                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
11744                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
11745                     };
11746                     genfn = fns[size][u];
11747                     genfn(tcg_res, tcg_env, tcg_op);
11748                     break;
11749                 }
11750                 case 0x4: /* CLS, CLZ */
11751                     if (u) {
11752                         if (size == 0) {
11753                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
11754                         } else {
11755                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
11756                         }
11757                     } else {
11758                         if (size == 0) {
11759                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
11760                         } else {
11761                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
11762                         }
11763                     }
11764                     break;
11765                 default:
11766                     g_assert_not_reached();
11767                 }
11768             }
11769 
11770             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11771         }
11772     }
11773     clear_vec_high(s, is_q, rd);
11774 
11775     if (tcg_rmode) {
11776         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
11777     }
11778 }
11779 
11780 /* AdvSIMD [scalar] two register miscellaneous (FP16)
11781  *
11782  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
11783  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11784  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11785  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11786  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
11787  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
11788  *
11789  * This actually covers two groups where scalar access is governed by
11790  * bit 28. A bunch of the instructions (float to integral) only exist
11791  * in the vector form and are un-allocated for the scalar decode. Also
11792  * in the scalar decode Q is always 1.
11793  */
11794 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
11795 {
11796     int fpop, opcode, a, u;
11797     int rn, rd;
11798     bool is_q;
11799     bool is_scalar;
11800     bool only_in_vector = false;
11801 
11802     int pass;
11803     TCGv_i32 tcg_rmode = NULL;
11804     TCGv_ptr tcg_fpstatus = NULL;
11805     bool need_fpst = true;
11806     int rmode = -1;
11807 
11808     if (!dc_isar_feature(aa64_fp16, s)) {
11809         unallocated_encoding(s);
11810         return;
11811     }
11812 
11813     rd = extract32(insn, 0, 5);
11814     rn = extract32(insn, 5, 5);
11815 
11816     a = extract32(insn, 23, 1);
11817     u = extract32(insn, 29, 1);
11818     is_scalar = extract32(insn, 28, 1);
11819     is_q = extract32(insn, 30, 1);
11820 
11821     opcode = extract32(insn, 12, 5);
11822     fpop = deposit32(opcode, 5, 1, a);
11823     fpop = deposit32(fpop, 6, 1, u);
11824 
11825     switch (fpop) {
11826     case 0x1d: /* SCVTF */
11827     case 0x5d: /* UCVTF */
11828     {
11829         int elements;
11830 
11831         if (is_scalar) {
11832             elements = 1;
11833         } else {
11834             elements = (is_q ? 8 : 4);
11835         }
11836 
11837         if (!fp_access_check(s)) {
11838             return;
11839         }
11840         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
11841         return;
11842     }
11843     break;
11844     case 0x2c: /* FCMGT (zero) */
11845     case 0x2d: /* FCMEQ (zero) */
11846     case 0x2e: /* FCMLT (zero) */
11847     case 0x6c: /* FCMGE (zero) */
11848     case 0x6d: /* FCMLE (zero) */
11849         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
11850         return;
11851     case 0x3d: /* FRECPE */
11852     case 0x3f: /* FRECPX */
11853         break;
11854     case 0x18: /* FRINTN */
11855         only_in_vector = true;
11856         rmode = FPROUNDING_TIEEVEN;
11857         break;
11858     case 0x19: /* FRINTM */
11859         only_in_vector = true;
11860         rmode = FPROUNDING_NEGINF;
11861         break;
11862     case 0x38: /* FRINTP */
11863         only_in_vector = true;
11864         rmode = FPROUNDING_POSINF;
11865         break;
11866     case 0x39: /* FRINTZ */
11867         only_in_vector = true;
11868         rmode = FPROUNDING_ZERO;
11869         break;
11870     case 0x58: /* FRINTA */
11871         only_in_vector = true;
11872         rmode = FPROUNDING_TIEAWAY;
11873         break;
11874     case 0x59: /* FRINTX */
11875     case 0x79: /* FRINTI */
11876         only_in_vector = true;
11877         /* current rounding mode */
11878         break;
11879     case 0x1a: /* FCVTNS */
11880         rmode = FPROUNDING_TIEEVEN;
11881         break;
11882     case 0x1b: /* FCVTMS */
11883         rmode = FPROUNDING_NEGINF;
11884         break;
11885     case 0x1c: /* FCVTAS */
11886         rmode = FPROUNDING_TIEAWAY;
11887         break;
11888     case 0x3a: /* FCVTPS */
11889         rmode = FPROUNDING_POSINF;
11890         break;
11891     case 0x3b: /* FCVTZS */
11892         rmode = FPROUNDING_ZERO;
11893         break;
11894     case 0x5a: /* FCVTNU */
11895         rmode = FPROUNDING_TIEEVEN;
11896         break;
11897     case 0x5b: /* FCVTMU */
11898         rmode = FPROUNDING_NEGINF;
11899         break;
11900     case 0x5c: /* FCVTAU */
11901         rmode = FPROUNDING_TIEAWAY;
11902         break;
11903     case 0x7a: /* FCVTPU */
11904         rmode = FPROUNDING_POSINF;
11905         break;
11906     case 0x7b: /* FCVTZU */
11907         rmode = FPROUNDING_ZERO;
11908         break;
11909     case 0x2f: /* FABS */
11910     case 0x6f: /* FNEG */
11911         need_fpst = false;
11912         break;
11913     case 0x7d: /* FRSQRTE */
11914     case 0x7f: /* FSQRT (vector) */
11915         break;
11916     default:
11917         unallocated_encoding(s);
11918         return;
11919     }
11920 
11921 
11922     /* Check additional constraints for the scalar encoding */
11923     if (is_scalar) {
11924         if (!is_q) {
11925             unallocated_encoding(s);
11926             return;
11927         }
11928         /* FRINTxx is only in the vector form */
11929         if (only_in_vector) {
11930             unallocated_encoding(s);
11931             return;
11932         }
11933     }
11934 
11935     if (!fp_access_check(s)) {
11936         return;
11937     }
11938 
11939     if (rmode >= 0 || need_fpst) {
11940         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
11941     }
11942 
11943     if (rmode >= 0) {
11944         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
11945     }
11946 
11947     if (is_scalar) {
11948         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
11949         TCGv_i32 tcg_res = tcg_temp_new_i32();
11950 
11951         switch (fpop) {
11952         case 0x1a: /* FCVTNS */
11953         case 0x1b: /* FCVTMS */
11954         case 0x1c: /* FCVTAS */
11955         case 0x3a: /* FCVTPS */
11956         case 0x3b: /* FCVTZS */
11957             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11958             break;
11959         case 0x3d: /* FRECPE */
11960             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11961             break;
11962         case 0x3f: /* FRECPX */
11963             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
11964             break;
11965         case 0x5a: /* FCVTNU */
11966         case 0x5b: /* FCVTMU */
11967         case 0x5c: /* FCVTAU */
11968         case 0x7a: /* FCVTPU */
11969         case 0x7b: /* FCVTZU */
11970             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11971             break;
11972         case 0x6f: /* FNEG */
11973             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11974             break;
11975         case 0x7d: /* FRSQRTE */
11976             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11977             break;
11978         default:
11979             g_assert_not_reached();
11980         }
11981 
11982         /* limit any sign extension going on */
11983         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
11984         write_fp_sreg(s, rd, tcg_res);
11985     } else {
11986         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
11987             TCGv_i32 tcg_op = tcg_temp_new_i32();
11988             TCGv_i32 tcg_res = tcg_temp_new_i32();
11989 
11990             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
11991 
11992             switch (fpop) {
11993             case 0x1a: /* FCVTNS */
11994             case 0x1b: /* FCVTMS */
11995             case 0x1c: /* FCVTAS */
11996             case 0x3a: /* FCVTPS */
11997             case 0x3b: /* FCVTZS */
11998                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11999                 break;
12000             case 0x3d: /* FRECPE */
12001                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12002                 break;
12003             case 0x5a: /* FCVTNU */
12004             case 0x5b: /* FCVTMU */
12005             case 0x5c: /* FCVTAU */
12006             case 0x7a: /* FCVTPU */
12007             case 0x7b: /* FCVTZU */
12008                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12009                 break;
12010             case 0x18: /* FRINTN */
12011             case 0x19: /* FRINTM */
12012             case 0x38: /* FRINTP */
12013             case 0x39: /* FRINTZ */
12014             case 0x58: /* FRINTA */
12015             case 0x79: /* FRINTI */
12016                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12017                 break;
12018             case 0x59: /* FRINTX */
12019                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12020                 break;
12021             case 0x2f: /* FABS */
12022                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12023                 break;
12024             case 0x6f: /* FNEG */
12025                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12026                 break;
12027             case 0x7d: /* FRSQRTE */
12028                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12029                 break;
12030             case 0x7f: /* FSQRT */
12031                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12032                 break;
12033             default:
12034                 g_assert_not_reached();
12035             }
12036 
12037             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12038         }
12039 
12040         clear_vec_high(s, is_q, rd);
12041     }
12042 
12043     if (tcg_rmode) {
12044         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12045     }
12046 }
12047 
12048 /* AdvSIMD scalar x indexed element
12049  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12050  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12051  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12052  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12053  * AdvSIMD vector x indexed element
12054  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12055  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12056  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12057  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12058  */
12059 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12060 {
12061     /* This encoding has two kinds of instruction:
12062      *  normal, where we perform elt x idxelt => elt for each
12063      *     element in the vector
12064      *  long, where we perform elt x idxelt and generate a result of
12065      *     double the width of the input element
12066      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12067      */
12068     bool is_scalar = extract32(insn, 28, 1);
12069     bool is_q = extract32(insn, 30, 1);
12070     bool u = extract32(insn, 29, 1);
12071     int size = extract32(insn, 22, 2);
12072     int l = extract32(insn, 21, 1);
12073     int m = extract32(insn, 20, 1);
12074     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12075     int rm = extract32(insn, 16, 4);
12076     int opcode = extract32(insn, 12, 4);
12077     int h = extract32(insn, 11, 1);
12078     int rn = extract32(insn, 5, 5);
12079     int rd = extract32(insn, 0, 5);
12080     bool is_long = false;
12081     int is_fp = 0;
12082     bool is_fp16 = false;
12083     int index;
12084     TCGv_ptr fpst;
12085 
12086     switch (16 * u + opcode) {
12087     case 0x02: /* SMLAL, SMLAL2 */
12088     case 0x12: /* UMLAL, UMLAL2 */
12089     case 0x06: /* SMLSL, SMLSL2 */
12090     case 0x16: /* UMLSL, UMLSL2 */
12091     case 0x0a: /* SMULL, SMULL2 */
12092     case 0x1a: /* UMULL, UMULL2 */
12093         if (is_scalar) {
12094             unallocated_encoding(s);
12095             return;
12096         }
12097         is_long = true;
12098         break;
12099     case 0x03: /* SQDMLAL, SQDMLAL2 */
12100     case 0x07: /* SQDMLSL, SQDMLSL2 */
12101     case 0x0b: /* SQDMULL, SQDMULL2 */
12102         is_long = true;
12103         break;
12104     case 0x1d: /* SQRDMLAH */
12105     case 0x1f: /* SQRDMLSH */
12106         if (!dc_isar_feature(aa64_rdm, s)) {
12107             unallocated_encoding(s);
12108             return;
12109         }
12110         break;
12111     case 0x0e: /* SDOT */
12112     case 0x1e: /* UDOT */
12113         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12114             unallocated_encoding(s);
12115             return;
12116         }
12117         break;
12118     case 0x0f:
12119         switch (size) {
12120         case 0: /* SUDOT */
12121         case 2: /* USDOT */
12122             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12123                 unallocated_encoding(s);
12124                 return;
12125             }
12126             size = MO_32;
12127             break;
12128         case 1: /* BFDOT */
12129             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12130                 unallocated_encoding(s);
12131                 return;
12132             }
12133             size = MO_32;
12134             break;
12135         case 3: /* BFMLAL{B,T} */
12136             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12137                 unallocated_encoding(s);
12138                 return;
12139             }
12140             /* can't set is_fp without other incorrect size checks */
12141             size = MO_16;
12142             break;
12143         default:
12144             unallocated_encoding(s);
12145             return;
12146         }
12147         break;
12148     case 0x11: /* FCMLA #0 */
12149     case 0x13: /* FCMLA #90 */
12150     case 0x15: /* FCMLA #180 */
12151     case 0x17: /* FCMLA #270 */
12152         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12153             unallocated_encoding(s);
12154             return;
12155         }
12156         is_fp = 2;
12157         break;
12158     default:
12159     case 0x00: /* FMLAL */
12160     case 0x01: /* FMLA */
12161     case 0x04: /* FMLSL */
12162     case 0x05: /* FMLS */
12163     case 0x08: /* MUL */
12164     case 0x09: /* FMUL */
12165     case 0x0c: /* SQDMULH */
12166     case 0x0d: /* SQRDMULH */
12167     case 0x10: /* MLA */
12168     case 0x14: /* MLS */
12169     case 0x18: /* FMLAL2 */
12170     case 0x19: /* FMULX */
12171     case 0x1c: /* FMLSL2 */
12172         unallocated_encoding(s);
12173         return;
12174     }
12175 
12176     switch (is_fp) {
12177     case 1: /* normal fp */
12178         unallocated_encoding(s); /* in decodetree */
12179         return;
12180 
12181     case 2: /* complex fp */
12182         /* Each indexable element is a complex pair.  */
12183         size += 1;
12184         switch (size) {
12185         case MO_32:
12186             if (h && !is_q) {
12187                 unallocated_encoding(s);
12188                 return;
12189             }
12190             is_fp16 = true;
12191             break;
12192         case MO_64:
12193             break;
12194         default:
12195             unallocated_encoding(s);
12196             return;
12197         }
12198         break;
12199 
12200     default: /* integer */
12201         switch (size) {
12202         case MO_8:
12203         case MO_64:
12204             unallocated_encoding(s);
12205             return;
12206         }
12207         break;
12208     }
12209     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12210         unallocated_encoding(s);
12211         return;
12212     }
12213 
12214     /* Given MemOp size, adjust register and indexing.  */
12215     switch (size) {
12216     case MO_16:
12217         index = h << 2 | l << 1 | m;
12218         break;
12219     case MO_32:
12220         index = h << 1 | l;
12221         rm |= m << 4;
12222         break;
12223     case MO_64:
12224         if (l || !is_q) {
12225             unallocated_encoding(s);
12226             return;
12227         }
12228         index = h;
12229         rm |= m << 4;
12230         break;
12231     default:
12232         g_assert_not_reached();
12233     }
12234 
12235     if (!fp_access_check(s)) {
12236         return;
12237     }
12238 
12239     if (is_fp) {
12240         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12241     } else {
12242         fpst = NULL;
12243     }
12244 
12245     switch (16 * u + opcode) {
12246     case 0x0e: /* SDOT */
12247     case 0x1e: /* UDOT */
12248         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12249                          u ? gen_helper_gvec_udot_idx_b
12250                          : gen_helper_gvec_sdot_idx_b);
12251         return;
12252     case 0x0f:
12253         switch (extract32(insn, 22, 2)) {
12254         case 0: /* SUDOT */
12255             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12256                              gen_helper_gvec_sudot_idx_b);
12257             return;
12258         case 1: /* BFDOT */
12259             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12260                              gen_helper_gvec_bfdot_idx);
12261             return;
12262         case 2: /* USDOT */
12263             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12264                              gen_helper_gvec_usdot_idx_b);
12265             return;
12266         case 3: /* BFMLAL{B,T} */
12267             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12268                               gen_helper_gvec_bfmlal_idx);
12269             return;
12270         }
12271         g_assert_not_reached();
12272     case 0x11: /* FCMLA #0 */
12273     case 0x13: /* FCMLA #90 */
12274     case 0x15: /* FCMLA #180 */
12275     case 0x17: /* FCMLA #270 */
12276         {
12277             int rot = extract32(insn, 13, 2);
12278             int data = (index << 2) | rot;
12279             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12280                                vec_full_reg_offset(s, rn),
12281                                vec_full_reg_offset(s, rm),
12282                                vec_full_reg_offset(s, rd), fpst,
12283                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12284                                size == MO_64
12285                                ? gen_helper_gvec_fcmlas_idx
12286                                : gen_helper_gvec_fcmlah_idx);
12287         }
12288         return;
12289     }
12290 
12291     if (size == 3) {
12292         g_assert_not_reached();
12293     } else if (!is_long) {
12294         /* 32 bit floating point, or 16 or 32 bit integer.
12295          * For the 16 bit scalar case we use the usual Neon helpers and
12296          * rely on the fact that 0 op 0 == 0 with no side effects.
12297          */
12298         TCGv_i32 tcg_idx = tcg_temp_new_i32();
12299         int pass, maxpasses;
12300 
12301         if (is_scalar) {
12302             maxpasses = 1;
12303         } else {
12304             maxpasses = is_q ? 4 : 2;
12305         }
12306 
12307         read_vec_element_i32(s, tcg_idx, rm, index, size);
12308 
12309         if (size == 1 && !is_scalar) {
12310             /* The simplest way to handle the 16x16 indexed ops is to duplicate
12311              * the index into both halves of the 32 bit tcg_idx and then use
12312              * the usual Neon helpers.
12313              */
12314             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12315         }
12316 
12317         for (pass = 0; pass < maxpasses; pass++) {
12318             TCGv_i32 tcg_op = tcg_temp_new_i32();
12319             TCGv_i32 tcg_res = tcg_temp_new_i32();
12320 
12321             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
12322 
12323             switch (16 * u + opcode) {
12324             case 0x10: /* MLA */
12325             case 0x14: /* MLS */
12326             {
12327                 static NeonGenTwoOpFn * const fns[2][2] = {
12328                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
12329                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
12330                 };
12331                 NeonGenTwoOpFn *genfn;
12332                 bool is_sub = opcode == 0x4;
12333 
12334                 if (size == 1) {
12335                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
12336                 } else {
12337                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
12338                 }
12339                 if (opcode == 0x8) {
12340                     break;
12341                 }
12342                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
12343                 genfn = fns[size - 1][is_sub];
12344                 genfn(tcg_res, tcg_op, tcg_res);
12345                 break;
12346             }
12347             case 0x0c: /* SQDMULH */
12348                 if (size == 1) {
12349                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
12350                                                tcg_op, tcg_idx);
12351                 } else {
12352                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
12353                                                tcg_op, tcg_idx);
12354                 }
12355                 break;
12356             case 0x0d: /* SQRDMULH */
12357                 if (size == 1) {
12358                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
12359                                                 tcg_op, tcg_idx);
12360                 } else {
12361                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
12362                                                 tcg_op, tcg_idx);
12363                 }
12364                 break;
12365             case 0x1d: /* SQRDMLAH */
12366                 read_vec_element_i32(s, tcg_res, rd, pass,
12367                                      is_scalar ? size : MO_32);
12368                 if (size == 1) {
12369                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
12370                                                 tcg_op, tcg_idx, tcg_res);
12371                 } else {
12372                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
12373                                                 tcg_op, tcg_idx, tcg_res);
12374                 }
12375                 break;
12376             case 0x1f: /* SQRDMLSH */
12377                 read_vec_element_i32(s, tcg_res, rd, pass,
12378                                      is_scalar ? size : MO_32);
12379                 if (size == 1) {
12380                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
12381                                                 tcg_op, tcg_idx, tcg_res);
12382                 } else {
12383                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
12384                                                 tcg_op, tcg_idx, tcg_res);
12385                 }
12386                 break;
12387             default:
12388             case 0x01: /* FMLA */
12389             case 0x05: /* FMLS */
12390             case 0x09: /* FMUL */
12391             case 0x19: /* FMULX */
12392                 g_assert_not_reached();
12393             }
12394 
12395             if (is_scalar) {
12396                 write_fp_sreg(s, rd, tcg_res);
12397             } else {
12398                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12399             }
12400         }
12401 
12402         clear_vec_high(s, is_q, rd);
12403     } else {
12404         /* long ops: 16x16->32 or 32x32->64 */
12405         TCGv_i64 tcg_res[2];
12406         int pass;
12407         bool satop = extract32(opcode, 0, 1);
12408         MemOp memop = MO_32;
12409 
12410         if (satop || !u) {
12411             memop |= MO_SIGN;
12412         }
12413 
12414         if (size == 2) {
12415             TCGv_i64 tcg_idx = tcg_temp_new_i64();
12416 
12417             read_vec_element(s, tcg_idx, rm, index, memop);
12418 
12419             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12420                 TCGv_i64 tcg_op = tcg_temp_new_i64();
12421                 TCGv_i64 tcg_passres;
12422                 int passelt;
12423 
12424                 if (is_scalar) {
12425                     passelt = 0;
12426                 } else {
12427                     passelt = pass + (is_q * 2);
12428                 }
12429 
12430                 read_vec_element(s, tcg_op, rn, passelt, memop);
12431 
12432                 tcg_res[pass] = tcg_temp_new_i64();
12433 
12434                 if (opcode == 0xa || opcode == 0xb) {
12435                     /* Non-accumulating ops */
12436                     tcg_passres = tcg_res[pass];
12437                 } else {
12438                     tcg_passres = tcg_temp_new_i64();
12439                 }
12440 
12441                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
12442 
12443                 if (satop) {
12444                     /* saturating, doubling */
12445                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
12446                                                       tcg_passres, tcg_passres);
12447                 }
12448 
12449                 if (opcode == 0xa || opcode == 0xb) {
12450                     continue;
12451                 }
12452 
12453                 /* Accumulating op: handle accumulate step */
12454                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12455 
12456                 switch (opcode) {
12457                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12458                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12459                     break;
12460                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12461                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12462                     break;
12463                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12464                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
12465                     /* fall through */
12466                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12467                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
12468                                                       tcg_res[pass],
12469                                                       tcg_passres);
12470                     break;
12471                 default:
12472                     g_assert_not_reached();
12473                 }
12474             }
12475 
12476             clear_vec_high(s, !is_scalar, rd);
12477         } else {
12478             TCGv_i32 tcg_idx = tcg_temp_new_i32();
12479 
12480             assert(size == 1);
12481             read_vec_element_i32(s, tcg_idx, rm, index, size);
12482 
12483             if (!is_scalar) {
12484                 /* The simplest way to handle the 16x16 indexed ops is to
12485                  * duplicate the index into both halves of the 32 bit tcg_idx
12486                  * and then use the usual Neon helpers.
12487                  */
12488                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12489             }
12490 
12491             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12492                 TCGv_i32 tcg_op = tcg_temp_new_i32();
12493                 TCGv_i64 tcg_passres;
12494 
12495                 if (is_scalar) {
12496                     read_vec_element_i32(s, tcg_op, rn, pass, size);
12497                 } else {
12498                     read_vec_element_i32(s, tcg_op, rn,
12499                                          pass + (is_q * 2), MO_32);
12500                 }
12501 
12502                 tcg_res[pass] = tcg_temp_new_i64();
12503 
12504                 if (opcode == 0xa || opcode == 0xb) {
12505                     /* Non-accumulating ops */
12506                     tcg_passres = tcg_res[pass];
12507                 } else {
12508                     tcg_passres = tcg_temp_new_i64();
12509                 }
12510 
12511                 if (memop & MO_SIGN) {
12512                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
12513                 } else {
12514                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
12515                 }
12516                 if (satop) {
12517                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
12518                                                       tcg_passres, tcg_passres);
12519                 }
12520 
12521                 if (opcode == 0xa || opcode == 0xb) {
12522                     continue;
12523                 }
12524 
12525                 /* Accumulating op: handle accumulate step */
12526                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12527 
12528                 switch (opcode) {
12529                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12530                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
12531                                              tcg_passres);
12532                     break;
12533                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12534                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
12535                                              tcg_passres);
12536                     break;
12537                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12538                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
12539                     /* fall through */
12540                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12541                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
12542                                                       tcg_res[pass],
12543                                                       tcg_passres);
12544                     break;
12545                 default:
12546                     g_assert_not_reached();
12547                 }
12548             }
12549 
12550             if (is_scalar) {
12551                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
12552             }
12553         }
12554 
12555         if (is_scalar) {
12556             tcg_res[1] = tcg_constant_i64(0);
12557         }
12558 
12559         for (pass = 0; pass < 2; pass++) {
12560             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12561         }
12562     }
12563 }
12564 
12565 /* C3.6 Data processing - SIMD, inc Crypto
12566  *
12567  * As the decode gets a little complex we are using a table based
12568  * approach for this part of the decode.
12569  */
12570 static const AArch64DecodeTable data_proc_simd[] = {
12571     /* pattern  ,  mask     ,  fn                        */
12572     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
12573     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
12574     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
12575     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
12576     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
12577     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
12578     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
12579     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
12580     { 0x0e000000, 0xbf208c00, disas_simd_tb },
12581     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
12582     { 0x2e000000, 0xbf208400, disas_simd_ext },
12583     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
12584     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
12585     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
12586     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
12587     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
12588     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
12589     { 0x00000000, 0x00000000, NULL }
12590 };
12591 
12592 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
12593 {
12594     /* Note that this is called with all non-FP cases from
12595      * table C3-6 so it must UNDEF for entries not specifically
12596      * allocated to instructions in that table.
12597      */
12598     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
12599     if (fn) {
12600         fn(s, insn);
12601     } else {
12602         unallocated_encoding(s);
12603     }
12604 }
12605 
12606 /* C3.6 Data processing - SIMD and floating point */
12607 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
12608 {
12609     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
12610         disas_data_proc_fp(s, insn);
12611     } else {
12612         /* SIMD, including crypto */
12613         disas_data_proc_simd(s, insn);
12614     }
12615 }
12616 
12617 static bool trans_OK(DisasContext *s, arg_OK *a)
12618 {
12619     return true;
12620 }
12621 
12622 static bool trans_FAIL(DisasContext *s, arg_OK *a)
12623 {
12624     s->is_nonstreaming = true;
12625     return true;
12626 }
12627 
12628 /**
12629  * is_guarded_page:
12630  * @env: The cpu environment
12631  * @s: The DisasContext
12632  *
12633  * Return true if the page is guarded.
12634  */
12635 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
12636 {
12637     uint64_t addr = s->base.pc_first;
12638 #ifdef CONFIG_USER_ONLY
12639     return page_get_flags(addr) & PAGE_BTI;
12640 #else
12641     CPUTLBEntryFull *full;
12642     void *host;
12643     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
12644     int flags;
12645 
12646     /*
12647      * We test this immediately after reading an insn, which means
12648      * that the TLB entry must be present and valid, and thus this
12649      * access will never raise an exception.
12650      */
12651     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
12652                               false, &host, &full, 0);
12653     assert(!(flags & TLB_INVALID_MASK));
12654 
12655     return full->extra.arm.guarded;
12656 #endif
12657 }
12658 
12659 /**
12660  * btype_destination_ok:
12661  * @insn: The instruction at the branch destination
12662  * @bt: SCTLR_ELx.BT
12663  * @btype: PSTATE.BTYPE, and is non-zero
12664  *
12665  * On a guarded page, there are a limited number of insns
12666  * that may be present at the branch target:
12667  *   - branch target identifiers,
12668  *   - paciasp, pacibsp,
12669  *   - BRK insn
12670  *   - HLT insn
12671  * Anything else causes a Branch Target Exception.
12672  *
12673  * Return true if the branch is compatible, false to raise BTITRAP.
12674  */
12675 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
12676 {
12677     if ((insn & 0xfffff01fu) == 0xd503201fu) {
12678         /* HINT space */
12679         switch (extract32(insn, 5, 7)) {
12680         case 0b011001: /* PACIASP */
12681         case 0b011011: /* PACIBSP */
12682             /*
12683              * If SCTLR_ELx.BT, then PACI*SP are not compatible
12684              * with btype == 3.  Otherwise all btype are ok.
12685              */
12686             return !bt || btype != 3;
12687         case 0b100000: /* BTI */
12688             /* Not compatible with any btype.  */
12689             return false;
12690         case 0b100010: /* BTI c */
12691             /* Not compatible with btype == 3 */
12692             return btype != 3;
12693         case 0b100100: /* BTI j */
12694             /* Not compatible with btype == 2 */
12695             return btype != 2;
12696         case 0b100110: /* BTI jc */
12697             /* Compatible with any btype.  */
12698             return true;
12699         }
12700     } else {
12701         switch (insn & 0xffe0001fu) {
12702         case 0xd4200000u: /* BRK */
12703         case 0xd4400000u: /* HLT */
12704             /* Give priority to the breakpoint exception.  */
12705             return true;
12706         }
12707     }
12708     return false;
12709 }
12710 
12711 /* C3.1 A64 instruction index by encoding */
12712 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
12713 {
12714     switch (extract32(insn, 25, 4)) {
12715     case 0x5:
12716     case 0xd:      /* Data processing - register */
12717         disas_data_proc_reg(s, insn);
12718         break;
12719     case 0x7:
12720     case 0xf:      /* Data processing - SIMD and floating point */
12721         disas_data_proc_simd_fp(s, insn);
12722         break;
12723     default:
12724         unallocated_encoding(s);
12725         break;
12726     }
12727 }
12728 
12729 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
12730                                           CPUState *cpu)
12731 {
12732     DisasContext *dc = container_of(dcbase, DisasContext, base);
12733     CPUARMState *env = cpu_env(cpu);
12734     ARMCPU *arm_cpu = env_archcpu(env);
12735     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
12736     int bound, core_mmu_idx;
12737 
12738     dc->isar = &arm_cpu->isar;
12739     dc->condjmp = 0;
12740     dc->pc_save = dc->base.pc_first;
12741     dc->aarch64 = true;
12742     dc->thumb = false;
12743     dc->sctlr_b = 0;
12744     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
12745     dc->condexec_mask = 0;
12746     dc->condexec_cond = 0;
12747     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
12748     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
12749     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
12750     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
12751     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
12752     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
12753 #if !defined(CONFIG_USER_ONLY)
12754     dc->user = (dc->current_el == 0);
12755 #endif
12756     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
12757     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
12758     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
12759     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
12760     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
12761     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
12762     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
12763     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
12764     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
12765     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
12766     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
12767     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
12768     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
12769     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
12770     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
12771     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
12772     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
12773     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
12774     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
12775     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
12776     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
12777     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
12778     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
12779     dc->nv1 = EX_TBFLAG_A64(tb_flags, NV1);
12780     dc->nv2 = EX_TBFLAG_A64(tb_flags, NV2);
12781     dc->nv2_mem_e20 = EX_TBFLAG_A64(tb_flags, NV2_MEM_E20);
12782     dc->nv2_mem_be = EX_TBFLAG_A64(tb_flags, NV2_MEM_BE);
12783     dc->vec_len = 0;
12784     dc->vec_stride = 0;
12785     dc->cp_regs = arm_cpu->cp_regs;
12786     dc->features = env->features;
12787     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
12788     dc->gm_blocksize = arm_cpu->gm_blocksize;
12789 
12790 #ifdef CONFIG_USER_ONLY
12791     /* In sve_probe_page, we assume TBI is enabled. */
12792     tcg_debug_assert(dc->tbid & 1);
12793 #endif
12794 
12795     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
12796 
12797     /* Single step state. The code-generation logic here is:
12798      *  SS_ACTIVE == 0:
12799      *   generate code with no special handling for single-stepping (except
12800      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
12801      *   this happens anyway because those changes are all system register or
12802      *   PSTATE writes).
12803      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
12804      *   emit code for one insn
12805      *   emit code to clear PSTATE.SS
12806      *   emit code to generate software step exception for completed step
12807      *   end TB (as usual for having generated an exception)
12808      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
12809      *   emit code to generate a software step exception
12810      *   end the TB
12811      */
12812     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
12813     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
12814     dc->is_ldex = false;
12815 
12816     /* Bound the number of insns to execute to those left on the page.  */
12817     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
12818 
12819     /* If architectural single step active, limit to 1.  */
12820     if (dc->ss_active) {
12821         bound = 1;
12822     }
12823     dc->base.max_insns = MIN(dc->base.max_insns, bound);
12824 }
12825 
12826 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
12827 {
12828 }
12829 
12830 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
12831 {
12832     DisasContext *dc = container_of(dcbase, DisasContext, base);
12833     target_ulong pc_arg = dc->base.pc_next;
12834 
12835     if (tb_cflags(dcbase->tb) & CF_PCREL) {
12836         pc_arg &= ~TARGET_PAGE_MASK;
12837     }
12838     tcg_gen_insn_start(pc_arg, 0, 0);
12839     dc->insn_start_updated = false;
12840 }
12841 
12842 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
12843 {
12844     DisasContext *s = container_of(dcbase, DisasContext, base);
12845     CPUARMState *env = cpu_env(cpu);
12846     uint64_t pc = s->base.pc_next;
12847     uint32_t insn;
12848 
12849     /* Singlestep exceptions have the highest priority. */
12850     if (s->ss_active && !s->pstate_ss) {
12851         /* Singlestep state is Active-pending.
12852          * If we're in this state at the start of a TB then either
12853          *  a) we just took an exception to an EL which is being debugged
12854          *     and this is the first insn in the exception handler
12855          *  b) debug exceptions were masked and we just unmasked them
12856          *     without changing EL (eg by clearing PSTATE.D)
12857          * In either case we're going to take a swstep exception in the
12858          * "did not step an insn" case, and so the syndrome ISV and EX
12859          * bits should be zero.
12860          */
12861         assert(s->base.num_insns == 1);
12862         gen_swstep_exception(s, 0, 0);
12863         s->base.is_jmp = DISAS_NORETURN;
12864         s->base.pc_next = pc + 4;
12865         return;
12866     }
12867 
12868     if (pc & 3) {
12869         /*
12870          * PC alignment fault.  This has priority over the instruction abort
12871          * that we would receive from a translation fault via arm_ldl_code.
12872          * This should only be possible after an indirect branch, at the
12873          * start of the TB.
12874          */
12875         assert(s->base.num_insns == 1);
12876         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
12877         s->base.is_jmp = DISAS_NORETURN;
12878         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
12879         return;
12880     }
12881 
12882     s->pc_curr = pc;
12883     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
12884     s->insn = insn;
12885     s->base.pc_next = pc + 4;
12886 
12887     s->fp_access_checked = false;
12888     s->sve_access_checked = false;
12889 
12890     if (s->pstate_il) {
12891         /*
12892          * Illegal execution state. This has priority over BTI
12893          * exceptions, but comes after instruction abort exceptions.
12894          */
12895         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
12896         return;
12897     }
12898 
12899     if (dc_isar_feature(aa64_bti, s)) {
12900         if (s->base.num_insns == 1) {
12901             /*
12902              * At the first insn of the TB, compute s->guarded_page.
12903              * We delayed computing this until successfully reading
12904              * the first insn of the TB, above.  This (mostly) ensures
12905              * that the softmmu tlb entry has been populated, and the
12906              * page table GP bit is available.
12907              *
12908              * Note that we need to compute this even if btype == 0,
12909              * because this value is used for BR instructions later
12910              * where ENV is not available.
12911              */
12912             s->guarded_page = is_guarded_page(env, s);
12913 
12914             /* First insn can have btype set to non-zero.  */
12915             tcg_debug_assert(s->btype >= 0);
12916 
12917             /*
12918              * Note that the Branch Target Exception has fairly high
12919              * priority -- below debugging exceptions but above most
12920              * everything else.  This allows us to handle this now
12921              * instead of waiting until the insn is otherwise decoded.
12922              */
12923             if (s->btype != 0
12924                 && s->guarded_page
12925                 && !btype_destination_ok(insn, s->bt, s->btype)) {
12926                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
12927                 return;
12928             }
12929         } else {
12930             /* Not the first insn: btype must be 0.  */
12931             tcg_debug_assert(s->btype == 0);
12932         }
12933     }
12934 
12935     s->is_nonstreaming = false;
12936     if (s->sme_trap_nonstreaming) {
12937         disas_sme_fa64(s, insn);
12938     }
12939 
12940     if (!disas_a64(s, insn) &&
12941         !disas_sme(s, insn) &&
12942         !disas_sve(s, insn)) {
12943         disas_a64_legacy(s, insn);
12944     }
12945 
12946     /*
12947      * After execution of most insns, btype is reset to 0.
12948      * Note that we set btype == -1 when the insn sets btype.
12949      */
12950     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
12951         reset_btype(s);
12952     }
12953 }
12954 
12955 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
12956 {
12957     DisasContext *dc = container_of(dcbase, DisasContext, base);
12958 
12959     if (unlikely(dc->ss_active)) {
12960         /* Note that this means single stepping WFI doesn't halt the CPU.
12961          * For conditional branch insns this is harmless unreachable code as
12962          * gen_goto_tb() has already handled emitting the debug exception
12963          * (and thus a tb-jump is not possible when singlestepping).
12964          */
12965         switch (dc->base.is_jmp) {
12966         default:
12967             gen_a64_update_pc(dc, 4);
12968             /* fall through */
12969         case DISAS_EXIT:
12970         case DISAS_JUMP:
12971             gen_step_complete_exception(dc);
12972             break;
12973         case DISAS_NORETURN:
12974             break;
12975         }
12976     } else {
12977         switch (dc->base.is_jmp) {
12978         case DISAS_NEXT:
12979         case DISAS_TOO_MANY:
12980             gen_goto_tb(dc, 1, 4);
12981             break;
12982         default:
12983         case DISAS_UPDATE_EXIT:
12984             gen_a64_update_pc(dc, 4);
12985             /* fall through */
12986         case DISAS_EXIT:
12987             tcg_gen_exit_tb(NULL, 0);
12988             break;
12989         case DISAS_UPDATE_NOCHAIN:
12990             gen_a64_update_pc(dc, 4);
12991             /* fall through */
12992         case DISAS_JUMP:
12993             tcg_gen_lookup_and_goto_ptr();
12994             break;
12995         case DISAS_NORETURN:
12996         case DISAS_SWI:
12997             break;
12998         case DISAS_WFE:
12999             gen_a64_update_pc(dc, 4);
13000             gen_helper_wfe(tcg_env);
13001             break;
13002         case DISAS_YIELD:
13003             gen_a64_update_pc(dc, 4);
13004             gen_helper_yield(tcg_env);
13005             break;
13006         case DISAS_WFI:
13007             /*
13008              * This is a special case because we don't want to just halt
13009              * the CPU if trying to debug across a WFI.
13010              */
13011             gen_a64_update_pc(dc, 4);
13012             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
13013             /*
13014              * The helper doesn't necessarily throw an exception, but we
13015              * must go back to the main loop to check for interrupts anyway.
13016              */
13017             tcg_gen_exit_tb(NULL, 0);
13018             break;
13019         }
13020     }
13021 }
13022 
13023 const TranslatorOps aarch64_translator_ops = {
13024     .init_disas_context = aarch64_tr_init_disas_context,
13025     .tb_start           = aarch64_tr_tb_start,
13026     .insn_start         = aarch64_tr_insn_start,
13027     .translate_insn     = aarch64_tr_translate_insn,
13028     .tb_stop            = aarch64_tr_tb_stop,
13029 };
13030