xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 99bb43c0)
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  * Include the generated decoders.
51  */
52 
53 #include "decode-sme-fa64.c.inc"
54 #include "decode-a64.c.inc"
55 
56 /* Table based decoder typedefs - used when the relevant bits for decode
57  * are too awkwardly scattered across the instruction (eg SIMD).
58  */
59 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
60 
61 typedef struct AArch64DecodeTable {
62     uint32_t pattern;
63     uint32_t mask;
64     AArch64DecodeFn *disas_fn;
65 } AArch64DecodeTable;
66 
67 /* initialize TCG globals.  */
68 void a64_translate_init(void)
69 {
70     int i;
71 
72     cpu_pc = tcg_global_mem_new_i64(cpu_env,
73                                     offsetof(CPUARMState, pc),
74                                     "pc");
75     for (i = 0; i < 32; i++) {
76         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
77                                           offsetof(CPUARMState, xregs[i]),
78                                           regnames[i]);
79     }
80 
81     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
82         offsetof(CPUARMState, exclusive_high), "exclusive_high");
83 }
84 
85 /*
86  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
87  */
88 static int get_a64_user_mem_index(DisasContext *s)
89 {
90     /*
91      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
92      * which is the usual mmu_idx for this cpu state.
93      */
94     ARMMMUIdx useridx = s->mmu_idx;
95 
96     if (s->unpriv) {
97         /*
98          * We have pre-computed the condition for AccType_UNPRIV.
99          * Therefore we should never get here with a mmu_idx for
100          * which we do not know the corresponding user mmu_idx.
101          */
102         switch (useridx) {
103         case ARMMMUIdx_E10_1:
104         case ARMMMUIdx_E10_1_PAN:
105             useridx = ARMMMUIdx_E10_0;
106             break;
107         case ARMMMUIdx_E20_2:
108         case ARMMMUIdx_E20_2_PAN:
109             useridx = ARMMMUIdx_E20_0;
110             break;
111         default:
112             g_assert_not_reached();
113         }
114     }
115     return arm_to_core_mmu_idx(useridx);
116 }
117 
118 static void set_btype_raw(int val)
119 {
120     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
121                    offsetof(CPUARMState, btype));
122 }
123 
124 static void set_btype(DisasContext *s, int val)
125 {
126     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
127     tcg_debug_assert(val >= 1 && val <= 3);
128     set_btype_raw(val);
129     s->btype = -1;
130 }
131 
132 static void reset_btype(DisasContext *s)
133 {
134     if (s->btype != 0) {
135         set_btype_raw(0);
136         s->btype = 0;
137     }
138 }
139 
140 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
141 {
142     assert(s->pc_save != -1);
143     if (tb_cflags(s->base.tb) & CF_PCREL) {
144         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
145     } else {
146         tcg_gen_movi_i64(dest, s->pc_curr + diff);
147     }
148 }
149 
150 void gen_a64_update_pc(DisasContext *s, target_long diff)
151 {
152     gen_pc_plus_diff(s, cpu_pc, diff);
153     s->pc_save = s->pc_curr + diff;
154 }
155 
156 /*
157  * Handle Top Byte Ignore (TBI) bits.
158  *
159  * If address tagging is enabled via the TCR TBI bits:
160  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
161  *    then the address is zero-extended, clearing bits [63:56]
162  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
163  *    and TBI1 controls addressses with bit 55 == 1.
164  *    If the appropriate TBI bit is set for the address then
165  *    the address is sign-extended from bit 55 into bits [63:56]
166  *
167  * Here We have concatenated TBI{1,0} into tbi.
168  */
169 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
170                                 TCGv_i64 src, int tbi)
171 {
172     if (tbi == 0) {
173         /* Load unmodified address */
174         tcg_gen_mov_i64(dst, src);
175     } else if (!regime_has_2_ranges(s->mmu_idx)) {
176         /* Force tag byte to all zero */
177         tcg_gen_extract_i64(dst, src, 0, 56);
178     } else {
179         /* Sign-extend from bit 55.  */
180         tcg_gen_sextract_i64(dst, src, 0, 56);
181 
182         switch (tbi) {
183         case 1:
184             /* tbi0 but !tbi1: only use the extension if positive */
185             tcg_gen_and_i64(dst, dst, src);
186             break;
187         case 2:
188             /* !tbi0 but tbi1: only use the extension if negative */
189             tcg_gen_or_i64(dst, dst, src);
190             break;
191         case 3:
192             /* tbi0 and tbi1: always use the extension */
193             break;
194         default:
195             g_assert_not_reached();
196         }
197     }
198 }
199 
200 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
201 {
202     /*
203      * If address tagging is enabled for instructions via the TCR TBI bits,
204      * then loading an address into the PC will clear out any tag.
205      */
206     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
207     s->pc_save = -1;
208 }
209 
210 /*
211  * Handle MTE and/or TBI.
212  *
213  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
214  * for the tag to be present in the FAR_ELx register.  But for user-only
215  * mode we do not have a TLB with which to implement this, so we must
216  * remove the top byte now.
217  *
218  * Always return a fresh temporary that we can increment independently
219  * of the write-back address.
220  */
221 
222 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
223 {
224     TCGv_i64 clean = tcg_temp_new_i64();
225 #ifdef CONFIG_USER_ONLY
226     gen_top_byte_ignore(s, clean, addr, s->tbid);
227 #else
228     tcg_gen_mov_i64(clean, addr);
229 #endif
230     return clean;
231 }
232 
233 /* Insert a zero tag into src, with the result at dst. */
234 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
235 {
236     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
237 }
238 
239 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
240                              MMUAccessType acc, int log2_size)
241 {
242     gen_helper_probe_access(cpu_env, ptr,
243                             tcg_constant_i32(acc),
244                             tcg_constant_i32(get_mem_index(s)),
245                             tcg_constant_i32(1 << log2_size));
246 }
247 
248 /*
249  * For MTE, check a single logical or atomic access.  This probes a single
250  * address, the exact one specified.  The size and alignment of the access
251  * is not relevant to MTE, per se, but watchpoints do require the size,
252  * and we want to recognize those before making any other changes to state.
253  */
254 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
255                                       bool is_write, bool tag_checked,
256                                       MemOp memop, bool is_unpriv,
257                                       int core_idx)
258 {
259     if (tag_checked && s->mte_active[is_unpriv]) {
260         TCGv_i64 ret;
261         int desc = 0;
262 
263         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
264         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
265         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
266         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
267         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
268         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
269 
270         ret = tcg_temp_new_i64();
271         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
272 
273         return ret;
274     }
275     return clean_data_tbi(s, addr);
276 }
277 
278 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
279                         bool tag_checked, MemOp memop)
280 {
281     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
282                                  false, get_mem_index(s));
283 }
284 
285 /*
286  * For MTE, check multiple logical sequential accesses.
287  */
288 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
289                         bool tag_checked, int total_size, MemOp single_mop)
290 {
291     if (tag_checked && s->mte_active[0]) {
292         TCGv_i64 ret;
293         int desc = 0;
294 
295         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
296         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
297         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
298         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
299         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
300         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
301 
302         ret = tcg_temp_new_i64();
303         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
304 
305         return ret;
306     }
307     return clean_data_tbi(s, addr);
308 }
309 
310 /*
311  * Generate the special alignment check that applies to AccType_ATOMIC
312  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
313  * naturally aligned, but it must not cross a 16-byte boundary.
314  * See AArch64.CheckAlignment().
315  */
316 static void check_lse2_align(DisasContext *s, int rn, int imm,
317                              bool is_write, MemOp mop)
318 {
319     TCGv_i32 tmp;
320     TCGv_i64 addr;
321     TCGLabel *over_label;
322     MMUAccessType type;
323     int mmu_idx;
324 
325     tmp = tcg_temp_new_i32();
326     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
327     tcg_gen_addi_i32(tmp, tmp, imm & 15);
328     tcg_gen_andi_i32(tmp, tmp, 15);
329     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
330 
331     over_label = gen_new_label();
332     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
333 
334     addr = tcg_temp_new_i64();
335     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
336 
337     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
338     mmu_idx = get_mem_index(s);
339     gen_helper_unaligned_access(cpu_env, addr, tcg_constant_i32(type),
340                                 tcg_constant_i32(mmu_idx));
341 
342     gen_set_label(over_label);
343 
344 }
345 
346 /* Handle the alignment check for AccType_ATOMIC instructions. */
347 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
348 {
349     MemOp size = mop & MO_SIZE;
350 
351     if (size == MO_8) {
352         return mop;
353     }
354 
355     /*
356      * If size == MO_128, this is a LDXP, and the operation is single-copy
357      * atomic for each doubleword, not the entire quadword; it still must
358      * be quadword aligned.
359      */
360     if (size == MO_128) {
361         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
362                                    MO_ATOM_IFALIGN_PAIR);
363     }
364     if (dc_isar_feature(aa64_lse2, s)) {
365         check_lse2_align(s, rn, 0, true, mop);
366     } else {
367         mop |= MO_ALIGN;
368     }
369     return finalize_memop(s, mop);
370 }
371 
372 /* Handle the alignment check for AccType_ORDERED instructions. */
373 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
374                                  bool is_write, MemOp mop)
375 {
376     MemOp size = mop & MO_SIZE;
377 
378     if (size == MO_8) {
379         return mop;
380     }
381     if (size == MO_128) {
382         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
383                                    MO_ATOM_IFALIGN_PAIR);
384     }
385     if (!dc_isar_feature(aa64_lse2, s)) {
386         mop |= MO_ALIGN;
387     } else if (!s->naa) {
388         check_lse2_align(s, rn, imm, is_write, mop);
389     }
390     return finalize_memop(s, mop);
391 }
392 
393 typedef struct DisasCompare64 {
394     TCGCond cond;
395     TCGv_i64 value;
396 } DisasCompare64;
397 
398 static void a64_test_cc(DisasCompare64 *c64, int cc)
399 {
400     DisasCompare c32;
401 
402     arm_test_cc(&c32, cc);
403 
404     /*
405      * Sign-extend the 32-bit value so that the GE/LT comparisons work
406      * properly.  The NE/EQ comparisons are also fine with this choice.
407       */
408     c64->cond = c32.cond;
409     c64->value = tcg_temp_new_i64();
410     tcg_gen_ext_i32_i64(c64->value, c32.value);
411 }
412 
413 static void gen_rebuild_hflags(DisasContext *s)
414 {
415     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
416 }
417 
418 static void gen_exception_internal(int excp)
419 {
420     assert(excp_is_internal(excp));
421     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
422 }
423 
424 static void gen_exception_internal_insn(DisasContext *s, int excp)
425 {
426     gen_a64_update_pc(s, 0);
427     gen_exception_internal(excp);
428     s->base.is_jmp = DISAS_NORETURN;
429 }
430 
431 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
432 {
433     gen_a64_update_pc(s, 0);
434     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
435     s->base.is_jmp = DISAS_NORETURN;
436 }
437 
438 static void gen_step_complete_exception(DisasContext *s)
439 {
440     /* We just completed step of an insn. Move from Active-not-pending
441      * to Active-pending, and then also take the swstep exception.
442      * This corresponds to making the (IMPDEF) choice to prioritize
443      * swstep exceptions over asynchronous exceptions taken to an exception
444      * level where debug is disabled. This choice has the advantage that
445      * we do not need to maintain internal state corresponding to the
446      * ISV/EX syndrome bits between completion of the step and generation
447      * of the exception, and our syndrome information is always correct.
448      */
449     gen_ss_advance(s);
450     gen_swstep_exception(s, 1, s->is_ldex);
451     s->base.is_jmp = DISAS_NORETURN;
452 }
453 
454 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
455 {
456     if (s->ss_active) {
457         return false;
458     }
459     return translator_use_goto_tb(&s->base, dest);
460 }
461 
462 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
463 {
464     if (use_goto_tb(s, s->pc_curr + diff)) {
465         /*
466          * For pcrel, the pc must always be up-to-date on entry to
467          * the linked TB, so that it can use simple additions for all
468          * further adjustments.  For !pcrel, the linked TB is compiled
469          * to know its full virtual address, so we can delay the
470          * update to pc to the unlinked path.  A long chain of links
471          * can thus avoid many updates to the PC.
472          */
473         if (tb_cflags(s->base.tb) & CF_PCREL) {
474             gen_a64_update_pc(s, diff);
475             tcg_gen_goto_tb(n);
476         } else {
477             tcg_gen_goto_tb(n);
478             gen_a64_update_pc(s, diff);
479         }
480         tcg_gen_exit_tb(s->base.tb, n);
481         s->base.is_jmp = DISAS_NORETURN;
482     } else {
483         gen_a64_update_pc(s, diff);
484         if (s->ss_active) {
485             gen_step_complete_exception(s);
486         } else {
487             tcg_gen_lookup_and_goto_ptr();
488             s->base.is_jmp = DISAS_NORETURN;
489         }
490     }
491 }
492 
493 /*
494  * Register access functions
495  *
496  * These functions are used for directly accessing a register in where
497  * changes to the final register value are likely to be made. If you
498  * need to use a register for temporary calculation (e.g. index type
499  * operations) use the read_* form.
500  *
501  * B1.2.1 Register mappings
502  *
503  * In instruction register encoding 31 can refer to ZR (zero register) or
504  * the SP (stack pointer) depending on context. In QEMU's case we map SP
505  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
506  * This is the point of the _sp forms.
507  */
508 TCGv_i64 cpu_reg(DisasContext *s, int reg)
509 {
510     if (reg == 31) {
511         TCGv_i64 t = tcg_temp_new_i64();
512         tcg_gen_movi_i64(t, 0);
513         return t;
514     } else {
515         return cpu_X[reg];
516     }
517 }
518 
519 /* register access for when 31 == SP */
520 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
521 {
522     return cpu_X[reg];
523 }
524 
525 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
526  * representing the register contents. This TCGv is an auto-freed
527  * temporary so it need not be explicitly freed, and may be modified.
528  */
529 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
530 {
531     TCGv_i64 v = tcg_temp_new_i64();
532     if (reg != 31) {
533         if (sf) {
534             tcg_gen_mov_i64(v, cpu_X[reg]);
535         } else {
536             tcg_gen_ext32u_i64(v, cpu_X[reg]);
537         }
538     } else {
539         tcg_gen_movi_i64(v, 0);
540     }
541     return v;
542 }
543 
544 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
545 {
546     TCGv_i64 v = tcg_temp_new_i64();
547     if (sf) {
548         tcg_gen_mov_i64(v, cpu_X[reg]);
549     } else {
550         tcg_gen_ext32u_i64(v, cpu_X[reg]);
551     }
552     return v;
553 }
554 
555 /* Return the offset into CPUARMState of a slice (from
556  * the least significant end) of FP register Qn (ie
557  * Dn, Sn, Hn or Bn).
558  * (Note that this is not the same mapping as for A32; see cpu.h)
559  */
560 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
561 {
562     return vec_reg_offset(s, regno, 0, size);
563 }
564 
565 /* Offset of the high half of the 128 bit vector Qn */
566 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
567 {
568     return vec_reg_offset(s, regno, 1, MO_64);
569 }
570 
571 /* Convenience accessors for reading and writing single and double
572  * FP registers. Writing clears the upper parts of the associated
573  * 128 bit vector register, as required by the architecture.
574  * Note that unlike the GP register accessors, the values returned
575  * by the read functions must be manually freed.
576  */
577 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
578 {
579     TCGv_i64 v = tcg_temp_new_i64();
580 
581     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
582     return v;
583 }
584 
585 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
586 {
587     TCGv_i32 v = tcg_temp_new_i32();
588 
589     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
590     return v;
591 }
592 
593 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
594 {
595     TCGv_i32 v = tcg_temp_new_i32();
596 
597     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
598     return v;
599 }
600 
601 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
602  * If SVE is not enabled, then there are only 128 bits in the vector.
603  */
604 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
605 {
606     unsigned ofs = fp_reg_offset(s, rd, MO_64);
607     unsigned vsz = vec_full_reg_size(s);
608 
609     /* Nop move, with side effect of clearing the tail. */
610     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
611 }
612 
613 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
614 {
615     unsigned ofs = fp_reg_offset(s, reg, MO_64);
616 
617     tcg_gen_st_i64(v, cpu_env, ofs);
618     clear_vec_high(s, false, reg);
619 }
620 
621 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
622 {
623     TCGv_i64 tmp = tcg_temp_new_i64();
624 
625     tcg_gen_extu_i32_i64(tmp, v);
626     write_fp_dreg(s, reg, tmp);
627 }
628 
629 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
630 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
631                          GVecGen2Fn *gvec_fn, int vece)
632 {
633     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
634             is_q ? 16 : 8, vec_full_reg_size(s));
635 }
636 
637 /* Expand a 2-operand + immediate AdvSIMD vector operation using
638  * an expander function.
639  */
640 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
641                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
642 {
643     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
644             imm, is_q ? 16 : 8, vec_full_reg_size(s));
645 }
646 
647 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
648 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
649                          GVecGen3Fn *gvec_fn, int vece)
650 {
651     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
652             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
653 }
654 
655 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
656 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
657                          int rx, GVecGen4Fn *gvec_fn, int vece)
658 {
659     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
660             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
661             is_q ? 16 : 8, vec_full_reg_size(s));
662 }
663 
664 /* Expand a 2-operand operation using an out-of-line helper.  */
665 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
666                              int rn, int data, gen_helper_gvec_2 *fn)
667 {
668     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
669                        vec_full_reg_offset(s, rn),
670                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
671 }
672 
673 /* Expand a 3-operand operation using an out-of-line helper.  */
674 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
675                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
676 {
677     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
678                        vec_full_reg_offset(s, rn),
679                        vec_full_reg_offset(s, rm),
680                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
681 }
682 
683 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
684  * an out-of-line helper.
685  */
686 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
687                               int rm, bool is_fp16, int data,
688                               gen_helper_gvec_3_ptr *fn)
689 {
690     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
691     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
692                        vec_full_reg_offset(s, rn),
693                        vec_full_reg_offset(s, rm), fpst,
694                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
695 }
696 
697 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
698 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
699                             int rm, gen_helper_gvec_3_ptr *fn)
700 {
701     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
702 
703     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
704     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
705                        vec_full_reg_offset(s, rn),
706                        vec_full_reg_offset(s, rm), qc_ptr,
707                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
708 }
709 
710 /* Expand a 4-operand operation using an out-of-line helper.  */
711 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
712                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
713 {
714     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
715                        vec_full_reg_offset(s, rn),
716                        vec_full_reg_offset(s, rm),
717                        vec_full_reg_offset(s, ra),
718                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
719 }
720 
721 /*
722  * Expand a 4-operand + fpstatus pointer + simd data value operation using
723  * an out-of-line helper.
724  */
725 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
726                               int rm, int ra, bool is_fp16, int data,
727                               gen_helper_gvec_4_ptr *fn)
728 {
729     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
730     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
731                        vec_full_reg_offset(s, rn),
732                        vec_full_reg_offset(s, rm),
733                        vec_full_reg_offset(s, ra), fpst,
734                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
735 }
736 
737 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
738  * than the 32 bit equivalent.
739  */
740 static inline void gen_set_NZ64(TCGv_i64 result)
741 {
742     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
743     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
744 }
745 
746 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
747 static inline void gen_logic_CC(int sf, TCGv_i64 result)
748 {
749     if (sf) {
750         gen_set_NZ64(result);
751     } else {
752         tcg_gen_extrl_i64_i32(cpu_ZF, result);
753         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
754     }
755     tcg_gen_movi_i32(cpu_CF, 0);
756     tcg_gen_movi_i32(cpu_VF, 0);
757 }
758 
759 /* dest = T0 + T1; compute C, N, V and Z flags */
760 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
761 {
762     TCGv_i64 result, flag, tmp;
763     result = tcg_temp_new_i64();
764     flag = tcg_temp_new_i64();
765     tmp = tcg_temp_new_i64();
766 
767     tcg_gen_movi_i64(tmp, 0);
768     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
769 
770     tcg_gen_extrl_i64_i32(cpu_CF, flag);
771 
772     gen_set_NZ64(result);
773 
774     tcg_gen_xor_i64(flag, result, t0);
775     tcg_gen_xor_i64(tmp, t0, t1);
776     tcg_gen_andc_i64(flag, flag, tmp);
777     tcg_gen_extrh_i64_i32(cpu_VF, flag);
778 
779     tcg_gen_mov_i64(dest, result);
780 }
781 
782 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
783 {
784     TCGv_i32 t0_32 = tcg_temp_new_i32();
785     TCGv_i32 t1_32 = tcg_temp_new_i32();
786     TCGv_i32 tmp = tcg_temp_new_i32();
787 
788     tcg_gen_movi_i32(tmp, 0);
789     tcg_gen_extrl_i64_i32(t0_32, t0);
790     tcg_gen_extrl_i64_i32(t1_32, t1);
791     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
792     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
793     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
794     tcg_gen_xor_i32(tmp, t0_32, t1_32);
795     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
796     tcg_gen_extu_i32_i64(dest, cpu_NF);
797 }
798 
799 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
800 {
801     if (sf) {
802         gen_add64_CC(dest, t0, t1);
803     } else {
804         gen_add32_CC(dest, t0, t1);
805     }
806 }
807 
808 /* dest = T0 - T1; compute C, N, V and Z flags */
809 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
810 {
811     /* 64 bit arithmetic */
812     TCGv_i64 result, flag, tmp;
813 
814     result = tcg_temp_new_i64();
815     flag = tcg_temp_new_i64();
816     tcg_gen_sub_i64(result, t0, t1);
817 
818     gen_set_NZ64(result);
819 
820     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
821     tcg_gen_extrl_i64_i32(cpu_CF, flag);
822 
823     tcg_gen_xor_i64(flag, result, t0);
824     tmp = tcg_temp_new_i64();
825     tcg_gen_xor_i64(tmp, t0, t1);
826     tcg_gen_and_i64(flag, flag, tmp);
827     tcg_gen_extrh_i64_i32(cpu_VF, flag);
828     tcg_gen_mov_i64(dest, result);
829 }
830 
831 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
832 {
833     /* 32 bit arithmetic */
834     TCGv_i32 t0_32 = tcg_temp_new_i32();
835     TCGv_i32 t1_32 = tcg_temp_new_i32();
836     TCGv_i32 tmp;
837 
838     tcg_gen_extrl_i64_i32(t0_32, t0);
839     tcg_gen_extrl_i64_i32(t1_32, t1);
840     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
841     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
842     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
843     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
844     tmp = tcg_temp_new_i32();
845     tcg_gen_xor_i32(tmp, t0_32, t1_32);
846     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
847     tcg_gen_extu_i32_i64(dest, cpu_NF);
848 }
849 
850 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
851 {
852     if (sf) {
853         gen_sub64_CC(dest, t0, t1);
854     } else {
855         gen_sub32_CC(dest, t0, t1);
856     }
857 }
858 
859 /* dest = T0 + T1 + CF; do not compute flags. */
860 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
861 {
862     TCGv_i64 flag = tcg_temp_new_i64();
863     tcg_gen_extu_i32_i64(flag, cpu_CF);
864     tcg_gen_add_i64(dest, t0, t1);
865     tcg_gen_add_i64(dest, dest, flag);
866 
867     if (!sf) {
868         tcg_gen_ext32u_i64(dest, dest);
869     }
870 }
871 
872 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
873 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
874 {
875     if (sf) {
876         TCGv_i64 result = tcg_temp_new_i64();
877         TCGv_i64 cf_64 = tcg_temp_new_i64();
878         TCGv_i64 vf_64 = tcg_temp_new_i64();
879         TCGv_i64 tmp = tcg_temp_new_i64();
880         TCGv_i64 zero = tcg_constant_i64(0);
881 
882         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
883         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
884         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
885         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
886         gen_set_NZ64(result);
887 
888         tcg_gen_xor_i64(vf_64, result, t0);
889         tcg_gen_xor_i64(tmp, t0, t1);
890         tcg_gen_andc_i64(vf_64, vf_64, tmp);
891         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
892 
893         tcg_gen_mov_i64(dest, result);
894     } else {
895         TCGv_i32 t0_32 = tcg_temp_new_i32();
896         TCGv_i32 t1_32 = tcg_temp_new_i32();
897         TCGv_i32 tmp = tcg_temp_new_i32();
898         TCGv_i32 zero = tcg_constant_i32(0);
899 
900         tcg_gen_extrl_i64_i32(t0_32, t0);
901         tcg_gen_extrl_i64_i32(t1_32, t1);
902         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
903         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
904 
905         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
906         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
907         tcg_gen_xor_i32(tmp, t0_32, t1_32);
908         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
909         tcg_gen_extu_i32_i64(dest, cpu_NF);
910     }
911 }
912 
913 /*
914  * Load/Store generators
915  */
916 
917 /*
918  * Store from GPR register to memory.
919  */
920 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
921                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
922                              bool iss_valid,
923                              unsigned int iss_srt,
924                              bool iss_sf, bool iss_ar)
925 {
926     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
927 
928     if (iss_valid) {
929         uint32_t syn;
930 
931         syn = syn_data_abort_with_iss(0,
932                                       (memop & MO_SIZE),
933                                       false,
934                                       iss_srt,
935                                       iss_sf,
936                                       iss_ar,
937                                       0, 0, 0, 0, 0, false);
938         disas_set_insn_syndrome(s, syn);
939     }
940 }
941 
942 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
943                       TCGv_i64 tcg_addr, MemOp memop,
944                       bool iss_valid,
945                       unsigned int iss_srt,
946                       bool iss_sf, bool iss_ar)
947 {
948     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
949                      iss_valid, iss_srt, iss_sf, iss_ar);
950 }
951 
952 /*
953  * Load from memory to GPR register
954  */
955 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
956                              MemOp memop, bool extend, int memidx,
957                              bool iss_valid, unsigned int iss_srt,
958                              bool iss_sf, bool iss_ar)
959 {
960     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
961 
962     if (extend && (memop & MO_SIGN)) {
963         g_assert((memop & MO_SIZE) <= MO_32);
964         tcg_gen_ext32u_i64(dest, dest);
965     }
966 
967     if (iss_valid) {
968         uint32_t syn;
969 
970         syn = syn_data_abort_with_iss(0,
971                                       (memop & MO_SIZE),
972                                       (memop & MO_SIGN) != 0,
973                                       iss_srt,
974                                       iss_sf,
975                                       iss_ar,
976                                       0, 0, 0, 0, 0, false);
977         disas_set_insn_syndrome(s, syn);
978     }
979 }
980 
981 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
982                       MemOp memop, bool extend,
983                       bool iss_valid, unsigned int iss_srt,
984                       bool iss_sf, bool iss_ar)
985 {
986     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
987                      iss_valid, iss_srt, iss_sf, iss_ar);
988 }
989 
990 /*
991  * Store from FP register to memory
992  */
993 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
994 {
995     /* This writes the bottom N bits of a 128 bit wide vector to memory */
996     TCGv_i64 tmplo = tcg_temp_new_i64();
997 
998     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
999 
1000     if ((mop & MO_SIZE) < MO_128) {
1001         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1002     } else {
1003         TCGv_i64 tmphi = tcg_temp_new_i64();
1004         TCGv_i128 t16 = tcg_temp_new_i128();
1005 
1006         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
1007         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1008 
1009         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1010     }
1011 }
1012 
1013 /*
1014  * Load from memory to FP register
1015  */
1016 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1017 {
1018     /* This always zero-extends and writes to a full 128 bit wide vector */
1019     TCGv_i64 tmplo = tcg_temp_new_i64();
1020     TCGv_i64 tmphi = NULL;
1021 
1022     if ((mop & MO_SIZE) < MO_128) {
1023         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1024     } else {
1025         TCGv_i128 t16 = tcg_temp_new_i128();
1026 
1027         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1028 
1029         tmphi = tcg_temp_new_i64();
1030         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1031     }
1032 
1033     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1034 
1035     if (tmphi) {
1036         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1037     }
1038     clear_vec_high(s, tmphi != NULL, destidx);
1039 }
1040 
1041 /*
1042  * Vector load/store helpers.
1043  *
1044  * The principal difference between this and a FP load is that we don't
1045  * zero extend as we are filling a partial chunk of the vector register.
1046  * These functions don't support 128 bit loads/stores, which would be
1047  * normal load/store operations.
1048  *
1049  * The _i32 versions are useful when operating on 32 bit quantities
1050  * (eg for floating point single or using Neon helper functions).
1051  */
1052 
1053 /* Get value of an element within a vector register */
1054 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1055                              int element, MemOp memop)
1056 {
1057     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1058     switch ((unsigned)memop) {
1059     case MO_8:
1060         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1061         break;
1062     case MO_16:
1063         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1064         break;
1065     case MO_32:
1066         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1067         break;
1068     case MO_8|MO_SIGN:
1069         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1070         break;
1071     case MO_16|MO_SIGN:
1072         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1073         break;
1074     case MO_32|MO_SIGN:
1075         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1076         break;
1077     case MO_64:
1078     case MO_64|MO_SIGN:
1079         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1080         break;
1081     default:
1082         g_assert_not_reached();
1083     }
1084 }
1085 
1086 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1087                                  int element, MemOp memop)
1088 {
1089     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1090     switch (memop) {
1091     case MO_8:
1092         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1093         break;
1094     case MO_16:
1095         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1096         break;
1097     case MO_8|MO_SIGN:
1098         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1099         break;
1100     case MO_16|MO_SIGN:
1101         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1102         break;
1103     case MO_32:
1104     case MO_32|MO_SIGN:
1105         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1106         break;
1107     default:
1108         g_assert_not_reached();
1109     }
1110 }
1111 
1112 /* Set value of an element within a vector register */
1113 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1114                               int element, MemOp memop)
1115 {
1116     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1117     switch (memop) {
1118     case MO_8:
1119         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1120         break;
1121     case MO_16:
1122         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1123         break;
1124     case MO_32:
1125         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1126         break;
1127     case MO_64:
1128         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1129         break;
1130     default:
1131         g_assert_not_reached();
1132     }
1133 }
1134 
1135 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1136                                   int destidx, 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_i32(tcg_src, cpu_env, vect_off);
1142         break;
1143     case MO_16:
1144         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1145         break;
1146     case MO_32:
1147         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1148         break;
1149     default:
1150         g_assert_not_reached();
1151     }
1152 }
1153 
1154 /* Store from vector register to memory */
1155 static void do_vec_st(DisasContext *s, int srcidx, int element,
1156                       TCGv_i64 tcg_addr, MemOp mop)
1157 {
1158     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1159 
1160     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1161     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1162 }
1163 
1164 /* Load from memory to vector register */
1165 static void do_vec_ld(DisasContext *s, int destidx, int element,
1166                       TCGv_i64 tcg_addr, MemOp mop)
1167 {
1168     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1169 
1170     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1171     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1172 }
1173 
1174 /* Check that FP/Neon access is enabled. If it is, return
1175  * true. If not, emit code to generate an appropriate exception,
1176  * and return false; the caller should not emit any code for
1177  * the instruction. Note that this check must happen after all
1178  * unallocated-encoding checks (otherwise the syndrome information
1179  * for the resulting exception will be incorrect).
1180  */
1181 static bool fp_access_check_only(DisasContext *s)
1182 {
1183     if (s->fp_excp_el) {
1184         assert(!s->fp_access_checked);
1185         s->fp_access_checked = true;
1186 
1187         gen_exception_insn_el(s, 0, EXCP_UDEF,
1188                               syn_fp_access_trap(1, 0xe, false, 0),
1189                               s->fp_excp_el);
1190         return false;
1191     }
1192     s->fp_access_checked = true;
1193     return true;
1194 }
1195 
1196 static bool fp_access_check(DisasContext *s)
1197 {
1198     if (!fp_access_check_only(s)) {
1199         return false;
1200     }
1201     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1202         gen_exception_insn(s, 0, EXCP_UDEF,
1203                            syn_smetrap(SME_ET_Streaming, false));
1204         return false;
1205     }
1206     return true;
1207 }
1208 
1209 /*
1210  * Check that SVE access is enabled.  If it is, return true.
1211  * If not, emit code to generate an appropriate exception and return false.
1212  * This function corresponds to CheckSVEEnabled().
1213  */
1214 bool sve_access_check(DisasContext *s)
1215 {
1216     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1217         assert(dc_isar_feature(aa64_sme, s));
1218         if (!sme_sm_enabled_check(s)) {
1219             goto fail_exit;
1220         }
1221     } else if (s->sve_excp_el) {
1222         gen_exception_insn_el(s, 0, EXCP_UDEF,
1223                               syn_sve_access_trap(), s->sve_excp_el);
1224         goto fail_exit;
1225     }
1226     s->sve_access_checked = true;
1227     return fp_access_check(s);
1228 
1229  fail_exit:
1230     /* Assert that we only raise one exception per instruction. */
1231     assert(!s->sve_access_checked);
1232     s->sve_access_checked = true;
1233     return false;
1234 }
1235 
1236 /*
1237  * Check that SME access is enabled, raise an exception if not.
1238  * Note that this function corresponds to CheckSMEAccess and is
1239  * only used directly for cpregs.
1240  */
1241 static bool sme_access_check(DisasContext *s)
1242 {
1243     if (s->sme_excp_el) {
1244         gen_exception_insn_el(s, 0, EXCP_UDEF,
1245                               syn_smetrap(SME_ET_AccessTrap, false),
1246                               s->sme_excp_el);
1247         return false;
1248     }
1249     return true;
1250 }
1251 
1252 /* This function corresponds to CheckSMEEnabled. */
1253 bool sme_enabled_check(DisasContext *s)
1254 {
1255     /*
1256      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1257      * to be zero when fp_excp_el has priority.  This is because we need
1258      * sme_excp_el by itself for cpregs access checks.
1259      */
1260     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1261         s->fp_access_checked = true;
1262         return sme_access_check(s);
1263     }
1264     return fp_access_check_only(s);
1265 }
1266 
1267 /* Common subroutine for CheckSMEAnd*Enabled. */
1268 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1269 {
1270     if (!sme_enabled_check(s)) {
1271         return false;
1272     }
1273     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1274         gen_exception_insn(s, 0, EXCP_UDEF,
1275                            syn_smetrap(SME_ET_NotStreaming, false));
1276         return false;
1277     }
1278     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1279         gen_exception_insn(s, 0, EXCP_UDEF,
1280                            syn_smetrap(SME_ET_InactiveZA, false));
1281         return false;
1282     }
1283     return true;
1284 }
1285 
1286 /*
1287  * This utility function is for doing register extension with an
1288  * optional shift. You will likely want to pass a temporary for the
1289  * destination register. See DecodeRegExtend() in the ARM ARM.
1290  */
1291 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1292                               int option, unsigned int shift)
1293 {
1294     int extsize = extract32(option, 0, 2);
1295     bool is_signed = extract32(option, 2, 1);
1296 
1297     if (is_signed) {
1298         switch (extsize) {
1299         case 0:
1300             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1301             break;
1302         case 1:
1303             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1304             break;
1305         case 2:
1306             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1307             break;
1308         case 3:
1309             tcg_gen_mov_i64(tcg_out, tcg_in);
1310             break;
1311         }
1312     } else {
1313         switch (extsize) {
1314         case 0:
1315             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1316             break;
1317         case 1:
1318             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1319             break;
1320         case 2:
1321             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1322             break;
1323         case 3:
1324             tcg_gen_mov_i64(tcg_out, tcg_in);
1325             break;
1326         }
1327     }
1328 
1329     if (shift) {
1330         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1331     }
1332 }
1333 
1334 static inline void gen_check_sp_alignment(DisasContext *s)
1335 {
1336     /* The AArch64 architecture mandates that (if enabled via PSTATE
1337      * or SCTLR bits) there is a check that SP is 16-aligned on every
1338      * SP-relative load or store (with an exception generated if it is not).
1339      * In line with general QEMU practice regarding misaligned accesses,
1340      * we omit these checks for the sake of guest program performance.
1341      * This function is provided as a hook so we can more easily add these
1342      * checks in future (possibly as a "favour catching guest program bugs
1343      * over speed" user selectable option).
1344      */
1345 }
1346 
1347 /*
1348  * This provides a simple table based table lookup decoder. It is
1349  * intended to be used when the relevant bits for decode are too
1350  * awkwardly placed and switch/if based logic would be confusing and
1351  * deeply nested. Since it's a linear search through the table, tables
1352  * should be kept small.
1353  *
1354  * It returns the first handler where insn & mask == pattern, or
1355  * NULL if there is no match.
1356  * The table is terminated by an empty mask (i.e. 0)
1357  */
1358 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1359                                                uint32_t insn)
1360 {
1361     const AArch64DecodeTable *tptr = table;
1362 
1363     while (tptr->mask) {
1364         if ((insn & tptr->mask) == tptr->pattern) {
1365             return tptr->disas_fn;
1366         }
1367         tptr++;
1368     }
1369     return NULL;
1370 }
1371 
1372 /*
1373  * The instruction disassembly implemented here matches
1374  * the instruction encoding classifications in chapter C4
1375  * of the ARM Architecture Reference Manual (DDI0487B_a);
1376  * classification names and decode diagrams here should generally
1377  * match up with those in the manual.
1378  */
1379 
1380 static bool trans_B(DisasContext *s, arg_i *a)
1381 {
1382     reset_btype(s);
1383     gen_goto_tb(s, 0, a->imm);
1384     return true;
1385 }
1386 
1387 static bool trans_BL(DisasContext *s, arg_i *a)
1388 {
1389     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1390     reset_btype(s);
1391     gen_goto_tb(s, 0, a->imm);
1392     return true;
1393 }
1394 
1395 
1396 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1397 {
1398     DisasLabel match;
1399     TCGv_i64 tcg_cmp;
1400 
1401     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1402     reset_btype(s);
1403 
1404     match = gen_disas_label(s);
1405     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1406                         tcg_cmp, 0, match.label);
1407     gen_goto_tb(s, 0, 4);
1408     set_disas_label(s, match);
1409     gen_goto_tb(s, 1, a->imm);
1410     return true;
1411 }
1412 
1413 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1414 {
1415     DisasLabel match;
1416     TCGv_i64 tcg_cmp;
1417 
1418     tcg_cmp = tcg_temp_new_i64();
1419     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1420 
1421     reset_btype(s);
1422 
1423     match = gen_disas_label(s);
1424     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1425                         tcg_cmp, 0, match.label);
1426     gen_goto_tb(s, 0, 4);
1427     set_disas_label(s, match);
1428     gen_goto_tb(s, 1, a->imm);
1429     return true;
1430 }
1431 
1432 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1433 {
1434     reset_btype(s);
1435     if (a->cond < 0x0e) {
1436         /* genuinely conditional branches */
1437         DisasLabel match = gen_disas_label(s);
1438         arm_gen_test_cc(a->cond, match.label);
1439         gen_goto_tb(s, 0, 4);
1440         set_disas_label(s, match);
1441         gen_goto_tb(s, 1, a->imm);
1442     } else {
1443         /* 0xe and 0xf are both "always" conditions */
1444         gen_goto_tb(s, 0, a->imm);
1445     }
1446     return true;
1447 }
1448 
1449 static void set_btype_for_br(DisasContext *s, int rn)
1450 {
1451     if (dc_isar_feature(aa64_bti, s)) {
1452         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1453         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1454     }
1455 }
1456 
1457 static void set_btype_for_blr(DisasContext *s)
1458 {
1459     if (dc_isar_feature(aa64_bti, s)) {
1460         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1461         set_btype(s, 2);
1462     }
1463 }
1464 
1465 static bool trans_BR(DisasContext *s, arg_r *a)
1466 {
1467     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1468     set_btype_for_br(s, a->rn);
1469     s->base.is_jmp = DISAS_JUMP;
1470     return true;
1471 }
1472 
1473 static bool trans_BLR(DisasContext *s, arg_r *a)
1474 {
1475     TCGv_i64 dst = cpu_reg(s, a->rn);
1476     TCGv_i64 lr = cpu_reg(s, 30);
1477     if (dst == lr) {
1478         TCGv_i64 tmp = tcg_temp_new_i64();
1479         tcg_gen_mov_i64(tmp, dst);
1480         dst = tmp;
1481     }
1482     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1483     gen_a64_set_pc(s, dst);
1484     set_btype_for_blr(s);
1485     s->base.is_jmp = DISAS_JUMP;
1486     return true;
1487 }
1488 
1489 static bool trans_RET(DisasContext *s, arg_r *a)
1490 {
1491     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1492     s->base.is_jmp = DISAS_JUMP;
1493     return true;
1494 }
1495 
1496 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1497                                    TCGv_i64 modifier, bool use_key_a)
1498 {
1499     TCGv_i64 truedst;
1500     /*
1501      * Return the branch target for a BRAA/RETA/etc, which is either
1502      * just the destination dst, or that value with the pauth check
1503      * done and the code removed from the high bits.
1504      */
1505     if (!s->pauth_active) {
1506         return dst;
1507     }
1508 
1509     truedst = tcg_temp_new_i64();
1510     if (use_key_a) {
1511         gen_helper_autia(truedst, cpu_env, dst, modifier);
1512     } else {
1513         gen_helper_autib(truedst, cpu_env, dst, modifier);
1514     }
1515     return truedst;
1516 }
1517 
1518 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1519 {
1520     TCGv_i64 dst;
1521 
1522     if (!dc_isar_feature(aa64_pauth, s)) {
1523         return false;
1524     }
1525 
1526     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1527     gen_a64_set_pc(s, dst);
1528     set_btype_for_br(s, a->rn);
1529     s->base.is_jmp = DISAS_JUMP;
1530     return true;
1531 }
1532 
1533 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1534 {
1535     TCGv_i64 dst, lr;
1536 
1537     if (!dc_isar_feature(aa64_pauth, s)) {
1538         return false;
1539     }
1540 
1541     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1542     lr = cpu_reg(s, 30);
1543     if (dst == lr) {
1544         TCGv_i64 tmp = tcg_temp_new_i64();
1545         tcg_gen_mov_i64(tmp, dst);
1546         dst = tmp;
1547     }
1548     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1549     gen_a64_set_pc(s, dst);
1550     set_btype_for_blr(s);
1551     s->base.is_jmp = DISAS_JUMP;
1552     return true;
1553 }
1554 
1555 static bool trans_RETA(DisasContext *s, arg_reta *a)
1556 {
1557     TCGv_i64 dst;
1558 
1559     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1560     gen_a64_set_pc(s, dst);
1561     s->base.is_jmp = DISAS_JUMP;
1562     return true;
1563 }
1564 
1565 static bool trans_BRA(DisasContext *s, arg_bra *a)
1566 {
1567     TCGv_i64 dst;
1568 
1569     if (!dc_isar_feature(aa64_pauth, s)) {
1570         return false;
1571     }
1572     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1573     gen_a64_set_pc(s, dst);
1574     set_btype_for_br(s, a->rn);
1575     s->base.is_jmp = DISAS_JUMP;
1576     return true;
1577 }
1578 
1579 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1580 {
1581     TCGv_i64 dst, lr;
1582 
1583     if (!dc_isar_feature(aa64_pauth, s)) {
1584         return false;
1585     }
1586     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1587     lr = cpu_reg(s, 30);
1588     if (dst == lr) {
1589         TCGv_i64 tmp = tcg_temp_new_i64();
1590         tcg_gen_mov_i64(tmp, dst);
1591         dst = tmp;
1592     }
1593     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1594     gen_a64_set_pc(s, dst);
1595     set_btype_for_blr(s);
1596     s->base.is_jmp = DISAS_JUMP;
1597     return true;
1598 }
1599 
1600 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1601 {
1602     TCGv_i64 dst;
1603 
1604     if (s->current_el == 0) {
1605         return false;
1606     }
1607     if (s->fgt_eret) {
1608         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1609         return true;
1610     }
1611     dst = tcg_temp_new_i64();
1612     tcg_gen_ld_i64(dst, cpu_env,
1613                    offsetof(CPUARMState, elr_el[s->current_el]));
1614 
1615     translator_io_start(&s->base);
1616 
1617     gen_helper_exception_return(cpu_env, dst);
1618     /* Must exit loop to check un-masked IRQs */
1619     s->base.is_jmp = DISAS_EXIT;
1620     return true;
1621 }
1622 
1623 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1624 {
1625     TCGv_i64 dst;
1626 
1627     if (!dc_isar_feature(aa64_pauth, s)) {
1628         return false;
1629     }
1630     if (s->current_el == 0) {
1631         return false;
1632     }
1633     /* The FGT trap takes precedence over an auth trap. */
1634     if (s->fgt_eret) {
1635         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1636         return true;
1637     }
1638     dst = tcg_temp_new_i64();
1639     tcg_gen_ld_i64(dst, cpu_env,
1640                    offsetof(CPUARMState, elr_el[s->current_el]));
1641 
1642     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1643 
1644     translator_io_start(&s->base);
1645 
1646     gen_helper_exception_return(cpu_env, dst);
1647     /* Must exit loop to check un-masked IRQs */
1648     s->base.is_jmp = DISAS_EXIT;
1649     return true;
1650 }
1651 
1652 /* HINT instruction group, including various allocated HINTs */
1653 static void handle_hint(DisasContext *s, uint32_t insn,
1654                         unsigned int op1, unsigned int op2, unsigned int crm)
1655 {
1656     unsigned int selector = crm << 3 | op2;
1657 
1658     if (op1 != 3) {
1659         unallocated_encoding(s);
1660         return;
1661     }
1662 
1663     switch (selector) {
1664     case 0b00000: /* NOP */
1665         break;
1666     case 0b00011: /* WFI */
1667         s->base.is_jmp = DISAS_WFI;
1668         break;
1669     case 0b00001: /* YIELD */
1670         /* When running in MTTCG we don't generate jumps to the yield and
1671          * WFE helpers as it won't affect the scheduling of other vCPUs.
1672          * If we wanted to more completely model WFE/SEV so we don't busy
1673          * spin unnecessarily we would need to do something more involved.
1674          */
1675         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1676             s->base.is_jmp = DISAS_YIELD;
1677         }
1678         break;
1679     case 0b00010: /* WFE */
1680         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1681             s->base.is_jmp = DISAS_WFE;
1682         }
1683         break;
1684     case 0b00100: /* SEV */
1685     case 0b00101: /* SEVL */
1686     case 0b00110: /* DGH */
1687         /* we treat all as NOP at least for now */
1688         break;
1689     case 0b00111: /* XPACLRI */
1690         if (s->pauth_active) {
1691             gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1692         }
1693         break;
1694     case 0b01000: /* PACIA1716 */
1695         if (s->pauth_active) {
1696             gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1697         }
1698         break;
1699     case 0b01010: /* PACIB1716 */
1700         if (s->pauth_active) {
1701             gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1702         }
1703         break;
1704     case 0b01100: /* AUTIA1716 */
1705         if (s->pauth_active) {
1706             gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1707         }
1708         break;
1709     case 0b01110: /* AUTIB1716 */
1710         if (s->pauth_active) {
1711             gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1712         }
1713         break;
1714     case 0b10000: /* ESB */
1715         /* Without RAS, we must implement this as NOP. */
1716         if (dc_isar_feature(aa64_ras, s)) {
1717             /*
1718              * QEMU does not have a source of physical SErrors,
1719              * so we are only concerned with virtual SErrors.
1720              * The pseudocode in the ARM for this case is
1721              *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1722              *      AArch64.vESBOperation();
1723              * Most of the condition can be evaluated at translation time.
1724              * Test for EL2 present, and defer test for SEL2 to runtime.
1725              */
1726             if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1727                 gen_helper_vesb(cpu_env);
1728             }
1729         }
1730         break;
1731     case 0b11000: /* PACIAZ */
1732         if (s->pauth_active) {
1733             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1734                              tcg_constant_i64(0));
1735         }
1736         break;
1737     case 0b11001: /* PACIASP */
1738         if (s->pauth_active) {
1739             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1740         }
1741         break;
1742     case 0b11010: /* PACIBZ */
1743         if (s->pauth_active) {
1744             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1745                              tcg_constant_i64(0));
1746         }
1747         break;
1748     case 0b11011: /* PACIBSP */
1749         if (s->pauth_active) {
1750             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1751         }
1752         break;
1753     case 0b11100: /* AUTIAZ */
1754         if (s->pauth_active) {
1755             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1756                              tcg_constant_i64(0));
1757         }
1758         break;
1759     case 0b11101: /* AUTIASP */
1760         if (s->pauth_active) {
1761             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1762         }
1763         break;
1764     case 0b11110: /* AUTIBZ */
1765         if (s->pauth_active) {
1766             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1767                              tcg_constant_i64(0));
1768         }
1769         break;
1770     case 0b11111: /* AUTIBSP */
1771         if (s->pauth_active) {
1772             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1773         }
1774         break;
1775     default:
1776         /* default specified as NOP equivalent */
1777         break;
1778     }
1779 }
1780 
1781 static void gen_clrex(DisasContext *s, uint32_t insn)
1782 {
1783     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1784 }
1785 
1786 /* CLREX, DSB, DMB, ISB */
1787 static void handle_sync(DisasContext *s, uint32_t insn,
1788                         unsigned int op1, unsigned int op2, unsigned int crm)
1789 {
1790     TCGBar bar;
1791 
1792     if (op1 != 3) {
1793         unallocated_encoding(s);
1794         return;
1795     }
1796 
1797     switch (op2) {
1798     case 2: /* CLREX */
1799         gen_clrex(s, insn);
1800         return;
1801     case 4: /* DSB */
1802     case 5: /* DMB */
1803         switch (crm & 3) {
1804         case 1: /* MBReqTypes_Reads */
1805             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1806             break;
1807         case 2: /* MBReqTypes_Writes */
1808             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1809             break;
1810         default: /* MBReqTypes_All */
1811             bar = TCG_BAR_SC | TCG_MO_ALL;
1812             break;
1813         }
1814         tcg_gen_mb(bar);
1815         return;
1816     case 6: /* ISB */
1817         /* We need to break the TB after this insn to execute
1818          * a self-modified code correctly and also to take
1819          * any pending interrupts immediately.
1820          */
1821         reset_btype(s);
1822         gen_goto_tb(s, 0, 4);
1823         return;
1824 
1825     case 7: /* SB */
1826         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1827             goto do_unallocated;
1828         }
1829         /*
1830          * TODO: There is no speculation barrier opcode for TCG;
1831          * MB and end the TB instead.
1832          */
1833         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1834         gen_goto_tb(s, 0, 4);
1835         return;
1836 
1837     default:
1838     do_unallocated:
1839         unallocated_encoding(s);
1840         return;
1841     }
1842 }
1843 
1844 static void gen_xaflag(void)
1845 {
1846     TCGv_i32 z = tcg_temp_new_i32();
1847 
1848     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1849 
1850     /*
1851      * (!C & !Z) << 31
1852      * (!(C | Z)) << 31
1853      * ~((C | Z) << 31)
1854      * ~-(C | Z)
1855      * (C | Z) - 1
1856      */
1857     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1858     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1859 
1860     /* !(Z & C) */
1861     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1862     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1863 
1864     /* (!C & Z) << 31 -> -(Z & ~C) */
1865     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1866     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1867 
1868     /* C | Z */
1869     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1870 }
1871 
1872 static void gen_axflag(void)
1873 {
1874     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1875     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1876 
1877     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1878     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1879 
1880     tcg_gen_movi_i32(cpu_NF, 0);
1881     tcg_gen_movi_i32(cpu_VF, 0);
1882 }
1883 
1884 /* MSR (immediate) - move immediate to processor state field */
1885 static void handle_msr_i(DisasContext *s, uint32_t insn,
1886                          unsigned int op1, unsigned int op2, unsigned int crm)
1887 {
1888     int op = op1 << 3 | op2;
1889 
1890     /* End the TB by default, chaining is ok.  */
1891     s->base.is_jmp = DISAS_TOO_MANY;
1892 
1893     switch (op) {
1894     case 0x00: /* CFINV */
1895         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1896             goto do_unallocated;
1897         }
1898         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1899         s->base.is_jmp = DISAS_NEXT;
1900         break;
1901 
1902     case 0x01: /* XAFlag */
1903         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1904             goto do_unallocated;
1905         }
1906         gen_xaflag();
1907         s->base.is_jmp = DISAS_NEXT;
1908         break;
1909 
1910     case 0x02: /* AXFlag */
1911         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1912             goto do_unallocated;
1913         }
1914         gen_axflag();
1915         s->base.is_jmp = DISAS_NEXT;
1916         break;
1917 
1918     case 0x03: /* UAO */
1919         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1920             goto do_unallocated;
1921         }
1922         if (crm & 1) {
1923             set_pstate_bits(PSTATE_UAO);
1924         } else {
1925             clear_pstate_bits(PSTATE_UAO);
1926         }
1927         gen_rebuild_hflags(s);
1928         break;
1929 
1930     case 0x04: /* PAN */
1931         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1932             goto do_unallocated;
1933         }
1934         if (crm & 1) {
1935             set_pstate_bits(PSTATE_PAN);
1936         } else {
1937             clear_pstate_bits(PSTATE_PAN);
1938         }
1939         gen_rebuild_hflags(s);
1940         break;
1941 
1942     case 0x05: /* SPSel */
1943         if (s->current_el == 0) {
1944             goto do_unallocated;
1945         }
1946         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1947         break;
1948 
1949     case 0x19: /* SSBS */
1950         if (!dc_isar_feature(aa64_ssbs, s)) {
1951             goto do_unallocated;
1952         }
1953         if (crm & 1) {
1954             set_pstate_bits(PSTATE_SSBS);
1955         } else {
1956             clear_pstate_bits(PSTATE_SSBS);
1957         }
1958         /* Don't need to rebuild hflags since SSBS is a nop */
1959         break;
1960 
1961     case 0x1a: /* DIT */
1962         if (!dc_isar_feature(aa64_dit, s)) {
1963             goto do_unallocated;
1964         }
1965         if (crm & 1) {
1966             set_pstate_bits(PSTATE_DIT);
1967         } else {
1968             clear_pstate_bits(PSTATE_DIT);
1969         }
1970         /* There's no need to rebuild hflags because DIT is a nop */
1971         break;
1972 
1973     case 0x1e: /* DAIFSet */
1974         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1975         break;
1976 
1977     case 0x1f: /* DAIFClear */
1978         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1979         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1980         s->base.is_jmp = DISAS_UPDATE_EXIT;
1981         break;
1982 
1983     case 0x1c: /* TCO */
1984         if (dc_isar_feature(aa64_mte, s)) {
1985             /* Full MTE is enabled -- set the TCO bit as directed. */
1986             if (crm & 1) {
1987                 set_pstate_bits(PSTATE_TCO);
1988             } else {
1989                 clear_pstate_bits(PSTATE_TCO);
1990             }
1991             gen_rebuild_hflags(s);
1992             /* Many factors, including TCO, go into MTE_ACTIVE. */
1993             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1994         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1995             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
1996             s->base.is_jmp = DISAS_NEXT;
1997         } else {
1998             goto do_unallocated;
1999         }
2000         break;
2001 
2002     case 0x1b: /* SVCR* */
2003         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
2004             goto do_unallocated;
2005         }
2006         if (sme_access_check(s)) {
2007             int old = s->pstate_sm | (s->pstate_za << 1);
2008             int new = (crm & 1) * 3;
2009             int msk = (crm >> 1) & 3;
2010 
2011             if ((old ^ new) & msk) {
2012                 /* At least one bit changes. */
2013                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2014                                     tcg_constant_i32(msk));
2015             } else {
2016                 s->base.is_jmp = DISAS_NEXT;
2017             }
2018         }
2019         break;
2020 
2021     default:
2022     do_unallocated:
2023         unallocated_encoding(s);
2024         return;
2025     }
2026 }
2027 
2028 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2029 {
2030     TCGv_i32 tmp = tcg_temp_new_i32();
2031     TCGv_i32 nzcv = tcg_temp_new_i32();
2032 
2033     /* build bit 31, N */
2034     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2035     /* build bit 30, Z */
2036     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2037     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2038     /* build bit 29, C */
2039     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2040     /* build bit 28, V */
2041     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2042     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2043     /* generate result */
2044     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2045 }
2046 
2047 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2048 {
2049     TCGv_i32 nzcv = tcg_temp_new_i32();
2050 
2051     /* take NZCV from R[t] */
2052     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2053 
2054     /* bit 31, N */
2055     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2056     /* bit 30, Z */
2057     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2058     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2059     /* bit 29, C */
2060     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2061     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2062     /* bit 28, V */
2063     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2064     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2065 }
2066 
2067 static void gen_sysreg_undef(DisasContext *s, bool isread,
2068                              uint8_t op0, uint8_t op1, uint8_t op2,
2069                              uint8_t crn, uint8_t crm, uint8_t rt)
2070 {
2071     /*
2072      * Generate code to emit an UNDEF with correct syndrome
2073      * information for a failed system register access.
2074      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2075      * but if FEAT_IDST is implemented then read accesses to registers
2076      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2077      * syndrome.
2078      */
2079     uint32_t syndrome;
2080 
2081     if (isread && dc_isar_feature(aa64_ids, s) &&
2082         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2083         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2084     } else {
2085         syndrome = syn_uncategorized();
2086     }
2087     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2088 }
2089 
2090 /* MRS - move from system register
2091  * MSR (register) - move to system register
2092  * SYS
2093  * SYSL
2094  * These are all essentially the same insn in 'read' and 'write'
2095  * versions, with varying op0 fields.
2096  */
2097 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2098                        unsigned int op0, unsigned int op1, unsigned int op2,
2099                        unsigned int crn, unsigned int crm, unsigned int rt)
2100 {
2101     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2102                                       crn, crm, op0, op1, op2);
2103     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2104     bool need_exit_tb = false;
2105     TCGv_ptr tcg_ri = NULL;
2106     TCGv_i64 tcg_rt;
2107 
2108     if (!ri) {
2109         /* Unknown register; this might be a guest error or a QEMU
2110          * unimplemented feature.
2111          */
2112         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2113                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2114                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2115         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2116         return;
2117     }
2118 
2119     /* Check access permissions */
2120     if (!cp_access_ok(s->current_el, ri, isread)) {
2121         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2122         return;
2123     }
2124 
2125     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2126         /* Emit code to perform further access permissions checks at
2127          * runtime; this may result in an exception.
2128          */
2129         uint32_t syndrome;
2130 
2131         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2132         gen_a64_update_pc(s, 0);
2133         tcg_ri = tcg_temp_new_ptr();
2134         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2135                                        tcg_constant_i32(key),
2136                                        tcg_constant_i32(syndrome),
2137                                        tcg_constant_i32(isread));
2138     } else if (ri->type & ARM_CP_RAISES_EXC) {
2139         /*
2140          * The readfn or writefn might raise an exception;
2141          * synchronize the CPU state in case it does.
2142          */
2143         gen_a64_update_pc(s, 0);
2144     }
2145 
2146     /* Handle special cases first */
2147     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2148     case 0:
2149         break;
2150     case ARM_CP_NOP:
2151         return;
2152     case ARM_CP_NZCV:
2153         tcg_rt = cpu_reg(s, rt);
2154         if (isread) {
2155             gen_get_nzcv(tcg_rt);
2156         } else {
2157             gen_set_nzcv(tcg_rt);
2158         }
2159         return;
2160     case ARM_CP_CURRENTEL:
2161         /* Reads as current EL value from pstate, which is
2162          * guaranteed to be constant by the tb flags.
2163          */
2164         tcg_rt = cpu_reg(s, rt);
2165         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2166         return;
2167     case ARM_CP_DC_ZVA:
2168         /* Writes clear the aligned block of memory which rt points into. */
2169         if (s->mte_active[0]) {
2170             int desc = 0;
2171 
2172             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2173             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2174             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2175 
2176             tcg_rt = tcg_temp_new_i64();
2177             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2178                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2179         } else {
2180             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2181         }
2182         gen_helper_dc_zva(cpu_env, tcg_rt);
2183         return;
2184     case ARM_CP_DC_GVA:
2185         {
2186             TCGv_i64 clean_addr, tag;
2187 
2188             /*
2189              * DC_GVA, like DC_ZVA, requires that we supply the original
2190              * pointer for an invalid page.  Probe that address first.
2191              */
2192             tcg_rt = cpu_reg(s, rt);
2193             clean_addr = clean_data_tbi(s, tcg_rt);
2194             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2195 
2196             if (s->ata) {
2197                 /* Extract the tag from the register to match STZGM.  */
2198                 tag = tcg_temp_new_i64();
2199                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2200                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2201             }
2202         }
2203         return;
2204     case ARM_CP_DC_GZVA:
2205         {
2206             TCGv_i64 clean_addr, tag;
2207 
2208             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2209             tcg_rt = cpu_reg(s, rt);
2210             clean_addr = clean_data_tbi(s, tcg_rt);
2211             gen_helper_dc_zva(cpu_env, clean_addr);
2212 
2213             if (s->ata) {
2214                 /* Extract the tag from the register to match STZGM.  */
2215                 tag = tcg_temp_new_i64();
2216                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2217                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2218             }
2219         }
2220         return;
2221     default:
2222         g_assert_not_reached();
2223     }
2224     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2225         return;
2226     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2227         return;
2228     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2229         return;
2230     }
2231 
2232     if (ri->type & ARM_CP_IO) {
2233         /* I/O operations must end the TB here (whether read or write) */
2234         need_exit_tb = translator_io_start(&s->base);
2235     }
2236 
2237     tcg_rt = cpu_reg(s, rt);
2238 
2239     if (isread) {
2240         if (ri->type & ARM_CP_CONST) {
2241             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2242         } else if (ri->readfn) {
2243             if (!tcg_ri) {
2244                 tcg_ri = gen_lookup_cp_reg(key);
2245             }
2246             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2247         } else {
2248             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2249         }
2250     } else {
2251         if (ri->type & ARM_CP_CONST) {
2252             /* If not forbidden by access permissions, treat as WI */
2253             return;
2254         } else if (ri->writefn) {
2255             if (!tcg_ri) {
2256                 tcg_ri = gen_lookup_cp_reg(key);
2257             }
2258             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2259         } else {
2260             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2261         }
2262     }
2263 
2264     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2265         /*
2266          * A write to any coprocessor regiser that ends a TB
2267          * must rebuild the hflags for the next TB.
2268          */
2269         gen_rebuild_hflags(s);
2270         /*
2271          * We default to ending the TB on a coprocessor register write,
2272          * but allow this to be suppressed by the register definition
2273          * (usually only necessary to work around guest bugs).
2274          */
2275         need_exit_tb = true;
2276     }
2277     if (need_exit_tb) {
2278         s->base.is_jmp = DISAS_UPDATE_EXIT;
2279     }
2280 }
2281 
2282 /* System
2283  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2284  * +---------------------+---+-----+-----+-------+-------+-----+------+
2285  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2286  * +---------------------+---+-----+-----+-------+-------+-----+------+
2287  */
2288 static void disas_system(DisasContext *s, uint32_t insn)
2289 {
2290     unsigned int l, op0, op1, crn, crm, op2, rt;
2291     l = extract32(insn, 21, 1);
2292     op0 = extract32(insn, 19, 2);
2293     op1 = extract32(insn, 16, 3);
2294     crn = extract32(insn, 12, 4);
2295     crm = extract32(insn, 8, 4);
2296     op2 = extract32(insn, 5, 3);
2297     rt = extract32(insn, 0, 5);
2298 
2299     if (op0 == 0) {
2300         if (l || rt != 31) {
2301             unallocated_encoding(s);
2302             return;
2303         }
2304         switch (crn) {
2305         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2306             handle_hint(s, insn, op1, op2, crm);
2307             break;
2308         case 3: /* CLREX, DSB, DMB, ISB */
2309             handle_sync(s, insn, op1, op2, crm);
2310             break;
2311         case 4: /* MSR (immediate) */
2312             handle_msr_i(s, insn, op1, op2, crm);
2313             break;
2314         default:
2315             unallocated_encoding(s);
2316             break;
2317         }
2318         return;
2319     }
2320     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2321 }
2322 
2323 /* Exception generation
2324  *
2325  *  31             24 23 21 20                     5 4   2 1  0
2326  * +-----------------+-----+------------------------+-----+----+
2327  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2328  * +-----------------------+------------------------+----------+
2329  */
2330 static void disas_exc(DisasContext *s, uint32_t insn)
2331 {
2332     int opc = extract32(insn, 21, 3);
2333     int op2_ll = extract32(insn, 0, 5);
2334     int imm16 = extract32(insn, 5, 16);
2335     uint32_t syndrome;
2336 
2337     switch (opc) {
2338     case 0:
2339         /* For SVC, HVC and SMC we advance the single-step state
2340          * machine before taking the exception. This is architecturally
2341          * mandated, to ensure that single-stepping a system call
2342          * instruction works properly.
2343          */
2344         switch (op2_ll) {
2345         case 1:                                                     /* SVC */
2346             syndrome = syn_aa64_svc(imm16);
2347             if (s->fgt_svc) {
2348                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2349                 break;
2350             }
2351             gen_ss_advance(s);
2352             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2353             break;
2354         case 2:                                                     /* HVC */
2355             if (s->current_el == 0) {
2356                 unallocated_encoding(s);
2357                 break;
2358             }
2359             /* The pre HVC helper handles cases when HVC gets trapped
2360              * as an undefined insn by runtime configuration.
2361              */
2362             gen_a64_update_pc(s, 0);
2363             gen_helper_pre_hvc(cpu_env);
2364             gen_ss_advance(s);
2365             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2366             break;
2367         case 3:                                                     /* SMC */
2368             if (s->current_el == 0) {
2369                 unallocated_encoding(s);
2370                 break;
2371             }
2372             gen_a64_update_pc(s, 0);
2373             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2374             gen_ss_advance(s);
2375             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2376             break;
2377         default:
2378             unallocated_encoding(s);
2379             break;
2380         }
2381         break;
2382     case 1:
2383         if (op2_ll != 0) {
2384             unallocated_encoding(s);
2385             break;
2386         }
2387         /* BRK */
2388         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2389         break;
2390     case 2:
2391         if (op2_ll != 0) {
2392             unallocated_encoding(s);
2393             break;
2394         }
2395         /* HLT. This has two purposes.
2396          * Architecturally, it is an external halting debug instruction.
2397          * Since QEMU doesn't implement external debug, we treat this as
2398          * it is required for halting debug disabled: it will UNDEF.
2399          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2400          */
2401         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2402             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2403         } else {
2404             unallocated_encoding(s);
2405         }
2406         break;
2407     case 5:
2408         if (op2_ll < 1 || op2_ll > 3) {
2409             unallocated_encoding(s);
2410             break;
2411         }
2412         /* DCPS1, DCPS2, DCPS3 */
2413         unallocated_encoding(s);
2414         break;
2415     default:
2416         unallocated_encoding(s);
2417         break;
2418     }
2419 }
2420 
2421 /* Branches, exception generating and system instructions */
2422 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2423 {
2424     switch (extract32(insn, 25, 7)) {
2425     case 0x6a: /* Exception generation / System */
2426         if (insn & (1 << 24)) {
2427             if (extract32(insn, 22, 2) == 0) {
2428                 disas_system(s, insn);
2429             } else {
2430                 unallocated_encoding(s);
2431             }
2432         } else {
2433             disas_exc(s, insn);
2434         }
2435         break;
2436     default:
2437         unallocated_encoding(s);
2438         break;
2439     }
2440 }
2441 
2442 /*
2443  * Load/Store exclusive instructions are implemented by remembering
2444  * the value/address loaded, and seeing if these are the same
2445  * when the store is performed. This is not actually the architecturally
2446  * mandated semantics, but it works for typical guest code sequences
2447  * and avoids having to monitor regular stores.
2448  *
2449  * The store exclusive uses the atomic cmpxchg primitives to avoid
2450  * races in multi-threaded linux-user and when MTTCG softmmu is
2451  * enabled.
2452  */
2453 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2454                                int size, bool is_pair)
2455 {
2456     int idx = get_mem_index(s);
2457     TCGv_i64 dirty_addr, clean_addr;
2458     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2459 
2460     s->is_ldex = true;
2461     dirty_addr = cpu_reg_sp(s, rn);
2462     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2463 
2464     g_assert(size <= 3);
2465     if (is_pair) {
2466         g_assert(size >= 2);
2467         if (size == 2) {
2468             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2469             if (s->be_data == MO_LE) {
2470                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2471                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2472             } else {
2473                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2474                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2475             }
2476         } else {
2477             TCGv_i128 t16 = tcg_temp_new_i128();
2478 
2479             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2480 
2481             if (s->be_data == MO_LE) {
2482                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2483                                       cpu_exclusive_high, t16);
2484             } else {
2485                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2486                                       cpu_exclusive_val, t16);
2487             }
2488             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2489             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2490         }
2491     } else {
2492         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2493         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2494     }
2495     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2496 }
2497 
2498 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2499                                 int rn, int size, int is_pair)
2500 {
2501     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2502      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2503      *     [addr] = {Rt};
2504      *     if (is_pair) {
2505      *         [addr + datasize] = {Rt2};
2506      *     }
2507      *     {Rd} = 0;
2508      * } else {
2509      *     {Rd} = 1;
2510      * }
2511      * env->exclusive_addr = -1;
2512      */
2513     TCGLabel *fail_label = gen_new_label();
2514     TCGLabel *done_label = gen_new_label();
2515     TCGv_i64 tmp, clean_addr;
2516     MemOp memop;
2517 
2518     /*
2519      * FIXME: We are out of spec here.  We have recorded only the address
2520      * from load_exclusive, not the entire range, and we assume that the
2521      * size of the access on both sides match.  The architecture allows the
2522      * store to be smaller than the load, so long as the stored bytes are
2523      * within the range recorded by the load.
2524      */
2525 
2526     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2527     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2528     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2529 
2530     /*
2531      * The write, and any associated faults, only happen if the virtual
2532      * and physical addresses pass the exclusive monitor check.  These
2533      * faults are exceedingly unlikely, because normally the guest uses
2534      * the exact same address register for the load_exclusive, and we
2535      * would have recognized these faults there.
2536      *
2537      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2538      * unaligned 4-byte write within the range of an aligned 8-byte load.
2539      * With LSE2, the store would need to cross a 16-byte boundary when the
2540      * load did not, which would mean the store is outside the range
2541      * recorded for the monitor, which would have failed a corrected monitor
2542      * check above.  For now, we assume no size change and retain the
2543      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2544      *
2545      * It is possible to trigger an MTE fault, by performing the load with
2546      * a virtual address with a valid tag and performing the store with the
2547      * same virtual address and a different invalid tag.
2548      */
2549     memop = size + is_pair;
2550     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2551         memop |= MO_ALIGN;
2552     }
2553     memop = finalize_memop(s, memop);
2554     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2555 
2556     tmp = tcg_temp_new_i64();
2557     if (is_pair) {
2558         if (size == 2) {
2559             if (s->be_data == MO_LE) {
2560                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2561             } else {
2562                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2563             }
2564             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2565                                        cpu_exclusive_val, tmp,
2566                                        get_mem_index(s), memop);
2567             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2568         } else {
2569             TCGv_i128 t16 = tcg_temp_new_i128();
2570             TCGv_i128 c16 = tcg_temp_new_i128();
2571             TCGv_i64 a, b;
2572 
2573             if (s->be_data == MO_LE) {
2574                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2575                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2576                                         cpu_exclusive_high);
2577             } else {
2578                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2579                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2580                                         cpu_exclusive_val);
2581             }
2582 
2583             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2584                                         get_mem_index(s), memop);
2585 
2586             a = tcg_temp_new_i64();
2587             b = tcg_temp_new_i64();
2588             if (s->be_data == MO_LE) {
2589                 tcg_gen_extr_i128_i64(a, b, t16);
2590             } else {
2591                 tcg_gen_extr_i128_i64(b, a, t16);
2592             }
2593 
2594             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2595             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2596             tcg_gen_or_i64(tmp, a, b);
2597 
2598             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2599         }
2600     } else {
2601         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2602                                    cpu_reg(s, rt), get_mem_index(s), memop);
2603         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2604     }
2605     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2606     tcg_gen_br(done_label);
2607 
2608     gen_set_label(fail_label);
2609     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2610     gen_set_label(done_label);
2611     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2612 }
2613 
2614 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2615                                  int rn, int size)
2616 {
2617     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2618     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2619     int memidx = get_mem_index(s);
2620     TCGv_i64 clean_addr;
2621     MemOp memop;
2622 
2623     if (rn == 31) {
2624         gen_check_sp_alignment(s);
2625     }
2626     memop = check_atomic_align(s, rn, size);
2627     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2628     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2629                                memidx, memop);
2630 }
2631 
2632 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2633                                       int rn, int size)
2634 {
2635     TCGv_i64 s1 = cpu_reg(s, rs);
2636     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2637     TCGv_i64 t1 = cpu_reg(s, rt);
2638     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2639     TCGv_i64 clean_addr;
2640     int memidx = get_mem_index(s);
2641     MemOp memop;
2642 
2643     if (rn == 31) {
2644         gen_check_sp_alignment(s);
2645     }
2646 
2647     /* This is a single atomic access, despite the "pair". */
2648     memop = check_atomic_align(s, rn, size + 1);
2649     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2650 
2651     if (size == 2) {
2652         TCGv_i64 cmp = tcg_temp_new_i64();
2653         TCGv_i64 val = tcg_temp_new_i64();
2654 
2655         if (s->be_data == MO_LE) {
2656             tcg_gen_concat32_i64(val, t1, t2);
2657             tcg_gen_concat32_i64(cmp, s1, s2);
2658         } else {
2659             tcg_gen_concat32_i64(val, t2, t1);
2660             tcg_gen_concat32_i64(cmp, s2, s1);
2661         }
2662 
2663         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2664 
2665         if (s->be_data == MO_LE) {
2666             tcg_gen_extr32_i64(s1, s2, cmp);
2667         } else {
2668             tcg_gen_extr32_i64(s2, s1, cmp);
2669         }
2670     } else {
2671         TCGv_i128 cmp = tcg_temp_new_i128();
2672         TCGv_i128 val = tcg_temp_new_i128();
2673 
2674         if (s->be_data == MO_LE) {
2675             tcg_gen_concat_i64_i128(val, t1, t2);
2676             tcg_gen_concat_i64_i128(cmp, s1, s2);
2677         } else {
2678             tcg_gen_concat_i64_i128(val, t2, t1);
2679             tcg_gen_concat_i64_i128(cmp, s2, s1);
2680         }
2681 
2682         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2683 
2684         if (s->be_data == MO_LE) {
2685             tcg_gen_extr_i128_i64(s1, s2, cmp);
2686         } else {
2687             tcg_gen_extr_i128_i64(s2, s1, cmp);
2688         }
2689     }
2690 }
2691 
2692 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2693  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2694  */
2695 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2696 {
2697     int opc0 = extract32(opc, 0, 1);
2698     int regsize;
2699 
2700     if (is_signed) {
2701         regsize = opc0 ? 32 : 64;
2702     } else {
2703         regsize = size == 3 ? 64 : 32;
2704     }
2705     return regsize == 64;
2706 }
2707 
2708 /* Load/store exclusive
2709  *
2710  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2711  * +-----+-------------+----+---+----+------+----+-------+------+------+
2712  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2713  * +-----+-------------+----+---+----+------+----+-------+------+------+
2714  *
2715  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2716  *   L: 0 -> store, 1 -> load
2717  *  o2: 0 -> exclusive, 1 -> not
2718  *  o1: 0 -> single register, 1 -> register pair
2719  *  o0: 1 -> load-acquire/store-release, 0 -> not
2720  */
2721 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2722 {
2723     int rt = extract32(insn, 0, 5);
2724     int rn = extract32(insn, 5, 5);
2725     int rt2 = extract32(insn, 10, 5);
2726     int rs = extract32(insn, 16, 5);
2727     int is_lasr = extract32(insn, 15, 1);
2728     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2729     int size = extract32(insn, 30, 2);
2730     TCGv_i64 clean_addr;
2731     MemOp memop;
2732 
2733     switch (o2_L_o1_o0) {
2734     case 0x0: /* STXR */
2735     case 0x1: /* STLXR */
2736         if (rn == 31) {
2737             gen_check_sp_alignment(s);
2738         }
2739         if (is_lasr) {
2740             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2741         }
2742         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2743         return;
2744 
2745     case 0x4: /* LDXR */
2746     case 0x5: /* LDAXR */
2747         if (rn == 31) {
2748             gen_check_sp_alignment(s);
2749         }
2750         gen_load_exclusive(s, rt, rt2, rn, size, false);
2751         if (is_lasr) {
2752             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2753         }
2754         return;
2755 
2756     case 0x8: /* STLLR */
2757         if (!dc_isar_feature(aa64_lor, s)) {
2758             break;
2759         }
2760         /* StoreLORelease is the same as Store-Release for QEMU.  */
2761         /* fall through */
2762     case 0x9: /* STLR */
2763         /* Generate ISS for non-exclusive accesses including LASR.  */
2764         if (rn == 31) {
2765             gen_check_sp_alignment(s);
2766         }
2767         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2768         memop = check_ordered_align(s, rn, 0, true, size);
2769         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2770                                     true, rn != 31, memop);
2771         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2772                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2773         return;
2774 
2775     case 0xc: /* LDLAR */
2776         if (!dc_isar_feature(aa64_lor, s)) {
2777             break;
2778         }
2779         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2780         /* fall through */
2781     case 0xd: /* LDAR */
2782         /* Generate ISS for non-exclusive accesses including LASR.  */
2783         if (rn == 31) {
2784             gen_check_sp_alignment(s);
2785         }
2786         memop = check_ordered_align(s, rn, 0, false, size);
2787         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2788                                     false, rn != 31, memop);
2789         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2790                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2791         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2792         return;
2793 
2794     case 0x2: case 0x3: /* CASP / STXP */
2795         if (size & 2) { /* STXP / STLXP */
2796             if (rn == 31) {
2797                 gen_check_sp_alignment(s);
2798             }
2799             if (is_lasr) {
2800                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2801             }
2802             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2803             return;
2804         }
2805         if (rt2 == 31
2806             && ((rt | rs) & 1) == 0
2807             && dc_isar_feature(aa64_atomics, s)) {
2808             /* CASP / CASPL */
2809             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2810             return;
2811         }
2812         break;
2813 
2814     case 0x6: case 0x7: /* CASPA / LDXP */
2815         if (size & 2) { /* LDXP / LDAXP */
2816             if (rn == 31) {
2817                 gen_check_sp_alignment(s);
2818             }
2819             gen_load_exclusive(s, rt, rt2, rn, size, true);
2820             if (is_lasr) {
2821                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2822             }
2823             return;
2824         }
2825         if (rt2 == 31
2826             && ((rt | rs) & 1) == 0
2827             && dc_isar_feature(aa64_atomics, s)) {
2828             /* CASPA / CASPAL */
2829             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2830             return;
2831         }
2832         break;
2833 
2834     case 0xa: /* CAS */
2835     case 0xb: /* CASL */
2836     case 0xe: /* CASA */
2837     case 0xf: /* CASAL */
2838         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2839             gen_compare_and_swap(s, rs, rt, rn, size);
2840             return;
2841         }
2842         break;
2843     }
2844     unallocated_encoding(s);
2845 }
2846 
2847 /*
2848  * Load register (literal)
2849  *
2850  *  31 30 29   27  26 25 24 23                5 4     0
2851  * +-----+-------+---+-----+-------------------+-------+
2852  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2853  * +-----+-------+---+-----+-------------------+-------+
2854  *
2855  * V: 1 -> vector (simd/fp)
2856  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2857  *                   10-> 32 bit signed, 11 -> prefetch
2858  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2859  */
2860 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2861 {
2862     int rt = extract32(insn, 0, 5);
2863     int64_t imm = sextract32(insn, 5, 19) << 2;
2864     bool is_vector = extract32(insn, 26, 1);
2865     int opc = extract32(insn, 30, 2);
2866     bool is_signed = false;
2867     int size = 2;
2868     TCGv_i64 tcg_rt, clean_addr;
2869     MemOp memop;
2870 
2871     if (is_vector) {
2872         if (opc == 3) {
2873             unallocated_encoding(s);
2874             return;
2875         }
2876         size = 2 + opc;
2877         if (!fp_access_check(s)) {
2878             return;
2879         }
2880         memop = finalize_memop_asimd(s, size);
2881     } else {
2882         if (opc == 3) {
2883             /* PRFM (literal) : prefetch */
2884             return;
2885         }
2886         size = 2 + extract32(opc, 0, 1);
2887         is_signed = extract32(opc, 1, 1);
2888         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2889     }
2890 
2891     tcg_rt = cpu_reg(s, rt);
2892 
2893     clean_addr = tcg_temp_new_i64();
2894     gen_pc_plus_diff(s, clean_addr, imm);
2895 
2896     if (is_vector) {
2897         do_fp_ld(s, rt, clean_addr, memop);
2898     } else {
2899         /* Only unsigned 32bit loads target 32bit registers.  */
2900         bool iss_sf = opc != 0;
2901         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2902     }
2903 }
2904 
2905 /*
2906  * LDNP (Load Pair - non-temporal hint)
2907  * LDP (Load Pair - non vector)
2908  * LDPSW (Load Pair Signed Word - non vector)
2909  * STNP (Store Pair - non-temporal hint)
2910  * STP (Store Pair - non vector)
2911  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2912  * LDP (Load Pair of SIMD&FP)
2913  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2914  * STP (Store Pair of SIMD&FP)
2915  *
2916  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2917  * +-----+-------+---+---+-------+---+-----------------------------+
2918  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2919  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2920  *
2921  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2922  *      LDPSW/STGP               01
2923  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2924  *   V: 0 -> GPR, 1 -> Vector
2925  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2926  *      10 -> signed offset, 11 -> pre-index
2927  *   L: 0 -> Store 1 -> Load
2928  *
2929  * Rt, Rt2 = GPR or SIMD registers to be stored
2930  * Rn = general purpose register containing address
2931  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2932  */
2933 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2934 {
2935     int rt = extract32(insn, 0, 5);
2936     int rn = extract32(insn, 5, 5);
2937     int rt2 = extract32(insn, 10, 5);
2938     uint64_t offset = sextract64(insn, 15, 7);
2939     int index = extract32(insn, 23, 2);
2940     bool is_vector = extract32(insn, 26, 1);
2941     bool is_load = extract32(insn, 22, 1);
2942     int opc = extract32(insn, 30, 2);
2943     bool is_signed = false;
2944     bool postindex = false;
2945     bool wback = false;
2946     bool set_tag = false;
2947     TCGv_i64 clean_addr, dirty_addr;
2948     MemOp mop;
2949     int size;
2950 
2951     if (opc == 3) {
2952         unallocated_encoding(s);
2953         return;
2954     }
2955 
2956     if (is_vector) {
2957         size = 2 + opc;
2958     } else if (opc == 1 && !is_load) {
2959         /* STGP */
2960         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2961             unallocated_encoding(s);
2962             return;
2963         }
2964         size = 3;
2965         set_tag = true;
2966     } else {
2967         size = 2 + extract32(opc, 1, 1);
2968         is_signed = extract32(opc, 0, 1);
2969         if (!is_load && is_signed) {
2970             unallocated_encoding(s);
2971             return;
2972         }
2973     }
2974 
2975     switch (index) {
2976     case 1: /* post-index */
2977         postindex = true;
2978         wback = true;
2979         break;
2980     case 0:
2981         /* signed offset with "non-temporal" hint. Since we don't emulate
2982          * caches we don't care about hints to the cache system about
2983          * data access patterns, and handle this identically to plain
2984          * signed offset.
2985          */
2986         if (is_signed) {
2987             /* There is no non-temporal-hint version of LDPSW */
2988             unallocated_encoding(s);
2989             return;
2990         }
2991         postindex = false;
2992         break;
2993     case 2: /* signed offset, rn not updated */
2994         postindex = false;
2995         break;
2996     case 3: /* pre-index */
2997         postindex = false;
2998         wback = true;
2999         break;
3000     }
3001 
3002     if (is_vector && !fp_access_check(s)) {
3003         return;
3004     }
3005 
3006     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
3007 
3008     if (rn == 31) {
3009         gen_check_sp_alignment(s);
3010     }
3011 
3012     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3013     if (!postindex) {
3014         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3015     }
3016 
3017     if (set_tag) {
3018         if (!s->ata) {
3019             /*
3020              * TODO: We could rely on the stores below, at least for
3021              * system mode, if we arrange to add MO_ALIGN_16.
3022              */
3023             gen_helper_stg_stub(cpu_env, dirty_addr);
3024         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3025             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3026         } else {
3027             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3028         }
3029     }
3030 
3031     if (is_vector) {
3032         mop = finalize_memop_asimd(s, size);
3033     } else {
3034         mop = finalize_memop(s, size);
3035     }
3036     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3037                                 (wback || rn != 31) && !set_tag,
3038                                 2 << size, mop);
3039 
3040     if (is_vector) {
3041         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3042         if (is_load) {
3043             do_fp_ld(s, rt, clean_addr, mop);
3044         } else {
3045             do_fp_st(s, rt, clean_addr, mop);
3046         }
3047         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3048         if (is_load) {
3049             do_fp_ld(s, rt2, clean_addr, mop);
3050         } else {
3051             do_fp_st(s, rt2, clean_addr, mop);
3052         }
3053     } else {
3054         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3055         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3056 
3057         /*
3058          * We built mop above for the single logical access -- rebuild it
3059          * now for the paired operation.
3060          *
3061          * With LSE2, non-sign-extending pairs are treated atomically if
3062          * aligned, and if unaligned one of the pair will be completely
3063          * within a 16-byte block and that element will be atomic.
3064          * Otherwise each element is separately atomic.
3065          * In all cases, issue one operation with the correct atomicity.
3066          *
3067          * This treats sign-extending loads like zero-extending loads,
3068          * since that reuses the most code below.
3069          */
3070         mop = size + 1;
3071         if (s->align_mem) {
3072             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3073         }
3074         mop = finalize_memop_pair(s, mop);
3075 
3076         if (is_load) {
3077             if (size == 2) {
3078                 int o2 = s->be_data == MO_LE ? 32 : 0;
3079                 int o1 = o2 ^ 32;
3080 
3081                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3082                 if (is_signed) {
3083                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3084                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3085                 } else {
3086                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3087                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3088                 }
3089             } else {
3090                 TCGv_i128 tmp = tcg_temp_new_i128();
3091 
3092                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3093                 if (s->be_data == MO_LE) {
3094                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3095                 } else {
3096                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3097                 }
3098             }
3099         } else {
3100             if (size == 2) {
3101                 TCGv_i64 tmp = tcg_temp_new_i64();
3102 
3103                 if (s->be_data == MO_LE) {
3104                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3105                 } else {
3106                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3107                 }
3108                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3109             } else {
3110                 TCGv_i128 tmp = tcg_temp_new_i128();
3111 
3112                 if (s->be_data == MO_LE) {
3113                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3114                 } else {
3115                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3116                 }
3117                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3118             }
3119         }
3120     }
3121 
3122     if (wback) {
3123         if (postindex) {
3124             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3125         }
3126         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3127     }
3128 }
3129 
3130 /*
3131  * Load/store (immediate post-indexed)
3132  * Load/store (immediate pre-indexed)
3133  * Load/store (unscaled immediate)
3134  *
3135  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3136  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3137  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3138  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3139  *
3140  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3141          10 -> unprivileged
3142  * V = 0 -> non-vector
3143  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3144  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3145  */
3146 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3147                                 int opc,
3148                                 int size,
3149                                 int rt,
3150                                 bool is_vector)
3151 {
3152     int rn = extract32(insn, 5, 5);
3153     int imm9 = sextract32(insn, 12, 9);
3154     int idx = extract32(insn, 10, 2);
3155     bool is_signed = false;
3156     bool is_store = false;
3157     bool is_extended = false;
3158     bool is_unpriv = (idx == 2);
3159     bool iss_valid;
3160     bool post_index;
3161     bool writeback;
3162     int memidx;
3163     MemOp memop;
3164     TCGv_i64 clean_addr, dirty_addr;
3165 
3166     if (is_vector) {
3167         size |= (opc & 2) << 1;
3168         if (size > 4 || is_unpriv) {
3169             unallocated_encoding(s);
3170             return;
3171         }
3172         is_store = ((opc & 1) == 0);
3173         if (!fp_access_check(s)) {
3174             return;
3175         }
3176         memop = finalize_memop_asimd(s, size);
3177     } else {
3178         if (size == 3 && opc == 2) {
3179             /* PRFM - prefetch */
3180             if (idx != 0) {
3181                 unallocated_encoding(s);
3182                 return;
3183             }
3184             return;
3185         }
3186         if (opc == 3 && size > 1) {
3187             unallocated_encoding(s);
3188             return;
3189         }
3190         is_store = (opc == 0);
3191         is_signed = !is_store && extract32(opc, 1, 1);
3192         is_extended = (size < 3) && extract32(opc, 0, 1);
3193         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3194     }
3195 
3196     switch (idx) {
3197     case 0:
3198     case 2:
3199         post_index = false;
3200         writeback = false;
3201         break;
3202     case 1:
3203         post_index = true;
3204         writeback = true;
3205         break;
3206     case 3:
3207         post_index = false;
3208         writeback = true;
3209         break;
3210     default:
3211         g_assert_not_reached();
3212     }
3213 
3214     iss_valid = !is_vector && !writeback;
3215 
3216     if (rn == 31) {
3217         gen_check_sp_alignment(s);
3218     }
3219 
3220     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3221     if (!post_index) {
3222         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3223     }
3224 
3225     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3226 
3227     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3228                                        writeback || rn != 31,
3229                                        memop, is_unpriv, memidx);
3230 
3231     if (is_vector) {
3232         if (is_store) {
3233             do_fp_st(s, rt, clean_addr, memop);
3234         } else {
3235             do_fp_ld(s, rt, clean_addr, memop);
3236         }
3237     } else {
3238         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3239         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3240 
3241         if (is_store) {
3242             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3243                              iss_valid, rt, iss_sf, false);
3244         } else {
3245             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3246                              is_extended, memidx,
3247                              iss_valid, rt, iss_sf, false);
3248         }
3249     }
3250 
3251     if (writeback) {
3252         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3253         if (post_index) {
3254             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3255         }
3256         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3257     }
3258 }
3259 
3260 /*
3261  * Load/store (register offset)
3262  *
3263  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3264  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3265  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3266  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3267  *
3268  * For non-vector:
3269  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3270  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3271  * For vector:
3272  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3273  *   opc<0>: 0 -> store, 1 -> load
3274  * V: 1 -> vector/simd
3275  * opt: extend encoding (see DecodeRegExtend)
3276  * S: if S=1 then scale (essentially index by sizeof(size))
3277  * Rt: register to transfer into/out of
3278  * Rn: address register or SP for base
3279  * Rm: offset register or ZR for offset
3280  */
3281 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3282                                    int opc,
3283                                    int size,
3284                                    int rt,
3285                                    bool is_vector)
3286 {
3287     int rn = extract32(insn, 5, 5);
3288     int shift = extract32(insn, 12, 1);
3289     int rm = extract32(insn, 16, 5);
3290     int opt = extract32(insn, 13, 3);
3291     bool is_signed = false;
3292     bool is_store = false;
3293     bool is_extended = false;
3294     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3295     MemOp memop;
3296 
3297     if (extract32(opt, 1, 1) == 0) {
3298         unallocated_encoding(s);
3299         return;
3300     }
3301 
3302     if (is_vector) {
3303         size |= (opc & 2) << 1;
3304         if (size > 4) {
3305             unallocated_encoding(s);
3306             return;
3307         }
3308         is_store = !extract32(opc, 0, 1);
3309         if (!fp_access_check(s)) {
3310             return;
3311         }
3312     } else {
3313         if (size == 3 && opc == 2) {
3314             /* PRFM - prefetch */
3315             return;
3316         }
3317         if (opc == 3 && size > 1) {
3318             unallocated_encoding(s);
3319             return;
3320         }
3321         is_store = (opc == 0);
3322         is_signed = !is_store && extract32(opc, 1, 1);
3323         is_extended = (size < 3) && extract32(opc, 0, 1);
3324     }
3325 
3326     if (rn == 31) {
3327         gen_check_sp_alignment(s);
3328     }
3329     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3330 
3331     tcg_rm = read_cpu_reg(s, rm, 1);
3332     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3333 
3334     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3335 
3336     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3337     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3338 
3339     if (is_vector) {
3340         if (is_store) {
3341             do_fp_st(s, rt, clean_addr, memop);
3342         } else {
3343             do_fp_ld(s, rt, clean_addr, memop);
3344         }
3345     } else {
3346         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3347         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3348 
3349         if (is_store) {
3350             do_gpr_st(s, tcg_rt, clean_addr, memop,
3351                       true, rt, iss_sf, false);
3352         } else {
3353             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3354                       is_extended, true, rt, iss_sf, false);
3355         }
3356     }
3357 }
3358 
3359 /*
3360  * Load/store (unsigned immediate)
3361  *
3362  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3363  * +----+-------+---+-----+-----+------------+-------+------+
3364  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3365  * +----+-------+---+-----+-----+------------+-------+------+
3366  *
3367  * For non-vector:
3368  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3369  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3370  * For vector:
3371  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3372  *   opc<0>: 0 -> store, 1 -> load
3373  * Rn: base address register (inc SP)
3374  * Rt: target register
3375  */
3376 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3377                                         int opc,
3378                                         int size,
3379                                         int rt,
3380                                         bool is_vector)
3381 {
3382     int rn = extract32(insn, 5, 5);
3383     unsigned int imm12 = extract32(insn, 10, 12);
3384     unsigned int offset;
3385     TCGv_i64 clean_addr, dirty_addr;
3386     bool is_store;
3387     bool is_signed = false;
3388     bool is_extended = false;
3389     MemOp memop;
3390 
3391     if (is_vector) {
3392         size |= (opc & 2) << 1;
3393         if (size > 4) {
3394             unallocated_encoding(s);
3395             return;
3396         }
3397         is_store = !extract32(opc, 0, 1);
3398         if (!fp_access_check(s)) {
3399             return;
3400         }
3401     } else {
3402         if (size == 3 && opc == 2) {
3403             /* PRFM - prefetch */
3404             return;
3405         }
3406         if (opc == 3 && size > 1) {
3407             unallocated_encoding(s);
3408             return;
3409         }
3410         is_store = (opc == 0);
3411         is_signed = !is_store && extract32(opc, 1, 1);
3412         is_extended = (size < 3) && extract32(opc, 0, 1);
3413     }
3414 
3415     if (rn == 31) {
3416         gen_check_sp_alignment(s);
3417     }
3418     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3419     offset = imm12 << size;
3420     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3421 
3422     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3423     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3424 
3425     if (is_vector) {
3426         if (is_store) {
3427             do_fp_st(s, rt, clean_addr, memop);
3428         } else {
3429             do_fp_ld(s, rt, clean_addr, memop);
3430         }
3431     } else {
3432         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3433         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3434         if (is_store) {
3435             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3436         } else {
3437             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3438                       is_extended, true, rt, iss_sf, false);
3439         }
3440     }
3441 }
3442 
3443 /* Atomic memory operations
3444  *
3445  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3446  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3447  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3448  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3449  *
3450  * Rt: the result register
3451  * Rn: base address or SP
3452  * Rs: the source register for the operation
3453  * V: vector flag (always 0 as of v8.3)
3454  * A: acquire flag
3455  * R: release flag
3456  */
3457 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3458                               int size, int rt, bool is_vector)
3459 {
3460     int rs = extract32(insn, 16, 5);
3461     int rn = extract32(insn, 5, 5);
3462     int o3_opc = extract32(insn, 12, 4);
3463     bool r = extract32(insn, 22, 1);
3464     bool a = extract32(insn, 23, 1);
3465     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3466     AtomicThreeOpFn *fn = NULL;
3467     MemOp mop = size;
3468 
3469     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3470         unallocated_encoding(s);
3471         return;
3472     }
3473     switch (o3_opc) {
3474     case 000: /* LDADD */
3475         fn = tcg_gen_atomic_fetch_add_i64;
3476         break;
3477     case 001: /* LDCLR */
3478         fn = tcg_gen_atomic_fetch_and_i64;
3479         break;
3480     case 002: /* LDEOR */
3481         fn = tcg_gen_atomic_fetch_xor_i64;
3482         break;
3483     case 003: /* LDSET */
3484         fn = tcg_gen_atomic_fetch_or_i64;
3485         break;
3486     case 004: /* LDSMAX */
3487         fn = tcg_gen_atomic_fetch_smax_i64;
3488         mop |= MO_SIGN;
3489         break;
3490     case 005: /* LDSMIN */
3491         fn = tcg_gen_atomic_fetch_smin_i64;
3492         mop |= MO_SIGN;
3493         break;
3494     case 006: /* LDUMAX */
3495         fn = tcg_gen_atomic_fetch_umax_i64;
3496         break;
3497     case 007: /* LDUMIN */
3498         fn = tcg_gen_atomic_fetch_umin_i64;
3499         break;
3500     case 010: /* SWP */
3501         fn = tcg_gen_atomic_xchg_i64;
3502         break;
3503     case 014: /* LDAPR, LDAPRH, LDAPRB */
3504         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3505             rs != 31 || a != 1 || r != 0) {
3506             unallocated_encoding(s);
3507             return;
3508         }
3509         break;
3510     default:
3511         unallocated_encoding(s);
3512         return;
3513     }
3514 
3515     if (rn == 31) {
3516         gen_check_sp_alignment(s);
3517     }
3518 
3519     mop = check_atomic_align(s, rn, mop);
3520     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3521 
3522     if (o3_opc == 014) {
3523         /*
3524          * LDAPR* are a special case because they are a simple load, not a
3525          * fetch-and-do-something op.
3526          * The architectural consistency requirements here are weaker than
3527          * full load-acquire (we only need "load-acquire processor consistent"),
3528          * but we choose to implement them as full LDAQ.
3529          */
3530         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3531                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3532         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3533         return;
3534     }
3535 
3536     tcg_rs = read_cpu_reg(s, rs, true);
3537     tcg_rt = cpu_reg(s, rt);
3538 
3539     if (o3_opc == 1) { /* LDCLR */
3540         tcg_gen_not_i64(tcg_rs, tcg_rs);
3541     }
3542 
3543     /* The tcg atomic primitives are all full barriers.  Therefore we
3544      * can ignore the Acquire and Release bits of this instruction.
3545      */
3546     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3547 
3548     if (mop & MO_SIGN) {
3549         switch (size) {
3550         case MO_8:
3551             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3552             break;
3553         case MO_16:
3554             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3555             break;
3556         case MO_32:
3557             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3558             break;
3559         case MO_64:
3560             break;
3561         default:
3562             g_assert_not_reached();
3563         }
3564     }
3565 }
3566 
3567 /*
3568  * PAC memory operations
3569  *
3570  *  31  30      27  26    24    22  21       12  11  10    5     0
3571  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3572  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3573  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3574  *
3575  * Rt: the result register
3576  * Rn: base address or SP
3577  * V: vector flag (always 0 as of v8.3)
3578  * M: clear for key DA, set for key DB
3579  * W: pre-indexing flag
3580  * S: sign for imm9.
3581  */
3582 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3583                            int size, int rt, bool is_vector)
3584 {
3585     int rn = extract32(insn, 5, 5);
3586     bool is_wback = extract32(insn, 11, 1);
3587     bool use_key_a = !extract32(insn, 23, 1);
3588     int offset;
3589     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3590     MemOp memop;
3591 
3592     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3593         unallocated_encoding(s);
3594         return;
3595     }
3596 
3597     if (rn == 31) {
3598         gen_check_sp_alignment(s);
3599     }
3600     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3601 
3602     if (s->pauth_active) {
3603         if (use_key_a) {
3604             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3605                              tcg_constant_i64(0));
3606         } else {
3607             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3608                              tcg_constant_i64(0));
3609         }
3610     }
3611 
3612     /* Form the 10-bit signed, scaled offset.  */
3613     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3614     offset = sextract32(offset << size, 0, 10 + size);
3615     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3616 
3617     memop = finalize_memop(s, size);
3618 
3619     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3620     clean_addr = gen_mte_check1(s, dirty_addr, false,
3621                                 is_wback || rn != 31, memop);
3622 
3623     tcg_rt = cpu_reg(s, rt);
3624     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3625               /* extend */ false, /* iss_valid */ !is_wback,
3626               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3627 
3628     if (is_wback) {
3629         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3630     }
3631 }
3632 
3633 /*
3634  * LDAPR/STLR (unscaled immediate)
3635  *
3636  *  31  30            24    22  21       12    10    5     0
3637  * +------+-------------+-----+---+--------+-----+----+-----+
3638  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3639  * +------+-------------+-----+---+--------+-----+----+-----+
3640  *
3641  * Rt: source or destination register
3642  * Rn: base register
3643  * imm9: unscaled immediate offset
3644  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3645  * size: size of load/store
3646  */
3647 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3648 {
3649     int rt = extract32(insn, 0, 5);
3650     int rn = extract32(insn, 5, 5);
3651     int offset = sextract32(insn, 12, 9);
3652     int opc = extract32(insn, 22, 2);
3653     int size = extract32(insn, 30, 2);
3654     TCGv_i64 clean_addr, dirty_addr;
3655     bool is_store = false;
3656     bool extend = false;
3657     bool iss_sf;
3658     MemOp mop = size;
3659 
3660     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3661         unallocated_encoding(s);
3662         return;
3663     }
3664 
3665     switch (opc) {
3666     case 0: /* STLURB */
3667         is_store = true;
3668         break;
3669     case 1: /* LDAPUR* */
3670         break;
3671     case 2: /* LDAPURS* 64-bit variant */
3672         if (size == 3) {
3673             unallocated_encoding(s);
3674             return;
3675         }
3676         mop |= MO_SIGN;
3677         break;
3678     case 3: /* LDAPURS* 32-bit variant */
3679         if (size > 1) {
3680             unallocated_encoding(s);
3681             return;
3682         }
3683         mop |= MO_SIGN;
3684         extend = true; /* zero-extend 32->64 after signed load */
3685         break;
3686     default:
3687         g_assert_not_reached();
3688     }
3689 
3690     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3691 
3692     if (rn == 31) {
3693         gen_check_sp_alignment(s);
3694     }
3695 
3696     mop = check_ordered_align(s, rn, offset, is_store, mop);
3697 
3698     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3699     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3700     clean_addr = clean_data_tbi(s, dirty_addr);
3701 
3702     if (is_store) {
3703         /* Store-Release semantics */
3704         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3705         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3706     } else {
3707         /*
3708          * Load-AcquirePC semantics; we implement as the slightly more
3709          * restrictive Load-Acquire.
3710          */
3711         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3712                   extend, true, rt, iss_sf, true);
3713         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3714     }
3715 }
3716 
3717 /* Load/store register (all forms) */
3718 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3719 {
3720     int rt = extract32(insn, 0, 5);
3721     int opc = extract32(insn, 22, 2);
3722     bool is_vector = extract32(insn, 26, 1);
3723     int size = extract32(insn, 30, 2);
3724 
3725     switch (extract32(insn, 24, 2)) {
3726     case 0:
3727         if (extract32(insn, 21, 1) == 0) {
3728             /* Load/store register (unscaled immediate)
3729              * Load/store immediate pre/post-indexed
3730              * Load/store register unprivileged
3731              */
3732             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3733             return;
3734         }
3735         switch (extract32(insn, 10, 2)) {
3736         case 0:
3737             disas_ldst_atomic(s, insn, size, rt, is_vector);
3738             return;
3739         case 2:
3740             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3741             return;
3742         default:
3743             disas_ldst_pac(s, insn, size, rt, is_vector);
3744             return;
3745         }
3746         break;
3747     case 1:
3748         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3749         return;
3750     }
3751     unallocated_encoding(s);
3752 }
3753 
3754 /* AdvSIMD load/store multiple structures
3755  *
3756  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3757  * +---+---+---------------+---+-------------+--------+------+------+------+
3758  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3759  * +---+---+---------------+---+-------------+--------+------+------+------+
3760  *
3761  * AdvSIMD load/store multiple structures (post-indexed)
3762  *
3763  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3764  * +---+---+---------------+---+---+---------+--------+------+------+------+
3765  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3766  * +---+---+---------------+---+---+---------+--------+------+------+------+
3767  *
3768  * Rt: first (or only) SIMD&FP register to be transferred
3769  * Rn: base address or SP
3770  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3771  */
3772 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3773 {
3774     int rt = extract32(insn, 0, 5);
3775     int rn = extract32(insn, 5, 5);
3776     int rm = extract32(insn, 16, 5);
3777     int size = extract32(insn, 10, 2);
3778     int opcode = extract32(insn, 12, 4);
3779     bool is_store = !extract32(insn, 22, 1);
3780     bool is_postidx = extract32(insn, 23, 1);
3781     bool is_q = extract32(insn, 30, 1);
3782     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3783     MemOp endian, align, mop;
3784 
3785     int total;    /* total bytes */
3786     int elements; /* elements per vector */
3787     int rpt;    /* num iterations */
3788     int selem;  /* structure elements */
3789     int r;
3790 
3791     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3792         unallocated_encoding(s);
3793         return;
3794     }
3795 
3796     if (!is_postidx && rm != 0) {
3797         unallocated_encoding(s);
3798         return;
3799     }
3800 
3801     /* From the shared decode logic */
3802     switch (opcode) {
3803     case 0x0:
3804         rpt = 1;
3805         selem = 4;
3806         break;
3807     case 0x2:
3808         rpt = 4;
3809         selem = 1;
3810         break;
3811     case 0x4:
3812         rpt = 1;
3813         selem = 3;
3814         break;
3815     case 0x6:
3816         rpt = 3;
3817         selem = 1;
3818         break;
3819     case 0x7:
3820         rpt = 1;
3821         selem = 1;
3822         break;
3823     case 0x8:
3824         rpt = 1;
3825         selem = 2;
3826         break;
3827     case 0xa:
3828         rpt = 2;
3829         selem = 1;
3830         break;
3831     default:
3832         unallocated_encoding(s);
3833         return;
3834     }
3835 
3836     if (size == 3 && !is_q && selem != 1) {
3837         /* reserved */
3838         unallocated_encoding(s);
3839         return;
3840     }
3841 
3842     if (!fp_access_check(s)) {
3843         return;
3844     }
3845 
3846     if (rn == 31) {
3847         gen_check_sp_alignment(s);
3848     }
3849 
3850     /* For our purposes, bytes are always little-endian.  */
3851     endian = s->be_data;
3852     if (size == 0) {
3853         endian = MO_LE;
3854     }
3855 
3856     total = rpt * selem * (is_q ? 16 : 8);
3857     tcg_rn = cpu_reg_sp(s, rn);
3858 
3859     /*
3860      * Issue the MTE check vs the logical repeat count, before we
3861      * promote consecutive little-endian elements below.
3862      */
3863     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3864                                 total, finalize_memop(s, size));
3865 
3866     /*
3867      * Consecutive little-endian elements from a single register
3868      * can be promoted to a larger little-endian operation.
3869      */
3870     align = MO_ALIGN;
3871     if (selem == 1 && endian == MO_LE) {
3872         align = pow2_align(size);
3873         size = 3;
3874     }
3875     if (!s->align_mem) {
3876         align = 0;
3877     }
3878     mop = endian | size | align;
3879 
3880     elements = (is_q ? 16 : 8) >> size;
3881     tcg_ebytes = tcg_constant_i64(1 << size);
3882     for (r = 0; r < rpt; r++) {
3883         int e;
3884         for (e = 0; e < elements; e++) {
3885             int xs;
3886             for (xs = 0; xs < selem; xs++) {
3887                 int tt = (rt + r + xs) % 32;
3888                 if (is_store) {
3889                     do_vec_st(s, tt, e, clean_addr, mop);
3890                 } else {
3891                     do_vec_ld(s, tt, e, clean_addr, mop);
3892                 }
3893                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3894             }
3895         }
3896     }
3897 
3898     if (!is_store) {
3899         /* For non-quad operations, setting a slice of the low
3900          * 64 bits of the register clears the high 64 bits (in
3901          * the ARM ARM pseudocode this is implicit in the fact
3902          * that 'rval' is a 64 bit wide variable).
3903          * For quad operations, we might still need to zero the
3904          * high bits of SVE.
3905          */
3906         for (r = 0; r < rpt * selem; r++) {
3907             int tt = (rt + r) % 32;
3908             clear_vec_high(s, is_q, tt);
3909         }
3910     }
3911 
3912     if (is_postidx) {
3913         if (rm == 31) {
3914             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3915         } else {
3916             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3917         }
3918     }
3919 }
3920 
3921 /* AdvSIMD load/store single structure
3922  *
3923  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3924  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3925  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3926  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3927  *
3928  * AdvSIMD load/store single structure (post-indexed)
3929  *
3930  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3931  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3932  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3933  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3934  *
3935  * Rt: first (or only) SIMD&FP register to be transferred
3936  * Rn: base address or SP
3937  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3938  * index = encoded in Q:S:size dependent on size
3939  *
3940  * lane_size = encoded in R, opc
3941  * transfer width = encoded in opc, S, size
3942  */
3943 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3944 {
3945     int rt = extract32(insn, 0, 5);
3946     int rn = extract32(insn, 5, 5);
3947     int rm = extract32(insn, 16, 5);
3948     int size = extract32(insn, 10, 2);
3949     int S = extract32(insn, 12, 1);
3950     int opc = extract32(insn, 13, 3);
3951     int R = extract32(insn, 21, 1);
3952     int is_load = extract32(insn, 22, 1);
3953     int is_postidx = extract32(insn, 23, 1);
3954     int is_q = extract32(insn, 30, 1);
3955 
3956     int scale = extract32(opc, 1, 2);
3957     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3958     bool replicate = false;
3959     int index = is_q << 3 | S << 2 | size;
3960     int xs, total;
3961     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3962     MemOp mop;
3963 
3964     if (extract32(insn, 31, 1)) {
3965         unallocated_encoding(s);
3966         return;
3967     }
3968     if (!is_postidx && rm != 0) {
3969         unallocated_encoding(s);
3970         return;
3971     }
3972 
3973     switch (scale) {
3974     case 3:
3975         if (!is_load || S) {
3976             unallocated_encoding(s);
3977             return;
3978         }
3979         scale = size;
3980         replicate = true;
3981         break;
3982     case 0:
3983         break;
3984     case 1:
3985         if (extract32(size, 0, 1)) {
3986             unallocated_encoding(s);
3987             return;
3988         }
3989         index >>= 1;
3990         break;
3991     case 2:
3992         if (extract32(size, 1, 1)) {
3993             unallocated_encoding(s);
3994             return;
3995         }
3996         if (!extract32(size, 0, 1)) {
3997             index >>= 2;
3998         } else {
3999             if (S) {
4000                 unallocated_encoding(s);
4001                 return;
4002             }
4003             index >>= 3;
4004             scale = 3;
4005         }
4006         break;
4007     default:
4008         g_assert_not_reached();
4009     }
4010 
4011     if (!fp_access_check(s)) {
4012         return;
4013     }
4014 
4015     if (rn == 31) {
4016         gen_check_sp_alignment(s);
4017     }
4018 
4019     total = selem << scale;
4020     tcg_rn = cpu_reg_sp(s, rn);
4021 
4022     mop = finalize_memop(s, scale);
4023 
4024     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4025                                 total, mop);
4026 
4027     tcg_ebytes = tcg_constant_i64(1 << scale);
4028     for (xs = 0; xs < selem; xs++) {
4029         if (replicate) {
4030             /* Load and replicate to all elements */
4031             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4032 
4033             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4034             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4035                                  (is_q + 1) * 8, vec_full_reg_size(s),
4036                                  tcg_tmp);
4037         } else {
4038             /* Load/store one element per register */
4039             if (is_load) {
4040                 do_vec_ld(s, rt, index, clean_addr, mop);
4041             } else {
4042                 do_vec_st(s, rt, index, clean_addr, mop);
4043             }
4044         }
4045         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4046         rt = (rt + 1) % 32;
4047     }
4048 
4049     if (is_postidx) {
4050         if (rm == 31) {
4051             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4052         } else {
4053             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4054         }
4055     }
4056 }
4057 
4058 /*
4059  * Load/Store memory tags
4060  *
4061  *  31 30 29         24     22  21     12    10      5      0
4062  * +-----+-------------+-----+---+------+-----+------+------+
4063  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4064  * +-----+-------------+-----+---+------+-----+------+------+
4065  */
4066 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4067 {
4068     int rt = extract32(insn, 0, 5);
4069     int rn = extract32(insn, 5, 5);
4070     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4071     int op2 = extract32(insn, 10, 2);
4072     int op1 = extract32(insn, 22, 2);
4073     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4074     int index = 0;
4075     TCGv_i64 addr, clean_addr, tcg_rt;
4076 
4077     /* We checked insn bits [29:24,21] in the caller.  */
4078     if (extract32(insn, 30, 2) != 3) {
4079         goto do_unallocated;
4080     }
4081 
4082     /*
4083      * @index is a tri-state variable which has 3 states:
4084      * < 0 : post-index, writeback
4085      * = 0 : signed offset
4086      * > 0 : pre-index, writeback
4087      */
4088     switch (op1) {
4089     case 0:
4090         if (op2 != 0) {
4091             /* STG */
4092             index = op2 - 2;
4093         } else {
4094             /* STZGM */
4095             if (s->current_el == 0 || offset != 0) {
4096                 goto do_unallocated;
4097             }
4098             is_mult = is_zero = true;
4099         }
4100         break;
4101     case 1:
4102         if (op2 != 0) {
4103             /* STZG */
4104             is_zero = true;
4105             index = op2 - 2;
4106         } else {
4107             /* LDG */
4108             is_load = true;
4109         }
4110         break;
4111     case 2:
4112         if (op2 != 0) {
4113             /* ST2G */
4114             is_pair = true;
4115             index = op2 - 2;
4116         } else {
4117             /* STGM */
4118             if (s->current_el == 0 || offset != 0) {
4119                 goto do_unallocated;
4120             }
4121             is_mult = true;
4122         }
4123         break;
4124     case 3:
4125         if (op2 != 0) {
4126             /* STZ2G */
4127             is_pair = is_zero = true;
4128             index = op2 - 2;
4129         } else {
4130             /* LDGM */
4131             if (s->current_el == 0 || offset != 0) {
4132                 goto do_unallocated;
4133             }
4134             is_mult = is_load = true;
4135         }
4136         break;
4137 
4138     default:
4139     do_unallocated:
4140         unallocated_encoding(s);
4141         return;
4142     }
4143 
4144     if (is_mult
4145         ? !dc_isar_feature(aa64_mte, s)
4146         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4147         goto do_unallocated;
4148     }
4149 
4150     if (rn == 31) {
4151         gen_check_sp_alignment(s);
4152     }
4153 
4154     addr = read_cpu_reg_sp(s, rn, true);
4155     if (index >= 0) {
4156         /* pre-index or signed offset */
4157         tcg_gen_addi_i64(addr, addr, offset);
4158     }
4159 
4160     if (is_mult) {
4161         tcg_rt = cpu_reg(s, rt);
4162 
4163         if (is_zero) {
4164             int size = 4 << s->dcz_blocksize;
4165 
4166             if (s->ata) {
4167                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4168             }
4169             /*
4170              * The non-tags portion of STZGM is mostly like DC_ZVA,
4171              * except the alignment happens before the access.
4172              */
4173             clean_addr = clean_data_tbi(s, addr);
4174             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4175             gen_helper_dc_zva(cpu_env, clean_addr);
4176         } else if (s->ata) {
4177             if (is_load) {
4178                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4179             } else {
4180                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4181             }
4182         } else {
4183             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4184             int size = 4 << GMID_EL1_BS;
4185 
4186             clean_addr = clean_data_tbi(s, addr);
4187             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4188             gen_probe_access(s, clean_addr, acc, size);
4189 
4190             if (is_load) {
4191                 /* The result tags are zeros.  */
4192                 tcg_gen_movi_i64(tcg_rt, 0);
4193             }
4194         }
4195         return;
4196     }
4197 
4198     if (is_load) {
4199         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4200         tcg_rt = cpu_reg(s, rt);
4201         if (s->ata) {
4202             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4203         } else {
4204             /*
4205              * Tag access disabled: we must check for aborts on the load
4206              * load from [rn+offset], and then insert a 0 tag into rt.
4207              */
4208             clean_addr = clean_data_tbi(s, addr);
4209             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4210             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4211         }
4212     } else {
4213         tcg_rt = cpu_reg_sp(s, rt);
4214         if (!s->ata) {
4215             /*
4216              * For STG and ST2G, we need to check alignment and probe memory.
4217              * TODO: For STZG and STZ2G, we could rely on the stores below,
4218              * at least for system mode; user-only won't enforce alignment.
4219              */
4220             if (is_pair) {
4221                 gen_helper_st2g_stub(cpu_env, addr);
4222             } else {
4223                 gen_helper_stg_stub(cpu_env, addr);
4224             }
4225         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4226             if (is_pair) {
4227                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4228             } else {
4229                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4230             }
4231         } else {
4232             if (is_pair) {
4233                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4234             } else {
4235                 gen_helper_stg(cpu_env, addr, tcg_rt);
4236             }
4237         }
4238     }
4239 
4240     if (is_zero) {
4241         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4242         TCGv_i64 zero64 = tcg_constant_i64(0);
4243         TCGv_i128 zero128 = tcg_temp_new_i128();
4244         int mem_index = get_mem_index(s);
4245         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4246 
4247         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4248 
4249         /* This is 1 or 2 atomic 16-byte operations. */
4250         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4251         if (is_pair) {
4252             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4253             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4254         }
4255     }
4256 
4257     if (index != 0) {
4258         /* pre-index or post-index */
4259         if (index < 0) {
4260             /* post-index */
4261             tcg_gen_addi_i64(addr, addr, offset);
4262         }
4263         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4264     }
4265 }
4266 
4267 /* Loads and stores */
4268 static void disas_ldst(DisasContext *s, uint32_t insn)
4269 {
4270     switch (extract32(insn, 24, 6)) {
4271     case 0x08: /* Load/store exclusive */
4272         disas_ldst_excl(s, insn);
4273         break;
4274     case 0x18: case 0x1c: /* Load register (literal) */
4275         disas_ld_lit(s, insn);
4276         break;
4277     case 0x28: case 0x29:
4278     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4279         disas_ldst_pair(s, insn);
4280         break;
4281     case 0x38: case 0x39:
4282     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4283         disas_ldst_reg(s, insn);
4284         break;
4285     case 0x0c: /* AdvSIMD load/store multiple structures */
4286         disas_ldst_multiple_struct(s, insn);
4287         break;
4288     case 0x0d: /* AdvSIMD load/store single structure */
4289         disas_ldst_single_struct(s, insn);
4290         break;
4291     case 0x19:
4292         if (extract32(insn, 21, 1) != 0) {
4293             disas_ldst_tag(s, insn);
4294         } else if (extract32(insn, 10, 2) == 0) {
4295             disas_ldst_ldapr_stlr(s, insn);
4296         } else {
4297             unallocated_encoding(s);
4298         }
4299         break;
4300     default:
4301         unallocated_encoding(s);
4302         break;
4303     }
4304 }
4305 
4306 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4307 
4308 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4309                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4310 {
4311     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4312     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4313     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4314 
4315     fn(tcg_rd, tcg_rn, tcg_imm);
4316     if (!a->sf) {
4317         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4318     }
4319     return true;
4320 }
4321 
4322 /*
4323  * PC-rel. addressing
4324  */
4325 
4326 static bool trans_ADR(DisasContext *s, arg_ri *a)
4327 {
4328     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4329     return true;
4330 }
4331 
4332 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4333 {
4334     int64_t offset = (int64_t)a->imm << 12;
4335 
4336     /* The page offset is ok for CF_PCREL. */
4337     offset -= s->pc_curr & 0xfff;
4338     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4339     return true;
4340 }
4341 
4342 /*
4343  * Add/subtract (immediate)
4344  */
4345 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4346 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4347 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4348 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4349 
4350 /*
4351  * Add/subtract (immediate, with tags)
4352  */
4353 
4354 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4355                                       bool sub_op)
4356 {
4357     TCGv_i64 tcg_rn, tcg_rd;
4358     int imm;
4359 
4360     imm = a->uimm6 << LOG2_TAG_GRANULE;
4361     if (sub_op) {
4362         imm = -imm;
4363     }
4364 
4365     tcg_rn = cpu_reg_sp(s, a->rn);
4366     tcg_rd = cpu_reg_sp(s, a->rd);
4367 
4368     if (s->ata) {
4369         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4370                            tcg_constant_i32(imm),
4371                            tcg_constant_i32(a->uimm4));
4372     } else {
4373         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4374         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4375     }
4376     return true;
4377 }
4378 
4379 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4380 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4381 
4382 /* The input should be a value in the bottom e bits (with higher
4383  * bits zero); returns that value replicated into every element
4384  * of size e in a 64 bit integer.
4385  */
4386 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4387 {
4388     assert(e != 0);
4389     while (e < 64) {
4390         mask |= mask << e;
4391         e *= 2;
4392     }
4393     return mask;
4394 }
4395 
4396 /*
4397  * Logical (immediate)
4398  */
4399 
4400 /*
4401  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4402  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4403  * value (ie should cause a guest UNDEF exception), and true if they are
4404  * valid, in which case the decoded bit pattern is written to result.
4405  */
4406 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4407                             unsigned int imms, unsigned int immr)
4408 {
4409     uint64_t mask;
4410     unsigned e, levels, s, r;
4411     int len;
4412 
4413     assert(immn < 2 && imms < 64 && immr < 64);
4414 
4415     /* The bit patterns we create here are 64 bit patterns which
4416      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4417      * 64 bits each. Each element contains the same value: a run
4418      * of between 1 and e-1 non-zero bits, rotated within the
4419      * element by between 0 and e-1 bits.
4420      *
4421      * The element size and run length are encoded into immn (1 bit)
4422      * and imms (6 bits) as follows:
4423      * 64 bit elements: immn = 1, imms = <length of run - 1>
4424      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4425      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4426      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4427      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4428      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4429      * Notice that immn = 0, imms = 11111x is the only combination
4430      * not covered by one of the above options; this is reserved.
4431      * Further, <length of run - 1> all-ones is a reserved pattern.
4432      *
4433      * In all cases the rotation is by immr % e (and immr is 6 bits).
4434      */
4435 
4436     /* First determine the element size */
4437     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4438     if (len < 1) {
4439         /* This is the immn == 0, imms == 0x11111x case */
4440         return false;
4441     }
4442     e = 1 << len;
4443 
4444     levels = e - 1;
4445     s = imms & levels;
4446     r = immr & levels;
4447 
4448     if (s == levels) {
4449         /* <length of run - 1> mustn't be all-ones. */
4450         return false;
4451     }
4452 
4453     /* Create the value of one element: s+1 set bits rotated
4454      * by r within the element (which is e bits wide)...
4455      */
4456     mask = MAKE_64BIT_MASK(0, s + 1);
4457     if (r) {
4458         mask = (mask >> r) | (mask << (e - r));
4459         mask &= MAKE_64BIT_MASK(0, e);
4460     }
4461     /* ...then replicate the element over the whole 64 bit value */
4462     mask = bitfield_replicate(mask, e);
4463     *result = mask;
4464     return true;
4465 }
4466 
4467 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4468                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4469 {
4470     TCGv_i64 tcg_rd, tcg_rn;
4471     uint64_t imm;
4472 
4473     /* Some immediate field values are reserved. */
4474     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4475                                 extract32(a->dbm, 0, 6),
4476                                 extract32(a->dbm, 6, 6))) {
4477         return false;
4478     }
4479     if (!a->sf) {
4480         imm &= 0xffffffffull;
4481     }
4482 
4483     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4484     tcg_rn = cpu_reg(s, a->rn);
4485 
4486     fn(tcg_rd, tcg_rn, imm);
4487     if (set_cc) {
4488         gen_logic_CC(a->sf, tcg_rd);
4489     }
4490     if (!a->sf) {
4491         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4492     }
4493     return true;
4494 }
4495 
4496 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4497 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4498 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4499 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4500 
4501 /*
4502  * Move wide (immediate)
4503  */
4504 
4505 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4506 {
4507     int pos = a->hw << 4;
4508     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4509     return true;
4510 }
4511 
4512 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4513 {
4514     int pos = a->hw << 4;
4515     uint64_t imm = a->imm;
4516 
4517     imm = ~(imm << pos);
4518     if (!a->sf) {
4519         imm = (uint32_t)imm;
4520     }
4521     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4522     return true;
4523 }
4524 
4525 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4526 {
4527     int pos = a->hw << 4;
4528     TCGv_i64 tcg_rd, tcg_im;
4529 
4530     tcg_rd = cpu_reg(s, a->rd);
4531     tcg_im = tcg_constant_i64(a->imm);
4532     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4533     if (!a->sf) {
4534         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4535     }
4536     return true;
4537 }
4538 
4539 /*
4540  * Bitfield
4541  */
4542 
4543 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4544 {
4545     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4546     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4547     unsigned int bitsize = a->sf ? 64 : 32;
4548     unsigned int ri = a->immr;
4549     unsigned int si = a->imms;
4550     unsigned int pos, len;
4551 
4552     if (si >= ri) {
4553         /* Wd<s-r:0> = Wn<s:r> */
4554         len = (si - ri) + 1;
4555         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4556         if (!a->sf) {
4557             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4558         }
4559     } else {
4560         /* Wd<32+s-r,32-r> = Wn<s:0> */
4561         len = si + 1;
4562         pos = (bitsize - ri) & (bitsize - 1);
4563 
4564         if (len < ri) {
4565             /*
4566              * Sign extend the destination field from len to fill the
4567              * balance of the word.  Let the deposit below insert all
4568              * of those sign bits.
4569              */
4570             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4571             len = ri;
4572         }
4573 
4574         /*
4575          * We start with zero, and we haven't modified any bits outside
4576          * bitsize, therefore no final zero-extension is unneeded for !sf.
4577          */
4578         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4579     }
4580     return true;
4581 }
4582 
4583 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4584 {
4585     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4586     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4587     unsigned int bitsize = a->sf ? 64 : 32;
4588     unsigned int ri = a->immr;
4589     unsigned int si = a->imms;
4590     unsigned int pos, len;
4591 
4592     tcg_rd = cpu_reg(s, a->rd);
4593     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4594 
4595     if (si >= ri) {
4596         /* Wd<s-r:0> = Wn<s:r> */
4597         len = (si - ri) + 1;
4598         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4599     } else {
4600         /* Wd<32+s-r,32-r> = Wn<s:0> */
4601         len = si + 1;
4602         pos = (bitsize - ri) & (bitsize - 1);
4603         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4604     }
4605     return true;
4606 }
4607 
4608 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4609 {
4610     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4611     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4612     unsigned int bitsize = a->sf ? 64 : 32;
4613     unsigned int ri = a->immr;
4614     unsigned int si = a->imms;
4615     unsigned int pos, len;
4616 
4617     tcg_rd = cpu_reg(s, a->rd);
4618     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4619 
4620     if (si >= ri) {
4621         /* Wd<s-r:0> = Wn<s:r> */
4622         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4623         len = (si - ri) + 1;
4624         pos = 0;
4625     } else {
4626         /* Wd<32+s-r,32-r> = Wn<s:0> */
4627         len = si + 1;
4628         pos = (bitsize - ri) & (bitsize - 1);
4629     }
4630 
4631     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4632     if (!a->sf) {
4633         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4634     }
4635     return true;
4636 }
4637 
4638 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4639 {
4640     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4641 
4642     tcg_rd = cpu_reg(s, a->rd);
4643 
4644     if (unlikely(a->imm == 0)) {
4645         /*
4646          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4647          * so an extract from bit 0 is a special case.
4648          */
4649         if (a->sf) {
4650             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4651         } else {
4652             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4653         }
4654     } else {
4655         tcg_rm = cpu_reg(s, a->rm);
4656         tcg_rn = cpu_reg(s, a->rn);
4657 
4658         if (a->sf) {
4659             /* Specialization to ROR happens in EXTRACT2.  */
4660             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4661         } else {
4662             TCGv_i32 t0 = tcg_temp_new_i32();
4663 
4664             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4665             if (a->rm == a->rn) {
4666                 tcg_gen_rotri_i32(t0, t0, a->imm);
4667             } else {
4668                 TCGv_i32 t1 = tcg_temp_new_i32();
4669                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4670                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4671             }
4672             tcg_gen_extu_i32_i64(tcg_rd, t0);
4673         }
4674     }
4675     return true;
4676 }
4677 
4678 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4679  * Note that it is the caller's responsibility to ensure that the
4680  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4681  * mandated semantics for out of range shifts.
4682  */
4683 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4684                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4685 {
4686     switch (shift_type) {
4687     case A64_SHIFT_TYPE_LSL:
4688         tcg_gen_shl_i64(dst, src, shift_amount);
4689         break;
4690     case A64_SHIFT_TYPE_LSR:
4691         tcg_gen_shr_i64(dst, src, shift_amount);
4692         break;
4693     case A64_SHIFT_TYPE_ASR:
4694         if (!sf) {
4695             tcg_gen_ext32s_i64(dst, src);
4696         }
4697         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4698         break;
4699     case A64_SHIFT_TYPE_ROR:
4700         if (sf) {
4701             tcg_gen_rotr_i64(dst, src, shift_amount);
4702         } else {
4703             TCGv_i32 t0, t1;
4704             t0 = tcg_temp_new_i32();
4705             t1 = tcg_temp_new_i32();
4706             tcg_gen_extrl_i64_i32(t0, src);
4707             tcg_gen_extrl_i64_i32(t1, shift_amount);
4708             tcg_gen_rotr_i32(t0, t0, t1);
4709             tcg_gen_extu_i32_i64(dst, t0);
4710         }
4711         break;
4712     default:
4713         assert(FALSE); /* all shift types should be handled */
4714         break;
4715     }
4716 
4717     if (!sf) { /* zero extend final result */
4718         tcg_gen_ext32u_i64(dst, dst);
4719     }
4720 }
4721 
4722 /* Shift a TCGv src by immediate, put result in dst.
4723  * The shift amount must be in range (this should always be true as the
4724  * relevant instructions will UNDEF on bad shift immediates).
4725  */
4726 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4727                           enum a64_shift_type shift_type, unsigned int shift_i)
4728 {
4729     assert(shift_i < (sf ? 64 : 32));
4730 
4731     if (shift_i == 0) {
4732         tcg_gen_mov_i64(dst, src);
4733     } else {
4734         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4735     }
4736 }
4737 
4738 /* Logical (shifted register)
4739  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4740  * +----+-----+-----------+-------+---+------+--------+------+------+
4741  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4742  * +----+-----+-----------+-------+---+------+--------+------+------+
4743  */
4744 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4745 {
4746     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4747     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4748 
4749     sf = extract32(insn, 31, 1);
4750     opc = extract32(insn, 29, 2);
4751     shift_type = extract32(insn, 22, 2);
4752     invert = extract32(insn, 21, 1);
4753     rm = extract32(insn, 16, 5);
4754     shift_amount = extract32(insn, 10, 6);
4755     rn = extract32(insn, 5, 5);
4756     rd = extract32(insn, 0, 5);
4757 
4758     if (!sf && (shift_amount & (1 << 5))) {
4759         unallocated_encoding(s);
4760         return;
4761     }
4762 
4763     tcg_rd = cpu_reg(s, rd);
4764 
4765     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4766         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4767          * register-register MOV and MVN, so it is worth special casing.
4768          */
4769         tcg_rm = cpu_reg(s, rm);
4770         if (invert) {
4771             tcg_gen_not_i64(tcg_rd, tcg_rm);
4772             if (!sf) {
4773                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4774             }
4775         } else {
4776             if (sf) {
4777                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4778             } else {
4779                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4780             }
4781         }
4782         return;
4783     }
4784 
4785     tcg_rm = read_cpu_reg(s, rm, sf);
4786 
4787     if (shift_amount) {
4788         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4789     }
4790 
4791     tcg_rn = cpu_reg(s, rn);
4792 
4793     switch (opc | (invert << 2)) {
4794     case 0: /* AND */
4795     case 3: /* ANDS */
4796         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4797         break;
4798     case 1: /* ORR */
4799         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4800         break;
4801     case 2: /* EOR */
4802         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4803         break;
4804     case 4: /* BIC */
4805     case 7: /* BICS */
4806         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4807         break;
4808     case 5: /* ORN */
4809         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4810         break;
4811     case 6: /* EON */
4812         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4813         break;
4814     default:
4815         assert(FALSE);
4816         break;
4817     }
4818 
4819     if (!sf) {
4820         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4821     }
4822 
4823     if (opc == 3) {
4824         gen_logic_CC(sf, tcg_rd);
4825     }
4826 }
4827 
4828 /*
4829  * Add/subtract (extended register)
4830  *
4831  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4832  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4833  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4834  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4835  *
4836  *  sf: 0 -> 32bit, 1 -> 64bit
4837  *  op: 0 -> add  , 1 -> sub
4838  *   S: 1 -> set flags
4839  * opt: 00
4840  * option: extension type (see DecodeRegExtend)
4841  * imm3: optional shift to Rm
4842  *
4843  * Rd = Rn + LSL(extend(Rm), amount)
4844  */
4845 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4846 {
4847     int rd = extract32(insn, 0, 5);
4848     int rn = extract32(insn, 5, 5);
4849     int imm3 = extract32(insn, 10, 3);
4850     int option = extract32(insn, 13, 3);
4851     int rm = extract32(insn, 16, 5);
4852     int opt = extract32(insn, 22, 2);
4853     bool setflags = extract32(insn, 29, 1);
4854     bool sub_op = extract32(insn, 30, 1);
4855     bool sf = extract32(insn, 31, 1);
4856 
4857     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4858     TCGv_i64 tcg_rd;
4859     TCGv_i64 tcg_result;
4860 
4861     if (imm3 > 4 || opt != 0) {
4862         unallocated_encoding(s);
4863         return;
4864     }
4865 
4866     /* non-flag setting ops may use SP */
4867     if (!setflags) {
4868         tcg_rd = cpu_reg_sp(s, rd);
4869     } else {
4870         tcg_rd = cpu_reg(s, rd);
4871     }
4872     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4873 
4874     tcg_rm = read_cpu_reg(s, rm, sf);
4875     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4876 
4877     tcg_result = tcg_temp_new_i64();
4878 
4879     if (!setflags) {
4880         if (sub_op) {
4881             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4882         } else {
4883             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4884         }
4885     } else {
4886         if (sub_op) {
4887             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4888         } else {
4889             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4890         }
4891     }
4892 
4893     if (sf) {
4894         tcg_gen_mov_i64(tcg_rd, tcg_result);
4895     } else {
4896         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4897     }
4898 }
4899 
4900 /*
4901  * Add/subtract (shifted register)
4902  *
4903  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4904  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4905  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4906  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4907  *
4908  *    sf: 0 -> 32bit, 1 -> 64bit
4909  *    op: 0 -> add  , 1 -> sub
4910  *     S: 1 -> set flags
4911  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4912  *  imm6: Shift amount to apply to Rm before the add/sub
4913  */
4914 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4915 {
4916     int rd = extract32(insn, 0, 5);
4917     int rn = extract32(insn, 5, 5);
4918     int imm6 = extract32(insn, 10, 6);
4919     int rm = extract32(insn, 16, 5);
4920     int shift_type = extract32(insn, 22, 2);
4921     bool setflags = extract32(insn, 29, 1);
4922     bool sub_op = extract32(insn, 30, 1);
4923     bool sf = extract32(insn, 31, 1);
4924 
4925     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4926     TCGv_i64 tcg_rn, tcg_rm;
4927     TCGv_i64 tcg_result;
4928 
4929     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4930         unallocated_encoding(s);
4931         return;
4932     }
4933 
4934     tcg_rn = read_cpu_reg(s, rn, sf);
4935     tcg_rm = read_cpu_reg(s, rm, sf);
4936 
4937     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4938 
4939     tcg_result = tcg_temp_new_i64();
4940 
4941     if (!setflags) {
4942         if (sub_op) {
4943             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4944         } else {
4945             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4946         }
4947     } else {
4948         if (sub_op) {
4949             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4950         } else {
4951             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4952         }
4953     }
4954 
4955     if (sf) {
4956         tcg_gen_mov_i64(tcg_rd, tcg_result);
4957     } else {
4958         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4959     }
4960 }
4961 
4962 /* Data-processing (3 source)
4963  *
4964  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4965  *  +--+------+-----------+------+------+----+------+------+------+
4966  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4967  *  +--+------+-----------+------+------+----+------+------+------+
4968  */
4969 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4970 {
4971     int rd = extract32(insn, 0, 5);
4972     int rn = extract32(insn, 5, 5);
4973     int ra = extract32(insn, 10, 5);
4974     int rm = extract32(insn, 16, 5);
4975     int op_id = (extract32(insn, 29, 3) << 4) |
4976         (extract32(insn, 21, 3) << 1) |
4977         extract32(insn, 15, 1);
4978     bool sf = extract32(insn, 31, 1);
4979     bool is_sub = extract32(op_id, 0, 1);
4980     bool is_high = extract32(op_id, 2, 1);
4981     bool is_signed = false;
4982     TCGv_i64 tcg_op1;
4983     TCGv_i64 tcg_op2;
4984     TCGv_i64 tcg_tmp;
4985 
4986     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4987     switch (op_id) {
4988     case 0x42: /* SMADDL */
4989     case 0x43: /* SMSUBL */
4990     case 0x44: /* SMULH */
4991         is_signed = true;
4992         break;
4993     case 0x0: /* MADD (32bit) */
4994     case 0x1: /* MSUB (32bit) */
4995     case 0x40: /* MADD (64bit) */
4996     case 0x41: /* MSUB (64bit) */
4997     case 0x4a: /* UMADDL */
4998     case 0x4b: /* UMSUBL */
4999     case 0x4c: /* UMULH */
5000         break;
5001     default:
5002         unallocated_encoding(s);
5003         return;
5004     }
5005 
5006     if (is_high) {
5007         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5008         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5009         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5010         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5011 
5012         if (is_signed) {
5013             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5014         } else {
5015             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5016         }
5017         return;
5018     }
5019 
5020     tcg_op1 = tcg_temp_new_i64();
5021     tcg_op2 = tcg_temp_new_i64();
5022     tcg_tmp = tcg_temp_new_i64();
5023 
5024     if (op_id < 0x42) {
5025         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5026         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5027     } else {
5028         if (is_signed) {
5029             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5030             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5031         } else {
5032             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5033             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5034         }
5035     }
5036 
5037     if (ra == 31 && !is_sub) {
5038         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5039         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5040     } else {
5041         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5042         if (is_sub) {
5043             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5044         } else {
5045             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5046         }
5047     }
5048 
5049     if (!sf) {
5050         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5051     }
5052 }
5053 
5054 /* Add/subtract (with carry)
5055  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5056  * +--+--+--+------------------------+------+-------------+------+-----+
5057  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5058  * +--+--+--+------------------------+------+-------------+------+-----+
5059  */
5060 
5061 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5062 {
5063     unsigned int sf, op, setflags, rm, rn, rd;
5064     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5065 
5066     sf = extract32(insn, 31, 1);
5067     op = extract32(insn, 30, 1);
5068     setflags = extract32(insn, 29, 1);
5069     rm = extract32(insn, 16, 5);
5070     rn = extract32(insn, 5, 5);
5071     rd = extract32(insn, 0, 5);
5072 
5073     tcg_rd = cpu_reg(s, rd);
5074     tcg_rn = cpu_reg(s, rn);
5075 
5076     if (op) {
5077         tcg_y = tcg_temp_new_i64();
5078         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5079     } else {
5080         tcg_y = cpu_reg(s, rm);
5081     }
5082 
5083     if (setflags) {
5084         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5085     } else {
5086         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5087     }
5088 }
5089 
5090 /*
5091  * Rotate right into flags
5092  *  31 30 29                21       15          10      5  4      0
5093  * +--+--+--+-----------------+--------+-----------+------+--+------+
5094  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5095  * +--+--+--+-----------------+--------+-----------+------+--+------+
5096  */
5097 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5098 {
5099     int mask = extract32(insn, 0, 4);
5100     int o2 = extract32(insn, 4, 1);
5101     int rn = extract32(insn, 5, 5);
5102     int imm6 = extract32(insn, 15, 6);
5103     int sf_op_s = extract32(insn, 29, 3);
5104     TCGv_i64 tcg_rn;
5105     TCGv_i32 nzcv;
5106 
5107     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5108         unallocated_encoding(s);
5109         return;
5110     }
5111 
5112     tcg_rn = read_cpu_reg(s, rn, 1);
5113     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5114 
5115     nzcv = tcg_temp_new_i32();
5116     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5117 
5118     if (mask & 8) { /* N */
5119         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5120     }
5121     if (mask & 4) { /* Z */
5122         tcg_gen_not_i32(cpu_ZF, nzcv);
5123         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5124     }
5125     if (mask & 2) { /* C */
5126         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5127     }
5128     if (mask & 1) { /* V */
5129         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5130     }
5131 }
5132 
5133 /*
5134  * Evaluate into flags
5135  *  31 30 29                21        15   14        10      5  4      0
5136  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5137  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5138  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5139  */
5140 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5141 {
5142     int o3_mask = extract32(insn, 0, 5);
5143     int rn = extract32(insn, 5, 5);
5144     int o2 = extract32(insn, 15, 6);
5145     int sz = extract32(insn, 14, 1);
5146     int sf_op_s = extract32(insn, 29, 3);
5147     TCGv_i32 tmp;
5148     int shift;
5149 
5150     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5151         !dc_isar_feature(aa64_condm_4, s)) {
5152         unallocated_encoding(s);
5153         return;
5154     }
5155     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5156 
5157     tmp = tcg_temp_new_i32();
5158     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5159     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5160     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5161     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5162     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5163 }
5164 
5165 /* Conditional compare (immediate / register)
5166  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5167  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5168  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5169  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5170  *        [1]                             y                [0]       [0]
5171  */
5172 static void disas_cc(DisasContext *s, uint32_t insn)
5173 {
5174     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5175     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5176     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5177     DisasCompare c;
5178 
5179     if (!extract32(insn, 29, 1)) {
5180         unallocated_encoding(s);
5181         return;
5182     }
5183     if (insn & (1 << 10 | 1 << 4)) {
5184         unallocated_encoding(s);
5185         return;
5186     }
5187     sf = extract32(insn, 31, 1);
5188     op = extract32(insn, 30, 1);
5189     is_imm = extract32(insn, 11, 1);
5190     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5191     cond = extract32(insn, 12, 4);
5192     rn = extract32(insn, 5, 5);
5193     nzcv = extract32(insn, 0, 4);
5194 
5195     /* Set T0 = !COND.  */
5196     tcg_t0 = tcg_temp_new_i32();
5197     arm_test_cc(&c, cond);
5198     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5199 
5200     /* Load the arguments for the new comparison.  */
5201     if (is_imm) {
5202         tcg_y = tcg_temp_new_i64();
5203         tcg_gen_movi_i64(tcg_y, y);
5204     } else {
5205         tcg_y = cpu_reg(s, y);
5206     }
5207     tcg_rn = cpu_reg(s, rn);
5208 
5209     /* Set the flags for the new comparison.  */
5210     tcg_tmp = tcg_temp_new_i64();
5211     if (op) {
5212         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5213     } else {
5214         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5215     }
5216 
5217     /* If COND was false, force the flags to #nzcv.  Compute two masks
5218      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5219      * For tcg hosts that support ANDC, we can make do with just T1.
5220      * In either case, allow the tcg optimizer to delete any unused mask.
5221      */
5222     tcg_t1 = tcg_temp_new_i32();
5223     tcg_t2 = tcg_temp_new_i32();
5224     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5225     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5226 
5227     if (nzcv & 8) { /* N */
5228         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5229     } else {
5230         if (TCG_TARGET_HAS_andc_i32) {
5231             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5232         } else {
5233             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5234         }
5235     }
5236     if (nzcv & 4) { /* Z */
5237         if (TCG_TARGET_HAS_andc_i32) {
5238             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5239         } else {
5240             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5241         }
5242     } else {
5243         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5244     }
5245     if (nzcv & 2) { /* C */
5246         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5247     } else {
5248         if (TCG_TARGET_HAS_andc_i32) {
5249             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5250         } else {
5251             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5252         }
5253     }
5254     if (nzcv & 1) { /* V */
5255         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5256     } else {
5257         if (TCG_TARGET_HAS_andc_i32) {
5258             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5259         } else {
5260             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5261         }
5262     }
5263 }
5264 
5265 /* Conditional select
5266  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5267  * +----+----+---+-----------------+------+------+-----+------+------+
5268  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5269  * +----+----+---+-----------------+------+------+-----+------+------+
5270  */
5271 static void disas_cond_select(DisasContext *s, uint32_t insn)
5272 {
5273     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5274     TCGv_i64 tcg_rd, zero;
5275     DisasCompare64 c;
5276 
5277     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5278         /* S == 1 or op2<1> == 1 */
5279         unallocated_encoding(s);
5280         return;
5281     }
5282     sf = extract32(insn, 31, 1);
5283     else_inv = extract32(insn, 30, 1);
5284     rm = extract32(insn, 16, 5);
5285     cond = extract32(insn, 12, 4);
5286     else_inc = extract32(insn, 10, 1);
5287     rn = extract32(insn, 5, 5);
5288     rd = extract32(insn, 0, 5);
5289 
5290     tcg_rd = cpu_reg(s, rd);
5291 
5292     a64_test_cc(&c, cond);
5293     zero = tcg_constant_i64(0);
5294 
5295     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5296         /* CSET & CSETM.  */
5297         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5298         if (else_inv) {
5299             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5300         }
5301     } else {
5302         TCGv_i64 t_true = cpu_reg(s, rn);
5303         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5304         if (else_inv && else_inc) {
5305             tcg_gen_neg_i64(t_false, t_false);
5306         } else if (else_inv) {
5307             tcg_gen_not_i64(t_false, t_false);
5308         } else if (else_inc) {
5309             tcg_gen_addi_i64(t_false, t_false, 1);
5310         }
5311         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5312     }
5313 
5314     if (!sf) {
5315         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5316     }
5317 }
5318 
5319 static void handle_clz(DisasContext *s, unsigned int sf,
5320                        unsigned int rn, unsigned int rd)
5321 {
5322     TCGv_i64 tcg_rd, tcg_rn;
5323     tcg_rd = cpu_reg(s, rd);
5324     tcg_rn = cpu_reg(s, rn);
5325 
5326     if (sf) {
5327         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5328     } else {
5329         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5330         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5331         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5332         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5333     }
5334 }
5335 
5336 static void handle_cls(DisasContext *s, unsigned int sf,
5337                        unsigned int rn, unsigned int rd)
5338 {
5339     TCGv_i64 tcg_rd, tcg_rn;
5340     tcg_rd = cpu_reg(s, rd);
5341     tcg_rn = cpu_reg(s, rn);
5342 
5343     if (sf) {
5344         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5345     } else {
5346         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5347         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5348         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5349         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5350     }
5351 }
5352 
5353 static void handle_rbit(DisasContext *s, unsigned int sf,
5354                         unsigned int rn, unsigned int rd)
5355 {
5356     TCGv_i64 tcg_rd, tcg_rn;
5357     tcg_rd = cpu_reg(s, rd);
5358     tcg_rn = cpu_reg(s, rn);
5359 
5360     if (sf) {
5361         gen_helper_rbit64(tcg_rd, tcg_rn);
5362     } else {
5363         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5364         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5365         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5366         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5367     }
5368 }
5369 
5370 /* REV with sf==1, opcode==3 ("REV64") */
5371 static void handle_rev64(DisasContext *s, unsigned int sf,
5372                          unsigned int rn, unsigned int rd)
5373 {
5374     if (!sf) {
5375         unallocated_encoding(s);
5376         return;
5377     }
5378     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5379 }
5380 
5381 /* REV with sf==0, opcode==2
5382  * REV32 (sf==1, opcode==2)
5383  */
5384 static void handle_rev32(DisasContext *s, unsigned int sf,
5385                          unsigned int rn, unsigned int rd)
5386 {
5387     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5388     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5389 
5390     if (sf) {
5391         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5392         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5393     } else {
5394         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5395     }
5396 }
5397 
5398 /* REV16 (opcode==1) */
5399 static void handle_rev16(DisasContext *s, unsigned int sf,
5400                          unsigned int rn, unsigned int rd)
5401 {
5402     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5403     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5404     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5405     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5406 
5407     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5408     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5409     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5410     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5411     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5412 }
5413 
5414 /* Data-processing (1 source)
5415  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5416  * +----+---+---+-----------------+---------+--------+------+------+
5417  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5418  * +----+---+---+-----------------+---------+--------+------+------+
5419  */
5420 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5421 {
5422     unsigned int sf, opcode, opcode2, rn, rd;
5423     TCGv_i64 tcg_rd;
5424 
5425     if (extract32(insn, 29, 1)) {
5426         unallocated_encoding(s);
5427         return;
5428     }
5429 
5430     sf = extract32(insn, 31, 1);
5431     opcode = extract32(insn, 10, 6);
5432     opcode2 = extract32(insn, 16, 5);
5433     rn = extract32(insn, 5, 5);
5434     rd = extract32(insn, 0, 5);
5435 
5436 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5437 
5438     switch (MAP(sf, opcode2, opcode)) {
5439     case MAP(0, 0x00, 0x00): /* RBIT */
5440     case MAP(1, 0x00, 0x00):
5441         handle_rbit(s, sf, rn, rd);
5442         break;
5443     case MAP(0, 0x00, 0x01): /* REV16 */
5444     case MAP(1, 0x00, 0x01):
5445         handle_rev16(s, sf, rn, rd);
5446         break;
5447     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5448     case MAP(1, 0x00, 0x02):
5449         handle_rev32(s, sf, rn, rd);
5450         break;
5451     case MAP(1, 0x00, 0x03): /* REV64 */
5452         handle_rev64(s, sf, rn, rd);
5453         break;
5454     case MAP(0, 0x00, 0x04): /* CLZ */
5455     case MAP(1, 0x00, 0x04):
5456         handle_clz(s, sf, rn, rd);
5457         break;
5458     case MAP(0, 0x00, 0x05): /* CLS */
5459     case MAP(1, 0x00, 0x05):
5460         handle_cls(s, sf, rn, rd);
5461         break;
5462     case MAP(1, 0x01, 0x00): /* PACIA */
5463         if (s->pauth_active) {
5464             tcg_rd = cpu_reg(s, rd);
5465             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5466         } else if (!dc_isar_feature(aa64_pauth, s)) {
5467             goto do_unallocated;
5468         }
5469         break;
5470     case MAP(1, 0x01, 0x01): /* PACIB */
5471         if (s->pauth_active) {
5472             tcg_rd = cpu_reg(s, rd);
5473             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5474         } else if (!dc_isar_feature(aa64_pauth, s)) {
5475             goto do_unallocated;
5476         }
5477         break;
5478     case MAP(1, 0x01, 0x02): /* PACDA */
5479         if (s->pauth_active) {
5480             tcg_rd = cpu_reg(s, rd);
5481             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5482         } else if (!dc_isar_feature(aa64_pauth, s)) {
5483             goto do_unallocated;
5484         }
5485         break;
5486     case MAP(1, 0x01, 0x03): /* PACDB */
5487         if (s->pauth_active) {
5488             tcg_rd = cpu_reg(s, rd);
5489             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5490         } else if (!dc_isar_feature(aa64_pauth, s)) {
5491             goto do_unallocated;
5492         }
5493         break;
5494     case MAP(1, 0x01, 0x04): /* AUTIA */
5495         if (s->pauth_active) {
5496             tcg_rd = cpu_reg(s, rd);
5497             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5498         } else if (!dc_isar_feature(aa64_pauth, s)) {
5499             goto do_unallocated;
5500         }
5501         break;
5502     case MAP(1, 0x01, 0x05): /* AUTIB */
5503         if (s->pauth_active) {
5504             tcg_rd = cpu_reg(s, rd);
5505             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5506         } else if (!dc_isar_feature(aa64_pauth, s)) {
5507             goto do_unallocated;
5508         }
5509         break;
5510     case MAP(1, 0x01, 0x06): /* AUTDA */
5511         if (s->pauth_active) {
5512             tcg_rd = cpu_reg(s, rd);
5513             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5514         } else if (!dc_isar_feature(aa64_pauth, s)) {
5515             goto do_unallocated;
5516         }
5517         break;
5518     case MAP(1, 0x01, 0x07): /* AUTDB */
5519         if (s->pauth_active) {
5520             tcg_rd = cpu_reg(s, rd);
5521             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5522         } else if (!dc_isar_feature(aa64_pauth, s)) {
5523             goto do_unallocated;
5524         }
5525         break;
5526     case MAP(1, 0x01, 0x08): /* PACIZA */
5527         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5528             goto do_unallocated;
5529         } else if (s->pauth_active) {
5530             tcg_rd = cpu_reg(s, rd);
5531             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5532         }
5533         break;
5534     case MAP(1, 0x01, 0x09): /* PACIZB */
5535         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5536             goto do_unallocated;
5537         } else if (s->pauth_active) {
5538             tcg_rd = cpu_reg(s, rd);
5539             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5540         }
5541         break;
5542     case MAP(1, 0x01, 0x0a): /* PACDZA */
5543         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5544             goto do_unallocated;
5545         } else if (s->pauth_active) {
5546             tcg_rd = cpu_reg(s, rd);
5547             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5548         }
5549         break;
5550     case MAP(1, 0x01, 0x0b): /* PACDZB */
5551         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5552             goto do_unallocated;
5553         } else if (s->pauth_active) {
5554             tcg_rd = cpu_reg(s, rd);
5555             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5556         }
5557         break;
5558     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5559         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5560             goto do_unallocated;
5561         } else if (s->pauth_active) {
5562             tcg_rd = cpu_reg(s, rd);
5563             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5564         }
5565         break;
5566     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5567         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5568             goto do_unallocated;
5569         } else if (s->pauth_active) {
5570             tcg_rd = cpu_reg(s, rd);
5571             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5572         }
5573         break;
5574     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5575         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5576             goto do_unallocated;
5577         } else if (s->pauth_active) {
5578             tcg_rd = cpu_reg(s, rd);
5579             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5580         }
5581         break;
5582     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5583         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5584             goto do_unallocated;
5585         } else if (s->pauth_active) {
5586             tcg_rd = cpu_reg(s, rd);
5587             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5588         }
5589         break;
5590     case MAP(1, 0x01, 0x10): /* XPACI */
5591         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5592             goto do_unallocated;
5593         } else if (s->pauth_active) {
5594             tcg_rd = cpu_reg(s, rd);
5595             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5596         }
5597         break;
5598     case MAP(1, 0x01, 0x11): /* XPACD */
5599         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5600             goto do_unallocated;
5601         } else if (s->pauth_active) {
5602             tcg_rd = cpu_reg(s, rd);
5603             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5604         }
5605         break;
5606     default:
5607     do_unallocated:
5608         unallocated_encoding(s);
5609         break;
5610     }
5611 
5612 #undef MAP
5613 }
5614 
5615 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5616                        unsigned int rm, unsigned int rn, unsigned int rd)
5617 {
5618     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5619     tcg_rd = cpu_reg(s, rd);
5620 
5621     if (!sf && is_signed) {
5622         tcg_n = tcg_temp_new_i64();
5623         tcg_m = tcg_temp_new_i64();
5624         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5625         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5626     } else {
5627         tcg_n = read_cpu_reg(s, rn, sf);
5628         tcg_m = read_cpu_reg(s, rm, sf);
5629     }
5630 
5631     if (is_signed) {
5632         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5633     } else {
5634         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5635     }
5636 
5637     if (!sf) { /* zero extend final result */
5638         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5639     }
5640 }
5641 
5642 /* LSLV, LSRV, ASRV, RORV */
5643 static void handle_shift_reg(DisasContext *s,
5644                              enum a64_shift_type shift_type, unsigned int sf,
5645                              unsigned int rm, unsigned int rn, unsigned int rd)
5646 {
5647     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5648     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5649     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5650 
5651     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5652     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5653 }
5654 
5655 /* CRC32[BHWX], CRC32C[BHWX] */
5656 static void handle_crc32(DisasContext *s,
5657                          unsigned int sf, unsigned int sz, bool crc32c,
5658                          unsigned int rm, unsigned int rn, unsigned int rd)
5659 {
5660     TCGv_i64 tcg_acc, tcg_val;
5661     TCGv_i32 tcg_bytes;
5662 
5663     if (!dc_isar_feature(aa64_crc32, s)
5664         || (sf == 1 && sz != 3)
5665         || (sf == 0 && sz == 3)) {
5666         unallocated_encoding(s);
5667         return;
5668     }
5669 
5670     if (sz == 3) {
5671         tcg_val = cpu_reg(s, rm);
5672     } else {
5673         uint64_t mask;
5674         switch (sz) {
5675         case 0:
5676             mask = 0xFF;
5677             break;
5678         case 1:
5679             mask = 0xFFFF;
5680             break;
5681         case 2:
5682             mask = 0xFFFFFFFF;
5683             break;
5684         default:
5685             g_assert_not_reached();
5686         }
5687         tcg_val = tcg_temp_new_i64();
5688         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5689     }
5690 
5691     tcg_acc = cpu_reg(s, rn);
5692     tcg_bytes = tcg_constant_i32(1 << sz);
5693 
5694     if (crc32c) {
5695         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5696     } else {
5697         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5698     }
5699 }
5700 
5701 /* Data-processing (2 source)
5702  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5703  * +----+---+---+-----------------+------+--------+------+------+
5704  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5705  * +----+---+---+-----------------+------+--------+------+------+
5706  */
5707 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5708 {
5709     unsigned int sf, rm, opcode, rn, rd, setflag;
5710     sf = extract32(insn, 31, 1);
5711     setflag = extract32(insn, 29, 1);
5712     rm = extract32(insn, 16, 5);
5713     opcode = extract32(insn, 10, 6);
5714     rn = extract32(insn, 5, 5);
5715     rd = extract32(insn, 0, 5);
5716 
5717     if (setflag && opcode != 0) {
5718         unallocated_encoding(s);
5719         return;
5720     }
5721 
5722     switch (opcode) {
5723     case 0: /* SUBP(S) */
5724         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5725             goto do_unallocated;
5726         } else {
5727             TCGv_i64 tcg_n, tcg_m, tcg_d;
5728 
5729             tcg_n = read_cpu_reg_sp(s, rn, true);
5730             tcg_m = read_cpu_reg_sp(s, rm, true);
5731             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5732             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5733             tcg_d = cpu_reg(s, rd);
5734 
5735             if (setflag) {
5736                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5737             } else {
5738                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5739             }
5740         }
5741         break;
5742     case 2: /* UDIV */
5743         handle_div(s, false, sf, rm, rn, rd);
5744         break;
5745     case 3: /* SDIV */
5746         handle_div(s, true, sf, rm, rn, rd);
5747         break;
5748     case 4: /* IRG */
5749         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5750             goto do_unallocated;
5751         }
5752         if (s->ata) {
5753             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5754                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5755         } else {
5756             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5757                                              cpu_reg_sp(s, rn));
5758         }
5759         break;
5760     case 5: /* GMI */
5761         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5762             goto do_unallocated;
5763         } else {
5764             TCGv_i64 t = tcg_temp_new_i64();
5765 
5766             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5767             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5768             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5769         }
5770         break;
5771     case 8: /* LSLV */
5772         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5773         break;
5774     case 9: /* LSRV */
5775         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5776         break;
5777     case 10: /* ASRV */
5778         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5779         break;
5780     case 11: /* RORV */
5781         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5782         break;
5783     case 12: /* PACGA */
5784         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5785             goto do_unallocated;
5786         }
5787         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5788                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5789         break;
5790     case 16:
5791     case 17:
5792     case 18:
5793     case 19:
5794     case 20:
5795     case 21:
5796     case 22:
5797     case 23: /* CRC32 */
5798     {
5799         int sz = extract32(opcode, 0, 2);
5800         bool crc32c = extract32(opcode, 2, 1);
5801         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5802         break;
5803     }
5804     default:
5805     do_unallocated:
5806         unallocated_encoding(s);
5807         break;
5808     }
5809 }
5810 
5811 /*
5812  * Data processing - register
5813  *  31  30 29  28      25    21  20  16      10         0
5814  * +--+---+--+---+-------+-----+-------+-------+---------+
5815  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5816  * +--+---+--+---+-------+-----+-------+-------+---------+
5817  */
5818 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5819 {
5820     int op0 = extract32(insn, 30, 1);
5821     int op1 = extract32(insn, 28, 1);
5822     int op2 = extract32(insn, 21, 4);
5823     int op3 = extract32(insn, 10, 6);
5824 
5825     if (!op1) {
5826         if (op2 & 8) {
5827             if (op2 & 1) {
5828                 /* Add/sub (extended register) */
5829                 disas_add_sub_ext_reg(s, insn);
5830             } else {
5831                 /* Add/sub (shifted register) */
5832                 disas_add_sub_reg(s, insn);
5833             }
5834         } else {
5835             /* Logical (shifted register) */
5836             disas_logic_reg(s, insn);
5837         }
5838         return;
5839     }
5840 
5841     switch (op2) {
5842     case 0x0:
5843         switch (op3) {
5844         case 0x00: /* Add/subtract (with carry) */
5845             disas_adc_sbc(s, insn);
5846             break;
5847 
5848         case 0x01: /* Rotate right into flags */
5849         case 0x21:
5850             disas_rotate_right_into_flags(s, insn);
5851             break;
5852 
5853         case 0x02: /* Evaluate into flags */
5854         case 0x12:
5855         case 0x22:
5856         case 0x32:
5857             disas_evaluate_into_flags(s, insn);
5858             break;
5859 
5860         default:
5861             goto do_unallocated;
5862         }
5863         break;
5864 
5865     case 0x2: /* Conditional compare */
5866         disas_cc(s, insn); /* both imm and reg forms */
5867         break;
5868 
5869     case 0x4: /* Conditional select */
5870         disas_cond_select(s, insn);
5871         break;
5872 
5873     case 0x6: /* Data-processing */
5874         if (op0) {    /* (1 source) */
5875             disas_data_proc_1src(s, insn);
5876         } else {      /* (2 source) */
5877             disas_data_proc_2src(s, insn);
5878         }
5879         break;
5880     case 0x8 ... 0xf: /* (3 source) */
5881         disas_data_proc_3src(s, insn);
5882         break;
5883 
5884     default:
5885     do_unallocated:
5886         unallocated_encoding(s);
5887         break;
5888     }
5889 }
5890 
5891 static void handle_fp_compare(DisasContext *s, int size,
5892                               unsigned int rn, unsigned int rm,
5893                               bool cmp_with_zero, bool signal_all_nans)
5894 {
5895     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5896     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5897 
5898     if (size == MO_64) {
5899         TCGv_i64 tcg_vn, tcg_vm;
5900 
5901         tcg_vn = read_fp_dreg(s, rn);
5902         if (cmp_with_zero) {
5903             tcg_vm = tcg_constant_i64(0);
5904         } else {
5905             tcg_vm = read_fp_dreg(s, rm);
5906         }
5907         if (signal_all_nans) {
5908             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5909         } else {
5910             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5911         }
5912     } else {
5913         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5914         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5915 
5916         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5917         if (cmp_with_zero) {
5918             tcg_gen_movi_i32(tcg_vm, 0);
5919         } else {
5920             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5921         }
5922 
5923         switch (size) {
5924         case MO_32:
5925             if (signal_all_nans) {
5926                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5927             } else {
5928                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5929             }
5930             break;
5931         case MO_16:
5932             if (signal_all_nans) {
5933                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5934             } else {
5935                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5936             }
5937             break;
5938         default:
5939             g_assert_not_reached();
5940         }
5941     }
5942 
5943     gen_set_nzcv(tcg_flags);
5944 }
5945 
5946 /* Floating point compare
5947  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5948  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5949  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5950  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5951  */
5952 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5953 {
5954     unsigned int mos, type, rm, op, rn, opc, op2r;
5955     int size;
5956 
5957     mos = extract32(insn, 29, 3);
5958     type = extract32(insn, 22, 2);
5959     rm = extract32(insn, 16, 5);
5960     op = extract32(insn, 14, 2);
5961     rn = extract32(insn, 5, 5);
5962     opc = extract32(insn, 3, 2);
5963     op2r = extract32(insn, 0, 3);
5964 
5965     if (mos || op || op2r) {
5966         unallocated_encoding(s);
5967         return;
5968     }
5969 
5970     switch (type) {
5971     case 0:
5972         size = MO_32;
5973         break;
5974     case 1:
5975         size = MO_64;
5976         break;
5977     case 3:
5978         size = MO_16;
5979         if (dc_isar_feature(aa64_fp16, s)) {
5980             break;
5981         }
5982         /* fallthru */
5983     default:
5984         unallocated_encoding(s);
5985         return;
5986     }
5987 
5988     if (!fp_access_check(s)) {
5989         return;
5990     }
5991 
5992     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5993 }
5994 
5995 /* Floating point conditional compare
5996  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5997  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5998  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5999  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6000  */
6001 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6002 {
6003     unsigned int mos, type, rm, cond, rn, op, nzcv;
6004     TCGLabel *label_continue = NULL;
6005     int size;
6006 
6007     mos = extract32(insn, 29, 3);
6008     type = extract32(insn, 22, 2);
6009     rm = extract32(insn, 16, 5);
6010     cond = extract32(insn, 12, 4);
6011     rn = extract32(insn, 5, 5);
6012     op = extract32(insn, 4, 1);
6013     nzcv = extract32(insn, 0, 4);
6014 
6015     if (mos) {
6016         unallocated_encoding(s);
6017         return;
6018     }
6019 
6020     switch (type) {
6021     case 0:
6022         size = MO_32;
6023         break;
6024     case 1:
6025         size = MO_64;
6026         break;
6027     case 3:
6028         size = MO_16;
6029         if (dc_isar_feature(aa64_fp16, s)) {
6030             break;
6031         }
6032         /* fallthru */
6033     default:
6034         unallocated_encoding(s);
6035         return;
6036     }
6037 
6038     if (!fp_access_check(s)) {
6039         return;
6040     }
6041 
6042     if (cond < 0x0e) { /* not always */
6043         TCGLabel *label_match = gen_new_label();
6044         label_continue = gen_new_label();
6045         arm_gen_test_cc(cond, label_match);
6046         /* nomatch: */
6047         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6048         tcg_gen_br(label_continue);
6049         gen_set_label(label_match);
6050     }
6051 
6052     handle_fp_compare(s, size, rn, rm, false, op);
6053 
6054     if (cond < 0x0e) {
6055         gen_set_label(label_continue);
6056     }
6057 }
6058 
6059 /* Floating point conditional select
6060  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6061  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6062  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6063  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6064  */
6065 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6066 {
6067     unsigned int mos, type, rm, cond, rn, rd;
6068     TCGv_i64 t_true, t_false;
6069     DisasCompare64 c;
6070     MemOp sz;
6071 
6072     mos = extract32(insn, 29, 3);
6073     type = extract32(insn, 22, 2);
6074     rm = extract32(insn, 16, 5);
6075     cond = extract32(insn, 12, 4);
6076     rn = extract32(insn, 5, 5);
6077     rd = extract32(insn, 0, 5);
6078 
6079     if (mos) {
6080         unallocated_encoding(s);
6081         return;
6082     }
6083 
6084     switch (type) {
6085     case 0:
6086         sz = MO_32;
6087         break;
6088     case 1:
6089         sz = MO_64;
6090         break;
6091     case 3:
6092         sz = MO_16;
6093         if (dc_isar_feature(aa64_fp16, s)) {
6094             break;
6095         }
6096         /* fallthru */
6097     default:
6098         unallocated_encoding(s);
6099         return;
6100     }
6101 
6102     if (!fp_access_check(s)) {
6103         return;
6104     }
6105 
6106     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6107     t_true = tcg_temp_new_i64();
6108     t_false = tcg_temp_new_i64();
6109     read_vec_element(s, t_true, rn, 0, sz);
6110     read_vec_element(s, t_false, rm, 0, sz);
6111 
6112     a64_test_cc(&c, cond);
6113     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6114                         t_true, t_false);
6115 
6116     /* Note that sregs & hregs write back zeros to the high bits,
6117        and we've already done the zero-extension.  */
6118     write_fp_dreg(s, rd, t_true);
6119 }
6120 
6121 /* Floating-point data-processing (1 source) - half precision */
6122 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6123 {
6124     TCGv_ptr fpst = NULL;
6125     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6126     TCGv_i32 tcg_res = tcg_temp_new_i32();
6127 
6128     switch (opcode) {
6129     case 0x0: /* FMOV */
6130         tcg_gen_mov_i32(tcg_res, tcg_op);
6131         break;
6132     case 0x1: /* FABS */
6133         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6134         break;
6135     case 0x2: /* FNEG */
6136         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6137         break;
6138     case 0x3: /* FSQRT */
6139         fpst = fpstatus_ptr(FPST_FPCR_F16);
6140         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6141         break;
6142     case 0x8: /* FRINTN */
6143     case 0x9: /* FRINTP */
6144     case 0xa: /* FRINTM */
6145     case 0xb: /* FRINTZ */
6146     case 0xc: /* FRINTA */
6147     {
6148         TCGv_i32 tcg_rmode;
6149 
6150         fpst = fpstatus_ptr(FPST_FPCR_F16);
6151         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6152         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6153         gen_restore_rmode(tcg_rmode, fpst);
6154         break;
6155     }
6156     case 0xe: /* FRINTX */
6157         fpst = fpstatus_ptr(FPST_FPCR_F16);
6158         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6159         break;
6160     case 0xf: /* FRINTI */
6161         fpst = fpstatus_ptr(FPST_FPCR_F16);
6162         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6163         break;
6164     default:
6165         g_assert_not_reached();
6166     }
6167 
6168     write_fp_sreg(s, rd, tcg_res);
6169 }
6170 
6171 /* Floating-point data-processing (1 source) - single precision */
6172 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6173 {
6174     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6175     TCGv_i32 tcg_op, tcg_res;
6176     TCGv_ptr fpst;
6177     int rmode = -1;
6178 
6179     tcg_op = read_fp_sreg(s, rn);
6180     tcg_res = tcg_temp_new_i32();
6181 
6182     switch (opcode) {
6183     case 0x0: /* FMOV */
6184         tcg_gen_mov_i32(tcg_res, tcg_op);
6185         goto done;
6186     case 0x1: /* FABS */
6187         gen_helper_vfp_abss(tcg_res, tcg_op);
6188         goto done;
6189     case 0x2: /* FNEG */
6190         gen_helper_vfp_negs(tcg_res, tcg_op);
6191         goto done;
6192     case 0x3: /* FSQRT */
6193         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6194         goto done;
6195     case 0x6: /* BFCVT */
6196         gen_fpst = gen_helper_bfcvt;
6197         break;
6198     case 0x8: /* FRINTN */
6199     case 0x9: /* FRINTP */
6200     case 0xa: /* FRINTM */
6201     case 0xb: /* FRINTZ */
6202     case 0xc: /* FRINTA */
6203         rmode = opcode & 7;
6204         gen_fpst = gen_helper_rints;
6205         break;
6206     case 0xe: /* FRINTX */
6207         gen_fpst = gen_helper_rints_exact;
6208         break;
6209     case 0xf: /* FRINTI */
6210         gen_fpst = gen_helper_rints;
6211         break;
6212     case 0x10: /* FRINT32Z */
6213         rmode = FPROUNDING_ZERO;
6214         gen_fpst = gen_helper_frint32_s;
6215         break;
6216     case 0x11: /* FRINT32X */
6217         gen_fpst = gen_helper_frint32_s;
6218         break;
6219     case 0x12: /* FRINT64Z */
6220         rmode = FPROUNDING_ZERO;
6221         gen_fpst = gen_helper_frint64_s;
6222         break;
6223     case 0x13: /* FRINT64X */
6224         gen_fpst = gen_helper_frint64_s;
6225         break;
6226     default:
6227         g_assert_not_reached();
6228     }
6229 
6230     fpst = fpstatus_ptr(FPST_FPCR);
6231     if (rmode >= 0) {
6232         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6233         gen_fpst(tcg_res, tcg_op, fpst);
6234         gen_restore_rmode(tcg_rmode, fpst);
6235     } else {
6236         gen_fpst(tcg_res, tcg_op, fpst);
6237     }
6238 
6239  done:
6240     write_fp_sreg(s, rd, tcg_res);
6241 }
6242 
6243 /* Floating-point data-processing (1 source) - double precision */
6244 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6245 {
6246     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6247     TCGv_i64 tcg_op, tcg_res;
6248     TCGv_ptr fpst;
6249     int rmode = -1;
6250 
6251     switch (opcode) {
6252     case 0x0: /* FMOV */
6253         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6254         return;
6255     }
6256 
6257     tcg_op = read_fp_dreg(s, rn);
6258     tcg_res = tcg_temp_new_i64();
6259 
6260     switch (opcode) {
6261     case 0x1: /* FABS */
6262         gen_helper_vfp_absd(tcg_res, tcg_op);
6263         goto done;
6264     case 0x2: /* FNEG */
6265         gen_helper_vfp_negd(tcg_res, tcg_op);
6266         goto done;
6267     case 0x3: /* FSQRT */
6268         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6269         goto done;
6270     case 0x8: /* FRINTN */
6271     case 0x9: /* FRINTP */
6272     case 0xa: /* FRINTM */
6273     case 0xb: /* FRINTZ */
6274     case 0xc: /* FRINTA */
6275         rmode = opcode & 7;
6276         gen_fpst = gen_helper_rintd;
6277         break;
6278     case 0xe: /* FRINTX */
6279         gen_fpst = gen_helper_rintd_exact;
6280         break;
6281     case 0xf: /* FRINTI */
6282         gen_fpst = gen_helper_rintd;
6283         break;
6284     case 0x10: /* FRINT32Z */
6285         rmode = FPROUNDING_ZERO;
6286         gen_fpst = gen_helper_frint32_d;
6287         break;
6288     case 0x11: /* FRINT32X */
6289         gen_fpst = gen_helper_frint32_d;
6290         break;
6291     case 0x12: /* FRINT64Z */
6292         rmode = FPROUNDING_ZERO;
6293         gen_fpst = gen_helper_frint64_d;
6294         break;
6295     case 0x13: /* FRINT64X */
6296         gen_fpst = gen_helper_frint64_d;
6297         break;
6298     default:
6299         g_assert_not_reached();
6300     }
6301 
6302     fpst = fpstatus_ptr(FPST_FPCR);
6303     if (rmode >= 0) {
6304         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6305         gen_fpst(tcg_res, tcg_op, fpst);
6306         gen_restore_rmode(tcg_rmode, fpst);
6307     } else {
6308         gen_fpst(tcg_res, tcg_op, fpst);
6309     }
6310 
6311  done:
6312     write_fp_dreg(s, rd, tcg_res);
6313 }
6314 
6315 static void handle_fp_fcvt(DisasContext *s, int opcode,
6316                            int rd, int rn, int dtype, int ntype)
6317 {
6318     switch (ntype) {
6319     case 0x0:
6320     {
6321         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6322         if (dtype == 1) {
6323             /* Single to double */
6324             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6325             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6326             write_fp_dreg(s, rd, tcg_rd);
6327         } else {
6328             /* Single to half */
6329             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6330             TCGv_i32 ahp = get_ahp_flag();
6331             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6332 
6333             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6334             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6335             write_fp_sreg(s, rd, tcg_rd);
6336         }
6337         break;
6338     }
6339     case 0x1:
6340     {
6341         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6342         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6343         if (dtype == 0) {
6344             /* Double to single */
6345             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6346         } else {
6347             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6348             TCGv_i32 ahp = get_ahp_flag();
6349             /* Double to half */
6350             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6351             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6352         }
6353         write_fp_sreg(s, rd, tcg_rd);
6354         break;
6355     }
6356     case 0x3:
6357     {
6358         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6359         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6360         TCGv_i32 tcg_ahp = get_ahp_flag();
6361         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6362         if (dtype == 0) {
6363             /* Half to single */
6364             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6365             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6366             write_fp_sreg(s, rd, tcg_rd);
6367         } else {
6368             /* Half to double */
6369             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6370             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6371             write_fp_dreg(s, rd, tcg_rd);
6372         }
6373         break;
6374     }
6375     default:
6376         g_assert_not_reached();
6377     }
6378 }
6379 
6380 /* Floating point data-processing (1 source)
6381  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6382  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6383  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6384  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6385  */
6386 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6387 {
6388     int mos = extract32(insn, 29, 3);
6389     int type = extract32(insn, 22, 2);
6390     int opcode = extract32(insn, 15, 6);
6391     int rn = extract32(insn, 5, 5);
6392     int rd = extract32(insn, 0, 5);
6393 
6394     if (mos) {
6395         goto do_unallocated;
6396     }
6397 
6398     switch (opcode) {
6399     case 0x4: case 0x5: case 0x7:
6400     {
6401         /* FCVT between half, single and double precision */
6402         int dtype = extract32(opcode, 0, 2);
6403         if (type == 2 || dtype == type) {
6404             goto do_unallocated;
6405         }
6406         if (!fp_access_check(s)) {
6407             return;
6408         }
6409 
6410         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6411         break;
6412     }
6413 
6414     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6415         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6416             goto do_unallocated;
6417         }
6418         /* fall through */
6419     case 0x0 ... 0x3:
6420     case 0x8 ... 0xc:
6421     case 0xe ... 0xf:
6422         /* 32-to-32 and 64-to-64 ops */
6423         switch (type) {
6424         case 0:
6425             if (!fp_access_check(s)) {
6426                 return;
6427             }
6428             handle_fp_1src_single(s, opcode, rd, rn);
6429             break;
6430         case 1:
6431             if (!fp_access_check(s)) {
6432                 return;
6433             }
6434             handle_fp_1src_double(s, opcode, rd, rn);
6435             break;
6436         case 3:
6437             if (!dc_isar_feature(aa64_fp16, s)) {
6438                 goto do_unallocated;
6439             }
6440 
6441             if (!fp_access_check(s)) {
6442                 return;
6443             }
6444             handle_fp_1src_half(s, opcode, rd, rn);
6445             break;
6446         default:
6447             goto do_unallocated;
6448         }
6449         break;
6450 
6451     case 0x6:
6452         switch (type) {
6453         case 1: /* BFCVT */
6454             if (!dc_isar_feature(aa64_bf16, s)) {
6455                 goto do_unallocated;
6456             }
6457             if (!fp_access_check(s)) {
6458                 return;
6459             }
6460             handle_fp_1src_single(s, opcode, rd, rn);
6461             break;
6462         default:
6463             goto do_unallocated;
6464         }
6465         break;
6466 
6467     default:
6468     do_unallocated:
6469         unallocated_encoding(s);
6470         break;
6471     }
6472 }
6473 
6474 /* Floating-point data-processing (2 source) - single precision */
6475 static void handle_fp_2src_single(DisasContext *s, int opcode,
6476                                   int rd, int rn, int rm)
6477 {
6478     TCGv_i32 tcg_op1;
6479     TCGv_i32 tcg_op2;
6480     TCGv_i32 tcg_res;
6481     TCGv_ptr fpst;
6482 
6483     tcg_res = tcg_temp_new_i32();
6484     fpst = fpstatus_ptr(FPST_FPCR);
6485     tcg_op1 = read_fp_sreg(s, rn);
6486     tcg_op2 = read_fp_sreg(s, rm);
6487 
6488     switch (opcode) {
6489     case 0x0: /* FMUL */
6490         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6491         break;
6492     case 0x1: /* FDIV */
6493         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6494         break;
6495     case 0x2: /* FADD */
6496         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6497         break;
6498     case 0x3: /* FSUB */
6499         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6500         break;
6501     case 0x4: /* FMAX */
6502         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6503         break;
6504     case 0x5: /* FMIN */
6505         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6506         break;
6507     case 0x6: /* FMAXNM */
6508         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6509         break;
6510     case 0x7: /* FMINNM */
6511         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6512         break;
6513     case 0x8: /* FNMUL */
6514         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6515         gen_helper_vfp_negs(tcg_res, tcg_res);
6516         break;
6517     }
6518 
6519     write_fp_sreg(s, rd, tcg_res);
6520 }
6521 
6522 /* Floating-point data-processing (2 source) - double precision */
6523 static void handle_fp_2src_double(DisasContext *s, int opcode,
6524                                   int rd, int rn, int rm)
6525 {
6526     TCGv_i64 tcg_op1;
6527     TCGv_i64 tcg_op2;
6528     TCGv_i64 tcg_res;
6529     TCGv_ptr fpst;
6530 
6531     tcg_res = tcg_temp_new_i64();
6532     fpst = fpstatus_ptr(FPST_FPCR);
6533     tcg_op1 = read_fp_dreg(s, rn);
6534     tcg_op2 = read_fp_dreg(s, rm);
6535 
6536     switch (opcode) {
6537     case 0x0: /* FMUL */
6538         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6539         break;
6540     case 0x1: /* FDIV */
6541         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6542         break;
6543     case 0x2: /* FADD */
6544         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6545         break;
6546     case 0x3: /* FSUB */
6547         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6548         break;
6549     case 0x4: /* FMAX */
6550         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6551         break;
6552     case 0x5: /* FMIN */
6553         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6554         break;
6555     case 0x6: /* FMAXNM */
6556         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6557         break;
6558     case 0x7: /* FMINNM */
6559         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6560         break;
6561     case 0x8: /* FNMUL */
6562         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6563         gen_helper_vfp_negd(tcg_res, tcg_res);
6564         break;
6565     }
6566 
6567     write_fp_dreg(s, rd, tcg_res);
6568 }
6569 
6570 /* Floating-point data-processing (2 source) - half precision */
6571 static void handle_fp_2src_half(DisasContext *s, int opcode,
6572                                 int rd, int rn, int rm)
6573 {
6574     TCGv_i32 tcg_op1;
6575     TCGv_i32 tcg_op2;
6576     TCGv_i32 tcg_res;
6577     TCGv_ptr fpst;
6578 
6579     tcg_res = tcg_temp_new_i32();
6580     fpst = fpstatus_ptr(FPST_FPCR_F16);
6581     tcg_op1 = read_fp_hreg(s, rn);
6582     tcg_op2 = read_fp_hreg(s, rm);
6583 
6584     switch (opcode) {
6585     case 0x0: /* FMUL */
6586         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6587         break;
6588     case 0x1: /* FDIV */
6589         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6590         break;
6591     case 0x2: /* FADD */
6592         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6593         break;
6594     case 0x3: /* FSUB */
6595         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6596         break;
6597     case 0x4: /* FMAX */
6598         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6599         break;
6600     case 0x5: /* FMIN */
6601         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6602         break;
6603     case 0x6: /* FMAXNM */
6604         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6605         break;
6606     case 0x7: /* FMINNM */
6607         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6608         break;
6609     case 0x8: /* FNMUL */
6610         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6611         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6612         break;
6613     default:
6614         g_assert_not_reached();
6615     }
6616 
6617     write_fp_sreg(s, rd, tcg_res);
6618 }
6619 
6620 /* Floating point data-processing (2 source)
6621  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6622  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6623  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6624  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6625  */
6626 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6627 {
6628     int mos = extract32(insn, 29, 3);
6629     int type = extract32(insn, 22, 2);
6630     int rd = extract32(insn, 0, 5);
6631     int rn = extract32(insn, 5, 5);
6632     int rm = extract32(insn, 16, 5);
6633     int opcode = extract32(insn, 12, 4);
6634 
6635     if (opcode > 8 || mos) {
6636         unallocated_encoding(s);
6637         return;
6638     }
6639 
6640     switch (type) {
6641     case 0:
6642         if (!fp_access_check(s)) {
6643             return;
6644         }
6645         handle_fp_2src_single(s, opcode, rd, rn, rm);
6646         break;
6647     case 1:
6648         if (!fp_access_check(s)) {
6649             return;
6650         }
6651         handle_fp_2src_double(s, opcode, rd, rn, rm);
6652         break;
6653     case 3:
6654         if (!dc_isar_feature(aa64_fp16, s)) {
6655             unallocated_encoding(s);
6656             return;
6657         }
6658         if (!fp_access_check(s)) {
6659             return;
6660         }
6661         handle_fp_2src_half(s, opcode, rd, rn, rm);
6662         break;
6663     default:
6664         unallocated_encoding(s);
6665     }
6666 }
6667 
6668 /* Floating-point data-processing (3 source) - single precision */
6669 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6670                                   int rd, int rn, int rm, int ra)
6671 {
6672     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6673     TCGv_i32 tcg_res = tcg_temp_new_i32();
6674     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6675 
6676     tcg_op1 = read_fp_sreg(s, rn);
6677     tcg_op2 = read_fp_sreg(s, rm);
6678     tcg_op3 = read_fp_sreg(s, ra);
6679 
6680     /* These are fused multiply-add, and must be done as one
6681      * floating point operation with no rounding between the
6682      * multiplication and addition steps.
6683      * NB that doing the negations here as separate steps is
6684      * correct : an input NaN should come out with its sign bit
6685      * flipped if it is a negated-input.
6686      */
6687     if (o1 == true) {
6688         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6689     }
6690 
6691     if (o0 != o1) {
6692         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6693     }
6694 
6695     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6696 
6697     write_fp_sreg(s, rd, tcg_res);
6698 }
6699 
6700 /* Floating-point data-processing (3 source) - double precision */
6701 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6702                                   int rd, int rn, int rm, int ra)
6703 {
6704     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6705     TCGv_i64 tcg_res = tcg_temp_new_i64();
6706     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6707 
6708     tcg_op1 = read_fp_dreg(s, rn);
6709     tcg_op2 = read_fp_dreg(s, rm);
6710     tcg_op3 = read_fp_dreg(s, ra);
6711 
6712     /* These are fused multiply-add, and must be done as one
6713      * floating point operation with no rounding between the
6714      * multiplication and addition steps.
6715      * NB that doing the negations here as separate steps is
6716      * correct : an input NaN should come out with its sign bit
6717      * flipped if it is a negated-input.
6718      */
6719     if (o1 == true) {
6720         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6721     }
6722 
6723     if (o0 != o1) {
6724         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6725     }
6726 
6727     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6728 
6729     write_fp_dreg(s, rd, tcg_res);
6730 }
6731 
6732 /* Floating-point data-processing (3 source) - half precision */
6733 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6734                                 int rd, int rn, int rm, int ra)
6735 {
6736     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6737     TCGv_i32 tcg_res = tcg_temp_new_i32();
6738     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6739 
6740     tcg_op1 = read_fp_hreg(s, rn);
6741     tcg_op2 = read_fp_hreg(s, rm);
6742     tcg_op3 = read_fp_hreg(s, ra);
6743 
6744     /* These are fused multiply-add, and must be done as one
6745      * floating point operation with no rounding between the
6746      * multiplication and addition steps.
6747      * NB that doing the negations here as separate steps is
6748      * correct : an input NaN should come out with its sign bit
6749      * flipped if it is a negated-input.
6750      */
6751     if (o1 == true) {
6752         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6753     }
6754 
6755     if (o0 != o1) {
6756         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6757     }
6758 
6759     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6760 
6761     write_fp_sreg(s, rd, tcg_res);
6762 }
6763 
6764 /* Floating point data-processing (3 source)
6765  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6766  * +---+---+---+-----------+------+----+------+----+------+------+------+
6767  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6768  * +---+---+---+-----------+------+----+------+----+------+------+------+
6769  */
6770 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6771 {
6772     int mos = extract32(insn, 29, 3);
6773     int type = extract32(insn, 22, 2);
6774     int rd = extract32(insn, 0, 5);
6775     int rn = extract32(insn, 5, 5);
6776     int ra = extract32(insn, 10, 5);
6777     int rm = extract32(insn, 16, 5);
6778     bool o0 = extract32(insn, 15, 1);
6779     bool o1 = extract32(insn, 21, 1);
6780 
6781     if (mos) {
6782         unallocated_encoding(s);
6783         return;
6784     }
6785 
6786     switch (type) {
6787     case 0:
6788         if (!fp_access_check(s)) {
6789             return;
6790         }
6791         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6792         break;
6793     case 1:
6794         if (!fp_access_check(s)) {
6795             return;
6796         }
6797         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6798         break;
6799     case 3:
6800         if (!dc_isar_feature(aa64_fp16, s)) {
6801             unallocated_encoding(s);
6802             return;
6803         }
6804         if (!fp_access_check(s)) {
6805             return;
6806         }
6807         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6808         break;
6809     default:
6810         unallocated_encoding(s);
6811     }
6812 }
6813 
6814 /* Floating point immediate
6815  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6816  * +---+---+---+-----------+------+---+------------+-------+------+------+
6817  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6818  * +---+---+---+-----------+------+---+------------+-------+------+------+
6819  */
6820 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6821 {
6822     int rd = extract32(insn, 0, 5);
6823     int imm5 = extract32(insn, 5, 5);
6824     int imm8 = extract32(insn, 13, 8);
6825     int type = extract32(insn, 22, 2);
6826     int mos = extract32(insn, 29, 3);
6827     uint64_t imm;
6828     MemOp sz;
6829 
6830     if (mos || imm5) {
6831         unallocated_encoding(s);
6832         return;
6833     }
6834 
6835     switch (type) {
6836     case 0:
6837         sz = MO_32;
6838         break;
6839     case 1:
6840         sz = MO_64;
6841         break;
6842     case 3:
6843         sz = MO_16;
6844         if (dc_isar_feature(aa64_fp16, s)) {
6845             break;
6846         }
6847         /* fallthru */
6848     default:
6849         unallocated_encoding(s);
6850         return;
6851     }
6852 
6853     if (!fp_access_check(s)) {
6854         return;
6855     }
6856 
6857     imm = vfp_expand_imm(sz, imm8);
6858     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6859 }
6860 
6861 /* Handle floating point <=> fixed point conversions. Note that we can
6862  * also deal with fp <=> integer conversions as a special case (scale == 64)
6863  * OPTME: consider handling that special case specially or at least skipping
6864  * the call to scalbn in the helpers for zero shifts.
6865  */
6866 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6867                            bool itof, int rmode, int scale, int sf, int type)
6868 {
6869     bool is_signed = !(opcode & 1);
6870     TCGv_ptr tcg_fpstatus;
6871     TCGv_i32 tcg_shift, tcg_single;
6872     TCGv_i64 tcg_double;
6873 
6874     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6875 
6876     tcg_shift = tcg_constant_i32(64 - scale);
6877 
6878     if (itof) {
6879         TCGv_i64 tcg_int = cpu_reg(s, rn);
6880         if (!sf) {
6881             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6882 
6883             if (is_signed) {
6884                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6885             } else {
6886                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6887             }
6888 
6889             tcg_int = tcg_extend;
6890         }
6891 
6892         switch (type) {
6893         case 1: /* float64 */
6894             tcg_double = tcg_temp_new_i64();
6895             if (is_signed) {
6896                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6897                                      tcg_shift, tcg_fpstatus);
6898             } else {
6899                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6900                                      tcg_shift, tcg_fpstatus);
6901             }
6902             write_fp_dreg(s, rd, tcg_double);
6903             break;
6904 
6905         case 0: /* float32 */
6906             tcg_single = tcg_temp_new_i32();
6907             if (is_signed) {
6908                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6909                                      tcg_shift, tcg_fpstatus);
6910             } else {
6911                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6912                                      tcg_shift, tcg_fpstatus);
6913             }
6914             write_fp_sreg(s, rd, tcg_single);
6915             break;
6916 
6917         case 3: /* float16 */
6918             tcg_single = tcg_temp_new_i32();
6919             if (is_signed) {
6920                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6921                                      tcg_shift, tcg_fpstatus);
6922             } else {
6923                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6924                                      tcg_shift, tcg_fpstatus);
6925             }
6926             write_fp_sreg(s, rd, tcg_single);
6927             break;
6928 
6929         default:
6930             g_assert_not_reached();
6931         }
6932     } else {
6933         TCGv_i64 tcg_int = cpu_reg(s, rd);
6934         TCGv_i32 tcg_rmode;
6935 
6936         if (extract32(opcode, 2, 1)) {
6937             /* There are too many rounding modes to all fit into rmode,
6938              * so FCVTA[US] is a special case.
6939              */
6940             rmode = FPROUNDING_TIEAWAY;
6941         }
6942 
6943         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6944 
6945         switch (type) {
6946         case 1: /* float64 */
6947             tcg_double = read_fp_dreg(s, rn);
6948             if (is_signed) {
6949                 if (!sf) {
6950                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6951                                          tcg_shift, tcg_fpstatus);
6952                 } else {
6953                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6954                                          tcg_shift, tcg_fpstatus);
6955                 }
6956             } else {
6957                 if (!sf) {
6958                     gen_helper_vfp_tould(tcg_int, tcg_double,
6959                                          tcg_shift, tcg_fpstatus);
6960                 } else {
6961                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6962                                          tcg_shift, tcg_fpstatus);
6963                 }
6964             }
6965             if (!sf) {
6966                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6967             }
6968             break;
6969 
6970         case 0: /* float32 */
6971             tcg_single = read_fp_sreg(s, rn);
6972             if (sf) {
6973                 if (is_signed) {
6974                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6975                                          tcg_shift, tcg_fpstatus);
6976                 } else {
6977                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6978                                          tcg_shift, tcg_fpstatus);
6979                 }
6980             } else {
6981                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6982                 if (is_signed) {
6983                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6984                                          tcg_shift, tcg_fpstatus);
6985                 } else {
6986                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6987                                          tcg_shift, tcg_fpstatus);
6988                 }
6989                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6990             }
6991             break;
6992 
6993         case 3: /* float16 */
6994             tcg_single = read_fp_sreg(s, rn);
6995             if (sf) {
6996                 if (is_signed) {
6997                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6998                                          tcg_shift, tcg_fpstatus);
6999                 } else {
7000                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7001                                          tcg_shift, tcg_fpstatus);
7002                 }
7003             } else {
7004                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7005                 if (is_signed) {
7006                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7007                                          tcg_shift, tcg_fpstatus);
7008                 } else {
7009                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7010                                          tcg_shift, tcg_fpstatus);
7011                 }
7012                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7013             }
7014             break;
7015 
7016         default:
7017             g_assert_not_reached();
7018         }
7019 
7020         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7021     }
7022 }
7023 
7024 /* Floating point <-> fixed point conversions
7025  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7026  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7027  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7028  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7029  */
7030 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7031 {
7032     int rd = extract32(insn, 0, 5);
7033     int rn = extract32(insn, 5, 5);
7034     int scale = extract32(insn, 10, 6);
7035     int opcode = extract32(insn, 16, 3);
7036     int rmode = extract32(insn, 19, 2);
7037     int type = extract32(insn, 22, 2);
7038     bool sbit = extract32(insn, 29, 1);
7039     bool sf = extract32(insn, 31, 1);
7040     bool itof;
7041 
7042     if (sbit || (!sf && scale < 32)) {
7043         unallocated_encoding(s);
7044         return;
7045     }
7046 
7047     switch (type) {
7048     case 0: /* float32 */
7049     case 1: /* float64 */
7050         break;
7051     case 3: /* float16 */
7052         if (dc_isar_feature(aa64_fp16, s)) {
7053             break;
7054         }
7055         /* fallthru */
7056     default:
7057         unallocated_encoding(s);
7058         return;
7059     }
7060 
7061     switch ((rmode << 3) | opcode) {
7062     case 0x2: /* SCVTF */
7063     case 0x3: /* UCVTF */
7064         itof = true;
7065         break;
7066     case 0x18: /* FCVTZS */
7067     case 0x19: /* FCVTZU */
7068         itof = false;
7069         break;
7070     default:
7071         unallocated_encoding(s);
7072         return;
7073     }
7074 
7075     if (!fp_access_check(s)) {
7076         return;
7077     }
7078 
7079     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7080 }
7081 
7082 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7083 {
7084     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7085      * without conversion.
7086      */
7087 
7088     if (itof) {
7089         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7090         TCGv_i64 tmp;
7091 
7092         switch (type) {
7093         case 0:
7094             /* 32 bit */
7095             tmp = tcg_temp_new_i64();
7096             tcg_gen_ext32u_i64(tmp, tcg_rn);
7097             write_fp_dreg(s, rd, tmp);
7098             break;
7099         case 1:
7100             /* 64 bit */
7101             write_fp_dreg(s, rd, tcg_rn);
7102             break;
7103         case 2:
7104             /* 64 bit to top half. */
7105             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7106             clear_vec_high(s, true, rd);
7107             break;
7108         case 3:
7109             /* 16 bit */
7110             tmp = tcg_temp_new_i64();
7111             tcg_gen_ext16u_i64(tmp, tcg_rn);
7112             write_fp_dreg(s, rd, tmp);
7113             break;
7114         default:
7115             g_assert_not_reached();
7116         }
7117     } else {
7118         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7119 
7120         switch (type) {
7121         case 0:
7122             /* 32 bit */
7123             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7124             break;
7125         case 1:
7126             /* 64 bit */
7127             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7128             break;
7129         case 2:
7130             /* 64 bits from top half */
7131             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7132             break;
7133         case 3:
7134             /* 16 bit */
7135             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7136             break;
7137         default:
7138             g_assert_not_reached();
7139         }
7140     }
7141 }
7142 
7143 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7144 {
7145     TCGv_i64 t = read_fp_dreg(s, rn);
7146     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7147 
7148     gen_helper_fjcvtzs(t, t, fpstatus);
7149 
7150     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7151     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7152     tcg_gen_movi_i32(cpu_CF, 0);
7153     tcg_gen_movi_i32(cpu_NF, 0);
7154     tcg_gen_movi_i32(cpu_VF, 0);
7155 }
7156 
7157 /* Floating point <-> integer conversions
7158  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7159  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7160  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7161  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7162  */
7163 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7164 {
7165     int rd = extract32(insn, 0, 5);
7166     int rn = extract32(insn, 5, 5);
7167     int opcode = extract32(insn, 16, 3);
7168     int rmode = extract32(insn, 19, 2);
7169     int type = extract32(insn, 22, 2);
7170     bool sbit = extract32(insn, 29, 1);
7171     bool sf = extract32(insn, 31, 1);
7172     bool itof = false;
7173 
7174     if (sbit) {
7175         goto do_unallocated;
7176     }
7177 
7178     switch (opcode) {
7179     case 2: /* SCVTF */
7180     case 3: /* UCVTF */
7181         itof = true;
7182         /* fallthru */
7183     case 4: /* FCVTAS */
7184     case 5: /* FCVTAU */
7185         if (rmode != 0) {
7186             goto do_unallocated;
7187         }
7188         /* fallthru */
7189     case 0: /* FCVT[NPMZ]S */
7190     case 1: /* FCVT[NPMZ]U */
7191         switch (type) {
7192         case 0: /* float32 */
7193         case 1: /* float64 */
7194             break;
7195         case 3: /* float16 */
7196             if (!dc_isar_feature(aa64_fp16, s)) {
7197                 goto do_unallocated;
7198             }
7199             break;
7200         default:
7201             goto do_unallocated;
7202         }
7203         if (!fp_access_check(s)) {
7204             return;
7205         }
7206         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7207         break;
7208 
7209     default:
7210         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7211         case 0b01100110: /* FMOV half <-> 32-bit int */
7212         case 0b01100111:
7213         case 0b11100110: /* FMOV half <-> 64-bit int */
7214         case 0b11100111:
7215             if (!dc_isar_feature(aa64_fp16, s)) {
7216                 goto do_unallocated;
7217             }
7218             /* fallthru */
7219         case 0b00000110: /* FMOV 32-bit */
7220         case 0b00000111:
7221         case 0b10100110: /* FMOV 64-bit */
7222         case 0b10100111:
7223         case 0b11001110: /* FMOV top half of 128-bit */
7224         case 0b11001111:
7225             if (!fp_access_check(s)) {
7226                 return;
7227             }
7228             itof = opcode & 1;
7229             handle_fmov(s, rd, rn, type, itof);
7230             break;
7231 
7232         case 0b00111110: /* FJCVTZS */
7233             if (!dc_isar_feature(aa64_jscvt, s)) {
7234                 goto do_unallocated;
7235             } else if (fp_access_check(s)) {
7236                 handle_fjcvtzs(s, rd, rn);
7237             }
7238             break;
7239 
7240         default:
7241         do_unallocated:
7242             unallocated_encoding(s);
7243             return;
7244         }
7245         break;
7246     }
7247 }
7248 
7249 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7250  *   31  30  29 28     25 24                          0
7251  * +---+---+---+---------+-----------------------------+
7252  * |   | 0 |   | 1 1 1 1 |                             |
7253  * +---+---+---+---------+-----------------------------+
7254  */
7255 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7256 {
7257     if (extract32(insn, 24, 1)) {
7258         /* Floating point data-processing (3 source) */
7259         disas_fp_3src(s, insn);
7260     } else if (extract32(insn, 21, 1) == 0) {
7261         /* Floating point to fixed point conversions */
7262         disas_fp_fixed_conv(s, insn);
7263     } else {
7264         switch (extract32(insn, 10, 2)) {
7265         case 1:
7266             /* Floating point conditional compare */
7267             disas_fp_ccomp(s, insn);
7268             break;
7269         case 2:
7270             /* Floating point data-processing (2 source) */
7271             disas_fp_2src(s, insn);
7272             break;
7273         case 3:
7274             /* Floating point conditional select */
7275             disas_fp_csel(s, insn);
7276             break;
7277         case 0:
7278             switch (ctz32(extract32(insn, 12, 4))) {
7279             case 0: /* [15:12] == xxx1 */
7280                 /* Floating point immediate */
7281                 disas_fp_imm(s, insn);
7282                 break;
7283             case 1: /* [15:12] == xx10 */
7284                 /* Floating point compare */
7285                 disas_fp_compare(s, insn);
7286                 break;
7287             case 2: /* [15:12] == x100 */
7288                 /* Floating point data-processing (1 source) */
7289                 disas_fp_1src(s, insn);
7290                 break;
7291             case 3: /* [15:12] == 1000 */
7292                 unallocated_encoding(s);
7293                 break;
7294             default: /* [15:12] == 0000 */
7295                 /* Floating point <-> integer conversions */
7296                 disas_fp_int_conv(s, insn);
7297                 break;
7298             }
7299             break;
7300         }
7301     }
7302 }
7303 
7304 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7305                      int pos)
7306 {
7307     /* Extract 64 bits from the middle of two concatenated 64 bit
7308      * vector register slices left:right. The extracted bits start
7309      * at 'pos' bits into the right (least significant) side.
7310      * We return the result in tcg_right, and guarantee not to
7311      * trash tcg_left.
7312      */
7313     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7314     assert(pos > 0 && pos < 64);
7315 
7316     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7317     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7318     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7319 }
7320 
7321 /* EXT
7322  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7323  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7324  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7325  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7326  */
7327 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7328 {
7329     int is_q = extract32(insn, 30, 1);
7330     int op2 = extract32(insn, 22, 2);
7331     int imm4 = extract32(insn, 11, 4);
7332     int rm = extract32(insn, 16, 5);
7333     int rn = extract32(insn, 5, 5);
7334     int rd = extract32(insn, 0, 5);
7335     int pos = imm4 << 3;
7336     TCGv_i64 tcg_resl, tcg_resh;
7337 
7338     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7339         unallocated_encoding(s);
7340         return;
7341     }
7342 
7343     if (!fp_access_check(s)) {
7344         return;
7345     }
7346 
7347     tcg_resh = tcg_temp_new_i64();
7348     tcg_resl = tcg_temp_new_i64();
7349 
7350     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7351      * either extracting 128 bits from a 128:128 concatenation, or
7352      * extracting 64 bits from a 64:64 concatenation.
7353      */
7354     if (!is_q) {
7355         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7356         if (pos != 0) {
7357             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7358             do_ext64(s, tcg_resh, tcg_resl, pos);
7359         }
7360     } else {
7361         TCGv_i64 tcg_hh;
7362         typedef struct {
7363             int reg;
7364             int elt;
7365         } EltPosns;
7366         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7367         EltPosns *elt = eltposns;
7368 
7369         if (pos >= 64) {
7370             elt++;
7371             pos -= 64;
7372         }
7373 
7374         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7375         elt++;
7376         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7377         elt++;
7378         if (pos != 0) {
7379             do_ext64(s, tcg_resh, tcg_resl, pos);
7380             tcg_hh = tcg_temp_new_i64();
7381             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7382             do_ext64(s, tcg_hh, tcg_resh, pos);
7383         }
7384     }
7385 
7386     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7387     if (is_q) {
7388         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7389     }
7390     clear_vec_high(s, is_q, rd);
7391 }
7392 
7393 /* TBL/TBX
7394  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7395  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7396  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7397  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7398  */
7399 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7400 {
7401     int op2 = extract32(insn, 22, 2);
7402     int is_q = extract32(insn, 30, 1);
7403     int rm = extract32(insn, 16, 5);
7404     int rn = extract32(insn, 5, 5);
7405     int rd = extract32(insn, 0, 5);
7406     int is_tbx = extract32(insn, 12, 1);
7407     int len = (extract32(insn, 13, 2) + 1) * 16;
7408 
7409     if (op2 != 0) {
7410         unallocated_encoding(s);
7411         return;
7412     }
7413 
7414     if (!fp_access_check(s)) {
7415         return;
7416     }
7417 
7418     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7419                        vec_full_reg_offset(s, rm), cpu_env,
7420                        is_q ? 16 : 8, vec_full_reg_size(s),
7421                        (len << 6) | (is_tbx << 5) | rn,
7422                        gen_helper_simd_tblx);
7423 }
7424 
7425 /* ZIP/UZP/TRN
7426  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7427  * +---+---+-------------+------+---+------+---+------------------+------+
7428  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7429  * +---+---+-------------+------+---+------+---+------------------+------+
7430  */
7431 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7432 {
7433     int rd = extract32(insn, 0, 5);
7434     int rn = extract32(insn, 5, 5);
7435     int rm = extract32(insn, 16, 5);
7436     int size = extract32(insn, 22, 2);
7437     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7438      * bit 2 indicates 1 vs 2 variant of the insn.
7439      */
7440     int opcode = extract32(insn, 12, 2);
7441     bool part = extract32(insn, 14, 1);
7442     bool is_q = extract32(insn, 30, 1);
7443     int esize = 8 << size;
7444     int i;
7445     int datasize = is_q ? 128 : 64;
7446     int elements = datasize / esize;
7447     TCGv_i64 tcg_res[2], tcg_ele;
7448 
7449     if (opcode == 0 || (size == 3 && !is_q)) {
7450         unallocated_encoding(s);
7451         return;
7452     }
7453 
7454     if (!fp_access_check(s)) {
7455         return;
7456     }
7457 
7458     tcg_res[0] = tcg_temp_new_i64();
7459     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7460     tcg_ele = tcg_temp_new_i64();
7461 
7462     for (i = 0; i < elements; i++) {
7463         int o, w;
7464 
7465         switch (opcode) {
7466         case 1: /* UZP1/2 */
7467         {
7468             int midpoint = elements / 2;
7469             if (i < midpoint) {
7470                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7471             } else {
7472                 read_vec_element(s, tcg_ele, rm,
7473                                  2 * (i - midpoint) + part, size);
7474             }
7475             break;
7476         }
7477         case 2: /* TRN1/2 */
7478             if (i & 1) {
7479                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7480             } else {
7481                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7482             }
7483             break;
7484         case 3: /* ZIP1/2 */
7485         {
7486             int base = part * elements / 2;
7487             if (i & 1) {
7488                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7489             } else {
7490                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7491             }
7492             break;
7493         }
7494         default:
7495             g_assert_not_reached();
7496         }
7497 
7498         w = (i * esize) / 64;
7499         o = (i * esize) % 64;
7500         if (o == 0) {
7501             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7502         } else {
7503             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7504             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7505         }
7506     }
7507 
7508     for (i = 0; i <= is_q; ++i) {
7509         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7510     }
7511     clear_vec_high(s, is_q, rd);
7512 }
7513 
7514 /*
7515  * do_reduction_op helper
7516  *
7517  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7518  * important for correct NaN propagation that we do these
7519  * operations in exactly the order specified by the pseudocode.
7520  *
7521  * This is a recursive function, TCG temps should be freed by the
7522  * calling function once it is done with the values.
7523  */
7524 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7525                                 int esize, int size, int vmap, TCGv_ptr fpst)
7526 {
7527     if (esize == size) {
7528         int element;
7529         MemOp msize = esize == 16 ? MO_16 : MO_32;
7530         TCGv_i32 tcg_elem;
7531 
7532         /* We should have one register left here */
7533         assert(ctpop8(vmap) == 1);
7534         element = ctz32(vmap);
7535         assert(element < 8);
7536 
7537         tcg_elem = tcg_temp_new_i32();
7538         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7539         return tcg_elem;
7540     } else {
7541         int bits = size / 2;
7542         int shift = ctpop8(vmap) / 2;
7543         int vmap_lo = (vmap >> shift) & vmap;
7544         int vmap_hi = (vmap & ~vmap_lo);
7545         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7546 
7547         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7548         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7549         tcg_res = tcg_temp_new_i32();
7550 
7551         switch (fpopcode) {
7552         case 0x0c: /* fmaxnmv half-precision */
7553             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7554             break;
7555         case 0x0f: /* fmaxv half-precision */
7556             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7557             break;
7558         case 0x1c: /* fminnmv half-precision */
7559             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7560             break;
7561         case 0x1f: /* fminv half-precision */
7562             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7563             break;
7564         case 0x2c: /* fmaxnmv */
7565             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7566             break;
7567         case 0x2f: /* fmaxv */
7568             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7569             break;
7570         case 0x3c: /* fminnmv */
7571             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7572             break;
7573         case 0x3f: /* fminv */
7574             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7575             break;
7576         default:
7577             g_assert_not_reached();
7578         }
7579         return tcg_res;
7580     }
7581 }
7582 
7583 /* AdvSIMD across lanes
7584  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7585  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7586  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7587  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7588  */
7589 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7590 {
7591     int rd = extract32(insn, 0, 5);
7592     int rn = extract32(insn, 5, 5);
7593     int size = extract32(insn, 22, 2);
7594     int opcode = extract32(insn, 12, 5);
7595     bool is_q = extract32(insn, 30, 1);
7596     bool is_u = extract32(insn, 29, 1);
7597     bool is_fp = false;
7598     bool is_min = false;
7599     int esize;
7600     int elements;
7601     int i;
7602     TCGv_i64 tcg_res, tcg_elt;
7603 
7604     switch (opcode) {
7605     case 0x1b: /* ADDV */
7606         if (is_u) {
7607             unallocated_encoding(s);
7608             return;
7609         }
7610         /* fall through */
7611     case 0x3: /* SADDLV, UADDLV */
7612     case 0xa: /* SMAXV, UMAXV */
7613     case 0x1a: /* SMINV, UMINV */
7614         if (size == 3 || (size == 2 && !is_q)) {
7615             unallocated_encoding(s);
7616             return;
7617         }
7618         break;
7619     case 0xc: /* FMAXNMV, FMINNMV */
7620     case 0xf: /* FMAXV, FMINV */
7621         /* Bit 1 of size field encodes min vs max and the actual size
7622          * depends on the encoding of the U bit. If not set (and FP16
7623          * enabled) then we do half-precision float instead of single
7624          * precision.
7625          */
7626         is_min = extract32(size, 1, 1);
7627         is_fp = true;
7628         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7629             size = 1;
7630         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7631             unallocated_encoding(s);
7632             return;
7633         } else {
7634             size = 2;
7635         }
7636         break;
7637     default:
7638         unallocated_encoding(s);
7639         return;
7640     }
7641 
7642     if (!fp_access_check(s)) {
7643         return;
7644     }
7645 
7646     esize = 8 << size;
7647     elements = (is_q ? 128 : 64) / esize;
7648 
7649     tcg_res = tcg_temp_new_i64();
7650     tcg_elt = tcg_temp_new_i64();
7651 
7652     /* These instructions operate across all lanes of a vector
7653      * to produce a single result. We can guarantee that a 64
7654      * bit intermediate is sufficient:
7655      *  + for [US]ADDLV the maximum element size is 32 bits, and
7656      *    the result type is 64 bits
7657      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7658      *    same as the element size, which is 32 bits at most
7659      * For the integer operations we can choose to work at 64
7660      * or 32 bits and truncate at the end; for simplicity
7661      * we use 64 bits always. The floating point
7662      * ops do require 32 bit intermediates, though.
7663      */
7664     if (!is_fp) {
7665         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7666 
7667         for (i = 1; i < elements; i++) {
7668             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7669 
7670             switch (opcode) {
7671             case 0x03: /* SADDLV / UADDLV */
7672             case 0x1b: /* ADDV */
7673                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7674                 break;
7675             case 0x0a: /* SMAXV / UMAXV */
7676                 if (is_u) {
7677                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7678                 } else {
7679                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7680                 }
7681                 break;
7682             case 0x1a: /* SMINV / UMINV */
7683                 if (is_u) {
7684                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7685                 } else {
7686                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7687                 }
7688                 break;
7689             default:
7690                 g_assert_not_reached();
7691             }
7692 
7693         }
7694     } else {
7695         /* Floating point vector reduction ops which work across 32
7696          * bit (single) or 16 bit (half-precision) intermediates.
7697          * Note that correct NaN propagation requires that we do these
7698          * operations in exactly the order specified by the pseudocode.
7699          */
7700         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7701         int fpopcode = opcode | is_min << 4 | is_u << 5;
7702         int vmap = (1 << elements) - 1;
7703         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7704                                              (is_q ? 128 : 64), vmap, fpst);
7705         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7706     }
7707 
7708     /* Now truncate the result to the width required for the final output */
7709     if (opcode == 0x03) {
7710         /* SADDLV, UADDLV: result is 2*esize */
7711         size++;
7712     }
7713 
7714     switch (size) {
7715     case 0:
7716         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7717         break;
7718     case 1:
7719         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7720         break;
7721     case 2:
7722         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7723         break;
7724     case 3:
7725         break;
7726     default:
7727         g_assert_not_reached();
7728     }
7729 
7730     write_fp_dreg(s, rd, tcg_res);
7731 }
7732 
7733 /* DUP (Element, Vector)
7734  *
7735  *  31  30   29              21 20    16 15        10  9    5 4    0
7736  * +---+---+-------------------+--------+-------------+------+------+
7737  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7738  * +---+---+-------------------+--------+-------------+------+------+
7739  *
7740  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7741  */
7742 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7743                              int imm5)
7744 {
7745     int size = ctz32(imm5);
7746     int index;
7747 
7748     if (size > 3 || (size == 3 && !is_q)) {
7749         unallocated_encoding(s);
7750         return;
7751     }
7752 
7753     if (!fp_access_check(s)) {
7754         return;
7755     }
7756 
7757     index = imm5 >> (size + 1);
7758     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7759                          vec_reg_offset(s, rn, index, size),
7760                          is_q ? 16 : 8, vec_full_reg_size(s));
7761 }
7762 
7763 /* DUP (element, scalar)
7764  *  31                   21 20    16 15        10  9    5 4    0
7765  * +-----------------------+--------+-------------+------+------+
7766  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7767  * +-----------------------+--------+-------------+------+------+
7768  */
7769 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7770                               int imm5)
7771 {
7772     int size = ctz32(imm5);
7773     int index;
7774     TCGv_i64 tmp;
7775 
7776     if (size > 3) {
7777         unallocated_encoding(s);
7778         return;
7779     }
7780 
7781     if (!fp_access_check(s)) {
7782         return;
7783     }
7784 
7785     index = imm5 >> (size + 1);
7786 
7787     /* This instruction just extracts the specified element and
7788      * zero-extends it into the bottom of the destination register.
7789      */
7790     tmp = tcg_temp_new_i64();
7791     read_vec_element(s, tmp, rn, index, size);
7792     write_fp_dreg(s, rd, tmp);
7793 }
7794 
7795 /* DUP (General)
7796  *
7797  *  31  30   29              21 20    16 15        10  9    5 4    0
7798  * +---+---+-------------------+--------+-------------+------+------+
7799  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7800  * +---+---+-------------------+--------+-------------+------+------+
7801  *
7802  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7803  */
7804 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7805                              int imm5)
7806 {
7807     int size = ctz32(imm5);
7808     uint32_t dofs, oprsz, maxsz;
7809 
7810     if (size > 3 || ((size == 3) && !is_q)) {
7811         unallocated_encoding(s);
7812         return;
7813     }
7814 
7815     if (!fp_access_check(s)) {
7816         return;
7817     }
7818 
7819     dofs = vec_full_reg_offset(s, rd);
7820     oprsz = is_q ? 16 : 8;
7821     maxsz = vec_full_reg_size(s);
7822 
7823     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7824 }
7825 
7826 /* INS (Element)
7827  *
7828  *  31                   21 20    16 15  14    11  10 9    5 4    0
7829  * +-----------------------+--------+------------+---+------+------+
7830  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7831  * +-----------------------+--------+------------+---+------+------+
7832  *
7833  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7834  * index: encoded in imm5<4:size+1>
7835  */
7836 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7837                              int imm4, int imm5)
7838 {
7839     int size = ctz32(imm5);
7840     int src_index, dst_index;
7841     TCGv_i64 tmp;
7842 
7843     if (size > 3) {
7844         unallocated_encoding(s);
7845         return;
7846     }
7847 
7848     if (!fp_access_check(s)) {
7849         return;
7850     }
7851 
7852     dst_index = extract32(imm5, 1+size, 5);
7853     src_index = extract32(imm4, size, 4);
7854 
7855     tmp = tcg_temp_new_i64();
7856 
7857     read_vec_element(s, tmp, rn, src_index, size);
7858     write_vec_element(s, tmp, rd, dst_index, size);
7859 
7860     /* INS is considered a 128-bit write for SVE. */
7861     clear_vec_high(s, true, rd);
7862 }
7863 
7864 
7865 /* INS (General)
7866  *
7867  *  31                   21 20    16 15        10  9    5 4    0
7868  * +-----------------------+--------+-------------+------+------+
7869  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7870  * +-----------------------+--------+-------------+------+------+
7871  *
7872  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7873  * index: encoded in imm5<4:size+1>
7874  */
7875 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7876 {
7877     int size = ctz32(imm5);
7878     int idx;
7879 
7880     if (size > 3) {
7881         unallocated_encoding(s);
7882         return;
7883     }
7884 
7885     if (!fp_access_check(s)) {
7886         return;
7887     }
7888 
7889     idx = extract32(imm5, 1 + size, 4 - size);
7890     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7891 
7892     /* INS is considered a 128-bit write for SVE. */
7893     clear_vec_high(s, true, rd);
7894 }
7895 
7896 /*
7897  * UMOV (General)
7898  * SMOV (General)
7899  *
7900  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7901  * +---+---+-------------------+--------+-------------+------+------+
7902  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7903  * +---+---+-------------------+--------+-------------+------+------+
7904  *
7905  * U: unsigned when set
7906  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7907  */
7908 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7909                                   int rn, int rd, int imm5)
7910 {
7911     int size = ctz32(imm5);
7912     int element;
7913     TCGv_i64 tcg_rd;
7914 
7915     /* Check for UnallocatedEncodings */
7916     if (is_signed) {
7917         if (size > 2 || (size == 2 && !is_q)) {
7918             unallocated_encoding(s);
7919             return;
7920         }
7921     } else {
7922         if (size > 3
7923             || (size < 3 && is_q)
7924             || (size == 3 && !is_q)) {
7925             unallocated_encoding(s);
7926             return;
7927         }
7928     }
7929 
7930     if (!fp_access_check(s)) {
7931         return;
7932     }
7933 
7934     element = extract32(imm5, 1+size, 4);
7935 
7936     tcg_rd = cpu_reg(s, rd);
7937     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7938     if (is_signed && !is_q) {
7939         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7940     }
7941 }
7942 
7943 /* AdvSIMD copy
7944  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7945  * +---+---+----+-----------------+------+---+------+---+------+------+
7946  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7947  * +---+---+----+-----------------+------+---+------+---+------+------+
7948  */
7949 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7950 {
7951     int rd = extract32(insn, 0, 5);
7952     int rn = extract32(insn, 5, 5);
7953     int imm4 = extract32(insn, 11, 4);
7954     int op = extract32(insn, 29, 1);
7955     int is_q = extract32(insn, 30, 1);
7956     int imm5 = extract32(insn, 16, 5);
7957 
7958     if (op) {
7959         if (is_q) {
7960             /* INS (element) */
7961             handle_simd_inse(s, rd, rn, imm4, imm5);
7962         } else {
7963             unallocated_encoding(s);
7964         }
7965     } else {
7966         switch (imm4) {
7967         case 0:
7968             /* DUP (element - vector) */
7969             handle_simd_dupe(s, is_q, rd, rn, imm5);
7970             break;
7971         case 1:
7972             /* DUP (general) */
7973             handle_simd_dupg(s, is_q, rd, rn, imm5);
7974             break;
7975         case 3:
7976             if (is_q) {
7977                 /* INS (general) */
7978                 handle_simd_insg(s, rd, rn, imm5);
7979             } else {
7980                 unallocated_encoding(s);
7981             }
7982             break;
7983         case 5:
7984         case 7:
7985             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7986             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7987             break;
7988         default:
7989             unallocated_encoding(s);
7990             break;
7991         }
7992     }
7993 }
7994 
7995 /* AdvSIMD modified immediate
7996  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7997  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7998  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7999  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8000  *
8001  * There are a number of operations that can be carried out here:
8002  *   MOVI - move (shifted) imm into register
8003  *   MVNI - move inverted (shifted) imm into register
8004  *   ORR  - bitwise OR of (shifted) imm with register
8005  *   BIC  - bitwise clear of (shifted) imm with register
8006  * With ARMv8.2 we also have:
8007  *   FMOV half-precision
8008  */
8009 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8010 {
8011     int rd = extract32(insn, 0, 5);
8012     int cmode = extract32(insn, 12, 4);
8013     int o2 = extract32(insn, 11, 1);
8014     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8015     bool is_neg = extract32(insn, 29, 1);
8016     bool is_q = extract32(insn, 30, 1);
8017     uint64_t imm = 0;
8018 
8019     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8020         /* Check for FMOV (vector, immediate) - half-precision */
8021         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8022             unallocated_encoding(s);
8023             return;
8024         }
8025     }
8026 
8027     if (!fp_access_check(s)) {
8028         return;
8029     }
8030 
8031     if (cmode == 15 && o2 && !is_neg) {
8032         /* FMOV (vector, immediate) - half-precision */
8033         imm = vfp_expand_imm(MO_16, abcdefgh);
8034         /* now duplicate across the lanes */
8035         imm = dup_const(MO_16, imm);
8036     } else {
8037         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8038     }
8039 
8040     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8041         /* MOVI or MVNI, with MVNI negation handled above.  */
8042         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8043                              vec_full_reg_size(s), imm);
8044     } else {
8045         /* ORR or BIC, with BIC negation to AND handled above.  */
8046         if (is_neg) {
8047             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8048         } else {
8049             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8050         }
8051     }
8052 }
8053 
8054 /* AdvSIMD scalar copy
8055  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8056  * +-----+----+-----------------+------+---+------+---+------+------+
8057  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8058  * +-----+----+-----------------+------+---+------+---+------+------+
8059  */
8060 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8061 {
8062     int rd = extract32(insn, 0, 5);
8063     int rn = extract32(insn, 5, 5);
8064     int imm4 = extract32(insn, 11, 4);
8065     int imm5 = extract32(insn, 16, 5);
8066     int op = extract32(insn, 29, 1);
8067 
8068     if (op != 0 || imm4 != 0) {
8069         unallocated_encoding(s);
8070         return;
8071     }
8072 
8073     /* DUP (element, scalar) */
8074     handle_simd_dupes(s, rd, rn, imm5);
8075 }
8076 
8077 /* AdvSIMD scalar pairwise
8078  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8079  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8080  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8081  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8082  */
8083 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8084 {
8085     int u = extract32(insn, 29, 1);
8086     int size = extract32(insn, 22, 2);
8087     int opcode = extract32(insn, 12, 5);
8088     int rn = extract32(insn, 5, 5);
8089     int rd = extract32(insn, 0, 5);
8090     TCGv_ptr fpst;
8091 
8092     /* For some ops (the FP ones), size[1] is part of the encoding.
8093      * For ADDP strictly it is not but size[1] is always 1 for valid
8094      * encodings.
8095      */
8096     opcode |= (extract32(size, 1, 1) << 5);
8097 
8098     switch (opcode) {
8099     case 0x3b: /* ADDP */
8100         if (u || size != 3) {
8101             unallocated_encoding(s);
8102             return;
8103         }
8104         if (!fp_access_check(s)) {
8105             return;
8106         }
8107 
8108         fpst = NULL;
8109         break;
8110     case 0xc: /* FMAXNMP */
8111     case 0xd: /* FADDP */
8112     case 0xf: /* FMAXP */
8113     case 0x2c: /* FMINNMP */
8114     case 0x2f: /* FMINP */
8115         /* FP op, size[0] is 32 or 64 bit*/
8116         if (!u) {
8117             if (!dc_isar_feature(aa64_fp16, s)) {
8118                 unallocated_encoding(s);
8119                 return;
8120             } else {
8121                 size = MO_16;
8122             }
8123         } else {
8124             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8125         }
8126 
8127         if (!fp_access_check(s)) {
8128             return;
8129         }
8130 
8131         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8132         break;
8133     default:
8134         unallocated_encoding(s);
8135         return;
8136     }
8137 
8138     if (size == MO_64) {
8139         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8140         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8141         TCGv_i64 tcg_res = tcg_temp_new_i64();
8142 
8143         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8144         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8145 
8146         switch (opcode) {
8147         case 0x3b: /* ADDP */
8148             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8149             break;
8150         case 0xc: /* FMAXNMP */
8151             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8152             break;
8153         case 0xd: /* FADDP */
8154             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8155             break;
8156         case 0xf: /* FMAXP */
8157             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8158             break;
8159         case 0x2c: /* FMINNMP */
8160             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8161             break;
8162         case 0x2f: /* FMINP */
8163             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8164             break;
8165         default:
8166             g_assert_not_reached();
8167         }
8168 
8169         write_fp_dreg(s, rd, tcg_res);
8170     } else {
8171         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8172         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8173         TCGv_i32 tcg_res = tcg_temp_new_i32();
8174 
8175         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8176         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8177 
8178         if (size == MO_16) {
8179             switch (opcode) {
8180             case 0xc: /* FMAXNMP */
8181                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8182                 break;
8183             case 0xd: /* FADDP */
8184                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8185                 break;
8186             case 0xf: /* FMAXP */
8187                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8188                 break;
8189             case 0x2c: /* FMINNMP */
8190                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8191                 break;
8192             case 0x2f: /* FMINP */
8193                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8194                 break;
8195             default:
8196                 g_assert_not_reached();
8197             }
8198         } else {
8199             switch (opcode) {
8200             case 0xc: /* FMAXNMP */
8201                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8202                 break;
8203             case 0xd: /* FADDP */
8204                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8205                 break;
8206             case 0xf: /* FMAXP */
8207                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8208                 break;
8209             case 0x2c: /* FMINNMP */
8210                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8211                 break;
8212             case 0x2f: /* FMINP */
8213                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8214                 break;
8215             default:
8216                 g_assert_not_reached();
8217             }
8218         }
8219 
8220         write_fp_sreg(s, rd, tcg_res);
8221     }
8222 }
8223 
8224 /*
8225  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8226  *
8227  * This code is handles the common shifting code and is used by both
8228  * the vector and scalar code.
8229  */
8230 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8231                                     TCGv_i64 tcg_rnd, bool accumulate,
8232                                     bool is_u, int size, int shift)
8233 {
8234     bool extended_result = false;
8235     bool round = tcg_rnd != NULL;
8236     int ext_lshift = 0;
8237     TCGv_i64 tcg_src_hi;
8238 
8239     if (round && size == 3) {
8240         extended_result = true;
8241         ext_lshift = 64 - shift;
8242         tcg_src_hi = tcg_temp_new_i64();
8243     } else if (shift == 64) {
8244         if (!accumulate && is_u) {
8245             /* result is zero */
8246             tcg_gen_movi_i64(tcg_res, 0);
8247             return;
8248         }
8249     }
8250 
8251     /* Deal with the rounding step */
8252     if (round) {
8253         if (extended_result) {
8254             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8255             if (!is_u) {
8256                 /* take care of sign extending tcg_res */
8257                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8258                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8259                                  tcg_src, tcg_src_hi,
8260                                  tcg_rnd, tcg_zero);
8261             } else {
8262                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8263                                  tcg_src, tcg_zero,
8264                                  tcg_rnd, tcg_zero);
8265             }
8266         } else {
8267             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8268         }
8269     }
8270 
8271     /* Now do the shift right */
8272     if (round && extended_result) {
8273         /* extended case, >64 bit precision required */
8274         if (ext_lshift == 0) {
8275             /* special case, only high bits matter */
8276             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8277         } else {
8278             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8279             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8280             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8281         }
8282     } else {
8283         if (is_u) {
8284             if (shift == 64) {
8285                 /* essentially shifting in 64 zeros */
8286                 tcg_gen_movi_i64(tcg_src, 0);
8287             } else {
8288                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8289             }
8290         } else {
8291             if (shift == 64) {
8292                 /* effectively extending the sign-bit */
8293                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8294             } else {
8295                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8296             }
8297         }
8298     }
8299 
8300     if (accumulate) {
8301         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8302     } else {
8303         tcg_gen_mov_i64(tcg_res, tcg_src);
8304     }
8305 }
8306 
8307 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8308 static void handle_scalar_simd_shri(DisasContext *s,
8309                                     bool is_u, int immh, int immb,
8310                                     int opcode, int rn, int rd)
8311 {
8312     const int size = 3;
8313     int immhb = immh << 3 | immb;
8314     int shift = 2 * (8 << size) - immhb;
8315     bool accumulate = false;
8316     bool round = false;
8317     bool insert = false;
8318     TCGv_i64 tcg_rn;
8319     TCGv_i64 tcg_rd;
8320     TCGv_i64 tcg_round;
8321 
8322     if (!extract32(immh, 3, 1)) {
8323         unallocated_encoding(s);
8324         return;
8325     }
8326 
8327     if (!fp_access_check(s)) {
8328         return;
8329     }
8330 
8331     switch (opcode) {
8332     case 0x02: /* SSRA / USRA (accumulate) */
8333         accumulate = true;
8334         break;
8335     case 0x04: /* SRSHR / URSHR (rounding) */
8336         round = true;
8337         break;
8338     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8339         accumulate = round = true;
8340         break;
8341     case 0x08: /* SRI */
8342         insert = true;
8343         break;
8344     }
8345 
8346     if (round) {
8347         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8348     } else {
8349         tcg_round = NULL;
8350     }
8351 
8352     tcg_rn = read_fp_dreg(s, rn);
8353     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8354 
8355     if (insert) {
8356         /* shift count same as element size is valid but does nothing;
8357          * special case to avoid potential shift by 64.
8358          */
8359         int esize = 8 << size;
8360         if (shift != esize) {
8361             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8362             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8363         }
8364     } else {
8365         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8366                                 accumulate, is_u, size, shift);
8367     }
8368 
8369     write_fp_dreg(s, rd, tcg_rd);
8370 }
8371 
8372 /* SHL/SLI - Scalar shift left */
8373 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8374                                     int immh, int immb, int opcode,
8375                                     int rn, int rd)
8376 {
8377     int size = 32 - clz32(immh) - 1;
8378     int immhb = immh << 3 | immb;
8379     int shift = immhb - (8 << size);
8380     TCGv_i64 tcg_rn;
8381     TCGv_i64 tcg_rd;
8382 
8383     if (!extract32(immh, 3, 1)) {
8384         unallocated_encoding(s);
8385         return;
8386     }
8387 
8388     if (!fp_access_check(s)) {
8389         return;
8390     }
8391 
8392     tcg_rn = read_fp_dreg(s, rn);
8393     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8394 
8395     if (insert) {
8396         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8397     } else {
8398         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8399     }
8400 
8401     write_fp_dreg(s, rd, tcg_rd);
8402 }
8403 
8404 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8405  * (signed/unsigned) narrowing */
8406 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8407                                    bool is_u_shift, bool is_u_narrow,
8408                                    int immh, int immb, int opcode,
8409                                    int rn, int rd)
8410 {
8411     int immhb = immh << 3 | immb;
8412     int size = 32 - clz32(immh) - 1;
8413     int esize = 8 << size;
8414     int shift = (2 * esize) - immhb;
8415     int elements = is_scalar ? 1 : (64 / esize);
8416     bool round = extract32(opcode, 0, 1);
8417     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8418     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8419     TCGv_i32 tcg_rd_narrowed;
8420     TCGv_i64 tcg_final;
8421 
8422     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8423         { gen_helper_neon_narrow_sat_s8,
8424           gen_helper_neon_unarrow_sat8 },
8425         { gen_helper_neon_narrow_sat_s16,
8426           gen_helper_neon_unarrow_sat16 },
8427         { gen_helper_neon_narrow_sat_s32,
8428           gen_helper_neon_unarrow_sat32 },
8429         { NULL, NULL },
8430     };
8431     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8432         gen_helper_neon_narrow_sat_u8,
8433         gen_helper_neon_narrow_sat_u16,
8434         gen_helper_neon_narrow_sat_u32,
8435         NULL
8436     };
8437     NeonGenNarrowEnvFn *narrowfn;
8438 
8439     int i;
8440 
8441     assert(size < 4);
8442 
8443     if (extract32(immh, 3, 1)) {
8444         unallocated_encoding(s);
8445         return;
8446     }
8447 
8448     if (!fp_access_check(s)) {
8449         return;
8450     }
8451 
8452     if (is_u_shift) {
8453         narrowfn = unsigned_narrow_fns[size];
8454     } else {
8455         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8456     }
8457 
8458     tcg_rn = tcg_temp_new_i64();
8459     tcg_rd = tcg_temp_new_i64();
8460     tcg_rd_narrowed = tcg_temp_new_i32();
8461     tcg_final = tcg_temp_new_i64();
8462 
8463     if (round) {
8464         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8465     } else {
8466         tcg_round = NULL;
8467     }
8468 
8469     for (i = 0; i < elements; i++) {
8470         read_vec_element(s, tcg_rn, rn, i, ldop);
8471         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8472                                 false, is_u_shift, size+1, shift);
8473         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8474         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8475         if (i == 0) {
8476             tcg_gen_mov_i64(tcg_final, tcg_rd);
8477         } else {
8478             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8479         }
8480     }
8481 
8482     if (!is_q) {
8483         write_vec_element(s, tcg_final, rd, 0, MO_64);
8484     } else {
8485         write_vec_element(s, tcg_final, rd, 1, MO_64);
8486     }
8487     clear_vec_high(s, is_q, rd);
8488 }
8489 
8490 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8491 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8492                              bool src_unsigned, bool dst_unsigned,
8493                              int immh, int immb, int rn, int rd)
8494 {
8495     int immhb = immh << 3 | immb;
8496     int size = 32 - clz32(immh) - 1;
8497     int shift = immhb - (8 << size);
8498     int pass;
8499 
8500     assert(immh != 0);
8501     assert(!(scalar && is_q));
8502 
8503     if (!scalar) {
8504         if (!is_q && extract32(immh, 3, 1)) {
8505             unallocated_encoding(s);
8506             return;
8507         }
8508 
8509         /* Since we use the variable-shift helpers we must
8510          * replicate the shift count into each element of
8511          * the tcg_shift value.
8512          */
8513         switch (size) {
8514         case 0:
8515             shift |= shift << 8;
8516             /* fall through */
8517         case 1:
8518             shift |= shift << 16;
8519             break;
8520         case 2:
8521         case 3:
8522             break;
8523         default:
8524             g_assert_not_reached();
8525         }
8526     }
8527 
8528     if (!fp_access_check(s)) {
8529         return;
8530     }
8531 
8532     if (size == 3) {
8533         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8534         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8535             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8536             { NULL, gen_helper_neon_qshl_u64 },
8537         };
8538         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8539         int maxpass = is_q ? 2 : 1;
8540 
8541         for (pass = 0; pass < maxpass; pass++) {
8542             TCGv_i64 tcg_op = tcg_temp_new_i64();
8543 
8544             read_vec_element(s, tcg_op, rn, pass, MO_64);
8545             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8546             write_vec_element(s, tcg_op, rd, pass, MO_64);
8547         }
8548         clear_vec_high(s, is_q, rd);
8549     } else {
8550         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8551         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8552             {
8553                 { gen_helper_neon_qshl_s8,
8554                   gen_helper_neon_qshl_s16,
8555                   gen_helper_neon_qshl_s32 },
8556                 { gen_helper_neon_qshlu_s8,
8557                   gen_helper_neon_qshlu_s16,
8558                   gen_helper_neon_qshlu_s32 }
8559             }, {
8560                 { NULL, NULL, NULL },
8561                 { gen_helper_neon_qshl_u8,
8562                   gen_helper_neon_qshl_u16,
8563                   gen_helper_neon_qshl_u32 }
8564             }
8565         };
8566         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8567         MemOp memop = scalar ? size : MO_32;
8568         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8569 
8570         for (pass = 0; pass < maxpass; pass++) {
8571             TCGv_i32 tcg_op = tcg_temp_new_i32();
8572 
8573             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8574             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8575             if (scalar) {
8576                 switch (size) {
8577                 case 0:
8578                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8579                     break;
8580                 case 1:
8581                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8582                     break;
8583                 case 2:
8584                     break;
8585                 default:
8586                     g_assert_not_reached();
8587                 }
8588                 write_fp_sreg(s, rd, tcg_op);
8589             } else {
8590                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8591             }
8592         }
8593 
8594         if (!scalar) {
8595             clear_vec_high(s, is_q, rd);
8596         }
8597     }
8598 }
8599 
8600 /* Common vector code for handling integer to FP conversion */
8601 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8602                                    int elements, int is_signed,
8603                                    int fracbits, int size)
8604 {
8605     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8606     TCGv_i32 tcg_shift = NULL;
8607 
8608     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8609     int pass;
8610 
8611     if (fracbits || size == MO_64) {
8612         tcg_shift = tcg_constant_i32(fracbits);
8613     }
8614 
8615     if (size == MO_64) {
8616         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8617         TCGv_i64 tcg_double = tcg_temp_new_i64();
8618 
8619         for (pass = 0; pass < elements; pass++) {
8620             read_vec_element(s, tcg_int64, rn, pass, mop);
8621 
8622             if (is_signed) {
8623                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8624                                      tcg_shift, tcg_fpst);
8625             } else {
8626                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8627                                      tcg_shift, tcg_fpst);
8628             }
8629             if (elements == 1) {
8630                 write_fp_dreg(s, rd, tcg_double);
8631             } else {
8632                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8633             }
8634         }
8635     } else {
8636         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8637         TCGv_i32 tcg_float = tcg_temp_new_i32();
8638 
8639         for (pass = 0; pass < elements; pass++) {
8640             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8641 
8642             switch (size) {
8643             case MO_32:
8644                 if (fracbits) {
8645                     if (is_signed) {
8646                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8647                                              tcg_shift, tcg_fpst);
8648                     } else {
8649                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8650                                              tcg_shift, tcg_fpst);
8651                     }
8652                 } else {
8653                     if (is_signed) {
8654                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8655                     } else {
8656                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8657                     }
8658                 }
8659                 break;
8660             case MO_16:
8661                 if (fracbits) {
8662                     if (is_signed) {
8663                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8664                                              tcg_shift, tcg_fpst);
8665                     } else {
8666                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8667                                              tcg_shift, tcg_fpst);
8668                     }
8669                 } else {
8670                     if (is_signed) {
8671                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8672                     } else {
8673                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8674                     }
8675                 }
8676                 break;
8677             default:
8678                 g_assert_not_reached();
8679             }
8680 
8681             if (elements == 1) {
8682                 write_fp_sreg(s, rd, tcg_float);
8683             } else {
8684                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8685             }
8686         }
8687     }
8688 
8689     clear_vec_high(s, elements << size == 16, rd);
8690 }
8691 
8692 /* UCVTF/SCVTF - Integer to FP conversion */
8693 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8694                                          bool is_q, bool is_u,
8695                                          int immh, int immb, int opcode,
8696                                          int rn, int rd)
8697 {
8698     int size, elements, fracbits;
8699     int immhb = immh << 3 | immb;
8700 
8701     if (immh & 8) {
8702         size = MO_64;
8703         if (!is_scalar && !is_q) {
8704             unallocated_encoding(s);
8705             return;
8706         }
8707     } else if (immh & 4) {
8708         size = MO_32;
8709     } else if (immh & 2) {
8710         size = MO_16;
8711         if (!dc_isar_feature(aa64_fp16, s)) {
8712             unallocated_encoding(s);
8713             return;
8714         }
8715     } else {
8716         /* immh == 0 would be a failure of the decode logic */
8717         g_assert(immh == 1);
8718         unallocated_encoding(s);
8719         return;
8720     }
8721 
8722     if (is_scalar) {
8723         elements = 1;
8724     } else {
8725         elements = (8 << is_q) >> size;
8726     }
8727     fracbits = (16 << size) - immhb;
8728 
8729     if (!fp_access_check(s)) {
8730         return;
8731     }
8732 
8733     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8734 }
8735 
8736 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8737 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8738                                          bool is_q, bool is_u,
8739                                          int immh, int immb, int rn, int rd)
8740 {
8741     int immhb = immh << 3 | immb;
8742     int pass, size, fracbits;
8743     TCGv_ptr tcg_fpstatus;
8744     TCGv_i32 tcg_rmode, tcg_shift;
8745 
8746     if (immh & 0x8) {
8747         size = MO_64;
8748         if (!is_scalar && !is_q) {
8749             unallocated_encoding(s);
8750             return;
8751         }
8752     } else if (immh & 0x4) {
8753         size = MO_32;
8754     } else if (immh & 0x2) {
8755         size = MO_16;
8756         if (!dc_isar_feature(aa64_fp16, s)) {
8757             unallocated_encoding(s);
8758             return;
8759         }
8760     } else {
8761         /* Should have split out AdvSIMD modified immediate earlier.  */
8762         assert(immh == 1);
8763         unallocated_encoding(s);
8764         return;
8765     }
8766 
8767     if (!fp_access_check(s)) {
8768         return;
8769     }
8770 
8771     assert(!(is_scalar && is_q));
8772 
8773     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8774     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8775     fracbits = (16 << size) - immhb;
8776     tcg_shift = tcg_constant_i32(fracbits);
8777 
8778     if (size == MO_64) {
8779         int maxpass = is_scalar ? 1 : 2;
8780 
8781         for (pass = 0; pass < maxpass; pass++) {
8782             TCGv_i64 tcg_op = tcg_temp_new_i64();
8783 
8784             read_vec_element(s, tcg_op, rn, pass, MO_64);
8785             if (is_u) {
8786                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8787             } else {
8788                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8789             }
8790             write_vec_element(s, tcg_op, rd, pass, MO_64);
8791         }
8792         clear_vec_high(s, is_q, rd);
8793     } else {
8794         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8795         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8796 
8797         switch (size) {
8798         case MO_16:
8799             if (is_u) {
8800                 fn = gen_helper_vfp_touhh;
8801             } else {
8802                 fn = gen_helper_vfp_toshh;
8803             }
8804             break;
8805         case MO_32:
8806             if (is_u) {
8807                 fn = gen_helper_vfp_touls;
8808             } else {
8809                 fn = gen_helper_vfp_tosls;
8810             }
8811             break;
8812         default:
8813             g_assert_not_reached();
8814         }
8815 
8816         for (pass = 0; pass < maxpass; pass++) {
8817             TCGv_i32 tcg_op = tcg_temp_new_i32();
8818 
8819             read_vec_element_i32(s, tcg_op, rn, pass, size);
8820             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8821             if (is_scalar) {
8822                 write_fp_sreg(s, rd, tcg_op);
8823             } else {
8824                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8825             }
8826         }
8827         if (!is_scalar) {
8828             clear_vec_high(s, is_q, rd);
8829         }
8830     }
8831 
8832     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8833 }
8834 
8835 /* AdvSIMD scalar shift by immediate
8836  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8837  * +-----+---+-------------+------+------+--------+---+------+------+
8838  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8839  * +-----+---+-------------+------+------+--------+---+------+------+
8840  *
8841  * This is the scalar version so it works on a fixed sized registers
8842  */
8843 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8844 {
8845     int rd = extract32(insn, 0, 5);
8846     int rn = extract32(insn, 5, 5);
8847     int opcode = extract32(insn, 11, 5);
8848     int immb = extract32(insn, 16, 3);
8849     int immh = extract32(insn, 19, 4);
8850     bool is_u = extract32(insn, 29, 1);
8851 
8852     if (immh == 0) {
8853         unallocated_encoding(s);
8854         return;
8855     }
8856 
8857     switch (opcode) {
8858     case 0x08: /* SRI */
8859         if (!is_u) {
8860             unallocated_encoding(s);
8861             return;
8862         }
8863         /* fall through */
8864     case 0x00: /* SSHR / USHR */
8865     case 0x02: /* SSRA / USRA */
8866     case 0x04: /* SRSHR / URSHR */
8867     case 0x06: /* SRSRA / URSRA */
8868         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8869         break;
8870     case 0x0a: /* SHL / SLI */
8871         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8872         break;
8873     case 0x1c: /* SCVTF, UCVTF */
8874         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8875                                      opcode, rn, rd);
8876         break;
8877     case 0x10: /* SQSHRUN, SQSHRUN2 */
8878     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8879         if (!is_u) {
8880             unallocated_encoding(s);
8881             return;
8882         }
8883         handle_vec_simd_sqshrn(s, true, false, false, true,
8884                                immh, immb, opcode, rn, rd);
8885         break;
8886     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8887     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8888         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8889                                immh, immb, opcode, rn, rd);
8890         break;
8891     case 0xc: /* SQSHLU */
8892         if (!is_u) {
8893             unallocated_encoding(s);
8894             return;
8895         }
8896         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8897         break;
8898     case 0xe: /* SQSHL, UQSHL */
8899         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8900         break;
8901     case 0x1f: /* FCVTZS, FCVTZU */
8902         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8903         break;
8904     default:
8905         unallocated_encoding(s);
8906         break;
8907     }
8908 }
8909 
8910 /* AdvSIMD scalar three different
8911  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8912  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8913  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8914  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8915  */
8916 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8917 {
8918     bool is_u = extract32(insn, 29, 1);
8919     int size = extract32(insn, 22, 2);
8920     int opcode = extract32(insn, 12, 4);
8921     int rm = extract32(insn, 16, 5);
8922     int rn = extract32(insn, 5, 5);
8923     int rd = extract32(insn, 0, 5);
8924 
8925     if (is_u) {
8926         unallocated_encoding(s);
8927         return;
8928     }
8929 
8930     switch (opcode) {
8931     case 0x9: /* SQDMLAL, SQDMLAL2 */
8932     case 0xb: /* SQDMLSL, SQDMLSL2 */
8933     case 0xd: /* SQDMULL, SQDMULL2 */
8934         if (size == 0 || size == 3) {
8935             unallocated_encoding(s);
8936             return;
8937         }
8938         break;
8939     default:
8940         unallocated_encoding(s);
8941         return;
8942     }
8943 
8944     if (!fp_access_check(s)) {
8945         return;
8946     }
8947 
8948     if (size == 2) {
8949         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8950         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8951         TCGv_i64 tcg_res = tcg_temp_new_i64();
8952 
8953         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8954         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8955 
8956         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8957         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8958 
8959         switch (opcode) {
8960         case 0xd: /* SQDMULL, SQDMULL2 */
8961             break;
8962         case 0xb: /* SQDMLSL, SQDMLSL2 */
8963             tcg_gen_neg_i64(tcg_res, tcg_res);
8964             /* fall through */
8965         case 0x9: /* SQDMLAL, SQDMLAL2 */
8966             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8967             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8968                                               tcg_res, tcg_op1);
8969             break;
8970         default:
8971             g_assert_not_reached();
8972         }
8973 
8974         write_fp_dreg(s, rd, tcg_res);
8975     } else {
8976         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8977         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8978         TCGv_i64 tcg_res = tcg_temp_new_i64();
8979 
8980         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8981         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8982 
8983         switch (opcode) {
8984         case 0xd: /* SQDMULL, SQDMULL2 */
8985             break;
8986         case 0xb: /* SQDMLSL, SQDMLSL2 */
8987             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8988             /* fall through */
8989         case 0x9: /* SQDMLAL, SQDMLAL2 */
8990         {
8991             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8992             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8993             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8994                                               tcg_res, tcg_op3);
8995             break;
8996         }
8997         default:
8998             g_assert_not_reached();
8999         }
9000 
9001         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9002         write_fp_dreg(s, rd, tcg_res);
9003     }
9004 }
9005 
9006 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9007                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9008 {
9009     /* Handle 64x64->64 opcodes which are shared between the scalar
9010      * and vector 3-same groups. We cover every opcode where size == 3
9011      * is valid in either the three-reg-same (integer, not pairwise)
9012      * or scalar-three-reg-same groups.
9013      */
9014     TCGCond cond;
9015 
9016     switch (opcode) {
9017     case 0x1: /* SQADD */
9018         if (u) {
9019             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9020         } else {
9021             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9022         }
9023         break;
9024     case 0x5: /* SQSUB */
9025         if (u) {
9026             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9027         } else {
9028             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9029         }
9030         break;
9031     case 0x6: /* CMGT, CMHI */
9032         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9033          * We implement this using setcond (test) and then negating.
9034          */
9035         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9036     do_cmop:
9037         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9038         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9039         break;
9040     case 0x7: /* CMGE, CMHS */
9041         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9042         goto do_cmop;
9043     case 0x11: /* CMTST, CMEQ */
9044         if (u) {
9045             cond = TCG_COND_EQ;
9046             goto do_cmop;
9047         }
9048         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9049         break;
9050     case 0x8: /* SSHL, USHL */
9051         if (u) {
9052             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9053         } else {
9054             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9055         }
9056         break;
9057     case 0x9: /* SQSHL, UQSHL */
9058         if (u) {
9059             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9060         } else {
9061             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9062         }
9063         break;
9064     case 0xa: /* SRSHL, URSHL */
9065         if (u) {
9066             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9067         } else {
9068             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9069         }
9070         break;
9071     case 0xb: /* SQRSHL, UQRSHL */
9072         if (u) {
9073             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9074         } else {
9075             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9076         }
9077         break;
9078     case 0x10: /* ADD, SUB */
9079         if (u) {
9080             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9081         } else {
9082             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9083         }
9084         break;
9085     default:
9086         g_assert_not_reached();
9087     }
9088 }
9089 
9090 /* Handle the 3-same-operands float operations; shared by the scalar
9091  * and vector encodings. The caller must filter out any encodings
9092  * not allocated for the encoding it is dealing with.
9093  */
9094 static void handle_3same_float(DisasContext *s, int size, int elements,
9095                                int fpopcode, int rd, int rn, int rm)
9096 {
9097     int pass;
9098     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9099 
9100     for (pass = 0; pass < elements; pass++) {
9101         if (size) {
9102             /* Double */
9103             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9104             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9105             TCGv_i64 tcg_res = tcg_temp_new_i64();
9106 
9107             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9108             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9109 
9110             switch (fpopcode) {
9111             case 0x39: /* FMLS */
9112                 /* As usual for ARM, separate negation for fused multiply-add */
9113                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9114                 /* fall through */
9115             case 0x19: /* FMLA */
9116                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9117                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9118                                        tcg_res, fpst);
9119                 break;
9120             case 0x18: /* FMAXNM */
9121                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9122                 break;
9123             case 0x1a: /* FADD */
9124                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9125                 break;
9126             case 0x1b: /* FMULX */
9127                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9128                 break;
9129             case 0x1c: /* FCMEQ */
9130                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9131                 break;
9132             case 0x1e: /* FMAX */
9133                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9134                 break;
9135             case 0x1f: /* FRECPS */
9136                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9137                 break;
9138             case 0x38: /* FMINNM */
9139                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9140                 break;
9141             case 0x3a: /* FSUB */
9142                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9143                 break;
9144             case 0x3e: /* FMIN */
9145                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9146                 break;
9147             case 0x3f: /* FRSQRTS */
9148                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9149                 break;
9150             case 0x5b: /* FMUL */
9151                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9152                 break;
9153             case 0x5c: /* FCMGE */
9154                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9155                 break;
9156             case 0x5d: /* FACGE */
9157                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9158                 break;
9159             case 0x5f: /* FDIV */
9160                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9161                 break;
9162             case 0x7a: /* FABD */
9163                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9164                 gen_helper_vfp_absd(tcg_res, tcg_res);
9165                 break;
9166             case 0x7c: /* FCMGT */
9167                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9168                 break;
9169             case 0x7d: /* FACGT */
9170                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9171                 break;
9172             default:
9173                 g_assert_not_reached();
9174             }
9175 
9176             write_vec_element(s, tcg_res, rd, pass, MO_64);
9177         } else {
9178             /* Single */
9179             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9180             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9181             TCGv_i32 tcg_res = tcg_temp_new_i32();
9182 
9183             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9184             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9185 
9186             switch (fpopcode) {
9187             case 0x39: /* FMLS */
9188                 /* As usual for ARM, separate negation for fused multiply-add */
9189                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9190                 /* fall through */
9191             case 0x19: /* FMLA */
9192                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9193                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9194                                        tcg_res, fpst);
9195                 break;
9196             case 0x1a: /* FADD */
9197                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9198                 break;
9199             case 0x1b: /* FMULX */
9200                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9201                 break;
9202             case 0x1c: /* FCMEQ */
9203                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9204                 break;
9205             case 0x1e: /* FMAX */
9206                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9207                 break;
9208             case 0x1f: /* FRECPS */
9209                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9210                 break;
9211             case 0x18: /* FMAXNM */
9212                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9213                 break;
9214             case 0x38: /* FMINNM */
9215                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9216                 break;
9217             case 0x3a: /* FSUB */
9218                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9219                 break;
9220             case 0x3e: /* FMIN */
9221                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9222                 break;
9223             case 0x3f: /* FRSQRTS */
9224                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9225                 break;
9226             case 0x5b: /* FMUL */
9227                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9228                 break;
9229             case 0x5c: /* FCMGE */
9230                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9231                 break;
9232             case 0x5d: /* FACGE */
9233                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9234                 break;
9235             case 0x5f: /* FDIV */
9236                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9237                 break;
9238             case 0x7a: /* FABD */
9239                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9240                 gen_helper_vfp_abss(tcg_res, tcg_res);
9241                 break;
9242             case 0x7c: /* FCMGT */
9243                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9244                 break;
9245             case 0x7d: /* FACGT */
9246                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9247                 break;
9248             default:
9249                 g_assert_not_reached();
9250             }
9251 
9252             if (elements == 1) {
9253                 /* scalar single so clear high part */
9254                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9255 
9256                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9257                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9258             } else {
9259                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9260             }
9261         }
9262     }
9263 
9264     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9265 }
9266 
9267 /* AdvSIMD scalar three same
9268  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9269  * +-----+---+-----------+------+---+------+--------+---+------+------+
9270  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9271  * +-----+---+-----------+------+---+------+--------+---+------+------+
9272  */
9273 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9274 {
9275     int rd = extract32(insn, 0, 5);
9276     int rn = extract32(insn, 5, 5);
9277     int opcode = extract32(insn, 11, 5);
9278     int rm = extract32(insn, 16, 5);
9279     int size = extract32(insn, 22, 2);
9280     bool u = extract32(insn, 29, 1);
9281     TCGv_i64 tcg_rd;
9282 
9283     if (opcode >= 0x18) {
9284         /* Floating point: U, size[1] and opcode indicate operation */
9285         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9286         switch (fpopcode) {
9287         case 0x1b: /* FMULX */
9288         case 0x1f: /* FRECPS */
9289         case 0x3f: /* FRSQRTS */
9290         case 0x5d: /* FACGE */
9291         case 0x7d: /* FACGT */
9292         case 0x1c: /* FCMEQ */
9293         case 0x5c: /* FCMGE */
9294         case 0x7c: /* FCMGT */
9295         case 0x7a: /* FABD */
9296             break;
9297         default:
9298             unallocated_encoding(s);
9299             return;
9300         }
9301 
9302         if (!fp_access_check(s)) {
9303             return;
9304         }
9305 
9306         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9307         return;
9308     }
9309 
9310     switch (opcode) {
9311     case 0x1: /* SQADD, UQADD */
9312     case 0x5: /* SQSUB, UQSUB */
9313     case 0x9: /* SQSHL, UQSHL */
9314     case 0xb: /* SQRSHL, UQRSHL */
9315         break;
9316     case 0x8: /* SSHL, USHL */
9317     case 0xa: /* SRSHL, URSHL */
9318     case 0x6: /* CMGT, CMHI */
9319     case 0x7: /* CMGE, CMHS */
9320     case 0x11: /* CMTST, CMEQ */
9321     case 0x10: /* ADD, SUB (vector) */
9322         if (size != 3) {
9323             unallocated_encoding(s);
9324             return;
9325         }
9326         break;
9327     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9328         if (size != 1 && size != 2) {
9329             unallocated_encoding(s);
9330             return;
9331         }
9332         break;
9333     default:
9334         unallocated_encoding(s);
9335         return;
9336     }
9337 
9338     if (!fp_access_check(s)) {
9339         return;
9340     }
9341 
9342     tcg_rd = tcg_temp_new_i64();
9343 
9344     if (size == 3) {
9345         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9346         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9347 
9348         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9349     } else {
9350         /* Do a single operation on the lowest element in the vector.
9351          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9352          * no side effects for all these operations.
9353          * OPTME: special-purpose helpers would avoid doing some
9354          * unnecessary work in the helper for the 8 and 16 bit cases.
9355          */
9356         NeonGenTwoOpEnvFn *genenvfn;
9357         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9358         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9359         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9360 
9361         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9362         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9363 
9364         switch (opcode) {
9365         case 0x1: /* SQADD, UQADD */
9366         {
9367             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9368                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9369                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9370                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9371             };
9372             genenvfn = fns[size][u];
9373             break;
9374         }
9375         case 0x5: /* SQSUB, UQSUB */
9376         {
9377             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9378                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9379                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9380                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9381             };
9382             genenvfn = fns[size][u];
9383             break;
9384         }
9385         case 0x9: /* SQSHL, UQSHL */
9386         {
9387             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9388                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9389                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9390                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9391             };
9392             genenvfn = fns[size][u];
9393             break;
9394         }
9395         case 0xb: /* SQRSHL, UQRSHL */
9396         {
9397             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9398                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9399                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9400                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9401             };
9402             genenvfn = fns[size][u];
9403             break;
9404         }
9405         case 0x16: /* SQDMULH, SQRDMULH */
9406         {
9407             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9408                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9409                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9410             };
9411             assert(size == 1 || size == 2);
9412             genenvfn = fns[size - 1][u];
9413             break;
9414         }
9415         default:
9416             g_assert_not_reached();
9417         }
9418 
9419         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9420         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9421     }
9422 
9423     write_fp_dreg(s, rd, tcg_rd);
9424 }
9425 
9426 /* AdvSIMD scalar three same FP16
9427  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9428  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9429  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9430  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9431  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9432  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9433  */
9434 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9435                                                   uint32_t insn)
9436 {
9437     int rd = extract32(insn, 0, 5);
9438     int rn = extract32(insn, 5, 5);
9439     int opcode = extract32(insn, 11, 3);
9440     int rm = extract32(insn, 16, 5);
9441     bool u = extract32(insn, 29, 1);
9442     bool a = extract32(insn, 23, 1);
9443     int fpopcode = opcode | (a << 3) |  (u << 4);
9444     TCGv_ptr fpst;
9445     TCGv_i32 tcg_op1;
9446     TCGv_i32 tcg_op2;
9447     TCGv_i32 tcg_res;
9448 
9449     switch (fpopcode) {
9450     case 0x03: /* FMULX */
9451     case 0x04: /* FCMEQ (reg) */
9452     case 0x07: /* FRECPS */
9453     case 0x0f: /* FRSQRTS */
9454     case 0x14: /* FCMGE (reg) */
9455     case 0x15: /* FACGE */
9456     case 0x1a: /* FABD */
9457     case 0x1c: /* FCMGT (reg) */
9458     case 0x1d: /* FACGT */
9459         break;
9460     default:
9461         unallocated_encoding(s);
9462         return;
9463     }
9464 
9465     if (!dc_isar_feature(aa64_fp16, s)) {
9466         unallocated_encoding(s);
9467     }
9468 
9469     if (!fp_access_check(s)) {
9470         return;
9471     }
9472 
9473     fpst = fpstatus_ptr(FPST_FPCR_F16);
9474 
9475     tcg_op1 = read_fp_hreg(s, rn);
9476     tcg_op2 = read_fp_hreg(s, rm);
9477     tcg_res = tcg_temp_new_i32();
9478 
9479     switch (fpopcode) {
9480     case 0x03: /* FMULX */
9481         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9482         break;
9483     case 0x04: /* FCMEQ (reg) */
9484         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9485         break;
9486     case 0x07: /* FRECPS */
9487         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9488         break;
9489     case 0x0f: /* FRSQRTS */
9490         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9491         break;
9492     case 0x14: /* FCMGE (reg) */
9493         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9494         break;
9495     case 0x15: /* FACGE */
9496         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9497         break;
9498     case 0x1a: /* FABD */
9499         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9500         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9501         break;
9502     case 0x1c: /* FCMGT (reg) */
9503         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9504         break;
9505     case 0x1d: /* FACGT */
9506         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9507         break;
9508     default:
9509         g_assert_not_reached();
9510     }
9511 
9512     write_fp_sreg(s, rd, tcg_res);
9513 }
9514 
9515 /* AdvSIMD scalar three same extra
9516  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9517  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9518  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9519  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9520  */
9521 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9522                                                    uint32_t insn)
9523 {
9524     int rd = extract32(insn, 0, 5);
9525     int rn = extract32(insn, 5, 5);
9526     int opcode = extract32(insn, 11, 4);
9527     int rm = extract32(insn, 16, 5);
9528     int size = extract32(insn, 22, 2);
9529     bool u = extract32(insn, 29, 1);
9530     TCGv_i32 ele1, ele2, ele3;
9531     TCGv_i64 res;
9532     bool feature;
9533 
9534     switch (u * 16 + opcode) {
9535     case 0x10: /* SQRDMLAH (vector) */
9536     case 0x11: /* SQRDMLSH (vector) */
9537         if (size != 1 && size != 2) {
9538             unallocated_encoding(s);
9539             return;
9540         }
9541         feature = dc_isar_feature(aa64_rdm, s);
9542         break;
9543     default:
9544         unallocated_encoding(s);
9545         return;
9546     }
9547     if (!feature) {
9548         unallocated_encoding(s);
9549         return;
9550     }
9551     if (!fp_access_check(s)) {
9552         return;
9553     }
9554 
9555     /* Do a single operation on the lowest element in the vector.
9556      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9557      * with no side effects for all these operations.
9558      * OPTME: special-purpose helpers would avoid doing some
9559      * unnecessary work in the helper for the 16 bit cases.
9560      */
9561     ele1 = tcg_temp_new_i32();
9562     ele2 = tcg_temp_new_i32();
9563     ele3 = tcg_temp_new_i32();
9564 
9565     read_vec_element_i32(s, ele1, rn, 0, size);
9566     read_vec_element_i32(s, ele2, rm, 0, size);
9567     read_vec_element_i32(s, ele3, rd, 0, size);
9568 
9569     switch (opcode) {
9570     case 0x0: /* SQRDMLAH */
9571         if (size == 1) {
9572             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9573         } else {
9574             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9575         }
9576         break;
9577     case 0x1: /* SQRDMLSH */
9578         if (size == 1) {
9579             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9580         } else {
9581             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9582         }
9583         break;
9584     default:
9585         g_assert_not_reached();
9586     }
9587 
9588     res = tcg_temp_new_i64();
9589     tcg_gen_extu_i32_i64(res, ele3);
9590     write_fp_dreg(s, rd, res);
9591 }
9592 
9593 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9594                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9595                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9596 {
9597     /* Handle 64->64 opcodes which are shared between the scalar and
9598      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9599      * is valid in either group and also the double-precision fp ops.
9600      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9601      * requires them.
9602      */
9603     TCGCond cond;
9604 
9605     switch (opcode) {
9606     case 0x4: /* CLS, CLZ */
9607         if (u) {
9608             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9609         } else {
9610             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9611         }
9612         break;
9613     case 0x5: /* NOT */
9614         /* This opcode is shared with CNT and RBIT but we have earlier
9615          * enforced that size == 3 if and only if this is the NOT insn.
9616          */
9617         tcg_gen_not_i64(tcg_rd, tcg_rn);
9618         break;
9619     case 0x7: /* SQABS, SQNEG */
9620         if (u) {
9621             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9622         } else {
9623             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9624         }
9625         break;
9626     case 0xa: /* CMLT */
9627         /* 64 bit integer comparison against zero, result is
9628          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9629          * subtracting 1.
9630          */
9631         cond = TCG_COND_LT;
9632     do_cmop:
9633         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9634         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9635         break;
9636     case 0x8: /* CMGT, CMGE */
9637         cond = u ? TCG_COND_GE : TCG_COND_GT;
9638         goto do_cmop;
9639     case 0x9: /* CMEQ, CMLE */
9640         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9641         goto do_cmop;
9642     case 0xb: /* ABS, NEG */
9643         if (u) {
9644             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9645         } else {
9646             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9647         }
9648         break;
9649     case 0x2f: /* FABS */
9650         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9651         break;
9652     case 0x6f: /* FNEG */
9653         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9654         break;
9655     case 0x7f: /* FSQRT */
9656         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9657         break;
9658     case 0x1a: /* FCVTNS */
9659     case 0x1b: /* FCVTMS */
9660     case 0x1c: /* FCVTAS */
9661     case 0x3a: /* FCVTPS */
9662     case 0x3b: /* FCVTZS */
9663         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9664         break;
9665     case 0x5a: /* FCVTNU */
9666     case 0x5b: /* FCVTMU */
9667     case 0x5c: /* FCVTAU */
9668     case 0x7a: /* FCVTPU */
9669     case 0x7b: /* FCVTZU */
9670         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9671         break;
9672     case 0x18: /* FRINTN */
9673     case 0x19: /* FRINTM */
9674     case 0x38: /* FRINTP */
9675     case 0x39: /* FRINTZ */
9676     case 0x58: /* FRINTA */
9677     case 0x79: /* FRINTI */
9678         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9679         break;
9680     case 0x59: /* FRINTX */
9681         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9682         break;
9683     case 0x1e: /* FRINT32Z */
9684     case 0x5e: /* FRINT32X */
9685         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9686         break;
9687     case 0x1f: /* FRINT64Z */
9688     case 0x5f: /* FRINT64X */
9689         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9690         break;
9691     default:
9692         g_assert_not_reached();
9693     }
9694 }
9695 
9696 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9697                                    bool is_scalar, bool is_u, bool is_q,
9698                                    int size, int rn, int rd)
9699 {
9700     bool is_double = (size == MO_64);
9701     TCGv_ptr fpst;
9702 
9703     if (!fp_access_check(s)) {
9704         return;
9705     }
9706 
9707     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9708 
9709     if (is_double) {
9710         TCGv_i64 tcg_op = tcg_temp_new_i64();
9711         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9712         TCGv_i64 tcg_res = tcg_temp_new_i64();
9713         NeonGenTwoDoubleOpFn *genfn;
9714         bool swap = false;
9715         int pass;
9716 
9717         switch (opcode) {
9718         case 0x2e: /* FCMLT (zero) */
9719             swap = true;
9720             /* fallthrough */
9721         case 0x2c: /* FCMGT (zero) */
9722             genfn = gen_helper_neon_cgt_f64;
9723             break;
9724         case 0x2d: /* FCMEQ (zero) */
9725             genfn = gen_helper_neon_ceq_f64;
9726             break;
9727         case 0x6d: /* FCMLE (zero) */
9728             swap = true;
9729             /* fall through */
9730         case 0x6c: /* FCMGE (zero) */
9731             genfn = gen_helper_neon_cge_f64;
9732             break;
9733         default:
9734             g_assert_not_reached();
9735         }
9736 
9737         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9738             read_vec_element(s, tcg_op, rn, pass, MO_64);
9739             if (swap) {
9740                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9741             } else {
9742                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9743             }
9744             write_vec_element(s, tcg_res, rd, pass, MO_64);
9745         }
9746 
9747         clear_vec_high(s, !is_scalar, rd);
9748     } else {
9749         TCGv_i32 tcg_op = tcg_temp_new_i32();
9750         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9751         TCGv_i32 tcg_res = tcg_temp_new_i32();
9752         NeonGenTwoSingleOpFn *genfn;
9753         bool swap = false;
9754         int pass, maxpasses;
9755 
9756         if (size == MO_16) {
9757             switch (opcode) {
9758             case 0x2e: /* FCMLT (zero) */
9759                 swap = true;
9760                 /* fall through */
9761             case 0x2c: /* FCMGT (zero) */
9762                 genfn = gen_helper_advsimd_cgt_f16;
9763                 break;
9764             case 0x2d: /* FCMEQ (zero) */
9765                 genfn = gen_helper_advsimd_ceq_f16;
9766                 break;
9767             case 0x6d: /* FCMLE (zero) */
9768                 swap = true;
9769                 /* fall through */
9770             case 0x6c: /* FCMGE (zero) */
9771                 genfn = gen_helper_advsimd_cge_f16;
9772                 break;
9773             default:
9774                 g_assert_not_reached();
9775             }
9776         } else {
9777             switch (opcode) {
9778             case 0x2e: /* FCMLT (zero) */
9779                 swap = true;
9780                 /* fall through */
9781             case 0x2c: /* FCMGT (zero) */
9782                 genfn = gen_helper_neon_cgt_f32;
9783                 break;
9784             case 0x2d: /* FCMEQ (zero) */
9785                 genfn = gen_helper_neon_ceq_f32;
9786                 break;
9787             case 0x6d: /* FCMLE (zero) */
9788                 swap = true;
9789                 /* fall through */
9790             case 0x6c: /* FCMGE (zero) */
9791                 genfn = gen_helper_neon_cge_f32;
9792                 break;
9793             default:
9794                 g_assert_not_reached();
9795             }
9796         }
9797 
9798         if (is_scalar) {
9799             maxpasses = 1;
9800         } else {
9801             int vector_size = 8 << is_q;
9802             maxpasses = vector_size >> size;
9803         }
9804 
9805         for (pass = 0; pass < maxpasses; pass++) {
9806             read_vec_element_i32(s, tcg_op, rn, pass, size);
9807             if (swap) {
9808                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9809             } else {
9810                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9811             }
9812             if (is_scalar) {
9813                 write_fp_sreg(s, rd, tcg_res);
9814             } else {
9815                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9816             }
9817         }
9818 
9819         if (!is_scalar) {
9820             clear_vec_high(s, is_q, rd);
9821         }
9822     }
9823 }
9824 
9825 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9826                                     bool is_scalar, bool is_u, bool is_q,
9827                                     int size, int rn, int rd)
9828 {
9829     bool is_double = (size == 3);
9830     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9831 
9832     if (is_double) {
9833         TCGv_i64 tcg_op = tcg_temp_new_i64();
9834         TCGv_i64 tcg_res = tcg_temp_new_i64();
9835         int pass;
9836 
9837         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9838             read_vec_element(s, tcg_op, rn, pass, MO_64);
9839             switch (opcode) {
9840             case 0x3d: /* FRECPE */
9841                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9842                 break;
9843             case 0x3f: /* FRECPX */
9844                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9845                 break;
9846             case 0x7d: /* FRSQRTE */
9847                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9848                 break;
9849             default:
9850                 g_assert_not_reached();
9851             }
9852             write_vec_element(s, tcg_res, rd, pass, MO_64);
9853         }
9854         clear_vec_high(s, !is_scalar, rd);
9855     } else {
9856         TCGv_i32 tcg_op = tcg_temp_new_i32();
9857         TCGv_i32 tcg_res = tcg_temp_new_i32();
9858         int pass, maxpasses;
9859 
9860         if (is_scalar) {
9861             maxpasses = 1;
9862         } else {
9863             maxpasses = is_q ? 4 : 2;
9864         }
9865 
9866         for (pass = 0; pass < maxpasses; pass++) {
9867             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9868 
9869             switch (opcode) {
9870             case 0x3c: /* URECPE */
9871                 gen_helper_recpe_u32(tcg_res, tcg_op);
9872                 break;
9873             case 0x3d: /* FRECPE */
9874                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9875                 break;
9876             case 0x3f: /* FRECPX */
9877                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9878                 break;
9879             case 0x7d: /* FRSQRTE */
9880                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9881                 break;
9882             default:
9883                 g_assert_not_reached();
9884             }
9885 
9886             if (is_scalar) {
9887                 write_fp_sreg(s, rd, tcg_res);
9888             } else {
9889                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9890             }
9891         }
9892         if (!is_scalar) {
9893             clear_vec_high(s, is_q, rd);
9894         }
9895     }
9896 }
9897 
9898 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9899                                 int opcode, bool u, bool is_q,
9900                                 int size, int rn, int rd)
9901 {
9902     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9903      * in the source becomes a size element in the destination).
9904      */
9905     int pass;
9906     TCGv_i32 tcg_res[2];
9907     int destelt = is_q ? 2 : 0;
9908     int passes = scalar ? 1 : 2;
9909 
9910     if (scalar) {
9911         tcg_res[1] = tcg_constant_i32(0);
9912     }
9913 
9914     for (pass = 0; pass < passes; pass++) {
9915         TCGv_i64 tcg_op = tcg_temp_new_i64();
9916         NeonGenNarrowFn *genfn = NULL;
9917         NeonGenNarrowEnvFn *genenvfn = NULL;
9918 
9919         if (scalar) {
9920             read_vec_element(s, tcg_op, rn, pass, size + 1);
9921         } else {
9922             read_vec_element(s, tcg_op, rn, pass, MO_64);
9923         }
9924         tcg_res[pass] = tcg_temp_new_i32();
9925 
9926         switch (opcode) {
9927         case 0x12: /* XTN, SQXTUN */
9928         {
9929             static NeonGenNarrowFn * const xtnfns[3] = {
9930                 gen_helper_neon_narrow_u8,
9931                 gen_helper_neon_narrow_u16,
9932                 tcg_gen_extrl_i64_i32,
9933             };
9934             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9935                 gen_helper_neon_unarrow_sat8,
9936                 gen_helper_neon_unarrow_sat16,
9937                 gen_helper_neon_unarrow_sat32,
9938             };
9939             if (u) {
9940                 genenvfn = sqxtunfns[size];
9941             } else {
9942                 genfn = xtnfns[size];
9943             }
9944             break;
9945         }
9946         case 0x14: /* SQXTN, UQXTN */
9947         {
9948             static NeonGenNarrowEnvFn * const fns[3][2] = {
9949                 { gen_helper_neon_narrow_sat_s8,
9950                   gen_helper_neon_narrow_sat_u8 },
9951                 { gen_helper_neon_narrow_sat_s16,
9952                   gen_helper_neon_narrow_sat_u16 },
9953                 { gen_helper_neon_narrow_sat_s32,
9954                   gen_helper_neon_narrow_sat_u32 },
9955             };
9956             genenvfn = fns[size][u];
9957             break;
9958         }
9959         case 0x16: /* FCVTN, FCVTN2 */
9960             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9961             if (size == 2) {
9962                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9963             } else {
9964                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9965                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9966                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9967                 TCGv_i32 ahp = get_ahp_flag();
9968 
9969                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9970                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9971                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9972                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9973             }
9974             break;
9975         case 0x36: /* BFCVTN, BFCVTN2 */
9976             {
9977                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9978                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9979             }
9980             break;
9981         case 0x56:  /* FCVTXN, FCVTXN2 */
9982             /* 64 bit to 32 bit float conversion
9983              * with von Neumann rounding (round to odd)
9984              */
9985             assert(size == 2);
9986             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9987             break;
9988         default:
9989             g_assert_not_reached();
9990         }
9991 
9992         if (genfn) {
9993             genfn(tcg_res[pass], tcg_op);
9994         } else if (genenvfn) {
9995             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9996         }
9997     }
9998 
9999     for (pass = 0; pass < 2; pass++) {
10000         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10001     }
10002     clear_vec_high(s, is_q, rd);
10003 }
10004 
10005 /* Remaining saturating accumulating ops */
10006 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10007                                 bool is_q, int size, int rn, int rd)
10008 {
10009     bool is_double = (size == 3);
10010 
10011     if (is_double) {
10012         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10013         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10014         int pass;
10015 
10016         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10017             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10018             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10019 
10020             if (is_u) { /* USQADD */
10021                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10022             } else { /* SUQADD */
10023                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10024             }
10025             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10026         }
10027         clear_vec_high(s, !is_scalar, rd);
10028     } else {
10029         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10030         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10031         int pass, maxpasses;
10032 
10033         if (is_scalar) {
10034             maxpasses = 1;
10035         } else {
10036             maxpasses = is_q ? 4 : 2;
10037         }
10038 
10039         for (pass = 0; pass < maxpasses; pass++) {
10040             if (is_scalar) {
10041                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10042                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10043             } else {
10044                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10045                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10046             }
10047 
10048             if (is_u) { /* USQADD */
10049                 switch (size) {
10050                 case 0:
10051                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10052                     break;
10053                 case 1:
10054                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10055                     break;
10056                 case 2:
10057                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10058                     break;
10059                 default:
10060                     g_assert_not_reached();
10061                 }
10062             } else { /* SUQADD */
10063                 switch (size) {
10064                 case 0:
10065                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10066                     break;
10067                 case 1:
10068                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10069                     break;
10070                 case 2:
10071                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10072                     break;
10073                 default:
10074                     g_assert_not_reached();
10075                 }
10076             }
10077 
10078             if (is_scalar) {
10079                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10080             }
10081             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10082         }
10083         clear_vec_high(s, is_q, rd);
10084     }
10085 }
10086 
10087 /* AdvSIMD scalar two reg misc
10088  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10089  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10090  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10091  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10092  */
10093 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10094 {
10095     int rd = extract32(insn, 0, 5);
10096     int rn = extract32(insn, 5, 5);
10097     int opcode = extract32(insn, 12, 5);
10098     int size = extract32(insn, 22, 2);
10099     bool u = extract32(insn, 29, 1);
10100     bool is_fcvt = false;
10101     int rmode;
10102     TCGv_i32 tcg_rmode;
10103     TCGv_ptr tcg_fpstatus;
10104 
10105     switch (opcode) {
10106     case 0x3: /* USQADD / SUQADD*/
10107         if (!fp_access_check(s)) {
10108             return;
10109         }
10110         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10111         return;
10112     case 0x7: /* SQABS / SQNEG */
10113         break;
10114     case 0xa: /* CMLT */
10115         if (u) {
10116             unallocated_encoding(s);
10117             return;
10118         }
10119         /* fall through */
10120     case 0x8: /* CMGT, CMGE */
10121     case 0x9: /* CMEQ, CMLE */
10122     case 0xb: /* ABS, NEG */
10123         if (size != 3) {
10124             unallocated_encoding(s);
10125             return;
10126         }
10127         break;
10128     case 0x12: /* SQXTUN */
10129         if (!u) {
10130             unallocated_encoding(s);
10131             return;
10132         }
10133         /* fall through */
10134     case 0x14: /* SQXTN, UQXTN */
10135         if (size == 3) {
10136             unallocated_encoding(s);
10137             return;
10138         }
10139         if (!fp_access_check(s)) {
10140             return;
10141         }
10142         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10143         return;
10144     case 0xc ... 0xf:
10145     case 0x16 ... 0x1d:
10146     case 0x1f:
10147         /* Floating point: U, size[1] and opcode indicate operation;
10148          * size[0] indicates single or double precision.
10149          */
10150         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10151         size = extract32(size, 0, 1) ? 3 : 2;
10152         switch (opcode) {
10153         case 0x2c: /* FCMGT (zero) */
10154         case 0x2d: /* FCMEQ (zero) */
10155         case 0x2e: /* FCMLT (zero) */
10156         case 0x6c: /* FCMGE (zero) */
10157         case 0x6d: /* FCMLE (zero) */
10158             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10159             return;
10160         case 0x1d: /* SCVTF */
10161         case 0x5d: /* UCVTF */
10162         {
10163             bool is_signed = (opcode == 0x1d);
10164             if (!fp_access_check(s)) {
10165                 return;
10166             }
10167             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10168             return;
10169         }
10170         case 0x3d: /* FRECPE */
10171         case 0x3f: /* FRECPX */
10172         case 0x7d: /* FRSQRTE */
10173             if (!fp_access_check(s)) {
10174                 return;
10175             }
10176             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10177             return;
10178         case 0x1a: /* FCVTNS */
10179         case 0x1b: /* FCVTMS */
10180         case 0x3a: /* FCVTPS */
10181         case 0x3b: /* FCVTZS */
10182         case 0x5a: /* FCVTNU */
10183         case 0x5b: /* FCVTMU */
10184         case 0x7a: /* FCVTPU */
10185         case 0x7b: /* FCVTZU */
10186             is_fcvt = true;
10187             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10188             break;
10189         case 0x1c: /* FCVTAS */
10190         case 0x5c: /* FCVTAU */
10191             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10192             is_fcvt = true;
10193             rmode = FPROUNDING_TIEAWAY;
10194             break;
10195         case 0x56: /* FCVTXN, FCVTXN2 */
10196             if (size == 2) {
10197                 unallocated_encoding(s);
10198                 return;
10199             }
10200             if (!fp_access_check(s)) {
10201                 return;
10202             }
10203             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10204             return;
10205         default:
10206             unallocated_encoding(s);
10207             return;
10208         }
10209         break;
10210     default:
10211         unallocated_encoding(s);
10212         return;
10213     }
10214 
10215     if (!fp_access_check(s)) {
10216         return;
10217     }
10218 
10219     if (is_fcvt) {
10220         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10221         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10222     } else {
10223         tcg_fpstatus = NULL;
10224         tcg_rmode = NULL;
10225     }
10226 
10227     if (size == 3) {
10228         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10229         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10230 
10231         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10232         write_fp_dreg(s, rd, tcg_rd);
10233     } else {
10234         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10235         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10236 
10237         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10238 
10239         switch (opcode) {
10240         case 0x7: /* SQABS, SQNEG */
10241         {
10242             NeonGenOneOpEnvFn *genfn;
10243             static NeonGenOneOpEnvFn * const fns[3][2] = {
10244                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10245                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10246                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10247             };
10248             genfn = fns[size][u];
10249             genfn(tcg_rd, cpu_env, tcg_rn);
10250             break;
10251         }
10252         case 0x1a: /* FCVTNS */
10253         case 0x1b: /* FCVTMS */
10254         case 0x1c: /* FCVTAS */
10255         case 0x3a: /* FCVTPS */
10256         case 0x3b: /* FCVTZS */
10257             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10258                                  tcg_fpstatus);
10259             break;
10260         case 0x5a: /* FCVTNU */
10261         case 0x5b: /* FCVTMU */
10262         case 0x5c: /* FCVTAU */
10263         case 0x7a: /* FCVTPU */
10264         case 0x7b: /* FCVTZU */
10265             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10266                                  tcg_fpstatus);
10267             break;
10268         default:
10269             g_assert_not_reached();
10270         }
10271 
10272         write_fp_sreg(s, rd, tcg_rd);
10273     }
10274 
10275     if (is_fcvt) {
10276         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10277     }
10278 }
10279 
10280 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10281 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10282                                  int immh, int immb, int opcode, int rn, int rd)
10283 {
10284     int size = 32 - clz32(immh) - 1;
10285     int immhb = immh << 3 | immb;
10286     int shift = 2 * (8 << size) - immhb;
10287     GVecGen2iFn *gvec_fn;
10288 
10289     if (extract32(immh, 3, 1) && !is_q) {
10290         unallocated_encoding(s);
10291         return;
10292     }
10293     tcg_debug_assert(size <= 3);
10294 
10295     if (!fp_access_check(s)) {
10296         return;
10297     }
10298 
10299     switch (opcode) {
10300     case 0x02: /* SSRA / USRA (accumulate) */
10301         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10302         break;
10303 
10304     case 0x08: /* SRI */
10305         gvec_fn = gen_gvec_sri;
10306         break;
10307 
10308     case 0x00: /* SSHR / USHR */
10309         if (is_u) {
10310             if (shift == 8 << size) {
10311                 /* Shift count the same size as element size produces zero.  */
10312                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10313                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10314                 return;
10315             }
10316             gvec_fn = tcg_gen_gvec_shri;
10317         } else {
10318             /* Shift count the same size as element size produces all sign.  */
10319             if (shift == 8 << size) {
10320                 shift -= 1;
10321             }
10322             gvec_fn = tcg_gen_gvec_sari;
10323         }
10324         break;
10325 
10326     case 0x04: /* SRSHR / URSHR (rounding) */
10327         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10328         break;
10329 
10330     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10331         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10332         break;
10333 
10334     default:
10335         g_assert_not_reached();
10336     }
10337 
10338     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10339 }
10340 
10341 /* SHL/SLI - Vector shift left */
10342 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10343                                  int immh, int immb, int opcode, int rn, int rd)
10344 {
10345     int size = 32 - clz32(immh) - 1;
10346     int immhb = immh << 3 | immb;
10347     int shift = immhb - (8 << size);
10348 
10349     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10350     assert(size >= 0 && size <= 3);
10351 
10352     if (extract32(immh, 3, 1) && !is_q) {
10353         unallocated_encoding(s);
10354         return;
10355     }
10356 
10357     if (!fp_access_check(s)) {
10358         return;
10359     }
10360 
10361     if (insert) {
10362         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10363     } else {
10364         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10365     }
10366 }
10367 
10368 /* USHLL/SHLL - Vector shift left with widening */
10369 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10370                                  int immh, int immb, int opcode, int rn, int rd)
10371 {
10372     int size = 32 - clz32(immh) - 1;
10373     int immhb = immh << 3 | immb;
10374     int shift = immhb - (8 << size);
10375     int dsize = 64;
10376     int esize = 8 << size;
10377     int elements = dsize/esize;
10378     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10379     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10380     int i;
10381 
10382     if (size >= 3) {
10383         unallocated_encoding(s);
10384         return;
10385     }
10386 
10387     if (!fp_access_check(s)) {
10388         return;
10389     }
10390 
10391     /* For the LL variants the store is larger than the load,
10392      * so if rd == rn we would overwrite parts of our input.
10393      * So load everything right now and use shifts in the main loop.
10394      */
10395     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10396 
10397     for (i = 0; i < elements; i++) {
10398         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10399         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10400         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10401         write_vec_element(s, tcg_rd, rd, i, size + 1);
10402     }
10403 }
10404 
10405 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10406 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10407                                  int immh, int immb, int opcode, int rn, int rd)
10408 {
10409     int immhb = immh << 3 | immb;
10410     int size = 32 - clz32(immh) - 1;
10411     int dsize = 64;
10412     int esize = 8 << size;
10413     int elements = dsize/esize;
10414     int shift = (2 * esize) - immhb;
10415     bool round = extract32(opcode, 0, 1);
10416     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10417     TCGv_i64 tcg_round;
10418     int i;
10419 
10420     if (extract32(immh, 3, 1)) {
10421         unallocated_encoding(s);
10422         return;
10423     }
10424 
10425     if (!fp_access_check(s)) {
10426         return;
10427     }
10428 
10429     tcg_rn = tcg_temp_new_i64();
10430     tcg_rd = tcg_temp_new_i64();
10431     tcg_final = tcg_temp_new_i64();
10432     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10433 
10434     if (round) {
10435         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10436     } else {
10437         tcg_round = NULL;
10438     }
10439 
10440     for (i = 0; i < elements; i++) {
10441         read_vec_element(s, tcg_rn, rn, i, size+1);
10442         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10443                                 false, true, size+1, shift);
10444 
10445         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10446     }
10447 
10448     if (!is_q) {
10449         write_vec_element(s, tcg_final, rd, 0, MO_64);
10450     } else {
10451         write_vec_element(s, tcg_final, rd, 1, MO_64);
10452     }
10453 
10454     clear_vec_high(s, is_q, rd);
10455 }
10456 
10457 
10458 /* AdvSIMD shift by immediate
10459  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10460  * +---+---+---+-------------+------+------+--------+---+------+------+
10461  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10462  * +---+---+---+-------------+------+------+--------+---+------+------+
10463  */
10464 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10465 {
10466     int rd = extract32(insn, 0, 5);
10467     int rn = extract32(insn, 5, 5);
10468     int opcode = extract32(insn, 11, 5);
10469     int immb = extract32(insn, 16, 3);
10470     int immh = extract32(insn, 19, 4);
10471     bool is_u = extract32(insn, 29, 1);
10472     bool is_q = extract32(insn, 30, 1);
10473 
10474     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10475     assert(immh != 0);
10476 
10477     switch (opcode) {
10478     case 0x08: /* SRI */
10479         if (!is_u) {
10480             unallocated_encoding(s);
10481             return;
10482         }
10483         /* fall through */
10484     case 0x00: /* SSHR / USHR */
10485     case 0x02: /* SSRA / USRA (accumulate) */
10486     case 0x04: /* SRSHR / URSHR (rounding) */
10487     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10488         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10489         break;
10490     case 0x0a: /* SHL / SLI */
10491         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10492         break;
10493     case 0x10: /* SHRN */
10494     case 0x11: /* RSHRN / SQRSHRUN */
10495         if (is_u) {
10496             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10497                                    opcode, rn, rd);
10498         } else {
10499             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10500         }
10501         break;
10502     case 0x12: /* SQSHRN / UQSHRN */
10503     case 0x13: /* SQRSHRN / UQRSHRN */
10504         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10505                                opcode, rn, rd);
10506         break;
10507     case 0x14: /* SSHLL / USHLL */
10508         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10509         break;
10510     case 0x1c: /* SCVTF / UCVTF */
10511         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10512                                      opcode, rn, rd);
10513         break;
10514     case 0xc: /* SQSHLU */
10515         if (!is_u) {
10516             unallocated_encoding(s);
10517             return;
10518         }
10519         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10520         break;
10521     case 0xe: /* SQSHL, UQSHL */
10522         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10523         break;
10524     case 0x1f: /* FCVTZS/ FCVTZU */
10525         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10526         return;
10527     default:
10528         unallocated_encoding(s);
10529         return;
10530     }
10531 }
10532 
10533 /* Generate code to do a "long" addition or subtraction, ie one done in
10534  * TCGv_i64 on vector lanes twice the width specified by size.
10535  */
10536 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10537                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10538 {
10539     static NeonGenTwo64OpFn * const fns[3][2] = {
10540         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10541         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10542         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10543     };
10544     NeonGenTwo64OpFn *genfn;
10545     assert(size < 3);
10546 
10547     genfn = fns[size][is_sub];
10548     genfn(tcg_res, tcg_op1, tcg_op2);
10549 }
10550 
10551 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10552                                 int opcode, int rd, int rn, int rm)
10553 {
10554     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10555     TCGv_i64 tcg_res[2];
10556     int pass, accop;
10557 
10558     tcg_res[0] = tcg_temp_new_i64();
10559     tcg_res[1] = tcg_temp_new_i64();
10560 
10561     /* Does this op do an adding accumulate, a subtracting accumulate,
10562      * or no accumulate at all?
10563      */
10564     switch (opcode) {
10565     case 5:
10566     case 8:
10567     case 9:
10568         accop = 1;
10569         break;
10570     case 10:
10571     case 11:
10572         accop = -1;
10573         break;
10574     default:
10575         accop = 0;
10576         break;
10577     }
10578 
10579     if (accop != 0) {
10580         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10581         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10582     }
10583 
10584     /* size == 2 means two 32x32->64 operations; this is worth special
10585      * casing because we can generally handle it inline.
10586      */
10587     if (size == 2) {
10588         for (pass = 0; pass < 2; pass++) {
10589             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10590             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10591             TCGv_i64 tcg_passres;
10592             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10593 
10594             int elt = pass + is_q * 2;
10595 
10596             read_vec_element(s, tcg_op1, rn, elt, memop);
10597             read_vec_element(s, tcg_op2, rm, elt, memop);
10598 
10599             if (accop == 0) {
10600                 tcg_passres = tcg_res[pass];
10601             } else {
10602                 tcg_passres = tcg_temp_new_i64();
10603             }
10604 
10605             switch (opcode) {
10606             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10607                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10608                 break;
10609             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10610                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10611                 break;
10612             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10613             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10614             {
10615                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10616                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10617 
10618                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10619                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10620                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10621                                     tcg_passres,
10622                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10623                 break;
10624             }
10625             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10626             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10627             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10628                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10629                 break;
10630             case 9: /* SQDMLAL, SQDMLAL2 */
10631             case 11: /* SQDMLSL, SQDMLSL2 */
10632             case 13: /* SQDMULL, SQDMULL2 */
10633                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10634                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10635                                                   tcg_passres, tcg_passres);
10636                 break;
10637             default:
10638                 g_assert_not_reached();
10639             }
10640 
10641             if (opcode == 9 || opcode == 11) {
10642                 /* saturating accumulate ops */
10643                 if (accop < 0) {
10644                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10645                 }
10646                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10647                                                   tcg_res[pass], tcg_passres);
10648             } else if (accop > 0) {
10649                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10650             } else if (accop < 0) {
10651                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10652             }
10653         }
10654     } else {
10655         /* size 0 or 1, generally helper functions */
10656         for (pass = 0; pass < 2; pass++) {
10657             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10658             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10659             TCGv_i64 tcg_passres;
10660             int elt = pass + is_q * 2;
10661 
10662             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10663             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10664 
10665             if (accop == 0) {
10666                 tcg_passres = tcg_res[pass];
10667             } else {
10668                 tcg_passres = tcg_temp_new_i64();
10669             }
10670 
10671             switch (opcode) {
10672             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10673             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10674             {
10675                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10676                 static NeonGenWidenFn * const widenfns[2][2] = {
10677                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10678                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10679                 };
10680                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10681 
10682                 widenfn(tcg_op2_64, tcg_op2);
10683                 widenfn(tcg_passres, tcg_op1);
10684                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10685                               tcg_passres, tcg_op2_64);
10686                 break;
10687             }
10688             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10689             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10690                 if (size == 0) {
10691                     if (is_u) {
10692                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10693                     } else {
10694                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10695                     }
10696                 } else {
10697                     if (is_u) {
10698                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10699                     } else {
10700                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10701                     }
10702                 }
10703                 break;
10704             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10705             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10706             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10707                 if (size == 0) {
10708                     if (is_u) {
10709                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10710                     } else {
10711                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10712                     }
10713                 } else {
10714                     if (is_u) {
10715                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10716                     } else {
10717                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10718                     }
10719                 }
10720                 break;
10721             case 9: /* SQDMLAL, SQDMLAL2 */
10722             case 11: /* SQDMLSL, SQDMLSL2 */
10723             case 13: /* SQDMULL, SQDMULL2 */
10724                 assert(size == 1);
10725                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10726                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10727                                                   tcg_passres, tcg_passres);
10728                 break;
10729             default:
10730                 g_assert_not_reached();
10731             }
10732 
10733             if (accop != 0) {
10734                 if (opcode == 9 || opcode == 11) {
10735                     /* saturating accumulate ops */
10736                     if (accop < 0) {
10737                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10738                     }
10739                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10740                                                       tcg_res[pass],
10741                                                       tcg_passres);
10742                 } else {
10743                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10744                                   tcg_res[pass], tcg_passres);
10745                 }
10746             }
10747         }
10748     }
10749 
10750     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10751     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10752 }
10753 
10754 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10755                             int opcode, int rd, int rn, int rm)
10756 {
10757     TCGv_i64 tcg_res[2];
10758     int part = is_q ? 2 : 0;
10759     int pass;
10760 
10761     for (pass = 0; pass < 2; pass++) {
10762         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10763         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10764         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10765         static NeonGenWidenFn * const widenfns[3][2] = {
10766             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10767             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10768             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10769         };
10770         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10771 
10772         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10773         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10774         widenfn(tcg_op2_wide, tcg_op2);
10775         tcg_res[pass] = tcg_temp_new_i64();
10776         gen_neon_addl(size, (opcode == 3),
10777                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10778     }
10779 
10780     for (pass = 0; pass < 2; pass++) {
10781         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10782     }
10783 }
10784 
10785 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10786 {
10787     tcg_gen_addi_i64(in, in, 1U << 31);
10788     tcg_gen_extrh_i64_i32(res, in);
10789 }
10790 
10791 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10792                                  int opcode, int rd, int rn, int rm)
10793 {
10794     TCGv_i32 tcg_res[2];
10795     int part = is_q ? 2 : 0;
10796     int pass;
10797 
10798     for (pass = 0; pass < 2; pass++) {
10799         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10800         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10801         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10802         static NeonGenNarrowFn * const narrowfns[3][2] = {
10803             { gen_helper_neon_narrow_high_u8,
10804               gen_helper_neon_narrow_round_high_u8 },
10805             { gen_helper_neon_narrow_high_u16,
10806               gen_helper_neon_narrow_round_high_u16 },
10807             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10808         };
10809         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10810 
10811         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10812         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10813 
10814         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10815 
10816         tcg_res[pass] = tcg_temp_new_i32();
10817         gennarrow(tcg_res[pass], tcg_wideres);
10818     }
10819 
10820     for (pass = 0; pass < 2; pass++) {
10821         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10822     }
10823     clear_vec_high(s, is_q, rd);
10824 }
10825 
10826 /* AdvSIMD three different
10827  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10828  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10829  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10830  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10831  */
10832 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10833 {
10834     /* Instructions in this group fall into three basic classes
10835      * (in each case with the operation working on each element in
10836      * the input vectors):
10837      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10838      *     128 bit input)
10839      * (2) wide 64 x 128 -> 128
10840      * (3) narrowing 128 x 128 -> 64
10841      * Here we do initial decode, catch unallocated cases and
10842      * dispatch to separate functions for each class.
10843      */
10844     int is_q = extract32(insn, 30, 1);
10845     int is_u = extract32(insn, 29, 1);
10846     int size = extract32(insn, 22, 2);
10847     int opcode = extract32(insn, 12, 4);
10848     int rm = extract32(insn, 16, 5);
10849     int rn = extract32(insn, 5, 5);
10850     int rd = extract32(insn, 0, 5);
10851 
10852     switch (opcode) {
10853     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10854     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10855         /* 64 x 128 -> 128 */
10856         if (size == 3) {
10857             unallocated_encoding(s);
10858             return;
10859         }
10860         if (!fp_access_check(s)) {
10861             return;
10862         }
10863         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10864         break;
10865     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10866     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10867         /* 128 x 128 -> 64 */
10868         if (size == 3) {
10869             unallocated_encoding(s);
10870             return;
10871         }
10872         if (!fp_access_check(s)) {
10873             return;
10874         }
10875         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10876         break;
10877     case 14: /* PMULL, PMULL2 */
10878         if (is_u) {
10879             unallocated_encoding(s);
10880             return;
10881         }
10882         switch (size) {
10883         case 0: /* PMULL.P8 */
10884             if (!fp_access_check(s)) {
10885                 return;
10886             }
10887             /* The Q field specifies lo/hi half input for this insn.  */
10888             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10889                              gen_helper_neon_pmull_h);
10890             break;
10891 
10892         case 3: /* PMULL.P64 */
10893             if (!dc_isar_feature(aa64_pmull, s)) {
10894                 unallocated_encoding(s);
10895                 return;
10896             }
10897             if (!fp_access_check(s)) {
10898                 return;
10899             }
10900             /* The Q field specifies lo/hi half input for this insn.  */
10901             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10902                              gen_helper_gvec_pmull_q);
10903             break;
10904 
10905         default:
10906             unallocated_encoding(s);
10907             break;
10908         }
10909         return;
10910     case 9: /* SQDMLAL, SQDMLAL2 */
10911     case 11: /* SQDMLSL, SQDMLSL2 */
10912     case 13: /* SQDMULL, SQDMULL2 */
10913         if (is_u || size == 0) {
10914             unallocated_encoding(s);
10915             return;
10916         }
10917         /* fall through */
10918     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10919     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10920     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10921     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10922     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10923     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10924     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10925         /* 64 x 64 -> 128 */
10926         if (size == 3) {
10927             unallocated_encoding(s);
10928             return;
10929         }
10930         if (!fp_access_check(s)) {
10931             return;
10932         }
10933 
10934         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10935         break;
10936     default:
10937         /* opcode 15 not allocated */
10938         unallocated_encoding(s);
10939         break;
10940     }
10941 }
10942 
10943 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10944 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10945 {
10946     int rd = extract32(insn, 0, 5);
10947     int rn = extract32(insn, 5, 5);
10948     int rm = extract32(insn, 16, 5);
10949     int size = extract32(insn, 22, 2);
10950     bool is_u = extract32(insn, 29, 1);
10951     bool is_q = extract32(insn, 30, 1);
10952 
10953     if (!fp_access_check(s)) {
10954         return;
10955     }
10956 
10957     switch (size + 4 * is_u) {
10958     case 0: /* AND */
10959         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10960         return;
10961     case 1: /* BIC */
10962         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10963         return;
10964     case 2: /* ORR */
10965         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10966         return;
10967     case 3: /* ORN */
10968         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10969         return;
10970     case 4: /* EOR */
10971         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10972         return;
10973 
10974     case 5: /* BSL bitwise select */
10975         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10976         return;
10977     case 6: /* BIT, bitwise insert if true */
10978         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10979         return;
10980     case 7: /* BIF, bitwise insert if false */
10981         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10982         return;
10983 
10984     default:
10985         g_assert_not_reached();
10986     }
10987 }
10988 
10989 /* Pairwise op subgroup of C3.6.16.
10990  *
10991  * This is called directly or via the handle_3same_float for float pairwise
10992  * operations where the opcode and size are calculated differently.
10993  */
10994 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10995                                    int size, int rn, int rm, int rd)
10996 {
10997     TCGv_ptr fpst;
10998     int pass;
10999 
11000     /* Floating point operations need fpst */
11001     if (opcode >= 0x58) {
11002         fpst = fpstatus_ptr(FPST_FPCR);
11003     } else {
11004         fpst = NULL;
11005     }
11006 
11007     if (!fp_access_check(s)) {
11008         return;
11009     }
11010 
11011     /* These operations work on the concatenated rm:rn, with each pair of
11012      * adjacent elements being operated on to produce an element in the result.
11013      */
11014     if (size == 3) {
11015         TCGv_i64 tcg_res[2];
11016 
11017         for (pass = 0; pass < 2; pass++) {
11018             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11019             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11020             int passreg = (pass == 0) ? rn : rm;
11021 
11022             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11023             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11024             tcg_res[pass] = tcg_temp_new_i64();
11025 
11026             switch (opcode) {
11027             case 0x17: /* ADDP */
11028                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11029                 break;
11030             case 0x58: /* FMAXNMP */
11031                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11032                 break;
11033             case 0x5a: /* FADDP */
11034                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11035                 break;
11036             case 0x5e: /* FMAXP */
11037                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11038                 break;
11039             case 0x78: /* FMINNMP */
11040                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11041                 break;
11042             case 0x7e: /* FMINP */
11043                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11044                 break;
11045             default:
11046                 g_assert_not_reached();
11047             }
11048         }
11049 
11050         for (pass = 0; pass < 2; pass++) {
11051             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11052         }
11053     } else {
11054         int maxpass = is_q ? 4 : 2;
11055         TCGv_i32 tcg_res[4];
11056 
11057         for (pass = 0; pass < maxpass; pass++) {
11058             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11059             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11060             NeonGenTwoOpFn *genfn = NULL;
11061             int passreg = pass < (maxpass / 2) ? rn : rm;
11062             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11063 
11064             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11065             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11066             tcg_res[pass] = tcg_temp_new_i32();
11067 
11068             switch (opcode) {
11069             case 0x17: /* ADDP */
11070             {
11071                 static NeonGenTwoOpFn * const fns[3] = {
11072                     gen_helper_neon_padd_u8,
11073                     gen_helper_neon_padd_u16,
11074                     tcg_gen_add_i32,
11075                 };
11076                 genfn = fns[size];
11077                 break;
11078             }
11079             case 0x14: /* SMAXP, UMAXP */
11080             {
11081                 static NeonGenTwoOpFn * const fns[3][2] = {
11082                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11083                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11084                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11085                 };
11086                 genfn = fns[size][u];
11087                 break;
11088             }
11089             case 0x15: /* SMINP, UMINP */
11090             {
11091                 static NeonGenTwoOpFn * const fns[3][2] = {
11092                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11093                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11094                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11095                 };
11096                 genfn = fns[size][u];
11097                 break;
11098             }
11099             /* The FP operations are all on single floats (32 bit) */
11100             case 0x58: /* FMAXNMP */
11101                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11102                 break;
11103             case 0x5a: /* FADDP */
11104                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11105                 break;
11106             case 0x5e: /* FMAXP */
11107                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11108                 break;
11109             case 0x78: /* FMINNMP */
11110                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11111                 break;
11112             case 0x7e: /* FMINP */
11113                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11114                 break;
11115             default:
11116                 g_assert_not_reached();
11117             }
11118 
11119             /* FP ops called directly, otherwise call now */
11120             if (genfn) {
11121                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11122             }
11123         }
11124 
11125         for (pass = 0; pass < maxpass; pass++) {
11126             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11127         }
11128         clear_vec_high(s, is_q, rd);
11129     }
11130 }
11131 
11132 /* Floating point op subgroup of C3.6.16. */
11133 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11134 {
11135     /* For floating point ops, the U, size[1] and opcode bits
11136      * together indicate the operation. size[0] indicates single
11137      * or double.
11138      */
11139     int fpopcode = extract32(insn, 11, 5)
11140         | (extract32(insn, 23, 1) << 5)
11141         | (extract32(insn, 29, 1) << 6);
11142     int is_q = extract32(insn, 30, 1);
11143     int size = extract32(insn, 22, 1);
11144     int rm = extract32(insn, 16, 5);
11145     int rn = extract32(insn, 5, 5);
11146     int rd = extract32(insn, 0, 5);
11147 
11148     int datasize = is_q ? 128 : 64;
11149     int esize = 32 << size;
11150     int elements = datasize / esize;
11151 
11152     if (size == 1 && !is_q) {
11153         unallocated_encoding(s);
11154         return;
11155     }
11156 
11157     switch (fpopcode) {
11158     case 0x58: /* FMAXNMP */
11159     case 0x5a: /* FADDP */
11160     case 0x5e: /* FMAXP */
11161     case 0x78: /* FMINNMP */
11162     case 0x7e: /* FMINP */
11163         if (size && !is_q) {
11164             unallocated_encoding(s);
11165             return;
11166         }
11167         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11168                                rn, rm, rd);
11169         return;
11170     case 0x1b: /* FMULX */
11171     case 0x1f: /* FRECPS */
11172     case 0x3f: /* FRSQRTS */
11173     case 0x5d: /* FACGE */
11174     case 0x7d: /* FACGT */
11175     case 0x19: /* FMLA */
11176     case 0x39: /* FMLS */
11177     case 0x18: /* FMAXNM */
11178     case 0x1a: /* FADD */
11179     case 0x1c: /* FCMEQ */
11180     case 0x1e: /* FMAX */
11181     case 0x38: /* FMINNM */
11182     case 0x3a: /* FSUB */
11183     case 0x3e: /* FMIN */
11184     case 0x5b: /* FMUL */
11185     case 0x5c: /* FCMGE */
11186     case 0x5f: /* FDIV */
11187     case 0x7a: /* FABD */
11188     case 0x7c: /* FCMGT */
11189         if (!fp_access_check(s)) {
11190             return;
11191         }
11192         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11193         return;
11194 
11195     case 0x1d: /* FMLAL  */
11196     case 0x3d: /* FMLSL  */
11197     case 0x59: /* FMLAL2 */
11198     case 0x79: /* FMLSL2 */
11199         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11200             unallocated_encoding(s);
11201             return;
11202         }
11203         if (fp_access_check(s)) {
11204             int is_s = extract32(insn, 23, 1);
11205             int is_2 = extract32(insn, 29, 1);
11206             int data = (is_2 << 1) | is_s;
11207             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11208                                vec_full_reg_offset(s, rn),
11209                                vec_full_reg_offset(s, rm), cpu_env,
11210                                is_q ? 16 : 8, vec_full_reg_size(s),
11211                                data, gen_helper_gvec_fmlal_a64);
11212         }
11213         return;
11214 
11215     default:
11216         unallocated_encoding(s);
11217         return;
11218     }
11219 }
11220 
11221 /* Integer op subgroup of C3.6.16. */
11222 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11223 {
11224     int is_q = extract32(insn, 30, 1);
11225     int u = extract32(insn, 29, 1);
11226     int size = extract32(insn, 22, 2);
11227     int opcode = extract32(insn, 11, 5);
11228     int rm = extract32(insn, 16, 5);
11229     int rn = extract32(insn, 5, 5);
11230     int rd = extract32(insn, 0, 5);
11231     int pass;
11232     TCGCond cond;
11233 
11234     switch (opcode) {
11235     case 0x13: /* MUL, PMUL */
11236         if (u && size != 0) {
11237             unallocated_encoding(s);
11238             return;
11239         }
11240         /* fall through */
11241     case 0x0: /* SHADD, UHADD */
11242     case 0x2: /* SRHADD, URHADD */
11243     case 0x4: /* SHSUB, UHSUB */
11244     case 0xc: /* SMAX, UMAX */
11245     case 0xd: /* SMIN, UMIN */
11246     case 0xe: /* SABD, UABD */
11247     case 0xf: /* SABA, UABA */
11248     case 0x12: /* MLA, MLS */
11249         if (size == 3) {
11250             unallocated_encoding(s);
11251             return;
11252         }
11253         break;
11254     case 0x16: /* SQDMULH, SQRDMULH */
11255         if (size == 0 || size == 3) {
11256             unallocated_encoding(s);
11257             return;
11258         }
11259         break;
11260     default:
11261         if (size == 3 && !is_q) {
11262             unallocated_encoding(s);
11263             return;
11264         }
11265         break;
11266     }
11267 
11268     if (!fp_access_check(s)) {
11269         return;
11270     }
11271 
11272     switch (opcode) {
11273     case 0x01: /* SQADD, UQADD */
11274         if (u) {
11275             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11276         } else {
11277             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11278         }
11279         return;
11280     case 0x05: /* SQSUB, UQSUB */
11281         if (u) {
11282             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11283         } else {
11284             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11285         }
11286         return;
11287     case 0x08: /* SSHL, USHL */
11288         if (u) {
11289             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11290         } else {
11291             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11292         }
11293         return;
11294     case 0x0c: /* SMAX, UMAX */
11295         if (u) {
11296             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11297         } else {
11298             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11299         }
11300         return;
11301     case 0x0d: /* SMIN, UMIN */
11302         if (u) {
11303             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11304         } else {
11305             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11306         }
11307         return;
11308     case 0xe: /* SABD, UABD */
11309         if (u) {
11310             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11311         } else {
11312             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11313         }
11314         return;
11315     case 0xf: /* SABA, UABA */
11316         if (u) {
11317             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11318         } else {
11319             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11320         }
11321         return;
11322     case 0x10: /* ADD, SUB */
11323         if (u) {
11324             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11325         } else {
11326             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11327         }
11328         return;
11329     case 0x13: /* MUL, PMUL */
11330         if (!u) { /* MUL */
11331             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11332         } else {  /* PMUL */
11333             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11334         }
11335         return;
11336     case 0x12: /* MLA, MLS */
11337         if (u) {
11338             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11339         } else {
11340             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11341         }
11342         return;
11343     case 0x16: /* SQDMULH, SQRDMULH */
11344         {
11345             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11346                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11347                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11348             };
11349             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11350         }
11351         return;
11352     case 0x11:
11353         if (!u) { /* CMTST */
11354             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11355             return;
11356         }
11357         /* else CMEQ */
11358         cond = TCG_COND_EQ;
11359         goto do_gvec_cmp;
11360     case 0x06: /* CMGT, CMHI */
11361         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11362         goto do_gvec_cmp;
11363     case 0x07: /* CMGE, CMHS */
11364         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11365     do_gvec_cmp:
11366         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11367                          vec_full_reg_offset(s, rn),
11368                          vec_full_reg_offset(s, rm),
11369                          is_q ? 16 : 8, vec_full_reg_size(s));
11370         return;
11371     }
11372 
11373     if (size == 3) {
11374         assert(is_q);
11375         for (pass = 0; pass < 2; pass++) {
11376             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11377             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11378             TCGv_i64 tcg_res = tcg_temp_new_i64();
11379 
11380             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11381             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11382 
11383             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11384 
11385             write_vec_element(s, tcg_res, rd, pass, MO_64);
11386         }
11387     } else {
11388         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11389             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11390             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11391             TCGv_i32 tcg_res = tcg_temp_new_i32();
11392             NeonGenTwoOpFn *genfn = NULL;
11393             NeonGenTwoOpEnvFn *genenvfn = NULL;
11394 
11395             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11396             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11397 
11398             switch (opcode) {
11399             case 0x0: /* SHADD, UHADD */
11400             {
11401                 static NeonGenTwoOpFn * const fns[3][2] = {
11402                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11403                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11404                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11405                 };
11406                 genfn = fns[size][u];
11407                 break;
11408             }
11409             case 0x2: /* SRHADD, URHADD */
11410             {
11411                 static NeonGenTwoOpFn * const fns[3][2] = {
11412                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11413                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11414                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11415                 };
11416                 genfn = fns[size][u];
11417                 break;
11418             }
11419             case 0x4: /* SHSUB, UHSUB */
11420             {
11421                 static NeonGenTwoOpFn * const fns[3][2] = {
11422                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11423                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11424                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11425                 };
11426                 genfn = fns[size][u];
11427                 break;
11428             }
11429             case 0x9: /* SQSHL, UQSHL */
11430             {
11431                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11432                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11433                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11434                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11435                 };
11436                 genenvfn = fns[size][u];
11437                 break;
11438             }
11439             case 0xa: /* SRSHL, URSHL */
11440             {
11441                 static NeonGenTwoOpFn * const fns[3][2] = {
11442                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11443                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11444                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11445                 };
11446                 genfn = fns[size][u];
11447                 break;
11448             }
11449             case 0xb: /* SQRSHL, UQRSHL */
11450             {
11451                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11452                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11453                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11454                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11455                 };
11456                 genenvfn = fns[size][u];
11457                 break;
11458             }
11459             default:
11460                 g_assert_not_reached();
11461             }
11462 
11463             if (genenvfn) {
11464                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11465             } else {
11466                 genfn(tcg_res, tcg_op1, tcg_op2);
11467             }
11468 
11469             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11470         }
11471     }
11472     clear_vec_high(s, is_q, rd);
11473 }
11474 
11475 /* AdvSIMD three same
11476  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11477  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11478  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11479  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11480  */
11481 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11482 {
11483     int opcode = extract32(insn, 11, 5);
11484 
11485     switch (opcode) {
11486     case 0x3: /* logic ops */
11487         disas_simd_3same_logic(s, insn);
11488         break;
11489     case 0x17: /* ADDP */
11490     case 0x14: /* SMAXP, UMAXP */
11491     case 0x15: /* SMINP, UMINP */
11492     {
11493         /* Pairwise operations */
11494         int is_q = extract32(insn, 30, 1);
11495         int u = extract32(insn, 29, 1);
11496         int size = extract32(insn, 22, 2);
11497         int rm = extract32(insn, 16, 5);
11498         int rn = extract32(insn, 5, 5);
11499         int rd = extract32(insn, 0, 5);
11500         if (opcode == 0x17) {
11501             if (u || (size == 3 && !is_q)) {
11502                 unallocated_encoding(s);
11503                 return;
11504             }
11505         } else {
11506             if (size == 3) {
11507                 unallocated_encoding(s);
11508                 return;
11509             }
11510         }
11511         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11512         break;
11513     }
11514     case 0x18 ... 0x31:
11515         /* floating point ops, sz[1] and U are part of opcode */
11516         disas_simd_3same_float(s, insn);
11517         break;
11518     default:
11519         disas_simd_3same_int(s, insn);
11520         break;
11521     }
11522 }
11523 
11524 /*
11525  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11526  *
11527  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11528  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11529  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11530  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11531  *
11532  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11533  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11534  *
11535  */
11536 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11537 {
11538     int opcode = extract32(insn, 11, 3);
11539     int u = extract32(insn, 29, 1);
11540     int a = extract32(insn, 23, 1);
11541     int is_q = extract32(insn, 30, 1);
11542     int rm = extract32(insn, 16, 5);
11543     int rn = extract32(insn, 5, 5);
11544     int rd = extract32(insn, 0, 5);
11545     /*
11546      * For these floating point ops, the U, a and opcode bits
11547      * together indicate the operation.
11548      */
11549     int fpopcode = opcode | (a << 3) | (u << 4);
11550     int datasize = is_q ? 128 : 64;
11551     int elements = datasize / 16;
11552     bool pairwise;
11553     TCGv_ptr fpst;
11554     int pass;
11555 
11556     switch (fpopcode) {
11557     case 0x0: /* FMAXNM */
11558     case 0x1: /* FMLA */
11559     case 0x2: /* FADD */
11560     case 0x3: /* FMULX */
11561     case 0x4: /* FCMEQ */
11562     case 0x6: /* FMAX */
11563     case 0x7: /* FRECPS */
11564     case 0x8: /* FMINNM */
11565     case 0x9: /* FMLS */
11566     case 0xa: /* FSUB */
11567     case 0xe: /* FMIN */
11568     case 0xf: /* FRSQRTS */
11569     case 0x13: /* FMUL */
11570     case 0x14: /* FCMGE */
11571     case 0x15: /* FACGE */
11572     case 0x17: /* FDIV */
11573     case 0x1a: /* FABD */
11574     case 0x1c: /* FCMGT */
11575     case 0x1d: /* FACGT */
11576         pairwise = false;
11577         break;
11578     case 0x10: /* FMAXNMP */
11579     case 0x12: /* FADDP */
11580     case 0x16: /* FMAXP */
11581     case 0x18: /* FMINNMP */
11582     case 0x1e: /* FMINP */
11583         pairwise = true;
11584         break;
11585     default:
11586         unallocated_encoding(s);
11587         return;
11588     }
11589 
11590     if (!dc_isar_feature(aa64_fp16, s)) {
11591         unallocated_encoding(s);
11592         return;
11593     }
11594 
11595     if (!fp_access_check(s)) {
11596         return;
11597     }
11598 
11599     fpst = fpstatus_ptr(FPST_FPCR_F16);
11600 
11601     if (pairwise) {
11602         int maxpass = is_q ? 8 : 4;
11603         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11604         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11605         TCGv_i32 tcg_res[8];
11606 
11607         for (pass = 0; pass < maxpass; pass++) {
11608             int passreg = pass < (maxpass / 2) ? rn : rm;
11609             int passelt = (pass << 1) & (maxpass - 1);
11610 
11611             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11612             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11613             tcg_res[pass] = tcg_temp_new_i32();
11614 
11615             switch (fpopcode) {
11616             case 0x10: /* FMAXNMP */
11617                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11618                                            fpst);
11619                 break;
11620             case 0x12: /* FADDP */
11621                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11622                 break;
11623             case 0x16: /* FMAXP */
11624                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11625                 break;
11626             case 0x18: /* FMINNMP */
11627                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11628                                            fpst);
11629                 break;
11630             case 0x1e: /* FMINP */
11631                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11632                 break;
11633             default:
11634                 g_assert_not_reached();
11635             }
11636         }
11637 
11638         for (pass = 0; pass < maxpass; pass++) {
11639             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11640         }
11641     } else {
11642         for (pass = 0; pass < elements; pass++) {
11643             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11644             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11645             TCGv_i32 tcg_res = tcg_temp_new_i32();
11646 
11647             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11648             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11649 
11650             switch (fpopcode) {
11651             case 0x0: /* FMAXNM */
11652                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11653                 break;
11654             case 0x1: /* FMLA */
11655                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11656                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11657                                            fpst);
11658                 break;
11659             case 0x2: /* FADD */
11660                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11661                 break;
11662             case 0x3: /* FMULX */
11663                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11664                 break;
11665             case 0x4: /* FCMEQ */
11666                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11667                 break;
11668             case 0x6: /* FMAX */
11669                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11670                 break;
11671             case 0x7: /* FRECPS */
11672                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11673                 break;
11674             case 0x8: /* FMINNM */
11675                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11676                 break;
11677             case 0x9: /* FMLS */
11678                 /* As usual for ARM, separate negation for fused multiply-add */
11679                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11680                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11681                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11682                                            fpst);
11683                 break;
11684             case 0xa: /* FSUB */
11685                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11686                 break;
11687             case 0xe: /* FMIN */
11688                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11689                 break;
11690             case 0xf: /* FRSQRTS */
11691                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11692                 break;
11693             case 0x13: /* FMUL */
11694                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11695                 break;
11696             case 0x14: /* FCMGE */
11697                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11698                 break;
11699             case 0x15: /* FACGE */
11700                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11701                 break;
11702             case 0x17: /* FDIV */
11703                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11704                 break;
11705             case 0x1a: /* FABD */
11706                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11707                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11708                 break;
11709             case 0x1c: /* FCMGT */
11710                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11711                 break;
11712             case 0x1d: /* FACGT */
11713                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11714                 break;
11715             default:
11716                 g_assert_not_reached();
11717             }
11718 
11719             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11720         }
11721     }
11722 
11723     clear_vec_high(s, is_q, rd);
11724 }
11725 
11726 /* AdvSIMD three same extra
11727  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11728  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11729  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11730  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11731  */
11732 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11733 {
11734     int rd = extract32(insn, 0, 5);
11735     int rn = extract32(insn, 5, 5);
11736     int opcode = extract32(insn, 11, 4);
11737     int rm = extract32(insn, 16, 5);
11738     int size = extract32(insn, 22, 2);
11739     bool u = extract32(insn, 29, 1);
11740     bool is_q = extract32(insn, 30, 1);
11741     bool feature;
11742     int rot;
11743 
11744     switch (u * 16 + opcode) {
11745     case 0x10: /* SQRDMLAH (vector) */
11746     case 0x11: /* SQRDMLSH (vector) */
11747         if (size != 1 && size != 2) {
11748             unallocated_encoding(s);
11749             return;
11750         }
11751         feature = dc_isar_feature(aa64_rdm, s);
11752         break;
11753     case 0x02: /* SDOT (vector) */
11754     case 0x12: /* UDOT (vector) */
11755         if (size != MO_32) {
11756             unallocated_encoding(s);
11757             return;
11758         }
11759         feature = dc_isar_feature(aa64_dp, s);
11760         break;
11761     case 0x03: /* USDOT */
11762         if (size != MO_32) {
11763             unallocated_encoding(s);
11764             return;
11765         }
11766         feature = dc_isar_feature(aa64_i8mm, s);
11767         break;
11768     case 0x04: /* SMMLA */
11769     case 0x14: /* UMMLA */
11770     case 0x05: /* USMMLA */
11771         if (!is_q || size != MO_32) {
11772             unallocated_encoding(s);
11773             return;
11774         }
11775         feature = dc_isar_feature(aa64_i8mm, s);
11776         break;
11777     case 0x18: /* FCMLA, #0 */
11778     case 0x19: /* FCMLA, #90 */
11779     case 0x1a: /* FCMLA, #180 */
11780     case 0x1b: /* FCMLA, #270 */
11781     case 0x1c: /* FCADD, #90 */
11782     case 0x1e: /* FCADD, #270 */
11783         if (size == 0
11784             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11785             || (size == 3 && !is_q)) {
11786             unallocated_encoding(s);
11787             return;
11788         }
11789         feature = dc_isar_feature(aa64_fcma, s);
11790         break;
11791     case 0x1d: /* BFMMLA */
11792         if (size != MO_16 || !is_q) {
11793             unallocated_encoding(s);
11794             return;
11795         }
11796         feature = dc_isar_feature(aa64_bf16, s);
11797         break;
11798     case 0x1f:
11799         switch (size) {
11800         case 1: /* BFDOT */
11801         case 3: /* BFMLAL{B,T} */
11802             feature = dc_isar_feature(aa64_bf16, s);
11803             break;
11804         default:
11805             unallocated_encoding(s);
11806             return;
11807         }
11808         break;
11809     default:
11810         unallocated_encoding(s);
11811         return;
11812     }
11813     if (!feature) {
11814         unallocated_encoding(s);
11815         return;
11816     }
11817     if (!fp_access_check(s)) {
11818         return;
11819     }
11820 
11821     switch (opcode) {
11822     case 0x0: /* SQRDMLAH (vector) */
11823         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11824         return;
11825 
11826     case 0x1: /* SQRDMLSH (vector) */
11827         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11828         return;
11829 
11830     case 0x2: /* SDOT / UDOT */
11831         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11832                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11833         return;
11834 
11835     case 0x3: /* USDOT */
11836         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11837         return;
11838 
11839     case 0x04: /* SMMLA, UMMLA */
11840         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11841                          u ? gen_helper_gvec_ummla_b
11842                          : gen_helper_gvec_smmla_b);
11843         return;
11844     case 0x05: /* USMMLA */
11845         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11846         return;
11847 
11848     case 0x8: /* FCMLA, #0 */
11849     case 0x9: /* FCMLA, #90 */
11850     case 0xa: /* FCMLA, #180 */
11851     case 0xb: /* FCMLA, #270 */
11852         rot = extract32(opcode, 0, 2);
11853         switch (size) {
11854         case 1:
11855             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11856                               gen_helper_gvec_fcmlah);
11857             break;
11858         case 2:
11859             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11860                               gen_helper_gvec_fcmlas);
11861             break;
11862         case 3:
11863             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11864                               gen_helper_gvec_fcmlad);
11865             break;
11866         default:
11867             g_assert_not_reached();
11868         }
11869         return;
11870 
11871     case 0xc: /* FCADD, #90 */
11872     case 0xe: /* FCADD, #270 */
11873         rot = extract32(opcode, 1, 1);
11874         switch (size) {
11875         case 1:
11876             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11877                               gen_helper_gvec_fcaddh);
11878             break;
11879         case 2:
11880             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11881                               gen_helper_gvec_fcadds);
11882             break;
11883         case 3:
11884             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11885                               gen_helper_gvec_fcaddd);
11886             break;
11887         default:
11888             g_assert_not_reached();
11889         }
11890         return;
11891 
11892     case 0xd: /* BFMMLA */
11893         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11894         return;
11895     case 0xf:
11896         switch (size) {
11897         case 1: /* BFDOT */
11898             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11899             break;
11900         case 3: /* BFMLAL{B,T} */
11901             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11902                               gen_helper_gvec_bfmlal);
11903             break;
11904         default:
11905             g_assert_not_reached();
11906         }
11907         return;
11908 
11909     default:
11910         g_assert_not_reached();
11911     }
11912 }
11913 
11914 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11915                                   int size, int rn, int rd)
11916 {
11917     /* Handle 2-reg-misc ops which are widening (so each size element
11918      * in the source becomes a 2*size element in the destination.
11919      * The only instruction like this is FCVTL.
11920      */
11921     int pass;
11922 
11923     if (size == 3) {
11924         /* 32 -> 64 bit fp conversion */
11925         TCGv_i64 tcg_res[2];
11926         int srcelt = is_q ? 2 : 0;
11927 
11928         for (pass = 0; pass < 2; pass++) {
11929             TCGv_i32 tcg_op = tcg_temp_new_i32();
11930             tcg_res[pass] = tcg_temp_new_i64();
11931 
11932             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11933             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11934         }
11935         for (pass = 0; pass < 2; pass++) {
11936             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11937         }
11938     } else {
11939         /* 16 -> 32 bit fp conversion */
11940         int srcelt = is_q ? 4 : 0;
11941         TCGv_i32 tcg_res[4];
11942         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11943         TCGv_i32 ahp = get_ahp_flag();
11944 
11945         for (pass = 0; pass < 4; pass++) {
11946             tcg_res[pass] = tcg_temp_new_i32();
11947 
11948             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11949             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11950                                            fpst, ahp);
11951         }
11952         for (pass = 0; pass < 4; pass++) {
11953             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11954         }
11955     }
11956 }
11957 
11958 static void handle_rev(DisasContext *s, int opcode, bool u,
11959                        bool is_q, int size, int rn, int rd)
11960 {
11961     int op = (opcode << 1) | u;
11962     int opsz = op + size;
11963     int grp_size = 3 - opsz;
11964     int dsize = is_q ? 128 : 64;
11965     int i;
11966 
11967     if (opsz >= 3) {
11968         unallocated_encoding(s);
11969         return;
11970     }
11971 
11972     if (!fp_access_check(s)) {
11973         return;
11974     }
11975 
11976     if (size == 0) {
11977         /* Special case bytes, use bswap op on each group of elements */
11978         int groups = dsize / (8 << grp_size);
11979 
11980         for (i = 0; i < groups; i++) {
11981             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11982 
11983             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11984             switch (grp_size) {
11985             case MO_16:
11986                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11987                 break;
11988             case MO_32:
11989                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11990                 break;
11991             case MO_64:
11992                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11993                 break;
11994             default:
11995                 g_assert_not_reached();
11996             }
11997             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11998         }
11999         clear_vec_high(s, is_q, rd);
12000     } else {
12001         int revmask = (1 << grp_size) - 1;
12002         int esize = 8 << size;
12003         int elements = dsize / esize;
12004         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12005         TCGv_i64 tcg_rd[2];
12006 
12007         for (i = 0; i < 2; i++) {
12008             tcg_rd[i] = tcg_temp_new_i64();
12009             tcg_gen_movi_i64(tcg_rd[i], 0);
12010         }
12011 
12012         for (i = 0; i < elements; i++) {
12013             int e_rev = (i & 0xf) ^ revmask;
12014             int w = (e_rev * esize) / 64;
12015             int o = (e_rev * esize) % 64;
12016 
12017             read_vec_element(s, tcg_rn, rn, i, size);
12018             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12019         }
12020 
12021         for (i = 0; i < 2; i++) {
12022             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12023         }
12024         clear_vec_high(s, true, rd);
12025     }
12026 }
12027 
12028 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12029                                   bool is_q, int size, int rn, int rd)
12030 {
12031     /* Implement the pairwise operations from 2-misc:
12032      * SADDLP, UADDLP, SADALP, UADALP.
12033      * These all add pairs of elements in the input to produce a
12034      * double-width result element in the output (possibly accumulating).
12035      */
12036     bool accum = (opcode == 0x6);
12037     int maxpass = is_q ? 2 : 1;
12038     int pass;
12039     TCGv_i64 tcg_res[2];
12040 
12041     if (size == 2) {
12042         /* 32 + 32 -> 64 op */
12043         MemOp memop = size + (u ? 0 : MO_SIGN);
12044 
12045         for (pass = 0; pass < maxpass; pass++) {
12046             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12047             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12048 
12049             tcg_res[pass] = tcg_temp_new_i64();
12050 
12051             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12052             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12053             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12054             if (accum) {
12055                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12056                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12057             }
12058         }
12059     } else {
12060         for (pass = 0; pass < maxpass; pass++) {
12061             TCGv_i64 tcg_op = tcg_temp_new_i64();
12062             NeonGenOne64OpFn *genfn;
12063             static NeonGenOne64OpFn * const fns[2][2] = {
12064                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12065                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12066             };
12067 
12068             genfn = fns[size][u];
12069 
12070             tcg_res[pass] = tcg_temp_new_i64();
12071 
12072             read_vec_element(s, tcg_op, rn, pass, MO_64);
12073             genfn(tcg_res[pass], tcg_op);
12074 
12075             if (accum) {
12076                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12077                 if (size == 0) {
12078                     gen_helper_neon_addl_u16(tcg_res[pass],
12079                                              tcg_res[pass], tcg_op);
12080                 } else {
12081                     gen_helper_neon_addl_u32(tcg_res[pass],
12082                                              tcg_res[pass], tcg_op);
12083                 }
12084             }
12085         }
12086     }
12087     if (!is_q) {
12088         tcg_res[1] = tcg_constant_i64(0);
12089     }
12090     for (pass = 0; pass < 2; pass++) {
12091         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12092     }
12093 }
12094 
12095 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12096 {
12097     /* Implement SHLL and SHLL2 */
12098     int pass;
12099     int part = is_q ? 2 : 0;
12100     TCGv_i64 tcg_res[2];
12101 
12102     for (pass = 0; pass < 2; pass++) {
12103         static NeonGenWidenFn * const widenfns[3] = {
12104             gen_helper_neon_widen_u8,
12105             gen_helper_neon_widen_u16,
12106             tcg_gen_extu_i32_i64,
12107         };
12108         NeonGenWidenFn *widenfn = widenfns[size];
12109         TCGv_i32 tcg_op = tcg_temp_new_i32();
12110 
12111         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12112         tcg_res[pass] = tcg_temp_new_i64();
12113         widenfn(tcg_res[pass], tcg_op);
12114         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12115     }
12116 
12117     for (pass = 0; pass < 2; pass++) {
12118         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12119     }
12120 }
12121 
12122 /* AdvSIMD two reg misc
12123  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12124  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12125  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12126  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12127  */
12128 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12129 {
12130     int size = extract32(insn, 22, 2);
12131     int opcode = extract32(insn, 12, 5);
12132     bool u = extract32(insn, 29, 1);
12133     bool is_q = extract32(insn, 30, 1);
12134     int rn = extract32(insn, 5, 5);
12135     int rd = extract32(insn, 0, 5);
12136     bool need_fpstatus = false;
12137     int rmode = -1;
12138     TCGv_i32 tcg_rmode;
12139     TCGv_ptr tcg_fpstatus;
12140 
12141     switch (opcode) {
12142     case 0x0: /* REV64, REV32 */
12143     case 0x1: /* REV16 */
12144         handle_rev(s, opcode, u, is_q, size, rn, rd);
12145         return;
12146     case 0x5: /* CNT, NOT, RBIT */
12147         if (u && size == 0) {
12148             /* NOT */
12149             break;
12150         } else if (u && size == 1) {
12151             /* RBIT */
12152             break;
12153         } else if (!u && size == 0) {
12154             /* CNT */
12155             break;
12156         }
12157         unallocated_encoding(s);
12158         return;
12159     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12160     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12161         if (size == 3) {
12162             unallocated_encoding(s);
12163             return;
12164         }
12165         if (!fp_access_check(s)) {
12166             return;
12167         }
12168 
12169         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12170         return;
12171     case 0x4: /* CLS, CLZ */
12172         if (size == 3) {
12173             unallocated_encoding(s);
12174             return;
12175         }
12176         break;
12177     case 0x2: /* SADDLP, UADDLP */
12178     case 0x6: /* SADALP, UADALP */
12179         if (size == 3) {
12180             unallocated_encoding(s);
12181             return;
12182         }
12183         if (!fp_access_check(s)) {
12184             return;
12185         }
12186         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12187         return;
12188     case 0x13: /* SHLL, SHLL2 */
12189         if (u == 0 || size == 3) {
12190             unallocated_encoding(s);
12191             return;
12192         }
12193         if (!fp_access_check(s)) {
12194             return;
12195         }
12196         handle_shll(s, is_q, size, rn, rd);
12197         return;
12198     case 0xa: /* CMLT */
12199         if (u == 1) {
12200             unallocated_encoding(s);
12201             return;
12202         }
12203         /* fall through */
12204     case 0x8: /* CMGT, CMGE */
12205     case 0x9: /* CMEQ, CMLE */
12206     case 0xb: /* ABS, NEG */
12207         if (size == 3 && !is_q) {
12208             unallocated_encoding(s);
12209             return;
12210         }
12211         break;
12212     case 0x3: /* SUQADD, USQADD */
12213         if (size == 3 && !is_q) {
12214             unallocated_encoding(s);
12215             return;
12216         }
12217         if (!fp_access_check(s)) {
12218             return;
12219         }
12220         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12221         return;
12222     case 0x7: /* SQABS, SQNEG */
12223         if (size == 3 && !is_q) {
12224             unallocated_encoding(s);
12225             return;
12226         }
12227         break;
12228     case 0xc ... 0xf:
12229     case 0x16 ... 0x1f:
12230     {
12231         /* Floating point: U, size[1] and opcode indicate operation;
12232          * size[0] indicates single or double precision.
12233          */
12234         int is_double = extract32(size, 0, 1);
12235         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12236         size = is_double ? 3 : 2;
12237         switch (opcode) {
12238         case 0x2f: /* FABS */
12239         case 0x6f: /* FNEG */
12240             if (size == 3 && !is_q) {
12241                 unallocated_encoding(s);
12242                 return;
12243             }
12244             break;
12245         case 0x1d: /* SCVTF */
12246         case 0x5d: /* UCVTF */
12247         {
12248             bool is_signed = (opcode == 0x1d) ? true : false;
12249             int elements = is_double ? 2 : is_q ? 4 : 2;
12250             if (is_double && !is_q) {
12251                 unallocated_encoding(s);
12252                 return;
12253             }
12254             if (!fp_access_check(s)) {
12255                 return;
12256             }
12257             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12258             return;
12259         }
12260         case 0x2c: /* FCMGT (zero) */
12261         case 0x2d: /* FCMEQ (zero) */
12262         case 0x2e: /* FCMLT (zero) */
12263         case 0x6c: /* FCMGE (zero) */
12264         case 0x6d: /* FCMLE (zero) */
12265             if (size == 3 && !is_q) {
12266                 unallocated_encoding(s);
12267                 return;
12268             }
12269             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12270             return;
12271         case 0x7f: /* FSQRT */
12272             if (size == 3 && !is_q) {
12273                 unallocated_encoding(s);
12274                 return;
12275             }
12276             break;
12277         case 0x1a: /* FCVTNS */
12278         case 0x1b: /* FCVTMS */
12279         case 0x3a: /* FCVTPS */
12280         case 0x3b: /* FCVTZS */
12281         case 0x5a: /* FCVTNU */
12282         case 0x5b: /* FCVTMU */
12283         case 0x7a: /* FCVTPU */
12284         case 0x7b: /* FCVTZU */
12285             need_fpstatus = true;
12286             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12287             if (size == 3 && !is_q) {
12288                 unallocated_encoding(s);
12289                 return;
12290             }
12291             break;
12292         case 0x5c: /* FCVTAU */
12293         case 0x1c: /* FCVTAS */
12294             need_fpstatus = true;
12295             rmode = FPROUNDING_TIEAWAY;
12296             if (size == 3 && !is_q) {
12297                 unallocated_encoding(s);
12298                 return;
12299             }
12300             break;
12301         case 0x3c: /* URECPE */
12302             if (size == 3) {
12303                 unallocated_encoding(s);
12304                 return;
12305             }
12306             /* fall through */
12307         case 0x3d: /* FRECPE */
12308         case 0x7d: /* FRSQRTE */
12309             if (size == 3 && !is_q) {
12310                 unallocated_encoding(s);
12311                 return;
12312             }
12313             if (!fp_access_check(s)) {
12314                 return;
12315             }
12316             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12317             return;
12318         case 0x56: /* FCVTXN, FCVTXN2 */
12319             if (size == 2) {
12320                 unallocated_encoding(s);
12321                 return;
12322             }
12323             /* fall through */
12324         case 0x16: /* FCVTN, FCVTN2 */
12325             /* handle_2misc_narrow does a 2*size -> size operation, but these
12326              * instructions encode the source size rather than dest size.
12327              */
12328             if (!fp_access_check(s)) {
12329                 return;
12330             }
12331             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12332             return;
12333         case 0x36: /* BFCVTN, BFCVTN2 */
12334             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12335                 unallocated_encoding(s);
12336                 return;
12337             }
12338             if (!fp_access_check(s)) {
12339                 return;
12340             }
12341             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12342             return;
12343         case 0x17: /* FCVTL, FCVTL2 */
12344             if (!fp_access_check(s)) {
12345                 return;
12346             }
12347             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12348             return;
12349         case 0x18: /* FRINTN */
12350         case 0x19: /* FRINTM */
12351         case 0x38: /* FRINTP */
12352         case 0x39: /* FRINTZ */
12353             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12354             /* fall through */
12355         case 0x59: /* FRINTX */
12356         case 0x79: /* FRINTI */
12357             need_fpstatus = true;
12358             if (size == 3 && !is_q) {
12359                 unallocated_encoding(s);
12360                 return;
12361             }
12362             break;
12363         case 0x58: /* FRINTA */
12364             rmode = FPROUNDING_TIEAWAY;
12365             need_fpstatus = true;
12366             if (size == 3 && !is_q) {
12367                 unallocated_encoding(s);
12368                 return;
12369             }
12370             break;
12371         case 0x7c: /* URSQRTE */
12372             if (size == 3) {
12373                 unallocated_encoding(s);
12374                 return;
12375             }
12376             break;
12377         case 0x1e: /* FRINT32Z */
12378         case 0x1f: /* FRINT64Z */
12379             rmode = FPROUNDING_ZERO;
12380             /* fall through */
12381         case 0x5e: /* FRINT32X */
12382         case 0x5f: /* FRINT64X */
12383             need_fpstatus = true;
12384             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12385                 unallocated_encoding(s);
12386                 return;
12387             }
12388             break;
12389         default:
12390             unallocated_encoding(s);
12391             return;
12392         }
12393         break;
12394     }
12395     default:
12396         unallocated_encoding(s);
12397         return;
12398     }
12399 
12400     if (!fp_access_check(s)) {
12401         return;
12402     }
12403 
12404     if (need_fpstatus || rmode >= 0) {
12405         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12406     } else {
12407         tcg_fpstatus = NULL;
12408     }
12409     if (rmode >= 0) {
12410         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12411     } else {
12412         tcg_rmode = NULL;
12413     }
12414 
12415     switch (opcode) {
12416     case 0x5:
12417         if (u && size == 0) { /* NOT */
12418             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12419             return;
12420         }
12421         break;
12422     case 0x8: /* CMGT, CMGE */
12423         if (u) {
12424             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12425         } else {
12426             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12427         }
12428         return;
12429     case 0x9: /* CMEQ, CMLE */
12430         if (u) {
12431             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12432         } else {
12433             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12434         }
12435         return;
12436     case 0xa: /* CMLT */
12437         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12438         return;
12439     case 0xb:
12440         if (u) { /* ABS, NEG */
12441             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12442         } else {
12443             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12444         }
12445         return;
12446     }
12447 
12448     if (size == 3) {
12449         /* All 64-bit element operations can be shared with scalar 2misc */
12450         int pass;
12451 
12452         /* Coverity claims (size == 3 && !is_q) has been eliminated
12453          * from all paths leading to here.
12454          */
12455         tcg_debug_assert(is_q);
12456         for (pass = 0; pass < 2; pass++) {
12457             TCGv_i64 tcg_op = tcg_temp_new_i64();
12458             TCGv_i64 tcg_res = tcg_temp_new_i64();
12459 
12460             read_vec_element(s, tcg_op, rn, pass, MO_64);
12461 
12462             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12463                             tcg_rmode, tcg_fpstatus);
12464 
12465             write_vec_element(s, tcg_res, rd, pass, MO_64);
12466         }
12467     } else {
12468         int pass;
12469 
12470         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12471             TCGv_i32 tcg_op = tcg_temp_new_i32();
12472             TCGv_i32 tcg_res = tcg_temp_new_i32();
12473 
12474             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12475 
12476             if (size == 2) {
12477                 /* Special cases for 32 bit elements */
12478                 switch (opcode) {
12479                 case 0x4: /* CLS */
12480                     if (u) {
12481                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12482                     } else {
12483                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12484                     }
12485                     break;
12486                 case 0x7: /* SQABS, SQNEG */
12487                     if (u) {
12488                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12489                     } else {
12490                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12491                     }
12492                     break;
12493                 case 0x2f: /* FABS */
12494                     gen_helper_vfp_abss(tcg_res, tcg_op);
12495                     break;
12496                 case 0x6f: /* FNEG */
12497                     gen_helper_vfp_negs(tcg_res, tcg_op);
12498                     break;
12499                 case 0x7f: /* FSQRT */
12500                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12501                     break;
12502                 case 0x1a: /* FCVTNS */
12503                 case 0x1b: /* FCVTMS */
12504                 case 0x1c: /* FCVTAS */
12505                 case 0x3a: /* FCVTPS */
12506                 case 0x3b: /* FCVTZS */
12507                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12508                                          tcg_constant_i32(0), tcg_fpstatus);
12509                     break;
12510                 case 0x5a: /* FCVTNU */
12511                 case 0x5b: /* FCVTMU */
12512                 case 0x5c: /* FCVTAU */
12513                 case 0x7a: /* FCVTPU */
12514                 case 0x7b: /* FCVTZU */
12515                     gen_helper_vfp_touls(tcg_res, tcg_op,
12516                                          tcg_constant_i32(0), tcg_fpstatus);
12517                     break;
12518                 case 0x18: /* FRINTN */
12519                 case 0x19: /* FRINTM */
12520                 case 0x38: /* FRINTP */
12521                 case 0x39: /* FRINTZ */
12522                 case 0x58: /* FRINTA */
12523                 case 0x79: /* FRINTI */
12524                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12525                     break;
12526                 case 0x59: /* FRINTX */
12527                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12528                     break;
12529                 case 0x7c: /* URSQRTE */
12530                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12531                     break;
12532                 case 0x1e: /* FRINT32Z */
12533                 case 0x5e: /* FRINT32X */
12534                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12535                     break;
12536                 case 0x1f: /* FRINT64Z */
12537                 case 0x5f: /* FRINT64X */
12538                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12539                     break;
12540                 default:
12541                     g_assert_not_reached();
12542                 }
12543             } else {
12544                 /* Use helpers for 8 and 16 bit elements */
12545                 switch (opcode) {
12546                 case 0x5: /* CNT, RBIT */
12547                     /* For these two insns size is part of the opcode specifier
12548                      * (handled earlier); they always operate on byte elements.
12549                      */
12550                     if (u) {
12551                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12552                     } else {
12553                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12554                     }
12555                     break;
12556                 case 0x7: /* SQABS, SQNEG */
12557                 {
12558                     NeonGenOneOpEnvFn *genfn;
12559                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12560                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12561                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12562                     };
12563                     genfn = fns[size][u];
12564                     genfn(tcg_res, cpu_env, tcg_op);
12565                     break;
12566                 }
12567                 case 0x4: /* CLS, CLZ */
12568                     if (u) {
12569                         if (size == 0) {
12570                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12571                         } else {
12572                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12573                         }
12574                     } else {
12575                         if (size == 0) {
12576                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12577                         } else {
12578                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12579                         }
12580                     }
12581                     break;
12582                 default:
12583                     g_assert_not_reached();
12584                 }
12585             }
12586 
12587             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12588         }
12589     }
12590     clear_vec_high(s, is_q, rd);
12591 
12592     if (tcg_rmode) {
12593         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12594     }
12595 }
12596 
12597 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12598  *
12599  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12600  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12601  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12602  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12603  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12604  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12605  *
12606  * This actually covers two groups where scalar access is governed by
12607  * bit 28. A bunch of the instructions (float to integral) only exist
12608  * in the vector form and are un-allocated for the scalar decode. Also
12609  * in the scalar decode Q is always 1.
12610  */
12611 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12612 {
12613     int fpop, opcode, a, u;
12614     int rn, rd;
12615     bool is_q;
12616     bool is_scalar;
12617     bool only_in_vector = false;
12618 
12619     int pass;
12620     TCGv_i32 tcg_rmode = NULL;
12621     TCGv_ptr tcg_fpstatus = NULL;
12622     bool need_fpst = true;
12623     int rmode = -1;
12624 
12625     if (!dc_isar_feature(aa64_fp16, s)) {
12626         unallocated_encoding(s);
12627         return;
12628     }
12629 
12630     rd = extract32(insn, 0, 5);
12631     rn = extract32(insn, 5, 5);
12632 
12633     a = extract32(insn, 23, 1);
12634     u = extract32(insn, 29, 1);
12635     is_scalar = extract32(insn, 28, 1);
12636     is_q = extract32(insn, 30, 1);
12637 
12638     opcode = extract32(insn, 12, 5);
12639     fpop = deposit32(opcode, 5, 1, a);
12640     fpop = deposit32(fpop, 6, 1, u);
12641 
12642     switch (fpop) {
12643     case 0x1d: /* SCVTF */
12644     case 0x5d: /* UCVTF */
12645     {
12646         int elements;
12647 
12648         if (is_scalar) {
12649             elements = 1;
12650         } else {
12651             elements = (is_q ? 8 : 4);
12652         }
12653 
12654         if (!fp_access_check(s)) {
12655             return;
12656         }
12657         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12658         return;
12659     }
12660     break;
12661     case 0x2c: /* FCMGT (zero) */
12662     case 0x2d: /* FCMEQ (zero) */
12663     case 0x2e: /* FCMLT (zero) */
12664     case 0x6c: /* FCMGE (zero) */
12665     case 0x6d: /* FCMLE (zero) */
12666         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12667         return;
12668     case 0x3d: /* FRECPE */
12669     case 0x3f: /* FRECPX */
12670         break;
12671     case 0x18: /* FRINTN */
12672         only_in_vector = true;
12673         rmode = FPROUNDING_TIEEVEN;
12674         break;
12675     case 0x19: /* FRINTM */
12676         only_in_vector = true;
12677         rmode = FPROUNDING_NEGINF;
12678         break;
12679     case 0x38: /* FRINTP */
12680         only_in_vector = true;
12681         rmode = FPROUNDING_POSINF;
12682         break;
12683     case 0x39: /* FRINTZ */
12684         only_in_vector = true;
12685         rmode = FPROUNDING_ZERO;
12686         break;
12687     case 0x58: /* FRINTA */
12688         only_in_vector = true;
12689         rmode = FPROUNDING_TIEAWAY;
12690         break;
12691     case 0x59: /* FRINTX */
12692     case 0x79: /* FRINTI */
12693         only_in_vector = true;
12694         /* current rounding mode */
12695         break;
12696     case 0x1a: /* FCVTNS */
12697         rmode = FPROUNDING_TIEEVEN;
12698         break;
12699     case 0x1b: /* FCVTMS */
12700         rmode = FPROUNDING_NEGINF;
12701         break;
12702     case 0x1c: /* FCVTAS */
12703         rmode = FPROUNDING_TIEAWAY;
12704         break;
12705     case 0x3a: /* FCVTPS */
12706         rmode = FPROUNDING_POSINF;
12707         break;
12708     case 0x3b: /* FCVTZS */
12709         rmode = FPROUNDING_ZERO;
12710         break;
12711     case 0x5a: /* FCVTNU */
12712         rmode = FPROUNDING_TIEEVEN;
12713         break;
12714     case 0x5b: /* FCVTMU */
12715         rmode = FPROUNDING_NEGINF;
12716         break;
12717     case 0x5c: /* FCVTAU */
12718         rmode = FPROUNDING_TIEAWAY;
12719         break;
12720     case 0x7a: /* FCVTPU */
12721         rmode = FPROUNDING_POSINF;
12722         break;
12723     case 0x7b: /* FCVTZU */
12724         rmode = FPROUNDING_ZERO;
12725         break;
12726     case 0x2f: /* FABS */
12727     case 0x6f: /* FNEG */
12728         need_fpst = false;
12729         break;
12730     case 0x7d: /* FRSQRTE */
12731     case 0x7f: /* FSQRT (vector) */
12732         break;
12733     default:
12734         unallocated_encoding(s);
12735         return;
12736     }
12737 
12738 
12739     /* Check additional constraints for the scalar encoding */
12740     if (is_scalar) {
12741         if (!is_q) {
12742             unallocated_encoding(s);
12743             return;
12744         }
12745         /* FRINTxx is only in the vector form */
12746         if (only_in_vector) {
12747             unallocated_encoding(s);
12748             return;
12749         }
12750     }
12751 
12752     if (!fp_access_check(s)) {
12753         return;
12754     }
12755 
12756     if (rmode >= 0 || need_fpst) {
12757         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12758     }
12759 
12760     if (rmode >= 0) {
12761         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12762     }
12763 
12764     if (is_scalar) {
12765         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12766         TCGv_i32 tcg_res = tcg_temp_new_i32();
12767 
12768         switch (fpop) {
12769         case 0x1a: /* FCVTNS */
12770         case 0x1b: /* FCVTMS */
12771         case 0x1c: /* FCVTAS */
12772         case 0x3a: /* FCVTPS */
12773         case 0x3b: /* FCVTZS */
12774             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12775             break;
12776         case 0x3d: /* FRECPE */
12777             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12778             break;
12779         case 0x3f: /* FRECPX */
12780             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12781             break;
12782         case 0x5a: /* FCVTNU */
12783         case 0x5b: /* FCVTMU */
12784         case 0x5c: /* FCVTAU */
12785         case 0x7a: /* FCVTPU */
12786         case 0x7b: /* FCVTZU */
12787             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12788             break;
12789         case 0x6f: /* FNEG */
12790             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12791             break;
12792         case 0x7d: /* FRSQRTE */
12793             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12794             break;
12795         default:
12796             g_assert_not_reached();
12797         }
12798 
12799         /* limit any sign extension going on */
12800         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12801         write_fp_sreg(s, rd, tcg_res);
12802     } else {
12803         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12804             TCGv_i32 tcg_op = tcg_temp_new_i32();
12805             TCGv_i32 tcg_res = tcg_temp_new_i32();
12806 
12807             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12808 
12809             switch (fpop) {
12810             case 0x1a: /* FCVTNS */
12811             case 0x1b: /* FCVTMS */
12812             case 0x1c: /* FCVTAS */
12813             case 0x3a: /* FCVTPS */
12814             case 0x3b: /* FCVTZS */
12815                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12816                 break;
12817             case 0x3d: /* FRECPE */
12818                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12819                 break;
12820             case 0x5a: /* FCVTNU */
12821             case 0x5b: /* FCVTMU */
12822             case 0x5c: /* FCVTAU */
12823             case 0x7a: /* FCVTPU */
12824             case 0x7b: /* FCVTZU */
12825                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12826                 break;
12827             case 0x18: /* FRINTN */
12828             case 0x19: /* FRINTM */
12829             case 0x38: /* FRINTP */
12830             case 0x39: /* FRINTZ */
12831             case 0x58: /* FRINTA */
12832             case 0x79: /* FRINTI */
12833                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12834                 break;
12835             case 0x59: /* FRINTX */
12836                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12837                 break;
12838             case 0x2f: /* FABS */
12839                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12840                 break;
12841             case 0x6f: /* FNEG */
12842                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12843                 break;
12844             case 0x7d: /* FRSQRTE */
12845                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12846                 break;
12847             case 0x7f: /* FSQRT */
12848                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12849                 break;
12850             default:
12851                 g_assert_not_reached();
12852             }
12853 
12854             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12855         }
12856 
12857         clear_vec_high(s, is_q, rd);
12858     }
12859 
12860     if (tcg_rmode) {
12861         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12862     }
12863 }
12864 
12865 /* AdvSIMD scalar x indexed element
12866  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12867  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12868  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12869  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12870  * AdvSIMD vector x indexed element
12871  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12872  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12873  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12874  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12875  */
12876 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12877 {
12878     /* This encoding has two kinds of instruction:
12879      *  normal, where we perform elt x idxelt => elt for each
12880      *     element in the vector
12881      *  long, where we perform elt x idxelt and generate a result of
12882      *     double the width of the input element
12883      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12884      */
12885     bool is_scalar = extract32(insn, 28, 1);
12886     bool is_q = extract32(insn, 30, 1);
12887     bool u = extract32(insn, 29, 1);
12888     int size = extract32(insn, 22, 2);
12889     int l = extract32(insn, 21, 1);
12890     int m = extract32(insn, 20, 1);
12891     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12892     int rm = extract32(insn, 16, 4);
12893     int opcode = extract32(insn, 12, 4);
12894     int h = extract32(insn, 11, 1);
12895     int rn = extract32(insn, 5, 5);
12896     int rd = extract32(insn, 0, 5);
12897     bool is_long = false;
12898     int is_fp = 0;
12899     bool is_fp16 = false;
12900     int index;
12901     TCGv_ptr fpst;
12902 
12903     switch (16 * u + opcode) {
12904     case 0x08: /* MUL */
12905     case 0x10: /* MLA */
12906     case 0x14: /* MLS */
12907         if (is_scalar) {
12908             unallocated_encoding(s);
12909             return;
12910         }
12911         break;
12912     case 0x02: /* SMLAL, SMLAL2 */
12913     case 0x12: /* UMLAL, UMLAL2 */
12914     case 0x06: /* SMLSL, SMLSL2 */
12915     case 0x16: /* UMLSL, UMLSL2 */
12916     case 0x0a: /* SMULL, SMULL2 */
12917     case 0x1a: /* UMULL, UMULL2 */
12918         if (is_scalar) {
12919             unallocated_encoding(s);
12920             return;
12921         }
12922         is_long = true;
12923         break;
12924     case 0x03: /* SQDMLAL, SQDMLAL2 */
12925     case 0x07: /* SQDMLSL, SQDMLSL2 */
12926     case 0x0b: /* SQDMULL, SQDMULL2 */
12927         is_long = true;
12928         break;
12929     case 0x0c: /* SQDMULH */
12930     case 0x0d: /* SQRDMULH */
12931         break;
12932     case 0x01: /* FMLA */
12933     case 0x05: /* FMLS */
12934     case 0x09: /* FMUL */
12935     case 0x19: /* FMULX */
12936         is_fp = 1;
12937         break;
12938     case 0x1d: /* SQRDMLAH */
12939     case 0x1f: /* SQRDMLSH */
12940         if (!dc_isar_feature(aa64_rdm, s)) {
12941             unallocated_encoding(s);
12942             return;
12943         }
12944         break;
12945     case 0x0e: /* SDOT */
12946     case 0x1e: /* UDOT */
12947         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12948             unallocated_encoding(s);
12949             return;
12950         }
12951         break;
12952     case 0x0f:
12953         switch (size) {
12954         case 0: /* SUDOT */
12955         case 2: /* USDOT */
12956             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12957                 unallocated_encoding(s);
12958                 return;
12959             }
12960             size = MO_32;
12961             break;
12962         case 1: /* BFDOT */
12963             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12964                 unallocated_encoding(s);
12965                 return;
12966             }
12967             size = MO_32;
12968             break;
12969         case 3: /* BFMLAL{B,T} */
12970             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12971                 unallocated_encoding(s);
12972                 return;
12973             }
12974             /* can't set is_fp without other incorrect size checks */
12975             size = MO_16;
12976             break;
12977         default:
12978             unallocated_encoding(s);
12979             return;
12980         }
12981         break;
12982     case 0x11: /* FCMLA #0 */
12983     case 0x13: /* FCMLA #90 */
12984     case 0x15: /* FCMLA #180 */
12985     case 0x17: /* FCMLA #270 */
12986         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12987             unallocated_encoding(s);
12988             return;
12989         }
12990         is_fp = 2;
12991         break;
12992     case 0x00: /* FMLAL */
12993     case 0x04: /* FMLSL */
12994     case 0x18: /* FMLAL2 */
12995     case 0x1c: /* FMLSL2 */
12996         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12997             unallocated_encoding(s);
12998             return;
12999         }
13000         size = MO_16;
13001         /* is_fp, but we pass cpu_env not fp_status.  */
13002         break;
13003     default:
13004         unallocated_encoding(s);
13005         return;
13006     }
13007 
13008     switch (is_fp) {
13009     case 1: /* normal fp */
13010         /* convert insn encoded size to MemOp size */
13011         switch (size) {
13012         case 0: /* half-precision */
13013             size = MO_16;
13014             is_fp16 = true;
13015             break;
13016         case MO_32: /* single precision */
13017         case MO_64: /* double precision */
13018             break;
13019         default:
13020             unallocated_encoding(s);
13021             return;
13022         }
13023         break;
13024 
13025     case 2: /* complex fp */
13026         /* Each indexable element is a complex pair.  */
13027         size += 1;
13028         switch (size) {
13029         case MO_32:
13030             if (h && !is_q) {
13031                 unallocated_encoding(s);
13032                 return;
13033             }
13034             is_fp16 = true;
13035             break;
13036         case MO_64:
13037             break;
13038         default:
13039             unallocated_encoding(s);
13040             return;
13041         }
13042         break;
13043 
13044     default: /* integer */
13045         switch (size) {
13046         case MO_8:
13047         case MO_64:
13048             unallocated_encoding(s);
13049             return;
13050         }
13051         break;
13052     }
13053     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13054         unallocated_encoding(s);
13055         return;
13056     }
13057 
13058     /* Given MemOp size, adjust register and indexing.  */
13059     switch (size) {
13060     case MO_16:
13061         index = h << 2 | l << 1 | m;
13062         break;
13063     case MO_32:
13064         index = h << 1 | l;
13065         rm |= m << 4;
13066         break;
13067     case MO_64:
13068         if (l || !is_q) {
13069             unallocated_encoding(s);
13070             return;
13071         }
13072         index = h;
13073         rm |= m << 4;
13074         break;
13075     default:
13076         g_assert_not_reached();
13077     }
13078 
13079     if (!fp_access_check(s)) {
13080         return;
13081     }
13082 
13083     if (is_fp) {
13084         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13085     } else {
13086         fpst = NULL;
13087     }
13088 
13089     switch (16 * u + opcode) {
13090     case 0x0e: /* SDOT */
13091     case 0x1e: /* UDOT */
13092         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13093                          u ? gen_helper_gvec_udot_idx_b
13094                          : gen_helper_gvec_sdot_idx_b);
13095         return;
13096     case 0x0f:
13097         switch (extract32(insn, 22, 2)) {
13098         case 0: /* SUDOT */
13099             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13100                              gen_helper_gvec_sudot_idx_b);
13101             return;
13102         case 1: /* BFDOT */
13103             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13104                              gen_helper_gvec_bfdot_idx);
13105             return;
13106         case 2: /* USDOT */
13107             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13108                              gen_helper_gvec_usdot_idx_b);
13109             return;
13110         case 3: /* BFMLAL{B,T} */
13111             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13112                               gen_helper_gvec_bfmlal_idx);
13113             return;
13114         }
13115         g_assert_not_reached();
13116     case 0x11: /* FCMLA #0 */
13117     case 0x13: /* FCMLA #90 */
13118     case 0x15: /* FCMLA #180 */
13119     case 0x17: /* FCMLA #270 */
13120         {
13121             int rot = extract32(insn, 13, 2);
13122             int data = (index << 2) | rot;
13123             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13124                                vec_full_reg_offset(s, rn),
13125                                vec_full_reg_offset(s, rm),
13126                                vec_full_reg_offset(s, rd), fpst,
13127                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13128                                size == MO_64
13129                                ? gen_helper_gvec_fcmlas_idx
13130                                : gen_helper_gvec_fcmlah_idx);
13131         }
13132         return;
13133 
13134     case 0x00: /* FMLAL */
13135     case 0x04: /* FMLSL */
13136     case 0x18: /* FMLAL2 */
13137     case 0x1c: /* FMLSL2 */
13138         {
13139             int is_s = extract32(opcode, 2, 1);
13140             int is_2 = u;
13141             int data = (index << 2) | (is_2 << 1) | is_s;
13142             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13143                                vec_full_reg_offset(s, rn),
13144                                vec_full_reg_offset(s, rm), cpu_env,
13145                                is_q ? 16 : 8, vec_full_reg_size(s),
13146                                data, gen_helper_gvec_fmlal_idx_a64);
13147         }
13148         return;
13149 
13150     case 0x08: /* MUL */
13151         if (!is_long && !is_scalar) {
13152             static gen_helper_gvec_3 * const fns[3] = {
13153                 gen_helper_gvec_mul_idx_h,
13154                 gen_helper_gvec_mul_idx_s,
13155                 gen_helper_gvec_mul_idx_d,
13156             };
13157             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13158                                vec_full_reg_offset(s, rn),
13159                                vec_full_reg_offset(s, rm),
13160                                is_q ? 16 : 8, vec_full_reg_size(s),
13161                                index, fns[size - 1]);
13162             return;
13163         }
13164         break;
13165 
13166     case 0x10: /* MLA */
13167         if (!is_long && !is_scalar) {
13168             static gen_helper_gvec_4 * const fns[3] = {
13169                 gen_helper_gvec_mla_idx_h,
13170                 gen_helper_gvec_mla_idx_s,
13171                 gen_helper_gvec_mla_idx_d,
13172             };
13173             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13174                                vec_full_reg_offset(s, rn),
13175                                vec_full_reg_offset(s, rm),
13176                                vec_full_reg_offset(s, rd),
13177                                is_q ? 16 : 8, vec_full_reg_size(s),
13178                                index, fns[size - 1]);
13179             return;
13180         }
13181         break;
13182 
13183     case 0x14: /* MLS */
13184         if (!is_long && !is_scalar) {
13185             static gen_helper_gvec_4 * const fns[3] = {
13186                 gen_helper_gvec_mls_idx_h,
13187                 gen_helper_gvec_mls_idx_s,
13188                 gen_helper_gvec_mls_idx_d,
13189             };
13190             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13191                                vec_full_reg_offset(s, rn),
13192                                vec_full_reg_offset(s, rm),
13193                                vec_full_reg_offset(s, rd),
13194                                is_q ? 16 : 8, vec_full_reg_size(s),
13195                                index, fns[size - 1]);
13196             return;
13197         }
13198         break;
13199     }
13200 
13201     if (size == 3) {
13202         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13203         int pass;
13204 
13205         assert(is_fp && is_q && !is_long);
13206 
13207         read_vec_element(s, tcg_idx, rm, index, MO_64);
13208 
13209         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13210             TCGv_i64 tcg_op = tcg_temp_new_i64();
13211             TCGv_i64 tcg_res = tcg_temp_new_i64();
13212 
13213             read_vec_element(s, tcg_op, rn, pass, MO_64);
13214 
13215             switch (16 * u + opcode) {
13216             case 0x05: /* FMLS */
13217                 /* As usual for ARM, separate negation for fused multiply-add */
13218                 gen_helper_vfp_negd(tcg_op, tcg_op);
13219                 /* fall through */
13220             case 0x01: /* FMLA */
13221                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13222                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13223                 break;
13224             case 0x09: /* FMUL */
13225                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13226                 break;
13227             case 0x19: /* FMULX */
13228                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13229                 break;
13230             default:
13231                 g_assert_not_reached();
13232             }
13233 
13234             write_vec_element(s, tcg_res, rd, pass, MO_64);
13235         }
13236 
13237         clear_vec_high(s, !is_scalar, rd);
13238     } else if (!is_long) {
13239         /* 32 bit floating point, or 16 or 32 bit integer.
13240          * For the 16 bit scalar case we use the usual Neon helpers and
13241          * rely on the fact that 0 op 0 == 0 with no side effects.
13242          */
13243         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13244         int pass, maxpasses;
13245 
13246         if (is_scalar) {
13247             maxpasses = 1;
13248         } else {
13249             maxpasses = is_q ? 4 : 2;
13250         }
13251 
13252         read_vec_element_i32(s, tcg_idx, rm, index, size);
13253 
13254         if (size == 1 && !is_scalar) {
13255             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13256              * the index into both halves of the 32 bit tcg_idx and then use
13257              * the usual Neon helpers.
13258              */
13259             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13260         }
13261 
13262         for (pass = 0; pass < maxpasses; pass++) {
13263             TCGv_i32 tcg_op = tcg_temp_new_i32();
13264             TCGv_i32 tcg_res = tcg_temp_new_i32();
13265 
13266             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13267 
13268             switch (16 * u + opcode) {
13269             case 0x08: /* MUL */
13270             case 0x10: /* MLA */
13271             case 0x14: /* MLS */
13272             {
13273                 static NeonGenTwoOpFn * const fns[2][2] = {
13274                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13275                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13276                 };
13277                 NeonGenTwoOpFn *genfn;
13278                 bool is_sub = opcode == 0x4;
13279 
13280                 if (size == 1) {
13281                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13282                 } else {
13283                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13284                 }
13285                 if (opcode == 0x8) {
13286                     break;
13287                 }
13288                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13289                 genfn = fns[size - 1][is_sub];
13290                 genfn(tcg_res, tcg_op, tcg_res);
13291                 break;
13292             }
13293             case 0x05: /* FMLS */
13294             case 0x01: /* FMLA */
13295                 read_vec_element_i32(s, tcg_res, rd, pass,
13296                                      is_scalar ? size : MO_32);
13297                 switch (size) {
13298                 case 1:
13299                     if (opcode == 0x5) {
13300                         /* As usual for ARM, separate negation for fused
13301                          * multiply-add */
13302                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13303                     }
13304                     if (is_scalar) {
13305                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13306                                                    tcg_res, fpst);
13307                     } else {
13308                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13309                                                     tcg_res, fpst);
13310                     }
13311                     break;
13312                 case 2:
13313                     if (opcode == 0x5) {
13314                         /* As usual for ARM, separate negation for
13315                          * fused multiply-add */
13316                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13317                     }
13318                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13319                                            tcg_res, fpst);
13320                     break;
13321                 default:
13322                     g_assert_not_reached();
13323                 }
13324                 break;
13325             case 0x09: /* FMUL */
13326                 switch (size) {
13327                 case 1:
13328                     if (is_scalar) {
13329                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13330                                                 tcg_idx, fpst);
13331                     } else {
13332                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13333                                                  tcg_idx, fpst);
13334                     }
13335                     break;
13336                 case 2:
13337                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13338                     break;
13339                 default:
13340                     g_assert_not_reached();
13341                 }
13342                 break;
13343             case 0x19: /* FMULX */
13344                 switch (size) {
13345                 case 1:
13346                     if (is_scalar) {
13347                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13348                                                  tcg_idx, fpst);
13349                     } else {
13350                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13351                                                   tcg_idx, fpst);
13352                     }
13353                     break;
13354                 case 2:
13355                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13356                     break;
13357                 default:
13358                     g_assert_not_reached();
13359                 }
13360                 break;
13361             case 0x0c: /* SQDMULH */
13362                 if (size == 1) {
13363                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13364                                                tcg_op, tcg_idx);
13365                 } else {
13366                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13367                                                tcg_op, tcg_idx);
13368                 }
13369                 break;
13370             case 0x0d: /* SQRDMULH */
13371                 if (size == 1) {
13372                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13373                                                 tcg_op, tcg_idx);
13374                 } else {
13375                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13376                                                 tcg_op, tcg_idx);
13377                 }
13378                 break;
13379             case 0x1d: /* SQRDMLAH */
13380                 read_vec_element_i32(s, tcg_res, rd, pass,
13381                                      is_scalar ? size : MO_32);
13382                 if (size == 1) {
13383                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13384                                                 tcg_op, tcg_idx, tcg_res);
13385                 } else {
13386                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13387                                                 tcg_op, tcg_idx, tcg_res);
13388                 }
13389                 break;
13390             case 0x1f: /* SQRDMLSH */
13391                 read_vec_element_i32(s, tcg_res, rd, pass,
13392                                      is_scalar ? size : MO_32);
13393                 if (size == 1) {
13394                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13395                                                 tcg_op, tcg_idx, tcg_res);
13396                 } else {
13397                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13398                                                 tcg_op, tcg_idx, tcg_res);
13399                 }
13400                 break;
13401             default:
13402                 g_assert_not_reached();
13403             }
13404 
13405             if (is_scalar) {
13406                 write_fp_sreg(s, rd, tcg_res);
13407             } else {
13408                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13409             }
13410         }
13411 
13412         clear_vec_high(s, is_q, rd);
13413     } else {
13414         /* long ops: 16x16->32 or 32x32->64 */
13415         TCGv_i64 tcg_res[2];
13416         int pass;
13417         bool satop = extract32(opcode, 0, 1);
13418         MemOp memop = MO_32;
13419 
13420         if (satop || !u) {
13421             memop |= MO_SIGN;
13422         }
13423 
13424         if (size == 2) {
13425             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13426 
13427             read_vec_element(s, tcg_idx, rm, index, memop);
13428 
13429             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13430                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13431                 TCGv_i64 tcg_passres;
13432                 int passelt;
13433 
13434                 if (is_scalar) {
13435                     passelt = 0;
13436                 } else {
13437                     passelt = pass + (is_q * 2);
13438                 }
13439 
13440                 read_vec_element(s, tcg_op, rn, passelt, memop);
13441 
13442                 tcg_res[pass] = tcg_temp_new_i64();
13443 
13444                 if (opcode == 0xa || opcode == 0xb) {
13445                     /* Non-accumulating ops */
13446                     tcg_passres = tcg_res[pass];
13447                 } else {
13448                     tcg_passres = tcg_temp_new_i64();
13449                 }
13450 
13451                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13452 
13453                 if (satop) {
13454                     /* saturating, doubling */
13455                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13456                                                       tcg_passres, tcg_passres);
13457                 }
13458 
13459                 if (opcode == 0xa || opcode == 0xb) {
13460                     continue;
13461                 }
13462 
13463                 /* Accumulating op: handle accumulate step */
13464                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13465 
13466                 switch (opcode) {
13467                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13468                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13469                     break;
13470                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13471                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13472                     break;
13473                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13474                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13475                     /* fall through */
13476                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13477                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13478                                                       tcg_res[pass],
13479                                                       tcg_passres);
13480                     break;
13481                 default:
13482                     g_assert_not_reached();
13483                 }
13484             }
13485 
13486             clear_vec_high(s, !is_scalar, rd);
13487         } else {
13488             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13489 
13490             assert(size == 1);
13491             read_vec_element_i32(s, tcg_idx, rm, index, size);
13492 
13493             if (!is_scalar) {
13494                 /* The simplest way to handle the 16x16 indexed ops is to
13495                  * duplicate the index into both halves of the 32 bit tcg_idx
13496                  * and then use the usual Neon helpers.
13497                  */
13498                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13499             }
13500 
13501             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13502                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13503                 TCGv_i64 tcg_passres;
13504 
13505                 if (is_scalar) {
13506                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13507                 } else {
13508                     read_vec_element_i32(s, tcg_op, rn,
13509                                          pass + (is_q * 2), MO_32);
13510                 }
13511 
13512                 tcg_res[pass] = tcg_temp_new_i64();
13513 
13514                 if (opcode == 0xa || opcode == 0xb) {
13515                     /* Non-accumulating ops */
13516                     tcg_passres = tcg_res[pass];
13517                 } else {
13518                     tcg_passres = tcg_temp_new_i64();
13519                 }
13520 
13521                 if (memop & MO_SIGN) {
13522                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13523                 } else {
13524                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13525                 }
13526                 if (satop) {
13527                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13528                                                       tcg_passres, tcg_passres);
13529                 }
13530 
13531                 if (opcode == 0xa || opcode == 0xb) {
13532                     continue;
13533                 }
13534 
13535                 /* Accumulating op: handle accumulate step */
13536                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13537 
13538                 switch (opcode) {
13539                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13540                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13541                                              tcg_passres);
13542                     break;
13543                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13544                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13545                                              tcg_passres);
13546                     break;
13547                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13548                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13549                     /* fall through */
13550                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13551                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13552                                                       tcg_res[pass],
13553                                                       tcg_passres);
13554                     break;
13555                 default:
13556                     g_assert_not_reached();
13557                 }
13558             }
13559 
13560             if (is_scalar) {
13561                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13562             }
13563         }
13564 
13565         if (is_scalar) {
13566             tcg_res[1] = tcg_constant_i64(0);
13567         }
13568 
13569         for (pass = 0; pass < 2; pass++) {
13570             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13571         }
13572     }
13573 }
13574 
13575 /* Crypto AES
13576  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13577  * +-----------------+------+-----------+--------+-----+------+------+
13578  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13579  * +-----------------+------+-----------+--------+-----+------+------+
13580  */
13581 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13582 {
13583     int size = extract32(insn, 22, 2);
13584     int opcode = extract32(insn, 12, 5);
13585     int rn = extract32(insn, 5, 5);
13586     int rd = extract32(insn, 0, 5);
13587     int decrypt;
13588     gen_helper_gvec_2 *genfn2 = NULL;
13589     gen_helper_gvec_3 *genfn3 = NULL;
13590 
13591     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13592         unallocated_encoding(s);
13593         return;
13594     }
13595 
13596     switch (opcode) {
13597     case 0x4: /* AESE */
13598         decrypt = 0;
13599         genfn3 = gen_helper_crypto_aese;
13600         break;
13601     case 0x6: /* AESMC */
13602         decrypt = 0;
13603         genfn2 = gen_helper_crypto_aesmc;
13604         break;
13605     case 0x5: /* AESD */
13606         decrypt = 1;
13607         genfn3 = gen_helper_crypto_aese;
13608         break;
13609     case 0x7: /* AESIMC */
13610         decrypt = 1;
13611         genfn2 = gen_helper_crypto_aesmc;
13612         break;
13613     default:
13614         unallocated_encoding(s);
13615         return;
13616     }
13617 
13618     if (!fp_access_check(s)) {
13619         return;
13620     }
13621     if (genfn2) {
13622         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13623     } else {
13624         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13625     }
13626 }
13627 
13628 /* Crypto three-reg SHA
13629  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13630  * +-----------------+------+---+------+---+--------+-----+------+------+
13631  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13632  * +-----------------+------+---+------+---+--------+-----+------+------+
13633  */
13634 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13635 {
13636     int size = extract32(insn, 22, 2);
13637     int opcode = extract32(insn, 12, 3);
13638     int rm = extract32(insn, 16, 5);
13639     int rn = extract32(insn, 5, 5);
13640     int rd = extract32(insn, 0, 5);
13641     gen_helper_gvec_3 *genfn;
13642     bool feature;
13643 
13644     if (size != 0) {
13645         unallocated_encoding(s);
13646         return;
13647     }
13648 
13649     switch (opcode) {
13650     case 0: /* SHA1C */
13651         genfn = gen_helper_crypto_sha1c;
13652         feature = dc_isar_feature(aa64_sha1, s);
13653         break;
13654     case 1: /* SHA1P */
13655         genfn = gen_helper_crypto_sha1p;
13656         feature = dc_isar_feature(aa64_sha1, s);
13657         break;
13658     case 2: /* SHA1M */
13659         genfn = gen_helper_crypto_sha1m;
13660         feature = dc_isar_feature(aa64_sha1, s);
13661         break;
13662     case 3: /* SHA1SU0 */
13663         genfn = gen_helper_crypto_sha1su0;
13664         feature = dc_isar_feature(aa64_sha1, s);
13665         break;
13666     case 4: /* SHA256H */
13667         genfn = gen_helper_crypto_sha256h;
13668         feature = dc_isar_feature(aa64_sha256, s);
13669         break;
13670     case 5: /* SHA256H2 */
13671         genfn = gen_helper_crypto_sha256h2;
13672         feature = dc_isar_feature(aa64_sha256, s);
13673         break;
13674     case 6: /* SHA256SU1 */
13675         genfn = gen_helper_crypto_sha256su1;
13676         feature = dc_isar_feature(aa64_sha256, s);
13677         break;
13678     default:
13679         unallocated_encoding(s);
13680         return;
13681     }
13682 
13683     if (!feature) {
13684         unallocated_encoding(s);
13685         return;
13686     }
13687 
13688     if (!fp_access_check(s)) {
13689         return;
13690     }
13691     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13692 }
13693 
13694 /* Crypto two-reg SHA
13695  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13696  * +-----------------+------+-----------+--------+-----+------+------+
13697  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13698  * +-----------------+------+-----------+--------+-----+------+------+
13699  */
13700 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13701 {
13702     int size = extract32(insn, 22, 2);
13703     int opcode = extract32(insn, 12, 5);
13704     int rn = extract32(insn, 5, 5);
13705     int rd = extract32(insn, 0, 5);
13706     gen_helper_gvec_2 *genfn;
13707     bool feature;
13708 
13709     if (size != 0) {
13710         unallocated_encoding(s);
13711         return;
13712     }
13713 
13714     switch (opcode) {
13715     case 0: /* SHA1H */
13716         feature = dc_isar_feature(aa64_sha1, s);
13717         genfn = gen_helper_crypto_sha1h;
13718         break;
13719     case 1: /* SHA1SU1 */
13720         feature = dc_isar_feature(aa64_sha1, s);
13721         genfn = gen_helper_crypto_sha1su1;
13722         break;
13723     case 2: /* SHA256SU0 */
13724         feature = dc_isar_feature(aa64_sha256, s);
13725         genfn = gen_helper_crypto_sha256su0;
13726         break;
13727     default:
13728         unallocated_encoding(s);
13729         return;
13730     }
13731 
13732     if (!feature) {
13733         unallocated_encoding(s);
13734         return;
13735     }
13736 
13737     if (!fp_access_check(s)) {
13738         return;
13739     }
13740     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13741 }
13742 
13743 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13744 {
13745     tcg_gen_rotli_i64(d, m, 1);
13746     tcg_gen_xor_i64(d, d, n);
13747 }
13748 
13749 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13750 {
13751     tcg_gen_rotli_vec(vece, d, m, 1);
13752     tcg_gen_xor_vec(vece, d, d, n);
13753 }
13754 
13755 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13756                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13757 {
13758     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13759     static const GVecGen3 op = {
13760         .fni8 = gen_rax1_i64,
13761         .fniv = gen_rax1_vec,
13762         .opt_opc = vecop_list,
13763         .fno = gen_helper_crypto_rax1,
13764         .vece = MO_64,
13765     };
13766     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13767 }
13768 
13769 /* Crypto three-reg SHA512
13770  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13771  * +-----------------------+------+---+---+-----+--------+------+------+
13772  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13773  * +-----------------------+------+---+---+-----+--------+------+------+
13774  */
13775 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13776 {
13777     int opcode = extract32(insn, 10, 2);
13778     int o =  extract32(insn, 14, 1);
13779     int rm = extract32(insn, 16, 5);
13780     int rn = extract32(insn, 5, 5);
13781     int rd = extract32(insn, 0, 5);
13782     bool feature;
13783     gen_helper_gvec_3 *oolfn = NULL;
13784     GVecGen3Fn *gvecfn = NULL;
13785 
13786     if (o == 0) {
13787         switch (opcode) {
13788         case 0: /* SHA512H */
13789             feature = dc_isar_feature(aa64_sha512, s);
13790             oolfn = gen_helper_crypto_sha512h;
13791             break;
13792         case 1: /* SHA512H2 */
13793             feature = dc_isar_feature(aa64_sha512, s);
13794             oolfn = gen_helper_crypto_sha512h2;
13795             break;
13796         case 2: /* SHA512SU1 */
13797             feature = dc_isar_feature(aa64_sha512, s);
13798             oolfn = gen_helper_crypto_sha512su1;
13799             break;
13800         case 3: /* RAX1 */
13801             feature = dc_isar_feature(aa64_sha3, s);
13802             gvecfn = gen_gvec_rax1;
13803             break;
13804         default:
13805             g_assert_not_reached();
13806         }
13807     } else {
13808         switch (opcode) {
13809         case 0: /* SM3PARTW1 */
13810             feature = dc_isar_feature(aa64_sm3, s);
13811             oolfn = gen_helper_crypto_sm3partw1;
13812             break;
13813         case 1: /* SM3PARTW2 */
13814             feature = dc_isar_feature(aa64_sm3, s);
13815             oolfn = gen_helper_crypto_sm3partw2;
13816             break;
13817         case 2: /* SM4EKEY */
13818             feature = dc_isar_feature(aa64_sm4, s);
13819             oolfn = gen_helper_crypto_sm4ekey;
13820             break;
13821         default:
13822             unallocated_encoding(s);
13823             return;
13824         }
13825     }
13826 
13827     if (!feature) {
13828         unallocated_encoding(s);
13829         return;
13830     }
13831 
13832     if (!fp_access_check(s)) {
13833         return;
13834     }
13835 
13836     if (oolfn) {
13837         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13838     } else {
13839         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13840     }
13841 }
13842 
13843 /* Crypto two-reg SHA512
13844  *  31                                     12  11  10  9    5 4    0
13845  * +-----------------------------------------+--------+------+------+
13846  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13847  * +-----------------------------------------+--------+------+------+
13848  */
13849 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13850 {
13851     int opcode = extract32(insn, 10, 2);
13852     int rn = extract32(insn, 5, 5);
13853     int rd = extract32(insn, 0, 5);
13854     bool feature;
13855 
13856     switch (opcode) {
13857     case 0: /* SHA512SU0 */
13858         feature = dc_isar_feature(aa64_sha512, s);
13859         break;
13860     case 1: /* SM4E */
13861         feature = dc_isar_feature(aa64_sm4, s);
13862         break;
13863     default:
13864         unallocated_encoding(s);
13865         return;
13866     }
13867 
13868     if (!feature) {
13869         unallocated_encoding(s);
13870         return;
13871     }
13872 
13873     if (!fp_access_check(s)) {
13874         return;
13875     }
13876 
13877     switch (opcode) {
13878     case 0: /* SHA512SU0 */
13879         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13880         break;
13881     case 1: /* SM4E */
13882         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13883         break;
13884     default:
13885         g_assert_not_reached();
13886     }
13887 }
13888 
13889 /* Crypto four-register
13890  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13891  * +-------------------+-----+------+---+------+------+------+
13892  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13893  * +-------------------+-----+------+---+------+------+------+
13894  */
13895 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13896 {
13897     int op0 = extract32(insn, 21, 2);
13898     int rm = extract32(insn, 16, 5);
13899     int ra = extract32(insn, 10, 5);
13900     int rn = extract32(insn, 5, 5);
13901     int rd = extract32(insn, 0, 5);
13902     bool feature;
13903 
13904     switch (op0) {
13905     case 0: /* EOR3 */
13906     case 1: /* BCAX */
13907         feature = dc_isar_feature(aa64_sha3, s);
13908         break;
13909     case 2: /* SM3SS1 */
13910         feature = dc_isar_feature(aa64_sm3, s);
13911         break;
13912     default:
13913         unallocated_encoding(s);
13914         return;
13915     }
13916 
13917     if (!feature) {
13918         unallocated_encoding(s);
13919         return;
13920     }
13921 
13922     if (!fp_access_check(s)) {
13923         return;
13924     }
13925 
13926     if (op0 < 2) {
13927         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13928         int pass;
13929 
13930         tcg_op1 = tcg_temp_new_i64();
13931         tcg_op2 = tcg_temp_new_i64();
13932         tcg_op3 = tcg_temp_new_i64();
13933         tcg_res[0] = tcg_temp_new_i64();
13934         tcg_res[1] = tcg_temp_new_i64();
13935 
13936         for (pass = 0; pass < 2; pass++) {
13937             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13938             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13939             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13940 
13941             if (op0 == 0) {
13942                 /* EOR3 */
13943                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13944             } else {
13945                 /* BCAX */
13946                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13947             }
13948             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13949         }
13950         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13951         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13952     } else {
13953         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13954 
13955         tcg_op1 = tcg_temp_new_i32();
13956         tcg_op2 = tcg_temp_new_i32();
13957         tcg_op3 = tcg_temp_new_i32();
13958         tcg_res = tcg_temp_new_i32();
13959         tcg_zero = tcg_constant_i32(0);
13960 
13961         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13962         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13963         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13964 
13965         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13966         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13967         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13968         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13969 
13970         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13971         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13972         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13973         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13974     }
13975 }
13976 
13977 /* Crypto XAR
13978  *  31                   21 20  16 15    10 9    5 4    0
13979  * +-----------------------+------+--------+------+------+
13980  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13981  * +-----------------------+------+--------+------+------+
13982  */
13983 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13984 {
13985     int rm = extract32(insn, 16, 5);
13986     int imm6 = extract32(insn, 10, 6);
13987     int rn = extract32(insn, 5, 5);
13988     int rd = extract32(insn, 0, 5);
13989 
13990     if (!dc_isar_feature(aa64_sha3, s)) {
13991         unallocated_encoding(s);
13992         return;
13993     }
13994 
13995     if (!fp_access_check(s)) {
13996         return;
13997     }
13998 
13999     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14000                  vec_full_reg_offset(s, rn),
14001                  vec_full_reg_offset(s, rm), imm6, 16,
14002                  vec_full_reg_size(s));
14003 }
14004 
14005 /* Crypto three-reg imm2
14006  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14007  * +-----------------------+------+-----+------+--------+------+------+
14008  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14009  * +-----------------------+------+-----+------+--------+------+------+
14010  */
14011 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14012 {
14013     static gen_helper_gvec_3 * const fns[4] = {
14014         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14015         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14016     };
14017     int opcode = extract32(insn, 10, 2);
14018     int imm2 = extract32(insn, 12, 2);
14019     int rm = extract32(insn, 16, 5);
14020     int rn = extract32(insn, 5, 5);
14021     int rd = extract32(insn, 0, 5);
14022 
14023     if (!dc_isar_feature(aa64_sm3, s)) {
14024         unallocated_encoding(s);
14025         return;
14026     }
14027 
14028     if (!fp_access_check(s)) {
14029         return;
14030     }
14031 
14032     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14033 }
14034 
14035 /* C3.6 Data processing - SIMD, inc Crypto
14036  *
14037  * As the decode gets a little complex we are using a table based
14038  * approach for this part of the decode.
14039  */
14040 static const AArch64DecodeTable data_proc_simd[] = {
14041     /* pattern  ,  mask     ,  fn                        */
14042     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14043     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14044     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14045     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14046     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14047     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14048     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14049     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14050     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14051     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14052     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14053     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14054     { 0x2e000000, 0xbf208400, disas_simd_ext },
14055     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14056     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14057     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14058     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14059     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14060     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14061     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14062     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14063     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14064     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14065     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14066     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14067     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14068     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14069     { 0xce800000, 0xffe00000, disas_crypto_xar },
14070     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14071     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14072     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14073     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14074     { 0x00000000, 0x00000000, NULL }
14075 };
14076 
14077 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14078 {
14079     /* Note that this is called with all non-FP cases from
14080      * table C3-6 so it must UNDEF for entries not specifically
14081      * allocated to instructions in that table.
14082      */
14083     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14084     if (fn) {
14085         fn(s, insn);
14086     } else {
14087         unallocated_encoding(s);
14088     }
14089 }
14090 
14091 /* C3.6 Data processing - SIMD and floating point */
14092 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14093 {
14094     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14095         disas_data_proc_fp(s, insn);
14096     } else {
14097         /* SIMD, including crypto */
14098         disas_data_proc_simd(s, insn);
14099     }
14100 }
14101 
14102 static bool trans_OK(DisasContext *s, arg_OK *a)
14103 {
14104     return true;
14105 }
14106 
14107 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14108 {
14109     s->is_nonstreaming = true;
14110     return true;
14111 }
14112 
14113 /**
14114  * is_guarded_page:
14115  * @env: The cpu environment
14116  * @s: The DisasContext
14117  *
14118  * Return true if the page is guarded.
14119  */
14120 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14121 {
14122     uint64_t addr = s->base.pc_first;
14123 #ifdef CONFIG_USER_ONLY
14124     return page_get_flags(addr) & PAGE_BTI;
14125 #else
14126     CPUTLBEntryFull *full;
14127     void *host;
14128     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14129     int flags;
14130 
14131     /*
14132      * We test this immediately after reading an insn, which means
14133      * that the TLB entry must be present and valid, and thus this
14134      * access will never raise an exception.
14135      */
14136     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14137                               false, &host, &full, 0);
14138     assert(!(flags & TLB_INVALID_MASK));
14139 
14140     return full->guarded;
14141 #endif
14142 }
14143 
14144 /**
14145  * btype_destination_ok:
14146  * @insn: The instruction at the branch destination
14147  * @bt: SCTLR_ELx.BT
14148  * @btype: PSTATE.BTYPE, and is non-zero
14149  *
14150  * On a guarded page, there are a limited number of insns
14151  * that may be present at the branch target:
14152  *   - branch target identifiers,
14153  *   - paciasp, pacibsp,
14154  *   - BRK insn
14155  *   - HLT insn
14156  * Anything else causes a Branch Target Exception.
14157  *
14158  * Return true if the branch is compatible, false to raise BTITRAP.
14159  */
14160 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14161 {
14162     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14163         /* HINT space */
14164         switch (extract32(insn, 5, 7)) {
14165         case 0b011001: /* PACIASP */
14166         case 0b011011: /* PACIBSP */
14167             /*
14168              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14169              * with btype == 3.  Otherwise all btype are ok.
14170              */
14171             return !bt || btype != 3;
14172         case 0b100000: /* BTI */
14173             /* Not compatible with any btype.  */
14174             return false;
14175         case 0b100010: /* BTI c */
14176             /* Not compatible with btype == 3 */
14177             return btype != 3;
14178         case 0b100100: /* BTI j */
14179             /* Not compatible with btype == 2 */
14180             return btype != 2;
14181         case 0b100110: /* BTI jc */
14182             /* Compatible with any btype.  */
14183             return true;
14184         }
14185     } else {
14186         switch (insn & 0xffe0001fu) {
14187         case 0xd4200000u: /* BRK */
14188         case 0xd4400000u: /* HLT */
14189             /* Give priority to the breakpoint exception.  */
14190             return true;
14191         }
14192     }
14193     return false;
14194 }
14195 
14196 /* C3.1 A64 instruction index by encoding */
14197 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14198 {
14199     switch (extract32(insn, 25, 4)) {
14200     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14201         disas_b_exc_sys(s, insn);
14202         break;
14203     case 0x4:
14204     case 0x6:
14205     case 0xc:
14206     case 0xe:      /* Loads and stores */
14207         disas_ldst(s, insn);
14208         break;
14209     case 0x5:
14210     case 0xd:      /* Data processing - register */
14211         disas_data_proc_reg(s, insn);
14212         break;
14213     case 0x7:
14214     case 0xf:      /* Data processing - SIMD and floating point */
14215         disas_data_proc_simd_fp(s, insn);
14216         break;
14217     default:
14218         unallocated_encoding(s);
14219         break;
14220     }
14221 }
14222 
14223 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14224                                           CPUState *cpu)
14225 {
14226     DisasContext *dc = container_of(dcbase, DisasContext, base);
14227     CPUARMState *env = cpu->env_ptr;
14228     ARMCPU *arm_cpu = env_archcpu(env);
14229     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14230     int bound, core_mmu_idx;
14231 
14232     dc->isar = &arm_cpu->isar;
14233     dc->condjmp = 0;
14234     dc->pc_save = dc->base.pc_first;
14235     dc->aarch64 = true;
14236     dc->thumb = false;
14237     dc->sctlr_b = 0;
14238     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14239     dc->condexec_mask = 0;
14240     dc->condexec_cond = 0;
14241     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14242     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14243     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14244     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14245     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14246     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14247 #if !defined(CONFIG_USER_ONLY)
14248     dc->user = (dc->current_el == 0);
14249 #endif
14250     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14251     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14252     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14253     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14254     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14255     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14256     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14257     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14258     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14259     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14260     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14261     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14262     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14263     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14264     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14265     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14266     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14267     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14268     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14269     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14270     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14271     dc->vec_len = 0;
14272     dc->vec_stride = 0;
14273     dc->cp_regs = arm_cpu->cp_regs;
14274     dc->features = env->features;
14275     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14276 
14277 #ifdef CONFIG_USER_ONLY
14278     /* In sve_probe_page, we assume TBI is enabled. */
14279     tcg_debug_assert(dc->tbid & 1);
14280 #endif
14281 
14282     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14283 
14284     /* Single step state. The code-generation logic here is:
14285      *  SS_ACTIVE == 0:
14286      *   generate code with no special handling for single-stepping (except
14287      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14288      *   this happens anyway because those changes are all system register or
14289      *   PSTATE writes).
14290      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14291      *   emit code for one insn
14292      *   emit code to clear PSTATE.SS
14293      *   emit code to generate software step exception for completed step
14294      *   end TB (as usual for having generated an exception)
14295      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14296      *   emit code to generate a software step exception
14297      *   end the TB
14298      */
14299     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14300     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14301     dc->is_ldex = false;
14302 
14303     /* Bound the number of insns to execute to those left on the page.  */
14304     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14305 
14306     /* If architectural single step active, limit to 1.  */
14307     if (dc->ss_active) {
14308         bound = 1;
14309     }
14310     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14311 }
14312 
14313 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14314 {
14315 }
14316 
14317 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14318 {
14319     DisasContext *dc = container_of(dcbase, DisasContext, base);
14320     target_ulong pc_arg = dc->base.pc_next;
14321 
14322     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14323         pc_arg &= ~TARGET_PAGE_MASK;
14324     }
14325     tcg_gen_insn_start(pc_arg, 0, 0);
14326     dc->insn_start = tcg_last_op();
14327 }
14328 
14329 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14330 {
14331     DisasContext *s = container_of(dcbase, DisasContext, base);
14332     CPUARMState *env = cpu->env_ptr;
14333     uint64_t pc = s->base.pc_next;
14334     uint32_t insn;
14335 
14336     /* Singlestep exceptions have the highest priority. */
14337     if (s->ss_active && !s->pstate_ss) {
14338         /* Singlestep state is Active-pending.
14339          * If we're in this state at the start of a TB then either
14340          *  a) we just took an exception to an EL which is being debugged
14341          *     and this is the first insn in the exception handler
14342          *  b) debug exceptions were masked and we just unmasked them
14343          *     without changing EL (eg by clearing PSTATE.D)
14344          * In either case we're going to take a swstep exception in the
14345          * "did not step an insn" case, and so the syndrome ISV and EX
14346          * bits should be zero.
14347          */
14348         assert(s->base.num_insns == 1);
14349         gen_swstep_exception(s, 0, 0);
14350         s->base.is_jmp = DISAS_NORETURN;
14351         s->base.pc_next = pc + 4;
14352         return;
14353     }
14354 
14355     if (pc & 3) {
14356         /*
14357          * PC alignment fault.  This has priority over the instruction abort
14358          * that we would receive from a translation fault via arm_ldl_code.
14359          * This should only be possible after an indirect branch, at the
14360          * start of the TB.
14361          */
14362         assert(s->base.num_insns == 1);
14363         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14364         s->base.is_jmp = DISAS_NORETURN;
14365         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14366         return;
14367     }
14368 
14369     s->pc_curr = pc;
14370     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14371     s->insn = insn;
14372     s->base.pc_next = pc + 4;
14373 
14374     s->fp_access_checked = false;
14375     s->sve_access_checked = false;
14376 
14377     if (s->pstate_il) {
14378         /*
14379          * Illegal execution state. This has priority over BTI
14380          * exceptions, but comes after instruction abort exceptions.
14381          */
14382         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14383         return;
14384     }
14385 
14386     if (dc_isar_feature(aa64_bti, s)) {
14387         if (s->base.num_insns == 1) {
14388             /*
14389              * At the first insn of the TB, compute s->guarded_page.
14390              * We delayed computing this until successfully reading
14391              * the first insn of the TB, above.  This (mostly) ensures
14392              * that the softmmu tlb entry has been populated, and the
14393              * page table GP bit is available.
14394              *
14395              * Note that we need to compute this even if btype == 0,
14396              * because this value is used for BR instructions later
14397              * where ENV is not available.
14398              */
14399             s->guarded_page = is_guarded_page(env, s);
14400 
14401             /* First insn can have btype set to non-zero.  */
14402             tcg_debug_assert(s->btype >= 0);
14403 
14404             /*
14405              * Note that the Branch Target Exception has fairly high
14406              * priority -- below debugging exceptions but above most
14407              * everything else.  This allows us to handle this now
14408              * instead of waiting until the insn is otherwise decoded.
14409              */
14410             if (s->btype != 0
14411                 && s->guarded_page
14412                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14413                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14414                 return;
14415             }
14416         } else {
14417             /* Not the first insn: btype must be 0.  */
14418             tcg_debug_assert(s->btype == 0);
14419         }
14420     }
14421 
14422     s->is_nonstreaming = false;
14423     if (s->sme_trap_nonstreaming) {
14424         disas_sme_fa64(s, insn);
14425     }
14426 
14427     if (!disas_a64(s, insn) &&
14428         !disas_sme(s, insn) &&
14429         !disas_sve(s, insn)) {
14430         disas_a64_legacy(s, insn);
14431     }
14432 
14433     /*
14434      * After execution of most insns, btype is reset to 0.
14435      * Note that we set btype == -1 when the insn sets btype.
14436      */
14437     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14438         reset_btype(s);
14439     }
14440 }
14441 
14442 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14443 {
14444     DisasContext *dc = container_of(dcbase, DisasContext, base);
14445 
14446     if (unlikely(dc->ss_active)) {
14447         /* Note that this means single stepping WFI doesn't halt the CPU.
14448          * For conditional branch insns this is harmless unreachable code as
14449          * gen_goto_tb() has already handled emitting the debug exception
14450          * (and thus a tb-jump is not possible when singlestepping).
14451          */
14452         switch (dc->base.is_jmp) {
14453         default:
14454             gen_a64_update_pc(dc, 4);
14455             /* fall through */
14456         case DISAS_EXIT:
14457         case DISAS_JUMP:
14458             gen_step_complete_exception(dc);
14459             break;
14460         case DISAS_NORETURN:
14461             break;
14462         }
14463     } else {
14464         switch (dc->base.is_jmp) {
14465         case DISAS_NEXT:
14466         case DISAS_TOO_MANY:
14467             gen_goto_tb(dc, 1, 4);
14468             break;
14469         default:
14470         case DISAS_UPDATE_EXIT:
14471             gen_a64_update_pc(dc, 4);
14472             /* fall through */
14473         case DISAS_EXIT:
14474             tcg_gen_exit_tb(NULL, 0);
14475             break;
14476         case DISAS_UPDATE_NOCHAIN:
14477             gen_a64_update_pc(dc, 4);
14478             /* fall through */
14479         case DISAS_JUMP:
14480             tcg_gen_lookup_and_goto_ptr();
14481             break;
14482         case DISAS_NORETURN:
14483         case DISAS_SWI:
14484             break;
14485         case DISAS_WFE:
14486             gen_a64_update_pc(dc, 4);
14487             gen_helper_wfe(cpu_env);
14488             break;
14489         case DISAS_YIELD:
14490             gen_a64_update_pc(dc, 4);
14491             gen_helper_yield(cpu_env);
14492             break;
14493         case DISAS_WFI:
14494             /*
14495              * This is a special case because we don't want to just halt
14496              * the CPU if trying to debug across a WFI.
14497              */
14498             gen_a64_update_pc(dc, 4);
14499             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14500             /*
14501              * The helper doesn't necessarily throw an exception, but we
14502              * must go back to the main loop to check for interrupts anyway.
14503              */
14504             tcg_gen_exit_tb(NULL, 0);
14505             break;
14506         }
14507     }
14508 }
14509 
14510 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14511                                  CPUState *cpu, FILE *logfile)
14512 {
14513     DisasContext *dc = container_of(dcbase, DisasContext, base);
14514 
14515     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14516     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14517 }
14518 
14519 const TranslatorOps aarch64_translator_ops = {
14520     .init_disas_context = aarch64_tr_init_disas_context,
14521     .tb_start           = aarch64_tr_tb_start,
14522     .insn_start         = aarch64_tr_insn_start,
14523     .translate_insn     = aarch64_tr_translate_insn,
14524     .tb_stop            = aarch64_tr_tb_stop,
14525     .disas_log          = aarch64_tr_disas_log,
14526 };
14527