xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 6e3c8049)
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 bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1816 {
1817     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1818     return true;
1819 }
1820 
1821 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1822 {
1823     /* We handle DSB and DMB the same way */
1824     TCGBar bar;
1825 
1826     switch (a->types) {
1827     case 1: /* MBReqTypes_Reads */
1828         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1829         break;
1830     case 2: /* MBReqTypes_Writes */
1831         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1832         break;
1833     default: /* MBReqTypes_All */
1834         bar = TCG_BAR_SC | TCG_MO_ALL;
1835         break;
1836     }
1837     tcg_gen_mb(bar);
1838     return true;
1839 }
1840 
1841 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1842 {
1843     /*
1844      * We need to break the TB after this insn to execute
1845      * self-modifying code correctly and also to take
1846      * any pending interrupts immediately.
1847      */
1848     reset_btype(s);
1849     gen_goto_tb(s, 0, 4);
1850     return true;
1851 }
1852 
1853 static bool trans_SB(DisasContext *s, arg_SB *a)
1854 {
1855     if (!dc_isar_feature(aa64_sb, s)) {
1856         return false;
1857     }
1858     /*
1859      * TODO: There is no speculation barrier opcode for TCG;
1860      * MB and end the TB instead.
1861      */
1862     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1863     gen_goto_tb(s, 0, 4);
1864     return true;
1865 }
1866 
1867 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1868 {
1869     if (!dc_isar_feature(aa64_condm_4, s)) {
1870         return false;
1871     }
1872     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1873     return true;
1874 }
1875 
1876 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1877 {
1878     TCGv_i32 z;
1879 
1880     if (!dc_isar_feature(aa64_condm_5, s)) {
1881         return false;
1882     }
1883 
1884     z = tcg_temp_new_i32();
1885 
1886     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1887 
1888     /*
1889      * (!C & !Z) << 31
1890      * (!(C | Z)) << 31
1891      * ~((C | Z) << 31)
1892      * ~-(C | Z)
1893      * (C | Z) - 1
1894      */
1895     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1896     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1897 
1898     /* !(Z & C) */
1899     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1900     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1901 
1902     /* (!C & Z) << 31 -> -(Z & ~C) */
1903     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1904     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1905 
1906     /* C | Z */
1907     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1908 
1909     return true;
1910 }
1911 
1912 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1913 {
1914     if (!dc_isar_feature(aa64_condm_5, s)) {
1915         return false;
1916     }
1917 
1918     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1919     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1920 
1921     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1922     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1923 
1924     tcg_gen_movi_i32(cpu_NF, 0);
1925     tcg_gen_movi_i32(cpu_VF, 0);
1926 
1927     return true;
1928 }
1929 
1930 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1931 {
1932     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1933         return false;
1934     }
1935     if (a->imm & 1) {
1936         set_pstate_bits(PSTATE_UAO);
1937     } else {
1938         clear_pstate_bits(PSTATE_UAO);
1939     }
1940     gen_rebuild_hflags(s);
1941     s->base.is_jmp = DISAS_TOO_MANY;
1942     return true;
1943 }
1944 
1945 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1946 {
1947     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1948         return false;
1949     }
1950     if (a->imm & 1) {
1951         set_pstate_bits(PSTATE_PAN);
1952     } else {
1953         clear_pstate_bits(PSTATE_PAN);
1954     }
1955     gen_rebuild_hflags(s);
1956     s->base.is_jmp = DISAS_TOO_MANY;
1957     return true;
1958 }
1959 
1960 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1961 {
1962     if (s->current_el == 0) {
1963         return false;
1964     }
1965     gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(a->imm & PSTATE_SP));
1966     s->base.is_jmp = DISAS_TOO_MANY;
1967     return true;
1968 }
1969 
1970 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
1971 {
1972     if (!dc_isar_feature(aa64_ssbs, s)) {
1973         return false;
1974     }
1975     if (a->imm & 1) {
1976         set_pstate_bits(PSTATE_SSBS);
1977     } else {
1978         clear_pstate_bits(PSTATE_SSBS);
1979     }
1980     /* Don't need to rebuild hflags since SSBS is a nop */
1981     s->base.is_jmp = DISAS_TOO_MANY;
1982     return true;
1983 }
1984 
1985 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
1986 {
1987     if (!dc_isar_feature(aa64_dit, s)) {
1988         return false;
1989     }
1990     if (a->imm & 1) {
1991         set_pstate_bits(PSTATE_DIT);
1992     } else {
1993         clear_pstate_bits(PSTATE_DIT);
1994     }
1995     /* There's no need to rebuild hflags because DIT is a nop */
1996     s->base.is_jmp = DISAS_TOO_MANY;
1997     return true;
1998 }
1999 
2000 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2001 {
2002     if (dc_isar_feature(aa64_mte, s)) {
2003         /* Full MTE is enabled -- set the TCO bit as directed. */
2004         if (a->imm & 1) {
2005             set_pstate_bits(PSTATE_TCO);
2006         } else {
2007             clear_pstate_bits(PSTATE_TCO);
2008         }
2009         gen_rebuild_hflags(s);
2010         /* Many factors, including TCO, go into MTE_ACTIVE. */
2011         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2012         return true;
2013     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2014         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2015         return true;
2016     } else {
2017         /* Insn not present */
2018         return false;
2019     }
2020 }
2021 
2022 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2023 {
2024     gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(a->imm));
2025     s->base.is_jmp = DISAS_TOO_MANY;
2026     return true;
2027 }
2028 
2029 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2030 {
2031     gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(a->imm));
2032     /* Exit the cpu loop to re-evaluate pending IRQs. */
2033     s->base.is_jmp = DISAS_UPDATE_EXIT;
2034     return true;
2035 }
2036 
2037 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2038 {
2039     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2040         return false;
2041     }
2042     if (sme_access_check(s)) {
2043         int old = s->pstate_sm | (s->pstate_za << 1);
2044         int new = a->imm * 3;
2045 
2046         if ((old ^ new) & a->mask) {
2047             /* At least one bit changes. */
2048             gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2049                                 tcg_constant_i32(a->mask));
2050             s->base.is_jmp = DISAS_TOO_MANY;
2051         }
2052     }
2053     return true;
2054 }
2055 
2056 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2057 {
2058     TCGv_i32 tmp = tcg_temp_new_i32();
2059     TCGv_i32 nzcv = tcg_temp_new_i32();
2060 
2061     /* build bit 31, N */
2062     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2063     /* build bit 30, Z */
2064     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2065     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2066     /* build bit 29, C */
2067     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2068     /* build bit 28, V */
2069     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2070     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2071     /* generate result */
2072     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2073 }
2074 
2075 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2076 {
2077     TCGv_i32 nzcv = tcg_temp_new_i32();
2078 
2079     /* take NZCV from R[t] */
2080     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2081 
2082     /* bit 31, N */
2083     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2084     /* bit 30, Z */
2085     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2086     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2087     /* bit 29, C */
2088     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2089     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2090     /* bit 28, V */
2091     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2092     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2093 }
2094 
2095 static void gen_sysreg_undef(DisasContext *s, bool isread,
2096                              uint8_t op0, uint8_t op1, uint8_t op2,
2097                              uint8_t crn, uint8_t crm, uint8_t rt)
2098 {
2099     /*
2100      * Generate code to emit an UNDEF with correct syndrome
2101      * information for a failed system register access.
2102      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2103      * but if FEAT_IDST is implemented then read accesses to registers
2104      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2105      * syndrome.
2106      */
2107     uint32_t syndrome;
2108 
2109     if (isread && dc_isar_feature(aa64_ids, s) &&
2110         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2111         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2112     } else {
2113         syndrome = syn_uncategorized();
2114     }
2115     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2116 }
2117 
2118 /* MRS - move from system register
2119  * MSR (register) - move to system register
2120  * SYS
2121  * SYSL
2122  * These are all essentially the same insn in 'read' and 'write'
2123  * versions, with varying op0 fields.
2124  */
2125 static void handle_sys(DisasContext *s, bool isread,
2126                        unsigned int op0, unsigned int op1, unsigned int op2,
2127                        unsigned int crn, unsigned int crm, unsigned int rt)
2128 {
2129     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2130                                       crn, crm, op0, op1, op2);
2131     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2132     bool need_exit_tb = false;
2133     TCGv_ptr tcg_ri = NULL;
2134     TCGv_i64 tcg_rt;
2135 
2136     if (!ri) {
2137         /* Unknown register; this might be a guest error or a QEMU
2138          * unimplemented feature.
2139          */
2140         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2141                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2142                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2143         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2144         return;
2145     }
2146 
2147     /* Check access permissions */
2148     if (!cp_access_ok(s->current_el, ri, isread)) {
2149         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2150         return;
2151     }
2152 
2153     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2154         /* Emit code to perform further access permissions checks at
2155          * runtime; this may result in an exception.
2156          */
2157         uint32_t syndrome;
2158 
2159         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2160         gen_a64_update_pc(s, 0);
2161         tcg_ri = tcg_temp_new_ptr();
2162         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2163                                        tcg_constant_i32(key),
2164                                        tcg_constant_i32(syndrome),
2165                                        tcg_constant_i32(isread));
2166     } else if (ri->type & ARM_CP_RAISES_EXC) {
2167         /*
2168          * The readfn or writefn might raise an exception;
2169          * synchronize the CPU state in case it does.
2170          */
2171         gen_a64_update_pc(s, 0);
2172     }
2173 
2174     /* Handle special cases first */
2175     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2176     case 0:
2177         break;
2178     case ARM_CP_NOP:
2179         return;
2180     case ARM_CP_NZCV:
2181         tcg_rt = cpu_reg(s, rt);
2182         if (isread) {
2183             gen_get_nzcv(tcg_rt);
2184         } else {
2185             gen_set_nzcv(tcg_rt);
2186         }
2187         return;
2188     case ARM_CP_CURRENTEL:
2189         /* Reads as current EL value from pstate, which is
2190          * guaranteed to be constant by the tb flags.
2191          */
2192         tcg_rt = cpu_reg(s, rt);
2193         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2194         return;
2195     case ARM_CP_DC_ZVA:
2196         /* Writes clear the aligned block of memory which rt points into. */
2197         if (s->mte_active[0]) {
2198             int desc = 0;
2199 
2200             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2201             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2202             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2203 
2204             tcg_rt = tcg_temp_new_i64();
2205             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2206                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2207         } else {
2208             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2209         }
2210         gen_helper_dc_zva(cpu_env, tcg_rt);
2211         return;
2212     case ARM_CP_DC_GVA:
2213         {
2214             TCGv_i64 clean_addr, tag;
2215 
2216             /*
2217              * DC_GVA, like DC_ZVA, requires that we supply the original
2218              * pointer for an invalid page.  Probe that address first.
2219              */
2220             tcg_rt = cpu_reg(s, rt);
2221             clean_addr = clean_data_tbi(s, tcg_rt);
2222             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2223 
2224             if (s->ata) {
2225                 /* Extract the tag from the register to match STZGM.  */
2226                 tag = tcg_temp_new_i64();
2227                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2228                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2229             }
2230         }
2231         return;
2232     case ARM_CP_DC_GZVA:
2233         {
2234             TCGv_i64 clean_addr, tag;
2235 
2236             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2237             tcg_rt = cpu_reg(s, rt);
2238             clean_addr = clean_data_tbi(s, tcg_rt);
2239             gen_helper_dc_zva(cpu_env, clean_addr);
2240 
2241             if (s->ata) {
2242                 /* Extract the tag from the register to match STZGM.  */
2243                 tag = tcg_temp_new_i64();
2244                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2245                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2246             }
2247         }
2248         return;
2249     default:
2250         g_assert_not_reached();
2251     }
2252     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2253         return;
2254     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2255         return;
2256     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2257         return;
2258     }
2259 
2260     if (ri->type & ARM_CP_IO) {
2261         /* I/O operations must end the TB here (whether read or write) */
2262         need_exit_tb = translator_io_start(&s->base);
2263     }
2264 
2265     tcg_rt = cpu_reg(s, rt);
2266 
2267     if (isread) {
2268         if (ri->type & ARM_CP_CONST) {
2269             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2270         } else if (ri->readfn) {
2271             if (!tcg_ri) {
2272                 tcg_ri = gen_lookup_cp_reg(key);
2273             }
2274             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2275         } else {
2276             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2277         }
2278     } else {
2279         if (ri->type & ARM_CP_CONST) {
2280             /* If not forbidden by access permissions, treat as WI */
2281             return;
2282         } else if (ri->writefn) {
2283             if (!tcg_ri) {
2284                 tcg_ri = gen_lookup_cp_reg(key);
2285             }
2286             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2287         } else {
2288             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2289         }
2290     }
2291 
2292     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2293         /*
2294          * A write to any coprocessor regiser that ends a TB
2295          * must rebuild the hflags for the next TB.
2296          */
2297         gen_rebuild_hflags(s);
2298         /*
2299          * We default to ending the TB on a coprocessor register write,
2300          * but allow this to be suppressed by the register definition
2301          * (usually only necessary to work around guest bugs).
2302          */
2303         need_exit_tb = true;
2304     }
2305     if (need_exit_tb) {
2306         s->base.is_jmp = DISAS_UPDATE_EXIT;
2307     }
2308 }
2309 
2310 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2311 {
2312     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2313     return true;
2314 }
2315 
2316 /* Exception generation
2317  *
2318  *  31             24 23 21 20                     5 4   2 1  0
2319  * +-----------------+-----+------------------------+-----+----+
2320  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2321  * +-----------------------+------------------------+----------+
2322  */
2323 static void disas_exc(DisasContext *s, uint32_t insn)
2324 {
2325     int opc = extract32(insn, 21, 3);
2326     int op2_ll = extract32(insn, 0, 5);
2327     int imm16 = extract32(insn, 5, 16);
2328     uint32_t syndrome;
2329 
2330     switch (opc) {
2331     case 0:
2332         /* For SVC, HVC and SMC we advance the single-step state
2333          * machine before taking the exception. This is architecturally
2334          * mandated, to ensure that single-stepping a system call
2335          * instruction works properly.
2336          */
2337         switch (op2_ll) {
2338         case 1:                                                     /* SVC */
2339             syndrome = syn_aa64_svc(imm16);
2340             if (s->fgt_svc) {
2341                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2342                 break;
2343             }
2344             gen_ss_advance(s);
2345             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2346             break;
2347         case 2:                                                     /* HVC */
2348             if (s->current_el == 0) {
2349                 unallocated_encoding(s);
2350                 break;
2351             }
2352             /* The pre HVC helper handles cases when HVC gets trapped
2353              * as an undefined insn by runtime configuration.
2354              */
2355             gen_a64_update_pc(s, 0);
2356             gen_helper_pre_hvc(cpu_env);
2357             gen_ss_advance(s);
2358             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2359             break;
2360         case 3:                                                     /* SMC */
2361             if (s->current_el == 0) {
2362                 unallocated_encoding(s);
2363                 break;
2364             }
2365             gen_a64_update_pc(s, 0);
2366             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2367             gen_ss_advance(s);
2368             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2369             break;
2370         default:
2371             unallocated_encoding(s);
2372             break;
2373         }
2374         break;
2375     case 1:
2376         if (op2_ll != 0) {
2377             unallocated_encoding(s);
2378             break;
2379         }
2380         /* BRK */
2381         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2382         break;
2383     case 2:
2384         if (op2_ll != 0) {
2385             unallocated_encoding(s);
2386             break;
2387         }
2388         /* HLT. This has two purposes.
2389          * Architecturally, it is an external halting debug instruction.
2390          * Since QEMU doesn't implement external debug, we treat this as
2391          * it is required for halting debug disabled: it will UNDEF.
2392          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2393          */
2394         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2395             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2396         } else {
2397             unallocated_encoding(s);
2398         }
2399         break;
2400     case 5:
2401         if (op2_ll < 1 || op2_ll > 3) {
2402             unallocated_encoding(s);
2403             break;
2404         }
2405         /* DCPS1, DCPS2, DCPS3 */
2406         unallocated_encoding(s);
2407         break;
2408     default:
2409         unallocated_encoding(s);
2410         break;
2411     }
2412 }
2413 
2414 /* Branches, exception generating and system instructions */
2415 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2416 {
2417     switch (extract32(insn, 25, 7)) {
2418     case 0x6a: /* Exception generation / System */
2419         if (insn & (1 << 24)) {
2420             unallocated_encoding(s);
2421         } else {
2422             disas_exc(s, insn);
2423         }
2424         break;
2425     default:
2426         unallocated_encoding(s);
2427         break;
2428     }
2429 }
2430 
2431 /*
2432  * Load/Store exclusive instructions are implemented by remembering
2433  * the value/address loaded, and seeing if these are the same
2434  * when the store is performed. This is not actually the architecturally
2435  * mandated semantics, but it works for typical guest code sequences
2436  * and avoids having to monitor regular stores.
2437  *
2438  * The store exclusive uses the atomic cmpxchg primitives to avoid
2439  * races in multi-threaded linux-user and when MTTCG softmmu is
2440  * enabled.
2441  */
2442 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2443                                int size, bool is_pair)
2444 {
2445     int idx = get_mem_index(s);
2446     TCGv_i64 dirty_addr, clean_addr;
2447     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2448 
2449     s->is_ldex = true;
2450     dirty_addr = cpu_reg_sp(s, rn);
2451     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2452 
2453     g_assert(size <= 3);
2454     if (is_pair) {
2455         g_assert(size >= 2);
2456         if (size == 2) {
2457             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2458             if (s->be_data == MO_LE) {
2459                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2460                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2461             } else {
2462                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2463                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2464             }
2465         } else {
2466             TCGv_i128 t16 = tcg_temp_new_i128();
2467 
2468             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2469 
2470             if (s->be_data == MO_LE) {
2471                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2472                                       cpu_exclusive_high, t16);
2473             } else {
2474                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2475                                       cpu_exclusive_val, t16);
2476             }
2477             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2478             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2479         }
2480     } else {
2481         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2482         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2483     }
2484     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2485 }
2486 
2487 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2488                                 int rn, int size, int is_pair)
2489 {
2490     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2491      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2492      *     [addr] = {Rt};
2493      *     if (is_pair) {
2494      *         [addr + datasize] = {Rt2};
2495      *     }
2496      *     {Rd} = 0;
2497      * } else {
2498      *     {Rd} = 1;
2499      * }
2500      * env->exclusive_addr = -1;
2501      */
2502     TCGLabel *fail_label = gen_new_label();
2503     TCGLabel *done_label = gen_new_label();
2504     TCGv_i64 tmp, clean_addr;
2505     MemOp memop;
2506 
2507     /*
2508      * FIXME: We are out of spec here.  We have recorded only the address
2509      * from load_exclusive, not the entire range, and we assume that the
2510      * size of the access on both sides match.  The architecture allows the
2511      * store to be smaller than the load, so long as the stored bytes are
2512      * within the range recorded by the load.
2513      */
2514 
2515     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2516     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2517     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2518 
2519     /*
2520      * The write, and any associated faults, only happen if the virtual
2521      * and physical addresses pass the exclusive monitor check.  These
2522      * faults are exceedingly unlikely, because normally the guest uses
2523      * the exact same address register for the load_exclusive, and we
2524      * would have recognized these faults there.
2525      *
2526      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2527      * unaligned 4-byte write within the range of an aligned 8-byte load.
2528      * With LSE2, the store would need to cross a 16-byte boundary when the
2529      * load did not, which would mean the store is outside the range
2530      * recorded for the monitor, which would have failed a corrected monitor
2531      * check above.  For now, we assume no size change and retain the
2532      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2533      *
2534      * It is possible to trigger an MTE fault, by performing the load with
2535      * a virtual address with a valid tag and performing the store with the
2536      * same virtual address and a different invalid tag.
2537      */
2538     memop = size + is_pair;
2539     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2540         memop |= MO_ALIGN;
2541     }
2542     memop = finalize_memop(s, memop);
2543     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2544 
2545     tmp = tcg_temp_new_i64();
2546     if (is_pair) {
2547         if (size == 2) {
2548             if (s->be_data == MO_LE) {
2549                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2550             } else {
2551                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2552             }
2553             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2554                                        cpu_exclusive_val, tmp,
2555                                        get_mem_index(s), memop);
2556             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2557         } else {
2558             TCGv_i128 t16 = tcg_temp_new_i128();
2559             TCGv_i128 c16 = tcg_temp_new_i128();
2560             TCGv_i64 a, b;
2561 
2562             if (s->be_data == MO_LE) {
2563                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2564                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2565                                         cpu_exclusive_high);
2566             } else {
2567                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2568                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2569                                         cpu_exclusive_val);
2570             }
2571 
2572             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2573                                         get_mem_index(s), memop);
2574 
2575             a = tcg_temp_new_i64();
2576             b = tcg_temp_new_i64();
2577             if (s->be_data == MO_LE) {
2578                 tcg_gen_extr_i128_i64(a, b, t16);
2579             } else {
2580                 tcg_gen_extr_i128_i64(b, a, t16);
2581             }
2582 
2583             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2584             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2585             tcg_gen_or_i64(tmp, a, b);
2586 
2587             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2588         }
2589     } else {
2590         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2591                                    cpu_reg(s, rt), get_mem_index(s), memop);
2592         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2593     }
2594     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2595     tcg_gen_br(done_label);
2596 
2597     gen_set_label(fail_label);
2598     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2599     gen_set_label(done_label);
2600     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2601 }
2602 
2603 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2604                                  int rn, int size)
2605 {
2606     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2607     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2608     int memidx = get_mem_index(s);
2609     TCGv_i64 clean_addr;
2610     MemOp memop;
2611 
2612     if (rn == 31) {
2613         gen_check_sp_alignment(s);
2614     }
2615     memop = check_atomic_align(s, rn, size);
2616     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2617     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2618                                memidx, memop);
2619 }
2620 
2621 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2622                                       int rn, int size)
2623 {
2624     TCGv_i64 s1 = cpu_reg(s, rs);
2625     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2626     TCGv_i64 t1 = cpu_reg(s, rt);
2627     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2628     TCGv_i64 clean_addr;
2629     int memidx = get_mem_index(s);
2630     MemOp memop;
2631 
2632     if (rn == 31) {
2633         gen_check_sp_alignment(s);
2634     }
2635 
2636     /* This is a single atomic access, despite the "pair". */
2637     memop = check_atomic_align(s, rn, size + 1);
2638     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2639 
2640     if (size == 2) {
2641         TCGv_i64 cmp = tcg_temp_new_i64();
2642         TCGv_i64 val = tcg_temp_new_i64();
2643 
2644         if (s->be_data == MO_LE) {
2645             tcg_gen_concat32_i64(val, t1, t2);
2646             tcg_gen_concat32_i64(cmp, s1, s2);
2647         } else {
2648             tcg_gen_concat32_i64(val, t2, t1);
2649             tcg_gen_concat32_i64(cmp, s2, s1);
2650         }
2651 
2652         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2653 
2654         if (s->be_data == MO_LE) {
2655             tcg_gen_extr32_i64(s1, s2, cmp);
2656         } else {
2657             tcg_gen_extr32_i64(s2, s1, cmp);
2658         }
2659     } else {
2660         TCGv_i128 cmp = tcg_temp_new_i128();
2661         TCGv_i128 val = tcg_temp_new_i128();
2662 
2663         if (s->be_data == MO_LE) {
2664             tcg_gen_concat_i64_i128(val, t1, t2);
2665             tcg_gen_concat_i64_i128(cmp, s1, s2);
2666         } else {
2667             tcg_gen_concat_i64_i128(val, t2, t1);
2668             tcg_gen_concat_i64_i128(cmp, s2, s1);
2669         }
2670 
2671         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2672 
2673         if (s->be_data == MO_LE) {
2674             tcg_gen_extr_i128_i64(s1, s2, cmp);
2675         } else {
2676             tcg_gen_extr_i128_i64(s2, s1, cmp);
2677         }
2678     }
2679 }
2680 
2681 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2682  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2683  */
2684 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2685 {
2686     int opc0 = extract32(opc, 0, 1);
2687     int regsize;
2688 
2689     if (is_signed) {
2690         regsize = opc0 ? 32 : 64;
2691     } else {
2692         regsize = size == 3 ? 64 : 32;
2693     }
2694     return regsize == 64;
2695 }
2696 
2697 /* Load/store exclusive
2698  *
2699  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2700  * +-----+-------------+----+---+----+------+----+-------+------+------+
2701  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2702  * +-----+-------------+----+---+----+------+----+-------+------+------+
2703  *
2704  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2705  *   L: 0 -> store, 1 -> load
2706  *  o2: 0 -> exclusive, 1 -> not
2707  *  o1: 0 -> single register, 1 -> register pair
2708  *  o0: 1 -> load-acquire/store-release, 0 -> not
2709  */
2710 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2711 {
2712     int rt = extract32(insn, 0, 5);
2713     int rn = extract32(insn, 5, 5);
2714     int rt2 = extract32(insn, 10, 5);
2715     int rs = extract32(insn, 16, 5);
2716     int is_lasr = extract32(insn, 15, 1);
2717     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2718     int size = extract32(insn, 30, 2);
2719     TCGv_i64 clean_addr;
2720     MemOp memop;
2721 
2722     switch (o2_L_o1_o0) {
2723     case 0x0: /* STXR */
2724     case 0x1: /* STLXR */
2725         if (rn == 31) {
2726             gen_check_sp_alignment(s);
2727         }
2728         if (is_lasr) {
2729             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2730         }
2731         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2732         return;
2733 
2734     case 0x4: /* LDXR */
2735     case 0x5: /* LDAXR */
2736         if (rn == 31) {
2737             gen_check_sp_alignment(s);
2738         }
2739         gen_load_exclusive(s, rt, rt2, rn, size, false);
2740         if (is_lasr) {
2741             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2742         }
2743         return;
2744 
2745     case 0x8: /* STLLR */
2746         if (!dc_isar_feature(aa64_lor, s)) {
2747             break;
2748         }
2749         /* StoreLORelease is the same as Store-Release for QEMU.  */
2750         /* fall through */
2751     case 0x9: /* STLR */
2752         /* Generate ISS for non-exclusive accesses including LASR.  */
2753         if (rn == 31) {
2754             gen_check_sp_alignment(s);
2755         }
2756         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2757         memop = check_ordered_align(s, rn, 0, true, size);
2758         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2759                                     true, rn != 31, memop);
2760         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2761                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2762         return;
2763 
2764     case 0xc: /* LDLAR */
2765         if (!dc_isar_feature(aa64_lor, s)) {
2766             break;
2767         }
2768         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2769         /* fall through */
2770     case 0xd: /* LDAR */
2771         /* Generate ISS for non-exclusive accesses including LASR.  */
2772         if (rn == 31) {
2773             gen_check_sp_alignment(s);
2774         }
2775         memop = check_ordered_align(s, rn, 0, false, size);
2776         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2777                                     false, rn != 31, memop);
2778         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2779                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2780         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2781         return;
2782 
2783     case 0x2: case 0x3: /* CASP / STXP */
2784         if (size & 2) { /* STXP / STLXP */
2785             if (rn == 31) {
2786                 gen_check_sp_alignment(s);
2787             }
2788             if (is_lasr) {
2789                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2790             }
2791             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2792             return;
2793         }
2794         if (rt2 == 31
2795             && ((rt | rs) & 1) == 0
2796             && dc_isar_feature(aa64_atomics, s)) {
2797             /* CASP / CASPL */
2798             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2799             return;
2800         }
2801         break;
2802 
2803     case 0x6: case 0x7: /* CASPA / LDXP */
2804         if (size & 2) { /* LDXP / LDAXP */
2805             if (rn == 31) {
2806                 gen_check_sp_alignment(s);
2807             }
2808             gen_load_exclusive(s, rt, rt2, rn, size, true);
2809             if (is_lasr) {
2810                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2811             }
2812             return;
2813         }
2814         if (rt2 == 31
2815             && ((rt | rs) & 1) == 0
2816             && dc_isar_feature(aa64_atomics, s)) {
2817             /* CASPA / CASPAL */
2818             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2819             return;
2820         }
2821         break;
2822 
2823     case 0xa: /* CAS */
2824     case 0xb: /* CASL */
2825     case 0xe: /* CASA */
2826     case 0xf: /* CASAL */
2827         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2828             gen_compare_and_swap(s, rs, rt, rn, size);
2829             return;
2830         }
2831         break;
2832     }
2833     unallocated_encoding(s);
2834 }
2835 
2836 /*
2837  * Load register (literal)
2838  *
2839  *  31 30 29   27  26 25 24 23                5 4     0
2840  * +-----+-------+---+-----+-------------------+-------+
2841  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2842  * +-----+-------+---+-----+-------------------+-------+
2843  *
2844  * V: 1 -> vector (simd/fp)
2845  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2846  *                   10-> 32 bit signed, 11 -> prefetch
2847  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2848  */
2849 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2850 {
2851     int rt = extract32(insn, 0, 5);
2852     int64_t imm = sextract32(insn, 5, 19) << 2;
2853     bool is_vector = extract32(insn, 26, 1);
2854     int opc = extract32(insn, 30, 2);
2855     bool is_signed = false;
2856     int size = 2;
2857     TCGv_i64 tcg_rt, clean_addr;
2858     MemOp memop;
2859 
2860     if (is_vector) {
2861         if (opc == 3) {
2862             unallocated_encoding(s);
2863             return;
2864         }
2865         size = 2 + opc;
2866         if (!fp_access_check(s)) {
2867             return;
2868         }
2869         memop = finalize_memop_asimd(s, size);
2870     } else {
2871         if (opc == 3) {
2872             /* PRFM (literal) : prefetch */
2873             return;
2874         }
2875         size = 2 + extract32(opc, 0, 1);
2876         is_signed = extract32(opc, 1, 1);
2877         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2878     }
2879 
2880     tcg_rt = cpu_reg(s, rt);
2881 
2882     clean_addr = tcg_temp_new_i64();
2883     gen_pc_plus_diff(s, clean_addr, imm);
2884 
2885     if (is_vector) {
2886         do_fp_ld(s, rt, clean_addr, memop);
2887     } else {
2888         /* Only unsigned 32bit loads target 32bit registers.  */
2889         bool iss_sf = opc != 0;
2890         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2891     }
2892 }
2893 
2894 /*
2895  * LDNP (Load Pair - non-temporal hint)
2896  * LDP (Load Pair - non vector)
2897  * LDPSW (Load Pair Signed Word - non vector)
2898  * STNP (Store Pair - non-temporal hint)
2899  * STP (Store Pair - non vector)
2900  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2901  * LDP (Load Pair of SIMD&FP)
2902  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2903  * STP (Store Pair of SIMD&FP)
2904  *
2905  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2906  * +-----+-------+---+---+-------+---+-----------------------------+
2907  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2908  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2909  *
2910  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2911  *      LDPSW/STGP               01
2912  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2913  *   V: 0 -> GPR, 1 -> Vector
2914  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2915  *      10 -> signed offset, 11 -> pre-index
2916  *   L: 0 -> Store 1 -> Load
2917  *
2918  * Rt, Rt2 = GPR or SIMD registers to be stored
2919  * Rn = general purpose register containing address
2920  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2921  */
2922 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2923 {
2924     int rt = extract32(insn, 0, 5);
2925     int rn = extract32(insn, 5, 5);
2926     int rt2 = extract32(insn, 10, 5);
2927     uint64_t offset = sextract64(insn, 15, 7);
2928     int index = extract32(insn, 23, 2);
2929     bool is_vector = extract32(insn, 26, 1);
2930     bool is_load = extract32(insn, 22, 1);
2931     int opc = extract32(insn, 30, 2);
2932     bool is_signed = false;
2933     bool postindex = false;
2934     bool wback = false;
2935     bool set_tag = false;
2936     TCGv_i64 clean_addr, dirty_addr;
2937     MemOp mop;
2938     int size;
2939 
2940     if (opc == 3) {
2941         unallocated_encoding(s);
2942         return;
2943     }
2944 
2945     if (is_vector) {
2946         size = 2 + opc;
2947     } else if (opc == 1 && !is_load) {
2948         /* STGP */
2949         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2950             unallocated_encoding(s);
2951             return;
2952         }
2953         size = 3;
2954         set_tag = true;
2955     } else {
2956         size = 2 + extract32(opc, 1, 1);
2957         is_signed = extract32(opc, 0, 1);
2958         if (!is_load && is_signed) {
2959             unallocated_encoding(s);
2960             return;
2961         }
2962     }
2963 
2964     switch (index) {
2965     case 1: /* post-index */
2966         postindex = true;
2967         wback = true;
2968         break;
2969     case 0:
2970         /* signed offset with "non-temporal" hint. Since we don't emulate
2971          * caches we don't care about hints to the cache system about
2972          * data access patterns, and handle this identically to plain
2973          * signed offset.
2974          */
2975         if (is_signed) {
2976             /* There is no non-temporal-hint version of LDPSW */
2977             unallocated_encoding(s);
2978             return;
2979         }
2980         postindex = false;
2981         break;
2982     case 2: /* signed offset, rn not updated */
2983         postindex = false;
2984         break;
2985     case 3: /* pre-index */
2986         postindex = false;
2987         wback = true;
2988         break;
2989     }
2990 
2991     if (is_vector && !fp_access_check(s)) {
2992         return;
2993     }
2994 
2995     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2996 
2997     if (rn == 31) {
2998         gen_check_sp_alignment(s);
2999     }
3000 
3001     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3002     if (!postindex) {
3003         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3004     }
3005 
3006     if (set_tag) {
3007         if (!s->ata) {
3008             /*
3009              * TODO: We could rely on the stores below, at least for
3010              * system mode, if we arrange to add MO_ALIGN_16.
3011              */
3012             gen_helper_stg_stub(cpu_env, dirty_addr);
3013         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3014             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3015         } else {
3016             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3017         }
3018     }
3019 
3020     if (is_vector) {
3021         mop = finalize_memop_asimd(s, size);
3022     } else {
3023         mop = finalize_memop(s, size);
3024     }
3025     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3026                                 (wback || rn != 31) && !set_tag,
3027                                 2 << size, mop);
3028 
3029     if (is_vector) {
3030         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3031         if (is_load) {
3032             do_fp_ld(s, rt, clean_addr, mop);
3033         } else {
3034             do_fp_st(s, rt, clean_addr, mop);
3035         }
3036         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3037         if (is_load) {
3038             do_fp_ld(s, rt2, clean_addr, mop);
3039         } else {
3040             do_fp_st(s, rt2, clean_addr, mop);
3041         }
3042     } else {
3043         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3044         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3045 
3046         /*
3047          * We built mop above for the single logical access -- rebuild it
3048          * now for the paired operation.
3049          *
3050          * With LSE2, non-sign-extending pairs are treated atomically if
3051          * aligned, and if unaligned one of the pair will be completely
3052          * within a 16-byte block and that element will be atomic.
3053          * Otherwise each element is separately atomic.
3054          * In all cases, issue one operation with the correct atomicity.
3055          *
3056          * This treats sign-extending loads like zero-extending loads,
3057          * since that reuses the most code below.
3058          */
3059         mop = size + 1;
3060         if (s->align_mem) {
3061             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3062         }
3063         mop = finalize_memop_pair(s, mop);
3064 
3065         if (is_load) {
3066             if (size == 2) {
3067                 int o2 = s->be_data == MO_LE ? 32 : 0;
3068                 int o1 = o2 ^ 32;
3069 
3070                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3071                 if (is_signed) {
3072                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3073                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3074                 } else {
3075                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3076                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3077                 }
3078             } else {
3079                 TCGv_i128 tmp = tcg_temp_new_i128();
3080 
3081                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3082                 if (s->be_data == MO_LE) {
3083                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3084                 } else {
3085                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3086                 }
3087             }
3088         } else {
3089             if (size == 2) {
3090                 TCGv_i64 tmp = tcg_temp_new_i64();
3091 
3092                 if (s->be_data == MO_LE) {
3093                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3094                 } else {
3095                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3096                 }
3097                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3098             } else {
3099                 TCGv_i128 tmp = tcg_temp_new_i128();
3100 
3101                 if (s->be_data == MO_LE) {
3102                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3103                 } else {
3104                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3105                 }
3106                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3107             }
3108         }
3109     }
3110 
3111     if (wback) {
3112         if (postindex) {
3113             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3114         }
3115         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3116     }
3117 }
3118 
3119 /*
3120  * Load/store (immediate post-indexed)
3121  * Load/store (immediate pre-indexed)
3122  * Load/store (unscaled immediate)
3123  *
3124  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3125  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3126  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3127  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3128  *
3129  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3130          10 -> unprivileged
3131  * V = 0 -> non-vector
3132  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3133  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3134  */
3135 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3136                                 int opc,
3137                                 int size,
3138                                 int rt,
3139                                 bool is_vector)
3140 {
3141     int rn = extract32(insn, 5, 5);
3142     int imm9 = sextract32(insn, 12, 9);
3143     int idx = extract32(insn, 10, 2);
3144     bool is_signed = false;
3145     bool is_store = false;
3146     bool is_extended = false;
3147     bool is_unpriv = (idx == 2);
3148     bool iss_valid;
3149     bool post_index;
3150     bool writeback;
3151     int memidx;
3152     MemOp memop;
3153     TCGv_i64 clean_addr, dirty_addr;
3154 
3155     if (is_vector) {
3156         size |= (opc & 2) << 1;
3157         if (size > 4 || is_unpriv) {
3158             unallocated_encoding(s);
3159             return;
3160         }
3161         is_store = ((opc & 1) == 0);
3162         if (!fp_access_check(s)) {
3163             return;
3164         }
3165         memop = finalize_memop_asimd(s, size);
3166     } else {
3167         if (size == 3 && opc == 2) {
3168             /* PRFM - prefetch */
3169             if (idx != 0) {
3170                 unallocated_encoding(s);
3171                 return;
3172             }
3173             return;
3174         }
3175         if (opc == 3 && size > 1) {
3176             unallocated_encoding(s);
3177             return;
3178         }
3179         is_store = (opc == 0);
3180         is_signed = !is_store && extract32(opc, 1, 1);
3181         is_extended = (size < 3) && extract32(opc, 0, 1);
3182         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3183     }
3184 
3185     switch (idx) {
3186     case 0:
3187     case 2:
3188         post_index = false;
3189         writeback = false;
3190         break;
3191     case 1:
3192         post_index = true;
3193         writeback = true;
3194         break;
3195     case 3:
3196         post_index = false;
3197         writeback = true;
3198         break;
3199     default:
3200         g_assert_not_reached();
3201     }
3202 
3203     iss_valid = !is_vector && !writeback;
3204 
3205     if (rn == 31) {
3206         gen_check_sp_alignment(s);
3207     }
3208 
3209     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3210     if (!post_index) {
3211         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3212     }
3213 
3214     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3215 
3216     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3217                                        writeback || rn != 31,
3218                                        memop, is_unpriv, memidx);
3219 
3220     if (is_vector) {
3221         if (is_store) {
3222             do_fp_st(s, rt, clean_addr, memop);
3223         } else {
3224             do_fp_ld(s, rt, clean_addr, memop);
3225         }
3226     } else {
3227         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3228         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3229 
3230         if (is_store) {
3231             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3232                              iss_valid, rt, iss_sf, false);
3233         } else {
3234             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3235                              is_extended, memidx,
3236                              iss_valid, rt, iss_sf, false);
3237         }
3238     }
3239 
3240     if (writeback) {
3241         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3242         if (post_index) {
3243             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3244         }
3245         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3246     }
3247 }
3248 
3249 /*
3250  * Load/store (register offset)
3251  *
3252  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3253  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3254  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3255  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3256  *
3257  * For non-vector:
3258  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3259  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3260  * For vector:
3261  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3262  *   opc<0>: 0 -> store, 1 -> load
3263  * V: 1 -> vector/simd
3264  * opt: extend encoding (see DecodeRegExtend)
3265  * S: if S=1 then scale (essentially index by sizeof(size))
3266  * Rt: register to transfer into/out of
3267  * Rn: address register or SP for base
3268  * Rm: offset register or ZR for offset
3269  */
3270 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3271                                    int opc,
3272                                    int size,
3273                                    int rt,
3274                                    bool is_vector)
3275 {
3276     int rn = extract32(insn, 5, 5);
3277     int shift = extract32(insn, 12, 1);
3278     int rm = extract32(insn, 16, 5);
3279     int opt = extract32(insn, 13, 3);
3280     bool is_signed = false;
3281     bool is_store = false;
3282     bool is_extended = false;
3283     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3284     MemOp memop;
3285 
3286     if (extract32(opt, 1, 1) == 0) {
3287         unallocated_encoding(s);
3288         return;
3289     }
3290 
3291     if (is_vector) {
3292         size |= (opc & 2) << 1;
3293         if (size > 4) {
3294             unallocated_encoding(s);
3295             return;
3296         }
3297         is_store = !extract32(opc, 0, 1);
3298         if (!fp_access_check(s)) {
3299             return;
3300         }
3301         memop = finalize_memop_asimd(s, size);
3302     } else {
3303         if (size == 3 && opc == 2) {
3304             /* PRFM - prefetch */
3305             return;
3306         }
3307         if (opc == 3 && size > 1) {
3308             unallocated_encoding(s);
3309             return;
3310         }
3311         is_store = (opc == 0);
3312         is_signed = !is_store && extract32(opc, 1, 1);
3313         is_extended = (size < 3) && extract32(opc, 0, 1);
3314         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3315     }
3316 
3317     if (rn == 31) {
3318         gen_check_sp_alignment(s);
3319     }
3320     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3321 
3322     tcg_rm = read_cpu_reg(s, rm, 1);
3323     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3324 
3325     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3326 
3327     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3328 
3329     if (is_vector) {
3330         if (is_store) {
3331             do_fp_st(s, rt, clean_addr, memop);
3332         } else {
3333             do_fp_ld(s, rt, clean_addr, memop);
3334         }
3335     } else {
3336         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3337         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3338 
3339         if (is_store) {
3340             do_gpr_st(s, tcg_rt, clean_addr, memop,
3341                       true, rt, iss_sf, false);
3342         } else {
3343             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3344                       is_extended, true, rt, iss_sf, false);
3345         }
3346     }
3347 }
3348 
3349 /*
3350  * Load/store (unsigned immediate)
3351  *
3352  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3353  * +----+-------+---+-----+-----+------------+-------+------+
3354  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3355  * +----+-------+---+-----+-----+------------+-------+------+
3356  *
3357  * For non-vector:
3358  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3359  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3360  * For vector:
3361  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3362  *   opc<0>: 0 -> store, 1 -> load
3363  * Rn: base address register (inc SP)
3364  * Rt: target register
3365  */
3366 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3367                                         int opc,
3368                                         int size,
3369                                         int rt,
3370                                         bool is_vector)
3371 {
3372     int rn = extract32(insn, 5, 5);
3373     unsigned int imm12 = extract32(insn, 10, 12);
3374     unsigned int offset;
3375     TCGv_i64 clean_addr, dirty_addr;
3376     bool is_store;
3377     bool is_signed = false;
3378     bool is_extended = false;
3379     MemOp memop;
3380 
3381     if (is_vector) {
3382         size |= (opc & 2) << 1;
3383         if (size > 4) {
3384             unallocated_encoding(s);
3385             return;
3386         }
3387         is_store = !extract32(opc, 0, 1);
3388         if (!fp_access_check(s)) {
3389             return;
3390         }
3391         memop = finalize_memop_asimd(s, size);
3392     } else {
3393         if (size == 3 && opc == 2) {
3394             /* PRFM - prefetch */
3395             return;
3396         }
3397         if (opc == 3 && size > 1) {
3398             unallocated_encoding(s);
3399             return;
3400         }
3401         is_store = (opc == 0);
3402         is_signed = !is_store && extract32(opc, 1, 1);
3403         is_extended = (size < 3) && extract32(opc, 0, 1);
3404         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3405     }
3406 
3407     if (rn == 31) {
3408         gen_check_sp_alignment(s);
3409     }
3410     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3411     offset = imm12 << size;
3412     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3413 
3414     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3415 
3416     if (is_vector) {
3417         if (is_store) {
3418             do_fp_st(s, rt, clean_addr, memop);
3419         } else {
3420             do_fp_ld(s, rt, clean_addr, memop);
3421         }
3422     } else {
3423         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3424         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3425         if (is_store) {
3426             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3427         } else {
3428             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3429                       is_extended, true, rt, iss_sf, false);
3430         }
3431     }
3432 }
3433 
3434 /* Atomic memory operations
3435  *
3436  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3437  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3438  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3439  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3440  *
3441  * Rt: the result register
3442  * Rn: base address or SP
3443  * Rs: the source register for the operation
3444  * V: vector flag (always 0 as of v8.3)
3445  * A: acquire flag
3446  * R: release flag
3447  */
3448 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3449                               int size, int rt, bool is_vector)
3450 {
3451     int rs = extract32(insn, 16, 5);
3452     int rn = extract32(insn, 5, 5);
3453     int o3_opc = extract32(insn, 12, 4);
3454     bool r = extract32(insn, 22, 1);
3455     bool a = extract32(insn, 23, 1);
3456     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3457     AtomicThreeOpFn *fn = NULL;
3458     MemOp mop = size;
3459 
3460     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3461         unallocated_encoding(s);
3462         return;
3463     }
3464     switch (o3_opc) {
3465     case 000: /* LDADD */
3466         fn = tcg_gen_atomic_fetch_add_i64;
3467         break;
3468     case 001: /* LDCLR */
3469         fn = tcg_gen_atomic_fetch_and_i64;
3470         break;
3471     case 002: /* LDEOR */
3472         fn = tcg_gen_atomic_fetch_xor_i64;
3473         break;
3474     case 003: /* LDSET */
3475         fn = tcg_gen_atomic_fetch_or_i64;
3476         break;
3477     case 004: /* LDSMAX */
3478         fn = tcg_gen_atomic_fetch_smax_i64;
3479         mop |= MO_SIGN;
3480         break;
3481     case 005: /* LDSMIN */
3482         fn = tcg_gen_atomic_fetch_smin_i64;
3483         mop |= MO_SIGN;
3484         break;
3485     case 006: /* LDUMAX */
3486         fn = tcg_gen_atomic_fetch_umax_i64;
3487         break;
3488     case 007: /* LDUMIN */
3489         fn = tcg_gen_atomic_fetch_umin_i64;
3490         break;
3491     case 010: /* SWP */
3492         fn = tcg_gen_atomic_xchg_i64;
3493         break;
3494     case 014: /* LDAPR, LDAPRH, LDAPRB */
3495         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3496             rs != 31 || a != 1 || r != 0) {
3497             unallocated_encoding(s);
3498             return;
3499         }
3500         break;
3501     default:
3502         unallocated_encoding(s);
3503         return;
3504     }
3505 
3506     if (rn == 31) {
3507         gen_check_sp_alignment(s);
3508     }
3509 
3510     mop = check_atomic_align(s, rn, mop);
3511     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3512 
3513     if (o3_opc == 014) {
3514         /*
3515          * LDAPR* are a special case because they are a simple load, not a
3516          * fetch-and-do-something op.
3517          * The architectural consistency requirements here are weaker than
3518          * full load-acquire (we only need "load-acquire processor consistent"),
3519          * but we choose to implement them as full LDAQ.
3520          */
3521         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3522                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3523         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3524         return;
3525     }
3526 
3527     tcg_rs = read_cpu_reg(s, rs, true);
3528     tcg_rt = cpu_reg(s, rt);
3529 
3530     if (o3_opc == 1) { /* LDCLR */
3531         tcg_gen_not_i64(tcg_rs, tcg_rs);
3532     }
3533 
3534     /* The tcg atomic primitives are all full barriers.  Therefore we
3535      * can ignore the Acquire and Release bits of this instruction.
3536      */
3537     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3538 
3539     if (mop & MO_SIGN) {
3540         switch (size) {
3541         case MO_8:
3542             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3543             break;
3544         case MO_16:
3545             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3546             break;
3547         case MO_32:
3548             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3549             break;
3550         case MO_64:
3551             break;
3552         default:
3553             g_assert_not_reached();
3554         }
3555     }
3556 }
3557 
3558 /*
3559  * PAC memory operations
3560  *
3561  *  31  30      27  26    24    22  21       12  11  10    5     0
3562  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3563  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3564  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3565  *
3566  * Rt: the result register
3567  * Rn: base address or SP
3568  * V: vector flag (always 0 as of v8.3)
3569  * M: clear for key DA, set for key DB
3570  * W: pre-indexing flag
3571  * S: sign for imm9.
3572  */
3573 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3574                            int size, int rt, bool is_vector)
3575 {
3576     int rn = extract32(insn, 5, 5);
3577     bool is_wback = extract32(insn, 11, 1);
3578     bool use_key_a = !extract32(insn, 23, 1);
3579     int offset;
3580     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3581     MemOp memop;
3582 
3583     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3584         unallocated_encoding(s);
3585         return;
3586     }
3587 
3588     if (rn == 31) {
3589         gen_check_sp_alignment(s);
3590     }
3591     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3592 
3593     if (s->pauth_active) {
3594         if (use_key_a) {
3595             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3596                              tcg_constant_i64(0));
3597         } else {
3598             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3599                              tcg_constant_i64(0));
3600         }
3601     }
3602 
3603     /* Form the 10-bit signed, scaled offset.  */
3604     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3605     offset = sextract32(offset << size, 0, 10 + size);
3606     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3607 
3608     memop = finalize_memop(s, size);
3609 
3610     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3611     clean_addr = gen_mte_check1(s, dirty_addr, false,
3612                                 is_wback || rn != 31, memop);
3613 
3614     tcg_rt = cpu_reg(s, rt);
3615     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3616               /* extend */ false, /* iss_valid */ !is_wback,
3617               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3618 
3619     if (is_wback) {
3620         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3621     }
3622 }
3623 
3624 /*
3625  * LDAPR/STLR (unscaled immediate)
3626  *
3627  *  31  30            24    22  21       12    10    5     0
3628  * +------+-------------+-----+---+--------+-----+----+-----+
3629  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3630  * +------+-------------+-----+---+--------+-----+----+-----+
3631  *
3632  * Rt: source or destination register
3633  * Rn: base register
3634  * imm9: unscaled immediate offset
3635  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3636  * size: size of load/store
3637  */
3638 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3639 {
3640     int rt = extract32(insn, 0, 5);
3641     int rn = extract32(insn, 5, 5);
3642     int offset = sextract32(insn, 12, 9);
3643     int opc = extract32(insn, 22, 2);
3644     int size = extract32(insn, 30, 2);
3645     TCGv_i64 clean_addr, dirty_addr;
3646     bool is_store = false;
3647     bool extend = false;
3648     bool iss_sf;
3649     MemOp mop = size;
3650 
3651     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3652         unallocated_encoding(s);
3653         return;
3654     }
3655 
3656     switch (opc) {
3657     case 0: /* STLURB */
3658         is_store = true;
3659         break;
3660     case 1: /* LDAPUR* */
3661         break;
3662     case 2: /* LDAPURS* 64-bit variant */
3663         if (size == 3) {
3664             unallocated_encoding(s);
3665             return;
3666         }
3667         mop |= MO_SIGN;
3668         break;
3669     case 3: /* LDAPURS* 32-bit variant */
3670         if (size > 1) {
3671             unallocated_encoding(s);
3672             return;
3673         }
3674         mop |= MO_SIGN;
3675         extend = true; /* zero-extend 32->64 after signed load */
3676         break;
3677     default:
3678         g_assert_not_reached();
3679     }
3680 
3681     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3682 
3683     if (rn == 31) {
3684         gen_check_sp_alignment(s);
3685     }
3686 
3687     mop = check_ordered_align(s, rn, offset, is_store, mop);
3688 
3689     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3690     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3691     clean_addr = clean_data_tbi(s, dirty_addr);
3692 
3693     if (is_store) {
3694         /* Store-Release semantics */
3695         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3696         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3697     } else {
3698         /*
3699          * Load-AcquirePC semantics; we implement as the slightly more
3700          * restrictive Load-Acquire.
3701          */
3702         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3703                   extend, true, rt, iss_sf, true);
3704         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3705     }
3706 }
3707 
3708 /* Load/store register (all forms) */
3709 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3710 {
3711     int rt = extract32(insn, 0, 5);
3712     int opc = extract32(insn, 22, 2);
3713     bool is_vector = extract32(insn, 26, 1);
3714     int size = extract32(insn, 30, 2);
3715 
3716     switch (extract32(insn, 24, 2)) {
3717     case 0:
3718         if (extract32(insn, 21, 1) == 0) {
3719             /* Load/store register (unscaled immediate)
3720              * Load/store immediate pre/post-indexed
3721              * Load/store register unprivileged
3722              */
3723             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3724             return;
3725         }
3726         switch (extract32(insn, 10, 2)) {
3727         case 0:
3728             disas_ldst_atomic(s, insn, size, rt, is_vector);
3729             return;
3730         case 2:
3731             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3732             return;
3733         default:
3734             disas_ldst_pac(s, insn, size, rt, is_vector);
3735             return;
3736         }
3737         break;
3738     case 1:
3739         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3740         return;
3741     }
3742     unallocated_encoding(s);
3743 }
3744 
3745 /* AdvSIMD load/store multiple structures
3746  *
3747  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3748  * +---+---+---------------+---+-------------+--------+------+------+------+
3749  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3750  * +---+---+---------------+---+-------------+--------+------+------+------+
3751  *
3752  * AdvSIMD load/store multiple structures (post-indexed)
3753  *
3754  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3755  * +---+---+---------------+---+---+---------+--------+------+------+------+
3756  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3757  * +---+---+---------------+---+---+---------+--------+------+------+------+
3758  *
3759  * Rt: first (or only) SIMD&FP register to be transferred
3760  * Rn: base address or SP
3761  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3762  */
3763 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3764 {
3765     int rt = extract32(insn, 0, 5);
3766     int rn = extract32(insn, 5, 5);
3767     int rm = extract32(insn, 16, 5);
3768     int size = extract32(insn, 10, 2);
3769     int opcode = extract32(insn, 12, 4);
3770     bool is_store = !extract32(insn, 22, 1);
3771     bool is_postidx = extract32(insn, 23, 1);
3772     bool is_q = extract32(insn, 30, 1);
3773     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3774     MemOp endian, align, mop;
3775 
3776     int total;    /* total bytes */
3777     int elements; /* elements per vector */
3778     int rpt;    /* num iterations */
3779     int selem;  /* structure elements */
3780     int r;
3781 
3782     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3783         unallocated_encoding(s);
3784         return;
3785     }
3786 
3787     if (!is_postidx && rm != 0) {
3788         unallocated_encoding(s);
3789         return;
3790     }
3791 
3792     /* From the shared decode logic */
3793     switch (opcode) {
3794     case 0x0:
3795         rpt = 1;
3796         selem = 4;
3797         break;
3798     case 0x2:
3799         rpt = 4;
3800         selem = 1;
3801         break;
3802     case 0x4:
3803         rpt = 1;
3804         selem = 3;
3805         break;
3806     case 0x6:
3807         rpt = 3;
3808         selem = 1;
3809         break;
3810     case 0x7:
3811         rpt = 1;
3812         selem = 1;
3813         break;
3814     case 0x8:
3815         rpt = 1;
3816         selem = 2;
3817         break;
3818     case 0xa:
3819         rpt = 2;
3820         selem = 1;
3821         break;
3822     default:
3823         unallocated_encoding(s);
3824         return;
3825     }
3826 
3827     if (size == 3 && !is_q && selem != 1) {
3828         /* reserved */
3829         unallocated_encoding(s);
3830         return;
3831     }
3832 
3833     if (!fp_access_check(s)) {
3834         return;
3835     }
3836 
3837     if (rn == 31) {
3838         gen_check_sp_alignment(s);
3839     }
3840 
3841     /* For our purposes, bytes are always little-endian.  */
3842     endian = s->be_data;
3843     if (size == 0) {
3844         endian = MO_LE;
3845     }
3846 
3847     total = rpt * selem * (is_q ? 16 : 8);
3848     tcg_rn = cpu_reg_sp(s, rn);
3849 
3850     /*
3851      * Issue the MTE check vs the logical repeat count, before we
3852      * promote consecutive little-endian elements below.
3853      */
3854     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3855                                 total, finalize_memop_asimd(s, size));
3856 
3857     /*
3858      * Consecutive little-endian elements from a single register
3859      * can be promoted to a larger little-endian operation.
3860      */
3861     align = MO_ALIGN;
3862     if (selem == 1 && endian == MO_LE) {
3863         align = pow2_align(size);
3864         size = 3;
3865     }
3866     if (!s->align_mem) {
3867         align = 0;
3868     }
3869     mop = endian | size | align;
3870 
3871     elements = (is_q ? 16 : 8) >> size;
3872     tcg_ebytes = tcg_constant_i64(1 << size);
3873     for (r = 0; r < rpt; r++) {
3874         int e;
3875         for (e = 0; e < elements; e++) {
3876             int xs;
3877             for (xs = 0; xs < selem; xs++) {
3878                 int tt = (rt + r + xs) % 32;
3879                 if (is_store) {
3880                     do_vec_st(s, tt, e, clean_addr, mop);
3881                 } else {
3882                     do_vec_ld(s, tt, e, clean_addr, mop);
3883                 }
3884                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3885             }
3886         }
3887     }
3888 
3889     if (!is_store) {
3890         /* For non-quad operations, setting a slice of the low
3891          * 64 bits of the register clears the high 64 bits (in
3892          * the ARM ARM pseudocode this is implicit in the fact
3893          * that 'rval' is a 64 bit wide variable).
3894          * For quad operations, we might still need to zero the
3895          * high bits of SVE.
3896          */
3897         for (r = 0; r < rpt * selem; r++) {
3898             int tt = (rt + r) % 32;
3899             clear_vec_high(s, is_q, tt);
3900         }
3901     }
3902 
3903     if (is_postidx) {
3904         if (rm == 31) {
3905             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3906         } else {
3907             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3908         }
3909     }
3910 }
3911 
3912 /* AdvSIMD load/store single structure
3913  *
3914  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3915  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3916  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3917  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3918  *
3919  * AdvSIMD load/store single structure (post-indexed)
3920  *
3921  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3922  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3923  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3924  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3925  *
3926  * Rt: first (or only) SIMD&FP register to be transferred
3927  * Rn: base address or SP
3928  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3929  * index = encoded in Q:S:size dependent on size
3930  *
3931  * lane_size = encoded in R, opc
3932  * transfer width = encoded in opc, S, size
3933  */
3934 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3935 {
3936     int rt = extract32(insn, 0, 5);
3937     int rn = extract32(insn, 5, 5);
3938     int rm = extract32(insn, 16, 5);
3939     int size = extract32(insn, 10, 2);
3940     int S = extract32(insn, 12, 1);
3941     int opc = extract32(insn, 13, 3);
3942     int R = extract32(insn, 21, 1);
3943     int is_load = extract32(insn, 22, 1);
3944     int is_postidx = extract32(insn, 23, 1);
3945     int is_q = extract32(insn, 30, 1);
3946 
3947     int scale = extract32(opc, 1, 2);
3948     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3949     bool replicate = false;
3950     int index = is_q << 3 | S << 2 | size;
3951     int xs, total;
3952     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3953     MemOp mop;
3954 
3955     if (extract32(insn, 31, 1)) {
3956         unallocated_encoding(s);
3957         return;
3958     }
3959     if (!is_postidx && rm != 0) {
3960         unallocated_encoding(s);
3961         return;
3962     }
3963 
3964     switch (scale) {
3965     case 3:
3966         if (!is_load || S) {
3967             unallocated_encoding(s);
3968             return;
3969         }
3970         scale = size;
3971         replicate = true;
3972         break;
3973     case 0:
3974         break;
3975     case 1:
3976         if (extract32(size, 0, 1)) {
3977             unallocated_encoding(s);
3978             return;
3979         }
3980         index >>= 1;
3981         break;
3982     case 2:
3983         if (extract32(size, 1, 1)) {
3984             unallocated_encoding(s);
3985             return;
3986         }
3987         if (!extract32(size, 0, 1)) {
3988             index >>= 2;
3989         } else {
3990             if (S) {
3991                 unallocated_encoding(s);
3992                 return;
3993             }
3994             index >>= 3;
3995             scale = 3;
3996         }
3997         break;
3998     default:
3999         g_assert_not_reached();
4000     }
4001 
4002     if (!fp_access_check(s)) {
4003         return;
4004     }
4005 
4006     if (rn == 31) {
4007         gen_check_sp_alignment(s);
4008     }
4009 
4010     total = selem << scale;
4011     tcg_rn = cpu_reg_sp(s, rn);
4012 
4013     mop = finalize_memop_asimd(s, scale);
4014 
4015     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4016                                 total, mop);
4017 
4018     tcg_ebytes = tcg_constant_i64(1 << scale);
4019     for (xs = 0; xs < selem; xs++) {
4020         if (replicate) {
4021             /* Load and replicate to all elements */
4022             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4023 
4024             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4025             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4026                                  (is_q + 1) * 8, vec_full_reg_size(s),
4027                                  tcg_tmp);
4028         } else {
4029             /* Load/store one element per register */
4030             if (is_load) {
4031                 do_vec_ld(s, rt, index, clean_addr, mop);
4032             } else {
4033                 do_vec_st(s, rt, index, clean_addr, mop);
4034             }
4035         }
4036         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4037         rt = (rt + 1) % 32;
4038     }
4039 
4040     if (is_postidx) {
4041         if (rm == 31) {
4042             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4043         } else {
4044             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4045         }
4046     }
4047 }
4048 
4049 /*
4050  * Load/Store memory tags
4051  *
4052  *  31 30 29         24     22  21     12    10      5      0
4053  * +-----+-------------+-----+---+------+-----+------+------+
4054  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4055  * +-----+-------------+-----+---+------+-----+------+------+
4056  */
4057 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4058 {
4059     int rt = extract32(insn, 0, 5);
4060     int rn = extract32(insn, 5, 5);
4061     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4062     int op2 = extract32(insn, 10, 2);
4063     int op1 = extract32(insn, 22, 2);
4064     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4065     int index = 0;
4066     TCGv_i64 addr, clean_addr, tcg_rt;
4067 
4068     /* We checked insn bits [29:24,21] in the caller.  */
4069     if (extract32(insn, 30, 2) != 3) {
4070         goto do_unallocated;
4071     }
4072 
4073     /*
4074      * @index is a tri-state variable which has 3 states:
4075      * < 0 : post-index, writeback
4076      * = 0 : signed offset
4077      * > 0 : pre-index, writeback
4078      */
4079     switch (op1) {
4080     case 0:
4081         if (op2 != 0) {
4082             /* STG */
4083             index = op2 - 2;
4084         } else {
4085             /* STZGM */
4086             if (s->current_el == 0 || offset != 0) {
4087                 goto do_unallocated;
4088             }
4089             is_mult = is_zero = true;
4090         }
4091         break;
4092     case 1:
4093         if (op2 != 0) {
4094             /* STZG */
4095             is_zero = true;
4096             index = op2 - 2;
4097         } else {
4098             /* LDG */
4099             is_load = true;
4100         }
4101         break;
4102     case 2:
4103         if (op2 != 0) {
4104             /* ST2G */
4105             is_pair = true;
4106             index = op2 - 2;
4107         } else {
4108             /* STGM */
4109             if (s->current_el == 0 || offset != 0) {
4110                 goto do_unallocated;
4111             }
4112             is_mult = true;
4113         }
4114         break;
4115     case 3:
4116         if (op2 != 0) {
4117             /* STZ2G */
4118             is_pair = is_zero = true;
4119             index = op2 - 2;
4120         } else {
4121             /* LDGM */
4122             if (s->current_el == 0 || offset != 0) {
4123                 goto do_unallocated;
4124             }
4125             is_mult = is_load = true;
4126         }
4127         break;
4128 
4129     default:
4130     do_unallocated:
4131         unallocated_encoding(s);
4132         return;
4133     }
4134 
4135     if (is_mult
4136         ? !dc_isar_feature(aa64_mte, s)
4137         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4138         goto do_unallocated;
4139     }
4140 
4141     if (rn == 31) {
4142         gen_check_sp_alignment(s);
4143     }
4144 
4145     addr = read_cpu_reg_sp(s, rn, true);
4146     if (index >= 0) {
4147         /* pre-index or signed offset */
4148         tcg_gen_addi_i64(addr, addr, offset);
4149     }
4150 
4151     if (is_mult) {
4152         tcg_rt = cpu_reg(s, rt);
4153 
4154         if (is_zero) {
4155             int size = 4 << s->dcz_blocksize;
4156 
4157             if (s->ata) {
4158                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4159             }
4160             /*
4161              * The non-tags portion of STZGM is mostly like DC_ZVA,
4162              * except the alignment happens before the access.
4163              */
4164             clean_addr = clean_data_tbi(s, addr);
4165             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4166             gen_helper_dc_zva(cpu_env, clean_addr);
4167         } else if (s->ata) {
4168             if (is_load) {
4169                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4170             } else {
4171                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4172             }
4173         } else {
4174             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4175             int size = 4 << GMID_EL1_BS;
4176 
4177             clean_addr = clean_data_tbi(s, addr);
4178             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4179             gen_probe_access(s, clean_addr, acc, size);
4180 
4181             if (is_load) {
4182                 /* The result tags are zeros.  */
4183                 tcg_gen_movi_i64(tcg_rt, 0);
4184             }
4185         }
4186         return;
4187     }
4188 
4189     if (is_load) {
4190         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4191         tcg_rt = cpu_reg(s, rt);
4192         if (s->ata) {
4193             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4194         } else {
4195             /*
4196              * Tag access disabled: we must check for aborts on the load
4197              * load from [rn+offset], and then insert a 0 tag into rt.
4198              */
4199             clean_addr = clean_data_tbi(s, addr);
4200             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4201             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4202         }
4203     } else {
4204         tcg_rt = cpu_reg_sp(s, rt);
4205         if (!s->ata) {
4206             /*
4207              * For STG and ST2G, we need to check alignment and probe memory.
4208              * TODO: For STZG and STZ2G, we could rely on the stores below,
4209              * at least for system mode; user-only won't enforce alignment.
4210              */
4211             if (is_pair) {
4212                 gen_helper_st2g_stub(cpu_env, addr);
4213             } else {
4214                 gen_helper_stg_stub(cpu_env, addr);
4215             }
4216         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4217             if (is_pair) {
4218                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4219             } else {
4220                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4221             }
4222         } else {
4223             if (is_pair) {
4224                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4225             } else {
4226                 gen_helper_stg(cpu_env, addr, tcg_rt);
4227             }
4228         }
4229     }
4230 
4231     if (is_zero) {
4232         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4233         TCGv_i64 zero64 = tcg_constant_i64(0);
4234         TCGv_i128 zero128 = tcg_temp_new_i128();
4235         int mem_index = get_mem_index(s);
4236         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4237 
4238         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4239 
4240         /* This is 1 or 2 atomic 16-byte operations. */
4241         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4242         if (is_pair) {
4243             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4244             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4245         }
4246     }
4247 
4248     if (index != 0) {
4249         /* pre-index or post-index */
4250         if (index < 0) {
4251             /* post-index */
4252             tcg_gen_addi_i64(addr, addr, offset);
4253         }
4254         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4255     }
4256 }
4257 
4258 /* Loads and stores */
4259 static void disas_ldst(DisasContext *s, uint32_t insn)
4260 {
4261     switch (extract32(insn, 24, 6)) {
4262     case 0x08: /* Load/store exclusive */
4263         disas_ldst_excl(s, insn);
4264         break;
4265     case 0x18: case 0x1c: /* Load register (literal) */
4266         disas_ld_lit(s, insn);
4267         break;
4268     case 0x28: case 0x29:
4269     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4270         disas_ldst_pair(s, insn);
4271         break;
4272     case 0x38: case 0x39:
4273     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4274         disas_ldst_reg(s, insn);
4275         break;
4276     case 0x0c: /* AdvSIMD load/store multiple structures */
4277         disas_ldst_multiple_struct(s, insn);
4278         break;
4279     case 0x0d: /* AdvSIMD load/store single structure */
4280         disas_ldst_single_struct(s, insn);
4281         break;
4282     case 0x19:
4283         if (extract32(insn, 21, 1) != 0) {
4284             disas_ldst_tag(s, insn);
4285         } else if (extract32(insn, 10, 2) == 0) {
4286             disas_ldst_ldapr_stlr(s, insn);
4287         } else {
4288             unallocated_encoding(s);
4289         }
4290         break;
4291     default:
4292         unallocated_encoding(s);
4293         break;
4294     }
4295 }
4296 
4297 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4298 
4299 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4300                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4301 {
4302     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4303     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4304     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4305 
4306     fn(tcg_rd, tcg_rn, tcg_imm);
4307     if (!a->sf) {
4308         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4309     }
4310     return true;
4311 }
4312 
4313 /*
4314  * PC-rel. addressing
4315  */
4316 
4317 static bool trans_ADR(DisasContext *s, arg_ri *a)
4318 {
4319     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4320     return true;
4321 }
4322 
4323 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4324 {
4325     int64_t offset = (int64_t)a->imm << 12;
4326 
4327     /* The page offset is ok for CF_PCREL. */
4328     offset -= s->pc_curr & 0xfff;
4329     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4330     return true;
4331 }
4332 
4333 /*
4334  * Add/subtract (immediate)
4335  */
4336 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4337 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4338 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4339 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4340 
4341 /*
4342  * Add/subtract (immediate, with tags)
4343  */
4344 
4345 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4346                                       bool sub_op)
4347 {
4348     TCGv_i64 tcg_rn, tcg_rd;
4349     int imm;
4350 
4351     imm = a->uimm6 << LOG2_TAG_GRANULE;
4352     if (sub_op) {
4353         imm = -imm;
4354     }
4355 
4356     tcg_rn = cpu_reg_sp(s, a->rn);
4357     tcg_rd = cpu_reg_sp(s, a->rd);
4358 
4359     if (s->ata) {
4360         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4361                            tcg_constant_i32(imm),
4362                            tcg_constant_i32(a->uimm4));
4363     } else {
4364         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4365         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4366     }
4367     return true;
4368 }
4369 
4370 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4371 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4372 
4373 /* The input should be a value in the bottom e bits (with higher
4374  * bits zero); returns that value replicated into every element
4375  * of size e in a 64 bit integer.
4376  */
4377 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4378 {
4379     assert(e != 0);
4380     while (e < 64) {
4381         mask |= mask << e;
4382         e *= 2;
4383     }
4384     return mask;
4385 }
4386 
4387 /*
4388  * Logical (immediate)
4389  */
4390 
4391 /*
4392  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4393  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4394  * value (ie should cause a guest UNDEF exception), and true if they are
4395  * valid, in which case the decoded bit pattern is written to result.
4396  */
4397 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4398                             unsigned int imms, unsigned int immr)
4399 {
4400     uint64_t mask;
4401     unsigned e, levels, s, r;
4402     int len;
4403 
4404     assert(immn < 2 && imms < 64 && immr < 64);
4405 
4406     /* The bit patterns we create here are 64 bit patterns which
4407      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4408      * 64 bits each. Each element contains the same value: a run
4409      * of between 1 and e-1 non-zero bits, rotated within the
4410      * element by between 0 and e-1 bits.
4411      *
4412      * The element size and run length are encoded into immn (1 bit)
4413      * and imms (6 bits) as follows:
4414      * 64 bit elements: immn = 1, imms = <length of run - 1>
4415      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4416      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4417      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4418      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4419      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4420      * Notice that immn = 0, imms = 11111x is the only combination
4421      * not covered by one of the above options; this is reserved.
4422      * Further, <length of run - 1> all-ones is a reserved pattern.
4423      *
4424      * In all cases the rotation is by immr % e (and immr is 6 bits).
4425      */
4426 
4427     /* First determine the element size */
4428     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4429     if (len < 1) {
4430         /* This is the immn == 0, imms == 0x11111x case */
4431         return false;
4432     }
4433     e = 1 << len;
4434 
4435     levels = e - 1;
4436     s = imms & levels;
4437     r = immr & levels;
4438 
4439     if (s == levels) {
4440         /* <length of run - 1> mustn't be all-ones. */
4441         return false;
4442     }
4443 
4444     /* Create the value of one element: s+1 set bits rotated
4445      * by r within the element (which is e bits wide)...
4446      */
4447     mask = MAKE_64BIT_MASK(0, s + 1);
4448     if (r) {
4449         mask = (mask >> r) | (mask << (e - r));
4450         mask &= MAKE_64BIT_MASK(0, e);
4451     }
4452     /* ...then replicate the element over the whole 64 bit value */
4453     mask = bitfield_replicate(mask, e);
4454     *result = mask;
4455     return true;
4456 }
4457 
4458 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4459                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4460 {
4461     TCGv_i64 tcg_rd, tcg_rn;
4462     uint64_t imm;
4463 
4464     /* Some immediate field values are reserved. */
4465     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4466                                 extract32(a->dbm, 0, 6),
4467                                 extract32(a->dbm, 6, 6))) {
4468         return false;
4469     }
4470     if (!a->sf) {
4471         imm &= 0xffffffffull;
4472     }
4473 
4474     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4475     tcg_rn = cpu_reg(s, a->rn);
4476 
4477     fn(tcg_rd, tcg_rn, imm);
4478     if (set_cc) {
4479         gen_logic_CC(a->sf, tcg_rd);
4480     }
4481     if (!a->sf) {
4482         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4483     }
4484     return true;
4485 }
4486 
4487 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4488 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4489 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4490 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4491 
4492 /*
4493  * Move wide (immediate)
4494  */
4495 
4496 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4497 {
4498     int pos = a->hw << 4;
4499     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4500     return true;
4501 }
4502 
4503 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4504 {
4505     int pos = a->hw << 4;
4506     uint64_t imm = a->imm;
4507 
4508     imm = ~(imm << pos);
4509     if (!a->sf) {
4510         imm = (uint32_t)imm;
4511     }
4512     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4513     return true;
4514 }
4515 
4516 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4517 {
4518     int pos = a->hw << 4;
4519     TCGv_i64 tcg_rd, tcg_im;
4520 
4521     tcg_rd = cpu_reg(s, a->rd);
4522     tcg_im = tcg_constant_i64(a->imm);
4523     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4524     if (!a->sf) {
4525         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4526     }
4527     return true;
4528 }
4529 
4530 /*
4531  * Bitfield
4532  */
4533 
4534 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4535 {
4536     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4537     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4538     unsigned int bitsize = a->sf ? 64 : 32;
4539     unsigned int ri = a->immr;
4540     unsigned int si = a->imms;
4541     unsigned int pos, len;
4542 
4543     if (si >= ri) {
4544         /* Wd<s-r:0> = Wn<s:r> */
4545         len = (si - ri) + 1;
4546         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4547         if (!a->sf) {
4548             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4549         }
4550     } else {
4551         /* Wd<32+s-r,32-r> = Wn<s:0> */
4552         len = si + 1;
4553         pos = (bitsize - ri) & (bitsize - 1);
4554 
4555         if (len < ri) {
4556             /*
4557              * Sign extend the destination field from len to fill the
4558              * balance of the word.  Let the deposit below insert all
4559              * of those sign bits.
4560              */
4561             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4562             len = ri;
4563         }
4564 
4565         /*
4566          * We start with zero, and we haven't modified any bits outside
4567          * bitsize, therefore no final zero-extension is unneeded for !sf.
4568          */
4569         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4570     }
4571     return true;
4572 }
4573 
4574 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4575 {
4576     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4577     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4578     unsigned int bitsize = a->sf ? 64 : 32;
4579     unsigned int ri = a->immr;
4580     unsigned int si = a->imms;
4581     unsigned int pos, len;
4582 
4583     tcg_rd = cpu_reg(s, a->rd);
4584     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4585 
4586     if (si >= ri) {
4587         /* Wd<s-r:0> = Wn<s:r> */
4588         len = (si - ri) + 1;
4589         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4590     } else {
4591         /* Wd<32+s-r,32-r> = Wn<s:0> */
4592         len = si + 1;
4593         pos = (bitsize - ri) & (bitsize - 1);
4594         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4595     }
4596     return true;
4597 }
4598 
4599 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4600 {
4601     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4602     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4603     unsigned int bitsize = a->sf ? 64 : 32;
4604     unsigned int ri = a->immr;
4605     unsigned int si = a->imms;
4606     unsigned int pos, len;
4607 
4608     tcg_rd = cpu_reg(s, a->rd);
4609     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4610 
4611     if (si >= ri) {
4612         /* Wd<s-r:0> = Wn<s:r> */
4613         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4614         len = (si - ri) + 1;
4615         pos = 0;
4616     } else {
4617         /* Wd<32+s-r,32-r> = Wn<s:0> */
4618         len = si + 1;
4619         pos = (bitsize - ri) & (bitsize - 1);
4620     }
4621 
4622     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4623     if (!a->sf) {
4624         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4625     }
4626     return true;
4627 }
4628 
4629 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4630 {
4631     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4632 
4633     tcg_rd = cpu_reg(s, a->rd);
4634 
4635     if (unlikely(a->imm == 0)) {
4636         /*
4637          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4638          * so an extract from bit 0 is a special case.
4639          */
4640         if (a->sf) {
4641             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4642         } else {
4643             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4644         }
4645     } else {
4646         tcg_rm = cpu_reg(s, a->rm);
4647         tcg_rn = cpu_reg(s, a->rn);
4648 
4649         if (a->sf) {
4650             /* Specialization to ROR happens in EXTRACT2.  */
4651             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4652         } else {
4653             TCGv_i32 t0 = tcg_temp_new_i32();
4654 
4655             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4656             if (a->rm == a->rn) {
4657                 tcg_gen_rotri_i32(t0, t0, a->imm);
4658             } else {
4659                 TCGv_i32 t1 = tcg_temp_new_i32();
4660                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4661                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4662             }
4663             tcg_gen_extu_i32_i64(tcg_rd, t0);
4664         }
4665     }
4666     return true;
4667 }
4668 
4669 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4670  * Note that it is the caller's responsibility to ensure that the
4671  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4672  * mandated semantics for out of range shifts.
4673  */
4674 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4675                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4676 {
4677     switch (shift_type) {
4678     case A64_SHIFT_TYPE_LSL:
4679         tcg_gen_shl_i64(dst, src, shift_amount);
4680         break;
4681     case A64_SHIFT_TYPE_LSR:
4682         tcg_gen_shr_i64(dst, src, shift_amount);
4683         break;
4684     case A64_SHIFT_TYPE_ASR:
4685         if (!sf) {
4686             tcg_gen_ext32s_i64(dst, src);
4687         }
4688         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4689         break;
4690     case A64_SHIFT_TYPE_ROR:
4691         if (sf) {
4692             tcg_gen_rotr_i64(dst, src, shift_amount);
4693         } else {
4694             TCGv_i32 t0, t1;
4695             t0 = tcg_temp_new_i32();
4696             t1 = tcg_temp_new_i32();
4697             tcg_gen_extrl_i64_i32(t0, src);
4698             tcg_gen_extrl_i64_i32(t1, shift_amount);
4699             tcg_gen_rotr_i32(t0, t0, t1);
4700             tcg_gen_extu_i32_i64(dst, t0);
4701         }
4702         break;
4703     default:
4704         assert(FALSE); /* all shift types should be handled */
4705         break;
4706     }
4707 
4708     if (!sf) { /* zero extend final result */
4709         tcg_gen_ext32u_i64(dst, dst);
4710     }
4711 }
4712 
4713 /* Shift a TCGv src by immediate, put result in dst.
4714  * The shift amount must be in range (this should always be true as the
4715  * relevant instructions will UNDEF on bad shift immediates).
4716  */
4717 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4718                           enum a64_shift_type shift_type, unsigned int shift_i)
4719 {
4720     assert(shift_i < (sf ? 64 : 32));
4721 
4722     if (shift_i == 0) {
4723         tcg_gen_mov_i64(dst, src);
4724     } else {
4725         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4726     }
4727 }
4728 
4729 /* Logical (shifted register)
4730  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4731  * +----+-----+-----------+-------+---+------+--------+------+------+
4732  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4733  * +----+-----+-----------+-------+---+------+--------+------+------+
4734  */
4735 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4736 {
4737     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4738     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4739 
4740     sf = extract32(insn, 31, 1);
4741     opc = extract32(insn, 29, 2);
4742     shift_type = extract32(insn, 22, 2);
4743     invert = extract32(insn, 21, 1);
4744     rm = extract32(insn, 16, 5);
4745     shift_amount = extract32(insn, 10, 6);
4746     rn = extract32(insn, 5, 5);
4747     rd = extract32(insn, 0, 5);
4748 
4749     if (!sf && (shift_amount & (1 << 5))) {
4750         unallocated_encoding(s);
4751         return;
4752     }
4753 
4754     tcg_rd = cpu_reg(s, rd);
4755 
4756     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4757         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4758          * register-register MOV and MVN, so it is worth special casing.
4759          */
4760         tcg_rm = cpu_reg(s, rm);
4761         if (invert) {
4762             tcg_gen_not_i64(tcg_rd, tcg_rm);
4763             if (!sf) {
4764                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4765             }
4766         } else {
4767             if (sf) {
4768                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4769             } else {
4770                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4771             }
4772         }
4773         return;
4774     }
4775 
4776     tcg_rm = read_cpu_reg(s, rm, sf);
4777 
4778     if (shift_amount) {
4779         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4780     }
4781 
4782     tcg_rn = cpu_reg(s, rn);
4783 
4784     switch (opc | (invert << 2)) {
4785     case 0: /* AND */
4786     case 3: /* ANDS */
4787         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4788         break;
4789     case 1: /* ORR */
4790         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4791         break;
4792     case 2: /* EOR */
4793         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4794         break;
4795     case 4: /* BIC */
4796     case 7: /* BICS */
4797         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4798         break;
4799     case 5: /* ORN */
4800         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4801         break;
4802     case 6: /* EON */
4803         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4804         break;
4805     default:
4806         assert(FALSE);
4807         break;
4808     }
4809 
4810     if (!sf) {
4811         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4812     }
4813 
4814     if (opc == 3) {
4815         gen_logic_CC(sf, tcg_rd);
4816     }
4817 }
4818 
4819 /*
4820  * Add/subtract (extended register)
4821  *
4822  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4823  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4824  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4825  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4826  *
4827  *  sf: 0 -> 32bit, 1 -> 64bit
4828  *  op: 0 -> add  , 1 -> sub
4829  *   S: 1 -> set flags
4830  * opt: 00
4831  * option: extension type (see DecodeRegExtend)
4832  * imm3: optional shift to Rm
4833  *
4834  * Rd = Rn + LSL(extend(Rm), amount)
4835  */
4836 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4837 {
4838     int rd = extract32(insn, 0, 5);
4839     int rn = extract32(insn, 5, 5);
4840     int imm3 = extract32(insn, 10, 3);
4841     int option = extract32(insn, 13, 3);
4842     int rm = extract32(insn, 16, 5);
4843     int opt = extract32(insn, 22, 2);
4844     bool setflags = extract32(insn, 29, 1);
4845     bool sub_op = extract32(insn, 30, 1);
4846     bool sf = extract32(insn, 31, 1);
4847 
4848     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4849     TCGv_i64 tcg_rd;
4850     TCGv_i64 tcg_result;
4851 
4852     if (imm3 > 4 || opt != 0) {
4853         unallocated_encoding(s);
4854         return;
4855     }
4856 
4857     /* non-flag setting ops may use SP */
4858     if (!setflags) {
4859         tcg_rd = cpu_reg_sp(s, rd);
4860     } else {
4861         tcg_rd = cpu_reg(s, rd);
4862     }
4863     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4864 
4865     tcg_rm = read_cpu_reg(s, rm, sf);
4866     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4867 
4868     tcg_result = tcg_temp_new_i64();
4869 
4870     if (!setflags) {
4871         if (sub_op) {
4872             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4873         } else {
4874             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4875         }
4876     } else {
4877         if (sub_op) {
4878             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4879         } else {
4880             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4881         }
4882     }
4883 
4884     if (sf) {
4885         tcg_gen_mov_i64(tcg_rd, tcg_result);
4886     } else {
4887         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4888     }
4889 }
4890 
4891 /*
4892  * Add/subtract (shifted register)
4893  *
4894  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4895  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4896  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4897  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4898  *
4899  *    sf: 0 -> 32bit, 1 -> 64bit
4900  *    op: 0 -> add  , 1 -> sub
4901  *     S: 1 -> set flags
4902  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4903  *  imm6: Shift amount to apply to Rm before the add/sub
4904  */
4905 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4906 {
4907     int rd = extract32(insn, 0, 5);
4908     int rn = extract32(insn, 5, 5);
4909     int imm6 = extract32(insn, 10, 6);
4910     int rm = extract32(insn, 16, 5);
4911     int shift_type = extract32(insn, 22, 2);
4912     bool setflags = extract32(insn, 29, 1);
4913     bool sub_op = extract32(insn, 30, 1);
4914     bool sf = extract32(insn, 31, 1);
4915 
4916     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4917     TCGv_i64 tcg_rn, tcg_rm;
4918     TCGv_i64 tcg_result;
4919 
4920     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4921         unallocated_encoding(s);
4922         return;
4923     }
4924 
4925     tcg_rn = read_cpu_reg(s, rn, sf);
4926     tcg_rm = read_cpu_reg(s, rm, sf);
4927 
4928     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4929 
4930     tcg_result = tcg_temp_new_i64();
4931 
4932     if (!setflags) {
4933         if (sub_op) {
4934             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4935         } else {
4936             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4937         }
4938     } else {
4939         if (sub_op) {
4940             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4941         } else {
4942             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4943         }
4944     }
4945 
4946     if (sf) {
4947         tcg_gen_mov_i64(tcg_rd, tcg_result);
4948     } else {
4949         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4950     }
4951 }
4952 
4953 /* Data-processing (3 source)
4954  *
4955  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4956  *  +--+------+-----------+------+------+----+------+------+------+
4957  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4958  *  +--+------+-----------+------+------+----+------+------+------+
4959  */
4960 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4961 {
4962     int rd = extract32(insn, 0, 5);
4963     int rn = extract32(insn, 5, 5);
4964     int ra = extract32(insn, 10, 5);
4965     int rm = extract32(insn, 16, 5);
4966     int op_id = (extract32(insn, 29, 3) << 4) |
4967         (extract32(insn, 21, 3) << 1) |
4968         extract32(insn, 15, 1);
4969     bool sf = extract32(insn, 31, 1);
4970     bool is_sub = extract32(op_id, 0, 1);
4971     bool is_high = extract32(op_id, 2, 1);
4972     bool is_signed = false;
4973     TCGv_i64 tcg_op1;
4974     TCGv_i64 tcg_op2;
4975     TCGv_i64 tcg_tmp;
4976 
4977     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4978     switch (op_id) {
4979     case 0x42: /* SMADDL */
4980     case 0x43: /* SMSUBL */
4981     case 0x44: /* SMULH */
4982         is_signed = true;
4983         break;
4984     case 0x0: /* MADD (32bit) */
4985     case 0x1: /* MSUB (32bit) */
4986     case 0x40: /* MADD (64bit) */
4987     case 0x41: /* MSUB (64bit) */
4988     case 0x4a: /* UMADDL */
4989     case 0x4b: /* UMSUBL */
4990     case 0x4c: /* UMULH */
4991         break;
4992     default:
4993         unallocated_encoding(s);
4994         return;
4995     }
4996 
4997     if (is_high) {
4998         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4999         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5000         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5001         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5002 
5003         if (is_signed) {
5004             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5005         } else {
5006             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5007         }
5008         return;
5009     }
5010 
5011     tcg_op1 = tcg_temp_new_i64();
5012     tcg_op2 = tcg_temp_new_i64();
5013     tcg_tmp = tcg_temp_new_i64();
5014 
5015     if (op_id < 0x42) {
5016         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5017         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5018     } else {
5019         if (is_signed) {
5020             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5021             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5022         } else {
5023             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5024             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5025         }
5026     }
5027 
5028     if (ra == 31 && !is_sub) {
5029         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5030         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5031     } else {
5032         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5033         if (is_sub) {
5034             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5035         } else {
5036             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5037         }
5038     }
5039 
5040     if (!sf) {
5041         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5042     }
5043 }
5044 
5045 /* Add/subtract (with carry)
5046  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5047  * +--+--+--+------------------------+------+-------------+------+-----+
5048  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5049  * +--+--+--+------------------------+------+-------------+------+-----+
5050  */
5051 
5052 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5053 {
5054     unsigned int sf, op, setflags, rm, rn, rd;
5055     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5056 
5057     sf = extract32(insn, 31, 1);
5058     op = extract32(insn, 30, 1);
5059     setflags = extract32(insn, 29, 1);
5060     rm = extract32(insn, 16, 5);
5061     rn = extract32(insn, 5, 5);
5062     rd = extract32(insn, 0, 5);
5063 
5064     tcg_rd = cpu_reg(s, rd);
5065     tcg_rn = cpu_reg(s, rn);
5066 
5067     if (op) {
5068         tcg_y = tcg_temp_new_i64();
5069         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5070     } else {
5071         tcg_y = cpu_reg(s, rm);
5072     }
5073 
5074     if (setflags) {
5075         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5076     } else {
5077         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5078     }
5079 }
5080 
5081 /*
5082  * Rotate right into flags
5083  *  31 30 29                21       15          10      5  4      0
5084  * +--+--+--+-----------------+--------+-----------+------+--+------+
5085  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5086  * +--+--+--+-----------------+--------+-----------+------+--+------+
5087  */
5088 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5089 {
5090     int mask = extract32(insn, 0, 4);
5091     int o2 = extract32(insn, 4, 1);
5092     int rn = extract32(insn, 5, 5);
5093     int imm6 = extract32(insn, 15, 6);
5094     int sf_op_s = extract32(insn, 29, 3);
5095     TCGv_i64 tcg_rn;
5096     TCGv_i32 nzcv;
5097 
5098     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5099         unallocated_encoding(s);
5100         return;
5101     }
5102 
5103     tcg_rn = read_cpu_reg(s, rn, 1);
5104     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5105 
5106     nzcv = tcg_temp_new_i32();
5107     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5108 
5109     if (mask & 8) { /* N */
5110         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5111     }
5112     if (mask & 4) { /* Z */
5113         tcg_gen_not_i32(cpu_ZF, nzcv);
5114         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5115     }
5116     if (mask & 2) { /* C */
5117         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5118     }
5119     if (mask & 1) { /* V */
5120         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5121     }
5122 }
5123 
5124 /*
5125  * Evaluate into flags
5126  *  31 30 29                21        15   14        10      5  4      0
5127  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5128  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5129  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5130  */
5131 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5132 {
5133     int o3_mask = extract32(insn, 0, 5);
5134     int rn = extract32(insn, 5, 5);
5135     int o2 = extract32(insn, 15, 6);
5136     int sz = extract32(insn, 14, 1);
5137     int sf_op_s = extract32(insn, 29, 3);
5138     TCGv_i32 tmp;
5139     int shift;
5140 
5141     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5142         !dc_isar_feature(aa64_condm_4, s)) {
5143         unallocated_encoding(s);
5144         return;
5145     }
5146     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5147 
5148     tmp = tcg_temp_new_i32();
5149     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5150     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5151     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5152     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5153     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5154 }
5155 
5156 /* Conditional compare (immediate / register)
5157  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5158  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5159  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5160  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5161  *        [1]                             y                [0]       [0]
5162  */
5163 static void disas_cc(DisasContext *s, uint32_t insn)
5164 {
5165     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5166     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5167     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5168     DisasCompare c;
5169 
5170     if (!extract32(insn, 29, 1)) {
5171         unallocated_encoding(s);
5172         return;
5173     }
5174     if (insn & (1 << 10 | 1 << 4)) {
5175         unallocated_encoding(s);
5176         return;
5177     }
5178     sf = extract32(insn, 31, 1);
5179     op = extract32(insn, 30, 1);
5180     is_imm = extract32(insn, 11, 1);
5181     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5182     cond = extract32(insn, 12, 4);
5183     rn = extract32(insn, 5, 5);
5184     nzcv = extract32(insn, 0, 4);
5185 
5186     /* Set T0 = !COND.  */
5187     tcg_t0 = tcg_temp_new_i32();
5188     arm_test_cc(&c, cond);
5189     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5190 
5191     /* Load the arguments for the new comparison.  */
5192     if (is_imm) {
5193         tcg_y = tcg_temp_new_i64();
5194         tcg_gen_movi_i64(tcg_y, y);
5195     } else {
5196         tcg_y = cpu_reg(s, y);
5197     }
5198     tcg_rn = cpu_reg(s, rn);
5199 
5200     /* Set the flags for the new comparison.  */
5201     tcg_tmp = tcg_temp_new_i64();
5202     if (op) {
5203         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5204     } else {
5205         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5206     }
5207 
5208     /* If COND was false, force the flags to #nzcv.  Compute two masks
5209      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5210      * For tcg hosts that support ANDC, we can make do with just T1.
5211      * In either case, allow the tcg optimizer to delete any unused mask.
5212      */
5213     tcg_t1 = tcg_temp_new_i32();
5214     tcg_t2 = tcg_temp_new_i32();
5215     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5216     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5217 
5218     if (nzcv & 8) { /* N */
5219         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5220     } else {
5221         if (TCG_TARGET_HAS_andc_i32) {
5222             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5223         } else {
5224             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5225         }
5226     }
5227     if (nzcv & 4) { /* Z */
5228         if (TCG_TARGET_HAS_andc_i32) {
5229             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5230         } else {
5231             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5232         }
5233     } else {
5234         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5235     }
5236     if (nzcv & 2) { /* C */
5237         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5238     } else {
5239         if (TCG_TARGET_HAS_andc_i32) {
5240             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5241         } else {
5242             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5243         }
5244     }
5245     if (nzcv & 1) { /* V */
5246         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5247     } else {
5248         if (TCG_TARGET_HAS_andc_i32) {
5249             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5250         } else {
5251             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5252         }
5253     }
5254 }
5255 
5256 /* Conditional select
5257  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5258  * +----+----+---+-----------------+------+------+-----+------+------+
5259  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5260  * +----+----+---+-----------------+------+------+-----+------+------+
5261  */
5262 static void disas_cond_select(DisasContext *s, uint32_t insn)
5263 {
5264     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5265     TCGv_i64 tcg_rd, zero;
5266     DisasCompare64 c;
5267 
5268     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5269         /* S == 1 or op2<1> == 1 */
5270         unallocated_encoding(s);
5271         return;
5272     }
5273     sf = extract32(insn, 31, 1);
5274     else_inv = extract32(insn, 30, 1);
5275     rm = extract32(insn, 16, 5);
5276     cond = extract32(insn, 12, 4);
5277     else_inc = extract32(insn, 10, 1);
5278     rn = extract32(insn, 5, 5);
5279     rd = extract32(insn, 0, 5);
5280 
5281     tcg_rd = cpu_reg(s, rd);
5282 
5283     a64_test_cc(&c, cond);
5284     zero = tcg_constant_i64(0);
5285 
5286     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5287         /* CSET & CSETM.  */
5288         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5289         if (else_inv) {
5290             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5291         }
5292     } else {
5293         TCGv_i64 t_true = cpu_reg(s, rn);
5294         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5295         if (else_inv && else_inc) {
5296             tcg_gen_neg_i64(t_false, t_false);
5297         } else if (else_inv) {
5298             tcg_gen_not_i64(t_false, t_false);
5299         } else if (else_inc) {
5300             tcg_gen_addi_i64(t_false, t_false, 1);
5301         }
5302         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5303     }
5304 
5305     if (!sf) {
5306         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5307     }
5308 }
5309 
5310 static void handle_clz(DisasContext *s, unsigned int sf,
5311                        unsigned int rn, unsigned int rd)
5312 {
5313     TCGv_i64 tcg_rd, tcg_rn;
5314     tcg_rd = cpu_reg(s, rd);
5315     tcg_rn = cpu_reg(s, rn);
5316 
5317     if (sf) {
5318         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5319     } else {
5320         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5321         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5322         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5323         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5324     }
5325 }
5326 
5327 static void handle_cls(DisasContext *s, unsigned int sf,
5328                        unsigned int rn, unsigned int rd)
5329 {
5330     TCGv_i64 tcg_rd, tcg_rn;
5331     tcg_rd = cpu_reg(s, rd);
5332     tcg_rn = cpu_reg(s, rn);
5333 
5334     if (sf) {
5335         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5336     } else {
5337         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5338         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5339         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5340         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5341     }
5342 }
5343 
5344 static void handle_rbit(DisasContext *s, unsigned int sf,
5345                         unsigned int rn, unsigned int rd)
5346 {
5347     TCGv_i64 tcg_rd, tcg_rn;
5348     tcg_rd = cpu_reg(s, rd);
5349     tcg_rn = cpu_reg(s, rn);
5350 
5351     if (sf) {
5352         gen_helper_rbit64(tcg_rd, tcg_rn);
5353     } else {
5354         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5355         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5356         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5357         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5358     }
5359 }
5360 
5361 /* REV with sf==1, opcode==3 ("REV64") */
5362 static void handle_rev64(DisasContext *s, unsigned int sf,
5363                          unsigned int rn, unsigned int rd)
5364 {
5365     if (!sf) {
5366         unallocated_encoding(s);
5367         return;
5368     }
5369     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5370 }
5371 
5372 /* REV with sf==0, opcode==2
5373  * REV32 (sf==1, opcode==2)
5374  */
5375 static void handle_rev32(DisasContext *s, unsigned int sf,
5376                          unsigned int rn, unsigned int rd)
5377 {
5378     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5379     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5380 
5381     if (sf) {
5382         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5383         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5384     } else {
5385         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5386     }
5387 }
5388 
5389 /* REV16 (opcode==1) */
5390 static void handle_rev16(DisasContext *s, unsigned int sf,
5391                          unsigned int rn, unsigned int rd)
5392 {
5393     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5394     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5395     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5396     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5397 
5398     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5399     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5400     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5401     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5402     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5403 }
5404 
5405 /* Data-processing (1 source)
5406  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5407  * +----+---+---+-----------------+---------+--------+------+------+
5408  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5409  * +----+---+---+-----------------+---------+--------+------+------+
5410  */
5411 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5412 {
5413     unsigned int sf, opcode, opcode2, rn, rd;
5414     TCGv_i64 tcg_rd;
5415 
5416     if (extract32(insn, 29, 1)) {
5417         unallocated_encoding(s);
5418         return;
5419     }
5420 
5421     sf = extract32(insn, 31, 1);
5422     opcode = extract32(insn, 10, 6);
5423     opcode2 = extract32(insn, 16, 5);
5424     rn = extract32(insn, 5, 5);
5425     rd = extract32(insn, 0, 5);
5426 
5427 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5428 
5429     switch (MAP(sf, opcode2, opcode)) {
5430     case MAP(0, 0x00, 0x00): /* RBIT */
5431     case MAP(1, 0x00, 0x00):
5432         handle_rbit(s, sf, rn, rd);
5433         break;
5434     case MAP(0, 0x00, 0x01): /* REV16 */
5435     case MAP(1, 0x00, 0x01):
5436         handle_rev16(s, sf, rn, rd);
5437         break;
5438     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5439     case MAP(1, 0x00, 0x02):
5440         handle_rev32(s, sf, rn, rd);
5441         break;
5442     case MAP(1, 0x00, 0x03): /* REV64 */
5443         handle_rev64(s, sf, rn, rd);
5444         break;
5445     case MAP(0, 0x00, 0x04): /* CLZ */
5446     case MAP(1, 0x00, 0x04):
5447         handle_clz(s, sf, rn, rd);
5448         break;
5449     case MAP(0, 0x00, 0x05): /* CLS */
5450     case MAP(1, 0x00, 0x05):
5451         handle_cls(s, sf, rn, rd);
5452         break;
5453     case MAP(1, 0x01, 0x00): /* PACIA */
5454         if (s->pauth_active) {
5455             tcg_rd = cpu_reg(s, rd);
5456             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5457         } else if (!dc_isar_feature(aa64_pauth, s)) {
5458             goto do_unallocated;
5459         }
5460         break;
5461     case MAP(1, 0x01, 0x01): /* PACIB */
5462         if (s->pauth_active) {
5463             tcg_rd = cpu_reg(s, rd);
5464             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5465         } else if (!dc_isar_feature(aa64_pauth, s)) {
5466             goto do_unallocated;
5467         }
5468         break;
5469     case MAP(1, 0x01, 0x02): /* PACDA */
5470         if (s->pauth_active) {
5471             tcg_rd = cpu_reg(s, rd);
5472             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5473         } else if (!dc_isar_feature(aa64_pauth, s)) {
5474             goto do_unallocated;
5475         }
5476         break;
5477     case MAP(1, 0x01, 0x03): /* PACDB */
5478         if (s->pauth_active) {
5479             tcg_rd = cpu_reg(s, rd);
5480             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5481         } else if (!dc_isar_feature(aa64_pauth, s)) {
5482             goto do_unallocated;
5483         }
5484         break;
5485     case MAP(1, 0x01, 0x04): /* AUTIA */
5486         if (s->pauth_active) {
5487             tcg_rd = cpu_reg(s, rd);
5488             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5489         } else if (!dc_isar_feature(aa64_pauth, s)) {
5490             goto do_unallocated;
5491         }
5492         break;
5493     case MAP(1, 0x01, 0x05): /* AUTIB */
5494         if (s->pauth_active) {
5495             tcg_rd = cpu_reg(s, rd);
5496             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5497         } else if (!dc_isar_feature(aa64_pauth, s)) {
5498             goto do_unallocated;
5499         }
5500         break;
5501     case MAP(1, 0x01, 0x06): /* AUTDA */
5502         if (s->pauth_active) {
5503             tcg_rd = cpu_reg(s, rd);
5504             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5505         } else if (!dc_isar_feature(aa64_pauth, s)) {
5506             goto do_unallocated;
5507         }
5508         break;
5509     case MAP(1, 0x01, 0x07): /* AUTDB */
5510         if (s->pauth_active) {
5511             tcg_rd = cpu_reg(s, rd);
5512             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5513         } else if (!dc_isar_feature(aa64_pauth, s)) {
5514             goto do_unallocated;
5515         }
5516         break;
5517     case MAP(1, 0x01, 0x08): /* PACIZA */
5518         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5519             goto do_unallocated;
5520         } else if (s->pauth_active) {
5521             tcg_rd = cpu_reg(s, rd);
5522             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5523         }
5524         break;
5525     case MAP(1, 0x01, 0x09): /* PACIZB */
5526         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5527             goto do_unallocated;
5528         } else if (s->pauth_active) {
5529             tcg_rd = cpu_reg(s, rd);
5530             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5531         }
5532         break;
5533     case MAP(1, 0x01, 0x0a): /* PACDZA */
5534         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5535             goto do_unallocated;
5536         } else if (s->pauth_active) {
5537             tcg_rd = cpu_reg(s, rd);
5538             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5539         }
5540         break;
5541     case MAP(1, 0x01, 0x0b): /* PACDZB */
5542         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5543             goto do_unallocated;
5544         } else if (s->pauth_active) {
5545             tcg_rd = cpu_reg(s, rd);
5546             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5547         }
5548         break;
5549     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5550         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5551             goto do_unallocated;
5552         } else if (s->pauth_active) {
5553             tcg_rd = cpu_reg(s, rd);
5554             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5555         }
5556         break;
5557     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5558         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5559             goto do_unallocated;
5560         } else if (s->pauth_active) {
5561             tcg_rd = cpu_reg(s, rd);
5562             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5563         }
5564         break;
5565     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5566         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5567             goto do_unallocated;
5568         } else if (s->pauth_active) {
5569             tcg_rd = cpu_reg(s, rd);
5570             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5571         }
5572         break;
5573     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5574         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5575             goto do_unallocated;
5576         } else if (s->pauth_active) {
5577             tcg_rd = cpu_reg(s, rd);
5578             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5579         }
5580         break;
5581     case MAP(1, 0x01, 0x10): /* XPACI */
5582         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5583             goto do_unallocated;
5584         } else if (s->pauth_active) {
5585             tcg_rd = cpu_reg(s, rd);
5586             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5587         }
5588         break;
5589     case MAP(1, 0x01, 0x11): /* XPACD */
5590         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5591             goto do_unallocated;
5592         } else if (s->pauth_active) {
5593             tcg_rd = cpu_reg(s, rd);
5594             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5595         }
5596         break;
5597     default:
5598     do_unallocated:
5599         unallocated_encoding(s);
5600         break;
5601     }
5602 
5603 #undef MAP
5604 }
5605 
5606 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5607                        unsigned int rm, unsigned int rn, unsigned int rd)
5608 {
5609     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5610     tcg_rd = cpu_reg(s, rd);
5611 
5612     if (!sf && is_signed) {
5613         tcg_n = tcg_temp_new_i64();
5614         tcg_m = tcg_temp_new_i64();
5615         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5616         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5617     } else {
5618         tcg_n = read_cpu_reg(s, rn, sf);
5619         tcg_m = read_cpu_reg(s, rm, sf);
5620     }
5621 
5622     if (is_signed) {
5623         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5624     } else {
5625         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5626     }
5627 
5628     if (!sf) { /* zero extend final result */
5629         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5630     }
5631 }
5632 
5633 /* LSLV, LSRV, ASRV, RORV */
5634 static void handle_shift_reg(DisasContext *s,
5635                              enum a64_shift_type shift_type, unsigned int sf,
5636                              unsigned int rm, unsigned int rn, unsigned int rd)
5637 {
5638     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5639     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5640     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5641 
5642     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5643     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5644 }
5645 
5646 /* CRC32[BHWX], CRC32C[BHWX] */
5647 static void handle_crc32(DisasContext *s,
5648                          unsigned int sf, unsigned int sz, bool crc32c,
5649                          unsigned int rm, unsigned int rn, unsigned int rd)
5650 {
5651     TCGv_i64 tcg_acc, tcg_val;
5652     TCGv_i32 tcg_bytes;
5653 
5654     if (!dc_isar_feature(aa64_crc32, s)
5655         || (sf == 1 && sz != 3)
5656         || (sf == 0 && sz == 3)) {
5657         unallocated_encoding(s);
5658         return;
5659     }
5660 
5661     if (sz == 3) {
5662         tcg_val = cpu_reg(s, rm);
5663     } else {
5664         uint64_t mask;
5665         switch (sz) {
5666         case 0:
5667             mask = 0xFF;
5668             break;
5669         case 1:
5670             mask = 0xFFFF;
5671             break;
5672         case 2:
5673             mask = 0xFFFFFFFF;
5674             break;
5675         default:
5676             g_assert_not_reached();
5677         }
5678         tcg_val = tcg_temp_new_i64();
5679         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5680     }
5681 
5682     tcg_acc = cpu_reg(s, rn);
5683     tcg_bytes = tcg_constant_i32(1 << sz);
5684 
5685     if (crc32c) {
5686         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5687     } else {
5688         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5689     }
5690 }
5691 
5692 /* Data-processing (2 source)
5693  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5694  * +----+---+---+-----------------+------+--------+------+------+
5695  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5696  * +----+---+---+-----------------+------+--------+------+------+
5697  */
5698 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5699 {
5700     unsigned int sf, rm, opcode, rn, rd, setflag;
5701     sf = extract32(insn, 31, 1);
5702     setflag = extract32(insn, 29, 1);
5703     rm = extract32(insn, 16, 5);
5704     opcode = extract32(insn, 10, 6);
5705     rn = extract32(insn, 5, 5);
5706     rd = extract32(insn, 0, 5);
5707 
5708     if (setflag && opcode != 0) {
5709         unallocated_encoding(s);
5710         return;
5711     }
5712 
5713     switch (opcode) {
5714     case 0: /* SUBP(S) */
5715         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5716             goto do_unallocated;
5717         } else {
5718             TCGv_i64 tcg_n, tcg_m, tcg_d;
5719 
5720             tcg_n = read_cpu_reg_sp(s, rn, true);
5721             tcg_m = read_cpu_reg_sp(s, rm, true);
5722             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5723             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5724             tcg_d = cpu_reg(s, rd);
5725 
5726             if (setflag) {
5727                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5728             } else {
5729                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5730             }
5731         }
5732         break;
5733     case 2: /* UDIV */
5734         handle_div(s, false, sf, rm, rn, rd);
5735         break;
5736     case 3: /* SDIV */
5737         handle_div(s, true, sf, rm, rn, rd);
5738         break;
5739     case 4: /* IRG */
5740         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5741             goto do_unallocated;
5742         }
5743         if (s->ata) {
5744             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5745                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5746         } else {
5747             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5748                                              cpu_reg_sp(s, rn));
5749         }
5750         break;
5751     case 5: /* GMI */
5752         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5753             goto do_unallocated;
5754         } else {
5755             TCGv_i64 t = tcg_temp_new_i64();
5756 
5757             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5758             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5759             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5760         }
5761         break;
5762     case 8: /* LSLV */
5763         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5764         break;
5765     case 9: /* LSRV */
5766         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5767         break;
5768     case 10: /* ASRV */
5769         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5770         break;
5771     case 11: /* RORV */
5772         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5773         break;
5774     case 12: /* PACGA */
5775         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5776             goto do_unallocated;
5777         }
5778         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5779                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5780         break;
5781     case 16:
5782     case 17:
5783     case 18:
5784     case 19:
5785     case 20:
5786     case 21:
5787     case 22:
5788     case 23: /* CRC32 */
5789     {
5790         int sz = extract32(opcode, 0, 2);
5791         bool crc32c = extract32(opcode, 2, 1);
5792         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5793         break;
5794     }
5795     default:
5796     do_unallocated:
5797         unallocated_encoding(s);
5798         break;
5799     }
5800 }
5801 
5802 /*
5803  * Data processing - register
5804  *  31  30 29  28      25    21  20  16      10         0
5805  * +--+---+--+---+-------+-----+-------+-------+---------+
5806  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5807  * +--+---+--+---+-------+-----+-------+-------+---------+
5808  */
5809 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5810 {
5811     int op0 = extract32(insn, 30, 1);
5812     int op1 = extract32(insn, 28, 1);
5813     int op2 = extract32(insn, 21, 4);
5814     int op3 = extract32(insn, 10, 6);
5815 
5816     if (!op1) {
5817         if (op2 & 8) {
5818             if (op2 & 1) {
5819                 /* Add/sub (extended register) */
5820                 disas_add_sub_ext_reg(s, insn);
5821             } else {
5822                 /* Add/sub (shifted register) */
5823                 disas_add_sub_reg(s, insn);
5824             }
5825         } else {
5826             /* Logical (shifted register) */
5827             disas_logic_reg(s, insn);
5828         }
5829         return;
5830     }
5831 
5832     switch (op2) {
5833     case 0x0:
5834         switch (op3) {
5835         case 0x00: /* Add/subtract (with carry) */
5836             disas_adc_sbc(s, insn);
5837             break;
5838 
5839         case 0x01: /* Rotate right into flags */
5840         case 0x21:
5841             disas_rotate_right_into_flags(s, insn);
5842             break;
5843 
5844         case 0x02: /* Evaluate into flags */
5845         case 0x12:
5846         case 0x22:
5847         case 0x32:
5848             disas_evaluate_into_flags(s, insn);
5849             break;
5850 
5851         default:
5852             goto do_unallocated;
5853         }
5854         break;
5855 
5856     case 0x2: /* Conditional compare */
5857         disas_cc(s, insn); /* both imm and reg forms */
5858         break;
5859 
5860     case 0x4: /* Conditional select */
5861         disas_cond_select(s, insn);
5862         break;
5863 
5864     case 0x6: /* Data-processing */
5865         if (op0) {    /* (1 source) */
5866             disas_data_proc_1src(s, insn);
5867         } else {      /* (2 source) */
5868             disas_data_proc_2src(s, insn);
5869         }
5870         break;
5871     case 0x8 ... 0xf: /* (3 source) */
5872         disas_data_proc_3src(s, insn);
5873         break;
5874 
5875     default:
5876     do_unallocated:
5877         unallocated_encoding(s);
5878         break;
5879     }
5880 }
5881 
5882 static void handle_fp_compare(DisasContext *s, int size,
5883                               unsigned int rn, unsigned int rm,
5884                               bool cmp_with_zero, bool signal_all_nans)
5885 {
5886     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5887     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5888 
5889     if (size == MO_64) {
5890         TCGv_i64 tcg_vn, tcg_vm;
5891 
5892         tcg_vn = read_fp_dreg(s, rn);
5893         if (cmp_with_zero) {
5894             tcg_vm = tcg_constant_i64(0);
5895         } else {
5896             tcg_vm = read_fp_dreg(s, rm);
5897         }
5898         if (signal_all_nans) {
5899             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5900         } else {
5901             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5902         }
5903     } else {
5904         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5905         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5906 
5907         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5908         if (cmp_with_zero) {
5909             tcg_gen_movi_i32(tcg_vm, 0);
5910         } else {
5911             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5912         }
5913 
5914         switch (size) {
5915         case MO_32:
5916             if (signal_all_nans) {
5917                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5918             } else {
5919                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5920             }
5921             break;
5922         case MO_16:
5923             if (signal_all_nans) {
5924                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5925             } else {
5926                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5927             }
5928             break;
5929         default:
5930             g_assert_not_reached();
5931         }
5932     }
5933 
5934     gen_set_nzcv(tcg_flags);
5935 }
5936 
5937 /* Floating point compare
5938  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5939  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5940  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5941  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5942  */
5943 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5944 {
5945     unsigned int mos, type, rm, op, rn, opc, op2r;
5946     int size;
5947 
5948     mos = extract32(insn, 29, 3);
5949     type = extract32(insn, 22, 2);
5950     rm = extract32(insn, 16, 5);
5951     op = extract32(insn, 14, 2);
5952     rn = extract32(insn, 5, 5);
5953     opc = extract32(insn, 3, 2);
5954     op2r = extract32(insn, 0, 3);
5955 
5956     if (mos || op || op2r) {
5957         unallocated_encoding(s);
5958         return;
5959     }
5960 
5961     switch (type) {
5962     case 0:
5963         size = MO_32;
5964         break;
5965     case 1:
5966         size = MO_64;
5967         break;
5968     case 3:
5969         size = MO_16;
5970         if (dc_isar_feature(aa64_fp16, s)) {
5971             break;
5972         }
5973         /* fallthru */
5974     default:
5975         unallocated_encoding(s);
5976         return;
5977     }
5978 
5979     if (!fp_access_check(s)) {
5980         return;
5981     }
5982 
5983     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5984 }
5985 
5986 /* Floating point conditional compare
5987  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5988  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5989  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5990  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5991  */
5992 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5993 {
5994     unsigned int mos, type, rm, cond, rn, op, nzcv;
5995     TCGLabel *label_continue = NULL;
5996     int size;
5997 
5998     mos = extract32(insn, 29, 3);
5999     type = extract32(insn, 22, 2);
6000     rm = extract32(insn, 16, 5);
6001     cond = extract32(insn, 12, 4);
6002     rn = extract32(insn, 5, 5);
6003     op = extract32(insn, 4, 1);
6004     nzcv = extract32(insn, 0, 4);
6005 
6006     if (mos) {
6007         unallocated_encoding(s);
6008         return;
6009     }
6010 
6011     switch (type) {
6012     case 0:
6013         size = MO_32;
6014         break;
6015     case 1:
6016         size = MO_64;
6017         break;
6018     case 3:
6019         size = MO_16;
6020         if (dc_isar_feature(aa64_fp16, s)) {
6021             break;
6022         }
6023         /* fallthru */
6024     default:
6025         unallocated_encoding(s);
6026         return;
6027     }
6028 
6029     if (!fp_access_check(s)) {
6030         return;
6031     }
6032 
6033     if (cond < 0x0e) { /* not always */
6034         TCGLabel *label_match = gen_new_label();
6035         label_continue = gen_new_label();
6036         arm_gen_test_cc(cond, label_match);
6037         /* nomatch: */
6038         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6039         tcg_gen_br(label_continue);
6040         gen_set_label(label_match);
6041     }
6042 
6043     handle_fp_compare(s, size, rn, rm, false, op);
6044 
6045     if (cond < 0x0e) {
6046         gen_set_label(label_continue);
6047     }
6048 }
6049 
6050 /* Floating point conditional select
6051  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6052  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6053  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6054  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6055  */
6056 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6057 {
6058     unsigned int mos, type, rm, cond, rn, rd;
6059     TCGv_i64 t_true, t_false;
6060     DisasCompare64 c;
6061     MemOp sz;
6062 
6063     mos = extract32(insn, 29, 3);
6064     type = extract32(insn, 22, 2);
6065     rm = extract32(insn, 16, 5);
6066     cond = extract32(insn, 12, 4);
6067     rn = extract32(insn, 5, 5);
6068     rd = extract32(insn, 0, 5);
6069 
6070     if (mos) {
6071         unallocated_encoding(s);
6072         return;
6073     }
6074 
6075     switch (type) {
6076     case 0:
6077         sz = MO_32;
6078         break;
6079     case 1:
6080         sz = MO_64;
6081         break;
6082     case 3:
6083         sz = MO_16;
6084         if (dc_isar_feature(aa64_fp16, s)) {
6085             break;
6086         }
6087         /* fallthru */
6088     default:
6089         unallocated_encoding(s);
6090         return;
6091     }
6092 
6093     if (!fp_access_check(s)) {
6094         return;
6095     }
6096 
6097     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6098     t_true = tcg_temp_new_i64();
6099     t_false = tcg_temp_new_i64();
6100     read_vec_element(s, t_true, rn, 0, sz);
6101     read_vec_element(s, t_false, rm, 0, sz);
6102 
6103     a64_test_cc(&c, cond);
6104     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6105                         t_true, t_false);
6106 
6107     /* Note that sregs & hregs write back zeros to the high bits,
6108        and we've already done the zero-extension.  */
6109     write_fp_dreg(s, rd, t_true);
6110 }
6111 
6112 /* Floating-point data-processing (1 source) - half precision */
6113 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6114 {
6115     TCGv_ptr fpst = NULL;
6116     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6117     TCGv_i32 tcg_res = tcg_temp_new_i32();
6118 
6119     switch (opcode) {
6120     case 0x0: /* FMOV */
6121         tcg_gen_mov_i32(tcg_res, tcg_op);
6122         break;
6123     case 0x1: /* FABS */
6124         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6125         break;
6126     case 0x2: /* FNEG */
6127         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6128         break;
6129     case 0x3: /* FSQRT */
6130         fpst = fpstatus_ptr(FPST_FPCR_F16);
6131         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6132         break;
6133     case 0x8: /* FRINTN */
6134     case 0x9: /* FRINTP */
6135     case 0xa: /* FRINTM */
6136     case 0xb: /* FRINTZ */
6137     case 0xc: /* FRINTA */
6138     {
6139         TCGv_i32 tcg_rmode;
6140 
6141         fpst = fpstatus_ptr(FPST_FPCR_F16);
6142         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6143         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6144         gen_restore_rmode(tcg_rmode, fpst);
6145         break;
6146     }
6147     case 0xe: /* FRINTX */
6148         fpst = fpstatus_ptr(FPST_FPCR_F16);
6149         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6150         break;
6151     case 0xf: /* FRINTI */
6152         fpst = fpstatus_ptr(FPST_FPCR_F16);
6153         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6154         break;
6155     default:
6156         g_assert_not_reached();
6157     }
6158 
6159     write_fp_sreg(s, rd, tcg_res);
6160 }
6161 
6162 /* Floating-point data-processing (1 source) - single precision */
6163 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6164 {
6165     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6166     TCGv_i32 tcg_op, tcg_res;
6167     TCGv_ptr fpst;
6168     int rmode = -1;
6169 
6170     tcg_op = read_fp_sreg(s, rn);
6171     tcg_res = tcg_temp_new_i32();
6172 
6173     switch (opcode) {
6174     case 0x0: /* FMOV */
6175         tcg_gen_mov_i32(tcg_res, tcg_op);
6176         goto done;
6177     case 0x1: /* FABS */
6178         gen_helper_vfp_abss(tcg_res, tcg_op);
6179         goto done;
6180     case 0x2: /* FNEG */
6181         gen_helper_vfp_negs(tcg_res, tcg_op);
6182         goto done;
6183     case 0x3: /* FSQRT */
6184         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6185         goto done;
6186     case 0x6: /* BFCVT */
6187         gen_fpst = gen_helper_bfcvt;
6188         break;
6189     case 0x8: /* FRINTN */
6190     case 0x9: /* FRINTP */
6191     case 0xa: /* FRINTM */
6192     case 0xb: /* FRINTZ */
6193     case 0xc: /* FRINTA */
6194         rmode = opcode & 7;
6195         gen_fpst = gen_helper_rints;
6196         break;
6197     case 0xe: /* FRINTX */
6198         gen_fpst = gen_helper_rints_exact;
6199         break;
6200     case 0xf: /* FRINTI */
6201         gen_fpst = gen_helper_rints;
6202         break;
6203     case 0x10: /* FRINT32Z */
6204         rmode = FPROUNDING_ZERO;
6205         gen_fpst = gen_helper_frint32_s;
6206         break;
6207     case 0x11: /* FRINT32X */
6208         gen_fpst = gen_helper_frint32_s;
6209         break;
6210     case 0x12: /* FRINT64Z */
6211         rmode = FPROUNDING_ZERO;
6212         gen_fpst = gen_helper_frint64_s;
6213         break;
6214     case 0x13: /* FRINT64X */
6215         gen_fpst = gen_helper_frint64_s;
6216         break;
6217     default:
6218         g_assert_not_reached();
6219     }
6220 
6221     fpst = fpstatus_ptr(FPST_FPCR);
6222     if (rmode >= 0) {
6223         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6224         gen_fpst(tcg_res, tcg_op, fpst);
6225         gen_restore_rmode(tcg_rmode, fpst);
6226     } else {
6227         gen_fpst(tcg_res, tcg_op, fpst);
6228     }
6229 
6230  done:
6231     write_fp_sreg(s, rd, tcg_res);
6232 }
6233 
6234 /* Floating-point data-processing (1 source) - double precision */
6235 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6236 {
6237     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6238     TCGv_i64 tcg_op, tcg_res;
6239     TCGv_ptr fpst;
6240     int rmode = -1;
6241 
6242     switch (opcode) {
6243     case 0x0: /* FMOV */
6244         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6245         return;
6246     }
6247 
6248     tcg_op = read_fp_dreg(s, rn);
6249     tcg_res = tcg_temp_new_i64();
6250 
6251     switch (opcode) {
6252     case 0x1: /* FABS */
6253         gen_helper_vfp_absd(tcg_res, tcg_op);
6254         goto done;
6255     case 0x2: /* FNEG */
6256         gen_helper_vfp_negd(tcg_res, tcg_op);
6257         goto done;
6258     case 0x3: /* FSQRT */
6259         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6260         goto done;
6261     case 0x8: /* FRINTN */
6262     case 0x9: /* FRINTP */
6263     case 0xa: /* FRINTM */
6264     case 0xb: /* FRINTZ */
6265     case 0xc: /* FRINTA */
6266         rmode = opcode & 7;
6267         gen_fpst = gen_helper_rintd;
6268         break;
6269     case 0xe: /* FRINTX */
6270         gen_fpst = gen_helper_rintd_exact;
6271         break;
6272     case 0xf: /* FRINTI */
6273         gen_fpst = gen_helper_rintd;
6274         break;
6275     case 0x10: /* FRINT32Z */
6276         rmode = FPROUNDING_ZERO;
6277         gen_fpst = gen_helper_frint32_d;
6278         break;
6279     case 0x11: /* FRINT32X */
6280         gen_fpst = gen_helper_frint32_d;
6281         break;
6282     case 0x12: /* FRINT64Z */
6283         rmode = FPROUNDING_ZERO;
6284         gen_fpst = gen_helper_frint64_d;
6285         break;
6286     case 0x13: /* FRINT64X */
6287         gen_fpst = gen_helper_frint64_d;
6288         break;
6289     default:
6290         g_assert_not_reached();
6291     }
6292 
6293     fpst = fpstatus_ptr(FPST_FPCR);
6294     if (rmode >= 0) {
6295         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6296         gen_fpst(tcg_res, tcg_op, fpst);
6297         gen_restore_rmode(tcg_rmode, fpst);
6298     } else {
6299         gen_fpst(tcg_res, tcg_op, fpst);
6300     }
6301 
6302  done:
6303     write_fp_dreg(s, rd, tcg_res);
6304 }
6305 
6306 static void handle_fp_fcvt(DisasContext *s, int opcode,
6307                            int rd, int rn, int dtype, int ntype)
6308 {
6309     switch (ntype) {
6310     case 0x0:
6311     {
6312         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6313         if (dtype == 1) {
6314             /* Single to double */
6315             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6316             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6317             write_fp_dreg(s, rd, tcg_rd);
6318         } else {
6319             /* Single to half */
6320             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6321             TCGv_i32 ahp = get_ahp_flag();
6322             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6323 
6324             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6325             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6326             write_fp_sreg(s, rd, tcg_rd);
6327         }
6328         break;
6329     }
6330     case 0x1:
6331     {
6332         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6333         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6334         if (dtype == 0) {
6335             /* Double to single */
6336             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6337         } else {
6338             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6339             TCGv_i32 ahp = get_ahp_flag();
6340             /* Double to half */
6341             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6342             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6343         }
6344         write_fp_sreg(s, rd, tcg_rd);
6345         break;
6346     }
6347     case 0x3:
6348     {
6349         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6350         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6351         TCGv_i32 tcg_ahp = get_ahp_flag();
6352         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6353         if (dtype == 0) {
6354             /* Half to single */
6355             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6356             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6357             write_fp_sreg(s, rd, tcg_rd);
6358         } else {
6359             /* Half to double */
6360             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6361             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6362             write_fp_dreg(s, rd, tcg_rd);
6363         }
6364         break;
6365     }
6366     default:
6367         g_assert_not_reached();
6368     }
6369 }
6370 
6371 /* Floating point data-processing (1 source)
6372  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6373  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6374  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6375  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6376  */
6377 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6378 {
6379     int mos = extract32(insn, 29, 3);
6380     int type = extract32(insn, 22, 2);
6381     int opcode = extract32(insn, 15, 6);
6382     int rn = extract32(insn, 5, 5);
6383     int rd = extract32(insn, 0, 5);
6384 
6385     if (mos) {
6386         goto do_unallocated;
6387     }
6388 
6389     switch (opcode) {
6390     case 0x4: case 0x5: case 0x7:
6391     {
6392         /* FCVT between half, single and double precision */
6393         int dtype = extract32(opcode, 0, 2);
6394         if (type == 2 || dtype == type) {
6395             goto do_unallocated;
6396         }
6397         if (!fp_access_check(s)) {
6398             return;
6399         }
6400 
6401         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6402         break;
6403     }
6404 
6405     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6406         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6407             goto do_unallocated;
6408         }
6409         /* fall through */
6410     case 0x0 ... 0x3:
6411     case 0x8 ... 0xc:
6412     case 0xe ... 0xf:
6413         /* 32-to-32 and 64-to-64 ops */
6414         switch (type) {
6415         case 0:
6416             if (!fp_access_check(s)) {
6417                 return;
6418             }
6419             handle_fp_1src_single(s, opcode, rd, rn);
6420             break;
6421         case 1:
6422             if (!fp_access_check(s)) {
6423                 return;
6424             }
6425             handle_fp_1src_double(s, opcode, rd, rn);
6426             break;
6427         case 3:
6428             if (!dc_isar_feature(aa64_fp16, s)) {
6429                 goto do_unallocated;
6430             }
6431 
6432             if (!fp_access_check(s)) {
6433                 return;
6434             }
6435             handle_fp_1src_half(s, opcode, rd, rn);
6436             break;
6437         default:
6438             goto do_unallocated;
6439         }
6440         break;
6441 
6442     case 0x6:
6443         switch (type) {
6444         case 1: /* BFCVT */
6445             if (!dc_isar_feature(aa64_bf16, s)) {
6446                 goto do_unallocated;
6447             }
6448             if (!fp_access_check(s)) {
6449                 return;
6450             }
6451             handle_fp_1src_single(s, opcode, rd, rn);
6452             break;
6453         default:
6454             goto do_unallocated;
6455         }
6456         break;
6457 
6458     default:
6459     do_unallocated:
6460         unallocated_encoding(s);
6461         break;
6462     }
6463 }
6464 
6465 /* Floating-point data-processing (2 source) - single precision */
6466 static void handle_fp_2src_single(DisasContext *s, int opcode,
6467                                   int rd, int rn, int rm)
6468 {
6469     TCGv_i32 tcg_op1;
6470     TCGv_i32 tcg_op2;
6471     TCGv_i32 tcg_res;
6472     TCGv_ptr fpst;
6473 
6474     tcg_res = tcg_temp_new_i32();
6475     fpst = fpstatus_ptr(FPST_FPCR);
6476     tcg_op1 = read_fp_sreg(s, rn);
6477     tcg_op2 = read_fp_sreg(s, rm);
6478 
6479     switch (opcode) {
6480     case 0x0: /* FMUL */
6481         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6482         break;
6483     case 0x1: /* FDIV */
6484         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6485         break;
6486     case 0x2: /* FADD */
6487         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6488         break;
6489     case 0x3: /* FSUB */
6490         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6491         break;
6492     case 0x4: /* FMAX */
6493         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6494         break;
6495     case 0x5: /* FMIN */
6496         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6497         break;
6498     case 0x6: /* FMAXNM */
6499         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6500         break;
6501     case 0x7: /* FMINNM */
6502         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6503         break;
6504     case 0x8: /* FNMUL */
6505         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6506         gen_helper_vfp_negs(tcg_res, tcg_res);
6507         break;
6508     }
6509 
6510     write_fp_sreg(s, rd, tcg_res);
6511 }
6512 
6513 /* Floating-point data-processing (2 source) - double precision */
6514 static void handle_fp_2src_double(DisasContext *s, int opcode,
6515                                   int rd, int rn, int rm)
6516 {
6517     TCGv_i64 tcg_op1;
6518     TCGv_i64 tcg_op2;
6519     TCGv_i64 tcg_res;
6520     TCGv_ptr fpst;
6521 
6522     tcg_res = tcg_temp_new_i64();
6523     fpst = fpstatus_ptr(FPST_FPCR);
6524     tcg_op1 = read_fp_dreg(s, rn);
6525     tcg_op2 = read_fp_dreg(s, rm);
6526 
6527     switch (opcode) {
6528     case 0x0: /* FMUL */
6529         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6530         break;
6531     case 0x1: /* FDIV */
6532         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6533         break;
6534     case 0x2: /* FADD */
6535         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6536         break;
6537     case 0x3: /* FSUB */
6538         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6539         break;
6540     case 0x4: /* FMAX */
6541         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6542         break;
6543     case 0x5: /* FMIN */
6544         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6545         break;
6546     case 0x6: /* FMAXNM */
6547         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6548         break;
6549     case 0x7: /* FMINNM */
6550         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6551         break;
6552     case 0x8: /* FNMUL */
6553         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6554         gen_helper_vfp_negd(tcg_res, tcg_res);
6555         break;
6556     }
6557 
6558     write_fp_dreg(s, rd, tcg_res);
6559 }
6560 
6561 /* Floating-point data-processing (2 source) - half precision */
6562 static void handle_fp_2src_half(DisasContext *s, int opcode,
6563                                 int rd, int rn, int rm)
6564 {
6565     TCGv_i32 tcg_op1;
6566     TCGv_i32 tcg_op2;
6567     TCGv_i32 tcg_res;
6568     TCGv_ptr fpst;
6569 
6570     tcg_res = tcg_temp_new_i32();
6571     fpst = fpstatus_ptr(FPST_FPCR_F16);
6572     tcg_op1 = read_fp_hreg(s, rn);
6573     tcg_op2 = read_fp_hreg(s, rm);
6574 
6575     switch (opcode) {
6576     case 0x0: /* FMUL */
6577         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6578         break;
6579     case 0x1: /* FDIV */
6580         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6581         break;
6582     case 0x2: /* FADD */
6583         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6584         break;
6585     case 0x3: /* FSUB */
6586         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6587         break;
6588     case 0x4: /* FMAX */
6589         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6590         break;
6591     case 0x5: /* FMIN */
6592         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6593         break;
6594     case 0x6: /* FMAXNM */
6595         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6596         break;
6597     case 0x7: /* FMINNM */
6598         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6599         break;
6600     case 0x8: /* FNMUL */
6601         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6602         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6603         break;
6604     default:
6605         g_assert_not_reached();
6606     }
6607 
6608     write_fp_sreg(s, rd, tcg_res);
6609 }
6610 
6611 /* Floating point data-processing (2 source)
6612  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6613  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6614  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6615  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6616  */
6617 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6618 {
6619     int mos = extract32(insn, 29, 3);
6620     int type = extract32(insn, 22, 2);
6621     int rd = extract32(insn, 0, 5);
6622     int rn = extract32(insn, 5, 5);
6623     int rm = extract32(insn, 16, 5);
6624     int opcode = extract32(insn, 12, 4);
6625 
6626     if (opcode > 8 || mos) {
6627         unallocated_encoding(s);
6628         return;
6629     }
6630 
6631     switch (type) {
6632     case 0:
6633         if (!fp_access_check(s)) {
6634             return;
6635         }
6636         handle_fp_2src_single(s, opcode, rd, rn, rm);
6637         break;
6638     case 1:
6639         if (!fp_access_check(s)) {
6640             return;
6641         }
6642         handle_fp_2src_double(s, opcode, rd, rn, rm);
6643         break;
6644     case 3:
6645         if (!dc_isar_feature(aa64_fp16, s)) {
6646             unallocated_encoding(s);
6647             return;
6648         }
6649         if (!fp_access_check(s)) {
6650             return;
6651         }
6652         handle_fp_2src_half(s, opcode, rd, rn, rm);
6653         break;
6654     default:
6655         unallocated_encoding(s);
6656     }
6657 }
6658 
6659 /* Floating-point data-processing (3 source) - single precision */
6660 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6661                                   int rd, int rn, int rm, int ra)
6662 {
6663     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6664     TCGv_i32 tcg_res = tcg_temp_new_i32();
6665     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6666 
6667     tcg_op1 = read_fp_sreg(s, rn);
6668     tcg_op2 = read_fp_sreg(s, rm);
6669     tcg_op3 = read_fp_sreg(s, ra);
6670 
6671     /* These are fused multiply-add, and must be done as one
6672      * floating point operation with no rounding between the
6673      * multiplication and addition steps.
6674      * NB that doing the negations here as separate steps is
6675      * correct : an input NaN should come out with its sign bit
6676      * flipped if it is a negated-input.
6677      */
6678     if (o1 == true) {
6679         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6680     }
6681 
6682     if (o0 != o1) {
6683         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6684     }
6685 
6686     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6687 
6688     write_fp_sreg(s, rd, tcg_res);
6689 }
6690 
6691 /* Floating-point data-processing (3 source) - double precision */
6692 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6693                                   int rd, int rn, int rm, int ra)
6694 {
6695     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6696     TCGv_i64 tcg_res = tcg_temp_new_i64();
6697     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6698 
6699     tcg_op1 = read_fp_dreg(s, rn);
6700     tcg_op2 = read_fp_dreg(s, rm);
6701     tcg_op3 = read_fp_dreg(s, ra);
6702 
6703     /* These are fused multiply-add, and must be done as one
6704      * floating point operation with no rounding between the
6705      * multiplication and addition steps.
6706      * NB that doing the negations here as separate steps is
6707      * correct : an input NaN should come out with its sign bit
6708      * flipped if it is a negated-input.
6709      */
6710     if (o1 == true) {
6711         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6712     }
6713 
6714     if (o0 != o1) {
6715         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6716     }
6717 
6718     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6719 
6720     write_fp_dreg(s, rd, tcg_res);
6721 }
6722 
6723 /* Floating-point data-processing (3 source) - half precision */
6724 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6725                                 int rd, int rn, int rm, int ra)
6726 {
6727     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6728     TCGv_i32 tcg_res = tcg_temp_new_i32();
6729     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6730 
6731     tcg_op1 = read_fp_hreg(s, rn);
6732     tcg_op2 = read_fp_hreg(s, rm);
6733     tcg_op3 = read_fp_hreg(s, ra);
6734 
6735     /* These are fused multiply-add, and must be done as one
6736      * floating point operation with no rounding between the
6737      * multiplication and addition steps.
6738      * NB that doing the negations here as separate steps is
6739      * correct : an input NaN should come out with its sign bit
6740      * flipped if it is a negated-input.
6741      */
6742     if (o1 == true) {
6743         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6744     }
6745 
6746     if (o0 != o1) {
6747         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6748     }
6749 
6750     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6751 
6752     write_fp_sreg(s, rd, tcg_res);
6753 }
6754 
6755 /* Floating point data-processing (3 source)
6756  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6757  * +---+---+---+-----------+------+----+------+----+------+------+------+
6758  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6759  * +---+---+---+-----------+------+----+------+----+------+------+------+
6760  */
6761 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6762 {
6763     int mos = extract32(insn, 29, 3);
6764     int type = extract32(insn, 22, 2);
6765     int rd = extract32(insn, 0, 5);
6766     int rn = extract32(insn, 5, 5);
6767     int ra = extract32(insn, 10, 5);
6768     int rm = extract32(insn, 16, 5);
6769     bool o0 = extract32(insn, 15, 1);
6770     bool o1 = extract32(insn, 21, 1);
6771 
6772     if (mos) {
6773         unallocated_encoding(s);
6774         return;
6775     }
6776 
6777     switch (type) {
6778     case 0:
6779         if (!fp_access_check(s)) {
6780             return;
6781         }
6782         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6783         break;
6784     case 1:
6785         if (!fp_access_check(s)) {
6786             return;
6787         }
6788         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6789         break;
6790     case 3:
6791         if (!dc_isar_feature(aa64_fp16, s)) {
6792             unallocated_encoding(s);
6793             return;
6794         }
6795         if (!fp_access_check(s)) {
6796             return;
6797         }
6798         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6799         break;
6800     default:
6801         unallocated_encoding(s);
6802     }
6803 }
6804 
6805 /* Floating point immediate
6806  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6807  * +---+---+---+-----------+------+---+------------+-------+------+------+
6808  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6809  * +---+---+---+-----------+------+---+------------+-------+------+------+
6810  */
6811 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6812 {
6813     int rd = extract32(insn, 0, 5);
6814     int imm5 = extract32(insn, 5, 5);
6815     int imm8 = extract32(insn, 13, 8);
6816     int type = extract32(insn, 22, 2);
6817     int mos = extract32(insn, 29, 3);
6818     uint64_t imm;
6819     MemOp sz;
6820 
6821     if (mos || imm5) {
6822         unallocated_encoding(s);
6823         return;
6824     }
6825 
6826     switch (type) {
6827     case 0:
6828         sz = MO_32;
6829         break;
6830     case 1:
6831         sz = MO_64;
6832         break;
6833     case 3:
6834         sz = MO_16;
6835         if (dc_isar_feature(aa64_fp16, s)) {
6836             break;
6837         }
6838         /* fallthru */
6839     default:
6840         unallocated_encoding(s);
6841         return;
6842     }
6843 
6844     if (!fp_access_check(s)) {
6845         return;
6846     }
6847 
6848     imm = vfp_expand_imm(sz, imm8);
6849     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6850 }
6851 
6852 /* Handle floating point <=> fixed point conversions. Note that we can
6853  * also deal with fp <=> integer conversions as a special case (scale == 64)
6854  * OPTME: consider handling that special case specially or at least skipping
6855  * the call to scalbn in the helpers for zero shifts.
6856  */
6857 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6858                            bool itof, int rmode, int scale, int sf, int type)
6859 {
6860     bool is_signed = !(opcode & 1);
6861     TCGv_ptr tcg_fpstatus;
6862     TCGv_i32 tcg_shift, tcg_single;
6863     TCGv_i64 tcg_double;
6864 
6865     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6866 
6867     tcg_shift = tcg_constant_i32(64 - scale);
6868 
6869     if (itof) {
6870         TCGv_i64 tcg_int = cpu_reg(s, rn);
6871         if (!sf) {
6872             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6873 
6874             if (is_signed) {
6875                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6876             } else {
6877                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6878             }
6879 
6880             tcg_int = tcg_extend;
6881         }
6882 
6883         switch (type) {
6884         case 1: /* float64 */
6885             tcg_double = tcg_temp_new_i64();
6886             if (is_signed) {
6887                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6888                                      tcg_shift, tcg_fpstatus);
6889             } else {
6890                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6891                                      tcg_shift, tcg_fpstatus);
6892             }
6893             write_fp_dreg(s, rd, tcg_double);
6894             break;
6895 
6896         case 0: /* float32 */
6897             tcg_single = tcg_temp_new_i32();
6898             if (is_signed) {
6899                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6900                                      tcg_shift, tcg_fpstatus);
6901             } else {
6902                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6903                                      tcg_shift, tcg_fpstatus);
6904             }
6905             write_fp_sreg(s, rd, tcg_single);
6906             break;
6907 
6908         case 3: /* float16 */
6909             tcg_single = tcg_temp_new_i32();
6910             if (is_signed) {
6911                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6912                                      tcg_shift, tcg_fpstatus);
6913             } else {
6914                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6915                                      tcg_shift, tcg_fpstatus);
6916             }
6917             write_fp_sreg(s, rd, tcg_single);
6918             break;
6919 
6920         default:
6921             g_assert_not_reached();
6922         }
6923     } else {
6924         TCGv_i64 tcg_int = cpu_reg(s, rd);
6925         TCGv_i32 tcg_rmode;
6926 
6927         if (extract32(opcode, 2, 1)) {
6928             /* There are too many rounding modes to all fit into rmode,
6929              * so FCVTA[US] is a special case.
6930              */
6931             rmode = FPROUNDING_TIEAWAY;
6932         }
6933 
6934         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6935 
6936         switch (type) {
6937         case 1: /* float64 */
6938             tcg_double = read_fp_dreg(s, rn);
6939             if (is_signed) {
6940                 if (!sf) {
6941                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6942                                          tcg_shift, tcg_fpstatus);
6943                 } else {
6944                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6945                                          tcg_shift, tcg_fpstatus);
6946                 }
6947             } else {
6948                 if (!sf) {
6949                     gen_helper_vfp_tould(tcg_int, tcg_double,
6950                                          tcg_shift, tcg_fpstatus);
6951                 } else {
6952                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6953                                          tcg_shift, tcg_fpstatus);
6954                 }
6955             }
6956             if (!sf) {
6957                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6958             }
6959             break;
6960 
6961         case 0: /* float32 */
6962             tcg_single = read_fp_sreg(s, rn);
6963             if (sf) {
6964                 if (is_signed) {
6965                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6966                                          tcg_shift, tcg_fpstatus);
6967                 } else {
6968                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6969                                          tcg_shift, tcg_fpstatus);
6970                 }
6971             } else {
6972                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6973                 if (is_signed) {
6974                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6975                                          tcg_shift, tcg_fpstatus);
6976                 } else {
6977                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6978                                          tcg_shift, tcg_fpstatus);
6979                 }
6980                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6981             }
6982             break;
6983 
6984         case 3: /* float16 */
6985             tcg_single = read_fp_sreg(s, rn);
6986             if (sf) {
6987                 if (is_signed) {
6988                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6989                                          tcg_shift, tcg_fpstatus);
6990                 } else {
6991                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6992                                          tcg_shift, tcg_fpstatus);
6993                 }
6994             } else {
6995                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6996                 if (is_signed) {
6997                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6998                                          tcg_shift, tcg_fpstatus);
6999                 } else {
7000                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7001                                          tcg_shift, tcg_fpstatus);
7002                 }
7003                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7004             }
7005             break;
7006 
7007         default:
7008             g_assert_not_reached();
7009         }
7010 
7011         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7012     }
7013 }
7014 
7015 /* Floating point <-> fixed point conversions
7016  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7017  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7018  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7019  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7020  */
7021 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7022 {
7023     int rd = extract32(insn, 0, 5);
7024     int rn = extract32(insn, 5, 5);
7025     int scale = extract32(insn, 10, 6);
7026     int opcode = extract32(insn, 16, 3);
7027     int rmode = extract32(insn, 19, 2);
7028     int type = extract32(insn, 22, 2);
7029     bool sbit = extract32(insn, 29, 1);
7030     bool sf = extract32(insn, 31, 1);
7031     bool itof;
7032 
7033     if (sbit || (!sf && scale < 32)) {
7034         unallocated_encoding(s);
7035         return;
7036     }
7037 
7038     switch (type) {
7039     case 0: /* float32 */
7040     case 1: /* float64 */
7041         break;
7042     case 3: /* float16 */
7043         if (dc_isar_feature(aa64_fp16, s)) {
7044             break;
7045         }
7046         /* fallthru */
7047     default:
7048         unallocated_encoding(s);
7049         return;
7050     }
7051 
7052     switch ((rmode << 3) | opcode) {
7053     case 0x2: /* SCVTF */
7054     case 0x3: /* UCVTF */
7055         itof = true;
7056         break;
7057     case 0x18: /* FCVTZS */
7058     case 0x19: /* FCVTZU */
7059         itof = false;
7060         break;
7061     default:
7062         unallocated_encoding(s);
7063         return;
7064     }
7065 
7066     if (!fp_access_check(s)) {
7067         return;
7068     }
7069 
7070     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7071 }
7072 
7073 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7074 {
7075     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7076      * without conversion.
7077      */
7078 
7079     if (itof) {
7080         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7081         TCGv_i64 tmp;
7082 
7083         switch (type) {
7084         case 0:
7085             /* 32 bit */
7086             tmp = tcg_temp_new_i64();
7087             tcg_gen_ext32u_i64(tmp, tcg_rn);
7088             write_fp_dreg(s, rd, tmp);
7089             break;
7090         case 1:
7091             /* 64 bit */
7092             write_fp_dreg(s, rd, tcg_rn);
7093             break;
7094         case 2:
7095             /* 64 bit to top half. */
7096             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7097             clear_vec_high(s, true, rd);
7098             break;
7099         case 3:
7100             /* 16 bit */
7101             tmp = tcg_temp_new_i64();
7102             tcg_gen_ext16u_i64(tmp, tcg_rn);
7103             write_fp_dreg(s, rd, tmp);
7104             break;
7105         default:
7106             g_assert_not_reached();
7107         }
7108     } else {
7109         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7110 
7111         switch (type) {
7112         case 0:
7113             /* 32 bit */
7114             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7115             break;
7116         case 1:
7117             /* 64 bit */
7118             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7119             break;
7120         case 2:
7121             /* 64 bits from top half */
7122             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7123             break;
7124         case 3:
7125             /* 16 bit */
7126             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7127             break;
7128         default:
7129             g_assert_not_reached();
7130         }
7131     }
7132 }
7133 
7134 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7135 {
7136     TCGv_i64 t = read_fp_dreg(s, rn);
7137     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7138 
7139     gen_helper_fjcvtzs(t, t, fpstatus);
7140 
7141     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7142     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7143     tcg_gen_movi_i32(cpu_CF, 0);
7144     tcg_gen_movi_i32(cpu_NF, 0);
7145     tcg_gen_movi_i32(cpu_VF, 0);
7146 }
7147 
7148 /* Floating point <-> integer conversions
7149  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7150  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7151  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7152  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7153  */
7154 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7155 {
7156     int rd = extract32(insn, 0, 5);
7157     int rn = extract32(insn, 5, 5);
7158     int opcode = extract32(insn, 16, 3);
7159     int rmode = extract32(insn, 19, 2);
7160     int type = extract32(insn, 22, 2);
7161     bool sbit = extract32(insn, 29, 1);
7162     bool sf = extract32(insn, 31, 1);
7163     bool itof = false;
7164 
7165     if (sbit) {
7166         goto do_unallocated;
7167     }
7168 
7169     switch (opcode) {
7170     case 2: /* SCVTF */
7171     case 3: /* UCVTF */
7172         itof = true;
7173         /* fallthru */
7174     case 4: /* FCVTAS */
7175     case 5: /* FCVTAU */
7176         if (rmode != 0) {
7177             goto do_unallocated;
7178         }
7179         /* fallthru */
7180     case 0: /* FCVT[NPMZ]S */
7181     case 1: /* FCVT[NPMZ]U */
7182         switch (type) {
7183         case 0: /* float32 */
7184         case 1: /* float64 */
7185             break;
7186         case 3: /* float16 */
7187             if (!dc_isar_feature(aa64_fp16, s)) {
7188                 goto do_unallocated;
7189             }
7190             break;
7191         default:
7192             goto do_unallocated;
7193         }
7194         if (!fp_access_check(s)) {
7195             return;
7196         }
7197         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7198         break;
7199 
7200     default:
7201         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7202         case 0b01100110: /* FMOV half <-> 32-bit int */
7203         case 0b01100111:
7204         case 0b11100110: /* FMOV half <-> 64-bit int */
7205         case 0b11100111:
7206             if (!dc_isar_feature(aa64_fp16, s)) {
7207                 goto do_unallocated;
7208             }
7209             /* fallthru */
7210         case 0b00000110: /* FMOV 32-bit */
7211         case 0b00000111:
7212         case 0b10100110: /* FMOV 64-bit */
7213         case 0b10100111:
7214         case 0b11001110: /* FMOV top half of 128-bit */
7215         case 0b11001111:
7216             if (!fp_access_check(s)) {
7217                 return;
7218             }
7219             itof = opcode & 1;
7220             handle_fmov(s, rd, rn, type, itof);
7221             break;
7222 
7223         case 0b00111110: /* FJCVTZS */
7224             if (!dc_isar_feature(aa64_jscvt, s)) {
7225                 goto do_unallocated;
7226             } else if (fp_access_check(s)) {
7227                 handle_fjcvtzs(s, rd, rn);
7228             }
7229             break;
7230 
7231         default:
7232         do_unallocated:
7233             unallocated_encoding(s);
7234             return;
7235         }
7236         break;
7237     }
7238 }
7239 
7240 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7241  *   31  30  29 28     25 24                          0
7242  * +---+---+---+---------+-----------------------------+
7243  * |   | 0 |   | 1 1 1 1 |                             |
7244  * +---+---+---+---------+-----------------------------+
7245  */
7246 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7247 {
7248     if (extract32(insn, 24, 1)) {
7249         /* Floating point data-processing (3 source) */
7250         disas_fp_3src(s, insn);
7251     } else if (extract32(insn, 21, 1) == 0) {
7252         /* Floating point to fixed point conversions */
7253         disas_fp_fixed_conv(s, insn);
7254     } else {
7255         switch (extract32(insn, 10, 2)) {
7256         case 1:
7257             /* Floating point conditional compare */
7258             disas_fp_ccomp(s, insn);
7259             break;
7260         case 2:
7261             /* Floating point data-processing (2 source) */
7262             disas_fp_2src(s, insn);
7263             break;
7264         case 3:
7265             /* Floating point conditional select */
7266             disas_fp_csel(s, insn);
7267             break;
7268         case 0:
7269             switch (ctz32(extract32(insn, 12, 4))) {
7270             case 0: /* [15:12] == xxx1 */
7271                 /* Floating point immediate */
7272                 disas_fp_imm(s, insn);
7273                 break;
7274             case 1: /* [15:12] == xx10 */
7275                 /* Floating point compare */
7276                 disas_fp_compare(s, insn);
7277                 break;
7278             case 2: /* [15:12] == x100 */
7279                 /* Floating point data-processing (1 source) */
7280                 disas_fp_1src(s, insn);
7281                 break;
7282             case 3: /* [15:12] == 1000 */
7283                 unallocated_encoding(s);
7284                 break;
7285             default: /* [15:12] == 0000 */
7286                 /* Floating point <-> integer conversions */
7287                 disas_fp_int_conv(s, insn);
7288                 break;
7289             }
7290             break;
7291         }
7292     }
7293 }
7294 
7295 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7296                      int pos)
7297 {
7298     /* Extract 64 bits from the middle of two concatenated 64 bit
7299      * vector register slices left:right. The extracted bits start
7300      * at 'pos' bits into the right (least significant) side.
7301      * We return the result in tcg_right, and guarantee not to
7302      * trash tcg_left.
7303      */
7304     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7305     assert(pos > 0 && pos < 64);
7306 
7307     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7308     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7309     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7310 }
7311 
7312 /* EXT
7313  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7314  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7315  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7316  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7317  */
7318 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7319 {
7320     int is_q = extract32(insn, 30, 1);
7321     int op2 = extract32(insn, 22, 2);
7322     int imm4 = extract32(insn, 11, 4);
7323     int rm = extract32(insn, 16, 5);
7324     int rn = extract32(insn, 5, 5);
7325     int rd = extract32(insn, 0, 5);
7326     int pos = imm4 << 3;
7327     TCGv_i64 tcg_resl, tcg_resh;
7328 
7329     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7330         unallocated_encoding(s);
7331         return;
7332     }
7333 
7334     if (!fp_access_check(s)) {
7335         return;
7336     }
7337 
7338     tcg_resh = tcg_temp_new_i64();
7339     tcg_resl = tcg_temp_new_i64();
7340 
7341     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7342      * either extracting 128 bits from a 128:128 concatenation, or
7343      * extracting 64 bits from a 64:64 concatenation.
7344      */
7345     if (!is_q) {
7346         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7347         if (pos != 0) {
7348             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7349             do_ext64(s, tcg_resh, tcg_resl, pos);
7350         }
7351     } else {
7352         TCGv_i64 tcg_hh;
7353         typedef struct {
7354             int reg;
7355             int elt;
7356         } EltPosns;
7357         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7358         EltPosns *elt = eltposns;
7359 
7360         if (pos >= 64) {
7361             elt++;
7362             pos -= 64;
7363         }
7364 
7365         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7366         elt++;
7367         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7368         elt++;
7369         if (pos != 0) {
7370             do_ext64(s, tcg_resh, tcg_resl, pos);
7371             tcg_hh = tcg_temp_new_i64();
7372             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7373             do_ext64(s, tcg_hh, tcg_resh, pos);
7374         }
7375     }
7376 
7377     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7378     if (is_q) {
7379         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7380     }
7381     clear_vec_high(s, is_q, rd);
7382 }
7383 
7384 /* TBL/TBX
7385  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7386  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7387  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7388  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7389  */
7390 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7391 {
7392     int op2 = extract32(insn, 22, 2);
7393     int is_q = extract32(insn, 30, 1);
7394     int rm = extract32(insn, 16, 5);
7395     int rn = extract32(insn, 5, 5);
7396     int rd = extract32(insn, 0, 5);
7397     int is_tbx = extract32(insn, 12, 1);
7398     int len = (extract32(insn, 13, 2) + 1) * 16;
7399 
7400     if (op2 != 0) {
7401         unallocated_encoding(s);
7402         return;
7403     }
7404 
7405     if (!fp_access_check(s)) {
7406         return;
7407     }
7408 
7409     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7410                        vec_full_reg_offset(s, rm), cpu_env,
7411                        is_q ? 16 : 8, vec_full_reg_size(s),
7412                        (len << 6) | (is_tbx << 5) | rn,
7413                        gen_helper_simd_tblx);
7414 }
7415 
7416 /* ZIP/UZP/TRN
7417  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7418  * +---+---+-------------+------+---+------+---+------------------+------+
7419  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7420  * +---+---+-------------+------+---+------+---+------------------+------+
7421  */
7422 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7423 {
7424     int rd = extract32(insn, 0, 5);
7425     int rn = extract32(insn, 5, 5);
7426     int rm = extract32(insn, 16, 5);
7427     int size = extract32(insn, 22, 2);
7428     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7429      * bit 2 indicates 1 vs 2 variant of the insn.
7430      */
7431     int opcode = extract32(insn, 12, 2);
7432     bool part = extract32(insn, 14, 1);
7433     bool is_q = extract32(insn, 30, 1);
7434     int esize = 8 << size;
7435     int i;
7436     int datasize = is_q ? 128 : 64;
7437     int elements = datasize / esize;
7438     TCGv_i64 tcg_res[2], tcg_ele;
7439 
7440     if (opcode == 0 || (size == 3 && !is_q)) {
7441         unallocated_encoding(s);
7442         return;
7443     }
7444 
7445     if (!fp_access_check(s)) {
7446         return;
7447     }
7448 
7449     tcg_res[0] = tcg_temp_new_i64();
7450     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7451     tcg_ele = tcg_temp_new_i64();
7452 
7453     for (i = 0; i < elements; i++) {
7454         int o, w;
7455 
7456         switch (opcode) {
7457         case 1: /* UZP1/2 */
7458         {
7459             int midpoint = elements / 2;
7460             if (i < midpoint) {
7461                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7462             } else {
7463                 read_vec_element(s, tcg_ele, rm,
7464                                  2 * (i - midpoint) + part, size);
7465             }
7466             break;
7467         }
7468         case 2: /* TRN1/2 */
7469             if (i & 1) {
7470                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7471             } else {
7472                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7473             }
7474             break;
7475         case 3: /* ZIP1/2 */
7476         {
7477             int base = part * elements / 2;
7478             if (i & 1) {
7479                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7480             } else {
7481                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7482             }
7483             break;
7484         }
7485         default:
7486             g_assert_not_reached();
7487         }
7488 
7489         w = (i * esize) / 64;
7490         o = (i * esize) % 64;
7491         if (o == 0) {
7492             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7493         } else {
7494             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7495             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7496         }
7497     }
7498 
7499     for (i = 0; i <= is_q; ++i) {
7500         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7501     }
7502     clear_vec_high(s, is_q, rd);
7503 }
7504 
7505 /*
7506  * do_reduction_op helper
7507  *
7508  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7509  * important for correct NaN propagation that we do these
7510  * operations in exactly the order specified by the pseudocode.
7511  *
7512  * This is a recursive function, TCG temps should be freed by the
7513  * calling function once it is done with the values.
7514  */
7515 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7516                                 int esize, int size, int vmap, TCGv_ptr fpst)
7517 {
7518     if (esize == size) {
7519         int element;
7520         MemOp msize = esize == 16 ? MO_16 : MO_32;
7521         TCGv_i32 tcg_elem;
7522 
7523         /* We should have one register left here */
7524         assert(ctpop8(vmap) == 1);
7525         element = ctz32(vmap);
7526         assert(element < 8);
7527 
7528         tcg_elem = tcg_temp_new_i32();
7529         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7530         return tcg_elem;
7531     } else {
7532         int bits = size / 2;
7533         int shift = ctpop8(vmap) / 2;
7534         int vmap_lo = (vmap >> shift) & vmap;
7535         int vmap_hi = (vmap & ~vmap_lo);
7536         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7537 
7538         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7539         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7540         tcg_res = tcg_temp_new_i32();
7541 
7542         switch (fpopcode) {
7543         case 0x0c: /* fmaxnmv half-precision */
7544             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7545             break;
7546         case 0x0f: /* fmaxv half-precision */
7547             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7548             break;
7549         case 0x1c: /* fminnmv half-precision */
7550             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7551             break;
7552         case 0x1f: /* fminv half-precision */
7553             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7554             break;
7555         case 0x2c: /* fmaxnmv */
7556             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7557             break;
7558         case 0x2f: /* fmaxv */
7559             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7560             break;
7561         case 0x3c: /* fminnmv */
7562             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7563             break;
7564         case 0x3f: /* fminv */
7565             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7566             break;
7567         default:
7568             g_assert_not_reached();
7569         }
7570         return tcg_res;
7571     }
7572 }
7573 
7574 /* AdvSIMD across lanes
7575  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7576  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7577  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7578  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7579  */
7580 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7581 {
7582     int rd = extract32(insn, 0, 5);
7583     int rn = extract32(insn, 5, 5);
7584     int size = extract32(insn, 22, 2);
7585     int opcode = extract32(insn, 12, 5);
7586     bool is_q = extract32(insn, 30, 1);
7587     bool is_u = extract32(insn, 29, 1);
7588     bool is_fp = false;
7589     bool is_min = false;
7590     int esize;
7591     int elements;
7592     int i;
7593     TCGv_i64 tcg_res, tcg_elt;
7594 
7595     switch (opcode) {
7596     case 0x1b: /* ADDV */
7597         if (is_u) {
7598             unallocated_encoding(s);
7599             return;
7600         }
7601         /* fall through */
7602     case 0x3: /* SADDLV, UADDLV */
7603     case 0xa: /* SMAXV, UMAXV */
7604     case 0x1a: /* SMINV, UMINV */
7605         if (size == 3 || (size == 2 && !is_q)) {
7606             unallocated_encoding(s);
7607             return;
7608         }
7609         break;
7610     case 0xc: /* FMAXNMV, FMINNMV */
7611     case 0xf: /* FMAXV, FMINV */
7612         /* Bit 1 of size field encodes min vs max and the actual size
7613          * depends on the encoding of the U bit. If not set (and FP16
7614          * enabled) then we do half-precision float instead of single
7615          * precision.
7616          */
7617         is_min = extract32(size, 1, 1);
7618         is_fp = true;
7619         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7620             size = 1;
7621         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7622             unallocated_encoding(s);
7623             return;
7624         } else {
7625             size = 2;
7626         }
7627         break;
7628     default:
7629         unallocated_encoding(s);
7630         return;
7631     }
7632 
7633     if (!fp_access_check(s)) {
7634         return;
7635     }
7636 
7637     esize = 8 << size;
7638     elements = (is_q ? 128 : 64) / esize;
7639 
7640     tcg_res = tcg_temp_new_i64();
7641     tcg_elt = tcg_temp_new_i64();
7642 
7643     /* These instructions operate across all lanes of a vector
7644      * to produce a single result. We can guarantee that a 64
7645      * bit intermediate is sufficient:
7646      *  + for [US]ADDLV the maximum element size is 32 bits, and
7647      *    the result type is 64 bits
7648      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7649      *    same as the element size, which is 32 bits at most
7650      * For the integer operations we can choose to work at 64
7651      * or 32 bits and truncate at the end; for simplicity
7652      * we use 64 bits always. The floating point
7653      * ops do require 32 bit intermediates, though.
7654      */
7655     if (!is_fp) {
7656         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7657 
7658         for (i = 1; i < elements; i++) {
7659             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7660 
7661             switch (opcode) {
7662             case 0x03: /* SADDLV / UADDLV */
7663             case 0x1b: /* ADDV */
7664                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7665                 break;
7666             case 0x0a: /* SMAXV / UMAXV */
7667                 if (is_u) {
7668                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7669                 } else {
7670                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7671                 }
7672                 break;
7673             case 0x1a: /* SMINV / UMINV */
7674                 if (is_u) {
7675                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7676                 } else {
7677                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7678                 }
7679                 break;
7680             default:
7681                 g_assert_not_reached();
7682             }
7683 
7684         }
7685     } else {
7686         /* Floating point vector reduction ops which work across 32
7687          * bit (single) or 16 bit (half-precision) intermediates.
7688          * Note that correct NaN propagation requires that we do these
7689          * operations in exactly the order specified by the pseudocode.
7690          */
7691         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7692         int fpopcode = opcode | is_min << 4 | is_u << 5;
7693         int vmap = (1 << elements) - 1;
7694         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7695                                              (is_q ? 128 : 64), vmap, fpst);
7696         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7697     }
7698 
7699     /* Now truncate the result to the width required for the final output */
7700     if (opcode == 0x03) {
7701         /* SADDLV, UADDLV: result is 2*esize */
7702         size++;
7703     }
7704 
7705     switch (size) {
7706     case 0:
7707         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7708         break;
7709     case 1:
7710         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7711         break;
7712     case 2:
7713         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7714         break;
7715     case 3:
7716         break;
7717     default:
7718         g_assert_not_reached();
7719     }
7720 
7721     write_fp_dreg(s, rd, tcg_res);
7722 }
7723 
7724 /* DUP (Element, Vector)
7725  *
7726  *  31  30   29              21 20    16 15        10  9    5 4    0
7727  * +---+---+-------------------+--------+-------------+------+------+
7728  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7729  * +---+---+-------------------+--------+-------------+------+------+
7730  *
7731  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7732  */
7733 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7734                              int imm5)
7735 {
7736     int size = ctz32(imm5);
7737     int index;
7738 
7739     if (size > 3 || (size == 3 && !is_q)) {
7740         unallocated_encoding(s);
7741         return;
7742     }
7743 
7744     if (!fp_access_check(s)) {
7745         return;
7746     }
7747 
7748     index = imm5 >> (size + 1);
7749     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7750                          vec_reg_offset(s, rn, index, size),
7751                          is_q ? 16 : 8, vec_full_reg_size(s));
7752 }
7753 
7754 /* DUP (element, scalar)
7755  *  31                   21 20    16 15        10  9    5 4    0
7756  * +-----------------------+--------+-------------+------+------+
7757  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7758  * +-----------------------+--------+-------------+------+------+
7759  */
7760 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7761                               int imm5)
7762 {
7763     int size = ctz32(imm5);
7764     int index;
7765     TCGv_i64 tmp;
7766 
7767     if (size > 3) {
7768         unallocated_encoding(s);
7769         return;
7770     }
7771 
7772     if (!fp_access_check(s)) {
7773         return;
7774     }
7775 
7776     index = imm5 >> (size + 1);
7777 
7778     /* This instruction just extracts the specified element and
7779      * zero-extends it into the bottom of the destination register.
7780      */
7781     tmp = tcg_temp_new_i64();
7782     read_vec_element(s, tmp, rn, index, size);
7783     write_fp_dreg(s, rd, tmp);
7784 }
7785 
7786 /* DUP (General)
7787  *
7788  *  31  30   29              21 20    16 15        10  9    5 4    0
7789  * +---+---+-------------------+--------+-------------+------+------+
7790  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7791  * +---+---+-------------------+--------+-------------+------+------+
7792  *
7793  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7794  */
7795 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7796                              int imm5)
7797 {
7798     int size = ctz32(imm5);
7799     uint32_t dofs, oprsz, maxsz;
7800 
7801     if (size > 3 || ((size == 3) && !is_q)) {
7802         unallocated_encoding(s);
7803         return;
7804     }
7805 
7806     if (!fp_access_check(s)) {
7807         return;
7808     }
7809 
7810     dofs = vec_full_reg_offset(s, rd);
7811     oprsz = is_q ? 16 : 8;
7812     maxsz = vec_full_reg_size(s);
7813 
7814     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7815 }
7816 
7817 /* INS (Element)
7818  *
7819  *  31                   21 20    16 15  14    11  10 9    5 4    0
7820  * +-----------------------+--------+------------+---+------+------+
7821  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7822  * +-----------------------+--------+------------+---+------+------+
7823  *
7824  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7825  * index: encoded in imm5<4:size+1>
7826  */
7827 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7828                              int imm4, int imm5)
7829 {
7830     int size = ctz32(imm5);
7831     int src_index, dst_index;
7832     TCGv_i64 tmp;
7833 
7834     if (size > 3) {
7835         unallocated_encoding(s);
7836         return;
7837     }
7838 
7839     if (!fp_access_check(s)) {
7840         return;
7841     }
7842 
7843     dst_index = extract32(imm5, 1+size, 5);
7844     src_index = extract32(imm4, size, 4);
7845 
7846     tmp = tcg_temp_new_i64();
7847 
7848     read_vec_element(s, tmp, rn, src_index, size);
7849     write_vec_element(s, tmp, rd, dst_index, size);
7850 
7851     /* INS is considered a 128-bit write for SVE. */
7852     clear_vec_high(s, true, rd);
7853 }
7854 
7855 
7856 /* INS (General)
7857  *
7858  *  31                   21 20    16 15        10  9    5 4    0
7859  * +-----------------------+--------+-------------+------+------+
7860  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7861  * +-----------------------+--------+-------------+------+------+
7862  *
7863  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7864  * index: encoded in imm5<4:size+1>
7865  */
7866 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7867 {
7868     int size = ctz32(imm5);
7869     int idx;
7870 
7871     if (size > 3) {
7872         unallocated_encoding(s);
7873         return;
7874     }
7875 
7876     if (!fp_access_check(s)) {
7877         return;
7878     }
7879 
7880     idx = extract32(imm5, 1 + size, 4 - size);
7881     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7882 
7883     /* INS is considered a 128-bit write for SVE. */
7884     clear_vec_high(s, true, rd);
7885 }
7886 
7887 /*
7888  * UMOV (General)
7889  * SMOV (General)
7890  *
7891  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7892  * +---+---+-------------------+--------+-------------+------+------+
7893  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7894  * +---+---+-------------------+--------+-------------+------+------+
7895  *
7896  * U: unsigned when set
7897  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7898  */
7899 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7900                                   int rn, int rd, int imm5)
7901 {
7902     int size = ctz32(imm5);
7903     int element;
7904     TCGv_i64 tcg_rd;
7905 
7906     /* Check for UnallocatedEncodings */
7907     if (is_signed) {
7908         if (size > 2 || (size == 2 && !is_q)) {
7909             unallocated_encoding(s);
7910             return;
7911         }
7912     } else {
7913         if (size > 3
7914             || (size < 3 && is_q)
7915             || (size == 3 && !is_q)) {
7916             unallocated_encoding(s);
7917             return;
7918         }
7919     }
7920 
7921     if (!fp_access_check(s)) {
7922         return;
7923     }
7924 
7925     element = extract32(imm5, 1+size, 4);
7926 
7927     tcg_rd = cpu_reg(s, rd);
7928     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7929     if (is_signed && !is_q) {
7930         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7931     }
7932 }
7933 
7934 /* AdvSIMD copy
7935  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7936  * +---+---+----+-----------------+------+---+------+---+------+------+
7937  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7938  * +---+---+----+-----------------+------+---+------+---+------+------+
7939  */
7940 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7941 {
7942     int rd = extract32(insn, 0, 5);
7943     int rn = extract32(insn, 5, 5);
7944     int imm4 = extract32(insn, 11, 4);
7945     int op = extract32(insn, 29, 1);
7946     int is_q = extract32(insn, 30, 1);
7947     int imm5 = extract32(insn, 16, 5);
7948 
7949     if (op) {
7950         if (is_q) {
7951             /* INS (element) */
7952             handle_simd_inse(s, rd, rn, imm4, imm5);
7953         } else {
7954             unallocated_encoding(s);
7955         }
7956     } else {
7957         switch (imm4) {
7958         case 0:
7959             /* DUP (element - vector) */
7960             handle_simd_dupe(s, is_q, rd, rn, imm5);
7961             break;
7962         case 1:
7963             /* DUP (general) */
7964             handle_simd_dupg(s, is_q, rd, rn, imm5);
7965             break;
7966         case 3:
7967             if (is_q) {
7968                 /* INS (general) */
7969                 handle_simd_insg(s, rd, rn, imm5);
7970             } else {
7971                 unallocated_encoding(s);
7972             }
7973             break;
7974         case 5:
7975         case 7:
7976             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7977             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7978             break;
7979         default:
7980             unallocated_encoding(s);
7981             break;
7982         }
7983     }
7984 }
7985 
7986 /* AdvSIMD modified immediate
7987  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7988  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7989  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7990  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7991  *
7992  * There are a number of operations that can be carried out here:
7993  *   MOVI - move (shifted) imm into register
7994  *   MVNI - move inverted (shifted) imm into register
7995  *   ORR  - bitwise OR of (shifted) imm with register
7996  *   BIC  - bitwise clear of (shifted) imm with register
7997  * With ARMv8.2 we also have:
7998  *   FMOV half-precision
7999  */
8000 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8001 {
8002     int rd = extract32(insn, 0, 5);
8003     int cmode = extract32(insn, 12, 4);
8004     int o2 = extract32(insn, 11, 1);
8005     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8006     bool is_neg = extract32(insn, 29, 1);
8007     bool is_q = extract32(insn, 30, 1);
8008     uint64_t imm = 0;
8009 
8010     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8011         /* Check for FMOV (vector, immediate) - half-precision */
8012         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8013             unallocated_encoding(s);
8014             return;
8015         }
8016     }
8017 
8018     if (!fp_access_check(s)) {
8019         return;
8020     }
8021 
8022     if (cmode == 15 && o2 && !is_neg) {
8023         /* FMOV (vector, immediate) - half-precision */
8024         imm = vfp_expand_imm(MO_16, abcdefgh);
8025         /* now duplicate across the lanes */
8026         imm = dup_const(MO_16, imm);
8027     } else {
8028         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8029     }
8030 
8031     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8032         /* MOVI or MVNI, with MVNI negation handled above.  */
8033         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8034                              vec_full_reg_size(s), imm);
8035     } else {
8036         /* ORR or BIC, with BIC negation to AND handled above.  */
8037         if (is_neg) {
8038             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8039         } else {
8040             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8041         }
8042     }
8043 }
8044 
8045 /* AdvSIMD scalar copy
8046  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8047  * +-----+----+-----------------+------+---+------+---+------+------+
8048  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8049  * +-----+----+-----------------+------+---+------+---+------+------+
8050  */
8051 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8052 {
8053     int rd = extract32(insn, 0, 5);
8054     int rn = extract32(insn, 5, 5);
8055     int imm4 = extract32(insn, 11, 4);
8056     int imm5 = extract32(insn, 16, 5);
8057     int op = extract32(insn, 29, 1);
8058 
8059     if (op != 0 || imm4 != 0) {
8060         unallocated_encoding(s);
8061         return;
8062     }
8063 
8064     /* DUP (element, scalar) */
8065     handle_simd_dupes(s, rd, rn, imm5);
8066 }
8067 
8068 /* AdvSIMD scalar pairwise
8069  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8070  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8071  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8072  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8073  */
8074 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8075 {
8076     int u = extract32(insn, 29, 1);
8077     int size = extract32(insn, 22, 2);
8078     int opcode = extract32(insn, 12, 5);
8079     int rn = extract32(insn, 5, 5);
8080     int rd = extract32(insn, 0, 5);
8081     TCGv_ptr fpst;
8082 
8083     /* For some ops (the FP ones), size[1] is part of the encoding.
8084      * For ADDP strictly it is not but size[1] is always 1 for valid
8085      * encodings.
8086      */
8087     opcode |= (extract32(size, 1, 1) << 5);
8088 
8089     switch (opcode) {
8090     case 0x3b: /* ADDP */
8091         if (u || size != 3) {
8092             unallocated_encoding(s);
8093             return;
8094         }
8095         if (!fp_access_check(s)) {
8096             return;
8097         }
8098 
8099         fpst = NULL;
8100         break;
8101     case 0xc: /* FMAXNMP */
8102     case 0xd: /* FADDP */
8103     case 0xf: /* FMAXP */
8104     case 0x2c: /* FMINNMP */
8105     case 0x2f: /* FMINP */
8106         /* FP op, size[0] is 32 or 64 bit*/
8107         if (!u) {
8108             if (!dc_isar_feature(aa64_fp16, s)) {
8109                 unallocated_encoding(s);
8110                 return;
8111             } else {
8112                 size = MO_16;
8113             }
8114         } else {
8115             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8116         }
8117 
8118         if (!fp_access_check(s)) {
8119             return;
8120         }
8121 
8122         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8123         break;
8124     default:
8125         unallocated_encoding(s);
8126         return;
8127     }
8128 
8129     if (size == MO_64) {
8130         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8131         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8132         TCGv_i64 tcg_res = tcg_temp_new_i64();
8133 
8134         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8135         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8136 
8137         switch (opcode) {
8138         case 0x3b: /* ADDP */
8139             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8140             break;
8141         case 0xc: /* FMAXNMP */
8142             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8143             break;
8144         case 0xd: /* FADDP */
8145             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8146             break;
8147         case 0xf: /* FMAXP */
8148             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8149             break;
8150         case 0x2c: /* FMINNMP */
8151             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8152             break;
8153         case 0x2f: /* FMINP */
8154             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8155             break;
8156         default:
8157             g_assert_not_reached();
8158         }
8159 
8160         write_fp_dreg(s, rd, tcg_res);
8161     } else {
8162         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8163         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8164         TCGv_i32 tcg_res = tcg_temp_new_i32();
8165 
8166         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8167         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8168 
8169         if (size == MO_16) {
8170             switch (opcode) {
8171             case 0xc: /* FMAXNMP */
8172                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8173                 break;
8174             case 0xd: /* FADDP */
8175                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8176                 break;
8177             case 0xf: /* FMAXP */
8178                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8179                 break;
8180             case 0x2c: /* FMINNMP */
8181                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8182                 break;
8183             case 0x2f: /* FMINP */
8184                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8185                 break;
8186             default:
8187                 g_assert_not_reached();
8188             }
8189         } else {
8190             switch (opcode) {
8191             case 0xc: /* FMAXNMP */
8192                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8193                 break;
8194             case 0xd: /* FADDP */
8195                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8196                 break;
8197             case 0xf: /* FMAXP */
8198                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8199                 break;
8200             case 0x2c: /* FMINNMP */
8201                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8202                 break;
8203             case 0x2f: /* FMINP */
8204                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8205                 break;
8206             default:
8207                 g_assert_not_reached();
8208             }
8209         }
8210 
8211         write_fp_sreg(s, rd, tcg_res);
8212     }
8213 }
8214 
8215 /*
8216  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8217  *
8218  * This code is handles the common shifting code and is used by both
8219  * the vector and scalar code.
8220  */
8221 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8222                                     TCGv_i64 tcg_rnd, bool accumulate,
8223                                     bool is_u, int size, int shift)
8224 {
8225     bool extended_result = false;
8226     bool round = tcg_rnd != NULL;
8227     int ext_lshift = 0;
8228     TCGv_i64 tcg_src_hi;
8229 
8230     if (round && size == 3) {
8231         extended_result = true;
8232         ext_lshift = 64 - shift;
8233         tcg_src_hi = tcg_temp_new_i64();
8234     } else if (shift == 64) {
8235         if (!accumulate && is_u) {
8236             /* result is zero */
8237             tcg_gen_movi_i64(tcg_res, 0);
8238             return;
8239         }
8240     }
8241 
8242     /* Deal with the rounding step */
8243     if (round) {
8244         if (extended_result) {
8245             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8246             if (!is_u) {
8247                 /* take care of sign extending tcg_res */
8248                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8249                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8250                                  tcg_src, tcg_src_hi,
8251                                  tcg_rnd, tcg_zero);
8252             } else {
8253                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8254                                  tcg_src, tcg_zero,
8255                                  tcg_rnd, tcg_zero);
8256             }
8257         } else {
8258             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8259         }
8260     }
8261 
8262     /* Now do the shift right */
8263     if (round && extended_result) {
8264         /* extended case, >64 bit precision required */
8265         if (ext_lshift == 0) {
8266             /* special case, only high bits matter */
8267             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8268         } else {
8269             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8270             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8271             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8272         }
8273     } else {
8274         if (is_u) {
8275             if (shift == 64) {
8276                 /* essentially shifting in 64 zeros */
8277                 tcg_gen_movi_i64(tcg_src, 0);
8278             } else {
8279                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8280             }
8281         } else {
8282             if (shift == 64) {
8283                 /* effectively extending the sign-bit */
8284                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8285             } else {
8286                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8287             }
8288         }
8289     }
8290 
8291     if (accumulate) {
8292         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8293     } else {
8294         tcg_gen_mov_i64(tcg_res, tcg_src);
8295     }
8296 }
8297 
8298 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8299 static void handle_scalar_simd_shri(DisasContext *s,
8300                                     bool is_u, int immh, int immb,
8301                                     int opcode, int rn, int rd)
8302 {
8303     const int size = 3;
8304     int immhb = immh << 3 | immb;
8305     int shift = 2 * (8 << size) - immhb;
8306     bool accumulate = false;
8307     bool round = false;
8308     bool insert = false;
8309     TCGv_i64 tcg_rn;
8310     TCGv_i64 tcg_rd;
8311     TCGv_i64 tcg_round;
8312 
8313     if (!extract32(immh, 3, 1)) {
8314         unallocated_encoding(s);
8315         return;
8316     }
8317 
8318     if (!fp_access_check(s)) {
8319         return;
8320     }
8321 
8322     switch (opcode) {
8323     case 0x02: /* SSRA / USRA (accumulate) */
8324         accumulate = true;
8325         break;
8326     case 0x04: /* SRSHR / URSHR (rounding) */
8327         round = true;
8328         break;
8329     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8330         accumulate = round = true;
8331         break;
8332     case 0x08: /* SRI */
8333         insert = true;
8334         break;
8335     }
8336 
8337     if (round) {
8338         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8339     } else {
8340         tcg_round = NULL;
8341     }
8342 
8343     tcg_rn = read_fp_dreg(s, rn);
8344     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8345 
8346     if (insert) {
8347         /* shift count same as element size is valid but does nothing;
8348          * special case to avoid potential shift by 64.
8349          */
8350         int esize = 8 << size;
8351         if (shift != esize) {
8352             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8353             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8354         }
8355     } else {
8356         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8357                                 accumulate, is_u, size, shift);
8358     }
8359 
8360     write_fp_dreg(s, rd, tcg_rd);
8361 }
8362 
8363 /* SHL/SLI - Scalar shift left */
8364 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8365                                     int immh, int immb, int opcode,
8366                                     int rn, int rd)
8367 {
8368     int size = 32 - clz32(immh) - 1;
8369     int immhb = immh << 3 | immb;
8370     int shift = immhb - (8 << size);
8371     TCGv_i64 tcg_rn;
8372     TCGv_i64 tcg_rd;
8373 
8374     if (!extract32(immh, 3, 1)) {
8375         unallocated_encoding(s);
8376         return;
8377     }
8378 
8379     if (!fp_access_check(s)) {
8380         return;
8381     }
8382 
8383     tcg_rn = read_fp_dreg(s, rn);
8384     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8385 
8386     if (insert) {
8387         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8388     } else {
8389         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8390     }
8391 
8392     write_fp_dreg(s, rd, tcg_rd);
8393 }
8394 
8395 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8396  * (signed/unsigned) narrowing */
8397 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8398                                    bool is_u_shift, bool is_u_narrow,
8399                                    int immh, int immb, int opcode,
8400                                    int rn, int rd)
8401 {
8402     int immhb = immh << 3 | immb;
8403     int size = 32 - clz32(immh) - 1;
8404     int esize = 8 << size;
8405     int shift = (2 * esize) - immhb;
8406     int elements = is_scalar ? 1 : (64 / esize);
8407     bool round = extract32(opcode, 0, 1);
8408     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8409     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8410     TCGv_i32 tcg_rd_narrowed;
8411     TCGv_i64 tcg_final;
8412 
8413     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8414         { gen_helper_neon_narrow_sat_s8,
8415           gen_helper_neon_unarrow_sat8 },
8416         { gen_helper_neon_narrow_sat_s16,
8417           gen_helper_neon_unarrow_sat16 },
8418         { gen_helper_neon_narrow_sat_s32,
8419           gen_helper_neon_unarrow_sat32 },
8420         { NULL, NULL },
8421     };
8422     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8423         gen_helper_neon_narrow_sat_u8,
8424         gen_helper_neon_narrow_sat_u16,
8425         gen_helper_neon_narrow_sat_u32,
8426         NULL
8427     };
8428     NeonGenNarrowEnvFn *narrowfn;
8429 
8430     int i;
8431 
8432     assert(size < 4);
8433 
8434     if (extract32(immh, 3, 1)) {
8435         unallocated_encoding(s);
8436         return;
8437     }
8438 
8439     if (!fp_access_check(s)) {
8440         return;
8441     }
8442 
8443     if (is_u_shift) {
8444         narrowfn = unsigned_narrow_fns[size];
8445     } else {
8446         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8447     }
8448 
8449     tcg_rn = tcg_temp_new_i64();
8450     tcg_rd = tcg_temp_new_i64();
8451     tcg_rd_narrowed = tcg_temp_new_i32();
8452     tcg_final = tcg_temp_new_i64();
8453 
8454     if (round) {
8455         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8456     } else {
8457         tcg_round = NULL;
8458     }
8459 
8460     for (i = 0; i < elements; i++) {
8461         read_vec_element(s, tcg_rn, rn, i, ldop);
8462         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8463                                 false, is_u_shift, size+1, shift);
8464         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8465         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8466         if (i == 0) {
8467             tcg_gen_mov_i64(tcg_final, tcg_rd);
8468         } else {
8469             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8470         }
8471     }
8472 
8473     if (!is_q) {
8474         write_vec_element(s, tcg_final, rd, 0, MO_64);
8475     } else {
8476         write_vec_element(s, tcg_final, rd, 1, MO_64);
8477     }
8478     clear_vec_high(s, is_q, rd);
8479 }
8480 
8481 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8482 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8483                              bool src_unsigned, bool dst_unsigned,
8484                              int immh, int immb, int rn, int rd)
8485 {
8486     int immhb = immh << 3 | immb;
8487     int size = 32 - clz32(immh) - 1;
8488     int shift = immhb - (8 << size);
8489     int pass;
8490 
8491     assert(immh != 0);
8492     assert(!(scalar && is_q));
8493 
8494     if (!scalar) {
8495         if (!is_q && extract32(immh, 3, 1)) {
8496             unallocated_encoding(s);
8497             return;
8498         }
8499 
8500         /* Since we use the variable-shift helpers we must
8501          * replicate the shift count into each element of
8502          * the tcg_shift value.
8503          */
8504         switch (size) {
8505         case 0:
8506             shift |= shift << 8;
8507             /* fall through */
8508         case 1:
8509             shift |= shift << 16;
8510             break;
8511         case 2:
8512         case 3:
8513             break;
8514         default:
8515             g_assert_not_reached();
8516         }
8517     }
8518 
8519     if (!fp_access_check(s)) {
8520         return;
8521     }
8522 
8523     if (size == 3) {
8524         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8525         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8526             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8527             { NULL, gen_helper_neon_qshl_u64 },
8528         };
8529         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8530         int maxpass = is_q ? 2 : 1;
8531 
8532         for (pass = 0; pass < maxpass; pass++) {
8533             TCGv_i64 tcg_op = tcg_temp_new_i64();
8534 
8535             read_vec_element(s, tcg_op, rn, pass, MO_64);
8536             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8537             write_vec_element(s, tcg_op, rd, pass, MO_64);
8538         }
8539         clear_vec_high(s, is_q, rd);
8540     } else {
8541         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8542         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8543             {
8544                 { gen_helper_neon_qshl_s8,
8545                   gen_helper_neon_qshl_s16,
8546                   gen_helper_neon_qshl_s32 },
8547                 { gen_helper_neon_qshlu_s8,
8548                   gen_helper_neon_qshlu_s16,
8549                   gen_helper_neon_qshlu_s32 }
8550             }, {
8551                 { NULL, NULL, NULL },
8552                 { gen_helper_neon_qshl_u8,
8553                   gen_helper_neon_qshl_u16,
8554                   gen_helper_neon_qshl_u32 }
8555             }
8556         };
8557         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8558         MemOp memop = scalar ? size : MO_32;
8559         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8560 
8561         for (pass = 0; pass < maxpass; pass++) {
8562             TCGv_i32 tcg_op = tcg_temp_new_i32();
8563 
8564             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8565             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8566             if (scalar) {
8567                 switch (size) {
8568                 case 0:
8569                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8570                     break;
8571                 case 1:
8572                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8573                     break;
8574                 case 2:
8575                     break;
8576                 default:
8577                     g_assert_not_reached();
8578                 }
8579                 write_fp_sreg(s, rd, tcg_op);
8580             } else {
8581                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8582             }
8583         }
8584 
8585         if (!scalar) {
8586             clear_vec_high(s, is_q, rd);
8587         }
8588     }
8589 }
8590 
8591 /* Common vector code for handling integer to FP conversion */
8592 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8593                                    int elements, int is_signed,
8594                                    int fracbits, int size)
8595 {
8596     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8597     TCGv_i32 tcg_shift = NULL;
8598 
8599     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8600     int pass;
8601 
8602     if (fracbits || size == MO_64) {
8603         tcg_shift = tcg_constant_i32(fracbits);
8604     }
8605 
8606     if (size == MO_64) {
8607         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8608         TCGv_i64 tcg_double = tcg_temp_new_i64();
8609 
8610         for (pass = 0; pass < elements; pass++) {
8611             read_vec_element(s, tcg_int64, rn, pass, mop);
8612 
8613             if (is_signed) {
8614                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8615                                      tcg_shift, tcg_fpst);
8616             } else {
8617                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8618                                      tcg_shift, tcg_fpst);
8619             }
8620             if (elements == 1) {
8621                 write_fp_dreg(s, rd, tcg_double);
8622             } else {
8623                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8624             }
8625         }
8626     } else {
8627         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8628         TCGv_i32 tcg_float = tcg_temp_new_i32();
8629 
8630         for (pass = 0; pass < elements; pass++) {
8631             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8632 
8633             switch (size) {
8634             case MO_32:
8635                 if (fracbits) {
8636                     if (is_signed) {
8637                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8638                                              tcg_shift, tcg_fpst);
8639                     } else {
8640                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8641                                              tcg_shift, tcg_fpst);
8642                     }
8643                 } else {
8644                     if (is_signed) {
8645                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8646                     } else {
8647                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8648                     }
8649                 }
8650                 break;
8651             case MO_16:
8652                 if (fracbits) {
8653                     if (is_signed) {
8654                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8655                                              tcg_shift, tcg_fpst);
8656                     } else {
8657                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8658                                              tcg_shift, tcg_fpst);
8659                     }
8660                 } else {
8661                     if (is_signed) {
8662                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8663                     } else {
8664                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8665                     }
8666                 }
8667                 break;
8668             default:
8669                 g_assert_not_reached();
8670             }
8671 
8672             if (elements == 1) {
8673                 write_fp_sreg(s, rd, tcg_float);
8674             } else {
8675                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8676             }
8677         }
8678     }
8679 
8680     clear_vec_high(s, elements << size == 16, rd);
8681 }
8682 
8683 /* UCVTF/SCVTF - Integer to FP conversion */
8684 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8685                                          bool is_q, bool is_u,
8686                                          int immh, int immb, int opcode,
8687                                          int rn, int rd)
8688 {
8689     int size, elements, fracbits;
8690     int immhb = immh << 3 | immb;
8691 
8692     if (immh & 8) {
8693         size = MO_64;
8694         if (!is_scalar && !is_q) {
8695             unallocated_encoding(s);
8696             return;
8697         }
8698     } else if (immh & 4) {
8699         size = MO_32;
8700     } else if (immh & 2) {
8701         size = MO_16;
8702         if (!dc_isar_feature(aa64_fp16, s)) {
8703             unallocated_encoding(s);
8704             return;
8705         }
8706     } else {
8707         /* immh == 0 would be a failure of the decode logic */
8708         g_assert(immh == 1);
8709         unallocated_encoding(s);
8710         return;
8711     }
8712 
8713     if (is_scalar) {
8714         elements = 1;
8715     } else {
8716         elements = (8 << is_q) >> size;
8717     }
8718     fracbits = (16 << size) - immhb;
8719 
8720     if (!fp_access_check(s)) {
8721         return;
8722     }
8723 
8724     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8725 }
8726 
8727 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8728 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8729                                          bool is_q, bool is_u,
8730                                          int immh, int immb, int rn, int rd)
8731 {
8732     int immhb = immh << 3 | immb;
8733     int pass, size, fracbits;
8734     TCGv_ptr tcg_fpstatus;
8735     TCGv_i32 tcg_rmode, tcg_shift;
8736 
8737     if (immh & 0x8) {
8738         size = MO_64;
8739         if (!is_scalar && !is_q) {
8740             unallocated_encoding(s);
8741             return;
8742         }
8743     } else if (immh & 0x4) {
8744         size = MO_32;
8745     } else if (immh & 0x2) {
8746         size = MO_16;
8747         if (!dc_isar_feature(aa64_fp16, s)) {
8748             unallocated_encoding(s);
8749             return;
8750         }
8751     } else {
8752         /* Should have split out AdvSIMD modified immediate earlier.  */
8753         assert(immh == 1);
8754         unallocated_encoding(s);
8755         return;
8756     }
8757 
8758     if (!fp_access_check(s)) {
8759         return;
8760     }
8761 
8762     assert(!(is_scalar && is_q));
8763 
8764     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8765     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8766     fracbits = (16 << size) - immhb;
8767     tcg_shift = tcg_constant_i32(fracbits);
8768 
8769     if (size == MO_64) {
8770         int maxpass = is_scalar ? 1 : 2;
8771 
8772         for (pass = 0; pass < maxpass; pass++) {
8773             TCGv_i64 tcg_op = tcg_temp_new_i64();
8774 
8775             read_vec_element(s, tcg_op, rn, pass, MO_64);
8776             if (is_u) {
8777                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8778             } else {
8779                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8780             }
8781             write_vec_element(s, tcg_op, rd, pass, MO_64);
8782         }
8783         clear_vec_high(s, is_q, rd);
8784     } else {
8785         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8786         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8787 
8788         switch (size) {
8789         case MO_16:
8790             if (is_u) {
8791                 fn = gen_helper_vfp_touhh;
8792             } else {
8793                 fn = gen_helper_vfp_toshh;
8794             }
8795             break;
8796         case MO_32:
8797             if (is_u) {
8798                 fn = gen_helper_vfp_touls;
8799             } else {
8800                 fn = gen_helper_vfp_tosls;
8801             }
8802             break;
8803         default:
8804             g_assert_not_reached();
8805         }
8806 
8807         for (pass = 0; pass < maxpass; pass++) {
8808             TCGv_i32 tcg_op = tcg_temp_new_i32();
8809 
8810             read_vec_element_i32(s, tcg_op, rn, pass, size);
8811             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8812             if (is_scalar) {
8813                 write_fp_sreg(s, rd, tcg_op);
8814             } else {
8815                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8816             }
8817         }
8818         if (!is_scalar) {
8819             clear_vec_high(s, is_q, rd);
8820         }
8821     }
8822 
8823     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8824 }
8825 
8826 /* AdvSIMD scalar shift by immediate
8827  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8828  * +-----+---+-------------+------+------+--------+---+------+------+
8829  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8830  * +-----+---+-------------+------+------+--------+---+------+------+
8831  *
8832  * This is the scalar version so it works on a fixed sized registers
8833  */
8834 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8835 {
8836     int rd = extract32(insn, 0, 5);
8837     int rn = extract32(insn, 5, 5);
8838     int opcode = extract32(insn, 11, 5);
8839     int immb = extract32(insn, 16, 3);
8840     int immh = extract32(insn, 19, 4);
8841     bool is_u = extract32(insn, 29, 1);
8842 
8843     if (immh == 0) {
8844         unallocated_encoding(s);
8845         return;
8846     }
8847 
8848     switch (opcode) {
8849     case 0x08: /* SRI */
8850         if (!is_u) {
8851             unallocated_encoding(s);
8852             return;
8853         }
8854         /* fall through */
8855     case 0x00: /* SSHR / USHR */
8856     case 0x02: /* SSRA / USRA */
8857     case 0x04: /* SRSHR / URSHR */
8858     case 0x06: /* SRSRA / URSRA */
8859         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8860         break;
8861     case 0x0a: /* SHL / SLI */
8862         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8863         break;
8864     case 0x1c: /* SCVTF, UCVTF */
8865         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8866                                      opcode, rn, rd);
8867         break;
8868     case 0x10: /* SQSHRUN, SQSHRUN2 */
8869     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8870         if (!is_u) {
8871             unallocated_encoding(s);
8872             return;
8873         }
8874         handle_vec_simd_sqshrn(s, true, false, false, true,
8875                                immh, immb, opcode, rn, rd);
8876         break;
8877     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8878     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8879         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8880                                immh, immb, opcode, rn, rd);
8881         break;
8882     case 0xc: /* SQSHLU */
8883         if (!is_u) {
8884             unallocated_encoding(s);
8885             return;
8886         }
8887         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8888         break;
8889     case 0xe: /* SQSHL, UQSHL */
8890         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8891         break;
8892     case 0x1f: /* FCVTZS, FCVTZU */
8893         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8894         break;
8895     default:
8896         unallocated_encoding(s);
8897         break;
8898     }
8899 }
8900 
8901 /* AdvSIMD scalar three different
8902  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8903  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8904  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8905  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8906  */
8907 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8908 {
8909     bool is_u = extract32(insn, 29, 1);
8910     int size = extract32(insn, 22, 2);
8911     int opcode = extract32(insn, 12, 4);
8912     int rm = extract32(insn, 16, 5);
8913     int rn = extract32(insn, 5, 5);
8914     int rd = extract32(insn, 0, 5);
8915 
8916     if (is_u) {
8917         unallocated_encoding(s);
8918         return;
8919     }
8920 
8921     switch (opcode) {
8922     case 0x9: /* SQDMLAL, SQDMLAL2 */
8923     case 0xb: /* SQDMLSL, SQDMLSL2 */
8924     case 0xd: /* SQDMULL, SQDMULL2 */
8925         if (size == 0 || size == 3) {
8926             unallocated_encoding(s);
8927             return;
8928         }
8929         break;
8930     default:
8931         unallocated_encoding(s);
8932         return;
8933     }
8934 
8935     if (!fp_access_check(s)) {
8936         return;
8937     }
8938 
8939     if (size == 2) {
8940         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8941         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8942         TCGv_i64 tcg_res = tcg_temp_new_i64();
8943 
8944         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8945         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8946 
8947         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8948         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8949 
8950         switch (opcode) {
8951         case 0xd: /* SQDMULL, SQDMULL2 */
8952             break;
8953         case 0xb: /* SQDMLSL, SQDMLSL2 */
8954             tcg_gen_neg_i64(tcg_res, tcg_res);
8955             /* fall through */
8956         case 0x9: /* SQDMLAL, SQDMLAL2 */
8957             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8958             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8959                                               tcg_res, tcg_op1);
8960             break;
8961         default:
8962             g_assert_not_reached();
8963         }
8964 
8965         write_fp_dreg(s, rd, tcg_res);
8966     } else {
8967         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8968         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8969         TCGv_i64 tcg_res = tcg_temp_new_i64();
8970 
8971         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8972         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8973 
8974         switch (opcode) {
8975         case 0xd: /* SQDMULL, SQDMULL2 */
8976             break;
8977         case 0xb: /* SQDMLSL, SQDMLSL2 */
8978             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8979             /* fall through */
8980         case 0x9: /* SQDMLAL, SQDMLAL2 */
8981         {
8982             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8983             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8984             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8985                                               tcg_res, tcg_op3);
8986             break;
8987         }
8988         default:
8989             g_assert_not_reached();
8990         }
8991 
8992         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8993         write_fp_dreg(s, rd, tcg_res);
8994     }
8995 }
8996 
8997 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8998                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8999 {
9000     /* Handle 64x64->64 opcodes which are shared between the scalar
9001      * and vector 3-same groups. We cover every opcode where size == 3
9002      * is valid in either the three-reg-same (integer, not pairwise)
9003      * or scalar-three-reg-same groups.
9004      */
9005     TCGCond cond;
9006 
9007     switch (opcode) {
9008     case 0x1: /* SQADD */
9009         if (u) {
9010             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9011         } else {
9012             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9013         }
9014         break;
9015     case 0x5: /* SQSUB */
9016         if (u) {
9017             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9018         } else {
9019             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9020         }
9021         break;
9022     case 0x6: /* CMGT, CMHI */
9023         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9024          * We implement this using setcond (test) and then negating.
9025          */
9026         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9027     do_cmop:
9028         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9029         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9030         break;
9031     case 0x7: /* CMGE, CMHS */
9032         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9033         goto do_cmop;
9034     case 0x11: /* CMTST, CMEQ */
9035         if (u) {
9036             cond = TCG_COND_EQ;
9037             goto do_cmop;
9038         }
9039         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9040         break;
9041     case 0x8: /* SSHL, USHL */
9042         if (u) {
9043             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9044         } else {
9045             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9046         }
9047         break;
9048     case 0x9: /* SQSHL, UQSHL */
9049         if (u) {
9050             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9051         } else {
9052             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9053         }
9054         break;
9055     case 0xa: /* SRSHL, URSHL */
9056         if (u) {
9057             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9058         } else {
9059             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9060         }
9061         break;
9062     case 0xb: /* SQRSHL, UQRSHL */
9063         if (u) {
9064             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9065         } else {
9066             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9067         }
9068         break;
9069     case 0x10: /* ADD, SUB */
9070         if (u) {
9071             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9072         } else {
9073             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9074         }
9075         break;
9076     default:
9077         g_assert_not_reached();
9078     }
9079 }
9080 
9081 /* Handle the 3-same-operands float operations; shared by the scalar
9082  * and vector encodings. The caller must filter out any encodings
9083  * not allocated for the encoding it is dealing with.
9084  */
9085 static void handle_3same_float(DisasContext *s, int size, int elements,
9086                                int fpopcode, int rd, int rn, int rm)
9087 {
9088     int pass;
9089     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9090 
9091     for (pass = 0; pass < elements; pass++) {
9092         if (size) {
9093             /* Double */
9094             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9095             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9096             TCGv_i64 tcg_res = tcg_temp_new_i64();
9097 
9098             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9099             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9100 
9101             switch (fpopcode) {
9102             case 0x39: /* FMLS */
9103                 /* As usual for ARM, separate negation for fused multiply-add */
9104                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9105                 /* fall through */
9106             case 0x19: /* FMLA */
9107                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9108                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9109                                        tcg_res, fpst);
9110                 break;
9111             case 0x18: /* FMAXNM */
9112                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9113                 break;
9114             case 0x1a: /* FADD */
9115                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9116                 break;
9117             case 0x1b: /* FMULX */
9118                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9119                 break;
9120             case 0x1c: /* FCMEQ */
9121                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9122                 break;
9123             case 0x1e: /* FMAX */
9124                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9125                 break;
9126             case 0x1f: /* FRECPS */
9127                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9128                 break;
9129             case 0x38: /* FMINNM */
9130                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9131                 break;
9132             case 0x3a: /* FSUB */
9133                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9134                 break;
9135             case 0x3e: /* FMIN */
9136                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9137                 break;
9138             case 0x3f: /* FRSQRTS */
9139                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9140                 break;
9141             case 0x5b: /* FMUL */
9142                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9143                 break;
9144             case 0x5c: /* FCMGE */
9145                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9146                 break;
9147             case 0x5d: /* FACGE */
9148                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9149                 break;
9150             case 0x5f: /* FDIV */
9151                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9152                 break;
9153             case 0x7a: /* FABD */
9154                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9155                 gen_helper_vfp_absd(tcg_res, tcg_res);
9156                 break;
9157             case 0x7c: /* FCMGT */
9158                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9159                 break;
9160             case 0x7d: /* FACGT */
9161                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9162                 break;
9163             default:
9164                 g_assert_not_reached();
9165             }
9166 
9167             write_vec_element(s, tcg_res, rd, pass, MO_64);
9168         } else {
9169             /* Single */
9170             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9171             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9172             TCGv_i32 tcg_res = tcg_temp_new_i32();
9173 
9174             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9175             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9176 
9177             switch (fpopcode) {
9178             case 0x39: /* FMLS */
9179                 /* As usual for ARM, separate negation for fused multiply-add */
9180                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9181                 /* fall through */
9182             case 0x19: /* FMLA */
9183                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9184                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9185                                        tcg_res, fpst);
9186                 break;
9187             case 0x1a: /* FADD */
9188                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9189                 break;
9190             case 0x1b: /* FMULX */
9191                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9192                 break;
9193             case 0x1c: /* FCMEQ */
9194                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9195                 break;
9196             case 0x1e: /* FMAX */
9197                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9198                 break;
9199             case 0x1f: /* FRECPS */
9200                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9201                 break;
9202             case 0x18: /* FMAXNM */
9203                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9204                 break;
9205             case 0x38: /* FMINNM */
9206                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9207                 break;
9208             case 0x3a: /* FSUB */
9209                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9210                 break;
9211             case 0x3e: /* FMIN */
9212                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9213                 break;
9214             case 0x3f: /* FRSQRTS */
9215                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9216                 break;
9217             case 0x5b: /* FMUL */
9218                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9219                 break;
9220             case 0x5c: /* FCMGE */
9221                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9222                 break;
9223             case 0x5d: /* FACGE */
9224                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9225                 break;
9226             case 0x5f: /* FDIV */
9227                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9228                 break;
9229             case 0x7a: /* FABD */
9230                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9231                 gen_helper_vfp_abss(tcg_res, tcg_res);
9232                 break;
9233             case 0x7c: /* FCMGT */
9234                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9235                 break;
9236             case 0x7d: /* FACGT */
9237                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9238                 break;
9239             default:
9240                 g_assert_not_reached();
9241             }
9242 
9243             if (elements == 1) {
9244                 /* scalar single so clear high part */
9245                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9246 
9247                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9248                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9249             } else {
9250                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9251             }
9252         }
9253     }
9254 
9255     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9256 }
9257 
9258 /* AdvSIMD scalar three same
9259  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9260  * +-----+---+-----------+------+---+------+--------+---+------+------+
9261  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9262  * +-----+---+-----------+------+---+------+--------+---+------+------+
9263  */
9264 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9265 {
9266     int rd = extract32(insn, 0, 5);
9267     int rn = extract32(insn, 5, 5);
9268     int opcode = extract32(insn, 11, 5);
9269     int rm = extract32(insn, 16, 5);
9270     int size = extract32(insn, 22, 2);
9271     bool u = extract32(insn, 29, 1);
9272     TCGv_i64 tcg_rd;
9273 
9274     if (opcode >= 0x18) {
9275         /* Floating point: U, size[1] and opcode indicate operation */
9276         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9277         switch (fpopcode) {
9278         case 0x1b: /* FMULX */
9279         case 0x1f: /* FRECPS */
9280         case 0x3f: /* FRSQRTS */
9281         case 0x5d: /* FACGE */
9282         case 0x7d: /* FACGT */
9283         case 0x1c: /* FCMEQ */
9284         case 0x5c: /* FCMGE */
9285         case 0x7c: /* FCMGT */
9286         case 0x7a: /* FABD */
9287             break;
9288         default:
9289             unallocated_encoding(s);
9290             return;
9291         }
9292 
9293         if (!fp_access_check(s)) {
9294             return;
9295         }
9296 
9297         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9298         return;
9299     }
9300 
9301     switch (opcode) {
9302     case 0x1: /* SQADD, UQADD */
9303     case 0x5: /* SQSUB, UQSUB */
9304     case 0x9: /* SQSHL, UQSHL */
9305     case 0xb: /* SQRSHL, UQRSHL */
9306         break;
9307     case 0x8: /* SSHL, USHL */
9308     case 0xa: /* SRSHL, URSHL */
9309     case 0x6: /* CMGT, CMHI */
9310     case 0x7: /* CMGE, CMHS */
9311     case 0x11: /* CMTST, CMEQ */
9312     case 0x10: /* ADD, SUB (vector) */
9313         if (size != 3) {
9314             unallocated_encoding(s);
9315             return;
9316         }
9317         break;
9318     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9319         if (size != 1 && size != 2) {
9320             unallocated_encoding(s);
9321             return;
9322         }
9323         break;
9324     default:
9325         unallocated_encoding(s);
9326         return;
9327     }
9328 
9329     if (!fp_access_check(s)) {
9330         return;
9331     }
9332 
9333     tcg_rd = tcg_temp_new_i64();
9334 
9335     if (size == 3) {
9336         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9337         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9338 
9339         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9340     } else {
9341         /* Do a single operation on the lowest element in the vector.
9342          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9343          * no side effects for all these operations.
9344          * OPTME: special-purpose helpers would avoid doing some
9345          * unnecessary work in the helper for the 8 and 16 bit cases.
9346          */
9347         NeonGenTwoOpEnvFn *genenvfn;
9348         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9349         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9350         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9351 
9352         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9353         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9354 
9355         switch (opcode) {
9356         case 0x1: /* SQADD, UQADD */
9357         {
9358             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9359                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9360                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9361                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9362             };
9363             genenvfn = fns[size][u];
9364             break;
9365         }
9366         case 0x5: /* SQSUB, UQSUB */
9367         {
9368             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9369                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9370                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9371                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9372             };
9373             genenvfn = fns[size][u];
9374             break;
9375         }
9376         case 0x9: /* SQSHL, UQSHL */
9377         {
9378             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9379                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9380                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9381                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9382             };
9383             genenvfn = fns[size][u];
9384             break;
9385         }
9386         case 0xb: /* SQRSHL, UQRSHL */
9387         {
9388             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9389                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9390                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9391                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9392             };
9393             genenvfn = fns[size][u];
9394             break;
9395         }
9396         case 0x16: /* SQDMULH, SQRDMULH */
9397         {
9398             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9399                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9400                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9401             };
9402             assert(size == 1 || size == 2);
9403             genenvfn = fns[size - 1][u];
9404             break;
9405         }
9406         default:
9407             g_assert_not_reached();
9408         }
9409 
9410         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9411         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9412     }
9413 
9414     write_fp_dreg(s, rd, tcg_rd);
9415 }
9416 
9417 /* AdvSIMD scalar three same FP16
9418  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9419  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9420  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9421  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9422  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9423  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9424  */
9425 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9426                                                   uint32_t insn)
9427 {
9428     int rd = extract32(insn, 0, 5);
9429     int rn = extract32(insn, 5, 5);
9430     int opcode = extract32(insn, 11, 3);
9431     int rm = extract32(insn, 16, 5);
9432     bool u = extract32(insn, 29, 1);
9433     bool a = extract32(insn, 23, 1);
9434     int fpopcode = opcode | (a << 3) |  (u << 4);
9435     TCGv_ptr fpst;
9436     TCGv_i32 tcg_op1;
9437     TCGv_i32 tcg_op2;
9438     TCGv_i32 tcg_res;
9439 
9440     switch (fpopcode) {
9441     case 0x03: /* FMULX */
9442     case 0x04: /* FCMEQ (reg) */
9443     case 0x07: /* FRECPS */
9444     case 0x0f: /* FRSQRTS */
9445     case 0x14: /* FCMGE (reg) */
9446     case 0x15: /* FACGE */
9447     case 0x1a: /* FABD */
9448     case 0x1c: /* FCMGT (reg) */
9449     case 0x1d: /* FACGT */
9450         break;
9451     default:
9452         unallocated_encoding(s);
9453         return;
9454     }
9455 
9456     if (!dc_isar_feature(aa64_fp16, s)) {
9457         unallocated_encoding(s);
9458     }
9459 
9460     if (!fp_access_check(s)) {
9461         return;
9462     }
9463 
9464     fpst = fpstatus_ptr(FPST_FPCR_F16);
9465 
9466     tcg_op1 = read_fp_hreg(s, rn);
9467     tcg_op2 = read_fp_hreg(s, rm);
9468     tcg_res = tcg_temp_new_i32();
9469 
9470     switch (fpopcode) {
9471     case 0x03: /* FMULX */
9472         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9473         break;
9474     case 0x04: /* FCMEQ (reg) */
9475         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9476         break;
9477     case 0x07: /* FRECPS */
9478         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9479         break;
9480     case 0x0f: /* FRSQRTS */
9481         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9482         break;
9483     case 0x14: /* FCMGE (reg) */
9484         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9485         break;
9486     case 0x15: /* FACGE */
9487         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9488         break;
9489     case 0x1a: /* FABD */
9490         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9491         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9492         break;
9493     case 0x1c: /* FCMGT (reg) */
9494         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9495         break;
9496     case 0x1d: /* FACGT */
9497         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9498         break;
9499     default:
9500         g_assert_not_reached();
9501     }
9502 
9503     write_fp_sreg(s, rd, tcg_res);
9504 }
9505 
9506 /* AdvSIMD scalar three same extra
9507  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9508  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9509  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9510  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9511  */
9512 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9513                                                    uint32_t insn)
9514 {
9515     int rd = extract32(insn, 0, 5);
9516     int rn = extract32(insn, 5, 5);
9517     int opcode = extract32(insn, 11, 4);
9518     int rm = extract32(insn, 16, 5);
9519     int size = extract32(insn, 22, 2);
9520     bool u = extract32(insn, 29, 1);
9521     TCGv_i32 ele1, ele2, ele3;
9522     TCGv_i64 res;
9523     bool feature;
9524 
9525     switch (u * 16 + opcode) {
9526     case 0x10: /* SQRDMLAH (vector) */
9527     case 0x11: /* SQRDMLSH (vector) */
9528         if (size != 1 && size != 2) {
9529             unallocated_encoding(s);
9530             return;
9531         }
9532         feature = dc_isar_feature(aa64_rdm, s);
9533         break;
9534     default:
9535         unallocated_encoding(s);
9536         return;
9537     }
9538     if (!feature) {
9539         unallocated_encoding(s);
9540         return;
9541     }
9542     if (!fp_access_check(s)) {
9543         return;
9544     }
9545 
9546     /* Do a single operation on the lowest element in the vector.
9547      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9548      * with no side effects for all these operations.
9549      * OPTME: special-purpose helpers would avoid doing some
9550      * unnecessary work in the helper for the 16 bit cases.
9551      */
9552     ele1 = tcg_temp_new_i32();
9553     ele2 = tcg_temp_new_i32();
9554     ele3 = tcg_temp_new_i32();
9555 
9556     read_vec_element_i32(s, ele1, rn, 0, size);
9557     read_vec_element_i32(s, ele2, rm, 0, size);
9558     read_vec_element_i32(s, ele3, rd, 0, size);
9559 
9560     switch (opcode) {
9561     case 0x0: /* SQRDMLAH */
9562         if (size == 1) {
9563             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9564         } else {
9565             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9566         }
9567         break;
9568     case 0x1: /* SQRDMLSH */
9569         if (size == 1) {
9570             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9571         } else {
9572             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9573         }
9574         break;
9575     default:
9576         g_assert_not_reached();
9577     }
9578 
9579     res = tcg_temp_new_i64();
9580     tcg_gen_extu_i32_i64(res, ele3);
9581     write_fp_dreg(s, rd, res);
9582 }
9583 
9584 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9585                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9586                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9587 {
9588     /* Handle 64->64 opcodes which are shared between the scalar and
9589      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9590      * is valid in either group and also the double-precision fp ops.
9591      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9592      * requires them.
9593      */
9594     TCGCond cond;
9595 
9596     switch (opcode) {
9597     case 0x4: /* CLS, CLZ */
9598         if (u) {
9599             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9600         } else {
9601             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9602         }
9603         break;
9604     case 0x5: /* NOT */
9605         /* This opcode is shared with CNT and RBIT but we have earlier
9606          * enforced that size == 3 if and only if this is the NOT insn.
9607          */
9608         tcg_gen_not_i64(tcg_rd, tcg_rn);
9609         break;
9610     case 0x7: /* SQABS, SQNEG */
9611         if (u) {
9612             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9613         } else {
9614             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9615         }
9616         break;
9617     case 0xa: /* CMLT */
9618         /* 64 bit integer comparison against zero, result is
9619          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9620          * subtracting 1.
9621          */
9622         cond = TCG_COND_LT;
9623     do_cmop:
9624         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9625         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9626         break;
9627     case 0x8: /* CMGT, CMGE */
9628         cond = u ? TCG_COND_GE : TCG_COND_GT;
9629         goto do_cmop;
9630     case 0x9: /* CMEQ, CMLE */
9631         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9632         goto do_cmop;
9633     case 0xb: /* ABS, NEG */
9634         if (u) {
9635             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9636         } else {
9637             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9638         }
9639         break;
9640     case 0x2f: /* FABS */
9641         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9642         break;
9643     case 0x6f: /* FNEG */
9644         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9645         break;
9646     case 0x7f: /* FSQRT */
9647         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9648         break;
9649     case 0x1a: /* FCVTNS */
9650     case 0x1b: /* FCVTMS */
9651     case 0x1c: /* FCVTAS */
9652     case 0x3a: /* FCVTPS */
9653     case 0x3b: /* FCVTZS */
9654         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9655         break;
9656     case 0x5a: /* FCVTNU */
9657     case 0x5b: /* FCVTMU */
9658     case 0x5c: /* FCVTAU */
9659     case 0x7a: /* FCVTPU */
9660     case 0x7b: /* FCVTZU */
9661         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9662         break;
9663     case 0x18: /* FRINTN */
9664     case 0x19: /* FRINTM */
9665     case 0x38: /* FRINTP */
9666     case 0x39: /* FRINTZ */
9667     case 0x58: /* FRINTA */
9668     case 0x79: /* FRINTI */
9669         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9670         break;
9671     case 0x59: /* FRINTX */
9672         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9673         break;
9674     case 0x1e: /* FRINT32Z */
9675     case 0x5e: /* FRINT32X */
9676         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9677         break;
9678     case 0x1f: /* FRINT64Z */
9679     case 0x5f: /* FRINT64X */
9680         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9681         break;
9682     default:
9683         g_assert_not_reached();
9684     }
9685 }
9686 
9687 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9688                                    bool is_scalar, bool is_u, bool is_q,
9689                                    int size, int rn, int rd)
9690 {
9691     bool is_double = (size == MO_64);
9692     TCGv_ptr fpst;
9693 
9694     if (!fp_access_check(s)) {
9695         return;
9696     }
9697 
9698     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9699 
9700     if (is_double) {
9701         TCGv_i64 tcg_op = tcg_temp_new_i64();
9702         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9703         TCGv_i64 tcg_res = tcg_temp_new_i64();
9704         NeonGenTwoDoubleOpFn *genfn;
9705         bool swap = false;
9706         int pass;
9707 
9708         switch (opcode) {
9709         case 0x2e: /* FCMLT (zero) */
9710             swap = true;
9711             /* fallthrough */
9712         case 0x2c: /* FCMGT (zero) */
9713             genfn = gen_helper_neon_cgt_f64;
9714             break;
9715         case 0x2d: /* FCMEQ (zero) */
9716             genfn = gen_helper_neon_ceq_f64;
9717             break;
9718         case 0x6d: /* FCMLE (zero) */
9719             swap = true;
9720             /* fall through */
9721         case 0x6c: /* FCMGE (zero) */
9722             genfn = gen_helper_neon_cge_f64;
9723             break;
9724         default:
9725             g_assert_not_reached();
9726         }
9727 
9728         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9729             read_vec_element(s, tcg_op, rn, pass, MO_64);
9730             if (swap) {
9731                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9732             } else {
9733                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9734             }
9735             write_vec_element(s, tcg_res, rd, pass, MO_64);
9736         }
9737 
9738         clear_vec_high(s, !is_scalar, rd);
9739     } else {
9740         TCGv_i32 tcg_op = tcg_temp_new_i32();
9741         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9742         TCGv_i32 tcg_res = tcg_temp_new_i32();
9743         NeonGenTwoSingleOpFn *genfn;
9744         bool swap = false;
9745         int pass, maxpasses;
9746 
9747         if (size == MO_16) {
9748             switch (opcode) {
9749             case 0x2e: /* FCMLT (zero) */
9750                 swap = true;
9751                 /* fall through */
9752             case 0x2c: /* FCMGT (zero) */
9753                 genfn = gen_helper_advsimd_cgt_f16;
9754                 break;
9755             case 0x2d: /* FCMEQ (zero) */
9756                 genfn = gen_helper_advsimd_ceq_f16;
9757                 break;
9758             case 0x6d: /* FCMLE (zero) */
9759                 swap = true;
9760                 /* fall through */
9761             case 0x6c: /* FCMGE (zero) */
9762                 genfn = gen_helper_advsimd_cge_f16;
9763                 break;
9764             default:
9765                 g_assert_not_reached();
9766             }
9767         } else {
9768             switch (opcode) {
9769             case 0x2e: /* FCMLT (zero) */
9770                 swap = true;
9771                 /* fall through */
9772             case 0x2c: /* FCMGT (zero) */
9773                 genfn = gen_helper_neon_cgt_f32;
9774                 break;
9775             case 0x2d: /* FCMEQ (zero) */
9776                 genfn = gen_helper_neon_ceq_f32;
9777                 break;
9778             case 0x6d: /* FCMLE (zero) */
9779                 swap = true;
9780                 /* fall through */
9781             case 0x6c: /* FCMGE (zero) */
9782                 genfn = gen_helper_neon_cge_f32;
9783                 break;
9784             default:
9785                 g_assert_not_reached();
9786             }
9787         }
9788 
9789         if (is_scalar) {
9790             maxpasses = 1;
9791         } else {
9792             int vector_size = 8 << is_q;
9793             maxpasses = vector_size >> size;
9794         }
9795 
9796         for (pass = 0; pass < maxpasses; pass++) {
9797             read_vec_element_i32(s, tcg_op, rn, pass, size);
9798             if (swap) {
9799                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9800             } else {
9801                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9802             }
9803             if (is_scalar) {
9804                 write_fp_sreg(s, rd, tcg_res);
9805             } else {
9806                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9807             }
9808         }
9809 
9810         if (!is_scalar) {
9811             clear_vec_high(s, is_q, rd);
9812         }
9813     }
9814 }
9815 
9816 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9817                                     bool is_scalar, bool is_u, bool is_q,
9818                                     int size, int rn, int rd)
9819 {
9820     bool is_double = (size == 3);
9821     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9822 
9823     if (is_double) {
9824         TCGv_i64 tcg_op = tcg_temp_new_i64();
9825         TCGv_i64 tcg_res = tcg_temp_new_i64();
9826         int pass;
9827 
9828         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9829             read_vec_element(s, tcg_op, rn, pass, MO_64);
9830             switch (opcode) {
9831             case 0x3d: /* FRECPE */
9832                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9833                 break;
9834             case 0x3f: /* FRECPX */
9835                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9836                 break;
9837             case 0x7d: /* FRSQRTE */
9838                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9839                 break;
9840             default:
9841                 g_assert_not_reached();
9842             }
9843             write_vec_element(s, tcg_res, rd, pass, MO_64);
9844         }
9845         clear_vec_high(s, !is_scalar, rd);
9846     } else {
9847         TCGv_i32 tcg_op = tcg_temp_new_i32();
9848         TCGv_i32 tcg_res = tcg_temp_new_i32();
9849         int pass, maxpasses;
9850 
9851         if (is_scalar) {
9852             maxpasses = 1;
9853         } else {
9854             maxpasses = is_q ? 4 : 2;
9855         }
9856 
9857         for (pass = 0; pass < maxpasses; pass++) {
9858             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9859 
9860             switch (opcode) {
9861             case 0x3c: /* URECPE */
9862                 gen_helper_recpe_u32(tcg_res, tcg_op);
9863                 break;
9864             case 0x3d: /* FRECPE */
9865                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9866                 break;
9867             case 0x3f: /* FRECPX */
9868                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9869                 break;
9870             case 0x7d: /* FRSQRTE */
9871                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9872                 break;
9873             default:
9874                 g_assert_not_reached();
9875             }
9876 
9877             if (is_scalar) {
9878                 write_fp_sreg(s, rd, tcg_res);
9879             } else {
9880                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9881             }
9882         }
9883         if (!is_scalar) {
9884             clear_vec_high(s, is_q, rd);
9885         }
9886     }
9887 }
9888 
9889 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9890                                 int opcode, bool u, bool is_q,
9891                                 int size, int rn, int rd)
9892 {
9893     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9894      * in the source becomes a size element in the destination).
9895      */
9896     int pass;
9897     TCGv_i32 tcg_res[2];
9898     int destelt = is_q ? 2 : 0;
9899     int passes = scalar ? 1 : 2;
9900 
9901     if (scalar) {
9902         tcg_res[1] = tcg_constant_i32(0);
9903     }
9904 
9905     for (pass = 0; pass < passes; pass++) {
9906         TCGv_i64 tcg_op = tcg_temp_new_i64();
9907         NeonGenNarrowFn *genfn = NULL;
9908         NeonGenNarrowEnvFn *genenvfn = NULL;
9909 
9910         if (scalar) {
9911             read_vec_element(s, tcg_op, rn, pass, size + 1);
9912         } else {
9913             read_vec_element(s, tcg_op, rn, pass, MO_64);
9914         }
9915         tcg_res[pass] = tcg_temp_new_i32();
9916 
9917         switch (opcode) {
9918         case 0x12: /* XTN, SQXTUN */
9919         {
9920             static NeonGenNarrowFn * const xtnfns[3] = {
9921                 gen_helper_neon_narrow_u8,
9922                 gen_helper_neon_narrow_u16,
9923                 tcg_gen_extrl_i64_i32,
9924             };
9925             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9926                 gen_helper_neon_unarrow_sat8,
9927                 gen_helper_neon_unarrow_sat16,
9928                 gen_helper_neon_unarrow_sat32,
9929             };
9930             if (u) {
9931                 genenvfn = sqxtunfns[size];
9932             } else {
9933                 genfn = xtnfns[size];
9934             }
9935             break;
9936         }
9937         case 0x14: /* SQXTN, UQXTN */
9938         {
9939             static NeonGenNarrowEnvFn * const fns[3][2] = {
9940                 { gen_helper_neon_narrow_sat_s8,
9941                   gen_helper_neon_narrow_sat_u8 },
9942                 { gen_helper_neon_narrow_sat_s16,
9943                   gen_helper_neon_narrow_sat_u16 },
9944                 { gen_helper_neon_narrow_sat_s32,
9945                   gen_helper_neon_narrow_sat_u32 },
9946             };
9947             genenvfn = fns[size][u];
9948             break;
9949         }
9950         case 0x16: /* FCVTN, FCVTN2 */
9951             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9952             if (size == 2) {
9953                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9954             } else {
9955                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9956                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9957                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9958                 TCGv_i32 ahp = get_ahp_flag();
9959 
9960                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9961                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9962                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9963                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9964             }
9965             break;
9966         case 0x36: /* BFCVTN, BFCVTN2 */
9967             {
9968                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9969                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9970             }
9971             break;
9972         case 0x56:  /* FCVTXN, FCVTXN2 */
9973             /* 64 bit to 32 bit float conversion
9974              * with von Neumann rounding (round to odd)
9975              */
9976             assert(size == 2);
9977             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9978             break;
9979         default:
9980             g_assert_not_reached();
9981         }
9982 
9983         if (genfn) {
9984             genfn(tcg_res[pass], tcg_op);
9985         } else if (genenvfn) {
9986             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9987         }
9988     }
9989 
9990     for (pass = 0; pass < 2; pass++) {
9991         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9992     }
9993     clear_vec_high(s, is_q, rd);
9994 }
9995 
9996 /* Remaining saturating accumulating ops */
9997 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9998                                 bool is_q, int size, int rn, int rd)
9999 {
10000     bool is_double = (size == 3);
10001 
10002     if (is_double) {
10003         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10004         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10005         int pass;
10006 
10007         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10008             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10009             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10010 
10011             if (is_u) { /* USQADD */
10012                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10013             } else { /* SUQADD */
10014                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10015             }
10016             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10017         }
10018         clear_vec_high(s, !is_scalar, rd);
10019     } else {
10020         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10021         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10022         int pass, maxpasses;
10023 
10024         if (is_scalar) {
10025             maxpasses = 1;
10026         } else {
10027             maxpasses = is_q ? 4 : 2;
10028         }
10029 
10030         for (pass = 0; pass < maxpasses; pass++) {
10031             if (is_scalar) {
10032                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10033                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10034             } else {
10035                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10036                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10037             }
10038 
10039             if (is_u) { /* USQADD */
10040                 switch (size) {
10041                 case 0:
10042                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10043                     break;
10044                 case 1:
10045                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10046                     break;
10047                 case 2:
10048                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10049                     break;
10050                 default:
10051                     g_assert_not_reached();
10052                 }
10053             } else { /* SUQADD */
10054                 switch (size) {
10055                 case 0:
10056                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10057                     break;
10058                 case 1:
10059                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10060                     break;
10061                 case 2:
10062                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10063                     break;
10064                 default:
10065                     g_assert_not_reached();
10066                 }
10067             }
10068 
10069             if (is_scalar) {
10070                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10071             }
10072             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10073         }
10074         clear_vec_high(s, is_q, rd);
10075     }
10076 }
10077 
10078 /* AdvSIMD scalar two reg misc
10079  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10080  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10081  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10082  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10083  */
10084 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10085 {
10086     int rd = extract32(insn, 0, 5);
10087     int rn = extract32(insn, 5, 5);
10088     int opcode = extract32(insn, 12, 5);
10089     int size = extract32(insn, 22, 2);
10090     bool u = extract32(insn, 29, 1);
10091     bool is_fcvt = false;
10092     int rmode;
10093     TCGv_i32 tcg_rmode;
10094     TCGv_ptr tcg_fpstatus;
10095 
10096     switch (opcode) {
10097     case 0x3: /* USQADD / SUQADD*/
10098         if (!fp_access_check(s)) {
10099             return;
10100         }
10101         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10102         return;
10103     case 0x7: /* SQABS / SQNEG */
10104         break;
10105     case 0xa: /* CMLT */
10106         if (u) {
10107             unallocated_encoding(s);
10108             return;
10109         }
10110         /* fall through */
10111     case 0x8: /* CMGT, CMGE */
10112     case 0x9: /* CMEQ, CMLE */
10113     case 0xb: /* ABS, NEG */
10114         if (size != 3) {
10115             unallocated_encoding(s);
10116             return;
10117         }
10118         break;
10119     case 0x12: /* SQXTUN */
10120         if (!u) {
10121             unallocated_encoding(s);
10122             return;
10123         }
10124         /* fall through */
10125     case 0x14: /* SQXTN, UQXTN */
10126         if (size == 3) {
10127             unallocated_encoding(s);
10128             return;
10129         }
10130         if (!fp_access_check(s)) {
10131             return;
10132         }
10133         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10134         return;
10135     case 0xc ... 0xf:
10136     case 0x16 ... 0x1d:
10137     case 0x1f:
10138         /* Floating point: U, size[1] and opcode indicate operation;
10139          * size[0] indicates single or double precision.
10140          */
10141         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10142         size = extract32(size, 0, 1) ? 3 : 2;
10143         switch (opcode) {
10144         case 0x2c: /* FCMGT (zero) */
10145         case 0x2d: /* FCMEQ (zero) */
10146         case 0x2e: /* FCMLT (zero) */
10147         case 0x6c: /* FCMGE (zero) */
10148         case 0x6d: /* FCMLE (zero) */
10149             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10150             return;
10151         case 0x1d: /* SCVTF */
10152         case 0x5d: /* UCVTF */
10153         {
10154             bool is_signed = (opcode == 0x1d);
10155             if (!fp_access_check(s)) {
10156                 return;
10157             }
10158             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10159             return;
10160         }
10161         case 0x3d: /* FRECPE */
10162         case 0x3f: /* FRECPX */
10163         case 0x7d: /* FRSQRTE */
10164             if (!fp_access_check(s)) {
10165                 return;
10166             }
10167             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10168             return;
10169         case 0x1a: /* FCVTNS */
10170         case 0x1b: /* FCVTMS */
10171         case 0x3a: /* FCVTPS */
10172         case 0x3b: /* FCVTZS */
10173         case 0x5a: /* FCVTNU */
10174         case 0x5b: /* FCVTMU */
10175         case 0x7a: /* FCVTPU */
10176         case 0x7b: /* FCVTZU */
10177             is_fcvt = true;
10178             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10179             break;
10180         case 0x1c: /* FCVTAS */
10181         case 0x5c: /* FCVTAU */
10182             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10183             is_fcvt = true;
10184             rmode = FPROUNDING_TIEAWAY;
10185             break;
10186         case 0x56: /* FCVTXN, FCVTXN2 */
10187             if (size == 2) {
10188                 unallocated_encoding(s);
10189                 return;
10190             }
10191             if (!fp_access_check(s)) {
10192                 return;
10193             }
10194             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10195             return;
10196         default:
10197             unallocated_encoding(s);
10198             return;
10199         }
10200         break;
10201     default:
10202         unallocated_encoding(s);
10203         return;
10204     }
10205 
10206     if (!fp_access_check(s)) {
10207         return;
10208     }
10209 
10210     if (is_fcvt) {
10211         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10212         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10213     } else {
10214         tcg_fpstatus = NULL;
10215         tcg_rmode = NULL;
10216     }
10217 
10218     if (size == 3) {
10219         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10220         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10221 
10222         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10223         write_fp_dreg(s, rd, tcg_rd);
10224     } else {
10225         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10226         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10227 
10228         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10229 
10230         switch (opcode) {
10231         case 0x7: /* SQABS, SQNEG */
10232         {
10233             NeonGenOneOpEnvFn *genfn;
10234             static NeonGenOneOpEnvFn * const fns[3][2] = {
10235                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10236                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10237                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10238             };
10239             genfn = fns[size][u];
10240             genfn(tcg_rd, cpu_env, tcg_rn);
10241             break;
10242         }
10243         case 0x1a: /* FCVTNS */
10244         case 0x1b: /* FCVTMS */
10245         case 0x1c: /* FCVTAS */
10246         case 0x3a: /* FCVTPS */
10247         case 0x3b: /* FCVTZS */
10248             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10249                                  tcg_fpstatus);
10250             break;
10251         case 0x5a: /* FCVTNU */
10252         case 0x5b: /* FCVTMU */
10253         case 0x5c: /* FCVTAU */
10254         case 0x7a: /* FCVTPU */
10255         case 0x7b: /* FCVTZU */
10256             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10257                                  tcg_fpstatus);
10258             break;
10259         default:
10260             g_assert_not_reached();
10261         }
10262 
10263         write_fp_sreg(s, rd, tcg_rd);
10264     }
10265 
10266     if (is_fcvt) {
10267         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10268     }
10269 }
10270 
10271 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10272 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10273                                  int immh, int immb, int opcode, int rn, int rd)
10274 {
10275     int size = 32 - clz32(immh) - 1;
10276     int immhb = immh << 3 | immb;
10277     int shift = 2 * (8 << size) - immhb;
10278     GVecGen2iFn *gvec_fn;
10279 
10280     if (extract32(immh, 3, 1) && !is_q) {
10281         unallocated_encoding(s);
10282         return;
10283     }
10284     tcg_debug_assert(size <= 3);
10285 
10286     if (!fp_access_check(s)) {
10287         return;
10288     }
10289 
10290     switch (opcode) {
10291     case 0x02: /* SSRA / USRA (accumulate) */
10292         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10293         break;
10294 
10295     case 0x08: /* SRI */
10296         gvec_fn = gen_gvec_sri;
10297         break;
10298 
10299     case 0x00: /* SSHR / USHR */
10300         if (is_u) {
10301             if (shift == 8 << size) {
10302                 /* Shift count the same size as element size produces zero.  */
10303                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10304                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10305                 return;
10306             }
10307             gvec_fn = tcg_gen_gvec_shri;
10308         } else {
10309             /* Shift count the same size as element size produces all sign.  */
10310             if (shift == 8 << size) {
10311                 shift -= 1;
10312             }
10313             gvec_fn = tcg_gen_gvec_sari;
10314         }
10315         break;
10316 
10317     case 0x04: /* SRSHR / URSHR (rounding) */
10318         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10319         break;
10320 
10321     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10322         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10323         break;
10324 
10325     default:
10326         g_assert_not_reached();
10327     }
10328 
10329     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10330 }
10331 
10332 /* SHL/SLI - Vector shift left */
10333 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10334                                  int immh, int immb, int opcode, int rn, int rd)
10335 {
10336     int size = 32 - clz32(immh) - 1;
10337     int immhb = immh << 3 | immb;
10338     int shift = immhb - (8 << size);
10339 
10340     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10341     assert(size >= 0 && size <= 3);
10342 
10343     if (extract32(immh, 3, 1) && !is_q) {
10344         unallocated_encoding(s);
10345         return;
10346     }
10347 
10348     if (!fp_access_check(s)) {
10349         return;
10350     }
10351 
10352     if (insert) {
10353         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10354     } else {
10355         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10356     }
10357 }
10358 
10359 /* USHLL/SHLL - Vector shift left with widening */
10360 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10361                                  int immh, int immb, int opcode, int rn, int rd)
10362 {
10363     int size = 32 - clz32(immh) - 1;
10364     int immhb = immh << 3 | immb;
10365     int shift = immhb - (8 << size);
10366     int dsize = 64;
10367     int esize = 8 << size;
10368     int elements = dsize/esize;
10369     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10370     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10371     int i;
10372 
10373     if (size >= 3) {
10374         unallocated_encoding(s);
10375         return;
10376     }
10377 
10378     if (!fp_access_check(s)) {
10379         return;
10380     }
10381 
10382     /* For the LL variants the store is larger than the load,
10383      * so if rd == rn we would overwrite parts of our input.
10384      * So load everything right now and use shifts in the main loop.
10385      */
10386     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10387 
10388     for (i = 0; i < elements; i++) {
10389         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10390         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10391         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10392         write_vec_element(s, tcg_rd, rd, i, size + 1);
10393     }
10394 }
10395 
10396 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10397 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10398                                  int immh, int immb, int opcode, int rn, int rd)
10399 {
10400     int immhb = immh << 3 | immb;
10401     int size = 32 - clz32(immh) - 1;
10402     int dsize = 64;
10403     int esize = 8 << size;
10404     int elements = dsize/esize;
10405     int shift = (2 * esize) - immhb;
10406     bool round = extract32(opcode, 0, 1);
10407     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10408     TCGv_i64 tcg_round;
10409     int i;
10410 
10411     if (extract32(immh, 3, 1)) {
10412         unallocated_encoding(s);
10413         return;
10414     }
10415 
10416     if (!fp_access_check(s)) {
10417         return;
10418     }
10419 
10420     tcg_rn = tcg_temp_new_i64();
10421     tcg_rd = tcg_temp_new_i64();
10422     tcg_final = tcg_temp_new_i64();
10423     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10424 
10425     if (round) {
10426         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10427     } else {
10428         tcg_round = NULL;
10429     }
10430 
10431     for (i = 0; i < elements; i++) {
10432         read_vec_element(s, tcg_rn, rn, i, size+1);
10433         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10434                                 false, true, size+1, shift);
10435 
10436         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10437     }
10438 
10439     if (!is_q) {
10440         write_vec_element(s, tcg_final, rd, 0, MO_64);
10441     } else {
10442         write_vec_element(s, tcg_final, rd, 1, MO_64);
10443     }
10444 
10445     clear_vec_high(s, is_q, rd);
10446 }
10447 
10448 
10449 /* AdvSIMD shift by immediate
10450  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10451  * +---+---+---+-------------+------+------+--------+---+------+------+
10452  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10453  * +---+---+---+-------------+------+------+--------+---+------+------+
10454  */
10455 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10456 {
10457     int rd = extract32(insn, 0, 5);
10458     int rn = extract32(insn, 5, 5);
10459     int opcode = extract32(insn, 11, 5);
10460     int immb = extract32(insn, 16, 3);
10461     int immh = extract32(insn, 19, 4);
10462     bool is_u = extract32(insn, 29, 1);
10463     bool is_q = extract32(insn, 30, 1);
10464 
10465     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10466     assert(immh != 0);
10467 
10468     switch (opcode) {
10469     case 0x08: /* SRI */
10470         if (!is_u) {
10471             unallocated_encoding(s);
10472             return;
10473         }
10474         /* fall through */
10475     case 0x00: /* SSHR / USHR */
10476     case 0x02: /* SSRA / USRA (accumulate) */
10477     case 0x04: /* SRSHR / URSHR (rounding) */
10478     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10479         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10480         break;
10481     case 0x0a: /* SHL / SLI */
10482         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10483         break;
10484     case 0x10: /* SHRN */
10485     case 0x11: /* RSHRN / SQRSHRUN */
10486         if (is_u) {
10487             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10488                                    opcode, rn, rd);
10489         } else {
10490             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10491         }
10492         break;
10493     case 0x12: /* SQSHRN / UQSHRN */
10494     case 0x13: /* SQRSHRN / UQRSHRN */
10495         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10496                                opcode, rn, rd);
10497         break;
10498     case 0x14: /* SSHLL / USHLL */
10499         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10500         break;
10501     case 0x1c: /* SCVTF / UCVTF */
10502         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10503                                      opcode, rn, rd);
10504         break;
10505     case 0xc: /* SQSHLU */
10506         if (!is_u) {
10507             unallocated_encoding(s);
10508             return;
10509         }
10510         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10511         break;
10512     case 0xe: /* SQSHL, UQSHL */
10513         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10514         break;
10515     case 0x1f: /* FCVTZS/ FCVTZU */
10516         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10517         return;
10518     default:
10519         unallocated_encoding(s);
10520         return;
10521     }
10522 }
10523 
10524 /* Generate code to do a "long" addition or subtraction, ie one done in
10525  * TCGv_i64 on vector lanes twice the width specified by size.
10526  */
10527 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10528                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10529 {
10530     static NeonGenTwo64OpFn * const fns[3][2] = {
10531         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10532         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10533         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10534     };
10535     NeonGenTwo64OpFn *genfn;
10536     assert(size < 3);
10537 
10538     genfn = fns[size][is_sub];
10539     genfn(tcg_res, tcg_op1, tcg_op2);
10540 }
10541 
10542 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10543                                 int opcode, int rd, int rn, int rm)
10544 {
10545     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10546     TCGv_i64 tcg_res[2];
10547     int pass, accop;
10548 
10549     tcg_res[0] = tcg_temp_new_i64();
10550     tcg_res[1] = tcg_temp_new_i64();
10551 
10552     /* Does this op do an adding accumulate, a subtracting accumulate,
10553      * or no accumulate at all?
10554      */
10555     switch (opcode) {
10556     case 5:
10557     case 8:
10558     case 9:
10559         accop = 1;
10560         break;
10561     case 10:
10562     case 11:
10563         accop = -1;
10564         break;
10565     default:
10566         accop = 0;
10567         break;
10568     }
10569 
10570     if (accop != 0) {
10571         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10572         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10573     }
10574 
10575     /* size == 2 means two 32x32->64 operations; this is worth special
10576      * casing because we can generally handle it inline.
10577      */
10578     if (size == 2) {
10579         for (pass = 0; pass < 2; pass++) {
10580             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10581             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10582             TCGv_i64 tcg_passres;
10583             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10584 
10585             int elt = pass + is_q * 2;
10586 
10587             read_vec_element(s, tcg_op1, rn, elt, memop);
10588             read_vec_element(s, tcg_op2, rm, elt, memop);
10589 
10590             if (accop == 0) {
10591                 tcg_passres = tcg_res[pass];
10592             } else {
10593                 tcg_passres = tcg_temp_new_i64();
10594             }
10595 
10596             switch (opcode) {
10597             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10598                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10599                 break;
10600             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10601                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10602                 break;
10603             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10604             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10605             {
10606                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10607                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10608 
10609                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10610                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10611                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10612                                     tcg_passres,
10613                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10614                 break;
10615             }
10616             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10617             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10618             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10619                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10620                 break;
10621             case 9: /* SQDMLAL, SQDMLAL2 */
10622             case 11: /* SQDMLSL, SQDMLSL2 */
10623             case 13: /* SQDMULL, SQDMULL2 */
10624                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10625                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10626                                                   tcg_passres, tcg_passres);
10627                 break;
10628             default:
10629                 g_assert_not_reached();
10630             }
10631 
10632             if (opcode == 9 || opcode == 11) {
10633                 /* saturating accumulate ops */
10634                 if (accop < 0) {
10635                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10636                 }
10637                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10638                                                   tcg_res[pass], tcg_passres);
10639             } else if (accop > 0) {
10640                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10641             } else if (accop < 0) {
10642                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10643             }
10644         }
10645     } else {
10646         /* size 0 or 1, generally helper functions */
10647         for (pass = 0; pass < 2; pass++) {
10648             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10649             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10650             TCGv_i64 tcg_passres;
10651             int elt = pass + is_q * 2;
10652 
10653             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10654             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10655 
10656             if (accop == 0) {
10657                 tcg_passres = tcg_res[pass];
10658             } else {
10659                 tcg_passres = tcg_temp_new_i64();
10660             }
10661 
10662             switch (opcode) {
10663             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10664             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10665             {
10666                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10667                 static NeonGenWidenFn * const widenfns[2][2] = {
10668                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10669                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10670                 };
10671                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10672 
10673                 widenfn(tcg_op2_64, tcg_op2);
10674                 widenfn(tcg_passres, tcg_op1);
10675                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10676                               tcg_passres, tcg_op2_64);
10677                 break;
10678             }
10679             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10680             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10681                 if (size == 0) {
10682                     if (is_u) {
10683                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10684                     } else {
10685                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10686                     }
10687                 } else {
10688                     if (is_u) {
10689                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10690                     } else {
10691                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10692                     }
10693                 }
10694                 break;
10695             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10696             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10697             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10698                 if (size == 0) {
10699                     if (is_u) {
10700                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10701                     } else {
10702                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10703                     }
10704                 } else {
10705                     if (is_u) {
10706                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10707                     } else {
10708                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10709                     }
10710                 }
10711                 break;
10712             case 9: /* SQDMLAL, SQDMLAL2 */
10713             case 11: /* SQDMLSL, SQDMLSL2 */
10714             case 13: /* SQDMULL, SQDMULL2 */
10715                 assert(size == 1);
10716                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10717                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10718                                                   tcg_passres, tcg_passres);
10719                 break;
10720             default:
10721                 g_assert_not_reached();
10722             }
10723 
10724             if (accop != 0) {
10725                 if (opcode == 9 || opcode == 11) {
10726                     /* saturating accumulate ops */
10727                     if (accop < 0) {
10728                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10729                     }
10730                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10731                                                       tcg_res[pass],
10732                                                       tcg_passres);
10733                 } else {
10734                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10735                                   tcg_res[pass], tcg_passres);
10736                 }
10737             }
10738         }
10739     }
10740 
10741     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10742     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10743 }
10744 
10745 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10746                             int opcode, int rd, int rn, int rm)
10747 {
10748     TCGv_i64 tcg_res[2];
10749     int part = is_q ? 2 : 0;
10750     int pass;
10751 
10752     for (pass = 0; pass < 2; pass++) {
10753         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10754         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10755         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10756         static NeonGenWidenFn * const widenfns[3][2] = {
10757             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10758             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10759             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10760         };
10761         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10762 
10763         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10764         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10765         widenfn(tcg_op2_wide, tcg_op2);
10766         tcg_res[pass] = tcg_temp_new_i64();
10767         gen_neon_addl(size, (opcode == 3),
10768                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10769     }
10770 
10771     for (pass = 0; pass < 2; pass++) {
10772         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10773     }
10774 }
10775 
10776 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10777 {
10778     tcg_gen_addi_i64(in, in, 1U << 31);
10779     tcg_gen_extrh_i64_i32(res, in);
10780 }
10781 
10782 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10783                                  int opcode, int rd, int rn, int rm)
10784 {
10785     TCGv_i32 tcg_res[2];
10786     int part = is_q ? 2 : 0;
10787     int pass;
10788 
10789     for (pass = 0; pass < 2; pass++) {
10790         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10791         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10792         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10793         static NeonGenNarrowFn * const narrowfns[3][2] = {
10794             { gen_helper_neon_narrow_high_u8,
10795               gen_helper_neon_narrow_round_high_u8 },
10796             { gen_helper_neon_narrow_high_u16,
10797               gen_helper_neon_narrow_round_high_u16 },
10798             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10799         };
10800         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10801 
10802         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10803         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10804 
10805         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10806 
10807         tcg_res[pass] = tcg_temp_new_i32();
10808         gennarrow(tcg_res[pass], tcg_wideres);
10809     }
10810 
10811     for (pass = 0; pass < 2; pass++) {
10812         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10813     }
10814     clear_vec_high(s, is_q, rd);
10815 }
10816 
10817 /* AdvSIMD three different
10818  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10819  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10820  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10821  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10822  */
10823 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10824 {
10825     /* Instructions in this group fall into three basic classes
10826      * (in each case with the operation working on each element in
10827      * the input vectors):
10828      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10829      *     128 bit input)
10830      * (2) wide 64 x 128 -> 128
10831      * (3) narrowing 128 x 128 -> 64
10832      * Here we do initial decode, catch unallocated cases and
10833      * dispatch to separate functions for each class.
10834      */
10835     int is_q = extract32(insn, 30, 1);
10836     int is_u = extract32(insn, 29, 1);
10837     int size = extract32(insn, 22, 2);
10838     int opcode = extract32(insn, 12, 4);
10839     int rm = extract32(insn, 16, 5);
10840     int rn = extract32(insn, 5, 5);
10841     int rd = extract32(insn, 0, 5);
10842 
10843     switch (opcode) {
10844     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10845     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10846         /* 64 x 128 -> 128 */
10847         if (size == 3) {
10848             unallocated_encoding(s);
10849             return;
10850         }
10851         if (!fp_access_check(s)) {
10852             return;
10853         }
10854         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10855         break;
10856     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10857     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10858         /* 128 x 128 -> 64 */
10859         if (size == 3) {
10860             unallocated_encoding(s);
10861             return;
10862         }
10863         if (!fp_access_check(s)) {
10864             return;
10865         }
10866         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10867         break;
10868     case 14: /* PMULL, PMULL2 */
10869         if (is_u) {
10870             unallocated_encoding(s);
10871             return;
10872         }
10873         switch (size) {
10874         case 0: /* PMULL.P8 */
10875             if (!fp_access_check(s)) {
10876                 return;
10877             }
10878             /* The Q field specifies lo/hi half input for this insn.  */
10879             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10880                              gen_helper_neon_pmull_h);
10881             break;
10882 
10883         case 3: /* PMULL.P64 */
10884             if (!dc_isar_feature(aa64_pmull, s)) {
10885                 unallocated_encoding(s);
10886                 return;
10887             }
10888             if (!fp_access_check(s)) {
10889                 return;
10890             }
10891             /* The Q field specifies lo/hi half input for this insn.  */
10892             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10893                              gen_helper_gvec_pmull_q);
10894             break;
10895 
10896         default:
10897             unallocated_encoding(s);
10898             break;
10899         }
10900         return;
10901     case 9: /* SQDMLAL, SQDMLAL2 */
10902     case 11: /* SQDMLSL, SQDMLSL2 */
10903     case 13: /* SQDMULL, SQDMULL2 */
10904         if (is_u || size == 0) {
10905             unallocated_encoding(s);
10906             return;
10907         }
10908         /* fall through */
10909     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10910     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10911     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10912     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10913     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10914     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10915     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10916         /* 64 x 64 -> 128 */
10917         if (size == 3) {
10918             unallocated_encoding(s);
10919             return;
10920         }
10921         if (!fp_access_check(s)) {
10922             return;
10923         }
10924 
10925         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10926         break;
10927     default:
10928         /* opcode 15 not allocated */
10929         unallocated_encoding(s);
10930         break;
10931     }
10932 }
10933 
10934 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10935 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10936 {
10937     int rd = extract32(insn, 0, 5);
10938     int rn = extract32(insn, 5, 5);
10939     int rm = extract32(insn, 16, 5);
10940     int size = extract32(insn, 22, 2);
10941     bool is_u = extract32(insn, 29, 1);
10942     bool is_q = extract32(insn, 30, 1);
10943 
10944     if (!fp_access_check(s)) {
10945         return;
10946     }
10947 
10948     switch (size + 4 * is_u) {
10949     case 0: /* AND */
10950         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10951         return;
10952     case 1: /* BIC */
10953         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10954         return;
10955     case 2: /* ORR */
10956         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10957         return;
10958     case 3: /* ORN */
10959         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10960         return;
10961     case 4: /* EOR */
10962         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10963         return;
10964 
10965     case 5: /* BSL bitwise select */
10966         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10967         return;
10968     case 6: /* BIT, bitwise insert if true */
10969         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10970         return;
10971     case 7: /* BIF, bitwise insert if false */
10972         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10973         return;
10974 
10975     default:
10976         g_assert_not_reached();
10977     }
10978 }
10979 
10980 /* Pairwise op subgroup of C3.6.16.
10981  *
10982  * This is called directly or via the handle_3same_float for float pairwise
10983  * operations where the opcode and size are calculated differently.
10984  */
10985 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10986                                    int size, int rn, int rm, int rd)
10987 {
10988     TCGv_ptr fpst;
10989     int pass;
10990 
10991     /* Floating point operations need fpst */
10992     if (opcode >= 0x58) {
10993         fpst = fpstatus_ptr(FPST_FPCR);
10994     } else {
10995         fpst = NULL;
10996     }
10997 
10998     if (!fp_access_check(s)) {
10999         return;
11000     }
11001 
11002     /* These operations work on the concatenated rm:rn, with each pair of
11003      * adjacent elements being operated on to produce an element in the result.
11004      */
11005     if (size == 3) {
11006         TCGv_i64 tcg_res[2];
11007 
11008         for (pass = 0; pass < 2; pass++) {
11009             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11010             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11011             int passreg = (pass == 0) ? rn : rm;
11012 
11013             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11014             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11015             tcg_res[pass] = tcg_temp_new_i64();
11016 
11017             switch (opcode) {
11018             case 0x17: /* ADDP */
11019                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11020                 break;
11021             case 0x58: /* FMAXNMP */
11022                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11023                 break;
11024             case 0x5a: /* FADDP */
11025                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11026                 break;
11027             case 0x5e: /* FMAXP */
11028                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11029                 break;
11030             case 0x78: /* FMINNMP */
11031                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11032                 break;
11033             case 0x7e: /* FMINP */
11034                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11035                 break;
11036             default:
11037                 g_assert_not_reached();
11038             }
11039         }
11040 
11041         for (pass = 0; pass < 2; pass++) {
11042             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11043         }
11044     } else {
11045         int maxpass = is_q ? 4 : 2;
11046         TCGv_i32 tcg_res[4];
11047 
11048         for (pass = 0; pass < maxpass; pass++) {
11049             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11050             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11051             NeonGenTwoOpFn *genfn = NULL;
11052             int passreg = pass < (maxpass / 2) ? rn : rm;
11053             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11054 
11055             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11056             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11057             tcg_res[pass] = tcg_temp_new_i32();
11058 
11059             switch (opcode) {
11060             case 0x17: /* ADDP */
11061             {
11062                 static NeonGenTwoOpFn * const fns[3] = {
11063                     gen_helper_neon_padd_u8,
11064                     gen_helper_neon_padd_u16,
11065                     tcg_gen_add_i32,
11066                 };
11067                 genfn = fns[size];
11068                 break;
11069             }
11070             case 0x14: /* SMAXP, UMAXP */
11071             {
11072                 static NeonGenTwoOpFn * const fns[3][2] = {
11073                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11074                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11075                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11076                 };
11077                 genfn = fns[size][u];
11078                 break;
11079             }
11080             case 0x15: /* SMINP, UMINP */
11081             {
11082                 static NeonGenTwoOpFn * const fns[3][2] = {
11083                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11084                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11085                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11086                 };
11087                 genfn = fns[size][u];
11088                 break;
11089             }
11090             /* The FP operations are all on single floats (32 bit) */
11091             case 0x58: /* FMAXNMP */
11092                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11093                 break;
11094             case 0x5a: /* FADDP */
11095                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11096                 break;
11097             case 0x5e: /* FMAXP */
11098                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11099                 break;
11100             case 0x78: /* FMINNMP */
11101                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11102                 break;
11103             case 0x7e: /* FMINP */
11104                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11105                 break;
11106             default:
11107                 g_assert_not_reached();
11108             }
11109 
11110             /* FP ops called directly, otherwise call now */
11111             if (genfn) {
11112                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11113             }
11114         }
11115 
11116         for (pass = 0; pass < maxpass; pass++) {
11117             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11118         }
11119         clear_vec_high(s, is_q, rd);
11120     }
11121 }
11122 
11123 /* Floating point op subgroup of C3.6.16. */
11124 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11125 {
11126     /* For floating point ops, the U, size[1] and opcode bits
11127      * together indicate the operation. size[0] indicates single
11128      * or double.
11129      */
11130     int fpopcode = extract32(insn, 11, 5)
11131         | (extract32(insn, 23, 1) << 5)
11132         | (extract32(insn, 29, 1) << 6);
11133     int is_q = extract32(insn, 30, 1);
11134     int size = extract32(insn, 22, 1);
11135     int rm = extract32(insn, 16, 5);
11136     int rn = extract32(insn, 5, 5);
11137     int rd = extract32(insn, 0, 5);
11138 
11139     int datasize = is_q ? 128 : 64;
11140     int esize = 32 << size;
11141     int elements = datasize / esize;
11142 
11143     if (size == 1 && !is_q) {
11144         unallocated_encoding(s);
11145         return;
11146     }
11147 
11148     switch (fpopcode) {
11149     case 0x58: /* FMAXNMP */
11150     case 0x5a: /* FADDP */
11151     case 0x5e: /* FMAXP */
11152     case 0x78: /* FMINNMP */
11153     case 0x7e: /* FMINP */
11154         if (size && !is_q) {
11155             unallocated_encoding(s);
11156             return;
11157         }
11158         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11159                                rn, rm, rd);
11160         return;
11161     case 0x1b: /* FMULX */
11162     case 0x1f: /* FRECPS */
11163     case 0x3f: /* FRSQRTS */
11164     case 0x5d: /* FACGE */
11165     case 0x7d: /* FACGT */
11166     case 0x19: /* FMLA */
11167     case 0x39: /* FMLS */
11168     case 0x18: /* FMAXNM */
11169     case 0x1a: /* FADD */
11170     case 0x1c: /* FCMEQ */
11171     case 0x1e: /* FMAX */
11172     case 0x38: /* FMINNM */
11173     case 0x3a: /* FSUB */
11174     case 0x3e: /* FMIN */
11175     case 0x5b: /* FMUL */
11176     case 0x5c: /* FCMGE */
11177     case 0x5f: /* FDIV */
11178     case 0x7a: /* FABD */
11179     case 0x7c: /* FCMGT */
11180         if (!fp_access_check(s)) {
11181             return;
11182         }
11183         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11184         return;
11185 
11186     case 0x1d: /* FMLAL  */
11187     case 0x3d: /* FMLSL  */
11188     case 0x59: /* FMLAL2 */
11189     case 0x79: /* FMLSL2 */
11190         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11191             unallocated_encoding(s);
11192             return;
11193         }
11194         if (fp_access_check(s)) {
11195             int is_s = extract32(insn, 23, 1);
11196             int is_2 = extract32(insn, 29, 1);
11197             int data = (is_2 << 1) | is_s;
11198             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11199                                vec_full_reg_offset(s, rn),
11200                                vec_full_reg_offset(s, rm), cpu_env,
11201                                is_q ? 16 : 8, vec_full_reg_size(s),
11202                                data, gen_helper_gvec_fmlal_a64);
11203         }
11204         return;
11205 
11206     default:
11207         unallocated_encoding(s);
11208         return;
11209     }
11210 }
11211 
11212 /* Integer op subgroup of C3.6.16. */
11213 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11214 {
11215     int is_q = extract32(insn, 30, 1);
11216     int u = extract32(insn, 29, 1);
11217     int size = extract32(insn, 22, 2);
11218     int opcode = extract32(insn, 11, 5);
11219     int rm = extract32(insn, 16, 5);
11220     int rn = extract32(insn, 5, 5);
11221     int rd = extract32(insn, 0, 5);
11222     int pass;
11223     TCGCond cond;
11224 
11225     switch (opcode) {
11226     case 0x13: /* MUL, PMUL */
11227         if (u && size != 0) {
11228             unallocated_encoding(s);
11229             return;
11230         }
11231         /* fall through */
11232     case 0x0: /* SHADD, UHADD */
11233     case 0x2: /* SRHADD, URHADD */
11234     case 0x4: /* SHSUB, UHSUB */
11235     case 0xc: /* SMAX, UMAX */
11236     case 0xd: /* SMIN, UMIN */
11237     case 0xe: /* SABD, UABD */
11238     case 0xf: /* SABA, UABA */
11239     case 0x12: /* MLA, MLS */
11240         if (size == 3) {
11241             unallocated_encoding(s);
11242             return;
11243         }
11244         break;
11245     case 0x16: /* SQDMULH, SQRDMULH */
11246         if (size == 0 || size == 3) {
11247             unallocated_encoding(s);
11248             return;
11249         }
11250         break;
11251     default:
11252         if (size == 3 && !is_q) {
11253             unallocated_encoding(s);
11254             return;
11255         }
11256         break;
11257     }
11258 
11259     if (!fp_access_check(s)) {
11260         return;
11261     }
11262 
11263     switch (opcode) {
11264     case 0x01: /* SQADD, UQADD */
11265         if (u) {
11266             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11267         } else {
11268             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11269         }
11270         return;
11271     case 0x05: /* SQSUB, UQSUB */
11272         if (u) {
11273             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11274         } else {
11275             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11276         }
11277         return;
11278     case 0x08: /* SSHL, USHL */
11279         if (u) {
11280             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11281         } else {
11282             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11283         }
11284         return;
11285     case 0x0c: /* SMAX, UMAX */
11286         if (u) {
11287             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11288         } else {
11289             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11290         }
11291         return;
11292     case 0x0d: /* SMIN, UMIN */
11293         if (u) {
11294             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11295         } else {
11296             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11297         }
11298         return;
11299     case 0xe: /* SABD, UABD */
11300         if (u) {
11301             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11302         } else {
11303             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11304         }
11305         return;
11306     case 0xf: /* SABA, UABA */
11307         if (u) {
11308             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11309         } else {
11310             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11311         }
11312         return;
11313     case 0x10: /* ADD, SUB */
11314         if (u) {
11315             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11316         } else {
11317             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11318         }
11319         return;
11320     case 0x13: /* MUL, PMUL */
11321         if (!u) { /* MUL */
11322             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11323         } else {  /* PMUL */
11324             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11325         }
11326         return;
11327     case 0x12: /* MLA, MLS */
11328         if (u) {
11329             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11330         } else {
11331             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11332         }
11333         return;
11334     case 0x16: /* SQDMULH, SQRDMULH */
11335         {
11336             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11337                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11338                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11339             };
11340             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11341         }
11342         return;
11343     case 0x11:
11344         if (!u) { /* CMTST */
11345             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11346             return;
11347         }
11348         /* else CMEQ */
11349         cond = TCG_COND_EQ;
11350         goto do_gvec_cmp;
11351     case 0x06: /* CMGT, CMHI */
11352         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11353         goto do_gvec_cmp;
11354     case 0x07: /* CMGE, CMHS */
11355         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11356     do_gvec_cmp:
11357         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11358                          vec_full_reg_offset(s, rn),
11359                          vec_full_reg_offset(s, rm),
11360                          is_q ? 16 : 8, vec_full_reg_size(s));
11361         return;
11362     }
11363 
11364     if (size == 3) {
11365         assert(is_q);
11366         for (pass = 0; pass < 2; pass++) {
11367             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11368             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11369             TCGv_i64 tcg_res = tcg_temp_new_i64();
11370 
11371             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11372             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11373 
11374             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11375 
11376             write_vec_element(s, tcg_res, rd, pass, MO_64);
11377         }
11378     } else {
11379         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11380             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11381             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11382             TCGv_i32 tcg_res = tcg_temp_new_i32();
11383             NeonGenTwoOpFn *genfn = NULL;
11384             NeonGenTwoOpEnvFn *genenvfn = NULL;
11385 
11386             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11387             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11388 
11389             switch (opcode) {
11390             case 0x0: /* SHADD, UHADD */
11391             {
11392                 static NeonGenTwoOpFn * const fns[3][2] = {
11393                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11394                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11395                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11396                 };
11397                 genfn = fns[size][u];
11398                 break;
11399             }
11400             case 0x2: /* SRHADD, URHADD */
11401             {
11402                 static NeonGenTwoOpFn * const fns[3][2] = {
11403                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11404                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11405                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11406                 };
11407                 genfn = fns[size][u];
11408                 break;
11409             }
11410             case 0x4: /* SHSUB, UHSUB */
11411             {
11412                 static NeonGenTwoOpFn * const fns[3][2] = {
11413                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11414                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11415                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11416                 };
11417                 genfn = fns[size][u];
11418                 break;
11419             }
11420             case 0x9: /* SQSHL, UQSHL */
11421             {
11422                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11423                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11424                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11425                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11426                 };
11427                 genenvfn = fns[size][u];
11428                 break;
11429             }
11430             case 0xa: /* SRSHL, URSHL */
11431             {
11432                 static NeonGenTwoOpFn * const fns[3][2] = {
11433                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11434                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11435                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11436                 };
11437                 genfn = fns[size][u];
11438                 break;
11439             }
11440             case 0xb: /* SQRSHL, UQRSHL */
11441             {
11442                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11443                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11444                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11445                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11446                 };
11447                 genenvfn = fns[size][u];
11448                 break;
11449             }
11450             default:
11451                 g_assert_not_reached();
11452             }
11453 
11454             if (genenvfn) {
11455                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11456             } else {
11457                 genfn(tcg_res, tcg_op1, tcg_op2);
11458             }
11459 
11460             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11461         }
11462     }
11463     clear_vec_high(s, is_q, rd);
11464 }
11465 
11466 /* AdvSIMD three same
11467  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11468  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11469  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11470  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11471  */
11472 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11473 {
11474     int opcode = extract32(insn, 11, 5);
11475 
11476     switch (opcode) {
11477     case 0x3: /* logic ops */
11478         disas_simd_3same_logic(s, insn);
11479         break;
11480     case 0x17: /* ADDP */
11481     case 0x14: /* SMAXP, UMAXP */
11482     case 0x15: /* SMINP, UMINP */
11483     {
11484         /* Pairwise operations */
11485         int is_q = extract32(insn, 30, 1);
11486         int u = extract32(insn, 29, 1);
11487         int size = extract32(insn, 22, 2);
11488         int rm = extract32(insn, 16, 5);
11489         int rn = extract32(insn, 5, 5);
11490         int rd = extract32(insn, 0, 5);
11491         if (opcode == 0x17) {
11492             if (u || (size == 3 && !is_q)) {
11493                 unallocated_encoding(s);
11494                 return;
11495             }
11496         } else {
11497             if (size == 3) {
11498                 unallocated_encoding(s);
11499                 return;
11500             }
11501         }
11502         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11503         break;
11504     }
11505     case 0x18 ... 0x31:
11506         /* floating point ops, sz[1] and U are part of opcode */
11507         disas_simd_3same_float(s, insn);
11508         break;
11509     default:
11510         disas_simd_3same_int(s, insn);
11511         break;
11512     }
11513 }
11514 
11515 /*
11516  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11517  *
11518  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11519  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11520  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11521  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11522  *
11523  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11524  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11525  *
11526  */
11527 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11528 {
11529     int opcode = extract32(insn, 11, 3);
11530     int u = extract32(insn, 29, 1);
11531     int a = extract32(insn, 23, 1);
11532     int is_q = extract32(insn, 30, 1);
11533     int rm = extract32(insn, 16, 5);
11534     int rn = extract32(insn, 5, 5);
11535     int rd = extract32(insn, 0, 5);
11536     /*
11537      * For these floating point ops, the U, a and opcode bits
11538      * together indicate the operation.
11539      */
11540     int fpopcode = opcode | (a << 3) | (u << 4);
11541     int datasize = is_q ? 128 : 64;
11542     int elements = datasize / 16;
11543     bool pairwise;
11544     TCGv_ptr fpst;
11545     int pass;
11546 
11547     switch (fpopcode) {
11548     case 0x0: /* FMAXNM */
11549     case 0x1: /* FMLA */
11550     case 0x2: /* FADD */
11551     case 0x3: /* FMULX */
11552     case 0x4: /* FCMEQ */
11553     case 0x6: /* FMAX */
11554     case 0x7: /* FRECPS */
11555     case 0x8: /* FMINNM */
11556     case 0x9: /* FMLS */
11557     case 0xa: /* FSUB */
11558     case 0xe: /* FMIN */
11559     case 0xf: /* FRSQRTS */
11560     case 0x13: /* FMUL */
11561     case 0x14: /* FCMGE */
11562     case 0x15: /* FACGE */
11563     case 0x17: /* FDIV */
11564     case 0x1a: /* FABD */
11565     case 0x1c: /* FCMGT */
11566     case 0x1d: /* FACGT */
11567         pairwise = false;
11568         break;
11569     case 0x10: /* FMAXNMP */
11570     case 0x12: /* FADDP */
11571     case 0x16: /* FMAXP */
11572     case 0x18: /* FMINNMP */
11573     case 0x1e: /* FMINP */
11574         pairwise = true;
11575         break;
11576     default:
11577         unallocated_encoding(s);
11578         return;
11579     }
11580 
11581     if (!dc_isar_feature(aa64_fp16, s)) {
11582         unallocated_encoding(s);
11583         return;
11584     }
11585 
11586     if (!fp_access_check(s)) {
11587         return;
11588     }
11589 
11590     fpst = fpstatus_ptr(FPST_FPCR_F16);
11591 
11592     if (pairwise) {
11593         int maxpass = is_q ? 8 : 4;
11594         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11595         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11596         TCGv_i32 tcg_res[8];
11597 
11598         for (pass = 0; pass < maxpass; pass++) {
11599             int passreg = pass < (maxpass / 2) ? rn : rm;
11600             int passelt = (pass << 1) & (maxpass - 1);
11601 
11602             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11603             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11604             tcg_res[pass] = tcg_temp_new_i32();
11605 
11606             switch (fpopcode) {
11607             case 0x10: /* FMAXNMP */
11608                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11609                                            fpst);
11610                 break;
11611             case 0x12: /* FADDP */
11612                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11613                 break;
11614             case 0x16: /* FMAXP */
11615                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11616                 break;
11617             case 0x18: /* FMINNMP */
11618                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11619                                            fpst);
11620                 break;
11621             case 0x1e: /* FMINP */
11622                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11623                 break;
11624             default:
11625                 g_assert_not_reached();
11626             }
11627         }
11628 
11629         for (pass = 0; pass < maxpass; pass++) {
11630             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11631         }
11632     } else {
11633         for (pass = 0; pass < elements; pass++) {
11634             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11635             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11636             TCGv_i32 tcg_res = tcg_temp_new_i32();
11637 
11638             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11639             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11640 
11641             switch (fpopcode) {
11642             case 0x0: /* FMAXNM */
11643                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11644                 break;
11645             case 0x1: /* FMLA */
11646                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11647                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11648                                            fpst);
11649                 break;
11650             case 0x2: /* FADD */
11651                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11652                 break;
11653             case 0x3: /* FMULX */
11654                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11655                 break;
11656             case 0x4: /* FCMEQ */
11657                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11658                 break;
11659             case 0x6: /* FMAX */
11660                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11661                 break;
11662             case 0x7: /* FRECPS */
11663                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11664                 break;
11665             case 0x8: /* FMINNM */
11666                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11667                 break;
11668             case 0x9: /* FMLS */
11669                 /* As usual for ARM, separate negation for fused multiply-add */
11670                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11671                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11672                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11673                                            fpst);
11674                 break;
11675             case 0xa: /* FSUB */
11676                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11677                 break;
11678             case 0xe: /* FMIN */
11679                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11680                 break;
11681             case 0xf: /* FRSQRTS */
11682                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11683                 break;
11684             case 0x13: /* FMUL */
11685                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11686                 break;
11687             case 0x14: /* FCMGE */
11688                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11689                 break;
11690             case 0x15: /* FACGE */
11691                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11692                 break;
11693             case 0x17: /* FDIV */
11694                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11695                 break;
11696             case 0x1a: /* FABD */
11697                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11698                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11699                 break;
11700             case 0x1c: /* FCMGT */
11701                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11702                 break;
11703             case 0x1d: /* FACGT */
11704                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11705                 break;
11706             default:
11707                 g_assert_not_reached();
11708             }
11709 
11710             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11711         }
11712     }
11713 
11714     clear_vec_high(s, is_q, rd);
11715 }
11716 
11717 /* AdvSIMD three same extra
11718  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11719  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11720  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11721  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11722  */
11723 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11724 {
11725     int rd = extract32(insn, 0, 5);
11726     int rn = extract32(insn, 5, 5);
11727     int opcode = extract32(insn, 11, 4);
11728     int rm = extract32(insn, 16, 5);
11729     int size = extract32(insn, 22, 2);
11730     bool u = extract32(insn, 29, 1);
11731     bool is_q = extract32(insn, 30, 1);
11732     bool feature;
11733     int rot;
11734 
11735     switch (u * 16 + opcode) {
11736     case 0x10: /* SQRDMLAH (vector) */
11737     case 0x11: /* SQRDMLSH (vector) */
11738         if (size != 1 && size != 2) {
11739             unallocated_encoding(s);
11740             return;
11741         }
11742         feature = dc_isar_feature(aa64_rdm, s);
11743         break;
11744     case 0x02: /* SDOT (vector) */
11745     case 0x12: /* UDOT (vector) */
11746         if (size != MO_32) {
11747             unallocated_encoding(s);
11748             return;
11749         }
11750         feature = dc_isar_feature(aa64_dp, s);
11751         break;
11752     case 0x03: /* USDOT */
11753         if (size != MO_32) {
11754             unallocated_encoding(s);
11755             return;
11756         }
11757         feature = dc_isar_feature(aa64_i8mm, s);
11758         break;
11759     case 0x04: /* SMMLA */
11760     case 0x14: /* UMMLA */
11761     case 0x05: /* USMMLA */
11762         if (!is_q || size != MO_32) {
11763             unallocated_encoding(s);
11764             return;
11765         }
11766         feature = dc_isar_feature(aa64_i8mm, s);
11767         break;
11768     case 0x18: /* FCMLA, #0 */
11769     case 0x19: /* FCMLA, #90 */
11770     case 0x1a: /* FCMLA, #180 */
11771     case 0x1b: /* FCMLA, #270 */
11772     case 0x1c: /* FCADD, #90 */
11773     case 0x1e: /* FCADD, #270 */
11774         if (size == 0
11775             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11776             || (size == 3 && !is_q)) {
11777             unallocated_encoding(s);
11778             return;
11779         }
11780         feature = dc_isar_feature(aa64_fcma, s);
11781         break;
11782     case 0x1d: /* BFMMLA */
11783         if (size != MO_16 || !is_q) {
11784             unallocated_encoding(s);
11785             return;
11786         }
11787         feature = dc_isar_feature(aa64_bf16, s);
11788         break;
11789     case 0x1f:
11790         switch (size) {
11791         case 1: /* BFDOT */
11792         case 3: /* BFMLAL{B,T} */
11793             feature = dc_isar_feature(aa64_bf16, s);
11794             break;
11795         default:
11796             unallocated_encoding(s);
11797             return;
11798         }
11799         break;
11800     default:
11801         unallocated_encoding(s);
11802         return;
11803     }
11804     if (!feature) {
11805         unallocated_encoding(s);
11806         return;
11807     }
11808     if (!fp_access_check(s)) {
11809         return;
11810     }
11811 
11812     switch (opcode) {
11813     case 0x0: /* SQRDMLAH (vector) */
11814         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11815         return;
11816 
11817     case 0x1: /* SQRDMLSH (vector) */
11818         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11819         return;
11820 
11821     case 0x2: /* SDOT / UDOT */
11822         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11823                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11824         return;
11825 
11826     case 0x3: /* USDOT */
11827         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11828         return;
11829 
11830     case 0x04: /* SMMLA, UMMLA */
11831         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11832                          u ? gen_helper_gvec_ummla_b
11833                          : gen_helper_gvec_smmla_b);
11834         return;
11835     case 0x05: /* USMMLA */
11836         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11837         return;
11838 
11839     case 0x8: /* FCMLA, #0 */
11840     case 0x9: /* FCMLA, #90 */
11841     case 0xa: /* FCMLA, #180 */
11842     case 0xb: /* FCMLA, #270 */
11843         rot = extract32(opcode, 0, 2);
11844         switch (size) {
11845         case 1:
11846             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11847                               gen_helper_gvec_fcmlah);
11848             break;
11849         case 2:
11850             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11851                               gen_helper_gvec_fcmlas);
11852             break;
11853         case 3:
11854             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11855                               gen_helper_gvec_fcmlad);
11856             break;
11857         default:
11858             g_assert_not_reached();
11859         }
11860         return;
11861 
11862     case 0xc: /* FCADD, #90 */
11863     case 0xe: /* FCADD, #270 */
11864         rot = extract32(opcode, 1, 1);
11865         switch (size) {
11866         case 1:
11867             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11868                               gen_helper_gvec_fcaddh);
11869             break;
11870         case 2:
11871             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11872                               gen_helper_gvec_fcadds);
11873             break;
11874         case 3:
11875             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11876                               gen_helper_gvec_fcaddd);
11877             break;
11878         default:
11879             g_assert_not_reached();
11880         }
11881         return;
11882 
11883     case 0xd: /* BFMMLA */
11884         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11885         return;
11886     case 0xf:
11887         switch (size) {
11888         case 1: /* BFDOT */
11889             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11890             break;
11891         case 3: /* BFMLAL{B,T} */
11892             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11893                               gen_helper_gvec_bfmlal);
11894             break;
11895         default:
11896             g_assert_not_reached();
11897         }
11898         return;
11899 
11900     default:
11901         g_assert_not_reached();
11902     }
11903 }
11904 
11905 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11906                                   int size, int rn, int rd)
11907 {
11908     /* Handle 2-reg-misc ops which are widening (so each size element
11909      * in the source becomes a 2*size element in the destination.
11910      * The only instruction like this is FCVTL.
11911      */
11912     int pass;
11913 
11914     if (size == 3) {
11915         /* 32 -> 64 bit fp conversion */
11916         TCGv_i64 tcg_res[2];
11917         int srcelt = is_q ? 2 : 0;
11918 
11919         for (pass = 0; pass < 2; pass++) {
11920             TCGv_i32 tcg_op = tcg_temp_new_i32();
11921             tcg_res[pass] = tcg_temp_new_i64();
11922 
11923             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11924             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11925         }
11926         for (pass = 0; pass < 2; pass++) {
11927             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11928         }
11929     } else {
11930         /* 16 -> 32 bit fp conversion */
11931         int srcelt = is_q ? 4 : 0;
11932         TCGv_i32 tcg_res[4];
11933         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11934         TCGv_i32 ahp = get_ahp_flag();
11935 
11936         for (pass = 0; pass < 4; pass++) {
11937             tcg_res[pass] = tcg_temp_new_i32();
11938 
11939             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11940             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11941                                            fpst, ahp);
11942         }
11943         for (pass = 0; pass < 4; pass++) {
11944             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11945         }
11946     }
11947 }
11948 
11949 static void handle_rev(DisasContext *s, int opcode, bool u,
11950                        bool is_q, int size, int rn, int rd)
11951 {
11952     int op = (opcode << 1) | u;
11953     int opsz = op + size;
11954     int grp_size = 3 - opsz;
11955     int dsize = is_q ? 128 : 64;
11956     int i;
11957 
11958     if (opsz >= 3) {
11959         unallocated_encoding(s);
11960         return;
11961     }
11962 
11963     if (!fp_access_check(s)) {
11964         return;
11965     }
11966 
11967     if (size == 0) {
11968         /* Special case bytes, use bswap op on each group of elements */
11969         int groups = dsize / (8 << grp_size);
11970 
11971         for (i = 0; i < groups; i++) {
11972             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11973 
11974             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11975             switch (grp_size) {
11976             case MO_16:
11977                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11978                 break;
11979             case MO_32:
11980                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11981                 break;
11982             case MO_64:
11983                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11984                 break;
11985             default:
11986                 g_assert_not_reached();
11987             }
11988             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11989         }
11990         clear_vec_high(s, is_q, rd);
11991     } else {
11992         int revmask = (1 << grp_size) - 1;
11993         int esize = 8 << size;
11994         int elements = dsize / esize;
11995         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11996         TCGv_i64 tcg_rd[2];
11997 
11998         for (i = 0; i < 2; i++) {
11999             tcg_rd[i] = tcg_temp_new_i64();
12000             tcg_gen_movi_i64(tcg_rd[i], 0);
12001         }
12002 
12003         for (i = 0; i < elements; i++) {
12004             int e_rev = (i & 0xf) ^ revmask;
12005             int w = (e_rev * esize) / 64;
12006             int o = (e_rev * esize) % 64;
12007 
12008             read_vec_element(s, tcg_rn, rn, i, size);
12009             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12010         }
12011 
12012         for (i = 0; i < 2; i++) {
12013             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12014         }
12015         clear_vec_high(s, true, rd);
12016     }
12017 }
12018 
12019 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12020                                   bool is_q, int size, int rn, int rd)
12021 {
12022     /* Implement the pairwise operations from 2-misc:
12023      * SADDLP, UADDLP, SADALP, UADALP.
12024      * These all add pairs of elements in the input to produce a
12025      * double-width result element in the output (possibly accumulating).
12026      */
12027     bool accum = (opcode == 0x6);
12028     int maxpass = is_q ? 2 : 1;
12029     int pass;
12030     TCGv_i64 tcg_res[2];
12031 
12032     if (size == 2) {
12033         /* 32 + 32 -> 64 op */
12034         MemOp memop = size + (u ? 0 : MO_SIGN);
12035 
12036         for (pass = 0; pass < maxpass; pass++) {
12037             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12038             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12039 
12040             tcg_res[pass] = tcg_temp_new_i64();
12041 
12042             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12043             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12044             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12045             if (accum) {
12046                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12047                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12048             }
12049         }
12050     } else {
12051         for (pass = 0; pass < maxpass; pass++) {
12052             TCGv_i64 tcg_op = tcg_temp_new_i64();
12053             NeonGenOne64OpFn *genfn;
12054             static NeonGenOne64OpFn * const fns[2][2] = {
12055                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12056                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12057             };
12058 
12059             genfn = fns[size][u];
12060 
12061             tcg_res[pass] = tcg_temp_new_i64();
12062 
12063             read_vec_element(s, tcg_op, rn, pass, MO_64);
12064             genfn(tcg_res[pass], tcg_op);
12065 
12066             if (accum) {
12067                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12068                 if (size == 0) {
12069                     gen_helper_neon_addl_u16(tcg_res[pass],
12070                                              tcg_res[pass], tcg_op);
12071                 } else {
12072                     gen_helper_neon_addl_u32(tcg_res[pass],
12073                                              tcg_res[pass], tcg_op);
12074                 }
12075             }
12076         }
12077     }
12078     if (!is_q) {
12079         tcg_res[1] = tcg_constant_i64(0);
12080     }
12081     for (pass = 0; pass < 2; pass++) {
12082         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12083     }
12084 }
12085 
12086 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12087 {
12088     /* Implement SHLL and SHLL2 */
12089     int pass;
12090     int part = is_q ? 2 : 0;
12091     TCGv_i64 tcg_res[2];
12092 
12093     for (pass = 0; pass < 2; pass++) {
12094         static NeonGenWidenFn * const widenfns[3] = {
12095             gen_helper_neon_widen_u8,
12096             gen_helper_neon_widen_u16,
12097             tcg_gen_extu_i32_i64,
12098         };
12099         NeonGenWidenFn *widenfn = widenfns[size];
12100         TCGv_i32 tcg_op = tcg_temp_new_i32();
12101 
12102         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12103         tcg_res[pass] = tcg_temp_new_i64();
12104         widenfn(tcg_res[pass], tcg_op);
12105         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12106     }
12107 
12108     for (pass = 0; pass < 2; pass++) {
12109         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12110     }
12111 }
12112 
12113 /* AdvSIMD two reg misc
12114  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12115  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12116  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12117  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12118  */
12119 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12120 {
12121     int size = extract32(insn, 22, 2);
12122     int opcode = extract32(insn, 12, 5);
12123     bool u = extract32(insn, 29, 1);
12124     bool is_q = extract32(insn, 30, 1);
12125     int rn = extract32(insn, 5, 5);
12126     int rd = extract32(insn, 0, 5);
12127     bool need_fpstatus = false;
12128     int rmode = -1;
12129     TCGv_i32 tcg_rmode;
12130     TCGv_ptr tcg_fpstatus;
12131 
12132     switch (opcode) {
12133     case 0x0: /* REV64, REV32 */
12134     case 0x1: /* REV16 */
12135         handle_rev(s, opcode, u, is_q, size, rn, rd);
12136         return;
12137     case 0x5: /* CNT, NOT, RBIT */
12138         if (u && size == 0) {
12139             /* NOT */
12140             break;
12141         } else if (u && size == 1) {
12142             /* RBIT */
12143             break;
12144         } else if (!u && size == 0) {
12145             /* CNT */
12146             break;
12147         }
12148         unallocated_encoding(s);
12149         return;
12150     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12151     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12152         if (size == 3) {
12153             unallocated_encoding(s);
12154             return;
12155         }
12156         if (!fp_access_check(s)) {
12157             return;
12158         }
12159 
12160         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12161         return;
12162     case 0x4: /* CLS, CLZ */
12163         if (size == 3) {
12164             unallocated_encoding(s);
12165             return;
12166         }
12167         break;
12168     case 0x2: /* SADDLP, UADDLP */
12169     case 0x6: /* SADALP, UADALP */
12170         if (size == 3) {
12171             unallocated_encoding(s);
12172             return;
12173         }
12174         if (!fp_access_check(s)) {
12175             return;
12176         }
12177         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12178         return;
12179     case 0x13: /* SHLL, SHLL2 */
12180         if (u == 0 || size == 3) {
12181             unallocated_encoding(s);
12182             return;
12183         }
12184         if (!fp_access_check(s)) {
12185             return;
12186         }
12187         handle_shll(s, is_q, size, rn, rd);
12188         return;
12189     case 0xa: /* CMLT */
12190         if (u == 1) {
12191             unallocated_encoding(s);
12192             return;
12193         }
12194         /* fall through */
12195     case 0x8: /* CMGT, CMGE */
12196     case 0x9: /* CMEQ, CMLE */
12197     case 0xb: /* ABS, NEG */
12198         if (size == 3 && !is_q) {
12199             unallocated_encoding(s);
12200             return;
12201         }
12202         break;
12203     case 0x3: /* SUQADD, USQADD */
12204         if (size == 3 && !is_q) {
12205             unallocated_encoding(s);
12206             return;
12207         }
12208         if (!fp_access_check(s)) {
12209             return;
12210         }
12211         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12212         return;
12213     case 0x7: /* SQABS, SQNEG */
12214         if (size == 3 && !is_q) {
12215             unallocated_encoding(s);
12216             return;
12217         }
12218         break;
12219     case 0xc ... 0xf:
12220     case 0x16 ... 0x1f:
12221     {
12222         /* Floating point: U, size[1] and opcode indicate operation;
12223          * size[0] indicates single or double precision.
12224          */
12225         int is_double = extract32(size, 0, 1);
12226         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12227         size = is_double ? 3 : 2;
12228         switch (opcode) {
12229         case 0x2f: /* FABS */
12230         case 0x6f: /* FNEG */
12231             if (size == 3 && !is_q) {
12232                 unallocated_encoding(s);
12233                 return;
12234             }
12235             break;
12236         case 0x1d: /* SCVTF */
12237         case 0x5d: /* UCVTF */
12238         {
12239             bool is_signed = (opcode == 0x1d) ? true : false;
12240             int elements = is_double ? 2 : is_q ? 4 : 2;
12241             if (is_double && !is_q) {
12242                 unallocated_encoding(s);
12243                 return;
12244             }
12245             if (!fp_access_check(s)) {
12246                 return;
12247             }
12248             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12249             return;
12250         }
12251         case 0x2c: /* FCMGT (zero) */
12252         case 0x2d: /* FCMEQ (zero) */
12253         case 0x2e: /* FCMLT (zero) */
12254         case 0x6c: /* FCMGE (zero) */
12255         case 0x6d: /* FCMLE (zero) */
12256             if (size == 3 && !is_q) {
12257                 unallocated_encoding(s);
12258                 return;
12259             }
12260             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12261             return;
12262         case 0x7f: /* FSQRT */
12263             if (size == 3 && !is_q) {
12264                 unallocated_encoding(s);
12265                 return;
12266             }
12267             break;
12268         case 0x1a: /* FCVTNS */
12269         case 0x1b: /* FCVTMS */
12270         case 0x3a: /* FCVTPS */
12271         case 0x3b: /* FCVTZS */
12272         case 0x5a: /* FCVTNU */
12273         case 0x5b: /* FCVTMU */
12274         case 0x7a: /* FCVTPU */
12275         case 0x7b: /* FCVTZU */
12276             need_fpstatus = true;
12277             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12278             if (size == 3 && !is_q) {
12279                 unallocated_encoding(s);
12280                 return;
12281             }
12282             break;
12283         case 0x5c: /* FCVTAU */
12284         case 0x1c: /* FCVTAS */
12285             need_fpstatus = true;
12286             rmode = FPROUNDING_TIEAWAY;
12287             if (size == 3 && !is_q) {
12288                 unallocated_encoding(s);
12289                 return;
12290             }
12291             break;
12292         case 0x3c: /* URECPE */
12293             if (size == 3) {
12294                 unallocated_encoding(s);
12295                 return;
12296             }
12297             /* fall through */
12298         case 0x3d: /* FRECPE */
12299         case 0x7d: /* FRSQRTE */
12300             if (size == 3 && !is_q) {
12301                 unallocated_encoding(s);
12302                 return;
12303             }
12304             if (!fp_access_check(s)) {
12305                 return;
12306             }
12307             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12308             return;
12309         case 0x56: /* FCVTXN, FCVTXN2 */
12310             if (size == 2) {
12311                 unallocated_encoding(s);
12312                 return;
12313             }
12314             /* fall through */
12315         case 0x16: /* FCVTN, FCVTN2 */
12316             /* handle_2misc_narrow does a 2*size -> size operation, but these
12317              * instructions encode the source size rather than dest size.
12318              */
12319             if (!fp_access_check(s)) {
12320                 return;
12321             }
12322             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12323             return;
12324         case 0x36: /* BFCVTN, BFCVTN2 */
12325             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12326                 unallocated_encoding(s);
12327                 return;
12328             }
12329             if (!fp_access_check(s)) {
12330                 return;
12331             }
12332             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12333             return;
12334         case 0x17: /* FCVTL, FCVTL2 */
12335             if (!fp_access_check(s)) {
12336                 return;
12337             }
12338             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12339             return;
12340         case 0x18: /* FRINTN */
12341         case 0x19: /* FRINTM */
12342         case 0x38: /* FRINTP */
12343         case 0x39: /* FRINTZ */
12344             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12345             /* fall through */
12346         case 0x59: /* FRINTX */
12347         case 0x79: /* FRINTI */
12348             need_fpstatus = true;
12349             if (size == 3 && !is_q) {
12350                 unallocated_encoding(s);
12351                 return;
12352             }
12353             break;
12354         case 0x58: /* FRINTA */
12355             rmode = FPROUNDING_TIEAWAY;
12356             need_fpstatus = true;
12357             if (size == 3 && !is_q) {
12358                 unallocated_encoding(s);
12359                 return;
12360             }
12361             break;
12362         case 0x7c: /* URSQRTE */
12363             if (size == 3) {
12364                 unallocated_encoding(s);
12365                 return;
12366             }
12367             break;
12368         case 0x1e: /* FRINT32Z */
12369         case 0x1f: /* FRINT64Z */
12370             rmode = FPROUNDING_ZERO;
12371             /* fall through */
12372         case 0x5e: /* FRINT32X */
12373         case 0x5f: /* FRINT64X */
12374             need_fpstatus = true;
12375             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12376                 unallocated_encoding(s);
12377                 return;
12378             }
12379             break;
12380         default:
12381             unallocated_encoding(s);
12382             return;
12383         }
12384         break;
12385     }
12386     default:
12387         unallocated_encoding(s);
12388         return;
12389     }
12390 
12391     if (!fp_access_check(s)) {
12392         return;
12393     }
12394 
12395     if (need_fpstatus || rmode >= 0) {
12396         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12397     } else {
12398         tcg_fpstatus = NULL;
12399     }
12400     if (rmode >= 0) {
12401         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12402     } else {
12403         tcg_rmode = NULL;
12404     }
12405 
12406     switch (opcode) {
12407     case 0x5:
12408         if (u && size == 0) { /* NOT */
12409             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12410             return;
12411         }
12412         break;
12413     case 0x8: /* CMGT, CMGE */
12414         if (u) {
12415             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12416         } else {
12417             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12418         }
12419         return;
12420     case 0x9: /* CMEQ, CMLE */
12421         if (u) {
12422             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12423         } else {
12424             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12425         }
12426         return;
12427     case 0xa: /* CMLT */
12428         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12429         return;
12430     case 0xb:
12431         if (u) { /* ABS, NEG */
12432             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12433         } else {
12434             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12435         }
12436         return;
12437     }
12438 
12439     if (size == 3) {
12440         /* All 64-bit element operations can be shared with scalar 2misc */
12441         int pass;
12442 
12443         /* Coverity claims (size == 3 && !is_q) has been eliminated
12444          * from all paths leading to here.
12445          */
12446         tcg_debug_assert(is_q);
12447         for (pass = 0; pass < 2; pass++) {
12448             TCGv_i64 tcg_op = tcg_temp_new_i64();
12449             TCGv_i64 tcg_res = tcg_temp_new_i64();
12450 
12451             read_vec_element(s, tcg_op, rn, pass, MO_64);
12452 
12453             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12454                             tcg_rmode, tcg_fpstatus);
12455 
12456             write_vec_element(s, tcg_res, rd, pass, MO_64);
12457         }
12458     } else {
12459         int pass;
12460 
12461         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12462             TCGv_i32 tcg_op = tcg_temp_new_i32();
12463             TCGv_i32 tcg_res = tcg_temp_new_i32();
12464 
12465             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12466 
12467             if (size == 2) {
12468                 /* Special cases for 32 bit elements */
12469                 switch (opcode) {
12470                 case 0x4: /* CLS */
12471                     if (u) {
12472                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12473                     } else {
12474                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12475                     }
12476                     break;
12477                 case 0x7: /* SQABS, SQNEG */
12478                     if (u) {
12479                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12480                     } else {
12481                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12482                     }
12483                     break;
12484                 case 0x2f: /* FABS */
12485                     gen_helper_vfp_abss(tcg_res, tcg_op);
12486                     break;
12487                 case 0x6f: /* FNEG */
12488                     gen_helper_vfp_negs(tcg_res, tcg_op);
12489                     break;
12490                 case 0x7f: /* FSQRT */
12491                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12492                     break;
12493                 case 0x1a: /* FCVTNS */
12494                 case 0x1b: /* FCVTMS */
12495                 case 0x1c: /* FCVTAS */
12496                 case 0x3a: /* FCVTPS */
12497                 case 0x3b: /* FCVTZS */
12498                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12499                                          tcg_constant_i32(0), tcg_fpstatus);
12500                     break;
12501                 case 0x5a: /* FCVTNU */
12502                 case 0x5b: /* FCVTMU */
12503                 case 0x5c: /* FCVTAU */
12504                 case 0x7a: /* FCVTPU */
12505                 case 0x7b: /* FCVTZU */
12506                     gen_helper_vfp_touls(tcg_res, tcg_op,
12507                                          tcg_constant_i32(0), tcg_fpstatus);
12508                     break;
12509                 case 0x18: /* FRINTN */
12510                 case 0x19: /* FRINTM */
12511                 case 0x38: /* FRINTP */
12512                 case 0x39: /* FRINTZ */
12513                 case 0x58: /* FRINTA */
12514                 case 0x79: /* FRINTI */
12515                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12516                     break;
12517                 case 0x59: /* FRINTX */
12518                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12519                     break;
12520                 case 0x7c: /* URSQRTE */
12521                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12522                     break;
12523                 case 0x1e: /* FRINT32Z */
12524                 case 0x5e: /* FRINT32X */
12525                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12526                     break;
12527                 case 0x1f: /* FRINT64Z */
12528                 case 0x5f: /* FRINT64X */
12529                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12530                     break;
12531                 default:
12532                     g_assert_not_reached();
12533                 }
12534             } else {
12535                 /* Use helpers for 8 and 16 bit elements */
12536                 switch (opcode) {
12537                 case 0x5: /* CNT, RBIT */
12538                     /* For these two insns size is part of the opcode specifier
12539                      * (handled earlier); they always operate on byte elements.
12540                      */
12541                     if (u) {
12542                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12543                     } else {
12544                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12545                     }
12546                     break;
12547                 case 0x7: /* SQABS, SQNEG */
12548                 {
12549                     NeonGenOneOpEnvFn *genfn;
12550                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12551                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12552                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12553                     };
12554                     genfn = fns[size][u];
12555                     genfn(tcg_res, cpu_env, tcg_op);
12556                     break;
12557                 }
12558                 case 0x4: /* CLS, CLZ */
12559                     if (u) {
12560                         if (size == 0) {
12561                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12562                         } else {
12563                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12564                         }
12565                     } else {
12566                         if (size == 0) {
12567                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12568                         } else {
12569                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12570                         }
12571                     }
12572                     break;
12573                 default:
12574                     g_assert_not_reached();
12575                 }
12576             }
12577 
12578             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12579         }
12580     }
12581     clear_vec_high(s, is_q, rd);
12582 
12583     if (tcg_rmode) {
12584         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12585     }
12586 }
12587 
12588 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12589  *
12590  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12591  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12592  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12593  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12594  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12595  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12596  *
12597  * This actually covers two groups where scalar access is governed by
12598  * bit 28. A bunch of the instructions (float to integral) only exist
12599  * in the vector form and are un-allocated for the scalar decode. Also
12600  * in the scalar decode Q is always 1.
12601  */
12602 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12603 {
12604     int fpop, opcode, a, u;
12605     int rn, rd;
12606     bool is_q;
12607     bool is_scalar;
12608     bool only_in_vector = false;
12609 
12610     int pass;
12611     TCGv_i32 tcg_rmode = NULL;
12612     TCGv_ptr tcg_fpstatus = NULL;
12613     bool need_fpst = true;
12614     int rmode = -1;
12615 
12616     if (!dc_isar_feature(aa64_fp16, s)) {
12617         unallocated_encoding(s);
12618         return;
12619     }
12620 
12621     rd = extract32(insn, 0, 5);
12622     rn = extract32(insn, 5, 5);
12623 
12624     a = extract32(insn, 23, 1);
12625     u = extract32(insn, 29, 1);
12626     is_scalar = extract32(insn, 28, 1);
12627     is_q = extract32(insn, 30, 1);
12628 
12629     opcode = extract32(insn, 12, 5);
12630     fpop = deposit32(opcode, 5, 1, a);
12631     fpop = deposit32(fpop, 6, 1, u);
12632 
12633     switch (fpop) {
12634     case 0x1d: /* SCVTF */
12635     case 0x5d: /* UCVTF */
12636     {
12637         int elements;
12638 
12639         if (is_scalar) {
12640             elements = 1;
12641         } else {
12642             elements = (is_q ? 8 : 4);
12643         }
12644 
12645         if (!fp_access_check(s)) {
12646             return;
12647         }
12648         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12649         return;
12650     }
12651     break;
12652     case 0x2c: /* FCMGT (zero) */
12653     case 0x2d: /* FCMEQ (zero) */
12654     case 0x2e: /* FCMLT (zero) */
12655     case 0x6c: /* FCMGE (zero) */
12656     case 0x6d: /* FCMLE (zero) */
12657         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12658         return;
12659     case 0x3d: /* FRECPE */
12660     case 0x3f: /* FRECPX */
12661         break;
12662     case 0x18: /* FRINTN */
12663         only_in_vector = true;
12664         rmode = FPROUNDING_TIEEVEN;
12665         break;
12666     case 0x19: /* FRINTM */
12667         only_in_vector = true;
12668         rmode = FPROUNDING_NEGINF;
12669         break;
12670     case 0x38: /* FRINTP */
12671         only_in_vector = true;
12672         rmode = FPROUNDING_POSINF;
12673         break;
12674     case 0x39: /* FRINTZ */
12675         only_in_vector = true;
12676         rmode = FPROUNDING_ZERO;
12677         break;
12678     case 0x58: /* FRINTA */
12679         only_in_vector = true;
12680         rmode = FPROUNDING_TIEAWAY;
12681         break;
12682     case 0x59: /* FRINTX */
12683     case 0x79: /* FRINTI */
12684         only_in_vector = true;
12685         /* current rounding mode */
12686         break;
12687     case 0x1a: /* FCVTNS */
12688         rmode = FPROUNDING_TIEEVEN;
12689         break;
12690     case 0x1b: /* FCVTMS */
12691         rmode = FPROUNDING_NEGINF;
12692         break;
12693     case 0x1c: /* FCVTAS */
12694         rmode = FPROUNDING_TIEAWAY;
12695         break;
12696     case 0x3a: /* FCVTPS */
12697         rmode = FPROUNDING_POSINF;
12698         break;
12699     case 0x3b: /* FCVTZS */
12700         rmode = FPROUNDING_ZERO;
12701         break;
12702     case 0x5a: /* FCVTNU */
12703         rmode = FPROUNDING_TIEEVEN;
12704         break;
12705     case 0x5b: /* FCVTMU */
12706         rmode = FPROUNDING_NEGINF;
12707         break;
12708     case 0x5c: /* FCVTAU */
12709         rmode = FPROUNDING_TIEAWAY;
12710         break;
12711     case 0x7a: /* FCVTPU */
12712         rmode = FPROUNDING_POSINF;
12713         break;
12714     case 0x7b: /* FCVTZU */
12715         rmode = FPROUNDING_ZERO;
12716         break;
12717     case 0x2f: /* FABS */
12718     case 0x6f: /* FNEG */
12719         need_fpst = false;
12720         break;
12721     case 0x7d: /* FRSQRTE */
12722     case 0x7f: /* FSQRT (vector) */
12723         break;
12724     default:
12725         unallocated_encoding(s);
12726         return;
12727     }
12728 
12729 
12730     /* Check additional constraints for the scalar encoding */
12731     if (is_scalar) {
12732         if (!is_q) {
12733             unallocated_encoding(s);
12734             return;
12735         }
12736         /* FRINTxx is only in the vector form */
12737         if (only_in_vector) {
12738             unallocated_encoding(s);
12739             return;
12740         }
12741     }
12742 
12743     if (!fp_access_check(s)) {
12744         return;
12745     }
12746 
12747     if (rmode >= 0 || need_fpst) {
12748         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12749     }
12750 
12751     if (rmode >= 0) {
12752         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12753     }
12754 
12755     if (is_scalar) {
12756         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12757         TCGv_i32 tcg_res = tcg_temp_new_i32();
12758 
12759         switch (fpop) {
12760         case 0x1a: /* FCVTNS */
12761         case 0x1b: /* FCVTMS */
12762         case 0x1c: /* FCVTAS */
12763         case 0x3a: /* FCVTPS */
12764         case 0x3b: /* FCVTZS */
12765             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12766             break;
12767         case 0x3d: /* FRECPE */
12768             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12769             break;
12770         case 0x3f: /* FRECPX */
12771             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12772             break;
12773         case 0x5a: /* FCVTNU */
12774         case 0x5b: /* FCVTMU */
12775         case 0x5c: /* FCVTAU */
12776         case 0x7a: /* FCVTPU */
12777         case 0x7b: /* FCVTZU */
12778             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12779             break;
12780         case 0x6f: /* FNEG */
12781             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12782             break;
12783         case 0x7d: /* FRSQRTE */
12784             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12785             break;
12786         default:
12787             g_assert_not_reached();
12788         }
12789 
12790         /* limit any sign extension going on */
12791         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12792         write_fp_sreg(s, rd, tcg_res);
12793     } else {
12794         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12795             TCGv_i32 tcg_op = tcg_temp_new_i32();
12796             TCGv_i32 tcg_res = tcg_temp_new_i32();
12797 
12798             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12799 
12800             switch (fpop) {
12801             case 0x1a: /* FCVTNS */
12802             case 0x1b: /* FCVTMS */
12803             case 0x1c: /* FCVTAS */
12804             case 0x3a: /* FCVTPS */
12805             case 0x3b: /* FCVTZS */
12806                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12807                 break;
12808             case 0x3d: /* FRECPE */
12809                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12810                 break;
12811             case 0x5a: /* FCVTNU */
12812             case 0x5b: /* FCVTMU */
12813             case 0x5c: /* FCVTAU */
12814             case 0x7a: /* FCVTPU */
12815             case 0x7b: /* FCVTZU */
12816                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12817                 break;
12818             case 0x18: /* FRINTN */
12819             case 0x19: /* FRINTM */
12820             case 0x38: /* FRINTP */
12821             case 0x39: /* FRINTZ */
12822             case 0x58: /* FRINTA */
12823             case 0x79: /* FRINTI */
12824                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12825                 break;
12826             case 0x59: /* FRINTX */
12827                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12828                 break;
12829             case 0x2f: /* FABS */
12830                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12831                 break;
12832             case 0x6f: /* FNEG */
12833                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12834                 break;
12835             case 0x7d: /* FRSQRTE */
12836                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12837                 break;
12838             case 0x7f: /* FSQRT */
12839                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12840                 break;
12841             default:
12842                 g_assert_not_reached();
12843             }
12844 
12845             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12846         }
12847 
12848         clear_vec_high(s, is_q, rd);
12849     }
12850 
12851     if (tcg_rmode) {
12852         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12853     }
12854 }
12855 
12856 /* AdvSIMD scalar x indexed element
12857  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12858  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12859  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12860  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12861  * AdvSIMD vector x indexed element
12862  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12863  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12864  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12865  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12866  */
12867 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12868 {
12869     /* This encoding has two kinds of instruction:
12870      *  normal, where we perform elt x idxelt => elt for each
12871      *     element in the vector
12872      *  long, where we perform elt x idxelt and generate a result of
12873      *     double the width of the input element
12874      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12875      */
12876     bool is_scalar = extract32(insn, 28, 1);
12877     bool is_q = extract32(insn, 30, 1);
12878     bool u = extract32(insn, 29, 1);
12879     int size = extract32(insn, 22, 2);
12880     int l = extract32(insn, 21, 1);
12881     int m = extract32(insn, 20, 1);
12882     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12883     int rm = extract32(insn, 16, 4);
12884     int opcode = extract32(insn, 12, 4);
12885     int h = extract32(insn, 11, 1);
12886     int rn = extract32(insn, 5, 5);
12887     int rd = extract32(insn, 0, 5);
12888     bool is_long = false;
12889     int is_fp = 0;
12890     bool is_fp16 = false;
12891     int index;
12892     TCGv_ptr fpst;
12893 
12894     switch (16 * u + opcode) {
12895     case 0x08: /* MUL */
12896     case 0x10: /* MLA */
12897     case 0x14: /* MLS */
12898         if (is_scalar) {
12899             unallocated_encoding(s);
12900             return;
12901         }
12902         break;
12903     case 0x02: /* SMLAL, SMLAL2 */
12904     case 0x12: /* UMLAL, UMLAL2 */
12905     case 0x06: /* SMLSL, SMLSL2 */
12906     case 0x16: /* UMLSL, UMLSL2 */
12907     case 0x0a: /* SMULL, SMULL2 */
12908     case 0x1a: /* UMULL, UMULL2 */
12909         if (is_scalar) {
12910             unallocated_encoding(s);
12911             return;
12912         }
12913         is_long = true;
12914         break;
12915     case 0x03: /* SQDMLAL, SQDMLAL2 */
12916     case 0x07: /* SQDMLSL, SQDMLSL2 */
12917     case 0x0b: /* SQDMULL, SQDMULL2 */
12918         is_long = true;
12919         break;
12920     case 0x0c: /* SQDMULH */
12921     case 0x0d: /* SQRDMULH */
12922         break;
12923     case 0x01: /* FMLA */
12924     case 0x05: /* FMLS */
12925     case 0x09: /* FMUL */
12926     case 0x19: /* FMULX */
12927         is_fp = 1;
12928         break;
12929     case 0x1d: /* SQRDMLAH */
12930     case 0x1f: /* SQRDMLSH */
12931         if (!dc_isar_feature(aa64_rdm, s)) {
12932             unallocated_encoding(s);
12933             return;
12934         }
12935         break;
12936     case 0x0e: /* SDOT */
12937     case 0x1e: /* UDOT */
12938         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12939             unallocated_encoding(s);
12940             return;
12941         }
12942         break;
12943     case 0x0f:
12944         switch (size) {
12945         case 0: /* SUDOT */
12946         case 2: /* USDOT */
12947             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12948                 unallocated_encoding(s);
12949                 return;
12950             }
12951             size = MO_32;
12952             break;
12953         case 1: /* BFDOT */
12954             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12955                 unallocated_encoding(s);
12956                 return;
12957             }
12958             size = MO_32;
12959             break;
12960         case 3: /* BFMLAL{B,T} */
12961             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12962                 unallocated_encoding(s);
12963                 return;
12964             }
12965             /* can't set is_fp without other incorrect size checks */
12966             size = MO_16;
12967             break;
12968         default:
12969             unallocated_encoding(s);
12970             return;
12971         }
12972         break;
12973     case 0x11: /* FCMLA #0 */
12974     case 0x13: /* FCMLA #90 */
12975     case 0x15: /* FCMLA #180 */
12976     case 0x17: /* FCMLA #270 */
12977         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12978             unallocated_encoding(s);
12979             return;
12980         }
12981         is_fp = 2;
12982         break;
12983     case 0x00: /* FMLAL */
12984     case 0x04: /* FMLSL */
12985     case 0x18: /* FMLAL2 */
12986     case 0x1c: /* FMLSL2 */
12987         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12988             unallocated_encoding(s);
12989             return;
12990         }
12991         size = MO_16;
12992         /* is_fp, but we pass cpu_env not fp_status.  */
12993         break;
12994     default:
12995         unallocated_encoding(s);
12996         return;
12997     }
12998 
12999     switch (is_fp) {
13000     case 1: /* normal fp */
13001         /* convert insn encoded size to MemOp size */
13002         switch (size) {
13003         case 0: /* half-precision */
13004             size = MO_16;
13005             is_fp16 = true;
13006             break;
13007         case MO_32: /* single precision */
13008         case MO_64: /* double precision */
13009             break;
13010         default:
13011             unallocated_encoding(s);
13012             return;
13013         }
13014         break;
13015 
13016     case 2: /* complex fp */
13017         /* Each indexable element is a complex pair.  */
13018         size += 1;
13019         switch (size) {
13020         case MO_32:
13021             if (h && !is_q) {
13022                 unallocated_encoding(s);
13023                 return;
13024             }
13025             is_fp16 = true;
13026             break;
13027         case MO_64:
13028             break;
13029         default:
13030             unallocated_encoding(s);
13031             return;
13032         }
13033         break;
13034 
13035     default: /* integer */
13036         switch (size) {
13037         case MO_8:
13038         case MO_64:
13039             unallocated_encoding(s);
13040             return;
13041         }
13042         break;
13043     }
13044     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13045         unallocated_encoding(s);
13046         return;
13047     }
13048 
13049     /* Given MemOp size, adjust register and indexing.  */
13050     switch (size) {
13051     case MO_16:
13052         index = h << 2 | l << 1 | m;
13053         break;
13054     case MO_32:
13055         index = h << 1 | l;
13056         rm |= m << 4;
13057         break;
13058     case MO_64:
13059         if (l || !is_q) {
13060             unallocated_encoding(s);
13061             return;
13062         }
13063         index = h;
13064         rm |= m << 4;
13065         break;
13066     default:
13067         g_assert_not_reached();
13068     }
13069 
13070     if (!fp_access_check(s)) {
13071         return;
13072     }
13073 
13074     if (is_fp) {
13075         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13076     } else {
13077         fpst = NULL;
13078     }
13079 
13080     switch (16 * u + opcode) {
13081     case 0x0e: /* SDOT */
13082     case 0x1e: /* UDOT */
13083         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13084                          u ? gen_helper_gvec_udot_idx_b
13085                          : gen_helper_gvec_sdot_idx_b);
13086         return;
13087     case 0x0f:
13088         switch (extract32(insn, 22, 2)) {
13089         case 0: /* SUDOT */
13090             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13091                              gen_helper_gvec_sudot_idx_b);
13092             return;
13093         case 1: /* BFDOT */
13094             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13095                              gen_helper_gvec_bfdot_idx);
13096             return;
13097         case 2: /* USDOT */
13098             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13099                              gen_helper_gvec_usdot_idx_b);
13100             return;
13101         case 3: /* BFMLAL{B,T} */
13102             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13103                               gen_helper_gvec_bfmlal_idx);
13104             return;
13105         }
13106         g_assert_not_reached();
13107     case 0x11: /* FCMLA #0 */
13108     case 0x13: /* FCMLA #90 */
13109     case 0x15: /* FCMLA #180 */
13110     case 0x17: /* FCMLA #270 */
13111         {
13112             int rot = extract32(insn, 13, 2);
13113             int data = (index << 2) | rot;
13114             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13115                                vec_full_reg_offset(s, rn),
13116                                vec_full_reg_offset(s, rm),
13117                                vec_full_reg_offset(s, rd), fpst,
13118                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13119                                size == MO_64
13120                                ? gen_helper_gvec_fcmlas_idx
13121                                : gen_helper_gvec_fcmlah_idx);
13122         }
13123         return;
13124 
13125     case 0x00: /* FMLAL */
13126     case 0x04: /* FMLSL */
13127     case 0x18: /* FMLAL2 */
13128     case 0x1c: /* FMLSL2 */
13129         {
13130             int is_s = extract32(opcode, 2, 1);
13131             int is_2 = u;
13132             int data = (index << 2) | (is_2 << 1) | is_s;
13133             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13134                                vec_full_reg_offset(s, rn),
13135                                vec_full_reg_offset(s, rm), cpu_env,
13136                                is_q ? 16 : 8, vec_full_reg_size(s),
13137                                data, gen_helper_gvec_fmlal_idx_a64);
13138         }
13139         return;
13140 
13141     case 0x08: /* MUL */
13142         if (!is_long && !is_scalar) {
13143             static gen_helper_gvec_3 * const fns[3] = {
13144                 gen_helper_gvec_mul_idx_h,
13145                 gen_helper_gvec_mul_idx_s,
13146                 gen_helper_gvec_mul_idx_d,
13147             };
13148             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13149                                vec_full_reg_offset(s, rn),
13150                                vec_full_reg_offset(s, rm),
13151                                is_q ? 16 : 8, vec_full_reg_size(s),
13152                                index, fns[size - 1]);
13153             return;
13154         }
13155         break;
13156 
13157     case 0x10: /* MLA */
13158         if (!is_long && !is_scalar) {
13159             static gen_helper_gvec_4 * const fns[3] = {
13160                 gen_helper_gvec_mla_idx_h,
13161                 gen_helper_gvec_mla_idx_s,
13162                 gen_helper_gvec_mla_idx_d,
13163             };
13164             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13165                                vec_full_reg_offset(s, rn),
13166                                vec_full_reg_offset(s, rm),
13167                                vec_full_reg_offset(s, rd),
13168                                is_q ? 16 : 8, vec_full_reg_size(s),
13169                                index, fns[size - 1]);
13170             return;
13171         }
13172         break;
13173 
13174     case 0x14: /* MLS */
13175         if (!is_long && !is_scalar) {
13176             static gen_helper_gvec_4 * const fns[3] = {
13177                 gen_helper_gvec_mls_idx_h,
13178                 gen_helper_gvec_mls_idx_s,
13179                 gen_helper_gvec_mls_idx_d,
13180             };
13181             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13182                                vec_full_reg_offset(s, rn),
13183                                vec_full_reg_offset(s, rm),
13184                                vec_full_reg_offset(s, rd),
13185                                is_q ? 16 : 8, vec_full_reg_size(s),
13186                                index, fns[size - 1]);
13187             return;
13188         }
13189         break;
13190     }
13191 
13192     if (size == 3) {
13193         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13194         int pass;
13195 
13196         assert(is_fp && is_q && !is_long);
13197 
13198         read_vec_element(s, tcg_idx, rm, index, MO_64);
13199 
13200         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13201             TCGv_i64 tcg_op = tcg_temp_new_i64();
13202             TCGv_i64 tcg_res = tcg_temp_new_i64();
13203 
13204             read_vec_element(s, tcg_op, rn, pass, MO_64);
13205 
13206             switch (16 * u + opcode) {
13207             case 0x05: /* FMLS */
13208                 /* As usual for ARM, separate negation for fused multiply-add */
13209                 gen_helper_vfp_negd(tcg_op, tcg_op);
13210                 /* fall through */
13211             case 0x01: /* FMLA */
13212                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13213                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13214                 break;
13215             case 0x09: /* FMUL */
13216                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13217                 break;
13218             case 0x19: /* FMULX */
13219                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13220                 break;
13221             default:
13222                 g_assert_not_reached();
13223             }
13224 
13225             write_vec_element(s, tcg_res, rd, pass, MO_64);
13226         }
13227 
13228         clear_vec_high(s, !is_scalar, rd);
13229     } else if (!is_long) {
13230         /* 32 bit floating point, or 16 or 32 bit integer.
13231          * For the 16 bit scalar case we use the usual Neon helpers and
13232          * rely on the fact that 0 op 0 == 0 with no side effects.
13233          */
13234         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13235         int pass, maxpasses;
13236 
13237         if (is_scalar) {
13238             maxpasses = 1;
13239         } else {
13240             maxpasses = is_q ? 4 : 2;
13241         }
13242 
13243         read_vec_element_i32(s, tcg_idx, rm, index, size);
13244 
13245         if (size == 1 && !is_scalar) {
13246             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13247              * the index into both halves of the 32 bit tcg_idx and then use
13248              * the usual Neon helpers.
13249              */
13250             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13251         }
13252 
13253         for (pass = 0; pass < maxpasses; pass++) {
13254             TCGv_i32 tcg_op = tcg_temp_new_i32();
13255             TCGv_i32 tcg_res = tcg_temp_new_i32();
13256 
13257             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13258 
13259             switch (16 * u + opcode) {
13260             case 0x08: /* MUL */
13261             case 0x10: /* MLA */
13262             case 0x14: /* MLS */
13263             {
13264                 static NeonGenTwoOpFn * const fns[2][2] = {
13265                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13266                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13267                 };
13268                 NeonGenTwoOpFn *genfn;
13269                 bool is_sub = opcode == 0x4;
13270 
13271                 if (size == 1) {
13272                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13273                 } else {
13274                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13275                 }
13276                 if (opcode == 0x8) {
13277                     break;
13278                 }
13279                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13280                 genfn = fns[size - 1][is_sub];
13281                 genfn(tcg_res, tcg_op, tcg_res);
13282                 break;
13283             }
13284             case 0x05: /* FMLS */
13285             case 0x01: /* FMLA */
13286                 read_vec_element_i32(s, tcg_res, rd, pass,
13287                                      is_scalar ? size : MO_32);
13288                 switch (size) {
13289                 case 1:
13290                     if (opcode == 0x5) {
13291                         /* As usual for ARM, separate negation for fused
13292                          * multiply-add */
13293                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13294                     }
13295                     if (is_scalar) {
13296                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13297                                                    tcg_res, fpst);
13298                     } else {
13299                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13300                                                     tcg_res, fpst);
13301                     }
13302                     break;
13303                 case 2:
13304                     if (opcode == 0x5) {
13305                         /* As usual for ARM, separate negation for
13306                          * fused multiply-add */
13307                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13308                     }
13309                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13310                                            tcg_res, fpst);
13311                     break;
13312                 default:
13313                     g_assert_not_reached();
13314                 }
13315                 break;
13316             case 0x09: /* FMUL */
13317                 switch (size) {
13318                 case 1:
13319                     if (is_scalar) {
13320                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13321                                                 tcg_idx, fpst);
13322                     } else {
13323                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13324                                                  tcg_idx, fpst);
13325                     }
13326                     break;
13327                 case 2:
13328                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13329                     break;
13330                 default:
13331                     g_assert_not_reached();
13332                 }
13333                 break;
13334             case 0x19: /* FMULX */
13335                 switch (size) {
13336                 case 1:
13337                     if (is_scalar) {
13338                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13339                                                  tcg_idx, fpst);
13340                     } else {
13341                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13342                                                   tcg_idx, fpst);
13343                     }
13344                     break;
13345                 case 2:
13346                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13347                     break;
13348                 default:
13349                     g_assert_not_reached();
13350                 }
13351                 break;
13352             case 0x0c: /* SQDMULH */
13353                 if (size == 1) {
13354                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13355                                                tcg_op, tcg_idx);
13356                 } else {
13357                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13358                                                tcg_op, tcg_idx);
13359                 }
13360                 break;
13361             case 0x0d: /* SQRDMULH */
13362                 if (size == 1) {
13363                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13364                                                 tcg_op, tcg_idx);
13365                 } else {
13366                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13367                                                 tcg_op, tcg_idx);
13368                 }
13369                 break;
13370             case 0x1d: /* SQRDMLAH */
13371                 read_vec_element_i32(s, tcg_res, rd, pass,
13372                                      is_scalar ? size : MO_32);
13373                 if (size == 1) {
13374                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13375                                                 tcg_op, tcg_idx, tcg_res);
13376                 } else {
13377                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13378                                                 tcg_op, tcg_idx, tcg_res);
13379                 }
13380                 break;
13381             case 0x1f: /* SQRDMLSH */
13382                 read_vec_element_i32(s, tcg_res, rd, pass,
13383                                      is_scalar ? size : MO_32);
13384                 if (size == 1) {
13385                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13386                                                 tcg_op, tcg_idx, tcg_res);
13387                 } else {
13388                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13389                                                 tcg_op, tcg_idx, tcg_res);
13390                 }
13391                 break;
13392             default:
13393                 g_assert_not_reached();
13394             }
13395 
13396             if (is_scalar) {
13397                 write_fp_sreg(s, rd, tcg_res);
13398             } else {
13399                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13400             }
13401         }
13402 
13403         clear_vec_high(s, is_q, rd);
13404     } else {
13405         /* long ops: 16x16->32 or 32x32->64 */
13406         TCGv_i64 tcg_res[2];
13407         int pass;
13408         bool satop = extract32(opcode, 0, 1);
13409         MemOp memop = MO_32;
13410 
13411         if (satop || !u) {
13412             memop |= MO_SIGN;
13413         }
13414 
13415         if (size == 2) {
13416             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13417 
13418             read_vec_element(s, tcg_idx, rm, index, memop);
13419 
13420             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13421                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13422                 TCGv_i64 tcg_passres;
13423                 int passelt;
13424 
13425                 if (is_scalar) {
13426                     passelt = 0;
13427                 } else {
13428                     passelt = pass + (is_q * 2);
13429                 }
13430 
13431                 read_vec_element(s, tcg_op, rn, passelt, memop);
13432 
13433                 tcg_res[pass] = tcg_temp_new_i64();
13434 
13435                 if (opcode == 0xa || opcode == 0xb) {
13436                     /* Non-accumulating ops */
13437                     tcg_passres = tcg_res[pass];
13438                 } else {
13439                     tcg_passres = tcg_temp_new_i64();
13440                 }
13441 
13442                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13443 
13444                 if (satop) {
13445                     /* saturating, doubling */
13446                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13447                                                       tcg_passres, tcg_passres);
13448                 }
13449 
13450                 if (opcode == 0xa || opcode == 0xb) {
13451                     continue;
13452                 }
13453 
13454                 /* Accumulating op: handle accumulate step */
13455                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13456 
13457                 switch (opcode) {
13458                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13459                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13460                     break;
13461                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13462                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13463                     break;
13464                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13465                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13466                     /* fall through */
13467                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13468                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13469                                                       tcg_res[pass],
13470                                                       tcg_passres);
13471                     break;
13472                 default:
13473                     g_assert_not_reached();
13474                 }
13475             }
13476 
13477             clear_vec_high(s, !is_scalar, rd);
13478         } else {
13479             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13480 
13481             assert(size == 1);
13482             read_vec_element_i32(s, tcg_idx, rm, index, size);
13483 
13484             if (!is_scalar) {
13485                 /* The simplest way to handle the 16x16 indexed ops is to
13486                  * duplicate the index into both halves of the 32 bit tcg_idx
13487                  * and then use the usual Neon helpers.
13488                  */
13489                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13490             }
13491 
13492             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13493                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13494                 TCGv_i64 tcg_passres;
13495 
13496                 if (is_scalar) {
13497                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13498                 } else {
13499                     read_vec_element_i32(s, tcg_op, rn,
13500                                          pass + (is_q * 2), MO_32);
13501                 }
13502 
13503                 tcg_res[pass] = tcg_temp_new_i64();
13504 
13505                 if (opcode == 0xa || opcode == 0xb) {
13506                     /* Non-accumulating ops */
13507                     tcg_passres = tcg_res[pass];
13508                 } else {
13509                     tcg_passres = tcg_temp_new_i64();
13510                 }
13511 
13512                 if (memop & MO_SIGN) {
13513                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13514                 } else {
13515                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13516                 }
13517                 if (satop) {
13518                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13519                                                       tcg_passres, tcg_passres);
13520                 }
13521 
13522                 if (opcode == 0xa || opcode == 0xb) {
13523                     continue;
13524                 }
13525 
13526                 /* Accumulating op: handle accumulate step */
13527                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13528 
13529                 switch (opcode) {
13530                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13531                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13532                                              tcg_passres);
13533                     break;
13534                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13535                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13536                                              tcg_passres);
13537                     break;
13538                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13539                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13540                     /* fall through */
13541                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13542                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13543                                                       tcg_res[pass],
13544                                                       tcg_passres);
13545                     break;
13546                 default:
13547                     g_assert_not_reached();
13548                 }
13549             }
13550 
13551             if (is_scalar) {
13552                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13553             }
13554         }
13555 
13556         if (is_scalar) {
13557             tcg_res[1] = tcg_constant_i64(0);
13558         }
13559 
13560         for (pass = 0; pass < 2; pass++) {
13561             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13562         }
13563     }
13564 }
13565 
13566 /* Crypto AES
13567  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13568  * +-----------------+------+-----------+--------+-----+------+------+
13569  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13570  * +-----------------+------+-----------+--------+-----+------+------+
13571  */
13572 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13573 {
13574     int size = extract32(insn, 22, 2);
13575     int opcode = extract32(insn, 12, 5);
13576     int rn = extract32(insn, 5, 5);
13577     int rd = extract32(insn, 0, 5);
13578     int decrypt;
13579     gen_helper_gvec_2 *genfn2 = NULL;
13580     gen_helper_gvec_3 *genfn3 = NULL;
13581 
13582     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13583         unallocated_encoding(s);
13584         return;
13585     }
13586 
13587     switch (opcode) {
13588     case 0x4: /* AESE */
13589         decrypt = 0;
13590         genfn3 = gen_helper_crypto_aese;
13591         break;
13592     case 0x6: /* AESMC */
13593         decrypt = 0;
13594         genfn2 = gen_helper_crypto_aesmc;
13595         break;
13596     case 0x5: /* AESD */
13597         decrypt = 1;
13598         genfn3 = gen_helper_crypto_aese;
13599         break;
13600     case 0x7: /* AESIMC */
13601         decrypt = 1;
13602         genfn2 = gen_helper_crypto_aesmc;
13603         break;
13604     default:
13605         unallocated_encoding(s);
13606         return;
13607     }
13608 
13609     if (!fp_access_check(s)) {
13610         return;
13611     }
13612     if (genfn2) {
13613         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13614     } else {
13615         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13616     }
13617 }
13618 
13619 /* Crypto three-reg SHA
13620  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13621  * +-----------------+------+---+------+---+--------+-----+------+------+
13622  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13623  * +-----------------+------+---+------+---+--------+-----+------+------+
13624  */
13625 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13626 {
13627     int size = extract32(insn, 22, 2);
13628     int opcode = extract32(insn, 12, 3);
13629     int rm = extract32(insn, 16, 5);
13630     int rn = extract32(insn, 5, 5);
13631     int rd = extract32(insn, 0, 5);
13632     gen_helper_gvec_3 *genfn;
13633     bool feature;
13634 
13635     if (size != 0) {
13636         unallocated_encoding(s);
13637         return;
13638     }
13639 
13640     switch (opcode) {
13641     case 0: /* SHA1C */
13642         genfn = gen_helper_crypto_sha1c;
13643         feature = dc_isar_feature(aa64_sha1, s);
13644         break;
13645     case 1: /* SHA1P */
13646         genfn = gen_helper_crypto_sha1p;
13647         feature = dc_isar_feature(aa64_sha1, s);
13648         break;
13649     case 2: /* SHA1M */
13650         genfn = gen_helper_crypto_sha1m;
13651         feature = dc_isar_feature(aa64_sha1, s);
13652         break;
13653     case 3: /* SHA1SU0 */
13654         genfn = gen_helper_crypto_sha1su0;
13655         feature = dc_isar_feature(aa64_sha1, s);
13656         break;
13657     case 4: /* SHA256H */
13658         genfn = gen_helper_crypto_sha256h;
13659         feature = dc_isar_feature(aa64_sha256, s);
13660         break;
13661     case 5: /* SHA256H2 */
13662         genfn = gen_helper_crypto_sha256h2;
13663         feature = dc_isar_feature(aa64_sha256, s);
13664         break;
13665     case 6: /* SHA256SU1 */
13666         genfn = gen_helper_crypto_sha256su1;
13667         feature = dc_isar_feature(aa64_sha256, s);
13668         break;
13669     default:
13670         unallocated_encoding(s);
13671         return;
13672     }
13673 
13674     if (!feature) {
13675         unallocated_encoding(s);
13676         return;
13677     }
13678 
13679     if (!fp_access_check(s)) {
13680         return;
13681     }
13682     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13683 }
13684 
13685 /* Crypto two-reg SHA
13686  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13687  * +-----------------+------+-----------+--------+-----+------+------+
13688  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13689  * +-----------------+------+-----------+--------+-----+------+------+
13690  */
13691 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13692 {
13693     int size = extract32(insn, 22, 2);
13694     int opcode = extract32(insn, 12, 5);
13695     int rn = extract32(insn, 5, 5);
13696     int rd = extract32(insn, 0, 5);
13697     gen_helper_gvec_2 *genfn;
13698     bool feature;
13699 
13700     if (size != 0) {
13701         unallocated_encoding(s);
13702         return;
13703     }
13704 
13705     switch (opcode) {
13706     case 0: /* SHA1H */
13707         feature = dc_isar_feature(aa64_sha1, s);
13708         genfn = gen_helper_crypto_sha1h;
13709         break;
13710     case 1: /* SHA1SU1 */
13711         feature = dc_isar_feature(aa64_sha1, s);
13712         genfn = gen_helper_crypto_sha1su1;
13713         break;
13714     case 2: /* SHA256SU0 */
13715         feature = dc_isar_feature(aa64_sha256, s);
13716         genfn = gen_helper_crypto_sha256su0;
13717         break;
13718     default:
13719         unallocated_encoding(s);
13720         return;
13721     }
13722 
13723     if (!feature) {
13724         unallocated_encoding(s);
13725         return;
13726     }
13727 
13728     if (!fp_access_check(s)) {
13729         return;
13730     }
13731     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13732 }
13733 
13734 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13735 {
13736     tcg_gen_rotli_i64(d, m, 1);
13737     tcg_gen_xor_i64(d, d, n);
13738 }
13739 
13740 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13741 {
13742     tcg_gen_rotli_vec(vece, d, m, 1);
13743     tcg_gen_xor_vec(vece, d, d, n);
13744 }
13745 
13746 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13747                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13748 {
13749     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13750     static const GVecGen3 op = {
13751         .fni8 = gen_rax1_i64,
13752         .fniv = gen_rax1_vec,
13753         .opt_opc = vecop_list,
13754         .fno = gen_helper_crypto_rax1,
13755         .vece = MO_64,
13756     };
13757     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13758 }
13759 
13760 /* Crypto three-reg SHA512
13761  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13762  * +-----------------------+------+---+---+-----+--------+------+------+
13763  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13764  * +-----------------------+------+---+---+-----+--------+------+------+
13765  */
13766 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13767 {
13768     int opcode = extract32(insn, 10, 2);
13769     int o =  extract32(insn, 14, 1);
13770     int rm = extract32(insn, 16, 5);
13771     int rn = extract32(insn, 5, 5);
13772     int rd = extract32(insn, 0, 5);
13773     bool feature;
13774     gen_helper_gvec_3 *oolfn = NULL;
13775     GVecGen3Fn *gvecfn = NULL;
13776 
13777     if (o == 0) {
13778         switch (opcode) {
13779         case 0: /* SHA512H */
13780             feature = dc_isar_feature(aa64_sha512, s);
13781             oolfn = gen_helper_crypto_sha512h;
13782             break;
13783         case 1: /* SHA512H2 */
13784             feature = dc_isar_feature(aa64_sha512, s);
13785             oolfn = gen_helper_crypto_sha512h2;
13786             break;
13787         case 2: /* SHA512SU1 */
13788             feature = dc_isar_feature(aa64_sha512, s);
13789             oolfn = gen_helper_crypto_sha512su1;
13790             break;
13791         case 3: /* RAX1 */
13792             feature = dc_isar_feature(aa64_sha3, s);
13793             gvecfn = gen_gvec_rax1;
13794             break;
13795         default:
13796             g_assert_not_reached();
13797         }
13798     } else {
13799         switch (opcode) {
13800         case 0: /* SM3PARTW1 */
13801             feature = dc_isar_feature(aa64_sm3, s);
13802             oolfn = gen_helper_crypto_sm3partw1;
13803             break;
13804         case 1: /* SM3PARTW2 */
13805             feature = dc_isar_feature(aa64_sm3, s);
13806             oolfn = gen_helper_crypto_sm3partw2;
13807             break;
13808         case 2: /* SM4EKEY */
13809             feature = dc_isar_feature(aa64_sm4, s);
13810             oolfn = gen_helper_crypto_sm4ekey;
13811             break;
13812         default:
13813             unallocated_encoding(s);
13814             return;
13815         }
13816     }
13817 
13818     if (!feature) {
13819         unallocated_encoding(s);
13820         return;
13821     }
13822 
13823     if (!fp_access_check(s)) {
13824         return;
13825     }
13826 
13827     if (oolfn) {
13828         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13829     } else {
13830         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13831     }
13832 }
13833 
13834 /* Crypto two-reg SHA512
13835  *  31                                     12  11  10  9    5 4    0
13836  * +-----------------------------------------+--------+------+------+
13837  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13838  * +-----------------------------------------+--------+------+------+
13839  */
13840 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13841 {
13842     int opcode = extract32(insn, 10, 2);
13843     int rn = extract32(insn, 5, 5);
13844     int rd = extract32(insn, 0, 5);
13845     bool feature;
13846 
13847     switch (opcode) {
13848     case 0: /* SHA512SU0 */
13849         feature = dc_isar_feature(aa64_sha512, s);
13850         break;
13851     case 1: /* SM4E */
13852         feature = dc_isar_feature(aa64_sm4, s);
13853         break;
13854     default:
13855         unallocated_encoding(s);
13856         return;
13857     }
13858 
13859     if (!feature) {
13860         unallocated_encoding(s);
13861         return;
13862     }
13863 
13864     if (!fp_access_check(s)) {
13865         return;
13866     }
13867 
13868     switch (opcode) {
13869     case 0: /* SHA512SU0 */
13870         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13871         break;
13872     case 1: /* SM4E */
13873         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13874         break;
13875     default:
13876         g_assert_not_reached();
13877     }
13878 }
13879 
13880 /* Crypto four-register
13881  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13882  * +-------------------+-----+------+---+------+------+------+
13883  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13884  * +-------------------+-----+------+---+------+------+------+
13885  */
13886 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13887 {
13888     int op0 = extract32(insn, 21, 2);
13889     int rm = extract32(insn, 16, 5);
13890     int ra = extract32(insn, 10, 5);
13891     int rn = extract32(insn, 5, 5);
13892     int rd = extract32(insn, 0, 5);
13893     bool feature;
13894 
13895     switch (op0) {
13896     case 0: /* EOR3 */
13897     case 1: /* BCAX */
13898         feature = dc_isar_feature(aa64_sha3, s);
13899         break;
13900     case 2: /* SM3SS1 */
13901         feature = dc_isar_feature(aa64_sm3, s);
13902         break;
13903     default:
13904         unallocated_encoding(s);
13905         return;
13906     }
13907 
13908     if (!feature) {
13909         unallocated_encoding(s);
13910         return;
13911     }
13912 
13913     if (!fp_access_check(s)) {
13914         return;
13915     }
13916 
13917     if (op0 < 2) {
13918         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13919         int pass;
13920 
13921         tcg_op1 = tcg_temp_new_i64();
13922         tcg_op2 = tcg_temp_new_i64();
13923         tcg_op3 = tcg_temp_new_i64();
13924         tcg_res[0] = tcg_temp_new_i64();
13925         tcg_res[1] = tcg_temp_new_i64();
13926 
13927         for (pass = 0; pass < 2; pass++) {
13928             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13929             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13930             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13931 
13932             if (op0 == 0) {
13933                 /* EOR3 */
13934                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13935             } else {
13936                 /* BCAX */
13937                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13938             }
13939             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13940         }
13941         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13942         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13943     } else {
13944         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13945 
13946         tcg_op1 = tcg_temp_new_i32();
13947         tcg_op2 = tcg_temp_new_i32();
13948         tcg_op3 = tcg_temp_new_i32();
13949         tcg_res = tcg_temp_new_i32();
13950         tcg_zero = tcg_constant_i32(0);
13951 
13952         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13953         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13954         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13955 
13956         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13957         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13958         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13959         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13960 
13961         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13962         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13963         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13964         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13965     }
13966 }
13967 
13968 /* Crypto XAR
13969  *  31                   21 20  16 15    10 9    5 4    0
13970  * +-----------------------+------+--------+------+------+
13971  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13972  * +-----------------------+------+--------+------+------+
13973  */
13974 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13975 {
13976     int rm = extract32(insn, 16, 5);
13977     int imm6 = extract32(insn, 10, 6);
13978     int rn = extract32(insn, 5, 5);
13979     int rd = extract32(insn, 0, 5);
13980 
13981     if (!dc_isar_feature(aa64_sha3, s)) {
13982         unallocated_encoding(s);
13983         return;
13984     }
13985 
13986     if (!fp_access_check(s)) {
13987         return;
13988     }
13989 
13990     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13991                  vec_full_reg_offset(s, rn),
13992                  vec_full_reg_offset(s, rm), imm6, 16,
13993                  vec_full_reg_size(s));
13994 }
13995 
13996 /* Crypto three-reg imm2
13997  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13998  * +-----------------------+------+-----+------+--------+------+------+
13999  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14000  * +-----------------------+------+-----+------+--------+------+------+
14001  */
14002 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14003 {
14004     static gen_helper_gvec_3 * const fns[4] = {
14005         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14006         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14007     };
14008     int opcode = extract32(insn, 10, 2);
14009     int imm2 = extract32(insn, 12, 2);
14010     int rm = extract32(insn, 16, 5);
14011     int rn = extract32(insn, 5, 5);
14012     int rd = extract32(insn, 0, 5);
14013 
14014     if (!dc_isar_feature(aa64_sm3, s)) {
14015         unallocated_encoding(s);
14016         return;
14017     }
14018 
14019     if (!fp_access_check(s)) {
14020         return;
14021     }
14022 
14023     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14024 }
14025 
14026 /* C3.6 Data processing - SIMD, inc Crypto
14027  *
14028  * As the decode gets a little complex we are using a table based
14029  * approach for this part of the decode.
14030  */
14031 static const AArch64DecodeTable data_proc_simd[] = {
14032     /* pattern  ,  mask     ,  fn                        */
14033     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14034     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14035     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14036     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14037     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14038     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14039     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14040     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14041     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14042     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14043     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14044     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14045     { 0x2e000000, 0xbf208400, disas_simd_ext },
14046     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14047     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14048     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14049     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14050     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14051     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14052     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14053     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14054     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14055     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14056     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14057     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14058     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14059     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14060     { 0xce800000, 0xffe00000, disas_crypto_xar },
14061     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14062     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14063     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14064     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14065     { 0x00000000, 0x00000000, NULL }
14066 };
14067 
14068 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14069 {
14070     /* Note that this is called with all non-FP cases from
14071      * table C3-6 so it must UNDEF for entries not specifically
14072      * allocated to instructions in that table.
14073      */
14074     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14075     if (fn) {
14076         fn(s, insn);
14077     } else {
14078         unallocated_encoding(s);
14079     }
14080 }
14081 
14082 /* C3.6 Data processing - SIMD and floating point */
14083 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14084 {
14085     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14086         disas_data_proc_fp(s, insn);
14087     } else {
14088         /* SIMD, including crypto */
14089         disas_data_proc_simd(s, insn);
14090     }
14091 }
14092 
14093 static bool trans_OK(DisasContext *s, arg_OK *a)
14094 {
14095     return true;
14096 }
14097 
14098 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14099 {
14100     s->is_nonstreaming = true;
14101     return true;
14102 }
14103 
14104 /**
14105  * is_guarded_page:
14106  * @env: The cpu environment
14107  * @s: The DisasContext
14108  *
14109  * Return true if the page is guarded.
14110  */
14111 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14112 {
14113     uint64_t addr = s->base.pc_first;
14114 #ifdef CONFIG_USER_ONLY
14115     return page_get_flags(addr) & PAGE_BTI;
14116 #else
14117     CPUTLBEntryFull *full;
14118     void *host;
14119     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14120     int flags;
14121 
14122     /*
14123      * We test this immediately after reading an insn, which means
14124      * that the TLB entry must be present and valid, and thus this
14125      * access will never raise an exception.
14126      */
14127     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14128                               false, &host, &full, 0);
14129     assert(!(flags & TLB_INVALID_MASK));
14130 
14131     return full->guarded;
14132 #endif
14133 }
14134 
14135 /**
14136  * btype_destination_ok:
14137  * @insn: The instruction at the branch destination
14138  * @bt: SCTLR_ELx.BT
14139  * @btype: PSTATE.BTYPE, and is non-zero
14140  *
14141  * On a guarded page, there are a limited number of insns
14142  * that may be present at the branch target:
14143  *   - branch target identifiers,
14144  *   - paciasp, pacibsp,
14145  *   - BRK insn
14146  *   - HLT insn
14147  * Anything else causes a Branch Target Exception.
14148  *
14149  * Return true if the branch is compatible, false to raise BTITRAP.
14150  */
14151 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14152 {
14153     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14154         /* HINT space */
14155         switch (extract32(insn, 5, 7)) {
14156         case 0b011001: /* PACIASP */
14157         case 0b011011: /* PACIBSP */
14158             /*
14159              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14160              * with btype == 3.  Otherwise all btype are ok.
14161              */
14162             return !bt || btype != 3;
14163         case 0b100000: /* BTI */
14164             /* Not compatible with any btype.  */
14165             return false;
14166         case 0b100010: /* BTI c */
14167             /* Not compatible with btype == 3 */
14168             return btype != 3;
14169         case 0b100100: /* BTI j */
14170             /* Not compatible with btype == 2 */
14171             return btype != 2;
14172         case 0b100110: /* BTI jc */
14173             /* Compatible with any btype.  */
14174             return true;
14175         }
14176     } else {
14177         switch (insn & 0xffe0001fu) {
14178         case 0xd4200000u: /* BRK */
14179         case 0xd4400000u: /* HLT */
14180             /* Give priority to the breakpoint exception.  */
14181             return true;
14182         }
14183     }
14184     return false;
14185 }
14186 
14187 /* C3.1 A64 instruction index by encoding */
14188 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14189 {
14190     switch (extract32(insn, 25, 4)) {
14191     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14192         disas_b_exc_sys(s, insn);
14193         break;
14194     case 0x4:
14195     case 0x6:
14196     case 0xc:
14197     case 0xe:      /* Loads and stores */
14198         disas_ldst(s, insn);
14199         break;
14200     case 0x5:
14201     case 0xd:      /* Data processing - register */
14202         disas_data_proc_reg(s, insn);
14203         break;
14204     case 0x7:
14205     case 0xf:      /* Data processing - SIMD and floating point */
14206         disas_data_proc_simd_fp(s, insn);
14207         break;
14208     default:
14209         unallocated_encoding(s);
14210         break;
14211     }
14212 }
14213 
14214 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14215                                           CPUState *cpu)
14216 {
14217     DisasContext *dc = container_of(dcbase, DisasContext, base);
14218     CPUARMState *env = cpu->env_ptr;
14219     ARMCPU *arm_cpu = env_archcpu(env);
14220     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14221     int bound, core_mmu_idx;
14222 
14223     dc->isar = &arm_cpu->isar;
14224     dc->condjmp = 0;
14225     dc->pc_save = dc->base.pc_first;
14226     dc->aarch64 = true;
14227     dc->thumb = false;
14228     dc->sctlr_b = 0;
14229     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14230     dc->condexec_mask = 0;
14231     dc->condexec_cond = 0;
14232     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14233     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14234     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14235     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14236     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14237     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14238 #if !defined(CONFIG_USER_ONLY)
14239     dc->user = (dc->current_el == 0);
14240 #endif
14241     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14242     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14243     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14244     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14245     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14246     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14247     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14248     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14249     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14250     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14251     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14252     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14253     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14254     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14255     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14256     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14257     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14258     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14259     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14260     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14261     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14262     dc->vec_len = 0;
14263     dc->vec_stride = 0;
14264     dc->cp_regs = arm_cpu->cp_regs;
14265     dc->features = env->features;
14266     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14267 
14268 #ifdef CONFIG_USER_ONLY
14269     /* In sve_probe_page, we assume TBI is enabled. */
14270     tcg_debug_assert(dc->tbid & 1);
14271 #endif
14272 
14273     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14274 
14275     /* Single step state. The code-generation logic here is:
14276      *  SS_ACTIVE == 0:
14277      *   generate code with no special handling for single-stepping (except
14278      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14279      *   this happens anyway because those changes are all system register or
14280      *   PSTATE writes).
14281      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14282      *   emit code for one insn
14283      *   emit code to clear PSTATE.SS
14284      *   emit code to generate software step exception for completed step
14285      *   end TB (as usual for having generated an exception)
14286      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14287      *   emit code to generate a software step exception
14288      *   end the TB
14289      */
14290     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14291     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14292     dc->is_ldex = false;
14293 
14294     /* Bound the number of insns to execute to those left on the page.  */
14295     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14296 
14297     /* If architectural single step active, limit to 1.  */
14298     if (dc->ss_active) {
14299         bound = 1;
14300     }
14301     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14302 }
14303 
14304 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14305 {
14306 }
14307 
14308 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14309 {
14310     DisasContext *dc = container_of(dcbase, DisasContext, base);
14311     target_ulong pc_arg = dc->base.pc_next;
14312 
14313     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14314         pc_arg &= ~TARGET_PAGE_MASK;
14315     }
14316     tcg_gen_insn_start(pc_arg, 0, 0);
14317     dc->insn_start = tcg_last_op();
14318 }
14319 
14320 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14321 {
14322     DisasContext *s = container_of(dcbase, DisasContext, base);
14323     CPUARMState *env = cpu->env_ptr;
14324     uint64_t pc = s->base.pc_next;
14325     uint32_t insn;
14326 
14327     /* Singlestep exceptions have the highest priority. */
14328     if (s->ss_active && !s->pstate_ss) {
14329         /* Singlestep state is Active-pending.
14330          * If we're in this state at the start of a TB then either
14331          *  a) we just took an exception to an EL which is being debugged
14332          *     and this is the first insn in the exception handler
14333          *  b) debug exceptions were masked and we just unmasked them
14334          *     without changing EL (eg by clearing PSTATE.D)
14335          * In either case we're going to take a swstep exception in the
14336          * "did not step an insn" case, and so the syndrome ISV and EX
14337          * bits should be zero.
14338          */
14339         assert(s->base.num_insns == 1);
14340         gen_swstep_exception(s, 0, 0);
14341         s->base.is_jmp = DISAS_NORETURN;
14342         s->base.pc_next = pc + 4;
14343         return;
14344     }
14345 
14346     if (pc & 3) {
14347         /*
14348          * PC alignment fault.  This has priority over the instruction abort
14349          * that we would receive from a translation fault via arm_ldl_code.
14350          * This should only be possible after an indirect branch, at the
14351          * start of the TB.
14352          */
14353         assert(s->base.num_insns == 1);
14354         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14355         s->base.is_jmp = DISAS_NORETURN;
14356         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14357         return;
14358     }
14359 
14360     s->pc_curr = pc;
14361     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14362     s->insn = insn;
14363     s->base.pc_next = pc + 4;
14364 
14365     s->fp_access_checked = false;
14366     s->sve_access_checked = false;
14367 
14368     if (s->pstate_il) {
14369         /*
14370          * Illegal execution state. This has priority over BTI
14371          * exceptions, but comes after instruction abort exceptions.
14372          */
14373         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14374         return;
14375     }
14376 
14377     if (dc_isar_feature(aa64_bti, s)) {
14378         if (s->base.num_insns == 1) {
14379             /*
14380              * At the first insn of the TB, compute s->guarded_page.
14381              * We delayed computing this until successfully reading
14382              * the first insn of the TB, above.  This (mostly) ensures
14383              * that the softmmu tlb entry has been populated, and the
14384              * page table GP bit is available.
14385              *
14386              * Note that we need to compute this even if btype == 0,
14387              * because this value is used for BR instructions later
14388              * where ENV is not available.
14389              */
14390             s->guarded_page = is_guarded_page(env, s);
14391 
14392             /* First insn can have btype set to non-zero.  */
14393             tcg_debug_assert(s->btype >= 0);
14394 
14395             /*
14396              * Note that the Branch Target Exception has fairly high
14397              * priority -- below debugging exceptions but above most
14398              * everything else.  This allows us to handle this now
14399              * instead of waiting until the insn is otherwise decoded.
14400              */
14401             if (s->btype != 0
14402                 && s->guarded_page
14403                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14404                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14405                 return;
14406             }
14407         } else {
14408             /* Not the first insn: btype must be 0.  */
14409             tcg_debug_assert(s->btype == 0);
14410         }
14411     }
14412 
14413     s->is_nonstreaming = false;
14414     if (s->sme_trap_nonstreaming) {
14415         disas_sme_fa64(s, insn);
14416     }
14417 
14418     if (!disas_a64(s, insn) &&
14419         !disas_sme(s, insn) &&
14420         !disas_sve(s, insn)) {
14421         disas_a64_legacy(s, insn);
14422     }
14423 
14424     /*
14425      * After execution of most insns, btype is reset to 0.
14426      * Note that we set btype == -1 when the insn sets btype.
14427      */
14428     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14429         reset_btype(s);
14430     }
14431 }
14432 
14433 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14434 {
14435     DisasContext *dc = container_of(dcbase, DisasContext, base);
14436 
14437     if (unlikely(dc->ss_active)) {
14438         /* Note that this means single stepping WFI doesn't halt the CPU.
14439          * For conditional branch insns this is harmless unreachable code as
14440          * gen_goto_tb() has already handled emitting the debug exception
14441          * (and thus a tb-jump is not possible when singlestepping).
14442          */
14443         switch (dc->base.is_jmp) {
14444         default:
14445             gen_a64_update_pc(dc, 4);
14446             /* fall through */
14447         case DISAS_EXIT:
14448         case DISAS_JUMP:
14449             gen_step_complete_exception(dc);
14450             break;
14451         case DISAS_NORETURN:
14452             break;
14453         }
14454     } else {
14455         switch (dc->base.is_jmp) {
14456         case DISAS_NEXT:
14457         case DISAS_TOO_MANY:
14458             gen_goto_tb(dc, 1, 4);
14459             break;
14460         default:
14461         case DISAS_UPDATE_EXIT:
14462             gen_a64_update_pc(dc, 4);
14463             /* fall through */
14464         case DISAS_EXIT:
14465             tcg_gen_exit_tb(NULL, 0);
14466             break;
14467         case DISAS_UPDATE_NOCHAIN:
14468             gen_a64_update_pc(dc, 4);
14469             /* fall through */
14470         case DISAS_JUMP:
14471             tcg_gen_lookup_and_goto_ptr();
14472             break;
14473         case DISAS_NORETURN:
14474         case DISAS_SWI:
14475             break;
14476         case DISAS_WFE:
14477             gen_a64_update_pc(dc, 4);
14478             gen_helper_wfe(cpu_env);
14479             break;
14480         case DISAS_YIELD:
14481             gen_a64_update_pc(dc, 4);
14482             gen_helper_yield(cpu_env);
14483             break;
14484         case DISAS_WFI:
14485             /*
14486              * This is a special case because we don't want to just halt
14487              * the CPU if trying to debug across a WFI.
14488              */
14489             gen_a64_update_pc(dc, 4);
14490             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14491             /*
14492              * The helper doesn't necessarily throw an exception, but we
14493              * must go back to the main loop to check for interrupts anyway.
14494              */
14495             tcg_gen_exit_tb(NULL, 0);
14496             break;
14497         }
14498     }
14499 }
14500 
14501 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14502                                  CPUState *cpu, FILE *logfile)
14503 {
14504     DisasContext *dc = container_of(dcbase, DisasContext, base);
14505 
14506     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14507     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14508 }
14509 
14510 const TranslatorOps aarch64_translator_ops = {
14511     .init_disas_context = aarch64_tr_init_disas_context,
14512     .tb_start           = aarch64_tr_tb_start,
14513     .insn_start         = aarch64_tr_insn_start,
14514     .translate_insn     = aarch64_tr_translate_insn,
14515     .tb_stop            = aarch64_tr_tb_stop,
14516     .disas_log          = aarch64_tr_disas_log,
14517 };
14518