xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 44463b96)
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 /*
5870  * Floating-point data-processing (3 source)
5871  */
5872 
5873 static bool do_fmadd(DisasContext *s, arg_rrrr_e *a, bool neg_a, bool neg_n)
5874 {
5875     TCGv_ptr fpst;
5876 
5877     /*
5878      * These are fused multiply-add.  Note that doing the negations here
5879      * as separate steps is correct: an input NaN should come out with
5880      * its sign bit flipped if it is a negated-input.
5881      */
5882     switch (a->esz) {
5883     case MO_64:
5884         if (fp_access_check(s)) {
5885             TCGv_i64 tn = read_fp_dreg(s, a->rn);
5886             TCGv_i64 tm = read_fp_dreg(s, a->rm);
5887             TCGv_i64 ta = read_fp_dreg(s, a->ra);
5888 
5889             if (neg_a) {
5890                 gen_vfp_negd(ta, ta);
5891             }
5892             if (neg_n) {
5893                 gen_vfp_negd(tn, tn);
5894             }
5895             fpst = fpstatus_ptr(FPST_FPCR);
5896             gen_helper_vfp_muladdd(ta, tn, tm, ta, fpst);
5897             write_fp_dreg(s, a->rd, ta);
5898         }
5899         break;
5900 
5901     case MO_32:
5902         if (fp_access_check(s)) {
5903             TCGv_i32 tn = read_fp_sreg(s, a->rn);
5904             TCGv_i32 tm = read_fp_sreg(s, a->rm);
5905             TCGv_i32 ta = read_fp_sreg(s, a->ra);
5906 
5907             if (neg_a) {
5908                 gen_vfp_negs(ta, ta);
5909             }
5910             if (neg_n) {
5911                 gen_vfp_negs(tn, tn);
5912             }
5913             fpst = fpstatus_ptr(FPST_FPCR);
5914             gen_helper_vfp_muladds(ta, tn, tm, ta, fpst);
5915             write_fp_sreg(s, a->rd, ta);
5916         }
5917         break;
5918 
5919     case MO_16:
5920         if (!dc_isar_feature(aa64_fp16, s)) {
5921             return false;
5922         }
5923         if (fp_access_check(s)) {
5924             TCGv_i32 tn = read_fp_hreg(s, a->rn);
5925             TCGv_i32 tm = read_fp_hreg(s, a->rm);
5926             TCGv_i32 ta = read_fp_hreg(s, a->ra);
5927 
5928             if (neg_a) {
5929                 gen_vfp_negh(ta, ta);
5930             }
5931             if (neg_n) {
5932                 gen_vfp_negh(tn, tn);
5933             }
5934             fpst = fpstatus_ptr(FPST_FPCR_F16);
5935             gen_helper_advsimd_muladdh(ta, tn, tm, ta, fpst);
5936             write_fp_sreg(s, a->rd, ta);
5937         }
5938         break;
5939 
5940     default:
5941         return false;
5942     }
5943     return true;
5944 }
5945 
5946 TRANS(FMADD, do_fmadd, a, false, false)
5947 TRANS(FNMADD, do_fmadd, a, true, true)
5948 TRANS(FMSUB, do_fmadd, a, false, true)
5949 TRANS(FNMSUB, do_fmadd, a, true, false)
5950 
5951 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
5952  * Note that it is the caller's responsibility to ensure that the
5953  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
5954  * mandated semantics for out of range shifts.
5955  */
5956 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
5957                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
5958 {
5959     switch (shift_type) {
5960     case A64_SHIFT_TYPE_LSL:
5961         tcg_gen_shl_i64(dst, src, shift_amount);
5962         break;
5963     case A64_SHIFT_TYPE_LSR:
5964         tcg_gen_shr_i64(dst, src, shift_amount);
5965         break;
5966     case A64_SHIFT_TYPE_ASR:
5967         if (!sf) {
5968             tcg_gen_ext32s_i64(dst, src);
5969         }
5970         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
5971         break;
5972     case A64_SHIFT_TYPE_ROR:
5973         if (sf) {
5974             tcg_gen_rotr_i64(dst, src, shift_amount);
5975         } else {
5976             TCGv_i32 t0, t1;
5977             t0 = tcg_temp_new_i32();
5978             t1 = tcg_temp_new_i32();
5979             tcg_gen_extrl_i64_i32(t0, src);
5980             tcg_gen_extrl_i64_i32(t1, shift_amount);
5981             tcg_gen_rotr_i32(t0, t0, t1);
5982             tcg_gen_extu_i32_i64(dst, t0);
5983         }
5984         break;
5985     default:
5986         assert(FALSE); /* all shift types should be handled */
5987         break;
5988     }
5989 
5990     if (!sf) { /* zero extend final result */
5991         tcg_gen_ext32u_i64(dst, dst);
5992     }
5993 }
5994 
5995 /* Shift a TCGv src by immediate, put result in dst.
5996  * The shift amount must be in range (this should always be true as the
5997  * relevant instructions will UNDEF on bad shift immediates).
5998  */
5999 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
6000                           enum a64_shift_type shift_type, unsigned int shift_i)
6001 {
6002     assert(shift_i < (sf ? 64 : 32));
6003 
6004     if (shift_i == 0) {
6005         tcg_gen_mov_i64(dst, src);
6006     } else {
6007         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
6008     }
6009 }
6010 
6011 /* Logical (shifted register)
6012  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
6013  * +----+-----+-----------+-------+---+------+--------+------+------+
6014  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
6015  * +----+-----+-----------+-------+---+------+--------+------+------+
6016  */
6017 static void disas_logic_reg(DisasContext *s, uint32_t insn)
6018 {
6019     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
6020     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
6021 
6022     sf = extract32(insn, 31, 1);
6023     opc = extract32(insn, 29, 2);
6024     shift_type = extract32(insn, 22, 2);
6025     invert = extract32(insn, 21, 1);
6026     rm = extract32(insn, 16, 5);
6027     shift_amount = extract32(insn, 10, 6);
6028     rn = extract32(insn, 5, 5);
6029     rd = extract32(insn, 0, 5);
6030 
6031     if (!sf && (shift_amount & (1 << 5))) {
6032         unallocated_encoding(s);
6033         return;
6034     }
6035 
6036     tcg_rd = cpu_reg(s, rd);
6037 
6038     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
6039         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
6040          * register-register MOV and MVN, so it is worth special casing.
6041          */
6042         tcg_rm = cpu_reg(s, rm);
6043         if (invert) {
6044             tcg_gen_not_i64(tcg_rd, tcg_rm);
6045             if (!sf) {
6046                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6047             }
6048         } else {
6049             if (sf) {
6050                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
6051             } else {
6052                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
6053             }
6054         }
6055         return;
6056     }
6057 
6058     tcg_rm = read_cpu_reg(s, rm, sf);
6059 
6060     if (shift_amount) {
6061         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
6062     }
6063 
6064     tcg_rn = cpu_reg(s, rn);
6065 
6066     switch (opc | (invert << 2)) {
6067     case 0: /* AND */
6068     case 3: /* ANDS */
6069         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
6070         break;
6071     case 1: /* ORR */
6072         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
6073         break;
6074     case 2: /* EOR */
6075         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
6076         break;
6077     case 4: /* BIC */
6078     case 7: /* BICS */
6079         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
6080         break;
6081     case 5: /* ORN */
6082         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
6083         break;
6084     case 6: /* EON */
6085         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
6086         break;
6087     default:
6088         assert(FALSE);
6089         break;
6090     }
6091 
6092     if (!sf) {
6093         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6094     }
6095 
6096     if (opc == 3) {
6097         gen_logic_CC(sf, tcg_rd);
6098     }
6099 }
6100 
6101 /*
6102  * Add/subtract (extended register)
6103  *
6104  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
6105  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
6106  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
6107  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
6108  *
6109  *  sf: 0 -> 32bit, 1 -> 64bit
6110  *  op: 0 -> add  , 1 -> sub
6111  *   S: 1 -> set flags
6112  * opt: 00
6113  * option: extension type (see DecodeRegExtend)
6114  * imm3: optional shift to Rm
6115  *
6116  * Rd = Rn + LSL(extend(Rm), amount)
6117  */
6118 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
6119 {
6120     int rd = extract32(insn, 0, 5);
6121     int rn = extract32(insn, 5, 5);
6122     int imm3 = extract32(insn, 10, 3);
6123     int option = extract32(insn, 13, 3);
6124     int rm = extract32(insn, 16, 5);
6125     int opt = extract32(insn, 22, 2);
6126     bool setflags = extract32(insn, 29, 1);
6127     bool sub_op = extract32(insn, 30, 1);
6128     bool sf = extract32(insn, 31, 1);
6129 
6130     TCGv_i64 tcg_rm, tcg_rn; /* temps */
6131     TCGv_i64 tcg_rd;
6132     TCGv_i64 tcg_result;
6133 
6134     if (imm3 > 4 || opt != 0) {
6135         unallocated_encoding(s);
6136         return;
6137     }
6138 
6139     /* non-flag setting ops may use SP */
6140     if (!setflags) {
6141         tcg_rd = cpu_reg_sp(s, rd);
6142     } else {
6143         tcg_rd = cpu_reg(s, rd);
6144     }
6145     tcg_rn = read_cpu_reg_sp(s, rn, sf);
6146 
6147     tcg_rm = read_cpu_reg(s, rm, sf);
6148     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
6149 
6150     tcg_result = tcg_temp_new_i64();
6151 
6152     if (!setflags) {
6153         if (sub_op) {
6154             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
6155         } else {
6156             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
6157         }
6158     } else {
6159         if (sub_op) {
6160             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
6161         } else {
6162             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
6163         }
6164     }
6165 
6166     if (sf) {
6167         tcg_gen_mov_i64(tcg_rd, tcg_result);
6168     } else {
6169         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
6170     }
6171 }
6172 
6173 /*
6174  * Add/subtract (shifted register)
6175  *
6176  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
6177  * +--+--+--+-----------+-----+--+-------+---------+------+------+
6178  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
6179  * +--+--+--+-----------+-----+--+-------+---------+------+------+
6180  *
6181  *    sf: 0 -> 32bit, 1 -> 64bit
6182  *    op: 0 -> add  , 1 -> sub
6183  *     S: 1 -> set flags
6184  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
6185  *  imm6: Shift amount to apply to Rm before the add/sub
6186  */
6187 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
6188 {
6189     int rd = extract32(insn, 0, 5);
6190     int rn = extract32(insn, 5, 5);
6191     int imm6 = extract32(insn, 10, 6);
6192     int rm = extract32(insn, 16, 5);
6193     int shift_type = extract32(insn, 22, 2);
6194     bool setflags = extract32(insn, 29, 1);
6195     bool sub_op = extract32(insn, 30, 1);
6196     bool sf = extract32(insn, 31, 1);
6197 
6198     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6199     TCGv_i64 tcg_rn, tcg_rm;
6200     TCGv_i64 tcg_result;
6201 
6202     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
6203         unallocated_encoding(s);
6204         return;
6205     }
6206 
6207     tcg_rn = read_cpu_reg(s, rn, sf);
6208     tcg_rm = read_cpu_reg(s, rm, sf);
6209 
6210     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
6211 
6212     tcg_result = tcg_temp_new_i64();
6213 
6214     if (!setflags) {
6215         if (sub_op) {
6216             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
6217         } else {
6218             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
6219         }
6220     } else {
6221         if (sub_op) {
6222             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
6223         } else {
6224             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
6225         }
6226     }
6227 
6228     if (sf) {
6229         tcg_gen_mov_i64(tcg_rd, tcg_result);
6230     } else {
6231         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
6232     }
6233 }
6234 
6235 /* Data-processing (3 source)
6236  *
6237  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
6238  *  +--+------+-----------+------+------+----+------+------+------+
6239  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6240  *  +--+------+-----------+------+------+----+------+------+------+
6241  */
6242 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
6243 {
6244     int rd = extract32(insn, 0, 5);
6245     int rn = extract32(insn, 5, 5);
6246     int ra = extract32(insn, 10, 5);
6247     int rm = extract32(insn, 16, 5);
6248     int op_id = (extract32(insn, 29, 3) << 4) |
6249         (extract32(insn, 21, 3) << 1) |
6250         extract32(insn, 15, 1);
6251     bool sf = extract32(insn, 31, 1);
6252     bool is_sub = extract32(op_id, 0, 1);
6253     bool is_high = extract32(op_id, 2, 1);
6254     bool is_signed = false;
6255     TCGv_i64 tcg_op1;
6256     TCGv_i64 tcg_op2;
6257     TCGv_i64 tcg_tmp;
6258 
6259     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
6260     switch (op_id) {
6261     case 0x42: /* SMADDL */
6262     case 0x43: /* SMSUBL */
6263     case 0x44: /* SMULH */
6264         is_signed = true;
6265         break;
6266     case 0x0: /* MADD (32bit) */
6267     case 0x1: /* MSUB (32bit) */
6268     case 0x40: /* MADD (64bit) */
6269     case 0x41: /* MSUB (64bit) */
6270     case 0x4a: /* UMADDL */
6271     case 0x4b: /* UMSUBL */
6272     case 0x4c: /* UMULH */
6273         break;
6274     default:
6275         unallocated_encoding(s);
6276         return;
6277     }
6278 
6279     if (is_high) {
6280         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
6281         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6282         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6283         TCGv_i64 tcg_rm = cpu_reg(s, rm);
6284 
6285         if (is_signed) {
6286             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
6287         } else {
6288             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
6289         }
6290         return;
6291     }
6292 
6293     tcg_op1 = tcg_temp_new_i64();
6294     tcg_op2 = tcg_temp_new_i64();
6295     tcg_tmp = tcg_temp_new_i64();
6296 
6297     if (op_id < 0x42) {
6298         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
6299         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
6300     } else {
6301         if (is_signed) {
6302             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
6303             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
6304         } else {
6305             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
6306             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
6307         }
6308     }
6309 
6310     if (ra == 31 && !is_sub) {
6311         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
6312         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
6313     } else {
6314         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
6315         if (is_sub) {
6316             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
6317         } else {
6318             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
6319         }
6320     }
6321 
6322     if (!sf) {
6323         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
6324     }
6325 }
6326 
6327 /* Add/subtract (with carry)
6328  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
6329  * +--+--+--+------------------------+------+-------------+------+-----+
6330  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
6331  * +--+--+--+------------------------+------+-------------+------+-----+
6332  */
6333 
6334 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
6335 {
6336     unsigned int sf, op, setflags, rm, rn, rd;
6337     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
6338 
6339     sf = extract32(insn, 31, 1);
6340     op = extract32(insn, 30, 1);
6341     setflags = extract32(insn, 29, 1);
6342     rm = extract32(insn, 16, 5);
6343     rn = extract32(insn, 5, 5);
6344     rd = extract32(insn, 0, 5);
6345 
6346     tcg_rd = cpu_reg(s, rd);
6347     tcg_rn = cpu_reg(s, rn);
6348 
6349     if (op) {
6350         tcg_y = tcg_temp_new_i64();
6351         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
6352     } else {
6353         tcg_y = cpu_reg(s, rm);
6354     }
6355 
6356     if (setflags) {
6357         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
6358     } else {
6359         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
6360     }
6361 }
6362 
6363 /*
6364  * Rotate right into flags
6365  *  31 30 29                21       15          10      5  4      0
6366  * +--+--+--+-----------------+--------+-----------+------+--+------+
6367  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
6368  * +--+--+--+-----------------+--------+-----------+------+--+------+
6369  */
6370 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
6371 {
6372     int mask = extract32(insn, 0, 4);
6373     int o2 = extract32(insn, 4, 1);
6374     int rn = extract32(insn, 5, 5);
6375     int imm6 = extract32(insn, 15, 6);
6376     int sf_op_s = extract32(insn, 29, 3);
6377     TCGv_i64 tcg_rn;
6378     TCGv_i32 nzcv;
6379 
6380     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
6381         unallocated_encoding(s);
6382         return;
6383     }
6384 
6385     tcg_rn = read_cpu_reg(s, rn, 1);
6386     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
6387 
6388     nzcv = tcg_temp_new_i32();
6389     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
6390 
6391     if (mask & 8) { /* N */
6392         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
6393     }
6394     if (mask & 4) { /* Z */
6395         tcg_gen_not_i32(cpu_ZF, nzcv);
6396         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
6397     }
6398     if (mask & 2) { /* C */
6399         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
6400     }
6401     if (mask & 1) { /* V */
6402         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
6403     }
6404 }
6405 
6406 /*
6407  * Evaluate into flags
6408  *  31 30 29                21        15   14        10      5  4      0
6409  * +--+--+--+-----------------+---------+----+---------+------+--+------+
6410  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
6411  * +--+--+--+-----------------+---------+----+---------+------+--+------+
6412  */
6413 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
6414 {
6415     int o3_mask = extract32(insn, 0, 5);
6416     int rn = extract32(insn, 5, 5);
6417     int o2 = extract32(insn, 15, 6);
6418     int sz = extract32(insn, 14, 1);
6419     int sf_op_s = extract32(insn, 29, 3);
6420     TCGv_i32 tmp;
6421     int shift;
6422 
6423     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
6424         !dc_isar_feature(aa64_condm_4, s)) {
6425         unallocated_encoding(s);
6426         return;
6427     }
6428     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
6429 
6430     tmp = tcg_temp_new_i32();
6431     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
6432     tcg_gen_shli_i32(cpu_NF, tmp, shift);
6433     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
6434     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
6435     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
6436 }
6437 
6438 /* Conditional compare (immediate / register)
6439  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
6440  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
6441  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
6442  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
6443  *        [1]                             y                [0]       [0]
6444  */
6445 static void disas_cc(DisasContext *s, uint32_t insn)
6446 {
6447     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
6448     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
6449     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
6450     DisasCompare c;
6451 
6452     if (!extract32(insn, 29, 1)) {
6453         unallocated_encoding(s);
6454         return;
6455     }
6456     if (insn & (1 << 10 | 1 << 4)) {
6457         unallocated_encoding(s);
6458         return;
6459     }
6460     sf = extract32(insn, 31, 1);
6461     op = extract32(insn, 30, 1);
6462     is_imm = extract32(insn, 11, 1);
6463     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
6464     cond = extract32(insn, 12, 4);
6465     rn = extract32(insn, 5, 5);
6466     nzcv = extract32(insn, 0, 4);
6467 
6468     /* Set T0 = !COND.  */
6469     tcg_t0 = tcg_temp_new_i32();
6470     arm_test_cc(&c, cond);
6471     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
6472 
6473     /* Load the arguments for the new comparison.  */
6474     if (is_imm) {
6475         tcg_y = tcg_temp_new_i64();
6476         tcg_gen_movi_i64(tcg_y, y);
6477     } else {
6478         tcg_y = cpu_reg(s, y);
6479     }
6480     tcg_rn = cpu_reg(s, rn);
6481 
6482     /* Set the flags for the new comparison.  */
6483     tcg_tmp = tcg_temp_new_i64();
6484     if (op) {
6485         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
6486     } else {
6487         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
6488     }
6489 
6490     /* If COND was false, force the flags to #nzcv.  Compute two masks
6491      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
6492      * For tcg hosts that support ANDC, we can make do with just T1.
6493      * In either case, allow the tcg optimizer to delete any unused mask.
6494      */
6495     tcg_t1 = tcg_temp_new_i32();
6496     tcg_t2 = tcg_temp_new_i32();
6497     tcg_gen_neg_i32(tcg_t1, tcg_t0);
6498     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
6499 
6500     if (nzcv & 8) { /* N */
6501         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
6502     } else {
6503         if (TCG_TARGET_HAS_andc_i32) {
6504             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
6505         } else {
6506             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
6507         }
6508     }
6509     if (nzcv & 4) { /* Z */
6510         if (TCG_TARGET_HAS_andc_i32) {
6511             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
6512         } else {
6513             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
6514         }
6515     } else {
6516         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
6517     }
6518     if (nzcv & 2) { /* C */
6519         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
6520     } else {
6521         if (TCG_TARGET_HAS_andc_i32) {
6522             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
6523         } else {
6524             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
6525         }
6526     }
6527     if (nzcv & 1) { /* V */
6528         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
6529     } else {
6530         if (TCG_TARGET_HAS_andc_i32) {
6531             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
6532         } else {
6533             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
6534         }
6535     }
6536 }
6537 
6538 /* Conditional select
6539  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
6540  * +----+----+---+-----------------+------+------+-----+------+------+
6541  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
6542  * +----+----+---+-----------------+------+------+-----+------+------+
6543  */
6544 static void disas_cond_select(DisasContext *s, uint32_t insn)
6545 {
6546     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
6547     TCGv_i64 tcg_rd, zero;
6548     DisasCompare64 c;
6549 
6550     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
6551         /* S == 1 or op2<1> == 1 */
6552         unallocated_encoding(s);
6553         return;
6554     }
6555     sf = extract32(insn, 31, 1);
6556     else_inv = extract32(insn, 30, 1);
6557     rm = extract32(insn, 16, 5);
6558     cond = extract32(insn, 12, 4);
6559     else_inc = extract32(insn, 10, 1);
6560     rn = extract32(insn, 5, 5);
6561     rd = extract32(insn, 0, 5);
6562 
6563     tcg_rd = cpu_reg(s, rd);
6564 
6565     a64_test_cc(&c, cond);
6566     zero = tcg_constant_i64(0);
6567 
6568     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
6569         /* CSET & CSETM.  */
6570         if (else_inv) {
6571             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
6572                                    tcg_rd, c.value, zero);
6573         } else {
6574             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
6575                                 tcg_rd, c.value, zero);
6576         }
6577     } else {
6578         TCGv_i64 t_true = cpu_reg(s, rn);
6579         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
6580         if (else_inv && else_inc) {
6581             tcg_gen_neg_i64(t_false, t_false);
6582         } else if (else_inv) {
6583             tcg_gen_not_i64(t_false, t_false);
6584         } else if (else_inc) {
6585             tcg_gen_addi_i64(t_false, t_false, 1);
6586         }
6587         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
6588     }
6589 
6590     if (!sf) {
6591         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6592     }
6593 }
6594 
6595 static void handle_clz(DisasContext *s, unsigned int sf,
6596                        unsigned int rn, unsigned int rd)
6597 {
6598     TCGv_i64 tcg_rd, tcg_rn;
6599     tcg_rd = cpu_reg(s, rd);
6600     tcg_rn = cpu_reg(s, rn);
6601 
6602     if (sf) {
6603         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
6604     } else {
6605         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6606         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6607         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
6608         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6609     }
6610 }
6611 
6612 static void handle_cls(DisasContext *s, unsigned int sf,
6613                        unsigned int rn, unsigned int rd)
6614 {
6615     TCGv_i64 tcg_rd, tcg_rn;
6616     tcg_rd = cpu_reg(s, rd);
6617     tcg_rn = cpu_reg(s, rn);
6618 
6619     if (sf) {
6620         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
6621     } else {
6622         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6623         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6624         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
6625         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6626     }
6627 }
6628 
6629 static void handle_rbit(DisasContext *s, unsigned int sf,
6630                         unsigned int rn, unsigned int rd)
6631 {
6632     TCGv_i64 tcg_rd, tcg_rn;
6633     tcg_rd = cpu_reg(s, rd);
6634     tcg_rn = cpu_reg(s, rn);
6635 
6636     if (sf) {
6637         gen_helper_rbit64(tcg_rd, tcg_rn);
6638     } else {
6639         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6640         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6641         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
6642         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6643     }
6644 }
6645 
6646 /* REV with sf==1, opcode==3 ("REV64") */
6647 static void handle_rev64(DisasContext *s, unsigned int sf,
6648                          unsigned int rn, unsigned int rd)
6649 {
6650     if (!sf) {
6651         unallocated_encoding(s);
6652         return;
6653     }
6654     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
6655 }
6656 
6657 /* REV with sf==0, opcode==2
6658  * REV32 (sf==1, opcode==2)
6659  */
6660 static void handle_rev32(DisasContext *s, unsigned int sf,
6661                          unsigned int rn, unsigned int rd)
6662 {
6663     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6664     TCGv_i64 tcg_rn = cpu_reg(s, rn);
6665 
6666     if (sf) {
6667         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
6668         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
6669     } else {
6670         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
6671     }
6672 }
6673 
6674 /* REV16 (opcode==1) */
6675 static void handle_rev16(DisasContext *s, unsigned int sf,
6676                          unsigned int rn, unsigned int rd)
6677 {
6678     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6679     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6680     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
6681     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
6682 
6683     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
6684     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
6685     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
6686     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
6687     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
6688 }
6689 
6690 /* Data-processing (1 source)
6691  *   31  30  29  28             21 20     16 15    10 9    5 4    0
6692  * +----+---+---+-----------------+---------+--------+------+------+
6693  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
6694  * +----+---+---+-----------------+---------+--------+------+------+
6695  */
6696 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
6697 {
6698     unsigned int sf, opcode, opcode2, rn, rd;
6699     TCGv_i64 tcg_rd;
6700 
6701     if (extract32(insn, 29, 1)) {
6702         unallocated_encoding(s);
6703         return;
6704     }
6705 
6706     sf = extract32(insn, 31, 1);
6707     opcode = extract32(insn, 10, 6);
6708     opcode2 = extract32(insn, 16, 5);
6709     rn = extract32(insn, 5, 5);
6710     rd = extract32(insn, 0, 5);
6711 
6712 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
6713 
6714     switch (MAP(sf, opcode2, opcode)) {
6715     case MAP(0, 0x00, 0x00): /* RBIT */
6716     case MAP(1, 0x00, 0x00):
6717         handle_rbit(s, sf, rn, rd);
6718         break;
6719     case MAP(0, 0x00, 0x01): /* REV16 */
6720     case MAP(1, 0x00, 0x01):
6721         handle_rev16(s, sf, rn, rd);
6722         break;
6723     case MAP(0, 0x00, 0x02): /* REV/REV32 */
6724     case MAP(1, 0x00, 0x02):
6725         handle_rev32(s, sf, rn, rd);
6726         break;
6727     case MAP(1, 0x00, 0x03): /* REV64 */
6728         handle_rev64(s, sf, rn, rd);
6729         break;
6730     case MAP(0, 0x00, 0x04): /* CLZ */
6731     case MAP(1, 0x00, 0x04):
6732         handle_clz(s, sf, rn, rd);
6733         break;
6734     case MAP(0, 0x00, 0x05): /* CLS */
6735     case MAP(1, 0x00, 0x05):
6736         handle_cls(s, sf, rn, rd);
6737         break;
6738     case MAP(1, 0x01, 0x00): /* PACIA */
6739         if (s->pauth_active) {
6740             tcg_rd = cpu_reg(s, rd);
6741             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6742         } else if (!dc_isar_feature(aa64_pauth, s)) {
6743             goto do_unallocated;
6744         }
6745         break;
6746     case MAP(1, 0x01, 0x01): /* PACIB */
6747         if (s->pauth_active) {
6748             tcg_rd = cpu_reg(s, rd);
6749             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6750         } else if (!dc_isar_feature(aa64_pauth, s)) {
6751             goto do_unallocated;
6752         }
6753         break;
6754     case MAP(1, 0x01, 0x02): /* PACDA */
6755         if (s->pauth_active) {
6756             tcg_rd = cpu_reg(s, rd);
6757             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6758         } else if (!dc_isar_feature(aa64_pauth, s)) {
6759             goto do_unallocated;
6760         }
6761         break;
6762     case MAP(1, 0x01, 0x03): /* PACDB */
6763         if (s->pauth_active) {
6764             tcg_rd = cpu_reg(s, rd);
6765             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6766         } else if (!dc_isar_feature(aa64_pauth, s)) {
6767             goto do_unallocated;
6768         }
6769         break;
6770     case MAP(1, 0x01, 0x04): /* AUTIA */
6771         if (s->pauth_active) {
6772             tcg_rd = cpu_reg(s, rd);
6773             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6774         } else if (!dc_isar_feature(aa64_pauth, s)) {
6775             goto do_unallocated;
6776         }
6777         break;
6778     case MAP(1, 0x01, 0x05): /* AUTIB */
6779         if (s->pauth_active) {
6780             tcg_rd = cpu_reg(s, rd);
6781             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6782         } else if (!dc_isar_feature(aa64_pauth, s)) {
6783             goto do_unallocated;
6784         }
6785         break;
6786     case MAP(1, 0x01, 0x06): /* AUTDA */
6787         if (s->pauth_active) {
6788             tcg_rd = cpu_reg(s, rd);
6789             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6790         } else if (!dc_isar_feature(aa64_pauth, s)) {
6791             goto do_unallocated;
6792         }
6793         break;
6794     case MAP(1, 0x01, 0x07): /* AUTDB */
6795         if (s->pauth_active) {
6796             tcg_rd = cpu_reg(s, rd);
6797             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6798         } else if (!dc_isar_feature(aa64_pauth, s)) {
6799             goto do_unallocated;
6800         }
6801         break;
6802     case MAP(1, 0x01, 0x08): /* PACIZA */
6803         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6804             goto do_unallocated;
6805         } else if (s->pauth_active) {
6806             tcg_rd = cpu_reg(s, rd);
6807             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6808         }
6809         break;
6810     case MAP(1, 0x01, 0x09): /* PACIZB */
6811         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6812             goto do_unallocated;
6813         } else if (s->pauth_active) {
6814             tcg_rd = cpu_reg(s, rd);
6815             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6816         }
6817         break;
6818     case MAP(1, 0x01, 0x0a): /* PACDZA */
6819         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6820             goto do_unallocated;
6821         } else if (s->pauth_active) {
6822             tcg_rd = cpu_reg(s, rd);
6823             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6824         }
6825         break;
6826     case MAP(1, 0x01, 0x0b): /* PACDZB */
6827         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6828             goto do_unallocated;
6829         } else if (s->pauth_active) {
6830             tcg_rd = cpu_reg(s, rd);
6831             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6832         }
6833         break;
6834     case MAP(1, 0x01, 0x0c): /* AUTIZA */
6835         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6836             goto do_unallocated;
6837         } else if (s->pauth_active) {
6838             tcg_rd = cpu_reg(s, rd);
6839             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6840         }
6841         break;
6842     case MAP(1, 0x01, 0x0d): /* AUTIZB */
6843         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6844             goto do_unallocated;
6845         } else if (s->pauth_active) {
6846             tcg_rd = cpu_reg(s, rd);
6847             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6848         }
6849         break;
6850     case MAP(1, 0x01, 0x0e): /* AUTDZA */
6851         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6852             goto do_unallocated;
6853         } else if (s->pauth_active) {
6854             tcg_rd = cpu_reg(s, rd);
6855             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6856         }
6857         break;
6858     case MAP(1, 0x01, 0x0f): /* AUTDZB */
6859         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6860             goto do_unallocated;
6861         } else if (s->pauth_active) {
6862             tcg_rd = cpu_reg(s, rd);
6863             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6864         }
6865         break;
6866     case MAP(1, 0x01, 0x10): /* XPACI */
6867         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6868             goto do_unallocated;
6869         } else if (s->pauth_active) {
6870             tcg_rd = cpu_reg(s, rd);
6871             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
6872         }
6873         break;
6874     case MAP(1, 0x01, 0x11): /* XPACD */
6875         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6876             goto do_unallocated;
6877         } else if (s->pauth_active) {
6878             tcg_rd = cpu_reg(s, rd);
6879             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
6880         }
6881         break;
6882     default:
6883     do_unallocated:
6884         unallocated_encoding(s);
6885         break;
6886     }
6887 
6888 #undef MAP
6889 }
6890 
6891 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
6892                        unsigned int rm, unsigned int rn, unsigned int rd)
6893 {
6894     TCGv_i64 tcg_n, tcg_m, tcg_rd;
6895     tcg_rd = cpu_reg(s, rd);
6896 
6897     if (!sf && is_signed) {
6898         tcg_n = tcg_temp_new_i64();
6899         tcg_m = tcg_temp_new_i64();
6900         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
6901         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
6902     } else {
6903         tcg_n = read_cpu_reg(s, rn, sf);
6904         tcg_m = read_cpu_reg(s, rm, sf);
6905     }
6906 
6907     if (is_signed) {
6908         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
6909     } else {
6910         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
6911     }
6912 
6913     if (!sf) { /* zero extend final result */
6914         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6915     }
6916 }
6917 
6918 /* LSLV, LSRV, ASRV, RORV */
6919 static void handle_shift_reg(DisasContext *s,
6920                              enum a64_shift_type shift_type, unsigned int sf,
6921                              unsigned int rm, unsigned int rn, unsigned int rd)
6922 {
6923     TCGv_i64 tcg_shift = tcg_temp_new_i64();
6924     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6925     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
6926 
6927     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
6928     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
6929 }
6930 
6931 /* CRC32[BHWX], CRC32C[BHWX] */
6932 static void handle_crc32(DisasContext *s,
6933                          unsigned int sf, unsigned int sz, bool crc32c,
6934                          unsigned int rm, unsigned int rn, unsigned int rd)
6935 {
6936     TCGv_i64 tcg_acc, tcg_val;
6937     TCGv_i32 tcg_bytes;
6938 
6939     if (!dc_isar_feature(aa64_crc32, s)
6940         || (sf == 1 && sz != 3)
6941         || (sf == 0 && sz == 3)) {
6942         unallocated_encoding(s);
6943         return;
6944     }
6945 
6946     if (sz == 3) {
6947         tcg_val = cpu_reg(s, rm);
6948     } else {
6949         uint64_t mask;
6950         switch (sz) {
6951         case 0:
6952             mask = 0xFF;
6953             break;
6954         case 1:
6955             mask = 0xFFFF;
6956             break;
6957         case 2:
6958             mask = 0xFFFFFFFF;
6959             break;
6960         default:
6961             g_assert_not_reached();
6962         }
6963         tcg_val = tcg_temp_new_i64();
6964         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
6965     }
6966 
6967     tcg_acc = cpu_reg(s, rn);
6968     tcg_bytes = tcg_constant_i32(1 << sz);
6969 
6970     if (crc32c) {
6971         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
6972     } else {
6973         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
6974     }
6975 }
6976 
6977 /* Data-processing (2 source)
6978  *   31   30  29 28             21 20  16 15    10 9    5 4    0
6979  * +----+---+---+-----------------+------+--------+------+------+
6980  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
6981  * +----+---+---+-----------------+------+--------+------+------+
6982  */
6983 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
6984 {
6985     unsigned int sf, rm, opcode, rn, rd, setflag;
6986     sf = extract32(insn, 31, 1);
6987     setflag = extract32(insn, 29, 1);
6988     rm = extract32(insn, 16, 5);
6989     opcode = extract32(insn, 10, 6);
6990     rn = extract32(insn, 5, 5);
6991     rd = extract32(insn, 0, 5);
6992 
6993     if (setflag && opcode != 0) {
6994         unallocated_encoding(s);
6995         return;
6996     }
6997 
6998     switch (opcode) {
6999     case 0: /* SUBP(S) */
7000         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
7001             goto do_unallocated;
7002         } else {
7003             TCGv_i64 tcg_n, tcg_m, tcg_d;
7004 
7005             tcg_n = read_cpu_reg_sp(s, rn, true);
7006             tcg_m = read_cpu_reg_sp(s, rm, true);
7007             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
7008             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
7009             tcg_d = cpu_reg(s, rd);
7010 
7011             if (setflag) {
7012                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
7013             } else {
7014                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
7015             }
7016         }
7017         break;
7018     case 2: /* UDIV */
7019         handle_div(s, false, sf, rm, rn, rd);
7020         break;
7021     case 3: /* SDIV */
7022         handle_div(s, true, sf, rm, rn, rd);
7023         break;
7024     case 4: /* IRG */
7025         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
7026             goto do_unallocated;
7027         }
7028         if (s->ata[0]) {
7029             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
7030                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
7031         } else {
7032             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
7033                                              cpu_reg_sp(s, rn));
7034         }
7035         break;
7036     case 5: /* GMI */
7037         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
7038             goto do_unallocated;
7039         } else {
7040             TCGv_i64 t = tcg_temp_new_i64();
7041 
7042             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
7043             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
7044             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
7045         }
7046         break;
7047     case 8: /* LSLV */
7048         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
7049         break;
7050     case 9: /* LSRV */
7051         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
7052         break;
7053     case 10: /* ASRV */
7054         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
7055         break;
7056     case 11: /* RORV */
7057         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
7058         break;
7059     case 12: /* PACGA */
7060         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
7061             goto do_unallocated;
7062         }
7063         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
7064                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
7065         break;
7066     case 16:
7067     case 17:
7068     case 18:
7069     case 19:
7070     case 20:
7071     case 21:
7072     case 22:
7073     case 23: /* CRC32 */
7074     {
7075         int sz = extract32(opcode, 0, 2);
7076         bool crc32c = extract32(opcode, 2, 1);
7077         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
7078         break;
7079     }
7080     default:
7081     do_unallocated:
7082         unallocated_encoding(s);
7083         break;
7084     }
7085 }
7086 
7087 /*
7088  * Data processing - register
7089  *  31  30 29  28      25    21  20  16      10         0
7090  * +--+---+--+---+-------+-----+-------+-------+---------+
7091  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
7092  * +--+---+--+---+-------+-----+-------+-------+---------+
7093  */
7094 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
7095 {
7096     int op0 = extract32(insn, 30, 1);
7097     int op1 = extract32(insn, 28, 1);
7098     int op2 = extract32(insn, 21, 4);
7099     int op3 = extract32(insn, 10, 6);
7100 
7101     if (!op1) {
7102         if (op2 & 8) {
7103             if (op2 & 1) {
7104                 /* Add/sub (extended register) */
7105                 disas_add_sub_ext_reg(s, insn);
7106             } else {
7107                 /* Add/sub (shifted register) */
7108                 disas_add_sub_reg(s, insn);
7109             }
7110         } else {
7111             /* Logical (shifted register) */
7112             disas_logic_reg(s, insn);
7113         }
7114         return;
7115     }
7116 
7117     switch (op2) {
7118     case 0x0:
7119         switch (op3) {
7120         case 0x00: /* Add/subtract (with carry) */
7121             disas_adc_sbc(s, insn);
7122             break;
7123 
7124         case 0x01: /* Rotate right into flags */
7125         case 0x21:
7126             disas_rotate_right_into_flags(s, insn);
7127             break;
7128 
7129         case 0x02: /* Evaluate into flags */
7130         case 0x12:
7131         case 0x22:
7132         case 0x32:
7133             disas_evaluate_into_flags(s, insn);
7134             break;
7135 
7136         default:
7137             goto do_unallocated;
7138         }
7139         break;
7140 
7141     case 0x2: /* Conditional compare */
7142         disas_cc(s, insn); /* both imm and reg forms */
7143         break;
7144 
7145     case 0x4: /* Conditional select */
7146         disas_cond_select(s, insn);
7147         break;
7148 
7149     case 0x6: /* Data-processing */
7150         if (op0) {    /* (1 source) */
7151             disas_data_proc_1src(s, insn);
7152         } else {      /* (2 source) */
7153             disas_data_proc_2src(s, insn);
7154         }
7155         break;
7156     case 0x8 ... 0xf: /* (3 source) */
7157         disas_data_proc_3src(s, insn);
7158         break;
7159 
7160     default:
7161     do_unallocated:
7162         unallocated_encoding(s);
7163         break;
7164     }
7165 }
7166 
7167 static void handle_fp_compare(DisasContext *s, int size,
7168                               unsigned int rn, unsigned int rm,
7169                               bool cmp_with_zero, bool signal_all_nans)
7170 {
7171     TCGv_i64 tcg_flags = tcg_temp_new_i64();
7172     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7173 
7174     if (size == MO_64) {
7175         TCGv_i64 tcg_vn, tcg_vm;
7176 
7177         tcg_vn = read_fp_dreg(s, rn);
7178         if (cmp_with_zero) {
7179             tcg_vm = tcg_constant_i64(0);
7180         } else {
7181             tcg_vm = read_fp_dreg(s, rm);
7182         }
7183         if (signal_all_nans) {
7184             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7185         } else {
7186             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7187         }
7188     } else {
7189         TCGv_i32 tcg_vn = tcg_temp_new_i32();
7190         TCGv_i32 tcg_vm = tcg_temp_new_i32();
7191 
7192         read_vec_element_i32(s, tcg_vn, rn, 0, size);
7193         if (cmp_with_zero) {
7194             tcg_gen_movi_i32(tcg_vm, 0);
7195         } else {
7196             read_vec_element_i32(s, tcg_vm, rm, 0, size);
7197         }
7198 
7199         switch (size) {
7200         case MO_32:
7201             if (signal_all_nans) {
7202                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7203             } else {
7204                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7205             }
7206             break;
7207         case MO_16:
7208             if (signal_all_nans) {
7209                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7210             } else {
7211                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
7212             }
7213             break;
7214         default:
7215             g_assert_not_reached();
7216         }
7217     }
7218 
7219     gen_set_nzcv(tcg_flags);
7220 }
7221 
7222 /* Floating point compare
7223  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
7224  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
7225  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
7226  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
7227  */
7228 static void disas_fp_compare(DisasContext *s, uint32_t insn)
7229 {
7230     unsigned int mos, type, rm, op, rn, opc, op2r;
7231     int size;
7232 
7233     mos = extract32(insn, 29, 3);
7234     type = extract32(insn, 22, 2);
7235     rm = extract32(insn, 16, 5);
7236     op = extract32(insn, 14, 2);
7237     rn = extract32(insn, 5, 5);
7238     opc = extract32(insn, 3, 2);
7239     op2r = extract32(insn, 0, 3);
7240 
7241     if (mos || op || op2r) {
7242         unallocated_encoding(s);
7243         return;
7244     }
7245 
7246     switch (type) {
7247     case 0:
7248         size = MO_32;
7249         break;
7250     case 1:
7251         size = MO_64;
7252         break;
7253     case 3:
7254         size = MO_16;
7255         if (dc_isar_feature(aa64_fp16, s)) {
7256             break;
7257         }
7258         /* fallthru */
7259     default:
7260         unallocated_encoding(s);
7261         return;
7262     }
7263 
7264     if (!fp_access_check(s)) {
7265         return;
7266     }
7267 
7268     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
7269 }
7270 
7271 /* Floating point conditional compare
7272  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
7273  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
7274  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
7275  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
7276  */
7277 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
7278 {
7279     unsigned int mos, type, rm, cond, rn, op, nzcv;
7280     TCGLabel *label_continue = NULL;
7281     int size;
7282 
7283     mos = extract32(insn, 29, 3);
7284     type = extract32(insn, 22, 2);
7285     rm = extract32(insn, 16, 5);
7286     cond = extract32(insn, 12, 4);
7287     rn = extract32(insn, 5, 5);
7288     op = extract32(insn, 4, 1);
7289     nzcv = extract32(insn, 0, 4);
7290 
7291     if (mos) {
7292         unallocated_encoding(s);
7293         return;
7294     }
7295 
7296     switch (type) {
7297     case 0:
7298         size = MO_32;
7299         break;
7300     case 1:
7301         size = MO_64;
7302         break;
7303     case 3:
7304         size = MO_16;
7305         if (dc_isar_feature(aa64_fp16, s)) {
7306             break;
7307         }
7308         /* fallthru */
7309     default:
7310         unallocated_encoding(s);
7311         return;
7312     }
7313 
7314     if (!fp_access_check(s)) {
7315         return;
7316     }
7317 
7318     if (cond < 0x0e) { /* not always */
7319         TCGLabel *label_match = gen_new_label();
7320         label_continue = gen_new_label();
7321         arm_gen_test_cc(cond, label_match);
7322         /* nomatch: */
7323         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
7324         tcg_gen_br(label_continue);
7325         gen_set_label(label_match);
7326     }
7327 
7328     handle_fp_compare(s, size, rn, rm, false, op);
7329 
7330     if (cond < 0x0e) {
7331         gen_set_label(label_continue);
7332     }
7333 }
7334 
7335 /* Floating point conditional select
7336  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
7337  * +---+---+---+-----------+------+---+------+------+-----+------+------+
7338  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
7339  * +---+---+---+-----------+------+---+------+------+-----+------+------+
7340  */
7341 static void disas_fp_csel(DisasContext *s, uint32_t insn)
7342 {
7343     unsigned int mos, type, rm, cond, rn, rd;
7344     TCGv_i64 t_true, t_false;
7345     DisasCompare64 c;
7346     MemOp sz;
7347 
7348     mos = extract32(insn, 29, 3);
7349     type = extract32(insn, 22, 2);
7350     rm = extract32(insn, 16, 5);
7351     cond = extract32(insn, 12, 4);
7352     rn = extract32(insn, 5, 5);
7353     rd = extract32(insn, 0, 5);
7354 
7355     if (mos) {
7356         unallocated_encoding(s);
7357         return;
7358     }
7359 
7360     switch (type) {
7361     case 0:
7362         sz = MO_32;
7363         break;
7364     case 1:
7365         sz = MO_64;
7366         break;
7367     case 3:
7368         sz = MO_16;
7369         if (dc_isar_feature(aa64_fp16, s)) {
7370             break;
7371         }
7372         /* fallthru */
7373     default:
7374         unallocated_encoding(s);
7375         return;
7376     }
7377 
7378     if (!fp_access_check(s)) {
7379         return;
7380     }
7381 
7382     /* Zero extend sreg & hreg inputs to 64 bits now.  */
7383     t_true = tcg_temp_new_i64();
7384     t_false = tcg_temp_new_i64();
7385     read_vec_element(s, t_true, rn, 0, sz);
7386     read_vec_element(s, t_false, rm, 0, sz);
7387 
7388     a64_test_cc(&c, cond);
7389     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
7390                         t_true, t_false);
7391 
7392     /* Note that sregs & hregs write back zeros to the high bits,
7393        and we've already done the zero-extension.  */
7394     write_fp_dreg(s, rd, t_true);
7395 }
7396 
7397 /* Floating-point data-processing (1 source) - half precision */
7398 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
7399 {
7400     TCGv_ptr fpst = NULL;
7401     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
7402     TCGv_i32 tcg_res = tcg_temp_new_i32();
7403 
7404     switch (opcode) {
7405     case 0x0: /* FMOV */
7406         tcg_gen_mov_i32(tcg_res, tcg_op);
7407         break;
7408     case 0x1: /* FABS */
7409         gen_vfp_absh(tcg_res, tcg_op);
7410         break;
7411     case 0x2: /* FNEG */
7412         gen_vfp_negh(tcg_res, tcg_op);
7413         break;
7414     case 0x3: /* FSQRT */
7415         fpst = fpstatus_ptr(FPST_FPCR_F16);
7416         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
7417         break;
7418     case 0x8: /* FRINTN */
7419     case 0x9: /* FRINTP */
7420     case 0xa: /* FRINTM */
7421     case 0xb: /* FRINTZ */
7422     case 0xc: /* FRINTA */
7423     {
7424         TCGv_i32 tcg_rmode;
7425 
7426         fpst = fpstatus_ptr(FPST_FPCR_F16);
7427         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
7428         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
7429         gen_restore_rmode(tcg_rmode, fpst);
7430         break;
7431     }
7432     case 0xe: /* FRINTX */
7433         fpst = fpstatus_ptr(FPST_FPCR_F16);
7434         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
7435         break;
7436     case 0xf: /* FRINTI */
7437         fpst = fpstatus_ptr(FPST_FPCR_F16);
7438         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
7439         break;
7440     default:
7441         g_assert_not_reached();
7442     }
7443 
7444     write_fp_sreg(s, rd, tcg_res);
7445 }
7446 
7447 /* Floating-point data-processing (1 source) - single precision */
7448 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
7449 {
7450     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
7451     TCGv_i32 tcg_op, tcg_res;
7452     TCGv_ptr fpst;
7453     int rmode = -1;
7454 
7455     tcg_op = read_fp_sreg(s, rn);
7456     tcg_res = tcg_temp_new_i32();
7457 
7458     switch (opcode) {
7459     case 0x0: /* FMOV */
7460         tcg_gen_mov_i32(tcg_res, tcg_op);
7461         goto done;
7462     case 0x1: /* FABS */
7463         gen_vfp_abss(tcg_res, tcg_op);
7464         goto done;
7465     case 0x2: /* FNEG */
7466         gen_vfp_negs(tcg_res, tcg_op);
7467         goto done;
7468     case 0x3: /* FSQRT */
7469         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
7470         goto done;
7471     case 0x6: /* BFCVT */
7472         gen_fpst = gen_helper_bfcvt;
7473         break;
7474     case 0x8: /* FRINTN */
7475     case 0x9: /* FRINTP */
7476     case 0xa: /* FRINTM */
7477     case 0xb: /* FRINTZ */
7478     case 0xc: /* FRINTA */
7479         rmode = opcode & 7;
7480         gen_fpst = gen_helper_rints;
7481         break;
7482     case 0xe: /* FRINTX */
7483         gen_fpst = gen_helper_rints_exact;
7484         break;
7485     case 0xf: /* FRINTI */
7486         gen_fpst = gen_helper_rints;
7487         break;
7488     case 0x10: /* FRINT32Z */
7489         rmode = FPROUNDING_ZERO;
7490         gen_fpst = gen_helper_frint32_s;
7491         break;
7492     case 0x11: /* FRINT32X */
7493         gen_fpst = gen_helper_frint32_s;
7494         break;
7495     case 0x12: /* FRINT64Z */
7496         rmode = FPROUNDING_ZERO;
7497         gen_fpst = gen_helper_frint64_s;
7498         break;
7499     case 0x13: /* FRINT64X */
7500         gen_fpst = gen_helper_frint64_s;
7501         break;
7502     default:
7503         g_assert_not_reached();
7504     }
7505 
7506     fpst = fpstatus_ptr(FPST_FPCR);
7507     if (rmode >= 0) {
7508         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
7509         gen_fpst(tcg_res, tcg_op, fpst);
7510         gen_restore_rmode(tcg_rmode, fpst);
7511     } else {
7512         gen_fpst(tcg_res, tcg_op, fpst);
7513     }
7514 
7515  done:
7516     write_fp_sreg(s, rd, tcg_res);
7517 }
7518 
7519 /* Floating-point data-processing (1 source) - double precision */
7520 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
7521 {
7522     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
7523     TCGv_i64 tcg_op, tcg_res;
7524     TCGv_ptr fpst;
7525     int rmode = -1;
7526 
7527     switch (opcode) {
7528     case 0x0: /* FMOV */
7529         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
7530         return;
7531     }
7532 
7533     tcg_op = read_fp_dreg(s, rn);
7534     tcg_res = tcg_temp_new_i64();
7535 
7536     switch (opcode) {
7537     case 0x1: /* FABS */
7538         gen_vfp_absd(tcg_res, tcg_op);
7539         goto done;
7540     case 0x2: /* FNEG */
7541         gen_vfp_negd(tcg_res, tcg_op);
7542         goto done;
7543     case 0x3: /* FSQRT */
7544         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
7545         goto done;
7546     case 0x8: /* FRINTN */
7547     case 0x9: /* FRINTP */
7548     case 0xa: /* FRINTM */
7549     case 0xb: /* FRINTZ */
7550     case 0xc: /* FRINTA */
7551         rmode = opcode & 7;
7552         gen_fpst = gen_helper_rintd;
7553         break;
7554     case 0xe: /* FRINTX */
7555         gen_fpst = gen_helper_rintd_exact;
7556         break;
7557     case 0xf: /* FRINTI */
7558         gen_fpst = gen_helper_rintd;
7559         break;
7560     case 0x10: /* FRINT32Z */
7561         rmode = FPROUNDING_ZERO;
7562         gen_fpst = gen_helper_frint32_d;
7563         break;
7564     case 0x11: /* FRINT32X */
7565         gen_fpst = gen_helper_frint32_d;
7566         break;
7567     case 0x12: /* FRINT64Z */
7568         rmode = FPROUNDING_ZERO;
7569         gen_fpst = gen_helper_frint64_d;
7570         break;
7571     case 0x13: /* FRINT64X */
7572         gen_fpst = gen_helper_frint64_d;
7573         break;
7574     default:
7575         g_assert_not_reached();
7576     }
7577 
7578     fpst = fpstatus_ptr(FPST_FPCR);
7579     if (rmode >= 0) {
7580         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
7581         gen_fpst(tcg_res, tcg_op, fpst);
7582         gen_restore_rmode(tcg_rmode, fpst);
7583     } else {
7584         gen_fpst(tcg_res, tcg_op, fpst);
7585     }
7586 
7587  done:
7588     write_fp_dreg(s, rd, tcg_res);
7589 }
7590 
7591 static void handle_fp_fcvt(DisasContext *s, int opcode,
7592                            int rd, int rn, int dtype, int ntype)
7593 {
7594     switch (ntype) {
7595     case 0x0:
7596     {
7597         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
7598         if (dtype == 1) {
7599             /* Single to double */
7600             TCGv_i64 tcg_rd = tcg_temp_new_i64();
7601             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
7602             write_fp_dreg(s, rd, tcg_rd);
7603         } else {
7604             /* Single to half */
7605             TCGv_i32 tcg_rd = tcg_temp_new_i32();
7606             TCGv_i32 ahp = get_ahp_flag();
7607             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7608 
7609             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
7610             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
7611             write_fp_sreg(s, rd, tcg_rd);
7612         }
7613         break;
7614     }
7615     case 0x1:
7616     {
7617         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7618         TCGv_i32 tcg_rd = tcg_temp_new_i32();
7619         if (dtype == 0) {
7620             /* Double to single */
7621             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
7622         } else {
7623             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7624             TCGv_i32 ahp = get_ahp_flag();
7625             /* Double to half */
7626             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
7627             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
7628         }
7629         write_fp_sreg(s, rd, tcg_rd);
7630         break;
7631     }
7632     case 0x3:
7633     {
7634         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
7635         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
7636         TCGv_i32 tcg_ahp = get_ahp_flag();
7637         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
7638         if (dtype == 0) {
7639             /* Half to single */
7640             TCGv_i32 tcg_rd = tcg_temp_new_i32();
7641             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
7642             write_fp_sreg(s, rd, tcg_rd);
7643         } else {
7644             /* Half to double */
7645             TCGv_i64 tcg_rd = tcg_temp_new_i64();
7646             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
7647             write_fp_dreg(s, rd, tcg_rd);
7648         }
7649         break;
7650     }
7651     default:
7652         g_assert_not_reached();
7653     }
7654 }
7655 
7656 /* Floating point data-processing (1 source)
7657  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
7658  * +---+---+---+-----------+------+---+--------+-----------+------+------+
7659  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
7660  * +---+---+---+-----------+------+---+--------+-----------+------+------+
7661  */
7662 static void disas_fp_1src(DisasContext *s, uint32_t insn)
7663 {
7664     int mos = extract32(insn, 29, 3);
7665     int type = extract32(insn, 22, 2);
7666     int opcode = extract32(insn, 15, 6);
7667     int rn = extract32(insn, 5, 5);
7668     int rd = extract32(insn, 0, 5);
7669 
7670     if (mos) {
7671         goto do_unallocated;
7672     }
7673 
7674     switch (opcode) {
7675     case 0x4: case 0x5: case 0x7:
7676     {
7677         /* FCVT between half, single and double precision */
7678         int dtype = extract32(opcode, 0, 2);
7679         if (type == 2 || dtype == type) {
7680             goto do_unallocated;
7681         }
7682         if (!fp_access_check(s)) {
7683             return;
7684         }
7685 
7686         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
7687         break;
7688     }
7689 
7690     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
7691         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
7692             goto do_unallocated;
7693         }
7694         /* fall through */
7695     case 0x0 ... 0x3:
7696     case 0x8 ... 0xc:
7697     case 0xe ... 0xf:
7698         /* 32-to-32 and 64-to-64 ops */
7699         switch (type) {
7700         case 0:
7701             if (!fp_access_check(s)) {
7702                 return;
7703             }
7704             handle_fp_1src_single(s, opcode, rd, rn);
7705             break;
7706         case 1:
7707             if (!fp_access_check(s)) {
7708                 return;
7709             }
7710             handle_fp_1src_double(s, opcode, rd, rn);
7711             break;
7712         case 3:
7713             if (!dc_isar_feature(aa64_fp16, s)) {
7714                 goto do_unallocated;
7715             }
7716 
7717             if (!fp_access_check(s)) {
7718                 return;
7719             }
7720             handle_fp_1src_half(s, opcode, rd, rn);
7721             break;
7722         default:
7723             goto do_unallocated;
7724         }
7725         break;
7726 
7727     case 0x6:
7728         switch (type) {
7729         case 1: /* BFCVT */
7730             if (!dc_isar_feature(aa64_bf16, s)) {
7731                 goto do_unallocated;
7732             }
7733             if (!fp_access_check(s)) {
7734                 return;
7735             }
7736             handle_fp_1src_single(s, opcode, rd, rn);
7737             break;
7738         default:
7739             goto do_unallocated;
7740         }
7741         break;
7742 
7743     default:
7744     do_unallocated:
7745         unallocated_encoding(s);
7746         break;
7747     }
7748 }
7749 
7750 /* Floating point immediate
7751  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
7752  * +---+---+---+-----------+------+---+------------+-------+------+------+
7753  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
7754  * +---+---+---+-----------+------+---+------------+-------+------+------+
7755  */
7756 static void disas_fp_imm(DisasContext *s, uint32_t insn)
7757 {
7758     int rd = extract32(insn, 0, 5);
7759     int imm5 = extract32(insn, 5, 5);
7760     int imm8 = extract32(insn, 13, 8);
7761     int type = extract32(insn, 22, 2);
7762     int mos = extract32(insn, 29, 3);
7763     uint64_t imm;
7764     MemOp sz;
7765 
7766     if (mos || imm5) {
7767         unallocated_encoding(s);
7768         return;
7769     }
7770 
7771     switch (type) {
7772     case 0:
7773         sz = MO_32;
7774         break;
7775     case 1:
7776         sz = MO_64;
7777         break;
7778     case 3:
7779         sz = MO_16;
7780         if (dc_isar_feature(aa64_fp16, s)) {
7781             break;
7782         }
7783         /* fallthru */
7784     default:
7785         unallocated_encoding(s);
7786         return;
7787     }
7788 
7789     if (!fp_access_check(s)) {
7790         return;
7791     }
7792 
7793     imm = vfp_expand_imm(sz, imm8);
7794     write_fp_dreg(s, rd, tcg_constant_i64(imm));
7795 }
7796 
7797 /* Handle floating point <=> fixed point conversions. Note that we can
7798  * also deal with fp <=> integer conversions as a special case (scale == 64)
7799  * OPTME: consider handling that special case specially or at least skipping
7800  * the call to scalbn in the helpers for zero shifts.
7801  */
7802 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
7803                            bool itof, int rmode, int scale, int sf, int type)
7804 {
7805     bool is_signed = !(opcode & 1);
7806     TCGv_ptr tcg_fpstatus;
7807     TCGv_i32 tcg_shift, tcg_single;
7808     TCGv_i64 tcg_double;
7809 
7810     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
7811 
7812     tcg_shift = tcg_constant_i32(64 - scale);
7813 
7814     if (itof) {
7815         TCGv_i64 tcg_int = cpu_reg(s, rn);
7816         if (!sf) {
7817             TCGv_i64 tcg_extend = tcg_temp_new_i64();
7818 
7819             if (is_signed) {
7820                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
7821             } else {
7822                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
7823             }
7824 
7825             tcg_int = tcg_extend;
7826         }
7827 
7828         switch (type) {
7829         case 1: /* float64 */
7830             tcg_double = tcg_temp_new_i64();
7831             if (is_signed) {
7832                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
7833                                      tcg_shift, tcg_fpstatus);
7834             } else {
7835                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
7836                                      tcg_shift, tcg_fpstatus);
7837             }
7838             write_fp_dreg(s, rd, tcg_double);
7839             break;
7840 
7841         case 0: /* float32 */
7842             tcg_single = tcg_temp_new_i32();
7843             if (is_signed) {
7844                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7845                                      tcg_shift, tcg_fpstatus);
7846             } else {
7847                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7848                                      tcg_shift, tcg_fpstatus);
7849             }
7850             write_fp_sreg(s, rd, tcg_single);
7851             break;
7852 
7853         case 3: /* float16 */
7854             tcg_single = tcg_temp_new_i32();
7855             if (is_signed) {
7856                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7857                                      tcg_shift, tcg_fpstatus);
7858             } else {
7859                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7860                                      tcg_shift, tcg_fpstatus);
7861             }
7862             write_fp_sreg(s, rd, tcg_single);
7863             break;
7864 
7865         default:
7866             g_assert_not_reached();
7867         }
7868     } else {
7869         TCGv_i64 tcg_int = cpu_reg(s, rd);
7870         TCGv_i32 tcg_rmode;
7871 
7872         if (extract32(opcode, 2, 1)) {
7873             /* There are too many rounding modes to all fit into rmode,
7874              * so FCVTA[US] is a special case.
7875              */
7876             rmode = FPROUNDING_TIEAWAY;
7877         }
7878 
7879         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
7880 
7881         switch (type) {
7882         case 1: /* float64 */
7883             tcg_double = read_fp_dreg(s, rn);
7884             if (is_signed) {
7885                 if (!sf) {
7886                     gen_helper_vfp_tosld(tcg_int, tcg_double,
7887                                          tcg_shift, tcg_fpstatus);
7888                 } else {
7889                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
7890                                          tcg_shift, tcg_fpstatus);
7891                 }
7892             } else {
7893                 if (!sf) {
7894                     gen_helper_vfp_tould(tcg_int, tcg_double,
7895                                          tcg_shift, tcg_fpstatus);
7896                 } else {
7897                     gen_helper_vfp_touqd(tcg_int, tcg_double,
7898                                          tcg_shift, tcg_fpstatus);
7899                 }
7900             }
7901             if (!sf) {
7902                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7903             }
7904             break;
7905 
7906         case 0: /* float32 */
7907             tcg_single = read_fp_sreg(s, rn);
7908             if (sf) {
7909                 if (is_signed) {
7910                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
7911                                          tcg_shift, tcg_fpstatus);
7912                 } else {
7913                     gen_helper_vfp_touqs(tcg_int, tcg_single,
7914                                          tcg_shift, tcg_fpstatus);
7915                 }
7916             } else {
7917                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7918                 if (is_signed) {
7919                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7920                                          tcg_shift, tcg_fpstatus);
7921                 } else {
7922                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7923                                          tcg_shift, tcg_fpstatus);
7924                 }
7925                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7926             }
7927             break;
7928 
7929         case 3: /* float16 */
7930             tcg_single = read_fp_sreg(s, rn);
7931             if (sf) {
7932                 if (is_signed) {
7933                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7934                                          tcg_shift, tcg_fpstatus);
7935                 } else {
7936                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7937                                          tcg_shift, tcg_fpstatus);
7938                 }
7939             } else {
7940                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7941                 if (is_signed) {
7942                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7943                                          tcg_shift, tcg_fpstatus);
7944                 } else {
7945                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7946                                          tcg_shift, tcg_fpstatus);
7947                 }
7948                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7949             }
7950             break;
7951 
7952         default:
7953             g_assert_not_reached();
7954         }
7955 
7956         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7957     }
7958 }
7959 
7960 /* Floating point <-> fixed point conversions
7961  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7962  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7963  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7964  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7965  */
7966 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7967 {
7968     int rd = extract32(insn, 0, 5);
7969     int rn = extract32(insn, 5, 5);
7970     int scale = extract32(insn, 10, 6);
7971     int opcode = extract32(insn, 16, 3);
7972     int rmode = extract32(insn, 19, 2);
7973     int type = extract32(insn, 22, 2);
7974     bool sbit = extract32(insn, 29, 1);
7975     bool sf = extract32(insn, 31, 1);
7976     bool itof;
7977 
7978     if (sbit || (!sf && scale < 32)) {
7979         unallocated_encoding(s);
7980         return;
7981     }
7982 
7983     switch (type) {
7984     case 0: /* float32 */
7985     case 1: /* float64 */
7986         break;
7987     case 3: /* float16 */
7988         if (dc_isar_feature(aa64_fp16, s)) {
7989             break;
7990         }
7991         /* fallthru */
7992     default:
7993         unallocated_encoding(s);
7994         return;
7995     }
7996 
7997     switch ((rmode << 3) | opcode) {
7998     case 0x2: /* SCVTF */
7999     case 0x3: /* UCVTF */
8000         itof = true;
8001         break;
8002     case 0x18: /* FCVTZS */
8003     case 0x19: /* FCVTZU */
8004         itof = false;
8005         break;
8006     default:
8007         unallocated_encoding(s);
8008         return;
8009     }
8010 
8011     if (!fp_access_check(s)) {
8012         return;
8013     }
8014 
8015     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
8016 }
8017 
8018 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
8019 {
8020     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
8021      * without conversion.
8022      */
8023 
8024     if (itof) {
8025         TCGv_i64 tcg_rn = cpu_reg(s, rn);
8026         TCGv_i64 tmp;
8027 
8028         switch (type) {
8029         case 0:
8030             /* 32 bit */
8031             tmp = tcg_temp_new_i64();
8032             tcg_gen_ext32u_i64(tmp, tcg_rn);
8033             write_fp_dreg(s, rd, tmp);
8034             break;
8035         case 1:
8036             /* 64 bit */
8037             write_fp_dreg(s, rd, tcg_rn);
8038             break;
8039         case 2:
8040             /* 64 bit to top half. */
8041             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
8042             clear_vec_high(s, true, rd);
8043             break;
8044         case 3:
8045             /* 16 bit */
8046             tmp = tcg_temp_new_i64();
8047             tcg_gen_ext16u_i64(tmp, tcg_rn);
8048             write_fp_dreg(s, rd, tmp);
8049             break;
8050         default:
8051             g_assert_not_reached();
8052         }
8053     } else {
8054         TCGv_i64 tcg_rd = cpu_reg(s, rd);
8055 
8056         switch (type) {
8057         case 0:
8058             /* 32 bit */
8059             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
8060             break;
8061         case 1:
8062             /* 64 bit */
8063             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
8064             break;
8065         case 2:
8066             /* 64 bits from top half */
8067             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
8068             break;
8069         case 3:
8070             /* 16 bit */
8071             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
8072             break;
8073         default:
8074             g_assert_not_reached();
8075         }
8076     }
8077 }
8078 
8079 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
8080 {
8081     TCGv_i64 t = read_fp_dreg(s, rn);
8082     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
8083 
8084     gen_helper_fjcvtzs(t, t, fpstatus);
8085 
8086     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
8087     tcg_gen_extrh_i64_i32(cpu_ZF, t);
8088     tcg_gen_movi_i32(cpu_CF, 0);
8089     tcg_gen_movi_i32(cpu_NF, 0);
8090     tcg_gen_movi_i32(cpu_VF, 0);
8091 }
8092 
8093 /* Floating point <-> integer conversions
8094  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
8095  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
8096  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
8097  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
8098  */
8099 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
8100 {
8101     int rd = extract32(insn, 0, 5);
8102     int rn = extract32(insn, 5, 5);
8103     int opcode = extract32(insn, 16, 3);
8104     int rmode = extract32(insn, 19, 2);
8105     int type = extract32(insn, 22, 2);
8106     bool sbit = extract32(insn, 29, 1);
8107     bool sf = extract32(insn, 31, 1);
8108     bool itof = false;
8109 
8110     if (sbit) {
8111         goto do_unallocated;
8112     }
8113 
8114     switch (opcode) {
8115     case 2: /* SCVTF */
8116     case 3: /* UCVTF */
8117         itof = true;
8118         /* fallthru */
8119     case 4: /* FCVTAS */
8120     case 5: /* FCVTAU */
8121         if (rmode != 0) {
8122             goto do_unallocated;
8123         }
8124         /* fallthru */
8125     case 0: /* FCVT[NPMZ]S */
8126     case 1: /* FCVT[NPMZ]U */
8127         switch (type) {
8128         case 0: /* float32 */
8129         case 1: /* float64 */
8130             break;
8131         case 3: /* float16 */
8132             if (!dc_isar_feature(aa64_fp16, s)) {
8133                 goto do_unallocated;
8134             }
8135             break;
8136         default:
8137             goto do_unallocated;
8138         }
8139         if (!fp_access_check(s)) {
8140             return;
8141         }
8142         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
8143         break;
8144 
8145     default:
8146         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
8147         case 0b01100110: /* FMOV half <-> 32-bit int */
8148         case 0b01100111:
8149         case 0b11100110: /* FMOV half <-> 64-bit int */
8150         case 0b11100111:
8151             if (!dc_isar_feature(aa64_fp16, s)) {
8152                 goto do_unallocated;
8153             }
8154             /* fallthru */
8155         case 0b00000110: /* FMOV 32-bit */
8156         case 0b00000111:
8157         case 0b10100110: /* FMOV 64-bit */
8158         case 0b10100111:
8159         case 0b11001110: /* FMOV top half of 128-bit */
8160         case 0b11001111:
8161             if (!fp_access_check(s)) {
8162                 return;
8163             }
8164             itof = opcode & 1;
8165             handle_fmov(s, rd, rn, type, itof);
8166             break;
8167 
8168         case 0b00111110: /* FJCVTZS */
8169             if (!dc_isar_feature(aa64_jscvt, s)) {
8170                 goto do_unallocated;
8171             } else if (fp_access_check(s)) {
8172                 handle_fjcvtzs(s, rd, rn);
8173             }
8174             break;
8175 
8176         default:
8177         do_unallocated:
8178             unallocated_encoding(s);
8179             return;
8180         }
8181         break;
8182     }
8183 }
8184 
8185 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
8186  *   31  30  29 28     25 24                          0
8187  * +---+---+---+---------+-----------------------------+
8188  * |   | 0 |   | 1 1 1 1 |                             |
8189  * +---+---+---+---------+-----------------------------+
8190  */
8191 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
8192 {
8193     if (extract32(insn, 24, 1)) {
8194         unallocated_encoding(s); /* in decodetree */
8195     } else if (extract32(insn, 21, 1) == 0) {
8196         /* Floating point to fixed point conversions */
8197         disas_fp_fixed_conv(s, insn);
8198     } else {
8199         switch (extract32(insn, 10, 2)) {
8200         case 1:
8201             /* Floating point conditional compare */
8202             disas_fp_ccomp(s, insn);
8203             break;
8204         case 2:
8205             /* Floating point data-processing (2 source) */
8206             unallocated_encoding(s); /* in decodetree */
8207             break;
8208         case 3:
8209             /* Floating point conditional select */
8210             disas_fp_csel(s, insn);
8211             break;
8212         case 0:
8213             switch (ctz32(extract32(insn, 12, 4))) {
8214             case 0: /* [15:12] == xxx1 */
8215                 /* Floating point immediate */
8216                 disas_fp_imm(s, insn);
8217                 break;
8218             case 1: /* [15:12] == xx10 */
8219                 /* Floating point compare */
8220                 disas_fp_compare(s, insn);
8221                 break;
8222             case 2: /* [15:12] == x100 */
8223                 /* Floating point data-processing (1 source) */
8224                 disas_fp_1src(s, insn);
8225                 break;
8226             case 3: /* [15:12] == 1000 */
8227                 unallocated_encoding(s);
8228                 break;
8229             default: /* [15:12] == 0000 */
8230                 /* Floating point <-> integer conversions */
8231                 disas_fp_int_conv(s, insn);
8232                 break;
8233             }
8234             break;
8235         }
8236     }
8237 }
8238 
8239 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
8240                      int pos)
8241 {
8242     /* Extract 64 bits from the middle of two concatenated 64 bit
8243      * vector register slices left:right. The extracted bits start
8244      * at 'pos' bits into the right (least significant) side.
8245      * We return the result in tcg_right, and guarantee not to
8246      * trash tcg_left.
8247      */
8248     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8249     assert(pos > 0 && pos < 64);
8250 
8251     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
8252     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
8253     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
8254 }
8255 
8256 /* EXT
8257  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
8258  * +---+---+-------------+-----+---+------+---+------+---+------+------+
8259  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
8260  * +---+---+-------------+-----+---+------+---+------+---+------+------+
8261  */
8262 static void disas_simd_ext(DisasContext *s, uint32_t insn)
8263 {
8264     int is_q = extract32(insn, 30, 1);
8265     int op2 = extract32(insn, 22, 2);
8266     int imm4 = extract32(insn, 11, 4);
8267     int rm = extract32(insn, 16, 5);
8268     int rn = extract32(insn, 5, 5);
8269     int rd = extract32(insn, 0, 5);
8270     int pos = imm4 << 3;
8271     TCGv_i64 tcg_resl, tcg_resh;
8272 
8273     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
8274         unallocated_encoding(s);
8275         return;
8276     }
8277 
8278     if (!fp_access_check(s)) {
8279         return;
8280     }
8281 
8282     tcg_resh = tcg_temp_new_i64();
8283     tcg_resl = tcg_temp_new_i64();
8284 
8285     /* Vd gets bits starting at pos bits into Vm:Vn. This is
8286      * either extracting 128 bits from a 128:128 concatenation, or
8287      * extracting 64 bits from a 64:64 concatenation.
8288      */
8289     if (!is_q) {
8290         read_vec_element(s, tcg_resl, rn, 0, MO_64);
8291         if (pos != 0) {
8292             read_vec_element(s, tcg_resh, rm, 0, MO_64);
8293             do_ext64(s, tcg_resh, tcg_resl, pos);
8294         }
8295     } else {
8296         TCGv_i64 tcg_hh;
8297         typedef struct {
8298             int reg;
8299             int elt;
8300         } EltPosns;
8301         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
8302         EltPosns *elt = eltposns;
8303 
8304         if (pos >= 64) {
8305             elt++;
8306             pos -= 64;
8307         }
8308 
8309         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
8310         elt++;
8311         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
8312         elt++;
8313         if (pos != 0) {
8314             do_ext64(s, tcg_resh, tcg_resl, pos);
8315             tcg_hh = tcg_temp_new_i64();
8316             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
8317             do_ext64(s, tcg_hh, tcg_resh, pos);
8318         }
8319     }
8320 
8321     write_vec_element(s, tcg_resl, rd, 0, MO_64);
8322     if (is_q) {
8323         write_vec_element(s, tcg_resh, rd, 1, MO_64);
8324     }
8325     clear_vec_high(s, is_q, rd);
8326 }
8327 
8328 /* TBL/TBX
8329  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
8330  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
8331  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
8332  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
8333  */
8334 static void disas_simd_tb(DisasContext *s, uint32_t insn)
8335 {
8336     int op2 = extract32(insn, 22, 2);
8337     int is_q = extract32(insn, 30, 1);
8338     int rm = extract32(insn, 16, 5);
8339     int rn = extract32(insn, 5, 5);
8340     int rd = extract32(insn, 0, 5);
8341     int is_tbx = extract32(insn, 12, 1);
8342     int len = (extract32(insn, 13, 2) + 1) * 16;
8343 
8344     if (op2 != 0) {
8345         unallocated_encoding(s);
8346         return;
8347     }
8348 
8349     if (!fp_access_check(s)) {
8350         return;
8351     }
8352 
8353     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
8354                        vec_full_reg_offset(s, rm), tcg_env,
8355                        is_q ? 16 : 8, vec_full_reg_size(s),
8356                        (len << 6) | (is_tbx << 5) | rn,
8357                        gen_helper_simd_tblx);
8358 }
8359 
8360 /* ZIP/UZP/TRN
8361  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
8362  * +---+---+-------------+------+---+------+---+------------------+------+
8363  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
8364  * +---+---+-------------+------+---+------+---+------------------+------+
8365  */
8366 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
8367 {
8368     int rd = extract32(insn, 0, 5);
8369     int rn = extract32(insn, 5, 5);
8370     int rm = extract32(insn, 16, 5);
8371     int size = extract32(insn, 22, 2);
8372     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
8373      * bit 2 indicates 1 vs 2 variant of the insn.
8374      */
8375     int opcode = extract32(insn, 12, 2);
8376     bool part = extract32(insn, 14, 1);
8377     bool is_q = extract32(insn, 30, 1);
8378     int esize = 8 << size;
8379     int i;
8380     int datasize = is_q ? 128 : 64;
8381     int elements = datasize / esize;
8382     TCGv_i64 tcg_res[2], tcg_ele;
8383 
8384     if (opcode == 0 || (size == 3 && !is_q)) {
8385         unallocated_encoding(s);
8386         return;
8387     }
8388 
8389     if (!fp_access_check(s)) {
8390         return;
8391     }
8392 
8393     tcg_res[0] = tcg_temp_new_i64();
8394     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
8395     tcg_ele = tcg_temp_new_i64();
8396 
8397     for (i = 0; i < elements; i++) {
8398         int o, w;
8399 
8400         switch (opcode) {
8401         case 1: /* UZP1/2 */
8402         {
8403             int midpoint = elements / 2;
8404             if (i < midpoint) {
8405                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
8406             } else {
8407                 read_vec_element(s, tcg_ele, rm,
8408                                  2 * (i - midpoint) + part, size);
8409             }
8410             break;
8411         }
8412         case 2: /* TRN1/2 */
8413             if (i & 1) {
8414                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
8415             } else {
8416                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
8417             }
8418             break;
8419         case 3: /* ZIP1/2 */
8420         {
8421             int base = part * elements / 2;
8422             if (i & 1) {
8423                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
8424             } else {
8425                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
8426             }
8427             break;
8428         }
8429         default:
8430             g_assert_not_reached();
8431         }
8432 
8433         w = (i * esize) / 64;
8434         o = (i * esize) % 64;
8435         if (o == 0) {
8436             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
8437         } else {
8438             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
8439             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
8440         }
8441     }
8442 
8443     for (i = 0; i <= is_q; ++i) {
8444         write_vec_element(s, tcg_res[i], rd, i, MO_64);
8445     }
8446     clear_vec_high(s, is_q, rd);
8447 }
8448 
8449 /*
8450  * do_reduction_op helper
8451  *
8452  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
8453  * important for correct NaN propagation that we do these
8454  * operations in exactly the order specified by the pseudocode.
8455  *
8456  * This is a recursive function, TCG temps should be freed by the
8457  * calling function once it is done with the values.
8458  */
8459 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
8460                                 int esize, int size, int vmap, TCGv_ptr fpst)
8461 {
8462     if (esize == size) {
8463         int element;
8464         MemOp msize = esize == 16 ? MO_16 : MO_32;
8465         TCGv_i32 tcg_elem;
8466 
8467         /* We should have one register left here */
8468         assert(ctpop8(vmap) == 1);
8469         element = ctz32(vmap);
8470         assert(element < 8);
8471 
8472         tcg_elem = tcg_temp_new_i32();
8473         read_vec_element_i32(s, tcg_elem, rn, element, msize);
8474         return tcg_elem;
8475     } else {
8476         int bits = size / 2;
8477         int shift = ctpop8(vmap) / 2;
8478         int vmap_lo = (vmap >> shift) & vmap;
8479         int vmap_hi = (vmap & ~vmap_lo);
8480         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
8481 
8482         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
8483         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
8484         tcg_res = tcg_temp_new_i32();
8485 
8486         switch (fpopcode) {
8487         case 0x0c: /* fmaxnmv half-precision */
8488             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
8489             break;
8490         case 0x0f: /* fmaxv half-precision */
8491             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
8492             break;
8493         case 0x1c: /* fminnmv half-precision */
8494             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
8495             break;
8496         case 0x1f: /* fminv half-precision */
8497             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
8498             break;
8499         case 0x2c: /* fmaxnmv */
8500             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
8501             break;
8502         case 0x2f: /* fmaxv */
8503             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
8504             break;
8505         case 0x3c: /* fminnmv */
8506             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
8507             break;
8508         case 0x3f: /* fminv */
8509             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
8510             break;
8511         default:
8512             g_assert_not_reached();
8513         }
8514         return tcg_res;
8515     }
8516 }
8517 
8518 /* AdvSIMD across lanes
8519  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8520  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8521  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8522  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8523  */
8524 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
8525 {
8526     int rd = extract32(insn, 0, 5);
8527     int rn = extract32(insn, 5, 5);
8528     int size = extract32(insn, 22, 2);
8529     int opcode = extract32(insn, 12, 5);
8530     bool is_q = extract32(insn, 30, 1);
8531     bool is_u = extract32(insn, 29, 1);
8532     bool is_fp = false;
8533     bool is_min = false;
8534     int esize;
8535     int elements;
8536     int i;
8537     TCGv_i64 tcg_res, tcg_elt;
8538 
8539     switch (opcode) {
8540     case 0x1b: /* ADDV */
8541         if (is_u) {
8542             unallocated_encoding(s);
8543             return;
8544         }
8545         /* fall through */
8546     case 0x3: /* SADDLV, UADDLV */
8547     case 0xa: /* SMAXV, UMAXV */
8548     case 0x1a: /* SMINV, UMINV */
8549         if (size == 3 || (size == 2 && !is_q)) {
8550             unallocated_encoding(s);
8551             return;
8552         }
8553         break;
8554     case 0xc: /* FMAXNMV, FMINNMV */
8555     case 0xf: /* FMAXV, FMINV */
8556         /* Bit 1 of size field encodes min vs max and the actual size
8557          * depends on the encoding of the U bit. If not set (and FP16
8558          * enabled) then we do half-precision float instead of single
8559          * precision.
8560          */
8561         is_min = extract32(size, 1, 1);
8562         is_fp = true;
8563         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
8564             size = 1;
8565         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
8566             unallocated_encoding(s);
8567             return;
8568         } else {
8569             size = 2;
8570         }
8571         break;
8572     default:
8573         unallocated_encoding(s);
8574         return;
8575     }
8576 
8577     if (!fp_access_check(s)) {
8578         return;
8579     }
8580 
8581     esize = 8 << size;
8582     elements = (is_q ? 128 : 64) / esize;
8583 
8584     tcg_res = tcg_temp_new_i64();
8585     tcg_elt = tcg_temp_new_i64();
8586 
8587     /* These instructions operate across all lanes of a vector
8588      * to produce a single result. We can guarantee that a 64
8589      * bit intermediate is sufficient:
8590      *  + for [US]ADDLV the maximum element size is 32 bits, and
8591      *    the result type is 64 bits
8592      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
8593      *    same as the element size, which is 32 bits at most
8594      * For the integer operations we can choose to work at 64
8595      * or 32 bits and truncate at the end; for simplicity
8596      * we use 64 bits always. The floating point
8597      * ops do require 32 bit intermediates, though.
8598      */
8599     if (!is_fp) {
8600         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
8601 
8602         for (i = 1; i < elements; i++) {
8603             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
8604 
8605             switch (opcode) {
8606             case 0x03: /* SADDLV / UADDLV */
8607             case 0x1b: /* ADDV */
8608                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
8609                 break;
8610             case 0x0a: /* SMAXV / UMAXV */
8611                 if (is_u) {
8612                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
8613                 } else {
8614                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
8615                 }
8616                 break;
8617             case 0x1a: /* SMINV / UMINV */
8618                 if (is_u) {
8619                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
8620                 } else {
8621                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
8622                 }
8623                 break;
8624             default:
8625                 g_assert_not_reached();
8626             }
8627 
8628         }
8629     } else {
8630         /* Floating point vector reduction ops which work across 32
8631          * bit (single) or 16 bit (half-precision) intermediates.
8632          * Note that correct NaN propagation requires that we do these
8633          * operations in exactly the order specified by the pseudocode.
8634          */
8635         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8636         int fpopcode = opcode | is_min << 4 | is_u << 5;
8637         int vmap = (1 << elements) - 1;
8638         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
8639                                              (is_q ? 128 : 64), vmap, fpst);
8640         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
8641     }
8642 
8643     /* Now truncate the result to the width required for the final output */
8644     if (opcode == 0x03) {
8645         /* SADDLV, UADDLV: result is 2*esize */
8646         size++;
8647     }
8648 
8649     switch (size) {
8650     case 0:
8651         tcg_gen_ext8u_i64(tcg_res, tcg_res);
8652         break;
8653     case 1:
8654         tcg_gen_ext16u_i64(tcg_res, tcg_res);
8655         break;
8656     case 2:
8657         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8658         break;
8659     case 3:
8660         break;
8661     default:
8662         g_assert_not_reached();
8663     }
8664 
8665     write_fp_dreg(s, rd, tcg_res);
8666 }
8667 
8668 /* AdvSIMD modified immediate
8669  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8670  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8671  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8672  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8673  *
8674  * There are a number of operations that can be carried out here:
8675  *   MOVI - move (shifted) imm into register
8676  *   MVNI - move inverted (shifted) imm into register
8677  *   ORR  - bitwise OR of (shifted) imm with register
8678  *   BIC  - bitwise clear of (shifted) imm with register
8679  * With ARMv8.2 we also have:
8680  *   FMOV half-precision
8681  */
8682 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8683 {
8684     int rd = extract32(insn, 0, 5);
8685     int cmode = extract32(insn, 12, 4);
8686     int o2 = extract32(insn, 11, 1);
8687     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8688     bool is_neg = extract32(insn, 29, 1);
8689     bool is_q = extract32(insn, 30, 1);
8690     uint64_t imm = 0;
8691 
8692     if (o2) {
8693         if (cmode != 0xf || is_neg) {
8694             unallocated_encoding(s);
8695             return;
8696         }
8697         /* FMOV (vector, immediate) - half-precision */
8698         if (!dc_isar_feature(aa64_fp16, s)) {
8699             unallocated_encoding(s);
8700             return;
8701         }
8702         imm = vfp_expand_imm(MO_16, abcdefgh);
8703         /* now duplicate across the lanes */
8704         imm = dup_const(MO_16, imm);
8705     } else {
8706         if (cmode == 0xf && is_neg && !is_q) {
8707             unallocated_encoding(s);
8708             return;
8709         }
8710         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8711     }
8712 
8713     if (!fp_access_check(s)) {
8714         return;
8715     }
8716 
8717     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8718         /* MOVI or MVNI, with MVNI negation handled above.  */
8719         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8720                              vec_full_reg_size(s), imm);
8721     } else {
8722         /* ORR or BIC, with BIC negation to AND handled above.  */
8723         if (is_neg) {
8724             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8725         } else {
8726             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8727         }
8728     }
8729 }
8730 
8731 /*
8732  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8733  *
8734  * This code is handles the common shifting code and is used by both
8735  * the vector and scalar code.
8736  */
8737 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8738                                     TCGv_i64 tcg_rnd, bool accumulate,
8739                                     bool is_u, int size, int shift)
8740 {
8741     bool extended_result = false;
8742     bool round = tcg_rnd != NULL;
8743     int ext_lshift = 0;
8744     TCGv_i64 tcg_src_hi;
8745 
8746     if (round && size == 3) {
8747         extended_result = true;
8748         ext_lshift = 64 - shift;
8749         tcg_src_hi = tcg_temp_new_i64();
8750     } else if (shift == 64) {
8751         if (!accumulate && is_u) {
8752             /* result is zero */
8753             tcg_gen_movi_i64(tcg_res, 0);
8754             return;
8755         }
8756     }
8757 
8758     /* Deal with the rounding step */
8759     if (round) {
8760         if (extended_result) {
8761             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8762             if (!is_u) {
8763                 /* take care of sign extending tcg_res */
8764                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8765                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8766                                  tcg_src, tcg_src_hi,
8767                                  tcg_rnd, tcg_zero);
8768             } else {
8769                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8770                                  tcg_src, tcg_zero,
8771                                  tcg_rnd, tcg_zero);
8772             }
8773         } else {
8774             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8775         }
8776     }
8777 
8778     /* Now do the shift right */
8779     if (round && extended_result) {
8780         /* extended case, >64 bit precision required */
8781         if (ext_lshift == 0) {
8782             /* special case, only high bits matter */
8783             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8784         } else {
8785             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8786             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8787             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8788         }
8789     } else {
8790         if (is_u) {
8791             if (shift == 64) {
8792                 /* essentially shifting in 64 zeros */
8793                 tcg_gen_movi_i64(tcg_src, 0);
8794             } else {
8795                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8796             }
8797         } else {
8798             if (shift == 64) {
8799                 /* effectively extending the sign-bit */
8800                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8801             } else {
8802                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8803             }
8804         }
8805     }
8806 
8807     if (accumulate) {
8808         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8809     } else {
8810         tcg_gen_mov_i64(tcg_res, tcg_src);
8811     }
8812 }
8813 
8814 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8815 static void handle_scalar_simd_shri(DisasContext *s,
8816                                     bool is_u, int immh, int immb,
8817                                     int opcode, int rn, int rd)
8818 {
8819     const int size = 3;
8820     int immhb = immh << 3 | immb;
8821     int shift = 2 * (8 << size) - immhb;
8822     bool accumulate = false;
8823     bool round = false;
8824     bool insert = false;
8825     TCGv_i64 tcg_rn;
8826     TCGv_i64 tcg_rd;
8827     TCGv_i64 tcg_round;
8828 
8829     if (!extract32(immh, 3, 1)) {
8830         unallocated_encoding(s);
8831         return;
8832     }
8833 
8834     if (!fp_access_check(s)) {
8835         return;
8836     }
8837 
8838     switch (opcode) {
8839     case 0x02: /* SSRA / USRA (accumulate) */
8840         accumulate = true;
8841         break;
8842     case 0x04: /* SRSHR / URSHR (rounding) */
8843         round = true;
8844         break;
8845     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8846         accumulate = round = true;
8847         break;
8848     case 0x08: /* SRI */
8849         insert = true;
8850         break;
8851     }
8852 
8853     if (round) {
8854         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8855     } else {
8856         tcg_round = NULL;
8857     }
8858 
8859     tcg_rn = read_fp_dreg(s, rn);
8860     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8861 
8862     if (insert) {
8863         /* shift count same as element size is valid but does nothing;
8864          * special case to avoid potential shift by 64.
8865          */
8866         int esize = 8 << size;
8867         if (shift != esize) {
8868             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8869             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8870         }
8871     } else {
8872         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8873                                 accumulate, is_u, size, shift);
8874     }
8875 
8876     write_fp_dreg(s, rd, tcg_rd);
8877 }
8878 
8879 /* SHL/SLI - Scalar shift left */
8880 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8881                                     int immh, int immb, int opcode,
8882                                     int rn, int rd)
8883 {
8884     int size = 32 - clz32(immh) - 1;
8885     int immhb = immh << 3 | immb;
8886     int shift = immhb - (8 << size);
8887     TCGv_i64 tcg_rn;
8888     TCGv_i64 tcg_rd;
8889 
8890     if (!extract32(immh, 3, 1)) {
8891         unallocated_encoding(s);
8892         return;
8893     }
8894 
8895     if (!fp_access_check(s)) {
8896         return;
8897     }
8898 
8899     tcg_rn = read_fp_dreg(s, rn);
8900     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8901 
8902     if (insert) {
8903         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8904     } else {
8905         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8906     }
8907 
8908     write_fp_dreg(s, rd, tcg_rd);
8909 }
8910 
8911 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8912  * (signed/unsigned) narrowing */
8913 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8914                                    bool is_u_shift, bool is_u_narrow,
8915                                    int immh, int immb, int opcode,
8916                                    int rn, int rd)
8917 {
8918     int immhb = immh << 3 | immb;
8919     int size = 32 - clz32(immh) - 1;
8920     int esize = 8 << size;
8921     int shift = (2 * esize) - immhb;
8922     int elements = is_scalar ? 1 : (64 / esize);
8923     bool round = extract32(opcode, 0, 1);
8924     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8925     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8926     TCGv_i32 tcg_rd_narrowed;
8927     TCGv_i64 tcg_final;
8928 
8929     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8930         { gen_helper_neon_narrow_sat_s8,
8931           gen_helper_neon_unarrow_sat8 },
8932         { gen_helper_neon_narrow_sat_s16,
8933           gen_helper_neon_unarrow_sat16 },
8934         { gen_helper_neon_narrow_sat_s32,
8935           gen_helper_neon_unarrow_sat32 },
8936         { NULL, NULL },
8937     };
8938     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8939         gen_helper_neon_narrow_sat_u8,
8940         gen_helper_neon_narrow_sat_u16,
8941         gen_helper_neon_narrow_sat_u32,
8942         NULL
8943     };
8944     NeonGenNarrowEnvFn *narrowfn;
8945 
8946     int i;
8947 
8948     assert(size < 4);
8949 
8950     if (extract32(immh, 3, 1)) {
8951         unallocated_encoding(s);
8952         return;
8953     }
8954 
8955     if (!fp_access_check(s)) {
8956         return;
8957     }
8958 
8959     if (is_u_shift) {
8960         narrowfn = unsigned_narrow_fns[size];
8961     } else {
8962         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8963     }
8964 
8965     tcg_rn = tcg_temp_new_i64();
8966     tcg_rd = tcg_temp_new_i64();
8967     tcg_rd_narrowed = tcg_temp_new_i32();
8968     tcg_final = tcg_temp_new_i64();
8969 
8970     if (round) {
8971         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8972     } else {
8973         tcg_round = NULL;
8974     }
8975 
8976     for (i = 0; i < elements; i++) {
8977         read_vec_element(s, tcg_rn, rn, i, ldop);
8978         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8979                                 false, is_u_shift, size+1, shift);
8980         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8981         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8982         if (i == 0) {
8983             tcg_gen_extract_i64(tcg_final, tcg_rd, 0, esize);
8984         } else {
8985             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8986         }
8987     }
8988 
8989     if (!is_q) {
8990         write_vec_element(s, tcg_final, rd, 0, MO_64);
8991     } else {
8992         write_vec_element(s, tcg_final, rd, 1, MO_64);
8993     }
8994     clear_vec_high(s, is_q, rd);
8995 }
8996 
8997 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8998 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8999                              bool src_unsigned, bool dst_unsigned,
9000                              int immh, int immb, int rn, int rd)
9001 {
9002     int immhb = immh << 3 | immb;
9003     int size = 32 - clz32(immh) - 1;
9004     int shift = immhb - (8 << size);
9005     int pass;
9006 
9007     assert(immh != 0);
9008     assert(!(scalar && is_q));
9009 
9010     if (!scalar) {
9011         if (!is_q && extract32(immh, 3, 1)) {
9012             unallocated_encoding(s);
9013             return;
9014         }
9015 
9016         /* Since we use the variable-shift helpers we must
9017          * replicate the shift count into each element of
9018          * the tcg_shift value.
9019          */
9020         switch (size) {
9021         case 0:
9022             shift |= shift << 8;
9023             /* fall through */
9024         case 1:
9025             shift |= shift << 16;
9026             break;
9027         case 2:
9028         case 3:
9029             break;
9030         default:
9031             g_assert_not_reached();
9032         }
9033     }
9034 
9035     if (!fp_access_check(s)) {
9036         return;
9037     }
9038 
9039     if (size == 3) {
9040         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
9041         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
9042             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
9043             { NULL, gen_helper_neon_qshl_u64 },
9044         };
9045         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
9046         int maxpass = is_q ? 2 : 1;
9047 
9048         for (pass = 0; pass < maxpass; pass++) {
9049             TCGv_i64 tcg_op = tcg_temp_new_i64();
9050 
9051             read_vec_element(s, tcg_op, rn, pass, MO_64);
9052             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
9053             write_vec_element(s, tcg_op, rd, pass, MO_64);
9054         }
9055         clear_vec_high(s, is_q, rd);
9056     } else {
9057         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
9058         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
9059             {
9060                 { gen_helper_neon_qshl_s8,
9061                   gen_helper_neon_qshl_s16,
9062                   gen_helper_neon_qshl_s32 },
9063                 { gen_helper_neon_qshlu_s8,
9064                   gen_helper_neon_qshlu_s16,
9065                   gen_helper_neon_qshlu_s32 }
9066             }, {
9067                 { NULL, NULL, NULL },
9068                 { gen_helper_neon_qshl_u8,
9069                   gen_helper_neon_qshl_u16,
9070                   gen_helper_neon_qshl_u32 }
9071             }
9072         };
9073         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
9074         MemOp memop = scalar ? size : MO_32;
9075         int maxpass = scalar ? 1 : is_q ? 4 : 2;
9076 
9077         for (pass = 0; pass < maxpass; pass++) {
9078             TCGv_i32 tcg_op = tcg_temp_new_i32();
9079 
9080             read_vec_element_i32(s, tcg_op, rn, pass, memop);
9081             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
9082             if (scalar) {
9083                 switch (size) {
9084                 case 0:
9085                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
9086                     break;
9087                 case 1:
9088                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
9089                     break;
9090                 case 2:
9091                     break;
9092                 default:
9093                     g_assert_not_reached();
9094                 }
9095                 write_fp_sreg(s, rd, tcg_op);
9096             } else {
9097                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
9098             }
9099         }
9100 
9101         if (!scalar) {
9102             clear_vec_high(s, is_q, rd);
9103         }
9104     }
9105 }
9106 
9107 /* Common vector code for handling integer to FP conversion */
9108 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
9109                                    int elements, int is_signed,
9110                                    int fracbits, int size)
9111 {
9112     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9113     TCGv_i32 tcg_shift = NULL;
9114 
9115     MemOp mop = size | (is_signed ? MO_SIGN : 0);
9116     int pass;
9117 
9118     if (fracbits || size == MO_64) {
9119         tcg_shift = tcg_constant_i32(fracbits);
9120     }
9121 
9122     if (size == MO_64) {
9123         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
9124         TCGv_i64 tcg_double = tcg_temp_new_i64();
9125 
9126         for (pass = 0; pass < elements; pass++) {
9127             read_vec_element(s, tcg_int64, rn, pass, mop);
9128 
9129             if (is_signed) {
9130                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
9131                                      tcg_shift, tcg_fpst);
9132             } else {
9133                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
9134                                      tcg_shift, tcg_fpst);
9135             }
9136             if (elements == 1) {
9137                 write_fp_dreg(s, rd, tcg_double);
9138             } else {
9139                 write_vec_element(s, tcg_double, rd, pass, MO_64);
9140             }
9141         }
9142     } else {
9143         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
9144         TCGv_i32 tcg_float = tcg_temp_new_i32();
9145 
9146         for (pass = 0; pass < elements; pass++) {
9147             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
9148 
9149             switch (size) {
9150             case MO_32:
9151                 if (fracbits) {
9152                     if (is_signed) {
9153                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
9154                                              tcg_shift, tcg_fpst);
9155                     } else {
9156                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
9157                                              tcg_shift, tcg_fpst);
9158                     }
9159                 } else {
9160                     if (is_signed) {
9161                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
9162                     } else {
9163                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
9164                     }
9165                 }
9166                 break;
9167             case MO_16:
9168                 if (fracbits) {
9169                     if (is_signed) {
9170                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
9171                                              tcg_shift, tcg_fpst);
9172                     } else {
9173                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
9174                                              tcg_shift, tcg_fpst);
9175                     }
9176                 } else {
9177                     if (is_signed) {
9178                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
9179                     } else {
9180                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
9181                     }
9182                 }
9183                 break;
9184             default:
9185                 g_assert_not_reached();
9186             }
9187 
9188             if (elements == 1) {
9189                 write_fp_sreg(s, rd, tcg_float);
9190             } else {
9191                 write_vec_element_i32(s, tcg_float, rd, pass, size);
9192             }
9193         }
9194     }
9195 
9196     clear_vec_high(s, elements << size == 16, rd);
9197 }
9198 
9199 /* UCVTF/SCVTF - Integer to FP conversion */
9200 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
9201                                          bool is_q, bool is_u,
9202                                          int immh, int immb, int opcode,
9203                                          int rn, int rd)
9204 {
9205     int size, elements, fracbits;
9206     int immhb = immh << 3 | immb;
9207 
9208     if (immh & 8) {
9209         size = MO_64;
9210         if (!is_scalar && !is_q) {
9211             unallocated_encoding(s);
9212             return;
9213         }
9214     } else if (immh & 4) {
9215         size = MO_32;
9216     } else if (immh & 2) {
9217         size = MO_16;
9218         if (!dc_isar_feature(aa64_fp16, s)) {
9219             unallocated_encoding(s);
9220             return;
9221         }
9222     } else {
9223         /* immh == 0 would be a failure of the decode logic */
9224         g_assert(immh == 1);
9225         unallocated_encoding(s);
9226         return;
9227     }
9228 
9229     if (is_scalar) {
9230         elements = 1;
9231     } else {
9232         elements = (8 << is_q) >> size;
9233     }
9234     fracbits = (16 << size) - immhb;
9235 
9236     if (!fp_access_check(s)) {
9237         return;
9238     }
9239 
9240     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
9241 }
9242 
9243 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
9244 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
9245                                          bool is_q, bool is_u,
9246                                          int immh, int immb, int rn, int rd)
9247 {
9248     int immhb = immh << 3 | immb;
9249     int pass, size, fracbits;
9250     TCGv_ptr tcg_fpstatus;
9251     TCGv_i32 tcg_rmode, tcg_shift;
9252 
9253     if (immh & 0x8) {
9254         size = MO_64;
9255         if (!is_scalar && !is_q) {
9256             unallocated_encoding(s);
9257             return;
9258         }
9259     } else if (immh & 0x4) {
9260         size = MO_32;
9261     } else if (immh & 0x2) {
9262         size = MO_16;
9263         if (!dc_isar_feature(aa64_fp16, s)) {
9264             unallocated_encoding(s);
9265             return;
9266         }
9267     } else {
9268         /* Should have split out AdvSIMD modified immediate earlier.  */
9269         assert(immh == 1);
9270         unallocated_encoding(s);
9271         return;
9272     }
9273 
9274     if (!fp_access_check(s)) {
9275         return;
9276     }
9277 
9278     assert(!(is_scalar && is_q));
9279 
9280     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9281     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
9282     fracbits = (16 << size) - immhb;
9283     tcg_shift = tcg_constant_i32(fracbits);
9284 
9285     if (size == MO_64) {
9286         int maxpass = is_scalar ? 1 : 2;
9287 
9288         for (pass = 0; pass < maxpass; pass++) {
9289             TCGv_i64 tcg_op = tcg_temp_new_i64();
9290 
9291             read_vec_element(s, tcg_op, rn, pass, MO_64);
9292             if (is_u) {
9293                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9294             } else {
9295                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9296             }
9297             write_vec_element(s, tcg_op, rd, pass, MO_64);
9298         }
9299         clear_vec_high(s, is_q, rd);
9300     } else {
9301         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
9302         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
9303 
9304         switch (size) {
9305         case MO_16:
9306             if (is_u) {
9307                 fn = gen_helper_vfp_touhh;
9308             } else {
9309                 fn = gen_helper_vfp_toshh;
9310             }
9311             break;
9312         case MO_32:
9313             if (is_u) {
9314                 fn = gen_helper_vfp_touls;
9315             } else {
9316                 fn = gen_helper_vfp_tosls;
9317             }
9318             break;
9319         default:
9320             g_assert_not_reached();
9321         }
9322 
9323         for (pass = 0; pass < maxpass; pass++) {
9324             TCGv_i32 tcg_op = tcg_temp_new_i32();
9325 
9326             read_vec_element_i32(s, tcg_op, rn, pass, size);
9327             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9328             if (is_scalar) {
9329                 if (size == MO_16 && !is_u) {
9330                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
9331                 }
9332                 write_fp_sreg(s, rd, tcg_op);
9333             } else {
9334                 write_vec_element_i32(s, tcg_op, rd, pass, size);
9335             }
9336         }
9337         if (!is_scalar) {
9338             clear_vec_high(s, is_q, rd);
9339         }
9340     }
9341 
9342     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
9343 }
9344 
9345 /* AdvSIMD scalar shift by immediate
9346  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
9347  * +-----+---+-------------+------+------+--------+---+------+------+
9348  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
9349  * +-----+---+-------------+------+------+--------+---+------+------+
9350  *
9351  * This is the scalar version so it works on a fixed sized registers
9352  */
9353 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9354 {
9355     int rd = extract32(insn, 0, 5);
9356     int rn = extract32(insn, 5, 5);
9357     int opcode = extract32(insn, 11, 5);
9358     int immb = extract32(insn, 16, 3);
9359     int immh = extract32(insn, 19, 4);
9360     bool is_u = extract32(insn, 29, 1);
9361 
9362     if (immh == 0) {
9363         unallocated_encoding(s);
9364         return;
9365     }
9366 
9367     switch (opcode) {
9368     case 0x08: /* SRI */
9369         if (!is_u) {
9370             unallocated_encoding(s);
9371             return;
9372         }
9373         /* fall through */
9374     case 0x00: /* SSHR / USHR */
9375     case 0x02: /* SSRA / USRA */
9376     case 0x04: /* SRSHR / URSHR */
9377     case 0x06: /* SRSRA / URSRA */
9378         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9379         break;
9380     case 0x0a: /* SHL / SLI */
9381         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9382         break;
9383     case 0x1c: /* SCVTF, UCVTF */
9384         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9385                                      opcode, rn, rd);
9386         break;
9387     case 0x10: /* SQSHRUN, SQSHRUN2 */
9388     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9389         if (!is_u) {
9390             unallocated_encoding(s);
9391             return;
9392         }
9393         handle_vec_simd_sqshrn(s, true, false, false, true,
9394                                immh, immb, opcode, rn, rd);
9395         break;
9396     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9397     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9398         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9399                                immh, immb, opcode, rn, rd);
9400         break;
9401     case 0xc: /* SQSHLU */
9402         if (!is_u) {
9403             unallocated_encoding(s);
9404             return;
9405         }
9406         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9407         break;
9408     case 0xe: /* SQSHL, UQSHL */
9409         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9410         break;
9411     case 0x1f: /* FCVTZS, FCVTZU */
9412         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9413         break;
9414     default:
9415         unallocated_encoding(s);
9416         break;
9417     }
9418 }
9419 
9420 /* AdvSIMD scalar three different
9421  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
9422  * +-----+---+-----------+------+---+------+--------+-----+------+------+
9423  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
9424  * +-----+---+-----------+------+---+------+--------+-----+------+------+
9425  */
9426 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9427 {
9428     bool is_u = extract32(insn, 29, 1);
9429     int size = extract32(insn, 22, 2);
9430     int opcode = extract32(insn, 12, 4);
9431     int rm = extract32(insn, 16, 5);
9432     int rn = extract32(insn, 5, 5);
9433     int rd = extract32(insn, 0, 5);
9434 
9435     if (is_u) {
9436         unallocated_encoding(s);
9437         return;
9438     }
9439 
9440     switch (opcode) {
9441     case 0x9: /* SQDMLAL, SQDMLAL2 */
9442     case 0xb: /* SQDMLSL, SQDMLSL2 */
9443     case 0xd: /* SQDMULL, SQDMULL2 */
9444         if (size == 0 || size == 3) {
9445             unallocated_encoding(s);
9446             return;
9447         }
9448         break;
9449     default:
9450         unallocated_encoding(s);
9451         return;
9452     }
9453 
9454     if (!fp_access_check(s)) {
9455         return;
9456     }
9457 
9458     if (size == 2) {
9459         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9460         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9461         TCGv_i64 tcg_res = tcg_temp_new_i64();
9462 
9463         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9464         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9465 
9466         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9467         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
9468 
9469         switch (opcode) {
9470         case 0xd: /* SQDMULL, SQDMULL2 */
9471             break;
9472         case 0xb: /* SQDMLSL, SQDMLSL2 */
9473             tcg_gen_neg_i64(tcg_res, tcg_res);
9474             /* fall through */
9475         case 0x9: /* SQDMLAL, SQDMLAL2 */
9476             read_vec_element(s, tcg_op1, rd, 0, MO_64);
9477             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
9478                                               tcg_res, tcg_op1);
9479             break;
9480         default:
9481             g_assert_not_reached();
9482         }
9483 
9484         write_fp_dreg(s, rd, tcg_res);
9485     } else {
9486         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9487         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9488         TCGv_i64 tcg_res = tcg_temp_new_i64();
9489 
9490         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9491         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
9492 
9493         switch (opcode) {
9494         case 0xd: /* SQDMULL, SQDMULL2 */
9495             break;
9496         case 0xb: /* SQDMLSL, SQDMLSL2 */
9497             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9498             /* fall through */
9499         case 0x9: /* SQDMLAL, SQDMLAL2 */
9500         {
9501             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9502             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9503             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
9504                                               tcg_res, tcg_op3);
9505             break;
9506         }
9507         default:
9508             g_assert_not_reached();
9509         }
9510 
9511         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9512         write_fp_dreg(s, rd, tcg_res);
9513     }
9514 }
9515 
9516 /* AdvSIMD scalar three same extra
9517  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9518  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9519  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9520  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9521  */
9522 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9523                                                    uint32_t insn)
9524 {
9525     int rd = extract32(insn, 0, 5);
9526     int rn = extract32(insn, 5, 5);
9527     int opcode = extract32(insn, 11, 4);
9528     int rm = extract32(insn, 16, 5);
9529     int size = extract32(insn, 22, 2);
9530     bool u = extract32(insn, 29, 1);
9531     TCGv_i32 ele1, ele2, ele3;
9532     TCGv_i64 res;
9533     bool feature;
9534 
9535     switch (u * 16 + opcode) {
9536     case 0x10: /* SQRDMLAH (vector) */
9537     case 0x11: /* SQRDMLSH (vector) */
9538         if (size != 1 && size != 2) {
9539             unallocated_encoding(s);
9540             return;
9541         }
9542         feature = dc_isar_feature(aa64_rdm, s);
9543         break;
9544     default:
9545         unallocated_encoding(s);
9546         return;
9547     }
9548     if (!feature) {
9549         unallocated_encoding(s);
9550         return;
9551     }
9552     if (!fp_access_check(s)) {
9553         return;
9554     }
9555 
9556     /* Do a single operation on the lowest element in the vector.
9557      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9558      * with no side effects for all these operations.
9559      * OPTME: special-purpose helpers would avoid doing some
9560      * unnecessary work in the helper for the 16 bit cases.
9561      */
9562     ele1 = tcg_temp_new_i32();
9563     ele2 = tcg_temp_new_i32();
9564     ele3 = tcg_temp_new_i32();
9565 
9566     read_vec_element_i32(s, ele1, rn, 0, size);
9567     read_vec_element_i32(s, ele2, rm, 0, size);
9568     read_vec_element_i32(s, ele3, rd, 0, size);
9569 
9570     switch (opcode) {
9571     case 0x0: /* SQRDMLAH */
9572         if (size == 1) {
9573             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9574         } else {
9575             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9576         }
9577         break;
9578     case 0x1: /* SQRDMLSH */
9579         if (size == 1) {
9580             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9581         } else {
9582             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9583         }
9584         break;
9585     default:
9586         g_assert_not_reached();
9587     }
9588 
9589     res = tcg_temp_new_i64();
9590     tcg_gen_extu_i32_i64(res, ele3);
9591     write_fp_dreg(s, rd, res);
9592 }
9593 
9594 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9595                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9596                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9597 {
9598     /* Handle 64->64 opcodes which are shared between the scalar and
9599      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9600      * is valid in either group and also the double-precision fp ops.
9601      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9602      * requires them.
9603      */
9604     TCGCond cond;
9605 
9606     switch (opcode) {
9607     case 0x4: /* CLS, CLZ */
9608         if (u) {
9609             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9610         } else {
9611             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9612         }
9613         break;
9614     case 0x5: /* NOT */
9615         /* This opcode is shared with CNT and RBIT but we have earlier
9616          * enforced that size == 3 if and only if this is the NOT insn.
9617          */
9618         tcg_gen_not_i64(tcg_rd, tcg_rn);
9619         break;
9620     case 0x7: /* SQABS, SQNEG */
9621         if (u) {
9622             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9623         } else {
9624             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9625         }
9626         break;
9627     case 0xa: /* CMLT */
9628         cond = TCG_COND_LT;
9629     do_cmop:
9630         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9631         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9632         break;
9633     case 0x8: /* CMGT, CMGE */
9634         cond = u ? TCG_COND_GE : TCG_COND_GT;
9635         goto do_cmop;
9636     case 0x9: /* CMEQ, CMLE */
9637         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9638         goto do_cmop;
9639     case 0xb: /* ABS, NEG */
9640         if (u) {
9641             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9642         } else {
9643             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9644         }
9645         break;
9646     case 0x2f: /* FABS */
9647         gen_vfp_absd(tcg_rd, tcg_rn);
9648         break;
9649     case 0x6f: /* FNEG */
9650         gen_vfp_negd(tcg_rd, tcg_rn);
9651         break;
9652     case 0x7f: /* FSQRT */
9653         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9654         break;
9655     case 0x1a: /* FCVTNS */
9656     case 0x1b: /* FCVTMS */
9657     case 0x1c: /* FCVTAS */
9658     case 0x3a: /* FCVTPS */
9659     case 0x3b: /* FCVTZS */
9660         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9661         break;
9662     case 0x5a: /* FCVTNU */
9663     case 0x5b: /* FCVTMU */
9664     case 0x5c: /* FCVTAU */
9665     case 0x7a: /* FCVTPU */
9666     case 0x7b: /* FCVTZU */
9667         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9668         break;
9669     case 0x18: /* FRINTN */
9670     case 0x19: /* FRINTM */
9671     case 0x38: /* FRINTP */
9672     case 0x39: /* FRINTZ */
9673     case 0x58: /* FRINTA */
9674     case 0x79: /* FRINTI */
9675         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9676         break;
9677     case 0x59: /* FRINTX */
9678         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9679         break;
9680     case 0x1e: /* FRINT32Z */
9681     case 0x5e: /* FRINT32X */
9682         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9683         break;
9684     case 0x1f: /* FRINT64Z */
9685     case 0x5f: /* FRINT64X */
9686         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9687         break;
9688     default:
9689         g_assert_not_reached();
9690     }
9691 }
9692 
9693 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9694                                    bool is_scalar, bool is_u, bool is_q,
9695                                    int size, int rn, int rd)
9696 {
9697     bool is_double = (size == MO_64);
9698     TCGv_ptr fpst;
9699 
9700     if (!fp_access_check(s)) {
9701         return;
9702     }
9703 
9704     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9705 
9706     if (is_double) {
9707         TCGv_i64 tcg_op = tcg_temp_new_i64();
9708         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9709         TCGv_i64 tcg_res = tcg_temp_new_i64();
9710         NeonGenTwoDoubleOpFn *genfn;
9711         bool swap = false;
9712         int pass;
9713 
9714         switch (opcode) {
9715         case 0x2e: /* FCMLT (zero) */
9716             swap = true;
9717             /* fallthrough */
9718         case 0x2c: /* FCMGT (zero) */
9719             genfn = gen_helper_neon_cgt_f64;
9720             break;
9721         case 0x2d: /* FCMEQ (zero) */
9722             genfn = gen_helper_neon_ceq_f64;
9723             break;
9724         case 0x6d: /* FCMLE (zero) */
9725             swap = true;
9726             /* fall through */
9727         case 0x6c: /* FCMGE (zero) */
9728             genfn = gen_helper_neon_cge_f64;
9729             break;
9730         default:
9731             g_assert_not_reached();
9732         }
9733 
9734         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9735             read_vec_element(s, tcg_op, rn, pass, MO_64);
9736             if (swap) {
9737                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9738             } else {
9739                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9740             }
9741             write_vec_element(s, tcg_res, rd, pass, MO_64);
9742         }
9743 
9744         clear_vec_high(s, !is_scalar, rd);
9745     } else {
9746         TCGv_i32 tcg_op = tcg_temp_new_i32();
9747         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9748         TCGv_i32 tcg_res = tcg_temp_new_i32();
9749         NeonGenTwoSingleOpFn *genfn;
9750         bool swap = false;
9751         int pass, maxpasses;
9752 
9753         if (size == MO_16) {
9754             switch (opcode) {
9755             case 0x2e: /* FCMLT (zero) */
9756                 swap = true;
9757                 /* fall through */
9758             case 0x2c: /* FCMGT (zero) */
9759                 genfn = gen_helper_advsimd_cgt_f16;
9760                 break;
9761             case 0x2d: /* FCMEQ (zero) */
9762                 genfn = gen_helper_advsimd_ceq_f16;
9763                 break;
9764             case 0x6d: /* FCMLE (zero) */
9765                 swap = true;
9766                 /* fall through */
9767             case 0x6c: /* FCMGE (zero) */
9768                 genfn = gen_helper_advsimd_cge_f16;
9769                 break;
9770             default:
9771                 g_assert_not_reached();
9772             }
9773         } else {
9774             switch (opcode) {
9775             case 0x2e: /* FCMLT (zero) */
9776                 swap = true;
9777                 /* fall through */
9778             case 0x2c: /* FCMGT (zero) */
9779                 genfn = gen_helper_neon_cgt_f32;
9780                 break;
9781             case 0x2d: /* FCMEQ (zero) */
9782                 genfn = gen_helper_neon_ceq_f32;
9783                 break;
9784             case 0x6d: /* FCMLE (zero) */
9785                 swap = true;
9786                 /* fall through */
9787             case 0x6c: /* FCMGE (zero) */
9788                 genfn = gen_helper_neon_cge_f32;
9789                 break;
9790             default:
9791                 g_assert_not_reached();
9792             }
9793         }
9794 
9795         if (is_scalar) {
9796             maxpasses = 1;
9797         } else {
9798             int vector_size = 8 << is_q;
9799             maxpasses = vector_size >> size;
9800         }
9801 
9802         for (pass = 0; pass < maxpasses; pass++) {
9803             read_vec_element_i32(s, tcg_op, rn, pass, size);
9804             if (swap) {
9805                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9806             } else {
9807                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9808             }
9809             if (is_scalar) {
9810                 write_fp_sreg(s, rd, tcg_res);
9811             } else {
9812                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9813             }
9814         }
9815 
9816         if (!is_scalar) {
9817             clear_vec_high(s, is_q, rd);
9818         }
9819     }
9820 }
9821 
9822 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9823                                     bool is_scalar, bool is_u, bool is_q,
9824                                     int size, int rn, int rd)
9825 {
9826     bool is_double = (size == 3);
9827     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9828 
9829     if (is_double) {
9830         TCGv_i64 tcg_op = tcg_temp_new_i64();
9831         TCGv_i64 tcg_res = tcg_temp_new_i64();
9832         int pass;
9833 
9834         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9835             read_vec_element(s, tcg_op, rn, pass, MO_64);
9836             switch (opcode) {
9837             case 0x3d: /* FRECPE */
9838                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9839                 break;
9840             case 0x3f: /* FRECPX */
9841                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9842                 break;
9843             case 0x7d: /* FRSQRTE */
9844                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9845                 break;
9846             default:
9847                 g_assert_not_reached();
9848             }
9849             write_vec_element(s, tcg_res, rd, pass, MO_64);
9850         }
9851         clear_vec_high(s, !is_scalar, rd);
9852     } else {
9853         TCGv_i32 tcg_op = tcg_temp_new_i32();
9854         TCGv_i32 tcg_res = tcg_temp_new_i32();
9855         int pass, maxpasses;
9856 
9857         if (is_scalar) {
9858             maxpasses = 1;
9859         } else {
9860             maxpasses = is_q ? 4 : 2;
9861         }
9862 
9863         for (pass = 0; pass < maxpasses; pass++) {
9864             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9865 
9866             switch (opcode) {
9867             case 0x3c: /* URECPE */
9868                 gen_helper_recpe_u32(tcg_res, tcg_op);
9869                 break;
9870             case 0x3d: /* FRECPE */
9871                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9872                 break;
9873             case 0x3f: /* FRECPX */
9874                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9875                 break;
9876             case 0x7d: /* FRSQRTE */
9877                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9878                 break;
9879             default:
9880                 g_assert_not_reached();
9881             }
9882 
9883             if (is_scalar) {
9884                 write_fp_sreg(s, rd, tcg_res);
9885             } else {
9886                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9887             }
9888         }
9889         if (!is_scalar) {
9890             clear_vec_high(s, is_q, rd);
9891         }
9892     }
9893 }
9894 
9895 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9896                                 int opcode, bool u, bool is_q,
9897                                 int size, int rn, int rd)
9898 {
9899     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9900      * in the source becomes a size element in the destination).
9901      */
9902     int pass;
9903     TCGv_i32 tcg_res[2];
9904     int destelt = is_q ? 2 : 0;
9905     int passes = scalar ? 1 : 2;
9906 
9907     if (scalar) {
9908         tcg_res[1] = tcg_constant_i32(0);
9909     }
9910 
9911     for (pass = 0; pass < passes; pass++) {
9912         TCGv_i64 tcg_op = tcg_temp_new_i64();
9913         NeonGenNarrowFn *genfn = NULL;
9914         NeonGenNarrowEnvFn *genenvfn = NULL;
9915 
9916         if (scalar) {
9917             read_vec_element(s, tcg_op, rn, pass, size + 1);
9918         } else {
9919             read_vec_element(s, tcg_op, rn, pass, MO_64);
9920         }
9921         tcg_res[pass] = tcg_temp_new_i32();
9922 
9923         switch (opcode) {
9924         case 0x12: /* XTN, SQXTUN */
9925         {
9926             static NeonGenNarrowFn * const xtnfns[3] = {
9927                 gen_helper_neon_narrow_u8,
9928                 gen_helper_neon_narrow_u16,
9929                 tcg_gen_extrl_i64_i32,
9930             };
9931             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9932                 gen_helper_neon_unarrow_sat8,
9933                 gen_helper_neon_unarrow_sat16,
9934                 gen_helper_neon_unarrow_sat32,
9935             };
9936             if (u) {
9937                 genenvfn = sqxtunfns[size];
9938             } else {
9939                 genfn = xtnfns[size];
9940             }
9941             break;
9942         }
9943         case 0x14: /* SQXTN, UQXTN */
9944         {
9945             static NeonGenNarrowEnvFn * const fns[3][2] = {
9946                 { gen_helper_neon_narrow_sat_s8,
9947                   gen_helper_neon_narrow_sat_u8 },
9948                 { gen_helper_neon_narrow_sat_s16,
9949                   gen_helper_neon_narrow_sat_u16 },
9950                 { gen_helper_neon_narrow_sat_s32,
9951                   gen_helper_neon_narrow_sat_u32 },
9952             };
9953             genenvfn = fns[size][u];
9954             break;
9955         }
9956         case 0x16: /* FCVTN, FCVTN2 */
9957             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9958             if (size == 2) {
9959                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9960             } else {
9961                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9962                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9963                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9964                 TCGv_i32 ahp = get_ahp_flag();
9965 
9966                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9967                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9968                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9969                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9970             }
9971             break;
9972         case 0x36: /* BFCVTN, BFCVTN2 */
9973             {
9974                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9975                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9976             }
9977             break;
9978         case 0x56:  /* FCVTXN, FCVTXN2 */
9979             /* 64 bit to 32 bit float conversion
9980              * with von Neumann rounding (round to odd)
9981              */
9982             assert(size == 2);
9983             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
9984             break;
9985         default:
9986             g_assert_not_reached();
9987         }
9988 
9989         if (genfn) {
9990             genfn(tcg_res[pass], tcg_op);
9991         } else if (genenvfn) {
9992             genenvfn(tcg_res[pass], tcg_env, tcg_op);
9993         }
9994     }
9995 
9996     for (pass = 0; pass < 2; pass++) {
9997         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9998     }
9999     clear_vec_high(s, is_q, rd);
10000 }
10001 
10002 /* AdvSIMD scalar two reg misc
10003  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10004  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10005  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10006  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10007  */
10008 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10009 {
10010     int rd = extract32(insn, 0, 5);
10011     int rn = extract32(insn, 5, 5);
10012     int opcode = extract32(insn, 12, 5);
10013     int size = extract32(insn, 22, 2);
10014     bool u = extract32(insn, 29, 1);
10015     bool is_fcvt = false;
10016     int rmode;
10017     TCGv_i32 tcg_rmode;
10018     TCGv_ptr tcg_fpstatus;
10019 
10020     switch (opcode) {
10021     case 0x7: /* SQABS / SQNEG */
10022         break;
10023     case 0xa: /* CMLT */
10024         if (u) {
10025             unallocated_encoding(s);
10026             return;
10027         }
10028         /* fall through */
10029     case 0x8: /* CMGT, CMGE */
10030     case 0x9: /* CMEQ, CMLE */
10031     case 0xb: /* ABS, NEG */
10032         if (size != 3) {
10033             unallocated_encoding(s);
10034             return;
10035         }
10036         break;
10037     case 0x12: /* SQXTUN */
10038         if (!u) {
10039             unallocated_encoding(s);
10040             return;
10041         }
10042         /* fall through */
10043     case 0x14: /* SQXTN, UQXTN */
10044         if (size == 3) {
10045             unallocated_encoding(s);
10046             return;
10047         }
10048         if (!fp_access_check(s)) {
10049             return;
10050         }
10051         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10052         return;
10053     case 0xc ... 0xf:
10054     case 0x16 ... 0x1d:
10055     case 0x1f:
10056         /* Floating point: U, size[1] and opcode indicate operation;
10057          * size[0] indicates single or double precision.
10058          */
10059         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10060         size = extract32(size, 0, 1) ? 3 : 2;
10061         switch (opcode) {
10062         case 0x2c: /* FCMGT (zero) */
10063         case 0x2d: /* FCMEQ (zero) */
10064         case 0x2e: /* FCMLT (zero) */
10065         case 0x6c: /* FCMGE (zero) */
10066         case 0x6d: /* FCMLE (zero) */
10067             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10068             return;
10069         case 0x1d: /* SCVTF */
10070         case 0x5d: /* UCVTF */
10071         {
10072             bool is_signed = (opcode == 0x1d);
10073             if (!fp_access_check(s)) {
10074                 return;
10075             }
10076             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10077             return;
10078         }
10079         case 0x3d: /* FRECPE */
10080         case 0x3f: /* FRECPX */
10081         case 0x7d: /* FRSQRTE */
10082             if (!fp_access_check(s)) {
10083                 return;
10084             }
10085             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10086             return;
10087         case 0x1a: /* FCVTNS */
10088         case 0x1b: /* FCVTMS */
10089         case 0x3a: /* FCVTPS */
10090         case 0x3b: /* FCVTZS */
10091         case 0x5a: /* FCVTNU */
10092         case 0x5b: /* FCVTMU */
10093         case 0x7a: /* FCVTPU */
10094         case 0x7b: /* FCVTZU */
10095             is_fcvt = true;
10096             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10097             break;
10098         case 0x1c: /* FCVTAS */
10099         case 0x5c: /* FCVTAU */
10100             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10101             is_fcvt = true;
10102             rmode = FPROUNDING_TIEAWAY;
10103             break;
10104         case 0x56: /* FCVTXN, FCVTXN2 */
10105             if (size == 2) {
10106                 unallocated_encoding(s);
10107                 return;
10108             }
10109             if (!fp_access_check(s)) {
10110                 return;
10111             }
10112             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10113             return;
10114         default:
10115             unallocated_encoding(s);
10116             return;
10117         }
10118         break;
10119     default:
10120     case 0x3: /* USQADD / SUQADD */
10121         unallocated_encoding(s);
10122         return;
10123     }
10124 
10125     if (!fp_access_check(s)) {
10126         return;
10127     }
10128 
10129     if (is_fcvt) {
10130         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10131         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10132     } else {
10133         tcg_fpstatus = NULL;
10134         tcg_rmode = NULL;
10135     }
10136 
10137     if (size == 3) {
10138         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10139         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10140 
10141         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10142         write_fp_dreg(s, rd, tcg_rd);
10143     } else {
10144         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10145         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10146 
10147         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10148 
10149         switch (opcode) {
10150         case 0x7: /* SQABS, SQNEG */
10151         {
10152             NeonGenOneOpEnvFn *genfn;
10153             static NeonGenOneOpEnvFn * const fns[3][2] = {
10154                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10155                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10156                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10157             };
10158             genfn = fns[size][u];
10159             genfn(tcg_rd, tcg_env, tcg_rn);
10160             break;
10161         }
10162         case 0x1a: /* FCVTNS */
10163         case 0x1b: /* FCVTMS */
10164         case 0x1c: /* FCVTAS */
10165         case 0x3a: /* FCVTPS */
10166         case 0x3b: /* FCVTZS */
10167             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10168                                  tcg_fpstatus);
10169             break;
10170         case 0x5a: /* FCVTNU */
10171         case 0x5b: /* FCVTMU */
10172         case 0x5c: /* FCVTAU */
10173         case 0x7a: /* FCVTPU */
10174         case 0x7b: /* FCVTZU */
10175             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10176                                  tcg_fpstatus);
10177             break;
10178         default:
10179             g_assert_not_reached();
10180         }
10181 
10182         write_fp_sreg(s, rd, tcg_rd);
10183     }
10184 
10185     if (is_fcvt) {
10186         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10187     }
10188 }
10189 
10190 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10191 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10192                                  int immh, int immb, int opcode, int rn, int rd)
10193 {
10194     int size = 32 - clz32(immh) - 1;
10195     int immhb = immh << 3 | immb;
10196     int shift = 2 * (8 << size) - immhb;
10197     GVecGen2iFn *gvec_fn;
10198 
10199     if (extract32(immh, 3, 1) && !is_q) {
10200         unallocated_encoding(s);
10201         return;
10202     }
10203     tcg_debug_assert(size <= 3);
10204 
10205     if (!fp_access_check(s)) {
10206         return;
10207     }
10208 
10209     switch (opcode) {
10210     case 0x02: /* SSRA / USRA (accumulate) */
10211         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10212         break;
10213 
10214     case 0x08: /* SRI */
10215         gvec_fn = gen_gvec_sri;
10216         break;
10217 
10218     case 0x00: /* SSHR / USHR */
10219         if (is_u) {
10220             if (shift == 8 << size) {
10221                 /* Shift count the same size as element size produces zero.  */
10222                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10223                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10224                 return;
10225             }
10226             gvec_fn = tcg_gen_gvec_shri;
10227         } else {
10228             /* Shift count the same size as element size produces all sign.  */
10229             if (shift == 8 << size) {
10230                 shift -= 1;
10231             }
10232             gvec_fn = tcg_gen_gvec_sari;
10233         }
10234         break;
10235 
10236     case 0x04: /* SRSHR / URSHR (rounding) */
10237         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10238         break;
10239 
10240     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10241         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10242         break;
10243 
10244     default:
10245         g_assert_not_reached();
10246     }
10247 
10248     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10249 }
10250 
10251 /* SHL/SLI - Vector shift left */
10252 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10253                                  int immh, int immb, int opcode, int rn, int rd)
10254 {
10255     int size = 32 - clz32(immh) - 1;
10256     int immhb = immh << 3 | immb;
10257     int shift = immhb - (8 << size);
10258 
10259     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10260     assert(size >= 0 && size <= 3);
10261 
10262     if (extract32(immh, 3, 1) && !is_q) {
10263         unallocated_encoding(s);
10264         return;
10265     }
10266 
10267     if (!fp_access_check(s)) {
10268         return;
10269     }
10270 
10271     if (insert) {
10272         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10273     } else {
10274         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10275     }
10276 }
10277 
10278 /* USHLL/SHLL - Vector shift left with widening */
10279 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10280                                  int immh, int immb, int opcode, int rn, int rd)
10281 {
10282     int size = 32 - clz32(immh) - 1;
10283     int immhb = immh << 3 | immb;
10284     int shift = immhb - (8 << size);
10285     int dsize = 64;
10286     int esize = 8 << size;
10287     int elements = dsize/esize;
10288     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10289     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10290     int i;
10291 
10292     if (size >= 3) {
10293         unallocated_encoding(s);
10294         return;
10295     }
10296 
10297     if (!fp_access_check(s)) {
10298         return;
10299     }
10300 
10301     /* For the LL variants the store is larger than the load,
10302      * so if rd == rn we would overwrite parts of our input.
10303      * So load everything right now and use shifts in the main loop.
10304      */
10305     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10306 
10307     for (i = 0; i < elements; i++) {
10308         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10309         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10310         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10311         write_vec_element(s, tcg_rd, rd, i, size + 1);
10312     }
10313 }
10314 
10315 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10316 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10317                                  int immh, int immb, int opcode, int rn, int rd)
10318 {
10319     int immhb = immh << 3 | immb;
10320     int size = 32 - clz32(immh) - 1;
10321     int dsize = 64;
10322     int esize = 8 << size;
10323     int elements = dsize/esize;
10324     int shift = (2 * esize) - immhb;
10325     bool round = extract32(opcode, 0, 1);
10326     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10327     TCGv_i64 tcg_round;
10328     int i;
10329 
10330     if (extract32(immh, 3, 1)) {
10331         unallocated_encoding(s);
10332         return;
10333     }
10334 
10335     if (!fp_access_check(s)) {
10336         return;
10337     }
10338 
10339     tcg_rn = tcg_temp_new_i64();
10340     tcg_rd = tcg_temp_new_i64();
10341     tcg_final = tcg_temp_new_i64();
10342     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10343 
10344     if (round) {
10345         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10346     } else {
10347         tcg_round = NULL;
10348     }
10349 
10350     for (i = 0; i < elements; i++) {
10351         read_vec_element(s, tcg_rn, rn, i, size+1);
10352         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10353                                 false, true, size+1, shift);
10354 
10355         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10356     }
10357 
10358     if (!is_q) {
10359         write_vec_element(s, tcg_final, rd, 0, MO_64);
10360     } else {
10361         write_vec_element(s, tcg_final, rd, 1, MO_64);
10362     }
10363 
10364     clear_vec_high(s, is_q, rd);
10365 }
10366 
10367 
10368 /* AdvSIMD shift by immediate
10369  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10370  * +---+---+---+-------------+------+------+--------+---+------+------+
10371  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10372  * +---+---+---+-------------+------+------+--------+---+------+------+
10373  */
10374 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10375 {
10376     int rd = extract32(insn, 0, 5);
10377     int rn = extract32(insn, 5, 5);
10378     int opcode = extract32(insn, 11, 5);
10379     int immb = extract32(insn, 16, 3);
10380     int immh = extract32(insn, 19, 4);
10381     bool is_u = extract32(insn, 29, 1);
10382     bool is_q = extract32(insn, 30, 1);
10383 
10384     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10385     assert(immh != 0);
10386 
10387     switch (opcode) {
10388     case 0x08: /* SRI */
10389         if (!is_u) {
10390             unallocated_encoding(s);
10391             return;
10392         }
10393         /* fall through */
10394     case 0x00: /* SSHR / USHR */
10395     case 0x02: /* SSRA / USRA (accumulate) */
10396     case 0x04: /* SRSHR / URSHR (rounding) */
10397     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10398         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10399         break;
10400     case 0x0a: /* SHL / SLI */
10401         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10402         break;
10403     case 0x10: /* SHRN */
10404     case 0x11: /* RSHRN / SQRSHRUN */
10405         if (is_u) {
10406             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10407                                    opcode, rn, rd);
10408         } else {
10409             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10410         }
10411         break;
10412     case 0x12: /* SQSHRN / UQSHRN */
10413     case 0x13: /* SQRSHRN / UQRSHRN */
10414         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10415                                opcode, rn, rd);
10416         break;
10417     case 0x14: /* SSHLL / USHLL */
10418         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10419         break;
10420     case 0x1c: /* SCVTF / UCVTF */
10421         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10422                                      opcode, rn, rd);
10423         break;
10424     case 0xc: /* SQSHLU */
10425         if (!is_u) {
10426             unallocated_encoding(s);
10427             return;
10428         }
10429         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10430         break;
10431     case 0xe: /* SQSHL, UQSHL */
10432         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10433         break;
10434     case 0x1f: /* FCVTZS/ FCVTZU */
10435         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10436         return;
10437     default:
10438         unallocated_encoding(s);
10439         return;
10440     }
10441 }
10442 
10443 /* Generate code to do a "long" addition or subtraction, ie one done in
10444  * TCGv_i64 on vector lanes twice the width specified by size.
10445  */
10446 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10447                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10448 {
10449     static NeonGenTwo64OpFn * const fns[3][2] = {
10450         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10451         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10452         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10453     };
10454     NeonGenTwo64OpFn *genfn;
10455     assert(size < 3);
10456 
10457     genfn = fns[size][is_sub];
10458     genfn(tcg_res, tcg_op1, tcg_op2);
10459 }
10460 
10461 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10462                                 int opcode, int rd, int rn, int rm)
10463 {
10464     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10465     TCGv_i64 tcg_res[2];
10466     int pass, accop;
10467 
10468     tcg_res[0] = tcg_temp_new_i64();
10469     tcg_res[1] = tcg_temp_new_i64();
10470 
10471     /* Does this op do an adding accumulate, a subtracting accumulate,
10472      * or no accumulate at all?
10473      */
10474     switch (opcode) {
10475     case 5:
10476     case 8:
10477     case 9:
10478         accop = 1;
10479         break;
10480     case 10:
10481     case 11:
10482         accop = -1;
10483         break;
10484     default:
10485         accop = 0;
10486         break;
10487     }
10488 
10489     if (accop != 0) {
10490         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10491         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10492     }
10493 
10494     /* size == 2 means two 32x32->64 operations; this is worth special
10495      * casing because we can generally handle it inline.
10496      */
10497     if (size == 2) {
10498         for (pass = 0; pass < 2; pass++) {
10499             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10500             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10501             TCGv_i64 tcg_passres;
10502             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10503 
10504             int elt = pass + is_q * 2;
10505 
10506             read_vec_element(s, tcg_op1, rn, elt, memop);
10507             read_vec_element(s, tcg_op2, rm, elt, memop);
10508 
10509             if (accop == 0) {
10510                 tcg_passres = tcg_res[pass];
10511             } else {
10512                 tcg_passres = tcg_temp_new_i64();
10513             }
10514 
10515             switch (opcode) {
10516             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10517                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10518                 break;
10519             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10520                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10521                 break;
10522             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10523             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10524             {
10525                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10526                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10527 
10528                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10529                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10530                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10531                                     tcg_passres,
10532                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10533                 break;
10534             }
10535             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10536             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10537             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10538                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10539                 break;
10540             case 9: /* SQDMLAL, SQDMLAL2 */
10541             case 11: /* SQDMLSL, SQDMLSL2 */
10542             case 13: /* SQDMULL, SQDMULL2 */
10543                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10544                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10545                                                   tcg_passres, tcg_passres);
10546                 break;
10547             default:
10548                 g_assert_not_reached();
10549             }
10550 
10551             if (opcode == 9 || opcode == 11) {
10552                 /* saturating accumulate ops */
10553                 if (accop < 0) {
10554                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10555                 }
10556                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10557                                                   tcg_res[pass], tcg_passres);
10558             } else if (accop > 0) {
10559                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10560             } else if (accop < 0) {
10561                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10562             }
10563         }
10564     } else {
10565         /* size 0 or 1, generally helper functions */
10566         for (pass = 0; pass < 2; pass++) {
10567             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10568             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10569             TCGv_i64 tcg_passres;
10570             int elt = pass + is_q * 2;
10571 
10572             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10573             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10574 
10575             if (accop == 0) {
10576                 tcg_passres = tcg_res[pass];
10577             } else {
10578                 tcg_passres = tcg_temp_new_i64();
10579             }
10580 
10581             switch (opcode) {
10582             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10583             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10584             {
10585                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10586                 static NeonGenWidenFn * const widenfns[2][2] = {
10587                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10588                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10589                 };
10590                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10591 
10592                 widenfn(tcg_op2_64, tcg_op2);
10593                 widenfn(tcg_passres, tcg_op1);
10594                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10595                               tcg_passres, tcg_op2_64);
10596                 break;
10597             }
10598             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10599             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10600                 if (size == 0) {
10601                     if (is_u) {
10602                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10603                     } else {
10604                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10605                     }
10606                 } else {
10607                     if (is_u) {
10608                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10609                     } else {
10610                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10611                     }
10612                 }
10613                 break;
10614             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10615             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10616             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10617                 if (size == 0) {
10618                     if (is_u) {
10619                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10620                     } else {
10621                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10622                     }
10623                 } else {
10624                     if (is_u) {
10625                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10626                     } else {
10627                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10628                     }
10629                 }
10630                 break;
10631             case 9: /* SQDMLAL, SQDMLAL2 */
10632             case 11: /* SQDMLSL, SQDMLSL2 */
10633             case 13: /* SQDMULL, SQDMULL2 */
10634                 assert(size == 1);
10635                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10636                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10637                                                   tcg_passres, tcg_passres);
10638                 break;
10639             default:
10640                 g_assert_not_reached();
10641             }
10642 
10643             if (accop != 0) {
10644                 if (opcode == 9 || opcode == 11) {
10645                     /* saturating accumulate ops */
10646                     if (accop < 0) {
10647                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10648                     }
10649                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10650                                                       tcg_res[pass],
10651                                                       tcg_passres);
10652                 } else {
10653                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10654                                   tcg_res[pass], tcg_passres);
10655                 }
10656             }
10657         }
10658     }
10659 
10660     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10661     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10662 }
10663 
10664 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10665                             int opcode, int rd, int rn, int rm)
10666 {
10667     TCGv_i64 tcg_res[2];
10668     int part = is_q ? 2 : 0;
10669     int pass;
10670 
10671     for (pass = 0; pass < 2; pass++) {
10672         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10673         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10674         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10675         static NeonGenWidenFn * const widenfns[3][2] = {
10676             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10677             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10678             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10679         };
10680         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10681 
10682         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10683         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10684         widenfn(tcg_op2_wide, tcg_op2);
10685         tcg_res[pass] = tcg_temp_new_i64();
10686         gen_neon_addl(size, (opcode == 3),
10687                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10688     }
10689 
10690     for (pass = 0; pass < 2; pass++) {
10691         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10692     }
10693 }
10694 
10695 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10696 {
10697     tcg_gen_addi_i64(in, in, 1U << 31);
10698     tcg_gen_extrh_i64_i32(res, in);
10699 }
10700 
10701 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10702                                  int opcode, int rd, int rn, int rm)
10703 {
10704     TCGv_i32 tcg_res[2];
10705     int part = is_q ? 2 : 0;
10706     int pass;
10707 
10708     for (pass = 0; pass < 2; pass++) {
10709         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10710         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10711         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10712         static NeonGenNarrowFn * const narrowfns[3][2] = {
10713             { gen_helper_neon_narrow_high_u8,
10714               gen_helper_neon_narrow_round_high_u8 },
10715             { gen_helper_neon_narrow_high_u16,
10716               gen_helper_neon_narrow_round_high_u16 },
10717             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10718         };
10719         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10720 
10721         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10722         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10723 
10724         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10725 
10726         tcg_res[pass] = tcg_temp_new_i32();
10727         gennarrow(tcg_res[pass], tcg_wideres);
10728     }
10729 
10730     for (pass = 0; pass < 2; pass++) {
10731         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10732     }
10733     clear_vec_high(s, is_q, rd);
10734 }
10735 
10736 /* AdvSIMD three different
10737  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10738  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10739  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10740  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10741  */
10742 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10743 {
10744     /* Instructions in this group fall into three basic classes
10745      * (in each case with the operation working on each element in
10746      * the input vectors):
10747      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10748      *     128 bit input)
10749      * (2) wide 64 x 128 -> 128
10750      * (3) narrowing 128 x 128 -> 64
10751      * Here we do initial decode, catch unallocated cases and
10752      * dispatch to separate functions for each class.
10753      */
10754     int is_q = extract32(insn, 30, 1);
10755     int is_u = extract32(insn, 29, 1);
10756     int size = extract32(insn, 22, 2);
10757     int opcode = extract32(insn, 12, 4);
10758     int rm = extract32(insn, 16, 5);
10759     int rn = extract32(insn, 5, 5);
10760     int rd = extract32(insn, 0, 5);
10761 
10762     switch (opcode) {
10763     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10764     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10765         /* 64 x 128 -> 128 */
10766         if (size == 3) {
10767             unallocated_encoding(s);
10768             return;
10769         }
10770         if (!fp_access_check(s)) {
10771             return;
10772         }
10773         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10774         break;
10775     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10776     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10777         /* 128 x 128 -> 64 */
10778         if (size == 3) {
10779             unallocated_encoding(s);
10780             return;
10781         }
10782         if (!fp_access_check(s)) {
10783             return;
10784         }
10785         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10786         break;
10787     case 14: /* PMULL, PMULL2 */
10788         if (is_u) {
10789             unallocated_encoding(s);
10790             return;
10791         }
10792         switch (size) {
10793         case 0: /* PMULL.P8 */
10794             if (!fp_access_check(s)) {
10795                 return;
10796             }
10797             /* The Q field specifies lo/hi half input for this insn.  */
10798             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10799                              gen_helper_neon_pmull_h);
10800             break;
10801 
10802         case 3: /* PMULL.P64 */
10803             if (!dc_isar_feature(aa64_pmull, s)) {
10804                 unallocated_encoding(s);
10805                 return;
10806             }
10807             if (!fp_access_check(s)) {
10808                 return;
10809             }
10810             /* The Q field specifies lo/hi half input for this insn.  */
10811             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10812                              gen_helper_gvec_pmull_q);
10813             break;
10814 
10815         default:
10816             unallocated_encoding(s);
10817             break;
10818         }
10819         return;
10820     case 9: /* SQDMLAL, SQDMLAL2 */
10821     case 11: /* SQDMLSL, SQDMLSL2 */
10822     case 13: /* SQDMULL, SQDMULL2 */
10823         if (is_u || size == 0) {
10824             unallocated_encoding(s);
10825             return;
10826         }
10827         /* fall through */
10828     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10829     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10830     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10831     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10832     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10833     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10834     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10835         /* 64 x 64 -> 128 */
10836         if (size == 3) {
10837             unallocated_encoding(s);
10838             return;
10839         }
10840         if (!fp_access_check(s)) {
10841             return;
10842         }
10843 
10844         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10845         break;
10846     default:
10847         /* opcode 15 not allocated */
10848         unallocated_encoding(s);
10849         break;
10850     }
10851 }
10852 
10853 /* AdvSIMD three same extra
10854  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
10855  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
10856  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
10857  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
10858  */
10859 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
10860 {
10861     int rd = extract32(insn, 0, 5);
10862     int rn = extract32(insn, 5, 5);
10863     int opcode = extract32(insn, 11, 4);
10864     int rm = extract32(insn, 16, 5);
10865     int size = extract32(insn, 22, 2);
10866     bool u = extract32(insn, 29, 1);
10867     bool is_q = extract32(insn, 30, 1);
10868     bool feature;
10869     int rot;
10870 
10871     switch (u * 16 + opcode) {
10872     case 0x10: /* SQRDMLAH (vector) */
10873     case 0x11: /* SQRDMLSH (vector) */
10874         if (size != 1 && size != 2) {
10875             unallocated_encoding(s);
10876             return;
10877         }
10878         feature = dc_isar_feature(aa64_rdm, s);
10879         break;
10880     case 0x02: /* SDOT (vector) */
10881     case 0x12: /* UDOT (vector) */
10882         if (size != MO_32) {
10883             unallocated_encoding(s);
10884             return;
10885         }
10886         feature = dc_isar_feature(aa64_dp, s);
10887         break;
10888     case 0x03: /* USDOT */
10889         if (size != MO_32) {
10890             unallocated_encoding(s);
10891             return;
10892         }
10893         feature = dc_isar_feature(aa64_i8mm, s);
10894         break;
10895     case 0x04: /* SMMLA */
10896     case 0x14: /* UMMLA */
10897     case 0x05: /* USMMLA */
10898         if (!is_q || size != MO_32) {
10899             unallocated_encoding(s);
10900             return;
10901         }
10902         feature = dc_isar_feature(aa64_i8mm, s);
10903         break;
10904     case 0x18: /* FCMLA, #0 */
10905     case 0x19: /* FCMLA, #90 */
10906     case 0x1a: /* FCMLA, #180 */
10907     case 0x1b: /* FCMLA, #270 */
10908     case 0x1c: /* FCADD, #90 */
10909     case 0x1e: /* FCADD, #270 */
10910         if (size == 0
10911             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
10912             || (size == 3 && !is_q)) {
10913             unallocated_encoding(s);
10914             return;
10915         }
10916         feature = dc_isar_feature(aa64_fcma, s);
10917         break;
10918     case 0x1d: /* BFMMLA */
10919         if (size != MO_16 || !is_q) {
10920             unallocated_encoding(s);
10921             return;
10922         }
10923         feature = dc_isar_feature(aa64_bf16, s);
10924         break;
10925     case 0x1f:
10926         switch (size) {
10927         case 1: /* BFDOT */
10928         case 3: /* BFMLAL{B,T} */
10929             feature = dc_isar_feature(aa64_bf16, s);
10930             break;
10931         default:
10932             unallocated_encoding(s);
10933             return;
10934         }
10935         break;
10936     default:
10937         unallocated_encoding(s);
10938         return;
10939     }
10940     if (!feature) {
10941         unallocated_encoding(s);
10942         return;
10943     }
10944     if (!fp_access_check(s)) {
10945         return;
10946     }
10947 
10948     switch (opcode) {
10949     case 0x0: /* SQRDMLAH (vector) */
10950         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
10951         return;
10952 
10953     case 0x1: /* SQRDMLSH (vector) */
10954         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
10955         return;
10956 
10957     case 0x2: /* SDOT / UDOT */
10958         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
10959                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
10960         return;
10961 
10962     case 0x3: /* USDOT */
10963         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
10964         return;
10965 
10966     case 0x04: /* SMMLA, UMMLA */
10967         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
10968                          u ? gen_helper_gvec_ummla_b
10969                          : gen_helper_gvec_smmla_b);
10970         return;
10971     case 0x05: /* USMMLA */
10972         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
10973         return;
10974 
10975     case 0x8: /* FCMLA, #0 */
10976     case 0x9: /* FCMLA, #90 */
10977     case 0xa: /* FCMLA, #180 */
10978     case 0xb: /* FCMLA, #270 */
10979         rot = extract32(opcode, 0, 2);
10980         switch (size) {
10981         case 1:
10982             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
10983                               gen_helper_gvec_fcmlah);
10984             break;
10985         case 2:
10986             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
10987                               gen_helper_gvec_fcmlas);
10988             break;
10989         case 3:
10990             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
10991                               gen_helper_gvec_fcmlad);
10992             break;
10993         default:
10994             g_assert_not_reached();
10995         }
10996         return;
10997 
10998     case 0xc: /* FCADD, #90 */
10999     case 0xe: /* FCADD, #270 */
11000         rot = extract32(opcode, 1, 1);
11001         switch (size) {
11002         case 1:
11003             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11004                               gen_helper_gvec_fcaddh);
11005             break;
11006         case 2:
11007             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11008                               gen_helper_gvec_fcadds);
11009             break;
11010         case 3:
11011             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11012                               gen_helper_gvec_fcaddd);
11013             break;
11014         default:
11015             g_assert_not_reached();
11016         }
11017         return;
11018 
11019     case 0xd: /* BFMMLA */
11020         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11021         return;
11022     case 0xf:
11023         switch (size) {
11024         case 1: /* BFDOT */
11025             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11026             break;
11027         case 3: /* BFMLAL{B,T} */
11028             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11029                               gen_helper_gvec_bfmlal);
11030             break;
11031         default:
11032             g_assert_not_reached();
11033         }
11034         return;
11035 
11036     default:
11037         g_assert_not_reached();
11038     }
11039 }
11040 
11041 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11042                                   int size, int rn, int rd)
11043 {
11044     /* Handle 2-reg-misc ops which are widening (so each size element
11045      * in the source becomes a 2*size element in the destination.
11046      * The only instruction like this is FCVTL.
11047      */
11048     int pass;
11049 
11050     if (size == 3) {
11051         /* 32 -> 64 bit fp conversion */
11052         TCGv_i64 tcg_res[2];
11053         int srcelt = is_q ? 2 : 0;
11054 
11055         for (pass = 0; pass < 2; pass++) {
11056             TCGv_i32 tcg_op = tcg_temp_new_i32();
11057             tcg_res[pass] = tcg_temp_new_i64();
11058 
11059             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11060             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11061         }
11062         for (pass = 0; pass < 2; pass++) {
11063             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11064         }
11065     } else {
11066         /* 16 -> 32 bit fp conversion */
11067         int srcelt = is_q ? 4 : 0;
11068         TCGv_i32 tcg_res[4];
11069         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11070         TCGv_i32 ahp = get_ahp_flag();
11071 
11072         for (pass = 0; pass < 4; pass++) {
11073             tcg_res[pass] = tcg_temp_new_i32();
11074 
11075             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11076             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11077                                            fpst, ahp);
11078         }
11079         for (pass = 0; pass < 4; pass++) {
11080             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11081         }
11082     }
11083 }
11084 
11085 static void handle_rev(DisasContext *s, int opcode, bool u,
11086                        bool is_q, int size, int rn, int rd)
11087 {
11088     int op = (opcode << 1) | u;
11089     int opsz = op + size;
11090     int grp_size = 3 - opsz;
11091     int dsize = is_q ? 128 : 64;
11092     int i;
11093 
11094     if (opsz >= 3) {
11095         unallocated_encoding(s);
11096         return;
11097     }
11098 
11099     if (!fp_access_check(s)) {
11100         return;
11101     }
11102 
11103     if (size == 0) {
11104         /* Special case bytes, use bswap op on each group of elements */
11105         int groups = dsize / (8 << grp_size);
11106 
11107         for (i = 0; i < groups; i++) {
11108             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11109 
11110             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11111             switch (grp_size) {
11112             case MO_16:
11113                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11114                 break;
11115             case MO_32:
11116                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11117                 break;
11118             case MO_64:
11119                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11120                 break;
11121             default:
11122                 g_assert_not_reached();
11123             }
11124             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11125         }
11126         clear_vec_high(s, is_q, rd);
11127     } else {
11128         int revmask = (1 << grp_size) - 1;
11129         int esize = 8 << size;
11130         int elements = dsize / esize;
11131         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11132         TCGv_i64 tcg_rd[2];
11133 
11134         for (i = 0; i < 2; i++) {
11135             tcg_rd[i] = tcg_temp_new_i64();
11136             tcg_gen_movi_i64(tcg_rd[i], 0);
11137         }
11138 
11139         for (i = 0; i < elements; i++) {
11140             int e_rev = (i & 0xf) ^ revmask;
11141             int w = (e_rev * esize) / 64;
11142             int o = (e_rev * esize) % 64;
11143 
11144             read_vec_element(s, tcg_rn, rn, i, size);
11145             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11146         }
11147 
11148         for (i = 0; i < 2; i++) {
11149             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11150         }
11151         clear_vec_high(s, true, rd);
11152     }
11153 }
11154 
11155 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11156                                   bool is_q, int size, int rn, int rd)
11157 {
11158     /* Implement the pairwise operations from 2-misc:
11159      * SADDLP, UADDLP, SADALP, UADALP.
11160      * These all add pairs of elements in the input to produce a
11161      * double-width result element in the output (possibly accumulating).
11162      */
11163     bool accum = (opcode == 0x6);
11164     int maxpass = is_q ? 2 : 1;
11165     int pass;
11166     TCGv_i64 tcg_res[2];
11167 
11168     if (size == 2) {
11169         /* 32 + 32 -> 64 op */
11170         MemOp memop = size + (u ? 0 : MO_SIGN);
11171 
11172         for (pass = 0; pass < maxpass; pass++) {
11173             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11174             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11175 
11176             tcg_res[pass] = tcg_temp_new_i64();
11177 
11178             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11179             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11180             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11181             if (accum) {
11182                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11183                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11184             }
11185         }
11186     } else {
11187         for (pass = 0; pass < maxpass; pass++) {
11188             TCGv_i64 tcg_op = tcg_temp_new_i64();
11189             NeonGenOne64OpFn *genfn;
11190             static NeonGenOne64OpFn * const fns[2][2] = {
11191                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11192                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11193             };
11194 
11195             genfn = fns[size][u];
11196 
11197             tcg_res[pass] = tcg_temp_new_i64();
11198 
11199             read_vec_element(s, tcg_op, rn, pass, MO_64);
11200             genfn(tcg_res[pass], tcg_op);
11201 
11202             if (accum) {
11203                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11204                 if (size == 0) {
11205                     gen_helper_neon_addl_u16(tcg_res[pass],
11206                                              tcg_res[pass], tcg_op);
11207                 } else {
11208                     gen_helper_neon_addl_u32(tcg_res[pass],
11209                                              tcg_res[pass], tcg_op);
11210                 }
11211             }
11212         }
11213     }
11214     if (!is_q) {
11215         tcg_res[1] = tcg_constant_i64(0);
11216     }
11217     for (pass = 0; pass < 2; pass++) {
11218         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11219     }
11220 }
11221 
11222 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11223 {
11224     /* Implement SHLL and SHLL2 */
11225     int pass;
11226     int part = is_q ? 2 : 0;
11227     TCGv_i64 tcg_res[2];
11228 
11229     for (pass = 0; pass < 2; pass++) {
11230         static NeonGenWidenFn * const widenfns[3] = {
11231             gen_helper_neon_widen_u8,
11232             gen_helper_neon_widen_u16,
11233             tcg_gen_extu_i32_i64,
11234         };
11235         NeonGenWidenFn *widenfn = widenfns[size];
11236         TCGv_i32 tcg_op = tcg_temp_new_i32();
11237 
11238         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11239         tcg_res[pass] = tcg_temp_new_i64();
11240         widenfn(tcg_res[pass], tcg_op);
11241         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11242     }
11243 
11244     for (pass = 0; pass < 2; pass++) {
11245         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11246     }
11247 }
11248 
11249 /* AdvSIMD two reg misc
11250  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11251  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11252  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11253  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11254  */
11255 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11256 {
11257     int size = extract32(insn, 22, 2);
11258     int opcode = extract32(insn, 12, 5);
11259     bool u = extract32(insn, 29, 1);
11260     bool is_q = extract32(insn, 30, 1);
11261     int rn = extract32(insn, 5, 5);
11262     int rd = extract32(insn, 0, 5);
11263     bool need_fpstatus = false;
11264     int rmode = -1;
11265     TCGv_i32 tcg_rmode;
11266     TCGv_ptr tcg_fpstatus;
11267 
11268     switch (opcode) {
11269     case 0x0: /* REV64, REV32 */
11270     case 0x1: /* REV16 */
11271         handle_rev(s, opcode, u, is_q, size, rn, rd);
11272         return;
11273     case 0x5: /* CNT, NOT, RBIT */
11274         if (u && size == 0) {
11275             /* NOT */
11276             break;
11277         } else if (u && size == 1) {
11278             /* RBIT */
11279             break;
11280         } else if (!u && size == 0) {
11281             /* CNT */
11282             break;
11283         }
11284         unallocated_encoding(s);
11285         return;
11286     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11287     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11288         if (size == 3) {
11289             unallocated_encoding(s);
11290             return;
11291         }
11292         if (!fp_access_check(s)) {
11293             return;
11294         }
11295 
11296         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11297         return;
11298     case 0x4: /* CLS, CLZ */
11299         if (size == 3) {
11300             unallocated_encoding(s);
11301             return;
11302         }
11303         break;
11304     case 0x2: /* SADDLP, UADDLP */
11305     case 0x6: /* SADALP, UADALP */
11306         if (size == 3) {
11307             unallocated_encoding(s);
11308             return;
11309         }
11310         if (!fp_access_check(s)) {
11311             return;
11312         }
11313         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11314         return;
11315     case 0x13: /* SHLL, SHLL2 */
11316         if (u == 0 || size == 3) {
11317             unallocated_encoding(s);
11318             return;
11319         }
11320         if (!fp_access_check(s)) {
11321             return;
11322         }
11323         handle_shll(s, is_q, size, rn, rd);
11324         return;
11325     case 0xa: /* CMLT */
11326         if (u == 1) {
11327             unallocated_encoding(s);
11328             return;
11329         }
11330         /* fall through */
11331     case 0x8: /* CMGT, CMGE */
11332     case 0x9: /* CMEQ, CMLE */
11333     case 0xb: /* ABS, NEG */
11334         if (size == 3 && !is_q) {
11335             unallocated_encoding(s);
11336             return;
11337         }
11338         break;
11339     case 0x7: /* SQABS, SQNEG */
11340         if (size == 3 && !is_q) {
11341             unallocated_encoding(s);
11342             return;
11343         }
11344         break;
11345     case 0xc ... 0xf:
11346     case 0x16 ... 0x1f:
11347     {
11348         /* Floating point: U, size[1] and opcode indicate operation;
11349          * size[0] indicates single or double precision.
11350          */
11351         int is_double = extract32(size, 0, 1);
11352         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11353         size = is_double ? 3 : 2;
11354         switch (opcode) {
11355         case 0x2f: /* FABS */
11356         case 0x6f: /* FNEG */
11357             if (size == 3 && !is_q) {
11358                 unallocated_encoding(s);
11359                 return;
11360             }
11361             break;
11362         case 0x1d: /* SCVTF */
11363         case 0x5d: /* UCVTF */
11364         {
11365             bool is_signed = (opcode == 0x1d) ? true : false;
11366             int elements = is_double ? 2 : is_q ? 4 : 2;
11367             if (is_double && !is_q) {
11368                 unallocated_encoding(s);
11369                 return;
11370             }
11371             if (!fp_access_check(s)) {
11372                 return;
11373             }
11374             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11375             return;
11376         }
11377         case 0x2c: /* FCMGT (zero) */
11378         case 0x2d: /* FCMEQ (zero) */
11379         case 0x2e: /* FCMLT (zero) */
11380         case 0x6c: /* FCMGE (zero) */
11381         case 0x6d: /* FCMLE (zero) */
11382             if (size == 3 && !is_q) {
11383                 unallocated_encoding(s);
11384                 return;
11385             }
11386             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11387             return;
11388         case 0x7f: /* FSQRT */
11389             if (size == 3 && !is_q) {
11390                 unallocated_encoding(s);
11391                 return;
11392             }
11393             break;
11394         case 0x1a: /* FCVTNS */
11395         case 0x1b: /* FCVTMS */
11396         case 0x3a: /* FCVTPS */
11397         case 0x3b: /* FCVTZS */
11398         case 0x5a: /* FCVTNU */
11399         case 0x5b: /* FCVTMU */
11400         case 0x7a: /* FCVTPU */
11401         case 0x7b: /* FCVTZU */
11402             need_fpstatus = true;
11403             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11404             if (size == 3 && !is_q) {
11405                 unallocated_encoding(s);
11406                 return;
11407             }
11408             break;
11409         case 0x5c: /* FCVTAU */
11410         case 0x1c: /* FCVTAS */
11411             need_fpstatus = true;
11412             rmode = FPROUNDING_TIEAWAY;
11413             if (size == 3 && !is_q) {
11414                 unallocated_encoding(s);
11415                 return;
11416             }
11417             break;
11418         case 0x3c: /* URECPE */
11419             if (size == 3) {
11420                 unallocated_encoding(s);
11421                 return;
11422             }
11423             /* fall through */
11424         case 0x3d: /* FRECPE */
11425         case 0x7d: /* FRSQRTE */
11426             if (size == 3 && !is_q) {
11427                 unallocated_encoding(s);
11428                 return;
11429             }
11430             if (!fp_access_check(s)) {
11431                 return;
11432             }
11433             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
11434             return;
11435         case 0x56: /* FCVTXN, FCVTXN2 */
11436             if (size == 2) {
11437                 unallocated_encoding(s);
11438                 return;
11439             }
11440             /* fall through */
11441         case 0x16: /* FCVTN, FCVTN2 */
11442             /* handle_2misc_narrow does a 2*size -> size operation, but these
11443              * instructions encode the source size rather than dest size.
11444              */
11445             if (!fp_access_check(s)) {
11446                 return;
11447             }
11448             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11449             return;
11450         case 0x36: /* BFCVTN, BFCVTN2 */
11451             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
11452                 unallocated_encoding(s);
11453                 return;
11454             }
11455             if (!fp_access_check(s)) {
11456                 return;
11457             }
11458             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11459             return;
11460         case 0x17: /* FCVTL, FCVTL2 */
11461             if (!fp_access_check(s)) {
11462                 return;
11463             }
11464             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
11465             return;
11466         case 0x18: /* FRINTN */
11467         case 0x19: /* FRINTM */
11468         case 0x38: /* FRINTP */
11469         case 0x39: /* FRINTZ */
11470             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11471             /* fall through */
11472         case 0x59: /* FRINTX */
11473         case 0x79: /* FRINTI */
11474             need_fpstatus = true;
11475             if (size == 3 && !is_q) {
11476                 unallocated_encoding(s);
11477                 return;
11478             }
11479             break;
11480         case 0x58: /* FRINTA */
11481             rmode = FPROUNDING_TIEAWAY;
11482             need_fpstatus = true;
11483             if (size == 3 && !is_q) {
11484                 unallocated_encoding(s);
11485                 return;
11486             }
11487             break;
11488         case 0x7c: /* URSQRTE */
11489             if (size == 3) {
11490                 unallocated_encoding(s);
11491                 return;
11492             }
11493             break;
11494         case 0x1e: /* FRINT32Z */
11495         case 0x1f: /* FRINT64Z */
11496             rmode = FPROUNDING_ZERO;
11497             /* fall through */
11498         case 0x5e: /* FRINT32X */
11499         case 0x5f: /* FRINT64X */
11500             need_fpstatus = true;
11501             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
11502                 unallocated_encoding(s);
11503                 return;
11504             }
11505             break;
11506         default:
11507             unallocated_encoding(s);
11508             return;
11509         }
11510         break;
11511     }
11512     default:
11513     case 0x3: /* SUQADD, USQADD */
11514         unallocated_encoding(s);
11515         return;
11516     }
11517 
11518     if (!fp_access_check(s)) {
11519         return;
11520     }
11521 
11522     if (need_fpstatus || rmode >= 0) {
11523         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
11524     } else {
11525         tcg_fpstatus = NULL;
11526     }
11527     if (rmode >= 0) {
11528         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
11529     } else {
11530         tcg_rmode = NULL;
11531     }
11532 
11533     switch (opcode) {
11534     case 0x5:
11535         if (u && size == 0) { /* NOT */
11536             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
11537             return;
11538         }
11539         break;
11540     case 0x8: /* CMGT, CMGE */
11541         if (u) {
11542             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
11543         } else {
11544             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
11545         }
11546         return;
11547     case 0x9: /* CMEQ, CMLE */
11548         if (u) {
11549             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
11550         } else {
11551             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
11552         }
11553         return;
11554     case 0xa: /* CMLT */
11555         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
11556         return;
11557     case 0xb:
11558         if (u) { /* ABS, NEG */
11559             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
11560         } else {
11561             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
11562         }
11563         return;
11564     }
11565 
11566     if (size == 3) {
11567         /* All 64-bit element operations can be shared with scalar 2misc */
11568         int pass;
11569 
11570         /* Coverity claims (size == 3 && !is_q) has been eliminated
11571          * from all paths leading to here.
11572          */
11573         tcg_debug_assert(is_q);
11574         for (pass = 0; pass < 2; pass++) {
11575             TCGv_i64 tcg_op = tcg_temp_new_i64();
11576             TCGv_i64 tcg_res = tcg_temp_new_i64();
11577 
11578             read_vec_element(s, tcg_op, rn, pass, MO_64);
11579 
11580             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
11581                             tcg_rmode, tcg_fpstatus);
11582 
11583             write_vec_element(s, tcg_res, rd, pass, MO_64);
11584         }
11585     } else {
11586         int pass;
11587 
11588         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11589             TCGv_i32 tcg_op = tcg_temp_new_i32();
11590             TCGv_i32 tcg_res = tcg_temp_new_i32();
11591 
11592             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
11593 
11594             if (size == 2) {
11595                 /* Special cases for 32 bit elements */
11596                 switch (opcode) {
11597                 case 0x4: /* CLS */
11598                     if (u) {
11599                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
11600                     } else {
11601                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
11602                     }
11603                     break;
11604                 case 0x7: /* SQABS, SQNEG */
11605                     if (u) {
11606                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
11607                     } else {
11608                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
11609                     }
11610                     break;
11611                 case 0x2f: /* FABS */
11612                     gen_vfp_abss(tcg_res, tcg_op);
11613                     break;
11614                 case 0x6f: /* FNEG */
11615                     gen_vfp_negs(tcg_res, tcg_op);
11616                     break;
11617                 case 0x7f: /* FSQRT */
11618                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
11619                     break;
11620                 case 0x1a: /* FCVTNS */
11621                 case 0x1b: /* FCVTMS */
11622                 case 0x1c: /* FCVTAS */
11623                 case 0x3a: /* FCVTPS */
11624                 case 0x3b: /* FCVTZS */
11625                     gen_helper_vfp_tosls(tcg_res, tcg_op,
11626                                          tcg_constant_i32(0), tcg_fpstatus);
11627                     break;
11628                 case 0x5a: /* FCVTNU */
11629                 case 0x5b: /* FCVTMU */
11630                 case 0x5c: /* FCVTAU */
11631                 case 0x7a: /* FCVTPU */
11632                 case 0x7b: /* FCVTZU */
11633                     gen_helper_vfp_touls(tcg_res, tcg_op,
11634                                          tcg_constant_i32(0), tcg_fpstatus);
11635                     break;
11636                 case 0x18: /* FRINTN */
11637                 case 0x19: /* FRINTM */
11638                 case 0x38: /* FRINTP */
11639                 case 0x39: /* FRINTZ */
11640                 case 0x58: /* FRINTA */
11641                 case 0x79: /* FRINTI */
11642                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
11643                     break;
11644                 case 0x59: /* FRINTX */
11645                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
11646                     break;
11647                 case 0x7c: /* URSQRTE */
11648                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
11649                     break;
11650                 case 0x1e: /* FRINT32Z */
11651                 case 0x5e: /* FRINT32X */
11652                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
11653                     break;
11654                 case 0x1f: /* FRINT64Z */
11655                 case 0x5f: /* FRINT64X */
11656                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
11657                     break;
11658                 default:
11659                     g_assert_not_reached();
11660                 }
11661             } else {
11662                 /* Use helpers for 8 and 16 bit elements */
11663                 switch (opcode) {
11664                 case 0x5: /* CNT, RBIT */
11665                     /* For these two insns size is part of the opcode specifier
11666                      * (handled earlier); they always operate on byte elements.
11667                      */
11668                     if (u) {
11669                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
11670                     } else {
11671                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
11672                     }
11673                     break;
11674                 case 0x7: /* SQABS, SQNEG */
11675                 {
11676                     NeonGenOneOpEnvFn *genfn;
11677                     static NeonGenOneOpEnvFn * const fns[2][2] = {
11678                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
11679                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
11680                     };
11681                     genfn = fns[size][u];
11682                     genfn(tcg_res, tcg_env, tcg_op);
11683                     break;
11684                 }
11685                 case 0x4: /* CLS, CLZ */
11686                     if (u) {
11687                         if (size == 0) {
11688                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
11689                         } else {
11690                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
11691                         }
11692                     } else {
11693                         if (size == 0) {
11694                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
11695                         } else {
11696                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
11697                         }
11698                     }
11699                     break;
11700                 default:
11701                     g_assert_not_reached();
11702                 }
11703             }
11704 
11705             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11706         }
11707     }
11708     clear_vec_high(s, is_q, rd);
11709 
11710     if (tcg_rmode) {
11711         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
11712     }
11713 }
11714 
11715 /* AdvSIMD [scalar] two register miscellaneous (FP16)
11716  *
11717  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
11718  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11719  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11720  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11721  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
11722  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
11723  *
11724  * This actually covers two groups where scalar access is governed by
11725  * bit 28. A bunch of the instructions (float to integral) only exist
11726  * in the vector form and are un-allocated for the scalar decode. Also
11727  * in the scalar decode Q is always 1.
11728  */
11729 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
11730 {
11731     int fpop, opcode, a, u;
11732     int rn, rd;
11733     bool is_q;
11734     bool is_scalar;
11735     bool only_in_vector = false;
11736 
11737     int pass;
11738     TCGv_i32 tcg_rmode = NULL;
11739     TCGv_ptr tcg_fpstatus = NULL;
11740     bool need_fpst = true;
11741     int rmode = -1;
11742 
11743     if (!dc_isar_feature(aa64_fp16, s)) {
11744         unallocated_encoding(s);
11745         return;
11746     }
11747 
11748     rd = extract32(insn, 0, 5);
11749     rn = extract32(insn, 5, 5);
11750 
11751     a = extract32(insn, 23, 1);
11752     u = extract32(insn, 29, 1);
11753     is_scalar = extract32(insn, 28, 1);
11754     is_q = extract32(insn, 30, 1);
11755 
11756     opcode = extract32(insn, 12, 5);
11757     fpop = deposit32(opcode, 5, 1, a);
11758     fpop = deposit32(fpop, 6, 1, u);
11759 
11760     switch (fpop) {
11761     case 0x1d: /* SCVTF */
11762     case 0x5d: /* UCVTF */
11763     {
11764         int elements;
11765 
11766         if (is_scalar) {
11767             elements = 1;
11768         } else {
11769             elements = (is_q ? 8 : 4);
11770         }
11771 
11772         if (!fp_access_check(s)) {
11773             return;
11774         }
11775         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
11776         return;
11777     }
11778     break;
11779     case 0x2c: /* FCMGT (zero) */
11780     case 0x2d: /* FCMEQ (zero) */
11781     case 0x2e: /* FCMLT (zero) */
11782     case 0x6c: /* FCMGE (zero) */
11783     case 0x6d: /* FCMLE (zero) */
11784         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
11785         return;
11786     case 0x3d: /* FRECPE */
11787     case 0x3f: /* FRECPX */
11788         break;
11789     case 0x18: /* FRINTN */
11790         only_in_vector = true;
11791         rmode = FPROUNDING_TIEEVEN;
11792         break;
11793     case 0x19: /* FRINTM */
11794         only_in_vector = true;
11795         rmode = FPROUNDING_NEGINF;
11796         break;
11797     case 0x38: /* FRINTP */
11798         only_in_vector = true;
11799         rmode = FPROUNDING_POSINF;
11800         break;
11801     case 0x39: /* FRINTZ */
11802         only_in_vector = true;
11803         rmode = FPROUNDING_ZERO;
11804         break;
11805     case 0x58: /* FRINTA */
11806         only_in_vector = true;
11807         rmode = FPROUNDING_TIEAWAY;
11808         break;
11809     case 0x59: /* FRINTX */
11810     case 0x79: /* FRINTI */
11811         only_in_vector = true;
11812         /* current rounding mode */
11813         break;
11814     case 0x1a: /* FCVTNS */
11815         rmode = FPROUNDING_TIEEVEN;
11816         break;
11817     case 0x1b: /* FCVTMS */
11818         rmode = FPROUNDING_NEGINF;
11819         break;
11820     case 0x1c: /* FCVTAS */
11821         rmode = FPROUNDING_TIEAWAY;
11822         break;
11823     case 0x3a: /* FCVTPS */
11824         rmode = FPROUNDING_POSINF;
11825         break;
11826     case 0x3b: /* FCVTZS */
11827         rmode = FPROUNDING_ZERO;
11828         break;
11829     case 0x5a: /* FCVTNU */
11830         rmode = FPROUNDING_TIEEVEN;
11831         break;
11832     case 0x5b: /* FCVTMU */
11833         rmode = FPROUNDING_NEGINF;
11834         break;
11835     case 0x5c: /* FCVTAU */
11836         rmode = FPROUNDING_TIEAWAY;
11837         break;
11838     case 0x7a: /* FCVTPU */
11839         rmode = FPROUNDING_POSINF;
11840         break;
11841     case 0x7b: /* FCVTZU */
11842         rmode = FPROUNDING_ZERO;
11843         break;
11844     case 0x2f: /* FABS */
11845     case 0x6f: /* FNEG */
11846         need_fpst = false;
11847         break;
11848     case 0x7d: /* FRSQRTE */
11849     case 0x7f: /* FSQRT (vector) */
11850         break;
11851     default:
11852         unallocated_encoding(s);
11853         return;
11854     }
11855 
11856 
11857     /* Check additional constraints for the scalar encoding */
11858     if (is_scalar) {
11859         if (!is_q) {
11860             unallocated_encoding(s);
11861             return;
11862         }
11863         /* FRINTxx is only in the vector form */
11864         if (only_in_vector) {
11865             unallocated_encoding(s);
11866             return;
11867         }
11868     }
11869 
11870     if (!fp_access_check(s)) {
11871         return;
11872     }
11873 
11874     if (rmode >= 0 || need_fpst) {
11875         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
11876     }
11877 
11878     if (rmode >= 0) {
11879         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
11880     }
11881 
11882     if (is_scalar) {
11883         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
11884         TCGv_i32 tcg_res = tcg_temp_new_i32();
11885 
11886         switch (fpop) {
11887         case 0x1a: /* FCVTNS */
11888         case 0x1b: /* FCVTMS */
11889         case 0x1c: /* FCVTAS */
11890         case 0x3a: /* FCVTPS */
11891         case 0x3b: /* FCVTZS */
11892             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11893             break;
11894         case 0x3d: /* FRECPE */
11895             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11896             break;
11897         case 0x3f: /* FRECPX */
11898             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
11899             break;
11900         case 0x5a: /* FCVTNU */
11901         case 0x5b: /* FCVTMU */
11902         case 0x5c: /* FCVTAU */
11903         case 0x7a: /* FCVTPU */
11904         case 0x7b: /* FCVTZU */
11905             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11906             break;
11907         case 0x6f: /* FNEG */
11908             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11909             break;
11910         case 0x7d: /* FRSQRTE */
11911             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11912             break;
11913         default:
11914             g_assert_not_reached();
11915         }
11916 
11917         /* limit any sign extension going on */
11918         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
11919         write_fp_sreg(s, rd, tcg_res);
11920     } else {
11921         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
11922             TCGv_i32 tcg_op = tcg_temp_new_i32();
11923             TCGv_i32 tcg_res = tcg_temp_new_i32();
11924 
11925             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
11926 
11927             switch (fpop) {
11928             case 0x1a: /* FCVTNS */
11929             case 0x1b: /* FCVTMS */
11930             case 0x1c: /* FCVTAS */
11931             case 0x3a: /* FCVTPS */
11932             case 0x3b: /* FCVTZS */
11933                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11934                 break;
11935             case 0x3d: /* FRECPE */
11936                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11937                 break;
11938             case 0x5a: /* FCVTNU */
11939             case 0x5b: /* FCVTMU */
11940             case 0x5c: /* FCVTAU */
11941             case 0x7a: /* FCVTPU */
11942             case 0x7b: /* FCVTZU */
11943                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11944                 break;
11945             case 0x18: /* FRINTN */
11946             case 0x19: /* FRINTM */
11947             case 0x38: /* FRINTP */
11948             case 0x39: /* FRINTZ */
11949             case 0x58: /* FRINTA */
11950             case 0x79: /* FRINTI */
11951                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
11952                 break;
11953             case 0x59: /* FRINTX */
11954                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
11955                 break;
11956             case 0x2f: /* FABS */
11957                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
11958                 break;
11959             case 0x6f: /* FNEG */
11960                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11961                 break;
11962             case 0x7d: /* FRSQRTE */
11963                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11964                 break;
11965             case 0x7f: /* FSQRT */
11966                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
11967                 break;
11968             default:
11969                 g_assert_not_reached();
11970             }
11971 
11972             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11973         }
11974 
11975         clear_vec_high(s, is_q, rd);
11976     }
11977 
11978     if (tcg_rmode) {
11979         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
11980     }
11981 }
11982 
11983 /* AdvSIMD scalar x indexed element
11984  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
11985  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11986  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
11987  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11988  * AdvSIMD vector x indexed element
11989  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
11990  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11991  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
11992  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11993  */
11994 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
11995 {
11996     /* This encoding has two kinds of instruction:
11997      *  normal, where we perform elt x idxelt => elt for each
11998      *     element in the vector
11999      *  long, where we perform elt x idxelt and generate a result of
12000      *     double the width of the input element
12001      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12002      */
12003     bool is_scalar = extract32(insn, 28, 1);
12004     bool is_q = extract32(insn, 30, 1);
12005     bool u = extract32(insn, 29, 1);
12006     int size = extract32(insn, 22, 2);
12007     int l = extract32(insn, 21, 1);
12008     int m = extract32(insn, 20, 1);
12009     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12010     int rm = extract32(insn, 16, 4);
12011     int opcode = extract32(insn, 12, 4);
12012     int h = extract32(insn, 11, 1);
12013     int rn = extract32(insn, 5, 5);
12014     int rd = extract32(insn, 0, 5);
12015     bool is_long = false;
12016     int is_fp = 0;
12017     bool is_fp16 = false;
12018     int index;
12019     TCGv_ptr fpst;
12020 
12021     switch (16 * u + opcode) {
12022     case 0x02: /* SMLAL, SMLAL2 */
12023     case 0x12: /* UMLAL, UMLAL2 */
12024     case 0x06: /* SMLSL, SMLSL2 */
12025     case 0x16: /* UMLSL, UMLSL2 */
12026     case 0x0a: /* SMULL, SMULL2 */
12027     case 0x1a: /* UMULL, UMULL2 */
12028         if (is_scalar) {
12029             unallocated_encoding(s);
12030             return;
12031         }
12032         is_long = true;
12033         break;
12034     case 0x03: /* SQDMLAL, SQDMLAL2 */
12035     case 0x07: /* SQDMLSL, SQDMLSL2 */
12036     case 0x0b: /* SQDMULL, SQDMULL2 */
12037         is_long = true;
12038         break;
12039     case 0x1d: /* SQRDMLAH */
12040     case 0x1f: /* SQRDMLSH */
12041         if (!dc_isar_feature(aa64_rdm, s)) {
12042             unallocated_encoding(s);
12043             return;
12044         }
12045         break;
12046     case 0x0e: /* SDOT */
12047     case 0x1e: /* UDOT */
12048         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12049             unallocated_encoding(s);
12050             return;
12051         }
12052         break;
12053     case 0x0f:
12054         switch (size) {
12055         case 0: /* SUDOT */
12056         case 2: /* USDOT */
12057             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12058                 unallocated_encoding(s);
12059                 return;
12060             }
12061             size = MO_32;
12062             break;
12063         case 1: /* BFDOT */
12064             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12065                 unallocated_encoding(s);
12066                 return;
12067             }
12068             size = MO_32;
12069             break;
12070         case 3: /* BFMLAL{B,T} */
12071             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12072                 unallocated_encoding(s);
12073                 return;
12074             }
12075             /* can't set is_fp without other incorrect size checks */
12076             size = MO_16;
12077             break;
12078         default:
12079             unallocated_encoding(s);
12080             return;
12081         }
12082         break;
12083     case 0x11: /* FCMLA #0 */
12084     case 0x13: /* FCMLA #90 */
12085     case 0x15: /* FCMLA #180 */
12086     case 0x17: /* FCMLA #270 */
12087         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12088             unallocated_encoding(s);
12089             return;
12090         }
12091         is_fp = 2;
12092         break;
12093     default:
12094     case 0x00: /* FMLAL */
12095     case 0x01: /* FMLA */
12096     case 0x04: /* FMLSL */
12097     case 0x05: /* FMLS */
12098     case 0x08: /* MUL */
12099     case 0x09: /* FMUL */
12100     case 0x0c: /* SQDMULH */
12101     case 0x0d: /* SQRDMULH */
12102     case 0x10: /* MLA */
12103     case 0x14: /* MLS */
12104     case 0x18: /* FMLAL2 */
12105     case 0x19: /* FMULX */
12106     case 0x1c: /* FMLSL2 */
12107         unallocated_encoding(s);
12108         return;
12109     }
12110 
12111     switch (is_fp) {
12112     case 1: /* normal fp */
12113         unallocated_encoding(s); /* in decodetree */
12114         return;
12115 
12116     case 2: /* complex fp */
12117         /* Each indexable element is a complex pair.  */
12118         size += 1;
12119         switch (size) {
12120         case MO_32:
12121             if (h && !is_q) {
12122                 unallocated_encoding(s);
12123                 return;
12124             }
12125             is_fp16 = true;
12126             break;
12127         case MO_64:
12128             break;
12129         default:
12130             unallocated_encoding(s);
12131             return;
12132         }
12133         break;
12134 
12135     default: /* integer */
12136         switch (size) {
12137         case MO_8:
12138         case MO_64:
12139             unallocated_encoding(s);
12140             return;
12141         }
12142         break;
12143     }
12144     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12145         unallocated_encoding(s);
12146         return;
12147     }
12148 
12149     /* Given MemOp size, adjust register and indexing.  */
12150     switch (size) {
12151     case MO_16:
12152         index = h << 2 | l << 1 | m;
12153         break;
12154     case MO_32:
12155         index = h << 1 | l;
12156         rm |= m << 4;
12157         break;
12158     case MO_64:
12159         if (l || !is_q) {
12160             unallocated_encoding(s);
12161             return;
12162         }
12163         index = h;
12164         rm |= m << 4;
12165         break;
12166     default:
12167         g_assert_not_reached();
12168     }
12169 
12170     if (!fp_access_check(s)) {
12171         return;
12172     }
12173 
12174     if (is_fp) {
12175         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12176     } else {
12177         fpst = NULL;
12178     }
12179 
12180     switch (16 * u + opcode) {
12181     case 0x0e: /* SDOT */
12182     case 0x1e: /* UDOT */
12183         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12184                          u ? gen_helper_gvec_udot_idx_b
12185                          : gen_helper_gvec_sdot_idx_b);
12186         return;
12187     case 0x0f:
12188         switch (extract32(insn, 22, 2)) {
12189         case 0: /* SUDOT */
12190             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12191                              gen_helper_gvec_sudot_idx_b);
12192             return;
12193         case 1: /* BFDOT */
12194             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12195                              gen_helper_gvec_bfdot_idx);
12196             return;
12197         case 2: /* USDOT */
12198             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12199                              gen_helper_gvec_usdot_idx_b);
12200             return;
12201         case 3: /* BFMLAL{B,T} */
12202             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12203                               gen_helper_gvec_bfmlal_idx);
12204             return;
12205         }
12206         g_assert_not_reached();
12207     case 0x11: /* FCMLA #0 */
12208     case 0x13: /* FCMLA #90 */
12209     case 0x15: /* FCMLA #180 */
12210     case 0x17: /* FCMLA #270 */
12211         {
12212             int rot = extract32(insn, 13, 2);
12213             int data = (index << 2) | rot;
12214             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12215                                vec_full_reg_offset(s, rn),
12216                                vec_full_reg_offset(s, rm),
12217                                vec_full_reg_offset(s, rd), fpst,
12218                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12219                                size == MO_64
12220                                ? gen_helper_gvec_fcmlas_idx
12221                                : gen_helper_gvec_fcmlah_idx);
12222         }
12223         return;
12224     }
12225 
12226     if (size == 3) {
12227         g_assert_not_reached();
12228     } else if (!is_long) {
12229         /* 32 bit floating point, or 16 or 32 bit integer.
12230          * For the 16 bit scalar case we use the usual Neon helpers and
12231          * rely on the fact that 0 op 0 == 0 with no side effects.
12232          */
12233         TCGv_i32 tcg_idx = tcg_temp_new_i32();
12234         int pass, maxpasses;
12235 
12236         if (is_scalar) {
12237             maxpasses = 1;
12238         } else {
12239             maxpasses = is_q ? 4 : 2;
12240         }
12241 
12242         read_vec_element_i32(s, tcg_idx, rm, index, size);
12243 
12244         if (size == 1 && !is_scalar) {
12245             /* The simplest way to handle the 16x16 indexed ops is to duplicate
12246              * the index into both halves of the 32 bit tcg_idx and then use
12247              * the usual Neon helpers.
12248              */
12249             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12250         }
12251 
12252         for (pass = 0; pass < maxpasses; pass++) {
12253             TCGv_i32 tcg_op = tcg_temp_new_i32();
12254             TCGv_i32 tcg_res = tcg_temp_new_i32();
12255 
12256             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
12257 
12258             switch (16 * u + opcode) {
12259             case 0x10: /* MLA */
12260             case 0x14: /* MLS */
12261             {
12262                 static NeonGenTwoOpFn * const fns[2][2] = {
12263                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
12264                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
12265                 };
12266                 NeonGenTwoOpFn *genfn;
12267                 bool is_sub = opcode == 0x4;
12268 
12269                 if (size == 1) {
12270                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
12271                 } else {
12272                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
12273                 }
12274                 if (opcode == 0x8) {
12275                     break;
12276                 }
12277                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
12278                 genfn = fns[size - 1][is_sub];
12279                 genfn(tcg_res, tcg_op, tcg_res);
12280                 break;
12281             }
12282             case 0x0c: /* SQDMULH */
12283                 if (size == 1) {
12284                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
12285                                                tcg_op, tcg_idx);
12286                 } else {
12287                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
12288                                                tcg_op, tcg_idx);
12289                 }
12290                 break;
12291             case 0x0d: /* SQRDMULH */
12292                 if (size == 1) {
12293                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
12294                                                 tcg_op, tcg_idx);
12295                 } else {
12296                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
12297                                                 tcg_op, tcg_idx);
12298                 }
12299                 break;
12300             case 0x1d: /* SQRDMLAH */
12301                 read_vec_element_i32(s, tcg_res, rd, pass,
12302                                      is_scalar ? size : MO_32);
12303                 if (size == 1) {
12304                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
12305                                                 tcg_op, tcg_idx, tcg_res);
12306                 } else {
12307                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
12308                                                 tcg_op, tcg_idx, tcg_res);
12309                 }
12310                 break;
12311             case 0x1f: /* SQRDMLSH */
12312                 read_vec_element_i32(s, tcg_res, rd, pass,
12313                                      is_scalar ? size : MO_32);
12314                 if (size == 1) {
12315                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
12316                                                 tcg_op, tcg_idx, tcg_res);
12317                 } else {
12318                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
12319                                                 tcg_op, tcg_idx, tcg_res);
12320                 }
12321                 break;
12322             default:
12323             case 0x01: /* FMLA */
12324             case 0x05: /* FMLS */
12325             case 0x09: /* FMUL */
12326             case 0x19: /* FMULX */
12327                 g_assert_not_reached();
12328             }
12329 
12330             if (is_scalar) {
12331                 write_fp_sreg(s, rd, tcg_res);
12332             } else {
12333                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12334             }
12335         }
12336 
12337         clear_vec_high(s, is_q, rd);
12338     } else {
12339         /* long ops: 16x16->32 or 32x32->64 */
12340         TCGv_i64 tcg_res[2];
12341         int pass;
12342         bool satop = extract32(opcode, 0, 1);
12343         MemOp memop = MO_32;
12344 
12345         if (satop || !u) {
12346             memop |= MO_SIGN;
12347         }
12348 
12349         if (size == 2) {
12350             TCGv_i64 tcg_idx = tcg_temp_new_i64();
12351 
12352             read_vec_element(s, tcg_idx, rm, index, memop);
12353 
12354             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12355                 TCGv_i64 tcg_op = tcg_temp_new_i64();
12356                 TCGv_i64 tcg_passres;
12357                 int passelt;
12358 
12359                 if (is_scalar) {
12360                     passelt = 0;
12361                 } else {
12362                     passelt = pass + (is_q * 2);
12363                 }
12364 
12365                 read_vec_element(s, tcg_op, rn, passelt, memop);
12366 
12367                 tcg_res[pass] = tcg_temp_new_i64();
12368 
12369                 if (opcode == 0xa || opcode == 0xb) {
12370                     /* Non-accumulating ops */
12371                     tcg_passres = tcg_res[pass];
12372                 } else {
12373                     tcg_passres = tcg_temp_new_i64();
12374                 }
12375 
12376                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
12377 
12378                 if (satop) {
12379                     /* saturating, doubling */
12380                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
12381                                                       tcg_passres, tcg_passres);
12382                 }
12383 
12384                 if (opcode == 0xa || opcode == 0xb) {
12385                     continue;
12386                 }
12387 
12388                 /* Accumulating op: handle accumulate step */
12389                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12390 
12391                 switch (opcode) {
12392                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12393                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12394                     break;
12395                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12396                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12397                     break;
12398                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12399                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
12400                     /* fall through */
12401                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12402                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
12403                                                       tcg_res[pass],
12404                                                       tcg_passres);
12405                     break;
12406                 default:
12407                     g_assert_not_reached();
12408                 }
12409             }
12410 
12411             clear_vec_high(s, !is_scalar, rd);
12412         } else {
12413             TCGv_i32 tcg_idx = tcg_temp_new_i32();
12414 
12415             assert(size == 1);
12416             read_vec_element_i32(s, tcg_idx, rm, index, size);
12417 
12418             if (!is_scalar) {
12419                 /* The simplest way to handle the 16x16 indexed ops is to
12420                  * duplicate the index into both halves of the 32 bit tcg_idx
12421                  * and then use the usual Neon helpers.
12422                  */
12423                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12424             }
12425 
12426             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12427                 TCGv_i32 tcg_op = tcg_temp_new_i32();
12428                 TCGv_i64 tcg_passres;
12429 
12430                 if (is_scalar) {
12431                     read_vec_element_i32(s, tcg_op, rn, pass, size);
12432                 } else {
12433                     read_vec_element_i32(s, tcg_op, rn,
12434                                          pass + (is_q * 2), MO_32);
12435                 }
12436 
12437                 tcg_res[pass] = tcg_temp_new_i64();
12438 
12439                 if (opcode == 0xa || opcode == 0xb) {
12440                     /* Non-accumulating ops */
12441                     tcg_passres = tcg_res[pass];
12442                 } else {
12443                     tcg_passres = tcg_temp_new_i64();
12444                 }
12445 
12446                 if (memop & MO_SIGN) {
12447                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
12448                 } else {
12449                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
12450                 }
12451                 if (satop) {
12452                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
12453                                                       tcg_passres, tcg_passres);
12454                 }
12455 
12456                 if (opcode == 0xa || opcode == 0xb) {
12457                     continue;
12458                 }
12459 
12460                 /* Accumulating op: handle accumulate step */
12461                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12462 
12463                 switch (opcode) {
12464                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12465                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
12466                                              tcg_passres);
12467                     break;
12468                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12469                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
12470                                              tcg_passres);
12471                     break;
12472                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12473                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
12474                     /* fall through */
12475                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12476                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
12477                                                       tcg_res[pass],
12478                                                       tcg_passres);
12479                     break;
12480                 default:
12481                     g_assert_not_reached();
12482                 }
12483             }
12484 
12485             if (is_scalar) {
12486                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
12487             }
12488         }
12489 
12490         if (is_scalar) {
12491             tcg_res[1] = tcg_constant_i64(0);
12492         }
12493 
12494         for (pass = 0; pass < 2; pass++) {
12495             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12496         }
12497     }
12498 }
12499 
12500 /* C3.6 Data processing - SIMD, inc Crypto
12501  *
12502  * As the decode gets a little complex we are using a table based
12503  * approach for this part of the decode.
12504  */
12505 static const AArch64DecodeTable data_proc_simd[] = {
12506     /* pattern  ,  mask     ,  fn                        */
12507     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
12508     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
12509     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
12510     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
12511     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
12512     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
12513     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
12514     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
12515     { 0x0e000000, 0xbf208c00, disas_simd_tb },
12516     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
12517     { 0x2e000000, 0xbf208400, disas_simd_ext },
12518     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
12519     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
12520     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
12521     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
12522     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
12523     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
12524     { 0x00000000, 0x00000000, NULL }
12525 };
12526 
12527 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
12528 {
12529     /* Note that this is called with all non-FP cases from
12530      * table C3-6 so it must UNDEF for entries not specifically
12531      * allocated to instructions in that table.
12532      */
12533     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
12534     if (fn) {
12535         fn(s, insn);
12536     } else {
12537         unallocated_encoding(s);
12538     }
12539 }
12540 
12541 /* C3.6 Data processing - SIMD and floating point */
12542 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
12543 {
12544     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
12545         disas_data_proc_fp(s, insn);
12546     } else {
12547         /* SIMD, including crypto */
12548         disas_data_proc_simd(s, insn);
12549     }
12550 }
12551 
12552 static bool trans_OK(DisasContext *s, arg_OK *a)
12553 {
12554     return true;
12555 }
12556 
12557 static bool trans_FAIL(DisasContext *s, arg_OK *a)
12558 {
12559     s->is_nonstreaming = true;
12560     return true;
12561 }
12562 
12563 /**
12564  * is_guarded_page:
12565  * @env: The cpu environment
12566  * @s: The DisasContext
12567  *
12568  * Return true if the page is guarded.
12569  */
12570 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
12571 {
12572     uint64_t addr = s->base.pc_first;
12573 #ifdef CONFIG_USER_ONLY
12574     return page_get_flags(addr) & PAGE_BTI;
12575 #else
12576     CPUTLBEntryFull *full;
12577     void *host;
12578     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
12579     int flags;
12580 
12581     /*
12582      * We test this immediately after reading an insn, which means
12583      * that the TLB entry must be present and valid, and thus this
12584      * access will never raise an exception.
12585      */
12586     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
12587                               false, &host, &full, 0);
12588     assert(!(flags & TLB_INVALID_MASK));
12589 
12590     return full->extra.arm.guarded;
12591 #endif
12592 }
12593 
12594 /**
12595  * btype_destination_ok:
12596  * @insn: The instruction at the branch destination
12597  * @bt: SCTLR_ELx.BT
12598  * @btype: PSTATE.BTYPE, and is non-zero
12599  *
12600  * On a guarded page, there are a limited number of insns
12601  * that may be present at the branch target:
12602  *   - branch target identifiers,
12603  *   - paciasp, pacibsp,
12604  *   - BRK insn
12605  *   - HLT insn
12606  * Anything else causes a Branch Target Exception.
12607  *
12608  * Return true if the branch is compatible, false to raise BTITRAP.
12609  */
12610 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
12611 {
12612     if ((insn & 0xfffff01fu) == 0xd503201fu) {
12613         /* HINT space */
12614         switch (extract32(insn, 5, 7)) {
12615         case 0b011001: /* PACIASP */
12616         case 0b011011: /* PACIBSP */
12617             /*
12618              * If SCTLR_ELx.BT, then PACI*SP are not compatible
12619              * with btype == 3.  Otherwise all btype are ok.
12620              */
12621             return !bt || btype != 3;
12622         case 0b100000: /* BTI */
12623             /* Not compatible with any btype.  */
12624             return false;
12625         case 0b100010: /* BTI c */
12626             /* Not compatible with btype == 3 */
12627             return btype != 3;
12628         case 0b100100: /* BTI j */
12629             /* Not compatible with btype == 2 */
12630             return btype != 2;
12631         case 0b100110: /* BTI jc */
12632             /* Compatible with any btype.  */
12633             return true;
12634         }
12635     } else {
12636         switch (insn & 0xffe0001fu) {
12637         case 0xd4200000u: /* BRK */
12638         case 0xd4400000u: /* HLT */
12639             /* Give priority to the breakpoint exception.  */
12640             return true;
12641         }
12642     }
12643     return false;
12644 }
12645 
12646 /* C3.1 A64 instruction index by encoding */
12647 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
12648 {
12649     switch (extract32(insn, 25, 4)) {
12650     case 0x5:
12651     case 0xd:      /* Data processing - register */
12652         disas_data_proc_reg(s, insn);
12653         break;
12654     case 0x7:
12655     case 0xf:      /* Data processing - SIMD and floating point */
12656         disas_data_proc_simd_fp(s, insn);
12657         break;
12658     default:
12659         unallocated_encoding(s);
12660         break;
12661     }
12662 }
12663 
12664 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
12665                                           CPUState *cpu)
12666 {
12667     DisasContext *dc = container_of(dcbase, DisasContext, base);
12668     CPUARMState *env = cpu_env(cpu);
12669     ARMCPU *arm_cpu = env_archcpu(env);
12670     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
12671     int bound, core_mmu_idx;
12672 
12673     dc->isar = &arm_cpu->isar;
12674     dc->condjmp = 0;
12675     dc->pc_save = dc->base.pc_first;
12676     dc->aarch64 = true;
12677     dc->thumb = false;
12678     dc->sctlr_b = 0;
12679     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
12680     dc->condexec_mask = 0;
12681     dc->condexec_cond = 0;
12682     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
12683     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
12684     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
12685     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
12686     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
12687     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
12688 #if !defined(CONFIG_USER_ONLY)
12689     dc->user = (dc->current_el == 0);
12690 #endif
12691     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
12692     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
12693     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
12694     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
12695     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
12696     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
12697     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
12698     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
12699     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
12700     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
12701     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
12702     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
12703     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
12704     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
12705     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
12706     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
12707     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
12708     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
12709     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
12710     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
12711     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
12712     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
12713     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
12714     dc->nv1 = EX_TBFLAG_A64(tb_flags, NV1);
12715     dc->nv2 = EX_TBFLAG_A64(tb_flags, NV2);
12716     dc->nv2_mem_e20 = EX_TBFLAG_A64(tb_flags, NV2_MEM_E20);
12717     dc->nv2_mem_be = EX_TBFLAG_A64(tb_flags, NV2_MEM_BE);
12718     dc->vec_len = 0;
12719     dc->vec_stride = 0;
12720     dc->cp_regs = arm_cpu->cp_regs;
12721     dc->features = env->features;
12722     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
12723     dc->gm_blocksize = arm_cpu->gm_blocksize;
12724 
12725 #ifdef CONFIG_USER_ONLY
12726     /* In sve_probe_page, we assume TBI is enabled. */
12727     tcg_debug_assert(dc->tbid & 1);
12728 #endif
12729 
12730     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
12731 
12732     /* Single step state. The code-generation logic here is:
12733      *  SS_ACTIVE == 0:
12734      *   generate code with no special handling for single-stepping (except
12735      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
12736      *   this happens anyway because those changes are all system register or
12737      *   PSTATE writes).
12738      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
12739      *   emit code for one insn
12740      *   emit code to clear PSTATE.SS
12741      *   emit code to generate software step exception for completed step
12742      *   end TB (as usual for having generated an exception)
12743      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
12744      *   emit code to generate a software step exception
12745      *   end the TB
12746      */
12747     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
12748     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
12749     dc->is_ldex = false;
12750 
12751     /* Bound the number of insns to execute to those left on the page.  */
12752     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
12753 
12754     /* If architectural single step active, limit to 1.  */
12755     if (dc->ss_active) {
12756         bound = 1;
12757     }
12758     dc->base.max_insns = MIN(dc->base.max_insns, bound);
12759 }
12760 
12761 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
12762 {
12763 }
12764 
12765 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
12766 {
12767     DisasContext *dc = container_of(dcbase, DisasContext, base);
12768     target_ulong pc_arg = dc->base.pc_next;
12769 
12770     if (tb_cflags(dcbase->tb) & CF_PCREL) {
12771         pc_arg &= ~TARGET_PAGE_MASK;
12772     }
12773     tcg_gen_insn_start(pc_arg, 0, 0);
12774     dc->insn_start_updated = false;
12775 }
12776 
12777 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
12778 {
12779     DisasContext *s = container_of(dcbase, DisasContext, base);
12780     CPUARMState *env = cpu_env(cpu);
12781     uint64_t pc = s->base.pc_next;
12782     uint32_t insn;
12783 
12784     /* Singlestep exceptions have the highest priority. */
12785     if (s->ss_active && !s->pstate_ss) {
12786         /* Singlestep state is Active-pending.
12787          * If we're in this state at the start of a TB then either
12788          *  a) we just took an exception to an EL which is being debugged
12789          *     and this is the first insn in the exception handler
12790          *  b) debug exceptions were masked and we just unmasked them
12791          *     without changing EL (eg by clearing PSTATE.D)
12792          * In either case we're going to take a swstep exception in the
12793          * "did not step an insn" case, and so the syndrome ISV and EX
12794          * bits should be zero.
12795          */
12796         assert(s->base.num_insns == 1);
12797         gen_swstep_exception(s, 0, 0);
12798         s->base.is_jmp = DISAS_NORETURN;
12799         s->base.pc_next = pc + 4;
12800         return;
12801     }
12802 
12803     if (pc & 3) {
12804         /*
12805          * PC alignment fault.  This has priority over the instruction abort
12806          * that we would receive from a translation fault via arm_ldl_code.
12807          * This should only be possible after an indirect branch, at the
12808          * start of the TB.
12809          */
12810         assert(s->base.num_insns == 1);
12811         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
12812         s->base.is_jmp = DISAS_NORETURN;
12813         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
12814         return;
12815     }
12816 
12817     s->pc_curr = pc;
12818     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
12819     s->insn = insn;
12820     s->base.pc_next = pc + 4;
12821 
12822     s->fp_access_checked = false;
12823     s->sve_access_checked = false;
12824 
12825     if (s->pstate_il) {
12826         /*
12827          * Illegal execution state. This has priority over BTI
12828          * exceptions, but comes after instruction abort exceptions.
12829          */
12830         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
12831         return;
12832     }
12833 
12834     if (dc_isar_feature(aa64_bti, s)) {
12835         if (s->base.num_insns == 1) {
12836             /*
12837              * At the first insn of the TB, compute s->guarded_page.
12838              * We delayed computing this until successfully reading
12839              * the first insn of the TB, above.  This (mostly) ensures
12840              * that the softmmu tlb entry has been populated, and the
12841              * page table GP bit is available.
12842              *
12843              * Note that we need to compute this even if btype == 0,
12844              * because this value is used for BR instructions later
12845              * where ENV is not available.
12846              */
12847             s->guarded_page = is_guarded_page(env, s);
12848 
12849             /* First insn can have btype set to non-zero.  */
12850             tcg_debug_assert(s->btype >= 0);
12851 
12852             /*
12853              * Note that the Branch Target Exception has fairly high
12854              * priority -- below debugging exceptions but above most
12855              * everything else.  This allows us to handle this now
12856              * instead of waiting until the insn is otherwise decoded.
12857              */
12858             if (s->btype != 0
12859                 && s->guarded_page
12860                 && !btype_destination_ok(insn, s->bt, s->btype)) {
12861                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
12862                 return;
12863             }
12864         } else {
12865             /* Not the first insn: btype must be 0.  */
12866             tcg_debug_assert(s->btype == 0);
12867         }
12868     }
12869 
12870     s->is_nonstreaming = false;
12871     if (s->sme_trap_nonstreaming) {
12872         disas_sme_fa64(s, insn);
12873     }
12874 
12875     if (!disas_a64(s, insn) &&
12876         !disas_sme(s, insn) &&
12877         !disas_sve(s, insn)) {
12878         disas_a64_legacy(s, insn);
12879     }
12880 
12881     /*
12882      * After execution of most insns, btype is reset to 0.
12883      * Note that we set btype == -1 when the insn sets btype.
12884      */
12885     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
12886         reset_btype(s);
12887     }
12888 }
12889 
12890 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
12891 {
12892     DisasContext *dc = container_of(dcbase, DisasContext, base);
12893 
12894     if (unlikely(dc->ss_active)) {
12895         /* Note that this means single stepping WFI doesn't halt the CPU.
12896          * For conditional branch insns this is harmless unreachable code as
12897          * gen_goto_tb() has already handled emitting the debug exception
12898          * (and thus a tb-jump is not possible when singlestepping).
12899          */
12900         switch (dc->base.is_jmp) {
12901         default:
12902             gen_a64_update_pc(dc, 4);
12903             /* fall through */
12904         case DISAS_EXIT:
12905         case DISAS_JUMP:
12906             gen_step_complete_exception(dc);
12907             break;
12908         case DISAS_NORETURN:
12909             break;
12910         }
12911     } else {
12912         switch (dc->base.is_jmp) {
12913         case DISAS_NEXT:
12914         case DISAS_TOO_MANY:
12915             gen_goto_tb(dc, 1, 4);
12916             break;
12917         default:
12918         case DISAS_UPDATE_EXIT:
12919             gen_a64_update_pc(dc, 4);
12920             /* fall through */
12921         case DISAS_EXIT:
12922             tcg_gen_exit_tb(NULL, 0);
12923             break;
12924         case DISAS_UPDATE_NOCHAIN:
12925             gen_a64_update_pc(dc, 4);
12926             /* fall through */
12927         case DISAS_JUMP:
12928             tcg_gen_lookup_and_goto_ptr();
12929             break;
12930         case DISAS_NORETURN:
12931         case DISAS_SWI:
12932             break;
12933         case DISAS_WFE:
12934             gen_a64_update_pc(dc, 4);
12935             gen_helper_wfe(tcg_env);
12936             break;
12937         case DISAS_YIELD:
12938             gen_a64_update_pc(dc, 4);
12939             gen_helper_yield(tcg_env);
12940             break;
12941         case DISAS_WFI:
12942             /*
12943              * This is a special case because we don't want to just halt
12944              * the CPU if trying to debug across a WFI.
12945              */
12946             gen_a64_update_pc(dc, 4);
12947             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
12948             /*
12949              * The helper doesn't necessarily throw an exception, but we
12950              * must go back to the main loop to check for interrupts anyway.
12951              */
12952             tcg_gen_exit_tb(NULL, 0);
12953             break;
12954         }
12955     }
12956 }
12957 
12958 const TranslatorOps aarch64_translator_ops = {
12959     .init_disas_context = aarch64_tr_init_disas_context,
12960     .tb_start           = aarch64_tr_tb_start,
12961     .insn_start         = aarch64_tr_insn_start,
12962     .translate_insn     = aarch64_tr_translate_insn,
12963     .tb_stop            = aarch64_tr_tb_stop,
12964 };
12965