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