xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 44e0ddee)
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 "translate.h"
22 #include "translate-a64.h"
23 #include "qemu/log.h"
24 #include "disas/disas.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(cpu_env,
95                                     offsetof(CPUARMState, pc),
96                                     "pc");
97     for (i = 0; i < 32; i++) {
98         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
99                                           offsetof(CPUARMState, xregs[i]),
100                                           regnames[i]);
101     }
102 
103     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
104         offsetof(CPUARMState, exclusive_high), "exclusive_high");
105 }
106 
107 /*
108  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
109  */
110 static int get_a64_user_mem_index(DisasContext *s)
111 {
112     /*
113      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
114      * which is the usual mmu_idx for this cpu state.
115      */
116     ARMMMUIdx useridx = s->mmu_idx;
117 
118     if (s->unpriv) {
119         /*
120          * We have pre-computed the condition for AccType_UNPRIV.
121          * Therefore we should never get here with a mmu_idx for
122          * which we do not know the corresponding user mmu_idx.
123          */
124         switch (useridx) {
125         case ARMMMUIdx_E10_1:
126         case ARMMMUIdx_E10_1_PAN:
127             useridx = ARMMMUIdx_E10_0;
128             break;
129         case ARMMMUIdx_E20_2:
130         case ARMMMUIdx_E20_2_PAN:
131             useridx = ARMMMUIdx_E20_0;
132             break;
133         default:
134             g_assert_not_reached();
135         }
136     }
137     return arm_to_core_mmu_idx(useridx);
138 }
139 
140 static void set_btype_raw(int val)
141 {
142     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
143                    offsetof(CPUARMState, btype));
144 }
145 
146 static void set_btype(DisasContext *s, int val)
147 {
148     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
149     tcg_debug_assert(val >= 1 && val <= 3);
150     set_btype_raw(val);
151     s->btype = -1;
152 }
153 
154 static void reset_btype(DisasContext *s)
155 {
156     if (s->btype != 0) {
157         set_btype_raw(0);
158         s->btype = 0;
159     }
160 }
161 
162 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
163 {
164     assert(s->pc_save != -1);
165     if (tb_cflags(s->base.tb) & CF_PCREL) {
166         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
167     } else {
168         tcg_gen_movi_i64(dest, s->pc_curr + diff);
169     }
170 }
171 
172 void gen_a64_update_pc(DisasContext *s, target_long diff)
173 {
174     gen_pc_plus_diff(s, cpu_pc, diff);
175     s->pc_save = s->pc_curr + diff;
176 }
177 
178 /*
179  * Handle Top Byte Ignore (TBI) bits.
180  *
181  * If address tagging is enabled via the TCR TBI bits:
182  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
183  *    then the address is zero-extended, clearing bits [63:56]
184  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
185  *    and TBI1 controls addresses with bit 55 == 1.
186  *    If the appropriate TBI bit is set for the address then
187  *    the address is sign-extended from bit 55 into bits [63:56]
188  *
189  * Here We have concatenated TBI{1,0} into tbi.
190  */
191 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
192                                 TCGv_i64 src, int tbi)
193 {
194     if (tbi == 0) {
195         /* Load unmodified address */
196         tcg_gen_mov_i64(dst, src);
197     } else if (!regime_has_2_ranges(s->mmu_idx)) {
198         /* Force tag byte to all zero */
199         tcg_gen_extract_i64(dst, src, 0, 56);
200     } else {
201         /* Sign-extend from bit 55.  */
202         tcg_gen_sextract_i64(dst, src, 0, 56);
203 
204         switch (tbi) {
205         case 1:
206             /* tbi0 but !tbi1: only use the extension if positive */
207             tcg_gen_and_i64(dst, dst, src);
208             break;
209         case 2:
210             /* !tbi0 but tbi1: only use the extension if negative */
211             tcg_gen_or_i64(dst, dst, src);
212             break;
213         case 3:
214             /* tbi0 and tbi1: always use the extension */
215             break;
216         default:
217             g_assert_not_reached();
218         }
219     }
220 }
221 
222 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
223 {
224     /*
225      * If address tagging is enabled for instructions via the TCR TBI bits,
226      * then loading an address into the PC will clear out any tag.
227      */
228     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
229     s->pc_save = -1;
230 }
231 
232 /*
233  * Handle MTE and/or TBI.
234  *
235  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
236  * for the tag to be present in the FAR_ELx register.  But for user-only
237  * mode we do not have a TLB with which to implement this, so we must
238  * remove the top byte now.
239  *
240  * Always return a fresh temporary that we can increment independently
241  * of the write-back address.
242  */
243 
244 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
245 {
246     TCGv_i64 clean = tcg_temp_new_i64();
247 #ifdef CONFIG_USER_ONLY
248     gen_top_byte_ignore(s, clean, addr, s->tbid);
249 #else
250     tcg_gen_mov_i64(clean, addr);
251 #endif
252     return clean;
253 }
254 
255 /* Insert a zero tag into src, with the result at dst. */
256 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
257 {
258     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
259 }
260 
261 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
262                              MMUAccessType acc, int log2_size)
263 {
264     gen_helper_probe_access(cpu_env, ptr,
265                             tcg_constant_i32(acc),
266                             tcg_constant_i32(get_mem_index(s)),
267                             tcg_constant_i32(1 << log2_size));
268 }
269 
270 /*
271  * For MTE, check a single logical or atomic access.  This probes a single
272  * address, the exact one specified.  The size and alignment of the access
273  * is not relevant to MTE, per se, but watchpoints do require the size,
274  * and we want to recognize those before making any other changes to state.
275  */
276 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
277                                       bool is_write, bool tag_checked,
278                                       MemOp memop, bool is_unpriv,
279                                       int core_idx)
280 {
281     if (tag_checked && s->mte_active[is_unpriv]) {
282         TCGv_i64 ret;
283         int desc = 0;
284 
285         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
286         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
287         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
288         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
289         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
290         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
291 
292         ret = tcg_temp_new_i64();
293         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
294 
295         return ret;
296     }
297     return clean_data_tbi(s, addr);
298 }
299 
300 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
301                         bool tag_checked, MemOp memop)
302 {
303     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
304                                  false, get_mem_index(s));
305 }
306 
307 /*
308  * For MTE, check multiple logical sequential accesses.
309  */
310 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
311                         bool tag_checked, int total_size, MemOp single_mop)
312 {
313     if (tag_checked && s->mte_active[0]) {
314         TCGv_i64 ret;
315         int desc = 0;
316 
317         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
318         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
319         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
320         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
321         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
322         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
323 
324         ret = tcg_temp_new_i64();
325         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
326 
327         return ret;
328     }
329     return clean_data_tbi(s, addr);
330 }
331 
332 /*
333  * Generate the special alignment check that applies to AccType_ATOMIC
334  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
335  * naturally aligned, but it must not cross a 16-byte boundary.
336  * See AArch64.CheckAlignment().
337  */
338 static void check_lse2_align(DisasContext *s, int rn, int imm,
339                              bool is_write, MemOp mop)
340 {
341     TCGv_i32 tmp;
342     TCGv_i64 addr;
343     TCGLabel *over_label;
344     MMUAccessType type;
345     int mmu_idx;
346 
347     tmp = tcg_temp_new_i32();
348     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
349     tcg_gen_addi_i32(tmp, tmp, imm & 15);
350     tcg_gen_andi_i32(tmp, tmp, 15);
351     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
352 
353     over_label = gen_new_label();
354     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
355 
356     addr = tcg_temp_new_i64();
357     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
358 
359     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
360     mmu_idx = get_mem_index(s);
361     gen_helper_unaligned_access(cpu_env, addr, tcg_constant_i32(type),
362                                 tcg_constant_i32(mmu_idx));
363 
364     gen_set_label(over_label);
365 
366 }
367 
368 /* Handle the alignment check for AccType_ATOMIC instructions. */
369 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
370 {
371     MemOp size = mop & MO_SIZE;
372 
373     if (size == MO_8) {
374         return mop;
375     }
376 
377     /*
378      * If size == MO_128, this is a LDXP, and the operation is single-copy
379      * atomic for each doubleword, not the entire quadword; it still must
380      * be quadword aligned.
381      */
382     if (size == MO_128) {
383         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
384                                    MO_ATOM_IFALIGN_PAIR);
385     }
386     if (dc_isar_feature(aa64_lse2, s)) {
387         check_lse2_align(s, rn, 0, true, mop);
388     } else {
389         mop |= MO_ALIGN;
390     }
391     return finalize_memop(s, mop);
392 }
393 
394 /* Handle the alignment check for AccType_ORDERED instructions. */
395 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
396                                  bool is_write, MemOp mop)
397 {
398     MemOp size = mop & MO_SIZE;
399 
400     if (size == MO_8) {
401         return mop;
402     }
403     if (size == MO_128) {
404         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
405                                    MO_ATOM_IFALIGN_PAIR);
406     }
407     if (!dc_isar_feature(aa64_lse2, s)) {
408         mop |= MO_ALIGN;
409     } else if (!s->naa) {
410         check_lse2_align(s, rn, imm, is_write, mop);
411     }
412     return finalize_memop(s, mop);
413 }
414 
415 typedef struct DisasCompare64 {
416     TCGCond cond;
417     TCGv_i64 value;
418 } DisasCompare64;
419 
420 static void a64_test_cc(DisasCompare64 *c64, int cc)
421 {
422     DisasCompare c32;
423 
424     arm_test_cc(&c32, cc);
425 
426     /*
427      * Sign-extend the 32-bit value so that the GE/LT comparisons work
428      * properly.  The NE/EQ comparisons are also fine with this choice.
429       */
430     c64->cond = c32.cond;
431     c64->value = tcg_temp_new_i64();
432     tcg_gen_ext_i32_i64(c64->value, c32.value);
433 }
434 
435 static void gen_rebuild_hflags(DisasContext *s)
436 {
437     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
438 }
439 
440 static void gen_exception_internal(int excp)
441 {
442     assert(excp_is_internal(excp));
443     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
444 }
445 
446 static void gen_exception_internal_insn(DisasContext *s, int excp)
447 {
448     gen_a64_update_pc(s, 0);
449     gen_exception_internal(excp);
450     s->base.is_jmp = DISAS_NORETURN;
451 }
452 
453 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
454 {
455     gen_a64_update_pc(s, 0);
456     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
457     s->base.is_jmp = DISAS_NORETURN;
458 }
459 
460 static void gen_step_complete_exception(DisasContext *s)
461 {
462     /* We just completed step of an insn. Move from Active-not-pending
463      * to Active-pending, and then also take the swstep exception.
464      * This corresponds to making the (IMPDEF) choice to prioritize
465      * swstep exceptions over asynchronous exceptions taken to an exception
466      * level where debug is disabled. This choice has the advantage that
467      * we do not need to maintain internal state corresponding to the
468      * ISV/EX syndrome bits between completion of the step and generation
469      * of the exception, and our syndrome information is always correct.
470      */
471     gen_ss_advance(s);
472     gen_swstep_exception(s, 1, s->is_ldex);
473     s->base.is_jmp = DISAS_NORETURN;
474 }
475 
476 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
477 {
478     if (s->ss_active) {
479         return false;
480     }
481     return translator_use_goto_tb(&s->base, dest);
482 }
483 
484 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
485 {
486     if (use_goto_tb(s, s->pc_curr + diff)) {
487         /*
488          * For pcrel, the pc must always be up-to-date on entry to
489          * the linked TB, so that it can use simple additions for all
490          * further adjustments.  For !pcrel, the linked TB is compiled
491          * to know its full virtual address, so we can delay the
492          * update to pc to the unlinked path.  A long chain of links
493          * can thus avoid many updates to the PC.
494          */
495         if (tb_cflags(s->base.tb) & CF_PCREL) {
496             gen_a64_update_pc(s, diff);
497             tcg_gen_goto_tb(n);
498         } else {
499             tcg_gen_goto_tb(n);
500             gen_a64_update_pc(s, diff);
501         }
502         tcg_gen_exit_tb(s->base.tb, n);
503         s->base.is_jmp = DISAS_NORETURN;
504     } else {
505         gen_a64_update_pc(s, diff);
506         if (s->ss_active) {
507             gen_step_complete_exception(s);
508         } else {
509             tcg_gen_lookup_and_goto_ptr();
510             s->base.is_jmp = DISAS_NORETURN;
511         }
512     }
513 }
514 
515 /*
516  * Register access functions
517  *
518  * These functions are used for directly accessing a register in where
519  * changes to the final register value are likely to be made. If you
520  * need to use a register for temporary calculation (e.g. index type
521  * operations) use the read_* form.
522  *
523  * B1.2.1 Register mappings
524  *
525  * In instruction register encoding 31 can refer to ZR (zero register) or
526  * the SP (stack pointer) depending on context. In QEMU's case we map SP
527  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
528  * This is the point of the _sp forms.
529  */
530 TCGv_i64 cpu_reg(DisasContext *s, int reg)
531 {
532     if (reg == 31) {
533         TCGv_i64 t = tcg_temp_new_i64();
534         tcg_gen_movi_i64(t, 0);
535         return t;
536     } else {
537         return cpu_X[reg];
538     }
539 }
540 
541 /* register access for when 31 == SP */
542 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
543 {
544     return cpu_X[reg];
545 }
546 
547 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
548  * representing the register contents. This TCGv is an auto-freed
549  * temporary so it need not be explicitly freed, and may be modified.
550  */
551 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
552 {
553     TCGv_i64 v = tcg_temp_new_i64();
554     if (reg != 31) {
555         if (sf) {
556             tcg_gen_mov_i64(v, cpu_X[reg]);
557         } else {
558             tcg_gen_ext32u_i64(v, cpu_X[reg]);
559         }
560     } else {
561         tcg_gen_movi_i64(v, 0);
562     }
563     return v;
564 }
565 
566 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
567 {
568     TCGv_i64 v = tcg_temp_new_i64();
569     if (sf) {
570         tcg_gen_mov_i64(v, cpu_X[reg]);
571     } else {
572         tcg_gen_ext32u_i64(v, cpu_X[reg]);
573     }
574     return v;
575 }
576 
577 /* Return the offset into CPUARMState of a slice (from
578  * the least significant end) of FP register Qn (ie
579  * Dn, Sn, Hn or Bn).
580  * (Note that this is not the same mapping as for A32; see cpu.h)
581  */
582 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
583 {
584     return vec_reg_offset(s, regno, 0, size);
585 }
586 
587 /* Offset of the high half of the 128 bit vector Qn */
588 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
589 {
590     return vec_reg_offset(s, regno, 1, MO_64);
591 }
592 
593 /* Convenience accessors for reading and writing single and double
594  * FP registers. Writing clears the upper parts of the associated
595  * 128 bit vector register, as required by the architecture.
596  * Note that unlike the GP register accessors, the values returned
597  * by the read functions must be manually freed.
598  */
599 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
600 {
601     TCGv_i64 v = tcg_temp_new_i64();
602 
603     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
604     return v;
605 }
606 
607 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
608 {
609     TCGv_i32 v = tcg_temp_new_i32();
610 
611     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
612     return v;
613 }
614 
615 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
616 {
617     TCGv_i32 v = tcg_temp_new_i32();
618 
619     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
620     return v;
621 }
622 
623 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
624  * If SVE is not enabled, then there are only 128 bits in the vector.
625  */
626 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
627 {
628     unsigned ofs = fp_reg_offset(s, rd, MO_64);
629     unsigned vsz = vec_full_reg_size(s);
630 
631     /* Nop move, with side effect of clearing the tail. */
632     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
633 }
634 
635 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
636 {
637     unsigned ofs = fp_reg_offset(s, reg, MO_64);
638 
639     tcg_gen_st_i64(v, cpu_env, ofs);
640     clear_vec_high(s, false, reg);
641 }
642 
643 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
644 {
645     TCGv_i64 tmp = tcg_temp_new_i64();
646 
647     tcg_gen_extu_i32_i64(tmp, v);
648     write_fp_dreg(s, reg, tmp);
649 }
650 
651 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
652 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
653                          GVecGen2Fn *gvec_fn, int vece)
654 {
655     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
656             is_q ? 16 : 8, vec_full_reg_size(s));
657 }
658 
659 /* Expand a 2-operand + immediate AdvSIMD vector operation using
660  * an expander function.
661  */
662 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
663                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
664 {
665     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
666             imm, is_q ? 16 : 8, vec_full_reg_size(s));
667 }
668 
669 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
670 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
671                          GVecGen3Fn *gvec_fn, int vece)
672 {
673     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
675 }
676 
677 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
678 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
679                          int rx, GVecGen4Fn *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), vec_full_reg_offset(s, rx),
683             is_q ? 16 : 8, vec_full_reg_size(s));
684 }
685 
686 /* Expand a 2-operand operation using an out-of-line helper.  */
687 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
688                              int rn, int data, gen_helper_gvec_2 *fn)
689 {
690     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
691                        vec_full_reg_offset(s, rn),
692                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
693 }
694 
695 /* Expand a 3-operand operation using an out-of-line helper.  */
696 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
697                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
698 {
699     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
700                        vec_full_reg_offset(s, rn),
701                        vec_full_reg_offset(s, rm),
702                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
703 }
704 
705 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
706  * an out-of-line helper.
707  */
708 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
709                               int rm, bool is_fp16, int data,
710                               gen_helper_gvec_3_ptr *fn)
711 {
712     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
713     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
714                        vec_full_reg_offset(s, rn),
715                        vec_full_reg_offset(s, rm), fpst,
716                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
717 }
718 
719 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
720 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
721                             int rm, gen_helper_gvec_3_ptr *fn)
722 {
723     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
724 
725     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
726     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
727                        vec_full_reg_offset(s, rn),
728                        vec_full_reg_offset(s, rm), qc_ptr,
729                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
730 }
731 
732 /* Expand a 4-operand operation using an out-of-line helper.  */
733 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
734                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
735 {
736     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
737                        vec_full_reg_offset(s, rn),
738                        vec_full_reg_offset(s, rm),
739                        vec_full_reg_offset(s, ra),
740                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
741 }
742 
743 /*
744  * Expand a 4-operand + fpstatus pointer + simd data value operation using
745  * an out-of-line helper.
746  */
747 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
748                               int rm, int ra, bool is_fp16, int data,
749                               gen_helper_gvec_4_ptr *fn)
750 {
751     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
752     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
753                        vec_full_reg_offset(s, rn),
754                        vec_full_reg_offset(s, rm),
755                        vec_full_reg_offset(s, ra), fpst,
756                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
757 }
758 
759 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
760  * than the 32 bit equivalent.
761  */
762 static inline void gen_set_NZ64(TCGv_i64 result)
763 {
764     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
765     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
766 }
767 
768 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
769 static inline void gen_logic_CC(int sf, TCGv_i64 result)
770 {
771     if (sf) {
772         gen_set_NZ64(result);
773     } else {
774         tcg_gen_extrl_i64_i32(cpu_ZF, result);
775         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
776     }
777     tcg_gen_movi_i32(cpu_CF, 0);
778     tcg_gen_movi_i32(cpu_VF, 0);
779 }
780 
781 /* dest = T0 + T1; compute C, N, V and Z flags */
782 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
783 {
784     TCGv_i64 result, flag, tmp;
785     result = tcg_temp_new_i64();
786     flag = tcg_temp_new_i64();
787     tmp = tcg_temp_new_i64();
788 
789     tcg_gen_movi_i64(tmp, 0);
790     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
791 
792     tcg_gen_extrl_i64_i32(cpu_CF, flag);
793 
794     gen_set_NZ64(result);
795 
796     tcg_gen_xor_i64(flag, result, t0);
797     tcg_gen_xor_i64(tmp, t0, t1);
798     tcg_gen_andc_i64(flag, flag, tmp);
799     tcg_gen_extrh_i64_i32(cpu_VF, flag);
800 
801     tcg_gen_mov_i64(dest, result);
802 }
803 
804 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
805 {
806     TCGv_i32 t0_32 = tcg_temp_new_i32();
807     TCGv_i32 t1_32 = tcg_temp_new_i32();
808     TCGv_i32 tmp = tcg_temp_new_i32();
809 
810     tcg_gen_movi_i32(tmp, 0);
811     tcg_gen_extrl_i64_i32(t0_32, t0);
812     tcg_gen_extrl_i64_i32(t1_32, t1);
813     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
814     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
815     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
816     tcg_gen_xor_i32(tmp, t0_32, t1_32);
817     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
818     tcg_gen_extu_i32_i64(dest, cpu_NF);
819 }
820 
821 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
822 {
823     if (sf) {
824         gen_add64_CC(dest, t0, t1);
825     } else {
826         gen_add32_CC(dest, t0, t1);
827     }
828 }
829 
830 /* dest = T0 - T1; compute C, N, V and Z flags */
831 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
832 {
833     /* 64 bit arithmetic */
834     TCGv_i64 result, flag, tmp;
835 
836     result = tcg_temp_new_i64();
837     flag = tcg_temp_new_i64();
838     tcg_gen_sub_i64(result, t0, t1);
839 
840     gen_set_NZ64(result);
841 
842     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
843     tcg_gen_extrl_i64_i32(cpu_CF, flag);
844 
845     tcg_gen_xor_i64(flag, result, t0);
846     tmp = tcg_temp_new_i64();
847     tcg_gen_xor_i64(tmp, t0, t1);
848     tcg_gen_and_i64(flag, flag, tmp);
849     tcg_gen_extrh_i64_i32(cpu_VF, flag);
850     tcg_gen_mov_i64(dest, result);
851 }
852 
853 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
854 {
855     /* 32 bit arithmetic */
856     TCGv_i32 t0_32 = tcg_temp_new_i32();
857     TCGv_i32 t1_32 = tcg_temp_new_i32();
858     TCGv_i32 tmp;
859 
860     tcg_gen_extrl_i64_i32(t0_32, t0);
861     tcg_gen_extrl_i64_i32(t1_32, t1);
862     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
863     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
864     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
865     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
866     tmp = tcg_temp_new_i32();
867     tcg_gen_xor_i32(tmp, t0_32, t1_32);
868     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
869     tcg_gen_extu_i32_i64(dest, cpu_NF);
870 }
871 
872 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
873 {
874     if (sf) {
875         gen_sub64_CC(dest, t0, t1);
876     } else {
877         gen_sub32_CC(dest, t0, t1);
878     }
879 }
880 
881 /* dest = T0 + T1 + CF; do not compute flags. */
882 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
883 {
884     TCGv_i64 flag = tcg_temp_new_i64();
885     tcg_gen_extu_i32_i64(flag, cpu_CF);
886     tcg_gen_add_i64(dest, t0, t1);
887     tcg_gen_add_i64(dest, dest, flag);
888 
889     if (!sf) {
890         tcg_gen_ext32u_i64(dest, dest);
891     }
892 }
893 
894 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
895 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
896 {
897     if (sf) {
898         TCGv_i64 result = tcg_temp_new_i64();
899         TCGv_i64 cf_64 = tcg_temp_new_i64();
900         TCGv_i64 vf_64 = tcg_temp_new_i64();
901         TCGv_i64 tmp = tcg_temp_new_i64();
902         TCGv_i64 zero = tcg_constant_i64(0);
903 
904         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
905         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
906         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
907         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
908         gen_set_NZ64(result);
909 
910         tcg_gen_xor_i64(vf_64, result, t0);
911         tcg_gen_xor_i64(tmp, t0, t1);
912         tcg_gen_andc_i64(vf_64, vf_64, tmp);
913         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
914 
915         tcg_gen_mov_i64(dest, result);
916     } else {
917         TCGv_i32 t0_32 = tcg_temp_new_i32();
918         TCGv_i32 t1_32 = tcg_temp_new_i32();
919         TCGv_i32 tmp = tcg_temp_new_i32();
920         TCGv_i32 zero = tcg_constant_i32(0);
921 
922         tcg_gen_extrl_i64_i32(t0_32, t0);
923         tcg_gen_extrl_i64_i32(t1_32, t1);
924         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
925         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
926 
927         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
928         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
929         tcg_gen_xor_i32(tmp, t0_32, t1_32);
930         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
931         tcg_gen_extu_i32_i64(dest, cpu_NF);
932     }
933 }
934 
935 /*
936  * Load/Store generators
937  */
938 
939 /*
940  * Store from GPR register to memory.
941  */
942 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
943                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
944                              bool iss_valid,
945                              unsigned int iss_srt,
946                              bool iss_sf, bool iss_ar)
947 {
948     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
949 
950     if (iss_valid) {
951         uint32_t syn;
952 
953         syn = syn_data_abort_with_iss(0,
954                                       (memop & MO_SIZE),
955                                       false,
956                                       iss_srt,
957                                       iss_sf,
958                                       iss_ar,
959                                       0, 0, 0, 0, 0, false);
960         disas_set_insn_syndrome(s, syn);
961     }
962 }
963 
964 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
965                       TCGv_i64 tcg_addr, MemOp memop,
966                       bool iss_valid,
967                       unsigned int iss_srt,
968                       bool iss_sf, bool iss_ar)
969 {
970     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
971                      iss_valid, iss_srt, iss_sf, iss_ar);
972 }
973 
974 /*
975  * Load from memory to GPR register
976  */
977 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
978                              MemOp memop, bool extend, int memidx,
979                              bool iss_valid, unsigned int iss_srt,
980                              bool iss_sf, bool iss_ar)
981 {
982     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
983 
984     if (extend && (memop & MO_SIGN)) {
985         g_assert((memop & MO_SIZE) <= MO_32);
986         tcg_gen_ext32u_i64(dest, dest);
987     }
988 
989     if (iss_valid) {
990         uint32_t syn;
991 
992         syn = syn_data_abort_with_iss(0,
993                                       (memop & MO_SIZE),
994                                       (memop & MO_SIGN) != 0,
995                                       iss_srt,
996                                       iss_sf,
997                                       iss_ar,
998                                       0, 0, 0, 0, 0, false);
999         disas_set_insn_syndrome(s, syn);
1000     }
1001 }
1002 
1003 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1004                       MemOp memop, bool extend,
1005                       bool iss_valid, unsigned int iss_srt,
1006                       bool iss_sf, bool iss_ar)
1007 {
1008     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1009                      iss_valid, iss_srt, iss_sf, iss_ar);
1010 }
1011 
1012 /*
1013  * Store from FP register to memory
1014  */
1015 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1016 {
1017     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1018     TCGv_i64 tmplo = tcg_temp_new_i64();
1019 
1020     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
1021 
1022     if ((mop & MO_SIZE) < MO_128) {
1023         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1024     } else {
1025         TCGv_i64 tmphi = tcg_temp_new_i64();
1026         TCGv_i128 t16 = tcg_temp_new_i128();
1027 
1028         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
1029         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1030 
1031         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1032     }
1033 }
1034 
1035 /*
1036  * Load from memory to FP register
1037  */
1038 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1039 {
1040     /* This always zero-extends and writes to a full 128 bit wide vector */
1041     TCGv_i64 tmplo = tcg_temp_new_i64();
1042     TCGv_i64 tmphi = NULL;
1043 
1044     if ((mop & MO_SIZE) < MO_128) {
1045         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1046     } else {
1047         TCGv_i128 t16 = tcg_temp_new_i128();
1048 
1049         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1050 
1051         tmphi = tcg_temp_new_i64();
1052         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1053     }
1054 
1055     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1056 
1057     if (tmphi) {
1058         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1059     }
1060     clear_vec_high(s, tmphi != NULL, destidx);
1061 }
1062 
1063 /*
1064  * Vector load/store helpers.
1065  *
1066  * The principal difference between this and a FP load is that we don't
1067  * zero extend as we are filling a partial chunk of the vector register.
1068  * These functions don't support 128 bit loads/stores, which would be
1069  * normal load/store operations.
1070  *
1071  * The _i32 versions are useful when operating on 32 bit quantities
1072  * (eg for floating point single or using Neon helper functions).
1073  */
1074 
1075 /* Get value of an element within a vector register */
1076 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1077                              int element, MemOp memop)
1078 {
1079     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1080     switch ((unsigned)memop) {
1081     case MO_8:
1082         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1083         break;
1084     case MO_16:
1085         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1086         break;
1087     case MO_32:
1088         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1089         break;
1090     case MO_8|MO_SIGN:
1091         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1092         break;
1093     case MO_16|MO_SIGN:
1094         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1095         break;
1096     case MO_32|MO_SIGN:
1097         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1098         break;
1099     case MO_64:
1100     case MO_64|MO_SIGN:
1101         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1102         break;
1103     default:
1104         g_assert_not_reached();
1105     }
1106 }
1107 
1108 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1109                                  int element, MemOp memop)
1110 {
1111     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1112     switch (memop) {
1113     case MO_8:
1114         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1115         break;
1116     case MO_16:
1117         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1118         break;
1119     case MO_8|MO_SIGN:
1120         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1121         break;
1122     case MO_16|MO_SIGN:
1123         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1124         break;
1125     case MO_32:
1126     case MO_32|MO_SIGN:
1127         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1128         break;
1129     default:
1130         g_assert_not_reached();
1131     }
1132 }
1133 
1134 /* Set value of an element within a vector register */
1135 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1136                               int element, MemOp memop)
1137 {
1138     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1139     switch (memop) {
1140     case MO_8:
1141         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1142         break;
1143     case MO_16:
1144         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1145         break;
1146     case MO_32:
1147         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1148         break;
1149     case MO_64:
1150         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1151         break;
1152     default:
1153         g_assert_not_reached();
1154     }
1155 }
1156 
1157 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1158                                   int destidx, int element, MemOp memop)
1159 {
1160     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1161     switch (memop) {
1162     case MO_8:
1163         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1164         break;
1165     case MO_16:
1166         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1167         break;
1168     case MO_32:
1169         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1170         break;
1171     default:
1172         g_assert_not_reached();
1173     }
1174 }
1175 
1176 /* Store from vector register to memory */
1177 static void do_vec_st(DisasContext *s, int srcidx, int element,
1178                       TCGv_i64 tcg_addr, MemOp mop)
1179 {
1180     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1181 
1182     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1183     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1184 }
1185 
1186 /* Load from memory to vector register */
1187 static void do_vec_ld(DisasContext *s, int destidx, int element,
1188                       TCGv_i64 tcg_addr, MemOp mop)
1189 {
1190     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1191 
1192     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1193     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1194 }
1195 
1196 /* Check that FP/Neon access is enabled. If it is, return
1197  * true. If not, emit code to generate an appropriate exception,
1198  * and return false; the caller should not emit any code for
1199  * the instruction. Note that this check must happen after all
1200  * unallocated-encoding checks (otherwise the syndrome information
1201  * for the resulting exception will be incorrect).
1202  */
1203 static bool fp_access_check_only(DisasContext *s)
1204 {
1205     if (s->fp_excp_el) {
1206         assert(!s->fp_access_checked);
1207         s->fp_access_checked = true;
1208 
1209         gen_exception_insn_el(s, 0, EXCP_UDEF,
1210                               syn_fp_access_trap(1, 0xe, false, 0),
1211                               s->fp_excp_el);
1212         return false;
1213     }
1214     s->fp_access_checked = true;
1215     return true;
1216 }
1217 
1218 static bool fp_access_check(DisasContext *s)
1219 {
1220     if (!fp_access_check_only(s)) {
1221         return false;
1222     }
1223     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1224         gen_exception_insn(s, 0, EXCP_UDEF,
1225                            syn_smetrap(SME_ET_Streaming, false));
1226         return false;
1227     }
1228     return true;
1229 }
1230 
1231 /*
1232  * Check that SVE access is enabled.  If it is, return true.
1233  * If not, emit code to generate an appropriate exception and return false.
1234  * This function corresponds to CheckSVEEnabled().
1235  */
1236 bool sve_access_check(DisasContext *s)
1237 {
1238     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1239         assert(dc_isar_feature(aa64_sme, s));
1240         if (!sme_sm_enabled_check(s)) {
1241             goto fail_exit;
1242         }
1243     } else if (s->sve_excp_el) {
1244         gen_exception_insn_el(s, 0, EXCP_UDEF,
1245                               syn_sve_access_trap(), s->sve_excp_el);
1246         goto fail_exit;
1247     }
1248     s->sve_access_checked = true;
1249     return fp_access_check(s);
1250 
1251  fail_exit:
1252     /* Assert that we only raise one exception per instruction. */
1253     assert(!s->sve_access_checked);
1254     s->sve_access_checked = true;
1255     return false;
1256 }
1257 
1258 /*
1259  * Check that SME access is enabled, raise an exception if not.
1260  * Note that this function corresponds to CheckSMEAccess and is
1261  * only used directly for cpregs.
1262  */
1263 static bool sme_access_check(DisasContext *s)
1264 {
1265     if (s->sme_excp_el) {
1266         gen_exception_insn_el(s, 0, EXCP_UDEF,
1267                               syn_smetrap(SME_ET_AccessTrap, false),
1268                               s->sme_excp_el);
1269         return false;
1270     }
1271     return true;
1272 }
1273 
1274 /* This function corresponds to CheckSMEEnabled. */
1275 bool sme_enabled_check(DisasContext *s)
1276 {
1277     /*
1278      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1279      * to be zero when fp_excp_el has priority.  This is because we need
1280      * sme_excp_el by itself for cpregs access checks.
1281      */
1282     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1283         s->fp_access_checked = true;
1284         return sme_access_check(s);
1285     }
1286     return fp_access_check_only(s);
1287 }
1288 
1289 /* Common subroutine for CheckSMEAnd*Enabled. */
1290 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1291 {
1292     if (!sme_enabled_check(s)) {
1293         return false;
1294     }
1295     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1296         gen_exception_insn(s, 0, EXCP_UDEF,
1297                            syn_smetrap(SME_ET_NotStreaming, false));
1298         return false;
1299     }
1300     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1301         gen_exception_insn(s, 0, EXCP_UDEF,
1302                            syn_smetrap(SME_ET_InactiveZA, false));
1303         return false;
1304     }
1305     return true;
1306 }
1307 
1308 /*
1309  * This utility function is for doing register extension with an
1310  * optional shift. You will likely want to pass a temporary for the
1311  * destination register. See DecodeRegExtend() in the ARM ARM.
1312  */
1313 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1314                               int option, unsigned int shift)
1315 {
1316     int extsize = extract32(option, 0, 2);
1317     bool is_signed = extract32(option, 2, 1);
1318 
1319     if (is_signed) {
1320         switch (extsize) {
1321         case 0:
1322             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1323             break;
1324         case 1:
1325             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1326             break;
1327         case 2:
1328             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1329             break;
1330         case 3:
1331             tcg_gen_mov_i64(tcg_out, tcg_in);
1332             break;
1333         }
1334     } else {
1335         switch (extsize) {
1336         case 0:
1337             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1338             break;
1339         case 1:
1340             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1341             break;
1342         case 2:
1343             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1344             break;
1345         case 3:
1346             tcg_gen_mov_i64(tcg_out, tcg_in);
1347             break;
1348         }
1349     }
1350 
1351     if (shift) {
1352         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1353     }
1354 }
1355 
1356 static inline void gen_check_sp_alignment(DisasContext *s)
1357 {
1358     /* The AArch64 architecture mandates that (if enabled via PSTATE
1359      * or SCTLR bits) there is a check that SP is 16-aligned on every
1360      * SP-relative load or store (with an exception generated if it is not).
1361      * In line with general QEMU practice regarding misaligned accesses,
1362      * we omit these checks for the sake of guest program performance.
1363      * This function is provided as a hook so we can more easily add these
1364      * checks in future (possibly as a "favour catching guest program bugs
1365      * over speed" user selectable option).
1366      */
1367 }
1368 
1369 /*
1370  * This provides a simple table based table lookup decoder. It is
1371  * intended to be used when the relevant bits for decode are too
1372  * awkwardly placed and switch/if based logic would be confusing and
1373  * deeply nested. Since it's a linear search through the table, tables
1374  * should be kept small.
1375  *
1376  * It returns the first handler where insn & mask == pattern, or
1377  * NULL if there is no match.
1378  * The table is terminated by an empty mask (i.e. 0)
1379  */
1380 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1381                                                uint32_t insn)
1382 {
1383     const AArch64DecodeTable *tptr = table;
1384 
1385     while (tptr->mask) {
1386         if ((insn & tptr->mask) == tptr->pattern) {
1387             return tptr->disas_fn;
1388         }
1389         tptr++;
1390     }
1391     return NULL;
1392 }
1393 
1394 /*
1395  * The instruction disassembly implemented here matches
1396  * the instruction encoding classifications in chapter C4
1397  * of the ARM Architecture Reference Manual (DDI0487B_a);
1398  * classification names and decode diagrams here should generally
1399  * match up with those in the manual.
1400  */
1401 
1402 static bool trans_B(DisasContext *s, arg_i *a)
1403 {
1404     reset_btype(s);
1405     gen_goto_tb(s, 0, a->imm);
1406     return true;
1407 }
1408 
1409 static bool trans_BL(DisasContext *s, arg_i *a)
1410 {
1411     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1412     reset_btype(s);
1413     gen_goto_tb(s, 0, a->imm);
1414     return true;
1415 }
1416 
1417 
1418 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1419 {
1420     DisasLabel match;
1421     TCGv_i64 tcg_cmp;
1422 
1423     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1424     reset_btype(s);
1425 
1426     match = gen_disas_label(s);
1427     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1428                         tcg_cmp, 0, match.label);
1429     gen_goto_tb(s, 0, 4);
1430     set_disas_label(s, match);
1431     gen_goto_tb(s, 1, a->imm);
1432     return true;
1433 }
1434 
1435 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1436 {
1437     DisasLabel match;
1438     TCGv_i64 tcg_cmp;
1439 
1440     tcg_cmp = tcg_temp_new_i64();
1441     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1442 
1443     reset_btype(s);
1444 
1445     match = gen_disas_label(s);
1446     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1447                         tcg_cmp, 0, match.label);
1448     gen_goto_tb(s, 0, 4);
1449     set_disas_label(s, match);
1450     gen_goto_tb(s, 1, a->imm);
1451     return true;
1452 }
1453 
1454 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1455 {
1456     reset_btype(s);
1457     if (a->cond < 0x0e) {
1458         /* genuinely conditional branches */
1459         DisasLabel match = gen_disas_label(s);
1460         arm_gen_test_cc(a->cond, match.label);
1461         gen_goto_tb(s, 0, 4);
1462         set_disas_label(s, match);
1463         gen_goto_tb(s, 1, a->imm);
1464     } else {
1465         /* 0xe and 0xf are both "always" conditions */
1466         gen_goto_tb(s, 0, a->imm);
1467     }
1468     return true;
1469 }
1470 
1471 static void set_btype_for_br(DisasContext *s, int rn)
1472 {
1473     if (dc_isar_feature(aa64_bti, s)) {
1474         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1475         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1476     }
1477 }
1478 
1479 static void set_btype_for_blr(DisasContext *s)
1480 {
1481     if (dc_isar_feature(aa64_bti, s)) {
1482         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1483         set_btype(s, 2);
1484     }
1485 }
1486 
1487 static bool trans_BR(DisasContext *s, arg_r *a)
1488 {
1489     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1490     set_btype_for_br(s, a->rn);
1491     s->base.is_jmp = DISAS_JUMP;
1492     return true;
1493 }
1494 
1495 static bool trans_BLR(DisasContext *s, arg_r *a)
1496 {
1497     TCGv_i64 dst = cpu_reg(s, a->rn);
1498     TCGv_i64 lr = cpu_reg(s, 30);
1499     if (dst == lr) {
1500         TCGv_i64 tmp = tcg_temp_new_i64();
1501         tcg_gen_mov_i64(tmp, dst);
1502         dst = tmp;
1503     }
1504     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1505     gen_a64_set_pc(s, dst);
1506     set_btype_for_blr(s);
1507     s->base.is_jmp = DISAS_JUMP;
1508     return true;
1509 }
1510 
1511 static bool trans_RET(DisasContext *s, arg_r *a)
1512 {
1513     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1514     s->base.is_jmp = DISAS_JUMP;
1515     return true;
1516 }
1517 
1518 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1519                                    TCGv_i64 modifier, bool use_key_a)
1520 {
1521     TCGv_i64 truedst;
1522     /*
1523      * Return the branch target for a BRAA/RETA/etc, which is either
1524      * just the destination dst, or that value with the pauth check
1525      * done and the code removed from the high bits.
1526      */
1527     if (!s->pauth_active) {
1528         return dst;
1529     }
1530 
1531     truedst = tcg_temp_new_i64();
1532     if (use_key_a) {
1533         gen_helper_autia_combined(truedst, cpu_env, dst, modifier);
1534     } else {
1535         gen_helper_autib_combined(truedst, cpu_env, dst, modifier);
1536     }
1537     return truedst;
1538 }
1539 
1540 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1541 {
1542     TCGv_i64 dst;
1543 
1544     if (!dc_isar_feature(aa64_pauth, s)) {
1545         return false;
1546     }
1547 
1548     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1549     gen_a64_set_pc(s, dst);
1550     set_btype_for_br(s, a->rn);
1551     s->base.is_jmp = DISAS_JUMP;
1552     return true;
1553 }
1554 
1555 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1556 {
1557     TCGv_i64 dst, lr;
1558 
1559     if (!dc_isar_feature(aa64_pauth, s)) {
1560         return false;
1561     }
1562 
1563     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1564     lr = cpu_reg(s, 30);
1565     if (dst == lr) {
1566         TCGv_i64 tmp = tcg_temp_new_i64();
1567         tcg_gen_mov_i64(tmp, dst);
1568         dst = tmp;
1569     }
1570     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1571     gen_a64_set_pc(s, dst);
1572     set_btype_for_blr(s);
1573     s->base.is_jmp = DISAS_JUMP;
1574     return true;
1575 }
1576 
1577 static bool trans_RETA(DisasContext *s, arg_reta *a)
1578 {
1579     TCGv_i64 dst;
1580 
1581     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1582     gen_a64_set_pc(s, dst);
1583     s->base.is_jmp = DISAS_JUMP;
1584     return true;
1585 }
1586 
1587 static bool trans_BRA(DisasContext *s, arg_bra *a)
1588 {
1589     TCGv_i64 dst;
1590 
1591     if (!dc_isar_feature(aa64_pauth, s)) {
1592         return false;
1593     }
1594     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1595     gen_a64_set_pc(s, dst);
1596     set_btype_for_br(s, a->rn);
1597     s->base.is_jmp = DISAS_JUMP;
1598     return true;
1599 }
1600 
1601 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1602 {
1603     TCGv_i64 dst, lr;
1604 
1605     if (!dc_isar_feature(aa64_pauth, s)) {
1606         return false;
1607     }
1608     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1609     lr = cpu_reg(s, 30);
1610     if (dst == lr) {
1611         TCGv_i64 tmp = tcg_temp_new_i64();
1612         tcg_gen_mov_i64(tmp, dst);
1613         dst = tmp;
1614     }
1615     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1616     gen_a64_set_pc(s, dst);
1617     set_btype_for_blr(s);
1618     s->base.is_jmp = DISAS_JUMP;
1619     return true;
1620 }
1621 
1622 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1623 {
1624     TCGv_i64 dst;
1625 
1626     if (s->current_el == 0) {
1627         return false;
1628     }
1629     if (s->fgt_eret) {
1630         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1631         return true;
1632     }
1633     dst = tcg_temp_new_i64();
1634     tcg_gen_ld_i64(dst, cpu_env,
1635                    offsetof(CPUARMState, elr_el[s->current_el]));
1636 
1637     translator_io_start(&s->base);
1638 
1639     gen_helper_exception_return(cpu_env, dst);
1640     /* Must exit loop to check un-masked IRQs */
1641     s->base.is_jmp = DISAS_EXIT;
1642     return true;
1643 }
1644 
1645 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1646 {
1647     TCGv_i64 dst;
1648 
1649     if (!dc_isar_feature(aa64_pauth, s)) {
1650         return false;
1651     }
1652     if (s->current_el == 0) {
1653         return false;
1654     }
1655     /* The FGT trap takes precedence over an auth trap. */
1656     if (s->fgt_eret) {
1657         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1658         return true;
1659     }
1660     dst = tcg_temp_new_i64();
1661     tcg_gen_ld_i64(dst, cpu_env,
1662                    offsetof(CPUARMState, elr_el[s->current_el]));
1663 
1664     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1665 
1666     translator_io_start(&s->base);
1667 
1668     gen_helper_exception_return(cpu_env, dst);
1669     /* Must exit loop to check un-masked IRQs */
1670     s->base.is_jmp = DISAS_EXIT;
1671     return true;
1672 }
1673 
1674 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1675 {
1676     return true;
1677 }
1678 
1679 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1680 {
1681     /*
1682      * When running in MTTCG we don't generate jumps to the yield and
1683      * WFE helpers as it won't affect the scheduling of other vCPUs.
1684      * If we wanted to more completely model WFE/SEV so we don't busy
1685      * spin unnecessarily we would need to do something more involved.
1686      */
1687     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1688         s->base.is_jmp = DISAS_YIELD;
1689     }
1690     return true;
1691 }
1692 
1693 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1694 {
1695     s->base.is_jmp = DISAS_WFI;
1696     return true;
1697 }
1698 
1699 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1700 {
1701     /*
1702      * When running in MTTCG we don't generate jumps to the yield and
1703      * WFE helpers as it won't affect the scheduling of other vCPUs.
1704      * If we wanted to more completely model WFE/SEV so we don't busy
1705      * spin unnecessarily we would need to do something more involved.
1706      */
1707     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1708         s->base.is_jmp = DISAS_WFE;
1709     }
1710     return true;
1711 }
1712 
1713 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1714 {
1715     if (s->pauth_active) {
1716         gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1717     }
1718     return true;
1719 }
1720 
1721 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1722 {
1723     if (s->pauth_active) {
1724         gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1725     }
1726     return true;
1727 }
1728 
1729 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1730 {
1731     if (s->pauth_active) {
1732         gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1733     }
1734     return true;
1735 }
1736 
1737 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1738 {
1739     if (s->pauth_active) {
1740         gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1741     }
1742     return true;
1743 }
1744 
1745 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1746 {
1747     if (s->pauth_active) {
1748         gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1749     }
1750     return true;
1751 }
1752 
1753 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1754 {
1755     /* Without RAS, we must implement this as NOP. */
1756     if (dc_isar_feature(aa64_ras, s)) {
1757         /*
1758          * QEMU does not have a source of physical SErrors,
1759          * so we are only concerned with virtual SErrors.
1760          * The pseudocode in the ARM for this case is
1761          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1762          *      AArch64.vESBOperation();
1763          * Most of the condition can be evaluated at translation time.
1764          * Test for EL2 present, and defer test for SEL2 to runtime.
1765          */
1766         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1767             gen_helper_vesb(cpu_env);
1768         }
1769     }
1770     return true;
1771 }
1772 
1773 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1774 {
1775     if (s->pauth_active) {
1776         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1777     }
1778     return true;
1779 }
1780 
1781 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1782 {
1783     if (s->pauth_active) {
1784         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1785     }
1786     return true;
1787 }
1788 
1789 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1790 {
1791     if (s->pauth_active) {
1792         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1793     }
1794     return true;
1795 }
1796 
1797 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1798 {
1799     if (s->pauth_active) {
1800         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1801     }
1802     return true;
1803 }
1804 
1805 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1806 {
1807     if (s->pauth_active) {
1808         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1809     }
1810     return true;
1811 }
1812 
1813 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1814 {
1815     if (s->pauth_active) {
1816         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1817     }
1818     return true;
1819 }
1820 
1821 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1822 {
1823     if (s->pauth_active) {
1824         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1825     }
1826     return true;
1827 }
1828 
1829 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1830 {
1831     if (s->pauth_active) {
1832         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1833     }
1834     return true;
1835 }
1836 
1837 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1838 {
1839     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1840     return true;
1841 }
1842 
1843 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1844 {
1845     /* We handle DSB and DMB the same way */
1846     TCGBar bar;
1847 
1848     switch (a->types) {
1849     case 1: /* MBReqTypes_Reads */
1850         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1851         break;
1852     case 2: /* MBReqTypes_Writes */
1853         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1854         break;
1855     default: /* MBReqTypes_All */
1856         bar = TCG_BAR_SC | TCG_MO_ALL;
1857         break;
1858     }
1859     tcg_gen_mb(bar);
1860     return true;
1861 }
1862 
1863 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1864 {
1865     /*
1866      * We need to break the TB after this insn to execute
1867      * self-modifying code correctly and also to take
1868      * any pending interrupts immediately.
1869      */
1870     reset_btype(s);
1871     gen_goto_tb(s, 0, 4);
1872     return true;
1873 }
1874 
1875 static bool trans_SB(DisasContext *s, arg_SB *a)
1876 {
1877     if (!dc_isar_feature(aa64_sb, s)) {
1878         return false;
1879     }
1880     /*
1881      * TODO: There is no speculation barrier opcode for TCG;
1882      * MB and end the TB instead.
1883      */
1884     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1885     gen_goto_tb(s, 0, 4);
1886     return true;
1887 }
1888 
1889 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1890 {
1891     if (!dc_isar_feature(aa64_condm_4, s)) {
1892         return false;
1893     }
1894     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1895     return true;
1896 }
1897 
1898 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1899 {
1900     TCGv_i32 z;
1901 
1902     if (!dc_isar_feature(aa64_condm_5, s)) {
1903         return false;
1904     }
1905 
1906     z = tcg_temp_new_i32();
1907 
1908     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1909 
1910     /*
1911      * (!C & !Z) << 31
1912      * (!(C | Z)) << 31
1913      * ~((C | Z) << 31)
1914      * ~-(C | Z)
1915      * (C | Z) - 1
1916      */
1917     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1918     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1919 
1920     /* !(Z & C) */
1921     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1922     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1923 
1924     /* (!C & Z) << 31 -> -(Z & ~C) */
1925     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1926     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1927 
1928     /* C | Z */
1929     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1930 
1931     return true;
1932 }
1933 
1934 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1935 {
1936     if (!dc_isar_feature(aa64_condm_5, s)) {
1937         return false;
1938     }
1939 
1940     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1941     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1942 
1943     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1944     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1945 
1946     tcg_gen_movi_i32(cpu_NF, 0);
1947     tcg_gen_movi_i32(cpu_VF, 0);
1948 
1949     return true;
1950 }
1951 
1952 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1953 {
1954     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1955         return false;
1956     }
1957     if (a->imm & 1) {
1958         set_pstate_bits(PSTATE_UAO);
1959     } else {
1960         clear_pstate_bits(PSTATE_UAO);
1961     }
1962     gen_rebuild_hflags(s);
1963     s->base.is_jmp = DISAS_TOO_MANY;
1964     return true;
1965 }
1966 
1967 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1968 {
1969     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1970         return false;
1971     }
1972     if (a->imm & 1) {
1973         set_pstate_bits(PSTATE_PAN);
1974     } else {
1975         clear_pstate_bits(PSTATE_PAN);
1976     }
1977     gen_rebuild_hflags(s);
1978     s->base.is_jmp = DISAS_TOO_MANY;
1979     return true;
1980 }
1981 
1982 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1983 {
1984     if (s->current_el == 0) {
1985         return false;
1986     }
1987     gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(a->imm & PSTATE_SP));
1988     s->base.is_jmp = DISAS_TOO_MANY;
1989     return true;
1990 }
1991 
1992 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
1993 {
1994     if (!dc_isar_feature(aa64_ssbs, s)) {
1995         return false;
1996     }
1997     if (a->imm & 1) {
1998         set_pstate_bits(PSTATE_SSBS);
1999     } else {
2000         clear_pstate_bits(PSTATE_SSBS);
2001     }
2002     /* Don't need to rebuild hflags since SSBS is a nop */
2003     s->base.is_jmp = DISAS_TOO_MANY;
2004     return true;
2005 }
2006 
2007 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
2008 {
2009     if (!dc_isar_feature(aa64_dit, s)) {
2010         return false;
2011     }
2012     if (a->imm & 1) {
2013         set_pstate_bits(PSTATE_DIT);
2014     } else {
2015         clear_pstate_bits(PSTATE_DIT);
2016     }
2017     /* There's no need to rebuild hflags because DIT is a nop */
2018     s->base.is_jmp = DISAS_TOO_MANY;
2019     return true;
2020 }
2021 
2022 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2023 {
2024     if (dc_isar_feature(aa64_mte, s)) {
2025         /* Full MTE is enabled -- set the TCO bit as directed. */
2026         if (a->imm & 1) {
2027             set_pstate_bits(PSTATE_TCO);
2028         } else {
2029             clear_pstate_bits(PSTATE_TCO);
2030         }
2031         gen_rebuild_hflags(s);
2032         /* Many factors, including TCO, go into MTE_ACTIVE. */
2033         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2034         return true;
2035     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2036         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2037         return true;
2038     } else {
2039         /* Insn not present */
2040         return false;
2041     }
2042 }
2043 
2044 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2045 {
2046     gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(a->imm));
2047     s->base.is_jmp = DISAS_TOO_MANY;
2048     return true;
2049 }
2050 
2051 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2052 {
2053     gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(a->imm));
2054     /* Exit the cpu loop to re-evaluate pending IRQs. */
2055     s->base.is_jmp = DISAS_UPDATE_EXIT;
2056     return true;
2057 }
2058 
2059 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2060 {
2061     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2062         return false;
2063     }
2064     if (sme_access_check(s)) {
2065         int old = s->pstate_sm | (s->pstate_za << 1);
2066         int new = a->imm * 3;
2067 
2068         if ((old ^ new) & a->mask) {
2069             /* At least one bit changes. */
2070             gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2071                                 tcg_constant_i32(a->mask));
2072             s->base.is_jmp = DISAS_TOO_MANY;
2073         }
2074     }
2075     return true;
2076 }
2077 
2078 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2079 {
2080     TCGv_i32 tmp = tcg_temp_new_i32();
2081     TCGv_i32 nzcv = tcg_temp_new_i32();
2082 
2083     /* build bit 31, N */
2084     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2085     /* build bit 30, Z */
2086     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2087     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2088     /* build bit 29, C */
2089     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2090     /* build bit 28, V */
2091     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2092     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2093     /* generate result */
2094     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2095 }
2096 
2097 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2098 {
2099     TCGv_i32 nzcv = tcg_temp_new_i32();
2100 
2101     /* take NZCV from R[t] */
2102     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2103 
2104     /* bit 31, N */
2105     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2106     /* bit 30, Z */
2107     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2108     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2109     /* bit 29, C */
2110     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2111     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2112     /* bit 28, V */
2113     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2114     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2115 }
2116 
2117 static void gen_sysreg_undef(DisasContext *s, bool isread,
2118                              uint8_t op0, uint8_t op1, uint8_t op2,
2119                              uint8_t crn, uint8_t crm, uint8_t rt)
2120 {
2121     /*
2122      * Generate code to emit an UNDEF with correct syndrome
2123      * information for a failed system register access.
2124      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2125      * but if FEAT_IDST is implemented then read accesses to registers
2126      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2127      * syndrome.
2128      */
2129     uint32_t syndrome;
2130 
2131     if (isread && dc_isar_feature(aa64_ids, s) &&
2132         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2133         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2134     } else {
2135         syndrome = syn_uncategorized();
2136     }
2137     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2138 }
2139 
2140 /* MRS - move from system register
2141  * MSR (register) - move to system register
2142  * SYS
2143  * SYSL
2144  * These are all essentially the same insn in 'read' and 'write'
2145  * versions, with varying op0 fields.
2146  */
2147 static void handle_sys(DisasContext *s, bool isread,
2148                        unsigned int op0, unsigned int op1, unsigned int op2,
2149                        unsigned int crn, unsigned int crm, unsigned int rt)
2150 {
2151     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2152                                       crn, crm, op0, op1, op2);
2153     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2154     bool need_exit_tb = false;
2155     TCGv_ptr tcg_ri = NULL;
2156     TCGv_i64 tcg_rt;
2157 
2158     if (!ri) {
2159         /* Unknown register; this might be a guest error or a QEMU
2160          * unimplemented feature.
2161          */
2162         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2163                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2164                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2165         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2166         return;
2167     }
2168 
2169     /* Check access permissions */
2170     if (!cp_access_ok(s->current_el, ri, isread)) {
2171         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2172         return;
2173     }
2174 
2175     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2176         /* Emit code to perform further access permissions checks at
2177          * runtime; this may result in an exception.
2178          */
2179         uint32_t syndrome;
2180 
2181         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2182         gen_a64_update_pc(s, 0);
2183         tcg_ri = tcg_temp_new_ptr();
2184         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2185                                        tcg_constant_i32(key),
2186                                        tcg_constant_i32(syndrome),
2187                                        tcg_constant_i32(isread));
2188     } else if (ri->type & ARM_CP_RAISES_EXC) {
2189         /*
2190          * The readfn or writefn might raise an exception;
2191          * synchronize the CPU state in case it does.
2192          */
2193         gen_a64_update_pc(s, 0);
2194     }
2195 
2196     /* Handle special cases first */
2197     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2198     case 0:
2199         break;
2200     case ARM_CP_NOP:
2201         return;
2202     case ARM_CP_NZCV:
2203         tcg_rt = cpu_reg(s, rt);
2204         if (isread) {
2205             gen_get_nzcv(tcg_rt);
2206         } else {
2207             gen_set_nzcv(tcg_rt);
2208         }
2209         return;
2210     case ARM_CP_CURRENTEL:
2211         /* Reads as current EL value from pstate, which is
2212          * guaranteed to be constant by the tb flags.
2213          */
2214         tcg_rt = cpu_reg(s, rt);
2215         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2216         return;
2217     case ARM_CP_DC_ZVA:
2218         /* Writes clear the aligned block of memory which rt points into. */
2219         if (s->mte_active[0]) {
2220             int desc = 0;
2221 
2222             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2223             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2224             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2225 
2226             tcg_rt = tcg_temp_new_i64();
2227             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2228                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2229         } else {
2230             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2231         }
2232         gen_helper_dc_zva(cpu_env, tcg_rt);
2233         return;
2234     case ARM_CP_DC_GVA:
2235         {
2236             TCGv_i64 clean_addr, tag;
2237 
2238             /*
2239              * DC_GVA, like DC_ZVA, requires that we supply the original
2240              * pointer for an invalid page.  Probe that address first.
2241              */
2242             tcg_rt = cpu_reg(s, rt);
2243             clean_addr = clean_data_tbi(s, tcg_rt);
2244             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2245 
2246             if (s->ata) {
2247                 /* Extract the tag from the register to match STZGM.  */
2248                 tag = tcg_temp_new_i64();
2249                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2250                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2251             }
2252         }
2253         return;
2254     case ARM_CP_DC_GZVA:
2255         {
2256             TCGv_i64 clean_addr, tag;
2257 
2258             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2259             tcg_rt = cpu_reg(s, rt);
2260             clean_addr = clean_data_tbi(s, tcg_rt);
2261             gen_helper_dc_zva(cpu_env, clean_addr);
2262 
2263             if (s->ata) {
2264                 /* Extract the tag from the register to match STZGM.  */
2265                 tag = tcg_temp_new_i64();
2266                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2267                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2268             }
2269         }
2270         return;
2271     default:
2272         g_assert_not_reached();
2273     }
2274     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2275         return;
2276     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2277         return;
2278     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2279         return;
2280     }
2281 
2282     if (ri->type & ARM_CP_IO) {
2283         /* I/O operations must end the TB here (whether read or write) */
2284         need_exit_tb = translator_io_start(&s->base);
2285     }
2286 
2287     tcg_rt = cpu_reg(s, rt);
2288 
2289     if (isread) {
2290         if (ri->type & ARM_CP_CONST) {
2291             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2292         } else if (ri->readfn) {
2293             if (!tcg_ri) {
2294                 tcg_ri = gen_lookup_cp_reg(key);
2295             }
2296             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2297         } else {
2298             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2299         }
2300     } else {
2301         if (ri->type & ARM_CP_CONST) {
2302             /* If not forbidden by access permissions, treat as WI */
2303             return;
2304         } else if (ri->writefn) {
2305             if (!tcg_ri) {
2306                 tcg_ri = gen_lookup_cp_reg(key);
2307             }
2308             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2309         } else {
2310             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2311         }
2312     }
2313 
2314     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2315         /*
2316          * A write to any coprocessor register that ends a TB
2317          * must rebuild the hflags for the next TB.
2318          */
2319         gen_rebuild_hflags(s);
2320         /*
2321          * We default to ending the TB on a coprocessor register write,
2322          * but allow this to be suppressed by the register definition
2323          * (usually only necessary to work around guest bugs).
2324          */
2325         need_exit_tb = true;
2326     }
2327     if (need_exit_tb) {
2328         s->base.is_jmp = DISAS_UPDATE_EXIT;
2329     }
2330 }
2331 
2332 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2333 {
2334     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2335     return true;
2336 }
2337 
2338 static bool trans_SVC(DisasContext *s, arg_i *a)
2339 {
2340     /*
2341      * For SVC, HVC and SMC we advance the single-step state
2342      * machine before taking the exception. This is architecturally
2343      * mandated, to ensure that single-stepping a system call
2344      * instruction works properly.
2345      */
2346     uint32_t syndrome = syn_aa64_svc(a->imm);
2347     if (s->fgt_svc) {
2348         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2349         return true;
2350     }
2351     gen_ss_advance(s);
2352     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2353     return true;
2354 }
2355 
2356 static bool trans_HVC(DisasContext *s, arg_i *a)
2357 {
2358     if (s->current_el == 0) {
2359         unallocated_encoding(s);
2360         return true;
2361     }
2362     /*
2363      * The pre HVC helper handles cases when HVC gets trapped
2364      * as an undefined insn by runtime configuration.
2365      */
2366     gen_a64_update_pc(s, 0);
2367     gen_helper_pre_hvc(cpu_env);
2368     /* Architecture requires ss advance before we do the actual work */
2369     gen_ss_advance(s);
2370     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), 2);
2371     return true;
2372 }
2373 
2374 static bool trans_SMC(DisasContext *s, arg_i *a)
2375 {
2376     if (s->current_el == 0) {
2377         unallocated_encoding(s);
2378         return true;
2379     }
2380     gen_a64_update_pc(s, 0);
2381     gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2382     /* Architecture requires ss advance before we do the actual work */
2383     gen_ss_advance(s);
2384     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2385     return true;
2386 }
2387 
2388 static bool trans_BRK(DisasContext *s, arg_i *a)
2389 {
2390     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2391     return true;
2392 }
2393 
2394 static bool trans_HLT(DisasContext *s, arg_i *a)
2395 {
2396     /*
2397      * HLT. This has two purposes.
2398      * Architecturally, it is an external halting debug instruction.
2399      * Since QEMU doesn't implement external debug, we treat this as
2400      * it is required for halting debug disabled: it will UNDEF.
2401      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2402      */
2403     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2404         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2405     } else {
2406         unallocated_encoding(s);
2407     }
2408     return true;
2409 }
2410 
2411 /*
2412  * Load/Store exclusive instructions are implemented by remembering
2413  * the value/address loaded, and seeing if these are the same
2414  * when the store is performed. This is not actually the architecturally
2415  * mandated semantics, but it works for typical guest code sequences
2416  * and avoids having to monitor regular stores.
2417  *
2418  * The store exclusive uses the atomic cmpxchg primitives to avoid
2419  * races in multi-threaded linux-user and when MTTCG softmmu is
2420  * enabled.
2421  */
2422 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2423                                int size, bool is_pair)
2424 {
2425     int idx = get_mem_index(s);
2426     TCGv_i64 dirty_addr, clean_addr;
2427     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2428 
2429     s->is_ldex = true;
2430     dirty_addr = cpu_reg_sp(s, rn);
2431     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2432 
2433     g_assert(size <= 3);
2434     if (is_pair) {
2435         g_assert(size >= 2);
2436         if (size == 2) {
2437             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2438             if (s->be_data == MO_LE) {
2439                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2440                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2441             } else {
2442                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2443                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2444             }
2445         } else {
2446             TCGv_i128 t16 = tcg_temp_new_i128();
2447 
2448             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2449 
2450             if (s->be_data == MO_LE) {
2451                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2452                                       cpu_exclusive_high, t16);
2453             } else {
2454                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2455                                       cpu_exclusive_val, t16);
2456             }
2457             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2458             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2459         }
2460     } else {
2461         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2462         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2463     }
2464     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2465 }
2466 
2467 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2468                                 int rn, int size, int is_pair)
2469 {
2470     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2471      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2472      *     [addr] = {Rt};
2473      *     if (is_pair) {
2474      *         [addr + datasize] = {Rt2};
2475      *     }
2476      *     {Rd} = 0;
2477      * } else {
2478      *     {Rd} = 1;
2479      * }
2480      * env->exclusive_addr = -1;
2481      */
2482     TCGLabel *fail_label = gen_new_label();
2483     TCGLabel *done_label = gen_new_label();
2484     TCGv_i64 tmp, clean_addr;
2485     MemOp memop;
2486 
2487     /*
2488      * FIXME: We are out of spec here.  We have recorded only the address
2489      * from load_exclusive, not the entire range, and we assume that the
2490      * size of the access on both sides match.  The architecture allows the
2491      * store to be smaller than the load, so long as the stored bytes are
2492      * within the range recorded by the load.
2493      */
2494 
2495     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2496     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2497     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2498 
2499     /*
2500      * The write, and any associated faults, only happen if the virtual
2501      * and physical addresses pass the exclusive monitor check.  These
2502      * faults are exceedingly unlikely, because normally the guest uses
2503      * the exact same address register for the load_exclusive, and we
2504      * would have recognized these faults there.
2505      *
2506      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2507      * unaligned 4-byte write within the range of an aligned 8-byte load.
2508      * With LSE2, the store would need to cross a 16-byte boundary when the
2509      * load did not, which would mean the store is outside the range
2510      * recorded for the monitor, which would have failed a corrected monitor
2511      * check above.  For now, we assume no size change and retain the
2512      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2513      *
2514      * It is possible to trigger an MTE fault, by performing the load with
2515      * a virtual address with a valid tag and performing the store with the
2516      * same virtual address and a different invalid tag.
2517      */
2518     memop = size + is_pair;
2519     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2520         memop |= MO_ALIGN;
2521     }
2522     memop = finalize_memop(s, memop);
2523     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2524 
2525     tmp = tcg_temp_new_i64();
2526     if (is_pair) {
2527         if (size == 2) {
2528             if (s->be_data == MO_LE) {
2529                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2530             } else {
2531                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2532             }
2533             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2534                                        cpu_exclusive_val, tmp,
2535                                        get_mem_index(s), memop);
2536             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2537         } else {
2538             TCGv_i128 t16 = tcg_temp_new_i128();
2539             TCGv_i128 c16 = tcg_temp_new_i128();
2540             TCGv_i64 a, b;
2541 
2542             if (s->be_data == MO_LE) {
2543                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2544                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2545                                         cpu_exclusive_high);
2546             } else {
2547                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2548                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2549                                         cpu_exclusive_val);
2550             }
2551 
2552             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2553                                         get_mem_index(s), memop);
2554 
2555             a = tcg_temp_new_i64();
2556             b = tcg_temp_new_i64();
2557             if (s->be_data == MO_LE) {
2558                 tcg_gen_extr_i128_i64(a, b, t16);
2559             } else {
2560                 tcg_gen_extr_i128_i64(b, a, t16);
2561             }
2562 
2563             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2564             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2565             tcg_gen_or_i64(tmp, a, b);
2566 
2567             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2568         }
2569     } else {
2570         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2571                                    cpu_reg(s, rt), get_mem_index(s), memop);
2572         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2573     }
2574     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2575     tcg_gen_br(done_label);
2576 
2577     gen_set_label(fail_label);
2578     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2579     gen_set_label(done_label);
2580     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2581 }
2582 
2583 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2584                                  int rn, int size)
2585 {
2586     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2587     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2588     int memidx = get_mem_index(s);
2589     TCGv_i64 clean_addr;
2590     MemOp memop;
2591 
2592     if (rn == 31) {
2593         gen_check_sp_alignment(s);
2594     }
2595     memop = check_atomic_align(s, rn, size);
2596     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2597     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2598                                memidx, memop);
2599 }
2600 
2601 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2602                                       int rn, int size)
2603 {
2604     TCGv_i64 s1 = cpu_reg(s, rs);
2605     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2606     TCGv_i64 t1 = cpu_reg(s, rt);
2607     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2608     TCGv_i64 clean_addr;
2609     int memidx = get_mem_index(s);
2610     MemOp memop;
2611 
2612     if (rn == 31) {
2613         gen_check_sp_alignment(s);
2614     }
2615 
2616     /* This is a single atomic access, despite the "pair". */
2617     memop = check_atomic_align(s, rn, size + 1);
2618     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2619 
2620     if (size == 2) {
2621         TCGv_i64 cmp = tcg_temp_new_i64();
2622         TCGv_i64 val = tcg_temp_new_i64();
2623 
2624         if (s->be_data == MO_LE) {
2625             tcg_gen_concat32_i64(val, t1, t2);
2626             tcg_gen_concat32_i64(cmp, s1, s2);
2627         } else {
2628             tcg_gen_concat32_i64(val, t2, t1);
2629             tcg_gen_concat32_i64(cmp, s2, s1);
2630         }
2631 
2632         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2633 
2634         if (s->be_data == MO_LE) {
2635             tcg_gen_extr32_i64(s1, s2, cmp);
2636         } else {
2637             tcg_gen_extr32_i64(s2, s1, cmp);
2638         }
2639     } else {
2640         TCGv_i128 cmp = tcg_temp_new_i128();
2641         TCGv_i128 val = tcg_temp_new_i128();
2642 
2643         if (s->be_data == MO_LE) {
2644             tcg_gen_concat_i64_i128(val, t1, t2);
2645             tcg_gen_concat_i64_i128(cmp, s1, s2);
2646         } else {
2647             tcg_gen_concat_i64_i128(val, t2, t1);
2648             tcg_gen_concat_i64_i128(cmp, s2, s1);
2649         }
2650 
2651         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2652 
2653         if (s->be_data == MO_LE) {
2654             tcg_gen_extr_i128_i64(s1, s2, cmp);
2655         } else {
2656             tcg_gen_extr_i128_i64(s2, s1, cmp);
2657         }
2658     }
2659 }
2660 
2661 /*
2662  * Compute the ISS.SF bit for syndrome information if an exception
2663  * is taken on a load or store. This indicates whether the instruction
2664  * is accessing a 32-bit or 64-bit register. This logic is derived
2665  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2666  */
2667 static bool ldst_iss_sf(int size, bool sign, bool ext)
2668 {
2669 
2670     if (sign) {
2671         /*
2672          * Signed loads are 64 bit results if we are not going to
2673          * do a zero-extend from 32 to 64 after the load.
2674          * (For a store, sign and ext are always false.)
2675          */
2676         return !ext;
2677     } else {
2678         /* Unsigned loads/stores work at the specified size */
2679         return size == MO_64;
2680     }
2681 }
2682 
2683 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2684 {
2685     if (a->rn == 31) {
2686         gen_check_sp_alignment(s);
2687     }
2688     if (a->lasr) {
2689         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2690     }
2691     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2692     return true;
2693 }
2694 
2695 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2696 {
2697     if (a->rn == 31) {
2698         gen_check_sp_alignment(s);
2699     }
2700     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2701     if (a->lasr) {
2702         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2703     }
2704     return true;
2705 }
2706 
2707 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2708 {
2709     TCGv_i64 clean_addr;
2710     MemOp memop;
2711     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2712 
2713     /*
2714      * StoreLORelease is the same as Store-Release for QEMU, but
2715      * needs the feature-test.
2716      */
2717     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2718         return false;
2719     }
2720     /* Generate ISS for non-exclusive accesses including LASR.  */
2721     if (a->rn == 31) {
2722         gen_check_sp_alignment(s);
2723     }
2724     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2725     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2726     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2727                                 true, a->rn != 31, memop);
2728     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2729               iss_sf, a->lasr);
2730     return true;
2731 }
2732 
2733 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2734 {
2735     TCGv_i64 clean_addr;
2736     MemOp memop;
2737     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2738 
2739     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2740     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2741         return false;
2742     }
2743     /* Generate ISS for non-exclusive accesses including LASR.  */
2744     if (a->rn == 31) {
2745         gen_check_sp_alignment(s);
2746     }
2747     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2748     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2749                                 false, a->rn != 31, memop);
2750     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2751               a->rt, iss_sf, a->lasr);
2752     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2753     return true;
2754 }
2755 
2756 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2757 {
2758     if (a->rn == 31) {
2759         gen_check_sp_alignment(s);
2760     }
2761     if (a->lasr) {
2762         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2763     }
2764     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2765     return true;
2766 }
2767 
2768 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2769 {
2770     if (a->rn == 31) {
2771         gen_check_sp_alignment(s);
2772     }
2773     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2774     if (a->lasr) {
2775         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2776     }
2777     return true;
2778 }
2779 
2780 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2781 {
2782     if (!dc_isar_feature(aa64_atomics, s)) {
2783         return false;
2784     }
2785     if (((a->rt | a->rs) & 1) != 0) {
2786         return false;
2787     }
2788 
2789     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2790     return true;
2791 }
2792 
2793 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2794 {
2795     if (!dc_isar_feature(aa64_atomics, s)) {
2796         return false;
2797     }
2798     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2799     return true;
2800 }
2801 
2802 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2803 {
2804     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2805     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
2806     TCGv_i64 clean_addr = tcg_temp_new_i64();
2807     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
2808 
2809     gen_pc_plus_diff(s, clean_addr, a->imm);
2810     do_gpr_ld(s, tcg_rt, clean_addr, memop,
2811               false, true, a->rt, iss_sf, false);
2812     return true;
2813 }
2814 
2815 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
2816 {
2817     /* Load register (literal), vector version */
2818     TCGv_i64 clean_addr;
2819     MemOp memop;
2820 
2821     if (!fp_access_check(s)) {
2822         return true;
2823     }
2824     memop = finalize_memop_asimd(s, a->sz);
2825     clean_addr = tcg_temp_new_i64();
2826     gen_pc_plus_diff(s, clean_addr, a->imm);
2827     do_fp_ld(s, a->rt, clean_addr, memop);
2828     return true;
2829 }
2830 
2831 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
2832                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
2833                                  uint64_t offset, bool is_store, MemOp mop)
2834 {
2835     if (a->rn == 31) {
2836         gen_check_sp_alignment(s);
2837     }
2838 
2839     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
2840     if (!a->p) {
2841         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
2842     }
2843 
2844     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
2845                                  (a->w || a->rn != 31), 2 << a->sz, mop);
2846 }
2847 
2848 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
2849                                   TCGv_i64 dirty_addr, uint64_t offset)
2850 {
2851     if (a->w) {
2852         if (a->p) {
2853             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2854         }
2855         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
2856     }
2857 }
2858 
2859 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
2860 {
2861     uint64_t offset = a->imm << a->sz;
2862     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2863     MemOp mop = finalize_memop(s, a->sz);
2864 
2865     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2866     tcg_rt = cpu_reg(s, a->rt);
2867     tcg_rt2 = cpu_reg(s, a->rt2);
2868     /*
2869      * We built mop above for the single logical access -- rebuild it
2870      * now for the paired operation.
2871      *
2872      * With LSE2, non-sign-extending pairs are treated atomically if
2873      * aligned, and if unaligned one of the pair will be completely
2874      * within a 16-byte block and that element will be atomic.
2875      * Otherwise each element is separately atomic.
2876      * In all cases, issue one operation with the correct atomicity.
2877      */
2878     mop = a->sz + 1;
2879     if (s->align_mem) {
2880         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2881     }
2882     mop = finalize_memop_pair(s, mop);
2883     if (a->sz == 2) {
2884         TCGv_i64 tmp = tcg_temp_new_i64();
2885 
2886         if (s->be_data == MO_LE) {
2887             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2888         } else {
2889             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2890         }
2891         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2892     } else {
2893         TCGv_i128 tmp = tcg_temp_new_i128();
2894 
2895         if (s->be_data == MO_LE) {
2896             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
2897         } else {
2898             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
2899         }
2900         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
2901     }
2902     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2903     return true;
2904 }
2905 
2906 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
2907 {
2908     uint64_t offset = a->imm << a->sz;
2909     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2910     MemOp mop = finalize_memop(s, a->sz);
2911 
2912     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2913     tcg_rt = cpu_reg(s, a->rt);
2914     tcg_rt2 = cpu_reg(s, a->rt2);
2915 
2916     /*
2917      * We built mop above for the single logical access -- rebuild it
2918      * now for the paired operation.
2919      *
2920      * With LSE2, non-sign-extending pairs are treated atomically if
2921      * aligned, and if unaligned one of the pair will be completely
2922      * within a 16-byte block and that element will be atomic.
2923      * Otherwise each element is separately atomic.
2924      * In all cases, issue one operation with the correct atomicity.
2925      *
2926      * This treats sign-extending loads like zero-extending loads,
2927      * since that reuses the most code below.
2928      */
2929     mop = a->sz + 1;
2930     if (s->align_mem) {
2931         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2932     }
2933     mop = finalize_memop_pair(s, mop);
2934     if (a->sz == 2) {
2935         int o2 = s->be_data == MO_LE ? 32 : 0;
2936         int o1 = o2 ^ 32;
2937 
2938         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2939         if (a->sign) {
2940             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2941             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2942         } else {
2943             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2944             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2945         }
2946     } else {
2947         TCGv_i128 tmp = tcg_temp_new_i128();
2948 
2949         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2950         if (s->be_data == MO_LE) {
2951             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2952         } else {
2953             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2954         }
2955     }
2956     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2957     return true;
2958 }
2959 
2960 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
2961 {
2962     uint64_t offset = a->imm << a->sz;
2963     TCGv_i64 clean_addr, dirty_addr;
2964     MemOp mop;
2965 
2966     if (!fp_access_check(s)) {
2967         return true;
2968     }
2969 
2970     /* LSE2 does not merge FP pairs; leave these as separate operations. */
2971     mop = finalize_memop_asimd(s, a->sz);
2972     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2973     do_fp_st(s, a->rt, clean_addr, mop);
2974     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
2975     do_fp_st(s, a->rt2, clean_addr, mop);
2976     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2977     return true;
2978 }
2979 
2980 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
2981 {
2982     uint64_t offset = a->imm << a->sz;
2983     TCGv_i64 clean_addr, dirty_addr;
2984     MemOp mop;
2985 
2986     if (!fp_access_check(s)) {
2987         return true;
2988     }
2989 
2990     /* LSE2 does not merge FP pairs; leave these as separate operations. */
2991     mop = finalize_memop_asimd(s, a->sz);
2992     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2993     do_fp_ld(s, a->rt, clean_addr, mop);
2994     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
2995     do_fp_ld(s, a->rt2, clean_addr, mop);
2996     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2997     return true;
2998 }
2999 
3000 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3001 {
3002     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3003     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3004     MemOp mop;
3005     TCGv_i128 tmp;
3006 
3007     /* STGP only comes in one size. */
3008     tcg_debug_assert(a->sz == MO_64);
3009 
3010     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3011         return false;
3012     }
3013 
3014     if (a->rn == 31) {
3015         gen_check_sp_alignment(s);
3016     }
3017 
3018     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3019     if (!a->p) {
3020         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3021     }
3022 
3023     clean_addr = clean_data_tbi(s, dirty_addr);
3024     tcg_rt = cpu_reg(s, a->rt);
3025     tcg_rt2 = cpu_reg(s, a->rt2);
3026 
3027     /*
3028      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3029      * and one tag operation.  We implement it as one single aligned 16-byte
3030      * memory operation for convenience.  Note that the alignment ensures
3031      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3032      */
3033     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3034 
3035     tmp = tcg_temp_new_i128();
3036     if (s->be_data == MO_LE) {
3037         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3038     } else {
3039         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3040     }
3041     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3042 
3043     /* Perform the tag store, if tag access enabled. */
3044     if (s->ata) {
3045         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3046             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3047         } else {
3048             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3049         }
3050     }
3051 
3052     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3053     return true;
3054 }
3055 
3056 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3057                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3058                                  uint64_t offset, bool is_store, MemOp mop)
3059 {
3060     int memidx;
3061 
3062     if (a->rn == 31) {
3063         gen_check_sp_alignment(s);
3064     }
3065 
3066     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3067     if (!a->p) {
3068         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3069     }
3070     memidx = a->unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3071     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3072                                         a->w || a->rn != 31,
3073                                         mop, a->unpriv, memidx);
3074 }
3075 
3076 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3077                                   TCGv_i64 dirty_addr, uint64_t offset)
3078 {
3079     if (a->w) {
3080         if (a->p) {
3081             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3082         }
3083         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3084     }
3085 }
3086 
3087 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3088 {
3089     bool iss_sf, iss_valid = !a->w;
3090     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3091     int memidx = a->unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3092     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3093 
3094     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3095 
3096     tcg_rt = cpu_reg(s, a->rt);
3097     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3098 
3099     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3100                      iss_valid, a->rt, iss_sf, false);
3101     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3102     return true;
3103 }
3104 
3105 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3106 {
3107     bool iss_sf, iss_valid = !a->w;
3108     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3109     int memidx = a->unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3110     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3111 
3112     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3113 
3114     tcg_rt = cpu_reg(s, a->rt);
3115     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3116 
3117     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3118                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3119     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3120     return true;
3121 }
3122 
3123 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3124 {
3125     TCGv_i64 clean_addr, dirty_addr;
3126     MemOp mop;
3127 
3128     if (!fp_access_check(s)) {
3129         return true;
3130     }
3131     mop = finalize_memop_asimd(s, a->sz);
3132     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3133     do_fp_st(s, a->rt, clean_addr, mop);
3134     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3135     return true;
3136 }
3137 
3138 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3139 {
3140     TCGv_i64 clean_addr, dirty_addr;
3141     MemOp mop;
3142 
3143     if (!fp_access_check(s)) {
3144         return true;
3145     }
3146     mop = finalize_memop_asimd(s, a->sz);
3147     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3148     do_fp_ld(s, a->rt, clean_addr, mop);
3149     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3150     return true;
3151 }
3152 
3153 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3154                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3155                              bool is_store, MemOp memop)
3156 {
3157     TCGv_i64 tcg_rm;
3158 
3159     if (a->rn == 31) {
3160         gen_check_sp_alignment(s);
3161     }
3162     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3163 
3164     tcg_rm = read_cpu_reg(s, a->rm, 1);
3165     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3166 
3167     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3168     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3169 }
3170 
3171 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3172 {
3173     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3174     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3175     MemOp memop;
3176 
3177     if (extract32(a->opt, 1, 1) == 0) {
3178         return false;
3179     }
3180 
3181     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3182     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3183     tcg_rt = cpu_reg(s, a->rt);
3184     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3185               a->ext, true, a->rt, iss_sf, false);
3186     return true;
3187 }
3188 
3189 static bool trans_STR(DisasContext *s, arg_ldst *a)
3190 {
3191     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3192     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3193     MemOp memop;
3194 
3195     if (extract32(a->opt, 1, 1) == 0) {
3196         return false;
3197     }
3198 
3199     memop = finalize_memop(s, a->sz);
3200     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3201     tcg_rt = cpu_reg(s, a->rt);
3202     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3203     return true;
3204 }
3205 
3206 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3207 {
3208     TCGv_i64 clean_addr, dirty_addr;
3209     MemOp memop;
3210 
3211     if (extract32(a->opt, 1, 1) == 0) {
3212         return false;
3213     }
3214 
3215     if (!fp_access_check(s)) {
3216         return true;
3217     }
3218 
3219     memop = finalize_memop_asimd(s, a->sz);
3220     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3221     do_fp_ld(s, a->rt, clean_addr, memop);
3222     return true;
3223 }
3224 
3225 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3226 {
3227     TCGv_i64 clean_addr, dirty_addr;
3228     MemOp memop;
3229 
3230     if (extract32(a->opt, 1, 1) == 0) {
3231         return false;
3232     }
3233 
3234     if (!fp_access_check(s)) {
3235         return true;
3236     }
3237 
3238     memop = finalize_memop_asimd(s, a->sz);
3239     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3240     do_fp_st(s, a->rt, clean_addr, memop);
3241     return true;
3242 }
3243 
3244 
3245 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3246                          int sign, bool invert)
3247 {
3248     MemOp mop = a->sz | sign;
3249     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3250 
3251     if (a->rn == 31) {
3252         gen_check_sp_alignment(s);
3253     }
3254     mop = check_atomic_align(s, a->rn, mop);
3255     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3256                                 a->rn != 31, mop);
3257     tcg_rs = read_cpu_reg(s, a->rs, true);
3258     tcg_rt = cpu_reg(s, a->rt);
3259     if (invert) {
3260         tcg_gen_not_i64(tcg_rs, tcg_rs);
3261     }
3262     /*
3263      * The tcg atomic primitives are all full barriers.  Therefore we
3264      * can ignore the Acquire and Release bits of this instruction.
3265      */
3266     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3267 
3268     if (mop & MO_SIGN) {
3269         switch (a->sz) {
3270         case MO_8:
3271             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3272             break;
3273         case MO_16:
3274             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3275             break;
3276         case MO_32:
3277             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3278             break;
3279         case MO_64:
3280             break;
3281         default:
3282             g_assert_not_reached();
3283         }
3284     }
3285     return true;
3286 }
3287 
3288 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3289 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3290 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3291 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3292 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3293 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3294 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3295 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3296 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3297 
3298 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3299 {
3300     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3301     TCGv_i64 clean_addr;
3302     MemOp mop;
3303 
3304     if (!dc_isar_feature(aa64_atomics, s) ||
3305         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3306         return false;
3307     }
3308     if (a->rn == 31) {
3309         gen_check_sp_alignment(s);
3310     }
3311     mop = check_atomic_align(s, a->rn, a->sz);
3312     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3313                                 a->rn != 31, mop);
3314     /*
3315      * LDAPR* are a special case because they are a simple load, not a
3316      * fetch-and-do-something op.
3317      * The architectural consistency requirements here are weaker than
3318      * full load-acquire (we only need "load-acquire processor consistent"),
3319      * but we choose to implement them as full LDAQ.
3320      */
3321     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3322               true, a->rt, iss_sf, true);
3323     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3324     return true;
3325 }
3326 
3327 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3328 {
3329     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3330     MemOp memop;
3331 
3332     /* Load with pointer authentication */
3333     if (!dc_isar_feature(aa64_pauth, s)) {
3334         return false;
3335     }
3336 
3337     if (a->rn == 31) {
3338         gen_check_sp_alignment(s);
3339     }
3340     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3341 
3342     if (s->pauth_active) {
3343         if (!a->m) {
3344             gen_helper_autda_combined(dirty_addr, cpu_env, dirty_addr,
3345                                       tcg_constant_i64(0));
3346         } else {
3347             gen_helper_autdb_combined(dirty_addr, cpu_env, dirty_addr,
3348                                       tcg_constant_i64(0));
3349         }
3350     }
3351 
3352     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3353 
3354     memop = finalize_memop(s, MO_64);
3355 
3356     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3357     clean_addr = gen_mte_check1(s, dirty_addr, false,
3358                                 a->w || a->rn != 31, memop);
3359 
3360     tcg_rt = cpu_reg(s, a->rt);
3361     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3362               /* extend */ false, /* iss_valid */ !a->w,
3363               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3364 
3365     if (a->w) {
3366         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3367     }
3368     return true;
3369 }
3370 
3371 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3372 {
3373     TCGv_i64 clean_addr, dirty_addr;
3374     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3375     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3376 
3377     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3378         return false;
3379     }
3380 
3381     if (a->rn == 31) {
3382         gen_check_sp_alignment(s);
3383     }
3384 
3385     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3386     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3387     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3388     clean_addr = clean_data_tbi(s, dirty_addr);
3389 
3390     /*
3391      * Load-AcquirePC semantics; we implement as the slightly more
3392      * restrictive Load-Acquire.
3393      */
3394     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3395               a->rt, iss_sf, true);
3396     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3397     return true;
3398 }
3399 
3400 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3401 {
3402     TCGv_i64 clean_addr, dirty_addr;
3403     MemOp mop = a->sz;
3404     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3405 
3406     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3407         return false;
3408     }
3409 
3410     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3411 
3412     if (a->rn == 31) {
3413         gen_check_sp_alignment(s);
3414     }
3415 
3416     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3417     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3418     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3419     clean_addr = clean_data_tbi(s, dirty_addr);
3420 
3421     /* Store-Release semantics */
3422     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3423     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3424     return true;
3425 }
3426 
3427 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3428 {
3429     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3430     MemOp endian, align, mop;
3431 
3432     int total;    /* total bytes */
3433     int elements; /* elements per vector */
3434     int r;
3435     int size = a->sz;
3436 
3437     if (!a->p && a->rm != 0) {
3438         /* For non-postindexed accesses the Rm field must be 0 */
3439         return false;
3440     }
3441     if (size == 3 && !a->q && a->selem != 1) {
3442         return false;
3443     }
3444     if (!fp_access_check(s)) {
3445         return true;
3446     }
3447 
3448     if (a->rn == 31) {
3449         gen_check_sp_alignment(s);
3450     }
3451 
3452     /* For our purposes, bytes are always little-endian.  */
3453     endian = s->be_data;
3454     if (size == 0) {
3455         endian = MO_LE;
3456     }
3457 
3458     total = a->rpt * a->selem * (a->q ? 16 : 8);
3459     tcg_rn = cpu_reg_sp(s, a->rn);
3460 
3461     /*
3462      * Issue the MTE check vs the logical repeat count, before we
3463      * promote consecutive little-endian elements below.
3464      */
3465     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3466                                 finalize_memop_asimd(s, size));
3467 
3468     /*
3469      * Consecutive little-endian elements from a single register
3470      * can be promoted to a larger little-endian operation.
3471      */
3472     align = MO_ALIGN;
3473     if (a->selem == 1 && endian == MO_LE) {
3474         align = pow2_align(size);
3475         size = 3;
3476     }
3477     if (!s->align_mem) {
3478         align = 0;
3479     }
3480     mop = endian | size | align;
3481 
3482     elements = (a->q ? 16 : 8) >> size;
3483     tcg_ebytes = tcg_constant_i64(1 << size);
3484     for (r = 0; r < a->rpt; r++) {
3485         int e;
3486         for (e = 0; e < elements; e++) {
3487             int xs;
3488             for (xs = 0; xs < a->selem; xs++) {
3489                 int tt = (a->rt + r + xs) % 32;
3490                 do_vec_ld(s, tt, e, clean_addr, mop);
3491                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3492             }
3493         }
3494     }
3495 
3496     /*
3497      * For non-quad operations, setting a slice of the low 64 bits of
3498      * the register clears the high 64 bits (in the ARM ARM pseudocode
3499      * this is implicit in the fact that 'rval' is a 64 bit wide
3500      * variable).  For quad operations, we might still need to zero
3501      * the high bits of SVE.
3502      */
3503     for (r = 0; r < a->rpt * a->selem; r++) {
3504         int tt = (a->rt + r) % 32;
3505         clear_vec_high(s, a->q, tt);
3506     }
3507 
3508     if (a->p) {
3509         if (a->rm == 31) {
3510             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3511         } else {
3512             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3513         }
3514     }
3515     return true;
3516 }
3517 
3518 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3519 {
3520     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3521     MemOp endian, align, mop;
3522 
3523     int total;    /* total bytes */
3524     int elements; /* elements per vector */
3525     int r;
3526     int size = a->sz;
3527 
3528     if (!a->p && a->rm != 0) {
3529         /* For non-postindexed accesses the Rm field must be 0 */
3530         return false;
3531     }
3532     if (size == 3 && !a->q && a->selem != 1) {
3533         return false;
3534     }
3535     if (!fp_access_check(s)) {
3536         return true;
3537     }
3538 
3539     if (a->rn == 31) {
3540         gen_check_sp_alignment(s);
3541     }
3542 
3543     /* For our purposes, bytes are always little-endian.  */
3544     endian = s->be_data;
3545     if (size == 0) {
3546         endian = MO_LE;
3547     }
3548 
3549     total = a->rpt * a->selem * (a->q ? 16 : 8);
3550     tcg_rn = cpu_reg_sp(s, a->rn);
3551 
3552     /*
3553      * Issue the MTE check vs the logical repeat count, before we
3554      * promote consecutive little-endian elements below.
3555      */
3556     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3557                                 finalize_memop_asimd(s, size));
3558 
3559     /*
3560      * Consecutive little-endian elements from a single register
3561      * can be promoted to a larger little-endian operation.
3562      */
3563     align = MO_ALIGN;
3564     if (a->selem == 1 && endian == MO_LE) {
3565         align = pow2_align(size);
3566         size = 3;
3567     }
3568     if (!s->align_mem) {
3569         align = 0;
3570     }
3571     mop = endian | size | align;
3572 
3573     elements = (a->q ? 16 : 8) >> size;
3574     tcg_ebytes = tcg_constant_i64(1 << size);
3575     for (r = 0; r < a->rpt; r++) {
3576         int e;
3577         for (e = 0; e < elements; e++) {
3578             int xs;
3579             for (xs = 0; xs < a->selem; xs++) {
3580                 int tt = (a->rt + r + xs) % 32;
3581                 do_vec_st(s, tt, e, clean_addr, mop);
3582                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3583             }
3584         }
3585     }
3586 
3587     if (a->p) {
3588         if (a->rm == 31) {
3589             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3590         } else {
3591             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3592         }
3593     }
3594     return true;
3595 }
3596 
3597 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3598 {
3599     int xs, total, rt;
3600     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3601     MemOp mop;
3602 
3603     if (!a->p && a->rm != 0) {
3604         return false;
3605     }
3606     if (!fp_access_check(s)) {
3607         return true;
3608     }
3609 
3610     if (a->rn == 31) {
3611         gen_check_sp_alignment(s);
3612     }
3613 
3614     total = a->selem << a->scale;
3615     tcg_rn = cpu_reg_sp(s, a->rn);
3616 
3617     mop = finalize_memop_asimd(s, a->scale);
3618     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3619                                 total, mop);
3620 
3621     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3622     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3623         do_vec_st(s, rt, a->index, clean_addr, mop);
3624         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3625     }
3626 
3627     if (a->p) {
3628         if (a->rm == 31) {
3629             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3630         } else {
3631             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3632         }
3633     }
3634     return true;
3635 }
3636 
3637 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3638 {
3639     int xs, total, rt;
3640     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3641     MemOp mop;
3642 
3643     if (!a->p && a->rm != 0) {
3644         return false;
3645     }
3646     if (!fp_access_check(s)) {
3647         return true;
3648     }
3649 
3650     if (a->rn == 31) {
3651         gen_check_sp_alignment(s);
3652     }
3653 
3654     total = a->selem << a->scale;
3655     tcg_rn = cpu_reg_sp(s, a->rn);
3656 
3657     mop = finalize_memop_asimd(s, a->scale);
3658     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3659                                 total, mop);
3660 
3661     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3662     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3663         do_vec_ld(s, rt, a->index, clean_addr, mop);
3664         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3665     }
3666 
3667     if (a->p) {
3668         if (a->rm == 31) {
3669             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3670         } else {
3671             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3672         }
3673     }
3674     return true;
3675 }
3676 
3677 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3678 {
3679     int xs, total, rt;
3680     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3681     MemOp mop;
3682 
3683     if (!a->p && a->rm != 0) {
3684         return false;
3685     }
3686     if (!fp_access_check(s)) {
3687         return true;
3688     }
3689 
3690     if (a->rn == 31) {
3691         gen_check_sp_alignment(s);
3692     }
3693 
3694     total = a->selem << a->scale;
3695     tcg_rn = cpu_reg_sp(s, a->rn);
3696 
3697     mop = finalize_memop_asimd(s, a->scale);
3698     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3699                                 total, mop);
3700 
3701     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3702     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3703         /* Load and replicate to all elements */
3704         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3705 
3706         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3707         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3708                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3709         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3710     }
3711 
3712     if (a->p) {
3713         if (a->rm == 31) {
3714             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3715         } else {
3716             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3717         }
3718     }
3719     return true;
3720 }
3721 
3722 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3723 {
3724     TCGv_i64 addr, clean_addr, tcg_rt;
3725     int size = 4 << s->dcz_blocksize;
3726 
3727     if (!dc_isar_feature(aa64_mte, s)) {
3728         return false;
3729     }
3730     if (s->current_el == 0) {
3731         return false;
3732     }
3733 
3734     if (a->rn == 31) {
3735         gen_check_sp_alignment(s);
3736     }
3737 
3738     addr = read_cpu_reg_sp(s, a->rn, true);
3739     tcg_gen_addi_i64(addr, addr, a->imm);
3740     tcg_rt = cpu_reg(s, a->rt);
3741 
3742     if (s->ata) {
3743         gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
3744     }
3745     /*
3746      * The non-tags portion of STZGM is mostly like DC_ZVA,
3747      * except the alignment happens before the access.
3748      */
3749     clean_addr = clean_data_tbi(s, addr);
3750     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3751     gen_helper_dc_zva(cpu_env, clean_addr);
3752     return true;
3753 }
3754 
3755 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3756 {
3757     TCGv_i64 addr, clean_addr, tcg_rt;
3758 
3759     if (!dc_isar_feature(aa64_mte, s)) {
3760         return false;
3761     }
3762     if (s->current_el == 0) {
3763         return false;
3764     }
3765 
3766     if (a->rn == 31) {
3767         gen_check_sp_alignment(s);
3768     }
3769 
3770     addr = read_cpu_reg_sp(s, a->rn, true);
3771     tcg_gen_addi_i64(addr, addr, a->imm);
3772     tcg_rt = cpu_reg(s, a->rt);
3773 
3774     if (s->ata) {
3775         gen_helper_stgm(cpu_env, addr, tcg_rt);
3776     } else {
3777         MMUAccessType acc = MMU_DATA_STORE;
3778         int size = 4 << s->gm_blocksize;
3779 
3780         clean_addr = clean_data_tbi(s, addr);
3781         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3782         gen_probe_access(s, clean_addr, acc, size);
3783     }
3784     return true;
3785 }
3786 
3787 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3788 {
3789     TCGv_i64 addr, clean_addr, tcg_rt;
3790 
3791     if (!dc_isar_feature(aa64_mte, s)) {
3792         return false;
3793     }
3794     if (s->current_el == 0) {
3795         return false;
3796     }
3797 
3798     if (a->rn == 31) {
3799         gen_check_sp_alignment(s);
3800     }
3801 
3802     addr = read_cpu_reg_sp(s, a->rn, true);
3803     tcg_gen_addi_i64(addr, addr, a->imm);
3804     tcg_rt = cpu_reg(s, a->rt);
3805 
3806     if (s->ata) {
3807         gen_helper_ldgm(tcg_rt, cpu_env, addr);
3808     } else {
3809         MMUAccessType acc = MMU_DATA_LOAD;
3810         int size = 4 << s->gm_blocksize;
3811 
3812         clean_addr = clean_data_tbi(s, addr);
3813         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3814         gen_probe_access(s, clean_addr, acc, size);
3815         /* The result tags are zeros.  */
3816         tcg_gen_movi_i64(tcg_rt, 0);
3817     }
3818     return true;
3819 }
3820 
3821 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
3822 {
3823     TCGv_i64 addr, clean_addr, tcg_rt;
3824 
3825     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3826         return false;
3827     }
3828 
3829     if (a->rn == 31) {
3830         gen_check_sp_alignment(s);
3831     }
3832 
3833     addr = read_cpu_reg_sp(s, a->rn, true);
3834     if (!a->p) {
3835         /* pre-index or signed offset */
3836         tcg_gen_addi_i64(addr, addr, a->imm);
3837     }
3838 
3839     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
3840     tcg_rt = cpu_reg(s, a->rt);
3841     if (s->ata) {
3842         gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
3843     } else {
3844         /*
3845          * Tag access disabled: we must check for aborts on the load
3846          * load from [rn+offset], and then insert a 0 tag into rt.
3847          */
3848         clean_addr = clean_data_tbi(s, addr);
3849         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
3850         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
3851     }
3852 
3853     if (a->w) {
3854         /* pre-index or post-index */
3855         if (a->p) {
3856             /* post-index */
3857             tcg_gen_addi_i64(addr, addr, a->imm);
3858         }
3859         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3860     }
3861     return true;
3862 }
3863 
3864 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
3865 {
3866     TCGv_i64 addr, tcg_rt;
3867 
3868     if (a->rn == 31) {
3869         gen_check_sp_alignment(s);
3870     }
3871 
3872     addr = read_cpu_reg_sp(s, a->rn, true);
3873     if (!a->p) {
3874         /* pre-index or signed offset */
3875         tcg_gen_addi_i64(addr, addr, a->imm);
3876     }
3877     tcg_rt = cpu_reg_sp(s, a->rt);
3878     if (!s->ata) {
3879         /*
3880          * For STG and ST2G, we need to check alignment and probe memory.
3881          * TODO: For STZG and STZ2G, we could rely on the stores below,
3882          * at least for system mode; user-only won't enforce alignment.
3883          */
3884         if (is_pair) {
3885             gen_helper_st2g_stub(cpu_env, addr);
3886         } else {
3887             gen_helper_stg_stub(cpu_env, addr);
3888         }
3889     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3890         if (is_pair) {
3891             gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
3892         } else {
3893             gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
3894         }
3895     } else {
3896         if (is_pair) {
3897             gen_helper_st2g(cpu_env, addr, tcg_rt);
3898         } else {
3899             gen_helper_stg(cpu_env, addr, tcg_rt);
3900         }
3901     }
3902 
3903     if (is_zero) {
3904         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
3905         TCGv_i64 zero64 = tcg_constant_i64(0);
3906         TCGv_i128 zero128 = tcg_temp_new_i128();
3907         int mem_index = get_mem_index(s);
3908         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
3909 
3910         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
3911 
3912         /* This is 1 or 2 atomic 16-byte operations. */
3913         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3914         if (is_pair) {
3915             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
3916             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3917         }
3918     }
3919 
3920     if (a->w) {
3921         /* pre-index or post-index */
3922         if (a->p) {
3923             /* post-index */
3924             tcg_gen_addi_i64(addr, addr, a->imm);
3925         }
3926         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3927     }
3928     return true;
3929 }
3930 
3931 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
3932 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
3933 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
3934 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
3935 
3936 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
3937 
3938 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
3939                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
3940 {
3941     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
3942     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
3943     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
3944 
3945     fn(tcg_rd, tcg_rn, tcg_imm);
3946     if (!a->sf) {
3947         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3948     }
3949     return true;
3950 }
3951 
3952 /*
3953  * PC-rel. addressing
3954  */
3955 
3956 static bool trans_ADR(DisasContext *s, arg_ri *a)
3957 {
3958     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
3959     return true;
3960 }
3961 
3962 static bool trans_ADRP(DisasContext *s, arg_ri *a)
3963 {
3964     int64_t offset = (int64_t)a->imm << 12;
3965 
3966     /* The page offset is ok for CF_PCREL. */
3967     offset -= s->pc_curr & 0xfff;
3968     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
3969     return true;
3970 }
3971 
3972 /*
3973  * Add/subtract (immediate)
3974  */
3975 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
3976 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
3977 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
3978 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
3979 
3980 /*
3981  * Add/subtract (immediate, with tags)
3982  */
3983 
3984 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
3985                                       bool sub_op)
3986 {
3987     TCGv_i64 tcg_rn, tcg_rd;
3988     int imm;
3989 
3990     imm = a->uimm6 << LOG2_TAG_GRANULE;
3991     if (sub_op) {
3992         imm = -imm;
3993     }
3994 
3995     tcg_rn = cpu_reg_sp(s, a->rn);
3996     tcg_rd = cpu_reg_sp(s, a->rd);
3997 
3998     if (s->ata) {
3999         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4000                            tcg_constant_i32(imm),
4001                            tcg_constant_i32(a->uimm4));
4002     } else {
4003         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4004         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4005     }
4006     return true;
4007 }
4008 
4009 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4010 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4011 
4012 /* The input should be a value in the bottom e bits (with higher
4013  * bits zero); returns that value replicated into every element
4014  * of size e in a 64 bit integer.
4015  */
4016 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4017 {
4018     assert(e != 0);
4019     while (e < 64) {
4020         mask |= mask << e;
4021         e *= 2;
4022     }
4023     return mask;
4024 }
4025 
4026 /*
4027  * Logical (immediate)
4028  */
4029 
4030 /*
4031  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4032  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4033  * value (ie should cause a guest UNDEF exception), and true if they are
4034  * valid, in which case the decoded bit pattern is written to result.
4035  */
4036 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4037                             unsigned int imms, unsigned int immr)
4038 {
4039     uint64_t mask;
4040     unsigned e, levels, s, r;
4041     int len;
4042 
4043     assert(immn < 2 && imms < 64 && immr < 64);
4044 
4045     /* The bit patterns we create here are 64 bit patterns which
4046      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4047      * 64 bits each. Each element contains the same value: a run
4048      * of between 1 and e-1 non-zero bits, rotated within the
4049      * element by between 0 and e-1 bits.
4050      *
4051      * The element size and run length are encoded into immn (1 bit)
4052      * and imms (6 bits) as follows:
4053      * 64 bit elements: immn = 1, imms = <length of run - 1>
4054      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4055      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4056      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4057      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4058      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4059      * Notice that immn = 0, imms = 11111x is the only combination
4060      * not covered by one of the above options; this is reserved.
4061      * Further, <length of run - 1> all-ones is a reserved pattern.
4062      *
4063      * In all cases the rotation is by immr % e (and immr is 6 bits).
4064      */
4065 
4066     /* First determine the element size */
4067     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4068     if (len < 1) {
4069         /* This is the immn == 0, imms == 0x11111x case */
4070         return false;
4071     }
4072     e = 1 << len;
4073 
4074     levels = e - 1;
4075     s = imms & levels;
4076     r = immr & levels;
4077 
4078     if (s == levels) {
4079         /* <length of run - 1> mustn't be all-ones. */
4080         return false;
4081     }
4082 
4083     /* Create the value of one element: s+1 set bits rotated
4084      * by r within the element (which is e bits wide)...
4085      */
4086     mask = MAKE_64BIT_MASK(0, s + 1);
4087     if (r) {
4088         mask = (mask >> r) | (mask << (e - r));
4089         mask &= MAKE_64BIT_MASK(0, e);
4090     }
4091     /* ...then replicate the element over the whole 64 bit value */
4092     mask = bitfield_replicate(mask, e);
4093     *result = mask;
4094     return true;
4095 }
4096 
4097 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4098                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4099 {
4100     TCGv_i64 tcg_rd, tcg_rn;
4101     uint64_t imm;
4102 
4103     /* Some immediate field values are reserved. */
4104     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4105                                 extract32(a->dbm, 0, 6),
4106                                 extract32(a->dbm, 6, 6))) {
4107         return false;
4108     }
4109     if (!a->sf) {
4110         imm &= 0xffffffffull;
4111     }
4112 
4113     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4114     tcg_rn = cpu_reg(s, a->rn);
4115 
4116     fn(tcg_rd, tcg_rn, imm);
4117     if (set_cc) {
4118         gen_logic_CC(a->sf, tcg_rd);
4119     }
4120     if (!a->sf) {
4121         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4122     }
4123     return true;
4124 }
4125 
4126 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4127 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4128 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4129 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4130 
4131 /*
4132  * Move wide (immediate)
4133  */
4134 
4135 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4136 {
4137     int pos = a->hw << 4;
4138     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4139     return true;
4140 }
4141 
4142 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4143 {
4144     int pos = a->hw << 4;
4145     uint64_t imm = a->imm;
4146 
4147     imm = ~(imm << pos);
4148     if (!a->sf) {
4149         imm = (uint32_t)imm;
4150     }
4151     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4152     return true;
4153 }
4154 
4155 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4156 {
4157     int pos = a->hw << 4;
4158     TCGv_i64 tcg_rd, tcg_im;
4159 
4160     tcg_rd = cpu_reg(s, a->rd);
4161     tcg_im = tcg_constant_i64(a->imm);
4162     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4163     if (!a->sf) {
4164         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4165     }
4166     return true;
4167 }
4168 
4169 /*
4170  * Bitfield
4171  */
4172 
4173 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4174 {
4175     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4176     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4177     unsigned int bitsize = a->sf ? 64 : 32;
4178     unsigned int ri = a->immr;
4179     unsigned int si = a->imms;
4180     unsigned int pos, len;
4181 
4182     if (si >= ri) {
4183         /* Wd<s-r:0> = Wn<s:r> */
4184         len = (si - ri) + 1;
4185         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4186         if (!a->sf) {
4187             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4188         }
4189     } else {
4190         /* Wd<32+s-r,32-r> = Wn<s:0> */
4191         len = si + 1;
4192         pos = (bitsize - ri) & (bitsize - 1);
4193 
4194         if (len < ri) {
4195             /*
4196              * Sign extend the destination field from len to fill the
4197              * balance of the word.  Let the deposit below insert all
4198              * of those sign bits.
4199              */
4200             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4201             len = ri;
4202         }
4203 
4204         /*
4205          * We start with zero, and we haven't modified any bits outside
4206          * bitsize, therefore no final zero-extension is unneeded for !sf.
4207          */
4208         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4209     }
4210     return true;
4211 }
4212 
4213 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4214 {
4215     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4216     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4217     unsigned int bitsize = a->sf ? 64 : 32;
4218     unsigned int ri = a->immr;
4219     unsigned int si = a->imms;
4220     unsigned int pos, len;
4221 
4222     tcg_rd = cpu_reg(s, a->rd);
4223     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4224 
4225     if (si >= ri) {
4226         /* Wd<s-r:0> = Wn<s:r> */
4227         len = (si - ri) + 1;
4228         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4229     } else {
4230         /* Wd<32+s-r,32-r> = Wn<s:0> */
4231         len = si + 1;
4232         pos = (bitsize - ri) & (bitsize - 1);
4233         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4234     }
4235     return true;
4236 }
4237 
4238 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4239 {
4240     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4241     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4242     unsigned int bitsize = a->sf ? 64 : 32;
4243     unsigned int ri = a->immr;
4244     unsigned int si = a->imms;
4245     unsigned int pos, len;
4246 
4247     tcg_rd = cpu_reg(s, a->rd);
4248     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4249 
4250     if (si >= ri) {
4251         /* Wd<s-r:0> = Wn<s:r> */
4252         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4253         len = (si - ri) + 1;
4254         pos = 0;
4255     } else {
4256         /* Wd<32+s-r,32-r> = Wn<s:0> */
4257         len = si + 1;
4258         pos = (bitsize - ri) & (bitsize - 1);
4259     }
4260 
4261     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4262     if (!a->sf) {
4263         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4264     }
4265     return true;
4266 }
4267 
4268 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4269 {
4270     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4271 
4272     tcg_rd = cpu_reg(s, a->rd);
4273 
4274     if (unlikely(a->imm == 0)) {
4275         /*
4276          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4277          * so an extract from bit 0 is a special case.
4278          */
4279         if (a->sf) {
4280             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4281         } else {
4282             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4283         }
4284     } else {
4285         tcg_rm = cpu_reg(s, a->rm);
4286         tcg_rn = cpu_reg(s, a->rn);
4287 
4288         if (a->sf) {
4289             /* Specialization to ROR happens in EXTRACT2.  */
4290             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4291         } else {
4292             TCGv_i32 t0 = tcg_temp_new_i32();
4293 
4294             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4295             if (a->rm == a->rn) {
4296                 tcg_gen_rotri_i32(t0, t0, a->imm);
4297             } else {
4298                 TCGv_i32 t1 = tcg_temp_new_i32();
4299                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4300                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4301             }
4302             tcg_gen_extu_i32_i64(tcg_rd, t0);
4303         }
4304     }
4305     return true;
4306 }
4307 
4308 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4309  * Note that it is the caller's responsibility to ensure that the
4310  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4311  * mandated semantics for out of range shifts.
4312  */
4313 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4314                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4315 {
4316     switch (shift_type) {
4317     case A64_SHIFT_TYPE_LSL:
4318         tcg_gen_shl_i64(dst, src, shift_amount);
4319         break;
4320     case A64_SHIFT_TYPE_LSR:
4321         tcg_gen_shr_i64(dst, src, shift_amount);
4322         break;
4323     case A64_SHIFT_TYPE_ASR:
4324         if (!sf) {
4325             tcg_gen_ext32s_i64(dst, src);
4326         }
4327         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4328         break;
4329     case A64_SHIFT_TYPE_ROR:
4330         if (sf) {
4331             tcg_gen_rotr_i64(dst, src, shift_amount);
4332         } else {
4333             TCGv_i32 t0, t1;
4334             t0 = tcg_temp_new_i32();
4335             t1 = tcg_temp_new_i32();
4336             tcg_gen_extrl_i64_i32(t0, src);
4337             tcg_gen_extrl_i64_i32(t1, shift_amount);
4338             tcg_gen_rotr_i32(t0, t0, t1);
4339             tcg_gen_extu_i32_i64(dst, t0);
4340         }
4341         break;
4342     default:
4343         assert(FALSE); /* all shift types should be handled */
4344         break;
4345     }
4346 
4347     if (!sf) { /* zero extend final result */
4348         tcg_gen_ext32u_i64(dst, dst);
4349     }
4350 }
4351 
4352 /* Shift a TCGv src by immediate, put result in dst.
4353  * The shift amount must be in range (this should always be true as the
4354  * relevant instructions will UNDEF on bad shift immediates).
4355  */
4356 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4357                           enum a64_shift_type shift_type, unsigned int shift_i)
4358 {
4359     assert(shift_i < (sf ? 64 : 32));
4360 
4361     if (shift_i == 0) {
4362         tcg_gen_mov_i64(dst, src);
4363     } else {
4364         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4365     }
4366 }
4367 
4368 /* Logical (shifted register)
4369  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4370  * +----+-----+-----------+-------+---+------+--------+------+------+
4371  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4372  * +----+-----+-----------+-------+---+------+--------+------+------+
4373  */
4374 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4375 {
4376     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4377     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4378 
4379     sf = extract32(insn, 31, 1);
4380     opc = extract32(insn, 29, 2);
4381     shift_type = extract32(insn, 22, 2);
4382     invert = extract32(insn, 21, 1);
4383     rm = extract32(insn, 16, 5);
4384     shift_amount = extract32(insn, 10, 6);
4385     rn = extract32(insn, 5, 5);
4386     rd = extract32(insn, 0, 5);
4387 
4388     if (!sf && (shift_amount & (1 << 5))) {
4389         unallocated_encoding(s);
4390         return;
4391     }
4392 
4393     tcg_rd = cpu_reg(s, rd);
4394 
4395     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4396         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4397          * register-register MOV and MVN, so it is worth special casing.
4398          */
4399         tcg_rm = cpu_reg(s, rm);
4400         if (invert) {
4401             tcg_gen_not_i64(tcg_rd, tcg_rm);
4402             if (!sf) {
4403                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4404             }
4405         } else {
4406             if (sf) {
4407                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4408             } else {
4409                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4410             }
4411         }
4412         return;
4413     }
4414 
4415     tcg_rm = read_cpu_reg(s, rm, sf);
4416 
4417     if (shift_amount) {
4418         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4419     }
4420 
4421     tcg_rn = cpu_reg(s, rn);
4422 
4423     switch (opc | (invert << 2)) {
4424     case 0: /* AND */
4425     case 3: /* ANDS */
4426         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4427         break;
4428     case 1: /* ORR */
4429         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4430         break;
4431     case 2: /* EOR */
4432         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4433         break;
4434     case 4: /* BIC */
4435     case 7: /* BICS */
4436         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4437         break;
4438     case 5: /* ORN */
4439         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4440         break;
4441     case 6: /* EON */
4442         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4443         break;
4444     default:
4445         assert(FALSE);
4446         break;
4447     }
4448 
4449     if (!sf) {
4450         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4451     }
4452 
4453     if (opc == 3) {
4454         gen_logic_CC(sf, tcg_rd);
4455     }
4456 }
4457 
4458 /*
4459  * Add/subtract (extended register)
4460  *
4461  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4462  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4463  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4464  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4465  *
4466  *  sf: 0 -> 32bit, 1 -> 64bit
4467  *  op: 0 -> add  , 1 -> sub
4468  *   S: 1 -> set flags
4469  * opt: 00
4470  * option: extension type (see DecodeRegExtend)
4471  * imm3: optional shift to Rm
4472  *
4473  * Rd = Rn + LSL(extend(Rm), amount)
4474  */
4475 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4476 {
4477     int rd = extract32(insn, 0, 5);
4478     int rn = extract32(insn, 5, 5);
4479     int imm3 = extract32(insn, 10, 3);
4480     int option = extract32(insn, 13, 3);
4481     int rm = extract32(insn, 16, 5);
4482     int opt = extract32(insn, 22, 2);
4483     bool setflags = extract32(insn, 29, 1);
4484     bool sub_op = extract32(insn, 30, 1);
4485     bool sf = extract32(insn, 31, 1);
4486 
4487     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4488     TCGv_i64 tcg_rd;
4489     TCGv_i64 tcg_result;
4490 
4491     if (imm3 > 4 || opt != 0) {
4492         unallocated_encoding(s);
4493         return;
4494     }
4495 
4496     /* non-flag setting ops may use SP */
4497     if (!setflags) {
4498         tcg_rd = cpu_reg_sp(s, rd);
4499     } else {
4500         tcg_rd = cpu_reg(s, rd);
4501     }
4502     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4503 
4504     tcg_rm = read_cpu_reg(s, rm, sf);
4505     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4506 
4507     tcg_result = tcg_temp_new_i64();
4508 
4509     if (!setflags) {
4510         if (sub_op) {
4511             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4512         } else {
4513             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4514         }
4515     } else {
4516         if (sub_op) {
4517             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4518         } else {
4519             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4520         }
4521     }
4522 
4523     if (sf) {
4524         tcg_gen_mov_i64(tcg_rd, tcg_result);
4525     } else {
4526         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4527     }
4528 }
4529 
4530 /*
4531  * Add/subtract (shifted register)
4532  *
4533  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4534  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4535  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4536  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4537  *
4538  *    sf: 0 -> 32bit, 1 -> 64bit
4539  *    op: 0 -> add  , 1 -> sub
4540  *     S: 1 -> set flags
4541  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4542  *  imm6: Shift amount to apply to Rm before the add/sub
4543  */
4544 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4545 {
4546     int rd = extract32(insn, 0, 5);
4547     int rn = extract32(insn, 5, 5);
4548     int imm6 = extract32(insn, 10, 6);
4549     int rm = extract32(insn, 16, 5);
4550     int shift_type = extract32(insn, 22, 2);
4551     bool setflags = extract32(insn, 29, 1);
4552     bool sub_op = extract32(insn, 30, 1);
4553     bool sf = extract32(insn, 31, 1);
4554 
4555     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4556     TCGv_i64 tcg_rn, tcg_rm;
4557     TCGv_i64 tcg_result;
4558 
4559     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4560         unallocated_encoding(s);
4561         return;
4562     }
4563 
4564     tcg_rn = read_cpu_reg(s, rn, sf);
4565     tcg_rm = read_cpu_reg(s, rm, sf);
4566 
4567     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4568 
4569     tcg_result = tcg_temp_new_i64();
4570 
4571     if (!setflags) {
4572         if (sub_op) {
4573             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4574         } else {
4575             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4576         }
4577     } else {
4578         if (sub_op) {
4579             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4580         } else {
4581             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4582         }
4583     }
4584 
4585     if (sf) {
4586         tcg_gen_mov_i64(tcg_rd, tcg_result);
4587     } else {
4588         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4589     }
4590 }
4591 
4592 /* Data-processing (3 source)
4593  *
4594  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4595  *  +--+------+-----------+------+------+----+------+------+------+
4596  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4597  *  +--+------+-----------+------+------+----+------+------+------+
4598  */
4599 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4600 {
4601     int rd = extract32(insn, 0, 5);
4602     int rn = extract32(insn, 5, 5);
4603     int ra = extract32(insn, 10, 5);
4604     int rm = extract32(insn, 16, 5);
4605     int op_id = (extract32(insn, 29, 3) << 4) |
4606         (extract32(insn, 21, 3) << 1) |
4607         extract32(insn, 15, 1);
4608     bool sf = extract32(insn, 31, 1);
4609     bool is_sub = extract32(op_id, 0, 1);
4610     bool is_high = extract32(op_id, 2, 1);
4611     bool is_signed = false;
4612     TCGv_i64 tcg_op1;
4613     TCGv_i64 tcg_op2;
4614     TCGv_i64 tcg_tmp;
4615 
4616     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4617     switch (op_id) {
4618     case 0x42: /* SMADDL */
4619     case 0x43: /* SMSUBL */
4620     case 0x44: /* SMULH */
4621         is_signed = true;
4622         break;
4623     case 0x0: /* MADD (32bit) */
4624     case 0x1: /* MSUB (32bit) */
4625     case 0x40: /* MADD (64bit) */
4626     case 0x41: /* MSUB (64bit) */
4627     case 0x4a: /* UMADDL */
4628     case 0x4b: /* UMSUBL */
4629     case 0x4c: /* UMULH */
4630         break;
4631     default:
4632         unallocated_encoding(s);
4633         return;
4634     }
4635 
4636     if (is_high) {
4637         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4638         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4639         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4640         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4641 
4642         if (is_signed) {
4643             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4644         } else {
4645             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4646         }
4647         return;
4648     }
4649 
4650     tcg_op1 = tcg_temp_new_i64();
4651     tcg_op2 = tcg_temp_new_i64();
4652     tcg_tmp = tcg_temp_new_i64();
4653 
4654     if (op_id < 0x42) {
4655         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4656         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4657     } else {
4658         if (is_signed) {
4659             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4660             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4661         } else {
4662             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4663             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4664         }
4665     }
4666 
4667     if (ra == 31 && !is_sub) {
4668         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4669         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4670     } else {
4671         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4672         if (is_sub) {
4673             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4674         } else {
4675             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4676         }
4677     }
4678 
4679     if (!sf) {
4680         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4681     }
4682 }
4683 
4684 /* Add/subtract (with carry)
4685  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4686  * +--+--+--+------------------------+------+-------------+------+-----+
4687  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4688  * +--+--+--+------------------------+------+-------------+------+-----+
4689  */
4690 
4691 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4692 {
4693     unsigned int sf, op, setflags, rm, rn, rd;
4694     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4695 
4696     sf = extract32(insn, 31, 1);
4697     op = extract32(insn, 30, 1);
4698     setflags = extract32(insn, 29, 1);
4699     rm = extract32(insn, 16, 5);
4700     rn = extract32(insn, 5, 5);
4701     rd = extract32(insn, 0, 5);
4702 
4703     tcg_rd = cpu_reg(s, rd);
4704     tcg_rn = cpu_reg(s, rn);
4705 
4706     if (op) {
4707         tcg_y = tcg_temp_new_i64();
4708         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4709     } else {
4710         tcg_y = cpu_reg(s, rm);
4711     }
4712 
4713     if (setflags) {
4714         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4715     } else {
4716         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4717     }
4718 }
4719 
4720 /*
4721  * Rotate right into flags
4722  *  31 30 29                21       15          10      5  4      0
4723  * +--+--+--+-----------------+--------+-----------+------+--+------+
4724  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4725  * +--+--+--+-----------------+--------+-----------+------+--+------+
4726  */
4727 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4728 {
4729     int mask = extract32(insn, 0, 4);
4730     int o2 = extract32(insn, 4, 1);
4731     int rn = extract32(insn, 5, 5);
4732     int imm6 = extract32(insn, 15, 6);
4733     int sf_op_s = extract32(insn, 29, 3);
4734     TCGv_i64 tcg_rn;
4735     TCGv_i32 nzcv;
4736 
4737     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4738         unallocated_encoding(s);
4739         return;
4740     }
4741 
4742     tcg_rn = read_cpu_reg(s, rn, 1);
4743     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4744 
4745     nzcv = tcg_temp_new_i32();
4746     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4747 
4748     if (mask & 8) { /* N */
4749         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4750     }
4751     if (mask & 4) { /* Z */
4752         tcg_gen_not_i32(cpu_ZF, nzcv);
4753         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4754     }
4755     if (mask & 2) { /* C */
4756         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4757     }
4758     if (mask & 1) { /* V */
4759         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4760     }
4761 }
4762 
4763 /*
4764  * Evaluate into flags
4765  *  31 30 29                21        15   14        10      5  4      0
4766  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4767  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
4768  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4769  */
4770 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4771 {
4772     int o3_mask = extract32(insn, 0, 5);
4773     int rn = extract32(insn, 5, 5);
4774     int o2 = extract32(insn, 15, 6);
4775     int sz = extract32(insn, 14, 1);
4776     int sf_op_s = extract32(insn, 29, 3);
4777     TCGv_i32 tmp;
4778     int shift;
4779 
4780     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4781         !dc_isar_feature(aa64_condm_4, s)) {
4782         unallocated_encoding(s);
4783         return;
4784     }
4785     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
4786 
4787     tmp = tcg_temp_new_i32();
4788     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4789     tcg_gen_shli_i32(cpu_NF, tmp, shift);
4790     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4791     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4792     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4793 }
4794 
4795 /* Conditional compare (immediate / register)
4796  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
4797  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4798  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
4799  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4800  *        [1]                             y                [0]       [0]
4801  */
4802 static void disas_cc(DisasContext *s, uint32_t insn)
4803 {
4804     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4805     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4806     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4807     DisasCompare c;
4808 
4809     if (!extract32(insn, 29, 1)) {
4810         unallocated_encoding(s);
4811         return;
4812     }
4813     if (insn & (1 << 10 | 1 << 4)) {
4814         unallocated_encoding(s);
4815         return;
4816     }
4817     sf = extract32(insn, 31, 1);
4818     op = extract32(insn, 30, 1);
4819     is_imm = extract32(insn, 11, 1);
4820     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4821     cond = extract32(insn, 12, 4);
4822     rn = extract32(insn, 5, 5);
4823     nzcv = extract32(insn, 0, 4);
4824 
4825     /* Set T0 = !COND.  */
4826     tcg_t0 = tcg_temp_new_i32();
4827     arm_test_cc(&c, cond);
4828     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4829 
4830     /* Load the arguments for the new comparison.  */
4831     if (is_imm) {
4832         tcg_y = tcg_temp_new_i64();
4833         tcg_gen_movi_i64(tcg_y, y);
4834     } else {
4835         tcg_y = cpu_reg(s, y);
4836     }
4837     tcg_rn = cpu_reg(s, rn);
4838 
4839     /* Set the flags for the new comparison.  */
4840     tcg_tmp = tcg_temp_new_i64();
4841     if (op) {
4842         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4843     } else {
4844         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4845     }
4846 
4847     /* If COND was false, force the flags to #nzcv.  Compute two masks
4848      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4849      * For tcg hosts that support ANDC, we can make do with just T1.
4850      * In either case, allow the tcg optimizer to delete any unused mask.
4851      */
4852     tcg_t1 = tcg_temp_new_i32();
4853     tcg_t2 = tcg_temp_new_i32();
4854     tcg_gen_neg_i32(tcg_t1, tcg_t0);
4855     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4856 
4857     if (nzcv & 8) { /* N */
4858         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4859     } else {
4860         if (TCG_TARGET_HAS_andc_i32) {
4861             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4862         } else {
4863             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4864         }
4865     }
4866     if (nzcv & 4) { /* Z */
4867         if (TCG_TARGET_HAS_andc_i32) {
4868             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4869         } else {
4870             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4871         }
4872     } else {
4873         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4874     }
4875     if (nzcv & 2) { /* C */
4876         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4877     } else {
4878         if (TCG_TARGET_HAS_andc_i32) {
4879             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4880         } else {
4881             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4882         }
4883     }
4884     if (nzcv & 1) { /* V */
4885         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4886     } else {
4887         if (TCG_TARGET_HAS_andc_i32) {
4888             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4889         } else {
4890             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4891         }
4892     }
4893 }
4894 
4895 /* Conditional select
4896  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
4897  * +----+----+---+-----------------+------+------+-----+------+------+
4898  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
4899  * +----+----+---+-----------------+------+------+-----+------+------+
4900  */
4901 static void disas_cond_select(DisasContext *s, uint32_t insn)
4902 {
4903     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4904     TCGv_i64 tcg_rd, zero;
4905     DisasCompare64 c;
4906 
4907     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4908         /* S == 1 or op2<1> == 1 */
4909         unallocated_encoding(s);
4910         return;
4911     }
4912     sf = extract32(insn, 31, 1);
4913     else_inv = extract32(insn, 30, 1);
4914     rm = extract32(insn, 16, 5);
4915     cond = extract32(insn, 12, 4);
4916     else_inc = extract32(insn, 10, 1);
4917     rn = extract32(insn, 5, 5);
4918     rd = extract32(insn, 0, 5);
4919 
4920     tcg_rd = cpu_reg(s, rd);
4921 
4922     a64_test_cc(&c, cond);
4923     zero = tcg_constant_i64(0);
4924 
4925     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4926         /* CSET & CSETM.  */
4927         if (else_inv) {
4928             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
4929                                    tcg_rd, c.value, zero);
4930         } else {
4931             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
4932                                 tcg_rd, c.value, zero);
4933         }
4934     } else {
4935         TCGv_i64 t_true = cpu_reg(s, rn);
4936         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4937         if (else_inv && else_inc) {
4938             tcg_gen_neg_i64(t_false, t_false);
4939         } else if (else_inv) {
4940             tcg_gen_not_i64(t_false, t_false);
4941         } else if (else_inc) {
4942             tcg_gen_addi_i64(t_false, t_false, 1);
4943         }
4944         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4945     }
4946 
4947     if (!sf) {
4948         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4949     }
4950 }
4951 
4952 static void handle_clz(DisasContext *s, unsigned int sf,
4953                        unsigned int rn, unsigned int rd)
4954 {
4955     TCGv_i64 tcg_rd, tcg_rn;
4956     tcg_rd = cpu_reg(s, rd);
4957     tcg_rn = cpu_reg(s, rn);
4958 
4959     if (sf) {
4960         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4961     } else {
4962         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4963         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4964         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4965         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4966     }
4967 }
4968 
4969 static void handle_cls(DisasContext *s, unsigned int sf,
4970                        unsigned int rn, unsigned int rd)
4971 {
4972     TCGv_i64 tcg_rd, tcg_rn;
4973     tcg_rd = cpu_reg(s, rd);
4974     tcg_rn = cpu_reg(s, rn);
4975 
4976     if (sf) {
4977         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4978     } else {
4979         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4980         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4981         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4982         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4983     }
4984 }
4985 
4986 static void handle_rbit(DisasContext *s, unsigned int sf,
4987                         unsigned int rn, unsigned int rd)
4988 {
4989     TCGv_i64 tcg_rd, tcg_rn;
4990     tcg_rd = cpu_reg(s, rd);
4991     tcg_rn = cpu_reg(s, rn);
4992 
4993     if (sf) {
4994         gen_helper_rbit64(tcg_rd, tcg_rn);
4995     } else {
4996         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4997         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4998         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4999         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5000     }
5001 }
5002 
5003 /* REV with sf==1, opcode==3 ("REV64") */
5004 static void handle_rev64(DisasContext *s, unsigned int sf,
5005                          unsigned int rn, unsigned int rd)
5006 {
5007     if (!sf) {
5008         unallocated_encoding(s);
5009         return;
5010     }
5011     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5012 }
5013 
5014 /* REV with sf==0, opcode==2
5015  * REV32 (sf==1, opcode==2)
5016  */
5017 static void handle_rev32(DisasContext *s, unsigned int sf,
5018                          unsigned int rn, unsigned int rd)
5019 {
5020     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5021     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5022 
5023     if (sf) {
5024         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5025         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5026     } else {
5027         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5028     }
5029 }
5030 
5031 /* REV16 (opcode==1) */
5032 static void handle_rev16(DisasContext *s, unsigned int sf,
5033                          unsigned int rn, unsigned int rd)
5034 {
5035     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5036     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5037     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5038     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5039 
5040     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5041     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5042     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5043     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5044     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5045 }
5046 
5047 /* Data-processing (1 source)
5048  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5049  * +----+---+---+-----------------+---------+--------+------+------+
5050  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5051  * +----+---+---+-----------------+---------+--------+------+------+
5052  */
5053 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5054 {
5055     unsigned int sf, opcode, opcode2, rn, rd;
5056     TCGv_i64 tcg_rd;
5057 
5058     if (extract32(insn, 29, 1)) {
5059         unallocated_encoding(s);
5060         return;
5061     }
5062 
5063     sf = extract32(insn, 31, 1);
5064     opcode = extract32(insn, 10, 6);
5065     opcode2 = extract32(insn, 16, 5);
5066     rn = extract32(insn, 5, 5);
5067     rd = extract32(insn, 0, 5);
5068 
5069 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5070 
5071     switch (MAP(sf, opcode2, opcode)) {
5072     case MAP(0, 0x00, 0x00): /* RBIT */
5073     case MAP(1, 0x00, 0x00):
5074         handle_rbit(s, sf, rn, rd);
5075         break;
5076     case MAP(0, 0x00, 0x01): /* REV16 */
5077     case MAP(1, 0x00, 0x01):
5078         handle_rev16(s, sf, rn, rd);
5079         break;
5080     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5081     case MAP(1, 0x00, 0x02):
5082         handle_rev32(s, sf, rn, rd);
5083         break;
5084     case MAP(1, 0x00, 0x03): /* REV64 */
5085         handle_rev64(s, sf, rn, rd);
5086         break;
5087     case MAP(0, 0x00, 0x04): /* CLZ */
5088     case MAP(1, 0x00, 0x04):
5089         handle_clz(s, sf, rn, rd);
5090         break;
5091     case MAP(0, 0x00, 0x05): /* CLS */
5092     case MAP(1, 0x00, 0x05):
5093         handle_cls(s, sf, rn, rd);
5094         break;
5095     case MAP(1, 0x01, 0x00): /* PACIA */
5096         if (s->pauth_active) {
5097             tcg_rd = cpu_reg(s, rd);
5098             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5099         } else if (!dc_isar_feature(aa64_pauth, s)) {
5100             goto do_unallocated;
5101         }
5102         break;
5103     case MAP(1, 0x01, 0x01): /* PACIB */
5104         if (s->pauth_active) {
5105             tcg_rd = cpu_reg(s, rd);
5106             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5107         } else if (!dc_isar_feature(aa64_pauth, s)) {
5108             goto do_unallocated;
5109         }
5110         break;
5111     case MAP(1, 0x01, 0x02): /* PACDA */
5112         if (s->pauth_active) {
5113             tcg_rd = cpu_reg(s, rd);
5114             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5115         } else if (!dc_isar_feature(aa64_pauth, s)) {
5116             goto do_unallocated;
5117         }
5118         break;
5119     case MAP(1, 0x01, 0x03): /* PACDB */
5120         if (s->pauth_active) {
5121             tcg_rd = cpu_reg(s, rd);
5122             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5123         } else if (!dc_isar_feature(aa64_pauth, s)) {
5124             goto do_unallocated;
5125         }
5126         break;
5127     case MAP(1, 0x01, 0x04): /* AUTIA */
5128         if (s->pauth_active) {
5129             tcg_rd = cpu_reg(s, rd);
5130             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5131         } else if (!dc_isar_feature(aa64_pauth, s)) {
5132             goto do_unallocated;
5133         }
5134         break;
5135     case MAP(1, 0x01, 0x05): /* AUTIB */
5136         if (s->pauth_active) {
5137             tcg_rd = cpu_reg(s, rd);
5138             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5139         } else if (!dc_isar_feature(aa64_pauth, s)) {
5140             goto do_unallocated;
5141         }
5142         break;
5143     case MAP(1, 0x01, 0x06): /* AUTDA */
5144         if (s->pauth_active) {
5145             tcg_rd = cpu_reg(s, rd);
5146             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5147         } else if (!dc_isar_feature(aa64_pauth, s)) {
5148             goto do_unallocated;
5149         }
5150         break;
5151     case MAP(1, 0x01, 0x07): /* AUTDB */
5152         if (s->pauth_active) {
5153             tcg_rd = cpu_reg(s, rd);
5154             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5155         } else if (!dc_isar_feature(aa64_pauth, s)) {
5156             goto do_unallocated;
5157         }
5158         break;
5159     case MAP(1, 0x01, 0x08): /* PACIZA */
5160         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5161             goto do_unallocated;
5162         } else if (s->pauth_active) {
5163             tcg_rd = cpu_reg(s, rd);
5164             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5165         }
5166         break;
5167     case MAP(1, 0x01, 0x09): /* PACIZB */
5168         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5169             goto do_unallocated;
5170         } else if (s->pauth_active) {
5171             tcg_rd = cpu_reg(s, rd);
5172             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5173         }
5174         break;
5175     case MAP(1, 0x01, 0x0a): /* PACDZA */
5176         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5177             goto do_unallocated;
5178         } else if (s->pauth_active) {
5179             tcg_rd = cpu_reg(s, rd);
5180             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5181         }
5182         break;
5183     case MAP(1, 0x01, 0x0b): /* PACDZB */
5184         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5185             goto do_unallocated;
5186         } else if (s->pauth_active) {
5187             tcg_rd = cpu_reg(s, rd);
5188             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5189         }
5190         break;
5191     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5192         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5193             goto do_unallocated;
5194         } else if (s->pauth_active) {
5195             tcg_rd = cpu_reg(s, rd);
5196             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5197         }
5198         break;
5199     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5200         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5201             goto do_unallocated;
5202         } else if (s->pauth_active) {
5203             tcg_rd = cpu_reg(s, rd);
5204             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5205         }
5206         break;
5207     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5208         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5209             goto do_unallocated;
5210         } else if (s->pauth_active) {
5211             tcg_rd = cpu_reg(s, rd);
5212             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5213         }
5214         break;
5215     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5216         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5217             goto do_unallocated;
5218         } else if (s->pauth_active) {
5219             tcg_rd = cpu_reg(s, rd);
5220             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5221         }
5222         break;
5223     case MAP(1, 0x01, 0x10): /* XPACI */
5224         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5225             goto do_unallocated;
5226         } else if (s->pauth_active) {
5227             tcg_rd = cpu_reg(s, rd);
5228             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5229         }
5230         break;
5231     case MAP(1, 0x01, 0x11): /* XPACD */
5232         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5233             goto do_unallocated;
5234         } else if (s->pauth_active) {
5235             tcg_rd = cpu_reg(s, rd);
5236             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5237         }
5238         break;
5239     default:
5240     do_unallocated:
5241         unallocated_encoding(s);
5242         break;
5243     }
5244 
5245 #undef MAP
5246 }
5247 
5248 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5249                        unsigned int rm, unsigned int rn, unsigned int rd)
5250 {
5251     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5252     tcg_rd = cpu_reg(s, rd);
5253 
5254     if (!sf && is_signed) {
5255         tcg_n = tcg_temp_new_i64();
5256         tcg_m = tcg_temp_new_i64();
5257         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5258         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5259     } else {
5260         tcg_n = read_cpu_reg(s, rn, sf);
5261         tcg_m = read_cpu_reg(s, rm, sf);
5262     }
5263 
5264     if (is_signed) {
5265         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5266     } else {
5267         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5268     }
5269 
5270     if (!sf) { /* zero extend final result */
5271         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5272     }
5273 }
5274 
5275 /* LSLV, LSRV, ASRV, RORV */
5276 static void handle_shift_reg(DisasContext *s,
5277                              enum a64_shift_type shift_type, unsigned int sf,
5278                              unsigned int rm, unsigned int rn, unsigned int rd)
5279 {
5280     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5281     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5282     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5283 
5284     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5285     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5286 }
5287 
5288 /* CRC32[BHWX], CRC32C[BHWX] */
5289 static void handle_crc32(DisasContext *s,
5290                          unsigned int sf, unsigned int sz, bool crc32c,
5291                          unsigned int rm, unsigned int rn, unsigned int rd)
5292 {
5293     TCGv_i64 tcg_acc, tcg_val;
5294     TCGv_i32 tcg_bytes;
5295 
5296     if (!dc_isar_feature(aa64_crc32, s)
5297         || (sf == 1 && sz != 3)
5298         || (sf == 0 && sz == 3)) {
5299         unallocated_encoding(s);
5300         return;
5301     }
5302 
5303     if (sz == 3) {
5304         tcg_val = cpu_reg(s, rm);
5305     } else {
5306         uint64_t mask;
5307         switch (sz) {
5308         case 0:
5309             mask = 0xFF;
5310             break;
5311         case 1:
5312             mask = 0xFFFF;
5313             break;
5314         case 2:
5315             mask = 0xFFFFFFFF;
5316             break;
5317         default:
5318             g_assert_not_reached();
5319         }
5320         tcg_val = tcg_temp_new_i64();
5321         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5322     }
5323 
5324     tcg_acc = cpu_reg(s, rn);
5325     tcg_bytes = tcg_constant_i32(1 << sz);
5326 
5327     if (crc32c) {
5328         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5329     } else {
5330         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5331     }
5332 }
5333 
5334 /* Data-processing (2 source)
5335  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5336  * +----+---+---+-----------------+------+--------+------+------+
5337  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5338  * +----+---+---+-----------------+------+--------+------+------+
5339  */
5340 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5341 {
5342     unsigned int sf, rm, opcode, rn, rd, setflag;
5343     sf = extract32(insn, 31, 1);
5344     setflag = extract32(insn, 29, 1);
5345     rm = extract32(insn, 16, 5);
5346     opcode = extract32(insn, 10, 6);
5347     rn = extract32(insn, 5, 5);
5348     rd = extract32(insn, 0, 5);
5349 
5350     if (setflag && opcode != 0) {
5351         unallocated_encoding(s);
5352         return;
5353     }
5354 
5355     switch (opcode) {
5356     case 0: /* SUBP(S) */
5357         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5358             goto do_unallocated;
5359         } else {
5360             TCGv_i64 tcg_n, tcg_m, tcg_d;
5361 
5362             tcg_n = read_cpu_reg_sp(s, rn, true);
5363             tcg_m = read_cpu_reg_sp(s, rm, true);
5364             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5365             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5366             tcg_d = cpu_reg(s, rd);
5367 
5368             if (setflag) {
5369                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5370             } else {
5371                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5372             }
5373         }
5374         break;
5375     case 2: /* UDIV */
5376         handle_div(s, false, sf, rm, rn, rd);
5377         break;
5378     case 3: /* SDIV */
5379         handle_div(s, true, sf, rm, rn, rd);
5380         break;
5381     case 4: /* IRG */
5382         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5383             goto do_unallocated;
5384         }
5385         if (s->ata) {
5386             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5387                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5388         } else {
5389             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5390                                              cpu_reg_sp(s, rn));
5391         }
5392         break;
5393     case 5: /* GMI */
5394         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5395             goto do_unallocated;
5396         } else {
5397             TCGv_i64 t = tcg_temp_new_i64();
5398 
5399             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5400             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5401             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5402         }
5403         break;
5404     case 8: /* LSLV */
5405         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5406         break;
5407     case 9: /* LSRV */
5408         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5409         break;
5410     case 10: /* ASRV */
5411         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5412         break;
5413     case 11: /* RORV */
5414         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5415         break;
5416     case 12: /* PACGA */
5417         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5418             goto do_unallocated;
5419         }
5420         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5421                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5422         break;
5423     case 16:
5424     case 17:
5425     case 18:
5426     case 19:
5427     case 20:
5428     case 21:
5429     case 22:
5430     case 23: /* CRC32 */
5431     {
5432         int sz = extract32(opcode, 0, 2);
5433         bool crc32c = extract32(opcode, 2, 1);
5434         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5435         break;
5436     }
5437     default:
5438     do_unallocated:
5439         unallocated_encoding(s);
5440         break;
5441     }
5442 }
5443 
5444 /*
5445  * Data processing - register
5446  *  31  30 29  28      25    21  20  16      10         0
5447  * +--+---+--+---+-------+-----+-------+-------+---------+
5448  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5449  * +--+---+--+---+-------+-----+-------+-------+---------+
5450  */
5451 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5452 {
5453     int op0 = extract32(insn, 30, 1);
5454     int op1 = extract32(insn, 28, 1);
5455     int op2 = extract32(insn, 21, 4);
5456     int op3 = extract32(insn, 10, 6);
5457 
5458     if (!op1) {
5459         if (op2 & 8) {
5460             if (op2 & 1) {
5461                 /* Add/sub (extended register) */
5462                 disas_add_sub_ext_reg(s, insn);
5463             } else {
5464                 /* Add/sub (shifted register) */
5465                 disas_add_sub_reg(s, insn);
5466             }
5467         } else {
5468             /* Logical (shifted register) */
5469             disas_logic_reg(s, insn);
5470         }
5471         return;
5472     }
5473 
5474     switch (op2) {
5475     case 0x0:
5476         switch (op3) {
5477         case 0x00: /* Add/subtract (with carry) */
5478             disas_adc_sbc(s, insn);
5479             break;
5480 
5481         case 0x01: /* Rotate right into flags */
5482         case 0x21:
5483             disas_rotate_right_into_flags(s, insn);
5484             break;
5485 
5486         case 0x02: /* Evaluate into flags */
5487         case 0x12:
5488         case 0x22:
5489         case 0x32:
5490             disas_evaluate_into_flags(s, insn);
5491             break;
5492 
5493         default:
5494             goto do_unallocated;
5495         }
5496         break;
5497 
5498     case 0x2: /* Conditional compare */
5499         disas_cc(s, insn); /* both imm and reg forms */
5500         break;
5501 
5502     case 0x4: /* Conditional select */
5503         disas_cond_select(s, insn);
5504         break;
5505 
5506     case 0x6: /* Data-processing */
5507         if (op0) {    /* (1 source) */
5508             disas_data_proc_1src(s, insn);
5509         } else {      /* (2 source) */
5510             disas_data_proc_2src(s, insn);
5511         }
5512         break;
5513     case 0x8 ... 0xf: /* (3 source) */
5514         disas_data_proc_3src(s, insn);
5515         break;
5516 
5517     default:
5518     do_unallocated:
5519         unallocated_encoding(s);
5520         break;
5521     }
5522 }
5523 
5524 static void handle_fp_compare(DisasContext *s, int size,
5525                               unsigned int rn, unsigned int rm,
5526                               bool cmp_with_zero, bool signal_all_nans)
5527 {
5528     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5529     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5530 
5531     if (size == MO_64) {
5532         TCGv_i64 tcg_vn, tcg_vm;
5533 
5534         tcg_vn = read_fp_dreg(s, rn);
5535         if (cmp_with_zero) {
5536             tcg_vm = tcg_constant_i64(0);
5537         } else {
5538             tcg_vm = read_fp_dreg(s, rm);
5539         }
5540         if (signal_all_nans) {
5541             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5542         } else {
5543             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5544         }
5545     } else {
5546         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5547         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5548 
5549         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5550         if (cmp_with_zero) {
5551             tcg_gen_movi_i32(tcg_vm, 0);
5552         } else {
5553             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5554         }
5555 
5556         switch (size) {
5557         case MO_32:
5558             if (signal_all_nans) {
5559                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5560             } else {
5561                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5562             }
5563             break;
5564         case MO_16:
5565             if (signal_all_nans) {
5566                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5567             } else {
5568                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5569             }
5570             break;
5571         default:
5572             g_assert_not_reached();
5573         }
5574     }
5575 
5576     gen_set_nzcv(tcg_flags);
5577 }
5578 
5579 /* Floating point compare
5580  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5581  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5582  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5583  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5584  */
5585 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5586 {
5587     unsigned int mos, type, rm, op, rn, opc, op2r;
5588     int size;
5589 
5590     mos = extract32(insn, 29, 3);
5591     type = extract32(insn, 22, 2);
5592     rm = extract32(insn, 16, 5);
5593     op = extract32(insn, 14, 2);
5594     rn = extract32(insn, 5, 5);
5595     opc = extract32(insn, 3, 2);
5596     op2r = extract32(insn, 0, 3);
5597 
5598     if (mos || op || op2r) {
5599         unallocated_encoding(s);
5600         return;
5601     }
5602 
5603     switch (type) {
5604     case 0:
5605         size = MO_32;
5606         break;
5607     case 1:
5608         size = MO_64;
5609         break;
5610     case 3:
5611         size = MO_16;
5612         if (dc_isar_feature(aa64_fp16, s)) {
5613             break;
5614         }
5615         /* fallthru */
5616     default:
5617         unallocated_encoding(s);
5618         return;
5619     }
5620 
5621     if (!fp_access_check(s)) {
5622         return;
5623     }
5624 
5625     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5626 }
5627 
5628 /* Floating point conditional compare
5629  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5630  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5631  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5632  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5633  */
5634 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5635 {
5636     unsigned int mos, type, rm, cond, rn, op, nzcv;
5637     TCGLabel *label_continue = NULL;
5638     int size;
5639 
5640     mos = extract32(insn, 29, 3);
5641     type = extract32(insn, 22, 2);
5642     rm = extract32(insn, 16, 5);
5643     cond = extract32(insn, 12, 4);
5644     rn = extract32(insn, 5, 5);
5645     op = extract32(insn, 4, 1);
5646     nzcv = extract32(insn, 0, 4);
5647 
5648     if (mos) {
5649         unallocated_encoding(s);
5650         return;
5651     }
5652 
5653     switch (type) {
5654     case 0:
5655         size = MO_32;
5656         break;
5657     case 1:
5658         size = MO_64;
5659         break;
5660     case 3:
5661         size = MO_16;
5662         if (dc_isar_feature(aa64_fp16, s)) {
5663             break;
5664         }
5665         /* fallthru */
5666     default:
5667         unallocated_encoding(s);
5668         return;
5669     }
5670 
5671     if (!fp_access_check(s)) {
5672         return;
5673     }
5674 
5675     if (cond < 0x0e) { /* not always */
5676         TCGLabel *label_match = gen_new_label();
5677         label_continue = gen_new_label();
5678         arm_gen_test_cc(cond, label_match);
5679         /* nomatch: */
5680         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5681         tcg_gen_br(label_continue);
5682         gen_set_label(label_match);
5683     }
5684 
5685     handle_fp_compare(s, size, rn, rm, false, op);
5686 
5687     if (cond < 0x0e) {
5688         gen_set_label(label_continue);
5689     }
5690 }
5691 
5692 /* Floating point conditional select
5693  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5694  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5695  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5696  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5697  */
5698 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5699 {
5700     unsigned int mos, type, rm, cond, rn, rd;
5701     TCGv_i64 t_true, t_false;
5702     DisasCompare64 c;
5703     MemOp sz;
5704 
5705     mos = extract32(insn, 29, 3);
5706     type = extract32(insn, 22, 2);
5707     rm = extract32(insn, 16, 5);
5708     cond = extract32(insn, 12, 4);
5709     rn = extract32(insn, 5, 5);
5710     rd = extract32(insn, 0, 5);
5711 
5712     if (mos) {
5713         unallocated_encoding(s);
5714         return;
5715     }
5716 
5717     switch (type) {
5718     case 0:
5719         sz = MO_32;
5720         break;
5721     case 1:
5722         sz = MO_64;
5723         break;
5724     case 3:
5725         sz = MO_16;
5726         if (dc_isar_feature(aa64_fp16, s)) {
5727             break;
5728         }
5729         /* fallthru */
5730     default:
5731         unallocated_encoding(s);
5732         return;
5733     }
5734 
5735     if (!fp_access_check(s)) {
5736         return;
5737     }
5738 
5739     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5740     t_true = tcg_temp_new_i64();
5741     t_false = tcg_temp_new_i64();
5742     read_vec_element(s, t_true, rn, 0, sz);
5743     read_vec_element(s, t_false, rm, 0, sz);
5744 
5745     a64_test_cc(&c, cond);
5746     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5747                         t_true, t_false);
5748 
5749     /* Note that sregs & hregs write back zeros to the high bits,
5750        and we've already done the zero-extension.  */
5751     write_fp_dreg(s, rd, t_true);
5752 }
5753 
5754 /* Floating-point data-processing (1 source) - half precision */
5755 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5756 {
5757     TCGv_ptr fpst = NULL;
5758     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5759     TCGv_i32 tcg_res = tcg_temp_new_i32();
5760 
5761     switch (opcode) {
5762     case 0x0: /* FMOV */
5763         tcg_gen_mov_i32(tcg_res, tcg_op);
5764         break;
5765     case 0x1: /* FABS */
5766         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5767         break;
5768     case 0x2: /* FNEG */
5769         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5770         break;
5771     case 0x3: /* FSQRT */
5772         fpst = fpstatus_ptr(FPST_FPCR_F16);
5773         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5774         break;
5775     case 0x8: /* FRINTN */
5776     case 0x9: /* FRINTP */
5777     case 0xa: /* FRINTM */
5778     case 0xb: /* FRINTZ */
5779     case 0xc: /* FRINTA */
5780     {
5781         TCGv_i32 tcg_rmode;
5782 
5783         fpst = fpstatus_ptr(FPST_FPCR_F16);
5784         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
5785         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5786         gen_restore_rmode(tcg_rmode, fpst);
5787         break;
5788     }
5789     case 0xe: /* FRINTX */
5790         fpst = fpstatus_ptr(FPST_FPCR_F16);
5791         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5792         break;
5793     case 0xf: /* FRINTI */
5794         fpst = fpstatus_ptr(FPST_FPCR_F16);
5795         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5796         break;
5797     default:
5798         g_assert_not_reached();
5799     }
5800 
5801     write_fp_sreg(s, rd, tcg_res);
5802 }
5803 
5804 /* Floating-point data-processing (1 source) - single precision */
5805 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5806 {
5807     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5808     TCGv_i32 tcg_op, tcg_res;
5809     TCGv_ptr fpst;
5810     int rmode = -1;
5811 
5812     tcg_op = read_fp_sreg(s, rn);
5813     tcg_res = tcg_temp_new_i32();
5814 
5815     switch (opcode) {
5816     case 0x0: /* FMOV */
5817         tcg_gen_mov_i32(tcg_res, tcg_op);
5818         goto done;
5819     case 0x1: /* FABS */
5820         gen_helper_vfp_abss(tcg_res, tcg_op);
5821         goto done;
5822     case 0x2: /* FNEG */
5823         gen_helper_vfp_negs(tcg_res, tcg_op);
5824         goto done;
5825     case 0x3: /* FSQRT */
5826         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
5827         goto done;
5828     case 0x6: /* BFCVT */
5829         gen_fpst = gen_helper_bfcvt;
5830         break;
5831     case 0x8: /* FRINTN */
5832     case 0x9: /* FRINTP */
5833     case 0xa: /* FRINTM */
5834     case 0xb: /* FRINTZ */
5835     case 0xc: /* FRINTA */
5836         rmode = opcode & 7;
5837         gen_fpst = gen_helper_rints;
5838         break;
5839     case 0xe: /* FRINTX */
5840         gen_fpst = gen_helper_rints_exact;
5841         break;
5842     case 0xf: /* FRINTI */
5843         gen_fpst = gen_helper_rints;
5844         break;
5845     case 0x10: /* FRINT32Z */
5846         rmode = FPROUNDING_ZERO;
5847         gen_fpst = gen_helper_frint32_s;
5848         break;
5849     case 0x11: /* FRINT32X */
5850         gen_fpst = gen_helper_frint32_s;
5851         break;
5852     case 0x12: /* FRINT64Z */
5853         rmode = FPROUNDING_ZERO;
5854         gen_fpst = gen_helper_frint64_s;
5855         break;
5856     case 0x13: /* FRINT64X */
5857         gen_fpst = gen_helper_frint64_s;
5858         break;
5859     default:
5860         g_assert_not_reached();
5861     }
5862 
5863     fpst = fpstatus_ptr(FPST_FPCR);
5864     if (rmode >= 0) {
5865         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
5866         gen_fpst(tcg_res, tcg_op, fpst);
5867         gen_restore_rmode(tcg_rmode, fpst);
5868     } else {
5869         gen_fpst(tcg_res, tcg_op, fpst);
5870     }
5871 
5872  done:
5873     write_fp_sreg(s, rd, tcg_res);
5874 }
5875 
5876 /* Floating-point data-processing (1 source) - double precision */
5877 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5878 {
5879     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
5880     TCGv_i64 tcg_op, tcg_res;
5881     TCGv_ptr fpst;
5882     int rmode = -1;
5883 
5884     switch (opcode) {
5885     case 0x0: /* FMOV */
5886         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5887         return;
5888     }
5889 
5890     tcg_op = read_fp_dreg(s, rn);
5891     tcg_res = tcg_temp_new_i64();
5892 
5893     switch (opcode) {
5894     case 0x1: /* FABS */
5895         gen_helper_vfp_absd(tcg_res, tcg_op);
5896         goto done;
5897     case 0x2: /* FNEG */
5898         gen_helper_vfp_negd(tcg_res, tcg_op);
5899         goto done;
5900     case 0x3: /* FSQRT */
5901         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
5902         goto done;
5903     case 0x8: /* FRINTN */
5904     case 0x9: /* FRINTP */
5905     case 0xa: /* FRINTM */
5906     case 0xb: /* FRINTZ */
5907     case 0xc: /* FRINTA */
5908         rmode = opcode & 7;
5909         gen_fpst = gen_helper_rintd;
5910         break;
5911     case 0xe: /* FRINTX */
5912         gen_fpst = gen_helper_rintd_exact;
5913         break;
5914     case 0xf: /* FRINTI */
5915         gen_fpst = gen_helper_rintd;
5916         break;
5917     case 0x10: /* FRINT32Z */
5918         rmode = FPROUNDING_ZERO;
5919         gen_fpst = gen_helper_frint32_d;
5920         break;
5921     case 0x11: /* FRINT32X */
5922         gen_fpst = gen_helper_frint32_d;
5923         break;
5924     case 0x12: /* FRINT64Z */
5925         rmode = FPROUNDING_ZERO;
5926         gen_fpst = gen_helper_frint64_d;
5927         break;
5928     case 0x13: /* FRINT64X */
5929         gen_fpst = gen_helper_frint64_d;
5930         break;
5931     default:
5932         g_assert_not_reached();
5933     }
5934 
5935     fpst = fpstatus_ptr(FPST_FPCR);
5936     if (rmode >= 0) {
5937         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
5938         gen_fpst(tcg_res, tcg_op, fpst);
5939         gen_restore_rmode(tcg_rmode, fpst);
5940     } else {
5941         gen_fpst(tcg_res, tcg_op, fpst);
5942     }
5943 
5944  done:
5945     write_fp_dreg(s, rd, tcg_res);
5946 }
5947 
5948 static void handle_fp_fcvt(DisasContext *s, int opcode,
5949                            int rd, int rn, int dtype, int ntype)
5950 {
5951     switch (ntype) {
5952     case 0x0:
5953     {
5954         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5955         if (dtype == 1) {
5956             /* Single to double */
5957             TCGv_i64 tcg_rd = tcg_temp_new_i64();
5958             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5959             write_fp_dreg(s, rd, tcg_rd);
5960         } else {
5961             /* Single to half */
5962             TCGv_i32 tcg_rd = tcg_temp_new_i32();
5963             TCGv_i32 ahp = get_ahp_flag();
5964             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
5965 
5966             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5967             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5968             write_fp_sreg(s, rd, tcg_rd);
5969         }
5970         break;
5971     }
5972     case 0x1:
5973     {
5974         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5975         TCGv_i32 tcg_rd = tcg_temp_new_i32();
5976         if (dtype == 0) {
5977             /* Double to single */
5978             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5979         } else {
5980             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
5981             TCGv_i32 ahp = get_ahp_flag();
5982             /* Double to half */
5983             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5984             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5985         }
5986         write_fp_sreg(s, rd, tcg_rd);
5987         break;
5988     }
5989     case 0x3:
5990     {
5991         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5992         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
5993         TCGv_i32 tcg_ahp = get_ahp_flag();
5994         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
5995         if (dtype == 0) {
5996             /* Half to single */
5997             TCGv_i32 tcg_rd = tcg_temp_new_i32();
5998             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5999             write_fp_sreg(s, rd, tcg_rd);
6000         } else {
6001             /* Half to double */
6002             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6003             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6004             write_fp_dreg(s, rd, tcg_rd);
6005         }
6006         break;
6007     }
6008     default:
6009         g_assert_not_reached();
6010     }
6011 }
6012 
6013 /* Floating point data-processing (1 source)
6014  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6015  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6016  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6017  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6018  */
6019 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6020 {
6021     int mos = extract32(insn, 29, 3);
6022     int type = extract32(insn, 22, 2);
6023     int opcode = extract32(insn, 15, 6);
6024     int rn = extract32(insn, 5, 5);
6025     int rd = extract32(insn, 0, 5);
6026 
6027     if (mos) {
6028         goto do_unallocated;
6029     }
6030 
6031     switch (opcode) {
6032     case 0x4: case 0x5: case 0x7:
6033     {
6034         /* FCVT between half, single and double precision */
6035         int dtype = extract32(opcode, 0, 2);
6036         if (type == 2 || dtype == type) {
6037             goto do_unallocated;
6038         }
6039         if (!fp_access_check(s)) {
6040             return;
6041         }
6042 
6043         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6044         break;
6045     }
6046 
6047     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6048         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6049             goto do_unallocated;
6050         }
6051         /* fall through */
6052     case 0x0 ... 0x3:
6053     case 0x8 ... 0xc:
6054     case 0xe ... 0xf:
6055         /* 32-to-32 and 64-to-64 ops */
6056         switch (type) {
6057         case 0:
6058             if (!fp_access_check(s)) {
6059                 return;
6060             }
6061             handle_fp_1src_single(s, opcode, rd, rn);
6062             break;
6063         case 1:
6064             if (!fp_access_check(s)) {
6065                 return;
6066             }
6067             handle_fp_1src_double(s, opcode, rd, rn);
6068             break;
6069         case 3:
6070             if (!dc_isar_feature(aa64_fp16, s)) {
6071                 goto do_unallocated;
6072             }
6073 
6074             if (!fp_access_check(s)) {
6075                 return;
6076             }
6077             handle_fp_1src_half(s, opcode, rd, rn);
6078             break;
6079         default:
6080             goto do_unallocated;
6081         }
6082         break;
6083 
6084     case 0x6:
6085         switch (type) {
6086         case 1: /* BFCVT */
6087             if (!dc_isar_feature(aa64_bf16, s)) {
6088                 goto do_unallocated;
6089             }
6090             if (!fp_access_check(s)) {
6091                 return;
6092             }
6093             handle_fp_1src_single(s, opcode, rd, rn);
6094             break;
6095         default:
6096             goto do_unallocated;
6097         }
6098         break;
6099 
6100     default:
6101     do_unallocated:
6102         unallocated_encoding(s);
6103         break;
6104     }
6105 }
6106 
6107 /* Floating-point data-processing (2 source) - single precision */
6108 static void handle_fp_2src_single(DisasContext *s, int opcode,
6109                                   int rd, int rn, int rm)
6110 {
6111     TCGv_i32 tcg_op1;
6112     TCGv_i32 tcg_op2;
6113     TCGv_i32 tcg_res;
6114     TCGv_ptr fpst;
6115 
6116     tcg_res = tcg_temp_new_i32();
6117     fpst = fpstatus_ptr(FPST_FPCR);
6118     tcg_op1 = read_fp_sreg(s, rn);
6119     tcg_op2 = read_fp_sreg(s, rm);
6120 
6121     switch (opcode) {
6122     case 0x0: /* FMUL */
6123         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6124         break;
6125     case 0x1: /* FDIV */
6126         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6127         break;
6128     case 0x2: /* FADD */
6129         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6130         break;
6131     case 0x3: /* FSUB */
6132         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6133         break;
6134     case 0x4: /* FMAX */
6135         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6136         break;
6137     case 0x5: /* FMIN */
6138         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6139         break;
6140     case 0x6: /* FMAXNM */
6141         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6142         break;
6143     case 0x7: /* FMINNM */
6144         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6145         break;
6146     case 0x8: /* FNMUL */
6147         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6148         gen_helper_vfp_negs(tcg_res, tcg_res);
6149         break;
6150     }
6151 
6152     write_fp_sreg(s, rd, tcg_res);
6153 }
6154 
6155 /* Floating-point data-processing (2 source) - double precision */
6156 static void handle_fp_2src_double(DisasContext *s, int opcode,
6157                                   int rd, int rn, int rm)
6158 {
6159     TCGv_i64 tcg_op1;
6160     TCGv_i64 tcg_op2;
6161     TCGv_i64 tcg_res;
6162     TCGv_ptr fpst;
6163 
6164     tcg_res = tcg_temp_new_i64();
6165     fpst = fpstatus_ptr(FPST_FPCR);
6166     tcg_op1 = read_fp_dreg(s, rn);
6167     tcg_op2 = read_fp_dreg(s, rm);
6168 
6169     switch (opcode) {
6170     case 0x0: /* FMUL */
6171         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6172         break;
6173     case 0x1: /* FDIV */
6174         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6175         break;
6176     case 0x2: /* FADD */
6177         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6178         break;
6179     case 0x3: /* FSUB */
6180         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6181         break;
6182     case 0x4: /* FMAX */
6183         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6184         break;
6185     case 0x5: /* FMIN */
6186         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6187         break;
6188     case 0x6: /* FMAXNM */
6189         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6190         break;
6191     case 0x7: /* FMINNM */
6192         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6193         break;
6194     case 0x8: /* FNMUL */
6195         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6196         gen_helper_vfp_negd(tcg_res, tcg_res);
6197         break;
6198     }
6199 
6200     write_fp_dreg(s, rd, tcg_res);
6201 }
6202 
6203 /* Floating-point data-processing (2 source) - half precision */
6204 static void handle_fp_2src_half(DisasContext *s, int opcode,
6205                                 int rd, int rn, int rm)
6206 {
6207     TCGv_i32 tcg_op1;
6208     TCGv_i32 tcg_op2;
6209     TCGv_i32 tcg_res;
6210     TCGv_ptr fpst;
6211 
6212     tcg_res = tcg_temp_new_i32();
6213     fpst = fpstatus_ptr(FPST_FPCR_F16);
6214     tcg_op1 = read_fp_hreg(s, rn);
6215     tcg_op2 = read_fp_hreg(s, rm);
6216 
6217     switch (opcode) {
6218     case 0x0: /* FMUL */
6219         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6220         break;
6221     case 0x1: /* FDIV */
6222         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6223         break;
6224     case 0x2: /* FADD */
6225         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6226         break;
6227     case 0x3: /* FSUB */
6228         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6229         break;
6230     case 0x4: /* FMAX */
6231         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6232         break;
6233     case 0x5: /* FMIN */
6234         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6235         break;
6236     case 0x6: /* FMAXNM */
6237         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6238         break;
6239     case 0x7: /* FMINNM */
6240         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6241         break;
6242     case 0x8: /* FNMUL */
6243         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6244         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6245         break;
6246     default:
6247         g_assert_not_reached();
6248     }
6249 
6250     write_fp_sreg(s, rd, tcg_res);
6251 }
6252 
6253 /* Floating point data-processing (2 source)
6254  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6255  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6256  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6257  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6258  */
6259 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6260 {
6261     int mos = extract32(insn, 29, 3);
6262     int type = extract32(insn, 22, 2);
6263     int rd = extract32(insn, 0, 5);
6264     int rn = extract32(insn, 5, 5);
6265     int rm = extract32(insn, 16, 5);
6266     int opcode = extract32(insn, 12, 4);
6267 
6268     if (opcode > 8 || mos) {
6269         unallocated_encoding(s);
6270         return;
6271     }
6272 
6273     switch (type) {
6274     case 0:
6275         if (!fp_access_check(s)) {
6276             return;
6277         }
6278         handle_fp_2src_single(s, opcode, rd, rn, rm);
6279         break;
6280     case 1:
6281         if (!fp_access_check(s)) {
6282             return;
6283         }
6284         handle_fp_2src_double(s, opcode, rd, rn, rm);
6285         break;
6286     case 3:
6287         if (!dc_isar_feature(aa64_fp16, s)) {
6288             unallocated_encoding(s);
6289             return;
6290         }
6291         if (!fp_access_check(s)) {
6292             return;
6293         }
6294         handle_fp_2src_half(s, opcode, rd, rn, rm);
6295         break;
6296     default:
6297         unallocated_encoding(s);
6298     }
6299 }
6300 
6301 /* Floating-point data-processing (3 source) - single precision */
6302 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6303                                   int rd, int rn, int rm, int ra)
6304 {
6305     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6306     TCGv_i32 tcg_res = tcg_temp_new_i32();
6307     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6308 
6309     tcg_op1 = read_fp_sreg(s, rn);
6310     tcg_op2 = read_fp_sreg(s, rm);
6311     tcg_op3 = read_fp_sreg(s, ra);
6312 
6313     /* These are fused multiply-add, and must be done as one
6314      * floating point operation with no rounding between the
6315      * multiplication and addition steps.
6316      * NB that doing the negations here as separate steps is
6317      * correct : an input NaN should come out with its sign bit
6318      * flipped if it is a negated-input.
6319      */
6320     if (o1 == true) {
6321         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6322     }
6323 
6324     if (o0 != o1) {
6325         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6326     }
6327 
6328     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6329 
6330     write_fp_sreg(s, rd, tcg_res);
6331 }
6332 
6333 /* Floating-point data-processing (3 source) - double precision */
6334 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6335                                   int rd, int rn, int rm, int ra)
6336 {
6337     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6338     TCGv_i64 tcg_res = tcg_temp_new_i64();
6339     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6340 
6341     tcg_op1 = read_fp_dreg(s, rn);
6342     tcg_op2 = read_fp_dreg(s, rm);
6343     tcg_op3 = read_fp_dreg(s, ra);
6344 
6345     /* These are fused multiply-add, and must be done as one
6346      * floating point operation with no rounding between the
6347      * multiplication and addition steps.
6348      * NB that doing the negations here as separate steps is
6349      * correct : an input NaN should come out with its sign bit
6350      * flipped if it is a negated-input.
6351      */
6352     if (o1 == true) {
6353         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6354     }
6355 
6356     if (o0 != o1) {
6357         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6358     }
6359 
6360     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6361 
6362     write_fp_dreg(s, rd, tcg_res);
6363 }
6364 
6365 /* Floating-point data-processing (3 source) - half precision */
6366 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6367                                 int rd, int rn, int rm, int ra)
6368 {
6369     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6370     TCGv_i32 tcg_res = tcg_temp_new_i32();
6371     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6372 
6373     tcg_op1 = read_fp_hreg(s, rn);
6374     tcg_op2 = read_fp_hreg(s, rm);
6375     tcg_op3 = read_fp_hreg(s, ra);
6376 
6377     /* These are fused multiply-add, and must be done as one
6378      * floating point operation with no rounding between the
6379      * multiplication and addition steps.
6380      * NB that doing the negations here as separate steps is
6381      * correct : an input NaN should come out with its sign bit
6382      * flipped if it is a negated-input.
6383      */
6384     if (o1 == true) {
6385         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6386     }
6387 
6388     if (o0 != o1) {
6389         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6390     }
6391 
6392     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6393 
6394     write_fp_sreg(s, rd, tcg_res);
6395 }
6396 
6397 /* Floating point data-processing (3 source)
6398  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6399  * +---+---+---+-----------+------+----+------+----+------+------+------+
6400  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6401  * +---+---+---+-----------+------+----+------+----+------+------+------+
6402  */
6403 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6404 {
6405     int mos = extract32(insn, 29, 3);
6406     int type = extract32(insn, 22, 2);
6407     int rd = extract32(insn, 0, 5);
6408     int rn = extract32(insn, 5, 5);
6409     int ra = extract32(insn, 10, 5);
6410     int rm = extract32(insn, 16, 5);
6411     bool o0 = extract32(insn, 15, 1);
6412     bool o1 = extract32(insn, 21, 1);
6413 
6414     if (mos) {
6415         unallocated_encoding(s);
6416         return;
6417     }
6418 
6419     switch (type) {
6420     case 0:
6421         if (!fp_access_check(s)) {
6422             return;
6423         }
6424         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6425         break;
6426     case 1:
6427         if (!fp_access_check(s)) {
6428             return;
6429         }
6430         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6431         break;
6432     case 3:
6433         if (!dc_isar_feature(aa64_fp16, s)) {
6434             unallocated_encoding(s);
6435             return;
6436         }
6437         if (!fp_access_check(s)) {
6438             return;
6439         }
6440         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6441         break;
6442     default:
6443         unallocated_encoding(s);
6444     }
6445 }
6446 
6447 /* Floating point immediate
6448  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6449  * +---+---+---+-----------+------+---+------------+-------+------+------+
6450  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6451  * +---+---+---+-----------+------+---+------------+-------+------+------+
6452  */
6453 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6454 {
6455     int rd = extract32(insn, 0, 5);
6456     int imm5 = extract32(insn, 5, 5);
6457     int imm8 = extract32(insn, 13, 8);
6458     int type = extract32(insn, 22, 2);
6459     int mos = extract32(insn, 29, 3);
6460     uint64_t imm;
6461     MemOp sz;
6462 
6463     if (mos || imm5) {
6464         unallocated_encoding(s);
6465         return;
6466     }
6467 
6468     switch (type) {
6469     case 0:
6470         sz = MO_32;
6471         break;
6472     case 1:
6473         sz = MO_64;
6474         break;
6475     case 3:
6476         sz = MO_16;
6477         if (dc_isar_feature(aa64_fp16, s)) {
6478             break;
6479         }
6480         /* fallthru */
6481     default:
6482         unallocated_encoding(s);
6483         return;
6484     }
6485 
6486     if (!fp_access_check(s)) {
6487         return;
6488     }
6489 
6490     imm = vfp_expand_imm(sz, imm8);
6491     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6492 }
6493 
6494 /* Handle floating point <=> fixed point conversions. Note that we can
6495  * also deal with fp <=> integer conversions as a special case (scale == 64)
6496  * OPTME: consider handling that special case specially or at least skipping
6497  * the call to scalbn in the helpers for zero shifts.
6498  */
6499 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6500                            bool itof, int rmode, int scale, int sf, int type)
6501 {
6502     bool is_signed = !(opcode & 1);
6503     TCGv_ptr tcg_fpstatus;
6504     TCGv_i32 tcg_shift, tcg_single;
6505     TCGv_i64 tcg_double;
6506 
6507     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6508 
6509     tcg_shift = tcg_constant_i32(64 - scale);
6510 
6511     if (itof) {
6512         TCGv_i64 tcg_int = cpu_reg(s, rn);
6513         if (!sf) {
6514             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6515 
6516             if (is_signed) {
6517                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6518             } else {
6519                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6520             }
6521 
6522             tcg_int = tcg_extend;
6523         }
6524 
6525         switch (type) {
6526         case 1: /* float64 */
6527             tcg_double = tcg_temp_new_i64();
6528             if (is_signed) {
6529                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6530                                      tcg_shift, tcg_fpstatus);
6531             } else {
6532                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6533                                      tcg_shift, tcg_fpstatus);
6534             }
6535             write_fp_dreg(s, rd, tcg_double);
6536             break;
6537 
6538         case 0: /* float32 */
6539             tcg_single = tcg_temp_new_i32();
6540             if (is_signed) {
6541                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6542                                      tcg_shift, tcg_fpstatus);
6543             } else {
6544                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6545                                      tcg_shift, tcg_fpstatus);
6546             }
6547             write_fp_sreg(s, rd, tcg_single);
6548             break;
6549 
6550         case 3: /* float16 */
6551             tcg_single = tcg_temp_new_i32();
6552             if (is_signed) {
6553                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6554                                      tcg_shift, tcg_fpstatus);
6555             } else {
6556                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6557                                      tcg_shift, tcg_fpstatus);
6558             }
6559             write_fp_sreg(s, rd, tcg_single);
6560             break;
6561 
6562         default:
6563             g_assert_not_reached();
6564         }
6565     } else {
6566         TCGv_i64 tcg_int = cpu_reg(s, rd);
6567         TCGv_i32 tcg_rmode;
6568 
6569         if (extract32(opcode, 2, 1)) {
6570             /* There are too many rounding modes to all fit into rmode,
6571              * so FCVTA[US] is a special case.
6572              */
6573             rmode = FPROUNDING_TIEAWAY;
6574         }
6575 
6576         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6577 
6578         switch (type) {
6579         case 1: /* float64 */
6580             tcg_double = read_fp_dreg(s, rn);
6581             if (is_signed) {
6582                 if (!sf) {
6583                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6584                                          tcg_shift, tcg_fpstatus);
6585                 } else {
6586                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6587                                          tcg_shift, tcg_fpstatus);
6588                 }
6589             } else {
6590                 if (!sf) {
6591                     gen_helper_vfp_tould(tcg_int, tcg_double,
6592                                          tcg_shift, tcg_fpstatus);
6593                 } else {
6594                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6595                                          tcg_shift, tcg_fpstatus);
6596                 }
6597             }
6598             if (!sf) {
6599                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6600             }
6601             break;
6602 
6603         case 0: /* float32 */
6604             tcg_single = read_fp_sreg(s, rn);
6605             if (sf) {
6606                 if (is_signed) {
6607                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6608                                          tcg_shift, tcg_fpstatus);
6609                 } else {
6610                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6611                                          tcg_shift, tcg_fpstatus);
6612                 }
6613             } else {
6614                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6615                 if (is_signed) {
6616                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6617                                          tcg_shift, tcg_fpstatus);
6618                 } else {
6619                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6620                                          tcg_shift, tcg_fpstatus);
6621                 }
6622                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6623             }
6624             break;
6625 
6626         case 3: /* float16 */
6627             tcg_single = read_fp_sreg(s, rn);
6628             if (sf) {
6629                 if (is_signed) {
6630                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6631                                          tcg_shift, tcg_fpstatus);
6632                 } else {
6633                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6634                                          tcg_shift, tcg_fpstatus);
6635                 }
6636             } else {
6637                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6638                 if (is_signed) {
6639                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6640                                          tcg_shift, tcg_fpstatus);
6641                 } else {
6642                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6643                                          tcg_shift, tcg_fpstatus);
6644                 }
6645                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6646             }
6647             break;
6648 
6649         default:
6650             g_assert_not_reached();
6651         }
6652 
6653         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6654     }
6655 }
6656 
6657 /* Floating point <-> fixed point conversions
6658  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6659  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6660  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6661  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6662  */
6663 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6664 {
6665     int rd = extract32(insn, 0, 5);
6666     int rn = extract32(insn, 5, 5);
6667     int scale = extract32(insn, 10, 6);
6668     int opcode = extract32(insn, 16, 3);
6669     int rmode = extract32(insn, 19, 2);
6670     int type = extract32(insn, 22, 2);
6671     bool sbit = extract32(insn, 29, 1);
6672     bool sf = extract32(insn, 31, 1);
6673     bool itof;
6674 
6675     if (sbit || (!sf && scale < 32)) {
6676         unallocated_encoding(s);
6677         return;
6678     }
6679 
6680     switch (type) {
6681     case 0: /* float32 */
6682     case 1: /* float64 */
6683         break;
6684     case 3: /* float16 */
6685         if (dc_isar_feature(aa64_fp16, s)) {
6686             break;
6687         }
6688         /* fallthru */
6689     default:
6690         unallocated_encoding(s);
6691         return;
6692     }
6693 
6694     switch ((rmode << 3) | opcode) {
6695     case 0x2: /* SCVTF */
6696     case 0x3: /* UCVTF */
6697         itof = true;
6698         break;
6699     case 0x18: /* FCVTZS */
6700     case 0x19: /* FCVTZU */
6701         itof = false;
6702         break;
6703     default:
6704         unallocated_encoding(s);
6705         return;
6706     }
6707 
6708     if (!fp_access_check(s)) {
6709         return;
6710     }
6711 
6712     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6713 }
6714 
6715 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6716 {
6717     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6718      * without conversion.
6719      */
6720 
6721     if (itof) {
6722         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6723         TCGv_i64 tmp;
6724 
6725         switch (type) {
6726         case 0:
6727             /* 32 bit */
6728             tmp = tcg_temp_new_i64();
6729             tcg_gen_ext32u_i64(tmp, tcg_rn);
6730             write_fp_dreg(s, rd, tmp);
6731             break;
6732         case 1:
6733             /* 64 bit */
6734             write_fp_dreg(s, rd, tcg_rn);
6735             break;
6736         case 2:
6737             /* 64 bit to top half. */
6738             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6739             clear_vec_high(s, true, rd);
6740             break;
6741         case 3:
6742             /* 16 bit */
6743             tmp = tcg_temp_new_i64();
6744             tcg_gen_ext16u_i64(tmp, tcg_rn);
6745             write_fp_dreg(s, rd, tmp);
6746             break;
6747         default:
6748             g_assert_not_reached();
6749         }
6750     } else {
6751         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6752 
6753         switch (type) {
6754         case 0:
6755             /* 32 bit */
6756             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6757             break;
6758         case 1:
6759             /* 64 bit */
6760             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6761             break;
6762         case 2:
6763             /* 64 bits from top half */
6764             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6765             break;
6766         case 3:
6767             /* 16 bit */
6768             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6769             break;
6770         default:
6771             g_assert_not_reached();
6772         }
6773     }
6774 }
6775 
6776 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6777 {
6778     TCGv_i64 t = read_fp_dreg(s, rn);
6779     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
6780 
6781     gen_helper_fjcvtzs(t, t, fpstatus);
6782 
6783     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6784     tcg_gen_extrh_i64_i32(cpu_ZF, t);
6785     tcg_gen_movi_i32(cpu_CF, 0);
6786     tcg_gen_movi_i32(cpu_NF, 0);
6787     tcg_gen_movi_i32(cpu_VF, 0);
6788 }
6789 
6790 /* Floating point <-> integer conversions
6791  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
6792  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6793  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6794  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6795  */
6796 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6797 {
6798     int rd = extract32(insn, 0, 5);
6799     int rn = extract32(insn, 5, 5);
6800     int opcode = extract32(insn, 16, 3);
6801     int rmode = extract32(insn, 19, 2);
6802     int type = extract32(insn, 22, 2);
6803     bool sbit = extract32(insn, 29, 1);
6804     bool sf = extract32(insn, 31, 1);
6805     bool itof = false;
6806 
6807     if (sbit) {
6808         goto do_unallocated;
6809     }
6810 
6811     switch (opcode) {
6812     case 2: /* SCVTF */
6813     case 3: /* UCVTF */
6814         itof = true;
6815         /* fallthru */
6816     case 4: /* FCVTAS */
6817     case 5: /* FCVTAU */
6818         if (rmode != 0) {
6819             goto do_unallocated;
6820         }
6821         /* fallthru */
6822     case 0: /* FCVT[NPMZ]S */
6823     case 1: /* FCVT[NPMZ]U */
6824         switch (type) {
6825         case 0: /* float32 */
6826         case 1: /* float64 */
6827             break;
6828         case 3: /* float16 */
6829             if (!dc_isar_feature(aa64_fp16, s)) {
6830                 goto do_unallocated;
6831             }
6832             break;
6833         default:
6834             goto do_unallocated;
6835         }
6836         if (!fp_access_check(s)) {
6837             return;
6838         }
6839         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6840         break;
6841 
6842     default:
6843         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6844         case 0b01100110: /* FMOV half <-> 32-bit int */
6845         case 0b01100111:
6846         case 0b11100110: /* FMOV half <-> 64-bit int */
6847         case 0b11100111:
6848             if (!dc_isar_feature(aa64_fp16, s)) {
6849                 goto do_unallocated;
6850             }
6851             /* fallthru */
6852         case 0b00000110: /* FMOV 32-bit */
6853         case 0b00000111:
6854         case 0b10100110: /* FMOV 64-bit */
6855         case 0b10100111:
6856         case 0b11001110: /* FMOV top half of 128-bit */
6857         case 0b11001111:
6858             if (!fp_access_check(s)) {
6859                 return;
6860             }
6861             itof = opcode & 1;
6862             handle_fmov(s, rd, rn, type, itof);
6863             break;
6864 
6865         case 0b00111110: /* FJCVTZS */
6866             if (!dc_isar_feature(aa64_jscvt, s)) {
6867                 goto do_unallocated;
6868             } else if (fp_access_check(s)) {
6869                 handle_fjcvtzs(s, rd, rn);
6870             }
6871             break;
6872 
6873         default:
6874         do_unallocated:
6875             unallocated_encoding(s);
6876             return;
6877         }
6878         break;
6879     }
6880 }
6881 
6882 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6883  *   31  30  29 28     25 24                          0
6884  * +---+---+---+---------+-----------------------------+
6885  * |   | 0 |   | 1 1 1 1 |                             |
6886  * +---+---+---+---------+-----------------------------+
6887  */
6888 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6889 {
6890     if (extract32(insn, 24, 1)) {
6891         /* Floating point data-processing (3 source) */
6892         disas_fp_3src(s, insn);
6893     } else if (extract32(insn, 21, 1) == 0) {
6894         /* Floating point to fixed point conversions */
6895         disas_fp_fixed_conv(s, insn);
6896     } else {
6897         switch (extract32(insn, 10, 2)) {
6898         case 1:
6899             /* Floating point conditional compare */
6900             disas_fp_ccomp(s, insn);
6901             break;
6902         case 2:
6903             /* Floating point data-processing (2 source) */
6904             disas_fp_2src(s, insn);
6905             break;
6906         case 3:
6907             /* Floating point conditional select */
6908             disas_fp_csel(s, insn);
6909             break;
6910         case 0:
6911             switch (ctz32(extract32(insn, 12, 4))) {
6912             case 0: /* [15:12] == xxx1 */
6913                 /* Floating point immediate */
6914                 disas_fp_imm(s, insn);
6915                 break;
6916             case 1: /* [15:12] == xx10 */
6917                 /* Floating point compare */
6918                 disas_fp_compare(s, insn);
6919                 break;
6920             case 2: /* [15:12] == x100 */
6921                 /* Floating point data-processing (1 source) */
6922                 disas_fp_1src(s, insn);
6923                 break;
6924             case 3: /* [15:12] == 1000 */
6925                 unallocated_encoding(s);
6926                 break;
6927             default: /* [15:12] == 0000 */
6928                 /* Floating point <-> integer conversions */
6929                 disas_fp_int_conv(s, insn);
6930                 break;
6931             }
6932             break;
6933         }
6934     }
6935 }
6936 
6937 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6938                      int pos)
6939 {
6940     /* Extract 64 bits from the middle of two concatenated 64 bit
6941      * vector register slices left:right. The extracted bits start
6942      * at 'pos' bits into the right (least significant) side.
6943      * We return the result in tcg_right, and guarantee not to
6944      * trash tcg_left.
6945      */
6946     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6947     assert(pos > 0 && pos < 64);
6948 
6949     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6950     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6951     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6952 }
6953 
6954 /* EXT
6955  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
6956  * +---+---+-------------+-----+---+------+---+------+---+------+------+
6957  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
6958  * +---+---+-------------+-----+---+------+---+------+---+------+------+
6959  */
6960 static void disas_simd_ext(DisasContext *s, uint32_t insn)
6961 {
6962     int is_q = extract32(insn, 30, 1);
6963     int op2 = extract32(insn, 22, 2);
6964     int imm4 = extract32(insn, 11, 4);
6965     int rm = extract32(insn, 16, 5);
6966     int rn = extract32(insn, 5, 5);
6967     int rd = extract32(insn, 0, 5);
6968     int pos = imm4 << 3;
6969     TCGv_i64 tcg_resl, tcg_resh;
6970 
6971     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6972         unallocated_encoding(s);
6973         return;
6974     }
6975 
6976     if (!fp_access_check(s)) {
6977         return;
6978     }
6979 
6980     tcg_resh = tcg_temp_new_i64();
6981     tcg_resl = tcg_temp_new_i64();
6982 
6983     /* Vd gets bits starting at pos bits into Vm:Vn. This is
6984      * either extracting 128 bits from a 128:128 concatenation, or
6985      * extracting 64 bits from a 64:64 concatenation.
6986      */
6987     if (!is_q) {
6988         read_vec_element(s, tcg_resl, rn, 0, MO_64);
6989         if (pos != 0) {
6990             read_vec_element(s, tcg_resh, rm, 0, MO_64);
6991             do_ext64(s, tcg_resh, tcg_resl, pos);
6992         }
6993     } else {
6994         TCGv_i64 tcg_hh;
6995         typedef struct {
6996             int reg;
6997             int elt;
6998         } EltPosns;
6999         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7000         EltPosns *elt = eltposns;
7001 
7002         if (pos >= 64) {
7003             elt++;
7004             pos -= 64;
7005         }
7006 
7007         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7008         elt++;
7009         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7010         elt++;
7011         if (pos != 0) {
7012             do_ext64(s, tcg_resh, tcg_resl, pos);
7013             tcg_hh = tcg_temp_new_i64();
7014             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7015             do_ext64(s, tcg_hh, tcg_resh, pos);
7016         }
7017     }
7018 
7019     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7020     if (is_q) {
7021         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7022     }
7023     clear_vec_high(s, is_q, rd);
7024 }
7025 
7026 /* TBL/TBX
7027  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7028  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7029  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7030  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7031  */
7032 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7033 {
7034     int op2 = extract32(insn, 22, 2);
7035     int is_q = extract32(insn, 30, 1);
7036     int rm = extract32(insn, 16, 5);
7037     int rn = extract32(insn, 5, 5);
7038     int rd = extract32(insn, 0, 5);
7039     int is_tbx = extract32(insn, 12, 1);
7040     int len = (extract32(insn, 13, 2) + 1) * 16;
7041 
7042     if (op2 != 0) {
7043         unallocated_encoding(s);
7044         return;
7045     }
7046 
7047     if (!fp_access_check(s)) {
7048         return;
7049     }
7050 
7051     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7052                        vec_full_reg_offset(s, rm), cpu_env,
7053                        is_q ? 16 : 8, vec_full_reg_size(s),
7054                        (len << 6) | (is_tbx << 5) | rn,
7055                        gen_helper_simd_tblx);
7056 }
7057 
7058 /* ZIP/UZP/TRN
7059  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7060  * +---+---+-------------+------+---+------+---+------------------+------+
7061  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7062  * +---+---+-------------+------+---+------+---+------------------+------+
7063  */
7064 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7065 {
7066     int rd = extract32(insn, 0, 5);
7067     int rn = extract32(insn, 5, 5);
7068     int rm = extract32(insn, 16, 5);
7069     int size = extract32(insn, 22, 2);
7070     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7071      * bit 2 indicates 1 vs 2 variant of the insn.
7072      */
7073     int opcode = extract32(insn, 12, 2);
7074     bool part = extract32(insn, 14, 1);
7075     bool is_q = extract32(insn, 30, 1);
7076     int esize = 8 << size;
7077     int i;
7078     int datasize = is_q ? 128 : 64;
7079     int elements = datasize / esize;
7080     TCGv_i64 tcg_res[2], tcg_ele;
7081 
7082     if (opcode == 0 || (size == 3 && !is_q)) {
7083         unallocated_encoding(s);
7084         return;
7085     }
7086 
7087     if (!fp_access_check(s)) {
7088         return;
7089     }
7090 
7091     tcg_res[0] = tcg_temp_new_i64();
7092     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7093     tcg_ele = tcg_temp_new_i64();
7094 
7095     for (i = 0; i < elements; i++) {
7096         int o, w;
7097 
7098         switch (opcode) {
7099         case 1: /* UZP1/2 */
7100         {
7101             int midpoint = elements / 2;
7102             if (i < midpoint) {
7103                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7104             } else {
7105                 read_vec_element(s, tcg_ele, rm,
7106                                  2 * (i - midpoint) + part, size);
7107             }
7108             break;
7109         }
7110         case 2: /* TRN1/2 */
7111             if (i & 1) {
7112                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7113             } else {
7114                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7115             }
7116             break;
7117         case 3: /* ZIP1/2 */
7118         {
7119             int base = part * elements / 2;
7120             if (i & 1) {
7121                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7122             } else {
7123                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7124             }
7125             break;
7126         }
7127         default:
7128             g_assert_not_reached();
7129         }
7130 
7131         w = (i * esize) / 64;
7132         o = (i * esize) % 64;
7133         if (o == 0) {
7134             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7135         } else {
7136             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7137             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7138         }
7139     }
7140 
7141     for (i = 0; i <= is_q; ++i) {
7142         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7143     }
7144     clear_vec_high(s, is_q, rd);
7145 }
7146 
7147 /*
7148  * do_reduction_op helper
7149  *
7150  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7151  * important for correct NaN propagation that we do these
7152  * operations in exactly the order specified by the pseudocode.
7153  *
7154  * This is a recursive function, TCG temps should be freed by the
7155  * calling function once it is done with the values.
7156  */
7157 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7158                                 int esize, int size, int vmap, TCGv_ptr fpst)
7159 {
7160     if (esize == size) {
7161         int element;
7162         MemOp msize = esize == 16 ? MO_16 : MO_32;
7163         TCGv_i32 tcg_elem;
7164 
7165         /* We should have one register left here */
7166         assert(ctpop8(vmap) == 1);
7167         element = ctz32(vmap);
7168         assert(element < 8);
7169 
7170         tcg_elem = tcg_temp_new_i32();
7171         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7172         return tcg_elem;
7173     } else {
7174         int bits = size / 2;
7175         int shift = ctpop8(vmap) / 2;
7176         int vmap_lo = (vmap >> shift) & vmap;
7177         int vmap_hi = (vmap & ~vmap_lo);
7178         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7179 
7180         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7181         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7182         tcg_res = tcg_temp_new_i32();
7183 
7184         switch (fpopcode) {
7185         case 0x0c: /* fmaxnmv half-precision */
7186             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7187             break;
7188         case 0x0f: /* fmaxv half-precision */
7189             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7190             break;
7191         case 0x1c: /* fminnmv half-precision */
7192             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7193             break;
7194         case 0x1f: /* fminv half-precision */
7195             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7196             break;
7197         case 0x2c: /* fmaxnmv */
7198             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7199             break;
7200         case 0x2f: /* fmaxv */
7201             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7202             break;
7203         case 0x3c: /* fminnmv */
7204             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7205             break;
7206         case 0x3f: /* fminv */
7207             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7208             break;
7209         default:
7210             g_assert_not_reached();
7211         }
7212         return tcg_res;
7213     }
7214 }
7215 
7216 /* AdvSIMD across lanes
7217  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7218  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7219  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7220  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7221  */
7222 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7223 {
7224     int rd = extract32(insn, 0, 5);
7225     int rn = extract32(insn, 5, 5);
7226     int size = extract32(insn, 22, 2);
7227     int opcode = extract32(insn, 12, 5);
7228     bool is_q = extract32(insn, 30, 1);
7229     bool is_u = extract32(insn, 29, 1);
7230     bool is_fp = false;
7231     bool is_min = false;
7232     int esize;
7233     int elements;
7234     int i;
7235     TCGv_i64 tcg_res, tcg_elt;
7236 
7237     switch (opcode) {
7238     case 0x1b: /* ADDV */
7239         if (is_u) {
7240             unallocated_encoding(s);
7241             return;
7242         }
7243         /* fall through */
7244     case 0x3: /* SADDLV, UADDLV */
7245     case 0xa: /* SMAXV, UMAXV */
7246     case 0x1a: /* SMINV, UMINV */
7247         if (size == 3 || (size == 2 && !is_q)) {
7248             unallocated_encoding(s);
7249             return;
7250         }
7251         break;
7252     case 0xc: /* FMAXNMV, FMINNMV */
7253     case 0xf: /* FMAXV, FMINV */
7254         /* Bit 1 of size field encodes min vs max and the actual size
7255          * depends on the encoding of the U bit. If not set (and FP16
7256          * enabled) then we do half-precision float instead of single
7257          * precision.
7258          */
7259         is_min = extract32(size, 1, 1);
7260         is_fp = true;
7261         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7262             size = 1;
7263         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7264             unallocated_encoding(s);
7265             return;
7266         } else {
7267             size = 2;
7268         }
7269         break;
7270     default:
7271         unallocated_encoding(s);
7272         return;
7273     }
7274 
7275     if (!fp_access_check(s)) {
7276         return;
7277     }
7278 
7279     esize = 8 << size;
7280     elements = (is_q ? 128 : 64) / esize;
7281 
7282     tcg_res = tcg_temp_new_i64();
7283     tcg_elt = tcg_temp_new_i64();
7284 
7285     /* These instructions operate across all lanes of a vector
7286      * to produce a single result. We can guarantee that a 64
7287      * bit intermediate is sufficient:
7288      *  + for [US]ADDLV the maximum element size is 32 bits, and
7289      *    the result type is 64 bits
7290      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7291      *    same as the element size, which is 32 bits at most
7292      * For the integer operations we can choose to work at 64
7293      * or 32 bits and truncate at the end; for simplicity
7294      * we use 64 bits always. The floating point
7295      * ops do require 32 bit intermediates, though.
7296      */
7297     if (!is_fp) {
7298         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7299 
7300         for (i = 1; i < elements; i++) {
7301             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7302 
7303             switch (opcode) {
7304             case 0x03: /* SADDLV / UADDLV */
7305             case 0x1b: /* ADDV */
7306                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7307                 break;
7308             case 0x0a: /* SMAXV / UMAXV */
7309                 if (is_u) {
7310                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7311                 } else {
7312                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7313                 }
7314                 break;
7315             case 0x1a: /* SMINV / UMINV */
7316                 if (is_u) {
7317                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7318                 } else {
7319                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7320                 }
7321                 break;
7322             default:
7323                 g_assert_not_reached();
7324             }
7325 
7326         }
7327     } else {
7328         /* Floating point vector reduction ops which work across 32
7329          * bit (single) or 16 bit (half-precision) intermediates.
7330          * Note that correct NaN propagation requires that we do these
7331          * operations in exactly the order specified by the pseudocode.
7332          */
7333         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7334         int fpopcode = opcode | is_min << 4 | is_u << 5;
7335         int vmap = (1 << elements) - 1;
7336         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7337                                              (is_q ? 128 : 64), vmap, fpst);
7338         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7339     }
7340 
7341     /* Now truncate the result to the width required for the final output */
7342     if (opcode == 0x03) {
7343         /* SADDLV, UADDLV: result is 2*esize */
7344         size++;
7345     }
7346 
7347     switch (size) {
7348     case 0:
7349         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7350         break;
7351     case 1:
7352         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7353         break;
7354     case 2:
7355         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7356         break;
7357     case 3:
7358         break;
7359     default:
7360         g_assert_not_reached();
7361     }
7362 
7363     write_fp_dreg(s, rd, tcg_res);
7364 }
7365 
7366 /* DUP (Element, Vector)
7367  *
7368  *  31  30   29              21 20    16 15        10  9    5 4    0
7369  * +---+---+-------------------+--------+-------------+------+------+
7370  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7371  * +---+---+-------------------+--------+-------------+------+------+
7372  *
7373  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7374  */
7375 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7376                              int imm5)
7377 {
7378     int size = ctz32(imm5);
7379     int index;
7380 
7381     if (size > 3 || (size == 3 && !is_q)) {
7382         unallocated_encoding(s);
7383         return;
7384     }
7385 
7386     if (!fp_access_check(s)) {
7387         return;
7388     }
7389 
7390     index = imm5 >> (size + 1);
7391     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7392                          vec_reg_offset(s, rn, index, size),
7393                          is_q ? 16 : 8, vec_full_reg_size(s));
7394 }
7395 
7396 /* DUP (element, scalar)
7397  *  31                   21 20    16 15        10  9    5 4    0
7398  * +-----------------------+--------+-------------+------+------+
7399  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7400  * +-----------------------+--------+-------------+------+------+
7401  */
7402 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7403                               int imm5)
7404 {
7405     int size = ctz32(imm5);
7406     int index;
7407     TCGv_i64 tmp;
7408 
7409     if (size > 3) {
7410         unallocated_encoding(s);
7411         return;
7412     }
7413 
7414     if (!fp_access_check(s)) {
7415         return;
7416     }
7417 
7418     index = imm5 >> (size + 1);
7419 
7420     /* This instruction just extracts the specified element and
7421      * zero-extends it into the bottom of the destination register.
7422      */
7423     tmp = tcg_temp_new_i64();
7424     read_vec_element(s, tmp, rn, index, size);
7425     write_fp_dreg(s, rd, tmp);
7426 }
7427 
7428 /* DUP (General)
7429  *
7430  *  31  30   29              21 20    16 15        10  9    5 4    0
7431  * +---+---+-------------------+--------+-------------+------+------+
7432  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7433  * +---+---+-------------------+--------+-------------+------+------+
7434  *
7435  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7436  */
7437 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7438                              int imm5)
7439 {
7440     int size = ctz32(imm5);
7441     uint32_t dofs, oprsz, maxsz;
7442 
7443     if (size > 3 || ((size == 3) && !is_q)) {
7444         unallocated_encoding(s);
7445         return;
7446     }
7447 
7448     if (!fp_access_check(s)) {
7449         return;
7450     }
7451 
7452     dofs = vec_full_reg_offset(s, rd);
7453     oprsz = is_q ? 16 : 8;
7454     maxsz = vec_full_reg_size(s);
7455 
7456     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7457 }
7458 
7459 /* INS (Element)
7460  *
7461  *  31                   21 20    16 15  14    11  10 9    5 4    0
7462  * +-----------------------+--------+------------+---+------+------+
7463  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7464  * +-----------------------+--------+------------+---+------+------+
7465  *
7466  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7467  * index: encoded in imm5<4:size+1>
7468  */
7469 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7470                              int imm4, int imm5)
7471 {
7472     int size = ctz32(imm5);
7473     int src_index, dst_index;
7474     TCGv_i64 tmp;
7475 
7476     if (size > 3) {
7477         unallocated_encoding(s);
7478         return;
7479     }
7480 
7481     if (!fp_access_check(s)) {
7482         return;
7483     }
7484 
7485     dst_index = extract32(imm5, 1+size, 5);
7486     src_index = extract32(imm4, size, 4);
7487 
7488     tmp = tcg_temp_new_i64();
7489 
7490     read_vec_element(s, tmp, rn, src_index, size);
7491     write_vec_element(s, tmp, rd, dst_index, size);
7492 
7493     /* INS is considered a 128-bit write for SVE. */
7494     clear_vec_high(s, true, rd);
7495 }
7496 
7497 
7498 /* INS (General)
7499  *
7500  *  31                   21 20    16 15        10  9    5 4    0
7501  * +-----------------------+--------+-------------+------+------+
7502  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7503  * +-----------------------+--------+-------------+------+------+
7504  *
7505  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7506  * index: encoded in imm5<4:size+1>
7507  */
7508 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7509 {
7510     int size = ctz32(imm5);
7511     int idx;
7512 
7513     if (size > 3) {
7514         unallocated_encoding(s);
7515         return;
7516     }
7517 
7518     if (!fp_access_check(s)) {
7519         return;
7520     }
7521 
7522     idx = extract32(imm5, 1 + size, 4 - size);
7523     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7524 
7525     /* INS is considered a 128-bit write for SVE. */
7526     clear_vec_high(s, true, rd);
7527 }
7528 
7529 /*
7530  * UMOV (General)
7531  * SMOV (General)
7532  *
7533  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7534  * +---+---+-------------------+--------+-------------+------+------+
7535  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7536  * +---+---+-------------------+--------+-------------+------+------+
7537  *
7538  * U: unsigned when set
7539  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7540  */
7541 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7542                                   int rn, int rd, int imm5)
7543 {
7544     int size = ctz32(imm5);
7545     int element;
7546     TCGv_i64 tcg_rd;
7547 
7548     /* Check for UnallocatedEncodings */
7549     if (is_signed) {
7550         if (size > 2 || (size == 2 && !is_q)) {
7551             unallocated_encoding(s);
7552             return;
7553         }
7554     } else {
7555         if (size > 3
7556             || (size < 3 && is_q)
7557             || (size == 3 && !is_q)) {
7558             unallocated_encoding(s);
7559             return;
7560         }
7561     }
7562 
7563     if (!fp_access_check(s)) {
7564         return;
7565     }
7566 
7567     element = extract32(imm5, 1+size, 4);
7568 
7569     tcg_rd = cpu_reg(s, rd);
7570     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7571     if (is_signed && !is_q) {
7572         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7573     }
7574 }
7575 
7576 /* AdvSIMD copy
7577  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7578  * +---+---+----+-----------------+------+---+------+---+------+------+
7579  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7580  * +---+---+----+-----------------+------+---+------+---+------+------+
7581  */
7582 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7583 {
7584     int rd = extract32(insn, 0, 5);
7585     int rn = extract32(insn, 5, 5);
7586     int imm4 = extract32(insn, 11, 4);
7587     int op = extract32(insn, 29, 1);
7588     int is_q = extract32(insn, 30, 1);
7589     int imm5 = extract32(insn, 16, 5);
7590 
7591     if (op) {
7592         if (is_q) {
7593             /* INS (element) */
7594             handle_simd_inse(s, rd, rn, imm4, imm5);
7595         } else {
7596             unallocated_encoding(s);
7597         }
7598     } else {
7599         switch (imm4) {
7600         case 0:
7601             /* DUP (element - vector) */
7602             handle_simd_dupe(s, is_q, rd, rn, imm5);
7603             break;
7604         case 1:
7605             /* DUP (general) */
7606             handle_simd_dupg(s, is_q, rd, rn, imm5);
7607             break;
7608         case 3:
7609             if (is_q) {
7610                 /* INS (general) */
7611                 handle_simd_insg(s, rd, rn, imm5);
7612             } else {
7613                 unallocated_encoding(s);
7614             }
7615             break;
7616         case 5:
7617         case 7:
7618             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7619             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7620             break;
7621         default:
7622             unallocated_encoding(s);
7623             break;
7624         }
7625     }
7626 }
7627 
7628 /* AdvSIMD modified immediate
7629  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7630  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7631  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7632  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7633  *
7634  * There are a number of operations that can be carried out here:
7635  *   MOVI - move (shifted) imm into register
7636  *   MVNI - move inverted (shifted) imm into register
7637  *   ORR  - bitwise OR of (shifted) imm with register
7638  *   BIC  - bitwise clear of (shifted) imm with register
7639  * With ARMv8.2 we also have:
7640  *   FMOV half-precision
7641  */
7642 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7643 {
7644     int rd = extract32(insn, 0, 5);
7645     int cmode = extract32(insn, 12, 4);
7646     int o2 = extract32(insn, 11, 1);
7647     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7648     bool is_neg = extract32(insn, 29, 1);
7649     bool is_q = extract32(insn, 30, 1);
7650     uint64_t imm = 0;
7651 
7652     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7653         /* Check for FMOV (vector, immediate) - half-precision */
7654         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7655             unallocated_encoding(s);
7656             return;
7657         }
7658     }
7659 
7660     if (!fp_access_check(s)) {
7661         return;
7662     }
7663 
7664     if (cmode == 15 && o2 && !is_neg) {
7665         /* FMOV (vector, immediate) - half-precision */
7666         imm = vfp_expand_imm(MO_16, abcdefgh);
7667         /* now duplicate across the lanes */
7668         imm = dup_const(MO_16, imm);
7669     } else {
7670         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7671     }
7672 
7673     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7674         /* MOVI or MVNI, with MVNI negation handled above.  */
7675         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7676                              vec_full_reg_size(s), imm);
7677     } else {
7678         /* ORR or BIC, with BIC negation to AND handled above.  */
7679         if (is_neg) {
7680             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7681         } else {
7682             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7683         }
7684     }
7685 }
7686 
7687 /* AdvSIMD scalar copy
7688  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7689  * +-----+----+-----------------+------+---+------+---+------+------+
7690  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7691  * +-----+----+-----------------+------+---+------+---+------+------+
7692  */
7693 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7694 {
7695     int rd = extract32(insn, 0, 5);
7696     int rn = extract32(insn, 5, 5);
7697     int imm4 = extract32(insn, 11, 4);
7698     int imm5 = extract32(insn, 16, 5);
7699     int op = extract32(insn, 29, 1);
7700 
7701     if (op != 0 || imm4 != 0) {
7702         unallocated_encoding(s);
7703         return;
7704     }
7705 
7706     /* DUP (element, scalar) */
7707     handle_simd_dupes(s, rd, rn, imm5);
7708 }
7709 
7710 /* AdvSIMD scalar pairwise
7711  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7712  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7713  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7714  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7715  */
7716 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7717 {
7718     int u = extract32(insn, 29, 1);
7719     int size = extract32(insn, 22, 2);
7720     int opcode = extract32(insn, 12, 5);
7721     int rn = extract32(insn, 5, 5);
7722     int rd = extract32(insn, 0, 5);
7723     TCGv_ptr fpst;
7724 
7725     /* For some ops (the FP ones), size[1] is part of the encoding.
7726      * For ADDP strictly it is not but size[1] is always 1 for valid
7727      * encodings.
7728      */
7729     opcode |= (extract32(size, 1, 1) << 5);
7730 
7731     switch (opcode) {
7732     case 0x3b: /* ADDP */
7733         if (u || size != 3) {
7734             unallocated_encoding(s);
7735             return;
7736         }
7737         if (!fp_access_check(s)) {
7738             return;
7739         }
7740 
7741         fpst = NULL;
7742         break;
7743     case 0xc: /* FMAXNMP */
7744     case 0xd: /* FADDP */
7745     case 0xf: /* FMAXP */
7746     case 0x2c: /* FMINNMP */
7747     case 0x2f: /* FMINP */
7748         /* FP op, size[0] is 32 or 64 bit*/
7749         if (!u) {
7750             if (!dc_isar_feature(aa64_fp16, s)) {
7751                 unallocated_encoding(s);
7752                 return;
7753             } else {
7754                 size = MO_16;
7755             }
7756         } else {
7757             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7758         }
7759 
7760         if (!fp_access_check(s)) {
7761             return;
7762         }
7763 
7764         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7765         break;
7766     default:
7767         unallocated_encoding(s);
7768         return;
7769     }
7770 
7771     if (size == MO_64) {
7772         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7773         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7774         TCGv_i64 tcg_res = tcg_temp_new_i64();
7775 
7776         read_vec_element(s, tcg_op1, rn, 0, MO_64);
7777         read_vec_element(s, tcg_op2, rn, 1, MO_64);
7778 
7779         switch (opcode) {
7780         case 0x3b: /* ADDP */
7781             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7782             break;
7783         case 0xc: /* FMAXNMP */
7784             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7785             break;
7786         case 0xd: /* FADDP */
7787             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7788             break;
7789         case 0xf: /* FMAXP */
7790             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7791             break;
7792         case 0x2c: /* FMINNMP */
7793             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7794             break;
7795         case 0x2f: /* FMINP */
7796             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7797             break;
7798         default:
7799             g_assert_not_reached();
7800         }
7801 
7802         write_fp_dreg(s, rd, tcg_res);
7803     } else {
7804         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7805         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7806         TCGv_i32 tcg_res = tcg_temp_new_i32();
7807 
7808         read_vec_element_i32(s, tcg_op1, rn, 0, size);
7809         read_vec_element_i32(s, tcg_op2, rn, 1, size);
7810 
7811         if (size == MO_16) {
7812             switch (opcode) {
7813             case 0xc: /* FMAXNMP */
7814                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7815                 break;
7816             case 0xd: /* FADDP */
7817                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7818                 break;
7819             case 0xf: /* FMAXP */
7820                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7821                 break;
7822             case 0x2c: /* FMINNMP */
7823                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7824                 break;
7825             case 0x2f: /* FMINP */
7826                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7827                 break;
7828             default:
7829                 g_assert_not_reached();
7830             }
7831         } else {
7832             switch (opcode) {
7833             case 0xc: /* FMAXNMP */
7834                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7835                 break;
7836             case 0xd: /* FADDP */
7837                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7838                 break;
7839             case 0xf: /* FMAXP */
7840                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7841                 break;
7842             case 0x2c: /* FMINNMP */
7843                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7844                 break;
7845             case 0x2f: /* FMINP */
7846                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7847                 break;
7848             default:
7849                 g_assert_not_reached();
7850             }
7851         }
7852 
7853         write_fp_sreg(s, rd, tcg_res);
7854     }
7855 }
7856 
7857 /*
7858  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7859  *
7860  * This code is handles the common shifting code and is used by both
7861  * the vector and scalar code.
7862  */
7863 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7864                                     TCGv_i64 tcg_rnd, bool accumulate,
7865                                     bool is_u, int size, int shift)
7866 {
7867     bool extended_result = false;
7868     bool round = tcg_rnd != NULL;
7869     int ext_lshift = 0;
7870     TCGv_i64 tcg_src_hi;
7871 
7872     if (round && size == 3) {
7873         extended_result = true;
7874         ext_lshift = 64 - shift;
7875         tcg_src_hi = tcg_temp_new_i64();
7876     } else if (shift == 64) {
7877         if (!accumulate && is_u) {
7878             /* result is zero */
7879             tcg_gen_movi_i64(tcg_res, 0);
7880             return;
7881         }
7882     }
7883 
7884     /* Deal with the rounding step */
7885     if (round) {
7886         if (extended_result) {
7887             TCGv_i64 tcg_zero = tcg_constant_i64(0);
7888             if (!is_u) {
7889                 /* take care of sign extending tcg_res */
7890                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
7891                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7892                                  tcg_src, tcg_src_hi,
7893                                  tcg_rnd, tcg_zero);
7894             } else {
7895                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7896                                  tcg_src, tcg_zero,
7897                                  tcg_rnd, tcg_zero);
7898             }
7899         } else {
7900             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
7901         }
7902     }
7903 
7904     /* Now do the shift right */
7905     if (round && extended_result) {
7906         /* extended case, >64 bit precision required */
7907         if (ext_lshift == 0) {
7908             /* special case, only high bits matter */
7909             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
7910         } else {
7911             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7912             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
7913             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
7914         }
7915     } else {
7916         if (is_u) {
7917             if (shift == 64) {
7918                 /* essentially shifting in 64 zeros */
7919                 tcg_gen_movi_i64(tcg_src, 0);
7920             } else {
7921                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7922             }
7923         } else {
7924             if (shift == 64) {
7925                 /* effectively extending the sign-bit */
7926                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
7927             } else {
7928                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
7929             }
7930         }
7931     }
7932 
7933     if (accumulate) {
7934         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
7935     } else {
7936         tcg_gen_mov_i64(tcg_res, tcg_src);
7937     }
7938 }
7939 
7940 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
7941 static void handle_scalar_simd_shri(DisasContext *s,
7942                                     bool is_u, int immh, int immb,
7943                                     int opcode, int rn, int rd)
7944 {
7945     const int size = 3;
7946     int immhb = immh << 3 | immb;
7947     int shift = 2 * (8 << size) - immhb;
7948     bool accumulate = false;
7949     bool round = false;
7950     bool insert = false;
7951     TCGv_i64 tcg_rn;
7952     TCGv_i64 tcg_rd;
7953     TCGv_i64 tcg_round;
7954 
7955     if (!extract32(immh, 3, 1)) {
7956         unallocated_encoding(s);
7957         return;
7958     }
7959 
7960     if (!fp_access_check(s)) {
7961         return;
7962     }
7963 
7964     switch (opcode) {
7965     case 0x02: /* SSRA / USRA (accumulate) */
7966         accumulate = true;
7967         break;
7968     case 0x04: /* SRSHR / URSHR (rounding) */
7969         round = true;
7970         break;
7971     case 0x06: /* SRSRA / URSRA (accum + rounding) */
7972         accumulate = round = true;
7973         break;
7974     case 0x08: /* SRI */
7975         insert = true;
7976         break;
7977     }
7978 
7979     if (round) {
7980         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
7981     } else {
7982         tcg_round = NULL;
7983     }
7984 
7985     tcg_rn = read_fp_dreg(s, rn);
7986     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
7987 
7988     if (insert) {
7989         /* shift count same as element size is valid but does nothing;
7990          * special case to avoid potential shift by 64.
7991          */
7992         int esize = 8 << size;
7993         if (shift != esize) {
7994             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
7995             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
7996         }
7997     } else {
7998         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7999                                 accumulate, is_u, size, shift);
8000     }
8001 
8002     write_fp_dreg(s, rd, tcg_rd);
8003 }
8004 
8005 /* SHL/SLI - Scalar shift left */
8006 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8007                                     int immh, int immb, int opcode,
8008                                     int rn, int rd)
8009 {
8010     int size = 32 - clz32(immh) - 1;
8011     int immhb = immh << 3 | immb;
8012     int shift = immhb - (8 << size);
8013     TCGv_i64 tcg_rn;
8014     TCGv_i64 tcg_rd;
8015 
8016     if (!extract32(immh, 3, 1)) {
8017         unallocated_encoding(s);
8018         return;
8019     }
8020 
8021     if (!fp_access_check(s)) {
8022         return;
8023     }
8024 
8025     tcg_rn = read_fp_dreg(s, rn);
8026     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8027 
8028     if (insert) {
8029         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8030     } else {
8031         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8032     }
8033 
8034     write_fp_dreg(s, rd, tcg_rd);
8035 }
8036 
8037 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8038  * (signed/unsigned) narrowing */
8039 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8040                                    bool is_u_shift, bool is_u_narrow,
8041                                    int immh, int immb, int opcode,
8042                                    int rn, int rd)
8043 {
8044     int immhb = immh << 3 | immb;
8045     int size = 32 - clz32(immh) - 1;
8046     int esize = 8 << size;
8047     int shift = (2 * esize) - immhb;
8048     int elements = is_scalar ? 1 : (64 / esize);
8049     bool round = extract32(opcode, 0, 1);
8050     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8051     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8052     TCGv_i32 tcg_rd_narrowed;
8053     TCGv_i64 tcg_final;
8054 
8055     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8056         { gen_helper_neon_narrow_sat_s8,
8057           gen_helper_neon_unarrow_sat8 },
8058         { gen_helper_neon_narrow_sat_s16,
8059           gen_helper_neon_unarrow_sat16 },
8060         { gen_helper_neon_narrow_sat_s32,
8061           gen_helper_neon_unarrow_sat32 },
8062         { NULL, NULL },
8063     };
8064     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8065         gen_helper_neon_narrow_sat_u8,
8066         gen_helper_neon_narrow_sat_u16,
8067         gen_helper_neon_narrow_sat_u32,
8068         NULL
8069     };
8070     NeonGenNarrowEnvFn *narrowfn;
8071 
8072     int i;
8073 
8074     assert(size < 4);
8075 
8076     if (extract32(immh, 3, 1)) {
8077         unallocated_encoding(s);
8078         return;
8079     }
8080 
8081     if (!fp_access_check(s)) {
8082         return;
8083     }
8084 
8085     if (is_u_shift) {
8086         narrowfn = unsigned_narrow_fns[size];
8087     } else {
8088         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8089     }
8090 
8091     tcg_rn = tcg_temp_new_i64();
8092     tcg_rd = tcg_temp_new_i64();
8093     tcg_rd_narrowed = tcg_temp_new_i32();
8094     tcg_final = tcg_temp_new_i64();
8095 
8096     if (round) {
8097         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8098     } else {
8099         tcg_round = NULL;
8100     }
8101 
8102     for (i = 0; i < elements; i++) {
8103         read_vec_element(s, tcg_rn, rn, i, ldop);
8104         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8105                                 false, is_u_shift, size+1, shift);
8106         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8107         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8108         if (i == 0) {
8109             tcg_gen_mov_i64(tcg_final, tcg_rd);
8110         } else {
8111             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8112         }
8113     }
8114 
8115     if (!is_q) {
8116         write_vec_element(s, tcg_final, rd, 0, MO_64);
8117     } else {
8118         write_vec_element(s, tcg_final, rd, 1, MO_64);
8119     }
8120     clear_vec_high(s, is_q, rd);
8121 }
8122 
8123 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8124 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8125                              bool src_unsigned, bool dst_unsigned,
8126                              int immh, int immb, int rn, int rd)
8127 {
8128     int immhb = immh << 3 | immb;
8129     int size = 32 - clz32(immh) - 1;
8130     int shift = immhb - (8 << size);
8131     int pass;
8132 
8133     assert(immh != 0);
8134     assert(!(scalar && is_q));
8135 
8136     if (!scalar) {
8137         if (!is_q && extract32(immh, 3, 1)) {
8138             unallocated_encoding(s);
8139             return;
8140         }
8141 
8142         /* Since we use the variable-shift helpers we must
8143          * replicate the shift count into each element of
8144          * the tcg_shift value.
8145          */
8146         switch (size) {
8147         case 0:
8148             shift |= shift << 8;
8149             /* fall through */
8150         case 1:
8151             shift |= shift << 16;
8152             break;
8153         case 2:
8154         case 3:
8155             break;
8156         default:
8157             g_assert_not_reached();
8158         }
8159     }
8160 
8161     if (!fp_access_check(s)) {
8162         return;
8163     }
8164 
8165     if (size == 3) {
8166         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8167         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8168             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8169             { NULL, gen_helper_neon_qshl_u64 },
8170         };
8171         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8172         int maxpass = is_q ? 2 : 1;
8173 
8174         for (pass = 0; pass < maxpass; pass++) {
8175             TCGv_i64 tcg_op = tcg_temp_new_i64();
8176 
8177             read_vec_element(s, tcg_op, rn, pass, MO_64);
8178             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8179             write_vec_element(s, tcg_op, rd, pass, MO_64);
8180         }
8181         clear_vec_high(s, is_q, rd);
8182     } else {
8183         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8184         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8185             {
8186                 { gen_helper_neon_qshl_s8,
8187                   gen_helper_neon_qshl_s16,
8188                   gen_helper_neon_qshl_s32 },
8189                 { gen_helper_neon_qshlu_s8,
8190                   gen_helper_neon_qshlu_s16,
8191                   gen_helper_neon_qshlu_s32 }
8192             }, {
8193                 { NULL, NULL, NULL },
8194                 { gen_helper_neon_qshl_u8,
8195                   gen_helper_neon_qshl_u16,
8196                   gen_helper_neon_qshl_u32 }
8197             }
8198         };
8199         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8200         MemOp memop = scalar ? size : MO_32;
8201         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8202 
8203         for (pass = 0; pass < maxpass; pass++) {
8204             TCGv_i32 tcg_op = tcg_temp_new_i32();
8205 
8206             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8207             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8208             if (scalar) {
8209                 switch (size) {
8210                 case 0:
8211                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8212                     break;
8213                 case 1:
8214                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8215                     break;
8216                 case 2:
8217                     break;
8218                 default:
8219                     g_assert_not_reached();
8220                 }
8221                 write_fp_sreg(s, rd, tcg_op);
8222             } else {
8223                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8224             }
8225         }
8226 
8227         if (!scalar) {
8228             clear_vec_high(s, is_q, rd);
8229         }
8230     }
8231 }
8232 
8233 /* Common vector code for handling integer to FP conversion */
8234 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8235                                    int elements, int is_signed,
8236                                    int fracbits, int size)
8237 {
8238     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8239     TCGv_i32 tcg_shift = NULL;
8240 
8241     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8242     int pass;
8243 
8244     if (fracbits || size == MO_64) {
8245         tcg_shift = tcg_constant_i32(fracbits);
8246     }
8247 
8248     if (size == MO_64) {
8249         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8250         TCGv_i64 tcg_double = tcg_temp_new_i64();
8251 
8252         for (pass = 0; pass < elements; pass++) {
8253             read_vec_element(s, tcg_int64, rn, pass, mop);
8254 
8255             if (is_signed) {
8256                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8257                                      tcg_shift, tcg_fpst);
8258             } else {
8259                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8260                                      tcg_shift, tcg_fpst);
8261             }
8262             if (elements == 1) {
8263                 write_fp_dreg(s, rd, tcg_double);
8264             } else {
8265                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8266             }
8267         }
8268     } else {
8269         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8270         TCGv_i32 tcg_float = tcg_temp_new_i32();
8271 
8272         for (pass = 0; pass < elements; pass++) {
8273             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8274 
8275             switch (size) {
8276             case MO_32:
8277                 if (fracbits) {
8278                     if (is_signed) {
8279                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8280                                              tcg_shift, tcg_fpst);
8281                     } else {
8282                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8283                                              tcg_shift, tcg_fpst);
8284                     }
8285                 } else {
8286                     if (is_signed) {
8287                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8288                     } else {
8289                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8290                     }
8291                 }
8292                 break;
8293             case MO_16:
8294                 if (fracbits) {
8295                     if (is_signed) {
8296                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8297                                              tcg_shift, tcg_fpst);
8298                     } else {
8299                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8300                                              tcg_shift, tcg_fpst);
8301                     }
8302                 } else {
8303                     if (is_signed) {
8304                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8305                     } else {
8306                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8307                     }
8308                 }
8309                 break;
8310             default:
8311                 g_assert_not_reached();
8312             }
8313 
8314             if (elements == 1) {
8315                 write_fp_sreg(s, rd, tcg_float);
8316             } else {
8317                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8318             }
8319         }
8320     }
8321 
8322     clear_vec_high(s, elements << size == 16, rd);
8323 }
8324 
8325 /* UCVTF/SCVTF - Integer to FP conversion */
8326 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8327                                          bool is_q, bool is_u,
8328                                          int immh, int immb, int opcode,
8329                                          int rn, int rd)
8330 {
8331     int size, elements, fracbits;
8332     int immhb = immh << 3 | immb;
8333 
8334     if (immh & 8) {
8335         size = MO_64;
8336         if (!is_scalar && !is_q) {
8337             unallocated_encoding(s);
8338             return;
8339         }
8340     } else if (immh & 4) {
8341         size = MO_32;
8342     } else if (immh & 2) {
8343         size = MO_16;
8344         if (!dc_isar_feature(aa64_fp16, s)) {
8345             unallocated_encoding(s);
8346             return;
8347         }
8348     } else {
8349         /* immh == 0 would be a failure of the decode logic */
8350         g_assert(immh == 1);
8351         unallocated_encoding(s);
8352         return;
8353     }
8354 
8355     if (is_scalar) {
8356         elements = 1;
8357     } else {
8358         elements = (8 << is_q) >> size;
8359     }
8360     fracbits = (16 << size) - immhb;
8361 
8362     if (!fp_access_check(s)) {
8363         return;
8364     }
8365 
8366     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8367 }
8368 
8369 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8370 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8371                                          bool is_q, bool is_u,
8372                                          int immh, int immb, int rn, int rd)
8373 {
8374     int immhb = immh << 3 | immb;
8375     int pass, size, fracbits;
8376     TCGv_ptr tcg_fpstatus;
8377     TCGv_i32 tcg_rmode, tcg_shift;
8378 
8379     if (immh & 0x8) {
8380         size = MO_64;
8381         if (!is_scalar && !is_q) {
8382             unallocated_encoding(s);
8383             return;
8384         }
8385     } else if (immh & 0x4) {
8386         size = MO_32;
8387     } else if (immh & 0x2) {
8388         size = MO_16;
8389         if (!dc_isar_feature(aa64_fp16, s)) {
8390             unallocated_encoding(s);
8391             return;
8392         }
8393     } else {
8394         /* Should have split out AdvSIMD modified immediate earlier.  */
8395         assert(immh == 1);
8396         unallocated_encoding(s);
8397         return;
8398     }
8399 
8400     if (!fp_access_check(s)) {
8401         return;
8402     }
8403 
8404     assert(!(is_scalar && is_q));
8405 
8406     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8407     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8408     fracbits = (16 << size) - immhb;
8409     tcg_shift = tcg_constant_i32(fracbits);
8410 
8411     if (size == MO_64) {
8412         int maxpass = is_scalar ? 1 : 2;
8413 
8414         for (pass = 0; pass < maxpass; pass++) {
8415             TCGv_i64 tcg_op = tcg_temp_new_i64();
8416 
8417             read_vec_element(s, tcg_op, rn, pass, MO_64);
8418             if (is_u) {
8419                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8420             } else {
8421                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8422             }
8423             write_vec_element(s, tcg_op, rd, pass, MO_64);
8424         }
8425         clear_vec_high(s, is_q, rd);
8426     } else {
8427         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8428         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8429 
8430         switch (size) {
8431         case MO_16:
8432             if (is_u) {
8433                 fn = gen_helper_vfp_touhh;
8434             } else {
8435                 fn = gen_helper_vfp_toshh;
8436             }
8437             break;
8438         case MO_32:
8439             if (is_u) {
8440                 fn = gen_helper_vfp_touls;
8441             } else {
8442                 fn = gen_helper_vfp_tosls;
8443             }
8444             break;
8445         default:
8446             g_assert_not_reached();
8447         }
8448 
8449         for (pass = 0; pass < maxpass; pass++) {
8450             TCGv_i32 tcg_op = tcg_temp_new_i32();
8451 
8452             read_vec_element_i32(s, tcg_op, rn, pass, size);
8453             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8454             if (is_scalar) {
8455                 write_fp_sreg(s, rd, tcg_op);
8456             } else {
8457                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8458             }
8459         }
8460         if (!is_scalar) {
8461             clear_vec_high(s, is_q, rd);
8462         }
8463     }
8464 
8465     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8466 }
8467 
8468 /* AdvSIMD scalar shift by immediate
8469  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8470  * +-----+---+-------------+------+------+--------+---+------+------+
8471  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8472  * +-----+---+-------------+------+------+--------+---+------+------+
8473  *
8474  * This is the scalar version so it works on a fixed sized registers
8475  */
8476 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8477 {
8478     int rd = extract32(insn, 0, 5);
8479     int rn = extract32(insn, 5, 5);
8480     int opcode = extract32(insn, 11, 5);
8481     int immb = extract32(insn, 16, 3);
8482     int immh = extract32(insn, 19, 4);
8483     bool is_u = extract32(insn, 29, 1);
8484 
8485     if (immh == 0) {
8486         unallocated_encoding(s);
8487         return;
8488     }
8489 
8490     switch (opcode) {
8491     case 0x08: /* SRI */
8492         if (!is_u) {
8493             unallocated_encoding(s);
8494             return;
8495         }
8496         /* fall through */
8497     case 0x00: /* SSHR / USHR */
8498     case 0x02: /* SSRA / USRA */
8499     case 0x04: /* SRSHR / URSHR */
8500     case 0x06: /* SRSRA / URSRA */
8501         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8502         break;
8503     case 0x0a: /* SHL / SLI */
8504         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8505         break;
8506     case 0x1c: /* SCVTF, UCVTF */
8507         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8508                                      opcode, rn, rd);
8509         break;
8510     case 0x10: /* SQSHRUN, SQSHRUN2 */
8511     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8512         if (!is_u) {
8513             unallocated_encoding(s);
8514             return;
8515         }
8516         handle_vec_simd_sqshrn(s, true, false, false, true,
8517                                immh, immb, opcode, rn, rd);
8518         break;
8519     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8520     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8521         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8522                                immh, immb, opcode, rn, rd);
8523         break;
8524     case 0xc: /* SQSHLU */
8525         if (!is_u) {
8526             unallocated_encoding(s);
8527             return;
8528         }
8529         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8530         break;
8531     case 0xe: /* SQSHL, UQSHL */
8532         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8533         break;
8534     case 0x1f: /* FCVTZS, FCVTZU */
8535         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8536         break;
8537     default:
8538         unallocated_encoding(s);
8539         break;
8540     }
8541 }
8542 
8543 /* AdvSIMD scalar three different
8544  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8545  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8546  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8547  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8548  */
8549 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8550 {
8551     bool is_u = extract32(insn, 29, 1);
8552     int size = extract32(insn, 22, 2);
8553     int opcode = extract32(insn, 12, 4);
8554     int rm = extract32(insn, 16, 5);
8555     int rn = extract32(insn, 5, 5);
8556     int rd = extract32(insn, 0, 5);
8557 
8558     if (is_u) {
8559         unallocated_encoding(s);
8560         return;
8561     }
8562 
8563     switch (opcode) {
8564     case 0x9: /* SQDMLAL, SQDMLAL2 */
8565     case 0xb: /* SQDMLSL, SQDMLSL2 */
8566     case 0xd: /* SQDMULL, SQDMULL2 */
8567         if (size == 0 || size == 3) {
8568             unallocated_encoding(s);
8569             return;
8570         }
8571         break;
8572     default:
8573         unallocated_encoding(s);
8574         return;
8575     }
8576 
8577     if (!fp_access_check(s)) {
8578         return;
8579     }
8580 
8581     if (size == 2) {
8582         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8583         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8584         TCGv_i64 tcg_res = tcg_temp_new_i64();
8585 
8586         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8587         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8588 
8589         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8590         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8591 
8592         switch (opcode) {
8593         case 0xd: /* SQDMULL, SQDMULL2 */
8594             break;
8595         case 0xb: /* SQDMLSL, SQDMLSL2 */
8596             tcg_gen_neg_i64(tcg_res, tcg_res);
8597             /* fall through */
8598         case 0x9: /* SQDMLAL, SQDMLAL2 */
8599             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8600             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8601                                               tcg_res, tcg_op1);
8602             break;
8603         default:
8604             g_assert_not_reached();
8605         }
8606 
8607         write_fp_dreg(s, rd, tcg_res);
8608     } else {
8609         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8610         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8611         TCGv_i64 tcg_res = tcg_temp_new_i64();
8612 
8613         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8614         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8615 
8616         switch (opcode) {
8617         case 0xd: /* SQDMULL, SQDMULL2 */
8618             break;
8619         case 0xb: /* SQDMLSL, SQDMLSL2 */
8620             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8621             /* fall through */
8622         case 0x9: /* SQDMLAL, SQDMLAL2 */
8623         {
8624             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8625             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8626             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8627                                               tcg_res, tcg_op3);
8628             break;
8629         }
8630         default:
8631             g_assert_not_reached();
8632         }
8633 
8634         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8635         write_fp_dreg(s, rd, tcg_res);
8636     }
8637 }
8638 
8639 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8640                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8641 {
8642     /* Handle 64x64->64 opcodes which are shared between the scalar
8643      * and vector 3-same groups. We cover every opcode where size == 3
8644      * is valid in either the three-reg-same (integer, not pairwise)
8645      * or scalar-three-reg-same groups.
8646      */
8647     TCGCond cond;
8648 
8649     switch (opcode) {
8650     case 0x1: /* SQADD */
8651         if (u) {
8652             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8653         } else {
8654             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8655         }
8656         break;
8657     case 0x5: /* SQSUB */
8658         if (u) {
8659             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8660         } else {
8661             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8662         }
8663         break;
8664     case 0x6: /* CMGT, CMHI */
8665         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8666     do_cmop:
8667         /* 64 bit integer comparison, result = test ? -1 : 0. */
8668         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8669         break;
8670     case 0x7: /* CMGE, CMHS */
8671         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8672         goto do_cmop;
8673     case 0x11: /* CMTST, CMEQ */
8674         if (u) {
8675             cond = TCG_COND_EQ;
8676             goto do_cmop;
8677         }
8678         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8679         break;
8680     case 0x8: /* SSHL, USHL */
8681         if (u) {
8682             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8683         } else {
8684             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8685         }
8686         break;
8687     case 0x9: /* SQSHL, UQSHL */
8688         if (u) {
8689             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8690         } else {
8691             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8692         }
8693         break;
8694     case 0xa: /* SRSHL, URSHL */
8695         if (u) {
8696             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8697         } else {
8698             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8699         }
8700         break;
8701     case 0xb: /* SQRSHL, UQRSHL */
8702         if (u) {
8703             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8704         } else {
8705             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8706         }
8707         break;
8708     case 0x10: /* ADD, SUB */
8709         if (u) {
8710             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8711         } else {
8712             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8713         }
8714         break;
8715     default:
8716         g_assert_not_reached();
8717     }
8718 }
8719 
8720 /* Handle the 3-same-operands float operations; shared by the scalar
8721  * and vector encodings. The caller must filter out any encodings
8722  * not allocated for the encoding it is dealing with.
8723  */
8724 static void handle_3same_float(DisasContext *s, int size, int elements,
8725                                int fpopcode, int rd, int rn, int rm)
8726 {
8727     int pass;
8728     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8729 
8730     for (pass = 0; pass < elements; pass++) {
8731         if (size) {
8732             /* Double */
8733             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8734             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8735             TCGv_i64 tcg_res = tcg_temp_new_i64();
8736 
8737             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8738             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8739 
8740             switch (fpopcode) {
8741             case 0x39: /* FMLS */
8742                 /* As usual for ARM, separate negation for fused multiply-add */
8743                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8744                 /* fall through */
8745             case 0x19: /* FMLA */
8746                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8747                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8748                                        tcg_res, fpst);
8749                 break;
8750             case 0x18: /* FMAXNM */
8751                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8752                 break;
8753             case 0x1a: /* FADD */
8754                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8755                 break;
8756             case 0x1b: /* FMULX */
8757                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8758                 break;
8759             case 0x1c: /* FCMEQ */
8760                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8761                 break;
8762             case 0x1e: /* FMAX */
8763                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8764                 break;
8765             case 0x1f: /* FRECPS */
8766                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8767                 break;
8768             case 0x38: /* FMINNM */
8769                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8770                 break;
8771             case 0x3a: /* FSUB */
8772                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8773                 break;
8774             case 0x3e: /* FMIN */
8775                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8776                 break;
8777             case 0x3f: /* FRSQRTS */
8778                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8779                 break;
8780             case 0x5b: /* FMUL */
8781                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8782                 break;
8783             case 0x5c: /* FCMGE */
8784                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8785                 break;
8786             case 0x5d: /* FACGE */
8787                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8788                 break;
8789             case 0x5f: /* FDIV */
8790                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8791                 break;
8792             case 0x7a: /* FABD */
8793                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8794                 gen_helper_vfp_absd(tcg_res, tcg_res);
8795                 break;
8796             case 0x7c: /* FCMGT */
8797                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8798                 break;
8799             case 0x7d: /* FACGT */
8800                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8801                 break;
8802             default:
8803                 g_assert_not_reached();
8804             }
8805 
8806             write_vec_element(s, tcg_res, rd, pass, MO_64);
8807         } else {
8808             /* Single */
8809             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8810             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8811             TCGv_i32 tcg_res = tcg_temp_new_i32();
8812 
8813             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8814             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8815 
8816             switch (fpopcode) {
8817             case 0x39: /* FMLS */
8818                 /* As usual for ARM, separate negation for fused multiply-add */
8819                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8820                 /* fall through */
8821             case 0x19: /* FMLA */
8822                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8823                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8824                                        tcg_res, fpst);
8825                 break;
8826             case 0x1a: /* FADD */
8827                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8828                 break;
8829             case 0x1b: /* FMULX */
8830                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8831                 break;
8832             case 0x1c: /* FCMEQ */
8833                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8834                 break;
8835             case 0x1e: /* FMAX */
8836                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8837                 break;
8838             case 0x1f: /* FRECPS */
8839                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8840                 break;
8841             case 0x18: /* FMAXNM */
8842                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8843                 break;
8844             case 0x38: /* FMINNM */
8845                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8846                 break;
8847             case 0x3a: /* FSUB */
8848                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8849                 break;
8850             case 0x3e: /* FMIN */
8851                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8852                 break;
8853             case 0x3f: /* FRSQRTS */
8854                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8855                 break;
8856             case 0x5b: /* FMUL */
8857                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
8858                 break;
8859             case 0x5c: /* FCMGE */
8860                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8861                 break;
8862             case 0x5d: /* FACGE */
8863                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8864                 break;
8865             case 0x5f: /* FDIV */
8866                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
8867                 break;
8868             case 0x7a: /* FABD */
8869                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8870                 gen_helper_vfp_abss(tcg_res, tcg_res);
8871                 break;
8872             case 0x7c: /* FCMGT */
8873                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8874                 break;
8875             case 0x7d: /* FACGT */
8876                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8877                 break;
8878             default:
8879                 g_assert_not_reached();
8880             }
8881 
8882             if (elements == 1) {
8883                 /* scalar single so clear high part */
8884                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8885 
8886                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
8887                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
8888             } else {
8889                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8890             }
8891         }
8892     }
8893 
8894     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
8895 }
8896 
8897 /* AdvSIMD scalar three same
8898  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
8899  * +-----+---+-----------+------+---+------+--------+---+------+------+
8900  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
8901  * +-----+---+-----------+------+---+------+--------+---+------+------+
8902  */
8903 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
8904 {
8905     int rd = extract32(insn, 0, 5);
8906     int rn = extract32(insn, 5, 5);
8907     int opcode = extract32(insn, 11, 5);
8908     int rm = extract32(insn, 16, 5);
8909     int size = extract32(insn, 22, 2);
8910     bool u = extract32(insn, 29, 1);
8911     TCGv_i64 tcg_rd;
8912 
8913     if (opcode >= 0x18) {
8914         /* Floating point: U, size[1] and opcode indicate operation */
8915         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
8916         switch (fpopcode) {
8917         case 0x1b: /* FMULX */
8918         case 0x1f: /* FRECPS */
8919         case 0x3f: /* FRSQRTS */
8920         case 0x5d: /* FACGE */
8921         case 0x7d: /* FACGT */
8922         case 0x1c: /* FCMEQ */
8923         case 0x5c: /* FCMGE */
8924         case 0x7c: /* FCMGT */
8925         case 0x7a: /* FABD */
8926             break;
8927         default:
8928             unallocated_encoding(s);
8929             return;
8930         }
8931 
8932         if (!fp_access_check(s)) {
8933             return;
8934         }
8935 
8936         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
8937         return;
8938     }
8939 
8940     switch (opcode) {
8941     case 0x1: /* SQADD, UQADD */
8942     case 0x5: /* SQSUB, UQSUB */
8943     case 0x9: /* SQSHL, UQSHL */
8944     case 0xb: /* SQRSHL, UQRSHL */
8945         break;
8946     case 0x8: /* SSHL, USHL */
8947     case 0xa: /* SRSHL, URSHL */
8948     case 0x6: /* CMGT, CMHI */
8949     case 0x7: /* CMGE, CMHS */
8950     case 0x11: /* CMTST, CMEQ */
8951     case 0x10: /* ADD, SUB (vector) */
8952         if (size != 3) {
8953             unallocated_encoding(s);
8954             return;
8955         }
8956         break;
8957     case 0x16: /* SQDMULH, SQRDMULH (vector) */
8958         if (size != 1 && size != 2) {
8959             unallocated_encoding(s);
8960             return;
8961         }
8962         break;
8963     default:
8964         unallocated_encoding(s);
8965         return;
8966     }
8967 
8968     if (!fp_access_check(s)) {
8969         return;
8970     }
8971 
8972     tcg_rd = tcg_temp_new_i64();
8973 
8974     if (size == 3) {
8975         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8976         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
8977 
8978         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
8979     } else {
8980         /* Do a single operation on the lowest element in the vector.
8981          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
8982          * no side effects for all these operations.
8983          * OPTME: special-purpose helpers would avoid doing some
8984          * unnecessary work in the helper for the 8 and 16 bit cases.
8985          */
8986         NeonGenTwoOpEnvFn *genenvfn;
8987         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8988         TCGv_i32 tcg_rm = tcg_temp_new_i32();
8989         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
8990 
8991         read_vec_element_i32(s, tcg_rn, rn, 0, size);
8992         read_vec_element_i32(s, tcg_rm, rm, 0, size);
8993 
8994         switch (opcode) {
8995         case 0x1: /* SQADD, UQADD */
8996         {
8997             static NeonGenTwoOpEnvFn * const fns[3][2] = {
8998                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
8999                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9000                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9001             };
9002             genenvfn = fns[size][u];
9003             break;
9004         }
9005         case 0x5: /* SQSUB, UQSUB */
9006         {
9007             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9008                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9009                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9010                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9011             };
9012             genenvfn = fns[size][u];
9013             break;
9014         }
9015         case 0x9: /* SQSHL, UQSHL */
9016         {
9017             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9018                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9019                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9020                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9021             };
9022             genenvfn = fns[size][u];
9023             break;
9024         }
9025         case 0xb: /* SQRSHL, UQRSHL */
9026         {
9027             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9028                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9029                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9030                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9031             };
9032             genenvfn = fns[size][u];
9033             break;
9034         }
9035         case 0x16: /* SQDMULH, SQRDMULH */
9036         {
9037             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9038                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9039                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9040             };
9041             assert(size == 1 || size == 2);
9042             genenvfn = fns[size - 1][u];
9043             break;
9044         }
9045         default:
9046             g_assert_not_reached();
9047         }
9048 
9049         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9050         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9051     }
9052 
9053     write_fp_dreg(s, rd, tcg_rd);
9054 }
9055 
9056 /* AdvSIMD scalar three same FP16
9057  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9058  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9059  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9060  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9061  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9062  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9063  */
9064 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9065                                                   uint32_t insn)
9066 {
9067     int rd = extract32(insn, 0, 5);
9068     int rn = extract32(insn, 5, 5);
9069     int opcode = extract32(insn, 11, 3);
9070     int rm = extract32(insn, 16, 5);
9071     bool u = extract32(insn, 29, 1);
9072     bool a = extract32(insn, 23, 1);
9073     int fpopcode = opcode | (a << 3) |  (u << 4);
9074     TCGv_ptr fpst;
9075     TCGv_i32 tcg_op1;
9076     TCGv_i32 tcg_op2;
9077     TCGv_i32 tcg_res;
9078 
9079     switch (fpopcode) {
9080     case 0x03: /* FMULX */
9081     case 0x04: /* FCMEQ (reg) */
9082     case 0x07: /* FRECPS */
9083     case 0x0f: /* FRSQRTS */
9084     case 0x14: /* FCMGE (reg) */
9085     case 0x15: /* FACGE */
9086     case 0x1a: /* FABD */
9087     case 0x1c: /* FCMGT (reg) */
9088     case 0x1d: /* FACGT */
9089         break;
9090     default:
9091         unallocated_encoding(s);
9092         return;
9093     }
9094 
9095     if (!dc_isar_feature(aa64_fp16, s)) {
9096         unallocated_encoding(s);
9097     }
9098 
9099     if (!fp_access_check(s)) {
9100         return;
9101     }
9102 
9103     fpst = fpstatus_ptr(FPST_FPCR_F16);
9104 
9105     tcg_op1 = read_fp_hreg(s, rn);
9106     tcg_op2 = read_fp_hreg(s, rm);
9107     tcg_res = tcg_temp_new_i32();
9108 
9109     switch (fpopcode) {
9110     case 0x03: /* FMULX */
9111         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9112         break;
9113     case 0x04: /* FCMEQ (reg) */
9114         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9115         break;
9116     case 0x07: /* FRECPS */
9117         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9118         break;
9119     case 0x0f: /* FRSQRTS */
9120         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9121         break;
9122     case 0x14: /* FCMGE (reg) */
9123         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9124         break;
9125     case 0x15: /* FACGE */
9126         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9127         break;
9128     case 0x1a: /* FABD */
9129         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9130         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9131         break;
9132     case 0x1c: /* FCMGT (reg) */
9133         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9134         break;
9135     case 0x1d: /* FACGT */
9136         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9137         break;
9138     default:
9139         g_assert_not_reached();
9140     }
9141 
9142     write_fp_sreg(s, rd, tcg_res);
9143 }
9144 
9145 /* AdvSIMD scalar three same extra
9146  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9147  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9148  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9149  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9150  */
9151 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9152                                                    uint32_t insn)
9153 {
9154     int rd = extract32(insn, 0, 5);
9155     int rn = extract32(insn, 5, 5);
9156     int opcode = extract32(insn, 11, 4);
9157     int rm = extract32(insn, 16, 5);
9158     int size = extract32(insn, 22, 2);
9159     bool u = extract32(insn, 29, 1);
9160     TCGv_i32 ele1, ele2, ele3;
9161     TCGv_i64 res;
9162     bool feature;
9163 
9164     switch (u * 16 + opcode) {
9165     case 0x10: /* SQRDMLAH (vector) */
9166     case 0x11: /* SQRDMLSH (vector) */
9167         if (size != 1 && size != 2) {
9168             unallocated_encoding(s);
9169             return;
9170         }
9171         feature = dc_isar_feature(aa64_rdm, s);
9172         break;
9173     default:
9174         unallocated_encoding(s);
9175         return;
9176     }
9177     if (!feature) {
9178         unallocated_encoding(s);
9179         return;
9180     }
9181     if (!fp_access_check(s)) {
9182         return;
9183     }
9184 
9185     /* Do a single operation on the lowest element in the vector.
9186      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9187      * with no side effects for all these operations.
9188      * OPTME: special-purpose helpers would avoid doing some
9189      * unnecessary work in the helper for the 16 bit cases.
9190      */
9191     ele1 = tcg_temp_new_i32();
9192     ele2 = tcg_temp_new_i32();
9193     ele3 = tcg_temp_new_i32();
9194 
9195     read_vec_element_i32(s, ele1, rn, 0, size);
9196     read_vec_element_i32(s, ele2, rm, 0, size);
9197     read_vec_element_i32(s, ele3, rd, 0, size);
9198 
9199     switch (opcode) {
9200     case 0x0: /* SQRDMLAH */
9201         if (size == 1) {
9202             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9203         } else {
9204             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9205         }
9206         break;
9207     case 0x1: /* SQRDMLSH */
9208         if (size == 1) {
9209             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9210         } else {
9211             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9212         }
9213         break;
9214     default:
9215         g_assert_not_reached();
9216     }
9217 
9218     res = tcg_temp_new_i64();
9219     tcg_gen_extu_i32_i64(res, ele3);
9220     write_fp_dreg(s, rd, res);
9221 }
9222 
9223 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9224                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9225                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9226 {
9227     /* Handle 64->64 opcodes which are shared between the scalar and
9228      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9229      * is valid in either group and also the double-precision fp ops.
9230      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9231      * requires them.
9232      */
9233     TCGCond cond;
9234 
9235     switch (opcode) {
9236     case 0x4: /* CLS, CLZ */
9237         if (u) {
9238             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9239         } else {
9240             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9241         }
9242         break;
9243     case 0x5: /* NOT */
9244         /* This opcode is shared with CNT and RBIT but we have earlier
9245          * enforced that size == 3 if and only if this is the NOT insn.
9246          */
9247         tcg_gen_not_i64(tcg_rd, tcg_rn);
9248         break;
9249     case 0x7: /* SQABS, SQNEG */
9250         if (u) {
9251             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9252         } else {
9253             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9254         }
9255         break;
9256     case 0xa: /* CMLT */
9257         cond = TCG_COND_LT;
9258     do_cmop:
9259         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9260         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9261         break;
9262     case 0x8: /* CMGT, CMGE */
9263         cond = u ? TCG_COND_GE : TCG_COND_GT;
9264         goto do_cmop;
9265     case 0x9: /* CMEQ, CMLE */
9266         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9267         goto do_cmop;
9268     case 0xb: /* ABS, NEG */
9269         if (u) {
9270             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9271         } else {
9272             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9273         }
9274         break;
9275     case 0x2f: /* FABS */
9276         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9277         break;
9278     case 0x6f: /* FNEG */
9279         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9280         break;
9281     case 0x7f: /* FSQRT */
9282         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9283         break;
9284     case 0x1a: /* FCVTNS */
9285     case 0x1b: /* FCVTMS */
9286     case 0x1c: /* FCVTAS */
9287     case 0x3a: /* FCVTPS */
9288     case 0x3b: /* FCVTZS */
9289         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9290         break;
9291     case 0x5a: /* FCVTNU */
9292     case 0x5b: /* FCVTMU */
9293     case 0x5c: /* FCVTAU */
9294     case 0x7a: /* FCVTPU */
9295     case 0x7b: /* FCVTZU */
9296         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9297         break;
9298     case 0x18: /* FRINTN */
9299     case 0x19: /* FRINTM */
9300     case 0x38: /* FRINTP */
9301     case 0x39: /* FRINTZ */
9302     case 0x58: /* FRINTA */
9303     case 0x79: /* FRINTI */
9304         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9305         break;
9306     case 0x59: /* FRINTX */
9307         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9308         break;
9309     case 0x1e: /* FRINT32Z */
9310     case 0x5e: /* FRINT32X */
9311         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9312         break;
9313     case 0x1f: /* FRINT64Z */
9314     case 0x5f: /* FRINT64X */
9315         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9316         break;
9317     default:
9318         g_assert_not_reached();
9319     }
9320 }
9321 
9322 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9323                                    bool is_scalar, bool is_u, bool is_q,
9324                                    int size, int rn, int rd)
9325 {
9326     bool is_double = (size == MO_64);
9327     TCGv_ptr fpst;
9328 
9329     if (!fp_access_check(s)) {
9330         return;
9331     }
9332 
9333     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9334 
9335     if (is_double) {
9336         TCGv_i64 tcg_op = tcg_temp_new_i64();
9337         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9338         TCGv_i64 tcg_res = tcg_temp_new_i64();
9339         NeonGenTwoDoubleOpFn *genfn;
9340         bool swap = false;
9341         int pass;
9342 
9343         switch (opcode) {
9344         case 0x2e: /* FCMLT (zero) */
9345             swap = true;
9346             /* fallthrough */
9347         case 0x2c: /* FCMGT (zero) */
9348             genfn = gen_helper_neon_cgt_f64;
9349             break;
9350         case 0x2d: /* FCMEQ (zero) */
9351             genfn = gen_helper_neon_ceq_f64;
9352             break;
9353         case 0x6d: /* FCMLE (zero) */
9354             swap = true;
9355             /* fall through */
9356         case 0x6c: /* FCMGE (zero) */
9357             genfn = gen_helper_neon_cge_f64;
9358             break;
9359         default:
9360             g_assert_not_reached();
9361         }
9362 
9363         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9364             read_vec_element(s, tcg_op, rn, pass, MO_64);
9365             if (swap) {
9366                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9367             } else {
9368                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9369             }
9370             write_vec_element(s, tcg_res, rd, pass, MO_64);
9371         }
9372 
9373         clear_vec_high(s, !is_scalar, rd);
9374     } else {
9375         TCGv_i32 tcg_op = tcg_temp_new_i32();
9376         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9377         TCGv_i32 tcg_res = tcg_temp_new_i32();
9378         NeonGenTwoSingleOpFn *genfn;
9379         bool swap = false;
9380         int pass, maxpasses;
9381 
9382         if (size == MO_16) {
9383             switch (opcode) {
9384             case 0x2e: /* FCMLT (zero) */
9385                 swap = true;
9386                 /* fall through */
9387             case 0x2c: /* FCMGT (zero) */
9388                 genfn = gen_helper_advsimd_cgt_f16;
9389                 break;
9390             case 0x2d: /* FCMEQ (zero) */
9391                 genfn = gen_helper_advsimd_ceq_f16;
9392                 break;
9393             case 0x6d: /* FCMLE (zero) */
9394                 swap = true;
9395                 /* fall through */
9396             case 0x6c: /* FCMGE (zero) */
9397                 genfn = gen_helper_advsimd_cge_f16;
9398                 break;
9399             default:
9400                 g_assert_not_reached();
9401             }
9402         } else {
9403             switch (opcode) {
9404             case 0x2e: /* FCMLT (zero) */
9405                 swap = true;
9406                 /* fall through */
9407             case 0x2c: /* FCMGT (zero) */
9408                 genfn = gen_helper_neon_cgt_f32;
9409                 break;
9410             case 0x2d: /* FCMEQ (zero) */
9411                 genfn = gen_helper_neon_ceq_f32;
9412                 break;
9413             case 0x6d: /* FCMLE (zero) */
9414                 swap = true;
9415                 /* fall through */
9416             case 0x6c: /* FCMGE (zero) */
9417                 genfn = gen_helper_neon_cge_f32;
9418                 break;
9419             default:
9420                 g_assert_not_reached();
9421             }
9422         }
9423 
9424         if (is_scalar) {
9425             maxpasses = 1;
9426         } else {
9427             int vector_size = 8 << is_q;
9428             maxpasses = vector_size >> size;
9429         }
9430 
9431         for (pass = 0; pass < maxpasses; pass++) {
9432             read_vec_element_i32(s, tcg_op, rn, pass, size);
9433             if (swap) {
9434                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9435             } else {
9436                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9437             }
9438             if (is_scalar) {
9439                 write_fp_sreg(s, rd, tcg_res);
9440             } else {
9441                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9442             }
9443         }
9444 
9445         if (!is_scalar) {
9446             clear_vec_high(s, is_q, rd);
9447         }
9448     }
9449 }
9450 
9451 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9452                                     bool is_scalar, bool is_u, bool is_q,
9453                                     int size, int rn, int rd)
9454 {
9455     bool is_double = (size == 3);
9456     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9457 
9458     if (is_double) {
9459         TCGv_i64 tcg_op = tcg_temp_new_i64();
9460         TCGv_i64 tcg_res = tcg_temp_new_i64();
9461         int pass;
9462 
9463         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9464             read_vec_element(s, tcg_op, rn, pass, MO_64);
9465             switch (opcode) {
9466             case 0x3d: /* FRECPE */
9467                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9468                 break;
9469             case 0x3f: /* FRECPX */
9470                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9471                 break;
9472             case 0x7d: /* FRSQRTE */
9473                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9474                 break;
9475             default:
9476                 g_assert_not_reached();
9477             }
9478             write_vec_element(s, tcg_res, rd, pass, MO_64);
9479         }
9480         clear_vec_high(s, !is_scalar, rd);
9481     } else {
9482         TCGv_i32 tcg_op = tcg_temp_new_i32();
9483         TCGv_i32 tcg_res = tcg_temp_new_i32();
9484         int pass, maxpasses;
9485 
9486         if (is_scalar) {
9487             maxpasses = 1;
9488         } else {
9489             maxpasses = is_q ? 4 : 2;
9490         }
9491 
9492         for (pass = 0; pass < maxpasses; pass++) {
9493             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9494 
9495             switch (opcode) {
9496             case 0x3c: /* URECPE */
9497                 gen_helper_recpe_u32(tcg_res, tcg_op);
9498                 break;
9499             case 0x3d: /* FRECPE */
9500                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9501                 break;
9502             case 0x3f: /* FRECPX */
9503                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9504                 break;
9505             case 0x7d: /* FRSQRTE */
9506                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9507                 break;
9508             default:
9509                 g_assert_not_reached();
9510             }
9511 
9512             if (is_scalar) {
9513                 write_fp_sreg(s, rd, tcg_res);
9514             } else {
9515                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9516             }
9517         }
9518         if (!is_scalar) {
9519             clear_vec_high(s, is_q, rd);
9520         }
9521     }
9522 }
9523 
9524 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9525                                 int opcode, bool u, bool is_q,
9526                                 int size, int rn, int rd)
9527 {
9528     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9529      * in the source becomes a size element in the destination).
9530      */
9531     int pass;
9532     TCGv_i32 tcg_res[2];
9533     int destelt = is_q ? 2 : 0;
9534     int passes = scalar ? 1 : 2;
9535 
9536     if (scalar) {
9537         tcg_res[1] = tcg_constant_i32(0);
9538     }
9539 
9540     for (pass = 0; pass < passes; pass++) {
9541         TCGv_i64 tcg_op = tcg_temp_new_i64();
9542         NeonGenNarrowFn *genfn = NULL;
9543         NeonGenNarrowEnvFn *genenvfn = NULL;
9544 
9545         if (scalar) {
9546             read_vec_element(s, tcg_op, rn, pass, size + 1);
9547         } else {
9548             read_vec_element(s, tcg_op, rn, pass, MO_64);
9549         }
9550         tcg_res[pass] = tcg_temp_new_i32();
9551 
9552         switch (opcode) {
9553         case 0x12: /* XTN, SQXTUN */
9554         {
9555             static NeonGenNarrowFn * const xtnfns[3] = {
9556                 gen_helper_neon_narrow_u8,
9557                 gen_helper_neon_narrow_u16,
9558                 tcg_gen_extrl_i64_i32,
9559             };
9560             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9561                 gen_helper_neon_unarrow_sat8,
9562                 gen_helper_neon_unarrow_sat16,
9563                 gen_helper_neon_unarrow_sat32,
9564             };
9565             if (u) {
9566                 genenvfn = sqxtunfns[size];
9567             } else {
9568                 genfn = xtnfns[size];
9569             }
9570             break;
9571         }
9572         case 0x14: /* SQXTN, UQXTN */
9573         {
9574             static NeonGenNarrowEnvFn * const fns[3][2] = {
9575                 { gen_helper_neon_narrow_sat_s8,
9576                   gen_helper_neon_narrow_sat_u8 },
9577                 { gen_helper_neon_narrow_sat_s16,
9578                   gen_helper_neon_narrow_sat_u16 },
9579                 { gen_helper_neon_narrow_sat_s32,
9580                   gen_helper_neon_narrow_sat_u32 },
9581             };
9582             genenvfn = fns[size][u];
9583             break;
9584         }
9585         case 0x16: /* FCVTN, FCVTN2 */
9586             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9587             if (size == 2) {
9588                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9589             } else {
9590                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9591                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9592                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9593                 TCGv_i32 ahp = get_ahp_flag();
9594 
9595                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9596                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9597                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9598                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9599             }
9600             break;
9601         case 0x36: /* BFCVTN, BFCVTN2 */
9602             {
9603                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9604                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9605             }
9606             break;
9607         case 0x56:  /* FCVTXN, FCVTXN2 */
9608             /* 64 bit to 32 bit float conversion
9609              * with von Neumann rounding (round to odd)
9610              */
9611             assert(size == 2);
9612             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9613             break;
9614         default:
9615             g_assert_not_reached();
9616         }
9617 
9618         if (genfn) {
9619             genfn(tcg_res[pass], tcg_op);
9620         } else if (genenvfn) {
9621             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9622         }
9623     }
9624 
9625     for (pass = 0; pass < 2; pass++) {
9626         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9627     }
9628     clear_vec_high(s, is_q, rd);
9629 }
9630 
9631 /* Remaining saturating accumulating ops */
9632 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9633                                 bool is_q, int size, int rn, int rd)
9634 {
9635     bool is_double = (size == 3);
9636 
9637     if (is_double) {
9638         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9639         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9640         int pass;
9641 
9642         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9643             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9644             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9645 
9646             if (is_u) { /* USQADD */
9647                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9648             } else { /* SUQADD */
9649                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9650             }
9651             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9652         }
9653         clear_vec_high(s, !is_scalar, rd);
9654     } else {
9655         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9656         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9657         int pass, maxpasses;
9658 
9659         if (is_scalar) {
9660             maxpasses = 1;
9661         } else {
9662             maxpasses = is_q ? 4 : 2;
9663         }
9664 
9665         for (pass = 0; pass < maxpasses; pass++) {
9666             if (is_scalar) {
9667                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9668                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9669             } else {
9670                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9671                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9672             }
9673 
9674             if (is_u) { /* USQADD */
9675                 switch (size) {
9676                 case 0:
9677                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9678                     break;
9679                 case 1:
9680                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9681                     break;
9682                 case 2:
9683                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9684                     break;
9685                 default:
9686                     g_assert_not_reached();
9687                 }
9688             } else { /* SUQADD */
9689                 switch (size) {
9690                 case 0:
9691                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9692                     break;
9693                 case 1:
9694                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9695                     break;
9696                 case 2:
9697                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9698                     break;
9699                 default:
9700                     g_assert_not_reached();
9701                 }
9702             }
9703 
9704             if (is_scalar) {
9705                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9706             }
9707             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9708         }
9709         clear_vec_high(s, is_q, rd);
9710     }
9711 }
9712 
9713 /* AdvSIMD scalar two reg misc
9714  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9715  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9716  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9717  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9718  */
9719 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9720 {
9721     int rd = extract32(insn, 0, 5);
9722     int rn = extract32(insn, 5, 5);
9723     int opcode = extract32(insn, 12, 5);
9724     int size = extract32(insn, 22, 2);
9725     bool u = extract32(insn, 29, 1);
9726     bool is_fcvt = false;
9727     int rmode;
9728     TCGv_i32 tcg_rmode;
9729     TCGv_ptr tcg_fpstatus;
9730 
9731     switch (opcode) {
9732     case 0x3: /* USQADD / SUQADD*/
9733         if (!fp_access_check(s)) {
9734             return;
9735         }
9736         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9737         return;
9738     case 0x7: /* SQABS / SQNEG */
9739         break;
9740     case 0xa: /* CMLT */
9741         if (u) {
9742             unallocated_encoding(s);
9743             return;
9744         }
9745         /* fall through */
9746     case 0x8: /* CMGT, CMGE */
9747     case 0x9: /* CMEQ, CMLE */
9748     case 0xb: /* ABS, NEG */
9749         if (size != 3) {
9750             unallocated_encoding(s);
9751             return;
9752         }
9753         break;
9754     case 0x12: /* SQXTUN */
9755         if (!u) {
9756             unallocated_encoding(s);
9757             return;
9758         }
9759         /* fall through */
9760     case 0x14: /* SQXTN, UQXTN */
9761         if (size == 3) {
9762             unallocated_encoding(s);
9763             return;
9764         }
9765         if (!fp_access_check(s)) {
9766             return;
9767         }
9768         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9769         return;
9770     case 0xc ... 0xf:
9771     case 0x16 ... 0x1d:
9772     case 0x1f:
9773         /* Floating point: U, size[1] and opcode indicate operation;
9774          * size[0] indicates single or double precision.
9775          */
9776         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9777         size = extract32(size, 0, 1) ? 3 : 2;
9778         switch (opcode) {
9779         case 0x2c: /* FCMGT (zero) */
9780         case 0x2d: /* FCMEQ (zero) */
9781         case 0x2e: /* FCMLT (zero) */
9782         case 0x6c: /* FCMGE (zero) */
9783         case 0x6d: /* FCMLE (zero) */
9784             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9785             return;
9786         case 0x1d: /* SCVTF */
9787         case 0x5d: /* UCVTF */
9788         {
9789             bool is_signed = (opcode == 0x1d);
9790             if (!fp_access_check(s)) {
9791                 return;
9792             }
9793             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9794             return;
9795         }
9796         case 0x3d: /* FRECPE */
9797         case 0x3f: /* FRECPX */
9798         case 0x7d: /* FRSQRTE */
9799             if (!fp_access_check(s)) {
9800                 return;
9801             }
9802             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9803             return;
9804         case 0x1a: /* FCVTNS */
9805         case 0x1b: /* FCVTMS */
9806         case 0x3a: /* FCVTPS */
9807         case 0x3b: /* FCVTZS */
9808         case 0x5a: /* FCVTNU */
9809         case 0x5b: /* FCVTMU */
9810         case 0x7a: /* FCVTPU */
9811         case 0x7b: /* FCVTZU */
9812             is_fcvt = true;
9813             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9814             break;
9815         case 0x1c: /* FCVTAS */
9816         case 0x5c: /* FCVTAU */
9817             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9818             is_fcvt = true;
9819             rmode = FPROUNDING_TIEAWAY;
9820             break;
9821         case 0x56: /* FCVTXN, FCVTXN2 */
9822             if (size == 2) {
9823                 unallocated_encoding(s);
9824                 return;
9825             }
9826             if (!fp_access_check(s)) {
9827                 return;
9828             }
9829             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9830             return;
9831         default:
9832             unallocated_encoding(s);
9833             return;
9834         }
9835         break;
9836     default:
9837         unallocated_encoding(s);
9838         return;
9839     }
9840 
9841     if (!fp_access_check(s)) {
9842         return;
9843     }
9844 
9845     if (is_fcvt) {
9846         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
9847         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
9848     } else {
9849         tcg_fpstatus = NULL;
9850         tcg_rmode = NULL;
9851     }
9852 
9853     if (size == 3) {
9854         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9855         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9856 
9857         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
9858         write_fp_dreg(s, rd, tcg_rd);
9859     } else {
9860         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9861         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9862 
9863         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9864 
9865         switch (opcode) {
9866         case 0x7: /* SQABS, SQNEG */
9867         {
9868             NeonGenOneOpEnvFn *genfn;
9869             static NeonGenOneOpEnvFn * const fns[3][2] = {
9870                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
9871                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
9872                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
9873             };
9874             genfn = fns[size][u];
9875             genfn(tcg_rd, cpu_env, tcg_rn);
9876             break;
9877         }
9878         case 0x1a: /* FCVTNS */
9879         case 0x1b: /* FCVTMS */
9880         case 0x1c: /* FCVTAS */
9881         case 0x3a: /* FCVTPS */
9882         case 0x3b: /* FCVTZS */
9883             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
9884                                  tcg_fpstatus);
9885             break;
9886         case 0x5a: /* FCVTNU */
9887         case 0x5b: /* FCVTMU */
9888         case 0x5c: /* FCVTAU */
9889         case 0x7a: /* FCVTPU */
9890         case 0x7b: /* FCVTZU */
9891             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
9892                                  tcg_fpstatus);
9893             break;
9894         default:
9895             g_assert_not_reached();
9896         }
9897 
9898         write_fp_sreg(s, rd, tcg_rd);
9899     }
9900 
9901     if (is_fcvt) {
9902         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
9903     }
9904 }
9905 
9906 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
9907 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
9908                                  int immh, int immb, int opcode, int rn, int rd)
9909 {
9910     int size = 32 - clz32(immh) - 1;
9911     int immhb = immh << 3 | immb;
9912     int shift = 2 * (8 << size) - immhb;
9913     GVecGen2iFn *gvec_fn;
9914 
9915     if (extract32(immh, 3, 1) && !is_q) {
9916         unallocated_encoding(s);
9917         return;
9918     }
9919     tcg_debug_assert(size <= 3);
9920 
9921     if (!fp_access_check(s)) {
9922         return;
9923     }
9924 
9925     switch (opcode) {
9926     case 0x02: /* SSRA / USRA (accumulate) */
9927         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
9928         break;
9929 
9930     case 0x08: /* SRI */
9931         gvec_fn = gen_gvec_sri;
9932         break;
9933 
9934     case 0x00: /* SSHR / USHR */
9935         if (is_u) {
9936             if (shift == 8 << size) {
9937                 /* Shift count the same size as element size produces zero.  */
9938                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
9939                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
9940                 return;
9941             }
9942             gvec_fn = tcg_gen_gvec_shri;
9943         } else {
9944             /* Shift count the same size as element size produces all sign.  */
9945             if (shift == 8 << size) {
9946                 shift -= 1;
9947             }
9948             gvec_fn = tcg_gen_gvec_sari;
9949         }
9950         break;
9951 
9952     case 0x04: /* SRSHR / URSHR (rounding) */
9953         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
9954         break;
9955 
9956     case 0x06: /* SRSRA / URSRA (accum + rounding) */
9957         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
9958         break;
9959 
9960     default:
9961         g_assert_not_reached();
9962     }
9963 
9964     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
9965 }
9966 
9967 /* SHL/SLI - Vector shift left */
9968 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
9969                                  int immh, int immb, int opcode, int rn, int rd)
9970 {
9971     int size = 32 - clz32(immh) - 1;
9972     int immhb = immh << 3 | immb;
9973     int shift = immhb - (8 << size);
9974 
9975     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
9976     assert(size >= 0 && size <= 3);
9977 
9978     if (extract32(immh, 3, 1) && !is_q) {
9979         unallocated_encoding(s);
9980         return;
9981     }
9982 
9983     if (!fp_access_check(s)) {
9984         return;
9985     }
9986 
9987     if (insert) {
9988         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
9989     } else {
9990         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
9991     }
9992 }
9993 
9994 /* USHLL/SHLL - Vector shift left with widening */
9995 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
9996                                  int immh, int immb, int opcode, int rn, int rd)
9997 {
9998     int size = 32 - clz32(immh) - 1;
9999     int immhb = immh << 3 | immb;
10000     int shift = immhb - (8 << size);
10001     int dsize = 64;
10002     int esize = 8 << size;
10003     int elements = dsize/esize;
10004     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10005     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10006     int i;
10007 
10008     if (size >= 3) {
10009         unallocated_encoding(s);
10010         return;
10011     }
10012 
10013     if (!fp_access_check(s)) {
10014         return;
10015     }
10016 
10017     /* For the LL variants the store is larger than the load,
10018      * so if rd == rn we would overwrite parts of our input.
10019      * So load everything right now and use shifts in the main loop.
10020      */
10021     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10022 
10023     for (i = 0; i < elements; i++) {
10024         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10025         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10026         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10027         write_vec_element(s, tcg_rd, rd, i, size + 1);
10028     }
10029 }
10030 
10031 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10032 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10033                                  int immh, int immb, int opcode, int rn, int rd)
10034 {
10035     int immhb = immh << 3 | immb;
10036     int size = 32 - clz32(immh) - 1;
10037     int dsize = 64;
10038     int esize = 8 << size;
10039     int elements = dsize/esize;
10040     int shift = (2 * esize) - immhb;
10041     bool round = extract32(opcode, 0, 1);
10042     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10043     TCGv_i64 tcg_round;
10044     int i;
10045 
10046     if (extract32(immh, 3, 1)) {
10047         unallocated_encoding(s);
10048         return;
10049     }
10050 
10051     if (!fp_access_check(s)) {
10052         return;
10053     }
10054 
10055     tcg_rn = tcg_temp_new_i64();
10056     tcg_rd = tcg_temp_new_i64();
10057     tcg_final = tcg_temp_new_i64();
10058     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10059 
10060     if (round) {
10061         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10062     } else {
10063         tcg_round = NULL;
10064     }
10065 
10066     for (i = 0; i < elements; i++) {
10067         read_vec_element(s, tcg_rn, rn, i, size+1);
10068         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10069                                 false, true, size+1, shift);
10070 
10071         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10072     }
10073 
10074     if (!is_q) {
10075         write_vec_element(s, tcg_final, rd, 0, MO_64);
10076     } else {
10077         write_vec_element(s, tcg_final, rd, 1, MO_64);
10078     }
10079 
10080     clear_vec_high(s, is_q, rd);
10081 }
10082 
10083 
10084 /* AdvSIMD shift by immediate
10085  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10086  * +---+---+---+-------------+------+------+--------+---+------+------+
10087  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10088  * +---+---+---+-------------+------+------+--------+---+------+------+
10089  */
10090 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10091 {
10092     int rd = extract32(insn, 0, 5);
10093     int rn = extract32(insn, 5, 5);
10094     int opcode = extract32(insn, 11, 5);
10095     int immb = extract32(insn, 16, 3);
10096     int immh = extract32(insn, 19, 4);
10097     bool is_u = extract32(insn, 29, 1);
10098     bool is_q = extract32(insn, 30, 1);
10099 
10100     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10101     assert(immh != 0);
10102 
10103     switch (opcode) {
10104     case 0x08: /* SRI */
10105         if (!is_u) {
10106             unallocated_encoding(s);
10107             return;
10108         }
10109         /* fall through */
10110     case 0x00: /* SSHR / USHR */
10111     case 0x02: /* SSRA / USRA (accumulate) */
10112     case 0x04: /* SRSHR / URSHR (rounding) */
10113     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10114         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10115         break;
10116     case 0x0a: /* SHL / SLI */
10117         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10118         break;
10119     case 0x10: /* SHRN */
10120     case 0x11: /* RSHRN / SQRSHRUN */
10121         if (is_u) {
10122             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10123                                    opcode, rn, rd);
10124         } else {
10125             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10126         }
10127         break;
10128     case 0x12: /* SQSHRN / UQSHRN */
10129     case 0x13: /* SQRSHRN / UQRSHRN */
10130         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10131                                opcode, rn, rd);
10132         break;
10133     case 0x14: /* SSHLL / USHLL */
10134         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10135         break;
10136     case 0x1c: /* SCVTF / UCVTF */
10137         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10138                                      opcode, rn, rd);
10139         break;
10140     case 0xc: /* SQSHLU */
10141         if (!is_u) {
10142             unallocated_encoding(s);
10143             return;
10144         }
10145         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10146         break;
10147     case 0xe: /* SQSHL, UQSHL */
10148         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10149         break;
10150     case 0x1f: /* FCVTZS/ FCVTZU */
10151         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10152         return;
10153     default:
10154         unallocated_encoding(s);
10155         return;
10156     }
10157 }
10158 
10159 /* Generate code to do a "long" addition or subtraction, ie one done in
10160  * TCGv_i64 on vector lanes twice the width specified by size.
10161  */
10162 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10163                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10164 {
10165     static NeonGenTwo64OpFn * const fns[3][2] = {
10166         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10167         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10168         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10169     };
10170     NeonGenTwo64OpFn *genfn;
10171     assert(size < 3);
10172 
10173     genfn = fns[size][is_sub];
10174     genfn(tcg_res, tcg_op1, tcg_op2);
10175 }
10176 
10177 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10178                                 int opcode, int rd, int rn, int rm)
10179 {
10180     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10181     TCGv_i64 tcg_res[2];
10182     int pass, accop;
10183 
10184     tcg_res[0] = tcg_temp_new_i64();
10185     tcg_res[1] = tcg_temp_new_i64();
10186 
10187     /* Does this op do an adding accumulate, a subtracting accumulate,
10188      * or no accumulate at all?
10189      */
10190     switch (opcode) {
10191     case 5:
10192     case 8:
10193     case 9:
10194         accop = 1;
10195         break;
10196     case 10:
10197     case 11:
10198         accop = -1;
10199         break;
10200     default:
10201         accop = 0;
10202         break;
10203     }
10204 
10205     if (accop != 0) {
10206         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10207         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10208     }
10209 
10210     /* size == 2 means two 32x32->64 operations; this is worth special
10211      * casing because we can generally handle it inline.
10212      */
10213     if (size == 2) {
10214         for (pass = 0; pass < 2; pass++) {
10215             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10216             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10217             TCGv_i64 tcg_passres;
10218             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10219 
10220             int elt = pass + is_q * 2;
10221 
10222             read_vec_element(s, tcg_op1, rn, elt, memop);
10223             read_vec_element(s, tcg_op2, rm, elt, memop);
10224 
10225             if (accop == 0) {
10226                 tcg_passres = tcg_res[pass];
10227             } else {
10228                 tcg_passres = tcg_temp_new_i64();
10229             }
10230 
10231             switch (opcode) {
10232             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10233                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10234                 break;
10235             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10236                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10237                 break;
10238             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10239             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10240             {
10241                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10242                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10243 
10244                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10245                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10246                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10247                                     tcg_passres,
10248                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10249                 break;
10250             }
10251             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10252             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10253             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10254                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10255                 break;
10256             case 9: /* SQDMLAL, SQDMLAL2 */
10257             case 11: /* SQDMLSL, SQDMLSL2 */
10258             case 13: /* SQDMULL, SQDMULL2 */
10259                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10260                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10261                                                   tcg_passres, tcg_passres);
10262                 break;
10263             default:
10264                 g_assert_not_reached();
10265             }
10266 
10267             if (opcode == 9 || opcode == 11) {
10268                 /* saturating accumulate ops */
10269                 if (accop < 0) {
10270                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10271                 }
10272                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10273                                                   tcg_res[pass], tcg_passres);
10274             } else if (accop > 0) {
10275                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10276             } else if (accop < 0) {
10277                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10278             }
10279         }
10280     } else {
10281         /* size 0 or 1, generally helper functions */
10282         for (pass = 0; pass < 2; pass++) {
10283             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10284             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10285             TCGv_i64 tcg_passres;
10286             int elt = pass + is_q * 2;
10287 
10288             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10289             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10290 
10291             if (accop == 0) {
10292                 tcg_passres = tcg_res[pass];
10293             } else {
10294                 tcg_passres = tcg_temp_new_i64();
10295             }
10296 
10297             switch (opcode) {
10298             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10299             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10300             {
10301                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10302                 static NeonGenWidenFn * const widenfns[2][2] = {
10303                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10304                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10305                 };
10306                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10307 
10308                 widenfn(tcg_op2_64, tcg_op2);
10309                 widenfn(tcg_passres, tcg_op1);
10310                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10311                               tcg_passres, tcg_op2_64);
10312                 break;
10313             }
10314             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10315             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10316                 if (size == 0) {
10317                     if (is_u) {
10318                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10319                     } else {
10320                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10321                     }
10322                 } else {
10323                     if (is_u) {
10324                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10325                     } else {
10326                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10327                     }
10328                 }
10329                 break;
10330             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10331             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10332             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10333                 if (size == 0) {
10334                     if (is_u) {
10335                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10336                     } else {
10337                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10338                     }
10339                 } else {
10340                     if (is_u) {
10341                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10342                     } else {
10343                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10344                     }
10345                 }
10346                 break;
10347             case 9: /* SQDMLAL, SQDMLAL2 */
10348             case 11: /* SQDMLSL, SQDMLSL2 */
10349             case 13: /* SQDMULL, SQDMULL2 */
10350                 assert(size == 1);
10351                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10352                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10353                                                   tcg_passres, tcg_passres);
10354                 break;
10355             default:
10356                 g_assert_not_reached();
10357             }
10358 
10359             if (accop != 0) {
10360                 if (opcode == 9 || opcode == 11) {
10361                     /* saturating accumulate ops */
10362                     if (accop < 0) {
10363                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10364                     }
10365                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10366                                                       tcg_res[pass],
10367                                                       tcg_passres);
10368                 } else {
10369                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10370                                   tcg_res[pass], tcg_passres);
10371                 }
10372             }
10373         }
10374     }
10375 
10376     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10377     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10378 }
10379 
10380 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10381                             int opcode, int rd, int rn, int rm)
10382 {
10383     TCGv_i64 tcg_res[2];
10384     int part = is_q ? 2 : 0;
10385     int pass;
10386 
10387     for (pass = 0; pass < 2; pass++) {
10388         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10389         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10390         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10391         static NeonGenWidenFn * const widenfns[3][2] = {
10392             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10393             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10394             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10395         };
10396         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10397 
10398         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10399         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10400         widenfn(tcg_op2_wide, tcg_op2);
10401         tcg_res[pass] = tcg_temp_new_i64();
10402         gen_neon_addl(size, (opcode == 3),
10403                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10404     }
10405 
10406     for (pass = 0; pass < 2; pass++) {
10407         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10408     }
10409 }
10410 
10411 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10412 {
10413     tcg_gen_addi_i64(in, in, 1U << 31);
10414     tcg_gen_extrh_i64_i32(res, in);
10415 }
10416 
10417 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10418                                  int opcode, int rd, int rn, int rm)
10419 {
10420     TCGv_i32 tcg_res[2];
10421     int part = is_q ? 2 : 0;
10422     int pass;
10423 
10424     for (pass = 0; pass < 2; pass++) {
10425         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10426         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10427         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10428         static NeonGenNarrowFn * const narrowfns[3][2] = {
10429             { gen_helper_neon_narrow_high_u8,
10430               gen_helper_neon_narrow_round_high_u8 },
10431             { gen_helper_neon_narrow_high_u16,
10432               gen_helper_neon_narrow_round_high_u16 },
10433             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10434         };
10435         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10436 
10437         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10438         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10439 
10440         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10441 
10442         tcg_res[pass] = tcg_temp_new_i32();
10443         gennarrow(tcg_res[pass], tcg_wideres);
10444     }
10445 
10446     for (pass = 0; pass < 2; pass++) {
10447         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10448     }
10449     clear_vec_high(s, is_q, rd);
10450 }
10451 
10452 /* AdvSIMD three different
10453  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10454  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10455  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10456  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10457  */
10458 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10459 {
10460     /* Instructions in this group fall into three basic classes
10461      * (in each case with the operation working on each element in
10462      * the input vectors):
10463      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10464      *     128 bit input)
10465      * (2) wide 64 x 128 -> 128
10466      * (3) narrowing 128 x 128 -> 64
10467      * Here we do initial decode, catch unallocated cases and
10468      * dispatch to separate functions for each class.
10469      */
10470     int is_q = extract32(insn, 30, 1);
10471     int is_u = extract32(insn, 29, 1);
10472     int size = extract32(insn, 22, 2);
10473     int opcode = extract32(insn, 12, 4);
10474     int rm = extract32(insn, 16, 5);
10475     int rn = extract32(insn, 5, 5);
10476     int rd = extract32(insn, 0, 5);
10477 
10478     switch (opcode) {
10479     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10480     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10481         /* 64 x 128 -> 128 */
10482         if (size == 3) {
10483             unallocated_encoding(s);
10484             return;
10485         }
10486         if (!fp_access_check(s)) {
10487             return;
10488         }
10489         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10490         break;
10491     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10492     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10493         /* 128 x 128 -> 64 */
10494         if (size == 3) {
10495             unallocated_encoding(s);
10496             return;
10497         }
10498         if (!fp_access_check(s)) {
10499             return;
10500         }
10501         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10502         break;
10503     case 14: /* PMULL, PMULL2 */
10504         if (is_u) {
10505             unallocated_encoding(s);
10506             return;
10507         }
10508         switch (size) {
10509         case 0: /* PMULL.P8 */
10510             if (!fp_access_check(s)) {
10511                 return;
10512             }
10513             /* The Q field specifies lo/hi half input for this insn.  */
10514             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10515                              gen_helper_neon_pmull_h);
10516             break;
10517 
10518         case 3: /* PMULL.P64 */
10519             if (!dc_isar_feature(aa64_pmull, s)) {
10520                 unallocated_encoding(s);
10521                 return;
10522             }
10523             if (!fp_access_check(s)) {
10524                 return;
10525             }
10526             /* The Q field specifies lo/hi half input for this insn.  */
10527             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10528                              gen_helper_gvec_pmull_q);
10529             break;
10530 
10531         default:
10532             unallocated_encoding(s);
10533             break;
10534         }
10535         return;
10536     case 9: /* SQDMLAL, SQDMLAL2 */
10537     case 11: /* SQDMLSL, SQDMLSL2 */
10538     case 13: /* SQDMULL, SQDMULL2 */
10539         if (is_u || size == 0) {
10540             unallocated_encoding(s);
10541             return;
10542         }
10543         /* fall through */
10544     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10545     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10546     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10547     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10548     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10549     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10550     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10551         /* 64 x 64 -> 128 */
10552         if (size == 3) {
10553             unallocated_encoding(s);
10554             return;
10555         }
10556         if (!fp_access_check(s)) {
10557             return;
10558         }
10559 
10560         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10561         break;
10562     default:
10563         /* opcode 15 not allocated */
10564         unallocated_encoding(s);
10565         break;
10566     }
10567 }
10568 
10569 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10570 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10571 {
10572     int rd = extract32(insn, 0, 5);
10573     int rn = extract32(insn, 5, 5);
10574     int rm = extract32(insn, 16, 5);
10575     int size = extract32(insn, 22, 2);
10576     bool is_u = extract32(insn, 29, 1);
10577     bool is_q = extract32(insn, 30, 1);
10578 
10579     if (!fp_access_check(s)) {
10580         return;
10581     }
10582 
10583     switch (size + 4 * is_u) {
10584     case 0: /* AND */
10585         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10586         return;
10587     case 1: /* BIC */
10588         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10589         return;
10590     case 2: /* ORR */
10591         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10592         return;
10593     case 3: /* ORN */
10594         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10595         return;
10596     case 4: /* EOR */
10597         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10598         return;
10599 
10600     case 5: /* BSL bitwise select */
10601         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10602         return;
10603     case 6: /* BIT, bitwise insert if true */
10604         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10605         return;
10606     case 7: /* BIF, bitwise insert if false */
10607         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10608         return;
10609 
10610     default:
10611         g_assert_not_reached();
10612     }
10613 }
10614 
10615 /* Pairwise op subgroup of C3.6.16.
10616  *
10617  * This is called directly or via the handle_3same_float for float pairwise
10618  * operations where the opcode and size are calculated differently.
10619  */
10620 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10621                                    int size, int rn, int rm, int rd)
10622 {
10623     TCGv_ptr fpst;
10624     int pass;
10625 
10626     /* Floating point operations need fpst */
10627     if (opcode >= 0x58) {
10628         fpst = fpstatus_ptr(FPST_FPCR);
10629     } else {
10630         fpst = NULL;
10631     }
10632 
10633     if (!fp_access_check(s)) {
10634         return;
10635     }
10636 
10637     /* These operations work on the concatenated rm:rn, with each pair of
10638      * adjacent elements being operated on to produce an element in the result.
10639      */
10640     if (size == 3) {
10641         TCGv_i64 tcg_res[2];
10642 
10643         for (pass = 0; pass < 2; pass++) {
10644             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10645             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10646             int passreg = (pass == 0) ? rn : rm;
10647 
10648             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10649             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10650             tcg_res[pass] = tcg_temp_new_i64();
10651 
10652             switch (opcode) {
10653             case 0x17: /* ADDP */
10654                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10655                 break;
10656             case 0x58: /* FMAXNMP */
10657                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10658                 break;
10659             case 0x5a: /* FADDP */
10660                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10661                 break;
10662             case 0x5e: /* FMAXP */
10663                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10664                 break;
10665             case 0x78: /* FMINNMP */
10666                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10667                 break;
10668             case 0x7e: /* FMINP */
10669                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10670                 break;
10671             default:
10672                 g_assert_not_reached();
10673             }
10674         }
10675 
10676         for (pass = 0; pass < 2; pass++) {
10677             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10678         }
10679     } else {
10680         int maxpass = is_q ? 4 : 2;
10681         TCGv_i32 tcg_res[4];
10682 
10683         for (pass = 0; pass < maxpass; pass++) {
10684             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10685             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10686             NeonGenTwoOpFn *genfn = NULL;
10687             int passreg = pass < (maxpass / 2) ? rn : rm;
10688             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10689 
10690             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10691             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10692             tcg_res[pass] = tcg_temp_new_i32();
10693 
10694             switch (opcode) {
10695             case 0x17: /* ADDP */
10696             {
10697                 static NeonGenTwoOpFn * const fns[3] = {
10698                     gen_helper_neon_padd_u8,
10699                     gen_helper_neon_padd_u16,
10700                     tcg_gen_add_i32,
10701                 };
10702                 genfn = fns[size];
10703                 break;
10704             }
10705             case 0x14: /* SMAXP, UMAXP */
10706             {
10707                 static NeonGenTwoOpFn * const fns[3][2] = {
10708                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10709                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10710                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10711                 };
10712                 genfn = fns[size][u];
10713                 break;
10714             }
10715             case 0x15: /* SMINP, UMINP */
10716             {
10717                 static NeonGenTwoOpFn * const fns[3][2] = {
10718                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10719                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10720                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10721                 };
10722                 genfn = fns[size][u];
10723                 break;
10724             }
10725             /* The FP operations are all on single floats (32 bit) */
10726             case 0x58: /* FMAXNMP */
10727                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10728                 break;
10729             case 0x5a: /* FADDP */
10730                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10731                 break;
10732             case 0x5e: /* FMAXP */
10733                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10734                 break;
10735             case 0x78: /* FMINNMP */
10736                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10737                 break;
10738             case 0x7e: /* FMINP */
10739                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10740                 break;
10741             default:
10742                 g_assert_not_reached();
10743             }
10744 
10745             /* FP ops called directly, otherwise call now */
10746             if (genfn) {
10747                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10748             }
10749         }
10750 
10751         for (pass = 0; pass < maxpass; pass++) {
10752             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10753         }
10754         clear_vec_high(s, is_q, rd);
10755     }
10756 }
10757 
10758 /* Floating point op subgroup of C3.6.16. */
10759 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10760 {
10761     /* For floating point ops, the U, size[1] and opcode bits
10762      * together indicate the operation. size[0] indicates single
10763      * or double.
10764      */
10765     int fpopcode = extract32(insn, 11, 5)
10766         | (extract32(insn, 23, 1) << 5)
10767         | (extract32(insn, 29, 1) << 6);
10768     int is_q = extract32(insn, 30, 1);
10769     int size = extract32(insn, 22, 1);
10770     int rm = extract32(insn, 16, 5);
10771     int rn = extract32(insn, 5, 5);
10772     int rd = extract32(insn, 0, 5);
10773 
10774     int datasize = is_q ? 128 : 64;
10775     int esize = 32 << size;
10776     int elements = datasize / esize;
10777 
10778     if (size == 1 && !is_q) {
10779         unallocated_encoding(s);
10780         return;
10781     }
10782 
10783     switch (fpopcode) {
10784     case 0x58: /* FMAXNMP */
10785     case 0x5a: /* FADDP */
10786     case 0x5e: /* FMAXP */
10787     case 0x78: /* FMINNMP */
10788     case 0x7e: /* FMINP */
10789         if (size && !is_q) {
10790             unallocated_encoding(s);
10791             return;
10792         }
10793         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10794                                rn, rm, rd);
10795         return;
10796     case 0x1b: /* FMULX */
10797     case 0x1f: /* FRECPS */
10798     case 0x3f: /* FRSQRTS */
10799     case 0x5d: /* FACGE */
10800     case 0x7d: /* FACGT */
10801     case 0x19: /* FMLA */
10802     case 0x39: /* FMLS */
10803     case 0x18: /* FMAXNM */
10804     case 0x1a: /* FADD */
10805     case 0x1c: /* FCMEQ */
10806     case 0x1e: /* FMAX */
10807     case 0x38: /* FMINNM */
10808     case 0x3a: /* FSUB */
10809     case 0x3e: /* FMIN */
10810     case 0x5b: /* FMUL */
10811     case 0x5c: /* FCMGE */
10812     case 0x5f: /* FDIV */
10813     case 0x7a: /* FABD */
10814     case 0x7c: /* FCMGT */
10815         if (!fp_access_check(s)) {
10816             return;
10817         }
10818         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10819         return;
10820 
10821     case 0x1d: /* FMLAL  */
10822     case 0x3d: /* FMLSL  */
10823     case 0x59: /* FMLAL2 */
10824     case 0x79: /* FMLSL2 */
10825         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10826             unallocated_encoding(s);
10827             return;
10828         }
10829         if (fp_access_check(s)) {
10830             int is_s = extract32(insn, 23, 1);
10831             int is_2 = extract32(insn, 29, 1);
10832             int data = (is_2 << 1) | is_s;
10833             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10834                                vec_full_reg_offset(s, rn),
10835                                vec_full_reg_offset(s, rm), cpu_env,
10836                                is_q ? 16 : 8, vec_full_reg_size(s),
10837                                data, gen_helper_gvec_fmlal_a64);
10838         }
10839         return;
10840 
10841     default:
10842         unallocated_encoding(s);
10843         return;
10844     }
10845 }
10846 
10847 /* Integer op subgroup of C3.6.16. */
10848 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10849 {
10850     int is_q = extract32(insn, 30, 1);
10851     int u = extract32(insn, 29, 1);
10852     int size = extract32(insn, 22, 2);
10853     int opcode = extract32(insn, 11, 5);
10854     int rm = extract32(insn, 16, 5);
10855     int rn = extract32(insn, 5, 5);
10856     int rd = extract32(insn, 0, 5);
10857     int pass;
10858     TCGCond cond;
10859 
10860     switch (opcode) {
10861     case 0x13: /* MUL, PMUL */
10862         if (u && size != 0) {
10863             unallocated_encoding(s);
10864             return;
10865         }
10866         /* fall through */
10867     case 0x0: /* SHADD, UHADD */
10868     case 0x2: /* SRHADD, URHADD */
10869     case 0x4: /* SHSUB, UHSUB */
10870     case 0xc: /* SMAX, UMAX */
10871     case 0xd: /* SMIN, UMIN */
10872     case 0xe: /* SABD, UABD */
10873     case 0xf: /* SABA, UABA */
10874     case 0x12: /* MLA, MLS */
10875         if (size == 3) {
10876             unallocated_encoding(s);
10877             return;
10878         }
10879         break;
10880     case 0x16: /* SQDMULH, SQRDMULH */
10881         if (size == 0 || size == 3) {
10882             unallocated_encoding(s);
10883             return;
10884         }
10885         break;
10886     default:
10887         if (size == 3 && !is_q) {
10888             unallocated_encoding(s);
10889             return;
10890         }
10891         break;
10892     }
10893 
10894     if (!fp_access_check(s)) {
10895         return;
10896     }
10897 
10898     switch (opcode) {
10899     case 0x01: /* SQADD, UQADD */
10900         if (u) {
10901             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
10902         } else {
10903             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
10904         }
10905         return;
10906     case 0x05: /* SQSUB, UQSUB */
10907         if (u) {
10908             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
10909         } else {
10910             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
10911         }
10912         return;
10913     case 0x08: /* SSHL, USHL */
10914         if (u) {
10915             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
10916         } else {
10917             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
10918         }
10919         return;
10920     case 0x0c: /* SMAX, UMAX */
10921         if (u) {
10922             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
10923         } else {
10924             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
10925         }
10926         return;
10927     case 0x0d: /* SMIN, UMIN */
10928         if (u) {
10929             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
10930         } else {
10931             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
10932         }
10933         return;
10934     case 0xe: /* SABD, UABD */
10935         if (u) {
10936             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
10937         } else {
10938             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
10939         }
10940         return;
10941     case 0xf: /* SABA, UABA */
10942         if (u) {
10943             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
10944         } else {
10945             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
10946         }
10947         return;
10948     case 0x10: /* ADD, SUB */
10949         if (u) {
10950             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
10951         } else {
10952             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
10953         }
10954         return;
10955     case 0x13: /* MUL, PMUL */
10956         if (!u) { /* MUL */
10957             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
10958         } else {  /* PMUL */
10959             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
10960         }
10961         return;
10962     case 0x12: /* MLA, MLS */
10963         if (u) {
10964             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
10965         } else {
10966             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
10967         }
10968         return;
10969     case 0x16: /* SQDMULH, SQRDMULH */
10970         {
10971             static gen_helper_gvec_3_ptr * const fns[2][2] = {
10972                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
10973                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
10974             };
10975             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
10976         }
10977         return;
10978     case 0x11:
10979         if (!u) { /* CMTST */
10980             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
10981             return;
10982         }
10983         /* else CMEQ */
10984         cond = TCG_COND_EQ;
10985         goto do_gvec_cmp;
10986     case 0x06: /* CMGT, CMHI */
10987         cond = u ? TCG_COND_GTU : TCG_COND_GT;
10988         goto do_gvec_cmp;
10989     case 0x07: /* CMGE, CMHS */
10990         cond = u ? TCG_COND_GEU : TCG_COND_GE;
10991     do_gvec_cmp:
10992         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
10993                          vec_full_reg_offset(s, rn),
10994                          vec_full_reg_offset(s, rm),
10995                          is_q ? 16 : 8, vec_full_reg_size(s));
10996         return;
10997     }
10998 
10999     if (size == 3) {
11000         assert(is_q);
11001         for (pass = 0; pass < 2; pass++) {
11002             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11003             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11004             TCGv_i64 tcg_res = tcg_temp_new_i64();
11005 
11006             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11007             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11008 
11009             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11010 
11011             write_vec_element(s, tcg_res, rd, pass, MO_64);
11012         }
11013     } else {
11014         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11015             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11016             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11017             TCGv_i32 tcg_res = tcg_temp_new_i32();
11018             NeonGenTwoOpFn *genfn = NULL;
11019             NeonGenTwoOpEnvFn *genenvfn = NULL;
11020 
11021             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11022             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11023 
11024             switch (opcode) {
11025             case 0x0: /* SHADD, UHADD */
11026             {
11027                 static NeonGenTwoOpFn * const fns[3][2] = {
11028                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11029                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11030                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11031                 };
11032                 genfn = fns[size][u];
11033                 break;
11034             }
11035             case 0x2: /* SRHADD, URHADD */
11036             {
11037                 static NeonGenTwoOpFn * const fns[3][2] = {
11038                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11039                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11040                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11041                 };
11042                 genfn = fns[size][u];
11043                 break;
11044             }
11045             case 0x4: /* SHSUB, UHSUB */
11046             {
11047                 static NeonGenTwoOpFn * const fns[3][2] = {
11048                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11049                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11050                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11051                 };
11052                 genfn = fns[size][u];
11053                 break;
11054             }
11055             case 0x9: /* SQSHL, UQSHL */
11056             {
11057                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11058                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11059                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11060                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11061                 };
11062                 genenvfn = fns[size][u];
11063                 break;
11064             }
11065             case 0xa: /* SRSHL, URSHL */
11066             {
11067                 static NeonGenTwoOpFn * const fns[3][2] = {
11068                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11069                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11070                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11071                 };
11072                 genfn = fns[size][u];
11073                 break;
11074             }
11075             case 0xb: /* SQRSHL, UQRSHL */
11076             {
11077                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11078                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11079                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11080                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11081                 };
11082                 genenvfn = fns[size][u];
11083                 break;
11084             }
11085             default:
11086                 g_assert_not_reached();
11087             }
11088 
11089             if (genenvfn) {
11090                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11091             } else {
11092                 genfn(tcg_res, tcg_op1, tcg_op2);
11093             }
11094 
11095             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11096         }
11097     }
11098     clear_vec_high(s, is_q, rd);
11099 }
11100 
11101 /* AdvSIMD three same
11102  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11103  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11104  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11105  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11106  */
11107 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11108 {
11109     int opcode = extract32(insn, 11, 5);
11110 
11111     switch (opcode) {
11112     case 0x3: /* logic ops */
11113         disas_simd_3same_logic(s, insn);
11114         break;
11115     case 0x17: /* ADDP */
11116     case 0x14: /* SMAXP, UMAXP */
11117     case 0x15: /* SMINP, UMINP */
11118     {
11119         /* Pairwise operations */
11120         int is_q = extract32(insn, 30, 1);
11121         int u = extract32(insn, 29, 1);
11122         int size = extract32(insn, 22, 2);
11123         int rm = extract32(insn, 16, 5);
11124         int rn = extract32(insn, 5, 5);
11125         int rd = extract32(insn, 0, 5);
11126         if (opcode == 0x17) {
11127             if (u || (size == 3 && !is_q)) {
11128                 unallocated_encoding(s);
11129                 return;
11130             }
11131         } else {
11132             if (size == 3) {
11133                 unallocated_encoding(s);
11134                 return;
11135             }
11136         }
11137         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11138         break;
11139     }
11140     case 0x18 ... 0x31:
11141         /* floating point ops, sz[1] and U are part of opcode */
11142         disas_simd_3same_float(s, insn);
11143         break;
11144     default:
11145         disas_simd_3same_int(s, insn);
11146         break;
11147     }
11148 }
11149 
11150 /*
11151  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11152  *
11153  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11154  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11155  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11156  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11157  *
11158  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11159  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11160  *
11161  */
11162 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11163 {
11164     int opcode = extract32(insn, 11, 3);
11165     int u = extract32(insn, 29, 1);
11166     int a = extract32(insn, 23, 1);
11167     int is_q = extract32(insn, 30, 1);
11168     int rm = extract32(insn, 16, 5);
11169     int rn = extract32(insn, 5, 5);
11170     int rd = extract32(insn, 0, 5);
11171     /*
11172      * For these floating point ops, the U, a and opcode bits
11173      * together indicate the operation.
11174      */
11175     int fpopcode = opcode | (a << 3) | (u << 4);
11176     int datasize = is_q ? 128 : 64;
11177     int elements = datasize / 16;
11178     bool pairwise;
11179     TCGv_ptr fpst;
11180     int pass;
11181 
11182     switch (fpopcode) {
11183     case 0x0: /* FMAXNM */
11184     case 0x1: /* FMLA */
11185     case 0x2: /* FADD */
11186     case 0x3: /* FMULX */
11187     case 0x4: /* FCMEQ */
11188     case 0x6: /* FMAX */
11189     case 0x7: /* FRECPS */
11190     case 0x8: /* FMINNM */
11191     case 0x9: /* FMLS */
11192     case 0xa: /* FSUB */
11193     case 0xe: /* FMIN */
11194     case 0xf: /* FRSQRTS */
11195     case 0x13: /* FMUL */
11196     case 0x14: /* FCMGE */
11197     case 0x15: /* FACGE */
11198     case 0x17: /* FDIV */
11199     case 0x1a: /* FABD */
11200     case 0x1c: /* FCMGT */
11201     case 0x1d: /* FACGT */
11202         pairwise = false;
11203         break;
11204     case 0x10: /* FMAXNMP */
11205     case 0x12: /* FADDP */
11206     case 0x16: /* FMAXP */
11207     case 0x18: /* FMINNMP */
11208     case 0x1e: /* FMINP */
11209         pairwise = true;
11210         break;
11211     default:
11212         unallocated_encoding(s);
11213         return;
11214     }
11215 
11216     if (!dc_isar_feature(aa64_fp16, s)) {
11217         unallocated_encoding(s);
11218         return;
11219     }
11220 
11221     if (!fp_access_check(s)) {
11222         return;
11223     }
11224 
11225     fpst = fpstatus_ptr(FPST_FPCR_F16);
11226 
11227     if (pairwise) {
11228         int maxpass = is_q ? 8 : 4;
11229         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11230         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11231         TCGv_i32 tcg_res[8];
11232 
11233         for (pass = 0; pass < maxpass; pass++) {
11234             int passreg = pass < (maxpass / 2) ? rn : rm;
11235             int passelt = (pass << 1) & (maxpass - 1);
11236 
11237             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11238             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11239             tcg_res[pass] = tcg_temp_new_i32();
11240 
11241             switch (fpopcode) {
11242             case 0x10: /* FMAXNMP */
11243                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11244                                            fpst);
11245                 break;
11246             case 0x12: /* FADDP */
11247                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11248                 break;
11249             case 0x16: /* FMAXP */
11250                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11251                 break;
11252             case 0x18: /* FMINNMP */
11253                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11254                                            fpst);
11255                 break;
11256             case 0x1e: /* FMINP */
11257                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11258                 break;
11259             default:
11260                 g_assert_not_reached();
11261             }
11262         }
11263 
11264         for (pass = 0; pass < maxpass; pass++) {
11265             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11266         }
11267     } else {
11268         for (pass = 0; pass < elements; pass++) {
11269             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11270             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11271             TCGv_i32 tcg_res = tcg_temp_new_i32();
11272 
11273             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11274             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11275 
11276             switch (fpopcode) {
11277             case 0x0: /* FMAXNM */
11278                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11279                 break;
11280             case 0x1: /* FMLA */
11281                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11282                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11283                                            fpst);
11284                 break;
11285             case 0x2: /* FADD */
11286                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11287                 break;
11288             case 0x3: /* FMULX */
11289                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11290                 break;
11291             case 0x4: /* FCMEQ */
11292                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11293                 break;
11294             case 0x6: /* FMAX */
11295                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11296                 break;
11297             case 0x7: /* FRECPS */
11298                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11299                 break;
11300             case 0x8: /* FMINNM */
11301                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11302                 break;
11303             case 0x9: /* FMLS */
11304                 /* As usual for ARM, separate negation for fused multiply-add */
11305                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11306                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11307                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11308                                            fpst);
11309                 break;
11310             case 0xa: /* FSUB */
11311                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11312                 break;
11313             case 0xe: /* FMIN */
11314                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11315                 break;
11316             case 0xf: /* FRSQRTS */
11317                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11318                 break;
11319             case 0x13: /* FMUL */
11320                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11321                 break;
11322             case 0x14: /* FCMGE */
11323                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11324                 break;
11325             case 0x15: /* FACGE */
11326                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11327                 break;
11328             case 0x17: /* FDIV */
11329                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11330                 break;
11331             case 0x1a: /* FABD */
11332                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11333                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11334                 break;
11335             case 0x1c: /* FCMGT */
11336                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11337                 break;
11338             case 0x1d: /* FACGT */
11339                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11340                 break;
11341             default:
11342                 g_assert_not_reached();
11343             }
11344 
11345             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11346         }
11347     }
11348 
11349     clear_vec_high(s, is_q, rd);
11350 }
11351 
11352 /* AdvSIMD three same extra
11353  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11354  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11355  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11356  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11357  */
11358 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11359 {
11360     int rd = extract32(insn, 0, 5);
11361     int rn = extract32(insn, 5, 5);
11362     int opcode = extract32(insn, 11, 4);
11363     int rm = extract32(insn, 16, 5);
11364     int size = extract32(insn, 22, 2);
11365     bool u = extract32(insn, 29, 1);
11366     bool is_q = extract32(insn, 30, 1);
11367     bool feature;
11368     int rot;
11369 
11370     switch (u * 16 + opcode) {
11371     case 0x10: /* SQRDMLAH (vector) */
11372     case 0x11: /* SQRDMLSH (vector) */
11373         if (size != 1 && size != 2) {
11374             unallocated_encoding(s);
11375             return;
11376         }
11377         feature = dc_isar_feature(aa64_rdm, s);
11378         break;
11379     case 0x02: /* SDOT (vector) */
11380     case 0x12: /* UDOT (vector) */
11381         if (size != MO_32) {
11382             unallocated_encoding(s);
11383             return;
11384         }
11385         feature = dc_isar_feature(aa64_dp, s);
11386         break;
11387     case 0x03: /* USDOT */
11388         if (size != MO_32) {
11389             unallocated_encoding(s);
11390             return;
11391         }
11392         feature = dc_isar_feature(aa64_i8mm, s);
11393         break;
11394     case 0x04: /* SMMLA */
11395     case 0x14: /* UMMLA */
11396     case 0x05: /* USMMLA */
11397         if (!is_q || size != MO_32) {
11398             unallocated_encoding(s);
11399             return;
11400         }
11401         feature = dc_isar_feature(aa64_i8mm, s);
11402         break;
11403     case 0x18: /* FCMLA, #0 */
11404     case 0x19: /* FCMLA, #90 */
11405     case 0x1a: /* FCMLA, #180 */
11406     case 0x1b: /* FCMLA, #270 */
11407     case 0x1c: /* FCADD, #90 */
11408     case 0x1e: /* FCADD, #270 */
11409         if (size == 0
11410             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11411             || (size == 3 && !is_q)) {
11412             unallocated_encoding(s);
11413             return;
11414         }
11415         feature = dc_isar_feature(aa64_fcma, s);
11416         break;
11417     case 0x1d: /* BFMMLA */
11418         if (size != MO_16 || !is_q) {
11419             unallocated_encoding(s);
11420             return;
11421         }
11422         feature = dc_isar_feature(aa64_bf16, s);
11423         break;
11424     case 0x1f:
11425         switch (size) {
11426         case 1: /* BFDOT */
11427         case 3: /* BFMLAL{B,T} */
11428             feature = dc_isar_feature(aa64_bf16, s);
11429             break;
11430         default:
11431             unallocated_encoding(s);
11432             return;
11433         }
11434         break;
11435     default:
11436         unallocated_encoding(s);
11437         return;
11438     }
11439     if (!feature) {
11440         unallocated_encoding(s);
11441         return;
11442     }
11443     if (!fp_access_check(s)) {
11444         return;
11445     }
11446 
11447     switch (opcode) {
11448     case 0x0: /* SQRDMLAH (vector) */
11449         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11450         return;
11451 
11452     case 0x1: /* SQRDMLSH (vector) */
11453         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11454         return;
11455 
11456     case 0x2: /* SDOT / UDOT */
11457         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11458                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11459         return;
11460 
11461     case 0x3: /* USDOT */
11462         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11463         return;
11464 
11465     case 0x04: /* SMMLA, UMMLA */
11466         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11467                          u ? gen_helper_gvec_ummla_b
11468                          : gen_helper_gvec_smmla_b);
11469         return;
11470     case 0x05: /* USMMLA */
11471         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11472         return;
11473 
11474     case 0x8: /* FCMLA, #0 */
11475     case 0x9: /* FCMLA, #90 */
11476     case 0xa: /* FCMLA, #180 */
11477     case 0xb: /* FCMLA, #270 */
11478         rot = extract32(opcode, 0, 2);
11479         switch (size) {
11480         case 1:
11481             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11482                               gen_helper_gvec_fcmlah);
11483             break;
11484         case 2:
11485             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11486                               gen_helper_gvec_fcmlas);
11487             break;
11488         case 3:
11489             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11490                               gen_helper_gvec_fcmlad);
11491             break;
11492         default:
11493             g_assert_not_reached();
11494         }
11495         return;
11496 
11497     case 0xc: /* FCADD, #90 */
11498     case 0xe: /* FCADD, #270 */
11499         rot = extract32(opcode, 1, 1);
11500         switch (size) {
11501         case 1:
11502             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11503                               gen_helper_gvec_fcaddh);
11504             break;
11505         case 2:
11506             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11507                               gen_helper_gvec_fcadds);
11508             break;
11509         case 3:
11510             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11511                               gen_helper_gvec_fcaddd);
11512             break;
11513         default:
11514             g_assert_not_reached();
11515         }
11516         return;
11517 
11518     case 0xd: /* BFMMLA */
11519         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11520         return;
11521     case 0xf:
11522         switch (size) {
11523         case 1: /* BFDOT */
11524             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11525             break;
11526         case 3: /* BFMLAL{B,T} */
11527             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11528                               gen_helper_gvec_bfmlal);
11529             break;
11530         default:
11531             g_assert_not_reached();
11532         }
11533         return;
11534 
11535     default:
11536         g_assert_not_reached();
11537     }
11538 }
11539 
11540 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11541                                   int size, int rn, int rd)
11542 {
11543     /* Handle 2-reg-misc ops which are widening (so each size element
11544      * in the source becomes a 2*size element in the destination.
11545      * The only instruction like this is FCVTL.
11546      */
11547     int pass;
11548 
11549     if (size == 3) {
11550         /* 32 -> 64 bit fp conversion */
11551         TCGv_i64 tcg_res[2];
11552         int srcelt = is_q ? 2 : 0;
11553 
11554         for (pass = 0; pass < 2; pass++) {
11555             TCGv_i32 tcg_op = tcg_temp_new_i32();
11556             tcg_res[pass] = tcg_temp_new_i64();
11557 
11558             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11559             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11560         }
11561         for (pass = 0; pass < 2; pass++) {
11562             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11563         }
11564     } else {
11565         /* 16 -> 32 bit fp conversion */
11566         int srcelt = is_q ? 4 : 0;
11567         TCGv_i32 tcg_res[4];
11568         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11569         TCGv_i32 ahp = get_ahp_flag();
11570 
11571         for (pass = 0; pass < 4; pass++) {
11572             tcg_res[pass] = tcg_temp_new_i32();
11573 
11574             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11575             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11576                                            fpst, ahp);
11577         }
11578         for (pass = 0; pass < 4; pass++) {
11579             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11580         }
11581     }
11582 }
11583 
11584 static void handle_rev(DisasContext *s, int opcode, bool u,
11585                        bool is_q, int size, int rn, int rd)
11586 {
11587     int op = (opcode << 1) | u;
11588     int opsz = op + size;
11589     int grp_size = 3 - opsz;
11590     int dsize = is_q ? 128 : 64;
11591     int i;
11592 
11593     if (opsz >= 3) {
11594         unallocated_encoding(s);
11595         return;
11596     }
11597 
11598     if (!fp_access_check(s)) {
11599         return;
11600     }
11601 
11602     if (size == 0) {
11603         /* Special case bytes, use bswap op on each group of elements */
11604         int groups = dsize / (8 << grp_size);
11605 
11606         for (i = 0; i < groups; i++) {
11607             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11608 
11609             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11610             switch (grp_size) {
11611             case MO_16:
11612                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11613                 break;
11614             case MO_32:
11615                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11616                 break;
11617             case MO_64:
11618                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11619                 break;
11620             default:
11621                 g_assert_not_reached();
11622             }
11623             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11624         }
11625         clear_vec_high(s, is_q, rd);
11626     } else {
11627         int revmask = (1 << grp_size) - 1;
11628         int esize = 8 << size;
11629         int elements = dsize / esize;
11630         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11631         TCGv_i64 tcg_rd[2];
11632 
11633         for (i = 0; i < 2; i++) {
11634             tcg_rd[i] = tcg_temp_new_i64();
11635             tcg_gen_movi_i64(tcg_rd[i], 0);
11636         }
11637 
11638         for (i = 0; i < elements; i++) {
11639             int e_rev = (i & 0xf) ^ revmask;
11640             int w = (e_rev * esize) / 64;
11641             int o = (e_rev * esize) % 64;
11642 
11643             read_vec_element(s, tcg_rn, rn, i, size);
11644             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11645         }
11646 
11647         for (i = 0; i < 2; i++) {
11648             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11649         }
11650         clear_vec_high(s, true, rd);
11651     }
11652 }
11653 
11654 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11655                                   bool is_q, int size, int rn, int rd)
11656 {
11657     /* Implement the pairwise operations from 2-misc:
11658      * SADDLP, UADDLP, SADALP, UADALP.
11659      * These all add pairs of elements in the input to produce a
11660      * double-width result element in the output (possibly accumulating).
11661      */
11662     bool accum = (opcode == 0x6);
11663     int maxpass = is_q ? 2 : 1;
11664     int pass;
11665     TCGv_i64 tcg_res[2];
11666 
11667     if (size == 2) {
11668         /* 32 + 32 -> 64 op */
11669         MemOp memop = size + (u ? 0 : MO_SIGN);
11670 
11671         for (pass = 0; pass < maxpass; pass++) {
11672             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11673             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11674 
11675             tcg_res[pass] = tcg_temp_new_i64();
11676 
11677             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11678             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11679             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11680             if (accum) {
11681                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11682                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11683             }
11684         }
11685     } else {
11686         for (pass = 0; pass < maxpass; pass++) {
11687             TCGv_i64 tcg_op = tcg_temp_new_i64();
11688             NeonGenOne64OpFn *genfn;
11689             static NeonGenOne64OpFn * const fns[2][2] = {
11690                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11691                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11692             };
11693 
11694             genfn = fns[size][u];
11695 
11696             tcg_res[pass] = tcg_temp_new_i64();
11697 
11698             read_vec_element(s, tcg_op, rn, pass, MO_64);
11699             genfn(tcg_res[pass], tcg_op);
11700 
11701             if (accum) {
11702                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11703                 if (size == 0) {
11704                     gen_helper_neon_addl_u16(tcg_res[pass],
11705                                              tcg_res[pass], tcg_op);
11706                 } else {
11707                     gen_helper_neon_addl_u32(tcg_res[pass],
11708                                              tcg_res[pass], tcg_op);
11709                 }
11710             }
11711         }
11712     }
11713     if (!is_q) {
11714         tcg_res[1] = tcg_constant_i64(0);
11715     }
11716     for (pass = 0; pass < 2; pass++) {
11717         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11718     }
11719 }
11720 
11721 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11722 {
11723     /* Implement SHLL and SHLL2 */
11724     int pass;
11725     int part = is_q ? 2 : 0;
11726     TCGv_i64 tcg_res[2];
11727 
11728     for (pass = 0; pass < 2; pass++) {
11729         static NeonGenWidenFn * const widenfns[3] = {
11730             gen_helper_neon_widen_u8,
11731             gen_helper_neon_widen_u16,
11732             tcg_gen_extu_i32_i64,
11733         };
11734         NeonGenWidenFn *widenfn = widenfns[size];
11735         TCGv_i32 tcg_op = tcg_temp_new_i32();
11736 
11737         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11738         tcg_res[pass] = tcg_temp_new_i64();
11739         widenfn(tcg_res[pass], tcg_op);
11740         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11741     }
11742 
11743     for (pass = 0; pass < 2; pass++) {
11744         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11745     }
11746 }
11747 
11748 /* AdvSIMD two reg misc
11749  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11750  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11751  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11752  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11753  */
11754 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11755 {
11756     int size = extract32(insn, 22, 2);
11757     int opcode = extract32(insn, 12, 5);
11758     bool u = extract32(insn, 29, 1);
11759     bool is_q = extract32(insn, 30, 1);
11760     int rn = extract32(insn, 5, 5);
11761     int rd = extract32(insn, 0, 5);
11762     bool need_fpstatus = false;
11763     int rmode = -1;
11764     TCGv_i32 tcg_rmode;
11765     TCGv_ptr tcg_fpstatus;
11766 
11767     switch (opcode) {
11768     case 0x0: /* REV64, REV32 */
11769     case 0x1: /* REV16 */
11770         handle_rev(s, opcode, u, is_q, size, rn, rd);
11771         return;
11772     case 0x5: /* CNT, NOT, RBIT */
11773         if (u && size == 0) {
11774             /* NOT */
11775             break;
11776         } else if (u && size == 1) {
11777             /* RBIT */
11778             break;
11779         } else if (!u && size == 0) {
11780             /* CNT */
11781             break;
11782         }
11783         unallocated_encoding(s);
11784         return;
11785     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11786     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11787         if (size == 3) {
11788             unallocated_encoding(s);
11789             return;
11790         }
11791         if (!fp_access_check(s)) {
11792             return;
11793         }
11794 
11795         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11796         return;
11797     case 0x4: /* CLS, CLZ */
11798         if (size == 3) {
11799             unallocated_encoding(s);
11800             return;
11801         }
11802         break;
11803     case 0x2: /* SADDLP, UADDLP */
11804     case 0x6: /* SADALP, UADALP */
11805         if (size == 3) {
11806             unallocated_encoding(s);
11807             return;
11808         }
11809         if (!fp_access_check(s)) {
11810             return;
11811         }
11812         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11813         return;
11814     case 0x13: /* SHLL, SHLL2 */
11815         if (u == 0 || size == 3) {
11816             unallocated_encoding(s);
11817             return;
11818         }
11819         if (!fp_access_check(s)) {
11820             return;
11821         }
11822         handle_shll(s, is_q, size, rn, rd);
11823         return;
11824     case 0xa: /* CMLT */
11825         if (u == 1) {
11826             unallocated_encoding(s);
11827             return;
11828         }
11829         /* fall through */
11830     case 0x8: /* CMGT, CMGE */
11831     case 0x9: /* CMEQ, CMLE */
11832     case 0xb: /* ABS, NEG */
11833         if (size == 3 && !is_q) {
11834             unallocated_encoding(s);
11835             return;
11836         }
11837         break;
11838     case 0x3: /* SUQADD, USQADD */
11839         if (size == 3 && !is_q) {
11840             unallocated_encoding(s);
11841             return;
11842         }
11843         if (!fp_access_check(s)) {
11844             return;
11845         }
11846         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11847         return;
11848     case 0x7: /* SQABS, SQNEG */
11849         if (size == 3 && !is_q) {
11850             unallocated_encoding(s);
11851             return;
11852         }
11853         break;
11854     case 0xc ... 0xf:
11855     case 0x16 ... 0x1f:
11856     {
11857         /* Floating point: U, size[1] and opcode indicate operation;
11858          * size[0] indicates single or double precision.
11859          */
11860         int is_double = extract32(size, 0, 1);
11861         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11862         size = is_double ? 3 : 2;
11863         switch (opcode) {
11864         case 0x2f: /* FABS */
11865         case 0x6f: /* FNEG */
11866             if (size == 3 && !is_q) {
11867                 unallocated_encoding(s);
11868                 return;
11869             }
11870             break;
11871         case 0x1d: /* SCVTF */
11872         case 0x5d: /* UCVTF */
11873         {
11874             bool is_signed = (opcode == 0x1d) ? true : false;
11875             int elements = is_double ? 2 : is_q ? 4 : 2;
11876             if (is_double && !is_q) {
11877                 unallocated_encoding(s);
11878                 return;
11879             }
11880             if (!fp_access_check(s)) {
11881                 return;
11882             }
11883             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11884             return;
11885         }
11886         case 0x2c: /* FCMGT (zero) */
11887         case 0x2d: /* FCMEQ (zero) */
11888         case 0x2e: /* FCMLT (zero) */
11889         case 0x6c: /* FCMGE (zero) */
11890         case 0x6d: /* FCMLE (zero) */
11891             if (size == 3 && !is_q) {
11892                 unallocated_encoding(s);
11893                 return;
11894             }
11895             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11896             return;
11897         case 0x7f: /* FSQRT */
11898             if (size == 3 && !is_q) {
11899                 unallocated_encoding(s);
11900                 return;
11901             }
11902             break;
11903         case 0x1a: /* FCVTNS */
11904         case 0x1b: /* FCVTMS */
11905         case 0x3a: /* FCVTPS */
11906         case 0x3b: /* FCVTZS */
11907         case 0x5a: /* FCVTNU */
11908         case 0x5b: /* FCVTMU */
11909         case 0x7a: /* FCVTPU */
11910         case 0x7b: /* FCVTZU */
11911             need_fpstatus = true;
11912             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11913             if (size == 3 && !is_q) {
11914                 unallocated_encoding(s);
11915                 return;
11916             }
11917             break;
11918         case 0x5c: /* FCVTAU */
11919         case 0x1c: /* FCVTAS */
11920             need_fpstatus = true;
11921             rmode = FPROUNDING_TIEAWAY;
11922             if (size == 3 && !is_q) {
11923                 unallocated_encoding(s);
11924                 return;
11925             }
11926             break;
11927         case 0x3c: /* URECPE */
11928             if (size == 3) {
11929                 unallocated_encoding(s);
11930                 return;
11931             }
11932             /* fall through */
11933         case 0x3d: /* FRECPE */
11934         case 0x7d: /* FRSQRTE */
11935             if (size == 3 && !is_q) {
11936                 unallocated_encoding(s);
11937                 return;
11938             }
11939             if (!fp_access_check(s)) {
11940                 return;
11941             }
11942             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
11943             return;
11944         case 0x56: /* FCVTXN, FCVTXN2 */
11945             if (size == 2) {
11946                 unallocated_encoding(s);
11947                 return;
11948             }
11949             /* fall through */
11950         case 0x16: /* FCVTN, FCVTN2 */
11951             /* handle_2misc_narrow does a 2*size -> size operation, but these
11952              * instructions encode the source size rather than dest size.
11953              */
11954             if (!fp_access_check(s)) {
11955                 return;
11956             }
11957             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11958             return;
11959         case 0x36: /* BFCVTN, BFCVTN2 */
11960             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
11961                 unallocated_encoding(s);
11962                 return;
11963             }
11964             if (!fp_access_check(s)) {
11965                 return;
11966             }
11967             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11968             return;
11969         case 0x17: /* FCVTL, FCVTL2 */
11970             if (!fp_access_check(s)) {
11971                 return;
11972             }
11973             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
11974             return;
11975         case 0x18: /* FRINTN */
11976         case 0x19: /* FRINTM */
11977         case 0x38: /* FRINTP */
11978         case 0x39: /* FRINTZ */
11979             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11980             /* fall through */
11981         case 0x59: /* FRINTX */
11982         case 0x79: /* FRINTI */
11983             need_fpstatus = true;
11984             if (size == 3 && !is_q) {
11985                 unallocated_encoding(s);
11986                 return;
11987             }
11988             break;
11989         case 0x58: /* FRINTA */
11990             rmode = FPROUNDING_TIEAWAY;
11991             need_fpstatus = true;
11992             if (size == 3 && !is_q) {
11993                 unallocated_encoding(s);
11994                 return;
11995             }
11996             break;
11997         case 0x7c: /* URSQRTE */
11998             if (size == 3) {
11999                 unallocated_encoding(s);
12000                 return;
12001             }
12002             break;
12003         case 0x1e: /* FRINT32Z */
12004         case 0x1f: /* FRINT64Z */
12005             rmode = FPROUNDING_ZERO;
12006             /* fall through */
12007         case 0x5e: /* FRINT32X */
12008         case 0x5f: /* FRINT64X */
12009             need_fpstatus = true;
12010             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12011                 unallocated_encoding(s);
12012                 return;
12013             }
12014             break;
12015         default:
12016             unallocated_encoding(s);
12017             return;
12018         }
12019         break;
12020     }
12021     default:
12022         unallocated_encoding(s);
12023         return;
12024     }
12025 
12026     if (!fp_access_check(s)) {
12027         return;
12028     }
12029 
12030     if (need_fpstatus || rmode >= 0) {
12031         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12032     } else {
12033         tcg_fpstatus = NULL;
12034     }
12035     if (rmode >= 0) {
12036         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12037     } else {
12038         tcg_rmode = NULL;
12039     }
12040 
12041     switch (opcode) {
12042     case 0x5:
12043         if (u && size == 0) { /* NOT */
12044             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12045             return;
12046         }
12047         break;
12048     case 0x8: /* CMGT, CMGE */
12049         if (u) {
12050             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12051         } else {
12052             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12053         }
12054         return;
12055     case 0x9: /* CMEQ, CMLE */
12056         if (u) {
12057             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12058         } else {
12059             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12060         }
12061         return;
12062     case 0xa: /* CMLT */
12063         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12064         return;
12065     case 0xb:
12066         if (u) { /* ABS, NEG */
12067             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12068         } else {
12069             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12070         }
12071         return;
12072     }
12073 
12074     if (size == 3) {
12075         /* All 64-bit element operations can be shared with scalar 2misc */
12076         int pass;
12077 
12078         /* Coverity claims (size == 3 && !is_q) has been eliminated
12079          * from all paths leading to here.
12080          */
12081         tcg_debug_assert(is_q);
12082         for (pass = 0; pass < 2; pass++) {
12083             TCGv_i64 tcg_op = tcg_temp_new_i64();
12084             TCGv_i64 tcg_res = tcg_temp_new_i64();
12085 
12086             read_vec_element(s, tcg_op, rn, pass, MO_64);
12087 
12088             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12089                             tcg_rmode, tcg_fpstatus);
12090 
12091             write_vec_element(s, tcg_res, rd, pass, MO_64);
12092         }
12093     } else {
12094         int pass;
12095 
12096         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12097             TCGv_i32 tcg_op = tcg_temp_new_i32();
12098             TCGv_i32 tcg_res = tcg_temp_new_i32();
12099 
12100             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12101 
12102             if (size == 2) {
12103                 /* Special cases for 32 bit elements */
12104                 switch (opcode) {
12105                 case 0x4: /* CLS */
12106                     if (u) {
12107                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12108                     } else {
12109                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12110                     }
12111                     break;
12112                 case 0x7: /* SQABS, SQNEG */
12113                     if (u) {
12114                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12115                     } else {
12116                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12117                     }
12118                     break;
12119                 case 0x2f: /* FABS */
12120                     gen_helper_vfp_abss(tcg_res, tcg_op);
12121                     break;
12122                 case 0x6f: /* FNEG */
12123                     gen_helper_vfp_negs(tcg_res, tcg_op);
12124                     break;
12125                 case 0x7f: /* FSQRT */
12126                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12127                     break;
12128                 case 0x1a: /* FCVTNS */
12129                 case 0x1b: /* FCVTMS */
12130                 case 0x1c: /* FCVTAS */
12131                 case 0x3a: /* FCVTPS */
12132                 case 0x3b: /* FCVTZS */
12133                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12134                                          tcg_constant_i32(0), tcg_fpstatus);
12135                     break;
12136                 case 0x5a: /* FCVTNU */
12137                 case 0x5b: /* FCVTMU */
12138                 case 0x5c: /* FCVTAU */
12139                 case 0x7a: /* FCVTPU */
12140                 case 0x7b: /* FCVTZU */
12141                     gen_helper_vfp_touls(tcg_res, tcg_op,
12142                                          tcg_constant_i32(0), tcg_fpstatus);
12143                     break;
12144                 case 0x18: /* FRINTN */
12145                 case 0x19: /* FRINTM */
12146                 case 0x38: /* FRINTP */
12147                 case 0x39: /* FRINTZ */
12148                 case 0x58: /* FRINTA */
12149                 case 0x79: /* FRINTI */
12150                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12151                     break;
12152                 case 0x59: /* FRINTX */
12153                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12154                     break;
12155                 case 0x7c: /* URSQRTE */
12156                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12157                     break;
12158                 case 0x1e: /* FRINT32Z */
12159                 case 0x5e: /* FRINT32X */
12160                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12161                     break;
12162                 case 0x1f: /* FRINT64Z */
12163                 case 0x5f: /* FRINT64X */
12164                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12165                     break;
12166                 default:
12167                     g_assert_not_reached();
12168                 }
12169             } else {
12170                 /* Use helpers for 8 and 16 bit elements */
12171                 switch (opcode) {
12172                 case 0x5: /* CNT, RBIT */
12173                     /* For these two insns size is part of the opcode specifier
12174                      * (handled earlier); they always operate on byte elements.
12175                      */
12176                     if (u) {
12177                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12178                     } else {
12179                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12180                     }
12181                     break;
12182                 case 0x7: /* SQABS, SQNEG */
12183                 {
12184                     NeonGenOneOpEnvFn *genfn;
12185                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12186                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12187                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12188                     };
12189                     genfn = fns[size][u];
12190                     genfn(tcg_res, cpu_env, tcg_op);
12191                     break;
12192                 }
12193                 case 0x4: /* CLS, CLZ */
12194                     if (u) {
12195                         if (size == 0) {
12196                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12197                         } else {
12198                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12199                         }
12200                     } else {
12201                         if (size == 0) {
12202                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12203                         } else {
12204                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12205                         }
12206                     }
12207                     break;
12208                 default:
12209                     g_assert_not_reached();
12210                 }
12211             }
12212 
12213             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12214         }
12215     }
12216     clear_vec_high(s, is_q, rd);
12217 
12218     if (tcg_rmode) {
12219         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12220     }
12221 }
12222 
12223 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12224  *
12225  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12226  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12227  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12228  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12229  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12230  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12231  *
12232  * This actually covers two groups where scalar access is governed by
12233  * bit 28. A bunch of the instructions (float to integral) only exist
12234  * in the vector form and are un-allocated for the scalar decode. Also
12235  * in the scalar decode Q is always 1.
12236  */
12237 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12238 {
12239     int fpop, opcode, a, u;
12240     int rn, rd;
12241     bool is_q;
12242     bool is_scalar;
12243     bool only_in_vector = false;
12244 
12245     int pass;
12246     TCGv_i32 tcg_rmode = NULL;
12247     TCGv_ptr tcg_fpstatus = NULL;
12248     bool need_fpst = true;
12249     int rmode = -1;
12250 
12251     if (!dc_isar_feature(aa64_fp16, s)) {
12252         unallocated_encoding(s);
12253         return;
12254     }
12255 
12256     rd = extract32(insn, 0, 5);
12257     rn = extract32(insn, 5, 5);
12258 
12259     a = extract32(insn, 23, 1);
12260     u = extract32(insn, 29, 1);
12261     is_scalar = extract32(insn, 28, 1);
12262     is_q = extract32(insn, 30, 1);
12263 
12264     opcode = extract32(insn, 12, 5);
12265     fpop = deposit32(opcode, 5, 1, a);
12266     fpop = deposit32(fpop, 6, 1, u);
12267 
12268     switch (fpop) {
12269     case 0x1d: /* SCVTF */
12270     case 0x5d: /* UCVTF */
12271     {
12272         int elements;
12273 
12274         if (is_scalar) {
12275             elements = 1;
12276         } else {
12277             elements = (is_q ? 8 : 4);
12278         }
12279 
12280         if (!fp_access_check(s)) {
12281             return;
12282         }
12283         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12284         return;
12285     }
12286     break;
12287     case 0x2c: /* FCMGT (zero) */
12288     case 0x2d: /* FCMEQ (zero) */
12289     case 0x2e: /* FCMLT (zero) */
12290     case 0x6c: /* FCMGE (zero) */
12291     case 0x6d: /* FCMLE (zero) */
12292         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12293         return;
12294     case 0x3d: /* FRECPE */
12295     case 0x3f: /* FRECPX */
12296         break;
12297     case 0x18: /* FRINTN */
12298         only_in_vector = true;
12299         rmode = FPROUNDING_TIEEVEN;
12300         break;
12301     case 0x19: /* FRINTM */
12302         only_in_vector = true;
12303         rmode = FPROUNDING_NEGINF;
12304         break;
12305     case 0x38: /* FRINTP */
12306         only_in_vector = true;
12307         rmode = FPROUNDING_POSINF;
12308         break;
12309     case 0x39: /* FRINTZ */
12310         only_in_vector = true;
12311         rmode = FPROUNDING_ZERO;
12312         break;
12313     case 0x58: /* FRINTA */
12314         only_in_vector = true;
12315         rmode = FPROUNDING_TIEAWAY;
12316         break;
12317     case 0x59: /* FRINTX */
12318     case 0x79: /* FRINTI */
12319         only_in_vector = true;
12320         /* current rounding mode */
12321         break;
12322     case 0x1a: /* FCVTNS */
12323         rmode = FPROUNDING_TIEEVEN;
12324         break;
12325     case 0x1b: /* FCVTMS */
12326         rmode = FPROUNDING_NEGINF;
12327         break;
12328     case 0x1c: /* FCVTAS */
12329         rmode = FPROUNDING_TIEAWAY;
12330         break;
12331     case 0x3a: /* FCVTPS */
12332         rmode = FPROUNDING_POSINF;
12333         break;
12334     case 0x3b: /* FCVTZS */
12335         rmode = FPROUNDING_ZERO;
12336         break;
12337     case 0x5a: /* FCVTNU */
12338         rmode = FPROUNDING_TIEEVEN;
12339         break;
12340     case 0x5b: /* FCVTMU */
12341         rmode = FPROUNDING_NEGINF;
12342         break;
12343     case 0x5c: /* FCVTAU */
12344         rmode = FPROUNDING_TIEAWAY;
12345         break;
12346     case 0x7a: /* FCVTPU */
12347         rmode = FPROUNDING_POSINF;
12348         break;
12349     case 0x7b: /* FCVTZU */
12350         rmode = FPROUNDING_ZERO;
12351         break;
12352     case 0x2f: /* FABS */
12353     case 0x6f: /* FNEG */
12354         need_fpst = false;
12355         break;
12356     case 0x7d: /* FRSQRTE */
12357     case 0x7f: /* FSQRT (vector) */
12358         break;
12359     default:
12360         unallocated_encoding(s);
12361         return;
12362     }
12363 
12364 
12365     /* Check additional constraints for the scalar encoding */
12366     if (is_scalar) {
12367         if (!is_q) {
12368             unallocated_encoding(s);
12369             return;
12370         }
12371         /* FRINTxx is only in the vector form */
12372         if (only_in_vector) {
12373             unallocated_encoding(s);
12374             return;
12375         }
12376     }
12377 
12378     if (!fp_access_check(s)) {
12379         return;
12380     }
12381 
12382     if (rmode >= 0 || need_fpst) {
12383         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12384     }
12385 
12386     if (rmode >= 0) {
12387         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12388     }
12389 
12390     if (is_scalar) {
12391         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12392         TCGv_i32 tcg_res = tcg_temp_new_i32();
12393 
12394         switch (fpop) {
12395         case 0x1a: /* FCVTNS */
12396         case 0x1b: /* FCVTMS */
12397         case 0x1c: /* FCVTAS */
12398         case 0x3a: /* FCVTPS */
12399         case 0x3b: /* FCVTZS */
12400             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12401             break;
12402         case 0x3d: /* FRECPE */
12403             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12404             break;
12405         case 0x3f: /* FRECPX */
12406             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12407             break;
12408         case 0x5a: /* FCVTNU */
12409         case 0x5b: /* FCVTMU */
12410         case 0x5c: /* FCVTAU */
12411         case 0x7a: /* FCVTPU */
12412         case 0x7b: /* FCVTZU */
12413             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12414             break;
12415         case 0x6f: /* FNEG */
12416             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12417             break;
12418         case 0x7d: /* FRSQRTE */
12419             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12420             break;
12421         default:
12422             g_assert_not_reached();
12423         }
12424 
12425         /* limit any sign extension going on */
12426         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12427         write_fp_sreg(s, rd, tcg_res);
12428     } else {
12429         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12430             TCGv_i32 tcg_op = tcg_temp_new_i32();
12431             TCGv_i32 tcg_res = tcg_temp_new_i32();
12432 
12433             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12434 
12435             switch (fpop) {
12436             case 0x1a: /* FCVTNS */
12437             case 0x1b: /* FCVTMS */
12438             case 0x1c: /* FCVTAS */
12439             case 0x3a: /* FCVTPS */
12440             case 0x3b: /* FCVTZS */
12441                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12442                 break;
12443             case 0x3d: /* FRECPE */
12444                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12445                 break;
12446             case 0x5a: /* FCVTNU */
12447             case 0x5b: /* FCVTMU */
12448             case 0x5c: /* FCVTAU */
12449             case 0x7a: /* FCVTPU */
12450             case 0x7b: /* FCVTZU */
12451                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12452                 break;
12453             case 0x18: /* FRINTN */
12454             case 0x19: /* FRINTM */
12455             case 0x38: /* FRINTP */
12456             case 0x39: /* FRINTZ */
12457             case 0x58: /* FRINTA */
12458             case 0x79: /* FRINTI */
12459                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12460                 break;
12461             case 0x59: /* FRINTX */
12462                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12463                 break;
12464             case 0x2f: /* FABS */
12465                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12466                 break;
12467             case 0x6f: /* FNEG */
12468                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12469                 break;
12470             case 0x7d: /* FRSQRTE */
12471                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12472                 break;
12473             case 0x7f: /* FSQRT */
12474                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12475                 break;
12476             default:
12477                 g_assert_not_reached();
12478             }
12479 
12480             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12481         }
12482 
12483         clear_vec_high(s, is_q, rd);
12484     }
12485 
12486     if (tcg_rmode) {
12487         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12488     }
12489 }
12490 
12491 /* AdvSIMD scalar x indexed element
12492  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12493  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12494  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12495  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12496  * AdvSIMD vector x indexed element
12497  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12498  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12499  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12500  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12501  */
12502 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12503 {
12504     /* This encoding has two kinds of instruction:
12505      *  normal, where we perform elt x idxelt => elt for each
12506      *     element in the vector
12507      *  long, where we perform elt x idxelt and generate a result of
12508      *     double the width of the input element
12509      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12510      */
12511     bool is_scalar = extract32(insn, 28, 1);
12512     bool is_q = extract32(insn, 30, 1);
12513     bool u = extract32(insn, 29, 1);
12514     int size = extract32(insn, 22, 2);
12515     int l = extract32(insn, 21, 1);
12516     int m = extract32(insn, 20, 1);
12517     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12518     int rm = extract32(insn, 16, 4);
12519     int opcode = extract32(insn, 12, 4);
12520     int h = extract32(insn, 11, 1);
12521     int rn = extract32(insn, 5, 5);
12522     int rd = extract32(insn, 0, 5);
12523     bool is_long = false;
12524     int is_fp = 0;
12525     bool is_fp16 = false;
12526     int index;
12527     TCGv_ptr fpst;
12528 
12529     switch (16 * u + opcode) {
12530     case 0x08: /* MUL */
12531     case 0x10: /* MLA */
12532     case 0x14: /* MLS */
12533         if (is_scalar) {
12534             unallocated_encoding(s);
12535             return;
12536         }
12537         break;
12538     case 0x02: /* SMLAL, SMLAL2 */
12539     case 0x12: /* UMLAL, UMLAL2 */
12540     case 0x06: /* SMLSL, SMLSL2 */
12541     case 0x16: /* UMLSL, UMLSL2 */
12542     case 0x0a: /* SMULL, SMULL2 */
12543     case 0x1a: /* UMULL, UMULL2 */
12544         if (is_scalar) {
12545             unallocated_encoding(s);
12546             return;
12547         }
12548         is_long = true;
12549         break;
12550     case 0x03: /* SQDMLAL, SQDMLAL2 */
12551     case 0x07: /* SQDMLSL, SQDMLSL2 */
12552     case 0x0b: /* SQDMULL, SQDMULL2 */
12553         is_long = true;
12554         break;
12555     case 0x0c: /* SQDMULH */
12556     case 0x0d: /* SQRDMULH */
12557         break;
12558     case 0x01: /* FMLA */
12559     case 0x05: /* FMLS */
12560     case 0x09: /* FMUL */
12561     case 0x19: /* FMULX */
12562         is_fp = 1;
12563         break;
12564     case 0x1d: /* SQRDMLAH */
12565     case 0x1f: /* SQRDMLSH */
12566         if (!dc_isar_feature(aa64_rdm, s)) {
12567             unallocated_encoding(s);
12568             return;
12569         }
12570         break;
12571     case 0x0e: /* SDOT */
12572     case 0x1e: /* UDOT */
12573         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12574             unallocated_encoding(s);
12575             return;
12576         }
12577         break;
12578     case 0x0f:
12579         switch (size) {
12580         case 0: /* SUDOT */
12581         case 2: /* USDOT */
12582             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12583                 unallocated_encoding(s);
12584                 return;
12585             }
12586             size = MO_32;
12587             break;
12588         case 1: /* BFDOT */
12589             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12590                 unallocated_encoding(s);
12591                 return;
12592             }
12593             size = MO_32;
12594             break;
12595         case 3: /* BFMLAL{B,T} */
12596             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12597                 unallocated_encoding(s);
12598                 return;
12599             }
12600             /* can't set is_fp without other incorrect size checks */
12601             size = MO_16;
12602             break;
12603         default:
12604             unallocated_encoding(s);
12605             return;
12606         }
12607         break;
12608     case 0x11: /* FCMLA #0 */
12609     case 0x13: /* FCMLA #90 */
12610     case 0x15: /* FCMLA #180 */
12611     case 0x17: /* FCMLA #270 */
12612         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12613             unallocated_encoding(s);
12614             return;
12615         }
12616         is_fp = 2;
12617         break;
12618     case 0x00: /* FMLAL */
12619     case 0x04: /* FMLSL */
12620     case 0x18: /* FMLAL2 */
12621     case 0x1c: /* FMLSL2 */
12622         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12623             unallocated_encoding(s);
12624             return;
12625         }
12626         size = MO_16;
12627         /* is_fp, but we pass cpu_env not fp_status.  */
12628         break;
12629     default:
12630         unallocated_encoding(s);
12631         return;
12632     }
12633 
12634     switch (is_fp) {
12635     case 1: /* normal fp */
12636         /* convert insn encoded size to MemOp size */
12637         switch (size) {
12638         case 0: /* half-precision */
12639             size = MO_16;
12640             is_fp16 = true;
12641             break;
12642         case MO_32: /* single precision */
12643         case MO_64: /* double precision */
12644             break;
12645         default:
12646             unallocated_encoding(s);
12647             return;
12648         }
12649         break;
12650 
12651     case 2: /* complex fp */
12652         /* Each indexable element is a complex pair.  */
12653         size += 1;
12654         switch (size) {
12655         case MO_32:
12656             if (h && !is_q) {
12657                 unallocated_encoding(s);
12658                 return;
12659             }
12660             is_fp16 = true;
12661             break;
12662         case MO_64:
12663             break;
12664         default:
12665             unallocated_encoding(s);
12666             return;
12667         }
12668         break;
12669 
12670     default: /* integer */
12671         switch (size) {
12672         case MO_8:
12673         case MO_64:
12674             unallocated_encoding(s);
12675             return;
12676         }
12677         break;
12678     }
12679     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12680         unallocated_encoding(s);
12681         return;
12682     }
12683 
12684     /* Given MemOp size, adjust register and indexing.  */
12685     switch (size) {
12686     case MO_16:
12687         index = h << 2 | l << 1 | m;
12688         break;
12689     case MO_32:
12690         index = h << 1 | l;
12691         rm |= m << 4;
12692         break;
12693     case MO_64:
12694         if (l || !is_q) {
12695             unallocated_encoding(s);
12696             return;
12697         }
12698         index = h;
12699         rm |= m << 4;
12700         break;
12701     default:
12702         g_assert_not_reached();
12703     }
12704 
12705     if (!fp_access_check(s)) {
12706         return;
12707     }
12708 
12709     if (is_fp) {
12710         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12711     } else {
12712         fpst = NULL;
12713     }
12714 
12715     switch (16 * u + opcode) {
12716     case 0x0e: /* SDOT */
12717     case 0x1e: /* UDOT */
12718         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12719                          u ? gen_helper_gvec_udot_idx_b
12720                          : gen_helper_gvec_sdot_idx_b);
12721         return;
12722     case 0x0f:
12723         switch (extract32(insn, 22, 2)) {
12724         case 0: /* SUDOT */
12725             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12726                              gen_helper_gvec_sudot_idx_b);
12727             return;
12728         case 1: /* BFDOT */
12729             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12730                              gen_helper_gvec_bfdot_idx);
12731             return;
12732         case 2: /* USDOT */
12733             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12734                              gen_helper_gvec_usdot_idx_b);
12735             return;
12736         case 3: /* BFMLAL{B,T} */
12737             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12738                               gen_helper_gvec_bfmlal_idx);
12739             return;
12740         }
12741         g_assert_not_reached();
12742     case 0x11: /* FCMLA #0 */
12743     case 0x13: /* FCMLA #90 */
12744     case 0x15: /* FCMLA #180 */
12745     case 0x17: /* FCMLA #270 */
12746         {
12747             int rot = extract32(insn, 13, 2);
12748             int data = (index << 2) | rot;
12749             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12750                                vec_full_reg_offset(s, rn),
12751                                vec_full_reg_offset(s, rm),
12752                                vec_full_reg_offset(s, rd), fpst,
12753                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12754                                size == MO_64
12755                                ? gen_helper_gvec_fcmlas_idx
12756                                : gen_helper_gvec_fcmlah_idx);
12757         }
12758         return;
12759 
12760     case 0x00: /* FMLAL */
12761     case 0x04: /* FMLSL */
12762     case 0x18: /* FMLAL2 */
12763     case 0x1c: /* FMLSL2 */
12764         {
12765             int is_s = extract32(opcode, 2, 1);
12766             int is_2 = u;
12767             int data = (index << 2) | (is_2 << 1) | is_s;
12768             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12769                                vec_full_reg_offset(s, rn),
12770                                vec_full_reg_offset(s, rm), cpu_env,
12771                                is_q ? 16 : 8, vec_full_reg_size(s),
12772                                data, gen_helper_gvec_fmlal_idx_a64);
12773         }
12774         return;
12775 
12776     case 0x08: /* MUL */
12777         if (!is_long && !is_scalar) {
12778             static gen_helper_gvec_3 * const fns[3] = {
12779                 gen_helper_gvec_mul_idx_h,
12780                 gen_helper_gvec_mul_idx_s,
12781                 gen_helper_gvec_mul_idx_d,
12782             };
12783             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
12784                                vec_full_reg_offset(s, rn),
12785                                vec_full_reg_offset(s, rm),
12786                                is_q ? 16 : 8, vec_full_reg_size(s),
12787                                index, fns[size - 1]);
12788             return;
12789         }
12790         break;
12791 
12792     case 0x10: /* MLA */
12793         if (!is_long && !is_scalar) {
12794             static gen_helper_gvec_4 * const fns[3] = {
12795                 gen_helper_gvec_mla_idx_h,
12796                 gen_helper_gvec_mla_idx_s,
12797                 gen_helper_gvec_mla_idx_d,
12798             };
12799             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12800                                vec_full_reg_offset(s, rn),
12801                                vec_full_reg_offset(s, rm),
12802                                vec_full_reg_offset(s, rd),
12803                                is_q ? 16 : 8, vec_full_reg_size(s),
12804                                index, fns[size - 1]);
12805             return;
12806         }
12807         break;
12808 
12809     case 0x14: /* MLS */
12810         if (!is_long && !is_scalar) {
12811             static gen_helper_gvec_4 * const fns[3] = {
12812                 gen_helper_gvec_mls_idx_h,
12813                 gen_helper_gvec_mls_idx_s,
12814                 gen_helper_gvec_mls_idx_d,
12815             };
12816             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12817                                vec_full_reg_offset(s, rn),
12818                                vec_full_reg_offset(s, rm),
12819                                vec_full_reg_offset(s, rd),
12820                                is_q ? 16 : 8, vec_full_reg_size(s),
12821                                index, fns[size - 1]);
12822             return;
12823         }
12824         break;
12825     }
12826 
12827     if (size == 3) {
12828         TCGv_i64 tcg_idx = tcg_temp_new_i64();
12829         int pass;
12830 
12831         assert(is_fp && is_q && !is_long);
12832 
12833         read_vec_element(s, tcg_idx, rm, index, MO_64);
12834 
12835         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12836             TCGv_i64 tcg_op = tcg_temp_new_i64();
12837             TCGv_i64 tcg_res = tcg_temp_new_i64();
12838 
12839             read_vec_element(s, tcg_op, rn, pass, MO_64);
12840 
12841             switch (16 * u + opcode) {
12842             case 0x05: /* FMLS */
12843                 /* As usual for ARM, separate negation for fused multiply-add */
12844                 gen_helper_vfp_negd(tcg_op, tcg_op);
12845                 /* fall through */
12846             case 0x01: /* FMLA */
12847                 read_vec_element(s, tcg_res, rd, pass, MO_64);
12848                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12849                 break;
12850             case 0x09: /* FMUL */
12851                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
12852                 break;
12853             case 0x19: /* FMULX */
12854                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
12855                 break;
12856             default:
12857                 g_assert_not_reached();
12858             }
12859 
12860             write_vec_element(s, tcg_res, rd, pass, MO_64);
12861         }
12862 
12863         clear_vec_high(s, !is_scalar, rd);
12864     } else if (!is_long) {
12865         /* 32 bit floating point, or 16 or 32 bit integer.
12866          * For the 16 bit scalar case we use the usual Neon helpers and
12867          * rely on the fact that 0 op 0 == 0 with no side effects.
12868          */
12869         TCGv_i32 tcg_idx = tcg_temp_new_i32();
12870         int pass, maxpasses;
12871 
12872         if (is_scalar) {
12873             maxpasses = 1;
12874         } else {
12875             maxpasses = is_q ? 4 : 2;
12876         }
12877 
12878         read_vec_element_i32(s, tcg_idx, rm, index, size);
12879 
12880         if (size == 1 && !is_scalar) {
12881             /* The simplest way to handle the 16x16 indexed ops is to duplicate
12882              * the index into both halves of the 32 bit tcg_idx and then use
12883              * the usual Neon helpers.
12884              */
12885             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12886         }
12887 
12888         for (pass = 0; pass < maxpasses; pass++) {
12889             TCGv_i32 tcg_op = tcg_temp_new_i32();
12890             TCGv_i32 tcg_res = tcg_temp_new_i32();
12891 
12892             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
12893 
12894             switch (16 * u + opcode) {
12895             case 0x08: /* MUL */
12896             case 0x10: /* MLA */
12897             case 0x14: /* MLS */
12898             {
12899                 static NeonGenTwoOpFn * const fns[2][2] = {
12900                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
12901                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
12902                 };
12903                 NeonGenTwoOpFn *genfn;
12904                 bool is_sub = opcode == 0x4;
12905 
12906                 if (size == 1) {
12907                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
12908                 } else {
12909                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
12910                 }
12911                 if (opcode == 0x8) {
12912                     break;
12913                 }
12914                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
12915                 genfn = fns[size - 1][is_sub];
12916                 genfn(tcg_res, tcg_op, tcg_res);
12917                 break;
12918             }
12919             case 0x05: /* FMLS */
12920             case 0x01: /* FMLA */
12921                 read_vec_element_i32(s, tcg_res, rd, pass,
12922                                      is_scalar ? size : MO_32);
12923                 switch (size) {
12924                 case 1:
12925                     if (opcode == 0x5) {
12926                         /* As usual for ARM, separate negation for fused
12927                          * multiply-add */
12928                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
12929                     }
12930                     if (is_scalar) {
12931                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
12932                                                    tcg_res, fpst);
12933                     } else {
12934                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
12935                                                     tcg_res, fpst);
12936                     }
12937                     break;
12938                 case 2:
12939                     if (opcode == 0x5) {
12940                         /* As usual for ARM, separate negation for
12941                          * fused multiply-add */
12942                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
12943                     }
12944                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
12945                                            tcg_res, fpst);
12946                     break;
12947                 default:
12948                     g_assert_not_reached();
12949                 }
12950                 break;
12951             case 0x09: /* FMUL */
12952                 switch (size) {
12953                 case 1:
12954                     if (is_scalar) {
12955                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
12956                                                 tcg_idx, fpst);
12957                     } else {
12958                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
12959                                                  tcg_idx, fpst);
12960                     }
12961                     break;
12962                 case 2:
12963                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
12964                     break;
12965                 default:
12966                     g_assert_not_reached();
12967                 }
12968                 break;
12969             case 0x19: /* FMULX */
12970                 switch (size) {
12971                 case 1:
12972                     if (is_scalar) {
12973                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
12974                                                  tcg_idx, fpst);
12975                     } else {
12976                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
12977                                                   tcg_idx, fpst);
12978                     }
12979                     break;
12980                 case 2:
12981                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
12982                     break;
12983                 default:
12984                     g_assert_not_reached();
12985                 }
12986                 break;
12987             case 0x0c: /* SQDMULH */
12988                 if (size == 1) {
12989                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
12990                                                tcg_op, tcg_idx);
12991                 } else {
12992                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
12993                                                tcg_op, tcg_idx);
12994                 }
12995                 break;
12996             case 0x0d: /* SQRDMULH */
12997                 if (size == 1) {
12998                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
12999                                                 tcg_op, tcg_idx);
13000                 } else {
13001                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13002                                                 tcg_op, tcg_idx);
13003                 }
13004                 break;
13005             case 0x1d: /* SQRDMLAH */
13006                 read_vec_element_i32(s, tcg_res, rd, pass,
13007                                      is_scalar ? size : MO_32);
13008                 if (size == 1) {
13009                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13010                                                 tcg_op, tcg_idx, tcg_res);
13011                 } else {
13012                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13013                                                 tcg_op, tcg_idx, tcg_res);
13014                 }
13015                 break;
13016             case 0x1f: /* SQRDMLSH */
13017                 read_vec_element_i32(s, tcg_res, rd, pass,
13018                                      is_scalar ? size : MO_32);
13019                 if (size == 1) {
13020                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13021                                                 tcg_op, tcg_idx, tcg_res);
13022                 } else {
13023                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13024                                                 tcg_op, tcg_idx, tcg_res);
13025                 }
13026                 break;
13027             default:
13028                 g_assert_not_reached();
13029             }
13030 
13031             if (is_scalar) {
13032                 write_fp_sreg(s, rd, tcg_res);
13033             } else {
13034                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13035             }
13036         }
13037 
13038         clear_vec_high(s, is_q, rd);
13039     } else {
13040         /* long ops: 16x16->32 or 32x32->64 */
13041         TCGv_i64 tcg_res[2];
13042         int pass;
13043         bool satop = extract32(opcode, 0, 1);
13044         MemOp memop = MO_32;
13045 
13046         if (satop || !u) {
13047             memop |= MO_SIGN;
13048         }
13049 
13050         if (size == 2) {
13051             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13052 
13053             read_vec_element(s, tcg_idx, rm, index, memop);
13054 
13055             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13056                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13057                 TCGv_i64 tcg_passres;
13058                 int passelt;
13059 
13060                 if (is_scalar) {
13061                     passelt = 0;
13062                 } else {
13063                     passelt = pass + (is_q * 2);
13064                 }
13065 
13066                 read_vec_element(s, tcg_op, rn, passelt, memop);
13067 
13068                 tcg_res[pass] = tcg_temp_new_i64();
13069 
13070                 if (opcode == 0xa || opcode == 0xb) {
13071                     /* Non-accumulating ops */
13072                     tcg_passres = tcg_res[pass];
13073                 } else {
13074                     tcg_passres = tcg_temp_new_i64();
13075                 }
13076 
13077                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13078 
13079                 if (satop) {
13080                     /* saturating, doubling */
13081                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13082                                                       tcg_passres, tcg_passres);
13083                 }
13084 
13085                 if (opcode == 0xa || opcode == 0xb) {
13086                     continue;
13087                 }
13088 
13089                 /* Accumulating op: handle accumulate step */
13090                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13091 
13092                 switch (opcode) {
13093                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13094                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13095                     break;
13096                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13097                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13098                     break;
13099                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13100                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13101                     /* fall through */
13102                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13103                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13104                                                       tcg_res[pass],
13105                                                       tcg_passres);
13106                     break;
13107                 default:
13108                     g_assert_not_reached();
13109                 }
13110             }
13111 
13112             clear_vec_high(s, !is_scalar, rd);
13113         } else {
13114             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13115 
13116             assert(size == 1);
13117             read_vec_element_i32(s, tcg_idx, rm, index, size);
13118 
13119             if (!is_scalar) {
13120                 /* The simplest way to handle the 16x16 indexed ops is to
13121                  * duplicate the index into both halves of the 32 bit tcg_idx
13122                  * and then use the usual Neon helpers.
13123                  */
13124                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13125             }
13126 
13127             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13128                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13129                 TCGv_i64 tcg_passres;
13130 
13131                 if (is_scalar) {
13132                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13133                 } else {
13134                     read_vec_element_i32(s, tcg_op, rn,
13135                                          pass + (is_q * 2), MO_32);
13136                 }
13137 
13138                 tcg_res[pass] = tcg_temp_new_i64();
13139 
13140                 if (opcode == 0xa || opcode == 0xb) {
13141                     /* Non-accumulating ops */
13142                     tcg_passres = tcg_res[pass];
13143                 } else {
13144                     tcg_passres = tcg_temp_new_i64();
13145                 }
13146 
13147                 if (memop & MO_SIGN) {
13148                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13149                 } else {
13150                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13151                 }
13152                 if (satop) {
13153                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13154                                                       tcg_passres, tcg_passres);
13155                 }
13156 
13157                 if (opcode == 0xa || opcode == 0xb) {
13158                     continue;
13159                 }
13160 
13161                 /* Accumulating op: handle accumulate step */
13162                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13163 
13164                 switch (opcode) {
13165                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13166                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13167                                              tcg_passres);
13168                     break;
13169                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13170                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13171                                              tcg_passres);
13172                     break;
13173                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13174                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13175                     /* fall through */
13176                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13177                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13178                                                       tcg_res[pass],
13179                                                       tcg_passres);
13180                     break;
13181                 default:
13182                     g_assert_not_reached();
13183                 }
13184             }
13185 
13186             if (is_scalar) {
13187                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13188             }
13189         }
13190 
13191         if (is_scalar) {
13192             tcg_res[1] = tcg_constant_i64(0);
13193         }
13194 
13195         for (pass = 0; pass < 2; pass++) {
13196             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13197         }
13198     }
13199 }
13200 
13201 /* Crypto AES
13202  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13203  * +-----------------+------+-----------+--------+-----+------+------+
13204  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13205  * +-----------------+------+-----------+--------+-----+------+------+
13206  */
13207 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13208 {
13209     int size = extract32(insn, 22, 2);
13210     int opcode = extract32(insn, 12, 5);
13211     int rn = extract32(insn, 5, 5);
13212     int rd = extract32(insn, 0, 5);
13213     gen_helper_gvec_2 *genfn2 = NULL;
13214     gen_helper_gvec_3 *genfn3 = NULL;
13215 
13216     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13217         unallocated_encoding(s);
13218         return;
13219     }
13220 
13221     switch (opcode) {
13222     case 0x4: /* AESE */
13223         genfn3 = gen_helper_crypto_aese;
13224         break;
13225     case 0x6: /* AESMC */
13226         genfn2 = gen_helper_crypto_aesmc;
13227         break;
13228     case 0x5: /* AESD */
13229         genfn3 = gen_helper_crypto_aesd;
13230         break;
13231     case 0x7: /* AESIMC */
13232         genfn2 = gen_helper_crypto_aesimc;
13233         break;
13234     default:
13235         unallocated_encoding(s);
13236         return;
13237     }
13238 
13239     if (!fp_access_check(s)) {
13240         return;
13241     }
13242     if (genfn2) {
13243         gen_gvec_op2_ool(s, true, rd, rn, 0, genfn2);
13244     } else {
13245         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, genfn3);
13246     }
13247 }
13248 
13249 /* Crypto three-reg SHA
13250  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13251  * +-----------------+------+---+------+---+--------+-----+------+------+
13252  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13253  * +-----------------+------+---+------+---+--------+-----+------+------+
13254  */
13255 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13256 {
13257     int size = extract32(insn, 22, 2);
13258     int opcode = extract32(insn, 12, 3);
13259     int rm = extract32(insn, 16, 5);
13260     int rn = extract32(insn, 5, 5);
13261     int rd = extract32(insn, 0, 5);
13262     gen_helper_gvec_3 *genfn;
13263     bool feature;
13264 
13265     if (size != 0) {
13266         unallocated_encoding(s);
13267         return;
13268     }
13269 
13270     switch (opcode) {
13271     case 0: /* SHA1C */
13272         genfn = gen_helper_crypto_sha1c;
13273         feature = dc_isar_feature(aa64_sha1, s);
13274         break;
13275     case 1: /* SHA1P */
13276         genfn = gen_helper_crypto_sha1p;
13277         feature = dc_isar_feature(aa64_sha1, s);
13278         break;
13279     case 2: /* SHA1M */
13280         genfn = gen_helper_crypto_sha1m;
13281         feature = dc_isar_feature(aa64_sha1, s);
13282         break;
13283     case 3: /* SHA1SU0 */
13284         genfn = gen_helper_crypto_sha1su0;
13285         feature = dc_isar_feature(aa64_sha1, s);
13286         break;
13287     case 4: /* SHA256H */
13288         genfn = gen_helper_crypto_sha256h;
13289         feature = dc_isar_feature(aa64_sha256, s);
13290         break;
13291     case 5: /* SHA256H2 */
13292         genfn = gen_helper_crypto_sha256h2;
13293         feature = dc_isar_feature(aa64_sha256, s);
13294         break;
13295     case 6: /* SHA256SU1 */
13296         genfn = gen_helper_crypto_sha256su1;
13297         feature = dc_isar_feature(aa64_sha256, s);
13298         break;
13299     default:
13300         unallocated_encoding(s);
13301         return;
13302     }
13303 
13304     if (!feature) {
13305         unallocated_encoding(s);
13306         return;
13307     }
13308 
13309     if (!fp_access_check(s)) {
13310         return;
13311     }
13312     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13313 }
13314 
13315 /* Crypto two-reg SHA
13316  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13317  * +-----------------+------+-----------+--------+-----+------+------+
13318  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13319  * +-----------------+------+-----------+--------+-----+------+------+
13320  */
13321 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13322 {
13323     int size = extract32(insn, 22, 2);
13324     int opcode = extract32(insn, 12, 5);
13325     int rn = extract32(insn, 5, 5);
13326     int rd = extract32(insn, 0, 5);
13327     gen_helper_gvec_2 *genfn;
13328     bool feature;
13329 
13330     if (size != 0) {
13331         unallocated_encoding(s);
13332         return;
13333     }
13334 
13335     switch (opcode) {
13336     case 0: /* SHA1H */
13337         feature = dc_isar_feature(aa64_sha1, s);
13338         genfn = gen_helper_crypto_sha1h;
13339         break;
13340     case 1: /* SHA1SU1 */
13341         feature = dc_isar_feature(aa64_sha1, s);
13342         genfn = gen_helper_crypto_sha1su1;
13343         break;
13344     case 2: /* SHA256SU0 */
13345         feature = dc_isar_feature(aa64_sha256, s);
13346         genfn = gen_helper_crypto_sha256su0;
13347         break;
13348     default:
13349         unallocated_encoding(s);
13350         return;
13351     }
13352 
13353     if (!feature) {
13354         unallocated_encoding(s);
13355         return;
13356     }
13357 
13358     if (!fp_access_check(s)) {
13359         return;
13360     }
13361     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13362 }
13363 
13364 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13365 {
13366     tcg_gen_rotli_i64(d, m, 1);
13367     tcg_gen_xor_i64(d, d, n);
13368 }
13369 
13370 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13371 {
13372     tcg_gen_rotli_vec(vece, d, m, 1);
13373     tcg_gen_xor_vec(vece, d, d, n);
13374 }
13375 
13376 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13377                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13378 {
13379     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13380     static const GVecGen3 op = {
13381         .fni8 = gen_rax1_i64,
13382         .fniv = gen_rax1_vec,
13383         .opt_opc = vecop_list,
13384         .fno = gen_helper_crypto_rax1,
13385         .vece = MO_64,
13386     };
13387     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13388 }
13389 
13390 /* Crypto three-reg SHA512
13391  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13392  * +-----------------------+------+---+---+-----+--------+------+------+
13393  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13394  * +-----------------------+------+---+---+-----+--------+------+------+
13395  */
13396 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13397 {
13398     int opcode = extract32(insn, 10, 2);
13399     int o =  extract32(insn, 14, 1);
13400     int rm = extract32(insn, 16, 5);
13401     int rn = extract32(insn, 5, 5);
13402     int rd = extract32(insn, 0, 5);
13403     bool feature;
13404     gen_helper_gvec_3 *oolfn = NULL;
13405     GVecGen3Fn *gvecfn = NULL;
13406 
13407     if (o == 0) {
13408         switch (opcode) {
13409         case 0: /* SHA512H */
13410             feature = dc_isar_feature(aa64_sha512, s);
13411             oolfn = gen_helper_crypto_sha512h;
13412             break;
13413         case 1: /* SHA512H2 */
13414             feature = dc_isar_feature(aa64_sha512, s);
13415             oolfn = gen_helper_crypto_sha512h2;
13416             break;
13417         case 2: /* SHA512SU1 */
13418             feature = dc_isar_feature(aa64_sha512, s);
13419             oolfn = gen_helper_crypto_sha512su1;
13420             break;
13421         case 3: /* RAX1 */
13422             feature = dc_isar_feature(aa64_sha3, s);
13423             gvecfn = gen_gvec_rax1;
13424             break;
13425         default:
13426             g_assert_not_reached();
13427         }
13428     } else {
13429         switch (opcode) {
13430         case 0: /* SM3PARTW1 */
13431             feature = dc_isar_feature(aa64_sm3, s);
13432             oolfn = gen_helper_crypto_sm3partw1;
13433             break;
13434         case 1: /* SM3PARTW2 */
13435             feature = dc_isar_feature(aa64_sm3, s);
13436             oolfn = gen_helper_crypto_sm3partw2;
13437             break;
13438         case 2: /* SM4EKEY */
13439             feature = dc_isar_feature(aa64_sm4, s);
13440             oolfn = gen_helper_crypto_sm4ekey;
13441             break;
13442         default:
13443             unallocated_encoding(s);
13444             return;
13445         }
13446     }
13447 
13448     if (!feature) {
13449         unallocated_encoding(s);
13450         return;
13451     }
13452 
13453     if (!fp_access_check(s)) {
13454         return;
13455     }
13456 
13457     if (oolfn) {
13458         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13459     } else {
13460         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13461     }
13462 }
13463 
13464 /* Crypto two-reg SHA512
13465  *  31                                     12  11  10  9    5 4    0
13466  * +-----------------------------------------+--------+------+------+
13467  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13468  * +-----------------------------------------+--------+------+------+
13469  */
13470 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13471 {
13472     int opcode = extract32(insn, 10, 2);
13473     int rn = extract32(insn, 5, 5);
13474     int rd = extract32(insn, 0, 5);
13475     bool feature;
13476 
13477     switch (opcode) {
13478     case 0: /* SHA512SU0 */
13479         feature = dc_isar_feature(aa64_sha512, s);
13480         break;
13481     case 1: /* SM4E */
13482         feature = dc_isar_feature(aa64_sm4, s);
13483         break;
13484     default:
13485         unallocated_encoding(s);
13486         return;
13487     }
13488 
13489     if (!feature) {
13490         unallocated_encoding(s);
13491         return;
13492     }
13493 
13494     if (!fp_access_check(s)) {
13495         return;
13496     }
13497 
13498     switch (opcode) {
13499     case 0: /* SHA512SU0 */
13500         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13501         break;
13502     case 1: /* SM4E */
13503         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13504         break;
13505     default:
13506         g_assert_not_reached();
13507     }
13508 }
13509 
13510 /* Crypto four-register
13511  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13512  * +-------------------+-----+------+---+------+------+------+
13513  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13514  * +-------------------+-----+------+---+------+------+------+
13515  */
13516 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13517 {
13518     int op0 = extract32(insn, 21, 2);
13519     int rm = extract32(insn, 16, 5);
13520     int ra = extract32(insn, 10, 5);
13521     int rn = extract32(insn, 5, 5);
13522     int rd = extract32(insn, 0, 5);
13523     bool feature;
13524 
13525     switch (op0) {
13526     case 0: /* EOR3 */
13527     case 1: /* BCAX */
13528         feature = dc_isar_feature(aa64_sha3, s);
13529         break;
13530     case 2: /* SM3SS1 */
13531         feature = dc_isar_feature(aa64_sm3, s);
13532         break;
13533     default:
13534         unallocated_encoding(s);
13535         return;
13536     }
13537 
13538     if (!feature) {
13539         unallocated_encoding(s);
13540         return;
13541     }
13542 
13543     if (!fp_access_check(s)) {
13544         return;
13545     }
13546 
13547     if (op0 < 2) {
13548         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13549         int pass;
13550 
13551         tcg_op1 = tcg_temp_new_i64();
13552         tcg_op2 = tcg_temp_new_i64();
13553         tcg_op3 = tcg_temp_new_i64();
13554         tcg_res[0] = tcg_temp_new_i64();
13555         tcg_res[1] = tcg_temp_new_i64();
13556 
13557         for (pass = 0; pass < 2; pass++) {
13558             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13559             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13560             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13561 
13562             if (op0 == 0) {
13563                 /* EOR3 */
13564                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13565             } else {
13566                 /* BCAX */
13567                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13568             }
13569             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13570         }
13571         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13572         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13573     } else {
13574         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13575 
13576         tcg_op1 = tcg_temp_new_i32();
13577         tcg_op2 = tcg_temp_new_i32();
13578         tcg_op3 = tcg_temp_new_i32();
13579         tcg_res = tcg_temp_new_i32();
13580         tcg_zero = tcg_constant_i32(0);
13581 
13582         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13583         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13584         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13585 
13586         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13587         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13588         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13589         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13590 
13591         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13592         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13593         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13594         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13595     }
13596 }
13597 
13598 /* Crypto XAR
13599  *  31                   21 20  16 15    10 9    5 4    0
13600  * +-----------------------+------+--------+------+------+
13601  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13602  * +-----------------------+------+--------+------+------+
13603  */
13604 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13605 {
13606     int rm = extract32(insn, 16, 5);
13607     int imm6 = extract32(insn, 10, 6);
13608     int rn = extract32(insn, 5, 5);
13609     int rd = extract32(insn, 0, 5);
13610 
13611     if (!dc_isar_feature(aa64_sha3, s)) {
13612         unallocated_encoding(s);
13613         return;
13614     }
13615 
13616     if (!fp_access_check(s)) {
13617         return;
13618     }
13619 
13620     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13621                  vec_full_reg_offset(s, rn),
13622                  vec_full_reg_offset(s, rm), imm6, 16,
13623                  vec_full_reg_size(s));
13624 }
13625 
13626 /* Crypto three-reg imm2
13627  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13628  * +-----------------------+------+-----+------+--------+------+------+
13629  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13630  * +-----------------------+------+-----+------+--------+------+------+
13631  */
13632 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13633 {
13634     static gen_helper_gvec_3 * const fns[4] = {
13635         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13636         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13637     };
13638     int opcode = extract32(insn, 10, 2);
13639     int imm2 = extract32(insn, 12, 2);
13640     int rm = extract32(insn, 16, 5);
13641     int rn = extract32(insn, 5, 5);
13642     int rd = extract32(insn, 0, 5);
13643 
13644     if (!dc_isar_feature(aa64_sm3, s)) {
13645         unallocated_encoding(s);
13646         return;
13647     }
13648 
13649     if (!fp_access_check(s)) {
13650         return;
13651     }
13652 
13653     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13654 }
13655 
13656 /* C3.6 Data processing - SIMD, inc Crypto
13657  *
13658  * As the decode gets a little complex we are using a table based
13659  * approach for this part of the decode.
13660  */
13661 static const AArch64DecodeTable data_proc_simd[] = {
13662     /* pattern  ,  mask     ,  fn                        */
13663     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13664     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13665     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13666     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13667     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13668     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13669     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13670     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13671     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13672     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13673     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13674     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13675     { 0x2e000000, 0xbf208400, disas_simd_ext },
13676     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13677     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13678     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13679     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13680     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13681     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13682     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13683     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13684     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13685     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13686     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13687     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13688     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13689     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13690     { 0xce800000, 0xffe00000, disas_crypto_xar },
13691     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13692     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13693     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13694     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13695     { 0x00000000, 0x00000000, NULL }
13696 };
13697 
13698 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13699 {
13700     /* Note that this is called with all non-FP cases from
13701      * table C3-6 so it must UNDEF for entries not specifically
13702      * allocated to instructions in that table.
13703      */
13704     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13705     if (fn) {
13706         fn(s, insn);
13707     } else {
13708         unallocated_encoding(s);
13709     }
13710 }
13711 
13712 /* C3.6 Data processing - SIMD and floating point */
13713 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13714 {
13715     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13716         disas_data_proc_fp(s, insn);
13717     } else {
13718         /* SIMD, including crypto */
13719         disas_data_proc_simd(s, insn);
13720     }
13721 }
13722 
13723 static bool trans_OK(DisasContext *s, arg_OK *a)
13724 {
13725     return true;
13726 }
13727 
13728 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13729 {
13730     s->is_nonstreaming = true;
13731     return true;
13732 }
13733 
13734 /**
13735  * is_guarded_page:
13736  * @env: The cpu environment
13737  * @s: The DisasContext
13738  *
13739  * Return true if the page is guarded.
13740  */
13741 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13742 {
13743     uint64_t addr = s->base.pc_first;
13744 #ifdef CONFIG_USER_ONLY
13745     return page_get_flags(addr) & PAGE_BTI;
13746 #else
13747     CPUTLBEntryFull *full;
13748     void *host;
13749     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13750     int flags;
13751 
13752     /*
13753      * We test this immediately after reading an insn, which means
13754      * that the TLB entry must be present and valid, and thus this
13755      * access will never raise an exception.
13756      */
13757     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13758                               false, &host, &full, 0);
13759     assert(!(flags & TLB_INVALID_MASK));
13760 
13761     return full->guarded;
13762 #endif
13763 }
13764 
13765 /**
13766  * btype_destination_ok:
13767  * @insn: The instruction at the branch destination
13768  * @bt: SCTLR_ELx.BT
13769  * @btype: PSTATE.BTYPE, and is non-zero
13770  *
13771  * On a guarded page, there are a limited number of insns
13772  * that may be present at the branch target:
13773  *   - branch target identifiers,
13774  *   - paciasp, pacibsp,
13775  *   - BRK insn
13776  *   - HLT insn
13777  * Anything else causes a Branch Target Exception.
13778  *
13779  * Return true if the branch is compatible, false to raise BTITRAP.
13780  */
13781 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13782 {
13783     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13784         /* HINT space */
13785         switch (extract32(insn, 5, 7)) {
13786         case 0b011001: /* PACIASP */
13787         case 0b011011: /* PACIBSP */
13788             /*
13789              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13790              * with btype == 3.  Otherwise all btype are ok.
13791              */
13792             return !bt || btype != 3;
13793         case 0b100000: /* BTI */
13794             /* Not compatible with any btype.  */
13795             return false;
13796         case 0b100010: /* BTI c */
13797             /* Not compatible with btype == 3 */
13798             return btype != 3;
13799         case 0b100100: /* BTI j */
13800             /* Not compatible with btype == 2 */
13801             return btype != 2;
13802         case 0b100110: /* BTI jc */
13803             /* Compatible with any btype.  */
13804             return true;
13805         }
13806     } else {
13807         switch (insn & 0xffe0001fu) {
13808         case 0xd4200000u: /* BRK */
13809         case 0xd4400000u: /* HLT */
13810             /* Give priority to the breakpoint exception.  */
13811             return true;
13812         }
13813     }
13814     return false;
13815 }
13816 
13817 /* C3.1 A64 instruction index by encoding */
13818 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13819 {
13820     switch (extract32(insn, 25, 4)) {
13821     case 0x5:
13822     case 0xd:      /* Data processing - register */
13823         disas_data_proc_reg(s, insn);
13824         break;
13825     case 0x7:
13826     case 0xf:      /* Data processing - SIMD and floating point */
13827         disas_data_proc_simd_fp(s, insn);
13828         break;
13829     default:
13830         unallocated_encoding(s);
13831         break;
13832     }
13833 }
13834 
13835 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13836                                           CPUState *cpu)
13837 {
13838     DisasContext *dc = container_of(dcbase, DisasContext, base);
13839     CPUARMState *env = cpu->env_ptr;
13840     ARMCPU *arm_cpu = env_archcpu(env);
13841     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13842     int bound, core_mmu_idx;
13843 
13844     dc->isar = &arm_cpu->isar;
13845     dc->condjmp = 0;
13846     dc->pc_save = dc->base.pc_first;
13847     dc->aarch64 = true;
13848     dc->thumb = false;
13849     dc->sctlr_b = 0;
13850     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13851     dc->condexec_mask = 0;
13852     dc->condexec_cond = 0;
13853     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
13854     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
13855     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
13856     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
13857     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
13858     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
13859 #if !defined(CONFIG_USER_ONLY)
13860     dc->user = (dc->current_el == 0);
13861 #endif
13862     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
13863     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
13864     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
13865     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
13866     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
13867     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
13868     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
13869     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
13870     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
13871     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
13872     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
13873     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
13874     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
13875     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
13876     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
13877     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
13878     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
13879     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
13880     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
13881     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
13882     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
13883     dc->vec_len = 0;
13884     dc->vec_stride = 0;
13885     dc->cp_regs = arm_cpu->cp_regs;
13886     dc->features = env->features;
13887     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
13888     dc->gm_blocksize = arm_cpu->gm_blocksize;
13889 
13890 #ifdef CONFIG_USER_ONLY
13891     /* In sve_probe_page, we assume TBI is enabled. */
13892     tcg_debug_assert(dc->tbid & 1);
13893 #endif
13894 
13895     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
13896 
13897     /* Single step state. The code-generation logic here is:
13898      *  SS_ACTIVE == 0:
13899      *   generate code with no special handling for single-stepping (except
13900      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
13901      *   this happens anyway because those changes are all system register or
13902      *   PSTATE writes).
13903      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
13904      *   emit code for one insn
13905      *   emit code to clear PSTATE.SS
13906      *   emit code to generate software step exception for completed step
13907      *   end TB (as usual for having generated an exception)
13908      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
13909      *   emit code to generate a software step exception
13910      *   end the TB
13911      */
13912     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
13913     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
13914     dc->is_ldex = false;
13915 
13916     /* Bound the number of insns to execute to those left on the page.  */
13917     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
13918 
13919     /* If architectural single step active, limit to 1.  */
13920     if (dc->ss_active) {
13921         bound = 1;
13922     }
13923     dc->base.max_insns = MIN(dc->base.max_insns, bound);
13924 }
13925 
13926 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
13927 {
13928 }
13929 
13930 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
13931 {
13932     DisasContext *dc = container_of(dcbase, DisasContext, base);
13933     target_ulong pc_arg = dc->base.pc_next;
13934 
13935     if (tb_cflags(dcbase->tb) & CF_PCREL) {
13936         pc_arg &= ~TARGET_PAGE_MASK;
13937     }
13938     tcg_gen_insn_start(pc_arg, 0, 0);
13939     dc->insn_start = tcg_last_op();
13940 }
13941 
13942 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
13943 {
13944     DisasContext *s = container_of(dcbase, DisasContext, base);
13945     CPUARMState *env = cpu->env_ptr;
13946     uint64_t pc = s->base.pc_next;
13947     uint32_t insn;
13948 
13949     /* Singlestep exceptions have the highest priority. */
13950     if (s->ss_active && !s->pstate_ss) {
13951         /* Singlestep state is Active-pending.
13952          * If we're in this state at the start of a TB then either
13953          *  a) we just took an exception to an EL which is being debugged
13954          *     and this is the first insn in the exception handler
13955          *  b) debug exceptions were masked and we just unmasked them
13956          *     without changing EL (eg by clearing PSTATE.D)
13957          * In either case we're going to take a swstep exception in the
13958          * "did not step an insn" case, and so the syndrome ISV and EX
13959          * bits should be zero.
13960          */
13961         assert(s->base.num_insns == 1);
13962         gen_swstep_exception(s, 0, 0);
13963         s->base.is_jmp = DISAS_NORETURN;
13964         s->base.pc_next = pc + 4;
13965         return;
13966     }
13967 
13968     if (pc & 3) {
13969         /*
13970          * PC alignment fault.  This has priority over the instruction abort
13971          * that we would receive from a translation fault via arm_ldl_code.
13972          * This should only be possible after an indirect branch, at the
13973          * start of the TB.
13974          */
13975         assert(s->base.num_insns == 1);
13976         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
13977         s->base.is_jmp = DISAS_NORETURN;
13978         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
13979         return;
13980     }
13981 
13982     s->pc_curr = pc;
13983     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
13984     s->insn = insn;
13985     s->base.pc_next = pc + 4;
13986 
13987     s->fp_access_checked = false;
13988     s->sve_access_checked = false;
13989 
13990     if (s->pstate_il) {
13991         /*
13992          * Illegal execution state. This has priority over BTI
13993          * exceptions, but comes after instruction abort exceptions.
13994          */
13995         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
13996         return;
13997     }
13998 
13999     if (dc_isar_feature(aa64_bti, s)) {
14000         if (s->base.num_insns == 1) {
14001             /*
14002              * At the first insn of the TB, compute s->guarded_page.
14003              * We delayed computing this until successfully reading
14004              * the first insn of the TB, above.  This (mostly) ensures
14005              * that the softmmu tlb entry has been populated, and the
14006              * page table GP bit is available.
14007              *
14008              * Note that we need to compute this even if btype == 0,
14009              * because this value is used for BR instructions later
14010              * where ENV is not available.
14011              */
14012             s->guarded_page = is_guarded_page(env, s);
14013 
14014             /* First insn can have btype set to non-zero.  */
14015             tcg_debug_assert(s->btype >= 0);
14016 
14017             /*
14018              * Note that the Branch Target Exception has fairly high
14019              * priority -- below debugging exceptions but above most
14020              * everything else.  This allows us to handle this now
14021              * instead of waiting until the insn is otherwise decoded.
14022              */
14023             if (s->btype != 0
14024                 && s->guarded_page
14025                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14026                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14027                 return;
14028             }
14029         } else {
14030             /* Not the first insn: btype must be 0.  */
14031             tcg_debug_assert(s->btype == 0);
14032         }
14033     }
14034 
14035     s->is_nonstreaming = false;
14036     if (s->sme_trap_nonstreaming) {
14037         disas_sme_fa64(s, insn);
14038     }
14039 
14040     if (!disas_a64(s, insn) &&
14041         !disas_sme(s, insn) &&
14042         !disas_sve(s, insn)) {
14043         disas_a64_legacy(s, insn);
14044     }
14045 
14046     /*
14047      * After execution of most insns, btype is reset to 0.
14048      * Note that we set btype == -1 when the insn sets btype.
14049      */
14050     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14051         reset_btype(s);
14052     }
14053 }
14054 
14055 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14056 {
14057     DisasContext *dc = container_of(dcbase, DisasContext, base);
14058 
14059     if (unlikely(dc->ss_active)) {
14060         /* Note that this means single stepping WFI doesn't halt the CPU.
14061          * For conditional branch insns this is harmless unreachable code as
14062          * gen_goto_tb() has already handled emitting the debug exception
14063          * (and thus a tb-jump is not possible when singlestepping).
14064          */
14065         switch (dc->base.is_jmp) {
14066         default:
14067             gen_a64_update_pc(dc, 4);
14068             /* fall through */
14069         case DISAS_EXIT:
14070         case DISAS_JUMP:
14071             gen_step_complete_exception(dc);
14072             break;
14073         case DISAS_NORETURN:
14074             break;
14075         }
14076     } else {
14077         switch (dc->base.is_jmp) {
14078         case DISAS_NEXT:
14079         case DISAS_TOO_MANY:
14080             gen_goto_tb(dc, 1, 4);
14081             break;
14082         default:
14083         case DISAS_UPDATE_EXIT:
14084             gen_a64_update_pc(dc, 4);
14085             /* fall through */
14086         case DISAS_EXIT:
14087             tcg_gen_exit_tb(NULL, 0);
14088             break;
14089         case DISAS_UPDATE_NOCHAIN:
14090             gen_a64_update_pc(dc, 4);
14091             /* fall through */
14092         case DISAS_JUMP:
14093             tcg_gen_lookup_and_goto_ptr();
14094             break;
14095         case DISAS_NORETURN:
14096         case DISAS_SWI:
14097             break;
14098         case DISAS_WFE:
14099             gen_a64_update_pc(dc, 4);
14100             gen_helper_wfe(cpu_env);
14101             break;
14102         case DISAS_YIELD:
14103             gen_a64_update_pc(dc, 4);
14104             gen_helper_yield(cpu_env);
14105             break;
14106         case DISAS_WFI:
14107             /*
14108              * This is a special case because we don't want to just halt
14109              * the CPU if trying to debug across a WFI.
14110              */
14111             gen_a64_update_pc(dc, 4);
14112             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14113             /*
14114              * The helper doesn't necessarily throw an exception, but we
14115              * must go back to the main loop to check for interrupts anyway.
14116              */
14117             tcg_gen_exit_tb(NULL, 0);
14118             break;
14119         }
14120     }
14121 }
14122 
14123 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14124                                  CPUState *cpu, FILE *logfile)
14125 {
14126     DisasContext *dc = container_of(dcbase, DisasContext, base);
14127 
14128     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14129     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14130 }
14131 
14132 const TranslatorOps aarch64_translator_ops = {
14133     .init_disas_context = aarch64_tr_init_disas_context,
14134     .tb_start           = aarch64_tr_tb_start,
14135     .insn_start         = aarch64_tr_insn_start,
14136     .translate_insn     = aarch64_tr_translate_insn,
14137     .tb_stop            = aarch64_tr_tb_stop,
14138     .disas_log          = aarch64_tr_disas_log,
14139 };
14140