xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 68496d41)
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         memop = finalize_memop_asimd(s, size);
3313     } else {
3314         if (size == 3 && opc == 2) {
3315             /* PRFM - prefetch */
3316             return;
3317         }
3318         if (opc == 3 && size > 1) {
3319             unallocated_encoding(s);
3320             return;
3321         }
3322         is_store = (opc == 0);
3323         is_signed = !is_store && extract32(opc, 1, 1);
3324         is_extended = (size < 3) && extract32(opc, 0, 1);
3325         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3326     }
3327 
3328     if (rn == 31) {
3329         gen_check_sp_alignment(s);
3330     }
3331     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3332 
3333     tcg_rm = read_cpu_reg(s, rm, 1);
3334     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3335 
3336     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3337 
3338     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3339 
3340     if (is_vector) {
3341         if (is_store) {
3342             do_fp_st(s, rt, clean_addr, memop);
3343         } else {
3344             do_fp_ld(s, rt, clean_addr, memop);
3345         }
3346     } else {
3347         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3348         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3349 
3350         if (is_store) {
3351             do_gpr_st(s, tcg_rt, clean_addr, memop,
3352                       true, rt, iss_sf, false);
3353         } else {
3354             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3355                       is_extended, true, rt, iss_sf, false);
3356         }
3357     }
3358 }
3359 
3360 /*
3361  * Load/store (unsigned immediate)
3362  *
3363  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3364  * +----+-------+---+-----+-----+------------+-------+------+
3365  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3366  * +----+-------+---+-----+-----+------------+-------+------+
3367  *
3368  * For non-vector:
3369  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3370  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3371  * For vector:
3372  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3373  *   opc<0>: 0 -> store, 1 -> load
3374  * Rn: base address register (inc SP)
3375  * Rt: target register
3376  */
3377 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3378                                         int opc,
3379                                         int size,
3380                                         int rt,
3381                                         bool is_vector)
3382 {
3383     int rn = extract32(insn, 5, 5);
3384     unsigned int imm12 = extract32(insn, 10, 12);
3385     unsigned int offset;
3386     TCGv_i64 clean_addr, dirty_addr;
3387     bool is_store;
3388     bool is_signed = false;
3389     bool is_extended = false;
3390     MemOp memop;
3391 
3392     if (is_vector) {
3393         size |= (opc & 2) << 1;
3394         if (size > 4) {
3395             unallocated_encoding(s);
3396             return;
3397         }
3398         is_store = !extract32(opc, 0, 1);
3399         if (!fp_access_check(s)) {
3400             return;
3401         }
3402         memop = finalize_memop_asimd(s, size);
3403     } else {
3404         if (size == 3 && opc == 2) {
3405             /* PRFM - prefetch */
3406             return;
3407         }
3408         if (opc == 3 && size > 1) {
3409             unallocated_encoding(s);
3410             return;
3411         }
3412         is_store = (opc == 0);
3413         is_signed = !is_store && extract32(opc, 1, 1);
3414         is_extended = (size < 3) && extract32(opc, 0, 1);
3415         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3416     }
3417 
3418     if (rn == 31) {
3419         gen_check_sp_alignment(s);
3420     }
3421     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3422     offset = imm12 << size;
3423     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3424 
3425     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3426 
3427     if (is_vector) {
3428         if (is_store) {
3429             do_fp_st(s, rt, clean_addr, memop);
3430         } else {
3431             do_fp_ld(s, rt, clean_addr, memop);
3432         }
3433     } else {
3434         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3435         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3436         if (is_store) {
3437             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3438         } else {
3439             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3440                       is_extended, true, rt, iss_sf, false);
3441         }
3442     }
3443 }
3444 
3445 /* Atomic memory operations
3446  *
3447  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3448  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3449  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3450  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3451  *
3452  * Rt: the result register
3453  * Rn: base address or SP
3454  * Rs: the source register for the operation
3455  * V: vector flag (always 0 as of v8.3)
3456  * A: acquire flag
3457  * R: release flag
3458  */
3459 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3460                               int size, int rt, bool is_vector)
3461 {
3462     int rs = extract32(insn, 16, 5);
3463     int rn = extract32(insn, 5, 5);
3464     int o3_opc = extract32(insn, 12, 4);
3465     bool r = extract32(insn, 22, 1);
3466     bool a = extract32(insn, 23, 1);
3467     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3468     AtomicThreeOpFn *fn = NULL;
3469     MemOp mop = size;
3470 
3471     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3472         unallocated_encoding(s);
3473         return;
3474     }
3475     switch (o3_opc) {
3476     case 000: /* LDADD */
3477         fn = tcg_gen_atomic_fetch_add_i64;
3478         break;
3479     case 001: /* LDCLR */
3480         fn = tcg_gen_atomic_fetch_and_i64;
3481         break;
3482     case 002: /* LDEOR */
3483         fn = tcg_gen_atomic_fetch_xor_i64;
3484         break;
3485     case 003: /* LDSET */
3486         fn = tcg_gen_atomic_fetch_or_i64;
3487         break;
3488     case 004: /* LDSMAX */
3489         fn = tcg_gen_atomic_fetch_smax_i64;
3490         mop |= MO_SIGN;
3491         break;
3492     case 005: /* LDSMIN */
3493         fn = tcg_gen_atomic_fetch_smin_i64;
3494         mop |= MO_SIGN;
3495         break;
3496     case 006: /* LDUMAX */
3497         fn = tcg_gen_atomic_fetch_umax_i64;
3498         break;
3499     case 007: /* LDUMIN */
3500         fn = tcg_gen_atomic_fetch_umin_i64;
3501         break;
3502     case 010: /* SWP */
3503         fn = tcg_gen_atomic_xchg_i64;
3504         break;
3505     case 014: /* LDAPR, LDAPRH, LDAPRB */
3506         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3507             rs != 31 || a != 1 || r != 0) {
3508             unallocated_encoding(s);
3509             return;
3510         }
3511         break;
3512     default:
3513         unallocated_encoding(s);
3514         return;
3515     }
3516 
3517     if (rn == 31) {
3518         gen_check_sp_alignment(s);
3519     }
3520 
3521     mop = check_atomic_align(s, rn, mop);
3522     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3523 
3524     if (o3_opc == 014) {
3525         /*
3526          * LDAPR* are a special case because they are a simple load, not a
3527          * fetch-and-do-something op.
3528          * The architectural consistency requirements here are weaker than
3529          * full load-acquire (we only need "load-acquire processor consistent"),
3530          * but we choose to implement them as full LDAQ.
3531          */
3532         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3533                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3534         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3535         return;
3536     }
3537 
3538     tcg_rs = read_cpu_reg(s, rs, true);
3539     tcg_rt = cpu_reg(s, rt);
3540 
3541     if (o3_opc == 1) { /* LDCLR */
3542         tcg_gen_not_i64(tcg_rs, tcg_rs);
3543     }
3544 
3545     /* The tcg atomic primitives are all full barriers.  Therefore we
3546      * can ignore the Acquire and Release bits of this instruction.
3547      */
3548     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3549 
3550     if (mop & MO_SIGN) {
3551         switch (size) {
3552         case MO_8:
3553             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3554             break;
3555         case MO_16:
3556             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3557             break;
3558         case MO_32:
3559             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3560             break;
3561         case MO_64:
3562             break;
3563         default:
3564             g_assert_not_reached();
3565         }
3566     }
3567 }
3568 
3569 /*
3570  * PAC memory operations
3571  *
3572  *  31  30      27  26    24    22  21       12  11  10    5     0
3573  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3574  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3575  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3576  *
3577  * Rt: the result register
3578  * Rn: base address or SP
3579  * V: vector flag (always 0 as of v8.3)
3580  * M: clear for key DA, set for key DB
3581  * W: pre-indexing flag
3582  * S: sign for imm9.
3583  */
3584 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3585                            int size, int rt, bool is_vector)
3586 {
3587     int rn = extract32(insn, 5, 5);
3588     bool is_wback = extract32(insn, 11, 1);
3589     bool use_key_a = !extract32(insn, 23, 1);
3590     int offset;
3591     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3592     MemOp memop;
3593 
3594     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3595         unallocated_encoding(s);
3596         return;
3597     }
3598 
3599     if (rn == 31) {
3600         gen_check_sp_alignment(s);
3601     }
3602     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3603 
3604     if (s->pauth_active) {
3605         if (use_key_a) {
3606             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3607                              tcg_constant_i64(0));
3608         } else {
3609             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3610                              tcg_constant_i64(0));
3611         }
3612     }
3613 
3614     /* Form the 10-bit signed, scaled offset.  */
3615     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3616     offset = sextract32(offset << size, 0, 10 + size);
3617     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3618 
3619     memop = finalize_memop(s, size);
3620 
3621     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3622     clean_addr = gen_mte_check1(s, dirty_addr, false,
3623                                 is_wback || rn != 31, memop);
3624 
3625     tcg_rt = cpu_reg(s, rt);
3626     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3627               /* extend */ false, /* iss_valid */ !is_wback,
3628               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3629 
3630     if (is_wback) {
3631         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3632     }
3633 }
3634 
3635 /*
3636  * LDAPR/STLR (unscaled immediate)
3637  *
3638  *  31  30            24    22  21       12    10    5     0
3639  * +------+-------------+-----+---+--------+-----+----+-----+
3640  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3641  * +------+-------------+-----+---+--------+-----+----+-----+
3642  *
3643  * Rt: source or destination register
3644  * Rn: base register
3645  * imm9: unscaled immediate offset
3646  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3647  * size: size of load/store
3648  */
3649 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3650 {
3651     int rt = extract32(insn, 0, 5);
3652     int rn = extract32(insn, 5, 5);
3653     int offset = sextract32(insn, 12, 9);
3654     int opc = extract32(insn, 22, 2);
3655     int size = extract32(insn, 30, 2);
3656     TCGv_i64 clean_addr, dirty_addr;
3657     bool is_store = false;
3658     bool extend = false;
3659     bool iss_sf;
3660     MemOp mop = size;
3661 
3662     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3663         unallocated_encoding(s);
3664         return;
3665     }
3666 
3667     switch (opc) {
3668     case 0: /* STLURB */
3669         is_store = true;
3670         break;
3671     case 1: /* LDAPUR* */
3672         break;
3673     case 2: /* LDAPURS* 64-bit variant */
3674         if (size == 3) {
3675             unallocated_encoding(s);
3676             return;
3677         }
3678         mop |= MO_SIGN;
3679         break;
3680     case 3: /* LDAPURS* 32-bit variant */
3681         if (size > 1) {
3682             unallocated_encoding(s);
3683             return;
3684         }
3685         mop |= MO_SIGN;
3686         extend = true; /* zero-extend 32->64 after signed load */
3687         break;
3688     default:
3689         g_assert_not_reached();
3690     }
3691 
3692     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3693 
3694     if (rn == 31) {
3695         gen_check_sp_alignment(s);
3696     }
3697 
3698     mop = check_ordered_align(s, rn, offset, is_store, mop);
3699 
3700     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3701     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3702     clean_addr = clean_data_tbi(s, dirty_addr);
3703 
3704     if (is_store) {
3705         /* Store-Release semantics */
3706         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3707         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3708     } else {
3709         /*
3710          * Load-AcquirePC semantics; we implement as the slightly more
3711          * restrictive Load-Acquire.
3712          */
3713         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3714                   extend, true, rt, iss_sf, true);
3715         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3716     }
3717 }
3718 
3719 /* Load/store register (all forms) */
3720 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3721 {
3722     int rt = extract32(insn, 0, 5);
3723     int opc = extract32(insn, 22, 2);
3724     bool is_vector = extract32(insn, 26, 1);
3725     int size = extract32(insn, 30, 2);
3726 
3727     switch (extract32(insn, 24, 2)) {
3728     case 0:
3729         if (extract32(insn, 21, 1) == 0) {
3730             /* Load/store register (unscaled immediate)
3731              * Load/store immediate pre/post-indexed
3732              * Load/store register unprivileged
3733              */
3734             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3735             return;
3736         }
3737         switch (extract32(insn, 10, 2)) {
3738         case 0:
3739             disas_ldst_atomic(s, insn, size, rt, is_vector);
3740             return;
3741         case 2:
3742             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3743             return;
3744         default:
3745             disas_ldst_pac(s, insn, size, rt, is_vector);
3746             return;
3747         }
3748         break;
3749     case 1:
3750         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3751         return;
3752     }
3753     unallocated_encoding(s);
3754 }
3755 
3756 /* AdvSIMD load/store multiple structures
3757  *
3758  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3759  * +---+---+---------------+---+-------------+--------+------+------+------+
3760  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3761  * +---+---+---------------+---+-------------+--------+------+------+------+
3762  *
3763  * AdvSIMD load/store multiple structures (post-indexed)
3764  *
3765  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3766  * +---+---+---------------+---+---+---------+--------+------+------+------+
3767  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3768  * +---+---+---------------+---+---+---------+--------+------+------+------+
3769  *
3770  * Rt: first (or only) SIMD&FP register to be transferred
3771  * Rn: base address or SP
3772  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3773  */
3774 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3775 {
3776     int rt = extract32(insn, 0, 5);
3777     int rn = extract32(insn, 5, 5);
3778     int rm = extract32(insn, 16, 5);
3779     int size = extract32(insn, 10, 2);
3780     int opcode = extract32(insn, 12, 4);
3781     bool is_store = !extract32(insn, 22, 1);
3782     bool is_postidx = extract32(insn, 23, 1);
3783     bool is_q = extract32(insn, 30, 1);
3784     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3785     MemOp endian, align, mop;
3786 
3787     int total;    /* total bytes */
3788     int elements; /* elements per vector */
3789     int rpt;    /* num iterations */
3790     int selem;  /* structure elements */
3791     int r;
3792 
3793     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3794         unallocated_encoding(s);
3795         return;
3796     }
3797 
3798     if (!is_postidx && rm != 0) {
3799         unallocated_encoding(s);
3800         return;
3801     }
3802 
3803     /* From the shared decode logic */
3804     switch (opcode) {
3805     case 0x0:
3806         rpt = 1;
3807         selem = 4;
3808         break;
3809     case 0x2:
3810         rpt = 4;
3811         selem = 1;
3812         break;
3813     case 0x4:
3814         rpt = 1;
3815         selem = 3;
3816         break;
3817     case 0x6:
3818         rpt = 3;
3819         selem = 1;
3820         break;
3821     case 0x7:
3822         rpt = 1;
3823         selem = 1;
3824         break;
3825     case 0x8:
3826         rpt = 1;
3827         selem = 2;
3828         break;
3829     case 0xa:
3830         rpt = 2;
3831         selem = 1;
3832         break;
3833     default:
3834         unallocated_encoding(s);
3835         return;
3836     }
3837 
3838     if (size == 3 && !is_q && selem != 1) {
3839         /* reserved */
3840         unallocated_encoding(s);
3841         return;
3842     }
3843 
3844     if (!fp_access_check(s)) {
3845         return;
3846     }
3847 
3848     if (rn == 31) {
3849         gen_check_sp_alignment(s);
3850     }
3851 
3852     /* For our purposes, bytes are always little-endian.  */
3853     endian = s->be_data;
3854     if (size == 0) {
3855         endian = MO_LE;
3856     }
3857 
3858     total = rpt * selem * (is_q ? 16 : 8);
3859     tcg_rn = cpu_reg_sp(s, rn);
3860 
3861     /*
3862      * Issue the MTE check vs the logical repeat count, before we
3863      * promote consecutive little-endian elements below.
3864      */
3865     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3866                                 total, finalize_memop_asimd(s, size));
3867 
3868     /*
3869      * Consecutive little-endian elements from a single register
3870      * can be promoted to a larger little-endian operation.
3871      */
3872     align = MO_ALIGN;
3873     if (selem == 1 && endian == MO_LE) {
3874         align = pow2_align(size);
3875         size = 3;
3876     }
3877     if (!s->align_mem) {
3878         align = 0;
3879     }
3880     mop = endian | size | align;
3881 
3882     elements = (is_q ? 16 : 8) >> size;
3883     tcg_ebytes = tcg_constant_i64(1 << size);
3884     for (r = 0; r < rpt; r++) {
3885         int e;
3886         for (e = 0; e < elements; e++) {
3887             int xs;
3888             for (xs = 0; xs < selem; xs++) {
3889                 int tt = (rt + r + xs) % 32;
3890                 if (is_store) {
3891                     do_vec_st(s, tt, e, clean_addr, mop);
3892                 } else {
3893                     do_vec_ld(s, tt, e, clean_addr, mop);
3894                 }
3895                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3896             }
3897         }
3898     }
3899 
3900     if (!is_store) {
3901         /* For non-quad operations, setting a slice of the low
3902          * 64 bits of the register clears the high 64 bits (in
3903          * the ARM ARM pseudocode this is implicit in the fact
3904          * that 'rval' is a 64 bit wide variable).
3905          * For quad operations, we might still need to zero the
3906          * high bits of SVE.
3907          */
3908         for (r = 0; r < rpt * selem; r++) {
3909             int tt = (rt + r) % 32;
3910             clear_vec_high(s, is_q, tt);
3911         }
3912     }
3913 
3914     if (is_postidx) {
3915         if (rm == 31) {
3916             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3917         } else {
3918             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3919         }
3920     }
3921 }
3922 
3923 /* AdvSIMD load/store single structure
3924  *
3925  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3926  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3927  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3928  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3929  *
3930  * AdvSIMD load/store single structure (post-indexed)
3931  *
3932  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3933  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3934  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3935  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3936  *
3937  * Rt: first (or only) SIMD&FP register to be transferred
3938  * Rn: base address or SP
3939  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3940  * index = encoded in Q:S:size dependent on size
3941  *
3942  * lane_size = encoded in R, opc
3943  * transfer width = encoded in opc, S, size
3944  */
3945 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3946 {
3947     int rt = extract32(insn, 0, 5);
3948     int rn = extract32(insn, 5, 5);
3949     int rm = extract32(insn, 16, 5);
3950     int size = extract32(insn, 10, 2);
3951     int S = extract32(insn, 12, 1);
3952     int opc = extract32(insn, 13, 3);
3953     int R = extract32(insn, 21, 1);
3954     int is_load = extract32(insn, 22, 1);
3955     int is_postidx = extract32(insn, 23, 1);
3956     int is_q = extract32(insn, 30, 1);
3957 
3958     int scale = extract32(opc, 1, 2);
3959     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3960     bool replicate = false;
3961     int index = is_q << 3 | S << 2 | size;
3962     int xs, total;
3963     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3964     MemOp mop;
3965 
3966     if (extract32(insn, 31, 1)) {
3967         unallocated_encoding(s);
3968         return;
3969     }
3970     if (!is_postidx && rm != 0) {
3971         unallocated_encoding(s);
3972         return;
3973     }
3974 
3975     switch (scale) {
3976     case 3:
3977         if (!is_load || S) {
3978             unallocated_encoding(s);
3979             return;
3980         }
3981         scale = size;
3982         replicate = true;
3983         break;
3984     case 0:
3985         break;
3986     case 1:
3987         if (extract32(size, 0, 1)) {
3988             unallocated_encoding(s);
3989             return;
3990         }
3991         index >>= 1;
3992         break;
3993     case 2:
3994         if (extract32(size, 1, 1)) {
3995             unallocated_encoding(s);
3996             return;
3997         }
3998         if (!extract32(size, 0, 1)) {
3999             index >>= 2;
4000         } else {
4001             if (S) {
4002                 unallocated_encoding(s);
4003                 return;
4004             }
4005             index >>= 3;
4006             scale = 3;
4007         }
4008         break;
4009     default:
4010         g_assert_not_reached();
4011     }
4012 
4013     if (!fp_access_check(s)) {
4014         return;
4015     }
4016 
4017     if (rn == 31) {
4018         gen_check_sp_alignment(s);
4019     }
4020 
4021     total = selem << scale;
4022     tcg_rn = cpu_reg_sp(s, rn);
4023 
4024     mop = finalize_memop_asimd(s, scale);
4025 
4026     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4027                                 total, mop);
4028 
4029     tcg_ebytes = tcg_constant_i64(1 << scale);
4030     for (xs = 0; xs < selem; xs++) {
4031         if (replicate) {
4032             /* Load and replicate to all elements */
4033             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4034 
4035             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4036             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4037                                  (is_q + 1) * 8, vec_full_reg_size(s),
4038                                  tcg_tmp);
4039         } else {
4040             /* Load/store one element per register */
4041             if (is_load) {
4042                 do_vec_ld(s, rt, index, clean_addr, mop);
4043             } else {
4044                 do_vec_st(s, rt, index, clean_addr, mop);
4045             }
4046         }
4047         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4048         rt = (rt + 1) % 32;
4049     }
4050 
4051     if (is_postidx) {
4052         if (rm == 31) {
4053             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4054         } else {
4055             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4056         }
4057     }
4058 }
4059 
4060 /*
4061  * Load/Store memory tags
4062  *
4063  *  31 30 29         24     22  21     12    10      5      0
4064  * +-----+-------------+-----+---+------+-----+------+------+
4065  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4066  * +-----+-------------+-----+---+------+-----+------+------+
4067  */
4068 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4069 {
4070     int rt = extract32(insn, 0, 5);
4071     int rn = extract32(insn, 5, 5);
4072     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4073     int op2 = extract32(insn, 10, 2);
4074     int op1 = extract32(insn, 22, 2);
4075     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4076     int index = 0;
4077     TCGv_i64 addr, clean_addr, tcg_rt;
4078 
4079     /* We checked insn bits [29:24,21] in the caller.  */
4080     if (extract32(insn, 30, 2) != 3) {
4081         goto do_unallocated;
4082     }
4083 
4084     /*
4085      * @index is a tri-state variable which has 3 states:
4086      * < 0 : post-index, writeback
4087      * = 0 : signed offset
4088      * > 0 : pre-index, writeback
4089      */
4090     switch (op1) {
4091     case 0:
4092         if (op2 != 0) {
4093             /* STG */
4094             index = op2 - 2;
4095         } else {
4096             /* STZGM */
4097             if (s->current_el == 0 || offset != 0) {
4098                 goto do_unallocated;
4099             }
4100             is_mult = is_zero = true;
4101         }
4102         break;
4103     case 1:
4104         if (op2 != 0) {
4105             /* STZG */
4106             is_zero = true;
4107             index = op2 - 2;
4108         } else {
4109             /* LDG */
4110             is_load = true;
4111         }
4112         break;
4113     case 2:
4114         if (op2 != 0) {
4115             /* ST2G */
4116             is_pair = true;
4117             index = op2 - 2;
4118         } else {
4119             /* STGM */
4120             if (s->current_el == 0 || offset != 0) {
4121                 goto do_unallocated;
4122             }
4123             is_mult = true;
4124         }
4125         break;
4126     case 3:
4127         if (op2 != 0) {
4128             /* STZ2G */
4129             is_pair = is_zero = true;
4130             index = op2 - 2;
4131         } else {
4132             /* LDGM */
4133             if (s->current_el == 0 || offset != 0) {
4134                 goto do_unallocated;
4135             }
4136             is_mult = is_load = true;
4137         }
4138         break;
4139 
4140     default:
4141     do_unallocated:
4142         unallocated_encoding(s);
4143         return;
4144     }
4145 
4146     if (is_mult
4147         ? !dc_isar_feature(aa64_mte, s)
4148         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4149         goto do_unallocated;
4150     }
4151 
4152     if (rn == 31) {
4153         gen_check_sp_alignment(s);
4154     }
4155 
4156     addr = read_cpu_reg_sp(s, rn, true);
4157     if (index >= 0) {
4158         /* pre-index or signed offset */
4159         tcg_gen_addi_i64(addr, addr, offset);
4160     }
4161 
4162     if (is_mult) {
4163         tcg_rt = cpu_reg(s, rt);
4164 
4165         if (is_zero) {
4166             int size = 4 << s->dcz_blocksize;
4167 
4168             if (s->ata) {
4169                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4170             }
4171             /*
4172              * The non-tags portion of STZGM is mostly like DC_ZVA,
4173              * except the alignment happens before the access.
4174              */
4175             clean_addr = clean_data_tbi(s, addr);
4176             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4177             gen_helper_dc_zva(cpu_env, clean_addr);
4178         } else if (s->ata) {
4179             if (is_load) {
4180                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4181             } else {
4182                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4183             }
4184         } else {
4185             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4186             int size = 4 << GMID_EL1_BS;
4187 
4188             clean_addr = clean_data_tbi(s, addr);
4189             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4190             gen_probe_access(s, clean_addr, acc, size);
4191 
4192             if (is_load) {
4193                 /* The result tags are zeros.  */
4194                 tcg_gen_movi_i64(tcg_rt, 0);
4195             }
4196         }
4197         return;
4198     }
4199 
4200     if (is_load) {
4201         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4202         tcg_rt = cpu_reg(s, rt);
4203         if (s->ata) {
4204             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4205         } else {
4206             /*
4207              * Tag access disabled: we must check for aborts on the load
4208              * load from [rn+offset], and then insert a 0 tag into rt.
4209              */
4210             clean_addr = clean_data_tbi(s, addr);
4211             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4212             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4213         }
4214     } else {
4215         tcg_rt = cpu_reg_sp(s, rt);
4216         if (!s->ata) {
4217             /*
4218              * For STG and ST2G, we need to check alignment and probe memory.
4219              * TODO: For STZG and STZ2G, we could rely on the stores below,
4220              * at least for system mode; user-only won't enforce alignment.
4221              */
4222             if (is_pair) {
4223                 gen_helper_st2g_stub(cpu_env, addr);
4224             } else {
4225                 gen_helper_stg_stub(cpu_env, addr);
4226             }
4227         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4228             if (is_pair) {
4229                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4230             } else {
4231                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4232             }
4233         } else {
4234             if (is_pair) {
4235                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4236             } else {
4237                 gen_helper_stg(cpu_env, addr, tcg_rt);
4238             }
4239         }
4240     }
4241 
4242     if (is_zero) {
4243         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4244         TCGv_i64 zero64 = tcg_constant_i64(0);
4245         TCGv_i128 zero128 = tcg_temp_new_i128();
4246         int mem_index = get_mem_index(s);
4247         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4248 
4249         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4250 
4251         /* This is 1 or 2 atomic 16-byte operations. */
4252         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4253         if (is_pair) {
4254             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4255             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4256         }
4257     }
4258 
4259     if (index != 0) {
4260         /* pre-index or post-index */
4261         if (index < 0) {
4262             /* post-index */
4263             tcg_gen_addi_i64(addr, addr, offset);
4264         }
4265         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4266     }
4267 }
4268 
4269 /* Loads and stores */
4270 static void disas_ldst(DisasContext *s, uint32_t insn)
4271 {
4272     switch (extract32(insn, 24, 6)) {
4273     case 0x08: /* Load/store exclusive */
4274         disas_ldst_excl(s, insn);
4275         break;
4276     case 0x18: case 0x1c: /* Load register (literal) */
4277         disas_ld_lit(s, insn);
4278         break;
4279     case 0x28: case 0x29:
4280     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4281         disas_ldst_pair(s, insn);
4282         break;
4283     case 0x38: case 0x39:
4284     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4285         disas_ldst_reg(s, insn);
4286         break;
4287     case 0x0c: /* AdvSIMD load/store multiple structures */
4288         disas_ldst_multiple_struct(s, insn);
4289         break;
4290     case 0x0d: /* AdvSIMD load/store single structure */
4291         disas_ldst_single_struct(s, insn);
4292         break;
4293     case 0x19:
4294         if (extract32(insn, 21, 1) != 0) {
4295             disas_ldst_tag(s, insn);
4296         } else if (extract32(insn, 10, 2) == 0) {
4297             disas_ldst_ldapr_stlr(s, insn);
4298         } else {
4299             unallocated_encoding(s);
4300         }
4301         break;
4302     default:
4303         unallocated_encoding(s);
4304         break;
4305     }
4306 }
4307 
4308 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4309 
4310 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4311                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4312 {
4313     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4314     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4315     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4316 
4317     fn(tcg_rd, tcg_rn, tcg_imm);
4318     if (!a->sf) {
4319         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4320     }
4321     return true;
4322 }
4323 
4324 /*
4325  * PC-rel. addressing
4326  */
4327 
4328 static bool trans_ADR(DisasContext *s, arg_ri *a)
4329 {
4330     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4331     return true;
4332 }
4333 
4334 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4335 {
4336     int64_t offset = (int64_t)a->imm << 12;
4337 
4338     /* The page offset is ok for CF_PCREL. */
4339     offset -= s->pc_curr & 0xfff;
4340     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4341     return true;
4342 }
4343 
4344 /*
4345  * Add/subtract (immediate)
4346  */
4347 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4348 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4349 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4350 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4351 
4352 /*
4353  * Add/subtract (immediate, with tags)
4354  */
4355 
4356 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4357                                       bool sub_op)
4358 {
4359     TCGv_i64 tcg_rn, tcg_rd;
4360     int imm;
4361 
4362     imm = a->uimm6 << LOG2_TAG_GRANULE;
4363     if (sub_op) {
4364         imm = -imm;
4365     }
4366 
4367     tcg_rn = cpu_reg_sp(s, a->rn);
4368     tcg_rd = cpu_reg_sp(s, a->rd);
4369 
4370     if (s->ata) {
4371         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4372                            tcg_constant_i32(imm),
4373                            tcg_constant_i32(a->uimm4));
4374     } else {
4375         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4376         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4377     }
4378     return true;
4379 }
4380 
4381 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4382 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4383 
4384 /* The input should be a value in the bottom e bits (with higher
4385  * bits zero); returns that value replicated into every element
4386  * of size e in a 64 bit integer.
4387  */
4388 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4389 {
4390     assert(e != 0);
4391     while (e < 64) {
4392         mask |= mask << e;
4393         e *= 2;
4394     }
4395     return mask;
4396 }
4397 
4398 /*
4399  * Logical (immediate)
4400  */
4401 
4402 /*
4403  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4404  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4405  * value (ie should cause a guest UNDEF exception), and true if they are
4406  * valid, in which case the decoded bit pattern is written to result.
4407  */
4408 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4409                             unsigned int imms, unsigned int immr)
4410 {
4411     uint64_t mask;
4412     unsigned e, levels, s, r;
4413     int len;
4414 
4415     assert(immn < 2 && imms < 64 && immr < 64);
4416 
4417     /* The bit patterns we create here are 64 bit patterns which
4418      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4419      * 64 bits each. Each element contains the same value: a run
4420      * of between 1 and e-1 non-zero bits, rotated within the
4421      * element by between 0 and e-1 bits.
4422      *
4423      * The element size and run length are encoded into immn (1 bit)
4424      * and imms (6 bits) as follows:
4425      * 64 bit elements: immn = 1, imms = <length of run - 1>
4426      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4427      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4428      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4429      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4430      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4431      * Notice that immn = 0, imms = 11111x is the only combination
4432      * not covered by one of the above options; this is reserved.
4433      * Further, <length of run - 1> all-ones is a reserved pattern.
4434      *
4435      * In all cases the rotation is by immr % e (and immr is 6 bits).
4436      */
4437 
4438     /* First determine the element size */
4439     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4440     if (len < 1) {
4441         /* This is the immn == 0, imms == 0x11111x case */
4442         return false;
4443     }
4444     e = 1 << len;
4445 
4446     levels = e - 1;
4447     s = imms & levels;
4448     r = immr & levels;
4449 
4450     if (s == levels) {
4451         /* <length of run - 1> mustn't be all-ones. */
4452         return false;
4453     }
4454 
4455     /* Create the value of one element: s+1 set bits rotated
4456      * by r within the element (which is e bits wide)...
4457      */
4458     mask = MAKE_64BIT_MASK(0, s + 1);
4459     if (r) {
4460         mask = (mask >> r) | (mask << (e - r));
4461         mask &= MAKE_64BIT_MASK(0, e);
4462     }
4463     /* ...then replicate the element over the whole 64 bit value */
4464     mask = bitfield_replicate(mask, e);
4465     *result = mask;
4466     return true;
4467 }
4468 
4469 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4470                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4471 {
4472     TCGv_i64 tcg_rd, tcg_rn;
4473     uint64_t imm;
4474 
4475     /* Some immediate field values are reserved. */
4476     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4477                                 extract32(a->dbm, 0, 6),
4478                                 extract32(a->dbm, 6, 6))) {
4479         return false;
4480     }
4481     if (!a->sf) {
4482         imm &= 0xffffffffull;
4483     }
4484 
4485     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4486     tcg_rn = cpu_reg(s, a->rn);
4487 
4488     fn(tcg_rd, tcg_rn, imm);
4489     if (set_cc) {
4490         gen_logic_CC(a->sf, tcg_rd);
4491     }
4492     if (!a->sf) {
4493         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4494     }
4495     return true;
4496 }
4497 
4498 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4499 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4500 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4501 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4502 
4503 /*
4504  * Move wide (immediate)
4505  */
4506 
4507 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4508 {
4509     int pos = a->hw << 4;
4510     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4511     return true;
4512 }
4513 
4514 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4515 {
4516     int pos = a->hw << 4;
4517     uint64_t imm = a->imm;
4518 
4519     imm = ~(imm << pos);
4520     if (!a->sf) {
4521         imm = (uint32_t)imm;
4522     }
4523     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4524     return true;
4525 }
4526 
4527 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4528 {
4529     int pos = a->hw << 4;
4530     TCGv_i64 tcg_rd, tcg_im;
4531 
4532     tcg_rd = cpu_reg(s, a->rd);
4533     tcg_im = tcg_constant_i64(a->imm);
4534     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4535     if (!a->sf) {
4536         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4537     }
4538     return true;
4539 }
4540 
4541 /*
4542  * Bitfield
4543  */
4544 
4545 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4546 {
4547     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4548     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4549     unsigned int bitsize = a->sf ? 64 : 32;
4550     unsigned int ri = a->immr;
4551     unsigned int si = a->imms;
4552     unsigned int pos, len;
4553 
4554     if (si >= ri) {
4555         /* Wd<s-r:0> = Wn<s:r> */
4556         len = (si - ri) + 1;
4557         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4558         if (!a->sf) {
4559             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4560         }
4561     } else {
4562         /* Wd<32+s-r,32-r> = Wn<s:0> */
4563         len = si + 1;
4564         pos = (bitsize - ri) & (bitsize - 1);
4565 
4566         if (len < ri) {
4567             /*
4568              * Sign extend the destination field from len to fill the
4569              * balance of the word.  Let the deposit below insert all
4570              * of those sign bits.
4571              */
4572             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4573             len = ri;
4574         }
4575 
4576         /*
4577          * We start with zero, and we haven't modified any bits outside
4578          * bitsize, therefore no final zero-extension is unneeded for !sf.
4579          */
4580         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4581     }
4582     return true;
4583 }
4584 
4585 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4586 {
4587     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4588     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4589     unsigned int bitsize = a->sf ? 64 : 32;
4590     unsigned int ri = a->immr;
4591     unsigned int si = a->imms;
4592     unsigned int pos, len;
4593 
4594     tcg_rd = cpu_reg(s, a->rd);
4595     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4596 
4597     if (si >= ri) {
4598         /* Wd<s-r:0> = Wn<s:r> */
4599         len = (si - ri) + 1;
4600         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4601     } else {
4602         /* Wd<32+s-r,32-r> = Wn<s:0> */
4603         len = si + 1;
4604         pos = (bitsize - ri) & (bitsize - 1);
4605         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4606     }
4607     return true;
4608 }
4609 
4610 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4611 {
4612     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4613     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4614     unsigned int bitsize = a->sf ? 64 : 32;
4615     unsigned int ri = a->immr;
4616     unsigned int si = a->imms;
4617     unsigned int pos, len;
4618 
4619     tcg_rd = cpu_reg(s, a->rd);
4620     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4621 
4622     if (si >= ri) {
4623         /* Wd<s-r:0> = Wn<s:r> */
4624         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4625         len = (si - ri) + 1;
4626         pos = 0;
4627     } else {
4628         /* Wd<32+s-r,32-r> = Wn<s:0> */
4629         len = si + 1;
4630         pos = (bitsize - ri) & (bitsize - 1);
4631     }
4632 
4633     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4634     if (!a->sf) {
4635         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4636     }
4637     return true;
4638 }
4639 
4640 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4641 {
4642     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4643 
4644     tcg_rd = cpu_reg(s, a->rd);
4645 
4646     if (unlikely(a->imm == 0)) {
4647         /*
4648          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4649          * so an extract from bit 0 is a special case.
4650          */
4651         if (a->sf) {
4652             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4653         } else {
4654             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4655         }
4656     } else {
4657         tcg_rm = cpu_reg(s, a->rm);
4658         tcg_rn = cpu_reg(s, a->rn);
4659 
4660         if (a->sf) {
4661             /* Specialization to ROR happens in EXTRACT2.  */
4662             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4663         } else {
4664             TCGv_i32 t0 = tcg_temp_new_i32();
4665 
4666             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4667             if (a->rm == a->rn) {
4668                 tcg_gen_rotri_i32(t0, t0, a->imm);
4669             } else {
4670                 TCGv_i32 t1 = tcg_temp_new_i32();
4671                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4672                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4673             }
4674             tcg_gen_extu_i32_i64(tcg_rd, t0);
4675         }
4676     }
4677     return true;
4678 }
4679 
4680 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4681  * Note that it is the caller's responsibility to ensure that the
4682  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4683  * mandated semantics for out of range shifts.
4684  */
4685 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4686                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4687 {
4688     switch (shift_type) {
4689     case A64_SHIFT_TYPE_LSL:
4690         tcg_gen_shl_i64(dst, src, shift_amount);
4691         break;
4692     case A64_SHIFT_TYPE_LSR:
4693         tcg_gen_shr_i64(dst, src, shift_amount);
4694         break;
4695     case A64_SHIFT_TYPE_ASR:
4696         if (!sf) {
4697             tcg_gen_ext32s_i64(dst, src);
4698         }
4699         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4700         break;
4701     case A64_SHIFT_TYPE_ROR:
4702         if (sf) {
4703             tcg_gen_rotr_i64(dst, src, shift_amount);
4704         } else {
4705             TCGv_i32 t0, t1;
4706             t0 = tcg_temp_new_i32();
4707             t1 = tcg_temp_new_i32();
4708             tcg_gen_extrl_i64_i32(t0, src);
4709             tcg_gen_extrl_i64_i32(t1, shift_amount);
4710             tcg_gen_rotr_i32(t0, t0, t1);
4711             tcg_gen_extu_i32_i64(dst, t0);
4712         }
4713         break;
4714     default:
4715         assert(FALSE); /* all shift types should be handled */
4716         break;
4717     }
4718 
4719     if (!sf) { /* zero extend final result */
4720         tcg_gen_ext32u_i64(dst, dst);
4721     }
4722 }
4723 
4724 /* Shift a TCGv src by immediate, put result in dst.
4725  * The shift amount must be in range (this should always be true as the
4726  * relevant instructions will UNDEF on bad shift immediates).
4727  */
4728 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4729                           enum a64_shift_type shift_type, unsigned int shift_i)
4730 {
4731     assert(shift_i < (sf ? 64 : 32));
4732 
4733     if (shift_i == 0) {
4734         tcg_gen_mov_i64(dst, src);
4735     } else {
4736         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4737     }
4738 }
4739 
4740 /* Logical (shifted register)
4741  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4742  * +----+-----+-----------+-------+---+------+--------+------+------+
4743  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4744  * +----+-----+-----------+-------+---+------+--------+------+------+
4745  */
4746 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4747 {
4748     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4749     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4750 
4751     sf = extract32(insn, 31, 1);
4752     opc = extract32(insn, 29, 2);
4753     shift_type = extract32(insn, 22, 2);
4754     invert = extract32(insn, 21, 1);
4755     rm = extract32(insn, 16, 5);
4756     shift_amount = extract32(insn, 10, 6);
4757     rn = extract32(insn, 5, 5);
4758     rd = extract32(insn, 0, 5);
4759 
4760     if (!sf && (shift_amount & (1 << 5))) {
4761         unallocated_encoding(s);
4762         return;
4763     }
4764 
4765     tcg_rd = cpu_reg(s, rd);
4766 
4767     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4768         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4769          * register-register MOV and MVN, so it is worth special casing.
4770          */
4771         tcg_rm = cpu_reg(s, rm);
4772         if (invert) {
4773             tcg_gen_not_i64(tcg_rd, tcg_rm);
4774             if (!sf) {
4775                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4776             }
4777         } else {
4778             if (sf) {
4779                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4780             } else {
4781                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4782             }
4783         }
4784         return;
4785     }
4786 
4787     tcg_rm = read_cpu_reg(s, rm, sf);
4788 
4789     if (shift_amount) {
4790         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4791     }
4792 
4793     tcg_rn = cpu_reg(s, rn);
4794 
4795     switch (opc | (invert << 2)) {
4796     case 0: /* AND */
4797     case 3: /* ANDS */
4798         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4799         break;
4800     case 1: /* ORR */
4801         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4802         break;
4803     case 2: /* EOR */
4804         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4805         break;
4806     case 4: /* BIC */
4807     case 7: /* BICS */
4808         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4809         break;
4810     case 5: /* ORN */
4811         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4812         break;
4813     case 6: /* EON */
4814         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4815         break;
4816     default:
4817         assert(FALSE);
4818         break;
4819     }
4820 
4821     if (!sf) {
4822         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4823     }
4824 
4825     if (opc == 3) {
4826         gen_logic_CC(sf, tcg_rd);
4827     }
4828 }
4829 
4830 /*
4831  * Add/subtract (extended register)
4832  *
4833  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4834  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4835  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4836  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4837  *
4838  *  sf: 0 -> 32bit, 1 -> 64bit
4839  *  op: 0 -> add  , 1 -> sub
4840  *   S: 1 -> set flags
4841  * opt: 00
4842  * option: extension type (see DecodeRegExtend)
4843  * imm3: optional shift to Rm
4844  *
4845  * Rd = Rn + LSL(extend(Rm), amount)
4846  */
4847 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4848 {
4849     int rd = extract32(insn, 0, 5);
4850     int rn = extract32(insn, 5, 5);
4851     int imm3 = extract32(insn, 10, 3);
4852     int option = extract32(insn, 13, 3);
4853     int rm = extract32(insn, 16, 5);
4854     int opt = extract32(insn, 22, 2);
4855     bool setflags = extract32(insn, 29, 1);
4856     bool sub_op = extract32(insn, 30, 1);
4857     bool sf = extract32(insn, 31, 1);
4858 
4859     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4860     TCGv_i64 tcg_rd;
4861     TCGv_i64 tcg_result;
4862 
4863     if (imm3 > 4 || opt != 0) {
4864         unallocated_encoding(s);
4865         return;
4866     }
4867 
4868     /* non-flag setting ops may use SP */
4869     if (!setflags) {
4870         tcg_rd = cpu_reg_sp(s, rd);
4871     } else {
4872         tcg_rd = cpu_reg(s, rd);
4873     }
4874     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4875 
4876     tcg_rm = read_cpu_reg(s, rm, sf);
4877     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4878 
4879     tcg_result = tcg_temp_new_i64();
4880 
4881     if (!setflags) {
4882         if (sub_op) {
4883             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4884         } else {
4885             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4886         }
4887     } else {
4888         if (sub_op) {
4889             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4890         } else {
4891             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4892         }
4893     }
4894 
4895     if (sf) {
4896         tcg_gen_mov_i64(tcg_rd, tcg_result);
4897     } else {
4898         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4899     }
4900 }
4901 
4902 /*
4903  * Add/subtract (shifted register)
4904  *
4905  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4906  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4907  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4908  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4909  *
4910  *    sf: 0 -> 32bit, 1 -> 64bit
4911  *    op: 0 -> add  , 1 -> sub
4912  *     S: 1 -> set flags
4913  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4914  *  imm6: Shift amount to apply to Rm before the add/sub
4915  */
4916 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4917 {
4918     int rd = extract32(insn, 0, 5);
4919     int rn = extract32(insn, 5, 5);
4920     int imm6 = extract32(insn, 10, 6);
4921     int rm = extract32(insn, 16, 5);
4922     int shift_type = extract32(insn, 22, 2);
4923     bool setflags = extract32(insn, 29, 1);
4924     bool sub_op = extract32(insn, 30, 1);
4925     bool sf = extract32(insn, 31, 1);
4926 
4927     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4928     TCGv_i64 tcg_rn, tcg_rm;
4929     TCGv_i64 tcg_result;
4930 
4931     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4932         unallocated_encoding(s);
4933         return;
4934     }
4935 
4936     tcg_rn = read_cpu_reg(s, rn, sf);
4937     tcg_rm = read_cpu_reg(s, rm, sf);
4938 
4939     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4940 
4941     tcg_result = tcg_temp_new_i64();
4942 
4943     if (!setflags) {
4944         if (sub_op) {
4945             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4946         } else {
4947             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4948         }
4949     } else {
4950         if (sub_op) {
4951             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4952         } else {
4953             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4954         }
4955     }
4956 
4957     if (sf) {
4958         tcg_gen_mov_i64(tcg_rd, tcg_result);
4959     } else {
4960         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4961     }
4962 }
4963 
4964 /* Data-processing (3 source)
4965  *
4966  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4967  *  +--+------+-----------+------+------+----+------+------+------+
4968  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4969  *  +--+------+-----------+------+------+----+------+------+------+
4970  */
4971 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4972 {
4973     int rd = extract32(insn, 0, 5);
4974     int rn = extract32(insn, 5, 5);
4975     int ra = extract32(insn, 10, 5);
4976     int rm = extract32(insn, 16, 5);
4977     int op_id = (extract32(insn, 29, 3) << 4) |
4978         (extract32(insn, 21, 3) << 1) |
4979         extract32(insn, 15, 1);
4980     bool sf = extract32(insn, 31, 1);
4981     bool is_sub = extract32(op_id, 0, 1);
4982     bool is_high = extract32(op_id, 2, 1);
4983     bool is_signed = false;
4984     TCGv_i64 tcg_op1;
4985     TCGv_i64 tcg_op2;
4986     TCGv_i64 tcg_tmp;
4987 
4988     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4989     switch (op_id) {
4990     case 0x42: /* SMADDL */
4991     case 0x43: /* SMSUBL */
4992     case 0x44: /* SMULH */
4993         is_signed = true;
4994         break;
4995     case 0x0: /* MADD (32bit) */
4996     case 0x1: /* MSUB (32bit) */
4997     case 0x40: /* MADD (64bit) */
4998     case 0x41: /* MSUB (64bit) */
4999     case 0x4a: /* UMADDL */
5000     case 0x4b: /* UMSUBL */
5001     case 0x4c: /* UMULH */
5002         break;
5003     default:
5004         unallocated_encoding(s);
5005         return;
5006     }
5007 
5008     if (is_high) {
5009         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5010         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5011         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5012         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5013 
5014         if (is_signed) {
5015             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5016         } else {
5017             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5018         }
5019         return;
5020     }
5021 
5022     tcg_op1 = tcg_temp_new_i64();
5023     tcg_op2 = tcg_temp_new_i64();
5024     tcg_tmp = tcg_temp_new_i64();
5025 
5026     if (op_id < 0x42) {
5027         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5028         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5029     } else {
5030         if (is_signed) {
5031             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5032             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5033         } else {
5034             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5035             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5036         }
5037     }
5038 
5039     if (ra == 31 && !is_sub) {
5040         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5041         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5042     } else {
5043         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5044         if (is_sub) {
5045             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5046         } else {
5047             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5048         }
5049     }
5050 
5051     if (!sf) {
5052         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5053     }
5054 }
5055 
5056 /* Add/subtract (with carry)
5057  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5058  * +--+--+--+------------------------+------+-------------+------+-----+
5059  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5060  * +--+--+--+------------------------+------+-------------+------+-----+
5061  */
5062 
5063 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5064 {
5065     unsigned int sf, op, setflags, rm, rn, rd;
5066     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5067 
5068     sf = extract32(insn, 31, 1);
5069     op = extract32(insn, 30, 1);
5070     setflags = extract32(insn, 29, 1);
5071     rm = extract32(insn, 16, 5);
5072     rn = extract32(insn, 5, 5);
5073     rd = extract32(insn, 0, 5);
5074 
5075     tcg_rd = cpu_reg(s, rd);
5076     tcg_rn = cpu_reg(s, rn);
5077 
5078     if (op) {
5079         tcg_y = tcg_temp_new_i64();
5080         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5081     } else {
5082         tcg_y = cpu_reg(s, rm);
5083     }
5084 
5085     if (setflags) {
5086         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5087     } else {
5088         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5089     }
5090 }
5091 
5092 /*
5093  * Rotate right into flags
5094  *  31 30 29                21       15          10      5  4      0
5095  * +--+--+--+-----------------+--------+-----------+------+--+------+
5096  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5097  * +--+--+--+-----------------+--------+-----------+------+--+------+
5098  */
5099 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5100 {
5101     int mask = extract32(insn, 0, 4);
5102     int o2 = extract32(insn, 4, 1);
5103     int rn = extract32(insn, 5, 5);
5104     int imm6 = extract32(insn, 15, 6);
5105     int sf_op_s = extract32(insn, 29, 3);
5106     TCGv_i64 tcg_rn;
5107     TCGv_i32 nzcv;
5108 
5109     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5110         unallocated_encoding(s);
5111         return;
5112     }
5113 
5114     tcg_rn = read_cpu_reg(s, rn, 1);
5115     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5116 
5117     nzcv = tcg_temp_new_i32();
5118     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5119 
5120     if (mask & 8) { /* N */
5121         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5122     }
5123     if (mask & 4) { /* Z */
5124         tcg_gen_not_i32(cpu_ZF, nzcv);
5125         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5126     }
5127     if (mask & 2) { /* C */
5128         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5129     }
5130     if (mask & 1) { /* V */
5131         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5132     }
5133 }
5134 
5135 /*
5136  * Evaluate into flags
5137  *  31 30 29                21        15   14        10      5  4      0
5138  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5139  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5140  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5141  */
5142 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5143 {
5144     int o3_mask = extract32(insn, 0, 5);
5145     int rn = extract32(insn, 5, 5);
5146     int o2 = extract32(insn, 15, 6);
5147     int sz = extract32(insn, 14, 1);
5148     int sf_op_s = extract32(insn, 29, 3);
5149     TCGv_i32 tmp;
5150     int shift;
5151 
5152     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5153         !dc_isar_feature(aa64_condm_4, s)) {
5154         unallocated_encoding(s);
5155         return;
5156     }
5157     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5158 
5159     tmp = tcg_temp_new_i32();
5160     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5161     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5162     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5163     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5164     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5165 }
5166 
5167 /* Conditional compare (immediate / register)
5168  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5169  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5170  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5171  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5172  *        [1]                             y                [0]       [0]
5173  */
5174 static void disas_cc(DisasContext *s, uint32_t insn)
5175 {
5176     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5177     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5178     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5179     DisasCompare c;
5180 
5181     if (!extract32(insn, 29, 1)) {
5182         unallocated_encoding(s);
5183         return;
5184     }
5185     if (insn & (1 << 10 | 1 << 4)) {
5186         unallocated_encoding(s);
5187         return;
5188     }
5189     sf = extract32(insn, 31, 1);
5190     op = extract32(insn, 30, 1);
5191     is_imm = extract32(insn, 11, 1);
5192     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5193     cond = extract32(insn, 12, 4);
5194     rn = extract32(insn, 5, 5);
5195     nzcv = extract32(insn, 0, 4);
5196 
5197     /* Set T0 = !COND.  */
5198     tcg_t0 = tcg_temp_new_i32();
5199     arm_test_cc(&c, cond);
5200     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5201 
5202     /* Load the arguments for the new comparison.  */
5203     if (is_imm) {
5204         tcg_y = tcg_temp_new_i64();
5205         tcg_gen_movi_i64(tcg_y, y);
5206     } else {
5207         tcg_y = cpu_reg(s, y);
5208     }
5209     tcg_rn = cpu_reg(s, rn);
5210 
5211     /* Set the flags for the new comparison.  */
5212     tcg_tmp = tcg_temp_new_i64();
5213     if (op) {
5214         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5215     } else {
5216         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5217     }
5218 
5219     /* If COND was false, force the flags to #nzcv.  Compute two masks
5220      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5221      * For tcg hosts that support ANDC, we can make do with just T1.
5222      * In either case, allow the tcg optimizer to delete any unused mask.
5223      */
5224     tcg_t1 = tcg_temp_new_i32();
5225     tcg_t2 = tcg_temp_new_i32();
5226     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5227     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5228 
5229     if (nzcv & 8) { /* N */
5230         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5231     } else {
5232         if (TCG_TARGET_HAS_andc_i32) {
5233             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5234         } else {
5235             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5236         }
5237     }
5238     if (nzcv & 4) { /* Z */
5239         if (TCG_TARGET_HAS_andc_i32) {
5240             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5241         } else {
5242             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5243         }
5244     } else {
5245         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5246     }
5247     if (nzcv & 2) { /* C */
5248         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5249     } else {
5250         if (TCG_TARGET_HAS_andc_i32) {
5251             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5252         } else {
5253             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5254         }
5255     }
5256     if (nzcv & 1) { /* V */
5257         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5258     } else {
5259         if (TCG_TARGET_HAS_andc_i32) {
5260             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5261         } else {
5262             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5263         }
5264     }
5265 }
5266 
5267 /* Conditional select
5268  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5269  * +----+----+---+-----------------+------+------+-----+------+------+
5270  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5271  * +----+----+---+-----------------+------+------+-----+------+------+
5272  */
5273 static void disas_cond_select(DisasContext *s, uint32_t insn)
5274 {
5275     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5276     TCGv_i64 tcg_rd, zero;
5277     DisasCompare64 c;
5278 
5279     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5280         /* S == 1 or op2<1> == 1 */
5281         unallocated_encoding(s);
5282         return;
5283     }
5284     sf = extract32(insn, 31, 1);
5285     else_inv = extract32(insn, 30, 1);
5286     rm = extract32(insn, 16, 5);
5287     cond = extract32(insn, 12, 4);
5288     else_inc = extract32(insn, 10, 1);
5289     rn = extract32(insn, 5, 5);
5290     rd = extract32(insn, 0, 5);
5291 
5292     tcg_rd = cpu_reg(s, rd);
5293 
5294     a64_test_cc(&c, cond);
5295     zero = tcg_constant_i64(0);
5296 
5297     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5298         /* CSET & CSETM.  */
5299         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5300         if (else_inv) {
5301             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5302         }
5303     } else {
5304         TCGv_i64 t_true = cpu_reg(s, rn);
5305         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5306         if (else_inv && else_inc) {
5307             tcg_gen_neg_i64(t_false, t_false);
5308         } else if (else_inv) {
5309             tcg_gen_not_i64(t_false, t_false);
5310         } else if (else_inc) {
5311             tcg_gen_addi_i64(t_false, t_false, 1);
5312         }
5313         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5314     }
5315 
5316     if (!sf) {
5317         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5318     }
5319 }
5320 
5321 static void handle_clz(DisasContext *s, unsigned int sf,
5322                        unsigned int rn, unsigned int rd)
5323 {
5324     TCGv_i64 tcg_rd, tcg_rn;
5325     tcg_rd = cpu_reg(s, rd);
5326     tcg_rn = cpu_reg(s, rn);
5327 
5328     if (sf) {
5329         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5330     } else {
5331         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5332         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5333         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5334         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5335     }
5336 }
5337 
5338 static void handle_cls(DisasContext *s, unsigned int sf,
5339                        unsigned int rn, unsigned int rd)
5340 {
5341     TCGv_i64 tcg_rd, tcg_rn;
5342     tcg_rd = cpu_reg(s, rd);
5343     tcg_rn = cpu_reg(s, rn);
5344 
5345     if (sf) {
5346         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5347     } else {
5348         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5349         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5350         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5351         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5352     }
5353 }
5354 
5355 static void handle_rbit(DisasContext *s, unsigned int sf,
5356                         unsigned int rn, unsigned int rd)
5357 {
5358     TCGv_i64 tcg_rd, tcg_rn;
5359     tcg_rd = cpu_reg(s, rd);
5360     tcg_rn = cpu_reg(s, rn);
5361 
5362     if (sf) {
5363         gen_helper_rbit64(tcg_rd, tcg_rn);
5364     } else {
5365         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5366         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5367         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5368         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5369     }
5370 }
5371 
5372 /* REV with sf==1, opcode==3 ("REV64") */
5373 static void handle_rev64(DisasContext *s, unsigned int sf,
5374                          unsigned int rn, unsigned int rd)
5375 {
5376     if (!sf) {
5377         unallocated_encoding(s);
5378         return;
5379     }
5380     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5381 }
5382 
5383 /* REV with sf==0, opcode==2
5384  * REV32 (sf==1, opcode==2)
5385  */
5386 static void handle_rev32(DisasContext *s, unsigned int sf,
5387                          unsigned int rn, unsigned int rd)
5388 {
5389     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5390     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5391 
5392     if (sf) {
5393         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5394         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5395     } else {
5396         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5397     }
5398 }
5399 
5400 /* REV16 (opcode==1) */
5401 static void handle_rev16(DisasContext *s, unsigned int sf,
5402                          unsigned int rn, unsigned int rd)
5403 {
5404     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5405     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5406     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5407     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5408 
5409     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5410     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5411     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5412     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5413     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5414 }
5415 
5416 /* Data-processing (1 source)
5417  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5418  * +----+---+---+-----------------+---------+--------+------+------+
5419  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5420  * +----+---+---+-----------------+---------+--------+------+------+
5421  */
5422 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5423 {
5424     unsigned int sf, opcode, opcode2, rn, rd;
5425     TCGv_i64 tcg_rd;
5426 
5427     if (extract32(insn, 29, 1)) {
5428         unallocated_encoding(s);
5429         return;
5430     }
5431 
5432     sf = extract32(insn, 31, 1);
5433     opcode = extract32(insn, 10, 6);
5434     opcode2 = extract32(insn, 16, 5);
5435     rn = extract32(insn, 5, 5);
5436     rd = extract32(insn, 0, 5);
5437 
5438 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5439 
5440     switch (MAP(sf, opcode2, opcode)) {
5441     case MAP(0, 0x00, 0x00): /* RBIT */
5442     case MAP(1, 0x00, 0x00):
5443         handle_rbit(s, sf, rn, rd);
5444         break;
5445     case MAP(0, 0x00, 0x01): /* REV16 */
5446     case MAP(1, 0x00, 0x01):
5447         handle_rev16(s, sf, rn, rd);
5448         break;
5449     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5450     case MAP(1, 0x00, 0x02):
5451         handle_rev32(s, sf, rn, rd);
5452         break;
5453     case MAP(1, 0x00, 0x03): /* REV64 */
5454         handle_rev64(s, sf, rn, rd);
5455         break;
5456     case MAP(0, 0x00, 0x04): /* CLZ */
5457     case MAP(1, 0x00, 0x04):
5458         handle_clz(s, sf, rn, rd);
5459         break;
5460     case MAP(0, 0x00, 0x05): /* CLS */
5461     case MAP(1, 0x00, 0x05):
5462         handle_cls(s, sf, rn, rd);
5463         break;
5464     case MAP(1, 0x01, 0x00): /* PACIA */
5465         if (s->pauth_active) {
5466             tcg_rd = cpu_reg(s, rd);
5467             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5468         } else if (!dc_isar_feature(aa64_pauth, s)) {
5469             goto do_unallocated;
5470         }
5471         break;
5472     case MAP(1, 0x01, 0x01): /* PACIB */
5473         if (s->pauth_active) {
5474             tcg_rd = cpu_reg(s, rd);
5475             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5476         } else if (!dc_isar_feature(aa64_pauth, s)) {
5477             goto do_unallocated;
5478         }
5479         break;
5480     case MAP(1, 0x01, 0x02): /* PACDA */
5481         if (s->pauth_active) {
5482             tcg_rd = cpu_reg(s, rd);
5483             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5484         } else if (!dc_isar_feature(aa64_pauth, s)) {
5485             goto do_unallocated;
5486         }
5487         break;
5488     case MAP(1, 0x01, 0x03): /* PACDB */
5489         if (s->pauth_active) {
5490             tcg_rd = cpu_reg(s, rd);
5491             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5492         } else if (!dc_isar_feature(aa64_pauth, s)) {
5493             goto do_unallocated;
5494         }
5495         break;
5496     case MAP(1, 0x01, 0x04): /* AUTIA */
5497         if (s->pauth_active) {
5498             tcg_rd = cpu_reg(s, rd);
5499             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5500         } else if (!dc_isar_feature(aa64_pauth, s)) {
5501             goto do_unallocated;
5502         }
5503         break;
5504     case MAP(1, 0x01, 0x05): /* AUTIB */
5505         if (s->pauth_active) {
5506             tcg_rd = cpu_reg(s, rd);
5507             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5508         } else if (!dc_isar_feature(aa64_pauth, s)) {
5509             goto do_unallocated;
5510         }
5511         break;
5512     case MAP(1, 0x01, 0x06): /* AUTDA */
5513         if (s->pauth_active) {
5514             tcg_rd = cpu_reg(s, rd);
5515             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5516         } else if (!dc_isar_feature(aa64_pauth, s)) {
5517             goto do_unallocated;
5518         }
5519         break;
5520     case MAP(1, 0x01, 0x07): /* AUTDB */
5521         if (s->pauth_active) {
5522             tcg_rd = cpu_reg(s, rd);
5523             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5524         } else if (!dc_isar_feature(aa64_pauth, s)) {
5525             goto do_unallocated;
5526         }
5527         break;
5528     case MAP(1, 0x01, 0x08): /* PACIZA */
5529         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5530             goto do_unallocated;
5531         } else if (s->pauth_active) {
5532             tcg_rd = cpu_reg(s, rd);
5533             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5534         }
5535         break;
5536     case MAP(1, 0x01, 0x09): /* PACIZB */
5537         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5538             goto do_unallocated;
5539         } else if (s->pauth_active) {
5540             tcg_rd = cpu_reg(s, rd);
5541             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5542         }
5543         break;
5544     case MAP(1, 0x01, 0x0a): /* PACDZA */
5545         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5546             goto do_unallocated;
5547         } else if (s->pauth_active) {
5548             tcg_rd = cpu_reg(s, rd);
5549             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5550         }
5551         break;
5552     case MAP(1, 0x01, 0x0b): /* PACDZB */
5553         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5554             goto do_unallocated;
5555         } else if (s->pauth_active) {
5556             tcg_rd = cpu_reg(s, rd);
5557             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5558         }
5559         break;
5560     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5561         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5562             goto do_unallocated;
5563         } else if (s->pauth_active) {
5564             tcg_rd = cpu_reg(s, rd);
5565             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5566         }
5567         break;
5568     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5569         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5570             goto do_unallocated;
5571         } else if (s->pauth_active) {
5572             tcg_rd = cpu_reg(s, rd);
5573             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5574         }
5575         break;
5576     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5577         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5578             goto do_unallocated;
5579         } else if (s->pauth_active) {
5580             tcg_rd = cpu_reg(s, rd);
5581             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5582         }
5583         break;
5584     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5585         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5586             goto do_unallocated;
5587         } else if (s->pauth_active) {
5588             tcg_rd = cpu_reg(s, rd);
5589             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5590         }
5591         break;
5592     case MAP(1, 0x01, 0x10): /* XPACI */
5593         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5594             goto do_unallocated;
5595         } else if (s->pauth_active) {
5596             tcg_rd = cpu_reg(s, rd);
5597             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5598         }
5599         break;
5600     case MAP(1, 0x01, 0x11): /* XPACD */
5601         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5602             goto do_unallocated;
5603         } else if (s->pauth_active) {
5604             tcg_rd = cpu_reg(s, rd);
5605             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5606         }
5607         break;
5608     default:
5609     do_unallocated:
5610         unallocated_encoding(s);
5611         break;
5612     }
5613 
5614 #undef MAP
5615 }
5616 
5617 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5618                        unsigned int rm, unsigned int rn, unsigned int rd)
5619 {
5620     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5621     tcg_rd = cpu_reg(s, rd);
5622 
5623     if (!sf && is_signed) {
5624         tcg_n = tcg_temp_new_i64();
5625         tcg_m = tcg_temp_new_i64();
5626         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5627         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5628     } else {
5629         tcg_n = read_cpu_reg(s, rn, sf);
5630         tcg_m = read_cpu_reg(s, rm, sf);
5631     }
5632 
5633     if (is_signed) {
5634         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5635     } else {
5636         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5637     }
5638 
5639     if (!sf) { /* zero extend final result */
5640         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5641     }
5642 }
5643 
5644 /* LSLV, LSRV, ASRV, RORV */
5645 static void handle_shift_reg(DisasContext *s,
5646                              enum a64_shift_type shift_type, unsigned int sf,
5647                              unsigned int rm, unsigned int rn, unsigned int rd)
5648 {
5649     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5650     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5651     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5652 
5653     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5654     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5655 }
5656 
5657 /* CRC32[BHWX], CRC32C[BHWX] */
5658 static void handle_crc32(DisasContext *s,
5659                          unsigned int sf, unsigned int sz, bool crc32c,
5660                          unsigned int rm, unsigned int rn, unsigned int rd)
5661 {
5662     TCGv_i64 tcg_acc, tcg_val;
5663     TCGv_i32 tcg_bytes;
5664 
5665     if (!dc_isar_feature(aa64_crc32, s)
5666         || (sf == 1 && sz != 3)
5667         || (sf == 0 && sz == 3)) {
5668         unallocated_encoding(s);
5669         return;
5670     }
5671 
5672     if (sz == 3) {
5673         tcg_val = cpu_reg(s, rm);
5674     } else {
5675         uint64_t mask;
5676         switch (sz) {
5677         case 0:
5678             mask = 0xFF;
5679             break;
5680         case 1:
5681             mask = 0xFFFF;
5682             break;
5683         case 2:
5684             mask = 0xFFFFFFFF;
5685             break;
5686         default:
5687             g_assert_not_reached();
5688         }
5689         tcg_val = tcg_temp_new_i64();
5690         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5691     }
5692 
5693     tcg_acc = cpu_reg(s, rn);
5694     tcg_bytes = tcg_constant_i32(1 << sz);
5695 
5696     if (crc32c) {
5697         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5698     } else {
5699         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5700     }
5701 }
5702 
5703 /* Data-processing (2 source)
5704  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5705  * +----+---+---+-----------------+------+--------+------+------+
5706  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5707  * +----+---+---+-----------------+------+--------+------+------+
5708  */
5709 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5710 {
5711     unsigned int sf, rm, opcode, rn, rd, setflag;
5712     sf = extract32(insn, 31, 1);
5713     setflag = extract32(insn, 29, 1);
5714     rm = extract32(insn, 16, 5);
5715     opcode = extract32(insn, 10, 6);
5716     rn = extract32(insn, 5, 5);
5717     rd = extract32(insn, 0, 5);
5718 
5719     if (setflag && opcode != 0) {
5720         unallocated_encoding(s);
5721         return;
5722     }
5723 
5724     switch (opcode) {
5725     case 0: /* SUBP(S) */
5726         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5727             goto do_unallocated;
5728         } else {
5729             TCGv_i64 tcg_n, tcg_m, tcg_d;
5730 
5731             tcg_n = read_cpu_reg_sp(s, rn, true);
5732             tcg_m = read_cpu_reg_sp(s, rm, true);
5733             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5734             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5735             tcg_d = cpu_reg(s, rd);
5736 
5737             if (setflag) {
5738                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5739             } else {
5740                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5741             }
5742         }
5743         break;
5744     case 2: /* UDIV */
5745         handle_div(s, false, sf, rm, rn, rd);
5746         break;
5747     case 3: /* SDIV */
5748         handle_div(s, true, sf, rm, rn, rd);
5749         break;
5750     case 4: /* IRG */
5751         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5752             goto do_unallocated;
5753         }
5754         if (s->ata) {
5755             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5756                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5757         } else {
5758             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5759                                              cpu_reg_sp(s, rn));
5760         }
5761         break;
5762     case 5: /* GMI */
5763         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5764             goto do_unallocated;
5765         } else {
5766             TCGv_i64 t = tcg_temp_new_i64();
5767 
5768             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5769             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5770             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5771         }
5772         break;
5773     case 8: /* LSLV */
5774         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5775         break;
5776     case 9: /* LSRV */
5777         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5778         break;
5779     case 10: /* ASRV */
5780         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5781         break;
5782     case 11: /* RORV */
5783         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5784         break;
5785     case 12: /* PACGA */
5786         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5787             goto do_unallocated;
5788         }
5789         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5790                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5791         break;
5792     case 16:
5793     case 17:
5794     case 18:
5795     case 19:
5796     case 20:
5797     case 21:
5798     case 22:
5799     case 23: /* CRC32 */
5800     {
5801         int sz = extract32(opcode, 0, 2);
5802         bool crc32c = extract32(opcode, 2, 1);
5803         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5804         break;
5805     }
5806     default:
5807     do_unallocated:
5808         unallocated_encoding(s);
5809         break;
5810     }
5811 }
5812 
5813 /*
5814  * Data processing - register
5815  *  31  30 29  28      25    21  20  16      10         0
5816  * +--+---+--+---+-------+-----+-------+-------+---------+
5817  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5818  * +--+---+--+---+-------+-----+-------+-------+---------+
5819  */
5820 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5821 {
5822     int op0 = extract32(insn, 30, 1);
5823     int op1 = extract32(insn, 28, 1);
5824     int op2 = extract32(insn, 21, 4);
5825     int op3 = extract32(insn, 10, 6);
5826 
5827     if (!op1) {
5828         if (op2 & 8) {
5829             if (op2 & 1) {
5830                 /* Add/sub (extended register) */
5831                 disas_add_sub_ext_reg(s, insn);
5832             } else {
5833                 /* Add/sub (shifted register) */
5834                 disas_add_sub_reg(s, insn);
5835             }
5836         } else {
5837             /* Logical (shifted register) */
5838             disas_logic_reg(s, insn);
5839         }
5840         return;
5841     }
5842 
5843     switch (op2) {
5844     case 0x0:
5845         switch (op3) {
5846         case 0x00: /* Add/subtract (with carry) */
5847             disas_adc_sbc(s, insn);
5848             break;
5849 
5850         case 0x01: /* Rotate right into flags */
5851         case 0x21:
5852             disas_rotate_right_into_flags(s, insn);
5853             break;
5854 
5855         case 0x02: /* Evaluate into flags */
5856         case 0x12:
5857         case 0x22:
5858         case 0x32:
5859             disas_evaluate_into_flags(s, insn);
5860             break;
5861 
5862         default:
5863             goto do_unallocated;
5864         }
5865         break;
5866 
5867     case 0x2: /* Conditional compare */
5868         disas_cc(s, insn); /* both imm and reg forms */
5869         break;
5870 
5871     case 0x4: /* Conditional select */
5872         disas_cond_select(s, insn);
5873         break;
5874 
5875     case 0x6: /* Data-processing */
5876         if (op0) {    /* (1 source) */
5877             disas_data_proc_1src(s, insn);
5878         } else {      /* (2 source) */
5879             disas_data_proc_2src(s, insn);
5880         }
5881         break;
5882     case 0x8 ... 0xf: /* (3 source) */
5883         disas_data_proc_3src(s, insn);
5884         break;
5885 
5886     default:
5887     do_unallocated:
5888         unallocated_encoding(s);
5889         break;
5890     }
5891 }
5892 
5893 static void handle_fp_compare(DisasContext *s, int size,
5894                               unsigned int rn, unsigned int rm,
5895                               bool cmp_with_zero, bool signal_all_nans)
5896 {
5897     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5898     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5899 
5900     if (size == MO_64) {
5901         TCGv_i64 tcg_vn, tcg_vm;
5902 
5903         tcg_vn = read_fp_dreg(s, rn);
5904         if (cmp_with_zero) {
5905             tcg_vm = tcg_constant_i64(0);
5906         } else {
5907             tcg_vm = read_fp_dreg(s, rm);
5908         }
5909         if (signal_all_nans) {
5910             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5911         } else {
5912             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5913         }
5914     } else {
5915         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5916         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5917 
5918         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5919         if (cmp_with_zero) {
5920             tcg_gen_movi_i32(tcg_vm, 0);
5921         } else {
5922             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5923         }
5924 
5925         switch (size) {
5926         case MO_32:
5927             if (signal_all_nans) {
5928                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5929             } else {
5930                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5931             }
5932             break;
5933         case MO_16:
5934             if (signal_all_nans) {
5935                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5936             } else {
5937                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5938             }
5939             break;
5940         default:
5941             g_assert_not_reached();
5942         }
5943     }
5944 
5945     gen_set_nzcv(tcg_flags);
5946 }
5947 
5948 /* Floating point compare
5949  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5950  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5951  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5952  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5953  */
5954 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5955 {
5956     unsigned int mos, type, rm, op, rn, opc, op2r;
5957     int size;
5958 
5959     mos = extract32(insn, 29, 3);
5960     type = extract32(insn, 22, 2);
5961     rm = extract32(insn, 16, 5);
5962     op = extract32(insn, 14, 2);
5963     rn = extract32(insn, 5, 5);
5964     opc = extract32(insn, 3, 2);
5965     op2r = extract32(insn, 0, 3);
5966 
5967     if (mos || op || op2r) {
5968         unallocated_encoding(s);
5969         return;
5970     }
5971 
5972     switch (type) {
5973     case 0:
5974         size = MO_32;
5975         break;
5976     case 1:
5977         size = MO_64;
5978         break;
5979     case 3:
5980         size = MO_16;
5981         if (dc_isar_feature(aa64_fp16, s)) {
5982             break;
5983         }
5984         /* fallthru */
5985     default:
5986         unallocated_encoding(s);
5987         return;
5988     }
5989 
5990     if (!fp_access_check(s)) {
5991         return;
5992     }
5993 
5994     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5995 }
5996 
5997 /* Floating point conditional compare
5998  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5999  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6000  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6001  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6002  */
6003 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6004 {
6005     unsigned int mos, type, rm, cond, rn, op, nzcv;
6006     TCGLabel *label_continue = NULL;
6007     int size;
6008 
6009     mos = extract32(insn, 29, 3);
6010     type = extract32(insn, 22, 2);
6011     rm = extract32(insn, 16, 5);
6012     cond = extract32(insn, 12, 4);
6013     rn = extract32(insn, 5, 5);
6014     op = extract32(insn, 4, 1);
6015     nzcv = extract32(insn, 0, 4);
6016 
6017     if (mos) {
6018         unallocated_encoding(s);
6019         return;
6020     }
6021 
6022     switch (type) {
6023     case 0:
6024         size = MO_32;
6025         break;
6026     case 1:
6027         size = MO_64;
6028         break;
6029     case 3:
6030         size = MO_16;
6031         if (dc_isar_feature(aa64_fp16, s)) {
6032             break;
6033         }
6034         /* fallthru */
6035     default:
6036         unallocated_encoding(s);
6037         return;
6038     }
6039 
6040     if (!fp_access_check(s)) {
6041         return;
6042     }
6043 
6044     if (cond < 0x0e) { /* not always */
6045         TCGLabel *label_match = gen_new_label();
6046         label_continue = gen_new_label();
6047         arm_gen_test_cc(cond, label_match);
6048         /* nomatch: */
6049         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6050         tcg_gen_br(label_continue);
6051         gen_set_label(label_match);
6052     }
6053 
6054     handle_fp_compare(s, size, rn, rm, false, op);
6055 
6056     if (cond < 0x0e) {
6057         gen_set_label(label_continue);
6058     }
6059 }
6060 
6061 /* Floating point conditional select
6062  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6063  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6064  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6065  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6066  */
6067 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6068 {
6069     unsigned int mos, type, rm, cond, rn, rd;
6070     TCGv_i64 t_true, t_false;
6071     DisasCompare64 c;
6072     MemOp sz;
6073 
6074     mos = extract32(insn, 29, 3);
6075     type = extract32(insn, 22, 2);
6076     rm = extract32(insn, 16, 5);
6077     cond = extract32(insn, 12, 4);
6078     rn = extract32(insn, 5, 5);
6079     rd = extract32(insn, 0, 5);
6080 
6081     if (mos) {
6082         unallocated_encoding(s);
6083         return;
6084     }
6085 
6086     switch (type) {
6087     case 0:
6088         sz = MO_32;
6089         break;
6090     case 1:
6091         sz = MO_64;
6092         break;
6093     case 3:
6094         sz = MO_16;
6095         if (dc_isar_feature(aa64_fp16, s)) {
6096             break;
6097         }
6098         /* fallthru */
6099     default:
6100         unallocated_encoding(s);
6101         return;
6102     }
6103 
6104     if (!fp_access_check(s)) {
6105         return;
6106     }
6107 
6108     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6109     t_true = tcg_temp_new_i64();
6110     t_false = tcg_temp_new_i64();
6111     read_vec_element(s, t_true, rn, 0, sz);
6112     read_vec_element(s, t_false, rm, 0, sz);
6113 
6114     a64_test_cc(&c, cond);
6115     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6116                         t_true, t_false);
6117 
6118     /* Note that sregs & hregs write back zeros to the high bits,
6119        and we've already done the zero-extension.  */
6120     write_fp_dreg(s, rd, t_true);
6121 }
6122 
6123 /* Floating-point data-processing (1 source) - half precision */
6124 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6125 {
6126     TCGv_ptr fpst = NULL;
6127     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6128     TCGv_i32 tcg_res = tcg_temp_new_i32();
6129 
6130     switch (opcode) {
6131     case 0x0: /* FMOV */
6132         tcg_gen_mov_i32(tcg_res, tcg_op);
6133         break;
6134     case 0x1: /* FABS */
6135         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6136         break;
6137     case 0x2: /* FNEG */
6138         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6139         break;
6140     case 0x3: /* FSQRT */
6141         fpst = fpstatus_ptr(FPST_FPCR_F16);
6142         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6143         break;
6144     case 0x8: /* FRINTN */
6145     case 0x9: /* FRINTP */
6146     case 0xa: /* FRINTM */
6147     case 0xb: /* FRINTZ */
6148     case 0xc: /* FRINTA */
6149     {
6150         TCGv_i32 tcg_rmode;
6151 
6152         fpst = fpstatus_ptr(FPST_FPCR_F16);
6153         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6154         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6155         gen_restore_rmode(tcg_rmode, fpst);
6156         break;
6157     }
6158     case 0xe: /* FRINTX */
6159         fpst = fpstatus_ptr(FPST_FPCR_F16);
6160         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6161         break;
6162     case 0xf: /* FRINTI */
6163         fpst = fpstatus_ptr(FPST_FPCR_F16);
6164         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6165         break;
6166     default:
6167         g_assert_not_reached();
6168     }
6169 
6170     write_fp_sreg(s, rd, tcg_res);
6171 }
6172 
6173 /* Floating-point data-processing (1 source) - single precision */
6174 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6175 {
6176     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6177     TCGv_i32 tcg_op, tcg_res;
6178     TCGv_ptr fpst;
6179     int rmode = -1;
6180 
6181     tcg_op = read_fp_sreg(s, rn);
6182     tcg_res = tcg_temp_new_i32();
6183 
6184     switch (opcode) {
6185     case 0x0: /* FMOV */
6186         tcg_gen_mov_i32(tcg_res, tcg_op);
6187         goto done;
6188     case 0x1: /* FABS */
6189         gen_helper_vfp_abss(tcg_res, tcg_op);
6190         goto done;
6191     case 0x2: /* FNEG */
6192         gen_helper_vfp_negs(tcg_res, tcg_op);
6193         goto done;
6194     case 0x3: /* FSQRT */
6195         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6196         goto done;
6197     case 0x6: /* BFCVT */
6198         gen_fpst = gen_helper_bfcvt;
6199         break;
6200     case 0x8: /* FRINTN */
6201     case 0x9: /* FRINTP */
6202     case 0xa: /* FRINTM */
6203     case 0xb: /* FRINTZ */
6204     case 0xc: /* FRINTA */
6205         rmode = opcode & 7;
6206         gen_fpst = gen_helper_rints;
6207         break;
6208     case 0xe: /* FRINTX */
6209         gen_fpst = gen_helper_rints_exact;
6210         break;
6211     case 0xf: /* FRINTI */
6212         gen_fpst = gen_helper_rints;
6213         break;
6214     case 0x10: /* FRINT32Z */
6215         rmode = FPROUNDING_ZERO;
6216         gen_fpst = gen_helper_frint32_s;
6217         break;
6218     case 0x11: /* FRINT32X */
6219         gen_fpst = gen_helper_frint32_s;
6220         break;
6221     case 0x12: /* FRINT64Z */
6222         rmode = FPROUNDING_ZERO;
6223         gen_fpst = gen_helper_frint64_s;
6224         break;
6225     case 0x13: /* FRINT64X */
6226         gen_fpst = gen_helper_frint64_s;
6227         break;
6228     default:
6229         g_assert_not_reached();
6230     }
6231 
6232     fpst = fpstatus_ptr(FPST_FPCR);
6233     if (rmode >= 0) {
6234         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6235         gen_fpst(tcg_res, tcg_op, fpst);
6236         gen_restore_rmode(tcg_rmode, fpst);
6237     } else {
6238         gen_fpst(tcg_res, tcg_op, fpst);
6239     }
6240 
6241  done:
6242     write_fp_sreg(s, rd, tcg_res);
6243 }
6244 
6245 /* Floating-point data-processing (1 source) - double precision */
6246 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6247 {
6248     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6249     TCGv_i64 tcg_op, tcg_res;
6250     TCGv_ptr fpst;
6251     int rmode = -1;
6252 
6253     switch (opcode) {
6254     case 0x0: /* FMOV */
6255         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6256         return;
6257     }
6258 
6259     tcg_op = read_fp_dreg(s, rn);
6260     tcg_res = tcg_temp_new_i64();
6261 
6262     switch (opcode) {
6263     case 0x1: /* FABS */
6264         gen_helper_vfp_absd(tcg_res, tcg_op);
6265         goto done;
6266     case 0x2: /* FNEG */
6267         gen_helper_vfp_negd(tcg_res, tcg_op);
6268         goto done;
6269     case 0x3: /* FSQRT */
6270         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6271         goto done;
6272     case 0x8: /* FRINTN */
6273     case 0x9: /* FRINTP */
6274     case 0xa: /* FRINTM */
6275     case 0xb: /* FRINTZ */
6276     case 0xc: /* FRINTA */
6277         rmode = opcode & 7;
6278         gen_fpst = gen_helper_rintd;
6279         break;
6280     case 0xe: /* FRINTX */
6281         gen_fpst = gen_helper_rintd_exact;
6282         break;
6283     case 0xf: /* FRINTI */
6284         gen_fpst = gen_helper_rintd;
6285         break;
6286     case 0x10: /* FRINT32Z */
6287         rmode = FPROUNDING_ZERO;
6288         gen_fpst = gen_helper_frint32_d;
6289         break;
6290     case 0x11: /* FRINT32X */
6291         gen_fpst = gen_helper_frint32_d;
6292         break;
6293     case 0x12: /* FRINT64Z */
6294         rmode = FPROUNDING_ZERO;
6295         gen_fpst = gen_helper_frint64_d;
6296         break;
6297     case 0x13: /* FRINT64X */
6298         gen_fpst = gen_helper_frint64_d;
6299         break;
6300     default:
6301         g_assert_not_reached();
6302     }
6303 
6304     fpst = fpstatus_ptr(FPST_FPCR);
6305     if (rmode >= 0) {
6306         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6307         gen_fpst(tcg_res, tcg_op, fpst);
6308         gen_restore_rmode(tcg_rmode, fpst);
6309     } else {
6310         gen_fpst(tcg_res, tcg_op, fpst);
6311     }
6312 
6313  done:
6314     write_fp_dreg(s, rd, tcg_res);
6315 }
6316 
6317 static void handle_fp_fcvt(DisasContext *s, int opcode,
6318                            int rd, int rn, int dtype, int ntype)
6319 {
6320     switch (ntype) {
6321     case 0x0:
6322     {
6323         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6324         if (dtype == 1) {
6325             /* Single to double */
6326             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6327             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6328             write_fp_dreg(s, rd, tcg_rd);
6329         } else {
6330             /* Single to half */
6331             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6332             TCGv_i32 ahp = get_ahp_flag();
6333             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6334 
6335             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6336             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6337             write_fp_sreg(s, rd, tcg_rd);
6338         }
6339         break;
6340     }
6341     case 0x1:
6342     {
6343         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6344         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6345         if (dtype == 0) {
6346             /* Double to single */
6347             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6348         } else {
6349             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6350             TCGv_i32 ahp = get_ahp_flag();
6351             /* Double to half */
6352             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6353             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6354         }
6355         write_fp_sreg(s, rd, tcg_rd);
6356         break;
6357     }
6358     case 0x3:
6359     {
6360         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6361         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6362         TCGv_i32 tcg_ahp = get_ahp_flag();
6363         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6364         if (dtype == 0) {
6365             /* Half to single */
6366             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6367             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6368             write_fp_sreg(s, rd, tcg_rd);
6369         } else {
6370             /* Half to double */
6371             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6372             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6373             write_fp_dreg(s, rd, tcg_rd);
6374         }
6375         break;
6376     }
6377     default:
6378         g_assert_not_reached();
6379     }
6380 }
6381 
6382 /* Floating point data-processing (1 source)
6383  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6384  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6385  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6386  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6387  */
6388 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6389 {
6390     int mos = extract32(insn, 29, 3);
6391     int type = extract32(insn, 22, 2);
6392     int opcode = extract32(insn, 15, 6);
6393     int rn = extract32(insn, 5, 5);
6394     int rd = extract32(insn, 0, 5);
6395 
6396     if (mos) {
6397         goto do_unallocated;
6398     }
6399 
6400     switch (opcode) {
6401     case 0x4: case 0x5: case 0x7:
6402     {
6403         /* FCVT between half, single and double precision */
6404         int dtype = extract32(opcode, 0, 2);
6405         if (type == 2 || dtype == type) {
6406             goto do_unallocated;
6407         }
6408         if (!fp_access_check(s)) {
6409             return;
6410         }
6411 
6412         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6413         break;
6414     }
6415 
6416     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6417         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6418             goto do_unallocated;
6419         }
6420         /* fall through */
6421     case 0x0 ... 0x3:
6422     case 0x8 ... 0xc:
6423     case 0xe ... 0xf:
6424         /* 32-to-32 and 64-to-64 ops */
6425         switch (type) {
6426         case 0:
6427             if (!fp_access_check(s)) {
6428                 return;
6429             }
6430             handle_fp_1src_single(s, opcode, rd, rn);
6431             break;
6432         case 1:
6433             if (!fp_access_check(s)) {
6434                 return;
6435             }
6436             handle_fp_1src_double(s, opcode, rd, rn);
6437             break;
6438         case 3:
6439             if (!dc_isar_feature(aa64_fp16, s)) {
6440                 goto do_unallocated;
6441             }
6442 
6443             if (!fp_access_check(s)) {
6444                 return;
6445             }
6446             handle_fp_1src_half(s, opcode, rd, rn);
6447             break;
6448         default:
6449             goto do_unallocated;
6450         }
6451         break;
6452 
6453     case 0x6:
6454         switch (type) {
6455         case 1: /* BFCVT */
6456             if (!dc_isar_feature(aa64_bf16, s)) {
6457                 goto do_unallocated;
6458             }
6459             if (!fp_access_check(s)) {
6460                 return;
6461             }
6462             handle_fp_1src_single(s, opcode, rd, rn);
6463             break;
6464         default:
6465             goto do_unallocated;
6466         }
6467         break;
6468 
6469     default:
6470     do_unallocated:
6471         unallocated_encoding(s);
6472         break;
6473     }
6474 }
6475 
6476 /* Floating-point data-processing (2 source) - single precision */
6477 static void handle_fp_2src_single(DisasContext *s, int opcode,
6478                                   int rd, int rn, int rm)
6479 {
6480     TCGv_i32 tcg_op1;
6481     TCGv_i32 tcg_op2;
6482     TCGv_i32 tcg_res;
6483     TCGv_ptr fpst;
6484 
6485     tcg_res = tcg_temp_new_i32();
6486     fpst = fpstatus_ptr(FPST_FPCR);
6487     tcg_op1 = read_fp_sreg(s, rn);
6488     tcg_op2 = read_fp_sreg(s, rm);
6489 
6490     switch (opcode) {
6491     case 0x0: /* FMUL */
6492         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6493         break;
6494     case 0x1: /* FDIV */
6495         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6496         break;
6497     case 0x2: /* FADD */
6498         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6499         break;
6500     case 0x3: /* FSUB */
6501         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6502         break;
6503     case 0x4: /* FMAX */
6504         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6505         break;
6506     case 0x5: /* FMIN */
6507         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6508         break;
6509     case 0x6: /* FMAXNM */
6510         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6511         break;
6512     case 0x7: /* FMINNM */
6513         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6514         break;
6515     case 0x8: /* FNMUL */
6516         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6517         gen_helper_vfp_negs(tcg_res, tcg_res);
6518         break;
6519     }
6520 
6521     write_fp_sreg(s, rd, tcg_res);
6522 }
6523 
6524 /* Floating-point data-processing (2 source) - double precision */
6525 static void handle_fp_2src_double(DisasContext *s, int opcode,
6526                                   int rd, int rn, int rm)
6527 {
6528     TCGv_i64 tcg_op1;
6529     TCGv_i64 tcg_op2;
6530     TCGv_i64 tcg_res;
6531     TCGv_ptr fpst;
6532 
6533     tcg_res = tcg_temp_new_i64();
6534     fpst = fpstatus_ptr(FPST_FPCR);
6535     tcg_op1 = read_fp_dreg(s, rn);
6536     tcg_op2 = read_fp_dreg(s, rm);
6537 
6538     switch (opcode) {
6539     case 0x0: /* FMUL */
6540         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6541         break;
6542     case 0x1: /* FDIV */
6543         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6544         break;
6545     case 0x2: /* FADD */
6546         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6547         break;
6548     case 0x3: /* FSUB */
6549         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6550         break;
6551     case 0x4: /* FMAX */
6552         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6553         break;
6554     case 0x5: /* FMIN */
6555         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6556         break;
6557     case 0x6: /* FMAXNM */
6558         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6559         break;
6560     case 0x7: /* FMINNM */
6561         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6562         break;
6563     case 0x8: /* FNMUL */
6564         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6565         gen_helper_vfp_negd(tcg_res, tcg_res);
6566         break;
6567     }
6568 
6569     write_fp_dreg(s, rd, tcg_res);
6570 }
6571 
6572 /* Floating-point data-processing (2 source) - half precision */
6573 static void handle_fp_2src_half(DisasContext *s, int opcode,
6574                                 int rd, int rn, int rm)
6575 {
6576     TCGv_i32 tcg_op1;
6577     TCGv_i32 tcg_op2;
6578     TCGv_i32 tcg_res;
6579     TCGv_ptr fpst;
6580 
6581     tcg_res = tcg_temp_new_i32();
6582     fpst = fpstatus_ptr(FPST_FPCR_F16);
6583     tcg_op1 = read_fp_hreg(s, rn);
6584     tcg_op2 = read_fp_hreg(s, rm);
6585 
6586     switch (opcode) {
6587     case 0x0: /* FMUL */
6588         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6589         break;
6590     case 0x1: /* FDIV */
6591         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6592         break;
6593     case 0x2: /* FADD */
6594         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6595         break;
6596     case 0x3: /* FSUB */
6597         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6598         break;
6599     case 0x4: /* FMAX */
6600         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6601         break;
6602     case 0x5: /* FMIN */
6603         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6604         break;
6605     case 0x6: /* FMAXNM */
6606         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6607         break;
6608     case 0x7: /* FMINNM */
6609         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6610         break;
6611     case 0x8: /* FNMUL */
6612         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6613         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6614         break;
6615     default:
6616         g_assert_not_reached();
6617     }
6618 
6619     write_fp_sreg(s, rd, tcg_res);
6620 }
6621 
6622 /* Floating point data-processing (2 source)
6623  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6624  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6625  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6626  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6627  */
6628 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6629 {
6630     int mos = extract32(insn, 29, 3);
6631     int type = extract32(insn, 22, 2);
6632     int rd = extract32(insn, 0, 5);
6633     int rn = extract32(insn, 5, 5);
6634     int rm = extract32(insn, 16, 5);
6635     int opcode = extract32(insn, 12, 4);
6636 
6637     if (opcode > 8 || mos) {
6638         unallocated_encoding(s);
6639         return;
6640     }
6641 
6642     switch (type) {
6643     case 0:
6644         if (!fp_access_check(s)) {
6645             return;
6646         }
6647         handle_fp_2src_single(s, opcode, rd, rn, rm);
6648         break;
6649     case 1:
6650         if (!fp_access_check(s)) {
6651             return;
6652         }
6653         handle_fp_2src_double(s, opcode, rd, rn, rm);
6654         break;
6655     case 3:
6656         if (!dc_isar_feature(aa64_fp16, s)) {
6657             unallocated_encoding(s);
6658             return;
6659         }
6660         if (!fp_access_check(s)) {
6661             return;
6662         }
6663         handle_fp_2src_half(s, opcode, rd, rn, rm);
6664         break;
6665     default:
6666         unallocated_encoding(s);
6667     }
6668 }
6669 
6670 /* Floating-point data-processing (3 source) - single precision */
6671 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6672                                   int rd, int rn, int rm, int ra)
6673 {
6674     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6675     TCGv_i32 tcg_res = tcg_temp_new_i32();
6676     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6677 
6678     tcg_op1 = read_fp_sreg(s, rn);
6679     tcg_op2 = read_fp_sreg(s, rm);
6680     tcg_op3 = read_fp_sreg(s, ra);
6681 
6682     /* These are fused multiply-add, and must be done as one
6683      * floating point operation with no rounding between the
6684      * multiplication and addition steps.
6685      * NB that doing the negations here as separate steps is
6686      * correct : an input NaN should come out with its sign bit
6687      * flipped if it is a negated-input.
6688      */
6689     if (o1 == true) {
6690         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6691     }
6692 
6693     if (o0 != o1) {
6694         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6695     }
6696 
6697     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6698 
6699     write_fp_sreg(s, rd, tcg_res);
6700 }
6701 
6702 /* Floating-point data-processing (3 source) - double precision */
6703 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6704                                   int rd, int rn, int rm, int ra)
6705 {
6706     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6707     TCGv_i64 tcg_res = tcg_temp_new_i64();
6708     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6709 
6710     tcg_op1 = read_fp_dreg(s, rn);
6711     tcg_op2 = read_fp_dreg(s, rm);
6712     tcg_op3 = read_fp_dreg(s, ra);
6713 
6714     /* These are fused multiply-add, and must be done as one
6715      * floating point operation with no rounding between the
6716      * multiplication and addition steps.
6717      * NB that doing the negations here as separate steps is
6718      * correct : an input NaN should come out with its sign bit
6719      * flipped if it is a negated-input.
6720      */
6721     if (o1 == true) {
6722         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6723     }
6724 
6725     if (o0 != o1) {
6726         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6727     }
6728 
6729     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6730 
6731     write_fp_dreg(s, rd, tcg_res);
6732 }
6733 
6734 /* Floating-point data-processing (3 source) - half precision */
6735 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6736                                 int rd, int rn, int rm, int ra)
6737 {
6738     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6739     TCGv_i32 tcg_res = tcg_temp_new_i32();
6740     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6741 
6742     tcg_op1 = read_fp_hreg(s, rn);
6743     tcg_op2 = read_fp_hreg(s, rm);
6744     tcg_op3 = read_fp_hreg(s, ra);
6745 
6746     /* These are fused multiply-add, and must be done as one
6747      * floating point operation with no rounding between the
6748      * multiplication and addition steps.
6749      * NB that doing the negations here as separate steps is
6750      * correct : an input NaN should come out with its sign bit
6751      * flipped if it is a negated-input.
6752      */
6753     if (o1 == true) {
6754         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6755     }
6756 
6757     if (o0 != o1) {
6758         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6759     }
6760 
6761     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6762 
6763     write_fp_sreg(s, rd, tcg_res);
6764 }
6765 
6766 /* Floating point data-processing (3 source)
6767  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6768  * +---+---+---+-----------+------+----+------+----+------+------+------+
6769  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6770  * +---+---+---+-----------+------+----+------+----+------+------+------+
6771  */
6772 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6773 {
6774     int mos = extract32(insn, 29, 3);
6775     int type = extract32(insn, 22, 2);
6776     int rd = extract32(insn, 0, 5);
6777     int rn = extract32(insn, 5, 5);
6778     int ra = extract32(insn, 10, 5);
6779     int rm = extract32(insn, 16, 5);
6780     bool o0 = extract32(insn, 15, 1);
6781     bool o1 = extract32(insn, 21, 1);
6782 
6783     if (mos) {
6784         unallocated_encoding(s);
6785         return;
6786     }
6787 
6788     switch (type) {
6789     case 0:
6790         if (!fp_access_check(s)) {
6791             return;
6792         }
6793         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6794         break;
6795     case 1:
6796         if (!fp_access_check(s)) {
6797             return;
6798         }
6799         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6800         break;
6801     case 3:
6802         if (!dc_isar_feature(aa64_fp16, s)) {
6803             unallocated_encoding(s);
6804             return;
6805         }
6806         if (!fp_access_check(s)) {
6807             return;
6808         }
6809         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6810         break;
6811     default:
6812         unallocated_encoding(s);
6813     }
6814 }
6815 
6816 /* Floating point immediate
6817  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6818  * +---+---+---+-----------+------+---+------------+-------+------+------+
6819  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6820  * +---+---+---+-----------+------+---+------------+-------+------+------+
6821  */
6822 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6823 {
6824     int rd = extract32(insn, 0, 5);
6825     int imm5 = extract32(insn, 5, 5);
6826     int imm8 = extract32(insn, 13, 8);
6827     int type = extract32(insn, 22, 2);
6828     int mos = extract32(insn, 29, 3);
6829     uint64_t imm;
6830     MemOp sz;
6831 
6832     if (mos || imm5) {
6833         unallocated_encoding(s);
6834         return;
6835     }
6836 
6837     switch (type) {
6838     case 0:
6839         sz = MO_32;
6840         break;
6841     case 1:
6842         sz = MO_64;
6843         break;
6844     case 3:
6845         sz = MO_16;
6846         if (dc_isar_feature(aa64_fp16, s)) {
6847             break;
6848         }
6849         /* fallthru */
6850     default:
6851         unallocated_encoding(s);
6852         return;
6853     }
6854 
6855     if (!fp_access_check(s)) {
6856         return;
6857     }
6858 
6859     imm = vfp_expand_imm(sz, imm8);
6860     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6861 }
6862 
6863 /* Handle floating point <=> fixed point conversions. Note that we can
6864  * also deal with fp <=> integer conversions as a special case (scale == 64)
6865  * OPTME: consider handling that special case specially or at least skipping
6866  * the call to scalbn in the helpers for zero shifts.
6867  */
6868 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6869                            bool itof, int rmode, int scale, int sf, int type)
6870 {
6871     bool is_signed = !(opcode & 1);
6872     TCGv_ptr tcg_fpstatus;
6873     TCGv_i32 tcg_shift, tcg_single;
6874     TCGv_i64 tcg_double;
6875 
6876     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6877 
6878     tcg_shift = tcg_constant_i32(64 - scale);
6879 
6880     if (itof) {
6881         TCGv_i64 tcg_int = cpu_reg(s, rn);
6882         if (!sf) {
6883             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6884 
6885             if (is_signed) {
6886                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6887             } else {
6888                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6889             }
6890 
6891             tcg_int = tcg_extend;
6892         }
6893 
6894         switch (type) {
6895         case 1: /* float64 */
6896             tcg_double = tcg_temp_new_i64();
6897             if (is_signed) {
6898                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6899                                      tcg_shift, tcg_fpstatus);
6900             } else {
6901                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6902                                      tcg_shift, tcg_fpstatus);
6903             }
6904             write_fp_dreg(s, rd, tcg_double);
6905             break;
6906 
6907         case 0: /* float32 */
6908             tcg_single = tcg_temp_new_i32();
6909             if (is_signed) {
6910                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6911                                      tcg_shift, tcg_fpstatus);
6912             } else {
6913                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6914                                      tcg_shift, tcg_fpstatus);
6915             }
6916             write_fp_sreg(s, rd, tcg_single);
6917             break;
6918 
6919         case 3: /* float16 */
6920             tcg_single = tcg_temp_new_i32();
6921             if (is_signed) {
6922                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6923                                      tcg_shift, tcg_fpstatus);
6924             } else {
6925                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6926                                      tcg_shift, tcg_fpstatus);
6927             }
6928             write_fp_sreg(s, rd, tcg_single);
6929             break;
6930 
6931         default:
6932             g_assert_not_reached();
6933         }
6934     } else {
6935         TCGv_i64 tcg_int = cpu_reg(s, rd);
6936         TCGv_i32 tcg_rmode;
6937 
6938         if (extract32(opcode, 2, 1)) {
6939             /* There are too many rounding modes to all fit into rmode,
6940              * so FCVTA[US] is a special case.
6941              */
6942             rmode = FPROUNDING_TIEAWAY;
6943         }
6944 
6945         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6946 
6947         switch (type) {
6948         case 1: /* float64 */
6949             tcg_double = read_fp_dreg(s, rn);
6950             if (is_signed) {
6951                 if (!sf) {
6952                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6953                                          tcg_shift, tcg_fpstatus);
6954                 } else {
6955                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6956                                          tcg_shift, tcg_fpstatus);
6957                 }
6958             } else {
6959                 if (!sf) {
6960                     gen_helper_vfp_tould(tcg_int, tcg_double,
6961                                          tcg_shift, tcg_fpstatus);
6962                 } else {
6963                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6964                                          tcg_shift, tcg_fpstatus);
6965                 }
6966             }
6967             if (!sf) {
6968                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6969             }
6970             break;
6971 
6972         case 0: /* float32 */
6973             tcg_single = read_fp_sreg(s, rn);
6974             if (sf) {
6975                 if (is_signed) {
6976                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6977                                          tcg_shift, tcg_fpstatus);
6978                 } else {
6979                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6980                                          tcg_shift, tcg_fpstatus);
6981                 }
6982             } else {
6983                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6984                 if (is_signed) {
6985                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6986                                          tcg_shift, tcg_fpstatus);
6987                 } else {
6988                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6989                                          tcg_shift, tcg_fpstatus);
6990                 }
6991                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6992             }
6993             break;
6994 
6995         case 3: /* float16 */
6996             tcg_single = read_fp_sreg(s, rn);
6997             if (sf) {
6998                 if (is_signed) {
6999                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7000                                          tcg_shift, tcg_fpstatus);
7001                 } else {
7002                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7003                                          tcg_shift, tcg_fpstatus);
7004                 }
7005             } else {
7006                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7007                 if (is_signed) {
7008                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7009                                          tcg_shift, tcg_fpstatus);
7010                 } else {
7011                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7012                                          tcg_shift, tcg_fpstatus);
7013                 }
7014                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7015             }
7016             break;
7017 
7018         default:
7019             g_assert_not_reached();
7020         }
7021 
7022         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7023     }
7024 }
7025 
7026 /* Floating point <-> fixed point conversions
7027  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7028  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7029  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7030  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7031  */
7032 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7033 {
7034     int rd = extract32(insn, 0, 5);
7035     int rn = extract32(insn, 5, 5);
7036     int scale = extract32(insn, 10, 6);
7037     int opcode = extract32(insn, 16, 3);
7038     int rmode = extract32(insn, 19, 2);
7039     int type = extract32(insn, 22, 2);
7040     bool sbit = extract32(insn, 29, 1);
7041     bool sf = extract32(insn, 31, 1);
7042     bool itof;
7043 
7044     if (sbit || (!sf && scale < 32)) {
7045         unallocated_encoding(s);
7046         return;
7047     }
7048 
7049     switch (type) {
7050     case 0: /* float32 */
7051     case 1: /* float64 */
7052         break;
7053     case 3: /* float16 */
7054         if (dc_isar_feature(aa64_fp16, s)) {
7055             break;
7056         }
7057         /* fallthru */
7058     default:
7059         unallocated_encoding(s);
7060         return;
7061     }
7062 
7063     switch ((rmode << 3) | opcode) {
7064     case 0x2: /* SCVTF */
7065     case 0x3: /* UCVTF */
7066         itof = true;
7067         break;
7068     case 0x18: /* FCVTZS */
7069     case 0x19: /* FCVTZU */
7070         itof = false;
7071         break;
7072     default:
7073         unallocated_encoding(s);
7074         return;
7075     }
7076 
7077     if (!fp_access_check(s)) {
7078         return;
7079     }
7080 
7081     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7082 }
7083 
7084 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7085 {
7086     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7087      * without conversion.
7088      */
7089 
7090     if (itof) {
7091         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7092         TCGv_i64 tmp;
7093 
7094         switch (type) {
7095         case 0:
7096             /* 32 bit */
7097             tmp = tcg_temp_new_i64();
7098             tcg_gen_ext32u_i64(tmp, tcg_rn);
7099             write_fp_dreg(s, rd, tmp);
7100             break;
7101         case 1:
7102             /* 64 bit */
7103             write_fp_dreg(s, rd, tcg_rn);
7104             break;
7105         case 2:
7106             /* 64 bit to top half. */
7107             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7108             clear_vec_high(s, true, rd);
7109             break;
7110         case 3:
7111             /* 16 bit */
7112             tmp = tcg_temp_new_i64();
7113             tcg_gen_ext16u_i64(tmp, tcg_rn);
7114             write_fp_dreg(s, rd, tmp);
7115             break;
7116         default:
7117             g_assert_not_reached();
7118         }
7119     } else {
7120         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7121 
7122         switch (type) {
7123         case 0:
7124             /* 32 bit */
7125             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7126             break;
7127         case 1:
7128             /* 64 bit */
7129             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7130             break;
7131         case 2:
7132             /* 64 bits from top half */
7133             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7134             break;
7135         case 3:
7136             /* 16 bit */
7137             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7138             break;
7139         default:
7140             g_assert_not_reached();
7141         }
7142     }
7143 }
7144 
7145 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7146 {
7147     TCGv_i64 t = read_fp_dreg(s, rn);
7148     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7149 
7150     gen_helper_fjcvtzs(t, t, fpstatus);
7151 
7152     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7153     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7154     tcg_gen_movi_i32(cpu_CF, 0);
7155     tcg_gen_movi_i32(cpu_NF, 0);
7156     tcg_gen_movi_i32(cpu_VF, 0);
7157 }
7158 
7159 /* Floating point <-> integer conversions
7160  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7161  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7162  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7163  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7164  */
7165 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7166 {
7167     int rd = extract32(insn, 0, 5);
7168     int rn = extract32(insn, 5, 5);
7169     int opcode = extract32(insn, 16, 3);
7170     int rmode = extract32(insn, 19, 2);
7171     int type = extract32(insn, 22, 2);
7172     bool sbit = extract32(insn, 29, 1);
7173     bool sf = extract32(insn, 31, 1);
7174     bool itof = false;
7175 
7176     if (sbit) {
7177         goto do_unallocated;
7178     }
7179 
7180     switch (opcode) {
7181     case 2: /* SCVTF */
7182     case 3: /* UCVTF */
7183         itof = true;
7184         /* fallthru */
7185     case 4: /* FCVTAS */
7186     case 5: /* FCVTAU */
7187         if (rmode != 0) {
7188             goto do_unallocated;
7189         }
7190         /* fallthru */
7191     case 0: /* FCVT[NPMZ]S */
7192     case 1: /* FCVT[NPMZ]U */
7193         switch (type) {
7194         case 0: /* float32 */
7195         case 1: /* float64 */
7196             break;
7197         case 3: /* float16 */
7198             if (!dc_isar_feature(aa64_fp16, s)) {
7199                 goto do_unallocated;
7200             }
7201             break;
7202         default:
7203             goto do_unallocated;
7204         }
7205         if (!fp_access_check(s)) {
7206             return;
7207         }
7208         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7209         break;
7210 
7211     default:
7212         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7213         case 0b01100110: /* FMOV half <-> 32-bit int */
7214         case 0b01100111:
7215         case 0b11100110: /* FMOV half <-> 64-bit int */
7216         case 0b11100111:
7217             if (!dc_isar_feature(aa64_fp16, s)) {
7218                 goto do_unallocated;
7219             }
7220             /* fallthru */
7221         case 0b00000110: /* FMOV 32-bit */
7222         case 0b00000111:
7223         case 0b10100110: /* FMOV 64-bit */
7224         case 0b10100111:
7225         case 0b11001110: /* FMOV top half of 128-bit */
7226         case 0b11001111:
7227             if (!fp_access_check(s)) {
7228                 return;
7229             }
7230             itof = opcode & 1;
7231             handle_fmov(s, rd, rn, type, itof);
7232             break;
7233 
7234         case 0b00111110: /* FJCVTZS */
7235             if (!dc_isar_feature(aa64_jscvt, s)) {
7236                 goto do_unallocated;
7237             } else if (fp_access_check(s)) {
7238                 handle_fjcvtzs(s, rd, rn);
7239             }
7240             break;
7241 
7242         default:
7243         do_unallocated:
7244             unallocated_encoding(s);
7245             return;
7246         }
7247         break;
7248     }
7249 }
7250 
7251 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7252  *   31  30  29 28     25 24                          0
7253  * +---+---+---+---------+-----------------------------+
7254  * |   | 0 |   | 1 1 1 1 |                             |
7255  * +---+---+---+---------+-----------------------------+
7256  */
7257 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7258 {
7259     if (extract32(insn, 24, 1)) {
7260         /* Floating point data-processing (3 source) */
7261         disas_fp_3src(s, insn);
7262     } else if (extract32(insn, 21, 1) == 0) {
7263         /* Floating point to fixed point conversions */
7264         disas_fp_fixed_conv(s, insn);
7265     } else {
7266         switch (extract32(insn, 10, 2)) {
7267         case 1:
7268             /* Floating point conditional compare */
7269             disas_fp_ccomp(s, insn);
7270             break;
7271         case 2:
7272             /* Floating point data-processing (2 source) */
7273             disas_fp_2src(s, insn);
7274             break;
7275         case 3:
7276             /* Floating point conditional select */
7277             disas_fp_csel(s, insn);
7278             break;
7279         case 0:
7280             switch (ctz32(extract32(insn, 12, 4))) {
7281             case 0: /* [15:12] == xxx1 */
7282                 /* Floating point immediate */
7283                 disas_fp_imm(s, insn);
7284                 break;
7285             case 1: /* [15:12] == xx10 */
7286                 /* Floating point compare */
7287                 disas_fp_compare(s, insn);
7288                 break;
7289             case 2: /* [15:12] == x100 */
7290                 /* Floating point data-processing (1 source) */
7291                 disas_fp_1src(s, insn);
7292                 break;
7293             case 3: /* [15:12] == 1000 */
7294                 unallocated_encoding(s);
7295                 break;
7296             default: /* [15:12] == 0000 */
7297                 /* Floating point <-> integer conversions */
7298                 disas_fp_int_conv(s, insn);
7299                 break;
7300             }
7301             break;
7302         }
7303     }
7304 }
7305 
7306 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7307                      int pos)
7308 {
7309     /* Extract 64 bits from the middle of two concatenated 64 bit
7310      * vector register slices left:right. The extracted bits start
7311      * at 'pos' bits into the right (least significant) side.
7312      * We return the result in tcg_right, and guarantee not to
7313      * trash tcg_left.
7314      */
7315     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7316     assert(pos > 0 && pos < 64);
7317 
7318     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7319     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7320     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7321 }
7322 
7323 /* EXT
7324  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7325  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7326  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7327  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7328  */
7329 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7330 {
7331     int is_q = extract32(insn, 30, 1);
7332     int op2 = extract32(insn, 22, 2);
7333     int imm4 = extract32(insn, 11, 4);
7334     int rm = extract32(insn, 16, 5);
7335     int rn = extract32(insn, 5, 5);
7336     int rd = extract32(insn, 0, 5);
7337     int pos = imm4 << 3;
7338     TCGv_i64 tcg_resl, tcg_resh;
7339 
7340     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7341         unallocated_encoding(s);
7342         return;
7343     }
7344 
7345     if (!fp_access_check(s)) {
7346         return;
7347     }
7348 
7349     tcg_resh = tcg_temp_new_i64();
7350     tcg_resl = tcg_temp_new_i64();
7351 
7352     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7353      * either extracting 128 bits from a 128:128 concatenation, or
7354      * extracting 64 bits from a 64:64 concatenation.
7355      */
7356     if (!is_q) {
7357         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7358         if (pos != 0) {
7359             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7360             do_ext64(s, tcg_resh, tcg_resl, pos);
7361         }
7362     } else {
7363         TCGv_i64 tcg_hh;
7364         typedef struct {
7365             int reg;
7366             int elt;
7367         } EltPosns;
7368         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7369         EltPosns *elt = eltposns;
7370 
7371         if (pos >= 64) {
7372             elt++;
7373             pos -= 64;
7374         }
7375 
7376         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7377         elt++;
7378         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7379         elt++;
7380         if (pos != 0) {
7381             do_ext64(s, tcg_resh, tcg_resl, pos);
7382             tcg_hh = tcg_temp_new_i64();
7383             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7384             do_ext64(s, tcg_hh, tcg_resh, pos);
7385         }
7386     }
7387 
7388     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7389     if (is_q) {
7390         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7391     }
7392     clear_vec_high(s, is_q, rd);
7393 }
7394 
7395 /* TBL/TBX
7396  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7397  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7398  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7399  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7400  */
7401 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7402 {
7403     int op2 = extract32(insn, 22, 2);
7404     int is_q = extract32(insn, 30, 1);
7405     int rm = extract32(insn, 16, 5);
7406     int rn = extract32(insn, 5, 5);
7407     int rd = extract32(insn, 0, 5);
7408     int is_tbx = extract32(insn, 12, 1);
7409     int len = (extract32(insn, 13, 2) + 1) * 16;
7410 
7411     if (op2 != 0) {
7412         unallocated_encoding(s);
7413         return;
7414     }
7415 
7416     if (!fp_access_check(s)) {
7417         return;
7418     }
7419 
7420     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7421                        vec_full_reg_offset(s, rm), cpu_env,
7422                        is_q ? 16 : 8, vec_full_reg_size(s),
7423                        (len << 6) | (is_tbx << 5) | rn,
7424                        gen_helper_simd_tblx);
7425 }
7426 
7427 /* ZIP/UZP/TRN
7428  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7429  * +---+---+-------------+------+---+------+---+------------------+------+
7430  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7431  * +---+---+-------------+------+---+------+---+------------------+------+
7432  */
7433 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7434 {
7435     int rd = extract32(insn, 0, 5);
7436     int rn = extract32(insn, 5, 5);
7437     int rm = extract32(insn, 16, 5);
7438     int size = extract32(insn, 22, 2);
7439     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7440      * bit 2 indicates 1 vs 2 variant of the insn.
7441      */
7442     int opcode = extract32(insn, 12, 2);
7443     bool part = extract32(insn, 14, 1);
7444     bool is_q = extract32(insn, 30, 1);
7445     int esize = 8 << size;
7446     int i;
7447     int datasize = is_q ? 128 : 64;
7448     int elements = datasize / esize;
7449     TCGv_i64 tcg_res[2], tcg_ele;
7450 
7451     if (opcode == 0 || (size == 3 && !is_q)) {
7452         unallocated_encoding(s);
7453         return;
7454     }
7455 
7456     if (!fp_access_check(s)) {
7457         return;
7458     }
7459 
7460     tcg_res[0] = tcg_temp_new_i64();
7461     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7462     tcg_ele = tcg_temp_new_i64();
7463 
7464     for (i = 0; i < elements; i++) {
7465         int o, w;
7466 
7467         switch (opcode) {
7468         case 1: /* UZP1/2 */
7469         {
7470             int midpoint = elements / 2;
7471             if (i < midpoint) {
7472                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7473             } else {
7474                 read_vec_element(s, tcg_ele, rm,
7475                                  2 * (i - midpoint) + part, size);
7476             }
7477             break;
7478         }
7479         case 2: /* TRN1/2 */
7480             if (i & 1) {
7481                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7482             } else {
7483                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7484             }
7485             break;
7486         case 3: /* ZIP1/2 */
7487         {
7488             int base = part * elements / 2;
7489             if (i & 1) {
7490                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7491             } else {
7492                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7493             }
7494             break;
7495         }
7496         default:
7497             g_assert_not_reached();
7498         }
7499 
7500         w = (i * esize) / 64;
7501         o = (i * esize) % 64;
7502         if (o == 0) {
7503             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7504         } else {
7505             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7506             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7507         }
7508     }
7509 
7510     for (i = 0; i <= is_q; ++i) {
7511         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7512     }
7513     clear_vec_high(s, is_q, rd);
7514 }
7515 
7516 /*
7517  * do_reduction_op helper
7518  *
7519  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7520  * important for correct NaN propagation that we do these
7521  * operations in exactly the order specified by the pseudocode.
7522  *
7523  * This is a recursive function, TCG temps should be freed by the
7524  * calling function once it is done with the values.
7525  */
7526 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7527                                 int esize, int size, int vmap, TCGv_ptr fpst)
7528 {
7529     if (esize == size) {
7530         int element;
7531         MemOp msize = esize == 16 ? MO_16 : MO_32;
7532         TCGv_i32 tcg_elem;
7533 
7534         /* We should have one register left here */
7535         assert(ctpop8(vmap) == 1);
7536         element = ctz32(vmap);
7537         assert(element < 8);
7538 
7539         tcg_elem = tcg_temp_new_i32();
7540         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7541         return tcg_elem;
7542     } else {
7543         int bits = size / 2;
7544         int shift = ctpop8(vmap) / 2;
7545         int vmap_lo = (vmap >> shift) & vmap;
7546         int vmap_hi = (vmap & ~vmap_lo);
7547         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7548 
7549         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7550         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7551         tcg_res = tcg_temp_new_i32();
7552 
7553         switch (fpopcode) {
7554         case 0x0c: /* fmaxnmv half-precision */
7555             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7556             break;
7557         case 0x0f: /* fmaxv half-precision */
7558             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7559             break;
7560         case 0x1c: /* fminnmv half-precision */
7561             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7562             break;
7563         case 0x1f: /* fminv half-precision */
7564             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7565             break;
7566         case 0x2c: /* fmaxnmv */
7567             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7568             break;
7569         case 0x2f: /* fmaxv */
7570             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7571             break;
7572         case 0x3c: /* fminnmv */
7573             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7574             break;
7575         case 0x3f: /* fminv */
7576             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7577             break;
7578         default:
7579             g_assert_not_reached();
7580         }
7581         return tcg_res;
7582     }
7583 }
7584 
7585 /* AdvSIMD across lanes
7586  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7587  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7588  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7589  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7590  */
7591 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7592 {
7593     int rd = extract32(insn, 0, 5);
7594     int rn = extract32(insn, 5, 5);
7595     int size = extract32(insn, 22, 2);
7596     int opcode = extract32(insn, 12, 5);
7597     bool is_q = extract32(insn, 30, 1);
7598     bool is_u = extract32(insn, 29, 1);
7599     bool is_fp = false;
7600     bool is_min = false;
7601     int esize;
7602     int elements;
7603     int i;
7604     TCGv_i64 tcg_res, tcg_elt;
7605 
7606     switch (opcode) {
7607     case 0x1b: /* ADDV */
7608         if (is_u) {
7609             unallocated_encoding(s);
7610             return;
7611         }
7612         /* fall through */
7613     case 0x3: /* SADDLV, UADDLV */
7614     case 0xa: /* SMAXV, UMAXV */
7615     case 0x1a: /* SMINV, UMINV */
7616         if (size == 3 || (size == 2 && !is_q)) {
7617             unallocated_encoding(s);
7618             return;
7619         }
7620         break;
7621     case 0xc: /* FMAXNMV, FMINNMV */
7622     case 0xf: /* FMAXV, FMINV */
7623         /* Bit 1 of size field encodes min vs max and the actual size
7624          * depends on the encoding of the U bit. If not set (and FP16
7625          * enabled) then we do half-precision float instead of single
7626          * precision.
7627          */
7628         is_min = extract32(size, 1, 1);
7629         is_fp = true;
7630         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7631             size = 1;
7632         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7633             unallocated_encoding(s);
7634             return;
7635         } else {
7636             size = 2;
7637         }
7638         break;
7639     default:
7640         unallocated_encoding(s);
7641         return;
7642     }
7643 
7644     if (!fp_access_check(s)) {
7645         return;
7646     }
7647 
7648     esize = 8 << size;
7649     elements = (is_q ? 128 : 64) / esize;
7650 
7651     tcg_res = tcg_temp_new_i64();
7652     tcg_elt = tcg_temp_new_i64();
7653 
7654     /* These instructions operate across all lanes of a vector
7655      * to produce a single result. We can guarantee that a 64
7656      * bit intermediate is sufficient:
7657      *  + for [US]ADDLV the maximum element size is 32 bits, and
7658      *    the result type is 64 bits
7659      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7660      *    same as the element size, which is 32 bits at most
7661      * For the integer operations we can choose to work at 64
7662      * or 32 bits and truncate at the end; for simplicity
7663      * we use 64 bits always. The floating point
7664      * ops do require 32 bit intermediates, though.
7665      */
7666     if (!is_fp) {
7667         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7668 
7669         for (i = 1; i < elements; i++) {
7670             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7671 
7672             switch (opcode) {
7673             case 0x03: /* SADDLV / UADDLV */
7674             case 0x1b: /* ADDV */
7675                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7676                 break;
7677             case 0x0a: /* SMAXV / UMAXV */
7678                 if (is_u) {
7679                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7680                 } else {
7681                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7682                 }
7683                 break;
7684             case 0x1a: /* SMINV / UMINV */
7685                 if (is_u) {
7686                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7687                 } else {
7688                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7689                 }
7690                 break;
7691             default:
7692                 g_assert_not_reached();
7693             }
7694 
7695         }
7696     } else {
7697         /* Floating point vector reduction ops which work across 32
7698          * bit (single) or 16 bit (half-precision) intermediates.
7699          * Note that correct NaN propagation requires that we do these
7700          * operations in exactly the order specified by the pseudocode.
7701          */
7702         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7703         int fpopcode = opcode | is_min << 4 | is_u << 5;
7704         int vmap = (1 << elements) - 1;
7705         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7706                                              (is_q ? 128 : 64), vmap, fpst);
7707         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7708     }
7709 
7710     /* Now truncate the result to the width required for the final output */
7711     if (opcode == 0x03) {
7712         /* SADDLV, UADDLV: result is 2*esize */
7713         size++;
7714     }
7715 
7716     switch (size) {
7717     case 0:
7718         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7719         break;
7720     case 1:
7721         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7722         break;
7723     case 2:
7724         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7725         break;
7726     case 3:
7727         break;
7728     default:
7729         g_assert_not_reached();
7730     }
7731 
7732     write_fp_dreg(s, rd, tcg_res);
7733 }
7734 
7735 /* DUP (Element, Vector)
7736  *
7737  *  31  30   29              21 20    16 15        10  9    5 4    0
7738  * +---+---+-------------------+--------+-------------+------+------+
7739  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7740  * +---+---+-------------------+--------+-------------+------+------+
7741  *
7742  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7743  */
7744 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7745                              int imm5)
7746 {
7747     int size = ctz32(imm5);
7748     int index;
7749 
7750     if (size > 3 || (size == 3 && !is_q)) {
7751         unallocated_encoding(s);
7752         return;
7753     }
7754 
7755     if (!fp_access_check(s)) {
7756         return;
7757     }
7758 
7759     index = imm5 >> (size + 1);
7760     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7761                          vec_reg_offset(s, rn, index, size),
7762                          is_q ? 16 : 8, vec_full_reg_size(s));
7763 }
7764 
7765 /* DUP (element, scalar)
7766  *  31                   21 20    16 15        10  9    5 4    0
7767  * +-----------------------+--------+-------------+------+------+
7768  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7769  * +-----------------------+--------+-------------+------+------+
7770  */
7771 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7772                               int imm5)
7773 {
7774     int size = ctz32(imm5);
7775     int index;
7776     TCGv_i64 tmp;
7777 
7778     if (size > 3) {
7779         unallocated_encoding(s);
7780         return;
7781     }
7782 
7783     if (!fp_access_check(s)) {
7784         return;
7785     }
7786 
7787     index = imm5 >> (size + 1);
7788 
7789     /* This instruction just extracts the specified element and
7790      * zero-extends it into the bottom of the destination register.
7791      */
7792     tmp = tcg_temp_new_i64();
7793     read_vec_element(s, tmp, rn, index, size);
7794     write_fp_dreg(s, rd, tmp);
7795 }
7796 
7797 /* DUP (General)
7798  *
7799  *  31  30   29              21 20    16 15        10  9    5 4    0
7800  * +---+---+-------------------+--------+-------------+------+------+
7801  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7802  * +---+---+-------------------+--------+-------------+------+------+
7803  *
7804  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7805  */
7806 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7807                              int imm5)
7808 {
7809     int size = ctz32(imm5);
7810     uint32_t dofs, oprsz, maxsz;
7811 
7812     if (size > 3 || ((size == 3) && !is_q)) {
7813         unallocated_encoding(s);
7814         return;
7815     }
7816 
7817     if (!fp_access_check(s)) {
7818         return;
7819     }
7820 
7821     dofs = vec_full_reg_offset(s, rd);
7822     oprsz = is_q ? 16 : 8;
7823     maxsz = vec_full_reg_size(s);
7824 
7825     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7826 }
7827 
7828 /* INS (Element)
7829  *
7830  *  31                   21 20    16 15  14    11  10 9    5 4    0
7831  * +-----------------------+--------+------------+---+------+------+
7832  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7833  * +-----------------------+--------+------------+---+------+------+
7834  *
7835  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7836  * index: encoded in imm5<4:size+1>
7837  */
7838 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7839                              int imm4, int imm5)
7840 {
7841     int size = ctz32(imm5);
7842     int src_index, dst_index;
7843     TCGv_i64 tmp;
7844 
7845     if (size > 3) {
7846         unallocated_encoding(s);
7847         return;
7848     }
7849 
7850     if (!fp_access_check(s)) {
7851         return;
7852     }
7853 
7854     dst_index = extract32(imm5, 1+size, 5);
7855     src_index = extract32(imm4, size, 4);
7856 
7857     tmp = tcg_temp_new_i64();
7858 
7859     read_vec_element(s, tmp, rn, src_index, size);
7860     write_vec_element(s, tmp, rd, dst_index, size);
7861 
7862     /* INS is considered a 128-bit write for SVE. */
7863     clear_vec_high(s, true, rd);
7864 }
7865 
7866 
7867 /* INS (General)
7868  *
7869  *  31                   21 20    16 15        10  9    5 4    0
7870  * +-----------------------+--------+-------------+------+------+
7871  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7872  * +-----------------------+--------+-------------+------+------+
7873  *
7874  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7875  * index: encoded in imm5<4:size+1>
7876  */
7877 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7878 {
7879     int size = ctz32(imm5);
7880     int idx;
7881 
7882     if (size > 3) {
7883         unallocated_encoding(s);
7884         return;
7885     }
7886 
7887     if (!fp_access_check(s)) {
7888         return;
7889     }
7890 
7891     idx = extract32(imm5, 1 + size, 4 - size);
7892     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7893 
7894     /* INS is considered a 128-bit write for SVE. */
7895     clear_vec_high(s, true, rd);
7896 }
7897 
7898 /*
7899  * UMOV (General)
7900  * SMOV (General)
7901  *
7902  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7903  * +---+---+-------------------+--------+-------------+------+------+
7904  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7905  * +---+---+-------------------+--------+-------------+------+------+
7906  *
7907  * U: unsigned when set
7908  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7909  */
7910 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7911                                   int rn, int rd, int imm5)
7912 {
7913     int size = ctz32(imm5);
7914     int element;
7915     TCGv_i64 tcg_rd;
7916 
7917     /* Check for UnallocatedEncodings */
7918     if (is_signed) {
7919         if (size > 2 || (size == 2 && !is_q)) {
7920             unallocated_encoding(s);
7921             return;
7922         }
7923     } else {
7924         if (size > 3
7925             || (size < 3 && is_q)
7926             || (size == 3 && !is_q)) {
7927             unallocated_encoding(s);
7928             return;
7929         }
7930     }
7931 
7932     if (!fp_access_check(s)) {
7933         return;
7934     }
7935 
7936     element = extract32(imm5, 1+size, 4);
7937 
7938     tcg_rd = cpu_reg(s, rd);
7939     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7940     if (is_signed && !is_q) {
7941         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7942     }
7943 }
7944 
7945 /* AdvSIMD copy
7946  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7947  * +---+---+----+-----------------+------+---+------+---+------+------+
7948  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7949  * +---+---+----+-----------------+------+---+------+---+------+------+
7950  */
7951 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7952 {
7953     int rd = extract32(insn, 0, 5);
7954     int rn = extract32(insn, 5, 5);
7955     int imm4 = extract32(insn, 11, 4);
7956     int op = extract32(insn, 29, 1);
7957     int is_q = extract32(insn, 30, 1);
7958     int imm5 = extract32(insn, 16, 5);
7959 
7960     if (op) {
7961         if (is_q) {
7962             /* INS (element) */
7963             handle_simd_inse(s, rd, rn, imm4, imm5);
7964         } else {
7965             unallocated_encoding(s);
7966         }
7967     } else {
7968         switch (imm4) {
7969         case 0:
7970             /* DUP (element - vector) */
7971             handle_simd_dupe(s, is_q, rd, rn, imm5);
7972             break;
7973         case 1:
7974             /* DUP (general) */
7975             handle_simd_dupg(s, is_q, rd, rn, imm5);
7976             break;
7977         case 3:
7978             if (is_q) {
7979                 /* INS (general) */
7980                 handle_simd_insg(s, rd, rn, imm5);
7981             } else {
7982                 unallocated_encoding(s);
7983             }
7984             break;
7985         case 5:
7986         case 7:
7987             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7988             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7989             break;
7990         default:
7991             unallocated_encoding(s);
7992             break;
7993         }
7994     }
7995 }
7996 
7997 /* AdvSIMD modified immediate
7998  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7999  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8000  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8001  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8002  *
8003  * There are a number of operations that can be carried out here:
8004  *   MOVI - move (shifted) imm into register
8005  *   MVNI - move inverted (shifted) imm into register
8006  *   ORR  - bitwise OR of (shifted) imm with register
8007  *   BIC  - bitwise clear of (shifted) imm with register
8008  * With ARMv8.2 we also have:
8009  *   FMOV half-precision
8010  */
8011 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8012 {
8013     int rd = extract32(insn, 0, 5);
8014     int cmode = extract32(insn, 12, 4);
8015     int o2 = extract32(insn, 11, 1);
8016     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8017     bool is_neg = extract32(insn, 29, 1);
8018     bool is_q = extract32(insn, 30, 1);
8019     uint64_t imm = 0;
8020 
8021     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8022         /* Check for FMOV (vector, immediate) - half-precision */
8023         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8024             unallocated_encoding(s);
8025             return;
8026         }
8027     }
8028 
8029     if (!fp_access_check(s)) {
8030         return;
8031     }
8032 
8033     if (cmode == 15 && o2 && !is_neg) {
8034         /* FMOV (vector, immediate) - half-precision */
8035         imm = vfp_expand_imm(MO_16, abcdefgh);
8036         /* now duplicate across the lanes */
8037         imm = dup_const(MO_16, imm);
8038     } else {
8039         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8040     }
8041 
8042     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8043         /* MOVI or MVNI, with MVNI negation handled above.  */
8044         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8045                              vec_full_reg_size(s), imm);
8046     } else {
8047         /* ORR or BIC, with BIC negation to AND handled above.  */
8048         if (is_neg) {
8049             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8050         } else {
8051             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8052         }
8053     }
8054 }
8055 
8056 /* AdvSIMD scalar copy
8057  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8058  * +-----+----+-----------------+------+---+------+---+------+------+
8059  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8060  * +-----+----+-----------------+------+---+------+---+------+------+
8061  */
8062 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8063 {
8064     int rd = extract32(insn, 0, 5);
8065     int rn = extract32(insn, 5, 5);
8066     int imm4 = extract32(insn, 11, 4);
8067     int imm5 = extract32(insn, 16, 5);
8068     int op = extract32(insn, 29, 1);
8069 
8070     if (op != 0 || imm4 != 0) {
8071         unallocated_encoding(s);
8072         return;
8073     }
8074 
8075     /* DUP (element, scalar) */
8076     handle_simd_dupes(s, rd, rn, imm5);
8077 }
8078 
8079 /* AdvSIMD scalar pairwise
8080  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8081  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8082  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8083  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8084  */
8085 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8086 {
8087     int u = extract32(insn, 29, 1);
8088     int size = extract32(insn, 22, 2);
8089     int opcode = extract32(insn, 12, 5);
8090     int rn = extract32(insn, 5, 5);
8091     int rd = extract32(insn, 0, 5);
8092     TCGv_ptr fpst;
8093 
8094     /* For some ops (the FP ones), size[1] is part of the encoding.
8095      * For ADDP strictly it is not but size[1] is always 1 for valid
8096      * encodings.
8097      */
8098     opcode |= (extract32(size, 1, 1) << 5);
8099 
8100     switch (opcode) {
8101     case 0x3b: /* ADDP */
8102         if (u || size != 3) {
8103             unallocated_encoding(s);
8104             return;
8105         }
8106         if (!fp_access_check(s)) {
8107             return;
8108         }
8109 
8110         fpst = NULL;
8111         break;
8112     case 0xc: /* FMAXNMP */
8113     case 0xd: /* FADDP */
8114     case 0xf: /* FMAXP */
8115     case 0x2c: /* FMINNMP */
8116     case 0x2f: /* FMINP */
8117         /* FP op, size[0] is 32 or 64 bit*/
8118         if (!u) {
8119             if (!dc_isar_feature(aa64_fp16, s)) {
8120                 unallocated_encoding(s);
8121                 return;
8122             } else {
8123                 size = MO_16;
8124             }
8125         } else {
8126             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8127         }
8128 
8129         if (!fp_access_check(s)) {
8130             return;
8131         }
8132 
8133         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8134         break;
8135     default:
8136         unallocated_encoding(s);
8137         return;
8138     }
8139 
8140     if (size == MO_64) {
8141         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8142         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8143         TCGv_i64 tcg_res = tcg_temp_new_i64();
8144 
8145         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8146         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8147 
8148         switch (opcode) {
8149         case 0x3b: /* ADDP */
8150             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8151             break;
8152         case 0xc: /* FMAXNMP */
8153             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8154             break;
8155         case 0xd: /* FADDP */
8156             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8157             break;
8158         case 0xf: /* FMAXP */
8159             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8160             break;
8161         case 0x2c: /* FMINNMP */
8162             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8163             break;
8164         case 0x2f: /* FMINP */
8165             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8166             break;
8167         default:
8168             g_assert_not_reached();
8169         }
8170 
8171         write_fp_dreg(s, rd, tcg_res);
8172     } else {
8173         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8174         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8175         TCGv_i32 tcg_res = tcg_temp_new_i32();
8176 
8177         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8178         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8179 
8180         if (size == MO_16) {
8181             switch (opcode) {
8182             case 0xc: /* FMAXNMP */
8183                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8184                 break;
8185             case 0xd: /* FADDP */
8186                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8187                 break;
8188             case 0xf: /* FMAXP */
8189                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8190                 break;
8191             case 0x2c: /* FMINNMP */
8192                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8193                 break;
8194             case 0x2f: /* FMINP */
8195                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8196                 break;
8197             default:
8198                 g_assert_not_reached();
8199             }
8200         } else {
8201             switch (opcode) {
8202             case 0xc: /* FMAXNMP */
8203                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8204                 break;
8205             case 0xd: /* FADDP */
8206                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8207                 break;
8208             case 0xf: /* FMAXP */
8209                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8210                 break;
8211             case 0x2c: /* FMINNMP */
8212                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8213                 break;
8214             case 0x2f: /* FMINP */
8215                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8216                 break;
8217             default:
8218                 g_assert_not_reached();
8219             }
8220         }
8221 
8222         write_fp_sreg(s, rd, tcg_res);
8223     }
8224 }
8225 
8226 /*
8227  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8228  *
8229  * This code is handles the common shifting code and is used by both
8230  * the vector and scalar code.
8231  */
8232 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8233                                     TCGv_i64 tcg_rnd, bool accumulate,
8234                                     bool is_u, int size, int shift)
8235 {
8236     bool extended_result = false;
8237     bool round = tcg_rnd != NULL;
8238     int ext_lshift = 0;
8239     TCGv_i64 tcg_src_hi;
8240 
8241     if (round && size == 3) {
8242         extended_result = true;
8243         ext_lshift = 64 - shift;
8244         tcg_src_hi = tcg_temp_new_i64();
8245     } else if (shift == 64) {
8246         if (!accumulate && is_u) {
8247             /* result is zero */
8248             tcg_gen_movi_i64(tcg_res, 0);
8249             return;
8250         }
8251     }
8252 
8253     /* Deal with the rounding step */
8254     if (round) {
8255         if (extended_result) {
8256             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8257             if (!is_u) {
8258                 /* take care of sign extending tcg_res */
8259                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8260                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8261                                  tcg_src, tcg_src_hi,
8262                                  tcg_rnd, tcg_zero);
8263             } else {
8264                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8265                                  tcg_src, tcg_zero,
8266                                  tcg_rnd, tcg_zero);
8267             }
8268         } else {
8269             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8270         }
8271     }
8272 
8273     /* Now do the shift right */
8274     if (round && extended_result) {
8275         /* extended case, >64 bit precision required */
8276         if (ext_lshift == 0) {
8277             /* special case, only high bits matter */
8278             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8279         } else {
8280             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8281             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8282             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8283         }
8284     } else {
8285         if (is_u) {
8286             if (shift == 64) {
8287                 /* essentially shifting in 64 zeros */
8288                 tcg_gen_movi_i64(tcg_src, 0);
8289             } else {
8290                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8291             }
8292         } else {
8293             if (shift == 64) {
8294                 /* effectively extending the sign-bit */
8295                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8296             } else {
8297                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8298             }
8299         }
8300     }
8301 
8302     if (accumulate) {
8303         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8304     } else {
8305         tcg_gen_mov_i64(tcg_res, tcg_src);
8306     }
8307 }
8308 
8309 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8310 static void handle_scalar_simd_shri(DisasContext *s,
8311                                     bool is_u, int immh, int immb,
8312                                     int opcode, int rn, int rd)
8313 {
8314     const int size = 3;
8315     int immhb = immh << 3 | immb;
8316     int shift = 2 * (8 << size) - immhb;
8317     bool accumulate = false;
8318     bool round = false;
8319     bool insert = false;
8320     TCGv_i64 tcg_rn;
8321     TCGv_i64 tcg_rd;
8322     TCGv_i64 tcg_round;
8323 
8324     if (!extract32(immh, 3, 1)) {
8325         unallocated_encoding(s);
8326         return;
8327     }
8328 
8329     if (!fp_access_check(s)) {
8330         return;
8331     }
8332 
8333     switch (opcode) {
8334     case 0x02: /* SSRA / USRA (accumulate) */
8335         accumulate = true;
8336         break;
8337     case 0x04: /* SRSHR / URSHR (rounding) */
8338         round = true;
8339         break;
8340     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8341         accumulate = round = true;
8342         break;
8343     case 0x08: /* SRI */
8344         insert = true;
8345         break;
8346     }
8347 
8348     if (round) {
8349         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8350     } else {
8351         tcg_round = NULL;
8352     }
8353 
8354     tcg_rn = read_fp_dreg(s, rn);
8355     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8356 
8357     if (insert) {
8358         /* shift count same as element size is valid but does nothing;
8359          * special case to avoid potential shift by 64.
8360          */
8361         int esize = 8 << size;
8362         if (shift != esize) {
8363             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8364             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8365         }
8366     } else {
8367         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8368                                 accumulate, is_u, size, shift);
8369     }
8370 
8371     write_fp_dreg(s, rd, tcg_rd);
8372 }
8373 
8374 /* SHL/SLI - Scalar shift left */
8375 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8376                                     int immh, int immb, int opcode,
8377                                     int rn, int rd)
8378 {
8379     int size = 32 - clz32(immh) - 1;
8380     int immhb = immh << 3 | immb;
8381     int shift = immhb - (8 << size);
8382     TCGv_i64 tcg_rn;
8383     TCGv_i64 tcg_rd;
8384 
8385     if (!extract32(immh, 3, 1)) {
8386         unallocated_encoding(s);
8387         return;
8388     }
8389 
8390     if (!fp_access_check(s)) {
8391         return;
8392     }
8393 
8394     tcg_rn = read_fp_dreg(s, rn);
8395     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8396 
8397     if (insert) {
8398         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8399     } else {
8400         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8401     }
8402 
8403     write_fp_dreg(s, rd, tcg_rd);
8404 }
8405 
8406 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8407  * (signed/unsigned) narrowing */
8408 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8409                                    bool is_u_shift, bool is_u_narrow,
8410                                    int immh, int immb, int opcode,
8411                                    int rn, int rd)
8412 {
8413     int immhb = immh << 3 | immb;
8414     int size = 32 - clz32(immh) - 1;
8415     int esize = 8 << size;
8416     int shift = (2 * esize) - immhb;
8417     int elements = is_scalar ? 1 : (64 / esize);
8418     bool round = extract32(opcode, 0, 1);
8419     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8420     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8421     TCGv_i32 tcg_rd_narrowed;
8422     TCGv_i64 tcg_final;
8423 
8424     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8425         { gen_helper_neon_narrow_sat_s8,
8426           gen_helper_neon_unarrow_sat8 },
8427         { gen_helper_neon_narrow_sat_s16,
8428           gen_helper_neon_unarrow_sat16 },
8429         { gen_helper_neon_narrow_sat_s32,
8430           gen_helper_neon_unarrow_sat32 },
8431         { NULL, NULL },
8432     };
8433     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8434         gen_helper_neon_narrow_sat_u8,
8435         gen_helper_neon_narrow_sat_u16,
8436         gen_helper_neon_narrow_sat_u32,
8437         NULL
8438     };
8439     NeonGenNarrowEnvFn *narrowfn;
8440 
8441     int i;
8442 
8443     assert(size < 4);
8444 
8445     if (extract32(immh, 3, 1)) {
8446         unallocated_encoding(s);
8447         return;
8448     }
8449 
8450     if (!fp_access_check(s)) {
8451         return;
8452     }
8453 
8454     if (is_u_shift) {
8455         narrowfn = unsigned_narrow_fns[size];
8456     } else {
8457         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8458     }
8459 
8460     tcg_rn = tcg_temp_new_i64();
8461     tcg_rd = tcg_temp_new_i64();
8462     tcg_rd_narrowed = tcg_temp_new_i32();
8463     tcg_final = tcg_temp_new_i64();
8464 
8465     if (round) {
8466         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8467     } else {
8468         tcg_round = NULL;
8469     }
8470 
8471     for (i = 0; i < elements; i++) {
8472         read_vec_element(s, tcg_rn, rn, i, ldop);
8473         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8474                                 false, is_u_shift, size+1, shift);
8475         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8476         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8477         if (i == 0) {
8478             tcg_gen_mov_i64(tcg_final, tcg_rd);
8479         } else {
8480             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8481         }
8482     }
8483 
8484     if (!is_q) {
8485         write_vec_element(s, tcg_final, rd, 0, MO_64);
8486     } else {
8487         write_vec_element(s, tcg_final, rd, 1, MO_64);
8488     }
8489     clear_vec_high(s, is_q, rd);
8490 }
8491 
8492 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8493 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8494                              bool src_unsigned, bool dst_unsigned,
8495                              int immh, int immb, int rn, int rd)
8496 {
8497     int immhb = immh << 3 | immb;
8498     int size = 32 - clz32(immh) - 1;
8499     int shift = immhb - (8 << size);
8500     int pass;
8501 
8502     assert(immh != 0);
8503     assert(!(scalar && is_q));
8504 
8505     if (!scalar) {
8506         if (!is_q && extract32(immh, 3, 1)) {
8507             unallocated_encoding(s);
8508             return;
8509         }
8510 
8511         /* Since we use the variable-shift helpers we must
8512          * replicate the shift count into each element of
8513          * the tcg_shift value.
8514          */
8515         switch (size) {
8516         case 0:
8517             shift |= shift << 8;
8518             /* fall through */
8519         case 1:
8520             shift |= shift << 16;
8521             break;
8522         case 2:
8523         case 3:
8524             break;
8525         default:
8526             g_assert_not_reached();
8527         }
8528     }
8529 
8530     if (!fp_access_check(s)) {
8531         return;
8532     }
8533 
8534     if (size == 3) {
8535         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8536         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8537             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8538             { NULL, gen_helper_neon_qshl_u64 },
8539         };
8540         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8541         int maxpass = is_q ? 2 : 1;
8542 
8543         for (pass = 0; pass < maxpass; pass++) {
8544             TCGv_i64 tcg_op = tcg_temp_new_i64();
8545 
8546             read_vec_element(s, tcg_op, rn, pass, MO_64);
8547             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8548             write_vec_element(s, tcg_op, rd, pass, MO_64);
8549         }
8550         clear_vec_high(s, is_q, rd);
8551     } else {
8552         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8553         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8554             {
8555                 { gen_helper_neon_qshl_s8,
8556                   gen_helper_neon_qshl_s16,
8557                   gen_helper_neon_qshl_s32 },
8558                 { gen_helper_neon_qshlu_s8,
8559                   gen_helper_neon_qshlu_s16,
8560                   gen_helper_neon_qshlu_s32 }
8561             }, {
8562                 { NULL, NULL, NULL },
8563                 { gen_helper_neon_qshl_u8,
8564                   gen_helper_neon_qshl_u16,
8565                   gen_helper_neon_qshl_u32 }
8566             }
8567         };
8568         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8569         MemOp memop = scalar ? size : MO_32;
8570         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8571 
8572         for (pass = 0; pass < maxpass; pass++) {
8573             TCGv_i32 tcg_op = tcg_temp_new_i32();
8574 
8575             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8576             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8577             if (scalar) {
8578                 switch (size) {
8579                 case 0:
8580                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8581                     break;
8582                 case 1:
8583                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8584                     break;
8585                 case 2:
8586                     break;
8587                 default:
8588                     g_assert_not_reached();
8589                 }
8590                 write_fp_sreg(s, rd, tcg_op);
8591             } else {
8592                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8593             }
8594         }
8595 
8596         if (!scalar) {
8597             clear_vec_high(s, is_q, rd);
8598         }
8599     }
8600 }
8601 
8602 /* Common vector code for handling integer to FP conversion */
8603 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8604                                    int elements, int is_signed,
8605                                    int fracbits, int size)
8606 {
8607     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8608     TCGv_i32 tcg_shift = NULL;
8609 
8610     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8611     int pass;
8612 
8613     if (fracbits || size == MO_64) {
8614         tcg_shift = tcg_constant_i32(fracbits);
8615     }
8616 
8617     if (size == MO_64) {
8618         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8619         TCGv_i64 tcg_double = tcg_temp_new_i64();
8620 
8621         for (pass = 0; pass < elements; pass++) {
8622             read_vec_element(s, tcg_int64, rn, pass, mop);
8623 
8624             if (is_signed) {
8625                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8626                                      tcg_shift, tcg_fpst);
8627             } else {
8628                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8629                                      tcg_shift, tcg_fpst);
8630             }
8631             if (elements == 1) {
8632                 write_fp_dreg(s, rd, tcg_double);
8633             } else {
8634                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8635             }
8636         }
8637     } else {
8638         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8639         TCGv_i32 tcg_float = tcg_temp_new_i32();
8640 
8641         for (pass = 0; pass < elements; pass++) {
8642             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8643 
8644             switch (size) {
8645             case MO_32:
8646                 if (fracbits) {
8647                     if (is_signed) {
8648                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8649                                              tcg_shift, tcg_fpst);
8650                     } else {
8651                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8652                                              tcg_shift, tcg_fpst);
8653                     }
8654                 } else {
8655                     if (is_signed) {
8656                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8657                     } else {
8658                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8659                     }
8660                 }
8661                 break;
8662             case MO_16:
8663                 if (fracbits) {
8664                     if (is_signed) {
8665                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8666                                              tcg_shift, tcg_fpst);
8667                     } else {
8668                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8669                                              tcg_shift, tcg_fpst);
8670                     }
8671                 } else {
8672                     if (is_signed) {
8673                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8674                     } else {
8675                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8676                     }
8677                 }
8678                 break;
8679             default:
8680                 g_assert_not_reached();
8681             }
8682 
8683             if (elements == 1) {
8684                 write_fp_sreg(s, rd, tcg_float);
8685             } else {
8686                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8687             }
8688         }
8689     }
8690 
8691     clear_vec_high(s, elements << size == 16, rd);
8692 }
8693 
8694 /* UCVTF/SCVTF - Integer to FP conversion */
8695 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8696                                          bool is_q, bool is_u,
8697                                          int immh, int immb, int opcode,
8698                                          int rn, int rd)
8699 {
8700     int size, elements, fracbits;
8701     int immhb = immh << 3 | immb;
8702 
8703     if (immh & 8) {
8704         size = MO_64;
8705         if (!is_scalar && !is_q) {
8706             unallocated_encoding(s);
8707             return;
8708         }
8709     } else if (immh & 4) {
8710         size = MO_32;
8711     } else if (immh & 2) {
8712         size = MO_16;
8713         if (!dc_isar_feature(aa64_fp16, s)) {
8714             unallocated_encoding(s);
8715             return;
8716         }
8717     } else {
8718         /* immh == 0 would be a failure of the decode logic */
8719         g_assert(immh == 1);
8720         unallocated_encoding(s);
8721         return;
8722     }
8723 
8724     if (is_scalar) {
8725         elements = 1;
8726     } else {
8727         elements = (8 << is_q) >> size;
8728     }
8729     fracbits = (16 << size) - immhb;
8730 
8731     if (!fp_access_check(s)) {
8732         return;
8733     }
8734 
8735     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8736 }
8737 
8738 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8739 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8740                                          bool is_q, bool is_u,
8741                                          int immh, int immb, int rn, int rd)
8742 {
8743     int immhb = immh << 3 | immb;
8744     int pass, size, fracbits;
8745     TCGv_ptr tcg_fpstatus;
8746     TCGv_i32 tcg_rmode, tcg_shift;
8747 
8748     if (immh & 0x8) {
8749         size = MO_64;
8750         if (!is_scalar && !is_q) {
8751             unallocated_encoding(s);
8752             return;
8753         }
8754     } else if (immh & 0x4) {
8755         size = MO_32;
8756     } else if (immh & 0x2) {
8757         size = MO_16;
8758         if (!dc_isar_feature(aa64_fp16, s)) {
8759             unallocated_encoding(s);
8760             return;
8761         }
8762     } else {
8763         /* Should have split out AdvSIMD modified immediate earlier.  */
8764         assert(immh == 1);
8765         unallocated_encoding(s);
8766         return;
8767     }
8768 
8769     if (!fp_access_check(s)) {
8770         return;
8771     }
8772 
8773     assert(!(is_scalar && is_q));
8774 
8775     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8776     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8777     fracbits = (16 << size) - immhb;
8778     tcg_shift = tcg_constant_i32(fracbits);
8779 
8780     if (size == MO_64) {
8781         int maxpass = is_scalar ? 1 : 2;
8782 
8783         for (pass = 0; pass < maxpass; pass++) {
8784             TCGv_i64 tcg_op = tcg_temp_new_i64();
8785 
8786             read_vec_element(s, tcg_op, rn, pass, MO_64);
8787             if (is_u) {
8788                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8789             } else {
8790                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8791             }
8792             write_vec_element(s, tcg_op, rd, pass, MO_64);
8793         }
8794         clear_vec_high(s, is_q, rd);
8795     } else {
8796         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8797         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8798 
8799         switch (size) {
8800         case MO_16:
8801             if (is_u) {
8802                 fn = gen_helper_vfp_touhh;
8803             } else {
8804                 fn = gen_helper_vfp_toshh;
8805             }
8806             break;
8807         case MO_32:
8808             if (is_u) {
8809                 fn = gen_helper_vfp_touls;
8810             } else {
8811                 fn = gen_helper_vfp_tosls;
8812             }
8813             break;
8814         default:
8815             g_assert_not_reached();
8816         }
8817 
8818         for (pass = 0; pass < maxpass; pass++) {
8819             TCGv_i32 tcg_op = tcg_temp_new_i32();
8820 
8821             read_vec_element_i32(s, tcg_op, rn, pass, size);
8822             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8823             if (is_scalar) {
8824                 write_fp_sreg(s, rd, tcg_op);
8825             } else {
8826                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8827             }
8828         }
8829         if (!is_scalar) {
8830             clear_vec_high(s, is_q, rd);
8831         }
8832     }
8833 
8834     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8835 }
8836 
8837 /* AdvSIMD scalar shift by immediate
8838  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8839  * +-----+---+-------------+------+------+--------+---+------+------+
8840  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8841  * +-----+---+-------------+------+------+--------+---+------+------+
8842  *
8843  * This is the scalar version so it works on a fixed sized registers
8844  */
8845 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8846 {
8847     int rd = extract32(insn, 0, 5);
8848     int rn = extract32(insn, 5, 5);
8849     int opcode = extract32(insn, 11, 5);
8850     int immb = extract32(insn, 16, 3);
8851     int immh = extract32(insn, 19, 4);
8852     bool is_u = extract32(insn, 29, 1);
8853 
8854     if (immh == 0) {
8855         unallocated_encoding(s);
8856         return;
8857     }
8858 
8859     switch (opcode) {
8860     case 0x08: /* SRI */
8861         if (!is_u) {
8862             unallocated_encoding(s);
8863             return;
8864         }
8865         /* fall through */
8866     case 0x00: /* SSHR / USHR */
8867     case 0x02: /* SSRA / USRA */
8868     case 0x04: /* SRSHR / URSHR */
8869     case 0x06: /* SRSRA / URSRA */
8870         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8871         break;
8872     case 0x0a: /* SHL / SLI */
8873         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8874         break;
8875     case 0x1c: /* SCVTF, UCVTF */
8876         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8877                                      opcode, rn, rd);
8878         break;
8879     case 0x10: /* SQSHRUN, SQSHRUN2 */
8880     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8881         if (!is_u) {
8882             unallocated_encoding(s);
8883             return;
8884         }
8885         handle_vec_simd_sqshrn(s, true, false, false, true,
8886                                immh, immb, opcode, rn, rd);
8887         break;
8888     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8889     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8890         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8891                                immh, immb, opcode, rn, rd);
8892         break;
8893     case 0xc: /* SQSHLU */
8894         if (!is_u) {
8895             unallocated_encoding(s);
8896             return;
8897         }
8898         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8899         break;
8900     case 0xe: /* SQSHL, UQSHL */
8901         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8902         break;
8903     case 0x1f: /* FCVTZS, FCVTZU */
8904         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8905         break;
8906     default:
8907         unallocated_encoding(s);
8908         break;
8909     }
8910 }
8911 
8912 /* AdvSIMD scalar three different
8913  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8914  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8915  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8916  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8917  */
8918 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8919 {
8920     bool is_u = extract32(insn, 29, 1);
8921     int size = extract32(insn, 22, 2);
8922     int opcode = extract32(insn, 12, 4);
8923     int rm = extract32(insn, 16, 5);
8924     int rn = extract32(insn, 5, 5);
8925     int rd = extract32(insn, 0, 5);
8926 
8927     if (is_u) {
8928         unallocated_encoding(s);
8929         return;
8930     }
8931 
8932     switch (opcode) {
8933     case 0x9: /* SQDMLAL, SQDMLAL2 */
8934     case 0xb: /* SQDMLSL, SQDMLSL2 */
8935     case 0xd: /* SQDMULL, SQDMULL2 */
8936         if (size == 0 || size == 3) {
8937             unallocated_encoding(s);
8938             return;
8939         }
8940         break;
8941     default:
8942         unallocated_encoding(s);
8943         return;
8944     }
8945 
8946     if (!fp_access_check(s)) {
8947         return;
8948     }
8949 
8950     if (size == 2) {
8951         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8952         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8953         TCGv_i64 tcg_res = tcg_temp_new_i64();
8954 
8955         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8956         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8957 
8958         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8959         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8960 
8961         switch (opcode) {
8962         case 0xd: /* SQDMULL, SQDMULL2 */
8963             break;
8964         case 0xb: /* SQDMLSL, SQDMLSL2 */
8965             tcg_gen_neg_i64(tcg_res, tcg_res);
8966             /* fall through */
8967         case 0x9: /* SQDMLAL, SQDMLAL2 */
8968             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8969             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8970                                               tcg_res, tcg_op1);
8971             break;
8972         default:
8973             g_assert_not_reached();
8974         }
8975 
8976         write_fp_dreg(s, rd, tcg_res);
8977     } else {
8978         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8979         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8980         TCGv_i64 tcg_res = tcg_temp_new_i64();
8981 
8982         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8983         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8984 
8985         switch (opcode) {
8986         case 0xd: /* SQDMULL, SQDMULL2 */
8987             break;
8988         case 0xb: /* SQDMLSL, SQDMLSL2 */
8989             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8990             /* fall through */
8991         case 0x9: /* SQDMLAL, SQDMLAL2 */
8992         {
8993             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8994             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8995             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8996                                               tcg_res, tcg_op3);
8997             break;
8998         }
8999         default:
9000             g_assert_not_reached();
9001         }
9002 
9003         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9004         write_fp_dreg(s, rd, tcg_res);
9005     }
9006 }
9007 
9008 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9009                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9010 {
9011     /* Handle 64x64->64 opcodes which are shared between the scalar
9012      * and vector 3-same groups. We cover every opcode where size == 3
9013      * is valid in either the three-reg-same (integer, not pairwise)
9014      * or scalar-three-reg-same groups.
9015      */
9016     TCGCond cond;
9017 
9018     switch (opcode) {
9019     case 0x1: /* SQADD */
9020         if (u) {
9021             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9022         } else {
9023             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9024         }
9025         break;
9026     case 0x5: /* SQSUB */
9027         if (u) {
9028             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9029         } else {
9030             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9031         }
9032         break;
9033     case 0x6: /* CMGT, CMHI */
9034         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9035          * We implement this using setcond (test) and then negating.
9036          */
9037         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9038     do_cmop:
9039         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9040         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9041         break;
9042     case 0x7: /* CMGE, CMHS */
9043         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9044         goto do_cmop;
9045     case 0x11: /* CMTST, CMEQ */
9046         if (u) {
9047             cond = TCG_COND_EQ;
9048             goto do_cmop;
9049         }
9050         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9051         break;
9052     case 0x8: /* SSHL, USHL */
9053         if (u) {
9054             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9055         } else {
9056             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9057         }
9058         break;
9059     case 0x9: /* SQSHL, UQSHL */
9060         if (u) {
9061             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9062         } else {
9063             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9064         }
9065         break;
9066     case 0xa: /* SRSHL, URSHL */
9067         if (u) {
9068             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9069         } else {
9070             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9071         }
9072         break;
9073     case 0xb: /* SQRSHL, UQRSHL */
9074         if (u) {
9075             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9076         } else {
9077             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9078         }
9079         break;
9080     case 0x10: /* ADD, SUB */
9081         if (u) {
9082             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9083         } else {
9084             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9085         }
9086         break;
9087     default:
9088         g_assert_not_reached();
9089     }
9090 }
9091 
9092 /* Handle the 3-same-operands float operations; shared by the scalar
9093  * and vector encodings. The caller must filter out any encodings
9094  * not allocated for the encoding it is dealing with.
9095  */
9096 static void handle_3same_float(DisasContext *s, int size, int elements,
9097                                int fpopcode, int rd, int rn, int rm)
9098 {
9099     int pass;
9100     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9101 
9102     for (pass = 0; pass < elements; pass++) {
9103         if (size) {
9104             /* Double */
9105             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9106             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9107             TCGv_i64 tcg_res = tcg_temp_new_i64();
9108 
9109             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9110             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9111 
9112             switch (fpopcode) {
9113             case 0x39: /* FMLS */
9114                 /* As usual for ARM, separate negation for fused multiply-add */
9115                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9116                 /* fall through */
9117             case 0x19: /* FMLA */
9118                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9119                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9120                                        tcg_res, fpst);
9121                 break;
9122             case 0x18: /* FMAXNM */
9123                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9124                 break;
9125             case 0x1a: /* FADD */
9126                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9127                 break;
9128             case 0x1b: /* FMULX */
9129                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9130                 break;
9131             case 0x1c: /* FCMEQ */
9132                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9133                 break;
9134             case 0x1e: /* FMAX */
9135                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9136                 break;
9137             case 0x1f: /* FRECPS */
9138                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9139                 break;
9140             case 0x38: /* FMINNM */
9141                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9142                 break;
9143             case 0x3a: /* FSUB */
9144                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9145                 break;
9146             case 0x3e: /* FMIN */
9147                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9148                 break;
9149             case 0x3f: /* FRSQRTS */
9150                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9151                 break;
9152             case 0x5b: /* FMUL */
9153                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9154                 break;
9155             case 0x5c: /* FCMGE */
9156                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9157                 break;
9158             case 0x5d: /* FACGE */
9159                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9160                 break;
9161             case 0x5f: /* FDIV */
9162                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9163                 break;
9164             case 0x7a: /* FABD */
9165                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9166                 gen_helper_vfp_absd(tcg_res, tcg_res);
9167                 break;
9168             case 0x7c: /* FCMGT */
9169                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9170                 break;
9171             case 0x7d: /* FACGT */
9172                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9173                 break;
9174             default:
9175                 g_assert_not_reached();
9176             }
9177 
9178             write_vec_element(s, tcg_res, rd, pass, MO_64);
9179         } else {
9180             /* Single */
9181             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9182             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9183             TCGv_i32 tcg_res = tcg_temp_new_i32();
9184 
9185             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9186             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9187 
9188             switch (fpopcode) {
9189             case 0x39: /* FMLS */
9190                 /* As usual for ARM, separate negation for fused multiply-add */
9191                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9192                 /* fall through */
9193             case 0x19: /* FMLA */
9194                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9195                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9196                                        tcg_res, fpst);
9197                 break;
9198             case 0x1a: /* FADD */
9199                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9200                 break;
9201             case 0x1b: /* FMULX */
9202                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9203                 break;
9204             case 0x1c: /* FCMEQ */
9205                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9206                 break;
9207             case 0x1e: /* FMAX */
9208                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9209                 break;
9210             case 0x1f: /* FRECPS */
9211                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9212                 break;
9213             case 0x18: /* FMAXNM */
9214                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9215                 break;
9216             case 0x38: /* FMINNM */
9217                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9218                 break;
9219             case 0x3a: /* FSUB */
9220                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9221                 break;
9222             case 0x3e: /* FMIN */
9223                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9224                 break;
9225             case 0x3f: /* FRSQRTS */
9226                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9227                 break;
9228             case 0x5b: /* FMUL */
9229                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9230                 break;
9231             case 0x5c: /* FCMGE */
9232                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9233                 break;
9234             case 0x5d: /* FACGE */
9235                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9236                 break;
9237             case 0x5f: /* FDIV */
9238                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9239                 break;
9240             case 0x7a: /* FABD */
9241                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9242                 gen_helper_vfp_abss(tcg_res, tcg_res);
9243                 break;
9244             case 0x7c: /* FCMGT */
9245                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9246                 break;
9247             case 0x7d: /* FACGT */
9248                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9249                 break;
9250             default:
9251                 g_assert_not_reached();
9252             }
9253 
9254             if (elements == 1) {
9255                 /* scalar single so clear high part */
9256                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9257 
9258                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9259                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9260             } else {
9261                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9262             }
9263         }
9264     }
9265 
9266     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9267 }
9268 
9269 /* AdvSIMD scalar three same
9270  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9271  * +-----+---+-----------+------+---+------+--------+---+------+------+
9272  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9273  * +-----+---+-----------+------+---+------+--------+---+------+------+
9274  */
9275 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9276 {
9277     int rd = extract32(insn, 0, 5);
9278     int rn = extract32(insn, 5, 5);
9279     int opcode = extract32(insn, 11, 5);
9280     int rm = extract32(insn, 16, 5);
9281     int size = extract32(insn, 22, 2);
9282     bool u = extract32(insn, 29, 1);
9283     TCGv_i64 tcg_rd;
9284 
9285     if (opcode >= 0x18) {
9286         /* Floating point: U, size[1] and opcode indicate operation */
9287         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9288         switch (fpopcode) {
9289         case 0x1b: /* FMULX */
9290         case 0x1f: /* FRECPS */
9291         case 0x3f: /* FRSQRTS */
9292         case 0x5d: /* FACGE */
9293         case 0x7d: /* FACGT */
9294         case 0x1c: /* FCMEQ */
9295         case 0x5c: /* FCMGE */
9296         case 0x7c: /* FCMGT */
9297         case 0x7a: /* FABD */
9298             break;
9299         default:
9300             unallocated_encoding(s);
9301             return;
9302         }
9303 
9304         if (!fp_access_check(s)) {
9305             return;
9306         }
9307 
9308         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9309         return;
9310     }
9311 
9312     switch (opcode) {
9313     case 0x1: /* SQADD, UQADD */
9314     case 0x5: /* SQSUB, UQSUB */
9315     case 0x9: /* SQSHL, UQSHL */
9316     case 0xb: /* SQRSHL, UQRSHL */
9317         break;
9318     case 0x8: /* SSHL, USHL */
9319     case 0xa: /* SRSHL, URSHL */
9320     case 0x6: /* CMGT, CMHI */
9321     case 0x7: /* CMGE, CMHS */
9322     case 0x11: /* CMTST, CMEQ */
9323     case 0x10: /* ADD, SUB (vector) */
9324         if (size != 3) {
9325             unallocated_encoding(s);
9326             return;
9327         }
9328         break;
9329     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9330         if (size != 1 && size != 2) {
9331             unallocated_encoding(s);
9332             return;
9333         }
9334         break;
9335     default:
9336         unallocated_encoding(s);
9337         return;
9338     }
9339 
9340     if (!fp_access_check(s)) {
9341         return;
9342     }
9343 
9344     tcg_rd = tcg_temp_new_i64();
9345 
9346     if (size == 3) {
9347         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9348         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9349 
9350         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9351     } else {
9352         /* Do a single operation on the lowest element in the vector.
9353          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9354          * no side effects for all these operations.
9355          * OPTME: special-purpose helpers would avoid doing some
9356          * unnecessary work in the helper for the 8 and 16 bit cases.
9357          */
9358         NeonGenTwoOpEnvFn *genenvfn;
9359         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9360         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9361         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9362 
9363         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9364         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9365 
9366         switch (opcode) {
9367         case 0x1: /* SQADD, UQADD */
9368         {
9369             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9370                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9371                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9372                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9373             };
9374             genenvfn = fns[size][u];
9375             break;
9376         }
9377         case 0x5: /* SQSUB, UQSUB */
9378         {
9379             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9380                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9381                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9382                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9383             };
9384             genenvfn = fns[size][u];
9385             break;
9386         }
9387         case 0x9: /* SQSHL, UQSHL */
9388         {
9389             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9390                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9391                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9392                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9393             };
9394             genenvfn = fns[size][u];
9395             break;
9396         }
9397         case 0xb: /* SQRSHL, UQRSHL */
9398         {
9399             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9400                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9401                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9402                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9403             };
9404             genenvfn = fns[size][u];
9405             break;
9406         }
9407         case 0x16: /* SQDMULH, SQRDMULH */
9408         {
9409             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9410                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9411                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9412             };
9413             assert(size == 1 || size == 2);
9414             genenvfn = fns[size - 1][u];
9415             break;
9416         }
9417         default:
9418             g_assert_not_reached();
9419         }
9420 
9421         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9422         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9423     }
9424 
9425     write_fp_dreg(s, rd, tcg_rd);
9426 }
9427 
9428 /* AdvSIMD scalar three same FP16
9429  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9430  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9431  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9432  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9433  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9434  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9435  */
9436 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9437                                                   uint32_t insn)
9438 {
9439     int rd = extract32(insn, 0, 5);
9440     int rn = extract32(insn, 5, 5);
9441     int opcode = extract32(insn, 11, 3);
9442     int rm = extract32(insn, 16, 5);
9443     bool u = extract32(insn, 29, 1);
9444     bool a = extract32(insn, 23, 1);
9445     int fpopcode = opcode | (a << 3) |  (u << 4);
9446     TCGv_ptr fpst;
9447     TCGv_i32 tcg_op1;
9448     TCGv_i32 tcg_op2;
9449     TCGv_i32 tcg_res;
9450 
9451     switch (fpopcode) {
9452     case 0x03: /* FMULX */
9453     case 0x04: /* FCMEQ (reg) */
9454     case 0x07: /* FRECPS */
9455     case 0x0f: /* FRSQRTS */
9456     case 0x14: /* FCMGE (reg) */
9457     case 0x15: /* FACGE */
9458     case 0x1a: /* FABD */
9459     case 0x1c: /* FCMGT (reg) */
9460     case 0x1d: /* FACGT */
9461         break;
9462     default:
9463         unallocated_encoding(s);
9464         return;
9465     }
9466 
9467     if (!dc_isar_feature(aa64_fp16, s)) {
9468         unallocated_encoding(s);
9469     }
9470 
9471     if (!fp_access_check(s)) {
9472         return;
9473     }
9474 
9475     fpst = fpstatus_ptr(FPST_FPCR_F16);
9476 
9477     tcg_op1 = read_fp_hreg(s, rn);
9478     tcg_op2 = read_fp_hreg(s, rm);
9479     tcg_res = tcg_temp_new_i32();
9480 
9481     switch (fpopcode) {
9482     case 0x03: /* FMULX */
9483         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9484         break;
9485     case 0x04: /* FCMEQ (reg) */
9486         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9487         break;
9488     case 0x07: /* FRECPS */
9489         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9490         break;
9491     case 0x0f: /* FRSQRTS */
9492         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9493         break;
9494     case 0x14: /* FCMGE (reg) */
9495         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9496         break;
9497     case 0x15: /* FACGE */
9498         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9499         break;
9500     case 0x1a: /* FABD */
9501         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9502         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9503         break;
9504     case 0x1c: /* FCMGT (reg) */
9505         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9506         break;
9507     case 0x1d: /* FACGT */
9508         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9509         break;
9510     default:
9511         g_assert_not_reached();
9512     }
9513 
9514     write_fp_sreg(s, rd, tcg_res);
9515 }
9516 
9517 /* AdvSIMD scalar three same extra
9518  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9519  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9520  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9521  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9522  */
9523 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9524                                                    uint32_t insn)
9525 {
9526     int rd = extract32(insn, 0, 5);
9527     int rn = extract32(insn, 5, 5);
9528     int opcode = extract32(insn, 11, 4);
9529     int rm = extract32(insn, 16, 5);
9530     int size = extract32(insn, 22, 2);
9531     bool u = extract32(insn, 29, 1);
9532     TCGv_i32 ele1, ele2, ele3;
9533     TCGv_i64 res;
9534     bool feature;
9535 
9536     switch (u * 16 + opcode) {
9537     case 0x10: /* SQRDMLAH (vector) */
9538     case 0x11: /* SQRDMLSH (vector) */
9539         if (size != 1 && size != 2) {
9540             unallocated_encoding(s);
9541             return;
9542         }
9543         feature = dc_isar_feature(aa64_rdm, s);
9544         break;
9545     default:
9546         unallocated_encoding(s);
9547         return;
9548     }
9549     if (!feature) {
9550         unallocated_encoding(s);
9551         return;
9552     }
9553     if (!fp_access_check(s)) {
9554         return;
9555     }
9556 
9557     /* Do a single operation on the lowest element in the vector.
9558      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9559      * with no side effects for all these operations.
9560      * OPTME: special-purpose helpers would avoid doing some
9561      * unnecessary work in the helper for the 16 bit cases.
9562      */
9563     ele1 = tcg_temp_new_i32();
9564     ele2 = tcg_temp_new_i32();
9565     ele3 = tcg_temp_new_i32();
9566 
9567     read_vec_element_i32(s, ele1, rn, 0, size);
9568     read_vec_element_i32(s, ele2, rm, 0, size);
9569     read_vec_element_i32(s, ele3, rd, 0, size);
9570 
9571     switch (opcode) {
9572     case 0x0: /* SQRDMLAH */
9573         if (size == 1) {
9574             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9575         } else {
9576             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9577         }
9578         break;
9579     case 0x1: /* SQRDMLSH */
9580         if (size == 1) {
9581             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9582         } else {
9583             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9584         }
9585         break;
9586     default:
9587         g_assert_not_reached();
9588     }
9589 
9590     res = tcg_temp_new_i64();
9591     tcg_gen_extu_i32_i64(res, ele3);
9592     write_fp_dreg(s, rd, res);
9593 }
9594 
9595 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9596                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9597                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9598 {
9599     /* Handle 64->64 opcodes which are shared between the scalar and
9600      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9601      * is valid in either group and also the double-precision fp ops.
9602      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9603      * requires them.
9604      */
9605     TCGCond cond;
9606 
9607     switch (opcode) {
9608     case 0x4: /* CLS, CLZ */
9609         if (u) {
9610             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9611         } else {
9612             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9613         }
9614         break;
9615     case 0x5: /* NOT */
9616         /* This opcode is shared with CNT and RBIT but we have earlier
9617          * enforced that size == 3 if and only if this is the NOT insn.
9618          */
9619         tcg_gen_not_i64(tcg_rd, tcg_rn);
9620         break;
9621     case 0x7: /* SQABS, SQNEG */
9622         if (u) {
9623             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9624         } else {
9625             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9626         }
9627         break;
9628     case 0xa: /* CMLT */
9629         /* 64 bit integer comparison against zero, result is
9630          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9631          * subtracting 1.
9632          */
9633         cond = TCG_COND_LT;
9634     do_cmop:
9635         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9636         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9637         break;
9638     case 0x8: /* CMGT, CMGE */
9639         cond = u ? TCG_COND_GE : TCG_COND_GT;
9640         goto do_cmop;
9641     case 0x9: /* CMEQ, CMLE */
9642         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9643         goto do_cmop;
9644     case 0xb: /* ABS, NEG */
9645         if (u) {
9646             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9647         } else {
9648             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9649         }
9650         break;
9651     case 0x2f: /* FABS */
9652         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9653         break;
9654     case 0x6f: /* FNEG */
9655         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9656         break;
9657     case 0x7f: /* FSQRT */
9658         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9659         break;
9660     case 0x1a: /* FCVTNS */
9661     case 0x1b: /* FCVTMS */
9662     case 0x1c: /* FCVTAS */
9663     case 0x3a: /* FCVTPS */
9664     case 0x3b: /* FCVTZS */
9665         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9666         break;
9667     case 0x5a: /* FCVTNU */
9668     case 0x5b: /* FCVTMU */
9669     case 0x5c: /* FCVTAU */
9670     case 0x7a: /* FCVTPU */
9671     case 0x7b: /* FCVTZU */
9672         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9673         break;
9674     case 0x18: /* FRINTN */
9675     case 0x19: /* FRINTM */
9676     case 0x38: /* FRINTP */
9677     case 0x39: /* FRINTZ */
9678     case 0x58: /* FRINTA */
9679     case 0x79: /* FRINTI */
9680         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9681         break;
9682     case 0x59: /* FRINTX */
9683         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9684         break;
9685     case 0x1e: /* FRINT32Z */
9686     case 0x5e: /* FRINT32X */
9687         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9688         break;
9689     case 0x1f: /* FRINT64Z */
9690     case 0x5f: /* FRINT64X */
9691         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9692         break;
9693     default:
9694         g_assert_not_reached();
9695     }
9696 }
9697 
9698 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9699                                    bool is_scalar, bool is_u, bool is_q,
9700                                    int size, int rn, int rd)
9701 {
9702     bool is_double = (size == MO_64);
9703     TCGv_ptr fpst;
9704 
9705     if (!fp_access_check(s)) {
9706         return;
9707     }
9708 
9709     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9710 
9711     if (is_double) {
9712         TCGv_i64 tcg_op = tcg_temp_new_i64();
9713         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9714         TCGv_i64 tcg_res = tcg_temp_new_i64();
9715         NeonGenTwoDoubleOpFn *genfn;
9716         bool swap = false;
9717         int pass;
9718 
9719         switch (opcode) {
9720         case 0x2e: /* FCMLT (zero) */
9721             swap = true;
9722             /* fallthrough */
9723         case 0x2c: /* FCMGT (zero) */
9724             genfn = gen_helper_neon_cgt_f64;
9725             break;
9726         case 0x2d: /* FCMEQ (zero) */
9727             genfn = gen_helper_neon_ceq_f64;
9728             break;
9729         case 0x6d: /* FCMLE (zero) */
9730             swap = true;
9731             /* fall through */
9732         case 0x6c: /* FCMGE (zero) */
9733             genfn = gen_helper_neon_cge_f64;
9734             break;
9735         default:
9736             g_assert_not_reached();
9737         }
9738 
9739         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9740             read_vec_element(s, tcg_op, rn, pass, MO_64);
9741             if (swap) {
9742                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9743             } else {
9744                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9745             }
9746             write_vec_element(s, tcg_res, rd, pass, MO_64);
9747         }
9748 
9749         clear_vec_high(s, !is_scalar, rd);
9750     } else {
9751         TCGv_i32 tcg_op = tcg_temp_new_i32();
9752         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9753         TCGv_i32 tcg_res = tcg_temp_new_i32();
9754         NeonGenTwoSingleOpFn *genfn;
9755         bool swap = false;
9756         int pass, maxpasses;
9757 
9758         if (size == MO_16) {
9759             switch (opcode) {
9760             case 0x2e: /* FCMLT (zero) */
9761                 swap = true;
9762                 /* fall through */
9763             case 0x2c: /* FCMGT (zero) */
9764                 genfn = gen_helper_advsimd_cgt_f16;
9765                 break;
9766             case 0x2d: /* FCMEQ (zero) */
9767                 genfn = gen_helper_advsimd_ceq_f16;
9768                 break;
9769             case 0x6d: /* FCMLE (zero) */
9770                 swap = true;
9771                 /* fall through */
9772             case 0x6c: /* FCMGE (zero) */
9773                 genfn = gen_helper_advsimd_cge_f16;
9774                 break;
9775             default:
9776                 g_assert_not_reached();
9777             }
9778         } else {
9779             switch (opcode) {
9780             case 0x2e: /* FCMLT (zero) */
9781                 swap = true;
9782                 /* fall through */
9783             case 0x2c: /* FCMGT (zero) */
9784                 genfn = gen_helper_neon_cgt_f32;
9785                 break;
9786             case 0x2d: /* FCMEQ (zero) */
9787                 genfn = gen_helper_neon_ceq_f32;
9788                 break;
9789             case 0x6d: /* FCMLE (zero) */
9790                 swap = true;
9791                 /* fall through */
9792             case 0x6c: /* FCMGE (zero) */
9793                 genfn = gen_helper_neon_cge_f32;
9794                 break;
9795             default:
9796                 g_assert_not_reached();
9797             }
9798         }
9799 
9800         if (is_scalar) {
9801             maxpasses = 1;
9802         } else {
9803             int vector_size = 8 << is_q;
9804             maxpasses = vector_size >> size;
9805         }
9806 
9807         for (pass = 0; pass < maxpasses; pass++) {
9808             read_vec_element_i32(s, tcg_op, rn, pass, size);
9809             if (swap) {
9810                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9811             } else {
9812                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9813             }
9814             if (is_scalar) {
9815                 write_fp_sreg(s, rd, tcg_res);
9816             } else {
9817                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9818             }
9819         }
9820 
9821         if (!is_scalar) {
9822             clear_vec_high(s, is_q, rd);
9823         }
9824     }
9825 }
9826 
9827 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9828                                     bool is_scalar, bool is_u, bool is_q,
9829                                     int size, int rn, int rd)
9830 {
9831     bool is_double = (size == 3);
9832     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9833 
9834     if (is_double) {
9835         TCGv_i64 tcg_op = tcg_temp_new_i64();
9836         TCGv_i64 tcg_res = tcg_temp_new_i64();
9837         int pass;
9838 
9839         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9840             read_vec_element(s, tcg_op, rn, pass, MO_64);
9841             switch (opcode) {
9842             case 0x3d: /* FRECPE */
9843                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9844                 break;
9845             case 0x3f: /* FRECPX */
9846                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9847                 break;
9848             case 0x7d: /* FRSQRTE */
9849                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9850                 break;
9851             default:
9852                 g_assert_not_reached();
9853             }
9854             write_vec_element(s, tcg_res, rd, pass, MO_64);
9855         }
9856         clear_vec_high(s, !is_scalar, rd);
9857     } else {
9858         TCGv_i32 tcg_op = tcg_temp_new_i32();
9859         TCGv_i32 tcg_res = tcg_temp_new_i32();
9860         int pass, maxpasses;
9861 
9862         if (is_scalar) {
9863             maxpasses = 1;
9864         } else {
9865             maxpasses = is_q ? 4 : 2;
9866         }
9867 
9868         for (pass = 0; pass < maxpasses; pass++) {
9869             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9870 
9871             switch (opcode) {
9872             case 0x3c: /* URECPE */
9873                 gen_helper_recpe_u32(tcg_res, tcg_op);
9874                 break;
9875             case 0x3d: /* FRECPE */
9876                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9877                 break;
9878             case 0x3f: /* FRECPX */
9879                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9880                 break;
9881             case 0x7d: /* FRSQRTE */
9882                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9883                 break;
9884             default:
9885                 g_assert_not_reached();
9886             }
9887 
9888             if (is_scalar) {
9889                 write_fp_sreg(s, rd, tcg_res);
9890             } else {
9891                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9892             }
9893         }
9894         if (!is_scalar) {
9895             clear_vec_high(s, is_q, rd);
9896         }
9897     }
9898 }
9899 
9900 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9901                                 int opcode, bool u, bool is_q,
9902                                 int size, int rn, int rd)
9903 {
9904     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9905      * in the source becomes a size element in the destination).
9906      */
9907     int pass;
9908     TCGv_i32 tcg_res[2];
9909     int destelt = is_q ? 2 : 0;
9910     int passes = scalar ? 1 : 2;
9911 
9912     if (scalar) {
9913         tcg_res[1] = tcg_constant_i32(0);
9914     }
9915 
9916     for (pass = 0; pass < passes; pass++) {
9917         TCGv_i64 tcg_op = tcg_temp_new_i64();
9918         NeonGenNarrowFn *genfn = NULL;
9919         NeonGenNarrowEnvFn *genenvfn = NULL;
9920 
9921         if (scalar) {
9922             read_vec_element(s, tcg_op, rn, pass, size + 1);
9923         } else {
9924             read_vec_element(s, tcg_op, rn, pass, MO_64);
9925         }
9926         tcg_res[pass] = tcg_temp_new_i32();
9927 
9928         switch (opcode) {
9929         case 0x12: /* XTN, SQXTUN */
9930         {
9931             static NeonGenNarrowFn * const xtnfns[3] = {
9932                 gen_helper_neon_narrow_u8,
9933                 gen_helper_neon_narrow_u16,
9934                 tcg_gen_extrl_i64_i32,
9935             };
9936             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9937                 gen_helper_neon_unarrow_sat8,
9938                 gen_helper_neon_unarrow_sat16,
9939                 gen_helper_neon_unarrow_sat32,
9940             };
9941             if (u) {
9942                 genenvfn = sqxtunfns[size];
9943             } else {
9944                 genfn = xtnfns[size];
9945             }
9946             break;
9947         }
9948         case 0x14: /* SQXTN, UQXTN */
9949         {
9950             static NeonGenNarrowEnvFn * const fns[3][2] = {
9951                 { gen_helper_neon_narrow_sat_s8,
9952                   gen_helper_neon_narrow_sat_u8 },
9953                 { gen_helper_neon_narrow_sat_s16,
9954                   gen_helper_neon_narrow_sat_u16 },
9955                 { gen_helper_neon_narrow_sat_s32,
9956                   gen_helper_neon_narrow_sat_u32 },
9957             };
9958             genenvfn = fns[size][u];
9959             break;
9960         }
9961         case 0x16: /* FCVTN, FCVTN2 */
9962             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9963             if (size == 2) {
9964                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9965             } else {
9966                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9967                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9968                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9969                 TCGv_i32 ahp = get_ahp_flag();
9970 
9971                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9972                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9973                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9974                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9975             }
9976             break;
9977         case 0x36: /* BFCVTN, BFCVTN2 */
9978             {
9979                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9980                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9981             }
9982             break;
9983         case 0x56:  /* FCVTXN, FCVTXN2 */
9984             /* 64 bit to 32 bit float conversion
9985              * with von Neumann rounding (round to odd)
9986              */
9987             assert(size == 2);
9988             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9989             break;
9990         default:
9991             g_assert_not_reached();
9992         }
9993 
9994         if (genfn) {
9995             genfn(tcg_res[pass], tcg_op);
9996         } else if (genenvfn) {
9997             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9998         }
9999     }
10000 
10001     for (pass = 0; pass < 2; pass++) {
10002         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10003     }
10004     clear_vec_high(s, is_q, rd);
10005 }
10006 
10007 /* Remaining saturating accumulating ops */
10008 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10009                                 bool is_q, int size, int rn, int rd)
10010 {
10011     bool is_double = (size == 3);
10012 
10013     if (is_double) {
10014         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10015         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10016         int pass;
10017 
10018         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10019             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10020             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10021 
10022             if (is_u) { /* USQADD */
10023                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10024             } else { /* SUQADD */
10025                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10026             }
10027             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10028         }
10029         clear_vec_high(s, !is_scalar, rd);
10030     } else {
10031         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10032         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10033         int pass, maxpasses;
10034 
10035         if (is_scalar) {
10036             maxpasses = 1;
10037         } else {
10038             maxpasses = is_q ? 4 : 2;
10039         }
10040 
10041         for (pass = 0; pass < maxpasses; pass++) {
10042             if (is_scalar) {
10043                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10044                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10045             } else {
10046                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10047                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10048             }
10049 
10050             if (is_u) { /* USQADD */
10051                 switch (size) {
10052                 case 0:
10053                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10054                     break;
10055                 case 1:
10056                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10057                     break;
10058                 case 2:
10059                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10060                     break;
10061                 default:
10062                     g_assert_not_reached();
10063                 }
10064             } else { /* SUQADD */
10065                 switch (size) {
10066                 case 0:
10067                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10068                     break;
10069                 case 1:
10070                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10071                     break;
10072                 case 2:
10073                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10074                     break;
10075                 default:
10076                     g_assert_not_reached();
10077                 }
10078             }
10079 
10080             if (is_scalar) {
10081                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10082             }
10083             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10084         }
10085         clear_vec_high(s, is_q, rd);
10086     }
10087 }
10088 
10089 /* AdvSIMD scalar two reg misc
10090  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10091  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10092  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10093  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10094  */
10095 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10096 {
10097     int rd = extract32(insn, 0, 5);
10098     int rn = extract32(insn, 5, 5);
10099     int opcode = extract32(insn, 12, 5);
10100     int size = extract32(insn, 22, 2);
10101     bool u = extract32(insn, 29, 1);
10102     bool is_fcvt = false;
10103     int rmode;
10104     TCGv_i32 tcg_rmode;
10105     TCGv_ptr tcg_fpstatus;
10106 
10107     switch (opcode) {
10108     case 0x3: /* USQADD / SUQADD*/
10109         if (!fp_access_check(s)) {
10110             return;
10111         }
10112         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10113         return;
10114     case 0x7: /* SQABS / SQNEG */
10115         break;
10116     case 0xa: /* CMLT */
10117         if (u) {
10118             unallocated_encoding(s);
10119             return;
10120         }
10121         /* fall through */
10122     case 0x8: /* CMGT, CMGE */
10123     case 0x9: /* CMEQ, CMLE */
10124     case 0xb: /* ABS, NEG */
10125         if (size != 3) {
10126             unallocated_encoding(s);
10127             return;
10128         }
10129         break;
10130     case 0x12: /* SQXTUN */
10131         if (!u) {
10132             unallocated_encoding(s);
10133             return;
10134         }
10135         /* fall through */
10136     case 0x14: /* SQXTN, UQXTN */
10137         if (size == 3) {
10138             unallocated_encoding(s);
10139             return;
10140         }
10141         if (!fp_access_check(s)) {
10142             return;
10143         }
10144         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10145         return;
10146     case 0xc ... 0xf:
10147     case 0x16 ... 0x1d:
10148     case 0x1f:
10149         /* Floating point: U, size[1] and opcode indicate operation;
10150          * size[0] indicates single or double precision.
10151          */
10152         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10153         size = extract32(size, 0, 1) ? 3 : 2;
10154         switch (opcode) {
10155         case 0x2c: /* FCMGT (zero) */
10156         case 0x2d: /* FCMEQ (zero) */
10157         case 0x2e: /* FCMLT (zero) */
10158         case 0x6c: /* FCMGE (zero) */
10159         case 0x6d: /* FCMLE (zero) */
10160             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10161             return;
10162         case 0x1d: /* SCVTF */
10163         case 0x5d: /* UCVTF */
10164         {
10165             bool is_signed = (opcode == 0x1d);
10166             if (!fp_access_check(s)) {
10167                 return;
10168             }
10169             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10170             return;
10171         }
10172         case 0x3d: /* FRECPE */
10173         case 0x3f: /* FRECPX */
10174         case 0x7d: /* FRSQRTE */
10175             if (!fp_access_check(s)) {
10176                 return;
10177             }
10178             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10179             return;
10180         case 0x1a: /* FCVTNS */
10181         case 0x1b: /* FCVTMS */
10182         case 0x3a: /* FCVTPS */
10183         case 0x3b: /* FCVTZS */
10184         case 0x5a: /* FCVTNU */
10185         case 0x5b: /* FCVTMU */
10186         case 0x7a: /* FCVTPU */
10187         case 0x7b: /* FCVTZU */
10188             is_fcvt = true;
10189             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10190             break;
10191         case 0x1c: /* FCVTAS */
10192         case 0x5c: /* FCVTAU */
10193             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10194             is_fcvt = true;
10195             rmode = FPROUNDING_TIEAWAY;
10196             break;
10197         case 0x56: /* FCVTXN, FCVTXN2 */
10198             if (size == 2) {
10199                 unallocated_encoding(s);
10200                 return;
10201             }
10202             if (!fp_access_check(s)) {
10203                 return;
10204             }
10205             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10206             return;
10207         default:
10208             unallocated_encoding(s);
10209             return;
10210         }
10211         break;
10212     default:
10213         unallocated_encoding(s);
10214         return;
10215     }
10216 
10217     if (!fp_access_check(s)) {
10218         return;
10219     }
10220 
10221     if (is_fcvt) {
10222         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10223         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10224     } else {
10225         tcg_fpstatus = NULL;
10226         tcg_rmode = NULL;
10227     }
10228 
10229     if (size == 3) {
10230         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10231         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10232 
10233         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10234         write_fp_dreg(s, rd, tcg_rd);
10235     } else {
10236         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10237         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10238 
10239         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10240 
10241         switch (opcode) {
10242         case 0x7: /* SQABS, SQNEG */
10243         {
10244             NeonGenOneOpEnvFn *genfn;
10245             static NeonGenOneOpEnvFn * const fns[3][2] = {
10246                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10247                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10248                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10249             };
10250             genfn = fns[size][u];
10251             genfn(tcg_rd, cpu_env, tcg_rn);
10252             break;
10253         }
10254         case 0x1a: /* FCVTNS */
10255         case 0x1b: /* FCVTMS */
10256         case 0x1c: /* FCVTAS */
10257         case 0x3a: /* FCVTPS */
10258         case 0x3b: /* FCVTZS */
10259             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10260                                  tcg_fpstatus);
10261             break;
10262         case 0x5a: /* FCVTNU */
10263         case 0x5b: /* FCVTMU */
10264         case 0x5c: /* FCVTAU */
10265         case 0x7a: /* FCVTPU */
10266         case 0x7b: /* FCVTZU */
10267             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10268                                  tcg_fpstatus);
10269             break;
10270         default:
10271             g_assert_not_reached();
10272         }
10273 
10274         write_fp_sreg(s, rd, tcg_rd);
10275     }
10276 
10277     if (is_fcvt) {
10278         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10279     }
10280 }
10281 
10282 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10283 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10284                                  int immh, int immb, int opcode, int rn, int rd)
10285 {
10286     int size = 32 - clz32(immh) - 1;
10287     int immhb = immh << 3 | immb;
10288     int shift = 2 * (8 << size) - immhb;
10289     GVecGen2iFn *gvec_fn;
10290 
10291     if (extract32(immh, 3, 1) && !is_q) {
10292         unallocated_encoding(s);
10293         return;
10294     }
10295     tcg_debug_assert(size <= 3);
10296 
10297     if (!fp_access_check(s)) {
10298         return;
10299     }
10300 
10301     switch (opcode) {
10302     case 0x02: /* SSRA / USRA (accumulate) */
10303         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10304         break;
10305 
10306     case 0x08: /* SRI */
10307         gvec_fn = gen_gvec_sri;
10308         break;
10309 
10310     case 0x00: /* SSHR / USHR */
10311         if (is_u) {
10312             if (shift == 8 << size) {
10313                 /* Shift count the same size as element size produces zero.  */
10314                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10315                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10316                 return;
10317             }
10318             gvec_fn = tcg_gen_gvec_shri;
10319         } else {
10320             /* Shift count the same size as element size produces all sign.  */
10321             if (shift == 8 << size) {
10322                 shift -= 1;
10323             }
10324             gvec_fn = tcg_gen_gvec_sari;
10325         }
10326         break;
10327 
10328     case 0x04: /* SRSHR / URSHR (rounding) */
10329         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10330         break;
10331 
10332     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10333         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10334         break;
10335 
10336     default:
10337         g_assert_not_reached();
10338     }
10339 
10340     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10341 }
10342 
10343 /* SHL/SLI - Vector shift left */
10344 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10345                                  int immh, int immb, int opcode, int rn, int rd)
10346 {
10347     int size = 32 - clz32(immh) - 1;
10348     int immhb = immh << 3 | immb;
10349     int shift = immhb - (8 << size);
10350 
10351     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10352     assert(size >= 0 && size <= 3);
10353 
10354     if (extract32(immh, 3, 1) && !is_q) {
10355         unallocated_encoding(s);
10356         return;
10357     }
10358 
10359     if (!fp_access_check(s)) {
10360         return;
10361     }
10362 
10363     if (insert) {
10364         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10365     } else {
10366         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10367     }
10368 }
10369 
10370 /* USHLL/SHLL - Vector shift left with widening */
10371 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10372                                  int immh, int immb, int opcode, int rn, int rd)
10373 {
10374     int size = 32 - clz32(immh) - 1;
10375     int immhb = immh << 3 | immb;
10376     int shift = immhb - (8 << size);
10377     int dsize = 64;
10378     int esize = 8 << size;
10379     int elements = dsize/esize;
10380     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10381     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10382     int i;
10383 
10384     if (size >= 3) {
10385         unallocated_encoding(s);
10386         return;
10387     }
10388 
10389     if (!fp_access_check(s)) {
10390         return;
10391     }
10392 
10393     /* For the LL variants the store is larger than the load,
10394      * so if rd == rn we would overwrite parts of our input.
10395      * So load everything right now and use shifts in the main loop.
10396      */
10397     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10398 
10399     for (i = 0; i < elements; i++) {
10400         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10401         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10402         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10403         write_vec_element(s, tcg_rd, rd, i, size + 1);
10404     }
10405 }
10406 
10407 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10408 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10409                                  int immh, int immb, int opcode, int rn, int rd)
10410 {
10411     int immhb = immh << 3 | immb;
10412     int size = 32 - clz32(immh) - 1;
10413     int dsize = 64;
10414     int esize = 8 << size;
10415     int elements = dsize/esize;
10416     int shift = (2 * esize) - immhb;
10417     bool round = extract32(opcode, 0, 1);
10418     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10419     TCGv_i64 tcg_round;
10420     int i;
10421 
10422     if (extract32(immh, 3, 1)) {
10423         unallocated_encoding(s);
10424         return;
10425     }
10426 
10427     if (!fp_access_check(s)) {
10428         return;
10429     }
10430 
10431     tcg_rn = tcg_temp_new_i64();
10432     tcg_rd = tcg_temp_new_i64();
10433     tcg_final = tcg_temp_new_i64();
10434     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10435 
10436     if (round) {
10437         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10438     } else {
10439         tcg_round = NULL;
10440     }
10441 
10442     for (i = 0; i < elements; i++) {
10443         read_vec_element(s, tcg_rn, rn, i, size+1);
10444         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10445                                 false, true, size+1, shift);
10446 
10447         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10448     }
10449 
10450     if (!is_q) {
10451         write_vec_element(s, tcg_final, rd, 0, MO_64);
10452     } else {
10453         write_vec_element(s, tcg_final, rd, 1, MO_64);
10454     }
10455 
10456     clear_vec_high(s, is_q, rd);
10457 }
10458 
10459 
10460 /* AdvSIMD shift by immediate
10461  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10462  * +---+---+---+-------------+------+------+--------+---+------+------+
10463  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10464  * +---+---+---+-------------+------+------+--------+---+------+------+
10465  */
10466 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10467 {
10468     int rd = extract32(insn, 0, 5);
10469     int rn = extract32(insn, 5, 5);
10470     int opcode = extract32(insn, 11, 5);
10471     int immb = extract32(insn, 16, 3);
10472     int immh = extract32(insn, 19, 4);
10473     bool is_u = extract32(insn, 29, 1);
10474     bool is_q = extract32(insn, 30, 1);
10475 
10476     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10477     assert(immh != 0);
10478 
10479     switch (opcode) {
10480     case 0x08: /* SRI */
10481         if (!is_u) {
10482             unallocated_encoding(s);
10483             return;
10484         }
10485         /* fall through */
10486     case 0x00: /* SSHR / USHR */
10487     case 0x02: /* SSRA / USRA (accumulate) */
10488     case 0x04: /* SRSHR / URSHR (rounding) */
10489     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10490         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10491         break;
10492     case 0x0a: /* SHL / SLI */
10493         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10494         break;
10495     case 0x10: /* SHRN */
10496     case 0x11: /* RSHRN / SQRSHRUN */
10497         if (is_u) {
10498             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10499                                    opcode, rn, rd);
10500         } else {
10501             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10502         }
10503         break;
10504     case 0x12: /* SQSHRN / UQSHRN */
10505     case 0x13: /* SQRSHRN / UQRSHRN */
10506         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10507                                opcode, rn, rd);
10508         break;
10509     case 0x14: /* SSHLL / USHLL */
10510         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10511         break;
10512     case 0x1c: /* SCVTF / UCVTF */
10513         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10514                                      opcode, rn, rd);
10515         break;
10516     case 0xc: /* SQSHLU */
10517         if (!is_u) {
10518             unallocated_encoding(s);
10519             return;
10520         }
10521         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10522         break;
10523     case 0xe: /* SQSHL, UQSHL */
10524         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10525         break;
10526     case 0x1f: /* FCVTZS/ FCVTZU */
10527         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10528         return;
10529     default:
10530         unallocated_encoding(s);
10531         return;
10532     }
10533 }
10534 
10535 /* Generate code to do a "long" addition or subtraction, ie one done in
10536  * TCGv_i64 on vector lanes twice the width specified by size.
10537  */
10538 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10539                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10540 {
10541     static NeonGenTwo64OpFn * const fns[3][2] = {
10542         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10543         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10544         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10545     };
10546     NeonGenTwo64OpFn *genfn;
10547     assert(size < 3);
10548 
10549     genfn = fns[size][is_sub];
10550     genfn(tcg_res, tcg_op1, tcg_op2);
10551 }
10552 
10553 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10554                                 int opcode, int rd, int rn, int rm)
10555 {
10556     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10557     TCGv_i64 tcg_res[2];
10558     int pass, accop;
10559 
10560     tcg_res[0] = tcg_temp_new_i64();
10561     tcg_res[1] = tcg_temp_new_i64();
10562 
10563     /* Does this op do an adding accumulate, a subtracting accumulate,
10564      * or no accumulate at all?
10565      */
10566     switch (opcode) {
10567     case 5:
10568     case 8:
10569     case 9:
10570         accop = 1;
10571         break;
10572     case 10:
10573     case 11:
10574         accop = -1;
10575         break;
10576     default:
10577         accop = 0;
10578         break;
10579     }
10580 
10581     if (accop != 0) {
10582         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10583         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10584     }
10585 
10586     /* size == 2 means two 32x32->64 operations; this is worth special
10587      * casing because we can generally handle it inline.
10588      */
10589     if (size == 2) {
10590         for (pass = 0; pass < 2; pass++) {
10591             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10592             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10593             TCGv_i64 tcg_passres;
10594             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10595 
10596             int elt = pass + is_q * 2;
10597 
10598             read_vec_element(s, tcg_op1, rn, elt, memop);
10599             read_vec_element(s, tcg_op2, rm, elt, memop);
10600 
10601             if (accop == 0) {
10602                 tcg_passres = tcg_res[pass];
10603             } else {
10604                 tcg_passres = tcg_temp_new_i64();
10605             }
10606 
10607             switch (opcode) {
10608             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10609                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10610                 break;
10611             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10612                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10613                 break;
10614             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10615             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10616             {
10617                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10618                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10619 
10620                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10621                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10622                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10623                                     tcg_passres,
10624                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10625                 break;
10626             }
10627             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10628             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10629             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10630                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10631                 break;
10632             case 9: /* SQDMLAL, SQDMLAL2 */
10633             case 11: /* SQDMLSL, SQDMLSL2 */
10634             case 13: /* SQDMULL, SQDMULL2 */
10635                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10636                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10637                                                   tcg_passres, tcg_passres);
10638                 break;
10639             default:
10640                 g_assert_not_reached();
10641             }
10642 
10643             if (opcode == 9 || opcode == 11) {
10644                 /* saturating accumulate ops */
10645                 if (accop < 0) {
10646                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10647                 }
10648                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10649                                                   tcg_res[pass], tcg_passres);
10650             } else if (accop > 0) {
10651                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10652             } else if (accop < 0) {
10653                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10654             }
10655         }
10656     } else {
10657         /* size 0 or 1, generally helper functions */
10658         for (pass = 0; pass < 2; pass++) {
10659             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10660             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10661             TCGv_i64 tcg_passres;
10662             int elt = pass + is_q * 2;
10663 
10664             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10665             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10666 
10667             if (accop == 0) {
10668                 tcg_passres = tcg_res[pass];
10669             } else {
10670                 tcg_passres = tcg_temp_new_i64();
10671             }
10672 
10673             switch (opcode) {
10674             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10675             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10676             {
10677                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10678                 static NeonGenWidenFn * const widenfns[2][2] = {
10679                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10680                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10681                 };
10682                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10683 
10684                 widenfn(tcg_op2_64, tcg_op2);
10685                 widenfn(tcg_passres, tcg_op1);
10686                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10687                               tcg_passres, tcg_op2_64);
10688                 break;
10689             }
10690             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10691             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10692                 if (size == 0) {
10693                     if (is_u) {
10694                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10695                     } else {
10696                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10697                     }
10698                 } else {
10699                     if (is_u) {
10700                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10701                     } else {
10702                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10703                     }
10704                 }
10705                 break;
10706             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10707             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10708             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10709                 if (size == 0) {
10710                     if (is_u) {
10711                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10712                     } else {
10713                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10714                     }
10715                 } else {
10716                     if (is_u) {
10717                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10718                     } else {
10719                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10720                     }
10721                 }
10722                 break;
10723             case 9: /* SQDMLAL, SQDMLAL2 */
10724             case 11: /* SQDMLSL, SQDMLSL2 */
10725             case 13: /* SQDMULL, SQDMULL2 */
10726                 assert(size == 1);
10727                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10728                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10729                                                   tcg_passres, tcg_passres);
10730                 break;
10731             default:
10732                 g_assert_not_reached();
10733             }
10734 
10735             if (accop != 0) {
10736                 if (opcode == 9 || opcode == 11) {
10737                     /* saturating accumulate ops */
10738                     if (accop < 0) {
10739                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10740                     }
10741                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10742                                                       tcg_res[pass],
10743                                                       tcg_passres);
10744                 } else {
10745                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10746                                   tcg_res[pass], tcg_passres);
10747                 }
10748             }
10749         }
10750     }
10751 
10752     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10753     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10754 }
10755 
10756 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10757                             int opcode, int rd, int rn, int rm)
10758 {
10759     TCGv_i64 tcg_res[2];
10760     int part = is_q ? 2 : 0;
10761     int pass;
10762 
10763     for (pass = 0; pass < 2; pass++) {
10764         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10765         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10766         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10767         static NeonGenWidenFn * const widenfns[3][2] = {
10768             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10769             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10770             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10771         };
10772         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10773 
10774         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10775         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10776         widenfn(tcg_op2_wide, tcg_op2);
10777         tcg_res[pass] = tcg_temp_new_i64();
10778         gen_neon_addl(size, (opcode == 3),
10779                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10780     }
10781 
10782     for (pass = 0; pass < 2; pass++) {
10783         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10784     }
10785 }
10786 
10787 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10788 {
10789     tcg_gen_addi_i64(in, in, 1U << 31);
10790     tcg_gen_extrh_i64_i32(res, in);
10791 }
10792 
10793 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10794                                  int opcode, int rd, int rn, int rm)
10795 {
10796     TCGv_i32 tcg_res[2];
10797     int part = is_q ? 2 : 0;
10798     int pass;
10799 
10800     for (pass = 0; pass < 2; pass++) {
10801         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10802         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10803         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10804         static NeonGenNarrowFn * const narrowfns[3][2] = {
10805             { gen_helper_neon_narrow_high_u8,
10806               gen_helper_neon_narrow_round_high_u8 },
10807             { gen_helper_neon_narrow_high_u16,
10808               gen_helper_neon_narrow_round_high_u16 },
10809             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10810         };
10811         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10812 
10813         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10814         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10815 
10816         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10817 
10818         tcg_res[pass] = tcg_temp_new_i32();
10819         gennarrow(tcg_res[pass], tcg_wideres);
10820     }
10821 
10822     for (pass = 0; pass < 2; pass++) {
10823         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10824     }
10825     clear_vec_high(s, is_q, rd);
10826 }
10827 
10828 /* AdvSIMD three different
10829  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10830  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10831  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10832  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10833  */
10834 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10835 {
10836     /* Instructions in this group fall into three basic classes
10837      * (in each case with the operation working on each element in
10838      * the input vectors):
10839      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10840      *     128 bit input)
10841      * (2) wide 64 x 128 -> 128
10842      * (3) narrowing 128 x 128 -> 64
10843      * Here we do initial decode, catch unallocated cases and
10844      * dispatch to separate functions for each class.
10845      */
10846     int is_q = extract32(insn, 30, 1);
10847     int is_u = extract32(insn, 29, 1);
10848     int size = extract32(insn, 22, 2);
10849     int opcode = extract32(insn, 12, 4);
10850     int rm = extract32(insn, 16, 5);
10851     int rn = extract32(insn, 5, 5);
10852     int rd = extract32(insn, 0, 5);
10853 
10854     switch (opcode) {
10855     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10856     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10857         /* 64 x 128 -> 128 */
10858         if (size == 3) {
10859             unallocated_encoding(s);
10860             return;
10861         }
10862         if (!fp_access_check(s)) {
10863             return;
10864         }
10865         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10866         break;
10867     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10868     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10869         /* 128 x 128 -> 64 */
10870         if (size == 3) {
10871             unallocated_encoding(s);
10872             return;
10873         }
10874         if (!fp_access_check(s)) {
10875             return;
10876         }
10877         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10878         break;
10879     case 14: /* PMULL, PMULL2 */
10880         if (is_u) {
10881             unallocated_encoding(s);
10882             return;
10883         }
10884         switch (size) {
10885         case 0: /* PMULL.P8 */
10886             if (!fp_access_check(s)) {
10887                 return;
10888             }
10889             /* The Q field specifies lo/hi half input for this insn.  */
10890             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10891                              gen_helper_neon_pmull_h);
10892             break;
10893 
10894         case 3: /* PMULL.P64 */
10895             if (!dc_isar_feature(aa64_pmull, s)) {
10896                 unallocated_encoding(s);
10897                 return;
10898             }
10899             if (!fp_access_check(s)) {
10900                 return;
10901             }
10902             /* The Q field specifies lo/hi half input for this insn.  */
10903             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10904                              gen_helper_gvec_pmull_q);
10905             break;
10906 
10907         default:
10908             unallocated_encoding(s);
10909             break;
10910         }
10911         return;
10912     case 9: /* SQDMLAL, SQDMLAL2 */
10913     case 11: /* SQDMLSL, SQDMLSL2 */
10914     case 13: /* SQDMULL, SQDMULL2 */
10915         if (is_u || size == 0) {
10916             unallocated_encoding(s);
10917             return;
10918         }
10919         /* fall through */
10920     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10921     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10922     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10923     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10924     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10925     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10926     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10927         /* 64 x 64 -> 128 */
10928         if (size == 3) {
10929             unallocated_encoding(s);
10930             return;
10931         }
10932         if (!fp_access_check(s)) {
10933             return;
10934         }
10935 
10936         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10937         break;
10938     default:
10939         /* opcode 15 not allocated */
10940         unallocated_encoding(s);
10941         break;
10942     }
10943 }
10944 
10945 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10946 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10947 {
10948     int rd = extract32(insn, 0, 5);
10949     int rn = extract32(insn, 5, 5);
10950     int rm = extract32(insn, 16, 5);
10951     int size = extract32(insn, 22, 2);
10952     bool is_u = extract32(insn, 29, 1);
10953     bool is_q = extract32(insn, 30, 1);
10954 
10955     if (!fp_access_check(s)) {
10956         return;
10957     }
10958 
10959     switch (size + 4 * is_u) {
10960     case 0: /* AND */
10961         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10962         return;
10963     case 1: /* BIC */
10964         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10965         return;
10966     case 2: /* ORR */
10967         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10968         return;
10969     case 3: /* ORN */
10970         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10971         return;
10972     case 4: /* EOR */
10973         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10974         return;
10975 
10976     case 5: /* BSL bitwise select */
10977         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10978         return;
10979     case 6: /* BIT, bitwise insert if true */
10980         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10981         return;
10982     case 7: /* BIF, bitwise insert if false */
10983         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10984         return;
10985 
10986     default:
10987         g_assert_not_reached();
10988     }
10989 }
10990 
10991 /* Pairwise op subgroup of C3.6.16.
10992  *
10993  * This is called directly or via the handle_3same_float for float pairwise
10994  * operations where the opcode and size are calculated differently.
10995  */
10996 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10997                                    int size, int rn, int rm, int rd)
10998 {
10999     TCGv_ptr fpst;
11000     int pass;
11001 
11002     /* Floating point operations need fpst */
11003     if (opcode >= 0x58) {
11004         fpst = fpstatus_ptr(FPST_FPCR);
11005     } else {
11006         fpst = NULL;
11007     }
11008 
11009     if (!fp_access_check(s)) {
11010         return;
11011     }
11012 
11013     /* These operations work on the concatenated rm:rn, with each pair of
11014      * adjacent elements being operated on to produce an element in the result.
11015      */
11016     if (size == 3) {
11017         TCGv_i64 tcg_res[2];
11018 
11019         for (pass = 0; pass < 2; pass++) {
11020             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11021             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11022             int passreg = (pass == 0) ? rn : rm;
11023 
11024             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11025             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11026             tcg_res[pass] = tcg_temp_new_i64();
11027 
11028             switch (opcode) {
11029             case 0x17: /* ADDP */
11030                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11031                 break;
11032             case 0x58: /* FMAXNMP */
11033                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11034                 break;
11035             case 0x5a: /* FADDP */
11036                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11037                 break;
11038             case 0x5e: /* FMAXP */
11039                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11040                 break;
11041             case 0x78: /* FMINNMP */
11042                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11043                 break;
11044             case 0x7e: /* FMINP */
11045                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11046                 break;
11047             default:
11048                 g_assert_not_reached();
11049             }
11050         }
11051 
11052         for (pass = 0; pass < 2; pass++) {
11053             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11054         }
11055     } else {
11056         int maxpass = is_q ? 4 : 2;
11057         TCGv_i32 tcg_res[4];
11058 
11059         for (pass = 0; pass < maxpass; pass++) {
11060             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11061             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11062             NeonGenTwoOpFn *genfn = NULL;
11063             int passreg = pass < (maxpass / 2) ? rn : rm;
11064             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11065 
11066             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11067             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11068             tcg_res[pass] = tcg_temp_new_i32();
11069 
11070             switch (opcode) {
11071             case 0x17: /* ADDP */
11072             {
11073                 static NeonGenTwoOpFn * const fns[3] = {
11074                     gen_helper_neon_padd_u8,
11075                     gen_helper_neon_padd_u16,
11076                     tcg_gen_add_i32,
11077                 };
11078                 genfn = fns[size];
11079                 break;
11080             }
11081             case 0x14: /* SMAXP, UMAXP */
11082             {
11083                 static NeonGenTwoOpFn * const fns[3][2] = {
11084                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11085                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11086                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11087                 };
11088                 genfn = fns[size][u];
11089                 break;
11090             }
11091             case 0x15: /* SMINP, UMINP */
11092             {
11093                 static NeonGenTwoOpFn * const fns[3][2] = {
11094                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11095                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11096                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11097                 };
11098                 genfn = fns[size][u];
11099                 break;
11100             }
11101             /* The FP operations are all on single floats (32 bit) */
11102             case 0x58: /* FMAXNMP */
11103                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11104                 break;
11105             case 0x5a: /* FADDP */
11106                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11107                 break;
11108             case 0x5e: /* FMAXP */
11109                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11110                 break;
11111             case 0x78: /* FMINNMP */
11112                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11113                 break;
11114             case 0x7e: /* FMINP */
11115                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11116                 break;
11117             default:
11118                 g_assert_not_reached();
11119             }
11120 
11121             /* FP ops called directly, otherwise call now */
11122             if (genfn) {
11123                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11124             }
11125         }
11126 
11127         for (pass = 0; pass < maxpass; pass++) {
11128             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11129         }
11130         clear_vec_high(s, is_q, rd);
11131     }
11132 }
11133 
11134 /* Floating point op subgroup of C3.6.16. */
11135 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11136 {
11137     /* For floating point ops, the U, size[1] and opcode bits
11138      * together indicate the operation. size[0] indicates single
11139      * or double.
11140      */
11141     int fpopcode = extract32(insn, 11, 5)
11142         | (extract32(insn, 23, 1) << 5)
11143         | (extract32(insn, 29, 1) << 6);
11144     int is_q = extract32(insn, 30, 1);
11145     int size = extract32(insn, 22, 1);
11146     int rm = extract32(insn, 16, 5);
11147     int rn = extract32(insn, 5, 5);
11148     int rd = extract32(insn, 0, 5);
11149 
11150     int datasize = is_q ? 128 : 64;
11151     int esize = 32 << size;
11152     int elements = datasize / esize;
11153 
11154     if (size == 1 && !is_q) {
11155         unallocated_encoding(s);
11156         return;
11157     }
11158 
11159     switch (fpopcode) {
11160     case 0x58: /* FMAXNMP */
11161     case 0x5a: /* FADDP */
11162     case 0x5e: /* FMAXP */
11163     case 0x78: /* FMINNMP */
11164     case 0x7e: /* FMINP */
11165         if (size && !is_q) {
11166             unallocated_encoding(s);
11167             return;
11168         }
11169         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11170                                rn, rm, rd);
11171         return;
11172     case 0x1b: /* FMULX */
11173     case 0x1f: /* FRECPS */
11174     case 0x3f: /* FRSQRTS */
11175     case 0x5d: /* FACGE */
11176     case 0x7d: /* FACGT */
11177     case 0x19: /* FMLA */
11178     case 0x39: /* FMLS */
11179     case 0x18: /* FMAXNM */
11180     case 0x1a: /* FADD */
11181     case 0x1c: /* FCMEQ */
11182     case 0x1e: /* FMAX */
11183     case 0x38: /* FMINNM */
11184     case 0x3a: /* FSUB */
11185     case 0x3e: /* FMIN */
11186     case 0x5b: /* FMUL */
11187     case 0x5c: /* FCMGE */
11188     case 0x5f: /* FDIV */
11189     case 0x7a: /* FABD */
11190     case 0x7c: /* FCMGT */
11191         if (!fp_access_check(s)) {
11192             return;
11193         }
11194         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11195         return;
11196 
11197     case 0x1d: /* FMLAL  */
11198     case 0x3d: /* FMLSL  */
11199     case 0x59: /* FMLAL2 */
11200     case 0x79: /* FMLSL2 */
11201         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11202             unallocated_encoding(s);
11203             return;
11204         }
11205         if (fp_access_check(s)) {
11206             int is_s = extract32(insn, 23, 1);
11207             int is_2 = extract32(insn, 29, 1);
11208             int data = (is_2 << 1) | is_s;
11209             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11210                                vec_full_reg_offset(s, rn),
11211                                vec_full_reg_offset(s, rm), cpu_env,
11212                                is_q ? 16 : 8, vec_full_reg_size(s),
11213                                data, gen_helper_gvec_fmlal_a64);
11214         }
11215         return;
11216 
11217     default:
11218         unallocated_encoding(s);
11219         return;
11220     }
11221 }
11222 
11223 /* Integer op subgroup of C3.6.16. */
11224 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11225 {
11226     int is_q = extract32(insn, 30, 1);
11227     int u = extract32(insn, 29, 1);
11228     int size = extract32(insn, 22, 2);
11229     int opcode = extract32(insn, 11, 5);
11230     int rm = extract32(insn, 16, 5);
11231     int rn = extract32(insn, 5, 5);
11232     int rd = extract32(insn, 0, 5);
11233     int pass;
11234     TCGCond cond;
11235 
11236     switch (opcode) {
11237     case 0x13: /* MUL, PMUL */
11238         if (u && size != 0) {
11239             unallocated_encoding(s);
11240             return;
11241         }
11242         /* fall through */
11243     case 0x0: /* SHADD, UHADD */
11244     case 0x2: /* SRHADD, URHADD */
11245     case 0x4: /* SHSUB, UHSUB */
11246     case 0xc: /* SMAX, UMAX */
11247     case 0xd: /* SMIN, UMIN */
11248     case 0xe: /* SABD, UABD */
11249     case 0xf: /* SABA, UABA */
11250     case 0x12: /* MLA, MLS */
11251         if (size == 3) {
11252             unallocated_encoding(s);
11253             return;
11254         }
11255         break;
11256     case 0x16: /* SQDMULH, SQRDMULH */
11257         if (size == 0 || size == 3) {
11258             unallocated_encoding(s);
11259             return;
11260         }
11261         break;
11262     default:
11263         if (size == 3 && !is_q) {
11264             unallocated_encoding(s);
11265             return;
11266         }
11267         break;
11268     }
11269 
11270     if (!fp_access_check(s)) {
11271         return;
11272     }
11273 
11274     switch (opcode) {
11275     case 0x01: /* SQADD, UQADD */
11276         if (u) {
11277             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11278         } else {
11279             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11280         }
11281         return;
11282     case 0x05: /* SQSUB, UQSUB */
11283         if (u) {
11284             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11285         } else {
11286             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11287         }
11288         return;
11289     case 0x08: /* SSHL, USHL */
11290         if (u) {
11291             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11292         } else {
11293             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11294         }
11295         return;
11296     case 0x0c: /* SMAX, UMAX */
11297         if (u) {
11298             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11299         } else {
11300             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11301         }
11302         return;
11303     case 0x0d: /* SMIN, UMIN */
11304         if (u) {
11305             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11306         } else {
11307             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11308         }
11309         return;
11310     case 0xe: /* SABD, UABD */
11311         if (u) {
11312             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11313         } else {
11314             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11315         }
11316         return;
11317     case 0xf: /* SABA, UABA */
11318         if (u) {
11319             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11320         } else {
11321             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11322         }
11323         return;
11324     case 0x10: /* ADD, SUB */
11325         if (u) {
11326             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11327         } else {
11328             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11329         }
11330         return;
11331     case 0x13: /* MUL, PMUL */
11332         if (!u) { /* MUL */
11333             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11334         } else {  /* PMUL */
11335             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11336         }
11337         return;
11338     case 0x12: /* MLA, MLS */
11339         if (u) {
11340             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11341         } else {
11342             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11343         }
11344         return;
11345     case 0x16: /* SQDMULH, SQRDMULH */
11346         {
11347             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11348                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11349                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11350             };
11351             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11352         }
11353         return;
11354     case 0x11:
11355         if (!u) { /* CMTST */
11356             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11357             return;
11358         }
11359         /* else CMEQ */
11360         cond = TCG_COND_EQ;
11361         goto do_gvec_cmp;
11362     case 0x06: /* CMGT, CMHI */
11363         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11364         goto do_gvec_cmp;
11365     case 0x07: /* CMGE, CMHS */
11366         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11367     do_gvec_cmp:
11368         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11369                          vec_full_reg_offset(s, rn),
11370                          vec_full_reg_offset(s, rm),
11371                          is_q ? 16 : 8, vec_full_reg_size(s));
11372         return;
11373     }
11374 
11375     if (size == 3) {
11376         assert(is_q);
11377         for (pass = 0; pass < 2; pass++) {
11378             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11379             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11380             TCGv_i64 tcg_res = tcg_temp_new_i64();
11381 
11382             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11383             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11384 
11385             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11386 
11387             write_vec_element(s, tcg_res, rd, pass, MO_64);
11388         }
11389     } else {
11390         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11391             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11392             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11393             TCGv_i32 tcg_res = tcg_temp_new_i32();
11394             NeonGenTwoOpFn *genfn = NULL;
11395             NeonGenTwoOpEnvFn *genenvfn = NULL;
11396 
11397             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11398             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11399 
11400             switch (opcode) {
11401             case 0x0: /* SHADD, UHADD */
11402             {
11403                 static NeonGenTwoOpFn * const fns[3][2] = {
11404                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11405                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11406                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11407                 };
11408                 genfn = fns[size][u];
11409                 break;
11410             }
11411             case 0x2: /* SRHADD, URHADD */
11412             {
11413                 static NeonGenTwoOpFn * const fns[3][2] = {
11414                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11415                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11416                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11417                 };
11418                 genfn = fns[size][u];
11419                 break;
11420             }
11421             case 0x4: /* SHSUB, UHSUB */
11422             {
11423                 static NeonGenTwoOpFn * const fns[3][2] = {
11424                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11425                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11426                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11427                 };
11428                 genfn = fns[size][u];
11429                 break;
11430             }
11431             case 0x9: /* SQSHL, UQSHL */
11432             {
11433                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11434                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11435                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11436                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11437                 };
11438                 genenvfn = fns[size][u];
11439                 break;
11440             }
11441             case 0xa: /* SRSHL, URSHL */
11442             {
11443                 static NeonGenTwoOpFn * const fns[3][2] = {
11444                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11445                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11446                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11447                 };
11448                 genfn = fns[size][u];
11449                 break;
11450             }
11451             case 0xb: /* SQRSHL, UQRSHL */
11452             {
11453                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11454                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11455                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11456                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11457                 };
11458                 genenvfn = fns[size][u];
11459                 break;
11460             }
11461             default:
11462                 g_assert_not_reached();
11463             }
11464 
11465             if (genenvfn) {
11466                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11467             } else {
11468                 genfn(tcg_res, tcg_op1, tcg_op2);
11469             }
11470 
11471             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11472         }
11473     }
11474     clear_vec_high(s, is_q, rd);
11475 }
11476 
11477 /* AdvSIMD three same
11478  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11479  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11480  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11481  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11482  */
11483 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11484 {
11485     int opcode = extract32(insn, 11, 5);
11486 
11487     switch (opcode) {
11488     case 0x3: /* logic ops */
11489         disas_simd_3same_logic(s, insn);
11490         break;
11491     case 0x17: /* ADDP */
11492     case 0x14: /* SMAXP, UMAXP */
11493     case 0x15: /* SMINP, UMINP */
11494     {
11495         /* Pairwise operations */
11496         int is_q = extract32(insn, 30, 1);
11497         int u = extract32(insn, 29, 1);
11498         int size = extract32(insn, 22, 2);
11499         int rm = extract32(insn, 16, 5);
11500         int rn = extract32(insn, 5, 5);
11501         int rd = extract32(insn, 0, 5);
11502         if (opcode == 0x17) {
11503             if (u || (size == 3 && !is_q)) {
11504                 unallocated_encoding(s);
11505                 return;
11506             }
11507         } else {
11508             if (size == 3) {
11509                 unallocated_encoding(s);
11510                 return;
11511             }
11512         }
11513         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11514         break;
11515     }
11516     case 0x18 ... 0x31:
11517         /* floating point ops, sz[1] and U are part of opcode */
11518         disas_simd_3same_float(s, insn);
11519         break;
11520     default:
11521         disas_simd_3same_int(s, insn);
11522         break;
11523     }
11524 }
11525 
11526 /*
11527  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11528  *
11529  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11530  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11531  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11532  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11533  *
11534  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11535  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11536  *
11537  */
11538 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11539 {
11540     int opcode = extract32(insn, 11, 3);
11541     int u = extract32(insn, 29, 1);
11542     int a = extract32(insn, 23, 1);
11543     int is_q = extract32(insn, 30, 1);
11544     int rm = extract32(insn, 16, 5);
11545     int rn = extract32(insn, 5, 5);
11546     int rd = extract32(insn, 0, 5);
11547     /*
11548      * For these floating point ops, the U, a and opcode bits
11549      * together indicate the operation.
11550      */
11551     int fpopcode = opcode | (a << 3) | (u << 4);
11552     int datasize = is_q ? 128 : 64;
11553     int elements = datasize / 16;
11554     bool pairwise;
11555     TCGv_ptr fpst;
11556     int pass;
11557 
11558     switch (fpopcode) {
11559     case 0x0: /* FMAXNM */
11560     case 0x1: /* FMLA */
11561     case 0x2: /* FADD */
11562     case 0x3: /* FMULX */
11563     case 0x4: /* FCMEQ */
11564     case 0x6: /* FMAX */
11565     case 0x7: /* FRECPS */
11566     case 0x8: /* FMINNM */
11567     case 0x9: /* FMLS */
11568     case 0xa: /* FSUB */
11569     case 0xe: /* FMIN */
11570     case 0xf: /* FRSQRTS */
11571     case 0x13: /* FMUL */
11572     case 0x14: /* FCMGE */
11573     case 0x15: /* FACGE */
11574     case 0x17: /* FDIV */
11575     case 0x1a: /* FABD */
11576     case 0x1c: /* FCMGT */
11577     case 0x1d: /* FACGT */
11578         pairwise = false;
11579         break;
11580     case 0x10: /* FMAXNMP */
11581     case 0x12: /* FADDP */
11582     case 0x16: /* FMAXP */
11583     case 0x18: /* FMINNMP */
11584     case 0x1e: /* FMINP */
11585         pairwise = true;
11586         break;
11587     default:
11588         unallocated_encoding(s);
11589         return;
11590     }
11591 
11592     if (!dc_isar_feature(aa64_fp16, s)) {
11593         unallocated_encoding(s);
11594         return;
11595     }
11596 
11597     if (!fp_access_check(s)) {
11598         return;
11599     }
11600 
11601     fpst = fpstatus_ptr(FPST_FPCR_F16);
11602 
11603     if (pairwise) {
11604         int maxpass = is_q ? 8 : 4;
11605         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11606         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11607         TCGv_i32 tcg_res[8];
11608 
11609         for (pass = 0; pass < maxpass; pass++) {
11610             int passreg = pass < (maxpass / 2) ? rn : rm;
11611             int passelt = (pass << 1) & (maxpass - 1);
11612 
11613             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11614             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11615             tcg_res[pass] = tcg_temp_new_i32();
11616 
11617             switch (fpopcode) {
11618             case 0x10: /* FMAXNMP */
11619                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11620                                            fpst);
11621                 break;
11622             case 0x12: /* FADDP */
11623                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11624                 break;
11625             case 0x16: /* FMAXP */
11626                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11627                 break;
11628             case 0x18: /* FMINNMP */
11629                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11630                                            fpst);
11631                 break;
11632             case 0x1e: /* FMINP */
11633                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11634                 break;
11635             default:
11636                 g_assert_not_reached();
11637             }
11638         }
11639 
11640         for (pass = 0; pass < maxpass; pass++) {
11641             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11642         }
11643     } else {
11644         for (pass = 0; pass < elements; pass++) {
11645             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11646             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11647             TCGv_i32 tcg_res = tcg_temp_new_i32();
11648 
11649             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11650             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11651 
11652             switch (fpopcode) {
11653             case 0x0: /* FMAXNM */
11654                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11655                 break;
11656             case 0x1: /* FMLA */
11657                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11658                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11659                                            fpst);
11660                 break;
11661             case 0x2: /* FADD */
11662                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11663                 break;
11664             case 0x3: /* FMULX */
11665                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11666                 break;
11667             case 0x4: /* FCMEQ */
11668                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11669                 break;
11670             case 0x6: /* FMAX */
11671                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11672                 break;
11673             case 0x7: /* FRECPS */
11674                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11675                 break;
11676             case 0x8: /* FMINNM */
11677                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11678                 break;
11679             case 0x9: /* FMLS */
11680                 /* As usual for ARM, separate negation for fused multiply-add */
11681                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11682                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11683                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11684                                            fpst);
11685                 break;
11686             case 0xa: /* FSUB */
11687                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11688                 break;
11689             case 0xe: /* FMIN */
11690                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11691                 break;
11692             case 0xf: /* FRSQRTS */
11693                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11694                 break;
11695             case 0x13: /* FMUL */
11696                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11697                 break;
11698             case 0x14: /* FCMGE */
11699                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11700                 break;
11701             case 0x15: /* FACGE */
11702                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11703                 break;
11704             case 0x17: /* FDIV */
11705                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11706                 break;
11707             case 0x1a: /* FABD */
11708                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11709                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11710                 break;
11711             case 0x1c: /* FCMGT */
11712                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11713                 break;
11714             case 0x1d: /* FACGT */
11715                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11716                 break;
11717             default:
11718                 g_assert_not_reached();
11719             }
11720 
11721             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11722         }
11723     }
11724 
11725     clear_vec_high(s, is_q, rd);
11726 }
11727 
11728 /* AdvSIMD three same extra
11729  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11730  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11731  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11732  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11733  */
11734 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11735 {
11736     int rd = extract32(insn, 0, 5);
11737     int rn = extract32(insn, 5, 5);
11738     int opcode = extract32(insn, 11, 4);
11739     int rm = extract32(insn, 16, 5);
11740     int size = extract32(insn, 22, 2);
11741     bool u = extract32(insn, 29, 1);
11742     bool is_q = extract32(insn, 30, 1);
11743     bool feature;
11744     int rot;
11745 
11746     switch (u * 16 + opcode) {
11747     case 0x10: /* SQRDMLAH (vector) */
11748     case 0x11: /* SQRDMLSH (vector) */
11749         if (size != 1 && size != 2) {
11750             unallocated_encoding(s);
11751             return;
11752         }
11753         feature = dc_isar_feature(aa64_rdm, s);
11754         break;
11755     case 0x02: /* SDOT (vector) */
11756     case 0x12: /* UDOT (vector) */
11757         if (size != MO_32) {
11758             unallocated_encoding(s);
11759             return;
11760         }
11761         feature = dc_isar_feature(aa64_dp, s);
11762         break;
11763     case 0x03: /* USDOT */
11764         if (size != MO_32) {
11765             unallocated_encoding(s);
11766             return;
11767         }
11768         feature = dc_isar_feature(aa64_i8mm, s);
11769         break;
11770     case 0x04: /* SMMLA */
11771     case 0x14: /* UMMLA */
11772     case 0x05: /* USMMLA */
11773         if (!is_q || size != MO_32) {
11774             unallocated_encoding(s);
11775             return;
11776         }
11777         feature = dc_isar_feature(aa64_i8mm, s);
11778         break;
11779     case 0x18: /* FCMLA, #0 */
11780     case 0x19: /* FCMLA, #90 */
11781     case 0x1a: /* FCMLA, #180 */
11782     case 0x1b: /* FCMLA, #270 */
11783     case 0x1c: /* FCADD, #90 */
11784     case 0x1e: /* FCADD, #270 */
11785         if (size == 0
11786             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11787             || (size == 3 && !is_q)) {
11788             unallocated_encoding(s);
11789             return;
11790         }
11791         feature = dc_isar_feature(aa64_fcma, s);
11792         break;
11793     case 0x1d: /* BFMMLA */
11794         if (size != MO_16 || !is_q) {
11795             unallocated_encoding(s);
11796             return;
11797         }
11798         feature = dc_isar_feature(aa64_bf16, s);
11799         break;
11800     case 0x1f:
11801         switch (size) {
11802         case 1: /* BFDOT */
11803         case 3: /* BFMLAL{B,T} */
11804             feature = dc_isar_feature(aa64_bf16, s);
11805             break;
11806         default:
11807             unallocated_encoding(s);
11808             return;
11809         }
11810         break;
11811     default:
11812         unallocated_encoding(s);
11813         return;
11814     }
11815     if (!feature) {
11816         unallocated_encoding(s);
11817         return;
11818     }
11819     if (!fp_access_check(s)) {
11820         return;
11821     }
11822 
11823     switch (opcode) {
11824     case 0x0: /* SQRDMLAH (vector) */
11825         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11826         return;
11827 
11828     case 0x1: /* SQRDMLSH (vector) */
11829         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11830         return;
11831 
11832     case 0x2: /* SDOT / UDOT */
11833         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11834                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11835         return;
11836 
11837     case 0x3: /* USDOT */
11838         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11839         return;
11840 
11841     case 0x04: /* SMMLA, UMMLA */
11842         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11843                          u ? gen_helper_gvec_ummla_b
11844                          : gen_helper_gvec_smmla_b);
11845         return;
11846     case 0x05: /* USMMLA */
11847         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11848         return;
11849 
11850     case 0x8: /* FCMLA, #0 */
11851     case 0x9: /* FCMLA, #90 */
11852     case 0xa: /* FCMLA, #180 */
11853     case 0xb: /* FCMLA, #270 */
11854         rot = extract32(opcode, 0, 2);
11855         switch (size) {
11856         case 1:
11857             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11858                               gen_helper_gvec_fcmlah);
11859             break;
11860         case 2:
11861             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11862                               gen_helper_gvec_fcmlas);
11863             break;
11864         case 3:
11865             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11866                               gen_helper_gvec_fcmlad);
11867             break;
11868         default:
11869             g_assert_not_reached();
11870         }
11871         return;
11872 
11873     case 0xc: /* FCADD, #90 */
11874     case 0xe: /* FCADD, #270 */
11875         rot = extract32(opcode, 1, 1);
11876         switch (size) {
11877         case 1:
11878             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11879                               gen_helper_gvec_fcaddh);
11880             break;
11881         case 2:
11882             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11883                               gen_helper_gvec_fcadds);
11884             break;
11885         case 3:
11886             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11887                               gen_helper_gvec_fcaddd);
11888             break;
11889         default:
11890             g_assert_not_reached();
11891         }
11892         return;
11893 
11894     case 0xd: /* BFMMLA */
11895         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11896         return;
11897     case 0xf:
11898         switch (size) {
11899         case 1: /* BFDOT */
11900             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11901             break;
11902         case 3: /* BFMLAL{B,T} */
11903             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11904                               gen_helper_gvec_bfmlal);
11905             break;
11906         default:
11907             g_assert_not_reached();
11908         }
11909         return;
11910 
11911     default:
11912         g_assert_not_reached();
11913     }
11914 }
11915 
11916 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11917                                   int size, int rn, int rd)
11918 {
11919     /* Handle 2-reg-misc ops which are widening (so each size element
11920      * in the source becomes a 2*size element in the destination.
11921      * The only instruction like this is FCVTL.
11922      */
11923     int pass;
11924 
11925     if (size == 3) {
11926         /* 32 -> 64 bit fp conversion */
11927         TCGv_i64 tcg_res[2];
11928         int srcelt = is_q ? 2 : 0;
11929 
11930         for (pass = 0; pass < 2; pass++) {
11931             TCGv_i32 tcg_op = tcg_temp_new_i32();
11932             tcg_res[pass] = tcg_temp_new_i64();
11933 
11934             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11935             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11936         }
11937         for (pass = 0; pass < 2; pass++) {
11938             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11939         }
11940     } else {
11941         /* 16 -> 32 bit fp conversion */
11942         int srcelt = is_q ? 4 : 0;
11943         TCGv_i32 tcg_res[4];
11944         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11945         TCGv_i32 ahp = get_ahp_flag();
11946 
11947         for (pass = 0; pass < 4; pass++) {
11948             tcg_res[pass] = tcg_temp_new_i32();
11949 
11950             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11951             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11952                                            fpst, ahp);
11953         }
11954         for (pass = 0; pass < 4; pass++) {
11955             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11956         }
11957     }
11958 }
11959 
11960 static void handle_rev(DisasContext *s, int opcode, bool u,
11961                        bool is_q, int size, int rn, int rd)
11962 {
11963     int op = (opcode << 1) | u;
11964     int opsz = op + size;
11965     int grp_size = 3 - opsz;
11966     int dsize = is_q ? 128 : 64;
11967     int i;
11968 
11969     if (opsz >= 3) {
11970         unallocated_encoding(s);
11971         return;
11972     }
11973 
11974     if (!fp_access_check(s)) {
11975         return;
11976     }
11977 
11978     if (size == 0) {
11979         /* Special case bytes, use bswap op on each group of elements */
11980         int groups = dsize / (8 << grp_size);
11981 
11982         for (i = 0; i < groups; i++) {
11983             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11984 
11985             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11986             switch (grp_size) {
11987             case MO_16:
11988                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11989                 break;
11990             case MO_32:
11991                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11992                 break;
11993             case MO_64:
11994                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11995                 break;
11996             default:
11997                 g_assert_not_reached();
11998             }
11999             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12000         }
12001         clear_vec_high(s, is_q, rd);
12002     } else {
12003         int revmask = (1 << grp_size) - 1;
12004         int esize = 8 << size;
12005         int elements = dsize / esize;
12006         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12007         TCGv_i64 tcg_rd[2];
12008 
12009         for (i = 0; i < 2; i++) {
12010             tcg_rd[i] = tcg_temp_new_i64();
12011             tcg_gen_movi_i64(tcg_rd[i], 0);
12012         }
12013 
12014         for (i = 0; i < elements; i++) {
12015             int e_rev = (i & 0xf) ^ revmask;
12016             int w = (e_rev * esize) / 64;
12017             int o = (e_rev * esize) % 64;
12018 
12019             read_vec_element(s, tcg_rn, rn, i, size);
12020             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12021         }
12022 
12023         for (i = 0; i < 2; i++) {
12024             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12025         }
12026         clear_vec_high(s, true, rd);
12027     }
12028 }
12029 
12030 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12031                                   bool is_q, int size, int rn, int rd)
12032 {
12033     /* Implement the pairwise operations from 2-misc:
12034      * SADDLP, UADDLP, SADALP, UADALP.
12035      * These all add pairs of elements in the input to produce a
12036      * double-width result element in the output (possibly accumulating).
12037      */
12038     bool accum = (opcode == 0x6);
12039     int maxpass = is_q ? 2 : 1;
12040     int pass;
12041     TCGv_i64 tcg_res[2];
12042 
12043     if (size == 2) {
12044         /* 32 + 32 -> 64 op */
12045         MemOp memop = size + (u ? 0 : MO_SIGN);
12046 
12047         for (pass = 0; pass < maxpass; pass++) {
12048             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12049             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12050 
12051             tcg_res[pass] = tcg_temp_new_i64();
12052 
12053             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12054             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12055             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12056             if (accum) {
12057                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12058                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12059             }
12060         }
12061     } else {
12062         for (pass = 0; pass < maxpass; pass++) {
12063             TCGv_i64 tcg_op = tcg_temp_new_i64();
12064             NeonGenOne64OpFn *genfn;
12065             static NeonGenOne64OpFn * const fns[2][2] = {
12066                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12067                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12068             };
12069 
12070             genfn = fns[size][u];
12071 
12072             tcg_res[pass] = tcg_temp_new_i64();
12073 
12074             read_vec_element(s, tcg_op, rn, pass, MO_64);
12075             genfn(tcg_res[pass], tcg_op);
12076 
12077             if (accum) {
12078                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12079                 if (size == 0) {
12080                     gen_helper_neon_addl_u16(tcg_res[pass],
12081                                              tcg_res[pass], tcg_op);
12082                 } else {
12083                     gen_helper_neon_addl_u32(tcg_res[pass],
12084                                              tcg_res[pass], tcg_op);
12085                 }
12086             }
12087         }
12088     }
12089     if (!is_q) {
12090         tcg_res[1] = tcg_constant_i64(0);
12091     }
12092     for (pass = 0; pass < 2; pass++) {
12093         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12094     }
12095 }
12096 
12097 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12098 {
12099     /* Implement SHLL and SHLL2 */
12100     int pass;
12101     int part = is_q ? 2 : 0;
12102     TCGv_i64 tcg_res[2];
12103 
12104     for (pass = 0; pass < 2; pass++) {
12105         static NeonGenWidenFn * const widenfns[3] = {
12106             gen_helper_neon_widen_u8,
12107             gen_helper_neon_widen_u16,
12108             tcg_gen_extu_i32_i64,
12109         };
12110         NeonGenWidenFn *widenfn = widenfns[size];
12111         TCGv_i32 tcg_op = tcg_temp_new_i32();
12112 
12113         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12114         tcg_res[pass] = tcg_temp_new_i64();
12115         widenfn(tcg_res[pass], tcg_op);
12116         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12117     }
12118 
12119     for (pass = 0; pass < 2; pass++) {
12120         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12121     }
12122 }
12123 
12124 /* AdvSIMD two reg misc
12125  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12126  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12127  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12128  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12129  */
12130 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12131 {
12132     int size = extract32(insn, 22, 2);
12133     int opcode = extract32(insn, 12, 5);
12134     bool u = extract32(insn, 29, 1);
12135     bool is_q = extract32(insn, 30, 1);
12136     int rn = extract32(insn, 5, 5);
12137     int rd = extract32(insn, 0, 5);
12138     bool need_fpstatus = false;
12139     int rmode = -1;
12140     TCGv_i32 tcg_rmode;
12141     TCGv_ptr tcg_fpstatus;
12142 
12143     switch (opcode) {
12144     case 0x0: /* REV64, REV32 */
12145     case 0x1: /* REV16 */
12146         handle_rev(s, opcode, u, is_q, size, rn, rd);
12147         return;
12148     case 0x5: /* CNT, NOT, RBIT */
12149         if (u && size == 0) {
12150             /* NOT */
12151             break;
12152         } else if (u && size == 1) {
12153             /* RBIT */
12154             break;
12155         } else if (!u && size == 0) {
12156             /* CNT */
12157             break;
12158         }
12159         unallocated_encoding(s);
12160         return;
12161     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12162     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12163         if (size == 3) {
12164             unallocated_encoding(s);
12165             return;
12166         }
12167         if (!fp_access_check(s)) {
12168             return;
12169         }
12170 
12171         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12172         return;
12173     case 0x4: /* CLS, CLZ */
12174         if (size == 3) {
12175             unallocated_encoding(s);
12176             return;
12177         }
12178         break;
12179     case 0x2: /* SADDLP, UADDLP */
12180     case 0x6: /* SADALP, UADALP */
12181         if (size == 3) {
12182             unallocated_encoding(s);
12183             return;
12184         }
12185         if (!fp_access_check(s)) {
12186             return;
12187         }
12188         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12189         return;
12190     case 0x13: /* SHLL, SHLL2 */
12191         if (u == 0 || size == 3) {
12192             unallocated_encoding(s);
12193             return;
12194         }
12195         if (!fp_access_check(s)) {
12196             return;
12197         }
12198         handle_shll(s, is_q, size, rn, rd);
12199         return;
12200     case 0xa: /* CMLT */
12201         if (u == 1) {
12202             unallocated_encoding(s);
12203             return;
12204         }
12205         /* fall through */
12206     case 0x8: /* CMGT, CMGE */
12207     case 0x9: /* CMEQ, CMLE */
12208     case 0xb: /* ABS, NEG */
12209         if (size == 3 && !is_q) {
12210             unallocated_encoding(s);
12211             return;
12212         }
12213         break;
12214     case 0x3: /* SUQADD, USQADD */
12215         if (size == 3 && !is_q) {
12216             unallocated_encoding(s);
12217             return;
12218         }
12219         if (!fp_access_check(s)) {
12220             return;
12221         }
12222         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12223         return;
12224     case 0x7: /* SQABS, SQNEG */
12225         if (size == 3 && !is_q) {
12226             unallocated_encoding(s);
12227             return;
12228         }
12229         break;
12230     case 0xc ... 0xf:
12231     case 0x16 ... 0x1f:
12232     {
12233         /* Floating point: U, size[1] and opcode indicate operation;
12234          * size[0] indicates single or double precision.
12235          */
12236         int is_double = extract32(size, 0, 1);
12237         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12238         size = is_double ? 3 : 2;
12239         switch (opcode) {
12240         case 0x2f: /* FABS */
12241         case 0x6f: /* FNEG */
12242             if (size == 3 && !is_q) {
12243                 unallocated_encoding(s);
12244                 return;
12245             }
12246             break;
12247         case 0x1d: /* SCVTF */
12248         case 0x5d: /* UCVTF */
12249         {
12250             bool is_signed = (opcode == 0x1d) ? true : false;
12251             int elements = is_double ? 2 : is_q ? 4 : 2;
12252             if (is_double && !is_q) {
12253                 unallocated_encoding(s);
12254                 return;
12255             }
12256             if (!fp_access_check(s)) {
12257                 return;
12258             }
12259             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12260             return;
12261         }
12262         case 0x2c: /* FCMGT (zero) */
12263         case 0x2d: /* FCMEQ (zero) */
12264         case 0x2e: /* FCMLT (zero) */
12265         case 0x6c: /* FCMGE (zero) */
12266         case 0x6d: /* FCMLE (zero) */
12267             if (size == 3 && !is_q) {
12268                 unallocated_encoding(s);
12269                 return;
12270             }
12271             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12272             return;
12273         case 0x7f: /* FSQRT */
12274             if (size == 3 && !is_q) {
12275                 unallocated_encoding(s);
12276                 return;
12277             }
12278             break;
12279         case 0x1a: /* FCVTNS */
12280         case 0x1b: /* FCVTMS */
12281         case 0x3a: /* FCVTPS */
12282         case 0x3b: /* FCVTZS */
12283         case 0x5a: /* FCVTNU */
12284         case 0x5b: /* FCVTMU */
12285         case 0x7a: /* FCVTPU */
12286         case 0x7b: /* FCVTZU */
12287             need_fpstatus = true;
12288             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12289             if (size == 3 && !is_q) {
12290                 unallocated_encoding(s);
12291                 return;
12292             }
12293             break;
12294         case 0x5c: /* FCVTAU */
12295         case 0x1c: /* FCVTAS */
12296             need_fpstatus = true;
12297             rmode = FPROUNDING_TIEAWAY;
12298             if (size == 3 && !is_q) {
12299                 unallocated_encoding(s);
12300                 return;
12301             }
12302             break;
12303         case 0x3c: /* URECPE */
12304             if (size == 3) {
12305                 unallocated_encoding(s);
12306                 return;
12307             }
12308             /* fall through */
12309         case 0x3d: /* FRECPE */
12310         case 0x7d: /* FRSQRTE */
12311             if (size == 3 && !is_q) {
12312                 unallocated_encoding(s);
12313                 return;
12314             }
12315             if (!fp_access_check(s)) {
12316                 return;
12317             }
12318             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12319             return;
12320         case 0x56: /* FCVTXN, FCVTXN2 */
12321             if (size == 2) {
12322                 unallocated_encoding(s);
12323                 return;
12324             }
12325             /* fall through */
12326         case 0x16: /* FCVTN, FCVTN2 */
12327             /* handle_2misc_narrow does a 2*size -> size operation, but these
12328              * instructions encode the source size rather than dest size.
12329              */
12330             if (!fp_access_check(s)) {
12331                 return;
12332             }
12333             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12334             return;
12335         case 0x36: /* BFCVTN, BFCVTN2 */
12336             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12337                 unallocated_encoding(s);
12338                 return;
12339             }
12340             if (!fp_access_check(s)) {
12341                 return;
12342             }
12343             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12344             return;
12345         case 0x17: /* FCVTL, FCVTL2 */
12346             if (!fp_access_check(s)) {
12347                 return;
12348             }
12349             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12350             return;
12351         case 0x18: /* FRINTN */
12352         case 0x19: /* FRINTM */
12353         case 0x38: /* FRINTP */
12354         case 0x39: /* FRINTZ */
12355             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12356             /* fall through */
12357         case 0x59: /* FRINTX */
12358         case 0x79: /* FRINTI */
12359             need_fpstatus = true;
12360             if (size == 3 && !is_q) {
12361                 unallocated_encoding(s);
12362                 return;
12363             }
12364             break;
12365         case 0x58: /* FRINTA */
12366             rmode = FPROUNDING_TIEAWAY;
12367             need_fpstatus = true;
12368             if (size == 3 && !is_q) {
12369                 unallocated_encoding(s);
12370                 return;
12371             }
12372             break;
12373         case 0x7c: /* URSQRTE */
12374             if (size == 3) {
12375                 unallocated_encoding(s);
12376                 return;
12377             }
12378             break;
12379         case 0x1e: /* FRINT32Z */
12380         case 0x1f: /* FRINT64Z */
12381             rmode = FPROUNDING_ZERO;
12382             /* fall through */
12383         case 0x5e: /* FRINT32X */
12384         case 0x5f: /* FRINT64X */
12385             need_fpstatus = true;
12386             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12387                 unallocated_encoding(s);
12388                 return;
12389             }
12390             break;
12391         default:
12392             unallocated_encoding(s);
12393             return;
12394         }
12395         break;
12396     }
12397     default:
12398         unallocated_encoding(s);
12399         return;
12400     }
12401 
12402     if (!fp_access_check(s)) {
12403         return;
12404     }
12405 
12406     if (need_fpstatus || rmode >= 0) {
12407         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12408     } else {
12409         tcg_fpstatus = NULL;
12410     }
12411     if (rmode >= 0) {
12412         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12413     } else {
12414         tcg_rmode = NULL;
12415     }
12416 
12417     switch (opcode) {
12418     case 0x5:
12419         if (u && size == 0) { /* NOT */
12420             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12421             return;
12422         }
12423         break;
12424     case 0x8: /* CMGT, CMGE */
12425         if (u) {
12426             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12427         } else {
12428             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12429         }
12430         return;
12431     case 0x9: /* CMEQ, CMLE */
12432         if (u) {
12433             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12434         } else {
12435             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12436         }
12437         return;
12438     case 0xa: /* CMLT */
12439         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12440         return;
12441     case 0xb:
12442         if (u) { /* ABS, NEG */
12443             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12444         } else {
12445             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12446         }
12447         return;
12448     }
12449 
12450     if (size == 3) {
12451         /* All 64-bit element operations can be shared with scalar 2misc */
12452         int pass;
12453 
12454         /* Coverity claims (size == 3 && !is_q) has been eliminated
12455          * from all paths leading to here.
12456          */
12457         tcg_debug_assert(is_q);
12458         for (pass = 0; pass < 2; pass++) {
12459             TCGv_i64 tcg_op = tcg_temp_new_i64();
12460             TCGv_i64 tcg_res = tcg_temp_new_i64();
12461 
12462             read_vec_element(s, tcg_op, rn, pass, MO_64);
12463 
12464             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12465                             tcg_rmode, tcg_fpstatus);
12466 
12467             write_vec_element(s, tcg_res, rd, pass, MO_64);
12468         }
12469     } else {
12470         int pass;
12471 
12472         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12473             TCGv_i32 tcg_op = tcg_temp_new_i32();
12474             TCGv_i32 tcg_res = tcg_temp_new_i32();
12475 
12476             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12477 
12478             if (size == 2) {
12479                 /* Special cases for 32 bit elements */
12480                 switch (opcode) {
12481                 case 0x4: /* CLS */
12482                     if (u) {
12483                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12484                     } else {
12485                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12486                     }
12487                     break;
12488                 case 0x7: /* SQABS, SQNEG */
12489                     if (u) {
12490                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12491                     } else {
12492                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12493                     }
12494                     break;
12495                 case 0x2f: /* FABS */
12496                     gen_helper_vfp_abss(tcg_res, tcg_op);
12497                     break;
12498                 case 0x6f: /* FNEG */
12499                     gen_helper_vfp_negs(tcg_res, tcg_op);
12500                     break;
12501                 case 0x7f: /* FSQRT */
12502                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12503                     break;
12504                 case 0x1a: /* FCVTNS */
12505                 case 0x1b: /* FCVTMS */
12506                 case 0x1c: /* FCVTAS */
12507                 case 0x3a: /* FCVTPS */
12508                 case 0x3b: /* FCVTZS */
12509                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12510                                          tcg_constant_i32(0), tcg_fpstatus);
12511                     break;
12512                 case 0x5a: /* FCVTNU */
12513                 case 0x5b: /* FCVTMU */
12514                 case 0x5c: /* FCVTAU */
12515                 case 0x7a: /* FCVTPU */
12516                 case 0x7b: /* FCVTZU */
12517                     gen_helper_vfp_touls(tcg_res, tcg_op,
12518                                          tcg_constant_i32(0), tcg_fpstatus);
12519                     break;
12520                 case 0x18: /* FRINTN */
12521                 case 0x19: /* FRINTM */
12522                 case 0x38: /* FRINTP */
12523                 case 0x39: /* FRINTZ */
12524                 case 0x58: /* FRINTA */
12525                 case 0x79: /* FRINTI */
12526                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12527                     break;
12528                 case 0x59: /* FRINTX */
12529                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12530                     break;
12531                 case 0x7c: /* URSQRTE */
12532                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12533                     break;
12534                 case 0x1e: /* FRINT32Z */
12535                 case 0x5e: /* FRINT32X */
12536                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12537                     break;
12538                 case 0x1f: /* FRINT64Z */
12539                 case 0x5f: /* FRINT64X */
12540                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12541                     break;
12542                 default:
12543                     g_assert_not_reached();
12544                 }
12545             } else {
12546                 /* Use helpers for 8 and 16 bit elements */
12547                 switch (opcode) {
12548                 case 0x5: /* CNT, RBIT */
12549                     /* For these two insns size is part of the opcode specifier
12550                      * (handled earlier); they always operate on byte elements.
12551                      */
12552                     if (u) {
12553                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12554                     } else {
12555                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12556                     }
12557                     break;
12558                 case 0x7: /* SQABS, SQNEG */
12559                 {
12560                     NeonGenOneOpEnvFn *genfn;
12561                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12562                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12563                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12564                     };
12565                     genfn = fns[size][u];
12566                     genfn(tcg_res, cpu_env, tcg_op);
12567                     break;
12568                 }
12569                 case 0x4: /* CLS, CLZ */
12570                     if (u) {
12571                         if (size == 0) {
12572                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12573                         } else {
12574                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12575                         }
12576                     } else {
12577                         if (size == 0) {
12578                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12579                         } else {
12580                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12581                         }
12582                     }
12583                     break;
12584                 default:
12585                     g_assert_not_reached();
12586                 }
12587             }
12588 
12589             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12590         }
12591     }
12592     clear_vec_high(s, is_q, rd);
12593 
12594     if (tcg_rmode) {
12595         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12596     }
12597 }
12598 
12599 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12600  *
12601  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12602  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12603  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12604  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12605  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12606  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12607  *
12608  * This actually covers two groups where scalar access is governed by
12609  * bit 28. A bunch of the instructions (float to integral) only exist
12610  * in the vector form and are un-allocated for the scalar decode. Also
12611  * in the scalar decode Q is always 1.
12612  */
12613 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12614 {
12615     int fpop, opcode, a, u;
12616     int rn, rd;
12617     bool is_q;
12618     bool is_scalar;
12619     bool only_in_vector = false;
12620 
12621     int pass;
12622     TCGv_i32 tcg_rmode = NULL;
12623     TCGv_ptr tcg_fpstatus = NULL;
12624     bool need_fpst = true;
12625     int rmode = -1;
12626 
12627     if (!dc_isar_feature(aa64_fp16, s)) {
12628         unallocated_encoding(s);
12629         return;
12630     }
12631 
12632     rd = extract32(insn, 0, 5);
12633     rn = extract32(insn, 5, 5);
12634 
12635     a = extract32(insn, 23, 1);
12636     u = extract32(insn, 29, 1);
12637     is_scalar = extract32(insn, 28, 1);
12638     is_q = extract32(insn, 30, 1);
12639 
12640     opcode = extract32(insn, 12, 5);
12641     fpop = deposit32(opcode, 5, 1, a);
12642     fpop = deposit32(fpop, 6, 1, u);
12643 
12644     switch (fpop) {
12645     case 0x1d: /* SCVTF */
12646     case 0x5d: /* UCVTF */
12647     {
12648         int elements;
12649 
12650         if (is_scalar) {
12651             elements = 1;
12652         } else {
12653             elements = (is_q ? 8 : 4);
12654         }
12655 
12656         if (!fp_access_check(s)) {
12657             return;
12658         }
12659         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12660         return;
12661     }
12662     break;
12663     case 0x2c: /* FCMGT (zero) */
12664     case 0x2d: /* FCMEQ (zero) */
12665     case 0x2e: /* FCMLT (zero) */
12666     case 0x6c: /* FCMGE (zero) */
12667     case 0x6d: /* FCMLE (zero) */
12668         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12669         return;
12670     case 0x3d: /* FRECPE */
12671     case 0x3f: /* FRECPX */
12672         break;
12673     case 0x18: /* FRINTN */
12674         only_in_vector = true;
12675         rmode = FPROUNDING_TIEEVEN;
12676         break;
12677     case 0x19: /* FRINTM */
12678         only_in_vector = true;
12679         rmode = FPROUNDING_NEGINF;
12680         break;
12681     case 0x38: /* FRINTP */
12682         only_in_vector = true;
12683         rmode = FPROUNDING_POSINF;
12684         break;
12685     case 0x39: /* FRINTZ */
12686         only_in_vector = true;
12687         rmode = FPROUNDING_ZERO;
12688         break;
12689     case 0x58: /* FRINTA */
12690         only_in_vector = true;
12691         rmode = FPROUNDING_TIEAWAY;
12692         break;
12693     case 0x59: /* FRINTX */
12694     case 0x79: /* FRINTI */
12695         only_in_vector = true;
12696         /* current rounding mode */
12697         break;
12698     case 0x1a: /* FCVTNS */
12699         rmode = FPROUNDING_TIEEVEN;
12700         break;
12701     case 0x1b: /* FCVTMS */
12702         rmode = FPROUNDING_NEGINF;
12703         break;
12704     case 0x1c: /* FCVTAS */
12705         rmode = FPROUNDING_TIEAWAY;
12706         break;
12707     case 0x3a: /* FCVTPS */
12708         rmode = FPROUNDING_POSINF;
12709         break;
12710     case 0x3b: /* FCVTZS */
12711         rmode = FPROUNDING_ZERO;
12712         break;
12713     case 0x5a: /* FCVTNU */
12714         rmode = FPROUNDING_TIEEVEN;
12715         break;
12716     case 0x5b: /* FCVTMU */
12717         rmode = FPROUNDING_NEGINF;
12718         break;
12719     case 0x5c: /* FCVTAU */
12720         rmode = FPROUNDING_TIEAWAY;
12721         break;
12722     case 0x7a: /* FCVTPU */
12723         rmode = FPROUNDING_POSINF;
12724         break;
12725     case 0x7b: /* FCVTZU */
12726         rmode = FPROUNDING_ZERO;
12727         break;
12728     case 0x2f: /* FABS */
12729     case 0x6f: /* FNEG */
12730         need_fpst = false;
12731         break;
12732     case 0x7d: /* FRSQRTE */
12733     case 0x7f: /* FSQRT (vector) */
12734         break;
12735     default:
12736         unallocated_encoding(s);
12737         return;
12738     }
12739 
12740 
12741     /* Check additional constraints for the scalar encoding */
12742     if (is_scalar) {
12743         if (!is_q) {
12744             unallocated_encoding(s);
12745             return;
12746         }
12747         /* FRINTxx is only in the vector form */
12748         if (only_in_vector) {
12749             unallocated_encoding(s);
12750             return;
12751         }
12752     }
12753 
12754     if (!fp_access_check(s)) {
12755         return;
12756     }
12757 
12758     if (rmode >= 0 || need_fpst) {
12759         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12760     }
12761 
12762     if (rmode >= 0) {
12763         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12764     }
12765 
12766     if (is_scalar) {
12767         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12768         TCGv_i32 tcg_res = tcg_temp_new_i32();
12769 
12770         switch (fpop) {
12771         case 0x1a: /* FCVTNS */
12772         case 0x1b: /* FCVTMS */
12773         case 0x1c: /* FCVTAS */
12774         case 0x3a: /* FCVTPS */
12775         case 0x3b: /* FCVTZS */
12776             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12777             break;
12778         case 0x3d: /* FRECPE */
12779             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12780             break;
12781         case 0x3f: /* FRECPX */
12782             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12783             break;
12784         case 0x5a: /* FCVTNU */
12785         case 0x5b: /* FCVTMU */
12786         case 0x5c: /* FCVTAU */
12787         case 0x7a: /* FCVTPU */
12788         case 0x7b: /* FCVTZU */
12789             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12790             break;
12791         case 0x6f: /* FNEG */
12792             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12793             break;
12794         case 0x7d: /* FRSQRTE */
12795             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12796             break;
12797         default:
12798             g_assert_not_reached();
12799         }
12800 
12801         /* limit any sign extension going on */
12802         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12803         write_fp_sreg(s, rd, tcg_res);
12804     } else {
12805         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12806             TCGv_i32 tcg_op = tcg_temp_new_i32();
12807             TCGv_i32 tcg_res = tcg_temp_new_i32();
12808 
12809             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12810 
12811             switch (fpop) {
12812             case 0x1a: /* FCVTNS */
12813             case 0x1b: /* FCVTMS */
12814             case 0x1c: /* FCVTAS */
12815             case 0x3a: /* FCVTPS */
12816             case 0x3b: /* FCVTZS */
12817                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12818                 break;
12819             case 0x3d: /* FRECPE */
12820                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12821                 break;
12822             case 0x5a: /* FCVTNU */
12823             case 0x5b: /* FCVTMU */
12824             case 0x5c: /* FCVTAU */
12825             case 0x7a: /* FCVTPU */
12826             case 0x7b: /* FCVTZU */
12827                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12828                 break;
12829             case 0x18: /* FRINTN */
12830             case 0x19: /* FRINTM */
12831             case 0x38: /* FRINTP */
12832             case 0x39: /* FRINTZ */
12833             case 0x58: /* FRINTA */
12834             case 0x79: /* FRINTI */
12835                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12836                 break;
12837             case 0x59: /* FRINTX */
12838                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12839                 break;
12840             case 0x2f: /* FABS */
12841                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12842                 break;
12843             case 0x6f: /* FNEG */
12844                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12845                 break;
12846             case 0x7d: /* FRSQRTE */
12847                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12848                 break;
12849             case 0x7f: /* FSQRT */
12850                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12851                 break;
12852             default:
12853                 g_assert_not_reached();
12854             }
12855 
12856             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12857         }
12858 
12859         clear_vec_high(s, is_q, rd);
12860     }
12861 
12862     if (tcg_rmode) {
12863         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12864     }
12865 }
12866 
12867 /* AdvSIMD scalar x indexed element
12868  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12869  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12870  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12871  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12872  * AdvSIMD vector x indexed element
12873  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12874  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12875  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12876  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12877  */
12878 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12879 {
12880     /* This encoding has two kinds of instruction:
12881      *  normal, where we perform elt x idxelt => elt for each
12882      *     element in the vector
12883      *  long, where we perform elt x idxelt and generate a result of
12884      *     double the width of the input element
12885      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12886      */
12887     bool is_scalar = extract32(insn, 28, 1);
12888     bool is_q = extract32(insn, 30, 1);
12889     bool u = extract32(insn, 29, 1);
12890     int size = extract32(insn, 22, 2);
12891     int l = extract32(insn, 21, 1);
12892     int m = extract32(insn, 20, 1);
12893     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12894     int rm = extract32(insn, 16, 4);
12895     int opcode = extract32(insn, 12, 4);
12896     int h = extract32(insn, 11, 1);
12897     int rn = extract32(insn, 5, 5);
12898     int rd = extract32(insn, 0, 5);
12899     bool is_long = false;
12900     int is_fp = 0;
12901     bool is_fp16 = false;
12902     int index;
12903     TCGv_ptr fpst;
12904 
12905     switch (16 * u + opcode) {
12906     case 0x08: /* MUL */
12907     case 0x10: /* MLA */
12908     case 0x14: /* MLS */
12909         if (is_scalar) {
12910             unallocated_encoding(s);
12911             return;
12912         }
12913         break;
12914     case 0x02: /* SMLAL, SMLAL2 */
12915     case 0x12: /* UMLAL, UMLAL2 */
12916     case 0x06: /* SMLSL, SMLSL2 */
12917     case 0x16: /* UMLSL, UMLSL2 */
12918     case 0x0a: /* SMULL, SMULL2 */
12919     case 0x1a: /* UMULL, UMULL2 */
12920         if (is_scalar) {
12921             unallocated_encoding(s);
12922             return;
12923         }
12924         is_long = true;
12925         break;
12926     case 0x03: /* SQDMLAL, SQDMLAL2 */
12927     case 0x07: /* SQDMLSL, SQDMLSL2 */
12928     case 0x0b: /* SQDMULL, SQDMULL2 */
12929         is_long = true;
12930         break;
12931     case 0x0c: /* SQDMULH */
12932     case 0x0d: /* SQRDMULH */
12933         break;
12934     case 0x01: /* FMLA */
12935     case 0x05: /* FMLS */
12936     case 0x09: /* FMUL */
12937     case 0x19: /* FMULX */
12938         is_fp = 1;
12939         break;
12940     case 0x1d: /* SQRDMLAH */
12941     case 0x1f: /* SQRDMLSH */
12942         if (!dc_isar_feature(aa64_rdm, s)) {
12943             unallocated_encoding(s);
12944             return;
12945         }
12946         break;
12947     case 0x0e: /* SDOT */
12948     case 0x1e: /* UDOT */
12949         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12950             unallocated_encoding(s);
12951             return;
12952         }
12953         break;
12954     case 0x0f:
12955         switch (size) {
12956         case 0: /* SUDOT */
12957         case 2: /* USDOT */
12958             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12959                 unallocated_encoding(s);
12960                 return;
12961             }
12962             size = MO_32;
12963             break;
12964         case 1: /* BFDOT */
12965             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12966                 unallocated_encoding(s);
12967                 return;
12968             }
12969             size = MO_32;
12970             break;
12971         case 3: /* BFMLAL{B,T} */
12972             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12973                 unallocated_encoding(s);
12974                 return;
12975             }
12976             /* can't set is_fp without other incorrect size checks */
12977             size = MO_16;
12978             break;
12979         default:
12980             unallocated_encoding(s);
12981             return;
12982         }
12983         break;
12984     case 0x11: /* FCMLA #0 */
12985     case 0x13: /* FCMLA #90 */
12986     case 0x15: /* FCMLA #180 */
12987     case 0x17: /* FCMLA #270 */
12988         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12989             unallocated_encoding(s);
12990             return;
12991         }
12992         is_fp = 2;
12993         break;
12994     case 0x00: /* FMLAL */
12995     case 0x04: /* FMLSL */
12996     case 0x18: /* FMLAL2 */
12997     case 0x1c: /* FMLSL2 */
12998         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12999             unallocated_encoding(s);
13000             return;
13001         }
13002         size = MO_16;
13003         /* is_fp, but we pass cpu_env not fp_status.  */
13004         break;
13005     default:
13006         unallocated_encoding(s);
13007         return;
13008     }
13009 
13010     switch (is_fp) {
13011     case 1: /* normal fp */
13012         /* convert insn encoded size to MemOp size */
13013         switch (size) {
13014         case 0: /* half-precision */
13015             size = MO_16;
13016             is_fp16 = true;
13017             break;
13018         case MO_32: /* single precision */
13019         case MO_64: /* double precision */
13020             break;
13021         default:
13022             unallocated_encoding(s);
13023             return;
13024         }
13025         break;
13026 
13027     case 2: /* complex fp */
13028         /* Each indexable element is a complex pair.  */
13029         size += 1;
13030         switch (size) {
13031         case MO_32:
13032             if (h && !is_q) {
13033                 unallocated_encoding(s);
13034                 return;
13035             }
13036             is_fp16 = true;
13037             break;
13038         case MO_64:
13039             break;
13040         default:
13041             unallocated_encoding(s);
13042             return;
13043         }
13044         break;
13045 
13046     default: /* integer */
13047         switch (size) {
13048         case MO_8:
13049         case MO_64:
13050             unallocated_encoding(s);
13051             return;
13052         }
13053         break;
13054     }
13055     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13056         unallocated_encoding(s);
13057         return;
13058     }
13059 
13060     /* Given MemOp size, adjust register and indexing.  */
13061     switch (size) {
13062     case MO_16:
13063         index = h << 2 | l << 1 | m;
13064         break;
13065     case MO_32:
13066         index = h << 1 | l;
13067         rm |= m << 4;
13068         break;
13069     case MO_64:
13070         if (l || !is_q) {
13071             unallocated_encoding(s);
13072             return;
13073         }
13074         index = h;
13075         rm |= m << 4;
13076         break;
13077     default:
13078         g_assert_not_reached();
13079     }
13080 
13081     if (!fp_access_check(s)) {
13082         return;
13083     }
13084 
13085     if (is_fp) {
13086         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13087     } else {
13088         fpst = NULL;
13089     }
13090 
13091     switch (16 * u + opcode) {
13092     case 0x0e: /* SDOT */
13093     case 0x1e: /* UDOT */
13094         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13095                          u ? gen_helper_gvec_udot_idx_b
13096                          : gen_helper_gvec_sdot_idx_b);
13097         return;
13098     case 0x0f:
13099         switch (extract32(insn, 22, 2)) {
13100         case 0: /* SUDOT */
13101             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13102                              gen_helper_gvec_sudot_idx_b);
13103             return;
13104         case 1: /* BFDOT */
13105             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13106                              gen_helper_gvec_bfdot_idx);
13107             return;
13108         case 2: /* USDOT */
13109             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13110                              gen_helper_gvec_usdot_idx_b);
13111             return;
13112         case 3: /* BFMLAL{B,T} */
13113             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13114                               gen_helper_gvec_bfmlal_idx);
13115             return;
13116         }
13117         g_assert_not_reached();
13118     case 0x11: /* FCMLA #0 */
13119     case 0x13: /* FCMLA #90 */
13120     case 0x15: /* FCMLA #180 */
13121     case 0x17: /* FCMLA #270 */
13122         {
13123             int rot = extract32(insn, 13, 2);
13124             int data = (index << 2) | rot;
13125             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13126                                vec_full_reg_offset(s, rn),
13127                                vec_full_reg_offset(s, rm),
13128                                vec_full_reg_offset(s, rd), fpst,
13129                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13130                                size == MO_64
13131                                ? gen_helper_gvec_fcmlas_idx
13132                                : gen_helper_gvec_fcmlah_idx);
13133         }
13134         return;
13135 
13136     case 0x00: /* FMLAL */
13137     case 0x04: /* FMLSL */
13138     case 0x18: /* FMLAL2 */
13139     case 0x1c: /* FMLSL2 */
13140         {
13141             int is_s = extract32(opcode, 2, 1);
13142             int is_2 = u;
13143             int data = (index << 2) | (is_2 << 1) | is_s;
13144             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13145                                vec_full_reg_offset(s, rn),
13146                                vec_full_reg_offset(s, rm), cpu_env,
13147                                is_q ? 16 : 8, vec_full_reg_size(s),
13148                                data, gen_helper_gvec_fmlal_idx_a64);
13149         }
13150         return;
13151 
13152     case 0x08: /* MUL */
13153         if (!is_long && !is_scalar) {
13154             static gen_helper_gvec_3 * const fns[3] = {
13155                 gen_helper_gvec_mul_idx_h,
13156                 gen_helper_gvec_mul_idx_s,
13157                 gen_helper_gvec_mul_idx_d,
13158             };
13159             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13160                                vec_full_reg_offset(s, rn),
13161                                vec_full_reg_offset(s, rm),
13162                                is_q ? 16 : 8, vec_full_reg_size(s),
13163                                index, fns[size - 1]);
13164             return;
13165         }
13166         break;
13167 
13168     case 0x10: /* MLA */
13169         if (!is_long && !is_scalar) {
13170             static gen_helper_gvec_4 * const fns[3] = {
13171                 gen_helper_gvec_mla_idx_h,
13172                 gen_helper_gvec_mla_idx_s,
13173                 gen_helper_gvec_mla_idx_d,
13174             };
13175             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13176                                vec_full_reg_offset(s, rn),
13177                                vec_full_reg_offset(s, rm),
13178                                vec_full_reg_offset(s, rd),
13179                                is_q ? 16 : 8, vec_full_reg_size(s),
13180                                index, fns[size - 1]);
13181             return;
13182         }
13183         break;
13184 
13185     case 0x14: /* MLS */
13186         if (!is_long && !is_scalar) {
13187             static gen_helper_gvec_4 * const fns[3] = {
13188                 gen_helper_gvec_mls_idx_h,
13189                 gen_helper_gvec_mls_idx_s,
13190                 gen_helper_gvec_mls_idx_d,
13191             };
13192             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13193                                vec_full_reg_offset(s, rn),
13194                                vec_full_reg_offset(s, rm),
13195                                vec_full_reg_offset(s, rd),
13196                                is_q ? 16 : 8, vec_full_reg_size(s),
13197                                index, fns[size - 1]);
13198             return;
13199         }
13200         break;
13201     }
13202 
13203     if (size == 3) {
13204         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13205         int pass;
13206 
13207         assert(is_fp && is_q && !is_long);
13208 
13209         read_vec_element(s, tcg_idx, rm, index, MO_64);
13210 
13211         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13212             TCGv_i64 tcg_op = tcg_temp_new_i64();
13213             TCGv_i64 tcg_res = tcg_temp_new_i64();
13214 
13215             read_vec_element(s, tcg_op, rn, pass, MO_64);
13216 
13217             switch (16 * u + opcode) {
13218             case 0x05: /* FMLS */
13219                 /* As usual for ARM, separate negation for fused multiply-add */
13220                 gen_helper_vfp_negd(tcg_op, tcg_op);
13221                 /* fall through */
13222             case 0x01: /* FMLA */
13223                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13224                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13225                 break;
13226             case 0x09: /* FMUL */
13227                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13228                 break;
13229             case 0x19: /* FMULX */
13230                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13231                 break;
13232             default:
13233                 g_assert_not_reached();
13234             }
13235 
13236             write_vec_element(s, tcg_res, rd, pass, MO_64);
13237         }
13238 
13239         clear_vec_high(s, !is_scalar, rd);
13240     } else if (!is_long) {
13241         /* 32 bit floating point, or 16 or 32 bit integer.
13242          * For the 16 bit scalar case we use the usual Neon helpers and
13243          * rely on the fact that 0 op 0 == 0 with no side effects.
13244          */
13245         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13246         int pass, maxpasses;
13247 
13248         if (is_scalar) {
13249             maxpasses = 1;
13250         } else {
13251             maxpasses = is_q ? 4 : 2;
13252         }
13253 
13254         read_vec_element_i32(s, tcg_idx, rm, index, size);
13255 
13256         if (size == 1 && !is_scalar) {
13257             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13258              * the index into both halves of the 32 bit tcg_idx and then use
13259              * the usual Neon helpers.
13260              */
13261             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13262         }
13263 
13264         for (pass = 0; pass < maxpasses; pass++) {
13265             TCGv_i32 tcg_op = tcg_temp_new_i32();
13266             TCGv_i32 tcg_res = tcg_temp_new_i32();
13267 
13268             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13269 
13270             switch (16 * u + opcode) {
13271             case 0x08: /* MUL */
13272             case 0x10: /* MLA */
13273             case 0x14: /* MLS */
13274             {
13275                 static NeonGenTwoOpFn * const fns[2][2] = {
13276                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13277                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13278                 };
13279                 NeonGenTwoOpFn *genfn;
13280                 bool is_sub = opcode == 0x4;
13281 
13282                 if (size == 1) {
13283                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13284                 } else {
13285                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13286                 }
13287                 if (opcode == 0x8) {
13288                     break;
13289                 }
13290                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13291                 genfn = fns[size - 1][is_sub];
13292                 genfn(tcg_res, tcg_op, tcg_res);
13293                 break;
13294             }
13295             case 0x05: /* FMLS */
13296             case 0x01: /* FMLA */
13297                 read_vec_element_i32(s, tcg_res, rd, pass,
13298                                      is_scalar ? size : MO_32);
13299                 switch (size) {
13300                 case 1:
13301                     if (opcode == 0x5) {
13302                         /* As usual for ARM, separate negation for fused
13303                          * multiply-add */
13304                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13305                     }
13306                     if (is_scalar) {
13307                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13308                                                    tcg_res, fpst);
13309                     } else {
13310                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13311                                                     tcg_res, fpst);
13312                     }
13313                     break;
13314                 case 2:
13315                     if (opcode == 0x5) {
13316                         /* As usual for ARM, separate negation for
13317                          * fused multiply-add */
13318                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13319                     }
13320                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13321                                            tcg_res, fpst);
13322                     break;
13323                 default:
13324                     g_assert_not_reached();
13325                 }
13326                 break;
13327             case 0x09: /* FMUL */
13328                 switch (size) {
13329                 case 1:
13330                     if (is_scalar) {
13331                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13332                                                 tcg_idx, fpst);
13333                     } else {
13334                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13335                                                  tcg_idx, fpst);
13336                     }
13337                     break;
13338                 case 2:
13339                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13340                     break;
13341                 default:
13342                     g_assert_not_reached();
13343                 }
13344                 break;
13345             case 0x19: /* FMULX */
13346                 switch (size) {
13347                 case 1:
13348                     if (is_scalar) {
13349                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13350                                                  tcg_idx, fpst);
13351                     } else {
13352                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13353                                                   tcg_idx, fpst);
13354                     }
13355                     break;
13356                 case 2:
13357                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13358                     break;
13359                 default:
13360                     g_assert_not_reached();
13361                 }
13362                 break;
13363             case 0x0c: /* SQDMULH */
13364                 if (size == 1) {
13365                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13366                                                tcg_op, tcg_idx);
13367                 } else {
13368                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13369                                                tcg_op, tcg_idx);
13370                 }
13371                 break;
13372             case 0x0d: /* SQRDMULH */
13373                 if (size == 1) {
13374                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13375                                                 tcg_op, tcg_idx);
13376                 } else {
13377                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13378                                                 tcg_op, tcg_idx);
13379                 }
13380                 break;
13381             case 0x1d: /* SQRDMLAH */
13382                 read_vec_element_i32(s, tcg_res, rd, pass,
13383                                      is_scalar ? size : MO_32);
13384                 if (size == 1) {
13385                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13386                                                 tcg_op, tcg_idx, tcg_res);
13387                 } else {
13388                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13389                                                 tcg_op, tcg_idx, tcg_res);
13390                 }
13391                 break;
13392             case 0x1f: /* SQRDMLSH */
13393                 read_vec_element_i32(s, tcg_res, rd, pass,
13394                                      is_scalar ? size : MO_32);
13395                 if (size == 1) {
13396                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13397                                                 tcg_op, tcg_idx, tcg_res);
13398                 } else {
13399                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13400                                                 tcg_op, tcg_idx, tcg_res);
13401                 }
13402                 break;
13403             default:
13404                 g_assert_not_reached();
13405             }
13406 
13407             if (is_scalar) {
13408                 write_fp_sreg(s, rd, tcg_res);
13409             } else {
13410                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13411             }
13412         }
13413 
13414         clear_vec_high(s, is_q, rd);
13415     } else {
13416         /* long ops: 16x16->32 or 32x32->64 */
13417         TCGv_i64 tcg_res[2];
13418         int pass;
13419         bool satop = extract32(opcode, 0, 1);
13420         MemOp memop = MO_32;
13421 
13422         if (satop || !u) {
13423             memop |= MO_SIGN;
13424         }
13425 
13426         if (size == 2) {
13427             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13428 
13429             read_vec_element(s, tcg_idx, rm, index, memop);
13430 
13431             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13432                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13433                 TCGv_i64 tcg_passres;
13434                 int passelt;
13435 
13436                 if (is_scalar) {
13437                     passelt = 0;
13438                 } else {
13439                     passelt = pass + (is_q * 2);
13440                 }
13441 
13442                 read_vec_element(s, tcg_op, rn, passelt, memop);
13443 
13444                 tcg_res[pass] = tcg_temp_new_i64();
13445 
13446                 if (opcode == 0xa || opcode == 0xb) {
13447                     /* Non-accumulating ops */
13448                     tcg_passres = tcg_res[pass];
13449                 } else {
13450                     tcg_passres = tcg_temp_new_i64();
13451                 }
13452 
13453                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13454 
13455                 if (satop) {
13456                     /* saturating, doubling */
13457                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13458                                                       tcg_passres, tcg_passres);
13459                 }
13460 
13461                 if (opcode == 0xa || opcode == 0xb) {
13462                     continue;
13463                 }
13464 
13465                 /* Accumulating op: handle accumulate step */
13466                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13467 
13468                 switch (opcode) {
13469                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13470                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13471                     break;
13472                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13473                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13474                     break;
13475                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13476                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13477                     /* fall through */
13478                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13479                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13480                                                       tcg_res[pass],
13481                                                       tcg_passres);
13482                     break;
13483                 default:
13484                     g_assert_not_reached();
13485                 }
13486             }
13487 
13488             clear_vec_high(s, !is_scalar, rd);
13489         } else {
13490             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13491 
13492             assert(size == 1);
13493             read_vec_element_i32(s, tcg_idx, rm, index, size);
13494 
13495             if (!is_scalar) {
13496                 /* The simplest way to handle the 16x16 indexed ops is to
13497                  * duplicate the index into both halves of the 32 bit tcg_idx
13498                  * and then use the usual Neon helpers.
13499                  */
13500                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13501             }
13502 
13503             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13504                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13505                 TCGv_i64 tcg_passres;
13506 
13507                 if (is_scalar) {
13508                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13509                 } else {
13510                     read_vec_element_i32(s, tcg_op, rn,
13511                                          pass + (is_q * 2), MO_32);
13512                 }
13513 
13514                 tcg_res[pass] = tcg_temp_new_i64();
13515 
13516                 if (opcode == 0xa || opcode == 0xb) {
13517                     /* Non-accumulating ops */
13518                     tcg_passres = tcg_res[pass];
13519                 } else {
13520                     tcg_passres = tcg_temp_new_i64();
13521                 }
13522 
13523                 if (memop & MO_SIGN) {
13524                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13525                 } else {
13526                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13527                 }
13528                 if (satop) {
13529                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13530                                                       tcg_passres, tcg_passres);
13531                 }
13532 
13533                 if (opcode == 0xa || opcode == 0xb) {
13534                     continue;
13535                 }
13536 
13537                 /* Accumulating op: handle accumulate step */
13538                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13539 
13540                 switch (opcode) {
13541                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13542                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13543                                              tcg_passres);
13544                     break;
13545                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13546                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13547                                              tcg_passres);
13548                     break;
13549                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13550                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13551                     /* fall through */
13552                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13553                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13554                                                       tcg_res[pass],
13555                                                       tcg_passres);
13556                     break;
13557                 default:
13558                     g_assert_not_reached();
13559                 }
13560             }
13561 
13562             if (is_scalar) {
13563                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13564             }
13565         }
13566 
13567         if (is_scalar) {
13568             tcg_res[1] = tcg_constant_i64(0);
13569         }
13570 
13571         for (pass = 0; pass < 2; pass++) {
13572             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13573         }
13574     }
13575 }
13576 
13577 /* Crypto AES
13578  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13579  * +-----------------+------+-----------+--------+-----+------+------+
13580  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13581  * +-----------------+------+-----------+--------+-----+------+------+
13582  */
13583 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13584 {
13585     int size = extract32(insn, 22, 2);
13586     int opcode = extract32(insn, 12, 5);
13587     int rn = extract32(insn, 5, 5);
13588     int rd = extract32(insn, 0, 5);
13589     int decrypt;
13590     gen_helper_gvec_2 *genfn2 = NULL;
13591     gen_helper_gvec_3 *genfn3 = NULL;
13592 
13593     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13594         unallocated_encoding(s);
13595         return;
13596     }
13597 
13598     switch (opcode) {
13599     case 0x4: /* AESE */
13600         decrypt = 0;
13601         genfn3 = gen_helper_crypto_aese;
13602         break;
13603     case 0x6: /* AESMC */
13604         decrypt = 0;
13605         genfn2 = gen_helper_crypto_aesmc;
13606         break;
13607     case 0x5: /* AESD */
13608         decrypt = 1;
13609         genfn3 = gen_helper_crypto_aese;
13610         break;
13611     case 0x7: /* AESIMC */
13612         decrypt = 1;
13613         genfn2 = gen_helper_crypto_aesmc;
13614         break;
13615     default:
13616         unallocated_encoding(s);
13617         return;
13618     }
13619 
13620     if (!fp_access_check(s)) {
13621         return;
13622     }
13623     if (genfn2) {
13624         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13625     } else {
13626         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13627     }
13628 }
13629 
13630 /* Crypto three-reg SHA
13631  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13632  * +-----------------+------+---+------+---+--------+-----+------+------+
13633  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13634  * +-----------------+------+---+------+---+--------+-----+------+------+
13635  */
13636 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13637 {
13638     int size = extract32(insn, 22, 2);
13639     int opcode = extract32(insn, 12, 3);
13640     int rm = extract32(insn, 16, 5);
13641     int rn = extract32(insn, 5, 5);
13642     int rd = extract32(insn, 0, 5);
13643     gen_helper_gvec_3 *genfn;
13644     bool feature;
13645 
13646     if (size != 0) {
13647         unallocated_encoding(s);
13648         return;
13649     }
13650 
13651     switch (opcode) {
13652     case 0: /* SHA1C */
13653         genfn = gen_helper_crypto_sha1c;
13654         feature = dc_isar_feature(aa64_sha1, s);
13655         break;
13656     case 1: /* SHA1P */
13657         genfn = gen_helper_crypto_sha1p;
13658         feature = dc_isar_feature(aa64_sha1, s);
13659         break;
13660     case 2: /* SHA1M */
13661         genfn = gen_helper_crypto_sha1m;
13662         feature = dc_isar_feature(aa64_sha1, s);
13663         break;
13664     case 3: /* SHA1SU0 */
13665         genfn = gen_helper_crypto_sha1su0;
13666         feature = dc_isar_feature(aa64_sha1, s);
13667         break;
13668     case 4: /* SHA256H */
13669         genfn = gen_helper_crypto_sha256h;
13670         feature = dc_isar_feature(aa64_sha256, s);
13671         break;
13672     case 5: /* SHA256H2 */
13673         genfn = gen_helper_crypto_sha256h2;
13674         feature = dc_isar_feature(aa64_sha256, s);
13675         break;
13676     case 6: /* SHA256SU1 */
13677         genfn = gen_helper_crypto_sha256su1;
13678         feature = dc_isar_feature(aa64_sha256, s);
13679         break;
13680     default:
13681         unallocated_encoding(s);
13682         return;
13683     }
13684 
13685     if (!feature) {
13686         unallocated_encoding(s);
13687         return;
13688     }
13689 
13690     if (!fp_access_check(s)) {
13691         return;
13692     }
13693     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13694 }
13695 
13696 /* Crypto two-reg SHA
13697  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13698  * +-----------------+------+-----------+--------+-----+------+------+
13699  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13700  * +-----------------+------+-----------+--------+-----+------+------+
13701  */
13702 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13703 {
13704     int size = extract32(insn, 22, 2);
13705     int opcode = extract32(insn, 12, 5);
13706     int rn = extract32(insn, 5, 5);
13707     int rd = extract32(insn, 0, 5);
13708     gen_helper_gvec_2 *genfn;
13709     bool feature;
13710 
13711     if (size != 0) {
13712         unallocated_encoding(s);
13713         return;
13714     }
13715 
13716     switch (opcode) {
13717     case 0: /* SHA1H */
13718         feature = dc_isar_feature(aa64_sha1, s);
13719         genfn = gen_helper_crypto_sha1h;
13720         break;
13721     case 1: /* SHA1SU1 */
13722         feature = dc_isar_feature(aa64_sha1, s);
13723         genfn = gen_helper_crypto_sha1su1;
13724         break;
13725     case 2: /* SHA256SU0 */
13726         feature = dc_isar_feature(aa64_sha256, s);
13727         genfn = gen_helper_crypto_sha256su0;
13728         break;
13729     default:
13730         unallocated_encoding(s);
13731         return;
13732     }
13733 
13734     if (!feature) {
13735         unallocated_encoding(s);
13736         return;
13737     }
13738 
13739     if (!fp_access_check(s)) {
13740         return;
13741     }
13742     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13743 }
13744 
13745 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13746 {
13747     tcg_gen_rotli_i64(d, m, 1);
13748     tcg_gen_xor_i64(d, d, n);
13749 }
13750 
13751 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13752 {
13753     tcg_gen_rotli_vec(vece, d, m, 1);
13754     tcg_gen_xor_vec(vece, d, d, n);
13755 }
13756 
13757 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13758                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13759 {
13760     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13761     static const GVecGen3 op = {
13762         .fni8 = gen_rax1_i64,
13763         .fniv = gen_rax1_vec,
13764         .opt_opc = vecop_list,
13765         .fno = gen_helper_crypto_rax1,
13766         .vece = MO_64,
13767     };
13768     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13769 }
13770 
13771 /* Crypto three-reg SHA512
13772  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13773  * +-----------------------+------+---+---+-----+--------+------+------+
13774  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13775  * +-----------------------+------+---+---+-----+--------+------+------+
13776  */
13777 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13778 {
13779     int opcode = extract32(insn, 10, 2);
13780     int o =  extract32(insn, 14, 1);
13781     int rm = extract32(insn, 16, 5);
13782     int rn = extract32(insn, 5, 5);
13783     int rd = extract32(insn, 0, 5);
13784     bool feature;
13785     gen_helper_gvec_3 *oolfn = NULL;
13786     GVecGen3Fn *gvecfn = NULL;
13787 
13788     if (o == 0) {
13789         switch (opcode) {
13790         case 0: /* SHA512H */
13791             feature = dc_isar_feature(aa64_sha512, s);
13792             oolfn = gen_helper_crypto_sha512h;
13793             break;
13794         case 1: /* SHA512H2 */
13795             feature = dc_isar_feature(aa64_sha512, s);
13796             oolfn = gen_helper_crypto_sha512h2;
13797             break;
13798         case 2: /* SHA512SU1 */
13799             feature = dc_isar_feature(aa64_sha512, s);
13800             oolfn = gen_helper_crypto_sha512su1;
13801             break;
13802         case 3: /* RAX1 */
13803             feature = dc_isar_feature(aa64_sha3, s);
13804             gvecfn = gen_gvec_rax1;
13805             break;
13806         default:
13807             g_assert_not_reached();
13808         }
13809     } else {
13810         switch (opcode) {
13811         case 0: /* SM3PARTW1 */
13812             feature = dc_isar_feature(aa64_sm3, s);
13813             oolfn = gen_helper_crypto_sm3partw1;
13814             break;
13815         case 1: /* SM3PARTW2 */
13816             feature = dc_isar_feature(aa64_sm3, s);
13817             oolfn = gen_helper_crypto_sm3partw2;
13818             break;
13819         case 2: /* SM4EKEY */
13820             feature = dc_isar_feature(aa64_sm4, s);
13821             oolfn = gen_helper_crypto_sm4ekey;
13822             break;
13823         default:
13824             unallocated_encoding(s);
13825             return;
13826         }
13827     }
13828 
13829     if (!feature) {
13830         unallocated_encoding(s);
13831         return;
13832     }
13833 
13834     if (!fp_access_check(s)) {
13835         return;
13836     }
13837 
13838     if (oolfn) {
13839         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13840     } else {
13841         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13842     }
13843 }
13844 
13845 /* Crypto two-reg SHA512
13846  *  31                                     12  11  10  9    5 4    0
13847  * +-----------------------------------------+--------+------+------+
13848  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13849  * +-----------------------------------------+--------+------+------+
13850  */
13851 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13852 {
13853     int opcode = extract32(insn, 10, 2);
13854     int rn = extract32(insn, 5, 5);
13855     int rd = extract32(insn, 0, 5);
13856     bool feature;
13857 
13858     switch (opcode) {
13859     case 0: /* SHA512SU0 */
13860         feature = dc_isar_feature(aa64_sha512, s);
13861         break;
13862     case 1: /* SM4E */
13863         feature = dc_isar_feature(aa64_sm4, s);
13864         break;
13865     default:
13866         unallocated_encoding(s);
13867         return;
13868     }
13869 
13870     if (!feature) {
13871         unallocated_encoding(s);
13872         return;
13873     }
13874 
13875     if (!fp_access_check(s)) {
13876         return;
13877     }
13878 
13879     switch (opcode) {
13880     case 0: /* SHA512SU0 */
13881         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13882         break;
13883     case 1: /* SM4E */
13884         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13885         break;
13886     default:
13887         g_assert_not_reached();
13888     }
13889 }
13890 
13891 /* Crypto four-register
13892  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13893  * +-------------------+-----+------+---+------+------+------+
13894  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13895  * +-------------------+-----+------+---+------+------+------+
13896  */
13897 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13898 {
13899     int op0 = extract32(insn, 21, 2);
13900     int rm = extract32(insn, 16, 5);
13901     int ra = extract32(insn, 10, 5);
13902     int rn = extract32(insn, 5, 5);
13903     int rd = extract32(insn, 0, 5);
13904     bool feature;
13905 
13906     switch (op0) {
13907     case 0: /* EOR3 */
13908     case 1: /* BCAX */
13909         feature = dc_isar_feature(aa64_sha3, s);
13910         break;
13911     case 2: /* SM3SS1 */
13912         feature = dc_isar_feature(aa64_sm3, s);
13913         break;
13914     default:
13915         unallocated_encoding(s);
13916         return;
13917     }
13918 
13919     if (!feature) {
13920         unallocated_encoding(s);
13921         return;
13922     }
13923 
13924     if (!fp_access_check(s)) {
13925         return;
13926     }
13927 
13928     if (op0 < 2) {
13929         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13930         int pass;
13931 
13932         tcg_op1 = tcg_temp_new_i64();
13933         tcg_op2 = tcg_temp_new_i64();
13934         tcg_op3 = tcg_temp_new_i64();
13935         tcg_res[0] = tcg_temp_new_i64();
13936         tcg_res[1] = tcg_temp_new_i64();
13937 
13938         for (pass = 0; pass < 2; pass++) {
13939             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13940             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13941             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13942 
13943             if (op0 == 0) {
13944                 /* EOR3 */
13945                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13946             } else {
13947                 /* BCAX */
13948                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13949             }
13950             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13951         }
13952         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13953         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13954     } else {
13955         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13956 
13957         tcg_op1 = tcg_temp_new_i32();
13958         tcg_op2 = tcg_temp_new_i32();
13959         tcg_op3 = tcg_temp_new_i32();
13960         tcg_res = tcg_temp_new_i32();
13961         tcg_zero = tcg_constant_i32(0);
13962 
13963         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13964         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13965         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13966 
13967         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13968         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13969         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13970         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13971 
13972         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13973         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13974         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13975         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13976     }
13977 }
13978 
13979 /* Crypto XAR
13980  *  31                   21 20  16 15    10 9    5 4    0
13981  * +-----------------------+------+--------+------+------+
13982  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13983  * +-----------------------+------+--------+------+------+
13984  */
13985 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13986 {
13987     int rm = extract32(insn, 16, 5);
13988     int imm6 = extract32(insn, 10, 6);
13989     int rn = extract32(insn, 5, 5);
13990     int rd = extract32(insn, 0, 5);
13991 
13992     if (!dc_isar_feature(aa64_sha3, s)) {
13993         unallocated_encoding(s);
13994         return;
13995     }
13996 
13997     if (!fp_access_check(s)) {
13998         return;
13999     }
14000 
14001     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14002                  vec_full_reg_offset(s, rn),
14003                  vec_full_reg_offset(s, rm), imm6, 16,
14004                  vec_full_reg_size(s));
14005 }
14006 
14007 /* Crypto three-reg imm2
14008  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14009  * +-----------------------+------+-----+------+--------+------+------+
14010  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14011  * +-----------------------+------+-----+------+--------+------+------+
14012  */
14013 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14014 {
14015     static gen_helper_gvec_3 * const fns[4] = {
14016         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14017         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14018     };
14019     int opcode = extract32(insn, 10, 2);
14020     int imm2 = extract32(insn, 12, 2);
14021     int rm = extract32(insn, 16, 5);
14022     int rn = extract32(insn, 5, 5);
14023     int rd = extract32(insn, 0, 5);
14024 
14025     if (!dc_isar_feature(aa64_sm3, s)) {
14026         unallocated_encoding(s);
14027         return;
14028     }
14029 
14030     if (!fp_access_check(s)) {
14031         return;
14032     }
14033 
14034     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14035 }
14036 
14037 /* C3.6 Data processing - SIMD, inc Crypto
14038  *
14039  * As the decode gets a little complex we are using a table based
14040  * approach for this part of the decode.
14041  */
14042 static const AArch64DecodeTable data_proc_simd[] = {
14043     /* pattern  ,  mask     ,  fn                        */
14044     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14045     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14046     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14047     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14048     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14049     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14050     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14051     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14052     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14053     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14054     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14055     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14056     { 0x2e000000, 0xbf208400, disas_simd_ext },
14057     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14058     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14059     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14060     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14061     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14062     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14063     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14064     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14065     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14066     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14067     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14068     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14069     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14070     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14071     { 0xce800000, 0xffe00000, disas_crypto_xar },
14072     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14073     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14074     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14075     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14076     { 0x00000000, 0x00000000, NULL }
14077 };
14078 
14079 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14080 {
14081     /* Note that this is called with all non-FP cases from
14082      * table C3-6 so it must UNDEF for entries not specifically
14083      * allocated to instructions in that table.
14084      */
14085     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14086     if (fn) {
14087         fn(s, insn);
14088     } else {
14089         unallocated_encoding(s);
14090     }
14091 }
14092 
14093 /* C3.6 Data processing - SIMD and floating point */
14094 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14095 {
14096     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14097         disas_data_proc_fp(s, insn);
14098     } else {
14099         /* SIMD, including crypto */
14100         disas_data_proc_simd(s, insn);
14101     }
14102 }
14103 
14104 static bool trans_OK(DisasContext *s, arg_OK *a)
14105 {
14106     return true;
14107 }
14108 
14109 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14110 {
14111     s->is_nonstreaming = true;
14112     return true;
14113 }
14114 
14115 /**
14116  * is_guarded_page:
14117  * @env: The cpu environment
14118  * @s: The DisasContext
14119  *
14120  * Return true if the page is guarded.
14121  */
14122 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14123 {
14124     uint64_t addr = s->base.pc_first;
14125 #ifdef CONFIG_USER_ONLY
14126     return page_get_flags(addr) & PAGE_BTI;
14127 #else
14128     CPUTLBEntryFull *full;
14129     void *host;
14130     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14131     int flags;
14132 
14133     /*
14134      * We test this immediately after reading an insn, which means
14135      * that the TLB entry must be present and valid, and thus this
14136      * access will never raise an exception.
14137      */
14138     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14139                               false, &host, &full, 0);
14140     assert(!(flags & TLB_INVALID_MASK));
14141 
14142     return full->guarded;
14143 #endif
14144 }
14145 
14146 /**
14147  * btype_destination_ok:
14148  * @insn: The instruction at the branch destination
14149  * @bt: SCTLR_ELx.BT
14150  * @btype: PSTATE.BTYPE, and is non-zero
14151  *
14152  * On a guarded page, there are a limited number of insns
14153  * that may be present at the branch target:
14154  *   - branch target identifiers,
14155  *   - paciasp, pacibsp,
14156  *   - BRK insn
14157  *   - HLT insn
14158  * Anything else causes a Branch Target Exception.
14159  *
14160  * Return true if the branch is compatible, false to raise BTITRAP.
14161  */
14162 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14163 {
14164     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14165         /* HINT space */
14166         switch (extract32(insn, 5, 7)) {
14167         case 0b011001: /* PACIASP */
14168         case 0b011011: /* PACIBSP */
14169             /*
14170              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14171              * with btype == 3.  Otherwise all btype are ok.
14172              */
14173             return !bt || btype != 3;
14174         case 0b100000: /* BTI */
14175             /* Not compatible with any btype.  */
14176             return false;
14177         case 0b100010: /* BTI c */
14178             /* Not compatible with btype == 3 */
14179             return btype != 3;
14180         case 0b100100: /* BTI j */
14181             /* Not compatible with btype == 2 */
14182             return btype != 2;
14183         case 0b100110: /* BTI jc */
14184             /* Compatible with any btype.  */
14185             return true;
14186         }
14187     } else {
14188         switch (insn & 0xffe0001fu) {
14189         case 0xd4200000u: /* BRK */
14190         case 0xd4400000u: /* HLT */
14191             /* Give priority to the breakpoint exception.  */
14192             return true;
14193         }
14194     }
14195     return false;
14196 }
14197 
14198 /* C3.1 A64 instruction index by encoding */
14199 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14200 {
14201     switch (extract32(insn, 25, 4)) {
14202     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14203         disas_b_exc_sys(s, insn);
14204         break;
14205     case 0x4:
14206     case 0x6:
14207     case 0xc:
14208     case 0xe:      /* Loads and stores */
14209         disas_ldst(s, insn);
14210         break;
14211     case 0x5:
14212     case 0xd:      /* Data processing - register */
14213         disas_data_proc_reg(s, insn);
14214         break;
14215     case 0x7:
14216     case 0xf:      /* Data processing - SIMD and floating point */
14217         disas_data_proc_simd_fp(s, insn);
14218         break;
14219     default:
14220         unallocated_encoding(s);
14221         break;
14222     }
14223 }
14224 
14225 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14226                                           CPUState *cpu)
14227 {
14228     DisasContext *dc = container_of(dcbase, DisasContext, base);
14229     CPUARMState *env = cpu->env_ptr;
14230     ARMCPU *arm_cpu = env_archcpu(env);
14231     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14232     int bound, core_mmu_idx;
14233 
14234     dc->isar = &arm_cpu->isar;
14235     dc->condjmp = 0;
14236     dc->pc_save = dc->base.pc_first;
14237     dc->aarch64 = true;
14238     dc->thumb = false;
14239     dc->sctlr_b = 0;
14240     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14241     dc->condexec_mask = 0;
14242     dc->condexec_cond = 0;
14243     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14244     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14245     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14246     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14247     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14248     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14249 #if !defined(CONFIG_USER_ONLY)
14250     dc->user = (dc->current_el == 0);
14251 #endif
14252     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14253     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14254     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14255     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14256     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14257     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14258     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14259     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14260     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14261     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14262     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14263     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14264     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14265     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14266     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14267     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14268     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14269     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14270     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14271     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14272     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14273     dc->vec_len = 0;
14274     dc->vec_stride = 0;
14275     dc->cp_regs = arm_cpu->cp_regs;
14276     dc->features = env->features;
14277     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14278 
14279 #ifdef CONFIG_USER_ONLY
14280     /* In sve_probe_page, we assume TBI is enabled. */
14281     tcg_debug_assert(dc->tbid & 1);
14282 #endif
14283 
14284     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14285 
14286     /* Single step state. The code-generation logic here is:
14287      *  SS_ACTIVE == 0:
14288      *   generate code with no special handling for single-stepping (except
14289      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14290      *   this happens anyway because those changes are all system register or
14291      *   PSTATE writes).
14292      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14293      *   emit code for one insn
14294      *   emit code to clear PSTATE.SS
14295      *   emit code to generate software step exception for completed step
14296      *   end TB (as usual for having generated an exception)
14297      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14298      *   emit code to generate a software step exception
14299      *   end the TB
14300      */
14301     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14302     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14303     dc->is_ldex = false;
14304 
14305     /* Bound the number of insns to execute to those left on the page.  */
14306     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14307 
14308     /* If architectural single step active, limit to 1.  */
14309     if (dc->ss_active) {
14310         bound = 1;
14311     }
14312     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14313 }
14314 
14315 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14316 {
14317 }
14318 
14319 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14320 {
14321     DisasContext *dc = container_of(dcbase, DisasContext, base);
14322     target_ulong pc_arg = dc->base.pc_next;
14323 
14324     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14325         pc_arg &= ~TARGET_PAGE_MASK;
14326     }
14327     tcg_gen_insn_start(pc_arg, 0, 0);
14328     dc->insn_start = tcg_last_op();
14329 }
14330 
14331 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14332 {
14333     DisasContext *s = container_of(dcbase, DisasContext, base);
14334     CPUARMState *env = cpu->env_ptr;
14335     uint64_t pc = s->base.pc_next;
14336     uint32_t insn;
14337 
14338     /* Singlestep exceptions have the highest priority. */
14339     if (s->ss_active && !s->pstate_ss) {
14340         /* Singlestep state is Active-pending.
14341          * If we're in this state at the start of a TB then either
14342          *  a) we just took an exception to an EL which is being debugged
14343          *     and this is the first insn in the exception handler
14344          *  b) debug exceptions were masked and we just unmasked them
14345          *     without changing EL (eg by clearing PSTATE.D)
14346          * In either case we're going to take a swstep exception in the
14347          * "did not step an insn" case, and so the syndrome ISV and EX
14348          * bits should be zero.
14349          */
14350         assert(s->base.num_insns == 1);
14351         gen_swstep_exception(s, 0, 0);
14352         s->base.is_jmp = DISAS_NORETURN;
14353         s->base.pc_next = pc + 4;
14354         return;
14355     }
14356 
14357     if (pc & 3) {
14358         /*
14359          * PC alignment fault.  This has priority over the instruction abort
14360          * that we would receive from a translation fault via arm_ldl_code.
14361          * This should only be possible after an indirect branch, at the
14362          * start of the TB.
14363          */
14364         assert(s->base.num_insns == 1);
14365         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14366         s->base.is_jmp = DISAS_NORETURN;
14367         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14368         return;
14369     }
14370 
14371     s->pc_curr = pc;
14372     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14373     s->insn = insn;
14374     s->base.pc_next = pc + 4;
14375 
14376     s->fp_access_checked = false;
14377     s->sve_access_checked = false;
14378 
14379     if (s->pstate_il) {
14380         /*
14381          * Illegal execution state. This has priority over BTI
14382          * exceptions, but comes after instruction abort exceptions.
14383          */
14384         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14385         return;
14386     }
14387 
14388     if (dc_isar_feature(aa64_bti, s)) {
14389         if (s->base.num_insns == 1) {
14390             /*
14391              * At the first insn of the TB, compute s->guarded_page.
14392              * We delayed computing this until successfully reading
14393              * the first insn of the TB, above.  This (mostly) ensures
14394              * that the softmmu tlb entry has been populated, and the
14395              * page table GP bit is available.
14396              *
14397              * Note that we need to compute this even if btype == 0,
14398              * because this value is used for BR instructions later
14399              * where ENV is not available.
14400              */
14401             s->guarded_page = is_guarded_page(env, s);
14402 
14403             /* First insn can have btype set to non-zero.  */
14404             tcg_debug_assert(s->btype >= 0);
14405 
14406             /*
14407              * Note that the Branch Target Exception has fairly high
14408              * priority -- below debugging exceptions but above most
14409              * everything else.  This allows us to handle this now
14410              * instead of waiting until the insn is otherwise decoded.
14411              */
14412             if (s->btype != 0
14413                 && s->guarded_page
14414                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14415                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14416                 return;
14417             }
14418         } else {
14419             /* Not the first insn: btype must be 0.  */
14420             tcg_debug_assert(s->btype == 0);
14421         }
14422     }
14423 
14424     s->is_nonstreaming = false;
14425     if (s->sme_trap_nonstreaming) {
14426         disas_sme_fa64(s, insn);
14427     }
14428 
14429     if (!disas_a64(s, insn) &&
14430         !disas_sme(s, insn) &&
14431         !disas_sve(s, insn)) {
14432         disas_a64_legacy(s, insn);
14433     }
14434 
14435     /*
14436      * After execution of most insns, btype is reset to 0.
14437      * Note that we set btype == -1 when the insn sets btype.
14438      */
14439     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14440         reset_btype(s);
14441     }
14442 }
14443 
14444 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14445 {
14446     DisasContext *dc = container_of(dcbase, DisasContext, base);
14447 
14448     if (unlikely(dc->ss_active)) {
14449         /* Note that this means single stepping WFI doesn't halt the CPU.
14450          * For conditional branch insns this is harmless unreachable code as
14451          * gen_goto_tb() has already handled emitting the debug exception
14452          * (and thus a tb-jump is not possible when singlestepping).
14453          */
14454         switch (dc->base.is_jmp) {
14455         default:
14456             gen_a64_update_pc(dc, 4);
14457             /* fall through */
14458         case DISAS_EXIT:
14459         case DISAS_JUMP:
14460             gen_step_complete_exception(dc);
14461             break;
14462         case DISAS_NORETURN:
14463             break;
14464         }
14465     } else {
14466         switch (dc->base.is_jmp) {
14467         case DISAS_NEXT:
14468         case DISAS_TOO_MANY:
14469             gen_goto_tb(dc, 1, 4);
14470             break;
14471         default:
14472         case DISAS_UPDATE_EXIT:
14473             gen_a64_update_pc(dc, 4);
14474             /* fall through */
14475         case DISAS_EXIT:
14476             tcg_gen_exit_tb(NULL, 0);
14477             break;
14478         case DISAS_UPDATE_NOCHAIN:
14479             gen_a64_update_pc(dc, 4);
14480             /* fall through */
14481         case DISAS_JUMP:
14482             tcg_gen_lookup_and_goto_ptr();
14483             break;
14484         case DISAS_NORETURN:
14485         case DISAS_SWI:
14486             break;
14487         case DISAS_WFE:
14488             gen_a64_update_pc(dc, 4);
14489             gen_helper_wfe(cpu_env);
14490             break;
14491         case DISAS_YIELD:
14492             gen_a64_update_pc(dc, 4);
14493             gen_helper_yield(cpu_env);
14494             break;
14495         case DISAS_WFI:
14496             /*
14497              * This is a special case because we don't want to just halt
14498              * the CPU if trying to debug across a WFI.
14499              */
14500             gen_a64_update_pc(dc, 4);
14501             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14502             /*
14503              * The helper doesn't necessarily throw an exception, but we
14504              * must go back to the main loop to check for interrupts anyway.
14505              */
14506             tcg_gen_exit_tb(NULL, 0);
14507             break;
14508         }
14509     }
14510 }
14511 
14512 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14513                                  CPUState *cpu, FILE *logfile)
14514 {
14515     DisasContext *dc = container_of(dcbase, DisasContext, base);
14516 
14517     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14518     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14519 }
14520 
14521 const TranslatorOps aarch64_translator_ops = {
14522     .init_disas_context = aarch64_tr_init_disas_context,
14523     .tb_start           = aarch64_tr_tb_start,
14524     .insn_start         = aarch64_tr_insn_start,
14525     .translate_insn     = aarch64_tr_translate_insn,
14526     .tb_stop            = aarch64_tr_tb_stop,
14527     .disas_log          = aarch64_tr_disas_log,
14528 };
14529