xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 28b5451b)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "exec/exec-all.h"
22 #include "translate.h"
23 #include "translate-a64.h"
24 #include "qemu/log.h"
25 #include "arm_ldst.h"
26 #include "semihosting/semihost.h"
27 #include "cpregs.h"
28 
29 static TCGv_i64 cpu_X[32];
30 static TCGv_i64 cpu_pc;
31 
32 /* Load/store exclusive handling */
33 static TCGv_i64 cpu_exclusive_high;
34 
35 static const char *regnames[] = {
36     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
37     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
38     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
39     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
40 };
41 
42 enum a64_shift_type {
43     A64_SHIFT_TYPE_LSL = 0,
44     A64_SHIFT_TYPE_LSR = 1,
45     A64_SHIFT_TYPE_ASR = 2,
46     A64_SHIFT_TYPE_ROR = 3
47 };
48 
49 /*
50  * Helpers for extracting complex instruction fields
51  */
52 
53 /*
54  * For load/store with an unsigned 12 bit immediate scaled by the element
55  * size. The input has the immediate field in bits [14:3] and the element
56  * size in [2:0].
57  */
58 static int uimm_scaled(DisasContext *s, int x)
59 {
60     unsigned imm = x >> 3;
61     unsigned scale = extract32(x, 0, 3);
62     return imm << scale;
63 }
64 
65 /* For load/store memory tags: scale offset by LOG2_TAG_GRANULE */
66 static int scale_by_log2_tag_granule(DisasContext *s, int x)
67 {
68     return x << LOG2_TAG_GRANULE;
69 }
70 
71 /*
72  * Include the generated decoders.
73  */
74 
75 #include "decode-sme-fa64.c.inc"
76 #include "decode-a64.c.inc"
77 
78 /* Table based decoder typedefs - used when the relevant bits for decode
79  * are too awkwardly scattered across the instruction (eg SIMD).
80  */
81 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
82 
83 typedef struct AArch64DecodeTable {
84     uint32_t pattern;
85     uint32_t mask;
86     AArch64DecodeFn *disas_fn;
87 } AArch64DecodeTable;
88 
89 /* initialize TCG globals.  */
90 void a64_translate_init(void)
91 {
92     int i;
93 
94     cpu_pc = tcg_global_mem_new_i64(tcg_env,
95                                     offsetof(CPUARMState, pc),
96                                     "pc");
97     for (i = 0; i < 32; i++) {
98         cpu_X[i] = tcg_global_mem_new_i64(tcg_env,
99                                           offsetof(CPUARMState, xregs[i]),
100                                           regnames[i]);
101     }
102 
103     cpu_exclusive_high = tcg_global_mem_new_i64(tcg_env,
104         offsetof(CPUARMState, exclusive_high), "exclusive_high");
105 }
106 
107 /*
108  * Return the core mmu_idx to use for A64 load/store insns which
109  * have a "unprivileged load/store" variant. Those insns access
110  * EL0 if executed from an EL which has control over EL0 (usually
111  * EL1) but behave like normal loads and stores if executed from
112  * elsewhere (eg EL3).
113  *
114  * @unpriv : true for the unprivileged encoding; false for the
115  *           normal encoding (in which case we will return the same
116  *           thing as get_mem_index().
117  */
118 static int get_a64_user_mem_index(DisasContext *s, bool unpriv)
119 {
120     /*
121      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
122      * which is the usual mmu_idx for this cpu state.
123      */
124     ARMMMUIdx useridx = s->mmu_idx;
125 
126     if (unpriv && s->unpriv) {
127         /*
128          * We have pre-computed the condition for AccType_UNPRIV.
129          * Therefore we should never get here with a mmu_idx for
130          * which we do not know the corresponding user mmu_idx.
131          */
132         switch (useridx) {
133         case ARMMMUIdx_E10_1:
134         case ARMMMUIdx_E10_1_PAN:
135             useridx = ARMMMUIdx_E10_0;
136             break;
137         case ARMMMUIdx_E20_2:
138         case ARMMMUIdx_E20_2_PAN:
139             useridx = ARMMMUIdx_E20_0;
140             break;
141         default:
142             g_assert_not_reached();
143         }
144     }
145     return arm_to_core_mmu_idx(useridx);
146 }
147 
148 static void set_btype_raw(int val)
149 {
150     tcg_gen_st_i32(tcg_constant_i32(val), tcg_env,
151                    offsetof(CPUARMState, btype));
152 }
153 
154 static void set_btype(DisasContext *s, int val)
155 {
156     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
157     tcg_debug_assert(val >= 1 && val <= 3);
158     set_btype_raw(val);
159     s->btype = -1;
160 }
161 
162 static void reset_btype(DisasContext *s)
163 {
164     if (s->btype != 0) {
165         set_btype_raw(0);
166         s->btype = 0;
167     }
168 }
169 
170 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
171 {
172     assert(s->pc_save != -1);
173     if (tb_cflags(s->base.tb) & CF_PCREL) {
174         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
175     } else {
176         tcg_gen_movi_i64(dest, s->pc_curr + diff);
177     }
178 }
179 
180 void gen_a64_update_pc(DisasContext *s, target_long diff)
181 {
182     gen_pc_plus_diff(s, cpu_pc, diff);
183     s->pc_save = s->pc_curr + diff;
184 }
185 
186 /*
187  * Handle Top Byte Ignore (TBI) bits.
188  *
189  * If address tagging is enabled via the TCR TBI bits:
190  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
191  *    then the address is zero-extended, clearing bits [63:56]
192  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
193  *    and TBI1 controls addresses with bit 55 == 1.
194  *    If the appropriate TBI bit is set for the address then
195  *    the address is sign-extended from bit 55 into bits [63:56]
196  *
197  * Here We have concatenated TBI{1,0} into tbi.
198  */
199 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
200                                 TCGv_i64 src, int tbi)
201 {
202     if (tbi == 0) {
203         /* Load unmodified address */
204         tcg_gen_mov_i64(dst, src);
205     } else if (!regime_has_2_ranges(s->mmu_idx)) {
206         /* Force tag byte to all zero */
207         tcg_gen_extract_i64(dst, src, 0, 56);
208     } else {
209         /* Sign-extend from bit 55.  */
210         tcg_gen_sextract_i64(dst, src, 0, 56);
211 
212         switch (tbi) {
213         case 1:
214             /* tbi0 but !tbi1: only use the extension if positive */
215             tcg_gen_and_i64(dst, dst, src);
216             break;
217         case 2:
218             /* !tbi0 but tbi1: only use the extension if negative */
219             tcg_gen_or_i64(dst, dst, src);
220             break;
221         case 3:
222             /* tbi0 and tbi1: always use the extension */
223             break;
224         default:
225             g_assert_not_reached();
226         }
227     }
228 }
229 
230 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
231 {
232     /*
233      * If address tagging is enabled for instructions via the TCR TBI bits,
234      * then loading an address into the PC will clear out any tag.
235      */
236     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
237     s->pc_save = -1;
238 }
239 
240 /*
241  * Handle MTE and/or TBI.
242  *
243  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
244  * for the tag to be present in the FAR_ELx register.  But for user-only
245  * mode we do not have a TLB with which to implement this, so we must
246  * remove the top byte now.
247  *
248  * Always return a fresh temporary that we can increment independently
249  * of the write-back address.
250  */
251 
252 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
253 {
254     TCGv_i64 clean = tcg_temp_new_i64();
255 #ifdef CONFIG_USER_ONLY
256     gen_top_byte_ignore(s, clean, addr, s->tbid);
257 #else
258     tcg_gen_mov_i64(clean, addr);
259 #endif
260     return clean;
261 }
262 
263 /* Insert a zero tag into src, with the result at dst. */
264 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
265 {
266     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
267 }
268 
269 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
270                              MMUAccessType acc, int log2_size)
271 {
272     gen_helper_probe_access(tcg_env, ptr,
273                             tcg_constant_i32(acc),
274                             tcg_constant_i32(get_mem_index(s)),
275                             tcg_constant_i32(1 << log2_size));
276 }
277 
278 /*
279  * For MTE, check a single logical or atomic access.  This probes a single
280  * address, the exact one specified.  The size and alignment of the access
281  * is not relevant to MTE, per se, but watchpoints do require the size,
282  * and we want to recognize those before making any other changes to state.
283  */
284 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
285                                       bool is_write, bool tag_checked,
286                                       MemOp memop, bool is_unpriv,
287                                       int core_idx)
288 {
289     if (tag_checked && s->mte_active[is_unpriv]) {
290         TCGv_i64 ret;
291         int desc = 0;
292 
293         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
294         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
295         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
296         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
297         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
298         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
299 
300         ret = tcg_temp_new_i64();
301         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
302 
303         return ret;
304     }
305     return clean_data_tbi(s, addr);
306 }
307 
308 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
309                         bool tag_checked, MemOp memop)
310 {
311     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
312                                  false, get_mem_index(s));
313 }
314 
315 /*
316  * For MTE, check multiple logical sequential accesses.
317  */
318 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
319                         bool tag_checked, int total_size, MemOp single_mop)
320 {
321     if (tag_checked && s->mte_active[0]) {
322         TCGv_i64 ret;
323         int desc = 0;
324 
325         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
326         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
327         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
328         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
329         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
330         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
331 
332         ret = tcg_temp_new_i64();
333         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
334 
335         return ret;
336     }
337     return clean_data_tbi(s, addr);
338 }
339 
340 /*
341  * Generate the special alignment check that applies to AccType_ATOMIC
342  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
343  * naturally aligned, but it must not cross a 16-byte boundary.
344  * See AArch64.CheckAlignment().
345  */
346 static void check_lse2_align(DisasContext *s, int rn, int imm,
347                              bool is_write, MemOp mop)
348 {
349     TCGv_i32 tmp;
350     TCGv_i64 addr;
351     TCGLabel *over_label;
352     MMUAccessType type;
353     int mmu_idx;
354 
355     tmp = tcg_temp_new_i32();
356     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
357     tcg_gen_addi_i32(tmp, tmp, imm & 15);
358     tcg_gen_andi_i32(tmp, tmp, 15);
359     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
360 
361     over_label = gen_new_label();
362     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
363 
364     addr = tcg_temp_new_i64();
365     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
366 
367     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
368     mmu_idx = get_mem_index(s);
369     gen_helper_unaligned_access(tcg_env, addr, tcg_constant_i32(type),
370                                 tcg_constant_i32(mmu_idx));
371 
372     gen_set_label(over_label);
373 
374 }
375 
376 /* Handle the alignment check for AccType_ATOMIC instructions. */
377 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
378 {
379     MemOp size = mop & MO_SIZE;
380 
381     if (size == MO_8) {
382         return mop;
383     }
384 
385     /*
386      * If size == MO_128, this is a LDXP, and the operation is single-copy
387      * atomic for each doubleword, not the entire quadword; it still must
388      * be quadword aligned.
389      */
390     if (size == MO_128) {
391         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
392                                    MO_ATOM_IFALIGN_PAIR);
393     }
394     if (dc_isar_feature(aa64_lse2, s)) {
395         check_lse2_align(s, rn, 0, true, mop);
396     } else {
397         mop |= MO_ALIGN;
398     }
399     return finalize_memop(s, mop);
400 }
401 
402 /* Handle the alignment check for AccType_ORDERED instructions. */
403 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
404                                  bool is_write, MemOp mop)
405 {
406     MemOp size = mop & MO_SIZE;
407 
408     if (size == MO_8) {
409         return mop;
410     }
411     if (size == MO_128) {
412         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
413                                    MO_ATOM_IFALIGN_PAIR);
414     }
415     if (!dc_isar_feature(aa64_lse2, s)) {
416         mop |= MO_ALIGN;
417     } else if (!s->naa) {
418         check_lse2_align(s, rn, imm, is_write, mop);
419     }
420     return finalize_memop(s, mop);
421 }
422 
423 typedef struct DisasCompare64 {
424     TCGCond cond;
425     TCGv_i64 value;
426 } DisasCompare64;
427 
428 static void a64_test_cc(DisasCompare64 *c64, int cc)
429 {
430     DisasCompare c32;
431 
432     arm_test_cc(&c32, cc);
433 
434     /*
435      * Sign-extend the 32-bit value so that the GE/LT comparisons work
436      * properly.  The NE/EQ comparisons are also fine with this choice.
437       */
438     c64->cond = c32.cond;
439     c64->value = tcg_temp_new_i64();
440     tcg_gen_ext_i32_i64(c64->value, c32.value);
441 }
442 
443 static void gen_rebuild_hflags(DisasContext *s)
444 {
445     gen_helper_rebuild_hflags_a64(tcg_env, tcg_constant_i32(s->current_el));
446 }
447 
448 static void gen_exception_internal(int excp)
449 {
450     assert(excp_is_internal(excp));
451     gen_helper_exception_internal(tcg_env, tcg_constant_i32(excp));
452 }
453 
454 static void gen_exception_internal_insn(DisasContext *s, int excp)
455 {
456     gen_a64_update_pc(s, 0);
457     gen_exception_internal(excp);
458     s->base.is_jmp = DISAS_NORETURN;
459 }
460 
461 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
462 {
463     gen_a64_update_pc(s, 0);
464     gen_helper_exception_bkpt_insn(tcg_env, tcg_constant_i32(syndrome));
465     s->base.is_jmp = DISAS_NORETURN;
466 }
467 
468 static void gen_step_complete_exception(DisasContext *s)
469 {
470     /* We just completed step of an insn. Move from Active-not-pending
471      * to Active-pending, and then also take the swstep exception.
472      * This corresponds to making the (IMPDEF) choice to prioritize
473      * swstep exceptions over asynchronous exceptions taken to an exception
474      * level where debug is disabled. This choice has the advantage that
475      * we do not need to maintain internal state corresponding to the
476      * ISV/EX syndrome bits between completion of the step and generation
477      * of the exception, and our syndrome information is always correct.
478      */
479     gen_ss_advance(s);
480     gen_swstep_exception(s, 1, s->is_ldex);
481     s->base.is_jmp = DISAS_NORETURN;
482 }
483 
484 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
485 {
486     if (s->ss_active) {
487         return false;
488     }
489     return translator_use_goto_tb(&s->base, dest);
490 }
491 
492 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
493 {
494     if (use_goto_tb(s, s->pc_curr + diff)) {
495         /*
496          * For pcrel, the pc must always be up-to-date on entry to
497          * the linked TB, so that it can use simple additions for all
498          * further adjustments.  For !pcrel, the linked TB is compiled
499          * to know its full virtual address, so we can delay the
500          * update to pc to the unlinked path.  A long chain of links
501          * can thus avoid many updates to the PC.
502          */
503         if (tb_cflags(s->base.tb) & CF_PCREL) {
504             gen_a64_update_pc(s, diff);
505             tcg_gen_goto_tb(n);
506         } else {
507             tcg_gen_goto_tb(n);
508             gen_a64_update_pc(s, diff);
509         }
510         tcg_gen_exit_tb(s->base.tb, n);
511         s->base.is_jmp = DISAS_NORETURN;
512     } else {
513         gen_a64_update_pc(s, diff);
514         if (s->ss_active) {
515             gen_step_complete_exception(s);
516         } else {
517             tcg_gen_lookup_and_goto_ptr();
518             s->base.is_jmp = DISAS_NORETURN;
519         }
520     }
521 }
522 
523 /*
524  * Register access functions
525  *
526  * These functions are used for directly accessing a register in where
527  * changes to the final register value are likely to be made. If you
528  * need to use a register for temporary calculation (e.g. index type
529  * operations) use the read_* form.
530  *
531  * B1.2.1 Register mappings
532  *
533  * In instruction register encoding 31 can refer to ZR (zero register) or
534  * the SP (stack pointer) depending on context. In QEMU's case we map SP
535  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
536  * This is the point of the _sp forms.
537  */
538 TCGv_i64 cpu_reg(DisasContext *s, int reg)
539 {
540     if (reg == 31) {
541         TCGv_i64 t = tcg_temp_new_i64();
542         tcg_gen_movi_i64(t, 0);
543         return t;
544     } else {
545         return cpu_X[reg];
546     }
547 }
548 
549 /* register access for when 31 == SP */
550 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
551 {
552     return cpu_X[reg];
553 }
554 
555 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
556  * representing the register contents. This TCGv is an auto-freed
557  * temporary so it need not be explicitly freed, and may be modified.
558  */
559 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
560 {
561     TCGv_i64 v = tcg_temp_new_i64();
562     if (reg != 31) {
563         if (sf) {
564             tcg_gen_mov_i64(v, cpu_X[reg]);
565         } else {
566             tcg_gen_ext32u_i64(v, cpu_X[reg]);
567         }
568     } else {
569         tcg_gen_movi_i64(v, 0);
570     }
571     return v;
572 }
573 
574 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
575 {
576     TCGv_i64 v = tcg_temp_new_i64();
577     if (sf) {
578         tcg_gen_mov_i64(v, cpu_X[reg]);
579     } else {
580         tcg_gen_ext32u_i64(v, cpu_X[reg]);
581     }
582     return v;
583 }
584 
585 /* Return the offset into CPUARMState of a slice (from
586  * the least significant end) of FP register Qn (ie
587  * Dn, Sn, Hn or Bn).
588  * (Note that this is not the same mapping as for A32; see cpu.h)
589  */
590 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
591 {
592     return vec_reg_offset(s, regno, 0, size);
593 }
594 
595 /* Offset of the high half of the 128 bit vector Qn */
596 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
597 {
598     return vec_reg_offset(s, regno, 1, MO_64);
599 }
600 
601 /* Convenience accessors for reading and writing single and double
602  * FP registers. Writing clears the upper parts of the associated
603  * 128 bit vector register, as required by the architecture.
604  * Note that unlike the GP register accessors, the values returned
605  * by the read functions must be manually freed.
606  */
607 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
608 {
609     TCGv_i64 v = tcg_temp_new_i64();
610 
611     tcg_gen_ld_i64(v, tcg_env, fp_reg_offset(s, reg, MO_64));
612     return v;
613 }
614 
615 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
616 {
617     TCGv_i32 v = tcg_temp_new_i32();
618 
619     tcg_gen_ld_i32(v, tcg_env, fp_reg_offset(s, reg, MO_32));
620     return v;
621 }
622 
623 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
624 {
625     TCGv_i32 v = tcg_temp_new_i32();
626 
627     tcg_gen_ld16u_i32(v, tcg_env, fp_reg_offset(s, reg, MO_16));
628     return v;
629 }
630 
631 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
632  * If SVE is not enabled, then there are only 128 bits in the vector.
633  */
634 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
635 {
636     unsigned ofs = fp_reg_offset(s, rd, MO_64);
637     unsigned vsz = vec_full_reg_size(s);
638 
639     /* Nop move, with side effect of clearing the tail. */
640     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
641 }
642 
643 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
644 {
645     unsigned ofs = fp_reg_offset(s, reg, MO_64);
646 
647     tcg_gen_st_i64(v, tcg_env, ofs);
648     clear_vec_high(s, false, reg);
649 }
650 
651 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
652 {
653     TCGv_i64 tmp = tcg_temp_new_i64();
654 
655     tcg_gen_extu_i32_i64(tmp, v);
656     write_fp_dreg(s, reg, tmp);
657 }
658 
659 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
660 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
661                          GVecGen2Fn *gvec_fn, int vece)
662 {
663     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
664             is_q ? 16 : 8, vec_full_reg_size(s));
665 }
666 
667 /* Expand a 2-operand + immediate AdvSIMD vector operation using
668  * an expander function.
669  */
670 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
671                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
672 {
673     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674             imm, is_q ? 16 : 8, vec_full_reg_size(s));
675 }
676 
677 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
678 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
679                          GVecGen3Fn *gvec_fn, int vece)
680 {
681     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
682             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
683 }
684 
685 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
686 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
687                          int rx, GVecGen4Fn *gvec_fn, int vece)
688 {
689     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
690             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
691             is_q ? 16 : 8, vec_full_reg_size(s));
692 }
693 
694 /* Expand a 2-operand operation using an out-of-line helper.  */
695 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
696                              int rn, int data, gen_helper_gvec_2 *fn)
697 {
698     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
699                        vec_full_reg_offset(s, rn),
700                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
701 }
702 
703 /* Expand a 3-operand operation using an out-of-line helper.  */
704 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
705                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
706 {
707     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
708                        vec_full_reg_offset(s, rn),
709                        vec_full_reg_offset(s, rm),
710                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
711 }
712 
713 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
714  * an out-of-line helper.
715  */
716 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
717                               int rm, bool is_fp16, int data,
718                               gen_helper_gvec_3_ptr *fn)
719 {
720     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
721     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
722                        vec_full_reg_offset(s, rn),
723                        vec_full_reg_offset(s, rm), fpst,
724                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
725 }
726 
727 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
728 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
729                             int rm, gen_helper_gvec_3_ptr *fn)
730 {
731     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
732 
733     tcg_gen_addi_ptr(qc_ptr, tcg_env, offsetof(CPUARMState, vfp.qc));
734     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
735                        vec_full_reg_offset(s, rn),
736                        vec_full_reg_offset(s, rm), qc_ptr,
737                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
738 }
739 
740 /* Expand a 4-operand operation using an out-of-line helper.  */
741 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
742                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
743 {
744     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
745                        vec_full_reg_offset(s, rn),
746                        vec_full_reg_offset(s, rm),
747                        vec_full_reg_offset(s, ra),
748                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
749 }
750 
751 /*
752  * Expand a 4-operand + fpstatus pointer + simd data value operation using
753  * an out-of-line helper.
754  */
755 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
756                               int rm, int ra, bool is_fp16, int data,
757                               gen_helper_gvec_4_ptr *fn)
758 {
759     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
760     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
761                        vec_full_reg_offset(s, rn),
762                        vec_full_reg_offset(s, rm),
763                        vec_full_reg_offset(s, ra), fpst,
764                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
765 }
766 
767 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
768  * than the 32 bit equivalent.
769  */
770 static inline void gen_set_NZ64(TCGv_i64 result)
771 {
772     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
773     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
774 }
775 
776 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
777 static inline void gen_logic_CC(int sf, TCGv_i64 result)
778 {
779     if (sf) {
780         gen_set_NZ64(result);
781     } else {
782         tcg_gen_extrl_i64_i32(cpu_ZF, result);
783         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
784     }
785     tcg_gen_movi_i32(cpu_CF, 0);
786     tcg_gen_movi_i32(cpu_VF, 0);
787 }
788 
789 /* dest = T0 + T1; compute C, N, V and Z flags */
790 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
791 {
792     TCGv_i64 result, flag, tmp;
793     result = tcg_temp_new_i64();
794     flag = tcg_temp_new_i64();
795     tmp = tcg_temp_new_i64();
796 
797     tcg_gen_movi_i64(tmp, 0);
798     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
799 
800     tcg_gen_extrl_i64_i32(cpu_CF, flag);
801 
802     gen_set_NZ64(result);
803 
804     tcg_gen_xor_i64(flag, result, t0);
805     tcg_gen_xor_i64(tmp, t0, t1);
806     tcg_gen_andc_i64(flag, flag, tmp);
807     tcg_gen_extrh_i64_i32(cpu_VF, flag);
808 
809     tcg_gen_mov_i64(dest, result);
810 }
811 
812 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
813 {
814     TCGv_i32 t0_32 = tcg_temp_new_i32();
815     TCGv_i32 t1_32 = tcg_temp_new_i32();
816     TCGv_i32 tmp = tcg_temp_new_i32();
817 
818     tcg_gen_movi_i32(tmp, 0);
819     tcg_gen_extrl_i64_i32(t0_32, t0);
820     tcg_gen_extrl_i64_i32(t1_32, t1);
821     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
822     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
823     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
824     tcg_gen_xor_i32(tmp, t0_32, t1_32);
825     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
826     tcg_gen_extu_i32_i64(dest, cpu_NF);
827 }
828 
829 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
830 {
831     if (sf) {
832         gen_add64_CC(dest, t0, t1);
833     } else {
834         gen_add32_CC(dest, t0, t1);
835     }
836 }
837 
838 /* dest = T0 - T1; compute C, N, V and Z flags */
839 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
840 {
841     /* 64 bit arithmetic */
842     TCGv_i64 result, flag, tmp;
843 
844     result = tcg_temp_new_i64();
845     flag = tcg_temp_new_i64();
846     tcg_gen_sub_i64(result, t0, t1);
847 
848     gen_set_NZ64(result);
849 
850     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
851     tcg_gen_extrl_i64_i32(cpu_CF, flag);
852 
853     tcg_gen_xor_i64(flag, result, t0);
854     tmp = tcg_temp_new_i64();
855     tcg_gen_xor_i64(tmp, t0, t1);
856     tcg_gen_and_i64(flag, flag, tmp);
857     tcg_gen_extrh_i64_i32(cpu_VF, flag);
858     tcg_gen_mov_i64(dest, result);
859 }
860 
861 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
862 {
863     /* 32 bit arithmetic */
864     TCGv_i32 t0_32 = tcg_temp_new_i32();
865     TCGv_i32 t1_32 = tcg_temp_new_i32();
866     TCGv_i32 tmp;
867 
868     tcg_gen_extrl_i64_i32(t0_32, t0);
869     tcg_gen_extrl_i64_i32(t1_32, t1);
870     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
871     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
872     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
873     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
874     tmp = tcg_temp_new_i32();
875     tcg_gen_xor_i32(tmp, t0_32, t1_32);
876     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
877     tcg_gen_extu_i32_i64(dest, cpu_NF);
878 }
879 
880 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
881 {
882     if (sf) {
883         gen_sub64_CC(dest, t0, t1);
884     } else {
885         gen_sub32_CC(dest, t0, t1);
886     }
887 }
888 
889 /* dest = T0 + T1 + CF; do not compute flags. */
890 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
891 {
892     TCGv_i64 flag = tcg_temp_new_i64();
893     tcg_gen_extu_i32_i64(flag, cpu_CF);
894     tcg_gen_add_i64(dest, t0, t1);
895     tcg_gen_add_i64(dest, dest, flag);
896 
897     if (!sf) {
898         tcg_gen_ext32u_i64(dest, dest);
899     }
900 }
901 
902 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
903 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
904 {
905     if (sf) {
906         TCGv_i64 result = tcg_temp_new_i64();
907         TCGv_i64 cf_64 = tcg_temp_new_i64();
908         TCGv_i64 vf_64 = tcg_temp_new_i64();
909         TCGv_i64 tmp = tcg_temp_new_i64();
910         TCGv_i64 zero = tcg_constant_i64(0);
911 
912         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
913         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
914         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
915         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
916         gen_set_NZ64(result);
917 
918         tcg_gen_xor_i64(vf_64, result, t0);
919         tcg_gen_xor_i64(tmp, t0, t1);
920         tcg_gen_andc_i64(vf_64, vf_64, tmp);
921         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
922 
923         tcg_gen_mov_i64(dest, result);
924     } else {
925         TCGv_i32 t0_32 = tcg_temp_new_i32();
926         TCGv_i32 t1_32 = tcg_temp_new_i32();
927         TCGv_i32 tmp = tcg_temp_new_i32();
928         TCGv_i32 zero = tcg_constant_i32(0);
929 
930         tcg_gen_extrl_i64_i32(t0_32, t0);
931         tcg_gen_extrl_i64_i32(t1_32, t1);
932         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
933         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
934 
935         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
936         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
937         tcg_gen_xor_i32(tmp, t0_32, t1_32);
938         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
939         tcg_gen_extu_i32_i64(dest, cpu_NF);
940     }
941 }
942 
943 /*
944  * Load/Store generators
945  */
946 
947 /*
948  * Store from GPR register to memory.
949  */
950 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
951                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
952                              bool iss_valid,
953                              unsigned int iss_srt,
954                              bool iss_sf, bool iss_ar)
955 {
956     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
957 
958     if (iss_valid) {
959         uint32_t syn;
960 
961         syn = syn_data_abort_with_iss(0,
962                                       (memop & MO_SIZE),
963                                       false,
964                                       iss_srt,
965                                       iss_sf,
966                                       iss_ar,
967                                       0, 0, 0, 0, 0, false);
968         disas_set_insn_syndrome(s, syn);
969     }
970 }
971 
972 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
973                       TCGv_i64 tcg_addr, MemOp memop,
974                       bool iss_valid,
975                       unsigned int iss_srt,
976                       bool iss_sf, bool iss_ar)
977 {
978     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
979                      iss_valid, iss_srt, iss_sf, iss_ar);
980 }
981 
982 /*
983  * Load from memory to GPR register
984  */
985 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
986                              MemOp memop, bool extend, int memidx,
987                              bool iss_valid, unsigned int iss_srt,
988                              bool iss_sf, bool iss_ar)
989 {
990     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
991 
992     if (extend && (memop & MO_SIGN)) {
993         g_assert((memop & MO_SIZE) <= MO_32);
994         tcg_gen_ext32u_i64(dest, dest);
995     }
996 
997     if (iss_valid) {
998         uint32_t syn;
999 
1000         syn = syn_data_abort_with_iss(0,
1001                                       (memop & MO_SIZE),
1002                                       (memop & MO_SIGN) != 0,
1003                                       iss_srt,
1004                                       iss_sf,
1005                                       iss_ar,
1006                                       0, 0, 0, 0, 0, false);
1007         disas_set_insn_syndrome(s, syn);
1008     }
1009 }
1010 
1011 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1012                       MemOp memop, bool extend,
1013                       bool iss_valid, unsigned int iss_srt,
1014                       bool iss_sf, bool iss_ar)
1015 {
1016     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1017                      iss_valid, iss_srt, iss_sf, iss_ar);
1018 }
1019 
1020 /*
1021  * Store from FP register to memory
1022  */
1023 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1024 {
1025     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1026     TCGv_i64 tmplo = tcg_temp_new_i64();
1027 
1028     tcg_gen_ld_i64(tmplo, tcg_env, fp_reg_offset(s, srcidx, MO_64));
1029 
1030     if ((mop & MO_SIZE) < MO_128) {
1031         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1032     } else {
1033         TCGv_i64 tmphi = tcg_temp_new_i64();
1034         TCGv_i128 t16 = tcg_temp_new_i128();
1035 
1036         tcg_gen_ld_i64(tmphi, tcg_env, fp_reg_hi_offset(s, srcidx));
1037         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1038 
1039         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1040     }
1041 }
1042 
1043 /*
1044  * Load from memory to FP register
1045  */
1046 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1047 {
1048     /* This always zero-extends and writes to a full 128 bit wide vector */
1049     TCGv_i64 tmplo = tcg_temp_new_i64();
1050     TCGv_i64 tmphi = NULL;
1051 
1052     if ((mop & MO_SIZE) < MO_128) {
1053         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1054     } else {
1055         TCGv_i128 t16 = tcg_temp_new_i128();
1056 
1057         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1058 
1059         tmphi = tcg_temp_new_i64();
1060         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1061     }
1062 
1063     tcg_gen_st_i64(tmplo, tcg_env, fp_reg_offset(s, destidx, MO_64));
1064 
1065     if (tmphi) {
1066         tcg_gen_st_i64(tmphi, tcg_env, fp_reg_hi_offset(s, destidx));
1067     }
1068     clear_vec_high(s, tmphi != NULL, destidx);
1069 }
1070 
1071 /*
1072  * Vector load/store helpers.
1073  *
1074  * The principal difference between this and a FP load is that we don't
1075  * zero extend as we are filling a partial chunk of the vector register.
1076  * These functions don't support 128 bit loads/stores, which would be
1077  * normal load/store operations.
1078  *
1079  * The _i32 versions are useful when operating on 32 bit quantities
1080  * (eg for floating point single or using Neon helper functions).
1081  */
1082 
1083 /* Get value of an element within a vector register */
1084 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1085                              int element, MemOp memop)
1086 {
1087     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1088     switch ((unsigned)memop) {
1089     case MO_8:
1090         tcg_gen_ld8u_i64(tcg_dest, tcg_env, vect_off);
1091         break;
1092     case MO_16:
1093         tcg_gen_ld16u_i64(tcg_dest, tcg_env, vect_off);
1094         break;
1095     case MO_32:
1096         tcg_gen_ld32u_i64(tcg_dest, tcg_env, vect_off);
1097         break;
1098     case MO_8|MO_SIGN:
1099         tcg_gen_ld8s_i64(tcg_dest, tcg_env, vect_off);
1100         break;
1101     case MO_16|MO_SIGN:
1102         tcg_gen_ld16s_i64(tcg_dest, tcg_env, vect_off);
1103         break;
1104     case MO_32|MO_SIGN:
1105         tcg_gen_ld32s_i64(tcg_dest, tcg_env, vect_off);
1106         break;
1107     case MO_64:
1108     case MO_64|MO_SIGN:
1109         tcg_gen_ld_i64(tcg_dest, tcg_env, vect_off);
1110         break;
1111     default:
1112         g_assert_not_reached();
1113     }
1114 }
1115 
1116 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1117                                  int element, MemOp memop)
1118 {
1119     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1120     switch (memop) {
1121     case MO_8:
1122         tcg_gen_ld8u_i32(tcg_dest, tcg_env, vect_off);
1123         break;
1124     case MO_16:
1125         tcg_gen_ld16u_i32(tcg_dest, tcg_env, vect_off);
1126         break;
1127     case MO_8|MO_SIGN:
1128         tcg_gen_ld8s_i32(tcg_dest, tcg_env, vect_off);
1129         break;
1130     case MO_16|MO_SIGN:
1131         tcg_gen_ld16s_i32(tcg_dest, tcg_env, vect_off);
1132         break;
1133     case MO_32:
1134     case MO_32|MO_SIGN:
1135         tcg_gen_ld_i32(tcg_dest, tcg_env, vect_off);
1136         break;
1137     default:
1138         g_assert_not_reached();
1139     }
1140 }
1141 
1142 /* Set value of an element within a vector register */
1143 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1144                               int element, MemOp memop)
1145 {
1146     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1147     switch (memop) {
1148     case MO_8:
1149         tcg_gen_st8_i64(tcg_src, tcg_env, vect_off);
1150         break;
1151     case MO_16:
1152         tcg_gen_st16_i64(tcg_src, tcg_env, vect_off);
1153         break;
1154     case MO_32:
1155         tcg_gen_st32_i64(tcg_src, tcg_env, vect_off);
1156         break;
1157     case MO_64:
1158         tcg_gen_st_i64(tcg_src, tcg_env, vect_off);
1159         break;
1160     default:
1161         g_assert_not_reached();
1162     }
1163 }
1164 
1165 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1166                                   int destidx, int element, MemOp memop)
1167 {
1168     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1169     switch (memop) {
1170     case MO_8:
1171         tcg_gen_st8_i32(tcg_src, tcg_env, vect_off);
1172         break;
1173     case MO_16:
1174         tcg_gen_st16_i32(tcg_src, tcg_env, vect_off);
1175         break;
1176     case MO_32:
1177         tcg_gen_st_i32(tcg_src, tcg_env, vect_off);
1178         break;
1179     default:
1180         g_assert_not_reached();
1181     }
1182 }
1183 
1184 /* Store from vector register to memory */
1185 static void do_vec_st(DisasContext *s, int srcidx, int element,
1186                       TCGv_i64 tcg_addr, MemOp mop)
1187 {
1188     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1189 
1190     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1191     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1192 }
1193 
1194 /* Load from memory to vector register */
1195 static void do_vec_ld(DisasContext *s, int destidx, int element,
1196                       TCGv_i64 tcg_addr, MemOp mop)
1197 {
1198     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1199 
1200     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1201     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1202 }
1203 
1204 /* Check that FP/Neon access is enabled. If it is, return
1205  * true. If not, emit code to generate an appropriate exception,
1206  * and return false; the caller should not emit any code for
1207  * the instruction. Note that this check must happen after all
1208  * unallocated-encoding checks (otherwise the syndrome information
1209  * for the resulting exception will be incorrect).
1210  */
1211 static bool fp_access_check_only(DisasContext *s)
1212 {
1213     if (s->fp_excp_el) {
1214         assert(!s->fp_access_checked);
1215         s->fp_access_checked = true;
1216 
1217         gen_exception_insn_el(s, 0, EXCP_UDEF,
1218                               syn_fp_access_trap(1, 0xe, false, 0),
1219                               s->fp_excp_el);
1220         return false;
1221     }
1222     s->fp_access_checked = true;
1223     return true;
1224 }
1225 
1226 static bool fp_access_check(DisasContext *s)
1227 {
1228     if (!fp_access_check_only(s)) {
1229         return false;
1230     }
1231     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1232         gen_exception_insn(s, 0, EXCP_UDEF,
1233                            syn_smetrap(SME_ET_Streaming, false));
1234         return false;
1235     }
1236     return true;
1237 }
1238 
1239 /*
1240  * Check that SVE access is enabled.  If it is, return true.
1241  * If not, emit code to generate an appropriate exception and return false.
1242  * This function corresponds to CheckSVEEnabled().
1243  */
1244 bool sve_access_check(DisasContext *s)
1245 {
1246     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1247         assert(dc_isar_feature(aa64_sme, s));
1248         if (!sme_sm_enabled_check(s)) {
1249             goto fail_exit;
1250         }
1251     } else if (s->sve_excp_el) {
1252         gen_exception_insn_el(s, 0, EXCP_UDEF,
1253                               syn_sve_access_trap(), s->sve_excp_el);
1254         goto fail_exit;
1255     }
1256     s->sve_access_checked = true;
1257     return fp_access_check(s);
1258 
1259  fail_exit:
1260     /* Assert that we only raise one exception per instruction. */
1261     assert(!s->sve_access_checked);
1262     s->sve_access_checked = true;
1263     return false;
1264 }
1265 
1266 /*
1267  * Check that SME access is enabled, raise an exception if not.
1268  * Note that this function corresponds to CheckSMEAccess and is
1269  * only used directly for cpregs.
1270  */
1271 static bool sme_access_check(DisasContext *s)
1272 {
1273     if (s->sme_excp_el) {
1274         gen_exception_insn_el(s, 0, EXCP_UDEF,
1275                               syn_smetrap(SME_ET_AccessTrap, false),
1276                               s->sme_excp_el);
1277         return false;
1278     }
1279     return true;
1280 }
1281 
1282 /* This function corresponds to CheckSMEEnabled. */
1283 bool sme_enabled_check(DisasContext *s)
1284 {
1285     /*
1286      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1287      * to be zero when fp_excp_el has priority.  This is because we need
1288      * sme_excp_el by itself for cpregs access checks.
1289      */
1290     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1291         s->fp_access_checked = true;
1292         return sme_access_check(s);
1293     }
1294     return fp_access_check_only(s);
1295 }
1296 
1297 /* Common subroutine for CheckSMEAnd*Enabled. */
1298 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1299 {
1300     if (!sme_enabled_check(s)) {
1301         return false;
1302     }
1303     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1304         gen_exception_insn(s, 0, EXCP_UDEF,
1305                            syn_smetrap(SME_ET_NotStreaming, false));
1306         return false;
1307     }
1308     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1309         gen_exception_insn(s, 0, EXCP_UDEF,
1310                            syn_smetrap(SME_ET_InactiveZA, false));
1311         return false;
1312     }
1313     return true;
1314 }
1315 
1316 /*
1317  * Expanders for AdvSIMD translation functions.
1318  */
1319 
1320 static bool do_gvec_op2_ool(DisasContext *s, arg_qrr_e *a, int data,
1321                             gen_helper_gvec_2 *fn)
1322 {
1323     if (!a->q && a->esz == MO_64) {
1324         return false;
1325     }
1326     if (fp_access_check(s)) {
1327         gen_gvec_op2_ool(s, a->q, a->rd, a->rn, data, fn);
1328     }
1329     return true;
1330 }
1331 
1332 static bool do_gvec_op3_ool(DisasContext *s, arg_qrrr_e *a, int data,
1333                             gen_helper_gvec_3 *fn)
1334 {
1335     if (!a->q && a->esz == MO_64) {
1336         return false;
1337     }
1338     if (fp_access_check(s)) {
1339         gen_gvec_op3_ool(s, a->q, a->rd, a->rn, a->rm, data, fn);
1340     }
1341     return true;
1342 }
1343 
1344 static bool do_gvec_fn3(DisasContext *s, arg_qrrr_e *a, GVecGen3Fn *fn)
1345 {
1346     if (!a->q && a->esz == MO_64) {
1347         return false;
1348     }
1349     if (fp_access_check(s)) {
1350         gen_gvec_fn3(s, a->q, a->rd, a->rn, a->rm, fn, a->esz);
1351     }
1352     return true;
1353 }
1354 
1355 static bool do_gvec_fn3_no64(DisasContext *s, arg_qrrr_e *a, GVecGen3Fn *fn)
1356 {
1357     if (a->esz == MO_64) {
1358         return false;
1359     }
1360     if (fp_access_check(s)) {
1361         gen_gvec_fn3(s, a->q, a->rd, a->rn, a->rm, fn, a->esz);
1362     }
1363     return true;
1364 }
1365 
1366 static bool do_gvec_fn4(DisasContext *s, arg_qrrrr_e *a, GVecGen4Fn *fn)
1367 {
1368     if (!a->q && a->esz == MO_64) {
1369         return false;
1370     }
1371     if (fp_access_check(s)) {
1372         gen_gvec_fn4(s, a->q, a->rd, a->rn, a->rm, a->ra, fn, a->esz);
1373     }
1374     return true;
1375 }
1376 
1377 /*
1378  * This utility function is for doing register extension with an
1379  * optional shift. You will likely want to pass a temporary for the
1380  * destination register. See DecodeRegExtend() in the ARM ARM.
1381  */
1382 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1383                               int option, unsigned int shift)
1384 {
1385     int extsize = extract32(option, 0, 2);
1386     bool is_signed = extract32(option, 2, 1);
1387 
1388     tcg_gen_ext_i64(tcg_out, tcg_in, extsize | (is_signed ? MO_SIGN : 0));
1389     tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1390 }
1391 
1392 static inline void gen_check_sp_alignment(DisasContext *s)
1393 {
1394     /* The AArch64 architecture mandates that (if enabled via PSTATE
1395      * or SCTLR bits) there is a check that SP is 16-aligned on every
1396      * SP-relative load or store (with an exception generated if it is not).
1397      * In line with general QEMU practice regarding misaligned accesses,
1398      * we omit these checks for the sake of guest program performance.
1399      * This function is provided as a hook so we can more easily add these
1400      * checks in future (possibly as a "favour catching guest program bugs
1401      * over speed" user selectable option).
1402      */
1403 }
1404 
1405 /*
1406  * This provides a simple table based table lookup decoder. It is
1407  * intended to be used when the relevant bits for decode are too
1408  * awkwardly placed and switch/if based logic would be confusing and
1409  * deeply nested. Since it's a linear search through the table, tables
1410  * should be kept small.
1411  *
1412  * It returns the first handler where insn & mask == pattern, or
1413  * NULL if there is no match.
1414  * The table is terminated by an empty mask (i.e. 0)
1415  */
1416 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1417                                                uint32_t insn)
1418 {
1419     const AArch64DecodeTable *tptr = table;
1420 
1421     while (tptr->mask) {
1422         if ((insn & tptr->mask) == tptr->pattern) {
1423             return tptr->disas_fn;
1424         }
1425         tptr++;
1426     }
1427     return NULL;
1428 }
1429 
1430 /*
1431  * The instruction disassembly implemented here matches
1432  * the instruction encoding classifications in chapter C4
1433  * of the ARM Architecture Reference Manual (DDI0487B_a);
1434  * classification names and decode diagrams here should generally
1435  * match up with those in the manual.
1436  */
1437 
1438 static bool trans_B(DisasContext *s, arg_i *a)
1439 {
1440     reset_btype(s);
1441     gen_goto_tb(s, 0, a->imm);
1442     return true;
1443 }
1444 
1445 static bool trans_BL(DisasContext *s, arg_i *a)
1446 {
1447     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1448     reset_btype(s);
1449     gen_goto_tb(s, 0, a->imm);
1450     return true;
1451 }
1452 
1453 
1454 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1455 {
1456     DisasLabel match;
1457     TCGv_i64 tcg_cmp;
1458 
1459     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1460     reset_btype(s);
1461 
1462     match = gen_disas_label(s);
1463     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1464                         tcg_cmp, 0, match.label);
1465     gen_goto_tb(s, 0, 4);
1466     set_disas_label(s, match);
1467     gen_goto_tb(s, 1, a->imm);
1468     return true;
1469 }
1470 
1471 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1472 {
1473     DisasLabel match;
1474     TCGv_i64 tcg_cmp;
1475 
1476     tcg_cmp = tcg_temp_new_i64();
1477     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1478 
1479     reset_btype(s);
1480 
1481     match = gen_disas_label(s);
1482     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1483                         tcg_cmp, 0, match.label);
1484     gen_goto_tb(s, 0, 4);
1485     set_disas_label(s, match);
1486     gen_goto_tb(s, 1, a->imm);
1487     return true;
1488 }
1489 
1490 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1491 {
1492     /* BC.cond is only present with FEAT_HBC */
1493     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
1494         return false;
1495     }
1496     reset_btype(s);
1497     if (a->cond < 0x0e) {
1498         /* genuinely conditional branches */
1499         DisasLabel match = gen_disas_label(s);
1500         arm_gen_test_cc(a->cond, match.label);
1501         gen_goto_tb(s, 0, 4);
1502         set_disas_label(s, match);
1503         gen_goto_tb(s, 1, a->imm);
1504     } else {
1505         /* 0xe and 0xf are both "always" conditions */
1506         gen_goto_tb(s, 0, a->imm);
1507     }
1508     return true;
1509 }
1510 
1511 static void set_btype_for_br(DisasContext *s, int rn)
1512 {
1513     if (dc_isar_feature(aa64_bti, s)) {
1514         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1515         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1516     }
1517 }
1518 
1519 static void set_btype_for_blr(DisasContext *s)
1520 {
1521     if (dc_isar_feature(aa64_bti, s)) {
1522         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1523         set_btype(s, 2);
1524     }
1525 }
1526 
1527 static bool trans_BR(DisasContext *s, arg_r *a)
1528 {
1529     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1530     set_btype_for_br(s, a->rn);
1531     s->base.is_jmp = DISAS_JUMP;
1532     return true;
1533 }
1534 
1535 static bool trans_BLR(DisasContext *s, arg_r *a)
1536 {
1537     TCGv_i64 dst = cpu_reg(s, a->rn);
1538     TCGv_i64 lr = cpu_reg(s, 30);
1539     if (dst == lr) {
1540         TCGv_i64 tmp = tcg_temp_new_i64();
1541         tcg_gen_mov_i64(tmp, dst);
1542         dst = tmp;
1543     }
1544     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1545     gen_a64_set_pc(s, dst);
1546     set_btype_for_blr(s);
1547     s->base.is_jmp = DISAS_JUMP;
1548     return true;
1549 }
1550 
1551 static bool trans_RET(DisasContext *s, arg_r *a)
1552 {
1553     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1554     s->base.is_jmp = DISAS_JUMP;
1555     return true;
1556 }
1557 
1558 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1559                                    TCGv_i64 modifier, bool use_key_a)
1560 {
1561     TCGv_i64 truedst;
1562     /*
1563      * Return the branch target for a BRAA/RETA/etc, which is either
1564      * just the destination dst, or that value with the pauth check
1565      * done and the code removed from the high bits.
1566      */
1567     if (!s->pauth_active) {
1568         return dst;
1569     }
1570 
1571     truedst = tcg_temp_new_i64();
1572     if (use_key_a) {
1573         gen_helper_autia_combined(truedst, tcg_env, dst, modifier);
1574     } else {
1575         gen_helper_autib_combined(truedst, tcg_env, dst, modifier);
1576     }
1577     return truedst;
1578 }
1579 
1580 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1581 {
1582     TCGv_i64 dst;
1583 
1584     if (!dc_isar_feature(aa64_pauth, s)) {
1585         return false;
1586     }
1587 
1588     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1589     gen_a64_set_pc(s, dst);
1590     set_btype_for_br(s, a->rn);
1591     s->base.is_jmp = DISAS_JUMP;
1592     return true;
1593 }
1594 
1595 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1596 {
1597     TCGv_i64 dst, lr;
1598 
1599     if (!dc_isar_feature(aa64_pauth, s)) {
1600         return false;
1601     }
1602 
1603     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1604     lr = cpu_reg(s, 30);
1605     if (dst == lr) {
1606         TCGv_i64 tmp = tcg_temp_new_i64();
1607         tcg_gen_mov_i64(tmp, dst);
1608         dst = tmp;
1609     }
1610     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1611     gen_a64_set_pc(s, dst);
1612     set_btype_for_blr(s);
1613     s->base.is_jmp = DISAS_JUMP;
1614     return true;
1615 }
1616 
1617 static bool trans_RETA(DisasContext *s, arg_reta *a)
1618 {
1619     TCGv_i64 dst;
1620 
1621     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1622     gen_a64_set_pc(s, dst);
1623     s->base.is_jmp = DISAS_JUMP;
1624     return true;
1625 }
1626 
1627 static bool trans_BRA(DisasContext *s, arg_bra *a)
1628 {
1629     TCGv_i64 dst;
1630 
1631     if (!dc_isar_feature(aa64_pauth, s)) {
1632         return false;
1633     }
1634     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1635     gen_a64_set_pc(s, dst);
1636     set_btype_for_br(s, a->rn);
1637     s->base.is_jmp = DISAS_JUMP;
1638     return true;
1639 }
1640 
1641 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1642 {
1643     TCGv_i64 dst, lr;
1644 
1645     if (!dc_isar_feature(aa64_pauth, s)) {
1646         return false;
1647     }
1648     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1649     lr = cpu_reg(s, 30);
1650     if (dst == lr) {
1651         TCGv_i64 tmp = tcg_temp_new_i64();
1652         tcg_gen_mov_i64(tmp, dst);
1653         dst = tmp;
1654     }
1655     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1656     gen_a64_set_pc(s, dst);
1657     set_btype_for_blr(s);
1658     s->base.is_jmp = DISAS_JUMP;
1659     return true;
1660 }
1661 
1662 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1663 {
1664     TCGv_i64 dst;
1665 
1666     if (s->current_el == 0) {
1667         return false;
1668     }
1669     if (s->trap_eret) {
1670         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(0), 2);
1671         return true;
1672     }
1673     dst = tcg_temp_new_i64();
1674     tcg_gen_ld_i64(dst, tcg_env,
1675                    offsetof(CPUARMState, elr_el[s->current_el]));
1676 
1677     translator_io_start(&s->base);
1678 
1679     gen_helper_exception_return(tcg_env, dst);
1680     /* Must exit loop to check un-masked IRQs */
1681     s->base.is_jmp = DISAS_EXIT;
1682     return true;
1683 }
1684 
1685 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1686 {
1687     TCGv_i64 dst;
1688 
1689     if (!dc_isar_feature(aa64_pauth, s)) {
1690         return false;
1691     }
1692     if (s->current_el == 0) {
1693         return false;
1694     }
1695     /* The FGT trap takes precedence over an auth trap. */
1696     if (s->trap_eret) {
1697         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(a->m ? 3 : 2), 2);
1698         return true;
1699     }
1700     dst = tcg_temp_new_i64();
1701     tcg_gen_ld_i64(dst, tcg_env,
1702                    offsetof(CPUARMState, elr_el[s->current_el]));
1703 
1704     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1705 
1706     translator_io_start(&s->base);
1707 
1708     gen_helper_exception_return(tcg_env, dst);
1709     /* Must exit loop to check un-masked IRQs */
1710     s->base.is_jmp = DISAS_EXIT;
1711     return true;
1712 }
1713 
1714 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1715 {
1716     return true;
1717 }
1718 
1719 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1720 {
1721     /*
1722      * When running in MTTCG we don't generate jumps to the yield and
1723      * WFE helpers as it won't affect the scheduling of other vCPUs.
1724      * If we wanted to more completely model WFE/SEV so we don't busy
1725      * spin unnecessarily we would need to do something more involved.
1726      */
1727     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1728         s->base.is_jmp = DISAS_YIELD;
1729     }
1730     return true;
1731 }
1732 
1733 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1734 {
1735     s->base.is_jmp = DISAS_WFI;
1736     return true;
1737 }
1738 
1739 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1740 {
1741     /*
1742      * When running in MTTCG we don't generate jumps to the yield and
1743      * WFE helpers as it won't affect the scheduling of other vCPUs.
1744      * If we wanted to more completely model WFE/SEV so we don't busy
1745      * spin unnecessarily we would need to do something more involved.
1746      */
1747     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1748         s->base.is_jmp = DISAS_WFE;
1749     }
1750     return true;
1751 }
1752 
1753 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1754 {
1755     if (s->pauth_active) {
1756         gen_helper_xpaci(cpu_X[30], tcg_env, cpu_X[30]);
1757     }
1758     return true;
1759 }
1760 
1761 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1762 {
1763     if (s->pauth_active) {
1764         gen_helper_pacia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1765     }
1766     return true;
1767 }
1768 
1769 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1770 {
1771     if (s->pauth_active) {
1772         gen_helper_pacib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1773     }
1774     return true;
1775 }
1776 
1777 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1778 {
1779     if (s->pauth_active) {
1780         gen_helper_autia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1781     }
1782     return true;
1783 }
1784 
1785 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1786 {
1787     if (s->pauth_active) {
1788         gen_helper_autib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1789     }
1790     return true;
1791 }
1792 
1793 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1794 {
1795     /* Without RAS, we must implement this as NOP. */
1796     if (dc_isar_feature(aa64_ras, s)) {
1797         /*
1798          * QEMU does not have a source of physical SErrors,
1799          * so we are only concerned with virtual SErrors.
1800          * The pseudocode in the ARM for this case is
1801          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1802          *      AArch64.vESBOperation();
1803          * Most of the condition can be evaluated at translation time.
1804          * Test for EL2 present, and defer test for SEL2 to runtime.
1805          */
1806         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1807             gen_helper_vesb(tcg_env);
1808         }
1809     }
1810     return true;
1811 }
1812 
1813 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1814 {
1815     if (s->pauth_active) {
1816         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1817     }
1818     return true;
1819 }
1820 
1821 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1822 {
1823     if (s->pauth_active) {
1824         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1825     }
1826     return true;
1827 }
1828 
1829 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1830 {
1831     if (s->pauth_active) {
1832         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1833     }
1834     return true;
1835 }
1836 
1837 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1838 {
1839     if (s->pauth_active) {
1840         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1841     }
1842     return true;
1843 }
1844 
1845 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1846 {
1847     if (s->pauth_active) {
1848         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1849     }
1850     return true;
1851 }
1852 
1853 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1854 {
1855     if (s->pauth_active) {
1856         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1857     }
1858     return true;
1859 }
1860 
1861 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1862 {
1863     if (s->pauth_active) {
1864         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1865     }
1866     return true;
1867 }
1868 
1869 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1870 {
1871     if (s->pauth_active) {
1872         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1873     }
1874     return true;
1875 }
1876 
1877 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1878 {
1879     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1880     return true;
1881 }
1882 
1883 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1884 {
1885     /* We handle DSB and DMB the same way */
1886     TCGBar bar;
1887 
1888     switch (a->types) {
1889     case 1: /* MBReqTypes_Reads */
1890         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1891         break;
1892     case 2: /* MBReqTypes_Writes */
1893         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1894         break;
1895     default: /* MBReqTypes_All */
1896         bar = TCG_BAR_SC | TCG_MO_ALL;
1897         break;
1898     }
1899     tcg_gen_mb(bar);
1900     return true;
1901 }
1902 
1903 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1904 {
1905     /*
1906      * We need to break the TB after this insn to execute
1907      * self-modifying code correctly and also to take
1908      * any pending interrupts immediately.
1909      */
1910     reset_btype(s);
1911     gen_goto_tb(s, 0, 4);
1912     return true;
1913 }
1914 
1915 static bool trans_SB(DisasContext *s, arg_SB *a)
1916 {
1917     if (!dc_isar_feature(aa64_sb, s)) {
1918         return false;
1919     }
1920     /*
1921      * TODO: There is no speculation barrier opcode for TCG;
1922      * MB and end the TB instead.
1923      */
1924     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1925     gen_goto_tb(s, 0, 4);
1926     return true;
1927 }
1928 
1929 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1930 {
1931     if (!dc_isar_feature(aa64_condm_4, s)) {
1932         return false;
1933     }
1934     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1935     return true;
1936 }
1937 
1938 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1939 {
1940     TCGv_i32 z;
1941 
1942     if (!dc_isar_feature(aa64_condm_5, s)) {
1943         return false;
1944     }
1945 
1946     z = tcg_temp_new_i32();
1947 
1948     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1949 
1950     /*
1951      * (!C & !Z) << 31
1952      * (!(C | Z)) << 31
1953      * ~((C | Z) << 31)
1954      * ~-(C | Z)
1955      * (C | Z) - 1
1956      */
1957     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1958     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1959 
1960     /* !(Z & C) */
1961     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1962     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1963 
1964     /* (!C & Z) << 31 -> -(Z & ~C) */
1965     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1966     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1967 
1968     /* C | Z */
1969     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1970 
1971     return true;
1972 }
1973 
1974 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1975 {
1976     if (!dc_isar_feature(aa64_condm_5, s)) {
1977         return false;
1978     }
1979 
1980     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1981     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1982 
1983     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1984     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1985 
1986     tcg_gen_movi_i32(cpu_NF, 0);
1987     tcg_gen_movi_i32(cpu_VF, 0);
1988 
1989     return true;
1990 }
1991 
1992 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1993 {
1994     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1995         return false;
1996     }
1997     if (a->imm & 1) {
1998         set_pstate_bits(PSTATE_UAO);
1999     } else {
2000         clear_pstate_bits(PSTATE_UAO);
2001     }
2002     gen_rebuild_hflags(s);
2003     s->base.is_jmp = DISAS_TOO_MANY;
2004     return true;
2005 }
2006 
2007 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
2008 {
2009     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
2010         return false;
2011     }
2012     if (a->imm & 1) {
2013         set_pstate_bits(PSTATE_PAN);
2014     } else {
2015         clear_pstate_bits(PSTATE_PAN);
2016     }
2017     gen_rebuild_hflags(s);
2018     s->base.is_jmp = DISAS_TOO_MANY;
2019     return true;
2020 }
2021 
2022 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
2023 {
2024     if (s->current_el == 0) {
2025         return false;
2026     }
2027     gen_helper_msr_i_spsel(tcg_env, tcg_constant_i32(a->imm & PSTATE_SP));
2028     s->base.is_jmp = DISAS_TOO_MANY;
2029     return true;
2030 }
2031 
2032 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
2033 {
2034     if (!dc_isar_feature(aa64_ssbs, s)) {
2035         return false;
2036     }
2037     if (a->imm & 1) {
2038         set_pstate_bits(PSTATE_SSBS);
2039     } else {
2040         clear_pstate_bits(PSTATE_SSBS);
2041     }
2042     /* Don't need to rebuild hflags since SSBS is a nop */
2043     s->base.is_jmp = DISAS_TOO_MANY;
2044     return true;
2045 }
2046 
2047 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
2048 {
2049     if (!dc_isar_feature(aa64_dit, s)) {
2050         return false;
2051     }
2052     if (a->imm & 1) {
2053         set_pstate_bits(PSTATE_DIT);
2054     } else {
2055         clear_pstate_bits(PSTATE_DIT);
2056     }
2057     /* There's no need to rebuild hflags because DIT is a nop */
2058     s->base.is_jmp = DISAS_TOO_MANY;
2059     return true;
2060 }
2061 
2062 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2063 {
2064     if (dc_isar_feature(aa64_mte, s)) {
2065         /* Full MTE is enabled -- set the TCO bit as directed. */
2066         if (a->imm & 1) {
2067             set_pstate_bits(PSTATE_TCO);
2068         } else {
2069             clear_pstate_bits(PSTATE_TCO);
2070         }
2071         gen_rebuild_hflags(s);
2072         /* Many factors, including TCO, go into MTE_ACTIVE. */
2073         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2074         return true;
2075     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2076         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2077         return true;
2078     } else {
2079         /* Insn not present */
2080         return false;
2081     }
2082 }
2083 
2084 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2085 {
2086     gen_helper_msr_i_daifset(tcg_env, tcg_constant_i32(a->imm));
2087     s->base.is_jmp = DISAS_TOO_MANY;
2088     return true;
2089 }
2090 
2091 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2092 {
2093     gen_helper_msr_i_daifclear(tcg_env, tcg_constant_i32(a->imm));
2094     /* Exit the cpu loop to re-evaluate pending IRQs. */
2095     s->base.is_jmp = DISAS_UPDATE_EXIT;
2096     return true;
2097 }
2098 
2099 static bool trans_MSR_i_ALLINT(DisasContext *s, arg_i *a)
2100 {
2101     if (!dc_isar_feature(aa64_nmi, s) || s->current_el == 0) {
2102         return false;
2103     }
2104 
2105     if (a->imm == 0) {
2106         clear_pstate_bits(PSTATE_ALLINT);
2107     } else if (s->current_el > 1) {
2108         set_pstate_bits(PSTATE_ALLINT);
2109     } else {
2110         gen_helper_msr_set_allint_el1(tcg_env);
2111     }
2112 
2113     /* Exit the cpu loop to re-evaluate pending IRQs. */
2114     s->base.is_jmp = DISAS_UPDATE_EXIT;
2115     return true;
2116 }
2117 
2118 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2119 {
2120     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2121         return false;
2122     }
2123     if (sme_access_check(s)) {
2124         int old = s->pstate_sm | (s->pstate_za << 1);
2125         int new = a->imm * 3;
2126 
2127         if ((old ^ new) & a->mask) {
2128             /* At least one bit changes. */
2129             gen_helper_set_svcr(tcg_env, tcg_constant_i32(new),
2130                                 tcg_constant_i32(a->mask));
2131             s->base.is_jmp = DISAS_TOO_MANY;
2132         }
2133     }
2134     return true;
2135 }
2136 
2137 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2138 {
2139     TCGv_i32 tmp = tcg_temp_new_i32();
2140     TCGv_i32 nzcv = tcg_temp_new_i32();
2141 
2142     /* build bit 31, N */
2143     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2144     /* build bit 30, Z */
2145     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2146     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2147     /* build bit 29, C */
2148     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2149     /* build bit 28, V */
2150     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2151     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2152     /* generate result */
2153     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2154 }
2155 
2156 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2157 {
2158     TCGv_i32 nzcv = tcg_temp_new_i32();
2159 
2160     /* take NZCV from R[t] */
2161     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2162 
2163     /* bit 31, N */
2164     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2165     /* bit 30, Z */
2166     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2167     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2168     /* bit 29, C */
2169     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2170     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2171     /* bit 28, V */
2172     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2173     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2174 }
2175 
2176 static void gen_sysreg_undef(DisasContext *s, bool isread,
2177                              uint8_t op0, uint8_t op1, uint8_t op2,
2178                              uint8_t crn, uint8_t crm, uint8_t rt)
2179 {
2180     /*
2181      * Generate code to emit an UNDEF with correct syndrome
2182      * information for a failed system register access.
2183      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2184      * but if FEAT_IDST is implemented then read accesses to registers
2185      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2186      * syndrome.
2187      */
2188     uint32_t syndrome;
2189 
2190     if (isread && dc_isar_feature(aa64_ids, s) &&
2191         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2192         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2193     } else {
2194         syndrome = syn_uncategorized();
2195     }
2196     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2197 }
2198 
2199 /* MRS - move from system register
2200  * MSR (register) - move to system register
2201  * SYS
2202  * SYSL
2203  * These are all essentially the same insn in 'read' and 'write'
2204  * versions, with varying op0 fields.
2205  */
2206 static void handle_sys(DisasContext *s, bool isread,
2207                        unsigned int op0, unsigned int op1, unsigned int op2,
2208                        unsigned int crn, unsigned int crm, unsigned int rt)
2209 {
2210     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2211                                       crn, crm, op0, op1, op2);
2212     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2213     bool need_exit_tb = false;
2214     bool nv_trap_to_el2 = false;
2215     bool nv_redirect_reg = false;
2216     bool skip_fp_access_checks = false;
2217     bool nv2_mem_redirect = false;
2218     TCGv_ptr tcg_ri = NULL;
2219     TCGv_i64 tcg_rt;
2220     uint32_t syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2221 
2222     if (crn == 11 || crn == 15) {
2223         /*
2224          * Check for TIDCP trap, which must take precedence over
2225          * the UNDEF for "no such register" etc.
2226          */
2227         switch (s->current_el) {
2228         case 0:
2229             if (dc_isar_feature(aa64_tidcp1, s)) {
2230                 gen_helper_tidcp_el0(tcg_env, tcg_constant_i32(syndrome));
2231             }
2232             break;
2233         case 1:
2234             gen_helper_tidcp_el1(tcg_env, tcg_constant_i32(syndrome));
2235             break;
2236         }
2237     }
2238 
2239     if (!ri) {
2240         /* Unknown register; this might be a guest error or a QEMU
2241          * unimplemented feature.
2242          */
2243         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2244                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2245                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2246         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2247         return;
2248     }
2249 
2250     if (s->nv2 && ri->nv2_redirect_offset) {
2251         /*
2252          * Some registers always redirect to memory; some only do so if
2253          * HCR_EL2.NV1 is 0, and some only if NV1 is 1 (these come in
2254          * pairs which share an offset; see the table in R_CSRPQ).
2255          */
2256         if (ri->nv2_redirect_offset & NV2_REDIR_NV1) {
2257             nv2_mem_redirect = s->nv1;
2258         } else if (ri->nv2_redirect_offset & NV2_REDIR_NO_NV1) {
2259             nv2_mem_redirect = !s->nv1;
2260         } else {
2261             nv2_mem_redirect = true;
2262         }
2263     }
2264 
2265     /* Check access permissions */
2266     if (!cp_access_ok(s->current_el, ri, isread)) {
2267         /*
2268          * FEAT_NV/NV2 handling does not do the usual FP access checks
2269          * for registers only accessible at EL2 (though it *does* do them
2270          * for registers accessible at EL1).
2271          */
2272         skip_fp_access_checks = true;
2273         if (s->nv2 && (ri->type & ARM_CP_NV2_REDIRECT)) {
2274             /*
2275              * This is one of the few EL2 registers which should redirect
2276              * to the equivalent EL1 register. We do that after running
2277              * the EL2 register's accessfn.
2278              */
2279             nv_redirect_reg = true;
2280             assert(!nv2_mem_redirect);
2281         } else if (nv2_mem_redirect) {
2282             /*
2283              * NV2 redirect-to-memory takes precedence over trap to EL2 or
2284              * UNDEF to EL1.
2285              */
2286         } else if (s->nv && arm_cpreg_traps_in_nv(ri)) {
2287             /*
2288              * This register / instruction exists and is an EL2 register, so
2289              * we must trap to EL2 if accessed in nested virtualization EL1
2290              * instead of UNDEFing. We'll do that after the usual access checks.
2291              * (This makes a difference only for a couple of registers like
2292              * VSTTBR_EL2 where the "UNDEF if NonSecure" should take priority
2293              * over the trap-to-EL2. Most trapped-by-FEAT_NV registers have
2294              * an accessfn which does nothing when called from EL1, because
2295              * the trap-to-EL3 controls which would apply to that register
2296              * at EL2 don't take priority over the FEAT_NV trap-to-EL2.)
2297              */
2298             nv_trap_to_el2 = true;
2299         } else {
2300             gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2301             return;
2302         }
2303     }
2304 
2305     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2306         /* Emit code to perform further access permissions checks at
2307          * runtime; this may result in an exception.
2308          */
2309         gen_a64_update_pc(s, 0);
2310         tcg_ri = tcg_temp_new_ptr();
2311         gen_helper_access_check_cp_reg(tcg_ri, tcg_env,
2312                                        tcg_constant_i32(key),
2313                                        tcg_constant_i32(syndrome),
2314                                        tcg_constant_i32(isread));
2315     } else if (ri->type & ARM_CP_RAISES_EXC) {
2316         /*
2317          * The readfn or writefn might raise an exception;
2318          * synchronize the CPU state in case it does.
2319          */
2320         gen_a64_update_pc(s, 0);
2321     }
2322 
2323     if (!skip_fp_access_checks) {
2324         if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2325             return;
2326         } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2327             return;
2328         } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2329             return;
2330         }
2331     }
2332 
2333     if (nv_trap_to_el2) {
2334         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2335         return;
2336     }
2337 
2338     if (nv_redirect_reg) {
2339         /*
2340          * FEAT_NV2 redirection of an EL2 register to an EL1 register.
2341          * Conveniently in all cases the encoding of the EL1 register is
2342          * identical to the EL2 register except that opc1 is 0.
2343          * Get the reginfo for the EL1 register to use for the actual access.
2344          * We don't use the EL1 register's access function, and
2345          * fine-grained-traps on EL1 also do not apply here.
2346          */
2347         key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2348                                  crn, crm, op0, 0, op2);
2349         ri = get_arm_cp_reginfo(s->cp_regs, key);
2350         assert(ri);
2351         assert(cp_access_ok(s->current_el, ri, isread));
2352         /*
2353          * We might not have done an update_pc earlier, so check we don't
2354          * need it. We could support this in future if necessary.
2355          */
2356         assert(!(ri->type & ARM_CP_RAISES_EXC));
2357     }
2358 
2359     if (nv2_mem_redirect) {
2360         /*
2361          * This system register is being redirected into an EL2 memory access.
2362          * This means it is not an IO operation, doesn't change hflags,
2363          * and need not end the TB, because it has no side effects.
2364          *
2365          * The access is 64-bit single copy atomic, guaranteed aligned because
2366          * of the definition of VCNR_EL2. Its endianness depends on
2367          * SCTLR_EL2.EE, not on the data endianness of EL1.
2368          * It is done under either the EL2 translation regime or the EL2&0
2369          * translation regime, depending on HCR_EL2.E2H. It behaves as if
2370          * PSTATE.PAN is 0.
2371          */
2372         TCGv_i64 ptr = tcg_temp_new_i64();
2373         MemOp mop = MO_64 | MO_ALIGN | MO_ATOM_IFALIGN;
2374         ARMMMUIdx armmemidx = s->nv2_mem_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
2375         int memidx = arm_to_core_mmu_idx(armmemidx);
2376         uint32_t syn;
2377 
2378         mop |= (s->nv2_mem_be ? MO_BE : MO_LE);
2379 
2380         tcg_gen_ld_i64(ptr, tcg_env, offsetof(CPUARMState, cp15.vncr_el2));
2381         tcg_gen_addi_i64(ptr, ptr,
2382                          (ri->nv2_redirect_offset & ~NV2_REDIR_FLAG_MASK));
2383         tcg_rt = cpu_reg(s, rt);
2384 
2385         syn = syn_data_abort_vncr(0, !isread, 0);
2386         disas_set_insn_syndrome(s, syn);
2387         if (isread) {
2388             tcg_gen_qemu_ld_i64(tcg_rt, ptr, memidx, mop);
2389         } else {
2390             tcg_gen_qemu_st_i64(tcg_rt, ptr, memidx, mop);
2391         }
2392         return;
2393     }
2394 
2395     /* Handle special cases first */
2396     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2397     case 0:
2398         break;
2399     case ARM_CP_NOP:
2400         return;
2401     case ARM_CP_NZCV:
2402         tcg_rt = cpu_reg(s, rt);
2403         if (isread) {
2404             gen_get_nzcv(tcg_rt);
2405         } else {
2406             gen_set_nzcv(tcg_rt);
2407         }
2408         return;
2409     case ARM_CP_CURRENTEL:
2410     {
2411         /*
2412          * Reads as current EL value from pstate, which is
2413          * guaranteed to be constant by the tb flags.
2414          * For nested virt we should report EL2.
2415          */
2416         int el = s->nv ? 2 : s->current_el;
2417         tcg_rt = cpu_reg(s, rt);
2418         tcg_gen_movi_i64(tcg_rt, el << 2);
2419         return;
2420     }
2421     case ARM_CP_DC_ZVA:
2422         /* Writes clear the aligned block of memory which rt points into. */
2423         if (s->mte_active[0]) {
2424             int desc = 0;
2425 
2426             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2427             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2428             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2429 
2430             tcg_rt = tcg_temp_new_i64();
2431             gen_helper_mte_check_zva(tcg_rt, tcg_env,
2432                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2433         } else {
2434             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2435         }
2436         gen_helper_dc_zva(tcg_env, tcg_rt);
2437         return;
2438     case ARM_CP_DC_GVA:
2439         {
2440             TCGv_i64 clean_addr, tag;
2441 
2442             /*
2443              * DC_GVA, like DC_ZVA, requires that we supply the original
2444              * pointer for an invalid page.  Probe that address first.
2445              */
2446             tcg_rt = cpu_reg(s, rt);
2447             clean_addr = clean_data_tbi(s, tcg_rt);
2448             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2449 
2450             if (s->ata[0]) {
2451                 /* Extract the tag from the register to match STZGM.  */
2452                 tag = tcg_temp_new_i64();
2453                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2454                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2455             }
2456         }
2457         return;
2458     case ARM_CP_DC_GZVA:
2459         {
2460             TCGv_i64 clean_addr, tag;
2461 
2462             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2463             tcg_rt = cpu_reg(s, rt);
2464             clean_addr = clean_data_tbi(s, tcg_rt);
2465             gen_helper_dc_zva(tcg_env, clean_addr);
2466 
2467             if (s->ata[0]) {
2468                 /* Extract the tag from the register to match STZGM.  */
2469                 tag = tcg_temp_new_i64();
2470                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2471                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2472             }
2473         }
2474         return;
2475     default:
2476         g_assert_not_reached();
2477     }
2478 
2479     if (ri->type & ARM_CP_IO) {
2480         /* I/O operations must end the TB here (whether read or write) */
2481         need_exit_tb = translator_io_start(&s->base);
2482     }
2483 
2484     tcg_rt = cpu_reg(s, rt);
2485 
2486     if (isread) {
2487         if (ri->type & ARM_CP_CONST) {
2488             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2489         } else if (ri->readfn) {
2490             if (!tcg_ri) {
2491                 tcg_ri = gen_lookup_cp_reg(key);
2492             }
2493             gen_helper_get_cp_reg64(tcg_rt, tcg_env, tcg_ri);
2494         } else {
2495             tcg_gen_ld_i64(tcg_rt, tcg_env, ri->fieldoffset);
2496         }
2497     } else {
2498         if (ri->type & ARM_CP_CONST) {
2499             /* If not forbidden by access permissions, treat as WI */
2500             return;
2501         } else if (ri->writefn) {
2502             if (!tcg_ri) {
2503                 tcg_ri = gen_lookup_cp_reg(key);
2504             }
2505             gen_helper_set_cp_reg64(tcg_env, tcg_ri, tcg_rt);
2506         } else {
2507             tcg_gen_st_i64(tcg_rt, tcg_env, ri->fieldoffset);
2508         }
2509     }
2510 
2511     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2512         /*
2513          * A write to any coprocessor register that ends a TB
2514          * must rebuild the hflags for the next TB.
2515          */
2516         gen_rebuild_hflags(s);
2517         /*
2518          * We default to ending the TB on a coprocessor register write,
2519          * but allow this to be suppressed by the register definition
2520          * (usually only necessary to work around guest bugs).
2521          */
2522         need_exit_tb = true;
2523     }
2524     if (need_exit_tb) {
2525         s->base.is_jmp = DISAS_UPDATE_EXIT;
2526     }
2527 }
2528 
2529 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2530 {
2531     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2532     return true;
2533 }
2534 
2535 static bool trans_SVC(DisasContext *s, arg_i *a)
2536 {
2537     /*
2538      * For SVC, HVC and SMC we advance the single-step state
2539      * machine before taking the exception. This is architecturally
2540      * mandated, to ensure that single-stepping a system call
2541      * instruction works properly.
2542      */
2543     uint32_t syndrome = syn_aa64_svc(a->imm);
2544     if (s->fgt_svc) {
2545         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2546         return true;
2547     }
2548     gen_ss_advance(s);
2549     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2550     return true;
2551 }
2552 
2553 static bool trans_HVC(DisasContext *s, arg_i *a)
2554 {
2555     int target_el = s->current_el == 3 ? 3 : 2;
2556 
2557     if (s->current_el == 0) {
2558         unallocated_encoding(s);
2559         return true;
2560     }
2561     /*
2562      * The pre HVC helper handles cases when HVC gets trapped
2563      * as an undefined insn by runtime configuration.
2564      */
2565     gen_a64_update_pc(s, 0);
2566     gen_helper_pre_hvc(tcg_env);
2567     /* Architecture requires ss advance before we do the actual work */
2568     gen_ss_advance(s);
2569     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), target_el);
2570     return true;
2571 }
2572 
2573 static bool trans_SMC(DisasContext *s, arg_i *a)
2574 {
2575     if (s->current_el == 0) {
2576         unallocated_encoding(s);
2577         return true;
2578     }
2579     gen_a64_update_pc(s, 0);
2580     gen_helper_pre_smc(tcg_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2581     /* Architecture requires ss advance before we do the actual work */
2582     gen_ss_advance(s);
2583     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2584     return true;
2585 }
2586 
2587 static bool trans_BRK(DisasContext *s, arg_i *a)
2588 {
2589     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2590     return true;
2591 }
2592 
2593 static bool trans_HLT(DisasContext *s, arg_i *a)
2594 {
2595     /*
2596      * HLT. This has two purposes.
2597      * Architecturally, it is an external halting debug instruction.
2598      * Since QEMU doesn't implement external debug, we treat this as
2599      * it is required for halting debug disabled: it will UNDEF.
2600      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2601      */
2602     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2603         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2604     } else {
2605         unallocated_encoding(s);
2606     }
2607     return true;
2608 }
2609 
2610 /*
2611  * Load/Store exclusive instructions are implemented by remembering
2612  * the value/address loaded, and seeing if these are the same
2613  * when the store is performed. This is not actually the architecturally
2614  * mandated semantics, but it works for typical guest code sequences
2615  * and avoids having to monitor regular stores.
2616  *
2617  * The store exclusive uses the atomic cmpxchg primitives to avoid
2618  * races in multi-threaded linux-user and when MTTCG softmmu is
2619  * enabled.
2620  */
2621 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2622                                int size, bool is_pair)
2623 {
2624     int idx = get_mem_index(s);
2625     TCGv_i64 dirty_addr, clean_addr;
2626     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2627 
2628     s->is_ldex = true;
2629     dirty_addr = cpu_reg_sp(s, rn);
2630     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2631 
2632     g_assert(size <= 3);
2633     if (is_pair) {
2634         g_assert(size >= 2);
2635         if (size == 2) {
2636             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2637             if (s->be_data == MO_LE) {
2638                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2639                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2640             } else {
2641                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2642                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2643             }
2644         } else {
2645             TCGv_i128 t16 = tcg_temp_new_i128();
2646 
2647             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2648 
2649             if (s->be_data == MO_LE) {
2650                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2651                                       cpu_exclusive_high, t16);
2652             } else {
2653                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2654                                       cpu_exclusive_val, t16);
2655             }
2656             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2657             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2658         }
2659     } else {
2660         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2661         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2662     }
2663     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2664 }
2665 
2666 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2667                                 int rn, int size, int is_pair)
2668 {
2669     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2670      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2671      *     [addr] = {Rt};
2672      *     if (is_pair) {
2673      *         [addr + datasize] = {Rt2};
2674      *     }
2675      *     {Rd} = 0;
2676      * } else {
2677      *     {Rd} = 1;
2678      * }
2679      * env->exclusive_addr = -1;
2680      */
2681     TCGLabel *fail_label = gen_new_label();
2682     TCGLabel *done_label = gen_new_label();
2683     TCGv_i64 tmp, clean_addr;
2684     MemOp memop;
2685 
2686     /*
2687      * FIXME: We are out of spec here.  We have recorded only the address
2688      * from load_exclusive, not the entire range, and we assume that the
2689      * size of the access on both sides match.  The architecture allows the
2690      * store to be smaller than the load, so long as the stored bytes are
2691      * within the range recorded by the load.
2692      */
2693 
2694     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2695     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2696     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2697 
2698     /*
2699      * The write, and any associated faults, only happen if the virtual
2700      * and physical addresses pass the exclusive monitor check.  These
2701      * faults are exceedingly unlikely, because normally the guest uses
2702      * the exact same address register for the load_exclusive, and we
2703      * would have recognized these faults there.
2704      *
2705      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2706      * unaligned 4-byte write within the range of an aligned 8-byte load.
2707      * With LSE2, the store would need to cross a 16-byte boundary when the
2708      * load did not, which would mean the store is outside the range
2709      * recorded for the monitor, which would have failed a corrected monitor
2710      * check above.  For now, we assume no size change and retain the
2711      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2712      *
2713      * It is possible to trigger an MTE fault, by performing the load with
2714      * a virtual address with a valid tag and performing the store with the
2715      * same virtual address and a different invalid tag.
2716      */
2717     memop = size + is_pair;
2718     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2719         memop |= MO_ALIGN;
2720     }
2721     memop = finalize_memop(s, memop);
2722     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2723 
2724     tmp = tcg_temp_new_i64();
2725     if (is_pair) {
2726         if (size == 2) {
2727             if (s->be_data == MO_LE) {
2728                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2729             } else {
2730                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2731             }
2732             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2733                                        cpu_exclusive_val, tmp,
2734                                        get_mem_index(s), memop);
2735             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2736         } else {
2737             TCGv_i128 t16 = tcg_temp_new_i128();
2738             TCGv_i128 c16 = tcg_temp_new_i128();
2739             TCGv_i64 a, b;
2740 
2741             if (s->be_data == MO_LE) {
2742                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2743                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2744                                         cpu_exclusive_high);
2745             } else {
2746                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2747                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2748                                         cpu_exclusive_val);
2749             }
2750 
2751             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2752                                         get_mem_index(s), memop);
2753 
2754             a = tcg_temp_new_i64();
2755             b = tcg_temp_new_i64();
2756             if (s->be_data == MO_LE) {
2757                 tcg_gen_extr_i128_i64(a, b, t16);
2758             } else {
2759                 tcg_gen_extr_i128_i64(b, a, t16);
2760             }
2761 
2762             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2763             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2764             tcg_gen_or_i64(tmp, a, b);
2765 
2766             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2767         }
2768     } else {
2769         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2770                                    cpu_reg(s, rt), get_mem_index(s), memop);
2771         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2772     }
2773     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2774     tcg_gen_br(done_label);
2775 
2776     gen_set_label(fail_label);
2777     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2778     gen_set_label(done_label);
2779     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2780 }
2781 
2782 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2783                                  int rn, int size)
2784 {
2785     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2786     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2787     int memidx = get_mem_index(s);
2788     TCGv_i64 clean_addr;
2789     MemOp memop;
2790 
2791     if (rn == 31) {
2792         gen_check_sp_alignment(s);
2793     }
2794     memop = check_atomic_align(s, rn, size);
2795     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2796     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2797                                memidx, memop);
2798 }
2799 
2800 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2801                                       int rn, int size)
2802 {
2803     TCGv_i64 s1 = cpu_reg(s, rs);
2804     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2805     TCGv_i64 t1 = cpu_reg(s, rt);
2806     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2807     TCGv_i64 clean_addr;
2808     int memidx = get_mem_index(s);
2809     MemOp memop;
2810 
2811     if (rn == 31) {
2812         gen_check_sp_alignment(s);
2813     }
2814 
2815     /* This is a single atomic access, despite the "pair". */
2816     memop = check_atomic_align(s, rn, size + 1);
2817     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2818 
2819     if (size == 2) {
2820         TCGv_i64 cmp = tcg_temp_new_i64();
2821         TCGv_i64 val = tcg_temp_new_i64();
2822 
2823         if (s->be_data == MO_LE) {
2824             tcg_gen_concat32_i64(val, t1, t2);
2825             tcg_gen_concat32_i64(cmp, s1, s2);
2826         } else {
2827             tcg_gen_concat32_i64(val, t2, t1);
2828             tcg_gen_concat32_i64(cmp, s2, s1);
2829         }
2830 
2831         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2832 
2833         if (s->be_data == MO_LE) {
2834             tcg_gen_extr32_i64(s1, s2, cmp);
2835         } else {
2836             tcg_gen_extr32_i64(s2, s1, cmp);
2837         }
2838     } else {
2839         TCGv_i128 cmp = tcg_temp_new_i128();
2840         TCGv_i128 val = tcg_temp_new_i128();
2841 
2842         if (s->be_data == MO_LE) {
2843             tcg_gen_concat_i64_i128(val, t1, t2);
2844             tcg_gen_concat_i64_i128(cmp, s1, s2);
2845         } else {
2846             tcg_gen_concat_i64_i128(val, t2, t1);
2847             tcg_gen_concat_i64_i128(cmp, s2, s1);
2848         }
2849 
2850         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2851 
2852         if (s->be_data == MO_LE) {
2853             tcg_gen_extr_i128_i64(s1, s2, cmp);
2854         } else {
2855             tcg_gen_extr_i128_i64(s2, s1, cmp);
2856         }
2857     }
2858 }
2859 
2860 /*
2861  * Compute the ISS.SF bit for syndrome information if an exception
2862  * is taken on a load or store. This indicates whether the instruction
2863  * is accessing a 32-bit or 64-bit register. This logic is derived
2864  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2865  */
2866 static bool ldst_iss_sf(int size, bool sign, bool ext)
2867 {
2868 
2869     if (sign) {
2870         /*
2871          * Signed loads are 64 bit results if we are not going to
2872          * do a zero-extend from 32 to 64 after the load.
2873          * (For a store, sign and ext are always false.)
2874          */
2875         return !ext;
2876     } else {
2877         /* Unsigned loads/stores work at the specified size */
2878         return size == MO_64;
2879     }
2880 }
2881 
2882 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2883 {
2884     if (a->rn == 31) {
2885         gen_check_sp_alignment(s);
2886     }
2887     if (a->lasr) {
2888         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2889     }
2890     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2891     return true;
2892 }
2893 
2894 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2895 {
2896     if (a->rn == 31) {
2897         gen_check_sp_alignment(s);
2898     }
2899     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2900     if (a->lasr) {
2901         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2902     }
2903     return true;
2904 }
2905 
2906 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2907 {
2908     TCGv_i64 clean_addr;
2909     MemOp memop;
2910     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2911 
2912     /*
2913      * StoreLORelease is the same as Store-Release for QEMU, but
2914      * needs the feature-test.
2915      */
2916     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2917         return false;
2918     }
2919     /* Generate ISS for non-exclusive accesses including LASR.  */
2920     if (a->rn == 31) {
2921         gen_check_sp_alignment(s);
2922     }
2923     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2924     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2925     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2926                                 true, a->rn != 31, memop);
2927     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2928               iss_sf, a->lasr);
2929     return true;
2930 }
2931 
2932 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2933 {
2934     TCGv_i64 clean_addr;
2935     MemOp memop;
2936     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2937 
2938     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2939     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2940         return false;
2941     }
2942     /* Generate ISS for non-exclusive accesses including LASR.  */
2943     if (a->rn == 31) {
2944         gen_check_sp_alignment(s);
2945     }
2946     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2947     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2948                                 false, a->rn != 31, memop);
2949     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2950               a->rt, iss_sf, a->lasr);
2951     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2952     return true;
2953 }
2954 
2955 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2956 {
2957     if (a->rn == 31) {
2958         gen_check_sp_alignment(s);
2959     }
2960     if (a->lasr) {
2961         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2962     }
2963     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2964     return true;
2965 }
2966 
2967 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2968 {
2969     if (a->rn == 31) {
2970         gen_check_sp_alignment(s);
2971     }
2972     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2973     if (a->lasr) {
2974         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2975     }
2976     return true;
2977 }
2978 
2979 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2980 {
2981     if (!dc_isar_feature(aa64_atomics, s)) {
2982         return false;
2983     }
2984     if (((a->rt | a->rs) & 1) != 0) {
2985         return false;
2986     }
2987 
2988     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2989     return true;
2990 }
2991 
2992 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2993 {
2994     if (!dc_isar_feature(aa64_atomics, s)) {
2995         return false;
2996     }
2997     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2998     return true;
2999 }
3000 
3001 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
3002 {
3003     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
3004     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
3005     TCGv_i64 clean_addr = tcg_temp_new_i64();
3006     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3007 
3008     gen_pc_plus_diff(s, clean_addr, a->imm);
3009     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3010               false, true, a->rt, iss_sf, false);
3011     return true;
3012 }
3013 
3014 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
3015 {
3016     /* Load register (literal), vector version */
3017     TCGv_i64 clean_addr;
3018     MemOp memop;
3019 
3020     if (!fp_access_check(s)) {
3021         return true;
3022     }
3023     memop = finalize_memop_asimd(s, a->sz);
3024     clean_addr = tcg_temp_new_i64();
3025     gen_pc_plus_diff(s, clean_addr, a->imm);
3026     do_fp_ld(s, a->rt, clean_addr, memop);
3027     return true;
3028 }
3029 
3030 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
3031                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3032                                  uint64_t offset, bool is_store, MemOp mop)
3033 {
3034     if (a->rn == 31) {
3035         gen_check_sp_alignment(s);
3036     }
3037 
3038     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3039     if (!a->p) {
3040         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3041     }
3042 
3043     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
3044                                  (a->w || a->rn != 31), 2 << a->sz, mop);
3045 }
3046 
3047 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
3048                                   TCGv_i64 dirty_addr, uint64_t offset)
3049 {
3050     if (a->w) {
3051         if (a->p) {
3052             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3053         }
3054         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3055     }
3056 }
3057 
3058 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
3059 {
3060     uint64_t offset = a->imm << a->sz;
3061     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3062     MemOp mop = finalize_memop(s, a->sz);
3063 
3064     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3065     tcg_rt = cpu_reg(s, a->rt);
3066     tcg_rt2 = cpu_reg(s, a->rt2);
3067     /*
3068      * We built mop above for the single logical access -- rebuild it
3069      * now for the paired operation.
3070      *
3071      * With LSE2, non-sign-extending pairs are treated atomically if
3072      * aligned, and if unaligned one of the pair will be completely
3073      * within a 16-byte block and that element will be atomic.
3074      * Otherwise each element is separately atomic.
3075      * In all cases, issue one operation with the correct atomicity.
3076      */
3077     mop = a->sz + 1;
3078     if (s->align_mem) {
3079         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3080     }
3081     mop = finalize_memop_pair(s, mop);
3082     if (a->sz == 2) {
3083         TCGv_i64 tmp = tcg_temp_new_i64();
3084 
3085         if (s->be_data == MO_LE) {
3086             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3087         } else {
3088             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3089         }
3090         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3091     } else {
3092         TCGv_i128 tmp = tcg_temp_new_i128();
3093 
3094         if (s->be_data == MO_LE) {
3095             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3096         } else {
3097             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3098         }
3099         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3100     }
3101     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3102     return true;
3103 }
3104 
3105 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
3106 {
3107     uint64_t offset = a->imm << a->sz;
3108     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3109     MemOp mop = finalize_memop(s, a->sz);
3110 
3111     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3112     tcg_rt = cpu_reg(s, a->rt);
3113     tcg_rt2 = cpu_reg(s, a->rt2);
3114 
3115     /*
3116      * We built mop above for the single logical access -- rebuild it
3117      * now for the paired operation.
3118      *
3119      * With LSE2, non-sign-extending pairs are treated atomically if
3120      * aligned, and if unaligned one of the pair will be completely
3121      * within a 16-byte block and that element will be atomic.
3122      * Otherwise each element is separately atomic.
3123      * In all cases, issue one operation with the correct atomicity.
3124      *
3125      * This treats sign-extending loads like zero-extending loads,
3126      * since that reuses the most code below.
3127      */
3128     mop = a->sz + 1;
3129     if (s->align_mem) {
3130         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3131     }
3132     mop = finalize_memop_pair(s, mop);
3133     if (a->sz == 2) {
3134         int o2 = s->be_data == MO_LE ? 32 : 0;
3135         int o1 = o2 ^ 32;
3136 
3137         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3138         if (a->sign) {
3139             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3140             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3141         } else {
3142             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3143             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3144         }
3145     } else {
3146         TCGv_i128 tmp = tcg_temp_new_i128();
3147 
3148         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3149         if (s->be_data == MO_LE) {
3150             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3151         } else {
3152             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3153         }
3154     }
3155     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3156     return true;
3157 }
3158 
3159 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
3160 {
3161     uint64_t offset = a->imm << a->sz;
3162     TCGv_i64 clean_addr, dirty_addr;
3163     MemOp mop;
3164 
3165     if (!fp_access_check(s)) {
3166         return true;
3167     }
3168 
3169     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3170     mop = finalize_memop_asimd(s, a->sz);
3171     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3172     do_fp_st(s, a->rt, clean_addr, mop);
3173     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3174     do_fp_st(s, a->rt2, clean_addr, mop);
3175     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3176     return true;
3177 }
3178 
3179 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
3180 {
3181     uint64_t offset = a->imm << a->sz;
3182     TCGv_i64 clean_addr, dirty_addr;
3183     MemOp mop;
3184 
3185     if (!fp_access_check(s)) {
3186         return true;
3187     }
3188 
3189     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3190     mop = finalize_memop_asimd(s, a->sz);
3191     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3192     do_fp_ld(s, a->rt, clean_addr, mop);
3193     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3194     do_fp_ld(s, a->rt2, clean_addr, mop);
3195     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3196     return true;
3197 }
3198 
3199 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3200 {
3201     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3202     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3203     MemOp mop;
3204     TCGv_i128 tmp;
3205 
3206     /* STGP only comes in one size. */
3207     tcg_debug_assert(a->sz == MO_64);
3208 
3209     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3210         return false;
3211     }
3212 
3213     if (a->rn == 31) {
3214         gen_check_sp_alignment(s);
3215     }
3216 
3217     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3218     if (!a->p) {
3219         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3220     }
3221 
3222     clean_addr = clean_data_tbi(s, dirty_addr);
3223     tcg_rt = cpu_reg(s, a->rt);
3224     tcg_rt2 = cpu_reg(s, a->rt2);
3225 
3226     /*
3227      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3228      * and one tag operation.  We implement it as one single aligned 16-byte
3229      * memory operation for convenience.  Note that the alignment ensures
3230      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3231      */
3232     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3233 
3234     tmp = tcg_temp_new_i128();
3235     if (s->be_data == MO_LE) {
3236         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3237     } else {
3238         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3239     }
3240     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3241 
3242     /* Perform the tag store, if tag access enabled. */
3243     if (s->ata[0]) {
3244         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3245             gen_helper_stg_parallel(tcg_env, dirty_addr, dirty_addr);
3246         } else {
3247             gen_helper_stg(tcg_env, dirty_addr, dirty_addr);
3248         }
3249     }
3250 
3251     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3252     return true;
3253 }
3254 
3255 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3256                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3257                                  uint64_t offset, bool is_store, MemOp mop)
3258 {
3259     int memidx;
3260 
3261     if (a->rn == 31) {
3262         gen_check_sp_alignment(s);
3263     }
3264 
3265     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3266     if (!a->p) {
3267         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3268     }
3269     memidx = get_a64_user_mem_index(s, a->unpriv);
3270     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3271                                         a->w || a->rn != 31,
3272                                         mop, a->unpriv, memidx);
3273 }
3274 
3275 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3276                                   TCGv_i64 dirty_addr, uint64_t offset)
3277 {
3278     if (a->w) {
3279         if (a->p) {
3280             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3281         }
3282         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3283     }
3284 }
3285 
3286 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3287 {
3288     bool iss_sf, iss_valid = !a->w;
3289     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3290     int memidx = get_a64_user_mem_index(s, a->unpriv);
3291     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3292 
3293     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3294 
3295     tcg_rt = cpu_reg(s, a->rt);
3296     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3297 
3298     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3299                      iss_valid, a->rt, iss_sf, false);
3300     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3301     return true;
3302 }
3303 
3304 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3305 {
3306     bool iss_sf, iss_valid = !a->w;
3307     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3308     int memidx = get_a64_user_mem_index(s, a->unpriv);
3309     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3310 
3311     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3312 
3313     tcg_rt = cpu_reg(s, a->rt);
3314     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3315 
3316     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3317                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3318     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3319     return true;
3320 }
3321 
3322 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3323 {
3324     TCGv_i64 clean_addr, dirty_addr;
3325     MemOp mop;
3326 
3327     if (!fp_access_check(s)) {
3328         return true;
3329     }
3330     mop = finalize_memop_asimd(s, a->sz);
3331     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3332     do_fp_st(s, a->rt, clean_addr, mop);
3333     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3334     return true;
3335 }
3336 
3337 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3338 {
3339     TCGv_i64 clean_addr, dirty_addr;
3340     MemOp mop;
3341 
3342     if (!fp_access_check(s)) {
3343         return true;
3344     }
3345     mop = finalize_memop_asimd(s, a->sz);
3346     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3347     do_fp_ld(s, a->rt, clean_addr, mop);
3348     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3349     return true;
3350 }
3351 
3352 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3353                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3354                              bool is_store, MemOp memop)
3355 {
3356     TCGv_i64 tcg_rm;
3357 
3358     if (a->rn == 31) {
3359         gen_check_sp_alignment(s);
3360     }
3361     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3362 
3363     tcg_rm = read_cpu_reg(s, a->rm, 1);
3364     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3365 
3366     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3367     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3368 }
3369 
3370 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3371 {
3372     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3373     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3374     MemOp memop;
3375 
3376     if (extract32(a->opt, 1, 1) == 0) {
3377         return false;
3378     }
3379 
3380     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3381     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3382     tcg_rt = cpu_reg(s, a->rt);
3383     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3384               a->ext, true, a->rt, iss_sf, false);
3385     return true;
3386 }
3387 
3388 static bool trans_STR(DisasContext *s, arg_ldst *a)
3389 {
3390     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3391     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3392     MemOp memop;
3393 
3394     if (extract32(a->opt, 1, 1) == 0) {
3395         return false;
3396     }
3397 
3398     memop = finalize_memop(s, a->sz);
3399     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3400     tcg_rt = cpu_reg(s, a->rt);
3401     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3402     return true;
3403 }
3404 
3405 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3406 {
3407     TCGv_i64 clean_addr, dirty_addr;
3408     MemOp memop;
3409 
3410     if (extract32(a->opt, 1, 1) == 0) {
3411         return false;
3412     }
3413 
3414     if (!fp_access_check(s)) {
3415         return true;
3416     }
3417 
3418     memop = finalize_memop_asimd(s, a->sz);
3419     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3420     do_fp_ld(s, a->rt, clean_addr, memop);
3421     return true;
3422 }
3423 
3424 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3425 {
3426     TCGv_i64 clean_addr, dirty_addr;
3427     MemOp memop;
3428 
3429     if (extract32(a->opt, 1, 1) == 0) {
3430         return false;
3431     }
3432 
3433     if (!fp_access_check(s)) {
3434         return true;
3435     }
3436 
3437     memop = finalize_memop_asimd(s, a->sz);
3438     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3439     do_fp_st(s, a->rt, clean_addr, memop);
3440     return true;
3441 }
3442 
3443 
3444 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3445                          int sign, bool invert)
3446 {
3447     MemOp mop = a->sz | sign;
3448     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3449 
3450     if (a->rn == 31) {
3451         gen_check_sp_alignment(s);
3452     }
3453     mop = check_atomic_align(s, a->rn, mop);
3454     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3455                                 a->rn != 31, mop);
3456     tcg_rs = read_cpu_reg(s, a->rs, true);
3457     tcg_rt = cpu_reg(s, a->rt);
3458     if (invert) {
3459         tcg_gen_not_i64(tcg_rs, tcg_rs);
3460     }
3461     /*
3462      * The tcg atomic primitives are all full barriers.  Therefore we
3463      * can ignore the Acquire and Release bits of this instruction.
3464      */
3465     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3466 
3467     if (mop & MO_SIGN) {
3468         switch (a->sz) {
3469         case MO_8:
3470             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3471             break;
3472         case MO_16:
3473             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3474             break;
3475         case MO_32:
3476             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3477             break;
3478         case MO_64:
3479             break;
3480         default:
3481             g_assert_not_reached();
3482         }
3483     }
3484     return true;
3485 }
3486 
3487 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3488 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3489 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3490 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3491 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3492 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3493 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3494 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3495 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3496 
3497 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3498 {
3499     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3500     TCGv_i64 clean_addr;
3501     MemOp mop;
3502 
3503     if (!dc_isar_feature(aa64_atomics, s) ||
3504         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3505         return false;
3506     }
3507     if (a->rn == 31) {
3508         gen_check_sp_alignment(s);
3509     }
3510     mop = check_atomic_align(s, a->rn, a->sz);
3511     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3512                                 a->rn != 31, mop);
3513     /*
3514      * LDAPR* are a special case because they are a simple load, not a
3515      * fetch-and-do-something op.
3516      * The architectural consistency requirements here are weaker than
3517      * full load-acquire (we only need "load-acquire processor consistent"),
3518      * but we choose to implement them as full LDAQ.
3519      */
3520     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3521               true, a->rt, iss_sf, true);
3522     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3523     return true;
3524 }
3525 
3526 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3527 {
3528     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3529     MemOp memop;
3530 
3531     /* Load with pointer authentication */
3532     if (!dc_isar_feature(aa64_pauth, s)) {
3533         return false;
3534     }
3535 
3536     if (a->rn == 31) {
3537         gen_check_sp_alignment(s);
3538     }
3539     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3540 
3541     if (s->pauth_active) {
3542         if (!a->m) {
3543             gen_helper_autda_combined(dirty_addr, tcg_env, dirty_addr,
3544                                       tcg_constant_i64(0));
3545         } else {
3546             gen_helper_autdb_combined(dirty_addr, tcg_env, dirty_addr,
3547                                       tcg_constant_i64(0));
3548         }
3549     }
3550 
3551     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3552 
3553     memop = finalize_memop(s, MO_64);
3554 
3555     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3556     clean_addr = gen_mte_check1(s, dirty_addr, false,
3557                                 a->w || a->rn != 31, memop);
3558 
3559     tcg_rt = cpu_reg(s, a->rt);
3560     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3561               /* extend */ false, /* iss_valid */ !a->w,
3562               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3563 
3564     if (a->w) {
3565         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3566     }
3567     return true;
3568 }
3569 
3570 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3571 {
3572     TCGv_i64 clean_addr, dirty_addr;
3573     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3574     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3575 
3576     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3577         return false;
3578     }
3579 
3580     if (a->rn == 31) {
3581         gen_check_sp_alignment(s);
3582     }
3583 
3584     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3585     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3586     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3587     clean_addr = clean_data_tbi(s, dirty_addr);
3588 
3589     /*
3590      * Load-AcquirePC semantics; we implement as the slightly more
3591      * restrictive Load-Acquire.
3592      */
3593     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3594               a->rt, iss_sf, true);
3595     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3596     return true;
3597 }
3598 
3599 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3600 {
3601     TCGv_i64 clean_addr, dirty_addr;
3602     MemOp mop = a->sz;
3603     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3604 
3605     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3606         return false;
3607     }
3608 
3609     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3610 
3611     if (a->rn == 31) {
3612         gen_check_sp_alignment(s);
3613     }
3614 
3615     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3616     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3617     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3618     clean_addr = clean_data_tbi(s, dirty_addr);
3619 
3620     /* Store-Release semantics */
3621     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3622     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3623     return true;
3624 }
3625 
3626 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3627 {
3628     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3629     MemOp endian, align, mop;
3630 
3631     int total;    /* total bytes */
3632     int elements; /* elements per vector */
3633     int r;
3634     int size = a->sz;
3635 
3636     if (!a->p && a->rm != 0) {
3637         /* For non-postindexed accesses the Rm field must be 0 */
3638         return false;
3639     }
3640     if (size == 3 && !a->q && a->selem != 1) {
3641         return false;
3642     }
3643     if (!fp_access_check(s)) {
3644         return true;
3645     }
3646 
3647     if (a->rn == 31) {
3648         gen_check_sp_alignment(s);
3649     }
3650 
3651     /* For our purposes, bytes are always little-endian.  */
3652     endian = s->be_data;
3653     if (size == 0) {
3654         endian = MO_LE;
3655     }
3656 
3657     total = a->rpt * a->selem * (a->q ? 16 : 8);
3658     tcg_rn = cpu_reg_sp(s, a->rn);
3659 
3660     /*
3661      * Issue the MTE check vs the logical repeat count, before we
3662      * promote consecutive little-endian elements below.
3663      */
3664     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3665                                 finalize_memop_asimd(s, size));
3666 
3667     /*
3668      * Consecutive little-endian elements from a single register
3669      * can be promoted to a larger little-endian operation.
3670      */
3671     align = MO_ALIGN;
3672     if (a->selem == 1 && endian == MO_LE) {
3673         align = pow2_align(size);
3674         size = 3;
3675     }
3676     if (!s->align_mem) {
3677         align = 0;
3678     }
3679     mop = endian | size | align;
3680 
3681     elements = (a->q ? 16 : 8) >> size;
3682     tcg_ebytes = tcg_constant_i64(1 << size);
3683     for (r = 0; r < a->rpt; r++) {
3684         int e;
3685         for (e = 0; e < elements; e++) {
3686             int xs;
3687             for (xs = 0; xs < a->selem; xs++) {
3688                 int tt = (a->rt + r + xs) % 32;
3689                 do_vec_ld(s, tt, e, clean_addr, mop);
3690                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3691             }
3692         }
3693     }
3694 
3695     /*
3696      * For non-quad operations, setting a slice of the low 64 bits of
3697      * the register clears the high 64 bits (in the ARM ARM pseudocode
3698      * this is implicit in the fact that 'rval' is a 64 bit wide
3699      * variable).  For quad operations, we might still need to zero
3700      * the high bits of SVE.
3701      */
3702     for (r = 0; r < a->rpt * a->selem; r++) {
3703         int tt = (a->rt + r) % 32;
3704         clear_vec_high(s, a->q, tt);
3705     }
3706 
3707     if (a->p) {
3708         if (a->rm == 31) {
3709             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3710         } else {
3711             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3712         }
3713     }
3714     return true;
3715 }
3716 
3717 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3718 {
3719     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3720     MemOp endian, align, mop;
3721 
3722     int total;    /* total bytes */
3723     int elements; /* elements per vector */
3724     int r;
3725     int size = a->sz;
3726 
3727     if (!a->p && a->rm != 0) {
3728         /* For non-postindexed accesses the Rm field must be 0 */
3729         return false;
3730     }
3731     if (size == 3 && !a->q && a->selem != 1) {
3732         return false;
3733     }
3734     if (!fp_access_check(s)) {
3735         return true;
3736     }
3737 
3738     if (a->rn == 31) {
3739         gen_check_sp_alignment(s);
3740     }
3741 
3742     /* For our purposes, bytes are always little-endian.  */
3743     endian = s->be_data;
3744     if (size == 0) {
3745         endian = MO_LE;
3746     }
3747 
3748     total = a->rpt * a->selem * (a->q ? 16 : 8);
3749     tcg_rn = cpu_reg_sp(s, a->rn);
3750 
3751     /*
3752      * Issue the MTE check vs the logical repeat count, before we
3753      * promote consecutive little-endian elements below.
3754      */
3755     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3756                                 finalize_memop_asimd(s, size));
3757 
3758     /*
3759      * Consecutive little-endian elements from a single register
3760      * can be promoted to a larger little-endian operation.
3761      */
3762     align = MO_ALIGN;
3763     if (a->selem == 1 && endian == MO_LE) {
3764         align = pow2_align(size);
3765         size = 3;
3766     }
3767     if (!s->align_mem) {
3768         align = 0;
3769     }
3770     mop = endian | size | align;
3771 
3772     elements = (a->q ? 16 : 8) >> size;
3773     tcg_ebytes = tcg_constant_i64(1 << size);
3774     for (r = 0; r < a->rpt; r++) {
3775         int e;
3776         for (e = 0; e < elements; e++) {
3777             int xs;
3778             for (xs = 0; xs < a->selem; xs++) {
3779                 int tt = (a->rt + r + xs) % 32;
3780                 do_vec_st(s, tt, e, clean_addr, mop);
3781                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3782             }
3783         }
3784     }
3785 
3786     if (a->p) {
3787         if (a->rm == 31) {
3788             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3789         } else {
3790             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3791         }
3792     }
3793     return true;
3794 }
3795 
3796 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3797 {
3798     int xs, total, rt;
3799     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3800     MemOp mop;
3801 
3802     if (!a->p && a->rm != 0) {
3803         return false;
3804     }
3805     if (!fp_access_check(s)) {
3806         return true;
3807     }
3808 
3809     if (a->rn == 31) {
3810         gen_check_sp_alignment(s);
3811     }
3812 
3813     total = a->selem << a->scale;
3814     tcg_rn = cpu_reg_sp(s, a->rn);
3815 
3816     mop = finalize_memop_asimd(s, a->scale);
3817     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3818                                 total, mop);
3819 
3820     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3821     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3822         do_vec_st(s, rt, a->index, clean_addr, mop);
3823         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3824     }
3825 
3826     if (a->p) {
3827         if (a->rm == 31) {
3828             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3829         } else {
3830             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3831         }
3832     }
3833     return true;
3834 }
3835 
3836 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3837 {
3838     int xs, total, rt;
3839     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3840     MemOp mop;
3841 
3842     if (!a->p && a->rm != 0) {
3843         return false;
3844     }
3845     if (!fp_access_check(s)) {
3846         return true;
3847     }
3848 
3849     if (a->rn == 31) {
3850         gen_check_sp_alignment(s);
3851     }
3852 
3853     total = a->selem << a->scale;
3854     tcg_rn = cpu_reg_sp(s, a->rn);
3855 
3856     mop = finalize_memop_asimd(s, a->scale);
3857     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3858                                 total, mop);
3859 
3860     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3861     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3862         do_vec_ld(s, rt, a->index, clean_addr, mop);
3863         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3864     }
3865 
3866     if (a->p) {
3867         if (a->rm == 31) {
3868             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3869         } else {
3870             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3871         }
3872     }
3873     return true;
3874 }
3875 
3876 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3877 {
3878     int xs, total, rt;
3879     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3880     MemOp mop;
3881 
3882     if (!a->p && a->rm != 0) {
3883         return false;
3884     }
3885     if (!fp_access_check(s)) {
3886         return true;
3887     }
3888 
3889     if (a->rn == 31) {
3890         gen_check_sp_alignment(s);
3891     }
3892 
3893     total = a->selem << a->scale;
3894     tcg_rn = cpu_reg_sp(s, a->rn);
3895 
3896     mop = finalize_memop_asimd(s, a->scale);
3897     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3898                                 total, mop);
3899 
3900     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3901     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3902         /* Load and replicate to all elements */
3903         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3904 
3905         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3906         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3907                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3908         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3909     }
3910 
3911     if (a->p) {
3912         if (a->rm == 31) {
3913             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3914         } else {
3915             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3916         }
3917     }
3918     return true;
3919 }
3920 
3921 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3922 {
3923     TCGv_i64 addr, clean_addr, tcg_rt;
3924     int size = 4 << s->dcz_blocksize;
3925 
3926     if (!dc_isar_feature(aa64_mte, s)) {
3927         return false;
3928     }
3929     if (s->current_el == 0) {
3930         return false;
3931     }
3932 
3933     if (a->rn == 31) {
3934         gen_check_sp_alignment(s);
3935     }
3936 
3937     addr = read_cpu_reg_sp(s, a->rn, true);
3938     tcg_gen_addi_i64(addr, addr, a->imm);
3939     tcg_rt = cpu_reg(s, a->rt);
3940 
3941     if (s->ata[0]) {
3942         gen_helper_stzgm_tags(tcg_env, addr, tcg_rt);
3943     }
3944     /*
3945      * The non-tags portion of STZGM is mostly like DC_ZVA,
3946      * except the alignment happens before the access.
3947      */
3948     clean_addr = clean_data_tbi(s, addr);
3949     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3950     gen_helper_dc_zva(tcg_env, clean_addr);
3951     return true;
3952 }
3953 
3954 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3955 {
3956     TCGv_i64 addr, clean_addr, tcg_rt;
3957 
3958     if (!dc_isar_feature(aa64_mte, s)) {
3959         return false;
3960     }
3961     if (s->current_el == 0) {
3962         return false;
3963     }
3964 
3965     if (a->rn == 31) {
3966         gen_check_sp_alignment(s);
3967     }
3968 
3969     addr = read_cpu_reg_sp(s, a->rn, true);
3970     tcg_gen_addi_i64(addr, addr, a->imm);
3971     tcg_rt = cpu_reg(s, a->rt);
3972 
3973     if (s->ata[0]) {
3974         gen_helper_stgm(tcg_env, addr, tcg_rt);
3975     } else {
3976         MMUAccessType acc = MMU_DATA_STORE;
3977         int size = 4 << s->gm_blocksize;
3978 
3979         clean_addr = clean_data_tbi(s, addr);
3980         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3981         gen_probe_access(s, clean_addr, acc, size);
3982     }
3983     return true;
3984 }
3985 
3986 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3987 {
3988     TCGv_i64 addr, clean_addr, tcg_rt;
3989 
3990     if (!dc_isar_feature(aa64_mte, s)) {
3991         return false;
3992     }
3993     if (s->current_el == 0) {
3994         return false;
3995     }
3996 
3997     if (a->rn == 31) {
3998         gen_check_sp_alignment(s);
3999     }
4000 
4001     addr = read_cpu_reg_sp(s, a->rn, true);
4002     tcg_gen_addi_i64(addr, addr, a->imm);
4003     tcg_rt = cpu_reg(s, a->rt);
4004 
4005     if (s->ata[0]) {
4006         gen_helper_ldgm(tcg_rt, tcg_env, addr);
4007     } else {
4008         MMUAccessType acc = MMU_DATA_LOAD;
4009         int size = 4 << s->gm_blocksize;
4010 
4011         clean_addr = clean_data_tbi(s, addr);
4012         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4013         gen_probe_access(s, clean_addr, acc, size);
4014         /* The result tags are zeros.  */
4015         tcg_gen_movi_i64(tcg_rt, 0);
4016     }
4017     return true;
4018 }
4019 
4020 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
4021 {
4022     TCGv_i64 addr, clean_addr, tcg_rt;
4023 
4024     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
4025         return false;
4026     }
4027 
4028     if (a->rn == 31) {
4029         gen_check_sp_alignment(s);
4030     }
4031 
4032     addr = read_cpu_reg_sp(s, a->rn, true);
4033     if (!a->p) {
4034         /* pre-index or signed offset */
4035         tcg_gen_addi_i64(addr, addr, a->imm);
4036     }
4037 
4038     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4039     tcg_rt = cpu_reg(s, a->rt);
4040     if (s->ata[0]) {
4041         gen_helper_ldg(tcg_rt, tcg_env, addr, tcg_rt);
4042     } else {
4043         /*
4044          * Tag access disabled: we must check for aborts on the load
4045          * load from [rn+offset], and then insert a 0 tag into rt.
4046          */
4047         clean_addr = clean_data_tbi(s, addr);
4048         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4049         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4050     }
4051 
4052     if (a->w) {
4053         /* pre-index or post-index */
4054         if (a->p) {
4055             /* post-index */
4056             tcg_gen_addi_i64(addr, addr, a->imm);
4057         }
4058         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
4059     }
4060     return true;
4061 }
4062 
4063 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
4064 {
4065     TCGv_i64 addr, tcg_rt;
4066 
4067     if (a->rn == 31) {
4068         gen_check_sp_alignment(s);
4069     }
4070 
4071     addr = read_cpu_reg_sp(s, a->rn, true);
4072     if (!a->p) {
4073         /* pre-index or signed offset */
4074         tcg_gen_addi_i64(addr, addr, a->imm);
4075     }
4076     tcg_rt = cpu_reg_sp(s, a->rt);
4077     if (!s->ata[0]) {
4078         /*
4079          * For STG and ST2G, we need to check alignment and probe memory.
4080          * TODO: For STZG and STZ2G, we could rely on the stores below,
4081          * at least for system mode; user-only won't enforce alignment.
4082          */
4083         if (is_pair) {
4084             gen_helper_st2g_stub(tcg_env, addr);
4085         } else {
4086             gen_helper_stg_stub(tcg_env, addr);
4087         }
4088     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4089         if (is_pair) {
4090             gen_helper_st2g_parallel(tcg_env, addr, tcg_rt);
4091         } else {
4092             gen_helper_stg_parallel(tcg_env, addr, tcg_rt);
4093         }
4094     } else {
4095         if (is_pair) {
4096             gen_helper_st2g(tcg_env, addr, tcg_rt);
4097         } else {
4098             gen_helper_stg(tcg_env, addr, tcg_rt);
4099         }
4100     }
4101 
4102     if (is_zero) {
4103         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4104         TCGv_i64 zero64 = tcg_constant_i64(0);
4105         TCGv_i128 zero128 = tcg_temp_new_i128();
4106         int mem_index = get_mem_index(s);
4107         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4108 
4109         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4110 
4111         /* This is 1 or 2 atomic 16-byte operations. */
4112         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4113         if (is_pair) {
4114             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4115             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4116         }
4117     }
4118 
4119     if (a->w) {
4120         /* pre-index or post-index */
4121         if (a->p) {
4122             /* post-index */
4123             tcg_gen_addi_i64(addr, addr, a->imm);
4124         }
4125         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
4126     }
4127     return true;
4128 }
4129 
4130 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
4131 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
4132 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
4133 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
4134 
4135 typedef void SetFn(TCGv_env, TCGv_i32, TCGv_i32);
4136 
4137 static bool do_SET(DisasContext *s, arg_set *a, bool is_epilogue,
4138                    bool is_setg, SetFn fn)
4139 {
4140     int memidx;
4141     uint32_t syndrome, desc = 0;
4142 
4143     if (is_setg && !dc_isar_feature(aa64_mte, s)) {
4144         return false;
4145     }
4146 
4147     /*
4148      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4149      * us to pull this check before the CheckMOPSEnabled() test
4150      * (which we do in the helper function)
4151      */
4152     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4153         a->rd == 31 || a->rn == 31) {
4154         return false;
4155     }
4156 
4157     memidx = get_a64_user_mem_index(s, a->unpriv);
4158 
4159     /*
4160      * We pass option_a == true, matching our implementation;
4161      * we pass wrong_option == false: helper function may set that bit.
4162      */
4163     syndrome = syn_mop(true, is_setg, (a->nontemp << 1) | a->unpriv,
4164                        is_epilogue, false, true, a->rd, a->rs, a->rn);
4165 
4166     if (is_setg ? s->ata[a->unpriv] : s->mte_active[a->unpriv]) {
4167         /* We may need to do MTE tag checking, so assemble the descriptor */
4168         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
4169         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
4170         desc = FIELD_DP32(desc, MTEDESC, WRITE, true);
4171         /* SIZEM1 and ALIGN we leave 0 (byte write) */
4172     }
4173     /* The helper function always needs the memidx even with MTE disabled */
4174     desc = FIELD_DP32(desc, MTEDESC, MIDX, memidx);
4175 
4176     /*
4177      * The helper needs the register numbers, but since they're in
4178      * the syndrome anyway, we let it extract them from there rather
4179      * than passing in an extra three integer arguments.
4180      */
4181     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(desc));
4182     return true;
4183 }
4184 
4185 TRANS_FEAT(SETP, aa64_mops, do_SET, a, false, false, gen_helper_setp)
4186 TRANS_FEAT(SETM, aa64_mops, do_SET, a, false, false, gen_helper_setm)
4187 TRANS_FEAT(SETE, aa64_mops, do_SET, a, true, false, gen_helper_sete)
4188 TRANS_FEAT(SETGP, aa64_mops, do_SET, a, false, true, gen_helper_setgp)
4189 TRANS_FEAT(SETGM, aa64_mops, do_SET, a, false, true, gen_helper_setgm)
4190 TRANS_FEAT(SETGE, aa64_mops, do_SET, a, true, true, gen_helper_setge)
4191 
4192 typedef void CpyFn(TCGv_env, TCGv_i32, TCGv_i32, TCGv_i32);
4193 
4194 static bool do_CPY(DisasContext *s, arg_cpy *a, bool is_epilogue, CpyFn fn)
4195 {
4196     int rmemidx, wmemidx;
4197     uint32_t syndrome, rdesc = 0, wdesc = 0;
4198     bool wunpriv = extract32(a->options, 0, 1);
4199     bool runpriv = extract32(a->options, 1, 1);
4200 
4201     /*
4202      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4203      * us to pull this check before the CheckMOPSEnabled() test
4204      * (which we do in the helper function)
4205      */
4206     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4207         a->rd == 31 || a->rs == 31 || a->rn == 31) {
4208         return false;
4209     }
4210 
4211     rmemidx = get_a64_user_mem_index(s, runpriv);
4212     wmemidx = get_a64_user_mem_index(s, wunpriv);
4213 
4214     /*
4215      * We pass option_a == true, matching our implementation;
4216      * we pass wrong_option == false: helper function may set that bit.
4217      */
4218     syndrome = syn_mop(false, false, a->options, is_epilogue,
4219                        false, true, a->rd, a->rs, a->rn);
4220 
4221     /* If we need to do MTE tag checking, assemble the descriptors */
4222     if (s->mte_active[runpriv]) {
4223         rdesc = FIELD_DP32(rdesc, MTEDESC, TBI, s->tbid);
4224         rdesc = FIELD_DP32(rdesc, MTEDESC, TCMA, s->tcma);
4225     }
4226     if (s->mte_active[wunpriv]) {
4227         wdesc = FIELD_DP32(wdesc, MTEDESC, TBI, s->tbid);
4228         wdesc = FIELD_DP32(wdesc, MTEDESC, TCMA, s->tcma);
4229         wdesc = FIELD_DP32(wdesc, MTEDESC, WRITE, true);
4230     }
4231     /* The helper function needs these parts of the descriptor regardless */
4232     rdesc = FIELD_DP32(rdesc, MTEDESC, MIDX, rmemidx);
4233     wdesc = FIELD_DP32(wdesc, MTEDESC, MIDX, wmemidx);
4234 
4235     /*
4236      * The helper needs the register numbers, but since they're in
4237      * the syndrome anyway, we let it extract them from there rather
4238      * than passing in an extra three integer arguments.
4239      */
4240     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(wdesc),
4241        tcg_constant_i32(rdesc));
4242     return true;
4243 }
4244 
4245 TRANS_FEAT(CPYP, aa64_mops, do_CPY, a, false, gen_helper_cpyp)
4246 TRANS_FEAT(CPYM, aa64_mops, do_CPY, a, false, gen_helper_cpym)
4247 TRANS_FEAT(CPYE, aa64_mops, do_CPY, a, true, gen_helper_cpye)
4248 TRANS_FEAT(CPYFP, aa64_mops, do_CPY, a, false, gen_helper_cpyfp)
4249 TRANS_FEAT(CPYFM, aa64_mops, do_CPY, a, false, gen_helper_cpyfm)
4250 TRANS_FEAT(CPYFE, aa64_mops, do_CPY, a, true, gen_helper_cpyfe)
4251 
4252 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4253 
4254 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4255                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4256 {
4257     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4258     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4259     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4260 
4261     fn(tcg_rd, tcg_rn, tcg_imm);
4262     if (!a->sf) {
4263         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4264     }
4265     return true;
4266 }
4267 
4268 /*
4269  * PC-rel. addressing
4270  */
4271 
4272 static bool trans_ADR(DisasContext *s, arg_ri *a)
4273 {
4274     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4275     return true;
4276 }
4277 
4278 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4279 {
4280     int64_t offset = (int64_t)a->imm << 12;
4281 
4282     /* The page offset is ok for CF_PCREL. */
4283     offset -= s->pc_curr & 0xfff;
4284     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4285     return true;
4286 }
4287 
4288 /*
4289  * Add/subtract (immediate)
4290  */
4291 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4292 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4293 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4294 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4295 
4296 /*
4297  * Add/subtract (immediate, with tags)
4298  */
4299 
4300 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4301                                       bool sub_op)
4302 {
4303     TCGv_i64 tcg_rn, tcg_rd;
4304     int imm;
4305 
4306     imm = a->uimm6 << LOG2_TAG_GRANULE;
4307     if (sub_op) {
4308         imm = -imm;
4309     }
4310 
4311     tcg_rn = cpu_reg_sp(s, a->rn);
4312     tcg_rd = cpu_reg_sp(s, a->rd);
4313 
4314     if (s->ata[0]) {
4315         gen_helper_addsubg(tcg_rd, tcg_env, tcg_rn,
4316                            tcg_constant_i32(imm),
4317                            tcg_constant_i32(a->uimm4));
4318     } else {
4319         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4320         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4321     }
4322     return true;
4323 }
4324 
4325 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4326 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4327 
4328 /* The input should be a value in the bottom e bits (with higher
4329  * bits zero); returns that value replicated into every element
4330  * of size e in a 64 bit integer.
4331  */
4332 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4333 {
4334     assert(e != 0);
4335     while (e < 64) {
4336         mask |= mask << e;
4337         e *= 2;
4338     }
4339     return mask;
4340 }
4341 
4342 /*
4343  * Logical (immediate)
4344  */
4345 
4346 /*
4347  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4348  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4349  * value (ie should cause a guest UNDEF exception), and true if they are
4350  * valid, in which case the decoded bit pattern is written to result.
4351  */
4352 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4353                             unsigned int imms, unsigned int immr)
4354 {
4355     uint64_t mask;
4356     unsigned e, levels, s, r;
4357     int len;
4358 
4359     assert(immn < 2 && imms < 64 && immr < 64);
4360 
4361     /* The bit patterns we create here are 64 bit patterns which
4362      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4363      * 64 bits each. Each element contains the same value: a run
4364      * of between 1 and e-1 non-zero bits, rotated within the
4365      * element by between 0 and e-1 bits.
4366      *
4367      * The element size and run length are encoded into immn (1 bit)
4368      * and imms (6 bits) as follows:
4369      * 64 bit elements: immn = 1, imms = <length of run - 1>
4370      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4371      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4372      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4373      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4374      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4375      * Notice that immn = 0, imms = 11111x is the only combination
4376      * not covered by one of the above options; this is reserved.
4377      * Further, <length of run - 1> all-ones is a reserved pattern.
4378      *
4379      * In all cases the rotation is by immr % e (and immr is 6 bits).
4380      */
4381 
4382     /* First determine the element size */
4383     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4384     if (len < 1) {
4385         /* This is the immn == 0, imms == 0x11111x case */
4386         return false;
4387     }
4388     e = 1 << len;
4389 
4390     levels = e - 1;
4391     s = imms & levels;
4392     r = immr & levels;
4393 
4394     if (s == levels) {
4395         /* <length of run - 1> mustn't be all-ones. */
4396         return false;
4397     }
4398 
4399     /* Create the value of one element: s+1 set bits rotated
4400      * by r within the element (which is e bits wide)...
4401      */
4402     mask = MAKE_64BIT_MASK(0, s + 1);
4403     if (r) {
4404         mask = (mask >> r) | (mask << (e - r));
4405         mask &= MAKE_64BIT_MASK(0, e);
4406     }
4407     /* ...then replicate the element over the whole 64 bit value */
4408     mask = bitfield_replicate(mask, e);
4409     *result = mask;
4410     return true;
4411 }
4412 
4413 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4414                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4415 {
4416     TCGv_i64 tcg_rd, tcg_rn;
4417     uint64_t imm;
4418 
4419     /* Some immediate field values are reserved. */
4420     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4421                                 extract32(a->dbm, 0, 6),
4422                                 extract32(a->dbm, 6, 6))) {
4423         return false;
4424     }
4425     if (!a->sf) {
4426         imm &= 0xffffffffull;
4427     }
4428 
4429     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4430     tcg_rn = cpu_reg(s, a->rn);
4431 
4432     fn(tcg_rd, tcg_rn, imm);
4433     if (set_cc) {
4434         gen_logic_CC(a->sf, tcg_rd);
4435     }
4436     if (!a->sf) {
4437         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4438     }
4439     return true;
4440 }
4441 
4442 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4443 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4444 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4445 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4446 
4447 /*
4448  * Move wide (immediate)
4449  */
4450 
4451 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4452 {
4453     int pos = a->hw << 4;
4454     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4455     return true;
4456 }
4457 
4458 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4459 {
4460     int pos = a->hw << 4;
4461     uint64_t imm = a->imm;
4462 
4463     imm = ~(imm << pos);
4464     if (!a->sf) {
4465         imm = (uint32_t)imm;
4466     }
4467     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4468     return true;
4469 }
4470 
4471 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4472 {
4473     int pos = a->hw << 4;
4474     TCGv_i64 tcg_rd, tcg_im;
4475 
4476     tcg_rd = cpu_reg(s, a->rd);
4477     tcg_im = tcg_constant_i64(a->imm);
4478     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4479     if (!a->sf) {
4480         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4481     }
4482     return true;
4483 }
4484 
4485 /*
4486  * Bitfield
4487  */
4488 
4489 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4490 {
4491     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4492     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4493     unsigned int bitsize = a->sf ? 64 : 32;
4494     unsigned int ri = a->immr;
4495     unsigned int si = a->imms;
4496     unsigned int pos, len;
4497 
4498     if (si >= ri) {
4499         /* Wd<s-r:0> = Wn<s:r> */
4500         len = (si - ri) + 1;
4501         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4502         if (!a->sf) {
4503             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4504         }
4505     } else {
4506         /* Wd<32+s-r,32-r> = Wn<s:0> */
4507         len = si + 1;
4508         pos = (bitsize - ri) & (bitsize - 1);
4509 
4510         if (len < ri) {
4511             /*
4512              * Sign extend the destination field from len to fill the
4513              * balance of the word.  Let the deposit below insert all
4514              * of those sign bits.
4515              */
4516             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4517             len = ri;
4518         }
4519 
4520         /*
4521          * We start with zero, and we haven't modified any bits outside
4522          * bitsize, therefore no final zero-extension is unneeded for !sf.
4523          */
4524         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4525     }
4526     return true;
4527 }
4528 
4529 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4530 {
4531     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4532     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4533     unsigned int bitsize = a->sf ? 64 : 32;
4534     unsigned int ri = a->immr;
4535     unsigned int si = a->imms;
4536     unsigned int pos, len;
4537 
4538     tcg_rd = cpu_reg(s, a->rd);
4539     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4540 
4541     if (si >= ri) {
4542         /* Wd<s-r:0> = Wn<s:r> */
4543         len = (si - ri) + 1;
4544         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4545     } else {
4546         /* Wd<32+s-r,32-r> = Wn<s:0> */
4547         len = si + 1;
4548         pos = (bitsize - ri) & (bitsize - 1);
4549         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4550     }
4551     return true;
4552 }
4553 
4554 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4555 {
4556     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4557     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4558     unsigned int bitsize = a->sf ? 64 : 32;
4559     unsigned int ri = a->immr;
4560     unsigned int si = a->imms;
4561     unsigned int pos, len;
4562 
4563     tcg_rd = cpu_reg(s, a->rd);
4564     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4565 
4566     if (si >= ri) {
4567         /* Wd<s-r:0> = Wn<s:r> */
4568         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4569         len = (si - ri) + 1;
4570         pos = 0;
4571     } else {
4572         /* Wd<32+s-r,32-r> = Wn<s:0> */
4573         len = si + 1;
4574         pos = (bitsize - ri) & (bitsize - 1);
4575     }
4576 
4577     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4578     if (!a->sf) {
4579         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4580     }
4581     return true;
4582 }
4583 
4584 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4585 {
4586     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4587 
4588     tcg_rd = cpu_reg(s, a->rd);
4589 
4590     if (unlikely(a->imm == 0)) {
4591         /*
4592          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4593          * so an extract from bit 0 is a special case.
4594          */
4595         if (a->sf) {
4596             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4597         } else {
4598             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4599         }
4600     } else {
4601         tcg_rm = cpu_reg(s, a->rm);
4602         tcg_rn = cpu_reg(s, a->rn);
4603 
4604         if (a->sf) {
4605             /* Specialization to ROR happens in EXTRACT2.  */
4606             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4607         } else {
4608             TCGv_i32 t0 = tcg_temp_new_i32();
4609 
4610             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4611             if (a->rm == a->rn) {
4612                 tcg_gen_rotri_i32(t0, t0, a->imm);
4613             } else {
4614                 TCGv_i32 t1 = tcg_temp_new_i32();
4615                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4616                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4617             }
4618             tcg_gen_extu_i32_i64(tcg_rd, t0);
4619         }
4620     }
4621     return true;
4622 }
4623 
4624 /*
4625  * Cryptographic AES, SHA, SHA512
4626  */
4627 
4628 TRANS_FEAT(AESE, aa64_aes, do_gvec_op3_ool, a, 0, gen_helper_crypto_aese)
4629 TRANS_FEAT(AESD, aa64_aes, do_gvec_op3_ool, a, 0, gen_helper_crypto_aesd)
4630 TRANS_FEAT(AESMC, aa64_aes, do_gvec_op2_ool, a, 0, gen_helper_crypto_aesmc)
4631 TRANS_FEAT(AESIMC, aa64_aes, do_gvec_op2_ool, a, 0, gen_helper_crypto_aesimc)
4632 
4633 TRANS_FEAT(SHA1C, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1c)
4634 TRANS_FEAT(SHA1P, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1p)
4635 TRANS_FEAT(SHA1M, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1m)
4636 TRANS_FEAT(SHA1SU0, aa64_sha1, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha1su0)
4637 
4638 TRANS_FEAT(SHA256H, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256h)
4639 TRANS_FEAT(SHA256H2, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256h2)
4640 TRANS_FEAT(SHA256SU1, aa64_sha256, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha256su1)
4641 
4642 TRANS_FEAT(SHA1H, aa64_sha1, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha1h)
4643 TRANS_FEAT(SHA1SU1, aa64_sha1, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha1su1)
4644 TRANS_FEAT(SHA256SU0, aa64_sha256, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha256su0)
4645 
4646 TRANS_FEAT(SHA512H, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512h)
4647 TRANS_FEAT(SHA512H2, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512h2)
4648 TRANS_FEAT(SHA512SU1, aa64_sha512, do_gvec_op3_ool, a, 0, gen_helper_crypto_sha512su1)
4649 TRANS_FEAT(RAX1, aa64_sha3, do_gvec_fn3, a, gen_gvec_rax1)
4650 TRANS_FEAT(SM3PARTW1, aa64_sm3, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm3partw1)
4651 TRANS_FEAT(SM3PARTW2, aa64_sm3, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm3partw2)
4652 TRANS_FEAT(SM4EKEY, aa64_sm4, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm4ekey)
4653 
4654 TRANS_FEAT(SHA512SU0, aa64_sha512, do_gvec_op2_ool, a, 0, gen_helper_crypto_sha512su0)
4655 TRANS_FEAT(SM4E, aa64_sm4, do_gvec_op3_ool, a, 0, gen_helper_crypto_sm4e)
4656 
4657 TRANS_FEAT(EOR3, aa64_sha3, do_gvec_fn4, a, gen_gvec_eor3)
4658 TRANS_FEAT(BCAX, aa64_sha3, do_gvec_fn4, a, gen_gvec_bcax)
4659 
4660 static bool trans_SM3SS1(DisasContext *s, arg_SM3SS1 *a)
4661 {
4662     if (!dc_isar_feature(aa64_sm3, s)) {
4663         return false;
4664     }
4665     if (fp_access_check(s)) {
4666         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
4667         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
4668         TCGv_i32 tcg_op3 = tcg_temp_new_i32();
4669         TCGv_i32 tcg_res = tcg_temp_new_i32();
4670         unsigned vsz, dofs;
4671 
4672         read_vec_element_i32(s, tcg_op1, a->rn, 3, MO_32);
4673         read_vec_element_i32(s, tcg_op2, a->rm, 3, MO_32);
4674         read_vec_element_i32(s, tcg_op3, a->ra, 3, MO_32);
4675 
4676         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
4677         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
4678         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
4679         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
4680 
4681         /* Clear the whole register first, then store bits [127:96]. */
4682         vsz = vec_full_reg_size(s);
4683         dofs = vec_full_reg_offset(s, a->rd);
4684         tcg_gen_gvec_dup_imm(MO_64, dofs, vsz, vsz, 0);
4685         write_vec_element_i32(s, tcg_res, a->rd, 3, MO_32);
4686     }
4687     return true;
4688 }
4689 
4690 static bool do_crypto3i(DisasContext *s, arg_crypto3i *a, gen_helper_gvec_3 *fn)
4691 {
4692     if (fp_access_check(s)) {
4693         gen_gvec_op3_ool(s, true, a->rd, a->rn, a->rm, a->imm, fn);
4694     }
4695     return true;
4696 }
4697 TRANS_FEAT(SM3TT1A, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt1a)
4698 TRANS_FEAT(SM3TT1B, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt1b)
4699 TRANS_FEAT(SM3TT2A, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt2a)
4700 TRANS_FEAT(SM3TT2B, aa64_sm3, do_crypto3i, a, gen_helper_crypto_sm3tt2b)
4701 
4702 static bool trans_XAR(DisasContext *s, arg_XAR *a)
4703 {
4704     if (!dc_isar_feature(aa64_sha3, s)) {
4705         return false;
4706     }
4707     if (fp_access_check(s)) {
4708         gen_gvec_xar(MO_64, vec_full_reg_offset(s, a->rd),
4709                      vec_full_reg_offset(s, a->rn),
4710                      vec_full_reg_offset(s, a->rm), a->imm, 16,
4711                      vec_full_reg_size(s));
4712     }
4713     return true;
4714 }
4715 
4716 /*
4717  * Advanced SIMD copy
4718  */
4719 
4720 static bool decode_esz_idx(int imm, MemOp *pesz, unsigned *pidx)
4721 {
4722     unsigned esz = ctz32(imm);
4723     if (esz <= MO_64) {
4724         *pesz = esz;
4725         *pidx = imm >> (esz + 1);
4726         return true;
4727     }
4728     return false;
4729 }
4730 
4731 static bool trans_DUP_element_s(DisasContext *s, arg_DUP_element_s *a)
4732 {
4733     MemOp esz;
4734     unsigned idx;
4735 
4736     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4737         return false;
4738     }
4739     if (fp_access_check(s)) {
4740         /*
4741          * This instruction just extracts the specified element and
4742          * zero-extends it into the bottom of the destination register.
4743          */
4744         TCGv_i64 tmp = tcg_temp_new_i64();
4745         read_vec_element(s, tmp, a->rn, idx, esz);
4746         write_fp_dreg(s, a->rd, tmp);
4747     }
4748     return true;
4749 }
4750 
4751 static bool trans_DUP_element_v(DisasContext *s, arg_DUP_element_v *a)
4752 {
4753     MemOp esz;
4754     unsigned idx;
4755 
4756     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4757         return false;
4758     }
4759     if (esz == MO_64 && !a->q) {
4760         return false;
4761     }
4762     if (fp_access_check(s)) {
4763         tcg_gen_gvec_dup_mem(esz, vec_full_reg_offset(s, a->rd),
4764                              vec_reg_offset(s, a->rn, idx, esz),
4765                              a->q ? 16 : 8, vec_full_reg_size(s));
4766     }
4767     return true;
4768 }
4769 
4770 static bool trans_DUP_general(DisasContext *s, arg_DUP_general *a)
4771 {
4772     MemOp esz;
4773     unsigned idx;
4774 
4775     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4776         return false;
4777     }
4778     if (esz == MO_64 && !a->q) {
4779         return false;
4780     }
4781     if (fp_access_check(s)) {
4782         tcg_gen_gvec_dup_i64(esz, vec_full_reg_offset(s, a->rd),
4783                              a->q ? 16 : 8, vec_full_reg_size(s),
4784                              cpu_reg(s, a->rn));
4785     }
4786     return true;
4787 }
4788 
4789 static bool do_smov_umov(DisasContext *s, arg_SMOV *a, MemOp is_signed)
4790 {
4791     MemOp esz;
4792     unsigned idx;
4793 
4794     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4795         return false;
4796     }
4797     if (is_signed) {
4798         if (esz == MO_64 || (esz == MO_32 && !a->q)) {
4799             return false;
4800         }
4801     } else {
4802         if (esz == MO_64 ? !a->q : a->q) {
4803             return false;
4804         }
4805     }
4806     if (fp_access_check(s)) {
4807         TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4808         read_vec_element(s, tcg_rd, a->rn, idx, esz | is_signed);
4809         if (is_signed && !a->q) {
4810             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4811         }
4812     }
4813     return true;
4814 }
4815 
4816 TRANS(SMOV, do_smov_umov, a, MO_SIGN)
4817 TRANS(UMOV, do_smov_umov, a, 0)
4818 
4819 static bool trans_INS_general(DisasContext *s, arg_INS_general *a)
4820 {
4821     MemOp esz;
4822     unsigned idx;
4823 
4824     if (!decode_esz_idx(a->imm, &esz, &idx)) {
4825         return false;
4826     }
4827     if (fp_access_check(s)) {
4828         write_vec_element(s, cpu_reg(s, a->rn), a->rd, idx, esz);
4829         clear_vec_high(s, true, a->rd);
4830     }
4831     return true;
4832 }
4833 
4834 static bool trans_INS_element(DisasContext *s, arg_INS_element *a)
4835 {
4836     MemOp esz;
4837     unsigned didx, sidx;
4838 
4839     if (!decode_esz_idx(a->di, &esz, &didx)) {
4840         return false;
4841     }
4842     sidx = a->si >> esz;
4843     if (fp_access_check(s)) {
4844         TCGv_i64 tmp = tcg_temp_new_i64();
4845 
4846         read_vec_element(s, tmp, a->rn, sidx, esz);
4847         write_vec_element(s, tmp, a->rd, didx, esz);
4848 
4849         /* INS is considered a 128-bit write for SVE. */
4850         clear_vec_high(s, true, a->rd);
4851     }
4852     return true;
4853 }
4854 
4855 /*
4856  * Advanced SIMD three same
4857  */
4858 
4859 typedef struct FPScalar {
4860     void (*gen_h)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
4861     void (*gen_s)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
4862     void (*gen_d)(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
4863 } FPScalar;
4864 
4865 static bool do_fp3_scalar(DisasContext *s, arg_rrr_e *a, const FPScalar *f)
4866 {
4867     switch (a->esz) {
4868     case MO_64:
4869         if (fp_access_check(s)) {
4870             TCGv_i64 t0 = read_fp_dreg(s, a->rn);
4871             TCGv_i64 t1 = read_fp_dreg(s, a->rm);
4872             f->gen_d(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
4873             write_fp_dreg(s, a->rd, t0);
4874         }
4875         break;
4876     case MO_32:
4877         if (fp_access_check(s)) {
4878             TCGv_i32 t0 = read_fp_sreg(s, a->rn);
4879             TCGv_i32 t1 = read_fp_sreg(s, a->rm);
4880             f->gen_s(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
4881             write_fp_sreg(s, a->rd, t0);
4882         }
4883         break;
4884     case MO_16:
4885         if (!dc_isar_feature(aa64_fp16, s)) {
4886             return false;
4887         }
4888         if (fp_access_check(s)) {
4889             TCGv_i32 t0 = read_fp_hreg(s, a->rn);
4890             TCGv_i32 t1 = read_fp_hreg(s, a->rm);
4891             f->gen_h(t0, t0, t1, fpstatus_ptr(FPST_FPCR_F16));
4892             write_fp_sreg(s, a->rd, t0);
4893         }
4894         break;
4895     default:
4896         return false;
4897     }
4898     return true;
4899 }
4900 
4901 static const FPScalar f_scalar_fadd = {
4902     gen_helper_vfp_addh,
4903     gen_helper_vfp_adds,
4904     gen_helper_vfp_addd,
4905 };
4906 TRANS(FADD_s, do_fp3_scalar, a, &f_scalar_fadd)
4907 
4908 static const FPScalar f_scalar_fsub = {
4909     gen_helper_vfp_subh,
4910     gen_helper_vfp_subs,
4911     gen_helper_vfp_subd,
4912 };
4913 TRANS(FSUB_s, do_fp3_scalar, a, &f_scalar_fsub)
4914 
4915 static const FPScalar f_scalar_fdiv = {
4916     gen_helper_vfp_divh,
4917     gen_helper_vfp_divs,
4918     gen_helper_vfp_divd,
4919 };
4920 TRANS(FDIV_s, do_fp3_scalar, a, &f_scalar_fdiv)
4921 
4922 static const FPScalar f_scalar_fmul = {
4923     gen_helper_vfp_mulh,
4924     gen_helper_vfp_muls,
4925     gen_helper_vfp_muld,
4926 };
4927 TRANS(FMUL_s, do_fp3_scalar, a, &f_scalar_fmul)
4928 
4929 static const FPScalar f_scalar_fmax = {
4930     gen_helper_advsimd_maxh,
4931     gen_helper_vfp_maxs,
4932     gen_helper_vfp_maxd,
4933 };
4934 TRANS(FMAX_s, do_fp3_scalar, a, &f_scalar_fmax)
4935 
4936 static const FPScalar f_scalar_fmin = {
4937     gen_helper_advsimd_minh,
4938     gen_helper_vfp_mins,
4939     gen_helper_vfp_mind,
4940 };
4941 TRANS(FMIN_s, do_fp3_scalar, a, &f_scalar_fmin)
4942 
4943 static const FPScalar f_scalar_fmaxnm = {
4944     gen_helper_advsimd_maxnumh,
4945     gen_helper_vfp_maxnums,
4946     gen_helper_vfp_maxnumd,
4947 };
4948 TRANS(FMAXNM_s, do_fp3_scalar, a, &f_scalar_fmaxnm)
4949 
4950 static const FPScalar f_scalar_fminnm = {
4951     gen_helper_advsimd_minnumh,
4952     gen_helper_vfp_minnums,
4953     gen_helper_vfp_minnumd,
4954 };
4955 TRANS(FMINNM_s, do_fp3_scalar, a, &f_scalar_fminnm)
4956 
4957 static const FPScalar f_scalar_fmulx = {
4958     gen_helper_advsimd_mulxh,
4959     gen_helper_vfp_mulxs,
4960     gen_helper_vfp_mulxd,
4961 };
4962 TRANS(FMULX_s, do_fp3_scalar, a, &f_scalar_fmulx)
4963 
4964 static void gen_fnmul_h(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
4965 {
4966     gen_helper_vfp_mulh(d, n, m, s);
4967     gen_vfp_negh(d, d);
4968 }
4969 
4970 static void gen_fnmul_s(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
4971 {
4972     gen_helper_vfp_muls(d, n, m, s);
4973     gen_vfp_negs(d, d);
4974 }
4975 
4976 static void gen_fnmul_d(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_ptr s)
4977 {
4978     gen_helper_vfp_muld(d, n, m, s);
4979     gen_vfp_negd(d, d);
4980 }
4981 
4982 static const FPScalar f_scalar_fnmul = {
4983     gen_fnmul_h,
4984     gen_fnmul_s,
4985     gen_fnmul_d,
4986 };
4987 TRANS(FNMUL_s, do_fp3_scalar, a, &f_scalar_fnmul)
4988 
4989 static const FPScalar f_scalar_fcmeq = {
4990     gen_helper_advsimd_ceq_f16,
4991     gen_helper_neon_ceq_f32,
4992     gen_helper_neon_ceq_f64,
4993 };
4994 TRANS(FCMEQ_s, do_fp3_scalar, a, &f_scalar_fcmeq)
4995 
4996 static const FPScalar f_scalar_fcmge = {
4997     gen_helper_advsimd_cge_f16,
4998     gen_helper_neon_cge_f32,
4999     gen_helper_neon_cge_f64,
5000 };
5001 TRANS(FCMGE_s, do_fp3_scalar, a, &f_scalar_fcmge)
5002 
5003 static const FPScalar f_scalar_fcmgt = {
5004     gen_helper_advsimd_cgt_f16,
5005     gen_helper_neon_cgt_f32,
5006     gen_helper_neon_cgt_f64,
5007 };
5008 TRANS(FCMGT_s, do_fp3_scalar, a, &f_scalar_fcmgt)
5009 
5010 static const FPScalar f_scalar_facge = {
5011     gen_helper_advsimd_acge_f16,
5012     gen_helper_neon_acge_f32,
5013     gen_helper_neon_acge_f64,
5014 };
5015 TRANS(FACGE_s, do_fp3_scalar, a, &f_scalar_facge)
5016 
5017 static const FPScalar f_scalar_facgt = {
5018     gen_helper_advsimd_acgt_f16,
5019     gen_helper_neon_acgt_f32,
5020     gen_helper_neon_acgt_f64,
5021 };
5022 TRANS(FACGT_s, do_fp3_scalar, a, &f_scalar_facgt)
5023 
5024 static void gen_fabd_h(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
5025 {
5026     gen_helper_vfp_subh(d, n, m, s);
5027     gen_vfp_absh(d, d);
5028 }
5029 
5030 static void gen_fabd_s(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, TCGv_ptr s)
5031 {
5032     gen_helper_vfp_subs(d, n, m, s);
5033     gen_vfp_abss(d, d);
5034 }
5035 
5036 static void gen_fabd_d(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_ptr s)
5037 {
5038     gen_helper_vfp_subd(d, n, m, s);
5039     gen_vfp_absd(d, d);
5040 }
5041 
5042 static const FPScalar f_scalar_fabd = {
5043     gen_fabd_h,
5044     gen_fabd_s,
5045     gen_fabd_d,
5046 };
5047 TRANS(FABD_s, do_fp3_scalar, a, &f_scalar_fabd)
5048 
5049 static const FPScalar f_scalar_frecps = {
5050     gen_helper_recpsf_f16,
5051     gen_helper_recpsf_f32,
5052     gen_helper_recpsf_f64,
5053 };
5054 TRANS(FRECPS_s, do_fp3_scalar, a, &f_scalar_frecps)
5055 
5056 static const FPScalar f_scalar_frsqrts = {
5057     gen_helper_rsqrtsf_f16,
5058     gen_helper_rsqrtsf_f32,
5059     gen_helper_rsqrtsf_f64,
5060 };
5061 TRANS(FRSQRTS_s, do_fp3_scalar, a, &f_scalar_frsqrts)
5062 
5063 static bool do_fp3_vector(DisasContext *s, arg_qrrr_e *a,
5064                           gen_helper_gvec_3_ptr * const fns[3])
5065 {
5066     MemOp esz = a->esz;
5067 
5068     switch (esz) {
5069     case MO_64:
5070         if (!a->q) {
5071             return false;
5072         }
5073         break;
5074     case MO_32:
5075         break;
5076     case MO_16:
5077         if (!dc_isar_feature(aa64_fp16, s)) {
5078             return false;
5079         }
5080         break;
5081     default:
5082         return false;
5083     }
5084     if (fp_access_check(s)) {
5085         gen_gvec_op3_fpst(s, a->q, a->rd, a->rn, a->rm,
5086                           esz == MO_16, 0, fns[esz - 1]);
5087     }
5088     return true;
5089 }
5090 
5091 static gen_helper_gvec_3_ptr * const f_vector_fadd[3] = {
5092     gen_helper_gvec_fadd_h,
5093     gen_helper_gvec_fadd_s,
5094     gen_helper_gvec_fadd_d,
5095 };
5096 TRANS(FADD_v, do_fp3_vector, a, f_vector_fadd)
5097 
5098 static gen_helper_gvec_3_ptr * const f_vector_fsub[3] = {
5099     gen_helper_gvec_fsub_h,
5100     gen_helper_gvec_fsub_s,
5101     gen_helper_gvec_fsub_d,
5102 };
5103 TRANS(FSUB_v, do_fp3_vector, a, f_vector_fsub)
5104 
5105 static gen_helper_gvec_3_ptr * const f_vector_fdiv[3] = {
5106     gen_helper_gvec_fdiv_h,
5107     gen_helper_gvec_fdiv_s,
5108     gen_helper_gvec_fdiv_d,
5109 };
5110 TRANS(FDIV_v, do_fp3_vector, a, f_vector_fdiv)
5111 
5112 static gen_helper_gvec_3_ptr * const f_vector_fmul[3] = {
5113     gen_helper_gvec_fmul_h,
5114     gen_helper_gvec_fmul_s,
5115     gen_helper_gvec_fmul_d,
5116 };
5117 TRANS(FMUL_v, do_fp3_vector, a, f_vector_fmul)
5118 
5119 static gen_helper_gvec_3_ptr * const f_vector_fmax[3] = {
5120     gen_helper_gvec_fmax_h,
5121     gen_helper_gvec_fmax_s,
5122     gen_helper_gvec_fmax_d,
5123 };
5124 TRANS(FMAX_v, do_fp3_vector, a, f_vector_fmax)
5125 
5126 static gen_helper_gvec_3_ptr * const f_vector_fmin[3] = {
5127     gen_helper_gvec_fmin_h,
5128     gen_helper_gvec_fmin_s,
5129     gen_helper_gvec_fmin_d,
5130 };
5131 TRANS(FMIN_v, do_fp3_vector, a, f_vector_fmin)
5132 
5133 static gen_helper_gvec_3_ptr * const f_vector_fmaxnm[3] = {
5134     gen_helper_gvec_fmaxnum_h,
5135     gen_helper_gvec_fmaxnum_s,
5136     gen_helper_gvec_fmaxnum_d,
5137 };
5138 TRANS(FMAXNM_v, do_fp3_vector, a, f_vector_fmaxnm)
5139 
5140 static gen_helper_gvec_3_ptr * const f_vector_fminnm[3] = {
5141     gen_helper_gvec_fminnum_h,
5142     gen_helper_gvec_fminnum_s,
5143     gen_helper_gvec_fminnum_d,
5144 };
5145 TRANS(FMINNM_v, do_fp3_vector, a, f_vector_fminnm)
5146 
5147 static gen_helper_gvec_3_ptr * const f_vector_fmulx[3] = {
5148     gen_helper_gvec_fmulx_h,
5149     gen_helper_gvec_fmulx_s,
5150     gen_helper_gvec_fmulx_d,
5151 };
5152 TRANS(FMULX_v, do_fp3_vector, a, f_vector_fmulx)
5153 
5154 static gen_helper_gvec_3_ptr * const f_vector_fmla[3] = {
5155     gen_helper_gvec_vfma_h,
5156     gen_helper_gvec_vfma_s,
5157     gen_helper_gvec_vfma_d,
5158 };
5159 TRANS(FMLA_v, do_fp3_vector, a, f_vector_fmla)
5160 
5161 static gen_helper_gvec_3_ptr * const f_vector_fmls[3] = {
5162     gen_helper_gvec_vfms_h,
5163     gen_helper_gvec_vfms_s,
5164     gen_helper_gvec_vfms_d,
5165 };
5166 TRANS(FMLS_v, do_fp3_vector, a, f_vector_fmls)
5167 
5168 static gen_helper_gvec_3_ptr * const f_vector_fcmeq[3] = {
5169     gen_helper_gvec_fceq_h,
5170     gen_helper_gvec_fceq_s,
5171     gen_helper_gvec_fceq_d,
5172 };
5173 TRANS(FCMEQ_v, do_fp3_vector, a, f_vector_fcmeq)
5174 
5175 static gen_helper_gvec_3_ptr * const f_vector_fcmge[3] = {
5176     gen_helper_gvec_fcge_h,
5177     gen_helper_gvec_fcge_s,
5178     gen_helper_gvec_fcge_d,
5179 };
5180 TRANS(FCMGE_v, do_fp3_vector, a, f_vector_fcmge)
5181 
5182 static gen_helper_gvec_3_ptr * const f_vector_fcmgt[3] = {
5183     gen_helper_gvec_fcgt_h,
5184     gen_helper_gvec_fcgt_s,
5185     gen_helper_gvec_fcgt_d,
5186 };
5187 TRANS(FCMGT_v, do_fp3_vector, a, f_vector_fcmgt)
5188 
5189 static gen_helper_gvec_3_ptr * const f_vector_facge[3] = {
5190     gen_helper_gvec_facge_h,
5191     gen_helper_gvec_facge_s,
5192     gen_helper_gvec_facge_d,
5193 };
5194 TRANS(FACGE_v, do_fp3_vector, a, f_vector_facge)
5195 
5196 static gen_helper_gvec_3_ptr * const f_vector_facgt[3] = {
5197     gen_helper_gvec_facgt_h,
5198     gen_helper_gvec_facgt_s,
5199     gen_helper_gvec_facgt_d,
5200 };
5201 TRANS(FACGT_v, do_fp3_vector, a, f_vector_facgt)
5202 
5203 static gen_helper_gvec_3_ptr * const f_vector_fabd[3] = {
5204     gen_helper_gvec_fabd_h,
5205     gen_helper_gvec_fabd_s,
5206     gen_helper_gvec_fabd_d,
5207 };
5208 TRANS(FABD_v, do_fp3_vector, a, f_vector_fabd)
5209 
5210 static gen_helper_gvec_3_ptr * const f_vector_frecps[3] = {
5211     gen_helper_gvec_recps_h,
5212     gen_helper_gvec_recps_s,
5213     gen_helper_gvec_recps_d,
5214 };
5215 TRANS(FRECPS_v, do_fp3_vector, a, f_vector_frecps)
5216 
5217 static gen_helper_gvec_3_ptr * const f_vector_frsqrts[3] = {
5218     gen_helper_gvec_rsqrts_h,
5219     gen_helper_gvec_rsqrts_s,
5220     gen_helper_gvec_rsqrts_d,
5221 };
5222 TRANS(FRSQRTS_v, do_fp3_vector, a, f_vector_frsqrts)
5223 
5224 static gen_helper_gvec_3_ptr * const f_vector_faddp[3] = {
5225     gen_helper_gvec_faddp_h,
5226     gen_helper_gvec_faddp_s,
5227     gen_helper_gvec_faddp_d,
5228 };
5229 TRANS(FADDP_v, do_fp3_vector, a, f_vector_faddp)
5230 
5231 static gen_helper_gvec_3_ptr * const f_vector_fmaxp[3] = {
5232     gen_helper_gvec_fmaxp_h,
5233     gen_helper_gvec_fmaxp_s,
5234     gen_helper_gvec_fmaxp_d,
5235 };
5236 TRANS(FMAXP_v, do_fp3_vector, a, f_vector_fmaxp)
5237 
5238 static gen_helper_gvec_3_ptr * const f_vector_fminp[3] = {
5239     gen_helper_gvec_fminp_h,
5240     gen_helper_gvec_fminp_s,
5241     gen_helper_gvec_fminp_d,
5242 };
5243 TRANS(FMINP_v, do_fp3_vector, a, f_vector_fminp)
5244 
5245 static gen_helper_gvec_3_ptr * const f_vector_fmaxnmp[3] = {
5246     gen_helper_gvec_fmaxnump_h,
5247     gen_helper_gvec_fmaxnump_s,
5248     gen_helper_gvec_fmaxnump_d,
5249 };
5250 TRANS(FMAXNMP_v, do_fp3_vector, a, f_vector_fmaxnmp)
5251 
5252 static gen_helper_gvec_3_ptr * const f_vector_fminnmp[3] = {
5253     gen_helper_gvec_fminnump_h,
5254     gen_helper_gvec_fminnump_s,
5255     gen_helper_gvec_fminnump_d,
5256 };
5257 TRANS(FMINNMP_v, do_fp3_vector, a, f_vector_fminnmp)
5258 
5259 TRANS(ADDP_v, do_gvec_fn3, a, gen_gvec_addp)
5260 TRANS(SMAXP_v, do_gvec_fn3_no64, a, gen_gvec_smaxp)
5261 TRANS(SMINP_v, do_gvec_fn3_no64, a, gen_gvec_sminp)
5262 TRANS(UMAXP_v, do_gvec_fn3_no64, a, gen_gvec_umaxp)
5263 TRANS(UMINP_v, do_gvec_fn3_no64, a, gen_gvec_uminp)
5264 
5265 /*
5266  * Advanced SIMD scalar/vector x indexed element
5267  */
5268 
5269 static bool do_fp3_scalar_idx(DisasContext *s, arg_rrx_e *a, const FPScalar *f)
5270 {
5271     switch (a->esz) {
5272     case MO_64:
5273         if (fp_access_check(s)) {
5274             TCGv_i64 t0 = read_fp_dreg(s, a->rn);
5275             TCGv_i64 t1 = tcg_temp_new_i64();
5276 
5277             read_vec_element(s, t1, a->rm, a->idx, MO_64);
5278             f->gen_d(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5279             write_fp_dreg(s, a->rd, t0);
5280         }
5281         break;
5282     case MO_32:
5283         if (fp_access_check(s)) {
5284             TCGv_i32 t0 = read_fp_sreg(s, a->rn);
5285             TCGv_i32 t1 = tcg_temp_new_i32();
5286 
5287             read_vec_element_i32(s, t1, a->rm, a->idx, MO_32);
5288             f->gen_s(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5289             write_fp_sreg(s, a->rd, t0);
5290         }
5291         break;
5292     case MO_16:
5293         if (!dc_isar_feature(aa64_fp16, s)) {
5294             return false;
5295         }
5296         if (fp_access_check(s)) {
5297             TCGv_i32 t0 = read_fp_hreg(s, a->rn);
5298             TCGv_i32 t1 = tcg_temp_new_i32();
5299 
5300             read_vec_element_i32(s, t1, a->rm, a->idx, MO_16);
5301             f->gen_h(t0, t0, t1, fpstatus_ptr(FPST_FPCR_F16));
5302             write_fp_sreg(s, a->rd, t0);
5303         }
5304         break;
5305     default:
5306         g_assert_not_reached();
5307     }
5308     return true;
5309 }
5310 
5311 TRANS(FMUL_si, do_fp3_scalar_idx, a, &f_scalar_fmul)
5312 TRANS(FMULX_si, do_fp3_scalar_idx, a, &f_scalar_fmulx)
5313 
5314 static bool do_fmla_scalar_idx(DisasContext *s, arg_rrx_e *a, bool neg)
5315 {
5316     switch (a->esz) {
5317     case MO_64:
5318         if (fp_access_check(s)) {
5319             TCGv_i64 t0 = read_fp_dreg(s, a->rd);
5320             TCGv_i64 t1 = read_fp_dreg(s, a->rn);
5321             TCGv_i64 t2 = tcg_temp_new_i64();
5322 
5323             read_vec_element(s, t2, a->rm, a->idx, MO_64);
5324             if (neg) {
5325                 gen_vfp_negd(t1, t1);
5326             }
5327             gen_helper_vfp_muladdd(t0, t1, t2, t0, fpstatus_ptr(FPST_FPCR));
5328             write_fp_dreg(s, a->rd, t0);
5329         }
5330         break;
5331     case MO_32:
5332         if (fp_access_check(s)) {
5333             TCGv_i32 t0 = read_fp_sreg(s, a->rd);
5334             TCGv_i32 t1 = read_fp_sreg(s, a->rn);
5335             TCGv_i32 t2 = tcg_temp_new_i32();
5336 
5337             read_vec_element_i32(s, t2, a->rm, a->idx, MO_32);
5338             if (neg) {
5339                 gen_vfp_negs(t1, t1);
5340             }
5341             gen_helper_vfp_muladds(t0, t1, t2, t0, fpstatus_ptr(FPST_FPCR));
5342             write_fp_sreg(s, a->rd, t0);
5343         }
5344         break;
5345     case MO_16:
5346         if (!dc_isar_feature(aa64_fp16, s)) {
5347             return false;
5348         }
5349         if (fp_access_check(s)) {
5350             TCGv_i32 t0 = read_fp_hreg(s, a->rd);
5351             TCGv_i32 t1 = read_fp_hreg(s, a->rn);
5352             TCGv_i32 t2 = tcg_temp_new_i32();
5353 
5354             read_vec_element_i32(s, t2, a->rm, a->idx, MO_16);
5355             if (neg) {
5356                 gen_vfp_negh(t1, t1);
5357             }
5358             gen_helper_advsimd_muladdh(t0, t1, t2, t0,
5359                                        fpstatus_ptr(FPST_FPCR_F16));
5360             write_fp_sreg(s, a->rd, t0);
5361         }
5362         break;
5363     default:
5364         g_assert_not_reached();
5365     }
5366     return true;
5367 }
5368 
5369 TRANS(FMLA_si, do_fmla_scalar_idx, a, false)
5370 TRANS(FMLS_si, do_fmla_scalar_idx, a, true)
5371 
5372 static bool do_fp3_vector_idx(DisasContext *s, arg_qrrx_e *a,
5373                               gen_helper_gvec_3_ptr * const fns[3])
5374 {
5375     MemOp esz = a->esz;
5376 
5377     switch (esz) {
5378     case MO_64:
5379         if (!a->q) {
5380             return false;
5381         }
5382         break;
5383     case MO_32:
5384         break;
5385     case MO_16:
5386         if (!dc_isar_feature(aa64_fp16, s)) {
5387             return false;
5388         }
5389         break;
5390     default:
5391         g_assert_not_reached();
5392     }
5393     if (fp_access_check(s)) {
5394         gen_gvec_op3_fpst(s, a->q, a->rd, a->rn, a->rm,
5395                           esz == MO_16, a->idx, fns[esz - 1]);
5396     }
5397     return true;
5398 }
5399 
5400 static gen_helper_gvec_3_ptr * const f_vector_idx_fmul[3] = {
5401     gen_helper_gvec_fmul_idx_h,
5402     gen_helper_gvec_fmul_idx_s,
5403     gen_helper_gvec_fmul_idx_d,
5404 };
5405 TRANS(FMUL_vi, do_fp3_vector_idx, a, f_vector_idx_fmul)
5406 
5407 static gen_helper_gvec_3_ptr * const f_vector_idx_fmulx[3] = {
5408     gen_helper_gvec_fmulx_idx_h,
5409     gen_helper_gvec_fmulx_idx_s,
5410     gen_helper_gvec_fmulx_idx_d,
5411 };
5412 TRANS(FMULX_vi, do_fp3_vector_idx, a, f_vector_idx_fmulx)
5413 
5414 static bool do_fmla_vector_idx(DisasContext *s, arg_qrrx_e *a, bool neg)
5415 {
5416     static gen_helper_gvec_4_ptr * const fns[3] = {
5417         gen_helper_gvec_fmla_idx_h,
5418         gen_helper_gvec_fmla_idx_s,
5419         gen_helper_gvec_fmla_idx_d,
5420     };
5421     MemOp esz = a->esz;
5422 
5423     switch (esz) {
5424     case MO_64:
5425         if (!a->q) {
5426             return false;
5427         }
5428         break;
5429     case MO_32:
5430         break;
5431     case MO_16:
5432         if (!dc_isar_feature(aa64_fp16, s)) {
5433             return false;
5434         }
5435         break;
5436     default:
5437         g_assert_not_reached();
5438     }
5439     if (fp_access_check(s)) {
5440         gen_gvec_op4_fpst(s, a->q, a->rd, a->rn, a->rm, a->rd,
5441                           esz == MO_16, (a->idx << 1) | neg,
5442                           fns[esz - 1]);
5443     }
5444     return true;
5445 }
5446 
5447 TRANS(FMLA_vi, do_fmla_vector_idx, a, false)
5448 TRANS(FMLS_vi, do_fmla_vector_idx, a, true)
5449 
5450 /*
5451  * Advanced SIMD scalar pairwise
5452  */
5453 
5454 static bool do_fp3_scalar_pair(DisasContext *s, arg_rr_e *a, const FPScalar *f)
5455 {
5456     switch (a->esz) {
5457     case MO_64:
5458         if (fp_access_check(s)) {
5459             TCGv_i64 t0 = tcg_temp_new_i64();
5460             TCGv_i64 t1 = tcg_temp_new_i64();
5461 
5462             read_vec_element(s, t0, a->rn, 0, MO_64);
5463             read_vec_element(s, t1, a->rn, 1, MO_64);
5464             f->gen_d(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5465             write_fp_dreg(s, a->rd, t0);
5466         }
5467         break;
5468     case MO_32:
5469         if (fp_access_check(s)) {
5470             TCGv_i32 t0 = tcg_temp_new_i32();
5471             TCGv_i32 t1 = tcg_temp_new_i32();
5472 
5473             read_vec_element_i32(s, t0, a->rn, 0, MO_32);
5474             read_vec_element_i32(s, t1, a->rn, 1, MO_32);
5475             f->gen_s(t0, t0, t1, fpstatus_ptr(FPST_FPCR));
5476             write_fp_sreg(s, a->rd, t0);
5477         }
5478         break;
5479     case MO_16:
5480         if (!dc_isar_feature(aa64_fp16, s)) {
5481             return false;
5482         }
5483         if (fp_access_check(s)) {
5484             TCGv_i32 t0 = tcg_temp_new_i32();
5485             TCGv_i32 t1 = tcg_temp_new_i32();
5486 
5487             read_vec_element_i32(s, t0, a->rn, 0, MO_16);
5488             read_vec_element_i32(s, t1, a->rn, 1, MO_16);
5489             f->gen_h(t0, t0, t1, fpstatus_ptr(FPST_FPCR_F16));
5490             write_fp_sreg(s, a->rd, t0);
5491         }
5492         break;
5493     default:
5494         g_assert_not_reached();
5495     }
5496     return true;
5497 }
5498 
5499 TRANS(FADDP_s, do_fp3_scalar_pair, a, &f_scalar_fadd)
5500 TRANS(FMAXP_s, do_fp3_scalar_pair, a, &f_scalar_fmax)
5501 TRANS(FMINP_s, do_fp3_scalar_pair, a, &f_scalar_fmin)
5502 TRANS(FMAXNMP_s, do_fp3_scalar_pair, a, &f_scalar_fmaxnm)
5503 TRANS(FMINNMP_s, do_fp3_scalar_pair, a, &f_scalar_fminnm)
5504 
5505 static bool trans_ADDP_s(DisasContext *s, arg_rr_e *a)
5506 {
5507     if (fp_access_check(s)) {
5508         TCGv_i64 t0 = tcg_temp_new_i64();
5509         TCGv_i64 t1 = tcg_temp_new_i64();
5510 
5511         read_vec_element(s, t0, a->rn, 0, MO_64);
5512         read_vec_element(s, t1, a->rn, 1, MO_64);
5513         tcg_gen_add_i64(t0, t0, t1);
5514         write_fp_dreg(s, a->rd, t0);
5515     }
5516     return true;
5517 }
5518 
5519 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
5520  * Note that it is the caller's responsibility to ensure that the
5521  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
5522  * mandated semantics for out of range shifts.
5523  */
5524 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
5525                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
5526 {
5527     switch (shift_type) {
5528     case A64_SHIFT_TYPE_LSL:
5529         tcg_gen_shl_i64(dst, src, shift_amount);
5530         break;
5531     case A64_SHIFT_TYPE_LSR:
5532         tcg_gen_shr_i64(dst, src, shift_amount);
5533         break;
5534     case A64_SHIFT_TYPE_ASR:
5535         if (!sf) {
5536             tcg_gen_ext32s_i64(dst, src);
5537         }
5538         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
5539         break;
5540     case A64_SHIFT_TYPE_ROR:
5541         if (sf) {
5542             tcg_gen_rotr_i64(dst, src, shift_amount);
5543         } else {
5544             TCGv_i32 t0, t1;
5545             t0 = tcg_temp_new_i32();
5546             t1 = tcg_temp_new_i32();
5547             tcg_gen_extrl_i64_i32(t0, src);
5548             tcg_gen_extrl_i64_i32(t1, shift_amount);
5549             tcg_gen_rotr_i32(t0, t0, t1);
5550             tcg_gen_extu_i32_i64(dst, t0);
5551         }
5552         break;
5553     default:
5554         assert(FALSE); /* all shift types should be handled */
5555         break;
5556     }
5557 
5558     if (!sf) { /* zero extend final result */
5559         tcg_gen_ext32u_i64(dst, dst);
5560     }
5561 }
5562 
5563 /* Shift a TCGv src by immediate, put result in dst.
5564  * The shift amount must be in range (this should always be true as the
5565  * relevant instructions will UNDEF on bad shift immediates).
5566  */
5567 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
5568                           enum a64_shift_type shift_type, unsigned int shift_i)
5569 {
5570     assert(shift_i < (sf ? 64 : 32));
5571 
5572     if (shift_i == 0) {
5573         tcg_gen_mov_i64(dst, src);
5574     } else {
5575         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
5576     }
5577 }
5578 
5579 /* Logical (shifted register)
5580  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
5581  * +----+-----+-----------+-------+---+------+--------+------+------+
5582  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
5583  * +----+-----+-----------+-------+---+------+--------+------+------+
5584  */
5585 static void disas_logic_reg(DisasContext *s, uint32_t insn)
5586 {
5587     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
5588     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
5589 
5590     sf = extract32(insn, 31, 1);
5591     opc = extract32(insn, 29, 2);
5592     shift_type = extract32(insn, 22, 2);
5593     invert = extract32(insn, 21, 1);
5594     rm = extract32(insn, 16, 5);
5595     shift_amount = extract32(insn, 10, 6);
5596     rn = extract32(insn, 5, 5);
5597     rd = extract32(insn, 0, 5);
5598 
5599     if (!sf && (shift_amount & (1 << 5))) {
5600         unallocated_encoding(s);
5601         return;
5602     }
5603 
5604     tcg_rd = cpu_reg(s, rd);
5605 
5606     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
5607         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
5608          * register-register MOV and MVN, so it is worth special casing.
5609          */
5610         tcg_rm = cpu_reg(s, rm);
5611         if (invert) {
5612             tcg_gen_not_i64(tcg_rd, tcg_rm);
5613             if (!sf) {
5614                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5615             }
5616         } else {
5617             if (sf) {
5618                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
5619             } else {
5620                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
5621             }
5622         }
5623         return;
5624     }
5625 
5626     tcg_rm = read_cpu_reg(s, rm, sf);
5627 
5628     if (shift_amount) {
5629         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
5630     }
5631 
5632     tcg_rn = cpu_reg(s, rn);
5633 
5634     switch (opc | (invert << 2)) {
5635     case 0: /* AND */
5636     case 3: /* ANDS */
5637         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
5638         break;
5639     case 1: /* ORR */
5640         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
5641         break;
5642     case 2: /* EOR */
5643         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
5644         break;
5645     case 4: /* BIC */
5646     case 7: /* BICS */
5647         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
5648         break;
5649     case 5: /* ORN */
5650         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
5651         break;
5652     case 6: /* EON */
5653         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
5654         break;
5655     default:
5656         assert(FALSE);
5657         break;
5658     }
5659 
5660     if (!sf) {
5661         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5662     }
5663 
5664     if (opc == 3) {
5665         gen_logic_CC(sf, tcg_rd);
5666     }
5667 }
5668 
5669 /*
5670  * Add/subtract (extended register)
5671  *
5672  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
5673  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
5674  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
5675  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
5676  *
5677  *  sf: 0 -> 32bit, 1 -> 64bit
5678  *  op: 0 -> add  , 1 -> sub
5679  *   S: 1 -> set flags
5680  * opt: 00
5681  * option: extension type (see DecodeRegExtend)
5682  * imm3: optional shift to Rm
5683  *
5684  * Rd = Rn + LSL(extend(Rm), amount)
5685  */
5686 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
5687 {
5688     int rd = extract32(insn, 0, 5);
5689     int rn = extract32(insn, 5, 5);
5690     int imm3 = extract32(insn, 10, 3);
5691     int option = extract32(insn, 13, 3);
5692     int rm = extract32(insn, 16, 5);
5693     int opt = extract32(insn, 22, 2);
5694     bool setflags = extract32(insn, 29, 1);
5695     bool sub_op = extract32(insn, 30, 1);
5696     bool sf = extract32(insn, 31, 1);
5697 
5698     TCGv_i64 tcg_rm, tcg_rn; /* temps */
5699     TCGv_i64 tcg_rd;
5700     TCGv_i64 tcg_result;
5701 
5702     if (imm3 > 4 || opt != 0) {
5703         unallocated_encoding(s);
5704         return;
5705     }
5706 
5707     /* non-flag setting ops may use SP */
5708     if (!setflags) {
5709         tcg_rd = cpu_reg_sp(s, rd);
5710     } else {
5711         tcg_rd = cpu_reg(s, rd);
5712     }
5713     tcg_rn = read_cpu_reg_sp(s, rn, sf);
5714 
5715     tcg_rm = read_cpu_reg(s, rm, sf);
5716     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
5717 
5718     tcg_result = tcg_temp_new_i64();
5719 
5720     if (!setflags) {
5721         if (sub_op) {
5722             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
5723         } else {
5724             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
5725         }
5726     } else {
5727         if (sub_op) {
5728             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
5729         } else {
5730             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
5731         }
5732     }
5733 
5734     if (sf) {
5735         tcg_gen_mov_i64(tcg_rd, tcg_result);
5736     } else {
5737         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
5738     }
5739 }
5740 
5741 /*
5742  * Add/subtract (shifted register)
5743  *
5744  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
5745  * +--+--+--+-----------+-----+--+-------+---------+------+------+
5746  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
5747  * +--+--+--+-----------+-----+--+-------+---------+------+------+
5748  *
5749  *    sf: 0 -> 32bit, 1 -> 64bit
5750  *    op: 0 -> add  , 1 -> sub
5751  *     S: 1 -> set flags
5752  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
5753  *  imm6: Shift amount to apply to Rm before the add/sub
5754  */
5755 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
5756 {
5757     int rd = extract32(insn, 0, 5);
5758     int rn = extract32(insn, 5, 5);
5759     int imm6 = extract32(insn, 10, 6);
5760     int rm = extract32(insn, 16, 5);
5761     int shift_type = extract32(insn, 22, 2);
5762     bool setflags = extract32(insn, 29, 1);
5763     bool sub_op = extract32(insn, 30, 1);
5764     bool sf = extract32(insn, 31, 1);
5765 
5766     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5767     TCGv_i64 tcg_rn, tcg_rm;
5768     TCGv_i64 tcg_result;
5769 
5770     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
5771         unallocated_encoding(s);
5772         return;
5773     }
5774 
5775     tcg_rn = read_cpu_reg(s, rn, sf);
5776     tcg_rm = read_cpu_reg(s, rm, sf);
5777 
5778     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
5779 
5780     tcg_result = tcg_temp_new_i64();
5781 
5782     if (!setflags) {
5783         if (sub_op) {
5784             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
5785         } else {
5786             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
5787         }
5788     } else {
5789         if (sub_op) {
5790             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
5791         } else {
5792             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
5793         }
5794     }
5795 
5796     if (sf) {
5797         tcg_gen_mov_i64(tcg_rd, tcg_result);
5798     } else {
5799         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
5800     }
5801 }
5802 
5803 /* Data-processing (3 source)
5804  *
5805  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
5806  *  +--+------+-----------+------+------+----+------+------+------+
5807  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
5808  *  +--+------+-----------+------+------+----+------+------+------+
5809  */
5810 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
5811 {
5812     int rd = extract32(insn, 0, 5);
5813     int rn = extract32(insn, 5, 5);
5814     int ra = extract32(insn, 10, 5);
5815     int rm = extract32(insn, 16, 5);
5816     int op_id = (extract32(insn, 29, 3) << 4) |
5817         (extract32(insn, 21, 3) << 1) |
5818         extract32(insn, 15, 1);
5819     bool sf = extract32(insn, 31, 1);
5820     bool is_sub = extract32(op_id, 0, 1);
5821     bool is_high = extract32(op_id, 2, 1);
5822     bool is_signed = false;
5823     TCGv_i64 tcg_op1;
5824     TCGv_i64 tcg_op2;
5825     TCGv_i64 tcg_tmp;
5826 
5827     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5828     switch (op_id) {
5829     case 0x42: /* SMADDL */
5830     case 0x43: /* SMSUBL */
5831     case 0x44: /* SMULH */
5832         is_signed = true;
5833         break;
5834     case 0x0: /* MADD (32bit) */
5835     case 0x1: /* MSUB (32bit) */
5836     case 0x40: /* MADD (64bit) */
5837     case 0x41: /* MSUB (64bit) */
5838     case 0x4a: /* UMADDL */
5839     case 0x4b: /* UMSUBL */
5840     case 0x4c: /* UMULH */
5841         break;
5842     default:
5843         unallocated_encoding(s);
5844         return;
5845     }
5846 
5847     if (is_high) {
5848         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5849         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5850         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5851         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5852 
5853         if (is_signed) {
5854             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5855         } else {
5856             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5857         }
5858         return;
5859     }
5860 
5861     tcg_op1 = tcg_temp_new_i64();
5862     tcg_op2 = tcg_temp_new_i64();
5863     tcg_tmp = tcg_temp_new_i64();
5864 
5865     if (op_id < 0x42) {
5866         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5867         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5868     } else {
5869         if (is_signed) {
5870             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5871             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5872         } else {
5873             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5874             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5875         }
5876     }
5877 
5878     if (ra == 31 && !is_sub) {
5879         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5880         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5881     } else {
5882         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5883         if (is_sub) {
5884             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5885         } else {
5886             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5887         }
5888     }
5889 
5890     if (!sf) {
5891         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5892     }
5893 }
5894 
5895 /* Add/subtract (with carry)
5896  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5897  * +--+--+--+------------------------+------+-------------+------+-----+
5898  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5899  * +--+--+--+------------------------+------+-------------+------+-----+
5900  */
5901 
5902 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5903 {
5904     unsigned int sf, op, setflags, rm, rn, rd;
5905     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5906 
5907     sf = extract32(insn, 31, 1);
5908     op = extract32(insn, 30, 1);
5909     setflags = extract32(insn, 29, 1);
5910     rm = extract32(insn, 16, 5);
5911     rn = extract32(insn, 5, 5);
5912     rd = extract32(insn, 0, 5);
5913 
5914     tcg_rd = cpu_reg(s, rd);
5915     tcg_rn = cpu_reg(s, rn);
5916 
5917     if (op) {
5918         tcg_y = tcg_temp_new_i64();
5919         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5920     } else {
5921         tcg_y = cpu_reg(s, rm);
5922     }
5923 
5924     if (setflags) {
5925         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5926     } else {
5927         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5928     }
5929 }
5930 
5931 /*
5932  * Rotate right into flags
5933  *  31 30 29                21       15          10      5  4      0
5934  * +--+--+--+-----------------+--------+-----------+------+--+------+
5935  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5936  * +--+--+--+-----------------+--------+-----------+------+--+------+
5937  */
5938 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5939 {
5940     int mask = extract32(insn, 0, 4);
5941     int o2 = extract32(insn, 4, 1);
5942     int rn = extract32(insn, 5, 5);
5943     int imm6 = extract32(insn, 15, 6);
5944     int sf_op_s = extract32(insn, 29, 3);
5945     TCGv_i64 tcg_rn;
5946     TCGv_i32 nzcv;
5947 
5948     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5949         unallocated_encoding(s);
5950         return;
5951     }
5952 
5953     tcg_rn = read_cpu_reg(s, rn, 1);
5954     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5955 
5956     nzcv = tcg_temp_new_i32();
5957     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5958 
5959     if (mask & 8) { /* N */
5960         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5961     }
5962     if (mask & 4) { /* Z */
5963         tcg_gen_not_i32(cpu_ZF, nzcv);
5964         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5965     }
5966     if (mask & 2) { /* C */
5967         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5968     }
5969     if (mask & 1) { /* V */
5970         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5971     }
5972 }
5973 
5974 /*
5975  * Evaluate into flags
5976  *  31 30 29                21        15   14        10      5  4      0
5977  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5978  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5979  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5980  */
5981 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5982 {
5983     int o3_mask = extract32(insn, 0, 5);
5984     int rn = extract32(insn, 5, 5);
5985     int o2 = extract32(insn, 15, 6);
5986     int sz = extract32(insn, 14, 1);
5987     int sf_op_s = extract32(insn, 29, 3);
5988     TCGv_i32 tmp;
5989     int shift;
5990 
5991     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5992         !dc_isar_feature(aa64_condm_4, s)) {
5993         unallocated_encoding(s);
5994         return;
5995     }
5996     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5997 
5998     tmp = tcg_temp_new_i32();
5999     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
6000     tcg_gen_shli_i32(cpu_NF, tmp, shift);
6001     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
6002     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
6003     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
6004 }
6005 
6006 /* Conditional compare (immediate / register)
6007  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
6008  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
6009  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
6010  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
6011  *        [1]                             y                [0]       [0]
6012  */
6013 static void disas_cc(DisasContext *s, uint32_t insn)
6014 {
6015     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
6016     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
6017     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
6018     DisasCompare c;
6019 
6020     if (!extract32(insn, 29, 1)) {
6021         unallocated_encoding(s);
6022         return;
6023     }
6024     if (insn & (1 << 10 | 1 << 4)) {
6025         unallocated_encoding(s);
6026         return;
6027     }
6028     sf = extract32(insn, 31, 1);
6029     op = extract32(insn, 30, 1);
6030     is_imm = extract32(insn, 11, 1);
6031     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
6032     cond = extract32(insn, 12, 4);
6033     rn = extract32(insn, 5, 5);
6034     nzcv = extract32(insn, 0, 4);
6035 
6036     /* Set T0 = !COND.  */
6037     tcg_t0 = tcg_temp_new_i32();
6038     arm_test_cc(&c, cond);
6039     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
6040 
6041     /* Load the arguments for the new comparison.  */
6042     if (is_imm) {
6043         tcg_y = tcg_temp_new_i64();
6044         tcg_gen_movi_i64(tcg_y, y);
6045     } else {
6046         tcg_y = cpu_reg(s, y);
6047     }
6048     tcg_rn = cpu_reg(s, rn);
6049 
6050     /* Set the flags for the new comparison.  */
6051     tcg_tmp = tcg_temp_new_i64();
6052     if (op) {
6053         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
6054     } else {
6055         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
6056     }
6057 
6058     /* If COND was false, force the flags to #nzcv.  Compute two masks
6059      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
6060      * For tcg hosts that support ANDC, we can make do with just T1.
6061      * In either case, allow the tcg optimizer to delete any unused mask.
6062      */
6063     tcg_t1 = tcg_temp_new_i32();
6064     tcg_t2 = tcg_temp_new_i32();
6065     tcg_gen_neg_i32(tcg_t1, tcg_t0);
6066     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
6067 
6068     if (nzcv & 8) { /* N */
6069         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
6070     } else {
6071         if (TCG_TARGET_HAS_andc_i32) {
6072             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
6073         } else {
6074             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
6075         }
6076     }
6077     if (nzcv & 4) { /* Z */
6078         if (TCG_TARGET_HAS_andc_i32) {
6079             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
6080         } else {
6081             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
6082         }
6083     } else {
6084         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
6085     }
6086     if (nzcv & 2) { /* C */
6087         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
6088     } else {
6089         if (TCG_TARGET_HAS_andc_i32) {
6090             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
6091         } else {
6092             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
6093         }
6094     }
6095     if (nzcv & 1) { /* V */
6096         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
6097     } else {
6098         if (TCG_TARGET_HAS_andc_i32) {
6099             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
6100         } else {
6101             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
6102         }
6103     }
6104 }
6105 
6106 /* Conditional select
6107  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
6108  * +----+----+---+-----------------+------+------+-----+------+------+
6109  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
6110  * +----+----+---+-----------------+------+------+-----+------+------+
6111  */
6112 static void disas_cond_select(DisasContext *s, uint32_t insn)
6113 {
6114     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
6115     TCGv_i64 tcg_rd, zero;
6116     DisasCompare64 c;
6117 
6118     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
6119         /* S == 1 or op2<1> == 1 */
6120         unallocated_encoding(s);
6121         return;
6122     }
6123     sf = extract32(insn, 31, 1);
6124     else_inv = extract32(insn, 30, 1);
6125     rm = extract32(insn, 16, 5);
6126     cond = extract32(insn, 12, 4);
6127     else_inc = extract32(insn, 10, 1);
6128     rn = extract32(insn, 5, 5);
6129     rd = extract32(insn, 0, 5);
6130 
6131     tcg_rd = cpu_reg(s, rd);
6132 
6133     a64_test_cc(&c, cond);
6134     zero = tcg_constant_i64(0);
6135 
6136     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
6137         /* CSET & CSETM.  */
6138         if (else_inv) {
6139             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
6140                                    tcg_rd, c.value, zero);
6141         } else {
6142             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
6143                                 tcg_rd, c.value, zero);
6144         }
6145     } else {
6146         TCGv_i64 t_true = cpu_reg(s, rn);
6147         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
6148         if (else_inv && else_inc) {
6149             tcg_gen_neg_i64(t_false, t_false);
6150         } else if (else_inv) {
6151             tcg_gen_not_i64(t_false, t_false);
6152         } else if (else_inc) {
6153             tcg_gen_addi_i64(t_false, t_false, 1);
6154         }
6155         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
6156     }
6157 
6158     if (!sf) {
6159         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6160     }
6161 }
6162 
6163 static void handle_clz(DisasContext *s, unsigned int sf,
6164                        unsigned int rn, unsigned int rd)
6165 {
6166     TCGv_i64 tcg_rd, tcg_rn;
6167     tcg_rd = cpu_reg(s, rd);
6168     tcg_rn = cpu_reg(s, rn);
6169 
6170     if (sf) {
6171         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
6172     } else {
6173         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6174         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6175         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
6176         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6177     }
6178 }
6179 
6180 static void handle_cls(DisasContext *s, unsigned int sf,
6181                        unsigned int rn, unsigned int rd)
6182 {
6183     TCGv_i64 tcg_rd, tcg_rn;
6184     tcg_rd = cpu_reg(s, rd);
6185     tcg_rn = cpu_reg(s, rn);
6186 
6187     if (sf) {
6188         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
6189     } else {
6190         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6191         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6192         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
6193         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6194     }
6195 }
6196 
6197 static void handle_rbit(DisasContext *s, unsigned int sf,
6198                         unsigned int rn, unsigned int rd)
6199 {
6200     TCGv_i64 tcg_rd, tcg_rn;
6201     tcg_rd = cpu_reg(s, rd);
6202     tcg_rn = cpu_reg(s, rn);
6203 
6204     if (sf) {
6205         gen_helper_rbit64(tcg_rd, tcg_rn);
6206     } else {
6207         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
6208         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
6209         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
6210         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
6211     }
6212 }
6213 
6214 /* REV with sf==1, opcode==3 ("REV64") */
6215 static void handle_rev64(DisasContext *s, unsigned int sf,
6216                          unsigned int rn, unsigned int rd)
6217 {
6218     if (!sf) {
6219         unallocated_encoding(s);
6220         return;
6221     }
6222     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
6223 }
6224 
6225 /* REV with sf==0, opcode==2
6226  * REV32 (sf==1, opcode==2)
6227  */
6228 static void handle_rev32(DisasContext *s, unsigned int sf,
6229                          unsigned int rn, unsigned int rd)
6230 {
6231     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6232     TCGv_i64 tcg_rn = cpu_reg(s, rn);
6233 
6234     if (sf) {
6235         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
6236         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
6237     } else {
6238         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
6239     }
6240 }
6241 
6242 /* REV16 (opcode==1) */
6243 static void handle_rev16(DisasContext *s, unsigned int sf,
6244                          unsigned int rn, unsigned int rd)
6245 {
6246     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6247     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6248     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
6249     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
6250 
6251     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
6252     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
6253     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
6254     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
6255     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
6256 }
6257 
6258 /* Data-processing (1 source)
6259  *   31  30  29  28             21 20     16 15    10 9    5 4    0
6260  * +----+---+---+-----------------+---------+--------+------+------+
6261  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
6262  * +----+---+---+-----------------+---------+--------+------+------+
6263  */
6264 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
6265 {
6266     unsigned int sf, opcode, opcode2, rn, rd;
6267     TCGv_i64 tcg_rd;
6268 
6269     if (extract32(insn, 29, 1)) {
6270         unallocated_encoding(s);
6271         return;
6272     }
6273 
6274     sf = extract32(insn, 31, 1);
6275     opcode = extract32(insn, 10, 6);
6276     opcode2 = extract32(insn, 16, 5);
6277     rn = extract32(insn, 5, 5);
6278     rd = extract32(insn, 0, 5);
6279 
6280 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
6281 
6282     switch (MAP(sf, opcode2, opcode)) {
6283     case MAP(0, 0x00, 0x00): /* RBIT */
6284     case MAP(1, 0x00, 0x00):
6285         handle_rbit(s, sf, rn, rd);
6286         break;
6287     case MAP(0, 0x00, 0x01): /* REV16 */
6288     case MAP(1, 0x00, 0x01):
6289         handle_rev16(s, sf, rn, rd);
6290         break;
6291     case MAP(0, 0x00, 0x02): /* REV/REV32 */
6292     case MAP(1, 0x00, 0x02):
6293         handle_rev32(s, sf, rn, rd);
6294         break;
6295     case MAP(1, 0x00, 0x03): /* REV64 */
6296         handle_rev64(s, sf, rn, rd);
6297         break;
6298     case MAP(0, 0x00, 0x04): /* CLZ */
6299     case MAP(1, 0x00, 0x04):
6300         handle_clz(s, sf, rn, rd);
6301         break;
6302     case MAP(0, 0x00, 0x05): /* CLS */
6303     case MAP(1, 0x00, 0x05):
6304         handle_cls(s, sf, rn, rd);
6305         break;
6306     case MAP(1, 0x01, 0x00): /* PACIA */
6307         if (s->pauth_active) {
6308             tcg_rd = cpu_reg(s, rd);
6309             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6310         } else if (!dc_isar_feature(aa64_pauth, s)) {
6311             goto do_unallocated;
6312         }
6313         break;
6314     case MAP(1, 0x01, 0x01): /* PACIB */
6315         if (s->pauth_active) {
6316             tcg_rd = cpu_reg(s, rd);
6317             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6318         } else if (!dc_isar_feature(aa64_pauth, s)) {
6319             goto do_unallocated;
6320         }
6321         break;
6322     case MAP(1, 0x01, 0x02): /* PACDA */
6323         if (s->pauth_active) {
6324             tcg_rd = cpu_reg(s, rd);
6325             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6326         } else if (!dc_isar_feature(aa64_pauth, s)) {
6327             goto do_unallocated;
6328         }
6329         break;
6330     case MAP(1, 0x01, 0x03): /* PACDB */
6331         if (s->pauth_active) {
6332             tcg_rd = cpu_reg(s, rd);
6333             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6334         } else if (!dc_isar_feature(aa64_pauth, s)) {
6335             goto do_unallocated;
6336         }
6337         break;
6338     case MAP(1, 0x01, 0x04): /* AUTIA */
6339         if (s->pauth_active) {
6340             tcg_rd = cpu_reg(s, rd);
6341             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6342         } else if (!dc_isar_feature(aa64_pauth, s)) {
6343             goto do_unallocated;
6344         }
6345         break;
6346     case MAP(1, 0x01, 0x05): /* AUTIB */
6347         if (s->pauth_active) {
6348             tcg_rd = cpu_reg(s, rd);
6349             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6350         } else if (!dc_isar_feature(aa64_pauth, s)) {
6351             goto do_unallocated;
6352         }
6353         break;
6354     case MAP(1, 0x01, 0x06): /* AUTDA */
6355         if (s->pauth_active) {
6356             tcg_rd = cpu_reg(s, rd);
6357             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6358         } else if (!dc_isar_feature(aa64_pauth, s)) {
6359             goto do_unallocated;
6360         }
6361         break;
6362     case MAP(1, 0x01, 0x07): /* AUTDB */
6363         if (s->pauth_active) {
6364             tcg_rd = cpu_reg(s, rd);
6365             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
6366         } else if (!dc_isar_feature(aa64_pauth, s)) {
6367             goto do_unallocated;
6368         }
6369         break;
6370     case MAP(1, 0x01, 0x08): /* PACIZA */
6371         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6372             goto do_unallocated;
6373         } else if (s->pauth_active) {
6374             tcg_rd = cpu_reg(s, rd);
6375             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6376         }
6377         break;
6378     case MAP(1, 0x01, 0x09): /* PACIZB */
6379         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6380             goto do_unallocated;
6381         } else if (s->pauth_active) {
6382             tcg_rd = cpu_reg(s, rd);
6383             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6384         }
6385         break;
6386     case MAP(1, 0x01, 0x0a): /* PACDZA */
6387         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6388             goto do_unallocated;
6389         } else if (s->pauth_active) {
6390             tcg_rd = cpu_reg(s, rd);
6391             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6392         }
6393         break;
6394     case MAP(1, 0x01, 0x0b): /* PACDZB */
6395         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6396             goto do_unallocated;
6397         } else if (s->pauth_active) {
6398             tcg_rd = cpu_reg(s, rd);
6399             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6400         }
6401         break;
6402     case MAP(1, 0x01, 0x0c): /* AUTIZA */
6403         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6404             goto do_unallocated;
6405         } else if (s->pauth_active) {
6406             tcg_rd = cpu_reg(s, rd);
6407             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6408         }
6409         break;
6410     case MAP(1, 0x01, 0x0d): /* AUTIZB */
6411         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6412             goto do_unallocated;
6413         } else if (s->pauth_active) {
6414             tcg_rd = cpu_reg(s, rd);
6415             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6416         }
6417         break;
6418     case MAP(1, 0x01, 0x0e): /* AUTDZA */
6419         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6420             goto do_unallocated;
6421         } else if (s->pauth_active) {
6422             tcg_rd = cpu_reg(s, rd);
6423             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6424         }
6425         break;
6426     case MAP(1, 0x01, 0x0f): /* AUTDZB */
6427         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6428             goto do_unallocated;
6429         } else if (s->pauth_active) {
6430             tcg_rd = cpu_reg(s, rd);
6431             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
6432         }
6433         break;
6434     case MAP(1, 0x01, 0x10): /* XPACI */
6435         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6436             goto do_unallocated;
6437         } else if (s->pauth_active) {
6438             tcg_rd = cpu_reg(s, rd);
6439             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
6440         }
6441         break;
6442     case MAP(1, 0x01, 0x11): /* XPACD */
6443         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
6444             goto do_unallocated;
6445         } else if (s->pauth_active) {
6446             tcg_rd = cpu_reg(s, rd);
6447             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
6448         }
6449         break;
6450     default:
6451     do_unallocated:
6452         unallocated_encoding(s);
6453         break;
6454     }
6455 
6456 #undef MAP
6457 }
6458 
6459 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
6460                        unsigned int rm, unsigned int rn, unsigned int rd)
6461 {
6462     TCGv_i64 tcg_n, tcg_m, tcg_rd;
6463     tcg_rd = cpu_reg(s, rd);
6464 
6465     if (!sf && is_signed) {
6466         tcg_n = tcg_temp_new_i64();
6467         tcg_m = tcg_temp_new_i64();
6468         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
6469         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
6470     } else {
6471         tcg_n = read_cpu_reg(s, rn, sf);
6472         tcg_m = read_cpu_reg(s, rm, sf);
6473     }
6474 
6475     if (is_signed) {
6476         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
6477     } else {
6478         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
6479     }
6480 
6481     if (!sf) { /* zero extend final result */
6482         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6483     }
6484 }
6485 
6486 /* LSLV, LSRV, ASRV, RORV */
6487 static void handle_shift_reg(DisasContext *s,
6488                              enum a64_shift_type shift_type, unsigned int sf,
6489                              unsigned int rm, unsigned int rn, unsigned int rd)
6490 {
6491     TCGv_i64 tcg_shift = tcg_temp_new_i64();
6492     TCGv_i64 tcg_rd = cpu_reg(s, rd);
6493     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
6494 
6495     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
6496     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
6497 }
6498 
6499 /* CRC32[BHWX], CRC32C[BHWX] */
6500 static void handle_crc32(DisasContext *s,
6501                          unsigned int sf, unsigned int sz, bool crc32c,
6502                          unsigned int rm, unsigned int rn, unsigned int rd)
6503 {
6504     TCGv_i64 tcg_acc, tcg_val;
6505     TCGv_i32 tcg_bytes;
6506 
6507     if (!dc_isar_feature(aa64_crc32, s)
6508         || (sf == 1 && sz != 3)
6509         || (sf == 0 && sz == 3)) {
6510         unallocated_encoding(s);
6511         return;
6512     }
6513 
6514     if (sz == 3) {
6515         tcg_val = cpu_reg(s, rm);
6516     } else {
6517         uint64_t mask;
6518         switch (sz) {
6519         case 0:
6520             mask = 0xFF;
6521             break;
6522         case 1:
6523             mask = 0xFFFF;
6524             break;
6525         case 2:
6526             mask = 0xFFFFFFFF;
6527             break;
6528         default:
6529             g_assert_not_reached();
6530         }
6531         tcg_val = tcg_temp_new_i64();
6532         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
6533     }
6534 
6535     tcg_acc = cpu_reg(s, rn);
6536     tcg_bytes = tcg_constant_i32(1 << sz);
6537 
6538     if (crc32c) {
6539         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
6540     } else {
6541         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
6542     }
6543 }
6544 
6545 /* Data-processing (2 source)
6546  *   31   30  29 28             21 20  16 15    10 9    5 4    0
6547  * +----+---+---+-----------------+------+--------+------+------+
6548  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
6549  * +----+---+---+-----------------+------+--------+------+------+
6550  */
6551 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
6552 {
6553     unsigned int sf, rm, opcode, rn, rd, setflag;
6554     sf = extract32(insn, 31, 1);
6555     setflag = extract32(insn, 29, 1);
6556     rm = extract32(insn, 16, 5);
6557     opcode = extract32(insn, 10, 6);
6558     rn = extract32(insn, 5, 5);
6559     rd = extract32(insn, 0, 5);
6560 
6561     if (setflag && opcode != 0) {
6562         unallocated_encoding(s);
6563         return;
6564     }
6565 
6566     switch (opcode) {
6567     case 0: /* SUBP(S) */
6568         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
6569             goto do_unallocated;
6570         } else {
6571             TCGv_i64 tcg_n, tcg_m, tcg_d;
6572 
6573             tcg_n = read_cpu_reg_sp(s, rn, true);
6574             tcg_m = read_cpu_reg_sp(s, rm, true);
6575             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
6576             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
6577             tcg_d = cpu_reg(s, rd);
6578 
6579             if (setflag) {
6580                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
6581             } else {
6582                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
6583             }
6584         }
6585         break;
6586     case 2: /* UDIV */
6587         handle_div(s, false, sf, rm, rn, rd);
6588         break;
6589     case 3: /* SDIV */
6590         handle_div(s, true, sf, rm, rn, rd);
6591         break;
6592     case 4: /* IRG */
6593         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
6594             goto do_unallocated;
6595         }
6596         if (s->ata[0]) {
6597             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
6598                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
6599         } else {
6600             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
6601                                              cpu_reg_sp(s, rn));
6602         }
6603         break;
6604     case 5: /* GMI */
6605         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
6606             goto do_unallocated;
6607         } else {
6608             TCGv_i64 t = tcg_temp_new_i64();
6609 
6610             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
6611             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
6612             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
6613         }
6614         break;
6615     case 8: /* LSLV */
6616         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
6617         break;
6618     case 9: /* LSRV */
6619         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
6620         break;
6621     case 10: /* ASRV */
6622         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
6623         break;
6624     case 11: /* RORV */
6625         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
6626         break;
6627     case 12: /* PACGA */
6628         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
6629             goto do_unallocated;
6630         }
6631         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
6632                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
6633         break;
6634     case 16:
6635     case 17:
6636     case 18:
6637     case 19:
6638     case 20:
6639     case 21:
6640     case 22:
6641     case 23: /* CRC32 */
6642     {
6643         int sz = extract32(opcode, 0, 2);
6644         bool crc32c = extract32(opcode, 2, 1);
6645         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
6646         break;
6647     }
6648     default:
6649     do_unallocated:
6650         unallocated_encoding(s);
6651         break;
6652     }
6653 }
6654 
6655 /*
6656  * Data processing - register
6657  *  31  30 29  28      25    21  20  16      10         0
6658  * +--+---+--+---+-------+-----+-------+-------+---------+
6659  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
6660  * +--+---+--+---+-------+-----+-------+-------+---------+
6661  */
6662 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
6663 {
6664     int op0 = extract32(insn, 30, 1);
6665     int op1 = extract32(insn, 28, 1);
6666     int op2 = extract32(insn, 21, 4);
6667     int op3 = extract32(insn, 10, 6);
6668 
6669     if (!op1) {
6670         if (op2 & 8) {
6671             if (op2 & 1) {
6672                 /* Add/sub (extended register) */
6673                 disas_add_sub_ext_reg(s, insn);
6674             } else {
6675                 /* Add/sub (shifted register) */
6676                 disas_add_sub_reg(s, insn);
6677             }
6678         } else {
6679             /* Logical (shifted register) */
6680             disas_logic_reg(s, insn);
6681         }
6682         return;
6683     }
6684 
6685     switch (op2) {
6686     case 0x0:
6687         switch (op3) {
6688         case 0x00: /* Add/subtract (with carry) */
6689             disas_adc_sbc(s, insn);
6690             break;
6691 
6692         case 0x01: /* Rotate right into flags */
6693         case 0x21:
6694             disas_rotate_right_into_flags(s, insn);
6695             break;
6696 
6697         case 0x02: /* Evaluate into flags */
6698         case 0x12:
6699         case 0x22:
6700         case 0x32:
6701             disas_evaluate_into_flags(s, insn);
6702             break;
6703 
6704         default:
6705             goto do_unallocated;
6706         }
6707         break;
6708 
6709     case 0x2: /* Conditional compare */
6710         disas_cc(s, insn); /* both imm and reg forms */
6711         break;
6712 
6713     case 0x4: /* Conditional select */
6714         disas_cond_select(s, insn);
6715         break;
6716 
6717     case 0x6: /* Data-processing */
6718         if (op0) {    /* (1 source) */
6719             disas_data_proc_1src(s, insn);
6720         } else {      /* (2 source) */
6721             disas_data_proc_2src(s, insn);
6722         }
6723         break;
6724     case 0x8 ... 0xf: /* (3 source) */
6725         disas_data_proc_3src(s, insn);
6726         break;
6727 
6728     default:
6729     do_unallocated:
6730         unallocated_encoding(s);
6731         break;
6732     }
6733 }
6734 
6735 static void handle_fp_compare(DisasContext *s, int size,
6736                               unsigned int rn, unsigned int rm,
6737                               bool cmp_with_zero, bool signal_all_nans)
6738 {
6739     TCGv_i64 tcg_flags = tcg_temp_new_i64();
6740     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
6741 
6742     if (size == MO_64) {
6743         TCGv_i64 tcg_vn, tcg_vm;
6744 
6745         tcg_vn = read_fp_dreg(s, rn);
6746         if (cmp_with_zero) {
6747             tcg_vm = tcg_constant_i64(0);
6748         } else {
6749             tcg_vm = read_fp_dreg(s, rm);
6750         }
6751         if (signal_all_nans) {
6752             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6753         } else {
6754             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6755         }
6756     } else {
6757         TCGv_i32 tcg_vn = tcg_temp_new_i32();
6758         TCGv_i32 tcg_vm = tcg_temp_new_i32();
6759 
6760         read_vec_element_i32(s, tcg_vn, rn, 0, size);
6761         if (cmp_with_zero) {
6762             tcg_gen_movi_i32(tcg_vm, 0);
6763         } else {
6764             read_vec_element_i32(s, tcg_vm, rm, 0, size);
6765         }
6766 
6767         switch (size) {
6768         case MO_32:
6769             if (signal_all_nans) {
6770                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6771             } else {
6772                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6773             }
6774             break;
6775         case MO_16:
6776             if (signal_all_nans) {
6777                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6778             } else {
6779                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6780             }
6781             break;
6782         default:
6783             g_assert_not_reached();
6784         }
6785     }
6786 
6787     gen_set_nzcv(tcg_flags);
6788 }
6789 
6790 /* Floating point compare
6791  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
6792  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6793  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
6794  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6795  */
6796 static void disas_fp_compare(DisasContext *s, uint32_t insn)
6797 {
6798     unsigned int mos, type, rm, op, rn, opc, op2r;
6799     int size;
6800 
6801     mos = extract32(insn, 29, 3);
6802     type = extract32(insn, 22, 2);
6803     rm = extract32(insn, 16, 5);
6804     op = extract32(insn, 14, 2);
6805     rn = extract32(insn, 5, 5);
6806     opc = extract32(insn, 3, 2);
6807     op2r = extract32(insn, 0, 3);
6808 
6809     if (mos || op || op2r) {
6810         unallocated_encoding(s);
6811         return;
6812     }
6813 
6814     switch (type) {
6815     case 0:
6816         size = MO_32;
6817         break;
6818     case 1:
6819         size = MO_64;
6820         break;
6821     case 3:
6822         size = MO_16;
6823         if (dc_isar_feature(aa64_fp16, s)) {
6824             break;
6825         }
6826         /* fallthru */
6827     default:
6828         unallocated_encoding(s);
6829         return;
6830     }
6831 
6832     if (!fp_access_check(s)) {
6833         return;
6834     }
6835 
6836     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6837 }
6838 
6839 /* Floating point conditional compare
6840  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6841  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6842  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6843  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6844  */
6845 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6846 {
6847     unsigned int mos, type, rm, cond, rn, op, nzcv;
6848     TCGLabel *label_continue = NULL;
6849     int size;
6850 
6851     mos = extract32(insn, 29, 3);
6852     type = extract32(insn, 22, 2);
6853     rm = extract32(insn, 16, 5);
6854     cond = extract32(insn, 12, 4);
6855     rn = extract32(insn, 5, 5);
6856     op = extract32(insn, 4, 1);
6857     nzcv = extract32(insn, 0, 4);
6858 
6859     if (mos) {
6860         unallocated_encoding(s);
6861         return;
6862     }
6863 
6864     switch (type) {
6865     case 0:
6866         size = MO_32;
6867         break;
6868     case 1:
6869         size = MO_64;
6870         break;
6871     case 3:
6872         size = MO_16;
6873         if (dc_isar_feature(aa64_fp16, s)) {
6874             break;
6875         }
6876         /* fallthru */
6877     default:
6878         unallocated_encoding(s);
6879         return;
6880     }
6881 
6882     if (!fp_access_check(s)) {
6883         return;
6884     }
6885 
6886     if (cond < 0x0e) { /* not always */
6887         TCGLabel *label_match = gen_new_label();
6888         label_continue = gen_new_label();
6889         arm_gen_test_cc(cond, label_match);
6890         /* nomatch: */
6891         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6892         tcg_gen_br(label_continue);
6893         gen_set_label(label_match);
6894     }
6895 
6896     handle_fp_compare(s, size, rn, rm, false, op);
6897 
6898     if (cond < 0x0e) {
6899         gen_set_label(label_continue);
6900     }
6901 }
6902 
6903 /* Floating point conditional select
6904  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6905  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6906  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6907  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6908  */
6909 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6910 {
6911     unsigned int mos, type, rm, cond, rn, rd;
6912     TCGv_i64 t_true, t_false;
6913     DisasCompare64 c;
6914     MemOp sz;
6915 
6916     mos = extract32(insn, 29, 3);
6917     type = extract32(insn, 22, 2);
6918     rm = extract32(insn, 16, 5);
6919     cond = extract32(insn, 12, 4);
6920     rn = extract32(insn, 5, 5);
6921     rd = extract32(insn, 0, 5);
6922 
6923     if (mos) {
6924         unallocated_encoding(s);
6925         return;
6926     }
6927 
6928     switch (type) {
6929     case 0:
6930         sz = MO_32;
6931         break;
6932     case 1:
6933         sz = MO_64;
6934         break;
6935     case 3:
6936         sz = MO_16;
6937         if (dc_isar_feature(aa64_fp16, s)) {
6938             break;
6939         }
6940         /* fallthru */
6941     default:
6942         unallocated_encoding(s);
6943         return;
6944     }
6945 
6946     if (!fp_access_check(s)) {
6947         return;
6948     }
6949 
6950     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6951     t_true = tcg_temp_new_i64();
6952     t_false = tcg_temp_new_i64();
6953     read_vec_element(s, t_true, rn, 0, sz);
6954     read_vec_element(s, t_false, rm, 0, sz);
6955 
6956     a64_test_cc(&c, cond);
6957     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6958                         t_true, t_false);
6959 
6960     /* Note that sregs & hregs write back zeros to the high bits,
6961        and we've already done the zero-extension.  */
6962     write_fp_dreg(s, rd, t_true);
6963 }
6964 
6965 /* Floating-point data-processing (1 source) - half precision */
6966 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6967 {
6968     TCGv_ptr fpst = NULL;
6969     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6970     TCGv_i32 tcg_res = tcg_temp_new_i32();
6971 
6972     switch (opcode) {
6973     case 0x0: /* FMOV */
6974         tcg_gen_mov_i32(tcg_res, tcg_op);
6975         break;
6976     case 0x1: /* FABS */
6977         gen_vfp_absh(tcg_res, tcg_op);
6978         break;
6979     case 0x2: /* FNEG */
6980         gen_vfp_negh(tcg_res, tcg_op);
6981         break;
6982     case 0x3: /* FSQRT */
6983         fpst = fpstatus_ptr(FPST_FPCR_F16);
6984         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6985         break;
6986     case 0x8: /* FRINTN */
6987     case 0x9: /* FRINTP */
6988     case 0xa: /* FRINTM */
6989     case 0xb: /* FRINTZ */
6990     case 0xc: /* FRINTA */
6991     {
6992         TCGv_i32 tcg_rmode;
6993 
6994         fpst = fpstatus_ptr(FPST_FPCR_F16);
6995         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6996         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6997         gen_restore_rmode(tcg_rmode, fpst);
6998         break;
6999     }
7000     case 0xe: /* FRINTX */
7001         fpst = fpstatus_ptr(FPST_FPCR_F16);
7002         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
7003         break;
7004     case 0xf: /* FRINTI */
7005         fpst = fpstatus_ptr(FPST_FPCR_F16);
7006         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
7007         break;
7008     default:
7009         g_assert_not_reached();
7010     }
7011 
7012     write_fp_sreg(s, rd, tcg_res);
7013 }
7014 
7015 /* Floating-point data-processing (1 source) - single precision */
7016 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
7017 {
7018     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
7019     TCGv_i32 tcg_op, tcg_res;
7020     TCGv_ptr fpst;
7021     int rmode = -1;
7022 
7023     tcg_op = read_fp_sreg(s, rn);
7024     tcg_res = tcg_temp_new_i32();
7025 
7026     switch (opcode) {
7027     case 0x0: /* FMOV */
7028         tcg_gen_mov_i32(tcg_res, tcg_op);
7029         goto done;
7030     case 0x1: /* FABS */
7031         gen_vfp_abss(tcg_res, tcg_op);
7032         goto done;
7033     case 0x2: /* FNEG */
7034         gen_vfp_negs(tcg_res, tcg_op);
7035         goto done;
7036     case 0x3: /* FSQRT */
7037         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
7038         goto done;
7039     case 0x6: /* BFCVT */
7040         gen_fpst = gen_helper_bfcvt;
7041         break;
7042     case 0x8: /* FRINTN */
7043     case 0x9: /* FRINTP */
7044     case 0xa: /* FRINTM */
7045     case 0xb: /* FRINTZ */
7046     case 0xc: /* FRINTA */
7047         rmode = opcode & 7;
7048         gen_fpst = gen_helper_rints;
7049         break;
7050     case 0xe: /* FRINTX */
7051         gen_fpst = gen_helper_rints_exact;
7052         break;
7053     case 0xf: /* FRINTI */
7054         gen_fpst = gen_helper_rints;
7055         break;
7056     case 0x10: /* FRINT32Z */
7057         rmode = FPROUNDING_ZERO;
7058         gen_fpst = gen_helper_frint32_s;
7059         break;
7060     case 0x11: /* FRINT32X */
7061         gen_fpst = gen_helper_frint32_s;
7062         break;
7063     case 0x12: /* FRINT64Z */
7064         rmode = FPROUNDING_ZERO;
7065         gen_fpst = gen_helper_frint64_s;
7066         break;
7067     case 0x13: /* FRINT64X */
7068         gen_fpst = gen_helper_frint64_s;
7069         break;
7070     default:
7071         g_assert_not_reached();
7072     }
7073 
7074     fpst = fpstatus_ptr(FPST_FPCR);
7075     if (rmode >= 0) {
7076         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
7077         gen_fpst(tcg_res, tcg_op, fpst);
7078         gen_restore_rmode(tcg_rmode, fpst);
7079     } else {
7080         gen_fpst(tcg_res, tcg_op, fpst);
7081     }
7082 
7083  done:
7084     write_fp_sreg(s, rd, tcg_res);
7085 }
7086 
7087 /* Floating-point data-processing (1 source) - double precision */
7088 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
7089 {
7090     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
7091     TCGv_i64 tcg_op, tcg_res;
7092     TCGv_ptr fpst;
7093     int rmode = -1;
7094 
7095     switch (opcode) {
7096     case 0x0: /* FMOV */
7097         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
7098         return;
7099     }
7100 
7101     tcg_op = read_fp_dreg(s, rn);
7102     tcg_res = tcg_temp_new_i64();
7103 
7104     switch (opcode) {
7105     case 0x1: /* FABS */
7106         gen_vfp_absd(tcg_res, tcg_op);
7107         goto done;
7108     case 0x2: /* FNEG */
7109         gen_vfp_negd(tcg_res, tcg_op);
7110         goto done;
7111     case 0x3: /* FSQRT */
7112         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
7113         goto done;
7114     case 0x8: /* FRINTN */
7115     case 0x9: /* FRINTP */
7116     case 0xa: /* FRINTM */
7117     case 0xb: /* FRINTZ */
7118     case 0xc: /* FRINTA */
7119         rmode = opcode & 7;
7120         gen_fpst = gen_helper_rintd;
7121         break;
7122     case 0xe: /* FRINTX */
7123         gen_fpst = gen_helper_rintd_exact;
7124         break;
7125     case 0xf: /* FRINTI */
7126         gen_fpst = gen_helper_rintd;
7127         break;
7128     case 0x10: /* FRINT32Z */
7129         rmode = FPROUNDING_ZERO;
7130         gen_fpst = gen_helper_frint32_d;
7131         break;
7132     case 0x11: /* FRINT32X */
7133         gen_fpst = gen_helper_frint32_d;
7134         break;
7135     case 0x12: /* FRINT64Z */
7136         rmode = FPROUNDING_ZERO;
7137         gen_fpst = gen_helper_frint64_d;
7138         break;
7139     case 0x13: /* FRINT64X */
7140         gen_fpst = gen_helper_frint64_d;
7141         break;
7142     default:
7143         g_assert_not_reached();
7144     }
7145 
7146     fpst = fpstatus_ptr(FPST_FPCR);
7147     if (rmode >= 0) {
7148         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
7149         gen_fpst(tcg_res, tcg_op, fpst);
7150         gen_restore_rmode(tcg_rmode, fpst);
7151     } else {
7152         gen_fpst(tcg_res, tcg_op, fpst);
7153     }
7154 
7155  done:
7156     write_fp_dreg(s, rd, tcg_res);
7157 }
7158 
7159 static void handle_fp_fcvt(DisasContext *s, int opcode,
7160                            int rd, int rn, int dtype, int ntype)
7161 {
7162     switch (ntype) {
7163     case 0x0:
7164     {
7165         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
7166         if (dtype == 1) {
7167             /* Single to double */
7168             TCGv_i64 tcg_rd = tcg_temp_new_i64();
7169             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
7170             write_fp_dreg(s, rd, tcg_rd);
7171         } else {
7172             /* Single to half */
7173             TCGv_i32 tcg_rd = tcg_temp_new_i32();
7174             TCGv_i32 ahp = get_ahp_flag();
7175             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7176 
7177             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
7178             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
7179             write_fp_sreg(s, rd, tcg_rd);
7180         }
7181         break;
7182     }
7183     case 0x1:
7184     {
7185         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7186         TCGv_i32 tcg_rd = tcg_temp_new_i32();
7187         if (dtype == 0) {
7188             /* Double to single */
7189             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
7190         } else {
7191             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7192             TCGv_i32 ahp = get_ahp_flag();
7193             /* Double to half */
7194             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
7195             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
7196         }
7197         write_fp_sreg(s, rd, tcg_rd);
7198         break;
7199     }
7200     case 0x3:
7201     {
7202         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
7203         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
7204         TCGv_i32 tcg_ahp = get_ahp_flag();
7205         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
7206         if (dtype == 0) {
7207             /* Half to single */
7208             TCGv_i32 tcg_rd = tcg_temp_new_i32();
7209             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
7210             write_fp_sreg(s, rd, tcg_rd);
7211         } else {
7212             /* Half to double */
7213             TCGv_i64 tcg_rd = tcg_temp_new_i64();
7214             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
7215             write_fp_dreg(s, rd, tcg_rd);
7216         }
7217         break;
7218     }
7219     default:
7220         g_assert_not_reached();
7221     }
7222 }
7223 
7224 /* Floating point data-processing (1 source)
7225  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
7226  * +---+---+---+-----------+------+---+--------+-----------+------+------+
7227  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
7228  * +---+---+---+-----------+------+---+--------+-----------+------+------+
7229  */
7230 static void disas_fp_1src(DisasContext *s, uint32_t insn)
7231 {
7232     int mos = extract32(insn, 29, 3);
7233     int type = extract32(insn, 22, 2);
7234     int opcode = extract32(insn, 15, 6);
7235     int rn = extract32(insn, 5, 5);
7236     int rd = extract32(insn, 0, 5);
7237 
7238     if (mos) {
7239         goto do_unallocated;
7240     }
7241 
7242     switch (opcode) {
7243     case 0x4: case 0x5: case 0x7:
7244     {
7245         /* FCVT between half, single and double precision */
7246         int dtype = extract32(opcode, 0, 2);
7247         if (type == 2 || dtype == type) {
7248             goto do_unallocated;
7249         }
7250         if (!fp_access_check(s)) {
7251             return;
7252         }
7253 
7254         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
7255         break;
7256     }
7257 
7258     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
7259         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
7260             goto do_unallocated;
7261         }
7262         /* fall through */
7263     case 0x0 ... 0x3:
7264     case 0x8 ... 0xc:
7265     case 0xe ... 0xf:
7266         /* 32-to-32 and 64-to-64 ops */
7267         switch (type) {
7268         case 0:
7269             if (!fp_access_check(s)) {
7270                 return;
7271             }
7272             handle_fp_1src_single(s, opcode, rd, rn);
7273             break;
7274         case 1:
7275             if (!fp_access_check(s)) {
7276                 return;
7277             }
7278             handle_fp_1src_double(s, opcode, rd, rn);
7279             break;
7280         case 3:
7281             if (!dc_isar_feature(aa64_fp16, s)) {
7282                 goto do_unallocated;
7283             }
7284 
7285             if (!fp_access_check(s)) {
7286                 return;
7287             }
7288             handle_fp_1src_half(s, opcode, rd, rn);
7289             break;
7290         default:
7291             goto do_unallocated;
7292         }
7293         break;
7294 
7295     case 0x6:
7296         switch (type) {
7297         case 1: /* BFCVT */
7298             if (!dc_isar_feature(aa64_bf16, s)) {
7299                 goto do_unallocated;
7300             }
7301             if (!fp_access_check(s)) {
7302                 return;
7303             }
7304             handle_fp_1src_single(s, opcode, rd, rn);
7305             break;
7306         default:
7307             goto do_unallocated;
7308         }
7309         break;
7310 
7311     default:
7312     do_unallocated:
7313         unallocated_encoding(s);
7314         break;
7315     }
7316 }
7317 
7318 /* Floating-point data-processing (3 source) - single precision */
7319 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
7320                                   int rd, int rn, int rm, int ra)
7321 {
7322     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
7323     TCGv_i32 tcg_res = tcg_temp_new_i32();
7324     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7325 
7326     tcg_op1 = read_fp_sreg(s, rn);
7327     tcg_op2 = read_fp_sreg(s, rm);
7328     tcg_op3 = read_fp_sreg(s, ra);
7329 
7330     /* These are fused multiply-add, and must be done as one
7331      * floating point operation with no rounding between the
7332      * multiplication and addition steps.
7333      * NB that doing the negations here as separate steps is
7334      * correct : an input NaN should come out with its sign bit
7335      * flipped if it is a negated-input.
7336      */
7337     if (o1 == true) {
7338         gen_vfp_negs(tcg_op3, tcg_op3);
7339     }
7340 
7341     if (o0 != o1) {
7342         gen_vfp_negs(tcg_op1, tcg_op1);
7343     }
7344 
7345     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
7346 
7347     write_fp_sreg(s, rd, tcg_res);
7348 }
7349 
7350 /* Floating-point data-processing (3 source) - double precision */
7351 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
7352                                   int rd, int rn, int rm, int ra)
7353 {
7354     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
7355     TCGv_i64 tcg_res = tcg_temp_new_i64();
7356     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
7357 
7358     tcg_op1 = read_fp_dreg(s, rn);
7359     tcg_op2 = read_fp_dreg(s, rm);
7360     tcg_op3 = read_fp_dreg(s, ra);
7361 
7362     /* These are fused multiply-add, and must be done as one
7363      * floating point operation with no rounding between the
7364      * multiplication and addition steps.
7365      * NB that doing the negations here as separate steps is
7366      * correct : an input NaN should come out with its sign bit
7367      * flipped if it is a negated-input.
7368      */
7369     if (o1 == true) {
7370         gen_vfp_negd(tcg_op3, tcg_op3);
7371     }
7372 
7373     if (o0 != o1) {
7374         gen_vfp_negd(tcg_op1, tcg_op1);
7375     }
7376 
7377     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
7378 
7379     write_fp_dreg(s, rd, tcg_res);
7380 }
7381 
7382 /* Floating-point data-processing (3 source) - half precision */
7383 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
7384                                 int rd, int rn, int rm, int ra)
7385 {
7386     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
7387     TCGv_i32 tcg_res = tcg_temp_new_i32();
7388     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
7389 
7390     tcg_op1 = read_fp_hreg(s, rn);
7391     tcg_op2 = read_fp_hreg(s, rm);
7392     tcg_op3 = read_fp_hreg(s, ra);
7393 
7394     /* These are fused multiply-add, and must be done as one
7395      * floating point operation with no rounding between the
7396      * multiplication and addition steps.
7397      * NB that doing the negations here as separate steps is
7398      * correct : an input NaN should come out with its sign bit
7399      * flipped if it is a negated-input.
7400      */
7401     if (o1 == true) {
7402         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
7403     }
7404 
7405     if (o0 != o1) {
7406         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
7407     }
7408 
7409     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
7410 
7411     write_fp_sreg(s, rd, tcg_res);
7412 }
7413 
7414 /* Floating point data-processing (3 source)
7415  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
7416  * +---+---+---+-----------+------+----+------+----+------+------+------+
7417  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
7418  * +---+---+---+-----------+------+----+------+----+------+------+------+
7419  */
7420 static void disas_fp_3src(DisasContext *s, uint32_t insn)
7421 {
7422     int mos = extract32(insn, 29, 3);
7423     int type = extract32(insn, 22, 2);
7424     int rd = extract32(insn, 0, 5);
7425     int rn = extract32(insn, 5, 5);
7426     int ra = extract32(insn, 10, 5);
7427     int rm = extract32(insn, 16, 5);
7428     bool o0 = extract32(insn, 15, 1);
7429     bool o1 = extract32(insn, 21, 1);
7430 
7431     if (mos) {
7432         unallocated_encoding(s);
7433         return;
7434     }
7435 
7436     switch (type) {
7437     case 0:
7438         if (!fp_access_check(s)) {
7439             return;
7440         }
7441         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
7442         break;
7443     case 1:
7444         if (!fp_access_check(s)) {
7445             return;
7446         }
7447         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
7448         break;
7449     case 3:
7450         if (!dc_isar_feature(aa64_fp16, s)) {
7451             unallocated_encoding(s);
7452             return;
7453         }
7454         if (!fp_access_check(s)) {
7455             return;
7456         }
7457         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
7458         break;
7459     default:
7460         unallocated_encoding(s);
7461     }
7462 }
7463 
7464 /* Floating point immediate
7465  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
7466  * +---+---+---+-----------+------+---+------------+-------+------+------+
7467  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
7468  * +---+---+---+-----------+------+---+------------+-------+------+------+
7469  */
7470 static void disas_fp_imm(DisasContext *s, uint32_t insn)
7471 {
7472     int rd = extract32(insn, 0, 5);
7473     int imm5 = extract32(insn, 5, 5);
7474     int imm8 = extract32(insn, 13, 8);
7475     int type = extract32(insn, 22, 2);
7476     int mos = extract32(insn, 29, 3);
7477     uint64_t imm;
7478     MemOp sz;
7479 
7480     if (mos || imm5) {
7481         unallocated_encoding(s);
7482         return;
7483     }
7484 
7485     switch (type) {
7486     case 0:
7487         sz = MO_32;
7488         break;
7489     case 1:
7490         sz = MO_64;
7491         break;
7492     case 3:
7493         sz = MO_16;
7494         if (dc_isar_feature(aa64_fp16, s)) {
7495             break;
7496         }
7497         /* fallthru */
7498     default:
7499         unallocated_encoding(s);
7500         return;
7501     }
7502 
7503     if (!fp_access_check(s)) {
7504         return;
7505     }
7506 
7507     imm = vfp_expand_imm(sz, imm8);
7508     write_fp_dreg(s, rd, tcg_constant_i64(imm));
7509 }
7510 
7511 /* Handle floating point <=> fixed point conversions. Note that we can
7512  * also deal with fp <=> integer conversions as a special case (scale == 64)
7513  * OPTME: consider handling that special case specially or at least skipping
7514  * the call to scalbn in the helpers for zero shifts.
7515  */
7516 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
7517                            bool itof, int rmode, int scale, int sf, int type)
7518 {
7519     bool is_signed = !(opcode & 1);
7520     TCGv_ptr tcg_fpstatus;
7521     TCGv_i32 tcg_shift, tcg_single;
7522     TCGv_i64 tcg_double;
7523 
7524     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
7525 
7526     tcg_shift = tcg_constant_i32(64 - scale);
7527 
7528     if (itof) {
7529         TCGv_i64 tcg_int = cpu_reg(s, rn);
7530         if (!sf) {
7531             TCGv_i64 tcg_extend = tcg_temp_new_i64();
7532 
7533             if (is_signed) {
7534                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
7535             } else {
7536                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
7537             }
7538 
7539             tcg_int = tcg_extend;
7540         }
7541 
7542         switch (type) {
7543         case 1: /* float64 */
7544             tcg_double = tcg_temp_new_i64();
7545             if (is_signed) {
7546                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
7547                                      tcg_shift, tcg_fpstatus);
7548             } else {
7549                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
7550                                      tcg_shift, tcg_fpstatus);
7551             }
7552             write_fp_dreg(s, rd, tcg_double);
7553             break;
7554 
7555         case 0: /* float32 */
7556             tcg_single = tcg_temp_new_i32();
7557             if (is_signed) {
7558                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7559                                      tcg_shift, tcg_fpstatus);
7560             } else {
7561                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7562                                      tcg_shift, tcg_fpstatus);
7563             }
7564             write_fp_sreg(s, rd, tcg_single);
7565             break;
7566 
7567         case 3: /* float16 */
7568             tcg_single = tcg_temp_new_i32();
7569             if (is_signed) {
7570                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7571                                      tcg_shift, tcg_fpstatus);
7572             } else {
7573                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7574                                      tcg_shift, tcg_fpstatus);
7575             }
7576             write_fp_sreg(s, rd, tcg_single);
7577             break;
7578 
7579         default:
7580             g_assert_not_reached();
7581         }
7582     } else {
7583         TCGv_i64 tcg_int = cpu_reg(s, rd);
7584         TCGv_i32 tcg_rmode;
7585 
7586         if (extract32(opcode, 2, 1)) {
7587             /* There are too many rounding modes to all fit into rmode,
7588              * so FCVTA[US] is a special case.
7589              */
7590             rmode = FPROUNDING_TIEAWAY;
7591         }
7592 
7593         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
7594 
7595         switch (type) {
7596         case 1: /* float64 */
7597             tcg_double = read_fp_dreg(s, rn);
7598             if (is_signed) {
7599                 if (!sf) {
7600                     gen_helper_vfp_tosld(tcg_int, tcg_double,
7601                                          tcg_shift, tcg_fpstatus);
7602                 } else {
7603                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
7604                                          tcg_shift, tcg_fpstatus);
7605                 }
7606             } else {
7607                 if (!sf) {
7608                     gen_helper_vfp_tould(tcg_int, tcg_double,
7609                                          tcg_shift, tcg_fpstatus);
7610                 } else {
7611                     gen_helper_vfp_touqd(tcg_int, tcg_double,
7612                                          tcg_shift, tcg_fpstatus);
7613                 }
7614             }
7615             if (!sf) {
7616                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7617             }
7618             break;
7619 
7620         case 0: /* float32 */
7621             tcg_single = read_fp_sreg(s, rn);
7622             if (sf) {
7623                 if (is_signed) {
7624                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
7625                                          tcg_shift, tcg_fpstatus);
7626                 } else {
7627                     gen_helper_vfp_touqs(tcg_int, tcg_single,
7628                                          tcg_shift, tcg_fpstatus);
7629                 }
7630             } else {
7631                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7632                 if (is_signed) {
7633                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7634                                          tcg_shift, tcg_fpstatus);
7635                 } else {
7636                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7637                                          tcg_shift, tcg_fpstatus);
7638                 }
7639                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7640             }
7641             break;
7642 
7643         case 3: /* float16 */
7644             tcg_single = read_fp_sreg(s, rn);
7645             if (sf) {
7646                 if (is_signed) {
7647                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7648                                          tcg_shift, tcg_fpstatus);
7649                 } else {
7650                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7651                                          tcg_shift, tcg_fpstatus);
7652                 }
7653             } else {
7654                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7655                 if (is_signed) {
7656                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7657                                          tcg_shift, tcg_fpstatus);
7658                 } else {
7659                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7660                                          tcg_shift, tcg_fpstatus);
7661                 }
7662                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7663             }
7664             break;
7665 
7666         default:
7667             g_assert_not_reached();
7668         }
7669 
7670         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7671     }
7672 }
7673 
7674 /* Floating point <-> fixed point conversions
7675  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7676  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7677  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7678  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7679  */
7680 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7681 {
7682     int rd = extract32(insn, 0, 5);
7683     int rn = extract32(insn, 5, 5);
7684     int scale = extract32(insn, 10, 6);
7685     int opcode = extract32(insn, 16, 3);
7686     int rmode = extract32(insn, 19, 2);
7687     int type = extract32(insn, 22, 2);
7688     bool sbit = extract32(insn, 29, 1);
7689     bool sf = extract32(insn, 31, 1);
7690     bool itof;
7691 
7692     if (sbit || (!sf && scale < 32)) {
7693         unallocated_encoding(s);
7694         return;
7695     }
7696 
7697     switch (type) {
7698     case 0: /* float32 */
7699     case 1: /* float64 */
7700         break;
7701     case 3: /* float16 */
7702         if (dc_isar_feature(aa64_fp16, s)) {
7703             break;
7704         }
7705         /* fallthru */
7706     default:
7707         unallocated_encoding(s);
7708         return;
7709     }
7710 
7711     switch ((rmode << 3) | opcode) {
7712     case 0x2: /* SCVTF */
7713     case 0x3: /* UCVTF */
7714         itof = true;
7715         break;
7716     case 0x18: /* FCVTZS */
7717     case 0x19: /* FCVTZU */
7718         itof = false;
7719         break;
7720     default:
7721         unallocated_encoding(s);
7722         return;
7723     }
7724 
7725     if (!fp_access_check(s)) {
7726         return;
7727     }
7728 
7729     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7730 }
7731 
7732 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7733 {
7734     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7735      * without conversion.
7736      */
7737 
7738     if (itof) {
7739         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7740         TCGv_i64 tmp;
7741 
7742         switch (type) {
7743         case 0:
7744             /* 32 bit */
7745             tmp = tcg_temp_new_i64();
7746             tcg_gen_ext32u_i64(tmp, tcg_rn);
7747             write_fp_dreg(s, rd, tmp);
7748             break;
7749         case 1:
7750             /* 64 bit */
7751             write_fp_dreg(s, rd, tcg_rn);
7752             break;
7753         case 2:
7754             /* 64 bit to top half. */
7755             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
7756             clear_vec_high(s, true, rd);
7757             break;
7758         case 3:
7759             /* 16 bit */
7760             tmp = tcg_temp_new_i64();
7761             tcg_gen_ext16u_i64(tmp, tcg_rn);
7762             write_fp_dreg(s, rd, tmp);
7763             break;
7764         default:
7765             g_assert_not_reached();
7766         }
7767     } else {
7768         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7769 
7770         switch (type) {
7771         case 0:
7772             /* 32 bit */
7773             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
7774             break;
7775         case 1:
7776             /* 64 bit */
7777             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
7778             break;
7779         case 2:
7780             /* 64 bits from top half */
7781             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
7782             break;
7783         case 3:
7784             /* 16 bit */
7785             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
7786             break;
7787         default:
7788             g_assert_not_reached();
7789         }
7790     }
7791 }
7792 
7793 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7794 {
7795     TCGv_i64 t = read_fp_dreg(s, rn);
7796     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7797 
7798     gen_helper_fjcvtzs(t, t, fpstatus);
7799 
7800     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7801     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7802     tcg_gen_movi_i32(cpu_CF, 0);
7803     tcg_gen_movi_i32(cpu_NF, 0);
7804     tcg_gen_movi_i32(cpu_VF, 0);
7805 }
7806 
7807 /* Floating point <-> integer conversions
7808  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7809  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7810  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7811  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7812  */
7813 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7814 {
7815     int rd = extract32(insn, 0, 5);
7816     int rn = extract32(insn, 5, 5);
7817     int opcode = extract32(insn, 16, 3);
7818     int rmode = extract32(insn, 19, 2);
7819     int type = extract32(insn, 22, 2);
7820     bool sbit = extract32(insn, 29, 1);
7821     bool sf = extract32(insn, 31, 1);
7822     bool itof = false;
7823 
7824     if (sbit) {
7825         goto do_unallocated;
7826     }
7827 
7828     switch (opcode) {
7829     case 2: /* SCVTF */
7830     case 3: /* UCVTF */
7831         itof = true;
7832         /* fallthru */
7833     case 4: /* FCVTAS */
7834     case 5: /* FCVTAU */
7835         if (rmode != 0) {
7836             goto do_unallocated;
7837         }
7838         /* fallthru */
7839     case 0: /* FCVT[NPMZ]S */
7840     case 1: /* FCVT[NPMZ]U */
7841         switch (type) {
7842         case 0: /* float32 */
7843         case 1: /* float64 */
7844             break;
7845         case 3: /* float16 */
7846             if (!dc_isar_feature(aa64_fp16, s)) {
7847                 goto do_unallocated;
7848             }
7849             break;
7850         default:
7851             goto do_unallocated;
7852         }
7853         if (!fp_access_check(s)) {
7854             return;
7855         }
7856         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7857         break;
7858 
7859     default:
7860         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7861         case 0b01100110: /* FMOV half <-> 32-bit int */
7862         case 0b01100111:
7863         case 0b11100110: /* FMOV half <-> 64-bit int */
7864         case 0b11100111:
7865             if (!dc_isar_feature(aa64_fp16, s)) {
7866                 goto do_unallocated;
7867             }
7868             /* fallthru */
7869         case 0b00000110: /* FMOV 32-bit */
7870         case 0b00000111:
7871         case 0b10100110: /* FMOV 64-bit */
7872         case 0b10100111:
7873         case 0b11001110: /* FMOV top half of 128-bit */
7874         case 0b11001111:
7875             if (!fp_access_check(s)) {
7876                 return;
7877             }
7878             itof = opcode & 1;
7879             handle_fmov(s, rd, rn, type, itof);
7880             break;
7881 
7882         case 0b00111110: /* FJCVTZS */
7883             if (!dc_isar_feature(aa64_jscvt, s)) {
7884                 goto do_unallocated;
7885             } else if (fp_access_check(s)) {
7886                 handle_fjcvtzs(s, rd, rn);
7887             }
7888             break;
7889 
7890         default:
7891         do_unallocated:
7892             unallocated_encoding(s);
7893             return;
7894         }
7895         break;
7896     }
7897 }
7898 
7899 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7900  *   31  30  29 28     25 24                          0
7901  * +---+---+---+---------+-----------------------------+
7902  * |   | 0 |   | 1 1 1 1 |                             |
7903  * +---+---+---+---------+-----------------------------+
7904  */
7905 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7906 {
7907     if (extract32(insn, 24, 1)) {
7908         /* Floating point data-processing (3 source) */
7909         disas_fp_3src(s, insn);
7910     } else if (extract32(insn, 21, 1) == 0) {
7911         /* Floating point to fixed point conversions */
7912         disas_fp_fixed_conv(s, insn);
7913     } else {
7914         switch (extract32(insn, 10, 2)) {
7915         case 1:
7916             /* Floating point conditional compare */
7917             disas_fp_ccomp(s, insn);
7918             break;
7919         case 2:
7920             /* Floating point data-processing (2 source) */
7921             unallocated_encoding(s); /* in decodetree */
7922             break;
7923         case 3:
7924             /* Floating point conditional select */
7925             disas_fp_csel(s, insn);
7926             break;
7927         case 0:
7928             switch (ctz32(extract32(insn, 12, 4))) {
7929             case 0: /* [15:12] == xxx1 */
7930                 /* Floating point immediate */
7931                 disas_fp_imm(s, insn);
7932                 break;
7933             case 1: /* [15:12] == xx10 */
7934                 /* Floating point compare */
7935                 disas_fp_compare(s, insn);
7936                 break;
7937             case 2: /* [15:12] == x100 */
7938                 /* Floating point data-processing (1 source) */
7939                 disas_fp_1src(s, insn);
7940                 break;
7941             case 3: /* [15:12] == 1000 */
7942                 unallocated_encoding(s);
7943                 break;
7944             default: /* [15:12] == 0000 */
7945                 /* Floating point <-> integer conversions */
7946                 disas_fp_int_conv(s, insn);
7947                 break;
7948             }
7949             break;
7950         }
7951     }
7952 }
7953 
7954 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7955                      int pos)
7956 {
7957     /* Extract 64 bits from the middle of two concatenated 64 bit
7958      * vector register slices left:right. The extracted bits start
7959      * at 'pos' bits into the right (least significant) side.
7960      * We return the result in tcg_right, and guarantee not to
7961      * trash tcg_left.
7962      */
7963     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7964     assert(pos > 0 && pos < 64);
7965 
7966     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7967     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7968     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7969 }
7970 
7971 /* EXT
7972  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7973  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7974  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7975  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7976  */
7977 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7978 {
7979     int is_q = extract32(insn, 30, 1);
7980     int op2 = extract32(insn, 22, 2);
7981     int imm4 = extract32(insn, 11, 4);
7982     int rm = extract32(insn, 16, 5);
7983     int rn = extract32(insn, 5, 5);
7984     int rd = extract32(insn, 0, 5);
7985     int pos = imm4 << 3;
7986     TCGv_i64 tcg_resl, tcg_resh;
7987 
7988     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7989         unallocated_encoding(s);
7990         return;
7991     }
7992 
7993     if (!fp_access_check(s)) {
7994         return;
7995     }
7996 
7997     tcg_resh = tcg_temp_new_i64();
7998     tcg_resl = tcg_temp_new_i64();
7999 
8000     /* Vd gets bits starting at pos bits into Vm:Vn. This is
8001      * either extracting 128 bits from a 128:128 concatenation, or
8002      * extracting 64 bits from a 64:64 concatenation.
8003      */
8004     if (!is_q) {
8005         read_vec_element(s, tcg_resl, rn, 0, MO_64);
8006         if (pos != 0) {
8007             read_vec_element(s, tcg_resh, rm, 0, MO_64);
8008             do_ext64(s, tcg_resh, tcg_resl, pos);
8009         }
8010     } else {
8011         TCGv_i64 tcg_hh;
8012         typedef struct {
8013             int reg;
8014             int elt;
8015         } EltPosns;
8016         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
8017         EltPosns *elt = eltposns;
8018 
8019         if (pos >= 64) {
8020             elt++;
8021             pos -= 64;
8022         }
8023 
8024         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
8025         elt++;
8026         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
8027         elt++;
8028         if (pos != 0) {
8029             do_ext64(s, tcg_resh, tcg_resl, pos);
8030             tcg_hh = tcg_temp_new_i64();
8031             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
8032             do_ext64(s, tcg_hh, tcg_resh, pos);
8033         }
8034     }
8035 
8036     write_vec_element(s, tcg_resl, rd, 0, MO_64);
8037     if (is_q) {
8038         write_vec_element(s, tcg_resh, rd, 1, MO_64);
8039     }
8040     clear_vec_high(s, is_q, rd);
8041 }
8042 
8043 /* TBL/TBX
8044  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
8045  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
8046  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
8047  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
8048  */
8049 static void disas_simd_tb(DisasContext *s, uint32_t insn)
8050 {
8051     int op2 = extract32(insn, 22, 2);
8052     int is_q = extract32(insn, 30, 1);
8053     int rm = extract32(insn, 16, 5);
8054     int rn = extract32(insn, 5, 5);
8055     int rd = extract32(insn, 0, 5);
8056     int is_tbx = extract32(insn, 12, 1);
8057     int len = (extract32(insn, 13, 2) + 1) * 16;
8058 
8059     if (op2 != 0) {
8060         unallocated_encoding(s);
8061         return;
8062     }
8063 
8064     if (!fp_access_check(s)) {
8065         return;
8066     }
8067 
8068     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
8069                        vec_full_reg_offset(s, rm), tcg_env,
8070                        is_q ? 16 : 8, vec_full_reg_size(s),
8071                        (len << 6) | (is_tbx << 5) | rn,
8072                        gen_helper_simd_tblx);
8073 }
8074 
8075 /* ZIP/UZP/TRN
8076  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
8077  * +---+---+-------------+------+---+------+---+------------------+------+
8078  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
8079  * +---+---+-------------+------+---+------+---+------------------+------+
8080  */
8081 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
8082 {
8083     int rd = extract32(insn, 0, 5);
8084     int rn = extract32(insn, 5, 5);
8085     int rm = extract32(insn, 16, 5);
8086     int size = extract32(insn, 22, 2);
8087     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
8088      * bit 2 indicates 1 vs 2 variant of the insn.
8089      */
8090     int opcode = extract32(insn, 12, 2);
8091     bool part = extract32(insn, 14, 1);
8092     bool is_q = extract32(insn, 30, 1);
8093     int esize = 8 << size;
8094     int i;
8095     int datasize = is_q ? 128 : 64;
8096     int elements = datasize / esize;
8097     TCGv_i64 tcg_res[2], tcg_ele;
8098 
8099     if (opcode == 0 || (size == 3 && !is_q)) {
8100         unallocated_encoding(s);
8101         return;
8102     }
8103 
8104     if (!fp_access_check(s)) {
8105         return;
8106     }
8107 
8108     tcg_res[0] = tcg_temp_new_i64();
8109     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
8110     tcg_ele = tcg_temp_new_i64();
8111 
8112     for (i = 0; i < elements; i++) {
8113         int o, w;
8114 
8115         switch (opcode) {
8116         case 1: /* UZP1/2 */
8117         {
8118             int midpoint = elements / 2;
8119             if (i < midpoint) {
8120                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
8121             } else {
8122                 read_vec_element(s, tcg_ele, rm,
8123                                  2 * (i - midpoint) + part, size);
8124             }
8125             break;
8126         }
8127         case 2: /* TRN1/2 */
8128             if (i & 1) {
8129                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
8130             } else {
8131                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
8132             }
8133             break;
8134         case 3: /* ZIP1/2 */
8135         {
8136             int base = part * elements / 2;
8137             if (i & 1) {
8138                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
8139             } else {
8140                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
8141             }
8142             break;
8143         }
8144         default:
8145             g_assert_not_reached();
8146         }
8147 
8148         w = (i * esize) / 64;
8149         o = (i * esize) % 64;
8150         if (o == 0) {
8151             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
8152         } else {
8153             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
8154             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
8155         }
8156     }
8157 
8158     for (i = 0; i <= is_q; ++i) {
8159         write_vec_element(s, tcg_res[i], rd, i, MO_64);
8160     }
8161     clear_vec_high(s, is_q, rd);
8162 }
8163 
8164 /*
8165  * do_reduction_op helper
8166  *
8167  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
8168  * important for correct NaN propagation that we do these
8169  * operations in exactly the order specified by the pseudocode.
8170  *
8171  * This is a recursive function, TCG temps should be freed by the
8172  * calling function once it is done with the values.
8173  */
8174 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
8175                                 int esize, int size, int vmap, TCGv_ptr fpst)
8176 {
8177     if (esize == size) {
8178         int element;
8179         MemOp msize = esize == 16 ? MO_16 : MO_32;
8180         TCGv_i32 tcg_elem;
8181 
8182         /* We should have one register left here */
8183         assert(ctpop8(vmap) == 1);
8184         element = ctz32(vmap);
8185         assert(element < 8);
8186 
8187         tcg_elem = tcg_temp_new_i32();
8188         read_vec_element_i32(s, tcg_elem, rn, element, msize);
8189         return tcg_elem;
8190     } else {
8191         int bits = size / 2;
8192         int shift = ctpop8(vmap) / 2;
8193         int vmap_lo = (vmap >> shift) & vmap;
8194         int vmap_hi = (vmap & ~vmap_lo);
8195         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
8196 
8197         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
8198         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
8199         tcg_res = tcg_temp_new_i32();
8200 
8201         switch (fpopcode) {
8202         case 0x0c: /* fmaxnmv half-precision */
8203             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
8204             break;
8205         case 0x0f: /* fmaxv half-precision */
8206             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
8207             break;
8208         case 0x1c: /* fminnmv half-precision */
8209             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
8210             break;
8211         case 0x1f: /* fminv half-precision */
8212             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
8213             break;
8214         case 0x2c: /* fmaxnmv */
8215             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
8216             break;
8217         case 0x2f: /* fmaxv */
8218             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
8219             break;
8220         case 0x3c: /* fminnmv */
8221             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
8222             break;
8223         case 0x3f: /* fminv */
8224             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
8225             break;
8226         default:
8227             g_assert_not_reached();
8228         }
8229         return tcg_res;
8230     }
8231 }
8232 
8233 /* AdvSIMD across lanes
8234  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8235  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8236  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8237  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8238  */
8239 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
8240 {
8241     int rd = extract32(insn, 0, 5);
8242     int rn = extract32(insn, 5, 5);
8243     int size = extract32(insn, 22, 2);
8244     int opcode = extract32(insn, 12, 5);
8245     bool is_q = extract32(insn, 30, 1);
8246     bool is_u = extract32(insn, 29, 1);
8247     bool is_fp = false;
8248     bool is_min = false;
8249     int esize;
8250     int elements;
8251     int i;
8252     TCGv_i64 tcg_res, tcg_elt;
8253 
8254     switch (opcode) {
8255     case 0x1b: /* ADDV */
8256         if (is_u) {
8257             unallocated_encoding(s);
8258             return;
8259         }
8260         /* fall through */
8261     case 0x3: /* SADDLV, UADDLV */
8262     case 0xa: /* SMAXV, UMAXV */
8263     case 0x1a: /* SMINV, UMINV */
8264         if (size == 3 || (size == 2 && !is_q)) {
8265             unallocated_encoding(s);
8266             return;
8267         }
8268         break;
8269     case 0xc: /* FMAXNMV, FMINNMV */
8270     case 0xf: /* FMAXV, FMINV */
8271         /* Bit 1 of size field encodes min vs max and the actual size
8272          * depends on the encoding of the U bit. If not set (and FP16
8273          * enabled) then we do half-precision float instead of single
8274          * precision.
8275          */
8276         is_min = extract32(size, 1, 1);
8277         is_fp = true;
8278         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
8279             size = 1;
8280         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
8281             unallocated_encoding(s);
8282             return;
8283         } else {
8284             size = 2;
8285         }
8286         break;
8287     default:
8288         unallocated_encoding(s);
8289         return;
8290     }
8291 
8292     if (!fp_access_check(s)) {
8293         return;
8294     }
8295 
8296     esize = 8 << size;
8297     elements = (is_q ? 128 : 64) / esize;
8298 
8299     tcg_res = tcg_temp_new_i64();
8300     tcg_elt = tcg_temp_new_i64();
8301 
8302     /* These instructions operate across all lanes of a vector
8303      * to produce a single result. We can guarantee that a 64
8304      * bit intermediate is sufficient:
8305      *  + for [US]ADDLV the maximum element size is 32 bits, and
8306      *    the result type is 64 bits
8307      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
8308      *    same as the element size, which is 32 bits at most
8309      * For the integer operations we can choose to work at 64
8310      * or 32 bits and truncate at the end; for simplicity
8311      * we use 64 bits always. The floating point
8312      * ops do require 32 bit intermediates, though.
8313      */
8314     if (!is_fp) {
8315         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
8316 
8317         for (i = 1; i < elements; i++) {
8318             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
8319 
8320             switch (opcode) {
8321             case 0x03: /* SADDLV / UADDLV */
8322             case 0x1b: /* ADDV */
8323                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
8324                 break;
8325             case 0x0a: /* SMAXV / UMAXV */
8326                 if (is_u) {
8327                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
8328                 } else {
8329                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
8330                 }
8331                 break;
8332             case 0x1a: /* SMINV / UMINV */
8333                 if (is_u) {
8334                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
8335                 } else {
8336                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
8337                 }
8338                 break;
8339             default:
8340                 g_assert_not_reached();
8341             }
8342 
8343         }
8344     } else {
8345         /* Floating point vector reduction ops which work across 32
8346          * bit (single) or 16 bit (half-precision) intermediates.
8347          * Note that correct NaN propagation requires that we do these
8348          * operations in exactly the order specified by the pseudocode.
8349          */
8350         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8351         int fpopcode = opcode | is_min << 4 | is_u << 5;
8352         int vmap = (1 << elements) - 1;
8353         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
8354                                              (is_q ? 128 : 64), vmap, fpst);
8355         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
8356     }
8357 
8358     /* Now truncate the result to the width required for the final output */
8359     if (opcode == 0x03) {
8360         /* SADDLV, UADDLV: result is 2*esize */
8361         size++;
8362     }
8363 
8364     switch (size) {
8365     case 0:
8366         tcg_gen_ext8u_i64(tcg_res, tcg_res);
8367         break;
8368     case 1:
8369         tcg_gen_ext16u_i64(tcg_res, tcg_res);
8370         break;
8371     case 2:
8372         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8373         break;
8374     case 3:
8375         break;
8376     default:
8377         g_assert_not_reached();
8378     }
8379 
8380     write_fp_dreg(s, rd, tcg_res);
8381 }
8382 
8383 /* AdvSIMD modified immediate
8384  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8385  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8386  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8387  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8388  *
8389  * There are a number of operations that can be carried out here:
8390  *   MOVI - move (shifted) imm into register
8391  *   MVNI - move inverted (shifted) imm into register
8392  *   ORR  - bitwise OR of (shifted) imm with register
8393  *   BIC  - bitwise clear of (shifted) imm with register
8394  * With ARMv8.2 we also have:
8395  *   FMOV half-precision
8396  */
8397 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8398 {
8399     int rd = extract32(insn, 0, 5);
8400     int cmode = extract32(insn, 12, 4);
8401     int o2 = extract32(insn, 11, 1);
8402     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8403     bool is_neg = extract32(insn, 29, 1);
8404     bool is_q = extract32(insn, 30, 1);
8405     uint64_t imm = 0;
8406 
8407     if (o2) {
8408         if (cmode != 0xf || is_neg) {
8409             unallocated_encoding(s);
8410             return;
8411         }
8412         /* FMOV (vector, immediate) - half-precision */
8413         if (!dc_isar_feature(aa64_fp16, s)) {
8414             unallocated_encoding(s);
8415             return;
8416         }
8417         imm = vfp_expand_imm(MO_16, abcdefgh);
8418         /* now duplicate across the lanes */
8419         imm = dup_const(MO_16, imm);
8420     } else {
8421         if (cmode == 0xf && is_neg && !is_q) {
8422             unallocated_encoding(s);
8423             return;
8424         }
8425         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8426     }
8427 
8428     if (!fp_access_check(s)) {
8429         return;
8430     }
8431 
8432     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8433         /* MOVI or MVNI, with MVNI negation handled above.  */
8434         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8435                              vec_full_reg_size(s), imm);
8436     } else {
8437         /* ORR or BIC, with BIC negation to AND handled above.  */
8438         if (is_neg) {
8439             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8440         } else {
8441             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8442         }
8443     }
8444 }
8445 
8446 /*
8447  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8448  *
8449  * This code is handles the common shifting code and is used by both
8450  * the vector and scalar code.
8451  */
8452 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8453                                     TCGv_i64 tcg_rnd, bool accumulate,
8454                                     bool is_u, int size, int shift)
8455 {
8456     bool extended_result = false;
8457     bool round = tcg_rnd != NULL;
8458     int ext_lshift = 0;
8459     TCGv_i64 tcg_src_hi;
8460 
8461     if (round && size == 3) {
8462         extended_result = true;
8463         ext_lshift = 64 - shift;
8464         tcg_src_hi = tcg_temp_new_i64();
8465     } else if (shift == 64) {
8466         if (!accumulate && is_u) {
8467             /* result is zero */
8468             tcg_gen_movi_i64(tcg_res, 0);
8469             return;
8470         }
8471     }
8472 
8473     /* Deal with the rounding step */
8474     if (round) {
8475         if (extended_result) {
8476             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8477             if (!is_u) {
8478                 /* take care of sign extending tcg_res */
8479                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8480                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8481                                  tcg_src, tcg_src_hi,
8482                                  tcg_rnd, tcg_zero);
8483             } else {
8484                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8485                                  tcg_src, tcg_zero,
8486                                  tcg_rnd, tcg_zero);
8487             }
8488         } else {
8489             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8490         }
8491     }
8492 
8493     /* Now do the shift right */
8494     if (round && extended_result) {
8495         /* extended case, >64 bit precision required */
8496         if (ext_lshift == 0) {
8497             /* special case, only high bits matter */
8498             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8499         } else {
8500             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8501             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8502             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8503         }
8504     } else {
8505         if (is_u) {
8506             if (shift == 64) {
8507                 /* essentially shifting in 64 zeros */
8508                 tcg_gen_movi_i64(tcg_src, 0);
8509             } else {
8510                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8511             }
8512         } else {
8513             if (shift == 64) {
8514                 /* effectively extending the sign-bit */
8515                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8516             } else {
8517                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8518             }
8519         }
8520     }
8521 
8522     if (accumulate) {
8523         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8524     } else {
8525         tcg_gen_mov_i64(tcg_res, tcg_src);
8526     }
8527 }
8528 
8529 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8530 static void handle_scalar_simd_shri(DisasContext *s,
8531                                     bool is_u, int immh, int immb,
8532                                     int opcode, int rn, int rd)
8533 {
8534     const int size = 3;
8535     int immhb = immh << 3 | immb;
8536     int shift = 2 * (8 << size) - immhb;
8537     bool accumulate = false;
8538     bool round = false;
8539     bool insert = false;
8540     TCGv_i64 tcg_rn;
8541     TCGv_i64 tcg_rd;
8542     TCGv_i64 tcg_round;
8543 
8544     if (!extract32(immh, 3, 1)) {
8545         unallocated_encoding(s);
8546         return;
8547     }
8548 
8549     if (!fp_access_check(s)) {
8550         return;
8551     }
8552 
8553     switch (opcode) {
8554     case 0x02: /* SSRA / USRA (accumulate) */
8555         accumulate = true;
8556         break;
8557     case 0x04: /* SRSHR / URSHR (rounding) */
8558         round = true;
8559         break;
8560     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8561         accumulate = round = true;
8562         break;
8563     case 0x08: /* SRI */
8564         insert = true;
8565         break;
8566     }
8567 
8568     if (round) {
8569         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8570     } else {
8571         tcg_round = NULL;
8572     }
8573 
8574     tcg_rn = read_fp_dreg(s, rn);
8575     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8576 
8577     if (insert) {
8578         /* shift count same as element size is valid but does nothing;
8579          * special case to avoid potential shift by 64.
8580          */
8581         int esize = 8 << size;
8582         if (shift != esize) {
8583             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8584             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8585         }
8586     } else {
8587         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8588                                 accumulate, is_u, size, shift);
8589     }
8590 
8591     write_fp_dreg(s, rd, tcg_rd);
8592 }
8593 
8594 /* SHL/SLI - Scalar shift left */
8595 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8596                                     int immh, int immb, int opcode,
8597                                     int rn, int rd)
8598 {
8599     int size = 32 - clz32(immh) - 1;
8600     int immhb = immh << 3 | immb;
8601     int shift = immhb - (8 << size);
8602     TCGv_i64 tcg_rn;
8603     TCGv_i64 tcg_rd;
8604 
8605     if (!extract32(immh, 3, 1)) {
8606         unallocated_encoding(s);
8607         return;
8608     }
8609 
8610     if (!fp_access_check(s)) {
8611         return;
8612     }
8613 
8614     tcg_rn = read_fp_dreg(s, rn);
8615     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8616 
8617     if (insert) {
8618         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8619     } else {
8620         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8621     }
8622 
8623     write_fp_dreg(s, rd, tcg_rd);
8624 }
8625 
8626 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8627  * (signed/unsigned) narrowing */
8628 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8629                                    bool is_u_shift, bool is_u_narrow,
8630                                    int immh, int immb, int opcode,
8631                                    int rn, int rd)
8632 {
8633     int immhb = immh << 3 | immb;
8634     int size = 32 - clz32(immh) - 1;
8635     int esize = 8 << size;
8636     int shift = (2 * esize) - immhb;
8637     int elements = is_scalar ? 1 : (64 / esize);
8638     bool round = extract32(opcode, 0, 1);
8639     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8640     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8641     TCGv_i32 tcg_rd_narrowed;
8642     TCGv_i64 tcg_final;
8643 
8644     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8645         { gen_helper_neon_narrow_sat_s8,
8646           gen_helper_neon_unarrow_sat8 },
8647         { gen_helper_neon_narrow_sat_s16,
8648           gen_helper_neon_unarrow_sat16 },
8649         { gen_helper_neon_narrow_sat_s32,
8650           gen_helper_neon_unarrow_sat32 },
8651         { NULL, NULL },
8652     };
8653     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8654         gen_helper_neon_narrow_sat_u8,
8655         gen_helper_neon_narrow_sat_u16,
8656         gen_helper_neon_narrow_sat_u32,
8657         NULL
8658     };
8659     NeonGenNarrowEnvFn *narrowfn;
8660 
8661     int i;
8662 
8663     assert(size < 4);
8664 
8665     if (extract32(immh, 3, 1)) {
8666         unallocated_encoding(s);
8667         return;
8668     }
8669 
8670     if (!fp_access_check(s)) {
8671         return;
8672     }
8673 
8674     if (is_u_shift) {
8675         narrowfn = unsigned_narrow_fns[size];
8676     } else {
8677         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8678     }
8679 
8680     tcg_rn = tcg_temp_new_i64();
8681     tcg_rd = tcg_temp_new_i64();
8682     tcg_rd_narrowed = tcg_temp_new_i32();
8683     tcg_final = tcg_temp_new_i64();
8684 
8685     if (round) {
8686         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8687     } else {
8688         tcg_round = NULL;
8689     }
8690 
8691     for (i = 0; i < elements; i++) {
8692         read_vec_element(s, tcg_rn, rn, i, ldop);
8693         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8694                                 false, is_u_shift, size+1, shift);
8695         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8696         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8697         if (i == 0) {
8698             tcg_gen_extract_i64(tcg_final, tcg_rd, 0, esize);
8699         } else {
8700             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8701         }
8702     }
8703 
8704     if (!is_q) {
8705         write_vec_element(s, tcg_final, rd, 0, MO_64);
8706     } else {
8707         write_vec_element(s, tcg_final, rd, 1, MO_64);
8708     }
8709     clear_vec_high(s, is_q, rd);
8710 }
8711 
8712 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8713 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8714                              bool src_unsigned, bool dst_unsigned,
8715                              int immh, int immb, int rn, int rd)
8716 {
8717     int immhb = immh << 3 | immb;
8718     int size = 32 - clz32(immh) - 1;
8719     int shift = immhb - (8 << size);
8720     int pass;
8721 
8722     assert(immh != 0);
8723     assert(!(scalar && is_q));
8724 
8725     if (!scalar) {
8726         if (!is_q && extract32(immh, 3, 1)) {
8727             unallocated_encoding(s);
8728             return;
8729         }
8730 
8731         /* Since we use the variable-shift helpers we must
8732          * replicate the shift count into each element of
8733          * the tcg_shift value.
8734          */
8735         switch (size) {
8736         case 0:
8737             shift |= shift << 8;
8738             /* fall through */
8739         case 1:
8740             shift |= shift << 16;
8741             break;
8742         case 2:
8743         case 3:
8744             break;
8745         default:
8746             g_assert_not_reached();
8747         }
8748     }
8749 
8750     if (!fp_access_check(s)) {
8751         return;
8752     }
8753 
8754     if (size == 3) {
8755         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8756         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8757             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8758             { NULL, gen_helper_neon_qshl_u64 },
8759         };
8760         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8761         int maxpass = is_q ? 2 : 1;
8762 
8763         for (pass = 0; pass < maxpass; pass++) {
8764             TCGv_i64 tcg_op = tcg_temp_new_i64();
8765 
8766             read_vec_element(s, tcg_op, rn, pass, MO_64);
8767             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8768             write_vec_element(s, tcg_op, rd, pass, MO_64);
8769         }
8770         clear_vec_high(s, is_q, rd);
8771     } else {
8772         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8773         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8774             {
8775                 { gen_helper_neon_qshl_s8,
8776                   gen_helper_neon_qshl_s16,
8777                   gen_helper_neon_qshl_s32 },
8778                 { gen_helper_neon_qshlu_s8,
8779                   gen_helper_neon_qshlu_s16,
8780                   gen_helper_neon_qshlu_s32 }
8781             }, {
8782                 { NULL, NULL, NULL },
8783                 { gen_helper_neon_qshl_u8,
8784                   gen_helper_neon_qshl_u16,
8785                   gen_helper_neon_qshl_u32 }
8786             }
8787         };
8788         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8789         MemOp memop = scalar ? size : MO_32;
8790         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8791 
8792         for (pass = 0; pass < maxpass; pass++) {
8793             TCGv_i32 tcg_op = tcg_temp_new_i32();
8794 
8795             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8796             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8797             if (scalar) {
8798                 switch (size) {
8799                 case 0:
8800                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8801                     break;
8802                 case 1:
8803                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8804                     break;
8805                 case 2:
8806                     break;
8807                 default:
8808                     g_assert_not_reached();
8809                 }
8810                 write_fp_sreg(s, rd, tcg_op);
8811             } else {
8812                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8813             }
8814         }
8815 
8816         if (!scalar) {
8817             clear_vec_high(s, is_q, rd);
8818         }
8819     }
8820 }
8821 
8822 /* Common vector code for handling integer to FP conversion */
8823 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8824                                    int elements, int is_signed,
8825                                    int fracbits, int size)
8826 {
8827     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8828     TCGv_i32 tcg_shift = NULL;
8829 
8830     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8831     int pass;
8832 
8833     if (fracbits || size == MO_64) {
8834         tcg_shift = tcg_constant_i32(fracbits);
8835     }
8836 
8837     if (size == MO_64) {
8838         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8839         TCGv_i64 tcg_double = tcg_temp_new_i64();
8840 
8841         for (pass = 0; pass < elements; pass++) {
8842             read_vec_element(s, tcg_int64, rn, pass, mop);
8843 
8844             if (is_signed) {
8845                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8846                                      tcg_shift, tcg_fpst);
8847             } else {
8848                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8849                                      tcg_shift, tcg_fpst);
8850             }
8851             if (elements == 1) {
8852                 write_fp_dreg(s, rd, tcg_double);
8853             } else {
8854                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8855             }
8856         }
8857     } else {
8858         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8859         TCGv_i32 tcg_float = tcg_temp_new_i32();
8860 
8861         for (pass = 0; pass < elements; pass++) {
8862             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8863 
8864             switch (size) {
8865             case MO_32:
8866                 if (fracbits) {
8867                     if (is_signed) {
8868                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8869                                              tcg_shift, tcg_fpst);
8870                     } else {
8871                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8872                                              tcg_shift, tcg_fpst);
8873                     }
8874                 } else {
8875                     if (is_signed) {
8876                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8877                     } else {
8878                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8879                     }
8880                 }
8881                 break;
8882             case MO_16:
8883                 if (fracbits) {
8884                     if (is_signed) {
8885                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8886                                              tcg_shift, tcg_fpst);
8887                     } else {
8888                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8889                                              tcg_shift, tcg_fpst);
8890                     }
8891                 } else {
8892                     if (is_signed) {
8893                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8894                     } else {
8895                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8896                     }
8897                 }
8898                 break;
8899             default:
8900                 g_assert_not_reached();
8901             }
8902 
8903             if (elements == 1) {
8904                 write_fp_sreg(s, rd, tcg_float);
8905             } else {
8906                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8907             }
8908         }
8909     }
8910 
8911     clear_vec_high(s, elements << size == 16, rd);
8912 }
8913 
8914 /* UCVTF/SCVTF - Integer to FP conversion */
8915 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8916                                          bool is_q, bool is_u,
8917                                          int immh, int immb, int opcode,
8918                                          int rn, int rd)
8919 {
8920     int size, elements, fracbits;
8921     int immhb = immh << 3 | immb;
8922 
8923     if (immh & 8) {
8924         size = MO_64;
8925         if (!is_scalar && !is_q) {
8926             unallocated_encoding(s);
8927             return;
8928         }
8929     } else if (immh & 4) {
8930         size = MO_32;
8931     } else if (immh & 2) {
8932         size = MO_16;
8933         if (!dc_isar_feature(aa64_fp16, s)) {
8934             unallocated_encoding(s);
8935             return;
8936         }
8937     } else {
8938         /* immh == 0 would be a failure of the decode logic */
8939         g_assert(immh == 1);
8940         unallocated_encoding(s);
8941         return;
8942     }
8943 
8944     if (is_scalar) {
8945         elements = 1;
8946     } else {
8947         elements = (8 << is_q) >> size;
8948     }
8949     fracbits = (16 << size) - immhb;
8950 
8951     if (!fp_access_check(s)) {
8952         return;
8953     }
8954 
8955     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8956 }
8957 
8958 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8959 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8960                                          bool is_q, bool is_u,
8961                                          int immh, int immb, int rn, int rd)
8962 {
8963     int immhb = immh << 3 | immb;
8964     int pass, size, fracbits;
8965     TCGv_ptr tcg_fpstatus;
8966     TCGv_i32 tcg_rmode, tcg_shift;
8967 
8968     if (immh & 0x8) {
8969         size = MO_64;
8970         if (!is_scalar && !is_q) {
8971             unallocated_encoding(s);
8972             return;
8973         }
8974     } else if (immh & 0x4) {
8975         size = MO_32;
8976     } else if (immh & 0x2) {
8977         size = MO_16;
8978         if (!dc_isar_feature(aa64_fp16, s)) {
8979             unallocated_encoding(s);
8980             return;
8981         }
8982     } else {
8983         /* Should have split out AdvSIMD modified immediate earlier.  */
8984         assert(immh == 1);
8985         unallocated_encoding(s);
8986         return;
8987     }
8988 
8989     if (!fp_access_check(s)) {
8990         return;
8991     }
8992 
8993     assert(!(is_scalar && is_q));
8994 
8995     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8996     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8997     fracbits = (16 << size) - immhb;
8998     tcg_shift = tcg_constant_i32(fracbits);
8999 
9000     if (size == MO_64) {
9001         int maxpass = is_scalar ? 1 : 2;
9002 
9003         for (pass = 0; pass < maxpass; pass++) {
9004             TCGv_i64 tcg_op = tcg_temp_new_i64();
9005 
9006             read_vec_element(s, tcg_op, rn, pass, MO_64);
9007             if (is_u) {
9008                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9009             } else {
9010                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9011             }
9012             write_vec_element(s, tcg_op, rd, pass, MO_64);
9013         }
9014         clear_vec_high(s, is_q, rd);
9015     } else {
9016         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
9017         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
9018 
9019         switch (size) {
9020         case MO_16:
9021             if (is_u) {
9022                 fn = gen_helper_vfp_touhh;
9023             } else {
9024                 fn = gen_helper_vfp_toshh;
9025             }
9026             break;
9027         case MO_32:
9028             if (is_u) {
9029                 fn = gen_helper_vfp_touls;
9030             } else {
9031                 fn = gen_helper_vfp_tosls;
9032             }
9033             break;
9034         default:
9035             g_assert_not_reached();
9036         }
9037 
9038         for (pass = 0; pass < maxpass; pass++) {
9039             TCGv_i32 tcg_op = tcg_temp_new_i32();
9040 
9041             read_vec_element_i32(s, tcg_op, rn, pass, size);
9042             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9043             if (is_scalar) {
9044                 if (size == MO_16 && !is_u) {
9045                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
9046                 }
9047                 write_fp_sreg(s, rd, tcg_op);
9048             } else {
9049                 write_vec_element_i32(s, tcg_op, rd, pass, size);
9050             }
9051         }
9052         if (!is_scalar) {
9053             clear_vec_high(s, is_q, rd);
9054         }
9055     }
9056 
9057     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
9058 }
9059 
9060 /* AdvSIMD scalar shift by immediate
9061  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
9062  * +-----+---+-------------+------+------+--------+---+------+------+
9063  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
9064  * +-----+---+-------------+------+------+--------+---+------+------+
9065  *
9066  * This is the scalar version so it works on a fixed sized registers
9067  */
9068 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9069 {
9070     int rd = extract32(insn, 0, 5);
9071     int rn = extract32(insn, 5, 5);
9072     int opcode = extract32(insn, 11, 5);
9073     int immb = extract32(insn, 16, 3);
9074     int immh = extract32(insn, 19, 4);
9075     bool is_u = extract32(insn, 29, 1);
9076 
9077     if (immh == 0) {
9078         unallocated_encoding(s);
9079         return;
9080     }
9081 
9082     switch (opcode) {
9083     case 0x08: /* SRI */
9084         if (!is_u) {
9085             unallocated_encoding(s);
9086             return;
9087         }
9088         /* fall through */
9089     case 0x00: /* SSHR / USHR */
9090     case 0x02: /* SSRA / USRA */
9091     case 0x04: /* SRSHR / URSHR */
9092     case 0x06: /* SRSRA / URSRA */
9093         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9094         break;
9095     case 0x0a: /* SHL / SLI */
9096         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9097         break;
9098     case 0x1c: /* SCVTF, UCVTF */
9099         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9100                                      opcode, rn, rd);
9101         break;
9102     case 0x10: /* SQSHRUN, SQSHRUN2 */
9103     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9104         if (!is_u) {
9105             unallocated_encoding(s);
9106             return;
9107         }
9108         handle_vec_simd_sqshrn(s, true, false, false, true,
9109                                immh, immb, opcode, rn, rd);
9110         break;
9111     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9112     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9113         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9114                                immh, immb, opcode, rn, rd);
9115         break;
9116     case 0xc: /* SQSHLU */
9117         if (!is_u) {
9118             unallocated_encoding(s);
9119             return;
9120         }
9121         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9122         break;
9123     case 0xe: /* SQSHL, UQSHL */
9124         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9125         break;
9126     case 0x1f: /* FCVTZS, FCVTZU */
9127         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9128         break;
9129     default:
9130         unallocated_encoding(s);
9131         break;
9132     }
9133 }
9134 
9135 /* AdvSIMD scalar three different
9136  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
9137  * +-----+---+-----------+------+---+------+--------+-----+------+------+
9138  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
9139  * +-----+---+-----------+------+---+------+--------+-----+------+------+
9140  */
9141 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9142 {
9143     bool is_u = extract32(insn, 29, 1);
9144     int size = extract32(insn, 22, 2);
9145     int opcode = extract32(insn, 12, 4);
9146     int rm = extract32(insn, 16, 5);
9147     int rn = extract32(insn, 5, 5);
9148     int rd = extract32(insn, 0, 5);
9149 
9150     if (is_u) {
9151         unallocated_encoding(s);
9152         return;
9153     }
9154 
9155     switch (opcode) {
9156     case 0x9: /* SQDMLAL, SQDMLAL2 */
9157     case 0xb: /* SQDMLSL, SQDMLSL2 */
9158     case 0xd: /* SQDMULL, SQDMULL2 */
9159         if (size == 0 || size == 3) {
9160             unallocated_encoding(s);
9161             return;
9162         }
9163         break;
9164     default:
9165         unallocated_encoding(s);
9166         return;
9167     }
9168 
9169     if (!fp_access_check(s)) {
9170         return;
9171     }
9172 
9173     if (size == 2) {
9174         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9175         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9176         TCGv_i64 tcg_res = tcg_temp_new_i64();
9177 
9178         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9179         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9180 
9181         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9182         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
9183 
9184         switch (opcode) {
9185         case 0xd: /* SQDMULL, SQDMULL2 */
9186             break;
9187         case 0xb: /* SQDMLSL, SQDMLSL2 */
9188             tcg_gen_neg_i64(tcg_res, tcg_res);
9189             /* fall through */
9190         case 0x9: /* SQDMLAL, SQDMLAL2 */
9191             read_vec_element(s, tcg_op1, rd, 0, MO_64);
9192             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
9193                                               tcg_res, tcg_op1);
9194             break;
9195         default:
9196             g_assert_not_reached();
9197         }
9198 
9199         write_fp_dreg(s, rd, tcg_res);
9200     } else {
9201         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9202         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9203         TCGv_i64 tcg_res = tcg_temp_new_i64();
9204 
9205         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9206         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
9207 
9208         switch (opcode) {
9209         case 0xd: /* SQDMULL, SQDMULL2 */
9210             break;
9211         case 0xb: /* SQDMLSL, SQDMLSL2 */
9212             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9213             /* fall through */
9214         case 0x9: /* SQDMLAL, SQDMLAL2 */
9215         {
9216             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9217             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9218             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
9219                                               tcg_res, tcg_op3);
9220             break;
9221         }
9222         default:
9223             g_assert_not_reached();
9224         }
9225 
9226         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9227         write_fp_dreg(s, rd, tcg_res);
9228     }
9229 }
9230 
9231 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9232                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9233 {
9234     /* Handle 64x64->64 opcodes which are shared between the scalar
9235      * and vector 3-same groups. We cover every opcode where size == 3
9236      * is valid in either the three-reg-same (integer, not pairwise)
9237      * or scalar-three-reg-same groups.
9238      */
9239     TCGCond cond;
9240 
9241     switch (opcode) {
9242     case 0x1: /* SQADD */
9243         if (u) {
9244             gen_helper_neon_qadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9245         } else {
9246             gen_helper_neon_qadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9247         }
9248         break;
9249     case 0x5: /* SQSUB */
9250         if (u) {
9251             gen_helper_neon_qsub_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9252         } else {
9253             gen_helper_neon_qsub_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9254         }
9255         break;
9256     case 0x6: /* CMGT, CMHI */
9257         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9258     do_cmop:
9259         /* 64 bit integer comparison, result = test ? -1 : 0. */
9260         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9261         break;
9262     case 0x7: /* CMGE, CMHS */
9263         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9264         goto do_cmop;
9265     case 0x11: /* CMTST, CMEQ */
9266         if (u) {
9267             cond = TCG_COND_EQ;
9268             goto do_cmop;
9269         }
9270         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9271         break;
9272     case 0x8: /* SSHL, USHL */
9273         if (u) {
9274             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9275         } else {
9276             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9277         }
9278         break;
9279     case 0x9: /* SQSHL, UQSHL */
9280         if (u) {
9281             gen_helper_neon_qshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9282         } else {
9283             gen_helper_neon_qshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9284         }
9285         break;
9286     case 0xa: /* SRSHL, URSHL */
9287         if (u) {
9288             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9289         } else {
9290             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9291         }
9292         break;
9293     case 0xb: /* SQRSHL, UQRSHL */
9294         if (u) {
9295             gen_helper_neon_qrshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9296         } else {
9297             gen_helper_neon_qrshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
9298         }
9299         break;
9300     case 0x10: /* ADD, SUB */
9301         if (u) {
9302             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9303         } else {
9304             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9305         }
9306         break;
9307     default:
9308         g_assert_not_reached();
9309     }
9310 }
9311 
9312 /* AdvSIMD scalar three same
9313  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9314  * +-----+---+-----------+------+---+------+--------+---+------+------+
9315  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9316  * +-----+---+-----------+------+---+------+--------+---+------+------+
9317  */
9318 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9319 {
9320     int rd = extract32(insn, 0, 5);
9321     int rn = extract32(insn, 5, 5);
9322     int opcode = extract32(insn, 11, 5);
9323     int rm = extract32(insn, 16, 5);
9324     int size = extract32(insn, 22, 2);
9325     bool u = extract32(insn, 29, 1);
9326     TCGv_i64 tcg_rd;
9327 
9328     switch (opcode) {
9329     case 0x1: /* SQADD, UQADD */
9330     case 0x5: /* SQSUB, UQSUB */
9331     case 0x9: /* SQSHL, UQSHL */
9332     case 0xb: /* SQRSHL, UQRSHL */
9333         break;
9334     case 0x8: /* SSHL, USHL */
9335     case 0xa: /* SRSHL, URSHL */
9336     case 0x6: /* CMGT, CMHI */
9337     case 0x7: /* CMGE, CMHS */
9338     case 0x11: /* CMTST, CMEQ */
9339     case 0x10: /* ADD, SUB (vector) */
9340         if (size != 3) {
9341             unallocated_encoding(s);
9342             return;
9343         }
9344         break;
9345     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9346         if (size != 1 && size != 2) {
9347             unallocated_encoding(s);
9348             return;
9349         }
9350         break;
9351     default:
9352         unallocated_encoding(s);
9353         return;
9354     }
9355 
9356     if (!fp_access_check(s)) {
9357         return;
9358     }
9359 
9360     tcg_rd = tcg_temp_new_i64();
9361 
9362     if (size == 3) {
9363         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9364         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9365 
9366         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9367     } else {
9368         /* Do a single operation on the lowest element in the vector.
9369          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9370          * no side effects for all these operations.
9371          * OPTME: special-purpose helpers would avoid doing some
9372          * unnecessary work in the helper for the 8 and 16 bit cases.
9373          */
9374         NeonGenTwoOpEnvFn *genenvfn;
9375         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9376         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9377         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9378 
9379         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9380         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9381 
9382         switch (opcode) {
9383         case 0x1: /* SQADD, UQADD */
9384         {
9385             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9386                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9387                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9388                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9389             };
9390             genenvfn = fns[size][u];
9391             break;
9392         }
9393         case 0x5: /* SQSUB, UQSUB */
9394         {
9395             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9396                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9397                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9398                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9399             };
9400             genenvfn = fns[size][u];
9401             break;
9402         }
9403         case 0x9: /* SQSHL, UQSHL */
9404         {
9405             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9406                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9407                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9408                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9409             };
9410             genenvfn = fns[size][u];
9411             break;
9412         }
9413         case 0xb: /* SQRSHL, UQRSHL */
9414         {
9415             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9416                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9417                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9418                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9419             };
9420             genenvfn = fns[size][u];
9421             break;
9422         }
9423         case 0x16: /* SQDMULH, SQRDMULH */
9424         {
9425             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9426                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9427                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9428             };
9429             assert(size == 1 || size == 2);
9430             genenvfn = fns[size - 1][u];
9431             break;
9432         }
9433         default:
9434             g_assert_not_reached();
9435         }
9436 
9437         genenvfn(tcg_rd32, tcg_env, tcg_rn, tcg_rm);
9438         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9439     }
9440 
9441     write_fp_dreg(s, rd, tcg_rd);
9442 }
9443 
9444 /* AdvSIMD scalar three same extra
9445  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9446  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9447  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9448  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9449  */
9450 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9451                                                    uint32_t insn)
9452 {
9453     int rd = extract32(insn, 0, 5);
9454     int rn = extract32(insn, 5, 5);
9455     int opcode = extract32(insn, 11, 4);
9456     int rm = extract32(insn, 16, 5);
9457     int size = extract32(insn, 22, 2);
9458     bool u = extract32(insn, 29, 1);
9459     TCGv_i32 ele1, ele2, ele3;
9460     TCGv_i64 res;
9461     bool feature;
9462 
9463     switch (u * 16 + opcode) {
9464     case 0x10: /* SQRDMLAH (vector) */
9465     case 0x11: /* SQRDMLSH (vector) */
9466         if (size != 1 && size != 2) {
9467             unallocated_encoding(s);
9468             return;
9469         }
9470         feature = dc_isar_feature(aa64_rdm, s);
9471         break;
9472     default:
9473         unallocated_encoding(s);
9474         return;
9475     }
9476     if (!feature) {
9477         unallocated_encoding(s);
9478         return;
9479     }
9480     if (!fp_access_check(s)) {
9481         return;
9482     }
9483 
9484     /* Do a single operation on the lowest element in the vector.
9485      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9486      * with no side effects for all these operations.
9487      * OPTME: special-purpose helpers would avoid doing some
9488      * unnecessary work in the helper for the 16 bit cases.
9489      */
9490     ele1 = tcg_temp_new_i32();
9491     ele2 = tcg_temp_new_i32();
9492     ele3 = tcg_temp_new_i32();
9493 
9494     read_vec_element_i32(s, ele1, rn, 0, size);
9495     read_vec_element_i32(s, ele2, rm, 0, size);
9496     read_vec_element_i32(s, ele3, rd, 0, size);
9497 
9498     switch (opcode) {
9499     case 0x0: /* SQRDMLAH */
9500         if (size == 1) {
9501             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9502         } else {
9503             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9504         }
9505         break;
9506     case 0x1: /* SQRDMLSH */
9507         if (size == 1) {
9508             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9509         } else {
9510             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9511         }
9512         break;
9513     default:
9514         g_assert_not_reached();
9515     }
9516 
9517     res = tcg_temp_new_i64();
9518     tcg_gen_extu_i32_i64(res, ele3);
9519     write_fp_dreg(s, rd, res);
9520 }
9521 
9522 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9523                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9524                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9525 {
9526     /* Handle 64->64 opcodes which are shared between the scalar and
9527      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9528      * is valid in either group and also the double-precision fp ops.
9529      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9530      * requires them.
9531      */
9532     TCGCond cond;
9533 
9534     switch (opcode) {
9535     case 0x4: /* CLS, CLZ */
9536         if (u) {
9537             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9538         } else {
9539             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9540         }
9541         break;
9542     case 0x5: /* NOT */
9543         /* This opcode is shared with CNT and RBIT but we have earlier
9544          * enforced that size == 3 if and only if this is the NOT insn.
9545          */
9546         tcg_gen_not_i64(tcg_rd, tcg_rn);
9547         break;
9548     case 0x7: /* SQABS, SQNEG */
9549         if (u) {
9550             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9551         } else {
9552             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9553         }
9554         break;
9555     case 0xa: /* CMLT */
9556         cond = TCG_COND_LT;
9557     do_cmop:
9558         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9559         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9560         break;
9561     case 0x8: /* CMGT, CMGE */
9562         cond = u ? TCG_COND_GE : TCG_COND_GT;
9563         goto do_cmop;
9564     case 0x9: /* CMEQ, CMLE */
9565         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9566         goto do_cmop;
9567     case 0xb: /* ABS, NEG */
9568         if (u) {
9569             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9570         } else {
9571             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9572         }
9573         break;
9574     case 0x2f: /* FABS */
9575         gen_vfp_absd(tcg_rd, tcg_rn);
9576         break;
9577     case 0x6f: /* FNEG */
9578         gen_vfp_negd(tcg_rd, tcg_rn);
9579         break;
9580     case 0x7f: /* FSQRT */
9581         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9582         break;
9583     case 0x1a: /* FCVTNS */
9584     case 0x1b: /* FCVTMS */
9585     case 0x1c: /* FCVTAS */
9586     case 0x3a: /* FCVTPS */
9587     case 0x3b: /* FCVTZS */
9588         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9589         break;
9590     case 0x5a: /* FCVTNU */
9591     case 0x5b: /* FCVTMU */
9592     case 0x5c: /* FCVTAU */
9593     case 0x7a: /* FCVTPU */
9594     case 0x7b: /* FCVTZU */
9595         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9596         break;
9597     case 0x18: /* FRINTN */
9598     case 0x19: /* FRINTM */
9599     case 0x38: /* FRINTP */
9600     case 0x39: /* FRINTZ */
9601     case 0x58: /* FRINTA */
9602     case 0x79: /* FRINTI */
9603         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9604         break;
9605     case 0x59: /* FRINTX */
9606         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9607         break;
9608     case 0x1e: /* FRINT32Z */
9609     case 0x5e: /* FRINT32X */
9610         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9611         break;
9612     case 0x1f: /* FRINT64Z */
9613     case 0x5f: /* FRINT64X */
9614         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9615         break;
9616     default:
9617         g_assert_not_reached();
9618     }
9619 }
9620 
9621 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9622                                    bool is_scalar, bool is_u, bool is_q,
9623                                    int size, int rn, int rd)
9624 {
9625     bool is_double = (size == MO_64);
9626     TCGv_ptr fpst;
9627 
9628     if (!fp_access_check(s)) {
9629         return;
9630     }
9631 
9632     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9633 
9634     if (is_double) {
9635         TCGv_i64 tcg_op = tcg_temp_new_i64();
9636         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9637         TCGv_i64 tcg_res = tcg_temp_new_i64();
9638         NeonGenTwoDoubleOpFn *genfn;
9639         bool swap = false;
9640         int pass;
9641 
9642         switch (opcode) {
9643         case 0x2e: /* FCMLT (zero) */
9644             swap = true;
9645             /* fallthrough */
9646         case 0x2c: /* FCMGT (zero) */
9647             genfn = gen_helper_neon_cgt_f64;
9648             break;
9649         case 0x2d: /* FCMEQ (zero) */
9650             genfn = gen_helper_neon_ceq_f64;
9651             break;
9652         case 0x6d: /* FCMLE (zero) */
9653             swap = true;
9654             /* fall through */
9655         case 0x6c: /* FCMGE (zero) */
9656             genfn = gen_helper_neon_cge_f64;
9657             break;
9658         default:
9659             g_assert_not_reached();
9660         }
9661 
9662         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9663             read_vec_element(s, tcg_op, rn, pass, MO_64);
9664             if (swap) {
9665                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9666             } else {
9667                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9668             }
9669             write_vec_element(s, tcg_res, rd, pass, MO_64);
9670         }
9671 
9672         clear_vec_high(s, !is_scalar, rd);
9673     } else {
9674         TCGv_i32 tcg_op = tcg_temp_new_i32();
9675         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9676         TCGv_i32 tcg_res = tcg_temp_new_i32();
9677         NeonGenTwoSingleOpFn *genfn;
9678         bool swap = false;
9679         int pass, maxpasses;
9680 
9681         if (size == MO_16) {
9682             switch (opcode) {
9683             case 0x2e: /* FCMLT (zero) */
9684                 swap = true;
9685                 /* fall through */
9686             case 0x2c: /* FCMGT (zero) */
9687                 genfn = gen_helper_advsimd_cgt_f16;
9688                 break;
9689             case 0x2d: /* FCMEQ (zero) */
9690                 genfn = gen_helper_advsimd_ceq_f16;
9691                 break;
9692             case 0x6d: /* FCMLE (zero) */
9693                 swap = true;
9694                 /* fall through */
9695             case 0x6c: /* FCMGE (zero) */
9696                 genfn = gen_helper_advsimd_cge_f16;
9697                 break;
9698             default:
9699                 g_assert_not_reached();
9700             }
9701         } else {
9702             switch (opcode) {
9703             case 0x2e: /* FCMLT (zero) */
9704                 swap = true;
9705                 /* fall through */
9706             case 0x2c: /* FCMGT (zero) */
9707                 genfn = gen_helper_neon_cgt_f32;
9708                 break;
9709             case 0x2d: /* FCMEQ (zero) */
9710                 genfn = gen_helper_neon_ceq_f32;
9711                 break;
9712             case 0x6d: /* FCMLE (zero) */
9713                 swap = true;
9714                 /* fall through */
9715             case 0x6c: /* FCMGE (zero) */
9716                 genfn = gen_helper_neon_cge_f32;
9717                 break;
9718             default:
9719                 g_assert_not_reached();
9720             }
9721         }
9722 
9723         if (is_scalar) {
9724             maxpasses = 1;
9725         } else {
9726             int vector_size = 8 << is_q;
9727             maxpasses = vector_size >> size;
9728         }
9729 
9730         for (pass = 0; pass < maxpasses; pass++) {
9731             read_vec_element_i32(s, tcg_op, rn, pass, size);
9732             if (swap) {
9733                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9734             } else {
9735                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9736             }
9737             if (is_scalar) {
9738                 write_fp_sreg(s, rd, tcg_res);
9739             } else {
9740                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9741             }
9742         }
9743 
9744         if (!is_scalar) {
9745             clear_vec_high(s, is_q, rd);
9746         }
9747     }
9748 }
9749 
9750 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9751                                     bool is_scalar, bool is_u, bool is_q,
9752                                     int size, int rn, int rd)
9753 {
9754     bool is_double = (size == 3);
9755     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9756 
9757     if (is_double) {
9758         TCGv_i64 tcg_op = tcg_temp_new_i64();
9759         TCGv_i64 tcg_res = tcg_temp_new_i64();
9760         int pass;
9761 
9762         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9763             read_vec_element(s, tcg_op, rn, pass, MO_64);
9764             switch (opcode) {
9765             case 0x3d: /* FRECPE */
9766                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9767                 break;
9768             case 0x3f: /* FRECPX */
9769                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9770                 break;
9771             case 0x7d: /* FRSQRTE */
9772                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9773                 break;
9774             default:
9775                 g_assert_not_reached();
9776             }
9777             write_vec_element(s, tcg_res, rd, pass, MO_64);
9778         }
9779         clear_vec_high(s, !is_scalar, rd);
9780     } else {
9781         TCGv_i32 tcg_op = tcg_temp_new_i32();
9782         TCGv_i32 tcg_res = tcg_temp_new_i32();
9783         int pass, maxpasses;
9784 
9785         if (is_scalar) {
9786             maxpasses = 1;
9787         } else {
9788             maxpasses = is_q ? 4 : 2;
9789         }
9790 
9791         for (pass = 0; pass < maxpasses; pass++) {
9792             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9793 
9794             switch (opcode) {
9795             case 0x3c: /* URECPE */
9796                 gen_helper_recpe_u32(tcg_res, tcg_op);
9797                 break;
9798             case 0x3d: /* FRECPE */
9799                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9800                 break;
9801             case 0x3f: /* FRECPX */
9802                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9803                 break;
9804             case 0x7d: /* FRSQRTE */
9805                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9806                 break;
9807             default:
9808                 g_assert_not_reached();
9809             }
9810 
9811             if (is_scalar) {
9812                 write_fp_sreg(s, rd, tcg_res);
9813             } else {
9814                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9815             }
9816         }
9817         if (!is_scalar) {
9818             clear_vec_high(s, is_q, rd);
9819         }
9820     }
9821 }
9822 
9823 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9824                                 int opcode, bool u, bool is_q,
9825                                 int size, int rn, int rd)
9826 {
9827     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9828      * in the source becomes a size element in the destination).
9829      */
9830     int pass;
9831     TCGv_i32 tcg_res[2];
9832     int destelt = is_q ? 2 : 0;
9833     int passes = scalar ? 1 : 2;
9834 
9835     if (scalar) {
9836         tcg_res[1] = tcg_constant_i32(0);
9837     }
9838 
9839     for (pass = 0; pass < passes; pass++) {
9840         TCGv_i64 tcg_op = tcg_temp_new_i64();
9841         NeonGenNarrowFn *genfn = NULL;
9842         NeonGenNarrowEnvFn *genenvfn = NULL;
9843 
9844         if (scalar) {
9845             read_vec_element(s, tcg_op, rn, pass, size + 1);
9846         } else {
9847             read_vec_element(s, tcg_op, rn, pass, MO_64);
9848         }
9849         tcg_res[pass] = tcg_temp_new_i32();
9850 
9851         switch (opcode) {
9852         case 0x12: /* XTN, SQXTUN */
9853         {
9854             static NeonGenNarrowFn * const xtnfns[3] = {
9855                 gen_helper_neon_narrow_u8,
9856                 gen_helper_neon_narrow_u16,
9857                 tcg_gen_extrl_i64_i32,
9858             };
9859             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9860                 gen_helper_neon_unarrow_sat8,
9861                 gen_helper_neon_unarrow_sat16,
9862                 gen_helper_neon_unarrow_sat32,
9863             };
9864             if (u) {
9865                 genenvfn = sqxtunfns[size];
9866             } else {
9867                 genfn = xtnfns[size];
9868             }
9869             break;
9870         }
9871         case 0x14: /* SQXTN, UQXTN */
9872         {
9873             static NeonGenNarrowEnvFn * const fns[3][2] = {
9874                 { gen_helper_neon_narrow_sat_s8,
9875                   gen_helper_neon_narrow_sat_u8 },
9876                 { gen_helper_neon_narrow_sat_s16,
9877                   gen_helper_neon_narrow_sat_u16 },
9878                 { gen_helper_neon_narrow_sat_s32,
9879                   gen_helper_neon_narrow_sat_u32 },
9880             };
9881             genenvfn = fns[size][u];
9882             break;
9883         }
9884         case 0x16: /* FCVTN, FCVTN2 */
9885             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9886             if (size == 2) {
9887                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9888             } else {
9889                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9890                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9891                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9892                 TCGv_i32 ahp = get_ahp_flag();
9893 
9894                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9895                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9896                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9897                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9898             }
9899             break;
9900         case 0x36: /* BFCVTN, BFCVTN2 */
9901             {
9902                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9903                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9904             }
9905             break;
9906         case 0x56:  /* FCVTXN, FCVTXN2 */
9907             /* 64 bit to 32 bit float conversion
9908              * with von Neumann rounding (round to odd)
9909              */
9910             assert(size == 2);
9911             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
9912             break;
9913         default:
9914             g_assert_not_reached();
9915         }
9916 
9917         if (genfn) {
9918             genfn(tcg_res[pass], tcg_op);
9919         } else if (genenvfn) {
9920             genenvfn(tcg_res[pass], tcg_env, tcg_op);
9921         }
9922     }
9923 
9924     for (pass = 0; pass < 2; pass++) {
9925         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9926     }
9927     clear_vec_high(s, is_q, rd);
9928 }
9929 
9930 /* Remaining saturating accumulating ops */
9931 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9932                                 bool is_q, int size, int rn, int rd)
9933 {
9934     bool is_double = (size == 3);
9935 
9936     if (is_double) {
9937         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9938         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9939         int pass;
9940 
9941         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9942             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9943             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9944 
9945             if (is_u) { /* USQADD */
9946                 gen_helper_neon_uqadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9947             } else { /* SUQADD */
9948                 gen_helper_neon_sqadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9949             }
9950             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9951         }
9952         clear_vec_high(s, !is_scalar, rd);
9953     } else {
9954         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9955         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9956         int pass, maxpasses;
9957 
9958         if (is_scalar) {
9959             maxpasses = 1;
9960         } else {
9961             maxpasses = is_q ? 4 : 2;
9962         }
9963 
9964         for (pass = 0; pass < maxpasses; pass++) {
9965             if (is_scalar) {
9966                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9967                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9968             } else {
9969                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9970                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9971             }
9972 
9973             if (is_u) { /* USQADD */
9974                 switch (size) {
9975                 case 0:
9976                     gen_helper_neon_uqadd_s8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9977                     break;
9978                 case 1:
9979                     gen_helper_neon_uqadd_s16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9980                     break;
9981                 case 2:
9982                     gen_helper_neon_uqadd_s32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9983                     break;
9984                 default:
9985                     g_assert_not_reached();
9986                 }
9987             } else { /* SUQADD */
9988                 switch (size) {
9989                 case 0:
9990                     gen_helper_neon_sqadd_u8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9991                     break;
9992                 case 1:
9993                     gen_helper_neon_sqadd_u16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9994                     break;
9995                 case 2:
9996                     gen_helper_neon_sqadd_u32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9997                     break;
9998                 default:
9999                     g_assert_not_reached();
10000                 }
10001             }
10002 
10003             if (is_scalar) {
10004                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10005             }
10006             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10007         }
10008         clear_vec_high(s, is_q, rd);
10009     }
10010 }
10011 
10012 /* AdvSIMD scalar two reg misc
10013  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10014  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10015  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10016  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10017  */
10018 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10019 {
10020     int rd = extract32(insn, 0, 5);
10021     int rn = extract32(insn, 5, 5);
10022     int opcode = extract32(insn, 12, 5);
10023     int size = extract32(insn, 22, 2);
10024     bool u = extract32(insn, 29, 1);
10025     bool is_fcvt = false;
10026     int rmode;
10027     TCGv_i32 tcg_rmode;
10028     TCGv_ptr tcg_fpstatus;
10029 
10030     switch (opcode) {
10031     case 0x3: /* USQADD / SUQADD*/
10032         if (!fp_access_check(s)) {
10033             return;
10034         }
10035         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10036         return;
10037     case 0x7: /* SQABS / SQNEG */
10038         break;
10039     case 0xa: /* CMLT */
10040         if (u) {
10041             unallocated_encoding(s);
10042             return;
10043         }
10044         /* fall through */
10045     case 0x8: /* CMGT, CMGE */
10046     case 0x9: /* CMEQ, CMLE */
10047     case 0xb: /* ABS, NEG */
10048         if (size != 3) {
10049             unallocated_encoding(s);
10050             return;
10051         }
10052         break;
10053     case 0x12: /* SQXTUN */
10054         if (!u) {
10055             unallocated_encoding(s);
10056             return;
10057         }
10058         /* fall through */
10059     case 0x14: /* SQXTN, UQXTN */
10060         if (size == 3) {
10061             unallocated_encoding(s);
10062             return;
10063         }
10064         if (!fp_access_check(s)) {
10065             return;
10066         }
10067         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10068         return;
10069     case 0xc ... 0xf:
10070     case 0x16 ... 0x1d:
10071     case 0x1f:
10072         /* Floating point: U, size[1] and opcode indicate operation;
10073          * size[0] indicates single or double precision.
10074          */
10075         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10076         size = extract32(size, 0, 1) ? 3 : 2;
10077         switch (opcode) {
10078         case 0x2c: /* FCMGT (zero) */
10079         case 0x2d: /* FCMEQ (zero) */
10080         case 0x2e: /* FCMLT (zero) */
10081         case 0x6c: /* FCMGE (zero) */
10082         case 0x6d: /* FCMLE (zero) */
10083             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10084             return;
10085         case 0x1d: /* SCVTF */
10086         case 0x5d: /* UCVTF */
10087         {
10088             bool is_signed = (opcode == 0x1d);
10089             if (!fp_access_check(s)) {
10090                 return;
10091             }
10092             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10093             return;
10094         }
10095         case 0x3d: /* FRECPE */
10096         case 0x3f: /* FRECPX */
10097         case 0x7d: /* FRSQRTE */
10098             if (!fp_access_check(s)) {
10099                 return;
10100             }
10101             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10102             return;
10103         case 0x1a: /* FCVTNS */
10104         case 0x1b: /* FCVTMS */
10105         case 0x3a: /* FCVTPS */
10106         case 0x3b: /* FCVTZS */
10107         case 0x5a: /* FCVTNU */
10108         case 0x5b: /* FCVTMU */
10109         case 0x7a: /* FCVTPU */
10110         case 0x7b: /* FCVTZU */
10111             is_fcvt = true;
10112             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10113             break;
10114         case 0x1c: /* FCVTAS */
10115         case 0x5c: /* FCVTAU */
10116             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10117             is_fcvt = true;
10118             rmode = FPROUNDING_TIEAWAY;
10119             break;
10120         case 0x56: /* FCVTXN, FCVTXN2 */
10121             if (size == 2) {
10122                 unallocated_encoding(s);
10123                 return;
10124             }
10125             if (!fp_access_check(s)) {
10126                 return;
10127             }
10128             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10129             return;
10130         default:
10131             unallocated_encoding(s);
10132             return;
10133         }
10134         break;
10135     default:
10136         unallocated_encoding(s);
10137         return;
10138     }
10139 
10140     if (!fp_access_check(s)) {
10141         return;
10142     }
10143 
10144     if (is_fcvt) {
10145         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10146         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10147     } else {
10148         tcg_fpstatus = NULL;
10149         tcg_rmode = NULL;
10150     }
10151 
10152     if (size == 3) {
10153         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10154         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10155 
10156         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10157         write_fp_dreg(s, rd, tcg_rd);
10158     } else {
10159         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10160         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10161 
10162         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10163 
10164         switch (opcode) {
10165         case 0x7: /* SQABS, SQNEG */
10166         {
10167             NeonGenOneOpEnvFn *genfn;
10168             static NeonGenOneOpEnvFn * const fns[3][2] = {
10169                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10170                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10171                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10172             };
10173             genfn = fns[size][u];
10174             genfn(tcg_rd, tcg_env, tcg_rn);
10175             break;
10176         }
10177         case 0x1a: /* FCVTNS */
10178         case 0x1b: /* FCVTMS */
10179         case 0x1c: /* FCVTAS */
10180         case 0x3a: /* FCVTPS */
10181         case 0x3b: /* FCVTZS */
10182             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10183                                  tcg_fpstatus);
10184             break;
10185         case 0x5a: /* FCVTNU */
10186         case 0x5b: /* FCVTMU */
10187         case 0x5c: /* FCVTAU */
10188         case 0x7a: /* FCVTPU */
10189         case 0x7b: /* FCVTZU */
10190             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10191                                  tcg_fpstatus);
10192             break;
10193         default:
10194             g_assert_not_reached();
10195         }
10196 
10197         write_fp_sreg(s, rd, tcg_rd);
10198     }
10199 
10200     if (is_fcvt) {
10201         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10202     }
10203 }
10204 
10205 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10206 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10207                                  int immh, int immb, int opcode, int rn, int rd)
10208 {
10209     int size = 32 - clz32(immh) - 1;
10210     int immhb = immh << 3 | immb;
10211     int shift = 2 * (8 << size) - immhb;
10212     GVecGen2iFn *gvec_fn;
10213 
10214     if (extract32(immh, 3, 1) && !is_q) {
10215         unallocated_encoding(s);
10216         return;
10217     }
10218     tcg_debug_assert(size <= 3);
10219 
10220     if (!fp_access_check(s)) {
10221         return;
10222     }
10223 
10224     switch (opcode) {
10225     case 0x02: /* SSRA / USRA (accumulate) */
10226         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10227         break;
10228 
10229     case 0x08: /* SRI */
10230         gvec_fn = gen_gvec_sri;
10231         break;
10232 
10233     case 0x00: /* SSHR / USHR */
10234         if (is_u) {
10235             if (shift == 8 << size) {
10236                 /* Shift count the same size as element size produces zero.  */
10237                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10238                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10239                 return;
10240             }
10241             gvec_fn = tcg_gen_gvec_shri;
10242         } else {
10243             /* Shift count the same size as element size produces all sign.  */
10244             if (shift == 8 << size) {
10245                 shift -= 1;
10246             }
10247             gvec_fn = tcg_gen_gvec_sari;
10248         }
10249         break;
10250 
10251     case 0x04: /* SRSHR / URSHR (rounding) */
10252         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10253         break;
10254 
10255     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10256         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10257         break;
10258 
10259     default:
10260         g_assert_not_reached();
10261     }
10262 
10263     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10264 }
10265 
10266 /* SHL/SLI - Vector shift left */
10267 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10268                                  int immh, int immb, int opcode, int rn, int rd)
10269 {
10270     int size = 32 - clz32(immh) - 1;
10271     int immhb = immh << 3 | immb;
10272     int shift = immhb - (8 << size);
10273 
10274     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10275     assert(size >= 0 && size <= 3);
10276 
10277     if (extract32(immh, 3, 1) && !is_q) {
10278         unallocated_encoding(s);
10279         return;
10280     }
10281 
10282     if (!fp_access_check(s)) {
10283         return;
10284     }
10285 
10286     if (insert) {
10287         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10288     } else {
10289         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10290     }
10291 }
10292 
10293 /* USHLL/SHLL - Vector shift left with widening */
10294 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10295                                  int immh, int immb, int opcode, int rn, int rd)
10296 {
10297     int size = 32 - clz32(immh) - 1;
10298     int immhb = immh << 3 | immb;
10299     int shift = immhb - (8 << size);
10300     int dsize = 64;
10301     int esize = 8 << size;
10302     int elements = dsize/esize;
10303     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10304     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10305     int i;
10306 
10307     if (size >= 3) {
10308         unallocated_encoding(s);
10309         return;
10310     }
10311 
10312     if (!fp_access_check(s)) {
10313         return;
10314     }
10315 
10316     /* For the LL variants the store is larger than the load,
10317      * so if rd == rn we would overwrite parts of our input.
10318      * So load everything right now and use shifts in the main loop.
10319      */
10320     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10321 
10322     for (i = 0; i < elements; i++) {
10323         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10324         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10325         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10326         write_vec_element(s, tcg_rd, rd, i, size + 1);
10327     }
10328 }
10329 
10330 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10331 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10332                                  int immh, int immb, int opcode, int rn, int rd)
10333 {
10334     int immhb = immh << 3 | immb;
10335     int size = 32 - clz32(immh) - 1;
10336     int dsize = 64;
10337     int esize = 8 << size;
10338     int elements = dsize/esize;
10339     int shift = (2 * esize) - immhb;
10340     bool round = extract32(opcode, 0, 1);
10341     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10342     TCGv_i64 tcg_round;
10343     int i;
10344 
10345     if (extract32(immh, 3, 1)) {
10346         unallocated_encoding(s);
10347         return;
10348     }
10349 
10350     if (!fp_access_check(s)) {
10351         return;
10352     }
10353 
10354     tcg_rn = tcg_temp_new_i64();
10355     tcg_rd = tcg_temp_new_i64();
10356     tcg_final = tcg_temp_new_i64();
10357     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10358 
10359     if (round) {
10360         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10361     } else {
10362         tcg_round = NULL;
10363     }
10364 
10365     for (i = 0; i < elements; i++) {
10366         read_vec_element(s, tcg_rn, rn, i, size+1);
10367         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10368                                 false, true, size+1, shift);
10369 
10370         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10371     }
10372 
10373     if (!is_q) {
10374         write_vec_element(s, tcg_final, rd, 0, MO_64);
10375     } else {
10376         write_vec_element(s, tcg_final, rd, 1, MO_64);
10377     }
10378 
10379     clear_vec_high(s, is_q, rd);
10380 }
10381 
10382 
10383 /* AdvSIMD shift by immediate
10384  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10385  * +---+---+---+-------------+------+------+--------+---+------+------+
10386  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10387  * +---+---+---+-------------+------+------+--------+---+------+------+
10388  */
10389 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10390 {
10391     int rd = extract32(insn, 0, 5);
10392     int rn = extract32(insn, 5, 5);
10393     int opcode = extract32(insn, 11, 5);
10394     int immb = extract32(insn, 16, 3);
10395     int immh = extract32(insn, 19, 4);
10396     bool is_u = extract32(insn, 29, 1);
10397     bool is_q = extract32(insn, 30, 1);
10398 
10399     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10400     assert(immh != 0);
10401 
10402     switch (opcode) {
10403     case 0x08: /* SRI */
10404         if (!is_u) {
10405             unallocated_encoding(s);
10406             return;
10407         }
10408         /* fall through */
10409     case 0x00: /* SSHR / USHR */
10410     case 0x02: /* SSRA / USRA (accumulate) */
10411     case 0x04: /* SRSHR / URSHR (rounding) */
10412     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10413         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10414         break;
10415     case 0x0a: /* SHL / SLI */
10416         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10417         break;
10418     case 0x10: /* SHRN */
10419     case 0x11: /* RSHRN / SQRSHRUN */
10420         if (is_u) {
10421             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10422                                    opcode, rn, rd);
10423         } else {
10424             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10425         }
10426         break;
10427     case 0x12: /* SQSHRN / UQSHRN */
10428     case 0x13: /* SQRSHRN / UQRSHRN */
10429         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10430                                opcode, rn, rd);
10431         break;
10432     case 0x14: /* SSHLL / USHLL */
10433         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10434         break;
10435     case 0x1c: /* SCVTF / UCVTF */
10436         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10437                                      opcode, rn, rd);
10438         break;
10439     case 0xc: /* SQSHLU */
10440         if (!is_u) {
10441             unallocated_encoding(s);
10442             return;
10443         }
10444         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10445         break;
10446     case 0xe: /* SQSHL, UQSHL */
10447         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10448         break;
10449     case 0x1f: /* FCVTZS/ FCVTZU */
10450         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10451         return;
10452     default:
10453         unallocated_encoding(s);
10454         return;
10455     }
10456 }
10457 
10458 /* Generate code to do a "long" addition or subtraction, ie one done in
10459  * TCGv_i64 on vector lanes twice the width specified by size.
10460  */
10461 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10462                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10463 {
10464     static NeonGenTwo64OpFn * const fns[3][2] = {
10465         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10466         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10467         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10468     };
10469     NeonGenTwo64OpFn *genfn;
10470     assert(size < 3);
10471 
10472     genfn = fns[size][is_sub];
10473     genfn(tcg_res, tcg_op1, tcg_op2);
10474 }
10475 
10476 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10477                                 int opcode, int rd, int rn, int rm)
10478 {
10479     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10480     TCGv_i64 tcg_res[2];
10481     int pass, accop;
10482 
10483     tcg_res[0] = tcg_temp_new_i64();
10484     tcg_res[1] = tcg_temp_new_i64();
10485 
10486     /* Does this op do an adding accumulate, a subtracting accumulate,
10487      * or no accumulate at all?
10488      */
10489     switch (opcode) {
10490     case 5:
10491     case 8:
10492     case 9:
10493         accop = 1;
10494         break;
10495     case 10:
10496     case 11:
10497         accop = -1;
10498         break;
10499     default:
10500         accop = 0;
10501         break;
10502     }
10503 
10504     if (accop != 0) {
10505         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10506         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10507     }
10508 
10509     /* size == 2 means two 32x32->64 operations; this is worth special
10510      * casing because we can generally handle it inline.
10511      */
10512     if (size == 2) {
10513         for (pass = 0; pass < 2; pass++) {
10514             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10515             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10516             TCGv_i64 tcg_passres;
10517             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10518 
10519             int elt = pass + is_q * 2;
10520 
10521             read_vec_element(s, tcg_op1, rn, elt, memop);
10522             read_vec_element(s, tcg_op2, rm, elt, memop);
10523 
10524             if (accop == 0) {
10525                 tcg_passres = tcg_res[pass];
10526             } else {
10527                 tcg_passres = tcg_temp_new_i64();
10528             }
10529 
10530             switch (opcode) {
10531             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10532                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10533                 break;
10534             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10535                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10536                 break;
10537             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10538             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10539             {
10540                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10541                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10542 
10543                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10544                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10545                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10546                                     tcg_passres,
10547                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10548                 break;
10549             }
10550             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10551             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10552             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10553                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10554                 break;
10555             case 9: /* SQDMLAL, SQDMLAL2 */
10556             case 11: /* SQDMLSL, SQDMLSL2 */
10557             case 13: /* SQDMULL, SQDMULL2 */
10558                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10559                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10560                                                   tcg_passres, tcg_passres);
10561                 break;
10562             default:
10563                 g_assert_not_reached();
10564             }
10565 
10566             if (opcode == 9 || opcode == 11) {
10567                 /* saturating accumulate ops */
10568                 if (accop < 0) {
10569                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10570                 }
10571                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10572                                                   tcg_res[pass], tcg_passres);
10573             } else if (accop > 0) {
10574                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10575             } else if (accop < 0) {
10576                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10577             }
10578         }
10579     } else {
10580         /* size 0 or 1, generally helper functions */
10581         for (pass = 0; pass < 2; pass++) {
10582             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10583             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10584             TCGv_i64 tcg_passres;
10585             int elt = pass + is_q * 2;
10586 
10587             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10588             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10589 
10590             if (accop == 0) {
10591                 tcg_passres = tcg_res[pass];
10592             } else {
10593                 tcg_passres = tcg_temp_new_i64();
10594             }
10595 
10596             switch (opcode) {
10597             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10598             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10599             {
10600                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10601                 static NeonGenWidenFn * const widenfns[2][2] = {
10602                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10603                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10604                 };
10605                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10606 
10607                 widenfn(tcg_op2_64, tcg_op2);
10608                 widenfn(tcg_passres, tcg_op1);
10609                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10610                               tcg_passres, tcg_op2_64);
10611                 break;
10612             }
10613             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10614             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10615                 if (size == 0) {
10616                     if (is_u) {
10617                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10618                     } else {
10619                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10620                     }
10621                 } else {
10622                     if (is_u) {
10623                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10624                     } else {
10625                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10626                     }
10627                 }
10628                 break;
10629             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10630             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10631             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10632                 if (size == 0) {
10633                     if (is_u) {
10634                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10635                     } else {
10636                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10637                     }
10638                 } else {
10639                     if (is_u) {
10640                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10641                     } else {
10642                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10643                     }
10644                 }
10645                 break;
10646             case 9: /* SQDMLAL, SQDMLAL2 */
10647             case 11: /* SQDMLSL, SQDMLSL2 */
10648             case 13: /* SQDMULL, SQDMULL2 */
10649                 assert(size == 1);
10650                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10651                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10652                                                   tcg_passres, tcg_passres);
10653                 break;
10654             default:
10655                 g_assert_not_reached();
10656             }
10657 
10658             if (accop != 0) {
10659                 if (opcode == 9 || opcode == 11) {
10660                     /* saturating accumulate ops */
10661                     if (accop < 0) {
10662                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10663                     }
10664                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10665                                                       tcg_res[pass],
10666                                                       tcg_passres);
10667                 } else {
10668                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10669                                   tcg_res[pass], tcg_passres);
10670                 }
10671             }
10672         }
10673     }
10674 
10675     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10676     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10677 }
10678 
10679 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10680                             int opcode, int rd, int rn, int rm)
10681 {
10682     TCGv_i64 tcg_res[2];
10683     int part = is_q ? 2 : 0;
10684     int pass;
10685 
10686     for (pass = 0; pass < 2; pass++) {
10687         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10688         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10689         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10690         static NeonGenWidenFn * const widenfns[3][2] = {
10691             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10692             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10693             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10694         };
10695         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10696 
10697         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10698         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10699         widenfn(tcg_op2_wide, tcg_op2);
10700         tcg_res[pass] = tcg_temp_new_i64();
10701         gen_neon_addl(size, (opcode == 3),
10702                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10703     }
10704 
10705     for (pass = 0; pass < 2; pass++) {
10706         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10707     }
10708 }
10709 
10710 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10711 {
10712     tcg_gen_addi_i64(in, in, 1U << 31);
10713     tcg_gen_extrh_i64_i32(res, in);
10714 }
10715 
10716 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10717                                  int opcode, int rd, int rn, int rm)
10718 {
10719     TCGv_i32 tcg_res[2];
10720     int part = is_q ? 2 : 0;
10721     int pass;
10722 
10723     for (pass = 0; pass < 2; pass++) {
10724         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10725         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10726         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10727         static NeonGenNarrowFn * const narrowfns[3][2] = {
10728             { gen_helper_neon_narrow_high_u8,
10729               gen_helper_neon_narrow_round_high_u8 },
10730             { gen_helper_neon_narrow_high_u16,
10731               gen_helper_neon_narrow_round_high_u16 },
10732             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10733         };
10734         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10735 
10736         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10737         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10738 
10739         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10740 
10741         tcg_res[pass] = tcg_temp_new_i32();
10742         gennarrow(tcg_res[pass], tcg_wideres);
10743     }
10744 
10745     for (pass = 0; pass < 2; pass++) {
10746         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10747     }
10748     clear_vec_high(s, is_q, rd);
10749 }
10750 
10751 /* AdvSIMD three different
10752  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10753  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10754  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10755  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10756  */
10757 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10758 {
10759     /* Instructions in this group fall into three basic classes
10760      * (in each case with the operation working on each element in
10761      * the input vectors):
10762      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10763      *     128 bit input)
10764      * (2) wide 64 x 128 -> 128
10765      * (3) narrowing 128 x 128 -> 64
10766      * Here we do initial decode, catch unallocated cases and
10767      * dispatch to separate functions for each class.
10768      */
10769     int is_q = extract32(insn, 30, 1);
10770     int is_u = extract32(insn, 29, 1);
10771     int size = extract32(insn, 22, 2);
10772     int opcode = extract32(insn, 12, 4);
10773     int rm = extract32(insn, 16, 5);
10774     int rn = extract32(insn, 5, 5);
10775     int rd = extract32(insn, 0, 5);
10776 
10777     switch (opcode) {
10778     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10779     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10780         /* 64 x 128 -> 128 */
10781         if (size == 3) {
10782             unallocated_encoding(s);
10783             return;
10784         }
10785         if (!fp_access_check(s)) {
10786             return;
10787         }
10788         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10789         break;
10790     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10791     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10792         /* 128 x 128 -> 64 */
10793         if (size == 3) {
10794             unallocated_encoding(s);
10795             return;
10796         }
10797         if (!fp_access_check(s)) {
10798             return;
10799         }
10800         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10801         break;
10802     case 14: /* PMULL, PMULL2 */
10803         if (is_u) {
10804             unallocated_encoding(s);
10805             return;
10806         }
10807         switch (size) {
10808         case 0: /* PMULL.P8 */
10809             if (!fp_access_check(s)) {
10810                 return;
10811             }
10812             /* The Q field specifies lo/hi half input for this insn.  */
10813             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10814                              gen_helper_neon_pmull_h);
10815             break;
10816 
10817         case 3: /* PMULL.P64 */
10818             if (!dc_isar_feature(aa64_pmull, s)) {
10819                 unallocated_encoding(s);
10820                 return;
10821             }
10822             if (!fp_access_check(s)) {
10823                 return;
10824             }
10825             /* The Q field specifies lo/hi half input for this insn.  */
10826             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10827                              gen_helper_gvec_pmull_q);
10828             break;
10829 
10830         default:
10831             unallocated_encoding(s);
10832             break;
10833         }
10834         return;
10835     case 9: /* SQDMLAL, SQDMLAL2 */
10836     case 11: /* SQDMLSL, SQDMLSL2 */
10837     case 13: /* SQDMULL, SQDMULL2 */
10838         if (is_u || size == 0) {
10839             unallocated_encoding(s);
10840             return;
10841         }
10842         /* fall through */
10843     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10844     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10845     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10846     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10847     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10848     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10849     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10850         /* 64 x 64 -> 128 */
10851         if (size == 3) {
10852             unallocated_encoding(s);
10853             return;
10854         }
10855         if (!fp_access_check(s)) {
10856             return;
10857         }
10858 
10859         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10860         break;
10861     default:
10862         /* opcode 15 not allocated */
10863         unallocated_encoding(s);
10864         break;
10865     }
10866 }
10867 
10868 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10869 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10870 {
10871     int rd = extract32(insn, 0, 5);
10872     int rn = extract32(insn, 5, 5);
10873     int rm = extract32(insn, 16, 5);
10874     int size = extract32(insn, 22, 2);
10875     bool is_u = extract32(insn, 29, 1);
10876     bool is_q = extract32(insn, 30, 1);
10877 
10878     if (!fp_access_check(s)) {
10879         return;
10880     }
10881 
10882     switch (size + 4 * is_u) {
10883     case 0: /* AND */
10884         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10885         return;
10886     case 1: /* BIC */
10887         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10888         return;
10889     case 2: /* ORR */
10890         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10891         return;
10892     case 3: /* ORN */
10893         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10894         return;
10895     case 4: /* EOR */
10896         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10897         return;
10898 
10899     case 5: /* BSL bitwise select */
10900         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10901         return;
10902     case 6: /* BIT, bitwise insert if true */
10903         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10904         return;
10905     case 7: /* BIF, bitwise insert if false */
10906         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10907         return;
10908 
10909     default:
10910         g_assert_not_reached();
10911     }
10912 }
10913 
10914 /* Floating point op subgroup of C3.6.16. */
10915 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10916 {
10917     /* For floating point ops, the U, size[1] and opcode bits
10918      * together indicate the operation. size[0] indicates single
10919      * or double.
10920      */
10921     int fpopcode = extract32(insn, 11, 5)
10922         | (extract32(insn, 23, 1) << 5)
10923         | (extract32(insn, 29, 1) << 6);
10924     int is_q = extract32(insn, 30, 1);
10925     int size = extract32(insn, 22, 1);
10926     int rm = extract32(insn, 16, 5);
10927     int rn = extract32(insn, 5, 5);
10928     int rd = extract32(insn, 0, 5);
10929 
10930     if (size == 1 && !is_q) {
10931         unallocated_encoding(s);
10932         return;
10933     }
10934 
10935     switch (fpopcode) {
10936     case 0x1d: /* FMLAL  */
10937     case 0x3d: /* FMLSL  */
10938     case 0x59: /* FMLAL2 */
10939     case 0x79: /* FMLSL2 */
10940         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10941             unallocated_encoding(s);
10942             return;
10943         }
10944         if (fp_access_check(s)) {
10945             int is_s = extract32(insn, 23, 1);
10946             int is_2 = extract32(insn, 29, 1);
10947             int data = (is_2 << 1) | is_s;
10948             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10949                                vec_full_reg_offset(s, rn),
10950                                vec_full_reg_offset(s, rm), tcg_env,
10951                                is_q ? 16 : 8, vec_full_reg_size(s),
10952                                data, gen_helper_gvec_fmlal_a64);
10953         }
10954         return;
10955 
10956     default:
10957     case 0x18: /* FMAXNM */
10958     case 0x19: /* FMLA */
10959     case 0x1a: /* FADD */
10960     case 0x1b: /* FMULX */
10961     case 0x1c: /* FCMEQ */
10962     case 0x1e: /* FMAX */
10963     case 0x1f: /* FRECPS */
10964     case 0x38: /* FMINNM */
10965     case 0x39: /* FMLS */
10966     case 0x3a: /* FSUB */
10967     case 0x3e: /* FMIN */
10968     case 0x3f: /* FRSQRTS */
10969     case 0x58: /* FMAXNMP */
10970     case 0x5a: /* FADDP */
10971     case 0x5b: /* FMUL */
10972     case 0x5c: /* FCMGE */
10973     case 0x5d: /* FACGE */
10974     case 0x5e: /* FMAXP */
10975     case 0x5f: /* FDIV */
10976     case 0x78: /* FMINNMP */
10977     case 0x7a: /* FABD */
10978     case 0x7d: /* FACGT */
10979     case 0x7c: /* FCMGT */
10980     case 0x7e: /* FMINP */
10981         unallocated_encoding(s);
10982         return;
10983     }
10984 }
10985 
10986 /* Integer op subgroup of C3.6.16. */
10987 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10988 {
10989     int is_q = extract32(insn, 30, 1);
10990     int u = extract32(insn, 29, 1);
10991     int size = extract32(insn, 22, 2);
10992     int opcode = extract32(insn, 11, 5);
10993     int rm = extract32(insn, 16, 5);
10994     int rn = extract32(insn, 5, 5);
10995     int rd = extract32(insn, 0, 5);
10996     int pass;
10997     TCGCond cond;
10998 
10999     switch (opcode) {
11000     case 0x13: /* MUL, PMUL */
11001         if (u && size != 0) {
11002             unallocated_encoding(s);
11003             return;
11004         }
11005         /* fall through */
11006     case 0x0: /* SHADD, UHADD */
11007     case 0x2: /* SRHADD, URHADD */
11008     case 0x4: /* SHSUB, UHSUB */
11009     case 0xc: /* SMAX, UMAX */
11010     case 0xd: /* SMIN, UMIN */
11011     case 0xe: /* SABD, UABD */
11012     case 0xf: /* SABA, UABA */
11013     case 0x12: /* MLA, MLS */
11014         if (size == 3) {
11015             unallocated_encoding(s);
11016             return;
11017         }
11018         break;
11019     case 0x16: /* SQDMULH, SQRDMULH */
11020         if (size == 0 || size == 3) {
11021             unallocated_encoding(s);
11022             return;
11023         }
11024         break;
11025     default:
11026         if (size == 3 && !is_q) {
11027             unallocated_encoding(s);
11028             return;
11029         }
11030         break;
11031     }
11032 
11033     if (!fp_access_check(s)) {
11034         return;
11035     }
11036 
11037     switch (opcode) {
11038     case 0x01: /* SQADD, UQADD */
11039         if (u) {
11040             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11041         } else {
11042             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11043         }
11044         return;
11045     case 0x05: /* SQSUB, UQSUB */
11046         if (u) {
11047             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11048         } else {
11049             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11050         }
11051         return;
11052     case 0x08: /* SSHL, USHL */
11053         if (u) {
11054             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11055         } else {
11056             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11057         }
11058         return;
11059     case 0x0c: /* SMAX, UMAX */
11060         if (u) {
11061             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11062         } else {
11063             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11064         }
11065         return;
11066     case 0x0d: /* SMIN, UMIN */
11067         if (u) {
11068             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11069         } else {
11070             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11071         }
11072         return;
11073     case 0xe: /* SABD, UABD */
11074         if (u) {
11075             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11076         } else {
11077             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11078         }
11079         return;
11080     case 0xf: /* SABA, UABA */
11081         if (u) {
11082             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11083         } else {
11084             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11085         }
11086         return;
11087     case 0x10: /* ADD, SUB */
11088         if (u) {
11089             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11090         } else {
11091             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11092         }
11093         return;
11094     case 0x13: /* MUL, PMUL */
11095         if (!u) { /* MUL */
11096             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11097         } else {  /* PMUL */
11098             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11099         }
11100         return;
11101     case 0x12: /* MLA, MLS */
11102         if (u) {
11103             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11104         } else {
11105             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11106         }
11107         return;
11108     case 0x16: /* SQDMULH, SQRDMULH */
11109         {
11110             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11111                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11112                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11113             };
11114             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11115         }
11116         return;
11117     case 0x11:
11118         if (!u) { /* CMTST */
11119             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11120             return;
11121         }
11122         /* else CMEQ */
11123         cond = TCG_COND_EQ;
11124         goto do_gvec_cmp;
11125     case 0x06: /* CMGT, CMHI */
11126         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11127         goto do_gvec_cmp;
11128     case 0x07: /* CMGE, CMHS */
11129         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11130     do_gvec_cmp:
11131         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11132                          vec_full_reg_offset(s, rn),
11133                          vec_full_reg_offset(s, rm),
11134                          is_q ? 16 : 8, vec_full_reg_size(s));
11135         return;
11136     }
11137 
11138     if (size == 3) {
11139         assert(is_q);
11140         for (pass = 0; pass < 2; pass++) {
11141             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11142             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11143             TCGv_i64 tcg_res = tcg_temp_new_i64();
11144 
11145             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11146             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11147 
11148             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11149 
11150             write_vec_element(s, tcg_res, rd, pass, MO_64);
11151         }
11152     } else {
11153         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11154             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11155             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11156             TCGv_i32 tcg_res = tcg_temp_new_i32();
11157             NeonGenTwoOpFn *genfn = NULL;
11158             NeonGenTwoOpEnvFn *genenvfn = NULL;
11159 
11160             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11161             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11162 
11163             switch (opcode) {
11164             case 0x0: /* SHADD, UHADD */
11165             {
11166                 static NeonGenTwoOpFn * const fns[3][2] = {
11167                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11168                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11169                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11170                 };
11171                 genfn = fns[size][u];
11172                 break;
11173             }
11174             case 0x2: /* SRHADD, URHADD */
11175             {
11176                 static NeonGenTwoOpFn * const fns[3][2] = {
11177                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11178                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11179                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11180                 };
11181                 genfn = fns[size][u];
11182                 break;
11183             }
11184             case 0x4: /* SHSUB, UHSUB */
11185             {
11186                 static NeonGenTwoOpFn * const fns[3][2] = {
11187                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11188                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11189                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11190                 };
11191                 genfn = fns[size][u];
11192                 break;
11193             }
11194             case 0x9: /* SQSHL, UQSHL */
11195             {
11196                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11197                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11198                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11199                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11200                 };
11201                 genenvfn = fns[size][u];
11202                 break;
11203             }
11204             case 0xa: /* SRSHL, URSHL */
11205             {
11206                 static NeonGenTwoOpFn * const fns[3][2] = {
11207                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11208                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11209                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11210                 };
11211                 genfn = fns[size][u];
11212                 break;
11213             }
11214             case 0xb: /* SQRSHL, UQRSHL */
11215             {
11216                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11217                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11218                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11219                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11220                 };
11221                 genenvfn = fns[size][u];
11222                 break;
11223             }
11224             default:
11225                 g_assert_not_reached();
11226             }
11227 
11228             if (genenvfn) {
11229                 genenvfn(tcg_res, tcg_env, tcg_op1, tcg_op2);
11230             } else {
11231                 genfn(tcg_res, tcg_op1, tcg_op2);
11232             }
11233 
11234             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11235         }
11236     }
11237     clear_vec_high(s, is_q, rd);
11238 }
11239 
11240 /* AdvSIMD three same
11241  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11242  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11243  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11244  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11245  */
11246 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11247 {
11248     int opcode = extract32(insn, 11, 5);
11249 
11250     switch (opcode) {
11251     case 0x3: /* logic ops */
11252         disas_simd_3same_logic(s, insn);
11253         break;
11254     case 0x18 ... 0x31:
11255         /* floating point ops, sz[1] and U are part of opcode */
11256         disas_simd_3same_float(s, insn);
11257         break;
11258     default:
11259         disas_simd_3same_int(s, insn);
11260         break;
11261     case 0x14: /* SMAXP, UMAXP */
11262     case 0x15: /* SMINP, UMINP */
11263     case 0x17: /* ADDP */
11264         unallocated_encoding(s);
11265         break;
11266     }
11267 }
11268 
11269 /* AdvSIMD three same extra
11270  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11271  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11272  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11273  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11274  */
11275 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11276 {
11277     int rd = extract32(insn, 0, 5);
11278     int rn = extract32(insn, 5, 5);
11279     int opcode = extract32(insn, 11, 4);
11280     int rm = extract32(insn, 16, 5);
11281     int size = extract32(insn, 22, 2);
11282     bool u = extract32(insn, 29, 1);
11283     bool is_q = extract32(insn, 30, 1);
11284     bool feature;
11285     int rot;
11286 
11287     switch (u * 16 + opcode) {
11288     case 0x10: /* SQRDMLAH (vector) */
11289     case 0x11: /* SQRDMLSH (vector) */
11290         if (size != 1 && size != 2) {
11291             unallocated_encoding(s);
11292             return;
11293         }
11294         feature = dc_isar_feature(aa64_rdm, s);
11295         break;
11296     case 0x02: /* SDOT (vector) */
11297     case 0x12: /* UDOT (vector) */
11298         if (size != MO_32) {
11299             unallocated_encoding(s);
11300             return;
11301         }
11302         feature = dc_isar_feature(aa64_dp, s);
11303         break;
11304     case 0x03: /* USDOT */
11305         if (size != MO_32) {
11306             unallocated_encoding(s);
11307             return;
11308         }
11309         feature = dc_isar_feature(aa64_i8mm, s);
11310         break;
11311     case 0x04: /* SMMLA */
11312     case 0x14: /* UMMLA */
11313     case 0x05: /* USMMLA */
11314         if (!is_q || size != MO_32) {
11315             unallocated_encoding(s);
11316             return;
11317         }
11318         feature = dc_isar_feature(aa64_i8mm, s);
11319         break;
11320     case 0x18: /* FCMLA, #0 */
11321     case 0x19: /* FCMLA, #90 */
11322     case 0x1a: /* FCMLA, #180 */
11323     case 0x1b: /* FCMLA, #270 */
11324     case 0x1c: /* FCADD, #90 */
11325     case 0x1e: /* FCADD, #270 */
11326         if (size == 0
11327             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11328             || (size == 3 && !is_q)) {
11329             unallocated_encoding(s);
11330             return;
11331         }
11332         feature = dc_isar_feature(aa64_fcma, s);
11333         break;
11334     case 0x1d: /* BFMMLA */
11335         if (size != MO_16 || !is_q) {
11336             unallocated_encoding(s);
11337             return;
11338         }
11339         feature = dc_isar_feature(aa64_bf16, s);
11340         break;
11341     case 0x1f:
11342         switch (size) {
11343         case 1: /* BFDOT */
11344         case 3: /* BFMLAL{B,T} */
11345             feature = dc_isar_feature(aa64_bf16, s);
11346             break;
11347         default:
11348             unallocated_encoding(s);
11349             return;
11350         }
11351         break;
11352     default:
11353         unallocated_encoding(s);
11354         return;
11355     }
11356     if (!feature) {
11357         unallocated_encoding(s);
11358         return;
11359     }
11360     if (!fp_access_check(s)) {
11361         return;
11362     }
11363 
11364     switch (opcode) {
11365     case 0x0: /* SQRDMLAH (vector) */
11366         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11367         return;
11368 
11369     case 0x1: /* SQRDMLSH (vector) */
11370         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11371         return;
11372 
11373     case 0x2: /* SDOT / UDOT */
11374         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11375                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11376         return;
11377 
11378     case 0x3: /* USDOT */
11379         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11380         return;
11381 
11382     case 0x04: /* SMMLA, UMMLA */
11383         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11384                          u ? gen_helper_gvec_ummla_b
11385                          : gen_helper_gvec_smmla_b);
11386         return;
11387     case 0x05: /* USMMLA */
11388         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11389         return;
11390 
11391     case 0x8: /* FCMLA, #0 */
11392     case 0x9: /* FCMLA, #90 */
11393     case 0xa: /* FCMLA, #180 */
11394     case 0xb: /* FCMLA, #270 */
11395         rot = extract32(opcode, 0, 2);
11396         switch (size) {
11397         case 1:
11398             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11399                               gen_helper_gvec_fcmlah);
11400             break;
11401         case 2:
11402             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11403                               gen_helper_gvec_fcmlas);
11404             break;
11405         case 3:
11406             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11407                               gen_helper_gvec_fcmlad);
11408             break;
11409         default:
11410             g_assert_not_reached();
11411         }
11412         return;
11413 
11414     case 0xc: /* FCADD, #90 */
11415     case 0xe: /* FCADD, #270 */
11416         rot = extract32(opcode, 1, 1);
11417         switch (size) {
11418         case 1:
11419             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11420                               gen_helper_gvec_fcaddh);
11421             break;
11422         case 2:
11423             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11424                               gen_helper_gvec_fcadds);
11425             break;
11426         case 3:
11427             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11428                               gen_helper_gvec_fcaddd);
11429             break;
11430         default:
11431             g_assert_not_reached();
11432         }
11433         return;
11434 
11435     case 0xd: /* BFMMLA */
11436         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11437         return;
11438     case 0xf:
11439         switch (size) {
11440         case 1: /* BFDOT */
11441             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11442             break;
11443         case 3: /* BFMLAL{B,T} */
11444             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11445                               gen_helper_gvec_bfmlal);
11446             break;
11447         default:
11448             g_assert_not_reached();
11449         }
11450         return;
11451 
11452     default:
11453         g_assert_not_reached();
11454     }
11455 }
11456 
11457 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11458                                   int size, int rn, int rd)
11459 {
11460     /* Handle 2-reg-misc ops which are widening (so each size element
11461      * in the source becomes a 2*size element in the destination.
11462      * The only instruction like this is FCVTL.
11463      */
11464     int pass;
11465 
11466     if (size == 3) {
11467         /* 32 -> 64 bit fp conversion */
11468         TCGv_i64 tcg_res[2];
11469         int srcelt = is_q ? 2 : 0;
11470 
11471         for (pass = 0; pass < 2; pass++) {
11472             TCGv_i32 tcg_op = tcg_temp_new_i32();
11473             tcg_res[pass] = tcg_temp_new_i64();
11474 
11475             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11476             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11477         }
11478         for (pass = 0; pass < 2; pass++) {
11479             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11480         }
11481     } else {
11482         /* 16 -> 32 bit fp conversion */
11483         int srcelt = is_q ? 4 : 0;
11484         TCGv_i32 tcg_res[4];
11485         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11486         TCGv_i32 ahp = get_ahp_flag();
11487 
11488         for (pass = 0; pass < 4; pass++) {
11489             tcg_res[pass] = tcg_temp_new_i32();
11490 
11491             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11492             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11493                                            fpst, ahp);
11494         }
11495         for (pass = 0; pass < 4; pass++) {
11496             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11497         }
11498     }
11499 }
11500 
11501 static void handle_rev(DisasContext *s, int opcode, bool u,
11502                        bool is_q, int size, int rn, int rd)
11503 {
11504     int op = (opcode << 1) | u;
11505     int opsz = op + size;
11506     int grp_size = 3 - opsz;
11507     int dsize = is_q ? 128 : 64;
11508     int i;
11509 
11510     if (opsz >= 3) {
11511         unallocated_encoding(s);
11512         return;
11513     }
11514 
11515     if (!fp_access_check(s)) {
11516         return;
11517     }
11518 
11519     if (size == 0) {
11520         /* Special case bytes, use bswap op on each group of elements */
11521         int groups = dsize / (8 << grp_size);
11522 
11523         for (i = 0; i < groups; i++) {
11524             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11525 
11526             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11527             switch (grp_size) {
11528             case MO_16:
11529                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11530                 break;
11531             case MO_32:
11532                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11533                 break;
11534             case MO_64:
11535                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11536                 break;
11537             default:
11538                 g_assert_not_reached();
11539             }
11540             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11541         }
11542         clear_vec_high(s, is_q, rd);
11543     } else {
11544         int revmask = (1 << grp_size) - 1;
11545         int esize = 8 << size;
11546         int elements = dsize / esize;
11547         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11548         TCGv_i64 tcg_rd[2];
11549 
11550         for (i = 0; i < 2; i++) {
11551             tcg_rd[i] = tcg_temp_new_i64();
11552             tcg_gen_movi_i64(tcg_rd[i], 0);
11553         }
11554 
11555         for (i = 0; i < elements; i++) {
11556             int e_rev = (i & 0xf) ^ revmask;
11557             int w = (e_rev * esize) / 64;
11558             int o = (e_rev * esize) % 64;
11559 
11560             read_vec_element(s, tcg_rn, rn, i, size);
11561             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11562         }
11563 
11564         for (i = 0; i < 2; i++) {
11565             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11566         }
11567         clear_vec_high(s, true, rd);
11568     }
11569 }
11570 
11571 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11572                                   bool is_q, int size, int rn, int rd)
11573 {
11574     /* Implement the pairwise operations from 2-misc:
11575      * SADDLP, UADDLP, SADALP, UADALP.
11576      * These all add pairs of elements in the input to produce a
11577      * double-width result element in the output (possibly accumulating).
11578      */
11579     bool accum = (opcode == 0x6);
11580     int maxpass = is_q ? 2 : 1;
11581     int pass;
11582     TCGv_i64 tcg_res[2];
11583 
11584     if (size == 2) {
11585         /* 32 + 32 -> 64 op */
11586         MemOp memop = size + (u ? 0 : MO_SIGN);
11587 
11588         for (pass = 0; pass < maxpass; pass++) {
11589             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11590             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11591 
11592             tcg_res[pass] = tcg_temp_new_i64();
11593 
11594             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11595             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11596             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11597             if (accum) {
11598                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11599                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11600             }
11601         }
11602     } else {
11603         for (pass = 0; pass < maxpass; pass++) {
11604             TCGv_i64 tcg_op = tcg_temp_new_i64();
11605             NeonGenOne64OpFn *genfn;
11606             static NeonGenOne64OpFn * const fns[2][2] = {
11607                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11608                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11609             };
11610 
11611             genfn = fns[size][u];
11612 
11613             tcg_res[pass] = tcg_temp_new_i64();
11614 
11615             read_vec_element(s, tcg_op, rn, pass, MO_64);
11616             genfn(tcg_res[pass], tcg_op);
11617 
11618             if (accum) {
11619                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11620                 if (size == 0) {
11621                     gen_helper_neon_addl_u16(tcg_res[pass],
11622                                              tcg_res[pass], tcg_op);
11623                 } else {
11624                     gen_helper_neon_addl_u32(tcg_res[pass],
11625                                              tcg_res[pass], tcg_op);
11626                 }
11627             }
11628         }
11629     }
11630     if (!is_q) {
11631         tcg_res[1] = tcg_constant_i64(0);
11632     }
11633     for (pass = 0; pass < 2; pass++) {
11634         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11635     }
11636 }
11637 
11638 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11639 {
11640     /* Implement SHLL and SHLL2 */
11641     int pass;
11642     int part = is_q ? 2 : 0;
11643     TCGv_i64 tcg_res[2];
11644 
11645     for (pass = 0; pass < 2; pass++) {
11646         static NeonGenWidenFn * const widenfns[3] = {
11647             gen_helper_neon_widen_u8,
11648             gen_helper_neon_widen_u16,
11649             tcg_gen_extu_i32_i64,
11650         };
11651         NeonGenWidenFn *widenfn = widenfns[size];
11652         TCGv_i32 tcg_op = tcg_temp_new_i32();
11653 
11654         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11655         tcg_res[pass] = tcg_temp_new_i64();
11656         widenfn(tcg_res[pass], tcg_op);
11657         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11658     }
11659 
11660     for (pass = 0; pass < 2; pass++) {
11661         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11662     }
11663 }
11664 
11665 /* AdvSIMD two reg misc
11666  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11667  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11668  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11669  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11670  */
11671 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11672 {
11673     int size = extract32(insn, 22, 2);
11674     int opcode = extract32(insn, 12, 5);
11675     bool u = extract32(insn, 29, 1);
11676     bool is_q = extract32(insn, 30, 1);
11677     int rn = extract32(insn, 5, 5);
11678     int rd = extract32(insn, 0, 5);
11679     bool need_fpstatus = false;
11680     int rmode = -1;
11681     TCGv_i32 tcg_rmode;
11682     TCGv_ptr tcg_fpstatus;
11683 
11684     switch (opcode) {
11685     case 0x0: /* REV64, REV32 */
11686     case 0x1: /* REV16 */
11687         handle_rev(s, opcode, u, is_q, size, rn, rd);
11688         return;
11689     case 0x5: /* CNT, NOT, RBIT */
11690         if (u && size == 0) {
11691             /* NOT */
11692             break;
11693         } else if (u && size == 1) {
11694             /* RBIT */
11695             break;
11696         } else if (!u && size == 0) {
11697             /* CNT */
11698             break;
11699         }
11700         unallocated_encoding(s);
11701         return;
11702     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11703     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11704         if (size == 3) {
11705             unallocated_encoding(s);
11706             return;
11707         }
11708         if (!fp_access_check(s)) {
11709             return;
11710         }
11711 
11712         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11713         return;
11714     case 0x4: /* CLS, CLZ */
11715         if (size == 3) {
11716             unallocated_encoding(s);
11717             return;
11718         }
11719         break;
11720     case 0x2: /* SADDLP, UADDLP */
11721     case 0x6: /* SADALP, UADALP */
11722         if (size == 3) {
11723             unallocated_encoding(s);
11724             return;
11725         }
11726         if (!fp_access_check(s)) {
11727             return;
11728         }
11729         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11730         return;
11731     case 0x13: /* SHLL, SHLL2 */
11732         if (u == 0 || size == 3) {
11733             unallocated_encoding(s);
11734             return;
11735         }
11736         if (!fp_access_check(s)) {
11737             return;
11738         }
11739         handle_shll(s, is_q, size, rn, rd);
11740         return;
11741     case 0xa: /* CMLT */
11742         if (u == 1) {
11743             unallocated_encoding(s);
11744             return;
11745         }
11746         /* fall through */
11747     case 0x8: /* CMGT, CMGE */
11748     case 0x9: /* CMEQ, CMLE */
11749     case 0xb: /* ABS, NEG */
11750         if (size == 3 && !is_q) {
11751             unallocated_encoding(s);
11752             return;
11753         }
11754         break;
11755     case 0x3: /* SUQADD, USQADD */
11756         if (size == 3 && !is_q) {
11757             unallocated_encoding(s);
11758             return;
11759         }
11760         if (!fp_access_check(s)) {
11761             return;
11762         }
11763         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11764         return;
11765     case 0x7: /* SQABS, SQNEG */
11766         if (size == 3 && !is_q) {
11767             unallocated_encoding(s);
11768             return;
11769         }
11770         break;
11771     case 0xc ... 0xf:
11772     case 0x16 ... 0x1f:
11773     {
11774         /* Floating point: U, size[1] and opcode indicate operation;
11775          * size[0] indicates single or double precision.
11776          */
11777         int is_double = extract32(size, 0, 1);
11778         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11779         size = is_double ? 3 : 2;
11780         switch (opcode) {
11781         case 0x2f: /* FABS */
11782         case 0x6f: /* FNEG */
11783             if (size == 3 && !is_q) {
11784                 unallocated_encoding(s);
11785                 return;
11786             }
11787             break;
11788         case 0x1d: /* SCVTF */
11789         case 0x5d: /* UCVTF */
11790         {
11791             bool is_signed = (opcode == 0x1d) ? true : false;
11792             int elements = is_double ? 2 : is_q ? 4 : 2;
11793             if (is_double && !is_q) {
11794                 unallocated_encoding(s);
11795                 return;
11796             }
11797             if (!fp_access_check(s)) {
11798                 return;
11799             }
11800             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11801             return;
11802         }
11803         case 0x2c: /* FCMGT (zero) */
11804         case 0x2d: /* FCMEQ (zero) */
11805         case 0x2e: /* FCMLT (zero) */
11806         case 0x6c: /* FCMGE (zero) */
11807         case 0x6d: /* FCMLE (zero) */
11808             if (size == 3 && !is_q) {
11809                 unallocated_encoding(s);
11810                 return;
11811             }
11812             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11813             return;
11814         case 0x7f: /* FSQRT */
11815             if (size == 3 && !is_q) {
11816                 unallocated_encoding(s);
11817                 return;
11818             }
11819             break;
11820         case 0x1a: /* FCVTNS */
11821         case 0x1b: /* FCVTMS */
11822         case 0x3a: /* FCVTPS */
11823         case 0x3b: /* FCVTZS */
11824         case 0x5a: /* FCVTNU */
11825         case 0x5b: /* FCVTMU */
11826         case 0x7a: /* FCVTPU */
11827         case 0x7b: /* FCVTZU */
11828             need_fpstatus = true;
11829             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11830             if (size == 3 && !is_q) {
11831                 unallocated_encoding(s);
11832                 return;
11833             }
11834             break;
11835         case 0x5c: /* FCVTAU */
11836         case 0x1c: /* FCVTAS */
11837             need_fpstatus = true;
11838             rmode = FPROUNDING_TIEAWAY;
11839             if (size == 3 && !is_q) {
11840                 unallocated_encoding(s);
11841                 return;
11842             }
11843             break;
11844         case 0x3c: /* URECPE */
11845             if (size == 3) {
11846                 unallocated_encoding(s);
11847                 return;
11848             }
11849             /* fall through */
11850         case 0x3d: /* FRECPE */
11851         case 0x7d: /* FRSQRTE */
11852             if (size == 3 && !is_q) {
11853                 unallocated_encoding(s);
11854                 return;
11855             }
11856             if (!fp_access_check(s)) {
11857                 return;
11858             }
11859             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
11860             return;
11861         case 0x56: /* FCVTXN, FCVTXN2 */
11862             if (size == 2) {
11863                 unallocated_encoding(s);
11864                 return;
11865             }
11866             /* fall through */
11867         case 0x16: /* FCVTN, FCVTN2 */
11868             /* handle_2misc_narrow does a 2*size -> size operation, but these
11869              * instructions encode the source size rather than dest size.
11870              */
11871             if (!fp_access_check(s)) {
11872                 return;
11873             }
11874             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11875             return;
11876         case 0x36: /* BFCVTN, BFCVTN2 */
11877             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
11878                 unallocated_encoding(s);
11879                 return;
11880             }
11881             if (!fp_access_check(s)) {
11882                 return;
11883             }
11884             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11885             return;
11886         case 0x17: /* FCVTL, FCVTL2 */
11887             if (!fp_access_check(s)) {
11888                 return;
11889             }
11890             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
11891             return;
11892         case 0x18: /* FRINTN */
11893         case 0x19: /* FRINTM */
11894         case 0x38: /* FRINTP */
11895         case 0x39: /* FRINTZ */
11896             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11897             /* fall through */
11898         case 0x59: /* FRINTX */
11899         case 0x79: /* FRINTI */
11900             need_fpstatus = true;
11901             if (size == 3 && !is_q) {
11902                 unallocated_encoding(s);
11903                 return;
11904             }
11905             break;
11906         case 0x58: /* FRINTA */
11907             rmode = FPROUNDING_TIEAWAY;
11908             need_fpstatus = true;
11909             if (size == 3 && !is_q) {
11910                 unallocated_encoding(s);
11911                 return;
11912             }
11913             break;
11914         case 0x7c: /* URSQRTE */
11915             if (size == 3) {
11916                 unallocated_encoding(s);
11917                 return;
11918             }
11919             break;
11920         case 0x1e: /* FRINT32Z */
11921         case 0x1f: /* FRINT64Z */
11922             rmode = FPROUNDING_ZERO;
11923             /* fall through */
11924         case 0x5e: /* FRINT32X */
11925         case 0x5f: /* FRINT64X */
11926             need_fpstatus = true;
11927             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
11928                 unallocated_encoding(s);
11929                 return;
11930             }
11931             break;
11932         default:
11933             unallocated_encoding(s);
11934             return;
11935         }
11936         break;
11937     }
11938     default:
11939         unallocated_encoding(s);
11940         return;
11941     }
11942 
11943     if (!fp_access_check(s)) {
11944         return;
11945     }
11946 
11947     if (need_fpstatus || rmode >= 0) {
11948         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
11949     } else {
11950         tcg_fpstatus = NULL;
11951     }
11952     if (rmode >= 0) {
11953         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
11954     } else {
11955         tcg_rmode = NULL;
11956     }
11957 
11958     switch (opcode) {
11959     case 0x5:
11960         if (u && size == 0) { /* NOT */
11961             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
11962             return;
11963         }
11964         break;
11965     case 0x8: /* CMGT, CMGE */
11966         if (u) {
11967             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
11968         } else {
11969             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
11970         }
11971         return;
11972     case 0x9: /* CMEQ, CMLE */
11973         if (u) {
11974             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
11975         } else {
11976             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
11977         }
11978         return;
11979     case 0xa: /* CMLT */
11980         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
11981         return;
11982     case 0xb:
11983         if (u) { /* ABS, NEG */
11984             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
11985         } else {
11986             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
11987         }
11988         return;
11989     }
11990 
11991     if (size == 3) {
11992         /* All 64-bit element operations can be shared with scalar 2misc */
11993         int pass;
11994 
11995         /* Coverity claims (size == 3 && !is_q) has been eliminated
11996          * from all paths leading to here.
11997          */
11998         tcg_debug_assert(is_q);
11999         for (pass = 0; pass < 2; pass++) {
12000             TCGv_i64 tcg_op = tcg_temp_new_i64();
12001             TCGv_i64 tcg_res = tcg_temp_new_i64();
12002 
12003             read_vec_element(s, tcg_op, rn, pass, MO_64);
12004 
12005             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12006                             tcg_rmode, tcg_fpstatus);
12007 
12008             write_vec_element(s, tcg_res, rd, pass, MO_64);
12009         }
12010     } else {
12011         int pass;
12012 
12013         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12014             TCGv_i32 tcg_op = tcg_temp_new_i32();
12015             TCGv_i32 tcg_res = tcg_temp_new_i32();
12016 
12017             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12018 
12019             if (size == 2) {
12020                 /* Special cases for 32 bit elements */
12021                 switch (opcode) {
12022                 case 0x4: /* CLS */
12023                     if (u) {
12024                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12025                     } else {
12026                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12027                     }
12028                     break;
12029                 case 0x7: /* SQABS, SQNEG */
12030                     if (u) {
12031                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
12032                     } else {
12033                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
12034                     }
12035                     break;
12036                 case 0x2f: /* FABS */
12037                     gen_vfp_abss(tcg_res, tcg_op);
12038                     break;
12039                 case 0x6f: /* FNEG */
12040                     gen_vfp_negs(tcg_res, tcg_op);
12041                     break;
12042                 case 0x7f: /* FSQRT */
12043                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
12044                     break;
12045                 case 0x1a: /* FCVTNS */
12046                 case 0x1b: /* FCVTMS */
12047                 case 0x1c: /* FCVTAS */
12048                 case 0x3a: /* FCVTPS */
12049                 case 0x3b: /* FCVTZS */
12050                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12051                                          tcg_constant_i32(0), tcg_fpstatus);
12052                     break;
12053                 case 0x5a: /* FCVTNU */
12054                 case 0x5b: /* FCVTMU */
12055                 case 0x5c: /* FCVTAU */
12056                 case 0x7a: /* FCVTPU */
12057                 case 0x7b: /* FCVTZU */
12058                     gen_helper_vfp_touls(tcg_res, tcg_op,
12059                                          tcg_constant_i32(0), tcg_fpstatus);
12060                     break;
12061                 case 0x18: /* FRINTN */
12062                 case 0x19: /* FRINTM */
12063                 case 0x38: /* FRINTP */
12064                 case 0x39: /* FRINTZ */
12065                 case 0x58: /* FRINTA */
12066                 case 0x79: /* FRINTI */
12067                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12068                     break;
12069                 case 0x59: /* FRINTX */
12070                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12071                     break;
12072                 case 0x7c: /* URSQRTE */
12073                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12074                     break;
12075                 case 0x1e: /* FRINT32Z */
12076                 case 0x5e: /* FRINT32X */
12077                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12078                     break;
12079                 case 0x1f: /* FRINT64Z */
12080                 case 0x5f: /* FRINT64X */
12081                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12082                     break;
12083                 default:
12084                     g_assert_not_reached();
12085                 }
12086             } else {
12087                 /* Use helpers for 8 and 16 bit elements */
12088                 switch (opcode) {
12089                 case 0x5: /* CNT, RBIT */
12090                     /* For these two insns size is part of the opcode specifier
12091                      * (handled earlier); they always operate on byte elements.
12092                      */
12093                     if (u) {
12094                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12095                     } else {
12096                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12097                     }
12098                     break;
12099                 case 0x7: /* SQABS, SQNEG */
12100                 {
12101                     NeonGenOneOpEnvFn *genfn;
12102                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12103                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12104                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12105                     };
12106                     genfn = fns[size][u];
12107                     genfn(tcg_res, tcg_env, tcg_op);
12108                     break;
12109                 }
12110                 case 0x4: /* CLS, CLZ */
12111                     if (u) {
12112                         if (size == 0) {
12113                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12114                         } else {
12115                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12116                         }
12117                     } else {
12118                         if (size == 0) {
12119                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12120                         } else {
12121                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12122                         }
12123                     }
12124                     break;
12125                 default:
12126                     g_assert_not_reached();
12127                 }
12128             }
12129 
12130             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12131         }
12132     }
12133     clear_vec_high(s, is_q, rd);
12134 
12135     if (tcg_rmode) {
12136         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12137     }
12138 }
12139 
12140 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12141  *
12142  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12143  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12144  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12145  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12146  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12147  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12148  *
12149  * This actually covers two groups where scalar access is governed by
12150  * bit 28. A bunch of the instructions (float to integral) only exist
12151  * in the vector form and are un-allocated for the scalar decode. Also
12152  * in the scalar decode Q is always 1.
12153  */
12154 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12155 {
12156     int fpop, opcode, a, u;
12157     int rn, rd;
12158     bool is_q;
12159     bool is_scalar;
12160     bool only_in_vector = false;
12161 
12162     int pass;
12163     TCGv_i32 tcg_rmode = NULL;
12164     TCGv_ptr tcg_fpstatus = NULL;
12165     bool need_fpst = true;
12166     int rmode = -1;
12167 
12168     if (!dc_isar_feature(aa64_fp16, s)) {
12169         unallocated_encoding(s);
12170         return;
12171     }
12172 
12173     rd = extract32(insn, 0, 5);
12174     rn = extract32(insn, 5, 5);
12175 
12176     a = extract32(insn, 23, 1);
12177     u = extract32(insn, 29, 1);
12178     is_scalar = extract32(insn, 28, 1);
12179     is_q = extract32(insn, 30, 1);
12180 
12181     opcode = extract32(insn, 12, 5);
12182     fpop = deposit32(opcode, 5, 1, a);
12183     fpop = deposit32(fpop, 6, 1, u);
12184 
12185     switch (fpop) {
12186     case 0x1d: /* SCVTF */
12187     case 0x5d: /* UCVTF */
12188     {
12189         int elements;
12190 
12191         if (is_scalar) {
12192             elements = 1;
12193         } else {
12194             elements = (is_q ? 8 : 4);
12195         }
12196 
12197         if (!fp_access_check(s)) {
12198             return;
12199         }
12200         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12201         return;
12202     }
12203     break;
12204     case 0x2c: /* FCMGT (zero) */
12205     case 0x2d: /* FCMEQ (zero) */
12206     case 0x2e: /* FCMLT (zero) */
12207     case 0x6c: /* FCMGE (zero) */
12208     case 0x6d: /* FCMLE (zero) */
12209         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12210         return;
12211     case 0x3d: /* FRECPE */
12212     case 0x3f: /* FRECPX */
12213         break;
12214     case 0x18: /* FRINTN */
12215         only_in_vector = true;
12216         rmode = FPROUNDING_TIEEVEN;
12217         break;
12218     case 0x19: /* FRINTM */
12219         only_in_vector = true;
12220         rmode = FPROUNDING_NEGINF;
12221         break;
12222     case 0x38: /* FRINTP */
12223         only_in_vector = true;
12224         rmode = FPROUNDING_POSINF;
12225         break;
12226     case 0x39: /* FRINTZ */
12227         only_in_vector = true;
12228         rmode = FPROUNDING_ZERO;
12229         break;
12230     case 0x58: /* FRINTA */
12231         only_in_vector = true;
12232         rmode = FPROUNDING_TIEAWAY;
12233         break;
12234     case 0x59: /* FRINTX */
12235     case 0x79: /* FRINTI */
12236         only_in_vector = true;
12237         /* current rounding mode */
12238         break;
12239     case 0x1a: /* FCVTNS */
12240         rmode = FPROUNDING_TIEEVEN;
12241         break;
12242     case 0x1b: /* FCVTMS */
12243         rmode = FPROUNDING_NEGINF;
12244         break;
12245     case 0x1c: /* FCVTAS */
12246         rmode = FPROUNDING_TIEAWAY;
12247         break;
12248     case 0x3a: /* FCVTPS */
12249         rmode = FPROUNDING_POSINF;
12250         break;
12251     case 0x3b: /* FCVTZS */
12252         rmode = FPROUNDING_ZERO;
12253         break;
12254     case 0x5a: /* FCVTNU */
12255         rmode = FPROUNDING_TIEEVEN;
12256         break;
12257     case 0x5b: /* FCVTMU */
12258         rmode = FPROUNDING_NEGINF;
12259         break;
12260     case 0x5c: /* FCVTAU */
12261         rmode = FPROUNDING_TIEAWAY;
12262         break;
12263     case 0x7a: /* FCVTPU */
12264         rmode = FPROUNDING_POSINF;
12265         break;
12266     case 0x7b: /* FCVTZU */
12267         rmode = FPROUNDING_ZERO;
12268         break;
12269     case 0x2f: /* FABS */
12270     case 0x6f: /* FNEG */
12271         need_fpst = false;
12272         break;
12273     case 0x7d: /* FRSQRTE */
12274     case 0x7f: /* FSQRT (vector) */
12275         break;
12276     default:
12277         unallocated_encoding(s);
12278         return;
12279     }
12280 
12281 
12282     /* Check additional constraints for the scalar encoding */
12283     if (is_scalar) {
12284         if (!is_q) {
12285             unallocated_encoding(s);
12286             return;
12287         }
12288         /* FRINTxx is only in the vector form */
12289         if (only_in_vector) {
12290             unallocated_encoding(s);
12291             return;
12292         }
12293     }
12294 
12295     if (!fp_access_check(s)) {
12296         return;
12297     }
12298 
12299     if (rmode >= 0 || need_fpst) {
12300         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12301     }
12302 
12303     if (rmode >= 0) {
12304         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12305     }
12306 
12307     if (is_scalar) {
12308         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12309         TCGv_i32 tcg_res = tcg_temp_new_i32();
12310 
12311         switch (fpop) {
12312         case 0x1a: /* FCVTNS */
12313         case 0x1b: /* FCVTMS */
12314         case 0x1c: /* FCVTAS */
12315         case 0x3a: /* FCVTPS */
12316         case 0x3b: /* FCVTZS */
12317             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12318             break;
12319         case 0x3d: /* FRECPE */
12320             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12321             break;
12322         case 0x3f: /* FRECPX */
12323             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12324             break;
12325         case 0x5a: /* FCVTNU */
12326         case 0x5b: /* FCVTMU */
12327         case 0x5c: /* FCVTAU */
12328         case 0x7a: /* FCVTPU */
12329         case 0x7b: /* FCVTZU */
12330             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12331             break;
12332         case 0x6f: /* FNEG */
12333             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12334             break;
12335         case 0x7d: /* FRSQRTE */
12336             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12337             break;
12338         default:
12339             g_assert_not_reached();
12340         }
12341 
12342         /* limit any sign extension going on */
12343         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12344         write_fp_sreg(s, rd, tcg_res);
12345     } else {
12346         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12347             TCGv_i32 tcg_op = tcg_temp_new_i32();
12348             TCGv_i32 tcg_res = tcg_temp_new_i32();
12349 
12350             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12351 
12352             switch (fpop) {
12353             case 0x1a: /* FCVTNS */
12354             case 0x1b: /* FCVTMS */
12355             case 0x1c: /* FCVTAS */
12356             case 0x3a: /* FCVTPS */
12357             case 0x3b: /* FCVTZS */
12358                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12359                 break;
12360             case 0x3d: /* FRECPE */
12361                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12362                 break;
12363             case 0x5a: /* FCVTNU */
12364             case 0x5b: /* FCVTMU */
12365             case 0x5c: /* FCVTAU */
12366             case 0x7a: /* FCVTPU */
12367             case 0x7b: /* FCVTZU */
12368                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12369                 break;
12370             case 0x18: /* FRINTN */
12371             case 0x19: /* FRINTM */
12372             case 0x38: /* FRINTP */
12373             case 0x39: /* FRINTZ */
12374             case 0x58: /* FRINTA */
12375             case 0x79: /* FRINTI */
12376                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12377                 break;
12378             case 0x59: /* FRINTX */
12379                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12380                 break;
12381             case 0x2f: /* FABS */
12382                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12383                 break;
12384             case 0x6f: /* FNEG */
12385                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12386                 break;
12387             case 0x7d: /* FRSQRTE */
12388                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12389                 break;
12390             case 0x7f: /* FSQRT */
12391                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12392                 break;
12393             default:
12394                 g_assert_not_reached();
12395             }
12396 
12397             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12398         }
12399 
12400         clear_vec_high(s, is_q, rd);
12401     }
12402 
12403     if (tcg_rmode) {
12404         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12405     }
12406 }
12407 
12408 /* AdvSIMD scalar x indexed element
12409  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12410  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12411  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12412  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12413  * AdvSIMD vector x indexed element
12414  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12415  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12416  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12417  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12418  */
12419 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12420 {
12421     /* This encoding has two kinds of instruction:
12422      *  normal, where we perform elt x idxelt => elt for each
12423      *     element in the vector
12424      *  long, where we perform elt x idxelt and generate a result of
12425      *     double the width of the input element
12426      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12427      */
12428     bool is_scalar = extract32(insn, 28, 1);
12429     bool is_q = extract32(insn, 30, 1);
12430     bool u = extract32(insn, 29, 1);
12431     int size = extract32(insn, 22, 2);
12432     int l = extract32(insn, 21, 1);
12433     int m = extract32(insn, 20, 1);
12434     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12435     int rm = extract32(insn, 16, 4);
12436     int opcode = extract32(insn, 12, 4);
12437     int h = extract32(insn, 11, 1);
12438     int rn = extract32(insn, 5, 5);
12439     int rd = extract32(insn, 0, 5);
12440     bool is_long = false;
12441     int is_fp = 0;
12442     bool is_fp16 = false;
12443     int index;
12444     TCGv_ptr fpst;
12445 
12446     switch (16 * u + opcode) {
12447     case 0x08: /* MUL */
12448     case 0x10: /* MLA */
12449     case 0x14: /* MLS */
12450         if (is_scalar) {
12451             unallocated_encoding(s);
12452             return;
12453         }
12454         break;
12455     case 0x02: /* SMLAL, SMLAL2 */
12456     case 0x12: /* UMLAL, UMLAL2 */
12457     case 0x06: /* SMLSL, SMLSL2 */
12458     case 0x16: /* UMLSL, UMLSL2 */
12459     case 0x0a: /* SMULL, SMULL2 */
12460     case 0x1a: /* UMULL, UMULL2 */
12461         if (is_scalar) {
12462             unallocated_encoding(s);
12463             return;
12464         }
12465         is_long = true;
12466         break;
12467     case 0x03: /* SQDMLAL, SQDMLAL2 */
12468     case 0x07: /* SQDMLSL, SQDMLSL2 */
12469     case 0x0b: /* SQDMULL, SQDMULL2 */
12470         is_long = true;
12471         break;
12472     case 0x0c: /* SQDMULH */
12473     case 0x0d: /* SQRDMULH */
12474         break;
12475     case 0x1d: /* SQRDMLAH */
12476     case 0x1f: /* SQRDMLSH */
12477         if (!dc_isar_feature(aa64_rdm, s)) {
12478             unallocated_encoding(s);
12479             return;
12480         }
12481         break;
12482     case 0x0e: /* SDOT */
12483     case 0x1e: /* UDOT */
12484         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12485             unallocated_encoding(s);
12486             return;
12487         }
12488         break;
12489     case 0x0f:
12490         switch (size) {
12491         case 0: /* SUDOT */
12492         case 2: /* USDOT */
12493             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12494                 unallocated_encoding(s);
12495                 return;
12496             }
12497             size = MO_32;
12498             break;
12499         case 1: /* BFDOT */
12500             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12501                 unallocated_encoding(s);
12502                 return;
12503             }
12504             size = MO_32;
12505             break;
12506         case 3: /* BFMLAL{B,T} */
12507             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12508                 unallocated_encoding(s);
12509                 return;
12510             }
12511             /* can't set is_fp without other incorrect size checks */
12512             size = MO_16;
12513             break;
12514         default:
12515             unallocated_encoding(s);
12516             return;
12517         }
12518         break;
12519     case 0x11: /* FCMLA #0 */
12520     case 0x13: /* FCMLA #90 */
12521     case 0x15: /* FCMLA #180 */
12522     case 0x17: /* FCMLA #270 */
12523         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12524             unallocated_encoding(s);
12525             return;
12526         }
12527         is_fp = 2;
12528         break;
12529     case 0x00: /* FMLAL */
12530     case 0x04: /* FMLSL */
12531     case 0x18: /* FMLAL2 */
12532     case 0x1c: /* FMLSL2 */
12533         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12534             unallocated_encoding(s);
12535             return;
12536         }
12537         size = MO_16;
12538         /* is_fp, but we pass tcg_env not fp_status.  */
12539         break;
12540     default:
12541     case 0x01: /* FMLA */
12542     case 0x05: /* FMLS */
12543     case 0x09: /* FMUL */
12544     case 0x19: /* FMULX */
12545         unallocated_encoding(s);
12546         return;
12547     }
12548 
12549     switch (is_fp) {
12550     case 1: /* normal fp */
12551         unallocated_encoding(s); /* in decodetree */
12552         return;
12553 
12554     case 2: /* complex fp */
12555         /* Each indexable element is a complex pair.  */
12556         size += 1;
12557         switch (size) {
12558         case MO_32:
12559             if (h && !is_q) {
12560                 unallocated_encoding(s);
12561                 return;
12562             }
12563             is_fp16 = true;
12564             break;
12565         case MO_64:
12566             break;
12567         default:
12568             unallocated_encoding(s);
12569             return;
12570         }
12571         break;
12572 
12573     default: /* integer */
12574         switch (size) {
12575         case MO_8:
12576         case MO_64:
12577             unallocated_encoding(s);
12578             return;
12579         }
12580         break;
12581     }
12582     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12583         unallocated_encoding(s);
12584         return;
12585     }
12586 
12587     /* Given MemOp size, adjust register and indexing.  */
12588     switch (size) {
12589     case MO_16:
12590         index = h << 2 | l << 1 | m;
12591         break;
12592     case MO_32:
12593         index = h << 1 | l;
12594         rm |= m << 4;
12595         break;
12596     case MO_64:
12597         if (l || !is_q) {
12598             unallocated_encoding(s);
12599             return;
12600         }
12601         index = h;
12602         rm |= m << 4;
12603         break;
12604     default:
12605         g_assert_not_reached();
12606     }
12607 
12608     if (!fp_access_check(s)) {
12609         return;
12610     }
12611 
12612     if (is_fp) {
12613         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12614     } else {
12615         fpst = NULL;
12616     }
12617 
12618     switch (16 * u + opcode) {
12619     case 0x0e: /* SDOT */
12620     case 0x1e: /* UDOT */
12621         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12622                          u ? gen_helper_gvec_udot_idx_b
12623                          : gen_helper_gvec_sdot_idx_b);
12624         return;
12625     case 0x0f:
12626         switch (extract32(insn, 22, 2)) {
12627         case 0: /* SUDOT */
12628             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12629                              gen_helper_gvec_sudot_idx_b);
12630             return;
12631         case 1: /* BFDOT */
12632             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12633                              gen_helper_gvec_bfdot_idx);
12634             return;
12635         case 2: /* USDOT */
12636             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12637                              gen_helper_gvec_usdot_idx_b);
12638             return;
12639         case 3: /* BFMLAL{B,T} */
12640             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12641                               gen_helper_gvec_bfmlal_idx);
12642             return;
12643         }
12644         g_assert_not_reached();
12645     case 0x11: /* FCMLA #0 */
12646     case 0x13: /* FCMLA #90 */
12647     case 0x15: /* FCMLA #180 */
12648     case 0x17: /* FCMLA #270 */
12649         {
12650             int rot = extract32(insn, 13, 2);
12651             int data = (index << 2) | rot;
12652             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12653                                vec_full_reg_offset(s, rn),
12654                                vec_full_reg_offset(s, rm),
12655                                vec_full_reg_offset(s, rd), fpst,
12656                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12657                                size == MO_64
12658                                ? gen_helper_gvec_fcmlas_idx
12659                                : gen_helper_gvec_fcmlah_idx);
12660         }
12661         return;
12662 
12663     case 0x00: /* FMLAL */
12664     case 0x04: /* FMLSL */
12665     case 0x18: /* FMLAL2 */
12666     case 0x1c: /* FMLSL2 */
12667         {
12668             int is_s = extract32(opcode, 2, 1);
12669             int is_2 = u;
12670             int data = (index << 2) | (is_2 << 1) | is_s;
12671             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12672                                vec_full_reg_offset(s, rn),
12673                                vec_full_reg_offset(s, rm), tcg_env,
12674                                is_q ? 16 : 8, vec_full_reg_size(s),
12675                                data, gen_helper_gvec_fmlal_idx_a64);
12676         }
12677         return;
12678 
12679     case 0x08: /* MUL */
12680         if (!is_long && !is_scalar) {
12681             static gen_helper_gvec_3 * const fns[3] = {
12682                 gen_helper_gvec_mul_idx_h,
12683                 gen_helper_gvec_mul_idx_s,
12684                 gen_helper_gvec_mul_idx_d,
12685             };
12686             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
12687                                vec_full_reg_offset(s, rn),
12688                                vec_full_reg_offset(s, rm),
12689                                is_q ? 16 : 8, vec_full_reg_size(s),
12690                                index, fns[size - 1]);
12691             return;
12692         }
12693         break;
12694 
12695     case 0x10: /* MLA */
12696         if (!is_long && !is_scalar) {
12697             static gen_helper_gvec_4 * const fns[3] = {
12698                 gen_helper_gvec_mla_idx_h,
12699                 gen_helper_gvec_mla_idx_s,
12700                 gen_helper_gvec_mla_idx_d,
12701             };
12702             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12703                                vec_full_reg_offset(s, rn),
12704                                vec_full_reg_offset(s, rm),
12705                                vec_full_reg_offset(s, rd),
12706                                is_q ? 16 : 8, vec_full_reg_size(s),
12707                                index, fns[size - 1]);
12708             return;
12709         }
12710         break;
12711 
12712     case 0x14: /* MLS */
12713         if (!is_long && !is_scalar) {
12714             static gen_helper_gvec_4 * const fns[3] = {
12715                 gen_helper_gvec_mls_idx_h,
12716                 gen_helper_gvec_mls_idx_s,
12717                 gen_helper_gvec_mls_idx_d,
12718             };
12719             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12720                                vec_full_reg_offset(s, rn),
12721                                vec_full_reg_offset(s, rm),
12722                                vec_full_reg_offset(s, rd),
12723                                is_q ? 16 : 8, vec_full_reg_size(s),
12724                                index, fns[size - 1]);
12725             return;
12726         }
12727         break;
12728     }
12729 
12730     if (size == 3) {
12731         g_assert_not_reached();
12732     } else if (!is_long) {
12733         /* 32 bit floating point, or 16 or 32 bit integer.
12734          * For the 16 bit scalar case we use the usual Neon helpers and
12735          * rely on the fact that 0 op 0 == 0 with no side effects.
12736          */
12737         TCGv_i32 tcg_idx = tcg_temp_new_i32();
12738         int pass, maxpasses;
12739 
12740         if (is_scalar) {
12741             maxpasses = 1;
12742         } else {
12743             maxpasses = is_q ? 4 : 2;
12744         }
12745 
12746         read_vec_element_i32(s, tcg_idx, rm, index, size);
12747 
12748         if (size == 1 && !is_scalar) {
12749             /* The simplest way to handle the 16x16 indexed ops is to duplicate
12750              * the index into both halves of the 32 bit tcg_idx and then use
12751              * the usual Neon helpers.
12752              */
12753             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12754         }
12755 
12756         for (pass = 0; pass < maxpasses; pass++) {
12757             TCGv_i32 tcg_op = tcg_temp_new_i32();
12758             TCGv_i32 tcg_res = tcg_temp_new_i32();
12759 
12760             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
12761 
12762             switch (16 * u + opcode) {
12763             case 0x08: /* MUL */
12764             case 0x10: /* MLA */
12765             case 0x14: /* MLS */
12766             {
12767                 static NeonGenTwoOpFn * const fns[2][2] = {
12768                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
12769                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
12770                 };
12771                 NeonGenTwoOpFn *genfn;
12772                 bool is_sub = opcode == 0x4;
12773 
12774                 if (size == 1) {
12775                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
12776                 } else {
12777                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
12778                 }
12779                 if (opcode == 0x8) {
12780                     break;
12781                 }
12782                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
12783                 genfn = fns[size - 1][is_sub];
12784                 genfn(tcg_res, tcg_op, tcg_res);
12785                 break;
12786             }
12787             case 0x0c: /* SQDMULH */
12788                 if (size == 1) {
12789                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
12790                                                tcg_op, tcg_idx);
12791                 } else {
12792                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
12793                                                tcg_op, tcg_idx);
12794                 }
12795                 break;
12796             case 0x0d: /* SQRDMULH */
12797                 if (size == 1) {
12798                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
12799                                                 tcg_op, tcg_idx);
12800                 } else {
12801                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
12802                                                 tcg_op, tcg_idx);
12803                 }
12804                 break;
12805             case 0x1d: /* SQRDMLAH */
12806                 read_vec_element_i32(s, tcg_res, rd, pass,
12807                                      is_scalar ? size : MO_32);
12808                 if (size == 1) {
12809                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
12810                                                 tcg_op, tcg_idx, tcg_res);
12811                 } else {
12812                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
12813                                                 tcg_op, tcg_idx, tcg_res);
12814                 }
12815                 break;
12816             case 0x1f: /* SQRDMLSH */
12817                 read_vec_element_i32(s, tcg_res, rd, pass,
12818                                      is_scalar ? size : MO_32);
12819                 if (size == 1) {
12820                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
12821                                                 tcg_op, tcg_idx, tcg_res);
12822                 } else {
12823                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
12824                                                 tcg_op, tcg_idx, tcg_res);
12825                 }
12826                 break;
12827             default:
12828             case 0x01: /* FMLA */
12829             case 0x05: /* FMLS */
12830             case 0x09: /* FMUL */
12831             case 0x19: /* FMULX */
12832                 g_assert_not_reached();
12833             }
12834 
12835             if (is_scalar) {
12836                 write_fp_sreg(s, rd, tcg_res);
12837             } else {
12838                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12839             }
12840         }
12841 
12842         clear_vec_high(s, is_q, rd);
12843     } else {
12844         /* long ops: 16x16->32 or 32x32->64 */
12845         TCGv_i64 tcg_res[2];
12846         int pass;
12847         bool satop = extract32(opcode, 0, 1);
12848         MemOp memop = MO_32;
12849 
12850         if (satop || !u) {
12851             memop |= MO_SIGN;
12852         }
12853 
12854         if (size == 2) {
12855             TCGv_i64 tcg_idx = tcg_temp_new_i64();
12856 
12857             read_vec_element(s, tcg_idx, rm, index, memop);
12858 
12859             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12860                 TCGv_i64 tcg_op = tcg_temp_new_i64();
12861                 TCGv_i64 tcg_passres;
12862                 int passelt;
12863 
12864                 if (is_scalar) {
12865                     passelt = 0;
12866                 } else {
12867                     passelt = pass + (is_q * 2);
12868                 }
12869 
12870                 read_vec_element(s, tcg_op, rn, passelt, memop);
12871 
12872                 tcg_res[pass] = tcg_temp_new_i64();
12873 
12874                 if (opcode == 0xa || opcode == 0xb) {
12875                     /* Non-accumulating ops */
12876                     tcg_passres = tcg_res[pass];
12877                 } else {
12878                     tcg_passres = tcg_temp_new_i64();
12879                 }
12880 
12881                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
12882 
12883                 if (satop) {
12884                     /* saturating, doubling */
12885                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
12886                                                       tcg_passres, tcg_passres);
12887                 }
12888 
12889                 if (opcode == 0xa || opcode == 0xb) {
12890                     continue;
12891                 }
12892 
12893                 /* Accumulating op: handle accumulate step */
12894                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12895 
12896                 switch (opcode) {
12897                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12898                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12899                     break;
12900                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12901                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12902                     break;
12903                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12904                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
12905                     /* fall through */
12906                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12907                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
12908                                                       tcg_res[pass],
12909                                                       tcg_passres);
12910                     break;
12911                 default:
12912                     g_assert_not_reached();
12913                 }
12914             }
12915 
12916             clear_vec_high(s, !is_scalar, rd);
12917         } else {
12918             TCGv_i32 tcg_idx = tcg_temp_new_i32();
12919 
12920             assert(size == 1);
12921             read_vec_element_i32(s, tcg_idx, rm, index, size);
12922 
12923             if (!is_scalar) {
12924                 /* The simplest way to handle the 16x16 indexed ops is to
12925                  * duplicate the index into both halves of the 32 bit tcg_idx
12926                  * and then use the usual Neon helpers.
12927                  */
12928                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12929             }
12930 
12931             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12932                 TCGv_i32 tcg_op = tcg_temp_new_i32();
12933                 TCGv_i64 tcg_passres;
12934 
12935                 if (is_scalar) {
12936                     read_vec_element_i32(s, tcg_op, rn, pass, size);
12937                 } else {
12938                     read_vec_element_i32(s, tcg_op, rn,
12939                                          pass + (is_q * 2), MO_32);
12940                 }
12941 
12942                 tcg_res[pass] = tcg_temp_new_i64();
12943 
12944                 if (opcode == 0xa || opcode == 0xb) {
12945                     /* Non-accumulating ops */
12946                     tcg_passres = tcg_res[pass];
12947                 } else {
12948                     tcg_passres = tcg_temp_new_i64();
12949                 }
12950 
12951                 if (memop & MO_SIGN) {
12952                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
12953                 } else {
12954                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
12955                 }
12956                 if (satop) {
12957                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
12958                                                       tcg_passres, tcg_passres);
12959                 }
12960 
12961                 if (opcode == 0xa || opcode == 0xb) {
12962                     continue;
12963                 }
12964 
12965                 /* Accumulating op: handle accumulate step */
12966                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12967 
12968                 switch (opcode) {
12969                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12970                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
12971                                              tcg_passres);
12972                     break;
12973                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12974                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
12975                                              tcg_passres);
12976                     break;
12977                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12978                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
12979                     /* fall through */
12980                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12981                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
12982                                                       tcg_res[pass],
12983                                                       tcg_passres);
12984                     break;
12985                 default:
12986                     g_assert_not_reached();
12987                 }
12988             }
12989 
12990             if (is_scalar) {
12991                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
12992             }
12993         }
12994 
12995         if (is_scalar) {
12996             tcg_res[1] = tcg_constant_i64(0);
12997         }
12998 
12999         for (pass = 0; pass < 2; pass++) {
13000             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13001         }
13002     }
13003 }
13004 
13005 /* C3.6 Data processing - SIMD, inc Crypto
13006  *
13007  * As the decode gets a little complex we are using a table based
13008  * approach for this part of the decode.
13009  */
13010 static const AArch64DecodeTable data_proc_simd[] = {
13011     /* pattern  ,  mask     ,  fn                        */
13012     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13013     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13014     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13015     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13016     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13017     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13018     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13019     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13020     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13021     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13022     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13023     { 0x2e000000, 0xbf208400, disas_simd_ext },
13024     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13025     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13026     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13027     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13028     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13029     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13030     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13031     { 0x00000000, 0x00000000, NULL }
13032 };
13033 
13034 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13035 {
13036     /* Note that this is called with all non-FP cases from
13037      * table C3-6 so it must UNDEF for entries not specifically
13038      * allocated to instructions in that table.
13039      */
13040     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13041     if (fn) {
13042         fn(s, insn);
13043     } else {
13044         unallocated_encoding(s);
13045     }
13046 }
13047 
13048 /* C3.6 Data processing - SIMD and floating point */
13049 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13050 {
13051     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13052         disas_data_proc_fp(s, insn);
13053     } else {
13054         /* SIMD, including crypto */
13055         disas_data_proc_simd(s, insn);
13056     }
13057 }
13058 
13059 static bool trans_OK(DisasContext *s, arg_OK *a)
13060 {
13061     return true;
13062 }
13063 
13064 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13065 {
13066     s->is_nonstreaming = true;
13067     return true;
13068 }
13069 
13070 /**
13071  * is_guarded_page:
13072  * @env: The cpu environment
13073  * @s: The DisasContext
13074  *
13075  * Return true if the page is guarded.
13076  */
13077 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13078 {
13079     uint64_t addr = s->base.pc_first;
13080 #ifdef CONFIG_USER_ONLY
13081     return page_get_flags(addr) & PAGE_BTI;
13082 #else
13083     CPUTLBEntryFull *full;
13084     void *host;
13085     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13086     int flags;
13087 
13088     /*
13089      * We test this immediately after reading an insn, which means
13090      * that the TLB entry must be present and valid, and thus this
13091      * access will never raise an exception.
13092      */
13093     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13094                               false, &host, &full, 0);
13095     assert(!(flags & TLB_INVALID_MASK));
13096 
13097     return full->extra.arm.guarded;
13098 #endif
13099 }
13100 
13101 /**
13102  * btype_destination_ok:
13103  * @insn: The instruction at the branch destination
13104  * @bt: SCTLR_ELx.BT
13105  * @btype: PSTATE.BTYPE, and is non-zero
13106  *
13107  * On a guarded page, there are a limited number of insns
13108  * that may be present at the branch target:
13109  *   - branch target identifiers,
13110  *   - paciasp, pacibsp,
13111  *   - BRK insn
13112  *   - HLT insn
13113  * Anything else causes a Branch Target Exception.
13114  *
13115  * Return true if the branch is compatible, false to raise BTITRAP.
13116  */
13117 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13118 {
13119     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13120         /* HINT space */
13121         switch (extract32(insn, 5, 7)) {
13122         case 0b011001: /* PACIASP */
13123         case 0b011011: /* PACIBSP */
13124             /*
13125              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13126              * with btype == 3.  Otherwise all btype are ok.
13127              */
13128             return !bt || btype != 3;
13129         case 0b100000: /* BTI */
13130             /* Not compatible with any btype.  */
13131             return false;
13132         case 0b100010: /* BTI c */
13133             /* Not compatible with btype == 3 */
13134             return btype != 3;
13135         case 0b100100: /* BTI j */
13136             /* Not compatible with btype == 2 */
13137             return btype != 2;
13138         case 0b100110: /* BTI jc */
13139             /* Compatible with any btype.  */
13140             return true;
13141         }
13142     } else {
13143         switch (insn & 0xffe0001fu) {
13144         case 0xd4200000u: /* BRK */
13145         case 0xd4400000u: /* HLT */
13146             /* Give priority to the breakpoint exception.  */
13147             return true;
13148         }
13149     }
13150     return false;
13151 }
13152 
13153 /* C3.1 A64 instruction index by encoding */
13154 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13155 {
13156     switch (extract32(insn, 25, 4)) {
13157     case 0x5:
13158     case 0xd:      /* Data processing - register */
13159         disas_data_proc_reg(s, insn);
13160         break;
13161     case 0x7:
13162     case 0xf:      /* Data processing - SIMD and floating point */
13163         disas_data_proc_simd_fp(s, insn);
13164         break;
13165     default:
13166         unallocated_encoding(s);
13167         break;
13168     }
13169 }
13170 
13171 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13172                                           CPUState *cpu)
13173 {
13174     DisasContext *dc = container_of(dcbase, DisasContext, base);
13175     CPUARMState *env = cpu_env(cpu);
13176     ARMCPU *arm_cpu = env_archcpu(env);
13177     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13178     int bound, core_mmu_idx;
13179 
13180     dc->isar = &arm_cpu->isar;
13181     dc->condjmp = 0;
13182     dc->pc_save = dc->base.pc_first;
13183     dc->aarch64 = true;
13184     dc->thumb = false;
13185     dc->sctlr_b = 0;
13186     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13187     dc->condexec_mask = 0;
13188     dc->condexec_cond = 0;
13189     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
13190     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
13191     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
13192     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
13193     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
13194     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
13195 #if !defined(CONFIG_USER_ONLY)
13196     dc->user = (dc->current_el == 0);
13197 #endif
13198     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
13199     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
13200     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
13201     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
13202     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
13203     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
13204     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
13205     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
13206     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
13207     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
13208     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
13209     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
13210     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
13211     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
13212     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
13213     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
13214     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
13215     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
13216     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
13217     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
13218     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
13219     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
13220     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
13221     dc->nv1 = EX_TBFLAG_A64(tb_flags, NV1);
13222     dc->nv2 = EX_TBFLAG_A64(tb_flags, NV2);
13223     dc->nv2_mem_e20 = EX_TBFLAG_A64(tb_flags, NV2_MEM_E20);
13224     dc->nv2_mem_be = EX_TBFLAG_A64(tb_flags, NV2_MEM_BE);
13225     dc->vec_len = 0;
13226     dc->vec_stride = 0;
13227     dc->cp_regs = arm_cpu->cp_regs;
13228     dc->features = env->features;
13229     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
13230     dc->gm_blocksize = arm_cpu->gm_blocksize;
13231 
13232 #ifdef CONFIG_USER_ONLY
13233     /* In sve_probe_page, we assume TBI is enabled. */
13234     tcg_debug_assert(dc->tbid & 1);
13235 #endif
13236 
13237     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
13238 
13239     /* Single step state. The code-generation logic here is:
13240      *  SS_ACTIVE == 0:
13241      *   generate code with no special handling for single-stepping (except
13242      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
13243      *   this happens anyway because those changes are all system register or
13244      *   PSTATE writes).
13245      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
13246      *   emit code for one insn
13247      *   emit code to clear PSTATE.SS
13248      *   emit code to generate software step exception for completed step
13249      *   end TB (as usual for having generated an exception)
13250      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
13251      *   emit code to generate a software step exception
13252      *   end the TB
13253      */
13254     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
13255     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
13256     dc->is_ldex = false;
13257 
13258     /* Bound the number of insns to execute to those left on the page.  */
13259     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
13260 
13261     /* If architectural single step active, limit to 1.  */
13262     if (dc->ss_active) {
13263         bound = 1;
13264     }
13265     dc->base.max_insns = MIN(dc->base.max_insns, bound);
13266 }
13267 
13268 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
13269 {
13270 }
13271 
13272 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
13273 {
13274     DisasContext *dc = container_of(dcbase, DisasContext, base);
13275     target_ulong pc_arg = dc->base.pc_next;
13276 
13277     if (tb_cflags(dcbase->tb) & CF_PCREL) {
13278         pc_arg &= ~TARGET_PAGE_MASK;
13279     }
13280     tcg_gen_insn_start(pc_arg, 0, 0);
13281     dc->insn_start_updated = false;
13282 }
13283 
13284 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
13285 {
13286     DisasContext *s = container_of(dcbase, DisasContext, base);
13287     CPUARMState *env = cpu_env(cpu);
13288     uint64_t pc = s->base.pc_next;
13289     uint32_t insn;
13290 
13291     /* Singlestep exceptions have the highest priority. */
13292     if (s->ss_active && !s->pstate_ss) {
13293         /* Singlestep state is Active-pending.
13294          * If we're in this state at the start of a TB then either
13295          *  a) we just took an exception to an EL which is being debugged
13296          *     and this is the first insn in the exception handler
13297          *  b) debug exceptions were masked and we just unmasked them
13298          *     without changing EL (eg by clearing PSTATE.D)
13299          * In either case we're going to take a swstep exception in the
13300          * "did not step an insn" case, and so the syndrome ISV and EX
13301          * bits should be zero.
13302          */
13303         assert(s->base.num_insns == 1);
13304         gen_swstep_exception(s, 0, 0);
13305         s->base.is_jmp = DISAS_NORETURN;
13306         s->base.pc_next = pc + 4;
13307         return;
13308     }
13309 
13310     if (pc & 3) {
13311         /*
13312          * PC alignment fault.  This has priority over the instruction abort
13313          * that we would receive from a translation fault via arm_ldl_code.
13314          * This should only be possible after an indirect branch, at the
13315          * start of the TB.
13316          */
13317         assert(s->base.num_insns == 1);
13318         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
13319         s->base.is_jmp = DISAS_NORETURN;
13320         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
13321         return;
13322     }
13323 
13324     s->pc_curr = pc;
13325     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
13326     s->insn = insn;
13327     s->base.pc_next = pc + 4;
13328 
13329     s->fp_access_checked = false;
13330     s->sve_access_checked = false;
13331 
13332     if (s->pstate_il) {
13333         /*
13334          * Illegal execution state. This has priority over BTI
13335          * exceptions, but comes after instruction abort exceptions.
13336          */
13337         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
13338         return;
13339     }
13340 
13341     if (dc_isar_feature(aa64_bti, s)) {
13342         if (s->base.num_insns == 1) {
13343             /*
13344              * At the first insn of the TB, compute s->guarded_page.
13345              * We delayed computing this until successfully reading
13346              * the first insn of the TB, above.  This (mostly) ensures
13347              * that the softmmu tlb entry has been populated, and the
13348              * page table GP bit is available.
13349              *
13350              * Note that we need to compute this even if btype == 0,
13351              * because this value is used for BR instructions later
13352              * where ENV is not available.
13353              */
13354             s->guarded_page = is_guarded_page(env, s);
13355 
13356             /* First insn can have btype set to non-zero.  */
13357             tcg_debug_assert(s->btype >= 0);
13358 
13359             /*
13360              * Note that the Branch Target Exception has fairly high
13361              * priority -- below debugging exceptions but above most
13362              * everything else.  This allows us to handle this now
13363              * instead of waiting until the insn is otherwise decoded.
13364              */
13365             if (s->btype != 0
13366                 && s->guarded_page
13367                 && !btype_destination_ok(insn, s->bt, s->btype)) {
13368                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
13369                 return;
13370             }
13371         } else {
13372             /* Not the first insn: btype must be 0.  */
13373             tcg_debug_assert(s->btype == 0);
13374         }
13375     }
13376 
13377     s->is_nonstreaming = false;
13378     if (s->sme_trap_nonstreaming) {
13379         disas_sme_fa64(s, insn);
13380     }
13381 
13382     if (!disas_a64(s, insn) &&
13383         !disas_sme(s, insn) &&
13384         !disas_sve(s, insn)) {
13385         disas_a64_legacy(s, insn);
13386     }
13387 
13388     /*
13389      * After execution of most insns, btype is reset to 0.
13390      * Note that we set btype == -1 when the insn sets btype.
13391      */
13392     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
13393         reset_btype(s);
13394     }
13395 }
13396 
13397 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
13398 {
13399     DisasContext *dc = container_of(dcbase, DisasContext, base);
13400 
13401     if (unlikely(dc->ss_active)) {
13402         /* Note that this means single stepping WFI doesn't halt the CPU.
13403          * For conditional branch insns this is harmless unreachable code as
13404          * gen_goto_tb() has already handled emitting the debug exception
13405          * (and thus a tb-jump is not possible when singlestepping).
13406          */
13407         switch (dc->base.is_jmp) {
13408         default:
13409             gen_a64_update_pc(dc, 4);
13410             /* fall through */
13411         case DISAS_EXIT:
13412         case DISAS_JUMP:
13413             gen_step_complete_exception(dc);
13414             break;
13415         case DISAS_NORETURN:
13416             break;
13417         }
13418     } else {
13419         switch (dc->base.is_jmp) {
13420         case DISAS_NEXT:
13421         case DISAS_TOO_MANY:
13422             gen_goto_tb(dc, 1, 4);
13423             break;
13424         default:
13425         case DISAS_UPDATE_EXIT:
13426             gen_a64_update_pc(dc, 4);
13427             /* fall through */
13428         case DISAS_EXIT:
13429             tcg_gen_exit_tb(NULL, 0);
13430             break;
13431         case DISAS_UPDATE_NOCHAIN:
13432             gen_a64_update_pc(dc, 4);
13433             /* fall through */
13434         case DISAS_JUMP:
13435             tcg_gen_lookup_and_goto_ptr();
13436             break;
13437         case DISAS_NORETURN:
13438         case DISAS_SWI:
13439             break;
13440         case DISAS_WFE:
13441             gen_a64_update_pc(dc, 4);
13442             gen_helper_wfe(tcg_env);
13443             break;
13444         case DISAS_YIELD:
13445             gen_a64_update_pc(dc, 4);
13446             gen_helper_yield(tcg_env);
13447             break;
13448         case DISAS_WFI:
13449             /*
13450              * This is a special case because we don't want to just halt
13451              * the CPU if trying to debug across a WFI.
13452              */
13453             gen_a64_update_pc(dc, 4);
13454             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
13455             /*
13456              * The helper doesn't necessarily throw an exception, but we
13457              * must go back to the main loop to check for interrupts anyway.
13458              */
13459             tcg_gen_exit_tb(NULL, 0);
13460             break;
13461         }
13462     }
13463 }
13464 
13465 const TranslatorOps aarch64_translator_ops = {
13466     .init_disas_context = aarch64_tr_init_disas_context,
13467     .tb_start           = aarch64_tr_tb_start,
13468     .insn_start         = aarch64_tr_insn_start,
13469     .translate_insn     = aarch64_tr_translate_insn,
13470     .tb_stop            = aarch64_tr_tb_stop,
13471 };
13472