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