xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 7fefc706)
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 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1653 {
1654     return true;
1655 }
1656 
1657 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1658 {
1659     /*
1660      * When running in MTTCG we don't generate jumps to the yield and
1661      * WFE helpers as it won't affect the scheduling of other vCPUs.
1662      * If we wanted to more completely model WFE/SEV so we don't busy
1663      * spin unnecessarily we would need to do something more involved.
1664      */
1665     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1666         s->base.is_jmp = DISAS_YIELD;
1667     }
1668     return true;
1669 }
1670 
1671 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1672 {
1673     s->base.is_jmp = DISAS_WFI;
1674     return true;
1675 }
1676 
1677 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1678 {
1679     /*
1680      * When running in MTTCG we don't generate jumps to the yield and
1681      * WFE helpers as it won't affect the scheduling of other vCPUs.
1682      * If we wanted to more completely model WFE/SEV so we don't busy
1683      * spin unnecessarily we would need to do something more involved.
1684      */
1685     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1686         s->base.is_jmp = DISAS_WFE;
1687     }
1688     return true;
1689 }
1690 
1691 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1692 {
1693     if (s->pauth_active) {
1694         gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1695     }
1696     return true;
1697 }
1698 
1699 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1700 {
1701     if (s->pauth_active) {
1702         gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1703     }
1704     return true;
1705 }
1706 
1707 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1708 {
1709     if (s->pauth_active) {
1710         gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1711     }
1712     return true;
1713 }
1714 
1715 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1716 {
1717     if (s->pauth_active) {
1718         gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1719     }
1720     return true;
1721 }
1722 
1723 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1724 {
1725     if (s->pauth_active) {
1726         gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1727     }
1728     return true;
1729 }
1730 
1731 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1732 {
1733     /* Without RAS, we must implement this as NOP. */
1734     if (dc_isar_feature(aa64_ras, s)) {
1735         /*
1736          * QEMU does not have a source of physical SErrors,
1737          * so we are only concerned with virtual SErrors.
1738          * The pseudocode in the ARM for this case is
1739          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1740          *      AArch64.vESBOperation();
1741          * Most of the condition can be evaluated at translation time.
1742          * Test for EL2 present, and defer test for SEL2 to runtime.
1743          */
1744         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1745             gen_helper_vesb(cpu_env);
1746         }
1747     }
1748     return true;
1749 }
1750 
1751 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1752 {
1753     if (s->pauth_active) {
1754         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1755     }
1756     return true;
1757 }
1758 
1759 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1760 {
1761     if (s->pauth_active) {
1762         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1763     }
1764     return true;
1765 }
1766 
1767 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1768 {
1769     if (s->pauth_active) {
1770         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1771     }
1772     return true;
1773 }
1774 
1775 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1776 {
1777     if (s->pauth_active) {
1778         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1779     }
1780     return true;
1781 }
1782 
1783 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1784 {
1785     if (s->pauth_active) {
1786         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1787     }
1788     return true;
1789 }
1790 
1791 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1792 {
1793     if (s->pauth_active) {
1794         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1795     }
1796     return true;
1797 }
1798 
1799 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1800 {
1801     if (s->pauth_active) {
1802         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1803     }
1804     return true;
1805 }
1806 
1807 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1808 {
1809     if (s->pauth_active) {
1810         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1811     }
1812     return true;
1813 }
1814 
1815 static void gen_clrex(DisasContext *s, uint32_t insn)
1816 {
1817     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1818 }
1819 
1820 /* CLREX, DSB, DMB, ISB */
1821 static void handle_sync(DisasContext *s, uint32_t insn,
1822                         unsigned int op1, unsigned int op2, unsigned int crm)
1823 {
1824     TCGBar bar;
1825 
1826     if (op1 != 3) {
1827         unallocated_encoding(s);
1828         return;
1829     }
1830 
1831     switch (op2) {
1832     case 2: /* CLREX */
1833         gen_clrex(s, insn);
1834         return;
1835     case 4: /* DSB */
1836     case 5: /* DMB */
1837         switch (crm & 3) {
1838         case 1: /* MBReqTypes_Reads */
1839             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1840             break;
1841         case 2: /* MBReqTypes_Writes */
1842             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1843             break;
1844         default: /* MBReqTypes_All */
1845             bar = TCG_BAR_SC | TCG_MO_ALL;
1846             break;
1847         }
1848         tcg_gen_mb(bar);
1849         return;
1850     case 6: /* ISB */
1851         /* We need to break the TB after this insn to execute
1852          * a self-modified code correctly and also to take
1853          * any pending interrupts immediately.
1854          */
1855         reset_btype(s);
1856         gen_goto_tb(s, 0, 4);
1857         return;
1858 
1859     case 7: /* SB */
1860         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1861             goto do_unallocated;
1862         }
1863         /*
1864          * TODO: There is no speculation barrier opcode for TCG;
1865          * MB and end the TB instead.
1866          */
1867         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1868         gen_goto_tb(s, 0, 4);
1869         return;
1870 
1871     default:
1872     do_unallocated:
1873         unallocated_encoding(s);
1874         return;
1875     }
1876 }
1877 
1878 static void gen_xaflag(void)
1879 {
1880     TCGv_i32 z = tcg_temp_new_i32();
1881 
1882     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1883 
1884     /*
1885      * (!C & !Z) << 31
1886      * (!(C | Z)) << 31
1887      * ~((C | Z) << 31)
1888      * ~-(C | Z)
1889      * (C | Z) - 1
1890      */
1891     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1892     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1893 
1894     /* !(Z & C) */
1895     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1896     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1897 
1898     /* (!C & Z) << 31 -> -(Z & ~C) */
1899     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1900     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1901 
1902     /* C | Z */
1903     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1904 }
1905 
1906 static void gen_axflag(void)
1907 {
1908     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1909     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1910 
1911     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1912     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1913 
1914     tcg_gen_movi_i32(cpu_NF, 0);
1915     tcg_gen_movi_i32(cpu_VF, 0);
1916 }
1917 
1918 /* MSR (immediate) - move immediate to processor state field */
1919 static void handle_msr_i(DisasContext *s, uint32_t insn,
1920                          unsigned int op1, unsigned int op2, unsigned int crm)
1921 {
1922     int op = op1 << 3 | op2;
1923 
1924     /* End the TB by default, chaining is ok.  */
1925     s->base.is_jmp = DISAS_TOO_MANY;
1926 
1927     switch (op) {
1928     case 0x00: /* CFINV */
1929         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1930             goto do_unallocated;
1931         }
1932         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1933         s->base.is_jmp = DISAS_NEXT;
1934         break;
1935 
1936     case 0x01: /* XAFlag */
1937         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1938             goto do_unallocated;
1939         }
1940         gen_xaflag();
1941         s->base.is_jmp = DISAS_NEXT;
1942         break;
1943 
1944     case 0x02: /* AXFlag */
1945         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1946             goto do_unallocated;
1947         }
1948         gen_axflag();
1949         s->base.is_jmp = DISAS_NEXT;
1950         break;
1951 
1952     case 0x03: /* UAO */
1953         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1954             goto do_unallocated;
1955         }
1956         if (crm & 1) {
1957             set_pstate_bits(PSTATE_UAO);
1958         } else {
1959             clear_pstate_bits(PSTATE_UAO);
1960         }
1961         gen_rebuild_hflags(s);
1962         break;
1963 
1964     case 0x04: /* PAN */
1965         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1966             goto do_unallocated;
1967         }
1968         if (crm & 1) {
1969             set_pstate_bits(PSTATE_PAN);
1970         } else {
1971             clear_pstate_bits(PSTATE_PAN);
1972         }
1973         gen_rebuild_hflags(s);
1974         break;
1975 
1976     case 0x05: /* SPSel */
1977         if (s->current_el == 0) {
1978             goto do_unallocated;
1979         }
1980         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1981         break;
1982 
1983     case 0x19: /* SSBS */
1984         if (!dc_isar_feature(aa64_ssbs, s)) {
1985             goto do_unallocated;
1986         }
1987         if (crm & 1) {
1988             set_pstate_bits(PSTATE_SSBS);
1989         } else {
1990             clear_pstate_bits(PSTATE_SSBS);
1991         }
1992         /* Don't need to rebuild hflags since SSBS is a nop */
1993         break;
1994 
1995     case 0x1a: /* DIT */
1996         if (!dc_isar_feature(aa64_dit, s)) {
1997             goto do_unallocated;
1998         }
1999         if (crm & 1) {
2000             set_pstate_bits(PSTATE_DIT);
2001         } else {
2002             clear_pstate_bits(PSTATE_DIT);
2003         }
2004         /* There's no need to rebuild hflags because DIT is a nop */
2005         break;
2006 
2007     case 0x1e: /* DAIFSet */
2008         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
2009         break;
2010 
2011     case 0x1f: /* DAIFClear */
2012         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
2013         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
2014         s->base.is_jmp = DISAS_UPDATE_EXIT;
2015         break;
2016 
2017     case 0x1c: /* TCO */
2018         if (dc_isar_feature(aa64_mte, s)) {
2019             /* Full MTE is enabled -- set the TCO bit as directed. */
2020             if (crm & 1) {
2021                 set_pstate_bits(PSTATE_TCO);
2022             } else {
2023                 clear_pstate_bits(PSTATE_TCO);
2024             }
2025             gen_rebuild_hflags(s);
2026             /* Many factors, including TCO, go into MTE_ACTIVE. */
2027             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2028         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2029             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2030             s->base.is_jmp = DISAS_NEXT;
2031         } else {
2032             goto do_unallocated;
2033         }
2034         break;
2035 
2036     case 0x1b: /* SVCR* */
2037         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
2038             goto do_unallocated;
2039         }
2040         if (sme_access_check(s)) {
2041             int old = s->pstate_sm | (s->pstate_za << 1);
2042             int new = (crm & 1) * 3;
2043             int msk = (crm >> 1) & 3;
2044 
2045             if ((old ^ new) & msk) {
2046                 /* At least one bit changes. */
2047                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2048                                     tcg_constant_i32(msk));
2049             } else {
2050                 s->base.is_jmp = DISAS_NEXT;
2051             }
2052         }
2053         break;
2054 
2055     default:
2056     do_unallocated:
2057         unallocated_encoding(s);
2058         return;
2059     }
2060 }
2061 
2062 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2063 {
2064     TCGv_i32 tmp = tcg_temp_new_i32();
2065     TCGv_i32 nzcv = tcg_temp_new_i32();
2066 
2067     /* build bit 31, N */
2068     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2069     /* build bit 30, Z */
2070     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2071     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2072     /* build bit 29, C */
2073     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2074     /* build bit 28, V */
2075     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2076     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2077     /* generate result */
2078     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2079 }
2080 
2081 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2082 {
2083     TCGv_i32 nzcv = tcg_temp_new_i32();
2084 
2085     /* take NZCV from R[t] */
2086     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2087 
2088     /* bit 31, N */
2089     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2090     /* bit 30, Z */
2091     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2092     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2093     /* bit 29, C */
2094     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2095     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2096     /* bit 28, V */
2097     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2098     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2099 }
2100 
2101 static void gen_sysreg_undef(DisasContext *s, bool isread,
2102                              uint8_t op0, uint8_t op1, uint8_t op2,
2103                              uint8_t crn, uint8_t crm, uint8_t rt)
2104 {
2105     /*
2106      * Generate code to emit an UNDEF with correct syndrome
2107      * information for a failed system register access.
2108      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2109      * but if FEAT_IDST is implemented then read accesses to registers
2110      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2111      * syndrome.
2112      */
2113     uint32_t syndrome;
2114 
2115     if (isread && dc_isar_feature(aa64_ids, s) &&
2116         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2117         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2118     } else {
2119         syndrome = syn_uncategorized();
2120     }
2121     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2122 }
2123 
2124 /* MRS - move from system register
2125  * MSR (register) - move to system register
2126  * SYS
2127  * SYSL
2128  * These are all essentially the same insn in 'read' and 'write'
2129  * versions, with varying op0 fields.
2130  */
2131 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2132                        unsigned int op0, unsigned int op1, unsigned int op2,
2133                        unsigned int crn, unsigned int crm, unsigned int rt)
2134 {
2135     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2136                                       crn, crm, op0, op1, op2);
2137     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2138     bool need_exit_tb = false;
2139     TCGv_ptr tcg_ri = NULL;
2140     TCGv_i64 tcg_rt;
2141 
2142     if (!ri) {
2143         /* Unknown register; this might be a guest error or a QEMU
2144          * unimplemented feature.
2145          */
2146         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2147                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2148                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2149         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2150         return;
2151     }
2152 
2153     /* Check access permissions */
2154     if (!cp_access_ok(s->current_el, ri, isread)) {
2155         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2156         return;
2157     }
2158 
2159     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2160         /* Emit code to perform further access permissions checks at
2161          * runtime; this may result in an exception.
2162          */
2163         uint32_t syndrome;
2164 
2165         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2166         gen_a64_update_pc(s, 0);
2167         tcg_ri = tcg_temp_new_ptr();
2168         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2169                                        tcg_constant_i32(key),
2170                                        tcg_constant_i32(syndrome),
2171                                        tcg_constant_i32(isread));
2172     } else if (ri->type & ARM_CP_RAISES_EXC) {
2173         /*
2174          * The readfn or writefn might raise an exception;
2175          * synchronize the CPU state in case it does.
2176          */
2177         gen_a64_update_pc(s, 0);
2178     }
2179 
2180     /* Handle special cases first */
2181     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2182     case 0:
2183         break;
2184     case ARM_CP_NOP:
2185         return;
2186     case ARM_CP_NZCV:
2187         tcg_rt = cpu_reg(s, rt);
2188         if (isread) {
2189             gen_get_nzcv(tcg_rt);
2190         } else {
2191             gen_set_nzcv(tcg_rt);
2192         }
2193         return;
2194     case ARM_CP_CURRENTEL:
2195         /* Reads as current EL value from pstate, which is
2196          * guaranteed to be constant by the tb flags.
2197          */
2198         tcg_rt = cpu_reg(s, rt);
2199         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2200         return;
2201     case ARM_CP_DC_ZVA:
2202         /* Writes clear the aligned block of memory which rt points into. */
2203         if (s->mte_active[0]) {
2204             int desc = 0;
2205 
2206             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2207             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2208             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2209 
2210             tcg_rt = tcg_temp_new_i64();
2211             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2212                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2213         } else {
2214             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2215         }
2216         gen_helper_dc_zva(cpu_env, tcg_rt);
2217         return;
2218     case ARM_CP_DC_GVA:
2219         {
2220             TCGv_i64 clean_addr, tag;
2221 
2222             /*
2223              * DC_GVA, like DC_ZVA, requires that we supply the original
2224              * pointer for an invalid page.  Probe that address first.
2225              */
2226             tcg_rt = cpu_reg(s, rt);
2227             clean_addr = clean_data_tbi(s, tcg_rt);
2228             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2229 
2230             if (s->ata) {
2231                 /* Extract the tag from the register to match STZGM.  */
2232                 tag = tcg_temp_new_i64();
2233                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2234                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2235             }
2236         }
2237         return;
2238     case ARM_CP_DC_GZVA:
2239         {
2240             TCGv_i64 clean_addr, tag;
2241 
2242             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2243             tcg_rt = cpu_reg(s, rt);
2244             clean_addr = clean_data_tbi(s, tcg_rt);
2245             gen_helper_dc_zva(cpu_env, clean_addr);
2246 
2247             if (s->ata) {
2248                 /* Extract the tag from the register to match STZGM.  */
2249                 tag = tcg_temp_new_i64();
2250                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2251                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2252             }
2253         }
2254         return;
2255     default:
2256         g_assert_not_reached();
2257     }
2258     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2259         return;
2260     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2261         return;
2262     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2263         return;
2264     }
2265 
2266     if (ri->type & ARM_CP_IO) {
2267         /* I/O operations must end the TB here (whether read or write) */
2268         need_exit_tb = translator_io_start(&s->base);
2269     }
2270 
2271     tcg_rt = cpu_reg(s, rt);
2272 
2273     if (isread) {
2274         if (ri->type & ARM_CP_CONST) {
2275             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2276         } else if (ri->readfn) {
2277             if (!tcg_ri) {
2278                 tcg_ri = gen_lookup_cp_reg(key);
2279             }
2280             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2281         } else {
2282             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2283         }
2284     } else {
2285         if (ri->type & ARM_CP_CONST) {
2286             /* If not forbidden by access permissions, treat as WI */
2287             return;
2288         } else if (ri->writefn) {
2289             if (!tcg_ri) {
2290                 tcg_ri = gen_lookup_cp_reg(key);
2291             }
2292             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2293         } else {
2294             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2295         }
2296     }
2297 
2298     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2299         /*
2300          * A write to any coprocessor regiser that ends a TB
2301          * must rebuild the hflags for the next TB.
2302          */
2303         gen_rebuild_hflags(s);
2304         /*
2305          * We default to ending the TB on a coprocessor register write,
2306          * but allow this to be suppressed by the register definition
2307          * (usually only necessary to work around guest bugs).
2308          */
2309         need_exit_tb = true;
2310     }
2311     if (need_exit_tb) {
2312         s->base.is_jmp = DISAS_UPDATE_EXIT;
2313     }
2314 }
2315 
2316 /* System
2317  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2318  * +---------------------+---+-----+-----+-------+-------+-----+------+
2319  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2320  * +---------------------+---+-----+-----+-------+-------+-----+------+
2321  */
2322 static void disas_system(DisasContext *s, uint32_t insn)
2323 {
2324     unsigned int l, op0, op1, crn, crm, op2, rt;
2325     l = extract32(insn, 21, 1);
2326     op0 = extract32(insn, 19, 2);
2327     op1 = extract32(insn, 16, 3);
2328     crn = extract32(insn, 12, 4);
2329     crm = extract32(insn, 8, 4);
2330     op2 = extract32(insn, 5, 3);
2331     rt = extract32(insn, 0, 5);
2332 
2333     if (op0 == 0) {
2334         if (l || rt != 31) {
2335             unallocated_encoding(s);
2336             return;
2337         }
2338         switch (crn) {
2339         case 3: /* CLREX, DSB, DMB, ISB */
2340             handle_sync(s, insn, op1, op2, crm);
2341             break;
2342         case 4: /* MSR (immediate) */
2343             handle_msr_i(s, insn, op1, op2, crm);
2344             break;
2345         default:
2346             unallocated_encoding(s);
2347             break;
2348         }
2349         return;
2350     }
2351     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2352 }
2353 
2354 /* Exception generation
2355  *
2356  *  31             24 23 21 20                     5 4   2 1  0
2357  * +-----------------+-----+------------------------+-----+----+
2358  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2359  * +-----------------------+------------------------+----------+
2360  */
2361 static void disas_exc(DisasContext *s, uint32_t insn)
2362 {
2363     int opc = extract32(insn, 21, 3);
2364     int op2_ll = extract32(insn, 0, 5);
2365     int imm16 = extract32(insn, 5, 16);
2366     uint32_t syndrome;
2367 
2368     switch (opc) {
2369     case 0:
2370         /* For SVC, HVC and SMC we advance the single-step state
2371          * machine before taking the exception. This is architecturally
2372          * mandated, to ensure that single-stepping a system call
2373          * instruction works properly.
2374          */
2375         switch (op2_ll) {
2376         case 1:                                                     /* SVC */
2377             syndrome = syn_aa64_svc(imm16);
2378             if (s->fgt_svc) {
2379                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2380                 break;
2381             }
2382             gen_ss_advance(s);
2383             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2384             break;
2385         case 2:                                                     /* HVC */
2386             if (s->current_el == 0) {
2387                 unallocated_encoding(s);
2388                 break;
2389             }
2390             /* The pre HVC helper handles cases when HVC gets trapped
2391              * as an undefined insn by runtime configuration.
2392              */
2393             gen_a64_update_pc(s, 0);
2394             gen_helper_pre_hvc(cpu_env);
2395             gen_ss_advance(s);
2396             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2397             break;
2398         case 3:                                                     /* SMC */
2399             if (s->current_el == 0) {
2400                 unallocated_encoding(s);
2401                 break;
2402             }
2403             gen_a64_update_pc(s, 0);
2404             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2405             gen_ss_advance(s);
2406             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2407             break;
2408         default:
2409             unallocated_encoding(s);
2410             break;
2411         }
2412         break;
2413     case 1:
2414         if (op2_ll != 0) {
2415             unallocated_encoding(s);
2416             break;
2417         }
2418         /* BRK */
2419         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2420         break;
2421     case 2:
2422         if (op2_ll != 0) {
2423             unallocated_encoding(s);
2424             break;
2425         }
2426         /* HLT. This has two purposes.
2427          * Architecturally, it is an external halting debug instruction.
2428          * Since QEMU doesn't implement external debug, we treat this as
2429          * it is required for halting debug disabled: it will UNDEF.
2430          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2431          */
2432         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2433             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2434         } else {
2435             unallocated_encoding(s);
2436         }
2437         break;
2438     case 5:
2439         if (op2_ll < 1 || op2_ll > 3) {
2440             unallocated_encoding(s);
2441             break;
2442         }
2443         /* DCPS1, DCPS2, DCPS3 */
2444         unallocated_encoding(s);
2445         break;
2446     default:
2447         unallocated_encoding(s);
2448         break;
2449     }
2450 }
2451 
2452 /* Branches, exception generating and system instructions */
2453 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2454 {
2455     switch (extract32(insn, 25, 7)) {
2456     case 0x6a: /* Exception generation / System */
2457         if (insn & (1 << 24)) {
2458             if (extract32(insn, 22, 2) == 0) {
2459                 disas_system(s, insn);
2460             } else {
2461                 unallocated_encoding(s);
2462             }
2463         } else {
2464             disas_exc(s, insn);
2465         }
2466         break;
2467     default:
2468         unallocated_encoding(s);
2469         break;
2470     }
2471 }
2472 
2473 /*
2474  * Load/Store exclusive instructions are implemented by remembering
2475  * the value/address loaded, and seeing if these are the same
2476  * when the store is performed. This is not actually the architecturally
2477  * mandated semantics, but it works for typical guest code sequences
2478  * and avoids having to monitor regular stores.
2479  *
2480  * The store exclusive uses the atomic cmpxchg primitives to avoid
2481  * races in multi-threaded linux-user and when MTTCG softmmu is
2482  * enabled.
2483  */
2484 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2485                                int size, bool is_pair)
2486 {
2487     int idx = get_mem_index(s);
2488     TCGv_i64 dirty_addr, clean_addr;
2489     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2490 
2491     s->is_ldex = true;
2492     dirty_addr = cpu_reg_sp(s, rn);
2493     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2494 
2495     g_assert(size <= 3);
2496     if (is_pair) {
2497         g_assert(size >= 2);
2498         if (size == 2) {
2499             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2500             if (s->be_data == MO_LE) {
2501                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2502                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2503             } else {
2504                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2505                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2506             }
2507         } else {
2508             TCGv_i128 t16 = tcg_temp_new_i128();
2509 
2510             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2511 
2512             if (s->be_data == MO_LE) {
2513                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2514                                       cpu_exclusive_high, t16);
2515             } else {
2516                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2517                                       cpu_exclusive_val, t16);
2518             }
2519             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2520             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2521         }
2522     } else {
2523         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2524         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2525     }
2526     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2527 }
2528 
2529 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2530                                 int rn, int size, int is_pair)
2531 {
2532     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2533      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2534      *     [addr] = {Rt};
2535      *     if (is_pair) {
2536      *         [addr + datasize] = {Rt2};
2537      *     }
2538      *     {Rd} = 0;
2539      * } else {
2540      *     {Rd} = 1;
2541      * }
2542      * env->exclusive_addr = -1;
2543      */
2544     TCGLabel *fail_label = gen_new_label();
2545     TCGLabel *done_label = gen_new_label();
2546     TCGv_i64 tmp, clean_addr;
2547     MemOp memop;
2548 
2549     /*
2550      * FIXME: We are out of spec here.  We have recorded only the address
2551      * from load_exclusive, not the entire range, and we assume that the
2552      * size of the access on both sides match.  The architecture allows the
2553      * store to be smaller than the load, so long as the stored bytes are
2554      * within the range recorded by the load.
2555      */
2556 
2557     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2558     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2559     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2560 
2561     /*
2562      * The write, and any associated faults, only happen if the virtual
2563      * and physical addresses pass the exclusive monitor check.  These
2564      * faults are exceedingly unlikely, because normally the guest uses
2565      * the exact same address register for the load_exclusive, and we
2566      * would have recognized these faults there.
2567      *
2568      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2569      * unaligned 4-byte write within the range of an aligned 8-byte load.
2570      * With LSE2, the store would need to cross a 16-byte boundary when the
2571      * load did not, which would mean the store is outside the range
2572      * recorded for the monitor, which would have failed a corrected monitor
2573      * check above.  For now, we assume no size change and retain the
2574      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2575      *
2576      * It is possible to trigger an MTE fault, by performing the load with
2577      * a virtual address with a valid tag and performing the store with the
2578      * same virtual address and a different invalid tag.
2579      */
2580     memop = size + is_pair;
2581     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2582         memop |= MO_ALIGN;
2583     }
2584     memop = finalize_memop(s, memop);
2585     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2586 
2587     tmp = tcg_temp_new_i64();
2588     if (is_pair) {
2589         if (size == 2) {
2590             if (s->be_data == MO_LE) {
2591                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2592             } else {
2593                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2594             }
2595             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2596                                        cpu_exclusive_val, tmp,
2597                                        get_mem_index(s), memop);
2598             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2599         } else {
2600             TCGv_i128 t16 = tcg_temp_new_i128();
2601             TCGv_i128 c16 = tcg_temp_new_i128();
2602             TCGv_i64 a, b;
2603 
2604             if (s->be_data == MO_LE) {
2605                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2606                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2607                                         cpu_exclusive_high);
2608             } else {
2609                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2610                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2611                                         cpu_exclusive_val);
2612             }
2613 
2614             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2615                                         get_mem_index(s), memop);
2616 
2617             a = tcg_temp_new_i64();
2618             b = tcg_temp_new_i64();
2619             if (s->be_data == MO_LE) {
2620                 tcg_gen_extr_i128_i64(a, b, t16);
2621             } else {
2622                 tcg_gen_extr_i128_i64(b, a, t16);
2623             }
2624 
2625             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2626             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2627             tcg_gen_or_i64(tmp, a, b);
2628 
2629             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2630         }
2631     } else {
2632         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2633                                    cpu_reg(s, rt), get_mem_index(s), memop);
2634         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2635     }
2636     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2637     tcg_gen_br(done_label);
2638 
2639     gen_set_label(fail_label);
2640     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2641     gen_set_label(done_label);
2642     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2643 }
2644 
2645 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2646                                  int rn, int size)
2647 {
2648     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2649     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2650     int memidx = get_mem_index(s);
2651     TCGv_i64 clean_addr;
2652     MemOp memop;
2653 
2654     if (rn == 31) {
2655         gen_check_sp_alignment(s);
2656     }
2657     memop = check_atomic_align(s, rn, size);
2658     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2659     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2660                                memidx, memop);
2661 }
2662 
2663 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2664                                       int rn, int size)
2665 {
2666     TCGv_i64 s1 = cpu_reg(s, rs);
2667     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2668     TCGv_i64 t1 = cpu_reg(s, rt);
2669     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2670     TCGv_i64 clean_addr;
2671     int memidx = get_mem_index(s);
2672     MemOp memop;
2673 
2674     if (rn == 31) {
2675         gen_check_sp_alignment(s);
2676     }
2677 
2678     /* This is a single atomic access, despite the "pair". */
2679     memop = check_atomic_align(s, rn, size + 1);
2680     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2681 
2682     if (size == 2) {
2683         TCGv_i64 cmp = tcg_temp_new_i64();
2684         TCGv_i64 val = tcg_temp_new_i64();
2685 
2686         if (s->be_data == MO_LE) {
2687             tcg_gen_concat32_i64(val, t1, t2);
2688             tcg_gen_concat32_i64(cmp, s1, s2);
2689         } else {
2690             tcg_gen_concat32_i64(val, t2, t1);
2691             tcg_gen_concat32_i64(cmp, s2, s1);
2692         }
2693 
2694         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2695 
2696         if (s->be_data == MO_LE) {
2697             tcg_gen_extr32_i64(s1, s2, cmp);
2698         } else {
2699             tcg_gen_extr32_i64(s2, s1, cmp);
2700         }
2701     } else {
2702         TCGv_i128 cmp = tcg_temp_new_i128();
2703         TCGv_i128 val = tcg_temp_new_i128();
2704 
2705         if (s->be_data == MO_LE) {
2706             tcg_gen_concat_i64_i128(val, t1, t2);
2707             tcg_gen_concat_i64_i128(cmp, s1, s2);
2708         } else {
2709             tcg_gen_concat_i64_i128(val, t2, t1);
2710             tcg_gen_concat_i64_i128(cmp, s2, s1);
2711         }
2712 
2713         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2714 
2715         if (s->be_data == MO_LE) {
2716             tcg_gen_extr_i128_i64(s1, s2, cmp);
2717         } else {
2718             tcg_gen_extr_i128_i64(s2, s1, cmp);
2719         }
2720     }
2721 }
2722 
2723 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2724  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2725  */
2726 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2727 {
2728     int opc0 = extract32(opc, 0, 1);
2729     int regsize;
2730 
2731     if (is_signed) {
2732         regsize = opc0 ? 32 : 64;
2733     } else {
2734         regsize = size == 3 ? 64 : 32;
2735     }
2736     return regsize == 64;
2737 }
2738 
2739 /* Load/store exclusive
2740  *
2741  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2742  * +-----+-------------+----+---+----+------+----+-------+------+------+
2743  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2744  * +-----+-------------+----+---+----+------+----+-------+------+------+
2745  *
2746  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2747  *   L: 0 -> store, 1 -> load
2748  *  o2: 0 -> exclusive, 1 -> not
2749  *  o1: 0 -> single register, 1 -> register pair
2750  *  o0: 1 -> load-acquire/store-release, 0 -> not
2751  */
2752 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2753 {
2754     int rt = extract32(insn, 0, 5);
2755     int rn = extract32(insn, 5, 5);
2756     int rt2 = extract32(insn, 10, 5);
2757     int rs = extract32(insn, 16, 5);
2758     int is_lasr = extract32(insn, 15, 1);
2759     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2760     int size = extract32(insn, 30, 2);
2761     TCGv_i64 clean_addr;
2762     MemOp memop;
2763 
2764     switch (o2_L_o1_o0) {
2765     case 0x0: /* STXR */
2766     case 0x1: /* STLXR */
2767         if (rn == 31) {
2768             gen_check_sp_alignment(s);
2769         }
2770         if (is_lasr) {
2771             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2772         }
2773         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2774         return;
2775 
2776     case 0x4: /* LDXR */
2777     case 0x5: /* LDAXR */
2778         if (rn == 31) {
2779             gen_check_sp_alignment(s);
2780         }
2781         gen_load_exclusive(s, rt, rt2, rn, size, false);
2782         if (is_lasr) {
2783             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2784         }
2785         return;
2786 
2787     case 0x8: /* STLLR */
2788         if (!dc_isar_feature(aa64_lor, s)) {
2789             break;
2790         }
2791         /* StoreLORelease is the same as Store-Release for QEMU.  */
2792         /* fall through */
2793     case 0x9: /* STLR */
2794         /* Generate ISS for non-exclusive accesses including LASR.  */
2795         if (rn == 31) {
2796             gen_check_sp_alignment(s);
2797         }
2798         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2799         memop = check_ordered_align(s, rn, 0, true, size);
2800         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2801                                     true, rn != 31, memop);
2802         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2803                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2804         return;
2805 
2806     case 0xc: /* LDLAR */
2807         if (!dc_isar_feature(aa64_lor, s)) {
2808             break;
2809         }
2810         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2811         /* fall through */
2812     case 0xd: /* LDAR */
2813         /* Generate ISS for non-exclusive accesses including LASR.  */
2814         if (rn == 31) {
2815             gen_check_sp_alignment(s);
2816         }
2817         memop = check_ordered_align(s, rn, 0, false, size);
2818         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2819                                     false, rn != 31, memop);
2820         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2821                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2822         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2823         return;
2824 
2825     case 0x2: case 0x3: /* CASP / STXP */
2826         if (size & 2) { /* STXP / STLXP */
2827             if (rn == 31) {
2828                 gen_check_sp_alignment(s);
2829             }
2830             if (is_lasr) {
2831                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2832             }
2833             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2834             return;
2835         }
2836         if (rt2 == 31
2837             && ((rt | rs) & 1) == 0
2838             && dc_isar_feature(aa64_atomics, s)) {
2839             /* CASP / CASPL */
2840             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2841             return;
2842         }
2843         break;
2844 
2845     case 0x6: case 0x7: /* CASPA / LDXP */
2846         if (size & 2) { /* LDXP / LDAXP */
2847             if (rn == 31) {
2848                 gen_check_sp_alignment(s);
2849             }
2850             gen_load_exclusive(s, rt, rt2, rn, size, true);
2851             if (is_lasr) {
2852                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2853             }
2854             return;
2855         }
2856         if (rt2 == 31
2857             && ((rt | rs) & 1) == 0
2858             && dc_isar_feature(aa64_atomics, s)) {
2859             /* CASPA / CASPAL */
2860             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2861             return;
2862         }
2863         break;
2864 
2865     case 0xa: /* CAS */
2866     case 0xb: /* CASL */
2867     case 0xe: /* CASA */
2868     case 0xf: /* CASAL */
2869         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2870             gen_compare_and_swap(s, rs, rt, rn, size);
2871             return;
2872         }
2873         break;
2874     }
2875     unallocated_encoding(s);
2876 }
2877 
2878 /*
2879  * Load register (literal)
2880  *
2881  *  31 30 29   27  26 25 24 23                5 4     0
2882  * +-----+-------+---+-----+-------------------+-------+
2883  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2884  * +-----+-------+---+-----+-------------------+-------+
2885  *
2886  * V: 1 -> vector (simd/fp)
2887  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2888  *                   10-> 32 bit signed, 11 -> prefetch
2889  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2890  */
2891 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2892 {
2893     int rt = extract32(insn, 0, 5);
2894     int64_t imm = sextract32(insn, 5, 19) << 2;
2895     bool is_vector = extract32(insn, 26, 1);
2896     int opc = extract32(insn, 30, 2);
2897     bool is_signed = false;
2898     int size = 2;
2899     TCGv_i64 tcg_rt, clean_addr;
2900     MemOp memop;
2901 
2902     if (is_vector) {
2903         if (opc == 3) {
2904             unallocated_encoding(s);
2905             return;
2906         }
2907         size = 2 + opc;
2908         if (!fp_access_check(s)) {
2909             return;
2910         }
2911         memop = finalize_memop_asimd(s, size);
2912     } else {
2913         if (opc == 3) {
2914             /* PRFM (literal) : prefetch */
2915             return;
2916         }
2917         size = 2 + extract32(opc, 0, 1);
2918         is_signed = extract32(opc, 1, 1);
2919         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2920     }
2921 
2922     tcg_rt = cpu_reg(s, rt);
2923 
2924     clean_addr = tcg_temp_new_i64();
2925     gen_pc_plus_diff(s, clean_addr, imm);
2926 
2927     if (is_vector) {
2928         do_fp_ld(s, rt, clean_addr, memop);
2929     } else {
2930         /* Only unsigned 32bit loads target 32bit registers.  */
2931         bool iss_sf = opc != 0;
2932         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2933     }
2934 }
2935 
2936 /*
2937  * LDNP (Load Pair - non-temporal hint)
2938  * LDP (Load Pair - non vector)
2939  * LDPSW (Load Pair Signed Word - non vector)
2940  * STNP (Store Pair - non-temporal hint)
2941  * STP (Store Pair - non vector)
2942  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2943  * LDP (Load Pair of SIMD&FP)
2944  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2945  * STP (Store Pair of SIMD&FP)
2946  *
2947  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2948  * +-----+-------+---+---+-------+---+-----------------------------+
2949  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2950  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2951  *
2952  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2953  *      LDPSW/STGP               01
2954  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2955  *   V: 0 -> GPR, 1 -> Vector
2956  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2957  *      10 -> signed offset, 11 -> pre-index
2958  *   L: 0 -> Store 1 -> Load
2959  *
2960  * Rt, Rt2 = GPR or SIMD registers to be stored
2961  * Rn = general purpose register containing address
2962  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2963  */
2964 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2965 {
2966     int rt = extract32(insn, 0, 5);
2967     int rn = extract32(insn, 5, 5);
2968     int rt2 = extract32(insn, 10, 5);
2969     uint64_t offset = sextract64(insn, 15, 7);
2970     int index = extract32(insn, 23, 2);
2971     bool is_vector = extract32(insn, 26, 1);
2972     bool is_load = extract32(insn, 22, 1);
2973     int opc = extract32(insn, 30, 2);
2974     bool is_signed = false;
2975     bool postindex = false;
2976     bool wback = false;
2977     bool set_tag = false;
2978     TCGv_i64 clean_addr, dirty_addr;
2979     MemOp mop;
2980     int size;
2981 
2982     if (opc == 3) {
2983         unallocated_encoding(s);
2984         return;
2985     }
2986 
2987     if (is_vector) {
2988         size = 2 + opc;
2989     } else if (opc == 1 && !is_load) {
2990         /* STGP */
2991         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2992             unallocated_encoding(s);
2993             return;
2994         }
2995         size = 3;
2996         set_tag = true;
2997     } else {
2998         size = 2 + extract32(opc, 1, 1);
2999         is_signed = extract32(opc, 0, 1);
3000         if (!is_load && is_signed) {
3001             unallocated_encoding(s);
3002             return;
3003         }
3004     }
3005 
3006     switch (index) {
3007     case 1: /* post-index */
3008         postindex = true;
3009         wback = true;
3010         break;
3011     case 0:
3012         /* signed offset with "non-temporal" hint. Since we don't emulate
3013          * caches we don't care about hints to the cache system about
3014          * data access patterns, and handle this identically to plain
3015          * signed offset.
3016          */
3017         if (is_signed) {
3018             /* There is no non-temporal-hint version of LDPSW */
3019             unallocated_encoding(s);
3020             return;
3021         }
3022         postindex = false;
3023         break;
3024     case 2: /* signed offset, rn not updated */
3025         postindex = false;
3026         break;
3027     case 3: /* pre-index */
3028         postindex = false;
3029         wback = true;
3030         break;
3031     }
3032 
3033     if (is_vector && !fp_access_check(s)) {
3034         return;
3035     }
3036 
3037     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
3038 
3039     if (rn == 31) {
3040         gen_check_sp_alignment(s);
3041     }
3042 
3043     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3044     if (!postindex) {
3045         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3046     }
3047 
3048     if (set_tag) {
3049         if (!s->ata) {
3050             /*
3051              * TODO: We could rely on the stores below, at least for
3052              * system mode, if we arrange to add MO_ALIGN_16.
3053              */
3054             gen_helper_stg_stub(cpu_env, dirty_addr);
3055         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3056             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3057         } else {
3058             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3059         }
3060     }
3061 
3062     if (is_vector) {
3063         mop = finalize_memop_asimd(s, size);
3064     } else {
3065         mop = finalize_memop(s, size);
3066     }
3067     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3068                                 (wback || rn != 31) && !set_tag,
3069                                 2 << size, mop);
3070 
3071     if (is_vector) {
3072         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3073         if (is_load) {
3074             do_fp_ld(s, rt, clean_addr, mop);
3075         } else {
3076             do_fp_st(s, rt, clean_addr, mop);
3077         }
3078         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3079         if (is_load) {
3080             do_fp_ld(s, rt2, clean_addr, mop);
3081         } else {
3082             do_fp_st(s, rt2, clean_addr, mop);
3083         }
3084     } else {
3085         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3086         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3087 
3088         /*
3089          * We built mop above for the single logical access -- rebuild it
3090          * now for the paired operation.
3091          *
3092          * With LSE2, non-sign-extending pairs are treated atomically if
3093          * aligned, and if unaligned one of the pair will be completely
3094          * within a 16-byte block and that element will be atomic.
3095          * Otherwise each element is separately atomic.
3096          * In all cases, issue one operation with the correct atomicity.
3097          *
3098          * This treats sign-extending loads like zero-extending loads,
3099          * since that reuses the most code below.
3100          */
3101         mop = size + 1;
3102         if (s->align_mem) {
3103             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3104         }
3105         mop = finalize_memop_pair(s, mop);
3106 
3107         if (is_load) {
3108             if (size == 2) {
3109                 int o2 = s->be_data == MO_LE ? 32 : 0;
3110                 int o1 = o2 ^ 32;
3111 
3112                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3113                 if (is_signed) {
3114                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3115                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3116                 } else {
3117                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3118                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3119                 }
3120             } else {
3121                 TCGv_i128 tmp = tcg_temp_new_i128();
3122 
3123                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3124                 if (s->be_data == MO_LE) {
3125                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3126                 } else {
3127                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3128                 }
3129             }
3130         } else {
3131             if (size == 2) {
3132                 TCGv_i64 tmp = tcg_temp_new_i64();
3133 
3134                 if (s->be_data == MO_LE) {
3135                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3136                 } else {
3137                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3138                 }
3139                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3140             } else {
3141                 TCGv_i128 tmp = tcg_temp_new_i128();
3142 
3143                 if (s->be_data == MO_LE) {
3144                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3145                 } else {
3146                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3147                 }
3148                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3149             }
3150         }
3151     }
3152 
3153     if (wback) {
3154         if (postindex) {
3155             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3156         }
3157         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3158     }
3159 }
3160 
3161 /*
3162  * Load/store (immediate post-indexed)
3163  * Load/store (immediate pre-indexed)
3164  * Load/store (unscaled immediate)
3165  *
3166  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3167  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3168  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3169  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3170  *
3171  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3172          10 -> unprivileged
3173  * V = 0 -> non-vector
3174  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3175  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3176  */
3177 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3178                                 int opc,
3179                                 int size,
3180                                 int rt,
3181                                 bool is_vector)
3182 {
3183     int rn = extract32(insn, 5, 5);
3184     int imm9 = sextract32(insn, 12, 9);
3185     int idx = extract32(insn, 10, 2);
3186     bool is_signed = false;
3187     bool is_store = false;
3188     bool is_extended = false;
3189     bool is_unpriv = (idx == 2);
3190     bool iss_valid;
3191     bool post_index;
3192     bool writeback;
3193     int memidx;
3194     MemOp memop;
3195     TCGv_i64 clean_addr, dirty_addr;
3196 
3197     if (is_vector) {
3198         size |= (opc & 2) << 1;
3199         if (size > 4 || is_unpriv) {
3200             unallocated_encoding(s);
3201             return;
3202         }
3203         is_store = ((opc & 1) == 0);
3204         if (!fp_access_check(s)) {
3205             return;
3206         }
3207         memop = finalize_memop_asimd(s, size);
3208     } else {
3209         if (size == 3 && opc == 2) {
3210             /* PRFM - prefetch */
3211             if (idx != 0) {
3212                 unallocated_encoding(s);
3213                 return;
3214             }
3215             return;
3216         }
3217         if (opc == 3 && size > 1) {
3218             unallocated_encoding(s);
3219             return;
3220         }
3221         is_store = (opc == 0);
3222         is_signed = !is_store && extract32(opc, 1, 1);
3223         is_extended = (size < 3) && extract32(opc, 0, 1);
3224         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3225     }
3226 
3227     switch (idx) {
3228     case 0:
3229     case 2:
3230         post_index = false;
3231         writeback = false;
3232         break;
3233     case 1:
3234         post_index = true;
3235         writeback = true;
3236         break;
3237     case 3:
3238         post_index = false;
3239         writeback = true;
3240         break;
3241     default:
3242         g_assert_not_reached();
3243     }
3244 
3245     iss_valid = !is_vector && !writeback;
3246 
3247     if (rn == 31) {
3248         gen_check_sp_alignment(s);
3249     }
3250 
3251     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3252     if (!post_index) {
3253         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3254     }
3255 
3256     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3257 
3258     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3259                                        writeback || rn != 31,
3260                                        memop, is_unpriv, memidx);
3261 
3262     if (is_vector) {
3263         if (is_store) {
3264             do_fp_st(s, rt, clean_addr, memop);
3265         } else {
3266             do_fp_ld(s, rt, clean_addr, memop);
3267         }
3268     } else {
3269         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3270         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3271 
3272         if (is_store) {
3273             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3274                              iss_valid, rt, iss_sf, false);
3275         } else {
3276             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3277                              is_extended, memidx,
3278                              iss_valid, rt, iss_sf, false);
3279         }
3280     }
3281 
3282     if (writeback) {
3283         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3284         if (post_index) {
3285             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3286         }
3287         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3288     }
3289 }
3290 
3291 /*
3292  * Load/store (register offset)
3293  *
3294  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3295  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3296  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3297  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3298  *
3299  * For non-vector:
3300  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3301  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3302  * For vector:
3303  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3304  *   opc<0>: 0 -> store, 1 -> load
3305  * V: 1 -> vector/simd
3306  * opt: extend encoding (see DecodeRegExtend)
3307  * S: if S=1 then scale (essentially index by sizeof(size))
3308  * Rt: register to transfer into/out of
3309  * Rn: address register or SP for base
3310  * Rm: offset register or ZR for offset
3311  */
3312 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3313                                    int opc,
3314                                    int size,
3315                                    int rt,
3316                                    bool is_vector)
3317 {
3318     int rn = extract32(insn, 5, 5);
3319     int shift = extract32(insn, 12, 1);
3320     int rm = extract32(insn, 16, 5);
3321     int opt = extract32(insn, 13, 3);
3322     bool is_signed = false;
3323     bool is_store = false;
3324     bool is_extended = false;
3325     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3326     MemOp memop;
3327 
3328     if (extract32(opt, 1, 1) == 0) {
3329         unallocated_encoding(s);
3330         return;
3331     }
3332 
3333     if (is_vector) {
3334         size |= (opc & 2) << 1;
3335         if (size > 4) {
3336             unallocated_encoding(s);
3337             return;
3338         }
3339         is_store = !extract32(opc, 0, 1);
3340         if (!fp_access_check(s)) {
3341             return;
3342         }
3343         memop = finalize_memop_asimd(s, size);
3344     } else {
3345         if (size == 3 && opc == 2) {
3346             /* PRFM - prefetch */
3347             return;
3348         }
3349         if (opc == 3 && size > 1) {
3350             unallocated_encoding(s);
3351             return;
3352         }
3353         is_store = (opc == 0);
3354         is_signed = !is_store && extract32(opc, 1, 1);
3355         is_extended = (size < 3) && extract32(opc, 0, 1);
3356         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3357     }
3358 
3359     if (rn == 31) {
3360         gen_check_sp_alignment(s);
3361     }
3362     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3363 
3364     tcg_rm = read_cpu_reg(s, rm, 1);
3365     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3366 
3367     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3368 
3369     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3370 
3371     if (is_vector) {
3372         if (is_store) {
3373             do_fp_st(s, rt, clean_addr, memop);
3374         } else {
3375             do_fp_ld(s, rt, clean_addr, memop);
3376         }
3377     } else {
3378         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3379         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3380 
3381         if (is_store) {
3382             do_gpr_st(s, tcg_rt, clean_addr, memop,
3383                       true, rt, iss_sf, false);
3384         } else {
3385             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3386                       is_extended, true, rt, iss_sf, false);
3387         }
3388     }
3389 }
3390 
3391 /*
3392  * Load/store (unsigned immediate)
3393  *
3394  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3395  * +----+-------+---+-----+-----+------------+-------+------+
3396  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3397  * +----+-------+---+-----+-----+------------+-------+------+
3398  *
3399  * For non-vector:
3400  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3401  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3402  * For vector:
3403  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3404  *   opc<0>: 0 -> store, 1 -> load
3405  * Rn: base address register (inc SP)
3406  * Rt: target register
3407  */
3408 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3409                                         int opc,
3410                                         int size,
3411                                         int rt,
3412                                         bool is_vector)
3413 {
3414     int rn = extract32(insn, 5, 5);
3415     unsigned int imm12 = extract32(insn, 10, 12);
3416     unsigned int offset;
3417     TCGv_i64 clean_addr, dirty_addr;
3418     bool is_store;
3419     bool is_signed = false;
3420     bool is_extended = false;
3421     MemOp memop;
3422 
3423     if (is_vector) {
3424         size |= (opc & 2) << 1;
3425         if (size > 4) {
3426             unallocated_encoding(s);
3427             return;
3428         }
3429         is_store = !extract32(opc, 0, 1);
3430         if (!fp_access_check(s)) {
3431             return;
3432         }
3433         memop = finalize_memop_asimd(s, size);
3434     } else {
3435         if (size == 3 && opc == 2) {
3436             /* PRFM - prefetch */
3437             return;
3438         }
3439         if (opc == 3 && size > 1) {
3440             unallocated_encoding(s);
3441             return;
3442         }
3443         is_store = (opc == 0);
3444         is_signed = !is_store && extract32(opc, 1, 1);
3445         is_extended = (size < 3) && extract32(opc, 0, 1);
3446         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3447     }
3448 
3449     if (rn == 31) {
3450         gen_check_sp_alignment(s);
3451     }
3452     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3453     offset = imm12 << size;
3454     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3455 
3456     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3457 
3458     if (is_vector) {
3459         if (is_store) {
3460             do_fp_st(s, rt, clean_addr, memop);
3461         } else {
3462             do_fp_ld(s, rt, clean_addr, memop);
3463         }
3464     } else {
3465         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3466         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3467         if (is_store) {
3468             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3469         } else {
3470             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3471                       is_extended, true, rt, iss_sf, false);
3472         }
3473     }
3474 }
3475 
3476 /* Atomic memory operations
3477  *
3478  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3479  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3480  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3481  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3482  *
3483  * Rt: the result register
3484  * Rn: base address or SP
3485  * Rs: the source register for the operation
3486  * V: vector flag (always 0 as of v8.3)
3487  * A: acquire flag
3488  * R: release flag
3489  */
3490 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3491                               int size, int rt, bool is_vector)
3492 {
3493     int rs = extract32(insn, 16, 5);
3494     int rn = extract32(insn, 5, 5);
3495     int o3_opc = extract32(insn, 12, 4);
3496     bool r = extract32(insn, 22, 1);
3497     bool a = extract32(insn, 23, 1);
3498     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3499     AtomicThreeOpFn *fn = NULL;
3500     MemOp mop = size;
3501 
3502     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3503         unallocated_encoding(s);
3504         return;
3505     }
3506     switch (o3_opc) {
3507     case 000: /* LDADD */
3508         fn = tcg_gen_atomic_fetch_add_i64;
3509         break;
3510     case 001: /* LDCLR */
3511         fn = tcg_gen_atomic_fetch_and_i64;
3512         break;
3513     case 002: /* LDEOR */
3514         fn = tcg_gen_atomic_fetch_xor_i64;
3515         break;
3516     case 003: /* LDSET */
3517         fn = tcg_gen_atomic_fetch_or_i64;
3518         break;
3519     case 004: /* LDSMAX */
3520         fn = tcg_gen_atomic_fetch_smax_i64;
3521         mop |= MO_SIGN;
3522         break;
3523     case 005: /* LDSMIN */
3524         fn = tcg_gen_atomic_fetch_smin_i64;
3525         mop |= MO_SIGN;
3526         break;
3527     case 006: /* LDUMAX */
3528         fn = tcg_gen_atomic_fetch_umax_i64;
3529         break;
3530     case 007: /* LDUMIN */
3531         fn = tcg_gen_atomic_fetch_umin_i64;
3532         break;
3533     case 010: /* SWP */
3534         fn = tcg_gen_atomic_xchg_i64;
3535         break;
3536     case 014: /* LDAPR, LDAPRH, LDAPRB */
3537         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3538             rs != 31 || a != 1 || r != 0) {
3539             unallocated_encoding(s);
3540             return;
3541         }
3542         break;
3543     default:
3544         unallocated_encoding(s);
3545         return;
3546     }
3547 
3548     if (rn == 31) {
3549         gen_check_sp_alignment(s);
3550     }
3551 
3552     mop = check_atomic_align(s, rn, mop);
3553     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3554 
3555     if (o3_opc == 014) {
3556         /*
3557          * LDAPR* are a special case because they are a simple load, not a
3558          * fetch-and-do-something op.
3559          * The architectural consistency requirements here are weaker than
3560          * full load-acquire (we only need "load-acquire processor consistent"),
3561          * but we choose to implement them as full LDAQ.
3562          */
3563         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3564                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3565         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3566         return;
3567     }
3568 
3569     tcg_rs = read_cpu_reg(s, rs, true);
3570     tcg_rt = cpu_reg(s, rt);
3571 
3572     if (o3_opc == 1) { /* LDCLR */
3573         tcg_gen_not_i64(tcg_rs, tcg_rs);
3574     }
3575 
3576     /* The tcg atomic primitives are all full barriers.  Therefore we
3577      * can ignore the Acquire and Release bits of this instruction.
3578      */
3579     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3580 
3581     if (mop & MO_SIGN) {
3582         switch (size) {
3583         case MO_8:
3584             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3585             break;
3586         case MO_16:
3587             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3588             break;
3589         case MO_32:
3590             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3591             break;
3592         case MO_64:
3593             break;
3594         default:
3595             g_assert_not_reached();
3596         }
3597     }
3598 }
3599 
3600 /*
3601  * PAC memory operations
3602  *
3603  *  31  30      27  26    24    22  21       12  11  10    5     0
3604  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3605  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3606  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3607  *
3608  * Rt: the result register
3609  * Rn: base address or SP
3610  * V: vector flag (always 0 as of v8.3)
3611  * M: clear for key DA, set for key DB
3612  * W: pre-indexing flag
3613  * S: sign for imm9.
3614  */
3615 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3616                            int size, int rt, bool is_vector)
3617 {
3618     int rn = extract32(insn, 5, 5);
3619     bool is_wback = extract32(insn, 11, 1);
3620     bool use_key_a = !extract32(insn, 23, 1);
3621     int offset;
3622     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3623     MemOp memop;
3624 
3625     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3626         unallocated_encoding(s);
3627         return;
3628     }
3629 
3630     if (rn == 31) {
3631         gen_check_sp_alignment(s);
3632     }
3633     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3634 
3635     if (s->pauth_active) {
3636         if (use_key_a) {
3637             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3638                              tcg_constant_i64(0));
3639         } else {
3640             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3641                              tcg_constant_i64(0));
3642         }
3643     }
3644 
3645     /* Form the 10-bit signed, scaled offset.  */
3646     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3647     offset = sextract32(offset << size, 0, 10 + size);
3648     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3649 
3650     memop = finalize_memop(s, size);
3651 
3652     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3653     clean_addr = gen_mte_check1(s, dirty_addr, false,
3654                                 is_wback || rn != 31, memop);
3655 
3656     tcg_rt = cpu_reg(s, rt);
3657     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3658               /* extend */ false, /* iss_valid */ !is_wback,
3659               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3660 
3661     if (is_wback) {
3662         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3663     }
3664 }
3665 
3666 /*
3667  * LDAPR/STLR (unscaled immediate)
3668  *
3669  *  31  30            24    22  21       12    10    5     0
3670  * +------+-------------+-----+---+--------+-----+----+-----+
3671  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3672  * +------+-------------+-----+---+--------+-----+----+-----+
3673  *
3674  * Rt: source or destination register
3675  * Rn: base register
3676  * imm9: unscaled immediate offset
3677  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3678  * size: size of load/store
3679  */
3680 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3681 {
3682     int rt = extract32(insn, 0, 5);
3683     int rn = extract32(insn, 5, 5);
3684     int offset = sextract32(insn, 12, 9);
3685     int opc = extract32(insn, 22, 2);
3686     int size = extract32(insn, 30, 2);
3687     TCGv_i64 clean_addr, dirty_addr;
3688     bool is_store = false;
3689     bool extend = false;
3690     bool iss_sf;
3691     MemOp mop = size;
3692 
3693     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3694         unallocated_encoding(s);
3695         return;
3696     }
3697 
3698     switch (opc) {
3699     case 0: /* STLURB */
3700         is_store = true;
3701         break;
3702     case 1: /* LDAPUR* */
3703         break;
3704     case 2: /* LDAPURS* 64-bit variant */
3705         if (size == 3) {
3706             unallocated_encoding(s);
3707             return;
3708         }
3709         mop |= MO_SIGN;
3710         break;
3711     case 3: /* LDAPURS* 32-bit variant */
3712         if (size > 1) {
3713             unallocated_encoding(s);
3714             return;
3715         }
3716         mop |= MO_SIGN;
3717         extend = true; /* zero-extend 32->64 after signed load */
3718         break;
3719     default:
3720         g_assert_not_reached();
3721     }
3722 
3723     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3724 
3725     if (rn == 31) {
3726         gen_check_sp_alignment(s);
3727     }
3728 
3729     mop = check_ordered_align(s, rn, offset, is_store, mop);
3730 
3731     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3732     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3733     clean_addr = clean_data_tbi(s, dirty_addr);
3734 
3735     if (is_store) {
3736         /* Store-Release semantics */
3737         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3738         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3739     } else {
3740         /*
3741          * Load-AcquirePC semantics; we implement as the slightly more
3742          * restrictive Load-Acquire.
3743          */
3744         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3745                   extend, true, rt, iss_sf, true);
3746         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3747     }
3748 }
3749 
3750 /* Load/store register (all forms) */
3751 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3752 {
3753     int rt = extract32(insn, 0, 5);
3754     int opc = extract32(insn, 22, 2);
3755     bool is_vector = extract32(insn, 26, 1);
3756     int size = extract32(insn, 30, 2);
3757 
3758     switch (extract32(insn, 24, 2)) {
3759     case 0:
3760         if (extract32(insn, 21, 1) == 0) {
3761             /* Load/store register (unscaled immediate)
3762              * Load/store immediate pre/post-indexed
3763              * Load/store register unprivileged
3764              */
3765             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3766             return;
3767         }
3768         switch (extract32(insn, 10, 2)) {
3769         case 0:
3770             disas_ldst_atomic(s, insn, size, rt, is_vector);
3771             return;
3772         case 2:
3773             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3774             return;
3775         default:
3776             disas_ldst_pac(s, insn, size, rt, is_vector);
3777             return;
3778         }
3779         break;
3780     case 1:
3781         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3782         return;
3783     }
3784     unallocated_encoding(s);
3785 }
3786 
3787 /* AdvSIMD load/store multiple structures
3788  *
3789  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3790  * +---+---+---------------+---+-------------+--------+------+------+------+
3791  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3792  * +---+---+---------------+---+-------------+--------+------+------+------+
3793  *
3794  * AdvSIMD load/store multiple structures (post-indexed)
3795  *
3796  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3797  * +---+---+---------------+---+---+---------+--------+------+------+------+
3798  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3799  * +---+---+---------------+---+---+---------+--------+------+------+------+
3800  *
3801  * Rt: first (or only) SIMD&FP register to be transferred
3802  * Rn: base address or SP
3803  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3804  */
3805 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3806 {
3807     int rt = extract32(insn, 0, 5);
3808     int rn = extract32(insn, 5, 5);
3809     int rm = extract32(insn, 16, 5);
3810     int size = extract32(insn, 10, 2);
3811     int opcode = extract32(insn, 12, 4);
3812     bool is_store = !extract32(insn, 22, 1);
3813     bool is_postidx = extract32(insn, 23, 1);
3814     bool is_q = extract32(insn, 30, 1);
3815     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3816     MemOp endian, align, mop;
3817 
3818     int total;    /* total bytes */
3819     int elements; /* elements per vector */
3820     int rpt;    /* num iterations */
3821     int selem;  /* structure elements */
3822     int r;
3823 
3824     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3825         unallocated_encoding(s);
3826         return;
3827     }
3828 
3829     if (!is_postidx && rm != 0) {
3830         unallocated_encoding(s);
3831         return;
3832     }
3833 
3834     /* From the shared decode logic */
3835     switch (opcode) {
3836     case 0x0:
3837         rpt = 1;
3838         selem = 4;
3839         break;
3840     case 0x2:
3841         rpt = 4;
3842         selem = 1;
3843         break;
3844     case 0x4:
3845         rpt = 1;
3846         selem = 3;
3847         break;
3848     case 0x6:
3849         rpt = 3;
3850         selem = 1;
3851         break;
3852     case 0x7:
3853         rpt = 1;
3854         selem = 1;
3855         break;
3856     case 0x8:
3857         rpt = 1;
3858         selem = 2;
3859         break;
3860     case 0xa:
3861         rpt = 2;
3862         selem = 1;
3863         break;
3864     default:
3865         unallocated_encoding(s);
3866         return;
3867     }
3868 
3869     if (size == 3 && !is_q && selem != 1) {
3870         /* reserved */
3871         unallocated_encoding(s);
3872         return;
3873     }
3874 
3875     if (!fp_access_check(s)) {
3876         return;
3877     }
3878 
3879     if (rn == 31) {
3880         gen_check_sp_alignment(s);
3881     }
3882 
3883     /* For our purposes, bytes are always little-endian.  */
3884     endian = s->be_data;
3885     if (size == 0) {
3886         endian = MO_LE;
3887     }
3888 
3889     total = rpt * selem * (is_q ? 16 : 8);
3890     tcg_rn = cpu_reg_sp(s, rn);
3891 
3892     /*
3893      * Issue the MTE check vs the logical repeat count, before we
3894      * promote consecutive little-endian elements below.
3895      */
3896     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3897                                 total, finalize_memop_asimd(s, size));
3898 
3899     /*
3900      * Consecutive little-endian elements from a single register
3901      * can be promoted to a larger little-endian operation.
3902      */
3903     align = MO_ALIGN;
3904     if (selem == 1 && endian == MO_LE) {
3905         align = pow2_align(size);
3906         size = 3;
3907     }
3908     if (!s->align_mem) {
3909         align = 0;
3910     }
3911     mop = endian | size | align;
3912 
3913     elements = (is_q ? 16 : 8) >> size;
3914     tcg_ebytes = tcg_constant_i64(1 << size);
3915     for (r = 0; r < rpt; r++) {
3916         int e;
3917         for (e = 0; e < elements; e++) {
3918             int xs;
3919             for (xs = 0; xs < selem; xs++) {
3920                 int tt = (rt + r + xs) % 32;
3921                 if (is_store) {
3922                     do_vec_st(s, tt, e, clean_addr, mop);
3923                 } else {
3924                     do_vec_ld(s, tt, e, clean_addr, mop);
3925                 }
3926                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3927             }
3928         }
3929     }
3930 
3931     if (!is_store) {
3932         /* For non-quad operations, setting a slice of the low
3933          * 64 bits of the register clears the high 64 bits (in
3934          * the ARM ARM pseudocode this is implicit in the fact
3935          * that 'rval' is a 64 bit wide variable).
3936          * For quad operations, we might still need to zero the
3937          * high bits of SVE.
3938          */
3939         for (r = 0; r < rpt * selem; r++) {
3940             int tt = (rt + r) % 32;
3941             clear_vec_high(s, is_q, tt);
3942         }
3943     }
3944 
3945     if (is_postidx) {
3946         if (rm == 31) {
3947             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3948         } else {
3949             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3950         }
3951     }
3952 }
3953 
3954 /* AdvSIMD load/store single structure
3955  *
3956  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3957  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3958  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3959  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3960  *
3961  * AdvSIMD load/store single structure (post-indexed)
3962  *
3963  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3964  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3965  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3966  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3967  *
3968  * Rt: first (or only) SIMD&FP register to be transferred
3969  * Rn: base address or SP
3970  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3971  * index = encoded in Q:S:size dependent on size
3972  *
3973  * lane_size = encoded in R, opc
3974  * transfer width = encoded in opc, S, size
3975  */
3976 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3977 {
3978     int rt = extract32(insn, 0, 5);
3979     int rn = extract32(insn, 5, 5);
3980     int rm = extract32(insn, 16, 5);
3981     int size = extract32(insn, 10, 2);
3982     int S = extract32(insn, 12, 1);
3983     int opc = extract32(insn, 13, 3);
3984     int R = extract32(insn, 21, 1);
3985     int is_load = extract32(insn, 22, 1);
3986     int is_postidx = extract32(insn, 23, 1);
3987     int is_q = extract32(insn, 30, 1);
3988 
3989     int scale = extract32(opc, 1, 2);
3990     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3991     bool replicate = false;
3992     int index = is_q << 3 | S << 2 | size;
3993     int xs, total;
3994     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3995     MemOp mop;
3996 
3997     if (extract32(insn, 31, 1)) {
3998         unallocated_encoding(s);
3999         return;
4000     }
4001     if (!is_postidx && rm != 0) {
4002         unallocated_encoding(s);
4003         return;
4004     }
4005 
4006     switch (scale) {
4007     case 3:
4008         if (!is_load || S) {
4009             unallocated_encoding(s);
4010             return;
4011         }
4012         scale = size;
4013         replicate = true;
4014         break;
4015     case 0:
4016         break;
4017     case 1:
4018         if (extract32(size, 0, 1)) {
4019             unallocated_encoding(s);
4020             return;
4021         }
4022         index >>= 1;
4023         break;
4024     case 2:
4025         if (extract32(size, 1, 1)) {
4026             unallocated_encoding(s);
4027             return;
4028         }
4029         if (!extract32(size, 0, 1)) {
4030             index >>= 2;
4031         } else {
4032             if (S) {
4033                 unallocated_encoding(s);
4034                 return;
4035             }
4036             index >>= 3;
4037             scale = 3;
4038         }
4039         break;
4040     default:
4041         g_assert_not_reached();
4042     }
4043 
4044     if (!fp_access_check(s)) {
4045         return;
4046     }
4047 
4048     if (rn == 31) {
4049         gen_check_sp_alignment(s);
4050     }
4051 
4052     total = selem << scale;
4053     tcg_rn = cpu_reg_sp(s, rn);
4054 
4055     mop = finalize_memop_asimd(s, scale);
4056 
4057     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4058                                 total, mop);
4059 
4060     tcg_ebytes = tcg_constant_i64(1 << scale);
4061     for (xs = 0; xs < selem; xs++) {
4062         if (replicate) {
4063             /* Load and replicate to all elements */
4064             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4065 
4066             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4067             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4068                                  (is_q + 1) * 8, vec_full_reg_size(s),
4069                                  tcg_tmp);
4070         } else {
4071             /* Load/store one element per register */
4072             if (is_load) {
4073                 do_vec_ld(s, rt, index, clean_addr, mop);
4074             } else {
4075                 do_vec_st(s, rt, index, clean_addr, mop);
4076             }
4077         }
4078         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4079         rt = (rt + 1) % 32;
4080     }
4081 
4082     if (is_postidx) {
4083         if (rm == 31) {
4084             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4085         } else {
4086             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4087         }
4088     }
4089 }
4090 
4091 /*
4092  * Load/Store memory tags
4093  *
4094  *  31 30 29         24     22  21     12    10      5      0
4095  * +-----+-------------+-----+---+------+-----+------+------+
4096  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4097  * +-----+-------------+-----+---+------+-----+------+------+
4098  */
4099 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4100 {
4101     int rt = extract32(insn, 0, 5);
4102     int rn = extract32(insn, 5, 5);
4103     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4104     int op2 = extract32(insn, 10, 2);
4105     int op1 = extract32(insn, 22, 2);
4106     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4107     int index = 0;
4108     TCGv_i64 addr, clean_addr, tcg_rt;
4109 
4110     /* We checked insn bits [29:24,21] in the caller.  */
4111     if (extract32(insn, 30, 2) != 3) {
4112         goto do_unallocated;
4113     }
4114 
4115     /*
4116      * @index is a tri-state variable which has 3 states:
4117      * < 0 : post-index, writeback
4118      * = 0 : signed offset
4119      * > 0 : pre-index, writeback
4120      */
4121     switch (op1) {
4122     case 0:
4123         if (op2 != 0) {
4124             /* STG */
4125             index = op2 - 2;
4126         } else {
4127             /* STZGM */
4128             if (s->current_el == 0 || offset != 0) {
4129                 goto do_unallocated;
4130             }
4131             is_mult = is_zero = true;
4132         }
4133         break;
4134     case 1:
4135         if (op2 != 0) {
4136             /* STZG */
4137             is_zero = true;
4138             index = op2 - 2;
4139         } else {
4140             /* LDG */
4141             is_load = true;
4142         }
4143         break;
4144     case 2:
4145         if (op2 != 0) {
4146             /* ST2G */
4147             is_pair = true;
4148             index = op2 - 2;
4149         } else {
4150             /* STGM */
4151             if (s->current_el == 0 || offset != 0) {
4152                 goto do_unallocated;
4153             }
4154             is_mult = true;
4155         }
4156         break;
4157     case 3:
4158         if (op2 != 0) {
4159             /* STZ2G */
4160             is_pair = is_zero = true;
4161             index = op2 - 2;
4162         } else {
4163             /* LDGM */
4164             if (s->current_el == 0 || offset != 0) {
4165                 goto do_unallocated;
4166             }
4167             is_mult = is_load = true;
4168         }
4169         break;
4170 
4171     default:
4172     do_unallocated:
4173         unallocated_encoding(s);
4174         return;
4175     }
4176 
4177     if (is_mult
4178         ? !dc_isar_feature(aa64_mte, s)
4179         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4180         goto do_unallocated;
4181     }
4182 
4183     if (rn == 31) {
4184         gen_check_sp_alignment(s);
4185     }
4186 
4187     addr = read_cpu_reg_sp(s, rn, true);
4188     if (index >= 0) {
4189         /* pre-index or signed offset */
4190         tcg_gen_addi_i64(addr, addr, offset);
4191     }
4192 
4193     if (is_mult) {
4194         tcg_rt = cpu_reg(s, rt);
4195 
4196         if (is_zero) {
4197             int size = 4 << s->dcz_blocksize;
4198 
4199             if (s->ata) {
4200                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4201             }
4202             /*
4203              * The non-tags portion of STZGM is mostly like DC_ZVA,
4204              * except the alignment happens before the access.
4205              */
4206             clean_addr = clean_data_tbi(s, addr);
4207             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4208             gen_helper_dc_zva(cpu_env, clean_addr);
4209         } else if (s->ata) {
4210             if (is_load) {
4211                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4212             } else {
4213                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4214             }
4215         } else {
4216             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4217             int size = 4 << GMID_EL1_BS;
4218 
4219             clean_addr = clean_data_tbi(s, addr);
4220             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4221             gen_probe_access(s, clean_addr, acc, size);
4222 
4223             if (is_load) {
4224                 /* The result tags are zeros.  */
4225                 tcg_gen_movi_i64(tcg_rt, 0);
4226             }
4227         }
4228         return;
4229     }
4230 
4231     if (is_load) {
4232         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4233         tcg_rt = cpu_reg(s, rt);
4234         if (s->ata) {
4235             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4236         } else {
4237             /*
4238              * Tag access disabled: we must check for aborts on the load
4239              * load from [rn+offset], and then insert a 0 tag into rt.
4240              */
4241             clean_addr = clean_data_tbi(s, addr);
4242             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4243             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4244         }
4245     } else {
4246         tcg_rt = cpu_reg_sp(s, rt);
4247         if (!s->ata) {
4248             /*
4249              * For STG and ST2G, we need to check alignment and probe memory.
4250              * TODO: For STZG and STZ2G, we could rely on the stores below,
4251              * at least for system mode; user-only won't enforce alignment.
4252              */
4253             if (is_pair) {
4254                 gen_helper_st2g_stub(cpu_env, addr);
4255             } else {
4256                 gen_helper_stg_stub(cpu_env, addr);
4257             }
4258         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4259             if (is_pair) {
4260                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4261             } else {
4262                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4263             }
4264         } else {
4265             if (is_pair) {
4266                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4267             } else {
4268                 gen_helper_stg(cpu_env, addr, tcg_rt);
4269             }
4270         }
4271     }
4272 
4273     if (is_zero) {
4274         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4275         TCGv_i64 zero64 = tcg_constant_i64(0);
4276         TCGv_i128 zero128 = tcg_temp_new_i128();
4277         int mem_index = get_mem_index(s);
4278         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4279 
4280         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4281 
4282         /* This is 1 or 2 atomic 16-byte operations. */
4283         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4284         if (is_pair) {
4285             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4286             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4287         }
4288     }
4289 
4290     if (index != 0) {
4291         /* pre-index or post-index */
4292         if (index < 0) {
4293             /* post-index */
4294             tcg_gen_addi_i64(addr, addr, offset);
4295         }
4296         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4297     }
4298 }
4299 
4300 /* Loads and stores */
4301 static void disas_ldst(DisasContext *s, uint32_t insn)
4302 {
4303     switch (extract32(insn, 24, 6)) {
4304     case 0x08: /* Load/store exclusive */
4305         disas_ldst_excl(s, insn);
4306         break;
4307     case 0x18: case 0x1c: /* Load register (literal) */
4308         disas_ld_lit(s, insn);
4309         break;
4310     case 0x28: case 0x29:
4311     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4312         disas_ldst_pair(s, insn);
4313         break;
4314     case 0x38: case 0x39:
4315     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4316         disas_ldst_reg(s, insn);
4317         break;
4318     case 0x0c: /* AdvSIMD load/store multiple structures */
4319         disas_ldst_multiple_struct(s, insn);
4320         break;
4321     case 0x0d: /* AdvSIMD load/store single structure */
4322         disas_ldst_single_struct(s, insn);
4323         break;
4324     case 0x19:
4325         if (extract32(insn, 21, 1) != 0) {
4326             disas_ldst_tag(s, insn);
4327         } else if (extract32(insn, 10, 2) == 0) {
4328             disas_ldst_ldapr_stlr(s, insn);
4329         } else {
4330             unallocated_encoding(s);
4331         }
4332         break;
4333     default:
4334         unallocated_encoding(s);
4335         break;
4336     }
4337 }
4338 
4339 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4340 
4341 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4342                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4343 {
4344     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4345     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4346     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4347 
4348     fn(tcg_rd, tcg_rn, tcg_imm);
4349     if (!a->sf) {
4350         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4351     }
4352     return true;
4353 }
4354 
4355 /*
4356  * PC-rel. addressing
4357  */
4358 
4359 static bool trans_ADR(DisasContext *s, arg_ri *a)
4360 {
4361     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4362     return true;
4363 }
4364 
4365 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4366 {
4367     int64_t offset = (int64_t)a->imm << 12;
4368 
4369     /* The page offset is ok for CF_PCREL. */
4370     offset -= s->pc_curr & 0xfff;
4371     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4372     return true;
4373 }
4374 
4375 /*
4376  * Add/subtract (immediate)
4377  */
4378 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4379 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4380 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4381 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4382 
4383 /*
4384  * Add/subtract (immediate, with tags)
4385  */
4386 
4387 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4388                                       bool sub_op)
4389 {
4390     TCGv_i64 tcg_rn, tcg_rd;
4391     int imm;
4392 
4393     imm = a->uimm6 << LOG2_TAG_GRANULE;
4394     if (sub_op) {
4395         imm = -imm;
4396     }
4397 
4398     tcg_rn = cpu_reg_sp(s, a->rn);
4399     tcg_rd = cpu_reg_sp(s, a->rd);
4400 
4401     if (s->ata) {
4402         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4403                            tcg_constant_i32(imm),
4404                            tcg_constant_i32(a->uimm4));
4405     } else {
4406         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4407         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4408     }
4409     return true;
4410 }
4411 
4412 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4413 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4414 
4415 /* The input should be a value in the bottom e bits (with higher
4416  * bits zero); returns that value replicated into every element
4417  * of size e in a 64 bit integer.
4418  */
4419 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4420 {
4421     assert(e != 0);
4422     while (e < 64) {
4423         mask |= mask << e;
4424         e *= 2;
4425     }
4426     return mask;
4427 }
4428 
4429 /*
4430  * Logical (immediate)
4431  */
4432 
4433 /*
4434  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4435  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4436  * value (ie should cause a guest UNDEF exception), and true if they are
4437  * valid, in which case the decoded bit pattern is written to result.
4438  */
4439 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4440                             unsigned int imms, unsigned int immr)
4441 {
4442     uint64_t mask;
4443     unsigned e, levels, s, r;
4444     int len;
4445 
4446     assert(immn < 2 && imms < 64 && immr < 64);
4447 
4448     /* The bit patterns we create here are 64 bit patterns which
4449      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4450      * 64 bits each. Each element contains the same value: a run
4451      * of between 1 and e-1 non-zero bits, rotated within the
4452      * element by between 0 and e-1 bits.
4453      *
4454      * The element size and run length are encoded into immn (1 bit)
4455      * and imms (6 bits) as follows:
4456      * 64 bit elements: immn = 1, imms = <length of run - 1>
4457      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4458      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4459      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4460      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4461      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4462      * Notice that immn = 0, imms = 11111x is the only combination
4463      * not covered by one of the above options; this is reserved.
4464      * Further, <length of run - 1> all-ones is a reserved pattern.
4465      *
4466      * In all cases the rotation is by immr % e (and immr is 6 bits).
4467      */
4468 
4469     /* First determine the element size */
4470     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4471     if (len < 1) {
4472         /* This is the immn == 0, imms == 0x11111x case */
4473         return false;
4474     }
4475     e = 1 << len;
4476 
4477     levels = e - 1;
4478     s = imms & levels;
4479     r = immr & levels;
4480 
4481     if (s == levels) {
4482         /* <length of run - 1> mustn't be all-ones. */
4483         return false;
4484     }
4485 
4486     /* Create the value of one element: s+1 set bits rotated
4487      * by r within the element (which is e bits wide)...
4488      */
4489     mask = MAKE_64BIT_MASK(0, s + 1);
4490     if (r) {
4491         mask = (mask >> r) | (mask << (e - r));
4492         mask &= MAKE_64BIT_MASK(0, e);
4493     }
4494     /* ...then replicate the element over the whole 64 bit value */
4495     mask = bitfield_replicate(mask, e);
4496     *result = mask;
4497     return true;
4498 }
4499 
4500 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4501                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4502 {
4503     TCGv_i64 tcg_rd, tcg_rn;
4504     uint64_t imm;
4505 
4506     /* Some immediate field values are reserved. */
4507     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4508                                 extract32(a->dbm, 0, 6),
4509                                 extract32(a->dbm, 6, 6))) {
4510         return false;
4511     }
4512     if (!a->sf) {
4513         imm &= 0xffffffffull;
4514     }
4515 
4516     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4517     tcg_rn = cpu_reg(s, a->rn);
4518 
4519     fn(tcg_rd, tcg_rn, imm);
4520     if (set_cc) {
4521         gen_logic_CC(a->sf, tcg_rd);
4522     }
4523     if (!a->sf) {
4524         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4525     }
4526     return true;
4527 }
4528 
4529 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4530 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4531 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4532 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4533 
4534 /*
4535  * Move wide (immediate)
4536  */
4537 
4538 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4539 {
4540     int pos = a->hw << 4;
4541     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4542     return true;
4543 }
4544 
4545 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4546 {
4547     int pos = a->hw << 4;
4548     uint64_t imm = a->imm;
4549 
4550     imm = ~(imm << pos);
4551     if (!a->sf) {
4552         imm = (uint32_t)imm;
4553     }
4554     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4555     return true;
4556 }
4557 
4558 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4559 {
4560     int pos = a->hw << 4;
4561     TCGv_i64 tcg_rd, tcg_im;
4562 
4563     tcg_rd = cpu_reg(s, a->rd);
4564     tcg_im = tcg_constant_i64(a->imm);
4565     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4566     if (!a->sf) {
4567         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4568     }
4569     return true;
4570 }
4571 
4572 /*
4573  * Bitfield
4574  */
4575 
4576 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4577 {
4578     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4579     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4580     unsigned int bitsize = a->sf ? 64 : 32;
4581     unsigned int ri = a->immr;
4582     unsigned int si = a->imms;
4583     unsigned int pos, len;
4584 
4585     if (si >= ri) {
4586         /* Wd<s-r:0> = Wn<s:r> */
4587         len = (si - ri) + 1;
4588         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4589         if (!a->sf) {
4590             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4591         }
4592     } else {
4593         /* Wd<32+s-r,32-r> = Wn<s:0> */
4594         len = si + 1;
4595         pos = (bitsize - ri) & (bitsize - 1);
4596 
4597         if (len < ri) {
4598             /*
4599              * Sign extend the destination field from len to fill the
4600              * balance of the word.  Let the deposit below insert all
4601              * of those sign bits.
4602              */
4603             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4604             len = ri;
4605         }
4606 
4607         /*
4608          * We start with zero, and we haven't modified any bits outside
4609          * bitsize, therefore no final zero-extension is unneeded for !sf.
4610          */
4611         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4612     }
4613     return true;
4614 }
4615 
4616 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4617 {
4618     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4619     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4620     unsigned int bitsize = a->sf ? 64 : 32;
4621     unsigned int ri = a->immr;
4622     unsigned int si = a->imms;
4623     unsigned int pos, len;
4624 
4625     tcg_rd = cpu_reg(s, a->rd);
4626     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4627 
4628     if (si >= ri) {
4629         /* Wd<s-r:0> = Wn<s:r> */
4630         len = (si - ri) + 1;
4631         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4632     } else {
4633         /* Wd<32+s-r,32-r> = Wn<s:0> */
4634         len = si + 1;
4635         pos = (bitsize - ri) & (bitsize - 1);
4636         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4637     }
4638     return true;
4639 }
4640 
4641 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4642 {
4643     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4644     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4645     unsigned int bitsize = a->sf ? 64 : 32;
4646     unsigned int ri = a->immr;
4647     unsigned int si = a->imms;
4648     unsigned int pos, len;
4649 
4650     tcg_rd = cpu_reg(s, a->rd);
4651     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4652 
4653     if (si >= ri) {
4654         /* Wd<s-r:0> = Wn<s:r> */
4655         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4656         len = (si - ri) + 1;
4657         pos = 0;
4658     } else {
4659         /* Wd<32+s-r,32-r> = Wn<s:0> */
4660         len = si + 1;
4661         pos = (bitsize - ri) & (bitsize - 1);
4662     }
4663 
4664     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4665     if (!a->sf) {
4666         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4667     }
4668     return true;
4669 }
4670 
4671 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4672 {
4673     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4674 
4675     tcg_rd = cpu_reg(s, a->rd);
4676 
4677     if (unlikely(a->imm == 0)) {
4678         /*
4679          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4680          * so an extract from bit 0 is a special case.
4681          */
4682         if (a->sf) {
4683             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4684         } else {
4685             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4686         }
4687     } else {
4688         tcg_rm = cpu_reg(s, a->rm);
4689         tcg_rn = cpu_reg(s, a->rn);
4690 
4691         if (a->sf) {
4692             /* Specialization to ROR happens in EXTRACT2.  */
4693             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4694         } else {
4695             TCGv_i32 t0 = tcg_temp_new_i32();
4696 
4697             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4698             if (a->rm == a->rn) {
4699                 tcg_gen_rotri_i32(t0, t0, a->imm);
4700             } else {
4701                 TCGv_i32 t1 = tcg_temp_new_i32();
4702                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4703                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4704             }
4705             tcg_gen_extu_i32_i64(tcg_rd, t0);
4706         }
4707     }
4708     return true;
4709 }
4710 
4711 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4712  * Note that it is the caller's responsibility to ensure that the
4713  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4714  * mandated semantics for out of range shifts.
4715  */
4716 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4717                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4718 {
4719     switch (shift_type) {
4720     case A64_SHIFT_TYPE_LSL:
4721         tcg_gen_shl_i64(dst, src, shift_amount);
4722         break;
4723     case A64_SHIFT_TYPE_LSR:
4724         tcg_gen_shr_i64(dst, src, shift_amount);
4725         break;
4726     case A64_SHIFT_TYPE_ASR:
4727         if (!sf) {
4728             tcg_gen_ext32s_i64(dst, src);
4729         }
4730         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4731         break;
4732     case A64_SHIFT_TYPE_ROR:
4733         if (sf) {
4734             tcg_gen_rotr_i64(dst, src, shift_amount);
4735         } else {
4736             TCGv_i32 t0, t1;
4737             t0 = tcg_temp_new_i32();
4738             t1 = tcg_temp_new_i32();
4739             tcg_gen_extrl_i64_i32(t0, src);
4740             tcg_gen_extrl_i64_i32(t1, shift_amount);
4741             tcg_gen_rotr_i32(t0, t0, t1);
4742             tcg_gen_extu_i32_i64(dst, t0);
4743         }
4744         break;
4745     default:
4746         assert(FALSE); /* all shift types should be handled */
4747         break;
4748     }
4749 
4750     if (!sf) { /* zero extend final result */
4751         tcg_gen_ext32u_i64(dst, dst);
4752     }
4753 }
4754 
4755 /* Shift a TCGv src by immediate, put result in dst.
4756  * The shift amount must be in range (this should always be true as the
4757  * relevant instructions will UNDEF on bad shift immediates).
4758  */
4759 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4760                           enum a64_shift_type shift_type, unsigned int shift_i)
4761 {
4762     assert(shift_i < (sf ? 64 : 32));
4763 
4764     if (shift_i == 0) {
4765         tcg_gen_mov_i64(dst, src);
4766     } else {
4767         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4768     }
4769 }
4770 
4771 /* Logical (shifted register)
4772  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4773  * +----+-----+-----------+-------+---+------+--------+------+------+
4774  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4775  * +----+-----+-----------+-------+---+------+--------+------+------+
4776  */
4777 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4778 {
4779     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4780     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4781 
4782     sf = extract32(insn, 31, 1);
4783     opc = extract32(insn, 29, 2);
4784     shift_type = extract32(insn, 22, 2);
4785     invert = extract32(insn, 21, 1);
4786     rm = extract32(insn, 16, 5);
4787     shift_amount = extract32(insn, 10, 6);
4788     rn = extract32(insn, 5, 5);
4789     rd = extract32(insn, 0, 5);
4790 
4791     if (!sf && (shift_amount & (1 << 5))) {
4792         unallocated_encoding(s);
4793         return;
4794     }
4795 
4796     tcg_rd = cpu_reg(s, rd);
4797 
4798     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4799         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4800          * register-register MOV and MVN, so it is worth special casing.
4801          */
4802         tcg_rm = cpu_reg(s, rm);
4803         if (invert) {
4804             tcg_gen_not_i64(tcg_rd, tcg_rm);
4805             if (!sf) {
4806                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4807             }
4808         } else {
4809             if (sf) {
4810                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4811             } else {
4812                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4813             }
4814         }
4815         return;
4816     }
4817 
4818     tcg_rm = read_cpu_reg(s, rm, sf);
4819 
4820     if (shift_amount) {
4821         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4822     }
4823 
4824     tcg_rn = cpu_reg(s, rn);
4825 
4826     switch (opc | (invert << 2)) {
4827     case 0: /* AND */
4828     case 3: /* ANDS */
4829         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4830         break;
4831     case 1: /* ORR */
4832         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4833         break;
4834     case 2: /* EOR */
4835         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4836         break;
4837     case 4: /* BIC */
4838     case 7: /* BICS */
4839         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4840         break;
4841     case 5: /* ORN */
4842         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4843         break;
4844     case 6: /* EON */
4845         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4846         break;
4847     default:
4848         assert(FALSE);
4849         break;
4850     }
4851 
4852     if (!sf) {
4853         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4854     }
4855 
4856     if (opc == 3) {
4857         gen_logic_CC(sf, tcg_rd);
4858     }
4859 }
4860 
4861 /*
4862  * Add/subtract (extended register)
4863  *
4864  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4865  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4866  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4867  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4868  *
4869  *  sf: 0 -> 32bit, 1 -> 64bit
4870  *  op: 0 -> add  , 1 -> sub
4871  *   S: 1 -> set flags
4872  * opt: 00
4873  * option: extension type (see DecodeRegExtend)
4874  * imm3: optional shift to Rm
4875  *
4876  * Rd = Rn + LSL(extend(Rm), amount)
4877  */
4878 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4879 {
4880     int rd = extract32(insn, 0, 5);
4881     int rn = extract32(insn, 5, 5);
4882     int imm3 = extract32(insn, 10, 3);
4883     int option = extract32(insn, 13, 3);
4884     int rm = extract32(insn, 16, 5);
4885     int opt = extract32(insn, 22, 2);
4886     bool setflags = extract32(insn, 29, 1);
4887     bool sub_op = extract32(insn, 30, 1);
4888     bool sf = extract32(insn, 31, 1);
4889 
4890     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4891     TCGv_i64 tcg_rd;
4892     TCGv_i64 tcg_result;
4893 
4894     if (imm3 > 4 || opt != 0) {
4895         unallocated_encoding(s);
4896         return;
4897     }
4898 
4899     /* non-flag setting ops may use SP */
4900     if (!setflags) {
4901         tcg_rd = cpu_reg_sp(s, rd);
4902     } else {
4903         tcg_rd = cpu_reg(s, rd);
4904     }
4905     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4906 
4907     tcg_rm = read_cpu_reg(s, rm, sf);
4908     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4909 
4910     tcg_result = tcg_temp_new_i64();
4911 
4912     if (!setflags) {
4913         if (sub_op) {
4914             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4915         } else {
4916             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4917         }
4918     } else {
4919         if (sub_op) {
4920             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4921         } else {
4922             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4923         }
4924     }
4925 
4926     if (sf) {
4927         tcg_gen_mov_i64(tcg_rd, tcg_result);
4928     } else {
4929         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4930     }
4931 }
4932 
4933 /*
4934  * Add/subtract (shifted register)
4935  *
4936  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4937  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4938  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4939  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4940  *
4941  *    sf: 0 -> 32bit, 1 -> 64bit
4942  *    op: 0 -> add  , 1 -> sub
4943  *     S: 1 -> set flags
4944  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4945  *  imm6: Shift amount to apply to Rm before the add/sub
4946  */
4947 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4948 {
4949     int rd = extract32(insn, 0, 5);
4950     int rn = extract32(insn, 5, 5);
4951     int imm6 = extract32(insn, 10, 6);
4952     int rm = extract32(insn, 16, 5);
4953     int shift_type = extract32(insn, 22, 2);
4954     bool setflags = extract32(insn, 29, 1);
4955     bool sub_op = extract32(insn, 30, 1);
4956     bool sf = extract32(insn, 31, 1);
4957 
4958     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4959     TCGv_i64 tcg_rn, tcg_rm;
4960     TCGv_i64 tcg_result;
4961 
4962     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4963         unallocated_encoding(s);
4964         return;
4965     }
4966 
4967     tcg_rn = read_cpu_reg(s, rn, sf);
4968     tcg_rm = read_cpu_reg(s, rm, sf);
4969 
4970     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4971 
4972     tcg_result = tcg_temp_new_i64();
4973 
4974     if (!setflags) {
4975         if (sub_op) {
4976             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4977         } else {
4978             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4979         }
4980     } else {
4981         if (sub_op) {
4982             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4983         } else {
4984             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4985         }
4986     }
4987 
4988     if (sf) {
4989         tcg_gen_mov_i64(tcg_rd, tcg_result);
4990     } else {
4991         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4992     }
4993 }
4994 
4995 /* Data-processing (3 source)
4996  *
4997  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4998  *  +--+------+-----------+------+------+----+------+------+------+
4999  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
5000  *  +--+------+-----------+------+------+----+------+------+------+
5001  */
5002 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
5003 {
5004     int rd = extract32(insn, 0, 5);
5005     int rn = extract32(insn, 5, 5);
5006     int ra = extract32(insn, 10, 5);
5007     int rm = extract32(insn, 16, 5);
5008     int op_id = (extract32(insn, 29, 3) << 4) |
5009         (extract32(insn, 21, 3) << 1) |
5010         extract32(insn, 15, 1);
5011     bool sf = extract32(insn, 31, 1);
5012     bool is_sub = extract32(op_id, 0, 1);
5013     bool is_high = extract32(op_id, 2, 1);
5014     bool is_signed = false;
5015     TCGv_i64 tcg_op1;
5016     TCGv_i64 tcg_op2;
5017     TCGv_i64 tcg_tmp;
5018 
5019     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5020     switch (op_id) {
5021     case 0x42: /* SMADDL */
5022     case 0x43: /* SMSUBL */
5023     case 0x44: /* SMULH */
5024         is_signed = true;
5025         break;
5026     case 0x0: /* MADD (32bit) */
5027     case 0x1: /* MSUB (32bit) */
5028     case 0x40: /* MADD (64bit) */
5029     case 0x41: /* MSUB (64bit) */
5030     case 0x4a: /* UMADDL */
5031     case 0x4b: /* UMSUBL */
5032     case 0x4c: /* UMULH */
5033         break;
5034     default:
5035         unallocated_encoding(s);
5036         return;
5037     }
5038 
5039     if (is_high) {
5040         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5041         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5042         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5043         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5044 
5045         if (is_signed) {
5046             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5047         } else {
5048             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5049         }
5050         return;
5051     }
5052 
5053     tcg_op1 = tcg_temp_new_i64();
5054     tcg_op2 = tcg_temp_new_i64();
5055     tcg_tmp = tcg_temp_new_i64();
5056 
5057     if (op_id < 0x42) {
5058         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5059         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5060     } else {
5061         if (is_signed) {
5062             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5063             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5064         } else {
5065             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5066             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5067         }
5068     }
5069 
5070     if (ra == 31 && !is_sub) {
5071         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5072         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5073     } else {
5074         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5075         if (is_sub) {
5076             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5077         } else {
5078             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5079         }
5080     }
5081 
5082     if (!sf) {
5083         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5084     }
5085 }
5086 
5087 /* Add/subtract (with carry)
5088  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5089  * +--+--+--+------------------------+------+-------------+------+-----+
5090  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5091  * +--+--+--+------------------------+------+-------------+------+-----+
5092  */
5093 
5094 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5095 {
5096     unsigned int sf, op, setflags, rm, rn, rd;
5097     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5098 
5099     sf = extract32(insn, 31, 1);
5100     op = extract32(insn, 30, 1);
5101     setflags = extract32(insn, 29, 1);
5102     rm = extract32(insn, 16, 5);
5103     rn = extract32(insn, 5, 5);
5104     rd = extract32(insn, 0, 5);
5105 
5106     tcg_rd = cpu_reg(s, rd);
5107     tcg_rn = cpu_reg(s, rn);
5108 
5109     if (op) {
5110         tcg_y = tcg_temp_new_i64();
5111         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5112     } else {
5113         tcg_y = cpu_reg(s, rm);
5114     }
5115 
5116     if (setflags) {
5117         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5118     } else {
5119         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5120     }
5121 }
5122 
5123 /*
5124  * Rotate right into flags
5125  *  31 30 29                21       15          10      5  4      0
5126  * +--+--+--+-----------------+--------+-----------+------+--+------+
5127  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5128  * +--+--+--+-----------------+--------+-----------+------+--+------+
5129  */
5130 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5131 {
5132     int mask = extract32(insn, 0, 4);
5133     int o2 = extract32(insn, 4, 1);
5134     int rn = extract32(insn, 5, 5);
5135     int imm6 = extract32(insn, 15, 6);
5136     int sf_op_s = extract32(insn, 29, 3);
5137     TCGv_i64 tcg_rn;
5138     TCGv_i32 nzcv;
5139 
5140     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5141         unallocated_encoding(s);
5142         return;
5143     }
5144 
5145     tcg_rn = read_cpu_reg(s, rn, 1);
5146     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5147 
5148     nzcv = tcg_temp_new_i32();
5149     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5150 
5151     if (mask & 8) { /* N */
5152         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5153     }
5154     if (mask & 4) { /* Z */
5155         tcg_gen_not_i32(cpu_ZF, nzcv);
5156         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5157     }
5158     if (mask & 2) { /* C */
5159         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5160     }
5161     if (mask & 1) { /* V */
5162         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5163     }
5164 }
5165 
5166 /*
5167  * Evaluate into flags
5168  *  31 30 29                21        15   14        10      5  4      0
5169  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5170  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5171  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5172  */
5173 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5174 {
5175     int o3_mask = extract32(insn, 0, 5);
5176     int rn = extract32(insn, 5, 5);
5177     int o2 = extract32(insn, 15, 6);
5178     int sz = extract32(insn, 14, 1);
5179     int sf_op_s = extract32(insn, 29, 3);
5180     TCGv_i32 tmp;
5181     int shift;
5182 
5183     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5184         !dc_isar_feature(aa64_condm_4, s)) {
5185         unallocated_encoding(s);
5186         return;
5187     }
5188     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5189 
5190     tmp = tcg_temp_new_i32();
5191     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5192     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5193     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5194     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5195     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5196 }
5197 
5198 /* Conditional compare (immediate / register)
5199  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5200  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5201  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5202  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5203  *        [1]                             y                [0]       [0]
5204  */
5205 static void disas_cc(DisasContext *s, uint32_t insn)
5206 {
5207     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5208     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5209     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5210     DisasCompare c;
5211 
5212     if (!extract32(insn, 29, 1)) {
5213         unallocated_encoding(s);
5214         return;
5215     }
5216     if (insn & (1 << 10 | 1 << 4)) {
5217         unallocated_encoding(s);
5218         return;
5219     }
5220     sf = extract32(insn, 31, 1);
5221     op = extract32(insn, 30, 1);
5222     is_imm = extract32(insn, 11, 1);
5223     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5224     cond = extract32(insn, 12, 4);
5225     rn = extract32(insn, 5, 5);
5226     nzcv = extract32(insn, 0, 4);
5227 
5228     /* Set T0 = !COND.  */
5229     tcg_t0 = tcg_temp_new_i32();
5230     arm_test_cc(&c, cond);
5231     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5232 
5233     /* Load the arguments for the new comparison.  */
5234     if (is_imm) {
5235         tcg_y = tcg_temp_new_i64();
5236         tcg_gen_movi_i64(tcg_y, y);
5237     } else {
5238         tcg_y = cpu_reg(s, y);
5239     }
5240     tcg_rn = cpu_reg(s, rn);
5241 
5242     /* Set the flags for the new comparison.  */
5243     tcg_tmp = tcg_temp_new_i64();
5244     if (op) {
5245         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5246     } else {
5247         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5248     }
5249 
5250     /* If COND was false, force the flags to #nzcv.  Compute two masks
5251      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5252      * For tcg hosts that support ANDC, we can make do with just T1.
5253      * In either case, allow the tcg optimizer to delete any unused mask.
5254      */
5255     tcg_t1 = tcg_temp_new_i32();
5256     tcg_t2 = tcg_temp_new_i32();
5257     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5258     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5259 
5260     if (nzcv & 8) { /* N */
5261         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5262     } else {
5263         if (TCG_TARGET_HAS_andc_i32) {
5264             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5265         } else {
5266             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5267         }
5268     }
5269     if (nzcv & 4) { /* Z */
5270         if (TCG_TARGET_HAS_andc_i32) {
5271             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5272         } else {
5273             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5274         }
5275     } else {
5276         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5277     }
5278     if (nzcv & 2) { /* C */
5279         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5280     } else {
5281         if (TCG_TARGET_HAS_andc_i32) {
5282             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5283         } else {
5284             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5285         }
5286     }
5287     if (nzcv & 1) { /* V */
5288         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5289     } else {
5290         if (TCG_TARGET_HAS_andc_i32) {
5291             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5292         } else {
5293             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5294         }
5295     }
5296 }
5297 
5298 /* Conditional select
5299  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5300  * +----+----+---+-----------------+------+------+-----+------+------+
5301  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5302  * +----+----+---+-----------------+------+------+-----+------+------+
5303  */
5304 static void disas_cond_select(DisasContext *s, uint32_t insn)
5305 {
5306     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5307     TCGv_i64 tcg_rd, zero;
5308     DisasCompare64 c;
5309 
5310     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5311         /* S == 1 or op2<1> == 1 */
5312         unallocated_encoding(s);
5313         return;
5314     }
5315     sf = extract32(insn, 31, 1);
5316     else_inv = extract32(insn, 30, 1);
5317     rm = extract32(insn, 16, 5);
5318     cond = extract32(insn, 12, 4);
5319     else_inc = extract32(insn, 10, 1);
5320     rn = extract32(insn, 5, 5);
5321     rd = extract32(insn, 0, 5);
5322 
5323     tcg_rd = cpu_reg(s, rd);
5324 
5325     a64_test_cc(&c, cond);
5326     zero = tcg_constant_i64(0);
5327 
5328     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5329         /* CSET & CSETM.  */
5330         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5331         if (else_inv) {
5332             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5333         }
5334     } else {
5335         TCGv_i64 t_true = cpu_reg(s, rn);
5336         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5337         if (else_inv && else_inc) {
5338             tcg_gen_neg_i64(t_false, t_false);
5339         } else if (else_inv) {
5340             tcg_gen_not_i64(t_false, t_false);
5341         } else if (else_inc) {
5342             tcg_gen_addi_i64(t_false, t_false, 1);
5343         }
5344         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5345     }
5346 
5347     if (!sf) {
5348         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5349     }
5350 }
5351 
5352 static void handle_clz(DisasContext *s, unsigned int sf,
5353                        unsigned int rn, unsigned int rd)
5354 {
5355     TCGv_i64 tcg_rd, tcg_rn;
5356     tcg_rd = cpu_reg(s, rd);
5357     tcg_rn = cpu_reg(s, rn);
5358 
5359     if (sf) {
5360         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5361     } else {
5362         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5363         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5364         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5365         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5366     }
5367 }
5368 
5369 static void handle_cls(DisasContext *s, unsigned int sf,
5370                        unsigned int rn, unsigned int rd)
5371 {
5372     TCGv_i64 tcg_rd, tcg_rn;
5373     tcg_rd = cpu_reg(s, rd);
5374     tcg_rn = cpu_reg(s, rn);
5375 
5376     if (sf) {
5377         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5378     } else {
5379         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5380         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5381         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5382         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5383     }
5384 }
5385 
5386 static void handle_rbit(DisasContext *s, unsigned int sf,
5387                         unsigned int rn, unsigned int rd)
5388 {
5389     TCGv_i64 tcg_rd, tcg_rn;
5390     tcg_rd = cpu_reg(s, rd);
5391     tcg_rn = cpu_reg(s, rn);
5392 
5393     if (sf) {
5394         gen_helper_rbit64(tcg_rd, tcg_rn);
5395     } else {
5396         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5397         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5398         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5399         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5400     }
5401 }
5402 
5403 /* REV with sf==1, opcode==3 ("REV64") */
5404 static void handle_rev64(DisasContext *s, unsigned int sf,
5405                          unsigned int rn, unsigned int rd)
5406 {
5407     if (!sf) {
5408         unallocated_encoding(s);
5409         return;
5410     }
5411     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5412 }
5413 
5414 /* REV with sf==0, opcode==2
5415  * REV32 (sf==1, opcode==2)
5416  */
5417 static void handle_rev32(DisasContext *s, unsigned int sf,
5418                          unsigned int rn, unsigned int rd)
5419 {
5420     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5421     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5422 
5423     if (sf) {
5424         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5425         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5426     } else {
5427         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5428     }
5429 }
5430 
5431 /* REV16 (opcode==1) */
5432 static void handle_rev16(DisasContext *s, unsigned int sf,
5433                          unsigned int rn, unsigned int rd)
5434 {
5435     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5436     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5437     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5438     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5439 
5440     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5441     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5442     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5443     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5444     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5445 }
5446 
5447 /* Data-processing (1 source)
5448  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5449  * +----+---+---+-----------------+---------+--------+------+------+
5450  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5451  * +----+---+---+-----------------+---------+--------+------+------+
5452  */
5453 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5454 {
5455     unsigned int sf, opcode, opcode2, rn, rd;
5456     TCGv_i64 tcg_rd;
5457 
5458     if (extract32(insn, 29, 1)) {
5459         unallocated_encoding(s);
5460         return;
5461     }
5462 
5463     sf = extract32(insn, 31, 1);
5464     opcode = extract32(insn, 10, 6);
5465     opcode2 = extract32(insn, 16, 5);
5466     rn = extract32(insn, 5, 5);
5467     rd = extract32(insn, 0, 5);
5468 
5469 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5470 
5471     switch (MAP(sf, opcode2, opcode)) {
5472     case MAP(0, 0x00, 0x00): /* RBIT */
5473     case MAP(1, 0x00, 0x00):
5474         handle_rbit(s, sf, rn, rd);
5475         break;
5476     case MAP(0, 0x00, 0x01): /* REV16 */
5477     case MAP(1, 0x00, 0x01):
5478         handle_rev16(s, sf, rn, rd);
5479         break;
5480     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5481     case MAP(1, 0x00, 0x02):
5482         handle_rev32(s, sf, rn, rd);
5483         break;
5484     case MAP(1, 0x00, 0x03): /* REV64 */
5485         handle_rev64(s, sf, rn, rd);
5486         break;
5487     case MAP(0, 0x00, 0x04): /* CLZ */
5488     case MAP(1, 0x00, 0x04):
5489         handle_clz(s, sf, rn, rd);
5490         break;
5491     case MAP(0, 0x00, 0x05): /* CLS */
5492     case MAP(1, 0x00, 0x05):
5493         handle_cls(s, sf, rn, rd);
5494         break;
5495     case MAP(1, 0x01, 0x00): /* PACIA */
5496         if (s->pauth_active) {
5497             tcg_rd = cpu_reg(s, rd);
5498             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5499         } else if (!dc_isar_feature(aa64_pauth, s)) {
5500             goto do_unallocated;
5501         }
5502         break;
5503     case MAP(1, 0x01, 0x01): /* PACIB */
5504         if (s->pauth_active) {
5505             tcg_rd = cpu_reg(s, rd);
5506             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5507         } else if (!dc_isar_feature(aa64_pauth, s)) {
5508             goto do_unallocated;
5509         }
5510         break;
5511     case MAP(1, 0x01, 0x02): /* PACDA */
5512         if (s->pauth_active) {
5513             tcg_rd = cpu_reg(s, rd);
5514             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5515         } else if (!dc_isar_feature(aa64_pauth, s)) {
5516             goto do_unallocated;
5517         }
5518         break;
5519     case MAP(1, 0x01, 0x03): /* PACDB */
5520         if (s->pauth_active) {
5521             tcg_rd = cpu_reg(s, rd);
5522             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5523         } else if (!dc_isar_feature(aa64_pauth, s)) {
5524             goto do_unallocated;
5525         }
5526         break;
5527     case MAP(1, 0x01, 0x04): /* AUTIA */
5528         if (s->pauth_active) {
5529             tcg_rd = cpu_reg(s, rd);
5530             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5531         } else if (!dc_isar_feature(aa64_pauth, s)) {
5532             goto do_unallocated;
5533         }
5534         break;
5535     case MAP(1, 0x01, 0x05): /* AUTIB */
5536         if (s->pauth_active) {
5537             tcg_rd = cpu_reg(s, rd);
5538             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5539         } else if (!dc_isar_feature(aa64_pauth, s)) {
5540             goto do_unallocated;
5541         }
5542         break;
5543     case MAP(1, 0x01, 0x06): /* AUTDA */
5544         if (s->pauth_active) {
5545             tcg_rd = cpu_reg(s, rd);
5546             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5547         } else if (!dc_isar_feature(aa64_pauth, s)) {
5548             goto do_unallocated;
5549         }
5550         break;
5551     case MAP(1, 0x01, 0x07): /* AUTDB */
5552         if (s->pauth_active) {
5553             tcg_rd = cpu_reg(s, rd);
5554             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5555         } else if (!dc_isar_feature(aa64_pauth, s)) {
5556             goto do_unallocated;
5557         }
5558         break;
5559     case MAP(1, 0x01, 0x08): /* PACIZA */
5560         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5561             goto do_unallocated;
5562         } else if (s->pauth_active) {
5563             tcg_rd = cpu_reg(s, rd);
5564             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5565         }
5566         break;
5567     case MAP(1, 0x01, 0x09): /* PACIZB */
5568         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5569             goto do_unallocated;
5570         } else if (s->pauth_active) {
5571             tcg_rd = cpu_reg(s, rd);
5572             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5573         }
5574         break;
5575     case MAP(1, 0x01, 0x0a): /* PACDZA */
5576         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5577             goto do_unallocated;
5578         } else if (s->pauth_active) {
5579             tcg_rd = cpu_reg(s, rd);
5580             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5581         }
5582         break;
5583     case MAP(1, 0x01, 0x0b): /* PACDZB */
5584         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5585             goto do_unallocated;
5586         } else if (s->pauth_active) {
5587             tcg_rd = cpu_reg(s, rd);
5588             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5589         }
5590         break;
5591     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5592         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5593             goto do_unallocated;
5594         } else if (s->pauth_active) {
5595             tcg_rd = cpu_reg(s, rd);
5596             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5597         }
5598         break;
5599     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5600         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5601             goto do_unallocated;
5602         } else if (s->pauth_active) {
5603             tcg_rd = cpu_reg(s, rd);
5604             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5605         }
5606         break;
5607     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5608         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5609             goto do_unallocated;
5610         } else if (s->pauth_active) {
5611             tcg_rd = cpu_reg(s, rd);
5612             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5613         }
5614         break;
5615     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5616         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5617             goto do_unallocated;
5618         } else if (s->pauth_active) {
5619             tcg_rd = cpu_reg(s, rd);
5620             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5621         }
5622         break;
5623     case MAP(1, 0x01, 0x10): /* XPACI */
5624         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5625             goto do_unallocated;
5626         } else if (s->pauth_active) {
5627             tcg_rd = cpu_reg(s, rd);
5628             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5629         }
5630         break;
5631     case MAP(1, 0x01, 0x11): /* XPACD */
5632         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5633             goto do_unallocated;
5634         } else if (s->pauth_active) {
5635             tcg_rd = cpu_reg(s, rd);
5636             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5637         }
5638         break;
5639     default:
5640     do_unallocated:
5641         unallocated_encoding(s);
5642         break;
5643     }
5644 
5645 #undef MAP
5646 }
5647 
5648 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5649                        unsigned int rm, unsigned int rn, unsigned int rd)
5650 {
5651     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5652     tcg_rd = cpu_reg(s, rd);
5653 
5654     if (!sf && is_signed) {
5655         tcg_n = tcg_temp_new_i64();
5656         tcg_m = tcg_temp_new_i64();
5657         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5658         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5659     } else {
5660         tcg_n = read_cpu_reg(s, rn, sf);
5661         tcg_m = read_cpu_reg(s, rm, sf);
5662     }
5663 
5664     if (is_signed) {
5665         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5666     } else {
5667         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5668     }
5669 
5670     if (!sf) { /* zero extend final result */
5671         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5672     }
5673 }
5674 
5675 /* LSLV, LSRV, ASRV, RORV */
5676 static void handle_shift_reg(DisasContext *s,
5677                              enum a64_shift_type shift_type, unsigned int sf,
5678                              unsigned int rm, unsigned int rn, unsigned int rd)
5679 {
5680     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5681     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5682     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5683 
5684     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5685     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5686 }
5687 
5688 /* CRC32[BHWX], CRC32C[BHWX] */
5689 static void handle_crc32(DisasContext *s,
5690                          unsigned int sf, unsigned int sz, bool crc32c,
5691                          unsigned int rm, unsigned int rn, unsigned int rd)
5692 {
5693     TCGv_i64 tcg_acc, tcg_val;
5694     TCGv_i32 tcg_bytes;
5695 
5696     if (!dc_isar_feature(aa64_crc32, s)
5697         || (sf == 1 && sz != 3)
5698         || (sf == 0 && sz == 3)) {
5699         unallocated_encoding(s);
5700         return;
5701     }
5702 
5703     if (sz == 3) {
5704         tcg_val = cpu_reg(s, rm);
5705     } else {
5706         uint64_t mask;
5707         switch (sz) {
5708         case 0:
5709             mask = 0xFF;
5710             break;
5711         case 1:
5712             mask = 0xFFFF;
5713             break;
5714         case 2:
5715             mask = 0xFFFFFFFF;
5716             break;
5717         default:
5718             g_assert_not_reached();
5719         }
5720         tcg_val = tcg_temp_new_i64();
5721         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5722     }
5723 
5724     tcg_acc = cpu_reg(s, rn);
5725     tcg_bytes = tcg_constant_i32(1 << sz);
5726 
5727     if (crc32c) {
5728         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5729     } else {
5730         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5731     }
5732 }
5733 
5734 /* Data-processing (2 source)
5735  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5736  * +----+---+---+-----------------+------+--------+------+------+
5737  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5738  * +----+---+---+-----------------+------+--------+------+------+
5739  */
5740 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5741 {
5742     unsigned int sf, rm, opcode, rn, rd, setflag;
5743     sf = extract32(insn, 31, 1);
5744     setflag = extract32(insn, 29, 1);
5745     rm = extract32(insn, 16, 5);
5746     opcode = extract32(insn, 10, 6);
5747     rn = extract32(insn, 5, 5);
5748     rd = extract32(insn, 0, 5);
5749 
5750     if (setflag && opcode != 0) {
5751         unallocated_encoding(s);
5752         return;
5753     }
5754 
5755     switch (opcode) {
5756     case 0: /* SUBP(S) */
5757         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5758             goto do_unallocated;
5759         } else {
5760             TCGv_i64 tcg_n, tcg_m, tcg_d;
5761 
5762             tcg_n = read_cpu_reg_sp(s, rn, true);
5763             tcg_m = read_cpu_reg_sp(s, rm, true);
5764             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5765             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5766             tcg_d = cpu_reg(s, rd);
5767 
5768             if (setflag) {
5769                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5770             } else {
5771                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5772             }
5773         }
5774         break;
5775     case 2: /* UDIV */
5776         handle_div(s, false, sf, rm, rn, rd);
5777         break;
5778     case 3: /* SDIV */
5779         handle_div(s, true, sf, rm, rn, rd);
5780         break;
5781     case 4: /* IRG */
5782         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5783             goto do_unallocated;
5784         }
5785         if (s->ata) {
5786             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5787                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5788         } else {
5789             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5790                                              cpu_reg_sp(s, rn));
5791         }
5792         break;
5793     case 5: /* GMI */
5794         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5795             goto do_unallocated;
5796         } else {
5797             TCGv_i64 t = tcg_temp_new_i64();
5798 
5799             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5800             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5801             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5802         }
5803         break;
5804     case 8: /* LSLV */
5805         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5806         break;
5807     case 9: /* LSRV */
5808         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5809         break;
5810     case 10: /* ASRV */
5811         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5812         break;
5813     case 11: /* RORV */
5814         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5815         break;
5816     case 12: /* PACGA */
5817         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5818             goto do_unallocated;
5819         }
5820         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5821                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5822         break;
5823     case 16:
5824     case 17:
5825     case 18:
5826     case 19:
5827     case 20:
5828     case 21:
5829     case 22:
5830     case 23: /* CRC32 */
5831     {
5832         int sz = extract32(opcode, 0, 2);
5833         bool crc32c = extract32(opcode, 2, 1);
5834         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5835         break;
5836     }
5837     default:
5838     do_unallocated:
5839         unallocated_encoding(s);
5840         break;
5841     }
5842 }
5843 
5844 /*
5845  * Data processing - register
5846  *  31  30 29  28      25    21  20  16      10         0
5847  * +--+---+--+---+-------+-----+-------+-------+---------+
5848  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5849  * +--+---+--+---+-------+-----+-------+-------+---------+
5850  */
5851 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5852 {
5853     int op0 = extract32(insn, 30, 1);
5854     int op1 = extract32(insn, 28, 1);
5855     int op2 = extract32(insn, 21, 4);
5856     int op3 = extract32(insn, 10, 6);
5857 
5858     if (!op1) {
5859         if (op2 & 8) {
5860             if (op2 & 1) {
5861                 /* Add/sub (extended register) */
5862                 disas_add_sub_ext_reg(s, insn);
5863             } else {
5864                 /* Add/sub (shifted register) */
5865                 disas_add_sub_reg(s, insn);
5866             }
5867         } else {
5868             /* Logical (shifted register) */
5869             disas_logic_reg(s, insn);
5870         }
5871         return;
5872     }
5873 
5874     switch (op2) {
5875     case 0x0:
5876         switch (op3) {
5877         case 0x00: /* Add/subtract (with carry) */
5878             disas_adc_sbc(s, insn);
5879             break;
5880 
5881         case 0x01: /* Rotate right into flags */
5882         case 0x21:
5883             disas_rotate_right_into_flags(s, insn);
5884             break;
5885 
5886         case 0x02: /* Evaluate into flags */
5887         case 0x12:
5888         case 0x22:
5889         case 0x32:
5890             disas_evaluate_into_flags(s, insn);
5891             break;
5892 
5893         default:
5894             goto do_unallocated;
5895         }
5896         break;
5897 
5898     case 0x2: /* Conditional compare */
5899         disas_cc(s, insn); /* both imm and reg forms */
5900         break;
5901 
5902     case 0x4: /* Conditional select */
5903         disas_cond_select(s, insn);
5904         break;
5905 
5906     case 0x6: /* Data-processing */
5907         if (op0) {    /* (1 source) */
5908             disas_data_proc_1src(s, insn);
5909         } else {      /* (2 source) */
5910             disas_data_proc_2src(s, insn);
5911         }
5912         break;
5913     case 0x8 ... 0xf: /* (3 source) */
5914         disas_data_proc_3src(s, insn);
5915         break;
5916 
5917     default:
5918     do_unallocated:
5919         unallocated_encoding(s);
5920         break;
5921     }
5922 }
5923 
5924 static void handle_fp_compare(DisasContext *s, int size,
5925                               unsigned int rn, unsigned int rm,
5926                               bool cmp_with_zero, bool signal_all_nans)
5927 {
5928     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5929     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5930 
5931     if (size == MO_64) {
5932         TCGv_i64 tcg_vn, tcg_vm;
5933 
5934         tcg_vn = read_fp_dreg(s, rn);
5935         if (cmp_with_zero) {
5936             tcg_vm = tcg_constant_i64(0);
5937         } else {
5938             tcg_vm = read_fp_dreg(s, rm);
5939         }
5940         if (signal_all_nans) {
5941             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5942         } else {
5943             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5944         }
5945     } else {
5946         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5947         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5948 
5949         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5950         if (cmp_with_zero) {
5951             tcg_gen_movi_i32(tcg_vm, 0);
5952         } else {
5953             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5954         }
5955 
5956         switch (size) {
5957         case MO_32:
5958             if (signal_all_nans) {
5959                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5960             } else {
5961                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5962             }
5963             break;
5964         case MO_16:
5965             if (signal_all_nans) {
5966                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5967             } else {
5968                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5969             }
5970             break;
5971         default:
5972             g_assert_not_reached();
5973         }
5974     }
5975 
5976     gen_set_nzcv(tcg_flags);
5977 }
5978 
5979 /* Floating point compare
5980  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5981  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5982  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5983  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5984  */
5985 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5986 {
5987     unsigned int mos, type, rm, op, rn, opc, op2r;
5988     int size;
5989 
5990     mos = extract32(insn, 29, 3);
5991     type = extract32(insn, 22, 2);
5992     rm = extract32(insn, 16, 5);
5993     op = extract32(insn, 14, 2);
5994     rn = extract32(insn, 5, 5);
5995     opc = extract32(insn, 3, 2);
5996     op2r = extract32(insn, 0, 3);
5997 
5998     if (mos || op || op2r) {
5999         unallocated_encoding(s);
6000         return;
6001     }
6002 
6003     switch (type) {
6004     case 0:
6005         size = MO_32;
6006         break;
6007     case 1:
6008         size = MO_64;
6009         break;
6010     case 3:
6011         size = MO_16;
6012         if (dc_isar_feature(aa64_fp16, s)) {
6013             break;
6014         }
6015         /* fallthru */
6016     default:
6017         unallocated_encoding(s);
6018         return;
6019     }
6020 
6021     if (!fp_access_check(s)) {
6022         return;
6023     }
6024 
6025     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6026 }
6027 
6028 /* Floating point conditional compare
6029  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6030  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6031  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6032  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6033  */
6034 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6035 {
6036     unsigned int mos, type, rm, cond, rn, op, nzcv;
6037     TCGLabel *label_continue = NULL;
6038     int size;
6039 
6040     mos = extract32(insn, 29, 3);
6041     type = extract32(insn, 22, 2);
6042     rm = extract32(insn, 16, 5);
6043     cond = extract32(insn, 12, 4);
6044     rn = extract32(insn, 5, 5);
6045     op = extract32(insn, 4, 1);
6046     nzcv = extract32(insn, 0, 4);
6047 
6048     if (mos) {
6049         unallocated_encoding(s);
6050         return;
6051     }
6052 
6053     switch (type) {
6054     case 0:
6055         size = MO_32;
6056         break;
6057     case 1:
6058         size = MO_64;
6059         break;
6060     case 3:
6061         size = MO_16;
6062         if (dc_isar_feature(aa64_fp16, s)) {
6063             break;
6064         }
6065         /* fallthru */
6066     default:
6067         unallocated_encoding(s);
6068         return;
6069     }
6070 
6071     if (!fp_access_check(s)) {
6072         return;
6073     }
6074 
6075     if (cond < 0x0e) { /* not always */
6076         TCGLabel *label_match = gen_new_label();
6077         label_continue = gen_new_label();
6078         arm_gen_test_cc(cond, label_match);
6079         /* nomatch: */
6080         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6081         tcg_gen_br(label_continue);
6082         gen_set_label(label_match);
6083     }
6084 
6085     handle_fp_compare(s, size, rn, rm, false, op);
6086 
6087     if (cond < 0x0e) {
6088         gen_set_label(label_continue);
6089     }
6090 }
6091 
6092 /* Floating point conditional select
6093  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6094  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6095  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6096  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6097  */
6098 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6099 {
6100     unsigned int mos, type, rm, cond, rn, rd;
6101     TCGv_i64 t_true, t_false;
6102     DisasCompare64 c;
6103     MemOp sz;
6104 
6105     mos = extract32(insn, 29, 3);
6106     type = extract32(insn, 22, 2);
6107     rm = extract32(insn, 16, 5);
6108     cond = extract32(insn, 12, 4);
6109     rn = extract32(insn, 5, 5);
6110     rd = extract32(insn, 0, 5);
6111 
6112     if (mos) {
6113         unallocated_encoding(s);
6114         return;
6115     }
6116 
6117     switch (type) {
6118     case 0:
6119         sz = MO_32;
6120         break;
6121     case 1:
6122         sz = MO_64;
6123         break;
6124     case 3:
6125         sz = MO_16;
6126         if (dc_isar_feature(aa64_fp16, s)) {
6127             break;
6128         }
6129         /* fallthru */
6130     default:
6131         unallocated_encoding(s);
6132         return;
6133     }
6134 
6135     if (!fp_access_check(s)) {
6136         return;
6137     }
6138 
6139     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6140     t_true = tcg_temp_new_i64();
6141     t_false = tcg_temp_new_i64();
6142     read_vec_element(s, t_true, rn, 0, sz);
6143     read_vec_element(s, t_false, rm, 0, sz);
6144 
6145     a64_test_cc(&c, cond);
6146     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6147                         t_true, t_false);
6148 
6149     /* Note that sregs & hregs write back zeros to the high bits,
6150        and we've already done the zero-extension.  */
6151     write_fp_dreg(s, rd, t_true);
6152 }
6153 
6154 /* Floating-point data-processing (1 source) - half precision */
6155 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6156 {
6157     TCGv_ptr fpst = NULL;
6158     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6159     TCGv_i32 tcg_res = tcg_temp_new_i32();
6160 
6161     switch (opcode) {
6162     case 0x0: /* FMOV */
6163         tcg_gen_mov_i32(tcg_res, tcg_op);
6164         break;
6165     case 0x1: /* FABS */
6166         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6167         break;
6168     case 0x2: /* FNEG */
6169         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6170         break;
6171     case 0x3: /* FSQRT */
6172         fpst = fpstatus_ptr(FPST_FPCR_F16);
6173         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6174         break;
6175     case 0x8: /* FRINTN */
6176     case 0x9: /* FRINTP */
6177     case 0xa: /* FRINTM */
6178     case 0xb: /* FRINTZ */
6179     case 0xc: /* FRINTA */
6180     {
6181         TCGv_i32 tcg_rmode;
6182 
6183         fpst = fpstatus_ptr(FPST_FPCR_F16);
6184         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6185         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6186         gen_restore_rmode(tcg_rmode, fpst);
6187         break;
6188     }
6189     case 0xe: /* FRINTX */
6190         fpst = fpstatus_ptr(FPST_FPCR_F16);
6191         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6192         break;
6193     case 0xf: /* FRINTI */
6194         fpst = fpstatus_ptr(FPST_FPCR_F16);
6195         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6196         break;
6197     default:
6198         g_assert_not_reached();
6199     }
6200 
6201     write_fp_sreg(s, rd, tcg_res);
6202 }
6203 
6204 /* Floating-point data-processing (1 source) - single precision */
6205 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6206 {
6207     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6208     TCGv_i32 tcg_op, tcg_res;
6209     TCGv_ptr fpst;
6210     int rmode = -1;
6211 
6212     tcg_op = read_fp_sreg(s, rn);
6213     tcg_res = tcg_temp_new_i32();
6214 
6215     switch (opcode) {
6216     case 0x0: /* FMOV */
6217         tcg_gen_mov_i32(tcg_res, tcg_op);
6218         goto done;
6219     case 0x1: /* FABS */
6220         gen_helper_vfp_abss(tcg_res, tcg_op);
6221         goto done;
6222     case 0x2: /* FNEG */
6223         gen_helper_vfp_negs(tcg_res, tcg_op);
6224         goto done;
6225     case 0x3: /* FSQRT */
6226         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6227         goto done;
6228     case 0x6: /* BFCVT */
6229         gen_fpst = gen_helper_bfcvt;
6230         break;
6231     case 0x8: /* FRINTN */
6232     case 0x9: /* FRINTP */
6233     case 0xa: /* FRINTM */
6234     case 0xb: /* FRINTZ */
6235     case 0xc: /* FRINTA */
6236         rmode = opcode & 7;
6237         gen_fpst = gen_helper_rints;
6238         break;
6239     case 0xe: /* FRINTX */
6240         gen_fpst = gen_helper_rints_exact;
6241         break;
6242     case 0xf: /* FRINTI */
6243         gen_fpst = gen_helper_rints;
6244         break;
6245     case 0x10: /* FRINT32Z */
6246         rmode = FPROUNDING_ZERO;
6247         gen_fpst = gen_helper_frint32_s;
6248         break;
6249     case 0x11: /* FRINT32X */
6250         gen_fpst = gen_helper_frint32_s;
6251         break;
6252     case 0x12: /* FRINT64Z */
6253         rmode = FPROUNDING_ZERO;
6254         gen_fpst = gen_helper_frint64_s;
6255         break;
6256     case 0x13: /* FRINT64X */
6257         gen_fpst = gen_helper_frint64_s;
6258         break;
6259     default:
6260         g_assert_not_reached();
6261     }
6262 
6263     fpst = fpstatus_ptr(FPST_FPCR);
6264     if (rmode >= 0) {
6265         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6266         gen_fpst(tcg_res, tcg_op, fpst);
6267         gen_restore_rmode(tcg_rmode, fpst);
6268     } else {
6269         gen_fpst(tcg_res, tcg_op, fpst);
6270     }
6271 
6272  done:
6273     write_fp_sreg(s, rd, tcg_res);
6274 }
6275 
6276 /* Floating-point data-processing (1 source) - double precision */
6277 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6278 {
6279     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6280     TCGv_i64 tcg_op, tcg_res;
6281     TCGv_ptr fpst;
6282     int rmode = -1;
6283 
6284     switch (opcode) {
6285     case 0x0: /* FMOV */
6286         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6287         return;
6288     }
6289 
6290     tcg_op = read_fp_dreg(s, rn);
6291     tcg_res = tcg_temp_new_i64();
6292 
6293     switch (opcode) {
6294     case 0x1: /* FABS */
6295         gen_helper_vfp_absd(tcg_res, tcg_op);
6296         goto done;
6297     case 0x2: /* FNEG */
6298         gen_helper_vfp_negd(tcg_res, tcg_op);
6299         goto done;
6300     case 0x3: /* FSQRT */
6301         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6302         goto done;
6303     case 0x8: /* FRINTN */
6304     case 0x9: /* FRINTP */
6305     case 0xa: /* FRINTM */
6306     case 0xb: /* FRINTZ */
6307     case 0xc: /* FRINTA */
6308         rmode = opcode & 7;
6309         gen_fpst = gen_helper_rintd;
6310         break;
6311     case 0xe: /* FRINTX */
6312         gen_fpst = gen_helper_rintd_exact;
6313         break;
6314     case 0xf: /* FRINTI */
6315         gen_fpst = gen_helper_rintd;
6316         break;
6317     case 0x10: /* FRINT32Z */
6318         rmode = FPROUNDING_ZERO;
6319         gen_fpst = gen_helper_frint32_d;
6320         break;
6321     case 0x11: /* FRINT32X */
6322         gen_fpst = gen_helper_frint32_d;
6323         break;
6324     case 0x12: /* FRINT64Z */
6325         rmode = FPROUNDING_ZERO;
6326         gen_fpst = gen_helper_frint64_d;
6327         break;
6328     case 0x13: /* FRINT64X */
6329         gen_fpst = gen_helper_frint64_d;
6330         break;
6331     default:
6332         g_assert_not_reached();
6333     }
6334 
6335     fpst = fpstatus_ptr(FPST_FPCR);
6336     if (rmode >= 0) {
6337         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6338         gen_fpst(tcg_res, tcg_op, fpst);
6339         gen_restore_rmode(tcg_rmode, fpst);
6340     } else {
6341         gen_fpst(tcg_res, tcg_op, fpst);
6342     }
6343 
6344  done:
6345     write_fp_dreg(s, rd, tcg_res);
6346 }
6347 
6348 static void handle_fp_fcvt(DisasContext *s, int opcode,
6349                            int rd, int rn, int dtype, int ntype)
6350 {
6351     switch (ntype) {
6352     case 0x0:
6353     {
6354         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6355         if (dtype == 1) {
6356             /* Single to double */
6357             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6358             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6359             write_fp_dreg(s, rd, tcg_rd);
6360         } else {
6361             /* Single to half */
6362             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6363             TCGv_i32 ahp = get_ahp_flag();
6364             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6365 
6366             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6367             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6368             write_fp_sreg(s, rd, tcg_rd);
6369         }
6370         break;
6371     }
6372     case 0x1:
6373     {
6374         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6375         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6376         if (dtype == 0) {
6377             /* Double to single */
6378             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6379         } else {
6380             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6381             TCGv_i32 ahp = get_ahp_flag();
6382             /* Double to half */
6383             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6384             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6385         }
6386         write_fp_sreg(s, rd, tcg_rd);
6387         break;
6388     }
6389     case 0x3:
6390     {
6391         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6392         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6393         TCGv_i32 tcg_ahp = get_ahp_flag();
6394         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6395         if (dtype == 0) {
6396             /* Half to single */
6397             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6398             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6399             write_fp_sreg(s, rd, tcg_rd);
6400         } else {
6401             /* Half to double */
6402             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6403             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6404             write_fp_dreg(s, rd, tcg_rd);
6405         }
6406         break;
6407     }
6408     default:
6409         g_assert_not_reached();
6410     }
6411 }
6412 
6413 /* Floating point data-processing (1 source)
6414  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6415  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6416  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6417  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6418  */
6419 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6420 {
6421     int mos = extract32(insn, 29, 3);
6422     int type = extract32(insn, 22, 2);
6423     int opcode = extract32(insn, 15, 6);
6424     int rn = extract32(insn, 5, 5);
6425     int rd = extract32(insn, 0, 5);
6426 
6427     if (mos) {
6428         goto do_unallocated;
6429     }
6430 
6431     switch (opcode) {
6432     case 0x4: case 0x5: case 0x7:
6433     {
6434         /* FCVT between half, single and double precision */
6435         int dtype = extract32(opcode, 0, 2);
6436         if (type == 2 || dtype == type) {
6437             goto do_unallocated;
6438         }
6439         if (!fp_access_check(s)) {
6440             return;
6441         }
6442 
6443         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6444         break;
6445     }
6446 
6447     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6448         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6449             goto do_unallocated;
6450         }
6451         /* fall through */
6452     case 0x0 ... 0x3:
6453     case 0x8 ... 0xc:
6454     case 0xe ... 0xf:
6455         /* 32-to-32 and 64-to-64 ops */
6456         switch (type) {
6457         case 0:
6458             if (!fp_access_check(s)) {
6459                 return;
6460             }
6461             handle_fp_1src_single(s, opcode, rd, rn);
6462             break;
6463         case 1:
6464             if (!fp_access_check(s)) {
6465                 return;
6466             }
6467             handle_fp_1src_double(s, opcode, rd, rn);
6468             break;
6469         case 3:
6470             if (!dc_isar_feature(aa64_fp16, s)) {
6471                 goto do_unallocated;
6472             }
6473 
6474             if (!fp_access_check(s)) {
6475                 return;
6476             }
6477             handle_fp_1src_half(s, opcode, rd, rn);
6478             break;
6479         default:
6480             goto do_unallocated;
6481         }
6482         break;
6483 
6484     case 0x6:
6485         switch (type) {
6486         case 1: /* BFCVT */
6487             if (!dc_isar_feature(aa64_bf16, s)) {
6488                 goto do_unallocated;
6489             }
6490             if (!fp_access_check(s)) {
6491                 return;
6492             }
6493             handle_fp_1src_single(s, opcode, rd, rn);
6494             break;
6495         default:
6496             goto do_unallocated;
6497         }
6498         break;
6499 
6500     default:
6501     do_unallocated:
6502         unallocated_encoding(s);
6503         break;
6504     }
6505 }
6506 
6507 /* Floating-point data-processing (2 source) - single precision */
6508 static void handle_fp_2src_single(DisasContext *s, int opcode,
6509                                   int rd, int rn, int rm)
6510 {
6511     TCGv_i32 tcg_op1;
6512     TCGv_i32 tcg_op2;
6513     TCGv_i32 tcg_res;
6514     TCGv_ptr fpst;
6515 
6516     tcg_res = tcg_temp_new_i32();
6517     fpst = fpstatus_ptr(FPST_FPCR);
6518     tcg_op1 = read_fp_sreg(s, rn);
6519     tcg_op2 = read_fp_sreg(s, rm);
6520 
6521     switch (opcode) {
6522     case 0x0: /* FMUL */
6523         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6524         break;
6525     case 0x1: /* FDIV */
6526         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6527         break;
6528     case 0x2: /* FADD */
6529         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6530         break;
6531     case 0x3: /* FSUB */
6532         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6533         break;
6534     case 0x4: /* FMAX */
6535         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6536         break;
6537     case 0x5: /* FMIN */
6538         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6539         break;
6540     case 0x6: /* FMAXNM */
6541         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6542         break;
6543     case 0x7: /* FMINNM */
6544         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6545         break;
6546     case 0x8: /* FNMUL */
6547         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6548         gen_helper_vfp_negs(tcg_res, tcg_res);
6549         break;
6550     }
6551 
6552     write_fp_sreg(s, rd, tcg_res);
6553 }
6554 
6555 /* Floating-point data-processing (2 source) - double precision */
6556 static void handle_fp_2src_double(DisasContext *s, int opcode,
6557                                   int rd, int rn, int rm)
6558 {
6559     TCGv_i64 tcg_op1;
6560     TCGv_i64 tcg_op2;
6561     TCGv_i64 tcg_res;
6562     TCGv_ptr fpst;
6563 
6564     tcg_res = tcg_temp_new_i64();
6565     fpst = fpstatus_ptr(FPST_FPCR);
6566     tcg_op1 = read_fp_dreg(s, rn);
6567     tcg_op2 = read_fp_dreg(s, rm);
6568 
6569     switch (opcode) {
6570     case 0x0: /* FMUL */
6571         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6572         break;
6573     case 0x1: /* FDIV */
6574         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6575         break;
6576     case 0x2: /* FADD */
6577         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6578         break;
6579     case 0x3: /* FSUB */
6580         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6581         break;
6582     case 0x4: /* FMAX */
6583         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6584         break;
6585     case 0x5: /* FMIN */
6586         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6587         break;
6588     case 0x6: /* FMAXNM */
6589         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6590         break;
6591     case 0x7: /* FMINNM */
6592         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6593         break;
6594     case 0x8: /* FNMUL */
6595         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6596         gen_helper_vfp_negd(tcg_res, tcg_res);
6597         break;
6598     }
6599 
6600     write_fp_dreg(s, rd, tcg_res);
6601 }
6602 
6603 /* Floating-point data-processing (2 source) - half precision */
6604 static void handle_fp_2src_half(DisasContext *s, int opcode,
6605                                 int rd, int rn, int rm)
6606 {
6607     TCGv_i32 tcg_op1;
6608     TCGv_i32 tcg_op2;
6609     TCGv_i32 tcg_res;
6610     TCGv_ptr fpst;
6611 
6612     tcg_res = tcg_temp_new_i32();
6613     fpst = fpstatus_ptr(FPST_FPCR_F16);
6614     tcg_op1 = read_fp_hreg(s, rn);
6615     tcg_op2 = read_fp_hreg(s, rm);
6616 
6617     switch (opcode) {
6618     case 0x0: /* FMUL */
6619         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6620         break;
6621     case 0x1: /* FDIV */
6622         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6623         break;
6624     case 0x2: /* FADD */
6625         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6626         break;
6627     case 0x3: /* FSUB */
6628         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6629         break;
6630     case 0x4: /* FMAX */
6631         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6632         break;
6633     case 0x5: /* FMIN */
6634         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6635         break;
6636     case 0x6: /* FMAXNM */
6637         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6638         break;
6639     case 0x7: /* FMINNM */
6640         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6641         break;
6642     case 0x8: /* FNMUL */
6643         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6644         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6645         break;
6646     default:
6647         g_assert_not_reached();
6648     }
6649 
6650     write_fp_sreg(s, rd, tcg_res);
6651 }
6652 
6653 /* Floating point data-processing (2 source)
6654  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6655  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6656  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6657  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6658  */
6659 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6660 {
6661     int mos = extract32(insn, 29, 3);
6662     int type = extract32(insn, 22, 2);
6663     int rd = extract32(insn, 0, 5);
6664     int rn = extract32(insn, 5, 5);
6665     int rm = extract32(insn, 16, 5);
6666     int opcode = extract32(insn, 12, 4);
6667 
6668     if (opcode > 8 || mos) {
6669         unallocated_encoding(s);
6670         return;
6671     }
6672 
6673     switch (type) {
6674     case 0:
6675         if (!fp_access_check(s)) {
6676             return;
6677         }
6678         handle_fp_2src_single(s, opcode, rd, rn, rm);
6679         break;
6680     case 1:
6681         if (!fp_access_check(s)) {
6682             return;
6683         }
6684         handle_fp_2src_double(s, opcode, rd, rn, rm);
6685         break;
6686     case 3:
6687         if (!dc_isar_feature(aa64_fp16, s)) {
6688             unallocated_encoding(s);
6689             return;
6690         }
6691         if (!fp_access_check(s)) {
6692             return;
6693         }
6694         handle_fp_2src_half(s, opcode, rd, rn, rm);
6695         break;
6696     default:
6697         unallocated_encoding(s);
6698     }
6699 }
6700 
6701 /* Floating-point data-processing (3 source) - single precision */
6702 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6703                                   int rd, int rn, int rm, int ra)
6704 {
6705     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6706     TCGv_i32 tcg_res = tcg_temp_new_i32();
6707     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6708 
6709     tcg_op1 = read_fp_sreg(s, rn);
6710     tcg_op2 = read_fp_sreg(s, rm);
6711     tcg_op3 = read_fp_sreg(s, ra);
6712 
6713     /* These are fused multiply-add, and must be done as one
6714      * floating point operation with no rounding between the
6715      * multiplication and addition steps.
6716      * NB that doing the negations here as separate steps is
6717      * correct : an input NaN should come out with its sign bit
6718      * flipped if it is a negated-input.
6719      */
6720     if (o1 == true) {
6721         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6722     }
6723 
6724     if (o0 != o1) {
6725         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6726     }
6727 
6728     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6729 
6730     write_fp_sreg(s, rd, tcg_res);
6731 }
6732 
6733 /* Floating-point data-processing (3 source) - double precision */
6734 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6735                                   int rd, int rn, int rm, int ra)
6736 {
6737     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6738     TCGv_i64 tcg_res = tcg_temp_new_i64();
6739     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6740 
6741     tcg_op1 = read_fp_dreg(s, rn);
6742     tcg_op2 = read_fp_dreg(s, rm);
6743     tcg_op3 = read_fp_dreg(s, ra);
6744 
6745     /* These are fused multiply-add, and must be done as one
6746      * floating point operation with no rounding between the
6747      * multiplication and addition steps.
6748      * NB that doing the negations here as separate steps is
6749      * correct : an input NaN should come out with its sign bit
6750      * flipped if it is a negated-input.
6751      */
6752     if (o1 == true) {
6753         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6754     }
6755 
6756     if (o0 != o1) {
6757         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6758     }
6759 
6760     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6761 
6762     write_fp_dreg(s, rd, tcg_res);
6763 }
6764 
6765 /* Floating-point data-processing (3 source) - half precision */
6766 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6767                                 int rd, int rn, int rm, int ra)
6768 {
6769     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6770     TCGv_i32 tcg_res = tcg_temp_new_i32();
6771     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6772 
6773     tcg_op1 = read_fp_hreg(s, rn);
6774     tcg_op2 = read_fp_hreg(s, rm);
6775     tcg_op3 = read_fp_hreg(s, ra);
6776 
6777     /* These are fused multiply-add, and must be done as one
6778      * floating point operation with no rounding between the
6779      * multiplication and addition steps.
6780      * NB that doing the negations here as separate steps is
6781      * correct : an input NaN should come out with its sign bit
6782      * flipped if it is a negated-input.
6783      */
6784     if (o1 == true) {
6785         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6786     }
6787 
6788     if (o0 != o1) {
6789         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6790     }
6791 
6792     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6793 
6794     write_fp_sreg(s, rd, tcg_res);
6795 }
6796 
6797 /* Floating point data-processing (3 source)
6798  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6799  * +---+---+---+-----------+------+----+------+----+------+------+------+
6800  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6801  * +---+---+---+-----------+------+----+------+----+------+------+------+
6802  */
6803 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6804 {
6805     int mos = extract32(insn, 29, 3);
6806     int type = extract32(insn, 22, 2);
6807     int rd = extract32(insn, 0, 5);
6808     int rn = extract32(insn, 5, 5);
6809     int ra = extract32(insn, 10, 5);
6810     int rm = extract32(insn, 16, 5);
6811     bool o0 = extract32(insn, 15, 1);
6812     bool o1 = extract32(insn, 21, 1);
6813 
6814     if (mos) {
6815         unallocated_encoding(s);
6816         return;
6817     }
6818 
6819     switch (type) {
6820     case 0:
6821         if (!fp_access_check(s)) {
6822             return;
6823         }
6824         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6825         break;
6826     case 1:
6827         if (!fp_access_check(s)) {
6828             return;
6829         }
6830         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6831         break;
6832     case 3:
6833         if (!dc_isar_feature(aa64_fp16, s)) {
6834             unallocated_encoding(s);
6835             return;
6836         }
6837         if (!fp_access_check(s)) {
6838             return;
6839         }
6840         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6841         break;
6842     default:
6843         unallocated_encoding(s);
6844     }
6845 }
6846 
6847 /* Floating point immediate
6848  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6849  * +---+---+---+-----------+------+---+------------+-------+------+------+
6850  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6851  * +---+---+---+-----------+------+---+------------+-------+------+------+
6852  */
6853 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6854 {
6855     int rd = extract32(insn, 0, 5);
6856     int imm5 = extract32(insn, 5, 5);
6857     int imm8 = extract32(insn, 13, 8);
6858     int type = extract32(insn, 22, 2);
6859     int mos = extract32(insn, 29, 3);
6860     uint64_t imm;
6861     MemOp sz;
6862 
6863     if (mos || imm5) {
6864         unallocated_encoding(s);
6865         return;
6866     }
6867 
6868     switch (type) {
6869     case 0:
6870         sz = MO_32;
6871         break;
6872     case 1:
6873         sz = MO_64;
6874         break;
6875     case 3:
6876         sz = MO_16;
6877         if (dc_isar_feature(aa64_fp16, s)) {
6878             break;
6879         }
6880         /* fallthru */
6881     default:
6882         unallocated_encoding(s);
6883         return;
6884     }
6885 
6886     if (!fp_access_check(s)) {
6887         return;
6888     }
6889 
6890     imm = vfp_expand_imm(sz, imm8);
6891     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6892 }
6893 
6894 /* Handle floating point <=> fixed point conversions. Note that we can
6895  * also deal with fp <=> integer conversions as a special case (scale == 64)
6896  * OPTME: consider handling that special case specially or at least skipping
6897  * the call to scalbn in the helpers for zero shifts.
6898  */
6899 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6900                            bool itof, int rmode, int scale, int sf, int type)
6901 {
6902     bool is_signed = !(opcode & 1);
6903     TCGv_ptr tcg_fpstatus;
6904     TCGv_i32 tcg_shift, tcg_single;
6905     TCGv_i64 tcg_double;
6906 
6907     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6908 
6909     tcg_shift = tcg_constant_i32(64 - scale);
6910 
6911     if (itof) {
6912         TCGv_i64 tcg_int = cpu_reg(s, rn);
6913         if (!sf) {
6914             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6915 
6916             if (is_signed) {
6917                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6918             } else {
6919                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6920             }
6921 
6922             tcg_int = tcg_extend;
6923         }
6924 
6925         switch (type) {
6926         case 1: /* float64 */
6927             tcg_double = tcg_temp_new_i64();
6928             if (is_signed) {
6929                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6930                                      tcg_shift, tcg_fpstatus);
6931             } else {
6932                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6933                                      tcg_shift, tcg_fpstatus);
6934             }
6935             write_fp_dreg(s, rd, tcg_double);
6936             break;
6937 
6938         case 0: /* float32 */
6939             tcg_single = tcg_temp_new_i32();
6940             if (is_signed) {
6941                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6942                                      tcg_shift, tcg_fpstatus);
6943             } else {
6944                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6945                                      tcg_shift, tcg_fpstatus);
6946             }
6947             write_fp_sreg(s, rd, tcg_single);
6948             break;
6949 
6950         case 3: /* float16 */
6951             tcg_single = tcg_temp_new_i32();
6952             if (is_signed) {
6953                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6954                                      tcg_shift, tcg_fpstatus);
6955             } else {
6956                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6957                                      tcg_shift, tcg_fpstatus);
6958             }
6959             write_fp_sreg(s, rd, tcg_single);
6960             break;
6961 
6962         default:
6963             g_assert_not_reached();
6964         }
6965     } else {
6966         TCGv_i64 tcg_int = cpu_reg(s, rd);
6967         TCGv_i32 tcg_rmode;
6968 
6969         if (extract32(opcode, 2, 1)) {
6970             /* There are too many rounding modes to all fit into rmode,
6971              * so FCVTA[US] is a special case.
6972              */
6973             rmode = FPROUNDING_TIEAWAY;
6974         }
6975 
6976         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6977 
6978         switch (type) {
6979         case 1: /* float64 */
6980             tcg_double = read_fp_dreg(s, rn);
6981             if (is_signed) {
6982                 if (!sf) {
6983                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6984                                          tcg_shift, tcg_fpstatus);
6985                 } else {
6986                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6987                                          tcg_shift, tcg_fpstatus);
6988                 }
6989             } else {
6990                 if (!sf) {
6991                     gen_helper_vfp_tould(tcg_int, tcg_double,
6992                                          tcg_shift, tcg_fpstatus);
6993                 } else {
6994                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6995                                          tcg_shift, tcg_fpstatus);
6996                 }
6997             }
6998             if (!sf) {
6999                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7000             }
7001             break;
7002 
7003         case 0: /* float32 */
7004             tcg_single = read_fp_sreg(s, rn);
7005             if (sf) {
7006                 if (is_signed) {
7007                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
7008                                          tcg_shift, tcg_fpstatus);
7009                 } else {
7010                     gen_helper_vfp_touqs(tcg_int, tcg_single,
7011                                          tcg_shift, tcg_fpstatus);
7012                 }
7013             } else {
7014                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7015                 if (is_signed) {
7016                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7017                                          tcg_shift, tcg_fpstatus);
7018                 } else {
7019                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7020                                          tcg_shift, tcg_fpstatus);
7021                 }
7022                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7023             }
7024             break;
7025 
7026         case 3: /* float16 */
7027             tcg_single = read_fp_sreg(s, rn);
7028             if (sf) {
7029                 if (is_signed) {
7030                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7031                                          tcg_shift, tcg_fpstatus);
7032                 } else {
7033                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7034                                          tcg_shift, tcg_fpstatus);
7035                 }
7036             } else {
7037                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7038                 if (is_signed) {
7039                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7040                                          tcg_shift, tcg_fpstatus);
7041                 } else {
7042                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7043                                          tcg_shift, tcg_fpstatus);
7044                 }
7045                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7046             }
7047             break;
7048 
7049         default:
7050             g_assert_not_reached();
7051         }
7052 
7053         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7054     }
7055 }
7056 
7057 /* Floating point <-> fixed point conversions
7058  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7059  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7060  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7061  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7062  */
7063 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7064 {
7065     int rd = extract32(insn, 0, 5);
7066     int rn = extract32(insn, 5, 5);
7067     int scale = extract32(insn, 10, 6);
7068     int opcode = extract32(insn, 16, 3);
7069     int rmode = extract32(insn, 19, 2);
7070     int type = extract32(insn, 22, 2);
7071     bool sbit = extract32(insn, 29, 1);
7072     bool sf = extract32(insn, 31, 1);
7073     bool itof;
7074 
7075     if (sbit || (!sf && scale < 32)) {
7076         unallocated_encoding(s);
7077         return;
7078     }
7079 
7080     switch (type) {
7081     case 0: /* float32 */
7082     case 1: /* float64 */
7083         break;
7084     case 3: /* float16 */
7085         if (dc_isar_feature(aa64_fp16, s)) {
7086             break;
7087         }
7088         /* fallthru */
7089     default:
7090         unallocated_encoding(s);
7091         return;
7092     }
7093 
7094     switch ((rmode << 3) | opcode) {
7095     case 0x2: /* SCVTF */
7096     case 0x3: /* UCVTF */
7097         itof = true;
7098         break;
7099     case 0x18: /* FCVTZS */
7100     case 0x19: /* FCVTZU */
7101         itof = false;
7102         break;
7103     default:
7104         unallocated_encoding(s);
7105         return;
7106     }
7107 
7108     if (!fp_access_check(s)) {
7109         return;
7110     }
7111 
7112     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7113 }
7114 
7115 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7116 {
7117     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7118      * without conversion.
7119      */
7120 
7121     if (itof) {
7122         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7123         TCGv_i64 tmp;
7124 
7125         switch (type) {
7126         case 0:
7127             /* 32 bit */
7128             tmp = tcg_temp_new_i64();
7129             tcg_gen_ext32u_i64(tmp, tcg_rn);
7130             write_fp_dreg(s, rd, tmp);
7131             break;
7132         case 1:
7133             /* 64 bit */
7134             write_fp_dreg(s, rd, tcg_rn);
7135             break;
7136         case 2:
7137             /* 64 bit to top half. */
7138             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7139             clear_vec_high(s, true, rd);
7140             break;
7141         case 3:
7142             /* 16 bit */
7143             tmp = tcg_temp_new_i64();
7144             tcg_gen_ext16u_i64(tmp, tcg_rn);
7145             write_fp_dreg(s, rd, tmp);
7146             break;
7147         default:
7148             g_assert_not_reached();
7149         }
7150     } else {
7151         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7152 
7153         switch (type) {
7154         case 0:
7155             /* 32 bit */
7156             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7157             break;
7158         case 1:
7159             /* 64 bit */
7160             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7161             break;
7162         case 2:
7163             /* 64 bits from top half */
7164             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7165             break;
7166         case 3:
7167             /* 16 bit */
7168             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7169             break;
7170         default:
7171             g_assert_not_reached();
7172         }
7173     }
7174 }
7175 
7176 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7177 {
7178     TCGv_i64 t = read_fp_dreg(s, rn);
7179     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7180 
7181     gen_helper_fjcvtzs(t, t, fpstatus);
7182 
7183     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7184     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7185     tcg_gen_movi_i32(cpu_CF, 0);
7186     tcg_gen_movi_i32(cpu_NF, 0);
7187     tcg_gen_movi_i32(cpu_VF, 0);
7188 }
7189 
7190 /* Floating point <-> integer conversions
7191  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7192  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7193  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7194  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7195  */
7196 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7197 {
7198     int rd = extract32(insn, 0, 5);
7199     int rn = extract32(insn, 5, 5);
7200     int opcode = extract32(insn, 16, 3);
7201     int rmode = extract32(insn, 19, 2);
7202     int type = extract32(insn, 22, 2);
7203     bool sbit = extract32(insn, 29, 1);
7204     bool sf = extract32(insn, 31, 1);
7205     bool itof = false;
7206 
7207     if (sbit) {
7208         goto do_unallocated;
7209     }
7210 
7211     switch (opcode) {
7212     case 2: /* SCVTF */
7213     case 3: /* UCVTF */
7214         itof = true;
7215         /* fallthru */
7216     case 4: /* FCVTAS */
7217     case 5: /* FCVTAU */
7218         if (rmode != 0) {
7219             goto do_unallocated;
7220         }
7221         /* fallthru */
7222     case 0: /* FCVT[NPMZ]S */
7223     case 1: /* FCVT[NPMZ]U */
7224         switch (type) {
7225         case 0: /* float32 */
7226         case 1: /* float64 */
7227             break;
7228         case 3: /* float16 */
7229             if (!dc_isar_feature(aa64_fp16, s)) {
7230                 goto do_unallocated;
7231             }
7232             break;
7233         default:
7234             goto do_unallocated;
7235         }
7236         if (!fp_access_check(s)) {
7237             return;
7238         }
7239         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7240         break;
7241 
7242     default:
7243         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7244         case 0b01100110: /* FMOV half <-> 32-bit int */
7245         case 0b01100111:
7246         case 0b11100110: /* FMOV half <-> 64-bit int */
7247         case 0b11100111:
7248             if (!dc_isar_feature(aa64_fp16, s)) {
7249                 goto do_unallocated;
7250             }
7251             /* fallthru */
7252         case 0b00000110: /* FMOV 32-bit */
7253         case 0b00000111:
7254         case 0b10100110: /* FMOV 64-bit */
7255         case 0b10100111:
7256         case 0b11001110: /* FMOV top half of 128-bit */
7257         case 0b11001111:
7258             if (!fp_access_check(s)) {
7259                 return;
7260             }
7261             itof = opcode & 1;
7262             handle_fmov(s, rd, rn, type, itof);
7263             break;
7264 
7265         case 0b00111110: /* FJCVTZS */
7266             if (!dc_isar_feature(aa64_jscvt, s)) {
7267                 goto do_unallocated;
7268             } else if (fp_access_check(s)) {
7269                 handle_fjcvtzs(s, rd, rn);
7270             }
7271             break;
7272 
7273         default:
7274         do_unallocated:
7275             unallocated_encoding(s);
7276             return;
7277         }
7278         break;
7279     }
7280 }
7281 
7282 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7283  *   31  30  29 28     25 24                          0
7284  * +---+---+---+---------+-----------------------------+
7285  * |   | 0 |   | 1 1 1 1 |                             |
7286  * +---+---+---+---------+-----------------------------+
7287  */
7288 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7289 {
7290     if (extract32(insn, 24, 1)) {
7291         /* Floating point data-processing (3 source) */
7292         disas_fp_3src(s, insn);
7293     } else if (extract32(insn, 21, 1) == 0) {
7294         /* Floating point to fixed point conversions */
7295         disas_fp_fixed_conv(s, insn);
7296     } else {
7297         switch (extract32(insn, 10, 2)) {
7298         case 1:
7299             /* Floating point conditional compare */
7300             disas_fp_ccomp(s, insn);
7301             break;
7302         case 2:
7303             /* Floating point data-processing (2 source) */
7304             disas_fp_2src(s, insn);
7305             break;
7306         case 3:
7307             /* Floating point conditional select */
7308             disas_fp_csel(s, insn);
7309             break;
7310         case 0:
7311             switch (ctz32(extract32(insn, 12, 4))) {
7312             case 0: /* [15:12] == xxx1 */
7313                 /* Floating point immediate */
7314                 disas_fp_imm(s, insn);
7315                 break;
7316             case 1: /* [15:12] == xx10 */
7317                 /* Floating point compare */
7318                 disas_fp_compare(s, insn);
7319                 break;
7320             case 2: /* [15:12] == x100 */
7321                 /* Floating point data-processing (1 source) */
7322                 disas_fp_1src(s, insn);
7323                 break;
7324             case 3: /* [15:12] == 1000 */
7325                 unallocated_encoding(s);
7326                 break;
7327             default: /* [15:12] == 0000 */
7328                 /* Floating point <-> integer conversions */
7329                 disas_fp_int_conv(s, insn);
7330                 break;
7331             }
7332             break;
7333         }
7334     }
7335 }
7336 
7337 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7338                      int pos)
7339 {
7340     /* Extract 64 bits from the middle of two concatenated 64 bit
7341      * vector register slices left:right. The extracted bits start
7342      * at 'pos' bits into the right (least significant) side.
7343      * We return the result in tcg_right, and guarantee not to
7344      * trash tcg_left.
7345      */
7346     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7347     assert(pos > 0 && pos < 64);
7348 
7349     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7350     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7351     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7352 }
7353 
7354 /* EXT
7355  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7356  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7357  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7358  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7359  */
7360 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7361 {
7362     int is_q = extract32(insn, 30, 1);
7363     int op2 = extract32(insn, 22, 2);
7364     int imm4 = extract32(insn, 11, 4);
7365     int rm = extract32(insn, 16, 5);
7366     int rn = extract32(insn, 5, 5);
7367     int rd = extract32(insn, 0, 5);
7368     int pos = imm4 << 3;
7369     TCGv_i64 tcg_resl, tcg_resh;
7370 
7371     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7372         unallocated_encoding(s);
7373         return;
7374     }
7375 
7376     if (!fp_access_check(s)) {
7377         return;
7378     }
7379 
7380     tcg_resh = tcg_temp_new_i64();
7381     tcg_resl = tcg_temp_new_i64();
7382 
7383     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7384      * either extracting 128 bits from a 128:128 concatenation, or
7385      * extracting 64 bits from a 64:64 concatenation.
7386      */
7387     if (!is_q) {
7388         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7389         if (pos != 0) {
7390             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7391             do_ext64(s, tcg_resh, tcg_resl, pos);
7392         }
7393     } else {
7394         TCGv_i64 tcg_hh;
7395         typedef struct {
7396             int reg;
7397             int elt;
7398         } EltPosns;
7399         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7400         EltPosns *elt = eltposns;
7401 
7402         if (pos >= 64) {
7403             elt++;
7404             pos -= 64;
7405         }
7406 
7407         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7408         elt++;
7409         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7410         elt++;
7411         if (pos != 0) {
7412             do_ext64(s, tcg_resh, tcg_resl, pos);
7413             tcg_hh = tcg_temp_new_i64();
7414             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7415             do_ext64(s, tcg_hh, tcg_resh, pos);
7416         }
7417     }
7418 
7419     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7420     if (is_q) {
7421         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7422     }
7423     clear_vec_high(s, is_q, rd);
7424 }
7425 
7426 /* TBL/TBX
7427  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7428  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7429  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7430  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7431  */
7432 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7433 {
7434     int op2 = extract32(insn, 22, 2);
7435     int is_q = extract32(insn, 30, 1);
7436     int rm = extract32(insn, 16, 5);
7437     int rn = extract32(insn, 5, 5);
7438     int rd = extract32(insn, 0, 5);
7439     int is_tbx = extract32(insn, 12, 1);
7440     int len = (extract32(insn, 13, 2) + 1) * 16;
7441 
7442     if (op2 != 0) {
7443         unallocated_encoding(s);
7444         return;
7445     }
7446 
7447     if (!fp_access_check(s)) {
7448         return;
7449     }
7450 
7451     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7452                        vec_full_reg_offset(s, rm), cpu_env,
7453                        is_q ? 16 : 8, vec_full_reg_size(s),
7454                        (len << 6) | (is_tbx << 5) | rn,
7455                        gen_helper_simd_tblx);
7456 }
7457 
7458 /* ZIP/UZP/TRN
7459  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7460  * +---+---+-------------+------+---+------+---+------------------+------+
7461  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7462  * +---+---+-------------+------+---+------+---+------------------+------+
7463  */
7464 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7465 {
7466     int rd = extract32(insn, 0, 5);
7467     int rn = extract32(insn, 5, 5);
7468     int rm = extract32(insn, 16, 5);
7469     int size = extract32(insn, 22, 2);
7470     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7471      * bit 2 indicates 1 vs 2 variant of the insn.
7472      */
7473     int opcode = extract32(insn, 12, 2);
7474     bool part = extract32(insn, 14, 1);
7475     bool is_q = extract32(insn, 30, 1);
7476     int esize = 8 << size;
7477     int i;
7478     int datasize = is_q ? 128 : 64;
7479     int elements = datasize / esize;
7480     TCGv_i64 tcg_res[2], tcg_ele;
7481 
7482     if (opcode == 0 || (size == 3 && !is_q)) {
7483         unallocated_encoding(s);
7484         return;
7485     }
7486 
7487     if (!fp_access_check(s)) {
7488         return;
7489     }
7490 
7491     tcg_res[0] = tcg_temp_new_i64();
7492     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7493     tcg_ele = tcg_temp_new_i64();
7494 
7495     for (i = 0; i < elements; i++) {
7496         int o, w;
7497 
7498         switch (opcode) {
7499         case 1: /* UZP1/2 */
7500         {
7501             int midpoint = elements / 2;
7502             if (i < midpoint) {
7503                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7504             } else {
7505                 read_vec_element(s, tcg_ele, rm,
7506                                  2 * (i - midpoint) + part, size);
7507             }
7508             break;
7509         }
7510         case 2: /* TRN1/2 */
7511             if (i & 1) {
7512                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7513             } else {
7514                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7515             }
7516             break;
7517         case 3: /* ZIP1/2 */
7518         {
7519             int base = part * elements / 2;
7520             if (i & 1) {
7521                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7522             } else {
7523                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7524             }
7525             break;
7526         }
7527         default:
7528             g_assert_not_reached();
7529         }
7530 
7531         w = (i * esize) / 64;
7532         o = (i * esize) % 64;
7533         if (o == 0) {
7534             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7535         } else {
7536             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7537             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7538         }
7539     }
7540 
7541     for (i = 0; i <= is_q; ++i) {
7542         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7543     }
7544     clear_vec_high(s, is_q, rd);
7545 }
7546 
7547 /*
7548  * do_reduction_op helper
7549  *
7550  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7551  * important for correct NaN propagation that we do these
7552  * operations in exactly the order specified by the pseudocode.
7553  *
7554  * This is a recursive function, TCG temps should be freed by the
7555  * calling function once it is done with the values.
7556  */
7557 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7558                                 int esize, int size, int vmap, TCGv_ptr fpst)
7559 {
7560     if (esize == size) {
7561         int element;
7562         MemOp msize = esize == 16 ? MO_16 : MO_32;
7563         TCGv_i32 tcg_elem;
7564 
7565         /* We should have one register left here */
7566         assert(ctpop8(vmap) == 1);
7567         element = ctz32(vmap);
7568         assert(element < 8);
7569 
7570         tcg_elem = tcg_temp_new_i32();
7571         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7572         return tcg_elem;
7573     } else {
7574         int bits = size / 2;
7575         int shift = ctpop8(vmap) / 2;
7576         int vmap_lo = (vmap >> shift) & vmap;
7577         int vmap_hi = (vmap & ~vmap_lo);
7578         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7579 
7580         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7581         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7582         tcg_res = tcg_temp_new_i32();
7583 
7584         switch (fpopcode) {
7585         case 0x0c: /* fmaxnmv half-precision */
7586             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7587             break;
7588         case 0x0f: /* fmaxv half-precision */
7589             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7590             break;
7591         case 0x1c: /* fminnmv half-precision */
7592             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7593             break;
7594         case 0x1f: /* fminv half-precision */
7595             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7596             break;
7597         case 0x2c: /* fmaxnmv */
7598             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7599             break;
7600         case 0x2f: /* fmaxv */
7601             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7602             break;
7603         case 0x3c: /* fminnmv */
7604             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7605             break;
7606         case 0x3f: /* fminv */
7607             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7608             break;
7609         default:
7610             g_assert_not_reached();
7611         }
7612         return tcg_res;
7613     }
7614 }
7615 
7616 /* AdvSIMD across lanes
7617  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7618  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7619  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7620  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7621  */
7622 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7623 {
7624     int rd = extract32(insn, 0, 5);
7625     int rn = extract32(insn, 5, 5);
7626     int size = extract32(insn, 22, 2);
7627     int opcode = extract32(insn, 12, 5);
7628     bool is_q = extract32(insn, 30, 1);
7629     bool is_u = extract32(insn, 29, 1);
7630     bool is_fp = false;
7631     bool is_min = false;
7632     int esize;
7633     int elements;
7634     int i;
7635     TCGv_i64 tcg_res, tcg_elt;
7636 
7637     switch (opcode) {
7638     case 0x1b: /* ADDV */
7639         if (is_u) {
7640             unallocated_encoding(s);
7641             return;
7642         }
7643         /* fall through */
7644     case 0x3: /* SADDLV, UADDLV */
7645     case 0xa: /* SMAXV, UMAXV */
7646     case 0x1a: /* SMINV, UMINV */
7647         if (size == 3 || (size == 2 && !is_q)) {
7648             unallocated_encoding(s);
7649             return;
7650         }
7651         break;
7652     case 0xc: /* FMAXNMV, FMINNMV */
7653     case 0xf: /* FMAXV, FMINV */
7654         /* Bit 1 of size field encodes min vs max and the actual size
7655          * depends on the encoding of the U bit. If not set (and FP16
7656          * enabled) then we do half-precision float instead of single
7657          * precision.
7658          */
7659         is_min = extract32(size, 1, 1);
7660         is_fp = true;
7661         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7662             size = 1;
7663         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7664             unallocated_encoding(s);
7665             return;
7666         } else {
7667             size = 2;
7668         }
7669         break;
7670     default:
7671         unallocated_encoding(s);
7672         return;
7673     }
7674 
7675     if (!fp_access_check(s)) {
7676         return;
7677     }
7678 
7679     esize = 8 << size;
7680     elements = (is_q ? 128 : 64) / esize;
7681 
7682     tcg_res = tcg_temp_new_i64();
7683     tcg_elt = tcg_temp_new_i64();
7684 
7685     /* These instructions operate across all lanes of a vector
7686      * to produce a single result. We can guarantee that a 64
7687      * bit intermediate is sufficient:
7688      *  + for [US]ADDLV the maximum element size is 32 bits, and
7689      *    the result type is 64 bits
7690      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7691      *    same as the element size, which is 32 bits at most
7692      * For the integer operations we can choose to work at 64
7693      * or 32 bits and truncate at the end; for simplicity
7694      * we use 64 bits always. The floating point
7695      * ops do require 32 bit intermediates, though.
7696      */
7697     if (!is_fp) {
7698         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7699 
7700         for (i = 1; i < elements; i++) {
7701             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7702 
7703             switch (opcode) {
7704             case 0x03: /* SADDLV / UADDLV */
7705             case 0x1b: /* ADDV */
7706                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7707                 break;
7708             case 0x0a: /* SMAXV / UMAXV */
7709                 if (is_u) {
7710                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7711                 } else {
7712                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7713                 }
7714                 break;
7715             case 0x1a: /* SMINV / UMINV */
7716                 if (is_u) {
7717                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7718                 } else {
7719                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7720                 }
7721                 break;
7722             default:
7723                 g_assert_not_reached();
7724             }
7725 
7726         }
7727     } else {
7728         /* Floating point vector reduction ops which work across 32
7729          * bit (single) or 16 bit (half-precision) intermediates.
7730          * Note that correct NaN propagation requires that we do these
7731          * operations in exactly the order specified by the pseudocode.
7732          */
7733         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7734         int fpopcode = opcode | is_min << 4 | is_u << 5;
7735         int vmap = (1 << elements) - 1;
7736         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7737                                              (is_q ? 128 : 64), vmap, fpst);
7738         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7739     }
7740 
7741     /* Now truncate the result to the width required for the final output */
7742     if (opcode == 0x03) {
7743         /* SADDLV, UADDLV: result is 2*esize */
7744         size++;
7745     }
7746 
7747     switch (size) {
7748     case 0:
7749         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7750         break;
7751     case 1:
7752         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7753         break;
7754     case 2:
7755         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7756         break;
7757     case 3:
7758         break;
7759     default:
7760         g_assert_not_reached();
7761     }
7762 
7763     write_fp_dreg(s, rd, tcg_res);
7764 }
7765 
7766 /* DUP (Element, Vector)
7767  *
7768  *  31  30   29              21 20    16 15        10  9    5 4    0
7769  * +---+---+-------------------+--------+-------------+------+------+
7770  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7771  * +---+---+-------------------+--------+-------------+------+------+
7772  *
7773  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7774  */
7775 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7776                              int imm5)
7777 {
7778     int size = ctz32(imm5);
7779     int index;
7780 
7781     if (size > 3 || (size == 3 && !is_q)) {
7782         unallocated_encoding(s);
7783         return;
7784     }
7785 
7786     if (!fp_access_check(s)) {
7787         return;
7788     }
7789 
7790     index = imm5 >> (size + 1);
7791     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7792                          vec_reg_offset(s, rn, index, size),
7793                          is_q ? 16 : 8, vec_full_reg_size(s));
7794 }
7795 
7796 /* DUP (element, scalar)
7797  *  31                   21 20    16 15        10  9    5 4    0
7798  * +-----------------------+--------+-------------+------+------+
7799  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7800  * +-----------------------+--------+-------------+------+------+
7801  */
7802 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7803                               int imm5)
7804 {
7805     int size = ctz32(imm5);
7806     int index;
7807     TCGv_i64 tmp;
7808 
7809     if (size > 3) {
7810         unallocated_encoding(s);
7811         return;
7812     }
7813 
7814     if (!fp_access_check(s)) {
7815         return;
7816     }
7817 
7818     index = imm5 >> (size + 1);
7819 
7820     /* This instruction just extracts the specified element and
7821      * zero-extends it into the bottom of the destination register.
7822      */
7823     tmp = tcg_temp_new_i64();
7824     read_vec_element(s, tmp, rn, index, size);
7825     write_fp_dreg(s, rd, tmp);
7826 }
7827 
7828 /* DUP (General)
7829  *
7830  *  31  30   29              21 20    16 15        10  9    5 4    0
7831  * +---+---+-------------------+--------+-------------+------+------+
7832  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7833  * +---+---+-------------------+--------+-------------+------+------+
7834  *
7835  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7836  */
7837 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7838                              int imm5)
7839 {
7840     int size = ctz32(imm5);
7841     uint32_t dofs, oprsz, maxsz;
7842 
7843     if (size > 3 || ((size == 3) && !is_q)) {
7844         unallocated_encoding(s);
7845         return;
7846     }
7847 
7848     if (!fp_access_check(s)) {
7849         return;
7850     }
7851 
7852     dofs = vec_full_reg_offset(s, rd);
7853     oprsz = is_q ? 16 : 8;
7854     maxsz = vec_full_reg_size(s);
7855 
7856     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7857 }
7858 
7859 /* INS (Element)
7860  *
7861  *  31                   21 20    16 15  14    11  10 9    5 4    0
7862  * +-----------------------+--------+------------+---+------+------+
7863  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7864  * +-----------------------+--------+------------+---+------+------+
7865  *
7866  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7867  * index: encoded in imm5<4:size+1>
7868  */
7869 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7870                              int imm4, int imm5)
7871 {
7872     int size = ctz32(imm5);
7873     int src_index, dst_index;
7874     TCGv_i64 tmp;
7875 
7876     if (size > 3) {
7877         unallocated_encoding(s);
7878         return;
7879     }
7880 
7881     if (!fp_access_check(s)) {
7882         return;
7883     }
7884 
7885     dst_index = extract32(imm5, 1+size, 5);
7886     src_index = extract32(imm4, size, 4);
7887 
7888     tmp = tcg_temp_new_i64();
7889 
7890     read_vec_element(s, tmp, rn, src_index, size);
7891     write_vec_element(s, tmp, rd, dst_index, size);
7892 
7893     /* INS is considered a 128-bit write for SVE. */
7894     clear_vec_high(s, true, rd);
7895 }
7896 
7897 
7898 /* INS (General)
7899  *
7900  *  31                   21 20    16 15        10  9    5 4    0
7901  * +-----------------------+--------+-------------+------+------+
7902  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7903  * +-----------------------+--------+-------------+------+------+
7904  *
7905  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7906  * index: encoded in imm5<4:size+1>
7907  */
7908 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7909 {
7910     int size = ctz32(imm5);
7911     int idx;
7912 
7913     if (size > 3) {
7914         unallocated_encoding(s);
7915         return;
7916     }
7917 
7918     if (!fp_access_check(s)) {
7919         return;
7920     }
7921 
7922     idx = extract32(imm5, 1 + size, 4 - size);
7923     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7924 
7925     /* INS is considered a 128-bit write for SVE. */
7926     clear_vec_high(s, true, rd);
7927 }
7928 
7929 /*
7930  * UMOV (General)
7931  * SMOV (General)
7932  *
7933  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7934  * +---+---+-------------------+--------+-------------+------+------+
7935  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7936  * +---+---+-------------------+--------+-------------+------+------+
7937  *
7938  * U: unsigned when set
7939  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7940  */
7941 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7942                                   int rn, int rd, int imm5)
7943 {
7944     int size = ctz32(imm5);
7945     int element;
7946     TCGv_i64 tcg_rd;
7947 
7948     /* Check for UnallocatedEncodings */
7949     if (is_signed) {
7950         if (size > 2 || (size == 2 && !is_q)) {
7951             unallocated_encoding(s);
7952             return;
7953         }
7954     } else {
7955         if (size > 3
7956             || (size < 3 && is_q)
7957             || (size == 3 && !is_q)) {
7958             unallocated_encoding(s);
7959             return;
7960         }
7961     }
7962 
7963     if (!fp_access_check(s)) {
7964         return;
7965     }
7966 
7967     element = extract32(imm5, 1+size, 4);
7968 
7969     tcg_rd = cpu_reg(s, rd);
7970     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7971     if (is_signed && !is_q) {
7972         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7973     }
7974 }
7975 
7976 /* AdvSIMD copy
7977  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7978  * +---+---+----+-----------------+------+---+------+---+------+------+
7979  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7980  * +---+---+----+-----------------+------+---+------+---+------+------+
7981  */
7982 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7983 {
7984     int rd = extract32(insn, 0, 5);
7985     int rn = extract32(insn, 5, 5);
7986     int imm4 = extract32(insn, 11, 4);
7987     int op = extract32(insn, 29, 1);
7988     int is_q = extract32(insn, 30, 1);
7989     int imm5 = extract32(insn, 16, 5);
7990 
7991     if (op) {
7992         if (is_q) {
7993             /* INS (element) */
7994             handle_simd_inse(s, rd, rn, imm4, imm5);
7995         } else {
7996             unallocated_encoding(s);
7997         }
7998     } else {
7999         switch (imm4) {
8000         case 0:
8001             /* DUP (element - vector) */
8002             handle_simd_dupe(s, is_q, rd, rn, imm5);
8003             break;
8004         case 1:
8005             /* DUP (general) */
8006             handle_simd_dupg(s, is_q, rd, rn, imm5);
8007             break;
8008         case 3:
8009             if (is_q) {
8010                 /* INS (general) */
8011                 handle_simd_insg(s, rd, rn, imm5);
8012             } else {
8013                 unallocated_encoding(s);
8014             }
8015             break;
8016         case 5:
8017         case 7:
8018             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8019             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8020             break;
8021         default:
8022             unallocated_encoding(s);
8023             break;
8024         }
8025     }
8026 }
8027 
8028 /* AdvSIMD modified immediate
8029  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8030  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8031  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8032  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8033  *
8034  * There are a number of operations that can be carried out here:
8035  *   MOVI - move (shifted) imm into register
8036  *   MVNI - move inverted (shifted) imm into register
8037  *   ORR  - bitwise OR of (shifted) imm with register
8038  *   BIC  - bitwise clear of (shifted) imm with register
8039  * With ARMv8.2 we also have:
8040  *   FMOV half-precision
8041  */
8042 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8043 {
8044     int rd = extract32(insn, 0, 5);
8045     int cmode = extract32(insn, 12, 4);
8046     int o2 = extract32(insn, 11, 1);
8047     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8048     bool is_neg = extract32(insn, 29, 1);
8049     bool is_q = extract32(insn, 30, 1);
8050     uint64_t imm = 0;
8051 
8052     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8053         /* Check for FMOV (vector, immediate) - half-precision */
8054         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8055             unallocated_encoding(s);
8056             return;
8057         }
8058     }
8059 
8060     if (!fp_access_check(s)) {
8061         return;
8062     }
8063 
8064     if (cmode == 15 && o2 && !is_neg) {
8065         /* FMOV (vector, immediate) - half-precision */
8066         imm = vfp_expand_imm(MO_16, abcdefgh);
8067         /* now duplicate across the lanes */
8068         imm = dup_const(MO_16, imm);
8069     } else {
8070         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8071     }
8072 
8073     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8074         /* MOVI or MVNI, with MVNI negation handled above.  */
8075         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8076                              vec_full_reg_size(s), imm);
8077     } else {
8078         /* ORR or BIC, with BIC negation to AND handled above.  */
8079         if (is_neg) {
8080             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8081         } else {
8082             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8083         }
8084     }
8085 }
8086 
8087 /* AdvSIMD scalar copy
8088  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8089  * +-----+----+-----------------+------+---+------+---+------+------+
8090  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8091  * +-----+----+-----------------+------+---+------+---+------+------+
8092  */
8093 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8094 {
8095     int rd = extract32(insn, 0, 5);
8096     int rn = extract32(insn, 5, 5);
8097     int imm4 = extract32(insn, 11, 4);
8098     int imm5 = extract32(insn, 16, 5);
8099     int op = extract32(insn, 29, 1);
8100 
8101     if (op != 0 || imm4 != 0) {
8102         unallocated_encoding(s);
8103         return;
8104     }
8105 
8106     /* DUP (element, scalar) */
8107     handle_simd_dupes(s, rd, rn, imm5);
8108 }
8109 
8110 /* AdvSIMD scalar pairwise
8111  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8112  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8113  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8114  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8115  */
8116 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8117 {
8118     int u = extract32(insn, 29, 1);
8119     int size = extract32(insn, 22, 2);
8120     int opcode = extract32(insn, 12, 5);
8121     int rn = extract32(insn, 5, 5);
8122     int rd = extract32(insn, 0, 5);
8123     TCGv_ptr fpst;
8124 
8125     /* For some ops (the FP ones), size[1] is part of the encoding.
8126      * For ADDP strictly it is not but size[1] is always 1 for valid
8127      * encodings.
8128      */
8129     opcode |= (extract32(size, 1, 1) << 5);
8130 
8131     switch (opcode) {
8132     case 0x3b: /* ADDP */
8133         if (u || size != 3) {
8134             unallocated_encoding(s);
8135             return;
8136         }
8137         if (!fp_access_check(s)) {
8138             return;
8139         }
8140 
8141         fpst = NULL;
8142         break;
8143     case 0xc: /* FMAXNMP */
8144     case 0xd: /* FADDP */
8145     case 0xf: /* FMAXP */
8146     case 0x2c: /* FMINNMP */
8147     case 0x2f: /* FMINP */
8148         /* FP op, size[0] is 32 or 64 bit*/
8149         if (!u) {
8150             if (!dc_isar_feature(aa64_fp16, s)) {
8151                 unallocated_encoding(s);
8152                 return;
8153             } else {
8154                 size = MO_16;
8155             }
8156         } else {
8157             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8158         }
8159 
8160         if (!fp_access_check(s)) {
8161             return;
8162         }
8163 
8164         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8165         break;
8166     default:
8167         unallocated_encoding(s);
8168         return;
8169     }
8170 
8171     if (size == MO_64) {
8172         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8173         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8174         TCGv_i64 tcg_res = tcg_temp_new_i64();
8175 
8176         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8177         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8178 
8179         switch (opcode) {
8180         case 0x3b: /* ADDP */
8181             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8182             break;
8183         case 0xc: /* FMAXNMP */
8184             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8185             break;
8186         case 0xd: /* FADDP */
8187             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8188             break;
8189         case 0xf: /* FMAXP */
8190             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8191             break;
8192         case 0x2c: /* FMINNMP */
8193             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8194             break;
8195         case 0x2f: /* FMINP */
8196             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8197             break;
8198         default:
8199             g_assert_not_reached();
8200         }
8201 
8202         write_fp_dreg(s, rd, tcg_res);
8203     } else {
8204         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8205         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8206         TCGv_i32 tcg_res = tcg_temp_new_i32();
8207 
8208         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8209         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8210 
8211         if (size == MO_16) {
8212             switch (opcode) {
8213             case 0xc: /* FMAXNMP */
8214                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8215                 break;
8216             case 0xd: /* FADDP */
8217                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8218                 break;
8219             case 0xf: /* FMAXP */
8220                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8221                 break;
8222             case 0x2c: /* FMINNMP */
8223                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8224                 break;
8225             case 0x2f: /* FMINP */
8226                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8227                 break;
8228             default:
8229                 g_assert_not_reached();
8230             }
8231         } else {
8232             switch (opcode) {
8233             case 0xc: /* FMAXNMP */
8234                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8235                 break;
8236             case 0xd: /* FADDP */
8237                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8238                 break;
8239             case 0xf: /* FMAXP */
8240                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8241                 break;
8242             case 0x2c: /* FMINNMP */
8243                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8244                 break;
8245             case 0x2f: /* FMINP */
8246                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8247                 break;
8248             default:
8249                 g_assert_not_reached();
8250             }
8251         }
8252 
8253         write_fp_sreg(s, rd, tcg_res);
8254     }
8255 }
8256 
8257 /*
8258  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8259  *
8260  * This code is handles the common shifting code and is used by both
8261  * the vector and scalar code.
8262  */
8263 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8264                                     TCGv_i64 tcg_rnd, bool accumulate,
8265                                     bool is_u, int size, int shift)
8266 {
8267     bool extended_result = false;
8268     bool round = tcg_rnd != NULL;
8269     int ext_lshift = 0;
8270     TCGv_i64 tcg_src_hi;
8271 
8272     if (round && size == 3) {
8273         extended_result = true;
8274         ext_lshift = 64 - shift;
8275         tcg_src_hi = tcg_temp_new_i64();
8276     } else if (shift == 64) {
8277         if (!accumulate && is_u) {
8278             /* result is zero */
8279             tcg_gen_movi_i64(tcg_res, 0);
8280             return;
8281         }
8282     }
8283 
8284     /* Deal with the rounding step */
8285     if (round) {
8286         if (extended_result) {
8287             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8288             if (!is_u) {
8289                 /* take care of sign extending tcg_res */
8290                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8291                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8292                                  tcg_src, tcg_src_hi,
8293                                  tcg_rnd, tcg_zero);
8294             } else {
8295                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8296                                  tcg_src, tcg_zero,
8297                                  tcg_rnd, tcg_zero);
8298             }
8299         } else {
8300             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8301         }
8302     }
8303 
8304     /* Now do the shift right */
8305     if (round && extended_result) {
8306         /* extended case, >64 bit precision required */
8307         if (ext_lshift == 0) {
8308             /* special case, only high bits matter */
8309             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8310         } else {
8311             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8312             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8313             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8314         }
8315     } else {
8316         if (is_u) {
8317             if (shift == 64) {
8318                 /* essentially shifting in 64 zeros */
8319                 tcg_gen_movi_i64(tcg_src, 0);
8320             } else {
8321                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8322             }
8323         } else {
8324             if (shift == 64) {
8325                 /* effectively extending the sign-bit */
8326                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8327             } else {
8328                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8329             }
8330         }
8331     }
8332 
8333     if (accumulate) {
8334         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8335     } else {
8336         tcg_gen_mov_i64(tcg_res, tcg_src);
8337     }
8338 }
8339 
8340 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8341 static void handle_scalar_simd_shri(DisasContext *s,
8342                                     bool is_u, int immh, int immb,
8343                                     int opcode, int rn, int rd)
8344 {
8345     const int size = 3;
8346     int immhb = immh << 3 | immb;
8347     int shift = 2 * (8 << size) - immhb;
8348     bool accumulate = false;
8349     bool round = false;
8350     bool insert = false;
8351     TCGv_i64 tcg_rn;
8352     TCGv_i64 tcg_rd;
8353     TCGv_i64 tcg_round;
8354 
8355     if (!extract32(immh, 3, 1)) {
8356         unallocated_encoding(s);
8357         return;
8358     }
8359 
8360     if (!fp_access_check(s)) {
8361         return;
8362     }
8363 
8364     switch (opcode) {
8365     case 0x02: /* SSRA / USRA (accumulate) */
8366         accumulate = true;
8367         break;
8368     case 0x04: /* SRSHR / URSHR (rounding) */
8369         round = true;
8370         break;
8371     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8372         accumulate = round = true;
8373         break;
8374     case 0x08: /* SRI */
8375         insert = true;
8376         break;
8377     }
8378 
8379     if (round) {
8380         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8381     } else {
8382         tcg_round = NULL;
8383     }
8384 
8385     tcg_rn = read_fp_dreg(s, rn);
8386     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8387 
8388     if (insert) {
8389         /* shift count same as element size is valid but does nothing;
8390          * special case to avoid potential shift by 64.
8391          */
8392         int esize = 8 << size;
8393         if (shift != esize) {
8394             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8395             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8396         }
8397     } else {
8398         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8399                                 accumulate, is_u, size, shift);
8400     }
8401 
8402     write_fp_dreg(s, rd, tcg_rd);
8403 }
8404 
8405 /* SHL/SLI - Scalar shift left */
8406 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8407                                     int immh, int immb, int opcode,
8408                                     int rn, int rd)
8409 {
8410     int size = 32 - clz32(immh) - 1;
8411     int immhb = immh << 3 | immb;
8412     int shift = immhb - (8 << size);
8413     TCGv_i64 tcg_rn;
8414     TCGv_i64 tcg_rd;
8415 
8416     if (!extract32(immh, 3, 1)) {
8417         unallocated_encoding(s);
8418         return;
8419     }
8420 
8421     if (!fp_access_check(s)) {
8422         return;
8423     }
8424 
8425     tcg_rn = read_fp_dreg(s, rn);
8426     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8427 
8428     if (insert) {
8429         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8430     } else {
8431         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8432     }
8433 
8434     write_fp_dreg(s, rd, tcg_rd);
8435 }
8436 
8437 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8438  * (signed/unsigned) narrowing */
8439 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8440                                    bool is_u_shift, bool is_u_narrow,
8441                                    int immh, int immb, int opcode,
8442                                    int rn, int rd)
8443 {
8444     int immhb = immh << 3 | immb;
8445     int size = 32 - clz32(immh) - 1;
8446     int esize = 8 << size;
8447     int shift = (2 * esize) - immhb;
8448     int elements = is_scalar ? 1 : (64 / esize);
8449     bool round = extract32(opcode, 0, 1);
8450     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8451     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8452     TCGv_i32 tcg_rd_narrowed;
8453     TCGv_i64 tcg_final;
8454 
8455     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8456         { gen_helper_neon_narrow_sat_s8,
8457           gen_helper_neon_unarrow_sat8 },
8458         { gen_helper_neon_narrow_sat_s16,
8459           gen_helper_neon_unarrow_sat16 },
8460         { gen_helper_neon_narrow_sat_s32,
8461           gen_helper_neon_unarrow_sat32 },
8462         { NULL, NULL },
8463     };
8464     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8465         gen_helper_neon_narrow_sat_u8,
8466         gen_helper_neon_narrow_sat_u16,
8467         gen_helper_neon_narrow_sat_u32,
8468         NULL
8469     };
8470     NeonGenNarrowEnvFn *narrowfn;
8471 
8472     int i;
8473 
8474     assert(size < 4);
8475 
8476     if (extract32(immh, 3, 1)) {
8477         unallocated_encoding(s);
8478         return;
8479     }
8480 
8481     if (!fp_access_check(s)) {
8482         return;
8483     }
8484 
8485     if (is_u_shift) {
8486         narrowfn = unsigned_narrow_fns[size];
8487     } else {
8488         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8489     }
8490 
8491     tcg_rn = tcg_temp_new_i64();
8492     tcg_rd = tcg_temp_new_i64();
8493     tcg_rd_narrowed = tcg_temp_new_i32();
8494     tcg_final = tcg_temp_new_i64();
8495 
8496     if (round) {
8497         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8498     } else {
8499         tcg_round = NULL;
8500     }
8501 
8502     for (i = 0; i < elements; i++) {
8503         read_vec_element(s, tcg_rn, rn, i, ldop);
8504         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8505                                 false, is_u_shift, size+1, shift);
8506         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8507         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8508         if (i == 0) {
8509             tcg_gen_mov_i64(tcg_final, tcg_rd);
8510         } else {
8511             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8512         }
8513     }
8514 
8515     if (!is_q) {
8516         write_vec_element(s, tcg_final, rd, 0, MO_64);
8517     } else {
8518         write_vec_element(s, tcg_final, rd, 1, MO_64);
8519     }
8520     clear_vec_high(s, is_q, rd);
8521 }
8522 
8523 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8524 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8525                              bool src_unsigned, bool dst_unsigned,
8526                              int immh, int immb, int rn, int rd)
8527 {
8528     int immhb = immh << 3 | immb;
8529     int size = 32 - clz32(immh) - 1;
8530     int shift = immhb - (8 << size);
8531     int pass;
8532 
8533     assert(immh != 0);
8534     assert(!(scalar && is_q));
8535 
8536     if (!scalar) {
8537         if (!is_q && extract32(immh, 3, 1)) {
8538             unallocated_encoding(s);
8539             return;
8540         }
8541 
8542         /* Since we use the variable-shift helpers we must
8543          * replicate the shift count into each element of
8544          * the tcg_shift value.
8545          */
8546         switch (size) {
8547         case 0:
8548             shift |= shift << 8;
8549             /* fall through */
8550         case 1:
8551             shift |= shift << 16;
8552             break;
8553         case 2:
8554         case 3:
8555             break;
8556         default:
8557             g_assert_not_reached();
8558         }
8559     }
8560 
8561     if (!fp_access_check(s)) {
8562         return;
8563     }
8564 
8565     if (size == 3) {
8566         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8567         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8568             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8569             { NULL, gen_helper_neon_qshl_u64 },
8570         };
8571         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8572         int maxpass = is_q ? 2 : 1;
8573 
8574         for (pass = 0; pass < maxpass; pass++) {
8575             TCGv_i64 tcg_op = tcg_temp_new_i64();
8576 
8577             read_vec_element(s, tcg_op, rn, pass, MO_64);
8578             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8579             write_vec_element(s, tcg_op, rd, pass, MO_64);
8580         }
8581         clear_vec_high(s, is_q, rd);
8582     } else {
8583         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8584         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8585             {
8586                 { gen_helper_neon_qshl_s8,
8587                   gen_helper_neon_qshl_s16,
8588                   gen_helper_neon_qshl_s32 },
8589                 { gen_helper_neon_qshlu_s8,
8590                   gen_helper_neon_qshlu_s16,
8591                   gen_helper_neon_qshlu_s32 }
8592             }, {
8593                 { NULL, NULL, NULL },
8594                 { gen_helper_neon_qshl_u8,
8595                   gen_helper_neon_qshl_u16,
8596                   gen_helper_neon_qshl_u32 }
8597             }
8598         };
8599         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8600         MemOp memop = scalar ? size : MO_32;
8601         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8602 
8603         for (pass = 0; pass < maxpass; pass++) {
8604             TCGv_i32 tcg_op = tcg_temp_new_i32();
8605 
8606             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8607             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8608             if (scalar) {
8609                 switch (size) {
8610                 case 0:
8611                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8612                     break;
8613                 case 1:
8614                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8615                     break;
8616                 case 2:
8617                     break;
8618                 default:
8619                     g_assert_not_reached();
8620                 }
8621                 write_fp_sreg(s, rd, tcg_op);
8622             } else {
8623                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8624             }
8625         }
8626 
8627         if (!scalar) {
8628             clear_vec_high(s, is_q, rd);
8629         }
8630     }
8631 }
8632 
8633 /* Common vector code for handling integer to FP conversion */
8634 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8635                                    int elements, int is_signed,
8636                                    int fracbits, int size)
8637 {
8638     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8639     TCGv_i32 tcg_shift = NULL;
8640 
8641     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8642     int pass;
8643 
8644     if (fracbits || size == MO_64) {
8645         tcg_shift = tcg_constant_i32(fracbits);
8646     }
8647 
8648     if (size == MO_64) {
8649         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8650         TCGv_i64 tcg_double = tcg_temp_new_i64();
8651 
8652         for (pass = 0; pass < elements; pass++) {
8653             read_vec_element(s, tcg_int64, rn, pass, mop);
8654 
8655             if (is_signed) {
8656                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8657                                      tcg_shift, tcg_fpst);
8658             } else {
8659                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8660                                      tcg_shift, tcg_fpst);
8661             }
8662             if (elements == 1) {
8663                 write_fp_dreg(s, rd, tcg_double);
8664             } else {
8665                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8666             }
8667         }
8668     } else {
8669         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8670         TCGv_i32 tcg_float = tcg_temp_new_i32();
8671 
8672         for (pass = 0; pass < elements; pass++) {
8673             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8674 
8675             switch (size) {
8676             case MO_32:
8677                 if (fracbits) {
8678                     if (is_signed) {
8679                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8680                                              tcg_shift, tcg_fpst);
8681                     } else {
8682                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8683                                              tcg_shift, tcg_fpst);
8684                     }
8685                 } else {
8686                     if (is_signed) {
8687                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8688                     } else {
8689                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8690                     }
8691                 }
8692                 break;
8693             case MO_16:
8694                 if (fracbits) {
8695                     if (is_signed) {
8696                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8697                                              tcg_shift, tcg_fpst);
8698                     } else {
8699                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8700                                              tcg_shift, tcg_fpst);
8701                     }
8702                 } else {
8703                     if (is_signed) {
8704                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8705                     } else {
8706                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8707                     }
8708                 }
8709                 break;
8710             default:
8711                 g_assert_not_reached();
8712             }
8713 
8714             if (elements == 1) {
8715                 write_fp_sreg(s, rd, tcg_float);
8716             } else {
8717                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8718             }
8719         }
8720     }
8721 
8722     clear_vec_high(s, elements << size == 16, rd);
8723 }
8724 
8725 /* UCVTF/SCVTF - Integer to FP conversion */
8726 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8727                                          bool is_q, bool is_u,
8728                                          int immh, int immb, int opcode,
8729                                          int rn, int rd)
8730 {
8731     int size, elements, fracbits;
8732     int immhb = immh << 3 | immb;
8733 
8734     if (immh & 8) {
8735         size = MO_64;
8736         if (!is_scalar && !is_q) {
8737             unallocated_encoding(s);
8738             return;
8739         }
8740     } else if (immh & 4) {
8741         size = MO_32;
8742     } else if (immh & 2) {
8743         size = MO_16;
8744         if (!dc_isar_feature(aa64_fp16, s)) {
8745             unallocated_encoding(s);
8746             return;
8747         }
8748     } else {
8749         /* immh == 0 would be a failure of the decode logic */
8750         g_assert(immh == 1);
8751         unallocated_encoding(s);
8752         return;
8753     }
8754 
8755     if (is_scalar) {
8756         elements = 1;
8757     } else {
8758         elements = (8 << is_q) >> size;
8759     }
8760     fracbits = (16 << size) - immhb;
8761 
8762     if (!fp_access_check(s)) {
8763         return;
8764     }
8765 
8766     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8767 }
8768 
8769 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8770 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8771                                          bool is_q, bool is_u,
8772                                          int immh, int immb, int rn, int rd)
8773 {
8774     int immhb = immh << 3 | immb;
8775     int pass, size, fracbits;
8776     TCGv_ptr tcg_fpstatus;
8777     TCGv_i32 tcg_rmode, tcg_shift;
8778 
8779     if (immh & 0x8) {
8780         size = MO_64;
8781         if (!is_scalar && !is_q) {
8782             unallocated_encoding(s);
8783             return;
8784         }
8785     } else if (immh & 0x4) {
8786         size = MO_32;
8787     } else if (immh & 0x2) {
8788         size = MO_16;
8789         if (!dc_isar_feature(aa64_fp16, s)) {
8790             unallocated_encoding(s);
8791             return;
8792         }
8793     } else {
8794         /* Should have split out AdvSIMD modified immediate earlier.  */
8795         assert(immh == 1);
8796         unallocated_encoding(s);
8797         return;
8798     }
8799 
8800     if (!fp_access_check(s)) {
8801         return;
8802     }
8803 
8804     assert(!(is_scalar && is_q));
8805 
8806     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8807     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8808     fracbits = (16 << size) - immhb;
8809     tcg_shift = tcg_constant_i32(fracbits);
8810 
8811     if (size == MO_64) {
8812         int maxpass = is_scalar ? 1 : 2;
8813 
8814         for (pass = 0; pass < maxpass; pass++) {
8815             TCGv_i64 tcg_op = tcg_temp_new_i64();
8816 
8817             read_vec_element(s, tcg_op, rn, pass, MO_64);
8818             if (is_u) {
8819                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8820             } else {
8821                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8822             }
8823             write_vec_element(s, tcg_op, rd, pass, MO_64);
8824         }
8825         clear_vec_high(s, is_q, rd);
8826     } else {
8827         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8828         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8829 
8830         switch (size) {
8831         case MO_16:
8832             if (is_u) {
8833                 fn = gen_helper_vfp_touhh;
8834             } else {
8835                 fn = gen_helper_vfp_toshh;
8836             }
8837             break;
8838         case MO_32:
8839             if (is_u) {
8840                 fn = gen_helper_vfp_touls;
8841             } else {
8842                 fn = gen_helper_vfp_tosls;
8843             }
8844             break;
8845         default:
8846             g_assert_not_reached();
8847         }
8848 
8849         for (pass = 0; pass < maxpass; pass++) {
8850             TCGv_i32 tcg_op = tcg_temp_new_i32();
8851 
8852             read_vec_element_i32(s, tcg_op, rn, pass, size);
8853             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8854             if (is_scalar) {
8855                 write_fp_sreg(s, rd, tcg_op);
8856             } else {
8857                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8858             }
8859         }
8860         if (!is_scalar) {
8861             clear_vec_high(s, is_q, rd);
8862         }
8863     }
8864 
8865     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8866 }
8867 
8868 /* AdvSIMD scalar shift by immediate
8869  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8870  * +-----+---+-------------+------+------+--------+---+------+------+
8871  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8872  * +-----+---+-------------+------+------+--------+---+------+------+
8873  *
8874  * This is the scalar version so it works on a fixed sized registers
8875  */
8876 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8877 {
8878     int rd = extract32(insn, 0, 5);
8879     int rn = extract32(insn, 5, 5);
8880     int opcode = extract32(insn, 11, 5);
8881     int immb = extract32(insn, 16, 3);
8882     int immh = extract32(insn, 19, 4);
8883     bool is_u = extract32(insn, 29, 1);
8884 
8885     if (immh == 0) {
8886         unallocated_encoding(s);
8887         return;
8888     }
8889 
8890     switch (opcode) {
8891     case 0x08: /* SRI */
8892         if (!is_u) {
8893             unallocated_encoding(s);
8894             return;
8895         }
8896         /* fall through */
8897     case 0x00: /* SSHR / USHR */
8898     case 0x02: /* SSRA / USRA */
8899     case 0x04: /* SRSHR / URSHR */
8900     case 0x06: /* SRSRA / URSRA */
8901         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8902         break;
8903     case 0x0a: /* SHL / SLI */
8904         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8905         break;
8906     case 0x1c: /* SCVTF, UCVTF */
8907         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8908                                      opcode, rn, rd);
8909         break;
8910     case 0x10: /* SQSHRUN, SQSHRUN2 */
8911     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8912         if (!is_u) {
8913             unallocated_encoding(s);
8914             return;
8915         }
8916         handle_vec_simd_sqshrn(s, true, false, false, true,
8917                                immh, immb, opcode, rn, rd);
8918         break;
8919     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8920     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8921         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8922                                immh, immb, opcode, rn, rd);
8923         break;
8924     case 0xc: /* SQSHLU */
8925         if (!is_u) {
8926             unallocated_encoding(s);
8927             return;
8928         }
8929         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8930         break;
8931     case 0xe: /* SQSHL, UQSHL */
8932         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8933         break;
8934     case 0x1f: /* FCVTZS, FCVTZU */
8935         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8936         break;
8937     default:
8938         unallocated_encoding(s);
8939         break;
8940     }
8941 }
8942 
8943 /* AdvSIMD scalar three different
8944  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8945  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8946  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8947  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8948  */
8949 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8950 {
8951     bool is_u = extract32(insn, 29, 1);
8952     int size = extract32(insn, 22, 2);
8953     int opcode = extract32(insn, 12, 4);
8954     int rm = extract32(insn, 16, 5);
8955     int rn = extract32(insn, 5, 5);
8956     int rd = extract32(insn, 0, 5);
8957 
8958     if (is_u) {
8959         unallocated_encoding(s);
8960         return;
8961     }
8962 
8963     switch (opcode) {
8964     case 0x9: /* SQDMLAL, SQDMLAL2 */
8965     case 0xb: /* SQDMLSL, SQDMLSL2 */
8966     case 0xd: /* SQDMULL, SQDMULL2 */
8967         if (size == 0 || size == 3) {
8968             unallocated_encoding(s);
8969             return;
8970         }
8971         break;
8972     default:
8973         unallocated_encoding(s);
8974         return;
8975     }
8976 
8977     if (!fp_access_check(s)) {
8978         return;
8979     }
8980 
8981     if (size == 2) {
8982         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8983         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8984         TCGv_i64 tcg_res = tcg_temp_new_i64();
8985 
8986         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8987         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8988 
8989         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8990         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8991 
8992         switch (opcode) {
8993         case 0xd: /* SQDMULL, SQDMULL2 */
8994             break;
8995         case 0xb: /* SQDMLSL, SQDMLSL2 */
8996             tcg_gen_neg_i64(tcg_res, tcg_res);
8997             /* fall through */
8998         case 0x9: /* SQDMLAL, SQDMLAL2 */
8999             read_vec_element(s, tcg_op1, rd, 0, MO_64);
9000             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
9001                                               tcg_res, tcg_op1);
9002             break;
9003         default:
9004             g_assert_not_reached();
9005         }
9006 
9007         write_fp_dreg(s, rd, tcg_res);
9008     } else {
9009         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9010         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9011         TCGv_i64 tcg_res = tcg_temp_new_i64();
9012 
9013         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9014         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9015 
9016         switch (opcode) {
9017         case 0xd: /* SQDMULL, SQDMULL2 */
9018             break;
9019         case 0xb: /* SQDMLSL, SQDMLSL2 */
9020             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9021             /* fall through */
9022         case 0x9: /* SQDMLAL, SQDMLAL2 */
9023         {
9024             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9025             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9026             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9027                                               tcg_res, tcg_op3);
9028             break;
9029         }
9030         default:
9031             g_assert_not_reached();
9032         }
9033 
9034         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9035         write_fp_dreg(s, rd, tcg_res);
9036     }
9037 }
9038 
9039 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9040                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9041 {
9042     /* Handle 64x64->64 opcodes which are shared between the scalar
9043      * and vector 3-same groups. We cover every opcode where size == 3
9044      * is valid in either the three-reg-same (integer, not pairwise)
9045      * or scalar-three-reg-same groups.
9046      */
9047     TCGCond cond;
9048 
9049     switch (opcode) {
9050     case 0x1: /* SQADD */
9051         if (u) {
9052             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9053         } else {
9054             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9055         }
9056         break;
9057     case 0x5: /* SQSUB */
9058         if (u) {
9059             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9060         } else {
9061             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9062         }
9063         break;
9064     case 0x6: /* CMGT, CMHI */
9065         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9066          * We implement this using setcond (test) and then negating.
9067          */
9068         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9069     do_cmop:
9070         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9071         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9072         break;
9073     case 0x7: /* CMGE, CMHS */
9074         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9075         goto do_cmop;
9076     case 0x11: /* CMTST, CMEQ */
9077         if (u) {
9078             cond = TCG_COND_EQ;
9079             goto do_cmop;
9080         }
9081         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9082         break;
9083     case 0x8: /* SSHL, USHL */
9084         if (u) {
9085             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9086         } else {
9087             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9088         }
9089         break;
9090     case 0x9: /* SQSHL, UQSHL */
9091         if (u) {
9092             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9093         } else {
9094             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9095         }
9096         break;
9097     case 0xa: /* SRSHL, URSHL */
9098         if (u) {
9099             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9100         } else {
9101             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9102         }
9103         break;
9104     case 0xb: /* SQRSHL, UQRSHL */
9105         if (u) {
9106             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9107         } else {
9108             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9109         }
9110         break;
9111     case 0x10: /* ADD, SUB */
9112         if (u) {
9113             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9114         } else {
9115             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9116         }
9117         break;
9118     default:
9119         g_assert_not_reached();
9120     }
9121 }
9122 
9123 /* Handle the 3-same-operands float operations; shared by the scalar
9124  * and vector encodings. The caller must filter out any encodings
9125  * not allocated for the encoding it is dealing with.
9126  */
9127 static void handle_3same_float(DisasContext *s, int size, int elements,
9128                                int fpopcode, int rd, int rn, int rm)
9129 {
9130     int pass;
9131     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9132 
9133     for (pass = 0; pass < elements; pass++) {
9134         if (size) {
9135             /* Double */
9136             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9137             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9138             TCGv_i64 tcg_res = tcg_temp_new_i64();
9139 
9140             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9141             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9142 
9143             switch (fpopcode) {
9144             case 0x39: /* FMLS */
9145                 /* As usual for ARM, separate negation for fused multiply-add */
9146                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9147                 /* fall through */
9148             case 0x19: /* FMLA */
9149                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9150                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9151                                        tcg_res, fpst);
9152                 break;
9153             case 0x18: /* FMAXNM */
9154                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9155                 break;
9156             case 0x1a: /* FADD */
9157                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9158                 break;
9159             case 0x1b: /* FMULX */
9160                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9161                 break;
9162             case 0x1c: /* FCMEQ */
9163                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9164                 break;
9165             case 0x1e: /* FMAX */
9166                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9167                 break;
9168             case 0x1f: /* FRECPS */
9169                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9170                 break;
9171             case 0x38: /* FMINNM */
9172                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9173                 break;
9174             case 0x3a: /* FSUB */
9175                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9176                 break;
9177             case 0x3e: /* FMIN */
9178                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9179                 break;
9180             case 0x3f: /* FRSQRTS */
9181                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9182                 break;
9183             case 0x5b: /* FMUL */
9184                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9185                 break;
9186             case 0x5c: /* FCMGE */
9187                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9188                 break;
9189             case 0x5d: /* FACGE */
9190                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9191                 break;
9192             case 0x5f: /* FDIV */
9193                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9194                 break;
9195             case 0x7a: /* FABD */
9196                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9197                 gen_helper_vfp_absd(tcg_res, tcg_res);
9198                 break;
9199             case 0x7c: /* FCMGT */
9200                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9201                 break;
9202             case 0x7d: /* FACGT */
9203                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9204                 break;
9205             default:
9206                 g_assert_not_reached();
9207             }
9208 
9209             write_vec_element(s, tcg_res, rd, pass, MO_64);
9210         } else {
9211             /* Single */
9212             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9213             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9214             TCGv_i32 tcg_res = tcg_temp_new_i32();
9215 
9216             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9217             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9218 
9219             switch (fpopcode) {
9220             case 0x39: /* FMLS */
9221                 /* As usual for ARM, separate negation for fused multiply-add */
9222                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9223                 /* fall through */
9224             case 0x19: /* FMLA */
9225                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9226                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9227                                        tcg_res, fpst);
9228                 break;
9229             case 0x1a: /* FADD */
9230                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9231                 break;
9232             case 0x1b: /* FMULX */
9233                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9234                 break;
9235             case 0x1c: /* FCMEQ */
9236                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9237                 break;
9238             case 0x1e: /* FMAX */
9239                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9240                 break;
9241             case 0x1f: /* FRECPS */
9242                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9243                 break;
9244             case 0x18: /* FMAXNM */
9245                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9246                 break;
9247             case 0x38: /* FMINNM */
9248                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9249                 break;
9250             case 0x3a: /* FSUB */
9251                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9252                 break;
9253             case 0x3e: /* FMIN */
9254                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9255                 break;
9256             case 0x3f: /* FRSQRTS */
9257                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9258                 break;
9259             case 0x5b: /* FMUL */
9260                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9261                 break;
9262             case 0x5c: /* FCMGE */
9263                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9264                 break;
9265             case 0x5d: /* FACGE */
9266                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9267                 break;
9268             case 0x5f: /* FDIV */
9269                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9270                 break;
9271             case 0x7a: /* FABD */
9272                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9273                 gen_helper_vfp_abss(tcg_res, tcg_res);
9274                 break;
9275             case 0x7c: /* FCMGT */
9276                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9277                 break;
9278             case 0x7d: /* FACGT */
9279                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9280                 break;
9281             default:
9282                 g_assert_not_reached();
9283             }
9284 
9285             if (elements == 1) {
9286                 /* scalar single so clear high part */
9287                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9288 
9289                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9290                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9291             } else {
9292                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9293             }
9294         }
9295     }
9296 
9297     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9298 }
9299 
9300 /* AdvSIMD scalar three same
9301  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9302  * +-----+---+-----------+------+---+------+--------+---+------+------+
9303  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9304  * +-----+---+-----------+------+---+------+--------+---+------+------+
9305  */
9306 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9307 {
9308     int rd = extract32(insn, 0, 5);
9309     int rn = extract32(insn, 5, 5);
9310     int opcode = extract32(insn, 11, 5);
9311     int rm = extract32(insn, 16, 5);
9312     int size = extract32(insn, 22, 2);
9313     bool u = extract32(insn, 29, 1);
9314     TCGv_i64 tcg_rd;
9315 
9316     if (opcode >= 0x18) {
9317         /* Floating point: U, size[1] and opcode indicate operation */
9318         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9319         switch (fpopcode) {
9320         case 0x1b: /* FMULX */
9321         case 0x1f: /* FRECPS */
9322         case 0x3f: /* FRSQRTS */
9323         case 0x5d: /* FACGE */
9324         case 0x7d: /* FACGT */
9325         case 0x1c: /* FCMEQ */
9326         case 0x5c: /* FCMGE */
9327         case 0x7c: /* FCMGT */
9328         case 0x7a: /* FABD */
9329             break;
9330         default:
9331             unallocated_encoding(s);
9332             return;
9333         }
9334 
9335         if (!fp_access_check(s)) {
9336             return;
9337         }
9338 
9339         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9340         return;
9341     }
9342 
9343     switch (opcode) {
9344     case 0x1: /* SQADD, UQADD */
9345     case 0x5: /* SQSUB, UQSUB */
9346     case 0x9: /* SQSHL, UQSHL */
9347     case 0xb: /* SQRSHL, UQRSHL */
9348         break;
9349     case 0x8: /* SSHL, USHL */
9350     case 0xa: /* SRSHL, URSHL */
9351     case 0x6: /* CMGT, CMHI */
9352     case 0x7: /* CMGE, CMHS */
9353     case 0x11: /* CMTST, CMEQ */
9354     case 0x10: /* ADD, SUB (vector) */
9355         if (size != 3) {
9356             unallocated_encoding(s);
9357             return;
9358         }
9359         break;
9360     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9361         if (size != 1 && size != 2) {
9362             unallocated_encoding(s);
9363             return;
9364         }
9365         break;
9366     default:
9367         unallocated_encoding(s);
9368         return;
9369     }
9370 
9371     if (!fp_access_check(s)) {
9372         return;
9373     }
9374 
9375     tcg_rd = tcg_temp_new_i64();
9376 
9377     if (size == 3) {
9378         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9379         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9380 
9381         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9382     } else {
9383         /* Do a single operation on the lowest element in the vector.
9384          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9385          * no side effects for all these operations.
9386          * OPTME: special-purpose helpers would avoid doing some
9387          * unnecessary work in the helper for the 8 and 16 bit cases.
9388          */
9389         NeonGenTwoOpEnvFn *genenvfn;
9390         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9391         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9392         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9393 
9394         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9395         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9396 
9397         switch (opcode) {
9398         case 0x1: /* SQADD, UQADD */
9399         {
9400             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9401                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9402                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9403                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9404             };
9405             genenvfn = fns[size][u];
9406             break;
9407         }
9408         case 0x5: /* SQSUB, UQSUB */
9409         {
9410             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9411                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9412                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9413                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9414             };
9415             genenvfn = fns[size][u];
9416             break;
9417         }
9418         case 0x9: /* SQSHL, UQSHL */
9419         {
9420             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9421                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9422                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9423                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9424             };
9425             genenvfn = fns[size][u];
9426             break;
9427         }
9428         case 0xb: /* SQRSHL, UQRSHL */
9429         {
9430             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9431                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9432                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9433                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9434             };
9435             genenvfn = fns[size][u];
9436             break;
9437         }
9438         case 0x16: /* SQDMULH, SQRDMULH */
9439         {
9440             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9441                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9442                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9443             };
9444             assert(size == 1 || size == 2);
9445             genenvfn = fns[size - 1][u];
9446             break;
9447         }
9448         default:
9449             g_assert_not_reached();
9450         }
9451 
9452         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9453         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9454     }
9455 
9456     write_fp_dreg(s, rd, tcg_rd);
9457 }
9458 
9459 /* AdvSIMD scalar three same FP16
9460  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9461  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9462  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9463  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9464  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9465  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9466  */
9467 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9468                                                   uint32_t insn)
9469 {
9470     int rd = extract32(insn, 0, 5);
9471     int rn = extract32(insn, 5, 5);
9472     int opcode = extract32(insn, 11, 3);
9473     int rm = extract32(insn, 16, 5);
9474     bool u = extract32(insn, 29, 1);
9475     bool a = extract32(insn, 23, 1);
9476     int fpopcode = opcode | (a << 3) |  (u << 4);
9477     TCGv_ptr fpst;
9478     TCGv_i32 tcg_op1;
9479     TCGv_i32 tcg_op2;
9480     TCGv_i32 tcg_res;
9481 
9482     switch (fpopcode) {
9483     case 0x03: /* FMULX */
9484     case 0x04: /* FCMEQ (reg) */
9485     case 0x07: /* FRECPS */
9486     case 0x0f: /* FRSQRTS */
9487     case 0x14: /* FCMGE (reg) */
9488     case 0x15: /* FACGE */
9489     case 0x1a: /* FABD */
9490     case 0x1c: /* FCMGT (reg) */
9491     case 0x1d: /* FACGT */
9492         break;
9493     default:
9494         unallocated_encoding(s);
9495         return;
9496     }
9497 
9498     if (!dc_isar_feature(aa64_fp16, s)) {
9499         unallocated_encoding(s);
9500     }
9501 
9502     if (!fp_access_check(s)) {
9503         return;
9504     }
9505 
9506     fpst = fpstatus_ptr(FPST_FPCR_F16);
9507 
9508     tcg_op1 = read_fp_hreg(s, rn);
9509     tcg_op2 = read_fp_hreg(s, rm);
9510     tcg_res = tcg_temp_new_i32();
9511 
9512     switch (fpopcode) {
9513     case 0x03: /* FMULX */
9514         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9515         break;
9516     case 0x04: /* FCMEQ (reg) */
9517         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9518         break;
9519     case 0x07: /* FRECPS */
9520         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9521         break;
9522     case 0x0f: /* FRSQRTS */
9523         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9524         break;
9525     case 0x14: /* FCMGE (reg) */
9526         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9527         break;
9528     case 0x15: /* FACGE */
9529         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9530         break;
9531     case 0x1a: /* FABD */
9532         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9533         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9534         break;
9535     case 0x1c: /* FCMGT (reg) */
9536         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9537         break;
9538     case 0x1d: /* FACGT */
9539         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9540         break;
9541     default:
9542         g_assert_not_reached();
9543     }
9544 
9545     write_fp_sreg(s, rd, tcg_res);
9546 }
9547 
9548 /* AdvSIMD scalar three same extra
9549  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9550  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9551  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9552  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9553  */
9554 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9555                                                    uint32_t insn)
9556 {
9557     int rd = extract32(insn, 0, 5);
9558     int rn = extract32(insn, 5, 5);
9559     int opcode = extract32(insn, 11, 4);
9560     int rm = extract32(insn, 16, 5);
9561     int size = extract32(insn, 22, 2);
9562     bool u = extract32(insn, 29, 1);
9563     TCGv_i32 ele1, ele2, ele3;
9564     TCGv_i64 res;
9565     bool feature;
9566 
9567     switch (u * 16 + opcode) {
9568     case 0x10: /* SQRDMLAH (vector) */
9569     case 0x11: /* SQRDMLSH (vector) */
9570         if (size != 1 && size != 2) {
9571             unallocated_encoding(s);
9572             return;
9573         }
9574         feature = dc_isar_feature(aa64_rdm, s);
9575         break;
9576     default:
9577         unallocated_encoding(s);
9578         return;
9579     }
9580     if (!feature) {
9581         unallocated_encoding(s);
9582         return;
9583     }
9584     if (!fp_access_check(s)) {
9585         return;
9586     }
9587 
9588     /* Do a single operation on the lowest element in the vector.
9589      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9590      * with no side effects for all these operations.
9591      * OPTME: special-purpose helpers would avoid doing some
9592      * unnecessary work in the helper for the 16 bit cases.
9593      */
9594     ele1 = tcg_temp_new_i32();
9595     ele2 = tcg_temp_new_i32();
9596     ele3 = tcg_temp_new_i32();
9597 
9598     read_vec_element_i32(s, ele1, rn, 0, size);
9599     read_vec_element_i32(s, ele2, rm, 0, size);
9600     read_vec_element_i32(s, ele3, rd, 0, size);
9601 
9602     switch (opcode) {
9603     case 0x0: /* SQRDMLAH */
9604         if (size == 1) {
9605             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9606         } else {
9607             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9608         }
9609         break;
9610     case 0x1: /* SQRDMLSH */
9611         if (size == 1) {
9612             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9613         } else {
9614             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9615         }
9616         break;
9617     default:
9618         g_assert_not_reached();
9619     }
9620 
9621     res = tcg_temp_new_i64();
9622     tcg_gen_extu_i32_i64(res, ele3);
9623     write_fp_dreg(s, rd, res);
9624 }
9625 
9626 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9627                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9628                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9629 {
9630     /* Handle 64->64 opcodes which are shared between the scalar and
9631      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9632      * is valid in either group and also the double-precision fp ops.
9633      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9634      * requires them.
9635      */
9636     TCGCond cond;
9637 
9638     switch (opcode) {
9639     case 0x4: /* CLS, CLZ */
9640         if (u) {
9641             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9642         } else {
9643             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9644         }
9645         break;
9646     case 0x5: /* NOT */
9647         /* This opcode is shared with CNT and RBIT but we have earlier
9648          * enforced that size == 3 if and only if this is the NOT insn.
9649          */
9650         tcg_gen_not_i64(tcg_rd, tcg_rn);
9651         break;
9652     case 0x7: /* SQABS, SQNEG */
9653         if (u) {
9654             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9655         } else {
9656             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9657         }
9658         break;
9659     case 0xa: /* CMLT */
9660         /* 64 bit integer comparison against zero, result is
9661          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9662          * subtracting 1.
9663          */
9664         cond = TCG_COND_LT;
9665     do_cmop:
9666         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9667         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9668         break;
9669     case 0x8: /* CMGT, CMGE */
9670         cond = u ? TCG_COND_GE : TCG_COND_GT;
9671         goto do_cmop;
9672     case 0x9: /* CMEQ, CMLE */
9673         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9674         goto do_cmop;
9675     case 0xb: /* ABS, NEG */
9676         if (u) {
9677             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9678         } else {
9679             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9680         }
9681         break;
9682     case 0x2f: /* FABS */
9683         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9684         break;
9685     case 0x6f: /* FNEG */
9686         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9687         break;
9688     case 0x7f: /* FSQRT */
9689         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9690         break;
9691     case 0x1a: /* FCVTNS */
9692     case 0x1b: /* FCVTMS */
9693     case 0x1c: /* FCVTAS */
9694     case 0x3a: /* FCVTPS */
9695     case 0x3b: /* FCVTZS */
9696         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9697         break;
9698     case 0x5a: /* FCVTNU */
9699     case 0x5b: /* FCVTMU */
9700     case 0x5c: /* FCVTAU */
9701     case 0x7a: /* FCVTPU */
9702     case 0x7b: /* FCVTZU */
9703         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9704         break;
9705     case 0x18: /* FRINTN */
9706     case 0x19: /* FRINTM */
9707     case 0x38: /* FRINTP */
9708     case 0x39: /* FRINTZ */
9709     case 0x58: /* FRINTA */
9710     case 0x79: /* FRINTI */
9711         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9712         break;
9713     case 0x59: /* FRINTX */
9714         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9715         break;
9716     case 0x1e: /* FRINT32Z */
9717     case 0x5e: /* FRINT32X */
9718         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9719         break;
9720     case 0x1f: /* FRINT64Z */
9721     case 0x5f: /* FRINT64X */
9722         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9723         break;
9724     default:
9725         g_assert_not_reached();
9726     }
9727 }
9728 
9729 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9730                                    bool is_scalar, bool is_u, bool is_q,
9731                                    int size, int rn, int rd)
9732 {
9733     bool is_double = (size == MO_64);
9734     TCGv_ptr fpst;
9735 
9736     if (!fp_access_check(s)) {
9737         return;
9738     }
9739 
9740     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9741 
9742     if (is_double) {
9743         TCGv_i64 tcg_op = tcg_temp_new_i64();
9744         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9745         TCGv_i64 tcg_res = tcg_temp_new_i64();
9746         NeonGenTwoDoubleOpFn *genfn;
9747         bool swap = false;
9748         int pass;
9749 
9750         switch (opcode) {
9751         case 0x2e: /* FCMLT (zero) */
9752             swap = true;
9753             /* fallthrough */
9754         case 0x2c: /* FCMGT (zero) */
9755             genfn = gen_helper_neon_cgt_f64;
9756             break;
9757         case 0x2d: /* FCMEQ (zero) */
9758             genfn = gen_helper_neon_ceq_f64;
9759             break;
9760         case 0x6d: /* FCMLE (zero) */
9761             swap = true;
9762             /* fall through */
9763         case 0x6c: /* FCMGE (zero) */
9764             genfn = gen_helper_neon_cge_f64;
9765             break;
9766         default:
9767             g_assert_not_reached();
9768         }
9769 
9770         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9771             read_vec_element(s, tcg_op, rn, pass, MO_64);
9772             if (swap) {
9773                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9774             } else {
9775                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9776             }
9777             write_vec_element(s, tcg_res, rd, pass, MO_64);
9778         }
9779 
9780         clear_vec_high(s, !is_scalar, rd);
9781     } else {
9782         TCGv_i32 tcg_op = tcg_temp_new_i32();
9783         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9784         TCGv_i32 tcg_res = tcg_temp_new_i32();
9785         NeonGenTwoSingleOpFn *genfn;
9786         bool swap = false;
9787         int pass, maxpasses;
9788 
9789         if (size == MO_16) {
9790             switch (opcode) {
9791             case 0x2e: /* FCMLT (zero) */
9792                 swap = true;
9793                 /* fall through */
9794             case 0x2c: /* FCMGT (zero) */
9795                 genfn = gen_helper_advsimd_cgt_f16;
9796                 break;
9797             case 0x2d: /* FCMEQ (zero) */
9798                 genfn = gen_helper_advsimd_ceq_f16;
9799                 break;
9800             case 0x6d: /* FCMLE (zero) */
9801                 swap = true;
9802                 /* fall through */
9803             case 0x6c: /* FCMGE (zero) */
9804                 genfn = gen_helper_advsimd_cge_f16;
9805                 break;
9806             default:
9807                 g_assert_not_reached();
9808             }
9809         } else {
9810             switch (opcode) {
9811             case 0x2e: /* FCMLT (zero) */
9812                 swap = true;
9813                 /* fall through */
9814             case 0x2c: /* FCMGT (zero) */
9815                 genfn = gen_helper_neon_cgt_f32;
9816                 break;
9817             case 0x2d: /* FCMEQ (zero) */
9818                 genfn = gen_helper_neon_ceq_f32;
9819                 break;
9820             case 0x6d: /* FCMLE (zero) */
9821                 swap = true;
9822                 /* fall through */
9823             case 0x6c: /* FCMGE (zero) */
9824                 genfn = gen_helper_neon_cge_f32;
9825                 break;
9826             default:
9827                 g_assert_not_reached();
9828             }
9829         }
9830 
9831         if (is_scalar) {
9832             maxpasses = 1;
9833         } else {
9834             int vector_size = 8 << is_q;
9835             maxpasses = vector_size >> size;
9836         }
9837 
9838         for (pass = 0; pass < maxpasses; pass++) {
9839             read_vec_element_i32(s, tcg_op, rn, pass, size);
9840             if (swap) {
9841                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9842             } else {
9843                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9844             }
9845             if (is_scalar) {
9846                 write_fp_sreg(s, rd, tcg_res);
9847             } else {
9848                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9849             }
9850         }
9851 
9852         if (!is_scalar) {
9853             clear_vec_high(s, is_q, rd);
9854         }
9855     }
9856 }
9857 
9858 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9859                                     bool is_scalar, bool is_u, bool is_q,
9860                                     int size, int rn, int rd)
9861 {
9862     bool is_double = (size == 3);
9863     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9864 
9865     if (is_double) {
9866         TCGv_i64 tcg_op = tcg_temp_new_i64();
9867         TCGv_i64 tcg_res = tcg_temp_new_i64();
9868         int pass;
9869 
9870         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9871             read_vec_element(s, tcg_op, rn, pass, MO_64);
9872             switch (opcode) {
9873             case 0x3d: /* FRECPE */
9874                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9875                 break;
9876             case 0x3f: /* FRECPX */
9877                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9878                 break;
9879             case 0x7d: /* FRSQRTE */
9880                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9881                 break;
9882             default:
9883                 g_assert_not_reached();
9884             }
9885             write_vec_element(s, tcg_res, rd, pass, MO_64);
9886         }
9887         clear_vec_high(s, !is_scalar, rd);
9888     } else {
9889         TCGv_i32 tcg_op = tcg_temp_new_i32();
9890         TCGv_i32 tcg_res = tcg_temp_new_i32();
9891         int pass, maxpasses;
9892 
9893         if (is_scalar) {
9894             maxpasses = 1;
9895         } else {
9896             maxpasses = is_q ? 4 : 2;
9897         }
9898 
9899         for (pass = 0; pass < maxpasses; pass++) {
9900             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9901 
9902             switch (opcode) {
9903             case 0x3c: /* URECPE */
9904                 gen_helper_recpe_u32(tcg_res, tcg_op);
9905                 break;
9906             case 0x3d: /* FRECPE */
9907                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9908                 break;
9909             case 0x3f: /* FRECPX */
9910                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9911                 break;
9912             case 0x7d: /* FRSQRTE */
9913                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9914                 break;
9915             default:
9916                 g_assert_not_reached();
9917             }
9918 
9919             if (is_scalar) {
9920                 write_fp_sreg(s, rd, tcg_res);
9921             } else {
9922                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9923             }
9924         }
9925         if (!is_scalar) {
9926             clear_vec_high(s, is_q, rd);
9927         }
9928     }
9929 }
9930 
9931 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9932                                 int opcode, bool u, bool is_q,
9933                                 int size, int rn, int rd)
9934 {
9935     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9936      * in the source becomes a size element in the destination).
9937      */
9938     int pass;
9939     TCGv_i32 tcg_res[2];
9940     int destelt = is_q ? 2 : 0;
9941     int passes = scalar ? 1 : 2;
9942 
9943     if (scalar) {
9944         tcg_res[1] = tcg_constant_i32(0);
9945     }
9946 
9947     for (pass = 0; pass < passes; pass++) {
9948         TCGv_i64 tcg_op = tcg_temp_new_i64();
9949         NeonGenNarrowFn *genfn = NULL;
9950         NeonGenNarrowEnvFn *genenvfn = NULL;
9951 
9952         if (scalar) {
9953             read_vec_element(s, tcg_op, rn, pass, size + 1);
9954         } else {
9955             read_vec_element(s, tcg_op, rn, pass, MO_64);
9956         }
9957         tcg_res[pass] = tcg_temp_new_i32();
9958 
9959         switch (opcode) {
9960         case 0x12: /* XTN, SQXTUN */
9961         {
9962             static NeonGenNarrowFn * const xtnfns[3] = {
9963                 gen_helper_neon_narrow_u8,
9964                 gen_helper_neon_narrow_u16,
9965                 tcg_gen_extrl_i64_i32,
9966             };
9967             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9968                 gen_helper_neon_unarrow_sat8,
9969                 gen_helper_neon_unarrow_sat16,
9970                 gen_helper_neon_unarrow_sat32,
9971             };
9972             if (u) {
9973                 genenvfn = sqxtunfns[size];
9974             } else {
9975                 genfn = xtnfns[size];
9976             }
9977             break;
9978         }
9979         case 0x14: /* SQXTN, UQXTN */
9980         {
9981             static NeonGenNarrowEnvFn * const fns[3][2] = {
9982                 { gen_helper_neon_narrow_sat_s8,
9983                   gen_helper_neon_narrow_sat_u8 },
9984                 { gen_helper_neon_narrow_sat_s16,
9985                   gen_helper_neon_narrow_sat_u16 },
9986                 { gen_helper_neon_narrow_sat_s32,
9987                   gen_helper_neon_narrow_sat_u32 },
9988             };
9989             genenvfn = fns[size][u];
9990             break;
9991         }
9992         case 0x16: /* FCVTN, FCVTN2 */
9993             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9994             if (size == 2) {
9995                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9996             } else {
9997                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9998                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9999                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10000                 TCGv_i32 ahp = get_ahp_flag();
10001 
10002                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10003                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10004                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10005                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10006             }
10007             break;
10008         case 0x36: /* BFCVTN, BFCVTN2 */
10009             {
10010                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10011                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
10012             }
10013             break;
10014         case 0x56:  /* FCVTXN, FCVTXN2 */
10015             /* 64 bit to 32 bit float conversion
10016              * with von Neumann rounding (round to odd)
10017              */
10018             assert(size == 2);
10019             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10020             break;
10021         default:
10022             g_assert_not_reached();
10023         }
10024 
10025         if (genfn) {
10026             genfn(tcg_res[pass], tcg_op);
10027         } else if (genenvfn) {
10028             genenvfn(tcg_res[pass], cpu_env, tcg_op);
10029         }
10030     }
10031 
10032     for (pass = 0; pass < 2; pass++) {
10033         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10034     }
10035     clear_vec_high(s, is_q, rd);
10036 }
10037 
10038 /* Remaining saturating accumulating ops */
10039 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10040                                 bool is_q, int size, int rn, int rd)
10041 {
10042     bool is_double = (size == 3);
10043 
10044     if (is_double) {
10045         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10046         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10047         int pass;
10048 
10049         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10050             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10051             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10052 
10053             if (is_u) { /* USQADD */
10054                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10055             } else { /* SUQADD */
10056                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10057             }
10058             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10059         }
10060         clear_vec_high(s, !is_scalar, rd);
10061     } else {
10062         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10063         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10064         int pass, maxpasses;
10065 
10066         if (is_scalar) {
10067             maxpasses = 1;
10068         } else {
10069             maxpasses = is_q ? 4 : 2;
10070         }
10071 
10072         for (pass = 0; pass < maxpasses; pass++) {
10073             if (is_scalar) {
10074                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10075                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10076             } else {
10077                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10078                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10079             }
10080 
10081             if (is_u) { /* USQADD */
10082                 switch (size) {
10083                 case 0:
10084                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10085                     break;
10086                 case 1:
10087                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10088                     break;
10089                 case 2:
10090                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10091                     break;
10092                 default:
10093                     g_assert_not_reached();
10094                 }
10095             } else { /* SUQADD */
10096                 switch (size) {
10097                 case 0:
10098                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10099                     break;
10100                 case 1:
10101                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10102                     break;
10103                 case 2:
10104                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10105                     break;
10106                 default:
10107                     g_assert_not_reached();
10108                 }
10109             }
10110 
10111             if (is_scalar) {
10112                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10113             }
10114             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10115         }
10116         clear_vec_high(s, is_q, rd);
10117     }
10118 }
10119 
10120 /* AdvSIMD scalar two reg misc
10121  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10122  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10123  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10124  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10125  */
10126 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10127 {
10128     int rd = extract32(insn, 0, 5);
10129     int rn = extract32(insn, 5, 5);
10130     int opcode = extract32(insn, 12, 5);
10131     int size = extract32(insn, 22, 2);
10132     bool u = extract32(insn, 29, 1);
10133     bool is_fcvt = false;
10134     int rmode;
10135     TCGv_i32 tcg_rmode;
10136     TCGv_ptr tcg_fpstatus;
10137 
10138     switch (opcode) {
10139     case 0x3: /* USQADD / SUQADD*/
10140         if (!fp_access_check(s)) {
10141             return;
10142         }
10143         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10144         return;
10145     case 0x7: /* SQABS / SQNEG */
10146         break;
10147     case 0xa: /* CMLT */
10148         if (u) {
10149             unallocated_encoding(s);
10150             return;
10151         }
10152         /* fall through */
10153     case 0x8: /* CMGT, CMGE */
10154     case 0x9: /* CMEQ, CMLE */
10155     case 0xb: /* ABS, NEG */
10156         if (size != 3) {
10157             unallocated_encoding(s);
10158             return;
10159         }
10160         break;
10161     case 0x12: /* SQXTUN */
10162         if (!u) {
10163             unallocated_encoding(s);
10164             return;
10165         }
10166         /* fall through */
10167     case 0x14: /* SQXTN, UQXTN */
10168         if (size == 3) {
10169             unallocated_encoding(s);
10170             return;
10171         }
10172         if (!fp_access_check(s)) {
10173             return;
10174         }
10175         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10176         return;
10177     case 0xc ... 0xf:
10178     case 0x16 ... 0x1d:
10179     case 0x1f:
10180         /* Floating point: U, size[1] and opcode indicate operation;
10181          * size[0] indicates single or double precision.
10182          */
10183         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10184         size = extract32(size, 0, 1) ? 3 : 2;
10185         switch (opcode) {
10186         case 0x2c: /* FCMGT (zero) */
10187         case 0x2d: /* FCMEQ (zero) */
10188         case 0x2e: /* FCMLT (zero) */
10189         case 0x6c: /* FCMGE (zero) */
10190         case 0x6d: /* FCMLE (zero) */
10191             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10192             return;
10193         case 0x1d: /* SCVTF */
10194         case 0x5d: /* UCVTF */
10195         {
10196             bool is_signed = (opcode == 0x1d);
10197             if (!fp_access_check(s)) {
10198                 return;
10199             }
10200             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10201             return;
10202         }
10203         case 0x3d: /* FRECPE */
10204         case 0x3f: /* FRECPX */
10205         case 0x7d: /* FRSQRTE */
10206             if (!fp_access_check(s)) {
10207                 return;
10208             }
10209             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10210             return;
10211         case 0x1a: /* FCVTNS */
10212         case 0x1b: /* FCVTMS */
10213         case 0x3a: /* FCVTPS */
10214         case 0x3b: /* FCVTZS */
10215         case 0x5a: /* FCVTNU */
10216         case 0x5b: /* FCVTMU */
10217         case 0x7a: /* FCVTPU */
10218         case 0x7b: /* FCVTZU */
10219             is_fcvt = true;
10220             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10221             break;
10222         case 0x1c: /* FCVTAS */
10223         case 0x5c: /* FCVTAU */
10224             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10225             is_fcvt = true;
10226             rmode = FPROUNDING_TIEAWAY;
10227             break;
10228         case 0x56: /* FCVTXN, FCVTXN2 */
10229             if (size == 2) {
10230                 unallocated_encoding(s);
10231                 return;
10232             }
10233             if (!fp_access_check(s)) {
10234                 return;
10235             }
10236             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10237             return;
10238         default:
10239             unallocated_encoding(s);
10240             return;
10241         }
10242         break;
10243     default:
10244         unallocated_encoding(s);
10245         return;
10246     }
10247 
10248     if (!fp_access_check(s)) {
10249         return;
10250     }
10251 
10252     if (is_fcvt) {
10253         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10254         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10255     } else {
10256         tcg_fpstatus = NULL;
10257         tcg_rmode = NULL;
10258     }
10259 
10260     if (size == 3) {
10261         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10262         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10263 
10264         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10265         write_fp_dreg(s, rd, tcg_rd);
10266     } else {
10267         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10268         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10269 
10270         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10271 
10272         switch (opcode) {
10273         case 0x7: /* SQABS, SQNEG */
10274         {
10275             NeonGenOneOpEnvFn *genfn;
10276             static NeonGenOneOpEnvFn * const fns[3][2] = {
10277                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10278                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10279                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10280             };
10281             genfn = fns[size][u];
10282             genfn(tcg_rd, cpu_env, tcg_rn);
10283             break;
10284         }
10285         case 0x1a: /* FCVTNS */
10286         case 0x1b: /* FCVTMS */
10287         case 0x1c: /* FCVTAS */
10288         case 0x3a: /* FCVTPS */
10289         case 0x3b: /* FCVTZS */
10290             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10291                                  tcg_fpstatus);
10292             break;
10293         case 0x5a: /* FCVTNU */
10294         case 0x5b: /* FCVTMU */
10295         case 0x5c: /* FCVTAU */
10296         case 0x7a: /* FCVTPU */
10297         case 0x7b: /* FCVTZU */
10298             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10299                                  tcg_fpstatus);
10300             break;
10301         default:
10302             g_assert_not_reached();
10303         }
10304 
10305         write_fp_sreg(s, rd, tcg_rd);
10306     }
10307 
10308     if (is_fcvt) {
10309         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10310     }
10311 }
10312 
10313 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10314 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10315                                  int immh, int immb, int opcode, int rn, int rd)
10316 {
10317     int size = 32 - clz32(immh) - 1;
10318     int immhb = immh << 3 | immb;
10319     int shift = 2 * (8 << size) - immhb;
10320     GVecGen2iFn *gvec_fn;
10321 
10322     if (extract32(immh, 3, 1) && !is_q) {
10323         unallocated_encoding(s);
10324         return;
10325     }
10326     tcg_debug_assert(size <= 3);
10327 
10328     if (!fp_access_check(s)) {
10329         return;
10330     }
10331 
10332     switch (opcode) {
10333     case 0x02: /* SSRA / USRA (accumulate) */
10334         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10335         break;
10336 
10337     case 0x08: /* SRI */
10338         gvec_fn = gen_gvec_sri;
10339         break;
10340 
10341     case 0x00: /* SSHR / USHR */
10342         if (is_u) {
10343             if (shift == 8 << size) {
10344                 /* Shift count the same size as element size produces zero.  */
10345                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10346                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10347                 return;
10348             }
10349             gvec_fn = tcg_gen_gvec_shri;
10350         } else {
10351             /* Shift count the same size as element size produces all sign.  */
10352             if (shift == 8 << size) {
10353                 shift -= 1;
10354             }
10355             gvec_fn = tcg_gen_gvec_sari;
10356         }
10357         break;
10358 
10359     case 0x04: /* SRSHR / URSHR (rounding) */
10360         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10361         break;
10362 
10363     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10364         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10365         break;
10366 
10367     default:
10368         g_assert_not_reached();
10369     }
10370 
10371     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10372 }
10373 
10374 /* SHL/SLI - Vector shift left */
10375 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10376                                  int immh, int immb, int opcode, int rn, int rd)
10377 {
10378     int size = 32 - clz32(immh) - 1;
10379     int immhb = immh << 3 | immb;
10380     int shift = immhb - (8 << size);
10381 
10382     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10383     assert(size >= 0 && size <= 3);
10384 
10385     if (extract32(immh, 3, 1) && !is_q) {
10386         unallocated_encoding(s);
10387         return;
10388     }
10389 
10390     if (!fp_access_check(s)) {
10391         return;
10392     }
10393 
10394     if (insert) {
10395         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10396     } else {
10397         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10398     }
10399 }
10400 
10401 /* USHLL/SHLL - Vector shift left with widening */
10402 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10403                                  int immh, int immb, int opcode, int rn, int rd)
10404 {
10405     int size = 32 - clz32(immh) - 1;
10406     int immhb = immh << 3 | immb;
10407     int shift = immhb - (8 << size);
10408     int dsize = 64;
10409     int esize = 8 << size;
10410     int elements = dsize/esize;
10411     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10412     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10413     int i;
10414 
10415     if (size >= 3) {
10416         unallocated_encoding(s);
10417         return;
10418     }
10419 
10420     if (!fp_access_check(s)) {
10421         return;
10422     }
10423 
10424     /* For the LL variants the store is larger than the load,
10425      * so if rd == rn we would overwrite parts of our input.
10426      * So load everything right now and use shifts in the main loop.
10427      */
10428     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10429 
10430     for (i = 0; i < elements; i++) {
10431         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10432         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10433         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10434         write_vec_element(s, tcg_rd, rd, i, size + 1);
10435     }
10436 }
10437 
10438 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10439 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10440                                  int immh, int immb, int opcode, int rn, int rd)
10441 {
10442     int immhb = immh << 3 | immb;
10443     int size = 32 - clz32(immh) - 1;
10444     int dsize = 64;
10445     int esize = 8 << size;
10446     int elements = dsize/esize;
10447     int shift = (2 * esize) - immhb;
10448     bool round = extract32(opcode, 0, 1);
10449     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10450     TCGv_i64 tcg_round;
10451     int i;
10452 
10453     if (extract32(immh, 3, 1)) {
10454         unallocated_encoding(s);
10455         return;
10456     }
10457 
10458     if (!fp_access_check(s)) {
10459         return;
10460     }
10461 
10462     tcg_rn = tcg_temp_new_i64();
10463     tcg_rd = tcg_temp_new_i64();
10464     tcg_final = tcg_temp_new_i64();
10465     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10466 
10467     if (round) {
10468         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10469     } else {
10470         tcg_round = NULL;
10471     }
10472 
10473     for (i = 0; i < elements; i++) {
10474         read_vec_element(s, tcg_rn, rn, i, size+1);
10475         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10476                                 false, true, size+1, shift);
10477 
10478         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10479     }
10480 
10481     if (!is_q) {
10482         write_vec_element(s, tcg_final, rd, 0, MO_64);
10483     } else {
10484         write_vec_element(s, tcg_final, rd, 1, MO_64);
10485     }
10486 
10487     clear_vec_high(s, is_q, rd);
10488 }
10489 
10490 
10491 /* AdvSIMD shift by immediate
10492  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10493  * +---+---+---+-------------+------+------+--------+---+------+------+
10494  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10495  * +---+---+---+-------------+------+------+--------+---+------+------+
10496  */
10497 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10498 {
10499     int rd = extract32(insn, 0, 5);
10500     int rn = extract32(insn, 5, 5);
10501     int opcode = extract32(insn, 11, 5);
10502     int immb = extract32(insn, 16, 3);
10503     int immh = extract32(insn, 19, 4);
10504     bool is_u = extract32(insn, 29, 1);
10505     bool is_q = extract32(insn, 30, 1);
10506 
10507     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10508     assert(immh != 0);
10509 
10510     switch (opcode) {
10511     case 0x08: /* SRI */
10512         if (!is_u) {
10513             unallocated_encoding(s);
10514             return;
10515         }
10516         /* fall through */
10517     case 0x00: /* SSHR / USHR */
10518     case 0x02: /* SSRA / USRA (accumulate) */
10519     case 0x04: /* SRSHR / URSHR (rounding) */
10520     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10521         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10522         break;
10523     case 0x0a: /* SHL / SLI */
10524         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10525         break;
10526     case 0x10: /* SHRN */
10527     case 0x11: /* RSHRN / SQRSHRUN */
10528         if (is_u) {
10529             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10530                                    opcode, rn, rd);
10531         } else {
10532             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10533         }
10534         break;
10535     case 0x12: /* SQSHRN / UQSHRN */
10536     case 0x13: /* SQRSHRN / UQRSHRN */
10537         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10538                                opcode, rn, rd);
10539         break;
10540     case 0x14: /* SSHLL / USHLL */
10541         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10542         break;
10543     case 0x1c: /* SCVTF / UCVTF */
10544         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10545                                      opcode, rn, rd);
10546         break;
10547     case 0xc: /* SQSHLU */
10548         if (!is_u) {
10549             unallocated_encoding(s);
10550             return;
10551         }
10552         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10553         break;
10554     case 0xe: /* SQSHL, UQSHL */
10555         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10556         break;
10557     case 0x1f: /* FCVTZS/ FCVTZU */
10558         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10559         return;
10560     default:
10561         unallocated_encoding(s);
10562         return;
10563     }
10564 }
10565 
10566 /* Generate code to do a "long" addition or subtraction, ie one done in
10567  * TCGv_i64 on vector lanes twice the width specified by size.
10568  */
10569 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10570                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10571 {
10572     static NeonGenTwo64OpFn * const fns[3][2] = {
10573         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10574         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10575         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10576     };
10577     NeonGenTwo64OpFn *genfn;
10578     assert(size < 3);
10579 
10580     genfn = fns[size][is_sub];
10581     genfn(tcg_res, tcg_op1, tcg_op2);
10582 }
10583 
10584 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10585                                 int opcode, int rd, int rn, int rm)
10586 {
10587     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10588     TCGv_i64 tcg_res[2];
10589     int pass, accop;
10590 
10591     tcg_res[0] = tcg_temp_new_i64();
10592     tcg_res[1] = tcg_temp_new_i64();
10593 
10594     /* Does this op do an adding accumulate, a subtracting accumulate,
10595      * or no accumulate at all?
10596      */
10597     switch (opcode) {
10598     case 5:
10599     case 8:
10600     case 9:
10601         accop = 1;
10602         break;
10603     case 10:
10604     case 11:
10605         accop = -1;
10606         break;
10607     default:
10608         accop = 0;
10609         break;
10610     }
10611 
10612     if (accop != 0) {
10613         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10614         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10615     }
10616 
10617     /* size == 2 means two 32x32->64 operations; this is worth special
10618      * casing because we can generally handle it inline.
10619      */
10620     if (size == 2) {
10621         for (pass = 0; pass < 2; pass++) {
10622             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10623             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10624             TCGv_i64 tcg_passres;
10625             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10626 
10627             int elt = pass + is_q * 2;
10628 
10629             read_vec_element(s, tcg_op1, rn, elt, memop);
10630             read_vec_element(s, tcg_op2, rm, elt, memop);
10631 
10632             if (accop == 0) {
10633                 tcg_passres = tcg_res[pass];
10634             } else {
10635                 tcg_passres = tcg_temp_new_i64();
10636             }
10637 
10638             switch (opcode) {
10639             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10640                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10641                 break;
10642             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10643                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10644                 break;
10645             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10646             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10647             {
10648                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10649                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10650 
10651                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10652                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10653                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10654                                     tcg_passres,
10655                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10656                 break;
10657             }
10658             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10659             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10660             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10661                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10662                 break;
10663             case 9: /* SQDMLAL, SQDMLAL2 */
10664             case 11: /* SQDMLSL, SQDMLSL2 */
10665             case 13: /* SQDMULL, SQDMULL2 */
10666                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10667                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10668                                                   tcg_passres, tcg_passres);
10669                 break;
10670             default:
10671                 g_assert_not_reached();
10672             }
10673 
10674             if (opcode == 9 || opcode == 11) {
10675                 /* saturating accumulate ops */
10676                 if (accop < 0) {
10677                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10678                 }
10679                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10680                                                   tcg_res[pass], tcg_passres);
10681             } else if (accop > 0) {
10682                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10683             } else if (accop < 0) {
10684                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10685             }
10686         }
10687     } else {
10688         /* size 0 or 1, generally helper functions */
10689         for (pass = 0; pass < 2; pass++) {
10690             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10691             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10692             TCGv_i64 tcg_passres;
10693             int elt = pass + is_q * 2;
10694 
10695             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10696             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10697 
10698             if (accop == 0) {
10699                 tcg_passres = tcg_res[pass];
10700             } else {
10701                 tcg_passres = tcg_temp_new_i64();
10702             }
10703 
10704             switch (opcode) {
10705             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10706             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10707             {
10708                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10709                 static NeonGenWidenFn * const widenfns[2][2] = {
10710                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10711                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10712                 };
10713                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10714 
10715                 widenfn(tcg_op2_64, tcg_op2);
10716                 widenfn(tcg_passres, tcg_op1);
10717                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10718                               tcg_passres, tcg_op2_64);
10719                 break;
10720             }
10721             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10722             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10723                 if (size == 0) {
10724                     if (is_u) {
10725                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10726                     } else {
10727                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10728                     }
10729                 } else {
10730                     if (is_u) {
10731                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10732                     } else {
10733                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10734                     }
10735                 }
10736                 break;
10737             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10738             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10739             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10740                 if (size == 0) {
10741                     if (is_u) {
10742                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10743                     } else {
10744                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10745                     }
10746                 } else {
10747                     if (is_u) {
10748                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10749                     } else {
10750                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10751                     }
10752                 }
10753                 break;
10754             case 9: /* SQDMLAL, SQDMLAL2 */
10755             case 11: /* SQDMLSL, SQDMLSL2 */
10756             case 13: /* SQDMULL, SQDMULL2 */
10757                 assert(size == 1);
10758                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10759                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10760                                                   tcg_passres, tcg_passres);
10761                 break;
10762             default:
10763                 g_assert_not_reached();
10764             }
10765 
10766             if (accop != 0) {
10767                 if (opcode == 9 || opcode == 11) {
10768                     /* saturating accumulate ops */
10769                     if (accop < 0) {
10770                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10771                     }
10772                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10773                                                       tcg_res[pass],
10774                                                       tcg_passres);
10775                 } else {
10776                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10777                                   tcg_res[pass], tcg_passres);
10778                 }
10779             }
10780         }
10781     }
10782 
10783     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10784     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10785 }
10786 
10787 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10788                             int opcode, int rd, int rn, int rm)
10789 {
10790     TCGv_i64 tcg_res[2];
10791     int part = is_q ? 2 : 0;
10792     int pass;
10793 
10794     for (pass = 0; pass < 2; pass++) {
10795         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10796         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10797         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10798         static NeonGenWidenFn * const widenfns[3][2] = {
10799             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10800             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10801             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10802         };
10803         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10804 
10805         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10806         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10807         widenfn(tcg_op2_wide, tcg_op2);
10808         tcg_res[pass] = tcg_temp_new_i64();
10809         gen_neon_addl(size, (opcode == 3),
10810                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10811     }
10812 
10813     for (pass = 0; pass < 2; pass++) {
10814         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10815     }
10816 }
10817 
10818 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10819 {
10820     tcg_gen_addi_i64(in, in, 1U << 31);
10821     tcg_gen_extrh_i64_i32(res, in);
10822 }
10823 
10824 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10825                                  int opcode, int rd, int rn, int rm)
10826 {
10827     TCGv_i32 tcg_res[2];
10828     int part = is_q ? 2 : 0;
10829     int pass;
10830 
10831     for (pass = 0; pass < 2; pass++) {
10832         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10833         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10834         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10835         static NeonGenNarrowFn * const narrowfns[3][2] = {
10836             { gen_helper_neon_narrow_high_u8,
10837               gen_helper_neon_narrow_round_high_u8 },
10838             { gen_helper_neon_narrow_high_u16,
10839               gen_helper_neon_narrow_round_high_u16 },
10840             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10841         };
10842         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10843 
10844         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10845         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10846 
10847         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10848 
10849         tcg_res[pass] = tcg_temp_new_i32();
10850         gennarrow(tcg_res[pass], tcg_wideres);
10851     }
10852 
10853     for (pass = 0; pass < 2; pass++) {
10854         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10855     }
10856     clear_vec_high(s, is_q, rd);
10857 }
10858 
10859 /* AdvSIMD three different
10860  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10861  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10862  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10863  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10864  */
10865 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10866 {
10867     /* Instructions in this group fall into three basic classes
10868      * (in each case with the operation working on each element in
10869      * the input vectors):
10870      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10871      *     128 bit input)
10872      * (2) wide 64 x 128 -> 128
10873      * (3) narrowing 128 x 128 -> 64
10874      * Here we do initial decode, catch unallocated cases and
10875      * dispatch to separate functions for each class.
10876      */
10877     int is_q = extract32(insn, 30, 1);
10878     int is_u = extract32(insn, 29, 1);
10879     int size = extract32(insn, 22, 2);
10880     int opcode = extract32(insn, 12, 4);
10881     int rm = extract32(insn, 16, 5);
10882     int rn = extract32(insn, 5, 5);
10883     int rd = extract32(insn, 0, 5);
10884 
10885     switch (opcode) {
10886     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10887     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10888         /* 64 x 128 -> 128 */
10889         if (size == 3) {
10890             unallocated_encoding(s);
10891             return;
10892         }
10893         if (!fp_access_check(s)) {
10894             return;
10895         }
10896         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10897         break;
10898     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10899     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10900         /* 128 x 128 -> 64 */
10901         if (size == 3) {
10902             unallocated_encoding(s);
10903             return;
10904         }
10905         if (!fp_access_check(s)) {
10906             return;
10907         }
10908         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10909         break;
10910     case 14: /* PMULL, PMULL2 */
10911         if (is_u) {
10912             unallocated_encoding(s);
10913             return;
10914         }
10915         switch (size) {
10916         case 0: /* PMULL.P8 */
10917             if (!fp_access_check(s)) {
10918                 return;
10919             }
10920             /* The Q field specifies lo/hi half input for this insn.  */
10921             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10922                              gen_helper_neon_pmull_h);
10923             break;
10924 
10925         case 3: /* PMULL.P64 */
10926             if (!dc_isar_feature(aa64_pmull, s)) {
10927                 unallocated_encoding(s);
10928                 return;
10929             }
10930             if (!fp_access_check(s)) {
10931                 return;
10932             }
10933             /* The Q field specifies lo/hi half input for this insn.  */
10934             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10935                              gen_helper_gvec_pmull_q);
10936             break;
10937 
10938         default:
10939             unallocated_encoding(s);
10940             break;
10941         }
10942         return;
10943     case 9: /* SQDMLAL, SQDMLAL2 */
10944     case 11: /* SQDMLSL, SQDMLSL2 */
10945     case 13: /* SQDMULL, SQDMULL2 */
10946         if (is_u || size == 0) {
10947             unallocated_encoding(s);
10948             return;
10949         }
10950         /* fall through */
10951     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10952     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10953     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10954     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10955     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10956     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10957     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10958         /* 64 x 64 -> 128 */
10959         if (size == 3) {
10960             unallocated_encoding(s);
10961             return;
10962         }
10963         if (!fp_access_check(s)) {
10964             return;
10965         }
10966 
10967         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10968         break;
10969     default:
10970         /* opcode 15 not allocated */
10971         unallocated_encoding(s);
10972         break;
10973     }
10974 }
10975 
10976 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10977 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10978 {
10979     int rd = extract32(insn, 0, 5);
10980     int rn = extract32(insn, 5, 5);
10981     int rm = extract32(insn, 16, 5);
10982     int size = extract32(insn, 22, 2);
10983     bool is_u = extract32(insn, 29, 1);
10984     bool is_q = extract32(insn, 30, 1);
10985 
10986     if (!fp_access_check(s)) {
10987         return;
10988     }
10989 
10990     switch (size + 4 * is_u) {
10991     case 0: /* AND */
10992         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10993         return;
10994     case 1: /* BIC */
10995         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10996         return;
10997     case 2: /* ORR */
10998         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10999         return;
11000     case 3: /* ORN */
11001         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
11002         return;
11003     case 4: /* EOR */
11004         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
11005         return;
11006 
11007     case 5: /* BSL bitwise select */
11008         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
11009         return;
11010     case 6: /* BIT, bitwise insert if true */
11011         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
11012         return;
11013     case 7: /* BIF, bitwise insert if false */
11014         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11015         return;
11016 
11017     default:
11018         g_assert_not_reached();
11019     }
11020 }
11021 
11022 /* Pairwise op subgroup of C3.6.16.
11023  *
11024  * This is called directly or via the handle_3same_float for float pairwise
11025  * operations where the opcode and size are calculated differently.
11026  */
11027 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11028                                    int size, int rn, int rm, int rd)
11029 {
11030     TCGv_ptr fpst;
11031     int pass;
11032 
11033     /* Floating point operations need fpst */
11034     if (opcode >= 0x58) {
11035         fpst = fpstatus_ptr(FPST_FPCR);
11036     } else {
11037         fpst = NULL;
11038     }
11039 
11040     if (!fp_access_check(s)) {
11041         return;
11042     }
11043 
11044     /* These operations work on the concatenated rm:rn, with each pair of
11045      * adjacent elements being operated on to produce an element in the result.
11046      */
11047     if (size == 3) {
11048         TCGv_i64 tcg_res[2];
11049 
11050         for (pass = 0; pass < 2; pass++) {
11051             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11052             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11053             int passreg = (pass == 0) ? rn : rm;
11054 
11055             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11056             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11057             tcg_res[pass] = tcg_temp_new_i64();
11058 
11059             switch (opcode) {
11060             case 0x17: /* ADDP */
11061                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11062                 break;
11063             case 0x58: /* FMAXNMP */
11064                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11065                 break;
11066             case 0x5a: /* FADDP */
11067                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11068                 break;
11069             case 0x5e: /* FMAXP */
11070                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11071                 break;
11072             case 0x78: /* FMINNMP */
11073                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11074                 break;
11075             case 0x7e: /* FMINP */
11076                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11077                 break;
11078             default:
11079                 g_assert_not_reached();
11080             }
11081         }
11082 
11083         for (pass = 0; pass < 2; pass++) {
11084             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11085         }
11086     } else {
11087         int maxpass = is_q ? 4 : 2;
11088         TCGv_i32 tcg_res[4];
11089 
11090         for (pass = 0; pass < maxpass; pass++) {
11091             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11092             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11093             NeonGenTwoOpFn *genfn = NULL;
11094             int passreg = pass < (maxpass / 2) ? rn : rm;
11095             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11096 
11097             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11098             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11099             tcg_res[pass] = tcg_temp_new_i32();
11100 
11101             switch (opcode) {
11102             case 0x17: /* ADDP */
11103             {
11104                 static NeonGenTwoOpFn * const fns[3] = {
11105                     gen_helper_neon_padd_u8,
11106                     gen_helper_neon_padd_u16,
11107                     tcg_gen_add_i32,
11108                 };
11109                 genfn = fns[size];
11110                 break;
11111             }
11112             case 0x14: /* SMAXP, UMAXP */
11113             {
11114                 static NeonGenTwoOpFn * const fns[3][2] = {
11115                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11116                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11117                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11118                 };
11119                 genfn = fns[size][u];
11120                 break;
11121             }
11122             case 0x15: /* SMINP, UMINP */
11123             {
11124                 static NeonGenTwoOpFn * const fns[3][2] = {
11125                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11126                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11127                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11128                 };
11129                 genfn = fns[size][u];
11130                 break;
11131             }
11132             /* The FP operations are all on single floats (32 bit) */
11133             case 0x58: /* FMAXNMP */
11134                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11135                 break;
11136             case 0x5a: /* FADDP */
11137                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11138                 break;
11139             case 0x5e: /* FMAXP */
11140                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11141                 break;
11142             case 0x78: /* FMINNMP */
11143                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11144                 break;
11145             case 0x7e: /* FMINP */
11146                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11147                 break;
11148             default:
11149                 g_assert_not_reached();
11150             }
11151 
11152             /* FP ops called directly, otherwise call now */
11153             if (genfn) {
11154                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11155             }
11156         }
11157 
11158         for (pass = 0; pass < maxpass; pass++) {
11159             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11160         }
11161         clear_vec_high(s, is_q, rd);
11162     }
11163 }
11164 
11165 /* Floating point op subgroup of C3.6.16. */
11166 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11167 {
11168     /* For floating point ops, the U, size[1] and opcode bits
11169      * together indicate the operation. size[0] indicates single
11170      * or double.
11171      */
11172     int fpopcode = extract32(insn, 11, 5)
11173         | (extract32(insn, 23, 1) << 5)
11174         | (extract32(insn, 29, 1) << 6);
11175     int is_q = extract32(insn, 30, 1);
11176     int size = extract32(insn, 22, 1);
11177     int rm = extract32(insn, 16, 5);
11178     int rn = extract32(insn, 5, 5);
11179     int rd = extract32(insn, 0, 5);
11180 
11181     int datasize = is_q ? 128 : 64;
11182     int esize = 32 << size;
11183     int elements = datasize / esize;
11184 
11185     if (size == 1 && !is_q) {
11186         unallocated_encoding(s);
11187         return;
11188     }
11189 
11190     switch (fpopcode) {
11191     case 0x58: /* FMAXNMP */
11192     case 0x5a: /* FADDP */
11193     case 0x5e: /* FMAXP */
11194     case 0x78: /* FMINNMP */
11195     case 0x7e: /* FMINP */
11196         if (size && !is_q) {
11197             unallocated_encoding(s);
11198             return;
11199         }
11200         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11201                                rn, rm, rd);
11202         return;
11203     case 0x1b: /* FMULX */
11204     case 0x1f: /* FRECPS */
11205     case 0x3f: /* FRSQRTS */
11206     case 0x5d: /* FACGE */
11207     case 0x7d: /* FACGT */
11208     case 0x19: /* FMLA */
11209     case 0x39: /* FMLS */
11210     case 0x18: /* FMAXNM */
11211     case 0x1a: /* FADD */
11212     case 0x1c: /* FCMEQ */
11213     case 0x1e: /* FMAX */
11214     case 0x38: /* FMINNM */
11215     case 0x3a: /* FSUB */
11216     case 0x3e: /* FMIN */
11217     case 0x5b: /* FMUL */
11218     case 0x5c: /* FCMGE */
11219     case 0x5f: /* FDIV */
11220     case 0x7a: /* FABD */
11221     case 0x7c: /* FCMGT */
11222         if (!fp_access_check(s)) {
11223             return;
11224         }
11225         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11226         return;
11227 
11228     case 0x1d: /* FMLAL  */
11229     case 0x3d: /* FMLSL  */
11230     case 0x59: /* FMLAL2 */
11231     case 0x79: /* FMLSL2 */
11232         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11233             unallocated_encoding(s);
11234             return;
11235         }
11236         if (fp_access_check(s)) {
11237             int is_s = extract32(insn, 23, 1);
11238             int is_2 = extract32(insn, 29, 1);
11239             int data = (is_2 << 1) | is_s;
11240             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11241                                vec_full_reg_offset(s, rn),
11242                                vec_full_reg_offset(s, rm), cpu_env,
11243                                is_q ? 16 : 8, vec_full_reg_size(s),
11244                                data, gen_helper_gvec_fmlal_a64);
11245         }
11246         return;
11247 
11248     default:
11249         unallocated_encoding(s);
11250         return;
11251     }
11252 }
11253 
11254 /* Integer op subgroup of C3.6.16. */
11255 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11256 {
11257     int is_q = extract32(insn, 30, 1);
11258     int u = extract32(insn, 29, 1);
11259     int size = extract32(insn, 22, 2);
11260     int opcode = extract32(insn, 11, 5);
11261     int rm = extract32(insn, 16, 5);
11262     int rn = extract32(insn, 5, 5);
11263     int rd = extract32(insn, 0, 5);
11264     int pass;
11265     TCGCond cond;
11266 
11267     switch (opcode) {
11268     case 0x13: /* MUL, PMUL */
11269         if (u && size != 0) {
11270             unallocated_encoding(s);
11271             return;
11272         }
11273         /* fall through */
11274     case 0x0: /* SHADD, UHADD */
11275     case 0x2: /* SRHADD, URHADD */
11276     case 0x4: /* SHSUB, UHSUB */
11277     case 0xc: /* SMAX, UMAX */
11278     case 0xd: /* SMIN, UMIN */
11279     case 0xe: /* SABD, UABD */
11280     case 0xf: /* SABA, UABA */
11281     case 0x12: /* MLA, MLS */
11282         if (size == 3) {
11283             unallocated_encoding(s);
11284             return;
11285         }
11286         break;
11287     case 0x16: /* SQDMULH, SQRDMULH */
11288         if (size == 0 || size == 3) {
11289             unallocated_encoding(s);
11290             return;
11291         }
11292         break;
11293     default:
11294         if (size == 3 && !is_q) {
11295             unallocated_encoding(s);
11296             return;
11297         }
11298         break;
11299     }
11300 
11301     if (!fp_access_check(s)) {
11302         return;
11303     }
11304 
11305     switch (opcode) {
11306     case 0x01: /* SQADD, UQADD */
11307         if (u) {
11308             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11309         } else {
11310             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11311         }
11312         return;
11313     case 0x05: /* SQSUB, UQSUB */
11314         if (u) {
11315             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11316         } else {
11317             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11318         }
11319         return;
11320     case 0x08: /* SSHL, USHL */
11321         if (u) {
11322             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11323         } else {
11324             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11325         }
11326         return;
11327     case 0x0c: /* SMAX, UMAX */
11328         if (u) {
11329             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11330         } else {
11331             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11332         }
11333         return;
11334     case 0x0d: /* SMIN, UMIN */
11335         if (u) {
11336             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11337         } else {
11338             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11339         }
11340         return;
11341     case 0xe: /* SABD, UABD */
11342         if (u) {
11343             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11344         } else {
11345             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11346         }
11347         return;
11348     case 0xf: /* SABA, UABA */
11349         if (u) {
11350             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11351         } else {
11352             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11353         }
11354         return;
11355     case 0x10: /* ADD, SUB */
11356         if (u) {
11357             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11358         } else {
11359             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11360         }
11361         return;
11362     case 0x13: /* MUL, PMUL */
11363         if (!u) { /* MUL */
11364             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11365         } else {  /* PMUL */
11366             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11367         }
11368         return;
11369     case 0x12: /* MLA, MLS */
11370         if (u) {
11371             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11372         } else {
11373             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11374         }
11375         return;
11376     case 0x16: /* SQDMULH, SQRDMULH */
11377         {
11378             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11379                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11380                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11381             };
11382             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11383         }
11384         return;
11385     case 0x11:
11386         if (!u) { /* CMTST */
11387             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11388             return;
11389         }
11390         /* else CMEQ */
11391         cond = TCG_COND_EQ;
11392         goto do_gvec_cmp;
11393     case 0x06: /* CMGT, CMHI */
11394         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11395         goto do_gvec_cmp;
11396     case 0x07: /* CMGE, CMHS */
11397         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11398     do_gvec_cmp:
11399         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11400                          vec_full_reg_offset(s, rn),
11401                          vec_full_reg_offset(s, rm),
11402                          is_q ? 16 : 8, vec_full_reg_size(s));
11403         return;
11404     }
11405 
11406     if (size == 3) {
11407         assert(is_q);
11408         for (pass = 0; pass < 2; pass++) {
11409             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11410             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11411             TCGv_i64 tcg_res = tcg_temp_new_i64();
11412 
11413             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11414             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11415 
11416             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11417 
11418             write_vec_element(s, tcg_res, rd, pass, MO_64);
11419         }
11420     } else {
11421         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11422             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11423             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11424             TCGv_i32 tcg_res = tcg_temp_new_i32();
11425             NeonGenTwoOpFn *genfn = NULL;
11426             NeonGenTwoOpEnvFn *genenvfn = NULL;
11427 
11428             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11429             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11430 
11431             switch (opcode) {
11432             case 0x0: /* SHADD, UHADD */
11433             {
11434                 static NeonGenTwoOpFn * const fns[3][2] = {
11435                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11436                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11437                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11438                 };
11439                 genfn = fns[size][u];
11440                 break;
11441             }
11442             case 0x2: /* SRHADD, URHADD */
11443             {
11444                 static NeonGenTwoOpFn * const fns[3][2] = {
11445                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11446                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11447                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11448                 };
11449                 genfn = fns[size][u];
11450                 break;
11451             }
11452             case 0x4: /* SHSUB, UHSUB */
11453             {
11454                 static NeonGenTwoOpFn * const fns[3][2] = {
11455                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11456                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11457                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11458                 };
11459                 genfn = fns[size][u];
11460                 break;
11461             }
11462             case 0x9: /* SQSHL, UQSHL */
11463             {
11464                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11465                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11466                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11467                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11468                 };
11469                 genenvfn = fns[size][u];
11470                 break;
11471             }
11472             case 0xa: /* SRSHL, URSHL */
11473             {
11474                 static NeonGenTwoOpFn * const fns[3][2] = {
11475                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11476                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11477                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11478                 };
11479                 genfn = fns[size][u];
11480                 break;
11481             }
11482             case 0xb: /* SQRSHL, UQRSHL */
11483             {
11484                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11485                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11486                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11487                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11488                 };
11489                 genenvfn = fns[size][u];
11490                 break;
11491             }
11492             default:
11493                 g_assert_not_reached();
11494             }
11495 
11496             if (genenvfn) {
11497                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11498             } else {
11499                 genfn(tcg_res, tcg_op1, tcg_op2);
11500             }
11501 
11502             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11503         }
11504     }
11505     clear_vec_high(s, is_q, rd);
11506 }
11507 
11508 /* AdvSIMD three same
11509  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11510  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11511  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11512  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11513  */
11514 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11515 {
11516     int opcode = extract32(insn, 11, 5);
11517 
11518     switch (opcode) {
11519     case 0x3: /* logic ops */
11520         disas_simd_3same_logic(s, insn);
11521         break;
11522     case 0x17: /* ADDP */
11523     case 0x14: /* SMAXP, UMAXP */
11524     case 0x15: /* SMINP, UMINP */
11525     {
11526         /* Pairwise operations */
11527         int is_q = extract32(insn, 30, 1);
11528         int u = extract32(insn, 29, 1);
11529         int size = extract32(insn, 22, 2);
11530         int rm = extract32(insn, 16, 5);
11531         int rn = extract32(insn, 5, 5);
11532         int rd = extract32(insn, 0, 5);
11533         if (opcode == 0x17) {
11534             if (u || (size == 3 && !is_q)) {
11535                 unallocated_encoding(s);
11536                 return;
11537             }
11538         } else {
11539             if (size == 3) {
11540                 unallocated_encoding(s);
11541                 return;
11542             }
11543         }
11544         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11545         break;
11546     }
11547     case 0x18 ... 0x31:
11548         /* floating point ops, sz[1] and U are part of opcode */
11549         disas_simd_3same_float(s, insn);
11550         break;
11551     default:
11552         disas_simd_3same_int(s, insn);
11553         break;
11554     }
11555 }
11556 
11557 /*
11558  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11559  *
11560  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11561  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11562  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11563  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11564  *
11565  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11566  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11567  *
11568  */
11569 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11570 {
11571     int opcode = extract32(insn, 11, 3);
11572     int u = extract32(insn, 29, 1);
11573     int a = extract32(insn, 23, 1);
11574     int is_q = extract32(insn, 30, 1);
11575     int rm = extract32(insn, 16, 5);
11576     int rn = extract32(insn, 5, 5);
11577     int rd = extract32(insn, 0, 5);
11578     /*
11579      * For these floating point ops, the U, a and opcode bits
11580      * together indicate the operation.
11581      */
11582     int fpopcode = opcode | (a << 3) | (u << 4);
11583     int datasize = is_q ? 128 : 64;
11584     int elements = datasize / 16;
11585     bool pairwise;
11586     TCGv_ptr fpst;
11587     int pass;
11588 
11589     switch (fpopcode) {
11590     case 0x0: /* FMAXNM */
11591     case 0x1: /* FMLA */
11592     case 0x2: /* FADD */
11593     case 0x3: /* FMULX */
11594     case 0x4: /* FCMEQ */
11595     case 0x6: /* FMAX */
11596     case 0x7: /* FRECPS */
11597     case 0x8: /* FMINNM */
11598     case 0x9: /* FMLS */
11599     case 0xa: /* FSUB */
11600     case 0xe: /* FMIN */
11601     case 0xf: /* FRSQRTS */
11602     case 0x13: /* FMUL */
11603     case 0x14: /* FCMGE */
11604     case 0x15: /* FACGE */
11605     case 0x17: /* FDIV */
11606     case 0x1a: /* FABD */
11607     case 0x1c: /* FCMGT */
11608     case 0x1d: /* FACGT */
11609         pairwise = false;
11610         break;
11611     case 0x10: /* FMAXNMP */
11612     case 0x12: /* FADDP */
11613     case 0x16: /* FMAXP */
11614     case 0x18: /* FMINNMP */
11615     case 0x1e: /* FMINP */
11616         pairwise = true;
11617         break;
11618     default:
11619         unallocated_encoding(s);
11620         return;
11621     }
11622 
11623     if (!dc_isar_feature(aa64_fp16, s)) {
11624         unallocated_encoding(s);
11625         return;
11626     }
11627 
11628     if (!fp_access_check(s)) {
11629         return;
11630     }
11631 
11632     fpst = fpstatus_ptr(FPST_FPCR_F16);
11633 
11634     if (pairwise) {
11635         int maxpass = is_q ? 8 : 4;
11636         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11637         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11638         TCGv_i32 tcg_res[8];
11639 
11640         for (pass = 0; pass < maxpass; pass++) {
11641             int passreg = pass < (maxpass / 2) ? rn : rm;
11642             int passelt = (pass << 1) & (maxpass - 1);
11643 
11644             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11645             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11646             tcg_res[pass] = tcg_temp_new_i32();
11647 
11648             switch (fpopcode) {
11649             case 0x10: /* FMAXNMP */
11650                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11651                                            fpst);
11652                 break;
11653             case 0x12: /* FADDP */
11654                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11655                 break;
11656             case 0x16: /* FMAXP */
11657                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11658                 break;
11659             case 0x18: /* FMINNMP */
11660                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11661                                            fpst);
11662                 break;
11663             case 0x1e: /* FMINP */
11664                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11665                 break;
11666             default:
11667                 g_assert_not_reached();
11668             }
11669         }
11670 
11671         for (pass = 0; pass < maxpass; pass++) {
11672             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11673         }
11674     } else {
11675         for (pass = 0; pass < elements; pass++) {
11676             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11677             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11678             TCGv_i32 tcg_res = tcg_temp_new_i32();
11679 
11680             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11681             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11682 
11683             switch (fpopcode) {
11684             case 0x0: /* FMAXNM */
11685                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11686                 break;
11687             case 0x1: /* FMLA */
11688                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11689                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11690                                            fpst);
11691                 break;
11692             case 0x2: /* FADD */
11693                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11694                 break;
11695             case 0x3: /* FMULX */
11696                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11697                 break;
11698             case 0x4: /* FCMEQ */
11699                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11700                 break;
11701             case 0x6: /* FMAX */
11702                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11703                 break;
11704             case 0x7: /* FRECPS */
11705                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11706                 break;
11707             case 0x8: /* FMINNM */
11708                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11709                 break;
11710             case 0x9: /* FMLS */
11711                 /* As usual for ARM, separate negation for fused multiply-add */
11712                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11713                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11714                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11715                                            fpst);
11716                 break;
11717             case 0xa: /* FSUB */
11718                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11719                 break;
11720             case 0xe: /* FMIN */
11721                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11722                 break;
11723             case 0xf: /* FRSQRTS */
11724                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11725                 break;
11726             case 0x13: /* FMUL */
11727                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11728                 break;
11729             case 0x14: /* FCMGE */
11730                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11731                 break;
11732             case 0x15: /* FACGE */
11733                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11734                 break;
11735             case 0x17: /* FDIV */
11736                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11737                 break;
11738             case 0x1a: /* FABD */
11739                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11740                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11741                 break;
11742             case 0x1c: /* FCMGT */
11743                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11744                 break;
11745             case 0x1d: /* FACGT */
11746                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11747                 break;
11748             default:
11749                 g_assert_not_reached();
11750             }
11751 
11752             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11753         }
11754     }
11755 
11756     clear_vec_high(s, is_q, rd);
11757 }
11758 
11759 /* AdvSIMD three same extra
11760  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11761  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11762  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11763  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11764  */
11765 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11766 {
11767     int rd = extract32(insn, 0, 5);
11768     int rn = extract32(insn, 5, 5);
11769     int opcode = extract32(insn, 11, 4);
11770     int rm = extract32(insn, 16, 5);
11771     int size = extract32(insn, 22, 2);
11772     bool u = extract32(insn, 29, 1);
11773     bool is_q = extract32(insn, 30, 1);
11774     bool feature;
11775     int rot;
11776 
11777     switch (u * 16 + opcode) {
11778     case 0x10: /* SQRDMLAH (vector) */
11779     case 0x11: /* SQRDMLSH (vector) */
11780         if (size != 1 && size != 2) {
11781             unallocated_encoding(s);
11782             return;
11783         }
11784         feature = dc_isar_feature(aa64_rdm, s);
11785         break;
11786     case 0x02: /* SDOT (vector) */
11787     case 0x12: /* UDOT (vector) */
11788         if (size != MO_32) {
11789             unallocated_encoding(s);
11790             return;
11791         }
11792         feature = dc_isar_feature(aa64_dp, s);
11793         break;
11794     case 0x03: /* USDOT */
11795         if (size != MO_32) {
11796             unallocated_encoding(s);
11797             return;
11798         }
11799         feature = dc_isar_feature(aa64_i8mm, s);
11800         break;
11801     case 0x04: /* SMMLA */
11802     case 0x14: /* UMMLA */
11803     case 0x05: /* USMMLA */
11804         if (!is_q || size != MO_32) {
11805             unallocated_encoding(s);
11806             return;
11807         }
11808         feature = dc_isar_feature(aa64_i8mm, s);
11809         break;
11810     case 0x18: /* FCMLA, #0 */
11811     case 0x19: /* FCMLA, #90 */
11812     case 0x1a: /* FCMLA, #180 */
11813     case 0x1b: /* FCMLA, #270 */
11814     case 0x1c: /* FCADD, #90 */
11815     case 0x1e: /* FCADD, #270 */
11816         if (size == 0
11817             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11818             || (size == 3 && !is_q)) {
11819             unallocated_encoding(s);
11820             return;
11821         }
11822         feature = dc_isar_feature(aa64_fcma, s);
11823         break;
11824     case 0x1d: /* BFMMLA */
11825         if (size != MO_16 || !is_q) {
11826             unallocated_encoding(s);
11827             return;
11828         }
11829         feature = dc_isar_feature(aa64_bf16, s);
11830         break;
11831     case 0x1f:
11832         switch (size) {
11833         case 1: /* BFDOT */
11834         case 3: /* BFMLAL{B,T} */
11835             feature = dc_isar_feature(aa64_bf16, s);
11836             break;
11837         default:
11838             unallocated_encoding(s);
11839             return;
11840         }
11841         break;
11842     default:
11843         unallocated_encoding(s);
11844         return;
11845     }
11846     if (!feature) {
11847         unallocated_encoding(s);
11848         return;
11849     }
11850     if (!fp_access_check(s)) {
11851         return;
11852     }
11853 
11854     switch (opcode) {
11855     case 0x0: /* SQRDMLAH (vector) */
11856         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11857         return;
11858 
11859     case 0x1: /* SQRDMLSH (vector) */
11860         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11861         return;
11862 
11863     case 0x2: /* SDOT / UDOT */
11864         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11865                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11866         return;
11867 
11868     case 0x3: /* USDOT */
11869         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11870         return;
11871 
11872     case 0x04: /* SMMLA, UMMLA */
11873         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11874                          u ? gen_helper_gvec_ummla_b
11875                          : gen_helper_gvec_smmla_b);
11876         return;
11877     case 0x05: /* USMMLA */
11878         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11879         return;
11880 
11881     case 0x8: /* FCMLA, #0 */
11882     case 0x9: /* FCMLA, #90 */
11883     case 0xa: /* FCMLA, #180 */
11884     case 0xb: /* FCMLA, #270 */
11885         rot = extract32(opcode, 0, 2);
11886         switch (size) {
11887         case 1:
11888             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11889                               gen_helper_gvec_fcmlah);
11890             break;
11891         case 2:
11892             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11893                               gen_helper_gvec_fcmlas);
11894             break;
11895         case 3:
11896             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11897                               gen_helper_gvec_fcmlad);
11898             break;
11899         default:
11900             g_assert_not_reached();
11901         }
11902         return;
11903 
11904     case 0xc: /* FCADD, #90 */
11905     case 0xe: /* FCADD, #270 */
11906         rot = extract32(opcode, 1, 1);
11907         switch (size) {
11908         case 1:
11909             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11910                               gen_helper_gvec_fcaddh);
11911             break;
11912         case 2:
11913             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11914                               gen_helper_gvec_fcadds);
11915             break;
11916         case 3:
11917             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11918                               gen_helper_gvec_fcaddd);
11919             break;
11920         default:
11921             g_assert_not_reached();
11922         }
11923         return;
11924 
11925     case 0xd: /* BFMMLA */
11926         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11927         return;
11928     case 0xf:
11929         switch (size) {
11930         case 1: /* BFDOT */
11931             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11932             break;
11933         case 3: /* BFMLAL{B,T} */
11934             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11935                               gen_helper_gvec_bfmlal);
11936             break;
11937         default:
11938             g_assert_not_reached();
11939         }
11940         return;
11941 
11942     default:
11943         g_assert_not_reached();
11944     }
11945 }
11946 
11947 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11948                                   int size, int rn, int rd)
11949 {
11950     /* Handle 2-reg-misc ops which are widening (so each size element
11951      * in the source becomes a 2*size element in the destination.
11952      * The only instruction like this is FCVTL.
11953      */
11954     int pass;
11955 
11956     if (size == 3) {
11957         /* 32 -> 64 bit fp conversion */
11958         TCGv_i64 tcg_res[2];
11959         int srcelt = is_q ? 2 : 0;
11960 
11961         for (pass = 0; pass < 2; pass++) {
11962             TCGv_i32 tcg_op = tcg_temp_new_i32();
11963             tcg_res[pass] = tcg_temp_new_i64();
11964 
11965             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11966             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11967         }
11968         for (pass = 0; pass < 2; pass++) {
11969             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11970         }
11971     } else {
11972         /* 16 -> 32 bit fp conversion */
11973         int srcelt = is_q ? 4 : 0;
11974         TCGv_i32 tcg_res[4];
11975         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11976         TCGv_i32 ahp = get_ahp_flag();
11977 
11978         for (pass = 0; pass < 4; pass++) {
11979             tcg_res[pass] = tcg_temp_new_i32();
11980 
11981             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11982             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11983                                            fpst, ahp);
11984         }
11985         for (pass = 0; pass < 4; pass++) {
11986             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11987         }
11988     }
11989 }
11990 
11991 static void handle_rev(DisasContext *s, int opcode, bool u,
11992                        bool is_q, int size, int rn, int rd)
11993 {
11994     int op = (opcode << 1) | u;
11995     int opsz = op + size;
11996     int grp_size = 3 - opsz;
11997     int dsize = is_q ? 128 : 64;
11998     int i;
11999 
12000     if (opsz >= 3) {
12001         unallocated_encoding(s);
12002         return;
12003     }
12004 
12005     if (!fp_access_check(s)) {
12006         return;
12007     }
12008 
12009     if (size == 0) {
12010         /* Special case bytes, use bswap op on each group of elements */
12011         int groups = dsize / (8 << grp_size);
12012 
12013         for (i = 0; i < groups; i++) {
12014             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12015 
12016             read_vec_element(s, tcg_tmp, rn, i, grp_size);
12017             switch (grp_size) {
12018             case MO_16:
12019                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12020                 break;
12021             case MO_32:
12022                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12023                 break;
12024             case MO_64:
12025                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12026                 break;
12027             default:
12028                 g_assert_not_reached();
12029             }
12030             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12031         }
12032         clear_vec_high(s, is_q, rd);
12033     } else {
12034         int revmask = (1 << grp_size) - 1;
12035         int esize = 8 << size;
12036         int elements = dsize / esize;
12037         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12038         TCGv_i64 tcg_rd[2];
12039 
12040         for (i = 0; i < 2; i++) {
12041             tcg_rd[i] = tcg_temp_new_i64();
12042             tcg_gen_movi_i64(tcg_rd[i], 0);
12043         }
12044 
12045         for (i = 0; i < elements; i++) {
12046             int e_rev = (i & 0xf) ^ revmask;
12047             int w = (e_rev * esize) / 64;
12048             int o = (e_rev * esize) % 64;
12049 
12050             read_vec_element(s, tcg_rn, rn, i, size);
12051             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12052         }
12053 
12054         for (i = 0; i < 2; i++) {
12055             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12056         }
12057         clear_vec_high(s, true, rd);
12058     }
12059 }
12060 
12061 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12062                                   bool is_q, int size, int rn, int rd)
12063 {
12064     /* Implement the pairwise operations from 2-misc:
12065      * SADDLP, UADDLP, SADALP, UADALP.
12066      * These all add pairs of elements in the input to produce a
12067      * double-width result element in the output (possibly accumulating).
12068      */
12069     bool accum = (opcode == 0x6);
12070     int maxpass = is_q ? 2 : 1;
12071     int pass;
12072     TCGv_i64 tcg_res[2];
12073 
12074     if (size == 2) {
12075         /* 32 + 32 -> 64 op */
12076         MemOp memop = size + (u ? 0 : MO_SIGN);
12077 
12078         for (pass = 0; pass < maxpass; pass++) {
12079             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12080             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12081 
12082             tcg_res[pass] = tcg_temp_new_i64();
12083 
12084             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12085             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12086             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12087             if (accum) {
12088                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12089                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12090             }
12091         }
12092     } else {
12093         for (pass = 0; pass < maxpass; pass++) {
12094             TCGv_i64 tcg_op = tcg_temp_new_i64();
12095             NeonGenOne64OpFn *genfn;
12096             static NeonGenOne64OpFn * const fns[2][2] = {
12097                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12098                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12099             };
12100 
12101             genfn = fns[size][u];
12102 
12103             tcg_res[pass] = tcg_temp_new_i64();
12104 
12105             read_vec_element(s, tcg_op, rn, pass, MO_64);
12106             genfn(tcg_res[pass], tcg_op);
12107 
12108             if (accum) {
12109                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12110                 if (size == 0) {
12111                     gen_helper_neon_addl_u16(tcg_res[pass],
12112                                              tcg_res[pass], tcg_op);
12113                 } else {
12114                     gen_helper_neon_addl_u32(tcg_res[pass],
12115                                              tcg_res[pass], tcg_op);
12116                 }
12117             }
12118         }
12119     }
12120     if (!is_q) {
12121         tcg_res[1] = tcg_constant_i64(0);
12122     }
12123     for (pass = 0; pass < 2; pass++) {
12124         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12125     }
12126 }
12127 
12128 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12129 {
12130     /* Implement SHLL and SHLL2 */
12131     int pass;
12132     int part = is_q ? 2 : 0;
12133     TCGv_i64 tcg_res[2];
12134 
12135     for (pass = 0; pass < 2; pass++) {
12136         static NeonGenWidenFn * const widenfns[3] = {
12137             gen_helper_neon_widen_u8,
12138             gen_helper_neon_widen_u16,
12139             tcg_gen_extu_i32_i64,
12140         };
12141         NeonGenWidenFn *widenfn = widenfns[size];
12142         TCGv_i32 tcg_op = tcg_temp_new_i32();
12143 
12144         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12145         tcg_res[pass] = tcg_temp_new_i64();
12146         widenfn(tcg_res[pass], tcg_op);
12147         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12148     }
12149 
12150     for (pass = 0; pass < 2; pass++) {
12151         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12152     }
12153 }
12154 
12155 /* AdvSIMD two reg misc
12156  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12157  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12158  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12159  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12160  */
12161 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12162 {
12163     int size = extract32(insn, 22, 2);
12164     int opcode = extract32(insn, 12, 5);
12165     bool u = extract32(insn, 29, 1);
12166     bool is_q = extract32(insn, 30, 1);
12167     int rn = extract32(insn, 5, 5);
12168     int rd = extract32(insn, 0, 5);
12169     bool need_fpstatus = false;
12170     int rmode = -1;
12171     TCGv_i32 tcg_rmode;
12172     TCGv_ptr tcg_fpstatus;
12173 
12174     switch (opcode) {
12175     case 0x0: /* REV64, REV32 */
12176     case 0x1: /* REV16 */
12177         handle_rev(s, opcode, u, is_q, size, rn, rd);
12178         return;
12179     case 0x5: /* CNT, NOT, RBIT */
12180         if (u && size == 0) {
12181             /* NOT */
12182             break;
12183         } else if (u && size == 1) {
12184             /* RBIT */
12185             break;
12186         } else if (!u && size == 0) {
12187             /* CNT */
12188             break;
12189         }
12190         unallocated_encoding(s);
12191         return;
12192     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12193     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12194         if (size == 3) {
12195             unallocated_encoding(s);
12196             return;
12197         }
12198         if (!fp_access_check(s)) {
12199             return;
12200         }
12201 
12202         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12203         return;
12204     case 0x4: /* CLS, CLZ */
12205         if (size == 3) {
12206             unallocated_encoding(s);
12207             return;
12208         }
12209         break;
12210     case 0x2: /* SADDLP, UADDLP */
12211     case 0x6: /* SADALP, UADALP */
12212         if (size == 3) {
12213             unallocated_encoding(s);
12214             return;
12215         }
12216         if (!fp_access_check(s)) {
12217             return;
12218         }
12219         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12220         return;
12221     case 0x13: /* SHLL, SHLL2 */
12222         if (u == 0 || size == 3) {
12223             unallocated_encoding(s);
12224             return;
12225         }
12226         if (!fp_access_check(s)) {
12227             return;
12228         }
12229         handle_shll(s, is_q, size, rn, rd);
12230         return;
12231     case 0xa: /* CMLT */
12232         if (u == 1) {
12233             unallocated_encoding(s);
12234             return;
12235         }
12236         /* fall through */
12237     case 0x8: /* CMGT, CMGE */
12238     case 0x9: /* CMEQ, CMLE */
12239     case 0xb: /* ABS, NEG */
12240         if (size == 3 && !is_q) {
12241             unallocated_encoding(s);
12242             return;
12243         }
12244         break;
12245     case 0x3: /* SUQADD, USQADD */
12246         if (size == 3 && !is_q) {
12247             unallocated_encoding(s);
12248             return;
12249         }
12250         if (!fp_access_check(s)) {
12251             return;
12252         }
12253         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12254         return;
12255     case 0x7: /* SQABS, SQNEG */
12256         if (size == 3 && !is_q) {
12257             unallocated_encoding(s);
12258             return;
12259         }
12260         break;
12261     case 0xc ... 0xf:
12262     case 0x16 ... 0x1f:
12263     {
12264         /* Floating point: U, size[1] and opcode indicate operation;
12265          * size[0] indicates single or double precision.
12266          */
12267         int is_double = extract32(size, 0, 1);
12268         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12269         size = is_double ? 3 : 2;
12270         switch (opcode) {
12271         case 0x2f: /* FABS */
12272         case 0x6f: /* FNEG */
12273             if (size == 3 && !is_q) {
12274                 unallocated_encoding(s);
12275                 return;
12276             }
12277             break;
12278         case 0x1d: /* SCVTF */
12279         case 0x5d: /* UCVTF */
12280         {
12281             bool is_signed = (opcode == 0x1d) ? true : false;
12282             int elements = is_double ? 2 : is_q ? 4 : 2;
12283             if (is_double && !is_q) {
12284                 unallocated_encoding(s);
12285                 return;
12286             }
12287             if (!fp_access_check(s)) {
12288                 return;
12289             }
12290             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12291             return;
12292         }
12293         case 0x2c: /* FCMGT (zero) */
12294         case 0x2d: /* FCMEQ (zero) */
12295         case 0x2e: /* FCMLT (zero) */
12296         case 0x6c: /* FCMGE (zero) */
12297         case 0x6d: /* FCMLE (zero) */
12298             if (size == 3 && !is_q) {
12299                 unallocated_encoding(s);
12300                 return;
12301             }
12302             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12303             return;
12304         case 0x7f: /* FSQRT */
12305             if (size == 3 && !is_q) {
12306                 unallocated_encoding(s);
12307                 return;
12308             }
12309             break;
12310         case 0x1a: /* FCVTNS */
12311         case 0x1b: /* FCVTMS */
12312         case 0x3a: /* FCVTPS */
12313         case 0x3b: /* FCVTZS */
12314         case 0x5a: /* FCVTNU */
12315         case 0x5b: /* FCVTMU */
12316         case 0x7a: /* FCVTPU */
12317         case 0x7b: /* FCVTZU */
12318             need_fpstatus = true;
12319             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12320             if (size == 3 && !is_q) {
12321                 unallocated_encoding(s);
12322                 return;
12323             }
12324             break;
12325         case 0x5c: /* FCVTAU */
12326         case 0x1c: /* FCVTAS */
12327             need_fpstatus = true;
12328             rmode = FPROUNDING_TIEAWAY;
12329             if (size == 3 && !is_q) {
12330                 unallocated_encoding(s);
12331                 return;
12332             }
12333             break;
12334         case 0x3c: /* URECPE */
12335             if (size == 3) {
12336                 unallocated_encoding(s);
12337                 return;
12338             }
12339             /* fall through */
12340         case 0x3d: /* FRECPE */
12341         case 0x7d: /* FRSQRTE */
12342             if (size == 3 && !is_q) {
12343                 unallocated_encoding(s);
12344                 return;
12345             }
12346             if (!fp_access_check(s)) {
12347                 return;
12348             }
12349             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12350             return;
12351         case 0x56: /* FCVTXN, FCVTXN2 */
12352             if (size == 2) {
12353                 unallocated_encoding(s);
12354                 return;
12355             }
12356             /* fall through */
12357         case 0x16: /* FCVTN, FCVTN2 */
12358             /* handle_2misc_narrow does a 2*size -> size operation, but these
12359              * instructions encode the source size rather than dest size.
12360              */
12361             if (!fp_access_check(s)) {
12362                 return;
12363             }
12364             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12365             return;
12366         case 0x36: /* BFCVTN, BFCVTN2 */
12367             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12368                 unallocated_encoding(s);
12369                 return;
12370             }
12371             if (!fp_access_check(s)) {
12372                 return;
12373             }
12374             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12375             return;
12376         case 0x17: /* FCVTL, FCVTL2 */
12377             if (!fp_access_check(s)) {
12378                 return;
12379             }
12380             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12381             return;
12382         case 0x18: /* FRINTN */
12383         case 0x19: /* FRINTM */
12384         case 0x38: /* FRINTP */
12385         case 0x39: /* FRINTZ */
12386             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12387             /* fall through */
12388         case 0x59: /* FRINTX */
12389         case 0x79: /* FRINTI */
12390             need_fpstatus = true;
12391             if (size == 3 && !is_q) {
12392                 unallocated_encoding(s);
12393                 return;
12394             }
12395             break;
12396         case 0x58: /* FRINTA */
12397             rmode = FPROUNDING_TIEAWAY;
12398             need_fpstatus = true;
12399             if (size == 3 && !is_q) {
12400                 unallocated_encoding(s);
12401                 return;
12402             }
12403             break;
12404         case 0x7c: /* URSQRTE */
12405             if (size == 3) {
12406                 unallocated_encoding(s);
12407                 return;
12408             }
12409             break;
12410         case 0x1e: /* FRINT32Z */
12411         case 0x1f: /* FRINT64Z */
12412             rmode = FPROUNDING_ZERO;
12413             /* fall through */
12414         case 0x5e: /* FRINT32X */
12415         case 0x5f: /* FRINT64X */
12416             need_fpstatus = true;
12417             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12418                 unallocated_encoding(s);
12419                 return;
12420             }
12421             break;
12422         default:
12423             unallocated_encoding(s);
12424             return;
12425         }
12426         break;
12427     }
12428     default:
12429         unallocated_encoding(s);
12430         return;
12431     }
12432 
12433     if (!fp_access_check(s)) {
12434         return;
12435     }
12436 
12437     if (need_fpstatus || rmode >= 0) {
12438         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12439     } else {
12440         tcg_fpstatus = NULL;
12441     }
12442     if (rmode >= 0) {
12443         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12444     } else {
12445         tcg_rmode = NULL;
12446     }
12447 
12448     switch (opcode) {
12449     case 0x5:
12450         if (u && size == 0) { /* NOT */
12451             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12452             return;
12453         }
12454         break;
12455     case 0x8: /* CMGT, CMGE */
12456         if (u) {
12457             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12458         } else {
12459             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12460         }
12461         return;
12462     case 0x9: /* CMEQ, CMLE */
12463         if (u) {
12464             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12465         } else {
12466             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12467         }
12468         return;
12469     case 0xa: /* CMLT */
12470         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12471         return;
12472     case 0xb:
12473         if (u) { /* ABS, NEG */
12474             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12475         } else {
12476             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12477         }
12478         return;
12479     }
12480 
12481     if (size == 3) {
12482         /* All 64-bit element operations can be shared with scalar 2misc */
12483         int pass;
12484 
12485         /* Coverity claims (size == 3 && !is_q) has been eliminated
12486          * from all paths leading to here.
12487          */
12488         tcg_debug_assert(is_q);
12489         for (pass = 0; pass < 2; pass++) {
12490             TCGv_i64 tcg_op = tcg_temp_new_i64();
12491             TCGv_i64 tcg_res = tcg_temp_new_i64();
12492 
12493             read_vec_element(s, tcg_op, rn, pass, MO_64);
12494 
12495             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12496                             tcg_rmode, tcg_fpstatus);
12497 
12498             write_vec_element(s, tcg_res, rd, pass, MO_64);
12499         }
12500     } else {
12501         int pass;
12502 
12503         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12504             TCGv_i32 tcg_op = tcg_temp_new_i32();
12505             TCGv_i32 tcg_res = tcg_temp_new_i32();
12506 
12507             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12508 
12509             if (size == 2) {
12510                 /* Special cases for 32 bit elements */
12511                 switch (opcode) {
12512                 case 0x4: /* CLS */
12513                     if (u) {
12514                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12515                     } else {
12516                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12517                     }
12518                     break;
12519                 case 0x7: /* SQABS, SQNEG */
12520                     if (u) {
12521                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12522                     } else {
12523                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12524                     }
12525                     break;
12526                 case 0x2f: /* FABS */
12527                     gen_helper_vfp_abss(tcg_res, tcg_op);
12528                     break;
12529                 case 0x6f: /* FNEG */
12530                     gen_helper_vfp_negs(tcg_res, tcg_op);
12531                     break;
12532                 case 0x7f: /* FSQRT */
12533                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12534                     break;
12535                 case 0x1a: /* FCVTNS */
12536                 case 0x1b: /* FCVTMS */
12537                 case 0x1c: /* FCVTAS */
12538                 case 0x3a: /* FCVTPS */
12539                 case 0x3b: /* FCVTZS */
12540                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12541                                          tcg_constant_i32(0), tcg_fpstatus);
12542                     break;
12543                 case 0x5a: /* FCVTNU */
12544                 case 0x5b: /* FCVTMU */
12545                 case 0x5c: /* FCVTAU */
12546                 case 0x7a: /* FCVTPU */
12547                 case 0x7b: /* FCVTZU */
12548                     gen_helper_vfp_touls(tcg_res, tcg_op,
12549                                          tcg_constant_i32(0), tcg_fpstatus);
12550                     break;
12551                 case 0x18: /* FRINTN */
12552                 case 0x19: /* FRINTM */
12553                 case 0x38: /* FRINTP */
12554                 case 0x39: /* FRINTZ */
12555                 case 0x58: /* FRINTA */
12556                 case 0x79: /* FRINTI */
12557                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12558                     break;
12559                 case 0x59: /* FRINTX */
12560                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12561                     break;
12562                 case 0x7c: /* URSQRTE */
12563                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12564                     break;
12565                 case 0x1e: /* FRINT32Z */
12566                 case 0x5e: /* FRINT32X */
12567                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12568                     break;
12569                 case 0x1f: /* FRINT64Z */
12570                 case 0x5f: /* FRINT64X */
12571                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12572                     break;
12573                 default:
12574                     g_assert_not_reached();
12575                 }
12576             } else {
12577                 /* Use helpers for 8 and 16 bit elements */
12578                 switch (opcode) {
12579                 case 0x5: /* CNT, RBIT */
12580                     /* For these two insns size is part of the opcode specifier
12581                      * (handled earlier); they always operate on byte elements.
12582                      */
12583                     if (u) {
12584                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12585                     } else {
12586                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12587                     }
12588                     break;
12589                 case 0x7: /* SQABS, SQNEG */
12590                 {
12591                     NeonGenOneOpEnvFn *genfn;
12592                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12593                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12594                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12595                     };
12596                     genfn = fns[size][u];
12597                     genfn(tcg_res, cpu_env, tcg_op);
12598                     break;
12599                 }
12600                 case 0x4: /* CLS, CLZ */
12601                     if (u) {
12602                         if (size == 0) {
12603                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12604                         } else {
12605                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12606                         }
12607                     } else {
12608                         if (size == 0) {
12609                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12610                         } else {
12611                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12612                         }
12613                     }
12614                     break;
12615                 default:
12616                     g_assert_not_reached();
12617                 }
12618             }
12619 
12620             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12621         }
12622     }
12623     clear_vec_high(s, is_q, rd);
12624 
12625     if (tcg_rmode) {
12626         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12627     }
12628 }
12629 
12630 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12631  *
12632  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12633  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12634  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12635  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12636  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12637  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12638  *
12639  * This actually covers two groups where scalar access is governed by
12640  * bit 28. A bunch of the instructions (float to integral) only exist
12641  * in the vector form and are un-allocated for the scalar decode. Also
12642  * in the scalar decode Q is always 1.
12643  */
12644 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12645 {
12646     int fpop, opcode, a, u;
12647     int rn, rd;
12648     bool is_q;
12649     bool is_scalar;
12650     bool only_in_vector = false;
12651 
12652     int pass;
12653     TCGv_i32 tcg_rmode = NULL;
12654     TCGv_ptr tcg_fpstatus = NULL;
12655     bool need_fpst = true;
12656     int rmode = -1;
12657 
12658     if (!dc_isar_feature(aa64_fp16, s)) {
12659         unallocated_encoding(s);
12660         return;
12661     }
12662 
12663     rd = extract32(insn, 0, 5);
12664     rn = extract32(insn, 5, 5);
12665 
12666     a = extract32(insn, 23, 1);
12667     u = extract32(insn, 29, 1);
12668     is_scalar = extract32(insn, 28, 1);
12669     is_q = extract32(insn, 30, 1);
12670 
12671     opcode = extract32(insn, 12, 5);
12672     fpop = deposit32(opcode, 5, 1, a);
12673     fpop = deposit32(fpop, 6, 1, u);
12674 
12675     switch (fpop) {
12676     case 0x1d: /* SCVTF */
12677     case 0x5d: /* UCVTF */
12678     {
12679         int elements;
12680 
12681         if (is_scalar) {
12682             elements = 1;
12683         } else {
12684             elements = (is_q ? 8 : 4);
12685         }
12686 
12687         if (!fp_access_check(s)) {
12688             return;
12689         }
12690         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12691         return;
12692     }
12693     break;
12694     case 0x2c: /* FCMGT (zero) */
12695     case 0x2d: /* FCMEQ (zero) */
12696     case 0x2e: /* FCMLT (zero) */
12697     case 0x6c: /* FCMGE (zero) */
12698     case 0x6d: /* FCMLE (zero) */
12699         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12700         return;
12701     case 0x3d: /* FRECPE */
12702     case 0x3f: /* FRECPX */
12703         break;
12704     case 0x18: /* FRINTN */
12705         only_in_vector = true;
12706         rmode = FPROUNDING_TIEEVEN;
12707         break;
12708     case 0x19: /* FRINTM */
12709         only_in_vector = true;
12710         rmode = FPROUNDING_NEGINF;
12711         break;
12712     case 0x38: /* FRINTP */
12713         only_in_vector = true;
12714         rmode = FPROUNDING_POSINF;
12715         break;
12716     case 0x39: /* FRINTZ */
12717         only_in_vector = true;
12718         rmode = FPROUNDING_ZERO;
12719         break;
12720     case 0x58: /* FRINTA */
12721         only_in_vector = true;
12722         rmode = FPROUNDING_TIEAWAY;
12723         break;
12724     case 0x59: /* FRINTX */
12725     case 0x79: /* FRINTI */
12726         only_in_vector = true;
12727         /* current rounding mode */
12728         break;
12729     case 0x1a: /* FCVTNS */
12730         rmode = FPROUNDING_TIEEVEN;
12731         break;
12732     case 0x1b: /* FCVTMS */
12733         rmode = FPROUNDING_NEGINF;
12734         break;
12735     case 0x1c: /* FCVTAS */
12736         rmode = FPROUNDING_TIEAWAY;
12737         break;
12738     case 0x3a: /* FCVTPS */
12739         rmode = FPROUNDING_POSINF;
12740         break;
12741     case 0x3b: /* FCVTZS */
12742         rmode = FPROUNDING_ZERO;
12743         break;
12744     case 0x5a: /* FCVTNU */
12745         rmode = FPROUNDING_TIEEVEN;
12746         break;
12747     case 0x5b: /* FCVTMU */
12748         rmode = FPROUNDING_NEGINF;
12749         break;
12750     case 0x5c: /* FCVTAU */
12751         rmode = FPROUNDING_TIEAWAY;
12752         break;
12753     case 0x7a: /* FCVTPU */
12754         rmode = FPROUNDING_POSINF;
12755         break;
12756     case 0x7b: /* FCVTZU */
12757         rmode = FPROUNDING_ZERO;
12758         break;
12759     case 0x2f: /* FABS */
12760     case 0x6f: /* FNEG */
12761         need_fpst = false;
12762         break;
12763     case 0x7d: /* FRSQRTE */
12764     case 0x7f: /* FSQRT (vector) */
12765         break;
12766     default:
12767         unallocated_encoding(s);
12768         return;
12769     }
12770 
12771 
12772     /* Check additional constraints for the scalar encoding */
12773     if (is_scalar) {
12774         if (!is_q) {
12775             unallocated_encoding(s);
12776             return;
12777         }
12778         /* FRINTxx is only in the vector form */
12779         if (only_in_vector) {
12780             unallocated_encoding(s);
12781             return;
12782         }
12783     }
12784 
12785     if (!fp_access_check(s)) {
12786         return;
12787     }
12788 
12789     if (rmode >= 0 || need_fpst) {
12790         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12791     }
12792 
12793     if (rmode >= 0) {
12794         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12795     }
12796 
12797     if (is_scalar) {
12798         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12799         TCGv_i32 tcg_res = tcg_temp_new_i32();
12800 
12801         switch (fpop) {
12802         case 0x1a: /* FCVTNS */
12803         case 0x1b: /* FCVTMS */
12804         case 0x1c: /* FCVTAS */
12805         case 0x3a: /* FCVTPS */
12806         case 0x3b: /* FCVTZS */
12807             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12808             break;
12809         case 0x3d: /* FRECPE */
12810             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12811             break;
12812         case 0x3f: /* FRECPX */
12813             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12814             break;
12815         case 0x5a: /* FCVTNU */
12816         case 0x5b: /* FCVTMU */
12817         case 0x5c: /* FCVTAU */
12818         case 0x7a: /* FCVTPU */
12819         case 0x7b: /* FCVTZU */
12820             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12821             break;
12822         case 0x6f: /* FNEG */
12823             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12824             break;
12825         case 0x7d: /* FRSQRTE */
12826             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12827             break;
12828         default:
12829             g_assert_not_reached();
12830         }
12831 
12832         /* limit any sign extension going on */
12833         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12834         write_fp_sreg(s, rd, tcg_res);
12835     } else {
12836         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12837             TCGv_i32 tcg_op = tcg_temp_new_i32();
12838             TCGv_i32 tcg_res = tcg_temp_new_i32();
12839 
12840             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12841 
12842             switch (fpop) {
12843             case 0x1a: /* FCVTNS */
12844             case 0x1b: /* FCVTMS */
12845             case 0x1c: /* FCVTAS */
12846             case 0x3a: /* FCVTPS */
12847             case 0x3b: /* FCVTZS */
12848                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12849                 break;
12850             case 0x3d: /* FRECPE */
12851                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12852                 break;
12853             case 0x5a: /* FCVTNU */
12854             case 0x5b: /* FCVTMU */
12855             case 0x5c: /* FCVTAU */
12856             case 0x7a: /* FCVTPU */
12857             case 0x7b: /* FCVTZU */
12858                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12859                 break;
12860             case 0x18: /* FRINTN */
12861             case 0x19: /* FRINTM */
12862             case 0x38: /* FRINTP */
12863             case 0x39: /* FRINTZ */
12864             case 0x58: /* FRINTA */
12865             case 0x79: /* FRINTI */
12866                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12867                 break;
12868             case 0x59: /* FRINTX */
12869                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12870                 break;
12871             case 0x2f: /* FABS */
12872                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12873                 break;
12874             case 0x6f: /* FNEG */
12875                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12876                 break;
12877             case 0x7d: /* FRSQRTE */
12878                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12879                 break;
12880             case 0x7f: /* FSQRT */
12881                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12882                 break;
12883             default:
12884                 g_assert_not_reached();
12885             }
12886 
12887             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12888         }
12889 
12890         clear_vec_high(s, is_q, rd);
12891     }
12892 
12893     if (tcg_rmode) {
12894         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12895     }
12896 }
12897 
12898 /* AdvSIMD scalar x indexed element
12899  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12900  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12901  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12902  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12903  * AdvSIMD vector x indexed element
12904  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12905  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12906  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12907  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12908  */
12909 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12910 {
12911     /* This encoding has two kinds of instruction:
12912      *  normal, where we perform elt x idxelt => elt for each
12913      *     element in the vector
12914      *  long, where we perform elt x idxelt and generate a result of
12915      *     double the width of the input element
12916      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12917      */
12918     bool is_scalar = extract32(insn, 28, 1);
12919     bool is_q = extract32(insn, 30, 1);
12920     bool u = extract32(insn, 29, 1);
12921     int size = extract32(insn, 22, 2);
12922     int l = extract32(insn, 21, 1);
12923     int m = extract32(insn, 20, 1);
12924     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12925     int rm = extract32(insn, 16, 4);
12926     int opcode = extract32(insn, 12, 4);
12927     int h = extract32(insn, 11, 1);
12928     int rn = extract32(insn, 5, 5);
12929     int rd = extract32(insn, 0, 5);
12930     bool is_long = false;
12931     int is_fp = 0;
12932     bool is_fp16 = false;
12933     int index;
12934     TCGv_ptr fpst;
12935 
12936     switch (16 * u + opcode) {
12937     case 0x08: /* MUL */
12938     case 0x10: /* MLA */
12939     case 0x14: /* MLS */
12940         if (is_scalar) {
12941             unallocated_encoding(s);
12942             return;
12943         }
12944         break;
12945     case 0x02: /* SMLAL, SMLAL2 */
12946     case 0x12: /* UMLAL, UMLAL2 */
12947     case 0x06: /* SMLSL, SMLSL2 */
12948     case 0x16: /* UMLSL, UMLSL2 */
12949     case 0x0a: /* SMULL, SMULL2 */
12950     case 0x1a: /* UMULL, UMULL2 */
12951         if (is_scalar) {
12952             unallocated_encoding(s);
12953             return;
12954         }
12955         is_long = true;
12956         break;
12957     case 0x03: /* SQDMLAL, SQDMLAL2 */
12958     case 0x07: /* SQDMLSL, SQDMLSL2 */
12959     case 0x0b: /* SQDMULL, SQDMULL2 */
12960         is_long = true;
12961         break;
12962     case 0x0c: /* SQDMULH */
12963     case 0x0d: /* SQRDMULH */
12964         break;
12965     case 0x01: /* FMLA */
12966     case 0x05: /* FMLS */
12967     case 0x09: /* FMUL */
12968     case 0x19: /* FMULX */
12969         is_fp = 1;
12970         break;
12971     case 0x1d: /* SQRDMLAH */
12972     case 0x1f: /* SQRDMLSH */
12973         if (!dc_isar_feature(aa64_rdm, s)) {
12974             unallocated_encoding(s);
12975             return;
12976         }
12977         break;
12978     case 0x0e: /* SDOT */
12979     case 0x1e: /* UDOT */
12980         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12981             unallocated_encoding(s);
12982             return;
12983         }
12984         break;
12985     case 0x0f:
12986         switch (size) {
12987         case 0: /* SUDOT */
12988         case 2: /* USDOT */
12989             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12990                 unallocated_encoding(s);
12991                 return;
12992             }
12993             size = MO_32;
12994             break;
12995         case 1: /* BFDOT */
12996             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12997                 unallocated_encoding(s);
12998                 return;
12999             }
13000             size = MO_32;
13001             break;
13002         case 3: /* BFMLAL{B,T} */
13003             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13004                 unallocated_encoding(s);
13005                 return;
13006             }
13007             /* can't set is_fp without other incorrect size checks */
13008             size = MO_16;
13009             break;
13010         default:
13011             unallocated_encoding(s);
13012             return;
13013         }
13014         break;
13015     case 0x11: /* FCMLA #0 */
13016     case 0x13: /* FCMLA #90 */
13017     case 0x15: /* FCMLA #180 */
13018     case 0x17: /* FCMLA #270 */
13019         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13020             unallocated_encoding(s);
13021             return;
13022         }
13023         is_fp = 2;
13024         break;
13025     case 0x00: /* FMLAL */
13026     case 0x04: /* FMLSL */
13027     case 0x18: /* FMLAL2 */
13028     case 0x1c: /* FMLSL2 */
13029         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13030             unallocated_encoding(s);
13031             return;
13032         }
13033         size = MO_16;
13034         /* is_fp, but we pass cpu_env not fp_status.  */
13035         break;
13036     default:
13037         unallocated_encoding(s);
13038         return;
13039     }
13040 
13041     switch (is_fp) {
13042     case 1: /* normal fp */
13043         /* convert insn encoded size to MemOp size */
13044         switch (size) {
13045         case 0: /* half-precision */
13046             size = MO_16;
13047             is_fp16 = true;
13048             break;
13049         case MO_32: /* single precision */
13050         case MO_64: /* double precision */
13051             break;
13052         default:
13053             unallocated_encoding(s);
13054             return;
13055         }
13056         break;
13057 
13058     case 2: /* complex fp */
13059         /* Each indexable element is a complex pair.  */
13060         size += 1;
13061         switch (size) {
13062         case MO_32:
13063             if (h && !is_q) {
13064                 unallocated_encoding(s);
13065                 return;
13066             }
13067             is_fp16 = true;
13068             break;
13069         case MO_64:
13070             break;
13071         default:
13072             unallocated_encoding(s);
13073             return;
13074         }
13075         break;
13076 
13077     default: /* integer */
13078         switch (size) {
13079         case MO_8:
13080         case MO_64:
13081             unallocated_encoding(s);
13082             return;
13083         }
13084         break;
13085     }
13086     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13087         unallocated_encoding(s);
13088         return;
13089     }
13090 
13091     /* Given MemOp size, adjust register and indexing.  */
13092     switch (size) {
13093     case MO_16:
13094         index = h << 2 | l << 1 | m;
13095         break;
13096     case MO_32:
13097         index = h << 1 | l;
13098         rm |= m << 4;
13099         break;
13100     case MO_64:
13101         if (l || !is_q) {
13102             unallocated_encoding(s);
13103             return;
13104         }
13105         index = h;
13106         rm |= m << 4;
13107         break;
13108     default:
13109         g_assert_not_reached();
13110     }
13111 
13112     if (!fp_access_check(s)) {
13113         return;
13114     }
13115 
13116     if (is_fp) {
13117         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13118     } else {
13119         fpst = NULL;
13120     }
13121 
13122     switch (16 * u + opcode) {
13123     case 0x0e: /* SDOT */
13124     case 0x1e: /* UDOT */
13125         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13126                          u ? gen_helper_gvec_udot_idx_b
13127                          : gen_helper_gvec_sdot_idx_b);
13128         return;
13129     case 0x0f:
13130         switch (extract32(insn, 22, 2)) {
13131         case 0: /* SUDOT */
13132             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13133                              gen_helper_gvec_sudot_idx_b);
13134             return;
13135         case 1: /* BFDOT */
13136             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13137                              gen_helper_gvec_bfdot_idx);
13138             return;
13139         case 2: /* USDOT */
13140             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13141                              gen_helper_gvec_usdot_idx_b);
13142             return;
13143         case 3: /* BFMLAL{B,T} */
13144             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13145                               gen_helper_gvec_bfmlal_idx);
13146             return;
13147         }
13148         g_assert_not_reached();
13149     case 0x11: /* FCMLA #0 */
13150     case 0x13: /* FCMLA #90 */
13151     case 0x15: /* FCMLA #180 */
13152     case 0x17: /* FCMLA #270 */
13153         {
13154             int rot = extract32(insn, 13, 2);
13155             int data = (index << 2) | rot;
13156             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13157                                vec_full_reg_offset(s, rn),
13158                                vec_full_reg_offset(s, rm),
13159                                vec_full_reg_offset(s, rd), fpst,
13160                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13161                                size == MO_64
13162                                ? gen_helper_gvec_fcmlas_idx
13163                                : gen_helper_gvec_fcmlah_idx);
13164         }
13165         return;
13166 
13167     case 0x00: /* FMLAL */
13168     case 0x04: /* FMLSL */
13169     case 0x18: /* FMLAL2 */
13170     case 0x1c: /* FMLSL2 */
13171         {
13172             int is_s = extract32(opcode, 2, 1);
13173             int is_2 = u;
13174             int data = (index << 2) | (is_2 << 1) | is_s;
13175             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13176                                vec_full_reg_offset(s, rn),
13177                                vec_full_reg_offset(s, rm), cpu_env,
13178                                is_q ? 16 : 8, vec_full_reg_size(s),
13179                                data, gen_helper_gvec_fmlal_idx_a64);
13180         }
13181         return;
13182 
13183     case 0x08: /* MUL */
13184         if (!is_long && !is_scalar) {
13185             static gen_helper_gvec_3 * const fns[3] = {
13186                 gen_helper_gvec_mul_idx_h,
13187                 gen_helper_gvec_mul_idx_s,
13188                 gen_helper_gvec_mul_idx_d,
13189             };
13190             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13191                                vec_full_reg_offset(s, rn),
13192                                vec_full_reg_offset(s, rm),
13193                                is_q ? 16 : 8, vec_full_reg_size(s),
13194                                index, fns[size - 1]);
13195             return;
13196         }
13197         break;
13198 
13199     case 0x10: /* MLA */
13200         if (!is_long && !is_scalar) {
13201             static gen_helper_gvec_4 * const fns[3] = {
13202                 gen_helper_gvec_mla_idx_h,
13203                 gen_helper_gvec_mla_idx_s,
13204                 gen_helper_gvec_mla_idx_d,
13205             };
13206             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13207                                vec_full_reg_offset(s, rn),
13208                                vec_full_reg_offset(s, rm),
13209                                vec_full_reg_offset(s, rd),
13210                                is_q ? 16 : 8, vec_full_reg_size(s),
13211                                index, fns[size - 1]);
13212             return;
13213         }
13214         break;
13215 
13216     case 0x14: /* MLS */
13217         if (!is_long && !is_scalar) {
13218             static gen_helper_gvec_4 * const fns[3] = {
13219                 gen_helper_gvec_mls_idx_h,
13220                 gen_helper_gvec_mls_idx_s,
13221                 gen_helper_gvec_mls_idx_d,
13222             };
13223             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13224                                vec_full_reg_offset(s, rn),
13225                                vec_full_reg_offset(s, rm),
13226                                vec_full_reg_offset(s, rd),
13227                                is_q ? 16 : 8, vec_full_reg_size(s),
13228                                index, fns[size - 1]);
13229             return;
13230         }
13231         break;
13232     }
13233 
13234     if (size == 3) {
13235         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13236         int pass;
13237 
13238         assert(is_fp && is_q && !is_long);
13239 
13240         read_vec_element(s, tcg_idx, rm, index, MO_64);
13241 
13242         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13243             TCGv_i64 tcg_op = tcg_temp_new_i64();
13244             TCGv_i64 tcg_res = tcg_temp_new_i64();
13245 
13246             read_vec_element(s, tcg_op, rn, pass, MO_64);
13247 
13248             switch (16 * u + opcode) {
13249             case 0x05: /* FMLS */
13250                 /* As usual for ARM, separate negation for fused multiply-add */
13251                 gen_helper_vfp_negd(tcg_op, tcg_op);
13252                 /* fall through */
13253             case 0x01: /* FMLA */
13254                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13255                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13256                 break;
13257             case 0x09: /* FMUL */
13258                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13259                 break;
13260             case 0x19: /* FMULX */
13261                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13262                 break;
13263             default:
13264                 g_assert_not_reached();
13265             }
13266 
13267             write_vec_element(s, tcg_res, rd, pass, MO_64);
13268         }
13269 
13270         clear_vec_high(s, !is_scalar, rd);
13271     } else if (!is_long) {
13272         /* 32 bit floating point, or 16 or 32 bit integer.
13273          * For the 16 bit scalar case we use the usual Neon helpers and
13274          * rely on the fact that 0 op 0 == 0 with no side effects.
13275          */
13276         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13277         int pass, maxpasses;
13278 
13279         if (is_scalar) {
13280             maxpasses = 1;
13281         } else {
13282             maxpasses = is_q ? 4 : 2;
13283         }
13284 
13285         read_vec_element_i32(s, tcg_idx, rm, index, size);
13286 
13287         if (size == 1 && !is_scalar) {
13288             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13289              * the index into both halves of the 32 bit tcg_idx and then use
13290              * the usual Neon helpers.
13291              */
13292             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13293         }
13294 
13295         for (pass = 0; pass < maxpasses; pass++) {
13296             TCGv_i32 tcg_op = tcg_temp_new_i32();
13297             TCGv_i32 tcg_res = tcg_temp_new_i32();
13298 
13299             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13300 
13301             switch (16 * u + opcode) {
13302             case 0x08: /* MUL */
13303             case 0x10: /* MLA */
13304             case 0x14: /* MLS */
13305             {
13306                 static NeonGenTwoOpFn * const fns[2][2] = {
13307                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13308                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13309                 };
13310                 NeonGenTwoOpFn *genfn;
13311                 bool is_sub = opcode == 0x4;
13312 
13313                 if (size == 1) {
13314                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13315                 } else {
13316                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13317                 }
13318                 if (opcode == 0x8) {
13319                     break;
13320                 }
13321                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13322                 genfn = fns[size - 1][is_sub];
13323                 genfn(tcg_res, tcg_op, tcg_res);
13324                 break;
13325             }
13326             case 0x05: /* FMLS */
13327             case 0x01: /* FMLA */
13328                 read_vec_element_i32(s, tcg_res, rd, pass,
13329                                      is_scalar ? size : MO_32);
13330                 switch (size) {
13331                 case 1:
13332                     if (opcode == 0x5) {
13333                         /* As usual for ARM, separate negation for fused
13334                          * multiply-add */
13335                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13336                     }
13337                     if (is_scalar) {
13338                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13339                                                    tcg_res, fpst);
13340                     } else {
13341                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13342                                                     tcg_res, fpst);
13343                     }
13344                     break;
13345                 case 2:
13346                     if (opcode == 0x5) {
13347                         /* As usual for ARM, separate negation for
13348                          * fused multiply-add */
13349                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13350                     }
13351                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13352                                            tcg_res, fpst);
13353                     break;
13354                 default:
13355                     g_assert_not_reached();
13356                 }
13357                 break;
13358             case 0x09: /* FMUL */
13359                 switch (size) {
13360                 case 1:
13361                     if (is_scalar) {
13362                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13363                                                 tcg_idx, fpst);
13364                     } else {
13365                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13366                                                  tcg_idx, fpst);
13367                     }
13368                     break;
13369                 case 2:
13370                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13371                     break;
13372                 default:
13373                     g_assert_not_reached();
13374                 }
13375                 break;
13376             case 0x19: /* FMULX */
13377                 switch (size) {
13378                 case 1:
13379                     if (is_scalar) {
13380                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13381                                                  tcg_idx, fpst);
13382                     } else {
13383                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13384                                                   tcg_idx, fpst);
13385                     }
13386                     break;
13387                 case 2:
13388                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13389                     break;
13390                 default:
13391                     g_assert_not_reached();
13392                 }
13393                 break;
13394             case 0x0c: /* SQDMULH */
13395                 if (size == 1) {
13396                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13397                                                tcg_op, tcg_idx);
13398                 } else {
13399                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13400                                                tcg_op, tcg_idx);
13401                 }
13402                 break;
13403             case 0x0d: /* SQRDMULH */
13404                 if (size == 1) {
13405                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13406                                                 tcg_op, tcg_idx);
13407                 } else {
13408                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13409                                                 tcg_op, tcg_idx);
13410                 }
13411                 break;
13412             case 0x1d: /* SQRDMLAH */
13413                 read_vec_element_i32(s, tcg_res, rd, pass,
13414                                      is_scalar ? size : MO_32);
13415                 if (size == 1) {
13416                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13417                                                 tcg_op, tcg_idx, tcg_res);
13418                 } else {
13419                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13420                                                 tcg_op, tcg_idx, tcg_res);
13421                 }
13422                 break;
13423             case 0x1f: /* SQRDMLSH */
13424                 read_vec_element_i32(s, tcg_res, rd, pass,
13425                                      is_scalar ? size : MO_32);
13426                 if (size == 1) {
13427                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13428                                                 tcg_op, tcg_idx, tcg_res);
13429                 } else {
13430                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13431                                                 tcg_op, tcg_idx, tcg_res);
13432                 }
13433                 break;
13434             default:
13435                 g_assert_not_reached();
13436             }
13437 
13438             if (is_scalar) {
13439                 write_fp_sreg(s, rd, tcg_res);
13440             } else {
13441                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13442             }
13443         }
13444 
13445         clear_vec_high(s, is_q, rd);
13446     } else {
13447         /* long ops: 16x16->32 or 32x32->64 */
13448         TCGv_i64 tcg_res[2];
13449         int pass;
13450         bool satop = extract32(opcode, 0, 1);
13451         MemOp memop = MO_32;
13452 
13453         if (satop || !u) {
13454             memop |= MO_SIGN;
13455         }
13456 
13457         if (size == 2) {
13458             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13459 
13460             read_vec_element(s, tcg_idx, rm, index, memop);
13461 
13462             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13463                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13464                 TCGv_i64 tcg_passres;
13465                 int passelt;
13466 
13467                 if (is_scalar) {
13468                     passelt = 0;
13469                 } else {
13470                     passelt = pass + (is_q * 2);
13471                 }
13472 
13473                 read_vec_element(s, tcg_op, rn, passelt, memop);
13474 
13475                 tcg_res[pass] = tcg_temp_new_i64();
13476 
13477                 if (opcode == 0xa || opcode == 0xb) {
13478                     /* Non-accumulating ops */
13479                     tcg_passres = tcg_res[pass];
13480                 } else {
13481                     tcg_passres = tcg_temp_new_i64();
13482                 }
13483 
13484                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13485 
13486                 if (satop) {
13487                     /* saturating, doubling */
13488                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13489                                                       tcg_passres, tcg_passres);
13490                 }
13491 
13492                 if (opcode == 0xa || opcode == 0xb) {
13493                     continue;
13494                 }
13495 
13496                 /* Accumulating op: handle accumulate step */
13497                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13498 
13499                 switch (opcode) {
13500                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13501                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13502                     break;
13503                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13504                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13505                     break;
13506                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13507                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13508                     /* fall through */
13509                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13510                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13511                                                       tcg_res[pass],
13512                                                       tcg_passres);
13513                     break;
13514                 default:
13515                     g_assert_not_reached();
13516                 }
13517             }
13518 
13519             clear_vec_high(s, !is_scalar, rd);
13520         } else {
13521             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13522 
13523             assert(size == 1);
13524             read_vec_element_i32(s, tcg_idx, rm, index, size);
13525 
13526             if (!is_scalar) {
13527                 /* The simplest way to handle the 16x16 indexed ops is to
13528                  * duplicate the index into both halves of the 32 bit tcg_idx
13529                  * and then use the usual Neon helpers.
13530                  */
13531                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13532             }
13533 
13534             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13535                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13536                 TCGv_i64 tcg_passres;
13537 
13538                 if (is_scalar) {
13539                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13540                 } else {
13541                     read_vec_element_i32(s, tcg_op, rn,
13542                                          pass + (is_q * 2), MO_32);
13543                 }
13544 
13545                 tcg_res[pass] = tcg_temp_new_i64();
13546 
13547                 if (opcode == 0xa || opcode == 0xb) {
13548                     /* Non-accumulating ops */
13549                     tcg_passres = tcg_res[pass];
13550                 } else {
13551                     tcg_passres = tcg_temp_new_i64();
13552                 }
13553 
13554                 if (memop & MO_SIGN) {
13555                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13556                 } else {
13557                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13558                 }
13559                 if (satop) {
13560                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13561                                                       tcg_passres, tcg_passres);
13562                 }
13563 
13564                 if (opcode == 0xa || opcode == 0xb) {
13565                     continue;
13566                 }
13567 
13568                 /* Accumulating op: handle accumulate step */
13569                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13570 
13571                 switch (opcode) {
13572                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13573                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13574                                              tcg_passres);
13575                     break;
13576                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13577                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13578                                              tcg_passres);
13579                     break;
13580                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13581                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13582                     /* fall through */
13583                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13584                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13585                                                       tcg_res[pass],
13586                                                       tcg_passres);
13587                     break;
13588                 default:
13589                     g_assert_not_reached();
13590                 }
13591             }
13592 
13593             if (is_scalar) {
13594                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13595             }
13596         }
13597 
13598         if (is_scalar) {
13599             tcg_res[1] = tcg_constant_i64(0);
13600         }
13601 
13602         for (pass = 0; pass < 2; pass++) {
13603             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13604         }
13605     }
13606 }
13607 
13608 /* Crypto AES
13609  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13610  * +-----------------+------+-----------+--------+-----+------+------+
13611  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13612  * +-----------------+------+-----------+--------+-----+------+------+
13613  */
13614 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13615 {
13616     int size = extract32(insn, 22, 2);
13617     int opcode = extract32(insn, 12, 5);
13618     int rn = extract32(insn, 5, 5);
13619     int rd = extract32(insn, 0, 5);
13620     int decrypt;
13621     gen_helper_gvec_2 *genfn2 = NULL;
13622     gen_helper_gvec_3 *genfn3 = NULL;
13623 
13624     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13625         unallocated_encoding(s);
13626         return;
13627     }
13628 
13629     switch (opcode) {
13630     case 0x4: /* AESE */
13631         decrypt = 0;
13632         genfn3 = gen_helper_crypto_aese;
13633         break;
13634     case 0x6: /* AESMC */
13635         decrypt = 0;
13636         genfn2 = gen_helper_crypto_aesmc;
13637         break;
13638     case 0x5: /* AESD */
13639         decrypt = 1;
13640         genfn3 = gen_helper_crypto_aese;
13641         break;
13642     case 0x7: /* AESIMC */
13643         decrypt = 1;
13644         genfn2 = gen_helper_crypto_aesmc;
13645         break;
13646     default:
13647         unallocated_encoding(s);
13648         return;
13649     }
13650 
13651     if (!fp_access_check(s)) {
13652         return;
13653     }
13654     if (genfn2) {
13655         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13656     } else {
13657         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13658     }
13659 }
13660 
13661 /* Crypto three-reg SHA
13662  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13663  * +-----------------+------+---+------+---+--------+-----+------+------+
13664  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13665  * +-----------------+------+---+------+---+--------+-----+------+------+
13666  */
13667 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13668 {
13669     int size = extract32(insn, 22, 2);
13670     int opcode = extract32(insn, 12, 3);
13671     int rm = extract32(insn, 16, 5);
13672     int rn = extract32(insn, 5, 5);
13673     int rd = extract32(insn, 0, 5);
13674     gen_helper_gvec_3 *genfn;
13675     bool feature;
13676 
13677     if (size != 0) {
13678         unallocated_encoding(s);
13679         return;
13680     }
13681 
13682     switch (opcode) {
13683     case 0: /* SHA1C */
13684         genfn = gen_helper_crypto_sha1c;
13685         feature = dc_isar_feature(aa64_sha1, s);
13686         break;
13687     case 1: /* SHA1P */
13688         genfn = gen_helper_crypto_sha1p;
13689         feature = dc_isar_feature(aa64_sha1, s);
13690         break;
13691     case 2: /* SHA1M */
13692         genfn = gen_helper_crypto_sha1m;
13693         feature = dc_isar_feature(aa64_sha1, s);
13694         break;
13695     case 3: /* SHA1SU0 */
13696         genfn = gen_helper_crypto_sha1su0;
13697         feature = dc_isar_feature(aa64_sha1, s);
13698         break;
13699     case 4: /* SHA256H */
13700         genfn = gen_helper_crypto_sha256h;
13701         feature = dc_isar_feature(aa64_sha256, s);
13702         break;
13703     case 5: /* SHA256H2 */
13704         genfn = gen_helper_crypto_sha256h2;
13705         feature = dc_isar_feature(aa64_sha256, s);
13706         break;
13707     case 6: /* SHA256SU1 */
13708         genfn = gen_helper_crypto_sha256su1;
13709         feature = dc_isar_feature(aa64_sha256, s);
13710         break;
13711     default:
13712         unallocated_encoding(s);
13713         return;
13714     }
13715 
13716     if (!feature) {
13717         unallocated_encoding(s);
13718         return;
13719     }
13720 
13721     if (!fp_access_check(s)) {
13722         return;
13723     }
13724     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13725 }
13726 
13727 /* Crypto two-reg SHA
13728  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13729  * +-----------------+------+-----------+--------+-----+------+------+
13730  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13731  * +-----------------+------+-----------+--------+-----+------+------+
13732  */
13733 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13734 {
13735     int size = extract32(insn, 22, 2);
13736     int opcode = extract32(insn, 12, 5);
13737     int rn = extract32(insn, 5, 5);
13738     int rd = extract32(insn, 0, 5);
13739     gen_helper_gvec_2 *genfn;
13740     bool feature;
13741 
13742     if (size != 0) {
13743         unallocated_encoding(s);
13744         return;
13745     }
13746 
13747     switch (opcode) {
13748     case 0: /* SHA1H */
13749         feature = dc_isar_feature(aa64_sha1, s);
13750         genfn = gen_helper_crypto_sha1h;
13751         break;
13752     case 1: /* SHA1SU1 */
13753         feature = dc_isar_feature(aa64_sha1, s);
13754         genfn = gen_helper_crypto_sha1su1;
13755         break;
13756     case 2: /* SHA256SU0 */
13757         feature = dc_isar_feature(aa64_sha256, s);
13758         genfn = gen_helper_crypto_sha256su0;
13759         break;
13760     default:
13761         unallocated_encoding(s);
13762         return;
13763     }
13764 
13765     if (!feature) {
13766         unallocated_encoding(s);
13767         return;
13768     }
13769 
13770     if (!fp_access_check(s)) {
13771         return;
13772     }
13773     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13774 }
13775 
13776 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13777 {
13778     tcg_gen_rotli_i64(d, m, 1);
13779     tcg_gen_xor_i64(d, d, n);
13780 }
13781 
13782 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13783 {
13784     tcg_gen_rotli_vec(vece, d, m, 1);
13785     tcg_gen_xor_vec(vece, d, d, n);
13786 }
13787 
13788 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13789                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13790 {
13791     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13792     static const GVecGen3 op = {
13793         .fni8 = gen_rax1_i64,
13794         .fniv = gen_rax1_vec,
13795         .opt_opc = vecop_list,
13796         .fno = gen_helper_crypto_rax1,
13797         .vece = MO_64,
13798     };
13799     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13800 }
13801 
13802 /* Crypto three-reg SHA512
13803  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13804  * +-----------------------+------+---+---+-----+--------+------+------+
13805  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13806  * +-----------------------+------+---+---+-----+--------+------+------+
13807  */
13808 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13809 {
13810     int opcode = extract32(insn, 10, 2);
13811     int o =  extract32(insn, 14, 1);
13812     int rm = extract32(insn, 16, 5);
13813     int rn = extract32(insn, 5, 5);
13814     int rd = extract32(insn, 0, 5);
13815     bool feature;
13816     gen_helper_gvec_3 *oolfn = NULL;
13817     GVecGen3Fn *gvecfn = NULL;
13818 
13819     if (o == 0) {
13820         switch (opcode) {
13821         case 0: /* SHA512H */
13822             feature = dc_isar_feature(aa64_sha512, s);
13823             oolfn = gen_helper_crypto_sha512h;
13824             break;
13825         case 1: /* SHA512H2 */
13826             feature = dc_isar_feature(aa64_sha512, s);
13827             oolfn = gen_helper_crypto_sha512h2;
13828             break;
13829         case 2: /* SHA512SU1 */
13830             feature = dc_isar_feature(aa64_sha512, s);
13831             oolfn = gen_helper_crypto_sha512su1;
13832             break;
13833         case 3: /* RAX1 */
13834             feature = dc_isar_feature(aa64_sha3, s);
13835             gvecfn = gen_gvec_rax1;
13836             break;
13837         default:
13838             g_assert_not_reached();
13839         }
13840     } else {
13841         switch (opcode) {
13842         case 0: /* SM3PARTW1 */
13843             feature = dc_isar_feature(aa64_sm3, s);
13844             oolfn = gen_helper_crypto_sm3partw1;
13845             break;
13846         case 1: /* SM3PARTW2 */
13847             feature = dc_isar_feature(aa64_sm3, s);
13848             oolfn = gen_helper_crypto_sm3partw2;
13849             break;
13850         case 2: /* SM4EKEY */
13851             feature = dc_isar_feature(aa64_sm4, s);
13852             oolfn = gen_helper_crypto_sm4ekey;
13853             break;
13854         default:
13855             unallocated_encoding(s);
13856             return;
13857         }
13858     }
13859 
13860     if (!feature) {
13861         unallocated_encoding(s);
13862         return;
13863     }
13864 
13865     if (!fp_access_check(s)) {
13866         return;
13867     }
13868 
13869     if (oolfn) {
13870         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13871     } else {
13872         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13873     }
13874 }
13875 
13876 /* Crypto two-reg SHA512
13877  *  31                                     12  11  10  9    5 4    0
13878  * +-----------------------------------------+--------+------+------+
13879  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13880  * +-----------------------------------------+--------+------+------+
13881  */
13882 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13883 {
13884     int opcode = extract32(insn, 10, 2);
13885     int rn = extract32(insn, 5, 5);
13886     int rd = extract32(insn, 0, 5);
13887     bool feature;
13888 
13889     switch (opcode) {
13890     case 0: /* SHA512SU0 */
13891         feature = dc_isar_feature(aa64_sha512, s);
13892         break;
13893     case 1: /* SM4E */
13894         feature = dc_isar_feature(aa64_sm4, s);
13895         break;
13896     default:
13897         unallocated_encoding(s);
13898         return;
13899     }
13900 
13901     if (!feature) {
13902         unallocated_encoding(s);
13903         return;
13904     }
13905 
13906     if (!fp_access_check(s)) {
13907         return;
13908     }
13909 
13910     switch (opcode) {
13911     case 0: /* SHA512SU0 */
13912         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13913         break;
13914     case 1: /* SM4E */
13915         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13916         break;
13917     default:
13918         g_assert_not_reached();
13919     }
13920 }
13921 
13922 /* Crypto four-register
13923  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13924  * +-------------------+-----+------+---+------+------+------+
13925  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13926  * +-------------------+-----+------+---+------+------+------+
13927  */
13928 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13929 {
13930     int op0 = extract32(insn, 21, 2);
13931     int rm = extract32(insn, 16, 5);
13932     int ra = extract32(insn, 10, 5);
13933     int rn = extract32(insn, 5, 5);
13934     int rd = extract32(insn, 0, 5);
13935     bool feature;
13936 
13937     switch (op0) {
13938     case 0: /* EOR3 */
13939     case 1: /* BCAX */
13940         feature = dc_isar_feature(aa64_sha3, s);
13941         break;
13942     case 2: /* SM3SS1 */
13943         feature = dc_isar_feature(aa64_sm3, s);
13944         break;
13945     default:
13946         unallocated_encoding(s);
13947         return;
13948     }
13949 
13950     if (!feature) {
13951         unallocated_encoding(s);
13952         return;
13953     }
13954 
13955     if (!fp_access_check(s)) {
13956         return;
13957     }
13958 
13959     if (op0 < 2) {
13960         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13961         int pass;
13962 
13963         tcg_op1 = tcg_temp_new_i64();
13964         tcg_op2 = tcg_temp_new_i64();
13965         tcg_op3 = tcg_temp_new_i64();
13966         tcg_res[0] = tcg_temp_new_i64();
13967         tcg_res[1] = tcg_temp_new_i64();
13968 
13969         for (pass = 0; pass < 2; pass++) {
13970             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13971             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13972             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13973 
13974             if (op0 == 0) {
13975                 /* EOR3 */
13976                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13977             } else {
13978                 /* BCAX */
13979                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13980             }
13981             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13982         }
13983         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13984         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13985     } else {
13986         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13987 
13988         tcg_op1 = tcg_temp_new_i32();
13989         tcg_op2 = tcg_temp_new_i32();
13990         tcg_op3 = tcg_temp_new_i32();
13991         tcg_res = tcg_temp_new_i32();
13992         tcg_zero = tcg_constant_i32(0);
13993 
13994         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13995         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13996         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13997 
13998         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13999         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
14000         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
14001         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
14002 
14003         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
14004         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
14005         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
14006         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
14007     }
14008 }
14009 
14010 /* Crypto XAR
14011  *  31                   21 20  16 15    10 9    5 4    0
14012  * +-----------------------+------+--------+------+------+
14013  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
14014  * +-----------------------+------+--------+------+------+
14015  */
14016 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14017 {
14018     int rm = extract32(insn, 16, 5);
14019     int imm6 = extract32(insn, 10, 6);
14020     int rn = extract32(insn, 5, 5);
14021     int rd = extract32(insn, 0, 5);
14022 
14023     if (!dc_isar_feature(aa64_sha3, s)) {
14024         unallocated_encoding(s);
14025         return;
14026     }
14027 
14028     if (!fp_access_check(s)) {
14029         return;
14030     }
14031 
14032     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14033                  vec_full_reg_offset(s, rn),
14034                  vec_full_reg_offset(s, rm), imm6, 16,
14035                  vec_full_reg_size(s));
14036 }
14037 
14038 /* Crypto three-reg imm2
14039  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14040  * +-----------------------+------+-----+------+--------+------+------+
14041  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14042  * +-----------------------+------+-----+------+--------+------+------+
14043  */
14044 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14045 {
14046     static gen_helper_gvec_3 * const fns[4] = {
14047         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14048         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14049     };
14050     int opcode = extract32(insn, 10, 2);
14051     int imm2 = extract32(insn, 12, 2);
14052     int rm = extract32(insn, 16, 5);
14053     int rn = extract32(insn, 5, 5);
14054     int rd = extract32(insn, 0, 5);
14055 
14056     if (!dc_isar_feature(aa64_sm3, s)) {
14057         unallocated_encoding(s);
14058         return;
14059     }
14060 
14061     if (!fp_access_check(s)) {
14062         return;
14063     }
14064 
14065     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14066 }
14067 
14068 /* C3.6 Data processing - SIMD, inc Crypto
14069  *
14070  * As the decode gets a little complex we are using a table based
14071  * approach for this part of the decode.
14072  */
14073 static const AArch64DecodeTable data_proc_simd[] = {
14074     /* pattern  ,  mask     ,  fn                        */
14075     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14076     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14077     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14078     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14079     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14080     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14081     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14082     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14083     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14084     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14085     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14086     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14087     { 0x2e000000, 0xbf208400, disas_simd_ext },
14088     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14089     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14090     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14091     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14092     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14093     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14094     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14095     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14096     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14097     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14098     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14099     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14100     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14101     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14102     { 0xce800000, 0xffe00000, disas_crypto_xar },
14103     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14104     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14105     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14106     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14107     { 0x00000000, 0x00000000, NULL }
14108 };
14109 
14110 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14111 {
14112     /* Note that this is called with all non-FP cases from
14113      * table C3-6 so it must UNDEF for entries not specifically
14114      * allocated to instructions in that table.
14115      */
14116     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14117     if (fn) {
14118         fn(s, insn);
14119     } else {
14120         unallocated_encoding(s);
14121     }
14122 }
14123 
14124 /* C3.6 Data processing - SIMD and floating point */
14125 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14126 {
14127     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14128         disas_data_proc_fp(s, insn);
14129     } else {
14130         /* SIMD, including crypto */
14131         disas_data_proc_simd(s, insn);
14132     }
14133 }
14134 
14135 static bool trans_OK(DisasContext *s, arg_OK *a)
14136 {
14137     return true;
14138 }
14139 
14140 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14141 {
14142     s->is_nonstreaming = true;
14143     return true;
14144 }
14145 
14146 /**
14147  * is_guarded_page:
14148  * @env: The cpu environment
14149  * @s: The DisasContext
14150  *
14151  * Return true if the page is guarded.
14152  */
14153 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14154 {
14155     uint64_t addr = s->base.pc_first;
14156 #ifdef CONFIG_USER_ONLY
14157     return page_get_flags(addr) & PAGE_BTI;
14158 #else
14159     CPUTLBEntryFull *full;
14160     void *host;
14161     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14162     int flags;
14163 
14164     /*
14165      * We test this immediately after reading an insn, which means
14166      * that the TLB entry must be present and valid, and thus this
14167      * access will never raise an exception.
14168      */
14169     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14170                               false, &host, &full, 0);
14171     assert(!(flags & TLB_INVALID_MASK));
14172 
14173     return full->guarded;
14174 #endif
14175 }
14176 
14177 /**
14178  * btype_destination_ok:
14179  * @insn: The instruction at the branch destination
14180  * @bt: SCTLR_ELx.BT
14181  * @btype: PSTATE.BTYPE, and is non-zero
14182  *
14183  * On a guarded page, there are a limited number of insns
14184  * that may be present at the branch target:
14185  *   - branch target identifiers,
14186  *   - paciasp, pacibsp,
14187  *   - BRK insn
14188  *   - HLT insn
14189  * Anything else causes a Branch Target Exception.
14190  *
14191  * Return true if the branch is compatible, false to raise BTITRAP.
14192  */
14193 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14194 {
14195     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14196         /* HINT space */
14197         switch (extract32(insn, 5, 7)) {
14198         case 0b011001: /* PACIASP */
14199         case 0b011011: /* PACIBSP */
14200             /*
14201              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14202              * with btype == 3.  Otherwise all btype are ok.
14203              */
14204             return !bt || btype != 3;
14205         case 0b100000: /* BTI */
14206             /* Not compatible with any btype.  */
14207             return false;
14208         case 0b100010: /* BTI c */
14209             /* Not compatible with btype == 3 */
14210             return btype != 3;
14211         case 0b100100: /* BTI j */
14212             /* Not compatible with btype == 2 */
14213             return btype != 2;
14214         case 0b100110: /* BTI jc */
14215             /* Compatible with any btype.  */
14216             return true;
14217         }
14218     } else {
14219         switch (insn & 0xffe0001fu) {
14220         case 0xd4200000u: /* BRK */
14221         case 0xd4400000u: /* HLT */
14222             /* Give priority to the breakpoint exception.  */
14223             return true;
14224         }
14225     }
14226     return false;
14227 }
14228 
14229 /* C3.1 A64 instruction index by encoding */
14230 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14231 {
14232     switch (extract32(insn, 25, 4)) {
14233     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14234         disas_b_exc_sys(s, insn);
14235         break;
14236     case 0x4:
14237     case 0x6:
14238     case 0xc:
14239     case 0xe:      /* Loads and stores */
14240         disas_ldst(s, insn);
14241         break;
14242     case 0x5:
14243     case 0xd:      /* Data processing - register */
14244         disas_data_proc_reg(s, insn);
14245         break;
14246     case 0x7:
14247     case 0xf:      /* Data processing - SIMD and floating point */
14248         disas_data_proc_simd_fp(s, insn);
14249         break;
14250     default:
14251         unallocated_encoding(s);
14252         break;
14253     }
14254 }
14255 
14256 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14257                                           CPUState *cpu)
14258 {
14259     DisasContext *dc = container_of(dcbase, DisasContext, base);
14260     CPUARMState *env = cpu->env_ptr;
14261     ARMCPU *arm_cpu = env_archcpu(env);
14262     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14263     int bound, core_mmu_idx;
14264 
14265     dc->isar = &arm_cpu->isar;
14266     dc->condjmp = 0;
14267     dc->pc_save = dc->base.pc_first;
14268     dc->aarch64 = true;
14269     dc->thumb = false;
14270     dc->sctlr_b = 0;
14271     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14272     dc->condexec_mask = 0;
14273     dc->condexec_cond = 0;
14274     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14275     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14276     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14277     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14278     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14279     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14280 #if !defined(CONFIG_USER_ONLY)
14281     dc->user = (dc->current_el == 0);
14282 #endif
14283     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14284     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14285     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14286     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14287     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14288     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14289     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14290     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14291     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14292     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14293     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14294     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14295     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14296     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14297     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14298     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14299     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14300     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14301     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14302     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14303     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14304     dc->vec_len = 0;
14305     dc->vec_stride = 0;
14306     dc->cp_regs = arm_cpu->cp_regs;
14307     dc->features = env->features;
14308     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14309 
14310 #ifdef CONFIG_USER_ONLY
14311     /* In sve_probe_page, we assume TBI is enabled. */
14312     tcg_debug_assert(dc->tbid & 1);
14313 #endif
14314 
14315     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14316 
14317     /* Single step state. The code-generation logic here is:
14318      *  SS_ACTIVE == 0:
14319      *   generate code with no special handling for single-stepping (except
14320      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14321      *   this happens anyway because those changes are all system register or
14322      *   PSTATE writes).
14323      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14324      *   emit code for one insn
14325      *   emit code to clear PSTATE.SS
14326      *   emit code to generate software step exception for completed step
14327      *   end TB (as usual for having generated an exception)
14328      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14329      *   emit code to generate a software step exception
14330      *   end the TB
14331      */
14332     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14333     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14334     dc->is_ldex = false;
14335 
14336     /* Bound the number of insns to execute to those left on the page.  */
14337     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14338 
14339     /* If architectural single step active, limit to 1.  */
14340     if (dc->ss_active) {
14341         bound = 1;
14342     }
14343     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14344 }
14345 
14346 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14347 {
14348 }
14349 
14350 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14351 {
14352     DisasContext *dc = container_of(dcbase, DisasContext, base);
14353     target_ulong pc_arg = dc->base.pc_next;
14354 
14355     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14356         pc_arg &= ~TARGET_PAGE_MASK;
14357     }
14358     tcg_gen_insn_start(pc_arg, 0, 0);
14359     dc->insn_start = tcg_last_op();
14360 }
14361 
14362 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14363 {
14364     DisasContext *s = container_of(dcbase, DisasContext, base);
14365     CPUARMState *env = cpu->env_ptr;
14366     uint64_t pc = s->base.pc_next;
14367     uint32_t insn;
14368 
14369     /* Singlestep exceptions have the highest priority. */
14370     if (s->ss_active && !s->pstate_ss) {
14371         /* Singlestep state is Active-pending.
14372          * If we're in this state at the start of a TB then either
14373          *  a) we just took an exception to an EL which is being debugged
14374          *     and this is the first insn in the exception handler
14375          *  b) debug exceptions were masked and we just unmasked them
14376          *     without changing EL (eg by clearing PSTATE.D)
14377          * In either case we're going to take a swstep exception in the
14378          * "did not step an insn" case, and so the syndrome ISV and EX
14379          * bits should be zero.
14380          */
14381         assert(s->base.num_insns == 1);
14382         gen_swstep_exception(s, 0, 0);
14383         s->base.is_jmp = DISAS_NORETURN;
14384         s->base.pc_next = pc + 4;
14385         return;
14386     }
14387 
14388     if (pc & 3) {
14389         /*
14390          * PC alignment fault.  This has priority over the instruction abort
14391          * that we would receive from a translation fault via arm_ldl_code.
14392          * This should only be possible after an indirect branch, at the
14393          * start of the TB.
14394          */
14395         assert(s->base.num_insns == 1);
14396         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14397         s->base.is_jmp = DISAS_NORETURN;
14398         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14399         return;
14400     }
14401 
14402     s->pc_curr = pc;
14403     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14404     s->insn = insn;
14405     s->base.pc_next = pc + 4;
14406 
14407     s->fp_access_checked = false;
14408     s->sve_access_checked = false;
14409 
14410     if (s->pstate_il) {
14411         /*
14412          * Illegal execution state. This has priority over BTI
14413          * exceptions, but comes after instruction abort exceptions.
14414          */
14415         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14416         return;
14417     }
14418 
14419     if (dc_isar_feature(aa64_bti, s)) {
14420         if (s->base.num_insns == 1) {
14421             /*
14422              * At the first insn of the TB, compute s->guarded_page.
14423              * We delayed computing this until successfully reading
14424              * the first insn of the TB, above.  This (mostly) ensures
14425              * that the softmmu tlb entry has been populated, and the
14426              * page table GP bit is available.
14427              *
14428              * Note that we need to compute this even if btype == 0,
14429              * because this value is used for BR instructions later
14430              * where ENV is not available.
14431              */
14432             s->guarded_page = is_guarded_page(env, s);
14433 
14434             /* First insn can have btype set to non-zero.  */
14435             tcg_debug_assert(s->btype >= 0);
14436 
14437             /*
14438              * Note that the Branch Target Exception has fairly high
14439              * priority -- below debugging exceptions but above most
14440              * everything else.  This allows us to handle this now
14441              * instead of waiting until the insn is otherwise decoded.
14442              */
14443             if (s->btype != 0
14444                 && s->guarded_page
14445                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14446                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14447                 return;
14448             }
14449         } else {
14450             /* Not the first insn: btype must be 0.  */
14451             tcg_debug_assert(s->btype == 0);
14452         }
14453     }
14454 
14455     s->is_nonstreaming = false;
14456     if (s->sme_trap_nonstreaming) {
14457         disas_sme_fa64(s, insn);
14458     }
14459 
14460     if (!disas_a64(s, insn) &&
14461         !disas_sme(s, insn) &&
14462         !disas_sve(s, insn)) {
14463         disas_a64_legacy(s, insn);
14464     }
14465 
14466     /*
14467      * After execution of most insns, btype is reset to 0.
14468      * Note that we set btype == -1 when the insn sets btype.
14469      */
14470     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14471         reset_btype(s);
14472     }
14473 }
14474 
14475 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14476 {
14477     DisasContext *dc = container_of(dcbase, DisasContext, base);
14478 
14479     if (unlikely(dc->ss_active)) {
14480         /* Note that this means single stepping WFI doesn't halt the CPU.
14481          * For conditional branch insns this is harmless unreachable code as
14482          * gen_goto_tb() has already handled emitting the debug exception
14483          * (and thus a tb-jump is not possible when singlestepping).
14484          */
14485         switch (dc->base.is_jmp) {
14486         default:
14487             gen_a64_update_pc(dc, 4);
14488             /* fall through */
14489         case DISAS_EXIT:
14490         case DISAS_JUMP:
14491             gen_step_complete_exception(dc);
14492             break;
14493         case DISAS_NORETURN:
14494             break;
14495         }
14496     } else {
14497         switch (dc->base.is_jmp) {
14498         case DISAS_NEXT:
14499         case DISAS_TOO_MANY:
14500             gen_goto_tb(dc, 1, 4);
14501             break;
14502         default:
14503         case DISAS_UPDATE_EXIT:
14504             gen_a64_update_pc(dc, 4);
14505             /* fall through */
14506         case DISAS_EXIT:
14507             tcg_gen_exit_tb(NULL, 0);
14508             break;
14509         case DISAS_UPDATE_NOCHAIN:
14510             gen_a64_update_pc(dc, 4);
14511             /* fall through */
14512         case DISAS_JUMP:
14513             tcg_gen_lookup_and_goto_ptr();
14514             break;
14515         case DISAS_NORETURN:
14516         case DISAS_SWI:
14517             break;
14518         case DISAS_WFE:
14519             gen_a64_update_pc(dc, 4);
14520             gen_helper_wfe(cpu_env);
14521             break;
14522         case DISAS_YIELD:
14523             gen_a64_update_pc(dc, 4);
14524             gen_helper_yield(cpu_env);
14525             break;
14526         case DISAS_WFI:
14527             /*
14528              * This is a special case because we don't want to just halt
14529              * the CPU if trying to debug across a WFI.
14530              */
14531             gen_a64_update_pc(dc, 4);
14532             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14533             /*
14534              * The helper doesn't necessarily throw an exception, but we
14535              * must go back to the main loop to check for interrupts anyway.
14536              */
14537             tcg_gen_exit_tb(NULL, 0);
14538             break;
14539         }
14540     }
14541 }
14542 
14543 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14544                                  CPUState *cpu, FILE *logfile)
14545 {
14546     DisasContext *dc = container_of(dcbase, DisasContext, base);
14547 
14548     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14549     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14550 }
14551 
14552 const TranslatorOps aarch64_translator_ops = {
14553     .init_disas_context = aarch64_tr_init_disas_context,
14554     .tb_start           = aarch64_tr_tb_start,
14555     .insn_start         = aarch64_tr_insn_start,
14556     .translate_insn     = aarch64_tr_translate_insn,
14557     .tb_stop            = aarch64_tr_tb_stop,
14558     .disas_log          = aarch64_tr_disas_log,
14559 };
14560