xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision d78b662f)
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 /* MSR (immediate) - move immediate to processor state field */
1931 static void handle_msr_i(DisasContext *s, uint32_t insn,
1932                          unsigned int op1, unsigned int op2, unsigned int crm)
1933 {
1934     int op = op1 << 3 | op2;
1935 
1936     /* End the TB by default, chaining is ok.  */
1937     s->base.is_jmp = DISAS_TOO_MANY;
1938 
1939     switch (op) {
1940     case 0x03: /* UAO */
1941         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1942             goto do_unallocated;
1943         }
1944         if (crm & 1) {
1945             set_pstate_bits(PSTATE_UAO);
1946         } else {
1947             clear_pstate_bits(PSTATE_UAO);
1948         }
1949         gen_rebuild_hflags(s);
1950         break;
1951 
1952     case 0x04: /* PAN */
1953         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1954             goto do_unallocated;
1955         }
1956         if (crm & 1) {
1957             set_pstate_bits(PSTATE_PAN);
1958         } else {
1959             clear_pstate_bits(PSTATE_PAN);
1960         }
1961         gen_rebuild_hflags(s);
1962         break;
1963 
1964     case 0x05: /* SPSel */
1965         if (s->current_el == 0) {
1966             goto do_unallocated;
1967         }
1968         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1969         break;
1970 
1971     case 0x19: /* SSBS */
1972         if (!dc_isar_feature(aa64_ssbs, s)) {
1973             goto do_unallocated;
1974         }
1975         if (crm & 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         break;
1982 
1983     case 0x1a: /* DIT */
1984         if (!dc_isar_feature(aa64_dit, s)) {
1985             goto do_unallocated;
1986         }
1987         if (crm & 1) {
1988             set_pstate_bits(PSTATE_DIT);
1989         } else {
1990             clear_pstate_bits(PSTATE_DIT);
1991         }
1992         /* There's no need to rebuild hflags because DIT is a nop */
1993         break;
1994 
1995     case 0x1e: /* DAIFSet */
1996         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1997         break;
1998 
1999     case 0x1f: /* DAIFClear */
2000         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
2001         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
2002         s->base.is_jmp = DISAS_UPDATE_EXIT;
2003         break;
2004 
2005     case 0x1c: /* TCO */
2006         if (dc_isar_feature(aa64_mte, s)) {
2007             /* Full MTE is enabled -- set the TCO bit as directed. */
2008             if (crm & 1) {
2009                 set_pstate_bits(PSTATE_TCO);
2010             } else {
2011                 clear_pstate_bits(PSTATE_TCO);
2012             }
2013             gen_rebuild_hflags(s);
2014             /* Many factors, including TCO, go into MTE_ACTIVE. */
2015             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2016         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2017             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2018             s->base.is_jmp = DISAS_NEXT;
2019         } else {
2020             goto do_unallocated;
2021         }
2022         break;
2023 
2024     case 0x1b: /* SVCR* */
2025         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
2026             goto do_unallocated;
2027         }
2028         if (sme_access_check(s)) {
2029             int old = s->pstate_sm | (s->pstate_za << 1);
2030             int new = (crm & 1) * 3;
2031             int msk = (crm >> 1) & 3;
2032 
2033             if ((old ^ new) & msk) {
2034                 /* At least one bit changes. */
2035                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2036                                     tcg_constant_i32(msk));
2037             } else {
2038                 s->base.is_jmp = DISAS_NEXT;
2039             }
2040         }
2041         break;
2042 
2043     default:
2044     do_unallocated:
2045         unallocated_encoding(s);
2046         return;
2047     }
2048 }
2049 
2050 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2051 {
2052     TCGv_i32 tmp = tcg_temp_new_i32();
2053     TCGv_i32 nzcv = tcg_temp_new_i32();
2054 
2055     /* build bit 31, N */
2056     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2057     /* build bit 30, Z */
2058     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2059     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2060     /* build bit 29, C */
2061     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2062     /* build bit 28, V */
2063     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2064     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2065     /* generate result */
2066     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2067 }
2068 
2069 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2070 {
2071     TCGv_i32 nzcv = tcg_temp_new_i32();
2072 
2073     /* take NZCV from R[t] */
2074     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2075 
2076     /* bit 31, N */
2077     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2078     /* bit 30, Z */
2079     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2080     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2081     /* bit 29, C */
2082     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2083     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2084     /* bit 28, V */
2085     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2086     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2087 }
2088 
2089 static void gen_sysreg_undef(DisasContext *s, bool isread,
2090                              uint8_t op0, uint8_t op1, uint8_t op2,
2091                              uint8_t crn, uint8_t crm, uint8_t rt)
2092 {
2093     /*
2094      * Generate code to emit an UNDEF with correct syndrome
2095      * information for a failed system register access.
2096      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2097      * but if FEAT_IDST is implemented then read accesses to registers
2098      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2099      * syndrome.
2100      */
2101     uint32_t syndrome;
2102 
2103     if (isread && dc_isar_feature(aa64_ids, s) &&
2104         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2105         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2106     } else {
2107         syndrome = syn_uncategorized();
2108     }
2109     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2110 }
2111 
2112 /* MRS - move from system register
2113  * MSR (register) - move to system register
2114  * SYS
2115  * SYSL
2116  * These are all essentially the same insn in 'read' and 'write'
2117  * versions, with varying op0 fields.
2118  */
2119 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2120                        unsigned int op0, unsigned int op1, unsigned int op2,
2121                        unsigned int crn, unsigned int crm, unsigned int rt)
2122 {
2123     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2124                                       crn, crm, op0, op1, op2);
2125     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2126     bool need_exit_tb = false;
2127     TCGv_ptr tcg_ri = NULL;
2128     TCGv_i64 tcg_rt;
2129 
2130     if (!ri) {
2131         /* Unknown register; this might be a guest error or a QEMU
2132          * unimplemented feature.
2133          */
2134         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2135                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2136                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2137         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2138         return;
2139     }
2140 
2141     /* Check access permissions */
2142     if (!cp_access_ok(s->current_el, ri, isread)) {
2143         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2144         return;
2145     }
2146 
2147     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2148         /* Emit code to perform further access permissions checks at
2149          * runtime; this may result in an exception.
2150          */
2151         uint32_t syndrome;
2152 
2153         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2154         gen_a64_update_pc(s, 0);
2155         tcg_ri = tcg_temp_new_ptr();
2156         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2157                                        tcg_constant_i32(key),
2158                                        tcg_constant_i32(syndrome),
2159                                        tcg_constant_i32(isread));
2160     } else if (ri->type & ARM_CP_RAISES_EXC) {
2161         /*
2162          * The readfn or writefn might raise an exception;
2163          * synchronize the CPU state in case it does.
2164          */
2165         gen_a64_update_pc(s, 0);
2166     }
2167 
2168     /* Handle special cases first */
2169     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2170     case 0:
2171         break;
2172     case ARM_CP_NOP:
2173         return;
2174     case ARM_CP_NZCV:
2175         tcg_rt = cpu_reg(s, rt);
2176         if (isread) {
2177             gen_get_nzcv(tcg_rt);
2178         } else {
2179             gen_set_nzcv(tcg_rt);
2180         }
2181         return;
2182     case ARM_CP_CURRENTEL:
2183         /* Reads as current EL value from pstate, which is
2184          * guaranteed to be constant by the tb flags.
2185          */
2186         tcg_rt = cpu_reg(s, rt);
2187         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2188         return;
2189     case ARM_CP_DC_ZVA:
2190         /* Writes clear the aligned block of memory which rt points into. */
2191         if (s->mte_active[0]) {
2192             int desc = 0;
2193 
2194             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2195             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2196             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2197 
2198             tcg_rt = tcg_temp_new_i64();
2199             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2200                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2201         } else {
2202             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2203         }
2204         gen_helper_dc_zva(cpu_env, tcg_rt);
2205         return;
2206     case ARM_CP_DC_GVA:
2207         {
2208             TCGv_i64 clean_addr, tag;
2209 
2210             /*
2211              * DC_GVA, like DC_ZVA, requires that we supply the original
2212              * pointer for an invalid page.  Probe that address first.
2213              */
2214             tcg_rt = cpu_reg(s, rt);
2215             clean_addr = clean_data_tbi(s, tcg_rt);
2216             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2217 
2218             if (s->ata) {
2219                 /* Extract the tag from the register to match STZGM.  */
2220                 tag = tcg_temp_new_i64();
2221                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2222                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2223             }
2224         }
2225         return;
2226     case ARM_CP_DC_GZVA:
2227         {
2228             TCGv_i64 clean_addr, tag;
2229 
2230             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2231             tcg_rt = cpu_reg(s, rt);
2232             clean_addr = clean_data_tbi(s, tcg_rt);
2233             gen_helper_dc_zva(cpu_env, clean_addr);
2234 
2235             if (s->ata) {
2236                 /* Extract the tag from the register to match STZGM.  */
2237                 tag = tcg_temp_new_i64();
2238                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2239                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2240             }
2241         }
2242         return;
2243     default:
2244         g_assert_not_reached();
2245     }
2246     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2247         return;
2248     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2249         return;
2250     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2251         return;
2252     }
2253 
2254     if (ri->type & ARM_CP_IO) {
2255         /* I/O operations must end the TB here (whether read or write) */
2256         need_exit_tb = translator_io_start(&s->base);
2257     }
2258 
2259     tcg_rt = cpu_reg(s, rt);
2260 
2261     if (isread) {
2262         if (ri->type & ARM_CP_CONST) {
2263             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2264         } else if (ri->readfn) {
2265             if (!tcg_ri) {
2266                 tcg_ri = gen_lookup_cp_reg(key);
2267             }
2268             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2269         } else {
2270             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2271         }
2272     } else {
2273         if (ri->type & ARM_CP_CONST) {
2274             /* If not forbidden by access permissions, treat as WI */
2275             return;
2276         } else if (ri->writefn) {
2277             if (!tcg_ri) {
2278                 tcg_ri = gen_lookup_cp_reg(key);
2279             }
2280             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2281         } else {
2282             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2283         }
2284     }
2285 
2286     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2287         /*
2288          * A write to any coprocessor regiser that ends a TB
2289          * must rebuild the hflags for the next TB.
2290          */
2291         gen_rebuild_hflags(s);
2292         /*
2293          * We default to ending the TB on a coprocessor register write,
2294          * but allow this to be suppressed by the register definition
2295          * (usually only necessary to work around guest bugs).
2296          */
2297         need_exit_tb = true;
2298     }
2299     if (need_exit_tb) {
2300         s->base.is_jmp = DISAS_UPDATE_EXIT;
2301     }
2302 }
2303 
2304 /* System
2305  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2306  * +---------------------+---+-----+-----+-------+-------+-----+------+
2307  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2308  * +---------------------+---+-----+-----+-------+-------+-----+------+
2309  */
2310 static void disas_system(DisasContext *s, uint32_t insn)
2311 {
2312     unsigned int l, op0, op1, crn, crm, op2, rt;
2313     l = extract32(insn, 21, 1);
2314     op0 = extract32(insn, 19, 2);
2315     op1 = extract32(insn, 16, 3);
2316     crn = extract32(insn, 12, 4);
2317     crm = extract32(insn, 8, 4);
2318     op2 = extract32(insn, 5, 3);
2319     rt = extract32(insn, 0, 5);
2320 
2321     if (op0 == 0) {
2322         if (l || rt != 31) {
2323             unallocated_encoding(s);
2324             return;
2325         }
2326         switch (crn) {
2327         case 4: /* MSR (immediate) */
2328             handle_msr_i(s, insn, op1, op2, crm);
2329             break;
2330         default:
2331             unallocated_encoding(s);
2332             break;
2333         }
2334         return;
2335     }
2336     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2337 }
2338 
2339 /* Exception generation
2340  *
2341  *  31             24 23 21 20                     5 4   2 1  0
2342  * +-----------------+-----+------------------------+-----+----+
2343  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2344  * +-----------------------+------------------------+----------+
2345  */
2346 static void disas_exc(DisasContext *s, uint32_t insn)
2347 {
2348     int opc = extract32(insn, 21, 3);
2349     int op2_ll = extract32(insn, 0, 5);
2350     int imm16 = extract32(insn, 5, 16);
2351     uint32_t syndrome;
2352 
2353     switch (opc) {
2354     case 0:
2355         /* For SVC, HVC and SMC we advance the single-step state
2356          * machine before taking the exception. This is architecturally
2357          * mandated, to ensure that single-stepping a system call
2358          * instruction works properly.
2359          */
2360         switch (op2_ll) {
2361         case 1:                                                     /* SVC */
2362             syndrome = syn_aa64_svc(imm16);
2363             if (s->fgt_svc) {
2364                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2365                 break;
2366             }
2367             gen_ss_advance(s);
2368             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2369             break;
2370         case 2:                                                     /* HVC */
2371             if (s->current_el == 0) {
2372                 unallocated_encoding(s);
2373                 break;
2374             }
2375             /* The pre HVC helper handles cases when HVC gets trapped
2376              * as an undefined insn by runtime configuration.
2377              */
2378             gen_a64_update_pc(s, 0);
2379             gen_helper_pre_hvc(cpu_env);
2380             gen_ss_advance(s);
2381             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2382             break;
2383         case 3:                                                     /* SMC */
2384             if (s->current_el == 0) {
2385                 unallocated_encoding(s);
2386                 break;
2387             }
2388             gen_a64_update_pc(s, 0);
2389             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2390             gen_ss_advance(s);
2391             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2392             break;
2393         default:
2394             unallocated_encoding(s);
2395             break;
2396         }
2397         break;
2398     case 1:
2399         if (op2_ll != 0) {
2400             unallocated_encoding(s);
2401             break;
2402         }
2403         /* BRK */
2404         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2405         break;
2406     case 2:
2407         if (op2_ll != 0) {
2408             unallocated_encoding(s);
2409             break;
2410         }
2411         /* HLT. This has two purposes.
2412          * Architecturally, it is an external halting debug instruction.
2413          * Since QEMU doesn't implement external debug, we treat this as
2414          * it is required for halting debug disabled: it will UNDEF.
2415          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2416          */
2417         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2418             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2419         } else {
2420             unallocated_encoding(s);
2421         }
2422         break;
2423     case 5:
2424         if (op2_ll < 1 || op2_ll > 3) {
2425             unallocated_encoding(s);
2426             break;
2427         }
2428         /* DCPS1, DCPS2, DCPS3 */
2429         unallocated_encoding(s);
2430         break;
2431     default:
2432         unallocated_encoding(s);
2433         break;
2434     }
2435 }
2436 
2437 /* Branches, exception generating and system instructions */
2438 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2439 {
2440     switch (extract32(insn, 25, 7)) {
2441     case 0x6a: /* Exception generation / System */
2442         if (insn & (1 << 24)) {
2443             if (extract32(insn, 22, 2) == 0) {
2444                 disas_system(s, insn);
2445             } else {
2446                 unallocated_encoding(s);
2447             }
2448         } else {
2449             disas_exc(s, insn);
2450         }
2451         break;
2452     default:
2453         unallocated_encoding(s);
2454         break;
2455     }
2456 }
2457 
2458 /*
2459  * Load/Store exclusive instructions are implemented by remembering
2460  * the value/address loaded, and seeing if these are the same
2461  * when the store is performed. This is not actually the architecturally
2462  * mandated semantics, but it works for typical guest code sequences
2463  * and avoids having to monitor regular stores.
2464  *
2465  * The store exclusive uses the atomic cmpxchg primitives to avoid
2466  * races in multi-threaded linux-user and when MTTCG softmmu is
2467  * enabled.
2468  */
2469 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2470                                int size, bool is_pair)
2471 {
2472     int idx = get_mem_index(s);
2473     TCGv_i64 dirty_addr, clean_addr;
2474     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2475 
2476     s->is_ldex = true;
2477     dirty_addr = cpu_reg_sp(s, rn);
2478     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2479 
2480     g_assert(size <= 3);
2481     if (is_pair) {
2482         g_assert(size >= 2);
2483         if (size == 2) {
2484             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2485             if (s->be_data == MO_LE) {
2486                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2487                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2488             } else {
2489                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2490                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2491             }
2492         } else {
2493             TCGv_i128 t16 = tcg_temp_new_i128();
2494 
2495             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2496 
2497             if (s->be_data == MO_LE) {
2498                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2499                                       cpu_exclusive_high, t16);
2500             } else {
2501                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2502                                       cpu_exclusive_val, t16);
2503             }
2504             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2505             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2506         }
2507     } else {
2508         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2509         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2510     }
2511     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2512 }
2513 
2514 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2515                                 int rn, int size, int is_pair)
2516 {
2517     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2518      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2519      *     [addr] = {Rt};
2520      *     if (is_pair) {
2521      *         [addr + datasize] = {Rt2};
2522      *     }
2523      *     {Rd} = 0;
2524      * } else {
2525      *     {Rd} = 1;
2526      * }
2527      * env->exclusive_addr = -1;
2528      */
2529     TCGLabel *fail_label = gen_new_label();
2530     TCGLabel *done_label = gen_new_label();
2531     TCGv_i64 tmp, clean_addr;
2532     MemOp memop;
2533 
2534     /*
2535      * FIXME: We are out of spec here.  We have recorded only the address
2536      * from load_exclusive, not the entire range, and we assume that the
2537      * size of the access on both sides match.  The architecture allows the
2538      * store to be smaller than the load, so long as the stored bytes are
2539      * within the range recorded by the load.
2540      */
2541 
2542     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2543     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2544     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2545 
2546     /*
2547      * The write, and any associated faults, only happen if the virtual
2548      * and physical addresses pass the exclusive monitor check.  These
2549      * faults are exceedingly unlikely, because normally the guest uses
2550      * the exact same address register for the load_exclusive, and we
2551      * would have recognized these faults there.
2552      *
2553      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2554      * unaligned 4-byte write within the range of an aligned 8-byte load.
2555      * With LSE2, the store would need to cross a 16-byte boundary when the
2556      * load did not, which would mean the store is outside the range
2557      * recorded for the monitor, which would have failed a corrected monitor
2558      * check above.  For now, we assume no size change and retain the
2559      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2560      *
2561      * It is possible to trigger an MTE fault, by performing the load with
2562      * a virtual address with a valid tag and performing the store with the
2563      * same virtual address and a different invalid tag.
2564      */
2565     memop = size + is_pair;
2566     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2567         memop |= MO_ALIGN;
2568     }
2569     memop = finalize_memop(s, memop);
2570     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2571 
2572     tmp = tcg_temp_new_i64();
2573     if (is_pair) {
2574         if (size == 2) {
2575             if (s->be_data == MO_LE) {
2576                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2577             } else {
2578                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2579             }
2580             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2581                                        cpu_exclusive_val, tmp,
2582                                        get_mem_index(s), memop);
2583             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2584         } else {
2585             TCGv_i128 t16 = tcg_temp_new_i128();
2586             TCGv_i128 c16 = tcg_temp_new_i128();
2587             TCGv_i64 a, b;
2588 
2589             if (s->be_data == MO_LE) {
2590                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2591                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2592                                         cpu_exclusive_high);
2593             } else {
2594                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2595                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2596                                         cpu_exclusive_val);
2597             }
2598 
2599             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2600                                         get_mem_index(s), memop);
2601 
2602             a = tcg_temp_new_i64();
2603             b = tcg_temp_new_i64();
2604             if (s->be_data == MO_LE) {
2605                 tcg_gen_extr_i128_i64(a, b, t16);
2606             } else {
2607                 tcg_gen_extr_i128_i64(b, a, t16);
2608             }
2609 
2610             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2611             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2612             tcg_gen_or_i64(tmp, a, b);
2613 
2614             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2615         }
2616     } else {
2617         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2618                                    cpu_reg(s, rt), get_mem_index(s), memop);
2619         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2620     }
2621     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2622     tcg_gen_br(done_label);
2623 
2624     gen_set_label(fail_label);
2625     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2626     gen_set_label(done_label);
2627     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2628 }
2629 
2630 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2631                                  int rn, int size)
2632 {
2633     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2634     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2635     int memidx = get_mem_index(s);
2636     TCGv_i64 clean_addr;
2637     MemOp memop;
2638 
2639     if (rn == 31) {
2640         gen_check_sp_alignment(s);
2641     }
2642     memop = check_atomic_align(s, rn, size);
2643     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2644     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2645                                memidx, memop);
2646 }
2647 
2648 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2649                                       int rn, int size)
2650 {
2651     TCGv_i64 s1 = cpu_reg(s, rs);
2652     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2653     TCGv_i64 t1 = cpu_reg(s, rt);
2654     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2655     TCGv_i64 clean_addr;
2656     int memidx = get_mem_index(s);
2657     MemOp memop;
2658 
2659     if (rn == 31) {
2660         gen_check_sp_alignment(s);
2661     }
2662 
2663     /* This is a single atomic access, despite the "pair". */
2664     memop = check_atomic_align(s, rn, size + 1);
2665     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2666 
2667     if (size == 2) {
2668         TCGv_i64 cmp = tcg_temp_new_i64();
2669         TCGv_i64 val = tcg_temp_new_i64();
2670 
2671         if (s->be_data == MO_LE) {
2672             tcg_gen_concat32_i64(val, t1, t2);
2673             tcg_gen_concat32_i64(cmp, s1, s2);
2674         } else {
2675             tcg_gen_concat32_i64(val, t2, t1);
2676             tcg_gen_concat32_i64(cmp, s2, s1);
2677         }
2678 
2679         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2680 
2681         if (s->be_data == MO_LE) {
2682             tcg_gen_extr32_i64(s1, s2, cmp);
2683         } else {
2684             tcg_gen_extr32_i64(s2, s1, cmp);
2685         }
2686     } else {
2687         TCGv_i128 cmp = tcg_temp_new_i128();
2688         TCGv_i128 val = tcg_temp_new_i128();
2689 
2690         if (s->be_data == MO_LE) {
2691             tcg_gen_concat_i64_i128(val, t1, t2);
2692             tcg_gen_concat_i64_i128(cmp, s1, s2);
2693         } else {
2694             tcg_gen_concat_i64_i128(val, t2, t1);
2695             tcg_gen_concat_i64_i128(cmp, s2, s1);
2696         }
2697 
2698         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2699 
2700         if (s->be_data == MO_LE) {
2701             tcg_gen_extr_i128_i64(s1, s2, cmp);
2702         } else {
2703             tcg_gen_extr_i128_i64(s2, s1, cmp);
2704         }
2705     }
2706 }
2707 
2708 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2709  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2710  */
2711 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2712 {
2713     int opc0 = extract32(opc, 0, 1);
2714     int regsize;
2715 
2716     if (is_signed) {
2717         regsize = opc0 ? 32 : 64;
2718     } else {
2719         regsize = size == 3 ? 64 : 32;
2720     }
2721     return regsize == 64;
2722 }
2723 
2724 /* Load/store exclusive
2725  *
2726  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2727  * +-----+-------------+----+---+----+------+----+-------+------+------+
2728  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2729  * +-----+-------------+----+---+----+------+----+-------+------+------+
2730  *
2731  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2732  *   L: 0 -> store, 1 -> load
2733  *  o2: 0 -> exclusive, 1 -> not
2734  *  o1: 0 -> single register, 1 -> register pair
2735  *  o0: 1 -> load-acquire/store-release, 0 -> not
2736  */
2737 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2738 {
2739     int rt = extract32(insn, 0, 5);
2740     int rn = extract32(insn, 5, 5);
2741     int rt2 = extract32(insn, 10, 5);
2742     int rs = extract32(insn, 16, 5);
2743     int is_lasr = extract32(insn, 15, 1);
2744     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2745     int size = extract32(insn, 30, 2);
2746     TCGv_i64 clean_addr;
2747     MemOp memop;
2748 
2749     switch (o2_L_o1_o0) {
2750     case 0x0: /* STXR */
2751     case 0x1: /* STLXR */
2752         if (rn == 31) {
2753             gen_check_sp_alignment(s);
2754         }
2755         if (is_lasr) {
2756             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2757         }
2758         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2759         return;
2760 
2761     case 0x4: /* LDXR */
2762     case 0x5: /* LDAXR */
2763         if (rn == 31) {
2764             gen_check_sp_alignment(s);
2765         }
2766         gen_load_exclusive(s, rt, rt2, rn, size, false);
2767         if (is_lasr) {
2768             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2769         }
2770         return;
2771 
2772     case 0x8: /* STLLR */
2773         if (!dc_isar_feature(aa64_lor, s)) {
2774             break;
2775         }
2776         /* StoreLORelease is the same as Store-Release for QEMU.  */
2777         /* fall through */
2778     case 0x9: /* STLR */
2779         /* Generate ISS for non-exclusive accesses including LASR.  */
2780         if (rn == 31) {
2781             gen_check_sp_alignment(s);
2782         }
2783         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2784         memop = check_ordered_align(s, rn, 0, true, size);
2785         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2786                                     true, rn != 31, memop);
2787         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2788                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2789         return;
2790 
2791     case 0xc: /* LDLAR */
2792         if (!dc_isar_feature(aa64_lor, s)) {
2793             break;
2794         }
2795         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2796         /* fall through */
2797     case 0xd: /* LDAR */
2798         /* Generate ISS for non-exclusive accesses including LASR.  */
2799         if (rn == 31) {
2800             gen_check_sp_alignment(s);
2801         }
2802         memop = check_ordered_align(s, rn, 0, false, size);
2803         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2804                                     false, rn != 31, memop);
2805         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2806                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2807         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2808         return;
2809 
2810     case 0x2: case 0x3: /* CASP / STXP */
2811         if (size & 2) { /* STXP / STLXP */
2812             if (rn == 31) {
2813                 gen_check_sp_alignment(s);
2814             }
2815             if (is_lasr) {
2816                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2817             }
2818             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2819             return;
2820         }
2821         if (rt2 == 31
2822             && ((rt | rs) & 1) == 0
2823             && dc_isar_feature(aa64_atomics, s)) {
2824             /* CASP / CASPL */
2825             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2826             return;
2827         }
2828         break;
2829 
2830     case 0x6: case 0x7: /* CASPA / LDXP */
2831         if (size & 2) { /* LDXP / LDAXP */
2832             if (rn == 31) {
2833                 gen_check_sp_alignment(s);
2834             }
2835             gen_load_exclusive(s, rt, rt2, rn, size, true);
2836             if (is_lasr) {
2837                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2838             }
2839             return;
2840         }
2841         if (rt2 == 31
2842             && ((rt | rs) & 1) == 0
2843             && dc_isar_feature(aa64_atomics, s)) {
2844             /* CASPA / CASPAL */
2845             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2846             return;
2847         }
2848         break;
2849 
2850     case 0xa: /* CAS */
2851     case 0xb: /* CASL */
2852     case 0xe: /* CASA */
2853     case 0xf: /* CASAL */
2854         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2855             gen_compare_and_swap(s, rs, rt, rn, size);
2856             return;
2857         }
2858         break;
2859     }
2860     unallocated_encoding(s);
2861 }
2862 
2863 /*
2864  * Load register (literal)
2865  *
2866  *  31 30 29   27  26 25 24 23                5 4     0
2867  * +-----+-------+---+-----+-------------------+-------+
2868  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2869  * +-----+-------+---+-----+-------------------+-------+
2870  *
2871  * V: 1 -> vector (simd/fp)
2872  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2873  *                   10-> 32 bit signed, 11 -> prefetch
2874  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2875  */
2876 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2877 {
2878     int rt = extract32(insn, 0, 5);
2879     int64_t imm = sextract32(insn, 5, 19) << 2;
2880     bool is_vector = extract32(insn, 26, 1);
2881     int opc = extract32(insn, 30, 2);
2882     bool is_signed = false;
2883     int size = 2;
2884     TCGv_i64 tcg_rt, clean_addr;
2885     MemOp memop;
2886 
2887     if (is_vector) {
2888         if (opc == 3) {
2889             unallocated_encoding(s);
2890             return;
2891         }
2892         size = 2 + opc;
2893         if (!fp_access_check(s)) {
2894             return;
2895         }
2896         memop = finalize_memop_asimd(s, size);
2897     } else {
2898         if (opc == 3) {
2899             /* PRFM (literal) : prefetch */
2900             return;
2901         }
2902         size = 2 + extract32(opc, 0, 1);
2903         is_signed = extract32(opc, 1, 1);
2904         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2905     }
2906 
2907     tcg_rt = cpu_reg(s, rt);
2908 
2909     clean_addr = tcg_temp_new_i64();
2910     gen_pc_plus_diff(s, clean_addr, imm);
2911 
2912     if (is_vector) {
2913         do_fp_ld(s, rt, clean_addr, memop);
2914     } else {
2915         /* Only unsigned 32bit loads target 32bit registers.  */
2916         bool iss_sf = opc != 0;
2917         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2918     }
2919 }
2920 
2921 /*
2922  * LDNP (Load Pair - non-temporal hint)
2923  * LDP (Load Pair - non vector)
2924  * LDPSW (Load Pair Signed Word - non vector)
2925  * STNP (Store Pair - non-temporal hint)
2926  * STP (Store Pair - non vector)
2927  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2928  * LDP (Load Pair of SIMD&FP)
2929  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2930  * STP (Store Pair of SIMD&FP)
2931  *
2932  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2933  * +-----+-------+---+---+-------+---+-----------------------------+
2934  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2935  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2936  *
2937  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2938  *      LDPSW/STGP               01
2939  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2940  *   V: 0 -> GPR, 1 -> Vector
2941  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2942  *      10 -> signed offset, 11 -> pre-index
2943  *   L: 0 -> Store 1 -> Load
2944  *
2945  * Rt, Rt2 = GPR or SIMD registers to be stored
2946  * Rn = general purpose register containing address
2947  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2948  */
2949 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2950 {
2951     int rt = extract32(insn, 0, 5);
2952     int rn = extract32(insn, 5, 5);
2953     int rt2 = extract32(insn, 10, 5);
2954     uint64_t offset = sextract64(insn, 15, 7);
2955     int index = extract32(insn, 23, 2);
2956     bool is_vector = extract32(insn, 26, 1);
2957     bool is_load = extract32(insn, 22, 1);
2958     int opc = extract32(insn, 30, 2);
2959     bool is_signed = false;
2960     bool postindex = false;
2961     bool wback = false;
2962     bool set_tag = false;
2963     TCGv_i64 clean_addr, dirty_addr;
2964     MemOp mop;
2965     int size;
2966 
2967     if (opc == 3) {
2968         unallocated_encoding(s);
2969         return;
2970     }
2971 
2972     if (is_vector) {
2973         size = 2 + opc;
2974     } else if (opc == 1 && !is_load) {
2975         /* STGP */
2976         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2977             unallocated_encoding(s);
2978             return;
2979         }
2980         size = 3;
2981         set_tag = true;
2982     } else {
2983         size = 2 + extract32(opc, 1, 1);
2984         is_signed = extract32(opc, 0, 1);
2985         if (!is_load && is_signed) {
2986             unallocated_encoding(s);
2987             return;
2988         }
2989     }
2990 
2991     switch (index) {
2992     case 1: /* post-index */
2993         postindex = true;
2994         wback = true;
2995         break;
2996     case 0:
2997         /* signed offset with "non-temporal" hint. Since we don't emulate
2998          * caches we don't care about hints to the cache system about
2999          * data access patterns, and handle this identically to plain
3000          * signed offset.
3001          */
3002         if (is_signed) {
3003             /* There is no non-temporal-hint version of LDPSW */
3004             unallocated_encoding(s);
3005             return;
3006         }
3007         postindex = false;
3008         break;
3009     case 2: /* signed offset, rn not updated */
3010         postindex = false;
3011         break;
3012     case 3: /* pre-index */
3013         postindex = false;
3014         wback = true;
3015         break;
3016     }
3017 
3018     if (is_vector && !fp_access_check(s)) {
3019         return;
3020     }
3021 
3022     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
3023 
3024     if (rn == 31) {
3025         gen_check_sp_alignment(s);
3026     }
3027 
3028     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3029     if (!postindex) {
3030         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3031     }
3032 
3033     if (set_tag) {
3034         if (!s->ata) {
3035             /*
3036              * TODO: We could rely on the stores below, at least for
3037              * system mode, if we arrange to add MO_ALIGN_16.
3038              */
3039             gen_helper_stg_stub(cpu_env, dirty_addr);
3040         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3041             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3042         } else {
3043             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3044         }
3045     }
3046 
3047     if (is_vector) {
3048         mop = finalize_memop_asimd(s, size);
3049     } else {
3050         mop = finalize_memop(s, size);
3051     }
3052     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3053                                 (wback || rn != 31) && !set_tag,
3054                                 2 << size, mop);
3055 
3056     if (is_vector) {
3057         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3058         if (is_load) {
3059             do_fp_ld(s, rt, clean_addr, mop);
3060         } else {
3061             do_fp_st(s, rt, clean_addr, mop);
3062         }
3063         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3064         if (is_load) {
3065             do_fp_ld(s, rt2, clean_addr, mop);
3066         } else {
3067             do_fp_st(s, rt2, clean_addr, mop);
3068         }
3069     } else {
3070         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3071         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3072 
3073         /*
3074          * We built mop above for the single logical access -- rebuild it
3075          * now for the paired operation.
3076          *
3077          * With LSE2, non-sign-extending pairs are treated atomically if
3078          * aligned, and if unaligned one of the pair will be completely
3079          * within a 16-byte block and that element will be atomic.
3080          * Otherwise each element is separately atomic.
3081          * In all cases, issue one operation with the correct atomicity.
3082          *
3083          * This treats sign-extending loads like zero-extending loads,
3084          * since that reuses the most code below.
3085          */
3086         mop = size + 1;
3087         if (s->align_mem) {
3088             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3089         }
3090         mop = finalize_memop_pair(s, mop);
3091 
3092         if (is_load) {
3093             if (size == 2) {
3094                 int o2 = s->be_data == MO_LE ? 32 : 0;
3095                 int o1 = o2 ^ 32;
3096 
3097                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3098                 if (is_signed) {
3099                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3100                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3101                 } else {
3102                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3103                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3104                 }
3105             } else {
3106                 TCGv_i128 tmp = tcg_temp_new_i128();
3107 
3108                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3109                 if (s->be_data == MO_LE) {
3110                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3111                 } else {
3112                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3113                 }
3114             }
3115         } else {
3116             if (size == 2) {
3117                 TCGv_i64 tmp = tcg_temp_new_i64();
3118 
3119                 if (s->be_data == MO_LE) {
3120                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3121                 } else {
3122                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3123                 }
3124                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3125             } else {
3126                 TCGv_i128 tmp = tcg_temp_new_i128();
3127 
3128                 if (s->be_data == MO_LE) {
3129                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3130                 } else {
3131                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3132                 }
3133                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3134             }
3135         }
3136     }
3137 
3138     if (wback) {
3139         if (postindex) {
3140             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3141         }
3142         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3143     }
3144 }
3145 
3146 /*
3147  * Load/store (immediate post-indexed)
3148  * Load/store (immediate pre-indexed)
3149  * Load/store (unscaled immediate)
3150  *
3151  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3152  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3153  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3154  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3155  *
3156  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3157          10 -> unprivileged
3158  * V = 0 -> non-vector
3159  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3160  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3161  */
3162 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3163                                 int opc,
3164                                 int size,
3165                                 int rt,
3166                                 bool is_vector)
3167 {
3168     int rn = extract32(insn, 5, 5);
3169     int imm9 = sextract32(insn, 12, 9);
3170     int idx = extract32(insn, 10, 2);
3171     bool is_signed = false;
3172     bool is_store = false;
3173     bool is_extended = false;
3174     bool is_unpriv = (idx == 2);
3175     bool iss_valid;
3176     bool post_index;
3177     bool writeback;
3178     int memidx;
3179     MemOp memop;
3180     TCGv_i64 clean_addr, dirty_addr;
3181 
3182     if (is_vector) {
3183         size |= (opc & 2) << 1;
3184         if (size > 4 || is_unpriv) {
3185             unallocated_encoding(s);
3186             return;
3187         }
3188         is_store = ((opc & 1) == 0);
3189         if (!fp_access_check(s)) {
3190             return;
3191         }
3192         memop = finalize_memop_asimd(s, size);
3193     } else {
3194         if (size == 3 && opc == 2) {
3195             /* PRFM - prefetch */
3196             if (idx != 0) {
3197                 unallocated_encoding(s);
3198                 return;
3199             }
3200             return;
3201         }
3202         if (opc == 3 && size > 1) {
3203             unallocated_encoding(s);
3204             return;
3205         }
3206         is_store = (opc == 0);
3207         is_signed = !is_store && extract32(opc, 1, 1);
3208         is_extended = (size < 3) && extract32(opc, 0, 1);
3209         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3210     }
3211 
3212     switch (idx) {
3213     case 0:
3214     case 2:
3215         post_index = false;
3216         writeback = false;
3217         break;
3218     case 1:
3219         post_index = true;
3220         writeback = true;
3221         break;
3222     case 3:
3223         post_index = false;
3224         writeback = true;
3225         break;
3226     default:
3227         g_assert_not_reached();
3228     }
3229 
3230     iss_valid = !is_vector && !writeback;
3231 
3232     if (rn == 31) {
3233         gen_check_sp_alignment(s);
3234     }
3235 
3236     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3237     if (!post_index) {
3238         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3239     }
3240 
3241     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3242 
3243     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3244                                        writeback || rn != 31,
3245                                        memop, is_unpriv, memidx);
3246 
3247     if (is_vector) {
3248         if (is_store) {
3249             do_fp_st(s, rt, clean_addr, memop);
3250         } else {
3251             do_fp_ld(s, rt, clean_addr, memop);
3252         }
3253     } else {
3254         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3255         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3256 
3257         if (is_store) {
3258             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3259                              iss_valid, rt, iss_sf, false);
3260         } else {
3261             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3262                              is_extended, memidx,
3263                              iss_valid, rt, iss_sf, false);
3264         }
3265     }
3266 
3267     if (writeback) {
3268         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3269         if (post_index) {
3270             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3271         }
3272         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3273     }
3274 }
3275 
3276 /*
3277  * Load/store (register offset)
3278  *
3279  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3280  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3281  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3282  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3283  *
3284  * For non-vector:
3285  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3286  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3287  * For vector:
3288  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3289  *   opc<0>: 0 -> store, 1 -> load
3290  * V: 1 -> vector/simd
3291  * opt: extend encoding (see DecodeRegExtend)
3292  * S: if S=1 then scale (essentially index by sizeof(size))
3293  * Rt: register to transfer into/out of
3294  * Rn: address register or SP for base
3295  * Rm: offset register or ZR for offset
3296  */
3297 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3298                                    int opc,
3299                                    int size,
3300                                    int rt,
3301                                    bool is_vector)
3302 {
3303     int rn = extract32(insn, 5, 5);
3304     int shift = extract32(insn, 12, 1);
3305     int rm = extract32(insn, 16, 5);
3306     int opt = extract32(insn, 13, 3);
3307     bool is_signed = false;
3308     bool is_store = false;
3309     bool is_extended = false;
3310     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3311     MemOp memop;
3312 
3313     if (extract32(opt, 1, 1) == 0) {
3314         unallocated_encoding(s);
3315         return;
3316     }
3317 
3318     if (is_vector) {
3319         size |= (opc & 2) << 1;
3320         if (size > 4) {
3321             unallocated_encoding(s);
3322             return;
3323         }
3324         is_store = !extract32(opc, 0, 1);
3325         if (!fp_access_check(s)) {
3326             return;
3327         }
3328         memop = finalize_memop_asimd(s, size);
3329     } else {
3330         if (size == 3 && opc == 2) {
3331             /* PRFM - prefetch */
3332             return;
3333         }
3334         if (opc == 3 && size > 1) {
3335             unallocated_encoding(s);
3336             return;
3337         }
3338         is_store = (opc == 0);
3339         is_signed = !is_store && extract32(opc, 1, 1);
3340         is_extended = (size < 3) && extract32(opc, 0, 1);
3341         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3342     }
3343 
3344     if (rn == 31) {
3345         gen_check_sp_alignment(s);
3346     }
3347     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3348 
3349     tcg_rm = read_cpu_reg(s, rm, 1);
3350     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3351 
3352     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3353 
3354     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3355 
3356     if (is_vector) {
3357         if (is_store) {
3358             do_fp_st(s, rt, clean_addr, memop);
3359         } else {
3360             do_fp_ld(s, rt, clean_addr, memop);
3361         }
3362     } else {
3363         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3364         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3365 
3366         if (is_store) {
3367             do_gpr_st(s, tcg_rt, clean_addr, memop,
3368                       true, rt, iss_sf, false);
3369         } else {
3370             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3371                       is_extended, true, rt, iss_sf, false);
3372         }
3373     }
3374 }
3375 
3376 /*
3377  * Load/store (unsigned immediate)
3378  *
3379  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3380  * +----+-------+---+-----+-----+------------+-------+------+
3381  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3382  * +----+-------+---+-----+-----+------------+-------+------+
3383  *
3384  * For non-vector:
3385  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3386  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3387  * For vector:
3388  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3389  *   opc<0>: 0 -> store, 1 -> load
3390  * Rn: base address register (inc SP)
3391  * Rt: target register
3392  */
3393 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3394                                         int opc,
3395                                         int size,
3396                                         int rt,
3397                                         bool is_vector)
3398 {
3399     int rn = extract32(insn, 5, 5);
3400     unsigned int imm12 = extract32(insn, 10, 12);
3401     unsigned int offset;
3402     TCGv_i64 clean_addr, dirty_addr;
3403     bool is_store;
3404     bool is_signed = false;
3405     bool is_extended = false;
3406     MemOp memop;
3407 
3408     if (is_vector) {
3409         size |= (opc & 2) << 1;
3410         if (size > 4) {
3411             unallocated_encoding(s);
3412             return;
3413         }
3414         is_store = !extract32(opc, 0, 1);
3415         if (!fp_access_check(s)) {
3416             return;
3417         }
3418         memop = finalize_memop_asimd(s, size);
3419     } else {
3420         if (size == 3 && opc == 2) {
3421             /* PRFM - prefetch */
3422             return;
3423         }
3424         if (opc == 3 && size > 1) {
3425             unallocated_encoding(s);
3426             return;
3427         }
3428         is_store = (opc == 0);
3429         is_signed = !is_store && extract32(opc, 1, 1);
3430         is_extended = (size < 3) && extract32(opc, 0, 1);
3431         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3432     }
3433 
3434     if (rn == 31) {
3435         gen_check_sp_alignment(s);
3436     }
3437     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3438     offset = imm12 << size;
3439     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3440 
3441     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3442 
3443     if (is_vector) {
3444         if (is_store) {
3445             do_fp_st(s, rt, clean_addr, memop);
3446         } else {
3447             do_fp_ld(s, rt, clean_addr, memop);
3448         }
3449     } else {
3450         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3451         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3452         if (is_store) {
3453             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3454         } else {
3455             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3456                       is_extended, true, rt, iss_sf, false);
3457         }
3458     }
3459 }
3460 
3461 /* Atomic memory operations
3462  *
3463  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3464  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3465  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3466  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3467  *
3468  * Rt: the result register
3469  * Rn: base address or SP
3470  * Rs: the source register for the operation
3471  * V: vector flag (always 0 as of v8.3)
3472  * A: acquire flag
3473  * R: release flag
3474  */
3475 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3476                               int size, int rt, bool is_vector)
3477 {
3478     int rs = extract32(insn, 16, 5);
3479     int rn = extract32(insn, 5, 5);
3480     int o3_opc = extract32(insn, 12, 4);
3481     bool r = extract32(insn, 22, 1);
3482     bool a = extract32(insn, 23, 1);
3483     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3484     AtomicThreeOpFn *fn = NULL;
3485     MemOp mop = size;
3486 
3487     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3488         unallocated_encoding(s);
3489         return;
3490     }
3491     switch (o3_opc) {
3492     case 000: /* LDADD */
3493         fn = tcg_gen_atomic_fetch_add_i64;
3494         break;
3495     case 001: /* LDCLR */
3496         fn = tcg_gen_atomic_fetch_and_i64;
3497         break;
3498     case 002: /* LDEOR */
3499         fn = tcg_gen_atomic_fetch_xor_i64;
3500         break;
3501     case 003: /* LDSET */
3502         fn = tcg_gen_atomic_fetch_or_i64;
3503         break;
3504     case 004: /* LDSMAX */
3505         fn = tcg_gen_atomic_fetch_smax_i64;
3506         mop |= MO_SIGN;
3507         break;
3508     case 005: /* LDSMIN */
3509         fn = tcg_gen_atomic_fetch_smin_i64;
3510         mop |= MO_SIGN;
3511         break;
3512     case 006: /* LDUMAX */
3513         fn = tcg_gen_atomic_fetch_umax_i64;
3514         break;
3515     case 007: /* LDUMIN */
3516         fn = tcg_gen_atomic_fetch_umin_i64;
3517         break;
3518     case 010: /* SWP */
3519         fn = tcg_gen_atomic_xchg_i64;
3520         break;
3521     case 014: /* LDAPR, LDAPRH, LDAPRB */
3522         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3523             rs != 31 || a != 1 || r != 0) {
3524             unallocated_encoding(s);
3525             return;
3526         }
3527         break;
3528     default:
3529         unallocated_encoding(s);
3530         return;
3531     }
3532 
3533     if (rn == 31) {
3534         gen_check_sp_alignment(s);
3535     }
3536 
3537     mop = check_atomic_align(s, rn, mop);
3538     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3539 
3540     if (o3_opc == 014) {
3541         /*
3542          * LDAPR* are a special case because they are a simple load, not a
3543          * fetch-and-do-something op.
3544          * The architectural consistency requirements here are weaker than
3545          * full load-acquire (we only need "load-acquire processor consistent"),
3546          * but we choose to implement them as full LDAQ.
3547          */
3548         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3549                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3550         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3551         return;
3552     }
3553 
3554     tcg_rs = read_cpu_reg(s, rs, true);
3555     tcg_rt = cpu_reg(s, rt);
3556 
3557     if (o3_opc == 1) { /* LDCLR */
3558         tcg_gen_not_i64(tcg_rs, tcg_rs);
3559     }
3560 
3561     /* The tcg atomic primitives are all full barriers.  Therefore we
3562      * can ignore the Acquire and Release bits of this instruction.
3563      */
3564     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3565 
3566     if (mop & MO_SIGN) {
3567         switch (size) {
3568         case MO_8:
3569             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3570             break;
3571         case MO_16:
3572             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3573             break;
3574         case MO_32:
3575             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3576             break;
3577         case MO_64:
3578             break;
3579         default:
3580             g_assert_not_reached();
3581         }
3582     }
3583 }
3584 
3585 /*
3586  * PAC memory operations
3587  *
3588  *  31  30      27  26    24    22  21       12  11  10    5     0
3589  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3590  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3591  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3592  *
3593  * Rt: the result register
3594  * Rn: base address or SP
3595  * V: vector flag (always 0 as of v8.3)
3596  * M: clear for key DA, set for key DB
3597  * W: pre-indexing flag
3598  * S: sign for imm9.
3599  */
3600 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3601                            int size, int rt, bool is_vector)
3602 {
3603     int rn = extract32(insn, 5, 5);
3604     bool is_wback = extract32(insn, 11, 1);
3605     bool use_key_a = !extract32(insn, 23, 1);
3606     int offset;
3607     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3608     MemOp memop;
3609 
3610     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3611         unallocated_encoding(s);
3612         return;
3613     }
3614 
3615     if (rn == 31) {
3616         gen_check_sp_alignment(s);
3617     }
3618     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3619 
3620     if (s->pauth_active) {
3621         if (use_key_a) {
3622             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3623                              tcg_constant_i64(0));
3624         } else {
3625             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3626                              tcg_constant_i64(0));
3627         }
3628     }
3629 
3630     /* Form the 10-bit signed, scaled offset.  */
3631     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3632     offset = sextract32(offset << size, 0, 10 + size);
3633     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3634 
3635     memop = finalize_memop(s, size);
3636 
3637     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3638     clean_addr = gen_mte_check1(s, dirty_addr, false,
3639                                 is_wback || rn != 31, memop);
3640 
3641     tcg_rt = cpu_reg(s, rt);
3642     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3643               /* extend */ false, /* iss_valid */ !is_wback,
3644               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3645 
3646     if (is_wback) {
3647         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3648     }
3649 }
3650 
3651 /*
3652  * LDAPR/STLR (unscaled immediate)
3653  *
3654  *  31  30            24    22  21       12    10    5     0
3655  * +------+-------------+-----+---+--------+-----+----+-----+
3656  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3657  * +------+-------------+-----+---+--------+-----+----+-----+
3658  *
3659  * Rt: source or destination register
3660  * Rn: base register
3661  * imm9: unscaled immediate offset
3662  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3663  * size: size of load/store
3664  */
3665 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3666 {
3667     int rt = extract32(insn, 0, 5);
3668     int rn = extract32(insn, 5, 5);
3669     int offset = sextract32(insn, 12, 9);
3670     int opc = extract32(insn, 22, 2);
3671     int size = extract32(insn, 30, 2);
3672     TCGv_i64 clean_addr, dirty_addr;
3673     bool is_store = false;
3674     bool extend = false;
3675     bool iss_sf;
3676     MemOp mop = size;
3677 
3678     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3679         unallocated_encoding(s);
3680         return;
3681     }
3682 
3683     switch (opc) {
3684     case 0: /* STLURB */
3685         is_store = true;
3686         break;
3687     case 1: /* LDAPUR* */
3688         break;
3689     case 2: /* LDAPURS* 64-bit variant */
3690         if (size == 3) {
3691             unallocated_encoding(s);
3692             return;
3693         }
3694         mop |= MO_SIGN;
3695         break;
3696     case 3: /* LDAPURS* 32-bit variant */
3697         if (size > 1) {
3698             unallocated_encoding(s);
3699             return;
3700         }
3701         mop |= MO_SIGN;
3702         extend = true; /* zero-extend 32->64 after signed load */
3703         break;
3704     default:
3705         g_assert_not_reached();
3706     }
3707 
3708     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3709 
3710     if (rn == 31) {
3711         gen_check_sp_alignment(s);
3712     }
3713 
3714     mop = check_ordered_align(s, rn, offset, is_store, mop);
3715 
3716     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3717     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3718     clean_addr = clean_data_tbi(s, dirty_addr);
3719 
3720     if (is_store) {
3721         /* Store-Release semantics */
3722         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3723         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3724     } else {
3725         /*
3726          * Load-AcquirePC semantics; we implement as the slightly more
3727          * restrictive Load-Acquire.
3728          */
3729         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3730                   extend, true, rt, iss_sf, true);
3731         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3732     }
3733 }
3734 
3735 /* Load/store register (all forms) */
3736 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3737 {
3738     int rt = extract32(insn, 0, 5);
3739     int opc = extract32(insn, 22, 2);
3740     bool is_vector = extract32(insn, 26, 1);
3741     int size = extract32(insn, 30, 2);
3742 
3743     switch (extract32(insn, 24, 2)) {
3744     case 0:
3745         if (extract32(insn, 21, 1) == 0) {
3746             /* Load/store register (unscaled immediate)
3747              * Load/store immediate pre/post-indexed
3748              * Load/store register unprivileged
3749              */
3750             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3751             return;
3752         }
3753         switch (extract32(insn, 10, 2)) {
3754         case 0:
3755             disas_ldst_atomic(s, insn, size, rt, is_vector);
3756             return;
3757         case 2:
3758             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3759             return;
3760         default:
3761             disas_ldst_pac(s, insn, size, rt, is_vector);
3762             return;
3763         }
3764         break;
3765     case 1:
3766         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3767         return;
3768     }
3769     unallocated_encoding(s);
3770 }
3771 
3772 /* AdvSIMD load/store multiple structures
3773  *
3774  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3775  * +---+---+---------------+---+-------------+--------+------+------+------+
3776  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3777  * +---+---+---------------+---+-------------+--------+------+------+------+
3778  *
3779  * AdvSIMD load/store multiple structures (post-indexed)
3780  *
3781  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3782  * +---+---+---------------+---+---+---------+--------+------+------+------+
3783  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3784  * +---+---+---------------+---+---+---------+--------+------+------+------+
3785  *
3786  * Rt: first (or only) SIMD&FP register to be transferred
3787  * Rn: base address or SP
3788  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3789  */
3790 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3791 {
3792     int rt = extract32(insn, 0, 5);
3793     int rn = extract32(insn, 5, 5);
3794     int rm = extract32(insn, 16, 5);
3795     int size = extract32(insn, 10, 2);
3796     int opcode = extract32(insn, 12, 4);
3797     bool is_store = !extract32(insn, 22, 1);
3798     bool is_postidx = extract32(insn, 23, 1);
3799     bool is_q = extract32(insn, 30, 1);
3800     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3801     MemOp endian, align, mop;
3802 
3803     int total;    /* total bytes */
3804     int elements; /* elements per vector */
3805     int rpt;    /* num iterations */
3806     int selem;  /* structure elements */
3807     int r;
3808 
3809     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3810         unallocated_encoding(s);
3811         return;
3812     }
3813 
3814     if (!is_postidx && rm != 0) {
3815         unallocated_encoding(s);
3816         return;
3817     }
3818 
3819     /* From the shared decode logic */
3820     switch (opcode) {
3821     case 0x0:
3822         rpt = 1;
3823         selem = 4;
3824         break;
3825     case 0x2:
3826         rpt = 4;
3827         selem = 1;
3828         break;
3829     case 0x4:
3830         rpt = 1;
3831         selem = 3;
3832         break;
3833     case 0x6:
3834         rpt = 3;
3835         selem = 1;
3836         break;
3837     case 0x7:
3838         rpt = 1;
3839         selem = 1;
3840         break;
3841     case 0x8:
3842         rpt = 1;
3843         selem = 2;
3844         break;
3845     case 0xa:
3846         rpt = 2;
3847         selem = 1;
3848         break;
3849     default:
3850         unallocated_encoding(s);
3851         return;
3852     }
3853 
3854     if (size == 3 && !is_q && selem != 1) {
3855         /* reserved */
3856         unallocated_encoding(s);
3857         return;
3858     }
3859 
3860     if (!fp_access_check(s)) {
3861         return;
3862     }
3863 
3864     if (rn == 31) {
3865         gen_check_sp_alignment(s);
3866     }
3867 
3868     /* For our purposes, bytes are always little-endian.  */
3869     endian = s->be_data;
3870     if (size == 0) {
3871         endian = MO_LE;
3872     }
3873 
3874     total = rpt * selem * (is_q ? 16 : 8);
3875     tcg_rn = cpu_reg_sp(s, rn);
3876 
3877     /*
3878      * Issue the MTE check vs the logical repeat count, before we
3879      * promote consecutive little-endian elements below.
3880      */
3881     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3882                                 total, finalize_memop_asimd(s, size));
3883 
3884     /*
3885      * Consecutive little-endian elements from a single register
3886      * can be promoted to a larger little-endian operation.
3887      */
3888     align = MO_ALIGN;
3889     if (selem == 1 && endian == MO_LE) {
3890         align = pow2_align(size);
3891         size = 3;
3892     }
3893     if (!s->align_mem) {
3894         align = 0;
3895     }
3896     mop = endian | size | align;
3897 
3898     elements = (is_q ? 16 : 8) >> size;
3899     tcg_ebytes = tcg_constant_i64(1 << size);
3900     for (r = 0; r < rpt; r++) {
3901         int e;
3902         for (e = 0; e < elements; e++) {
3903             int xs;
3904             for (xs = 0; xs < selem; xs++) {
3905                 int tt = (rt + r + xs) % 32;
3906                 if (is_store) {
3907                     do_vec_st(s, tt, e, clean_addr, mop);
3908                 } else {
3909                     do_vec_ld(s, tt, e, clean_addr, mop);
3910                 }
3911                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3912             }
3913         }
3914     }
3915 
3916     if (!is_store) {
3917         /* For non-quad operations, setting a slice of the low
3918          * 64 bits of the register clears the high 64 bits (in
3919          * the ARM ARM pseudocode this is implicit in the fact
3920          * that 'rval' is a 64 bit wide variable).
3921          * For quad operations, we might still need to zero the
3922          * high bits of SVE.
3923          */
3924         for (r = 0; r < rpt * selem; r++) {
3925             int tt = (rt + r) % 32;
3926             clear_vec_high(s, is_q, tt);
3927         }
3928     }
3929 
3930     if (is_postidx) {
3931         if (rm == 31) {
3932             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3933         } else {
3934             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3935         }
3936     }
3937 }
3938 
3939 /* AdvSIMD load/store single structure
3940  *
3941  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3942  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3943  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3944  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3945  *
3946  * AdvSIMD load/store single structure (post-indexed)
3947  *
3948  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3949  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3950  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3951  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3952  *
3953  * Rt: first (or only) SIMD&FP register to be transferred
3954  * Rn: base address or SP
3955  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3956  * index = encoded in Q:S:size dependent on size
3957  *
3958  * lane_size = encoded in R, opc
3959  * transfer width = encoded in opc, S, size
3960  */
3961 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3962 {
3963     int rt = extract32(insn, 0, 5);
3964     int rn = extract32(insn, 5, 5);
3965     int rm = extract32(insn, 16, 5);
3966     int size = extract32(insn, 10, 2);
3967     int S = extract32(insn, 12, 1);
3968     int opc = extract32(insn, 13, 3);
3969     int R = extract32(insn, 21, 1);
3970     int is_load = extract32(insn, 22, 1);
3971     int is_postidx = extract32(insn, 23, 1);
3972     int is_q = extract32(insn, 30, 1);
3973 
3974     int scale = extract32(opc, 1, 2);
3975     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3976     bool replicate = false;
3977     int index = is_q << 3 | S << 2 | size;
3978     int xs, total;
3979     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3980     MemOp mop;
3981 
3982     if (extract32(insn, 31, 1)) {
3983         unallocated_encoding(s);
3984         return;
3985     }
3986     if (!is_postidx && rm != 0) {
3987         unallocated_encoding(s);
3988         return;
3989     }
3990 
3991     switch (scale) {
3992     case 3:
3993         if (!is_load || S) {
3994             unallocated_encoding(s);
3995             return;
3996         }
3997         scale = size;
3998         replicate = true;
3999         break;
4000     case 0:
4001         break;
4002     case 1:
4003         if (extract32(size, 0, 1)) {
4004             unallocated_encoding(s);
4005             return;
4006         }
4007         index >>= 1;
4008         break;
4009     case 2:
4010         if (extract32(size, 1, 1)) {
4011             unallocated_encoding(s);
4012             return;
4013         }
4014         if (!extract32(size, 0, 1)) {
4015             index >>= 2;
4016         } else {
4017             if (S) {
4018                 unallocated_encoding(s);
4019                 return;
4020             }
4021             index >>= 3;
4022             scale = 3;
4023         }
4024         break;
4025     default:
4026         g_assert_not_reached();
4027     }
4028 
4029     if (!fp_access_check(s)) {
4030         return;
4031     }
4032 
4033     if (rn == 31) {
4034         gen_check_sp_alignment(s);
4035     }
4036 
4037     total = selem << scale;
4038     tcg_rn = cpu_reg_sp(s, rn);
4039 
4040     mop = finalize_memop_asimd(s, scale);
4041 
4042     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4043                                 total, mop);
4044 
4045     tcg_ebytes = tcg_constant_i64(1 << scale);
4046     for (xs = 0; xs < selem; xs++) {
4047         if (replicate) {
4048             /* Load and replicate to all elements */
4049             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4050 
4051             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4052             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4053                                  (is_q + 1) * 8, vec_full_reg_size(s),
4054                                  tcg_tmp);
4055         } else {
4056             /* Load/store one element per register */
4057             if (is_load) {
4058                 do_vec_ld(s, rt, index, clean_addr, mop);
4059             } else {
4060                 do_vec_st(s, rt, index, clean_addr, mop);
4061             }
4062         }
4063         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4064         rt = (rt + 1) % 32;
4065     }
4066 
4067     if (is_postidx) {
4068         if (rm == 31) {
4069             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4070         } else {
4071             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4072         }
4073     }
4074 }
4075 
4076 /*
4077  * Load/Store memory tags
4078  *
4079  *  31 30 29         24     22  21     12    10      5      0
4080  * +-----+-------------+-----+---+------+-----+------+------+
4081  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4082  * +-----+-------------+-----+---+------+-----+------+------+
4083  */
4084 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4085 {
4086     int rt = extract32(insn, 0, 5);
4087     int rn = extract32(insn, 5, 5);
4088     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4089     int op2 = extract32(insn, 10, 2);
4090     int op1 = extract32(insn, 22, 2);
4091     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4092     int index = 0;
4093     TCGv_i64 addr, clean_addr, tcg_rt;
4094 
4095     /* We checked insn bits [29:24,21] in the caller.  */
4096     if (extract32(insn, 30, 2) != 3) {
4097         goto do_unallocated;
4098     }
4099 
4100     /*
4101      * @index is a tri-state variable which has 3 states:
4102      * < 0 : post-index, writeback
4103      * = 0 : signed offset
4104      * > 0 : pre-index, writeback
4105      */
4106     switch (op1) {
4107     case 0:
4108         if (op2 != 0) {
4109             /* STG */
4110             index = op2 - 2;
4111         } else {
4112             /* STZGM */
4113             if (s->current_el == 0 || offset != 0) {
4114                 goto do_unallocated;
4115             }
4116             is_mult = is_zero = true;
4117         }
4118         break;
4119     case 1:
4120         if (op2 != 0) {
4121             /* STZG */
4122             is_zero = true;
4123             index = op2 - 2;
4124         } else {
4125             /* LDG */
4126             is_load = true;
4127         }
4128         break;
4129     case 2:
4130         if (op2 != 0) {
4131             /* ST2G */
4132             is_pair = true;
4133             index = op2 - 2;
4134         } else {
4135             /* STGM */
4136             if (s->current_el == 0 || offset != 0) {
4137                 goto do_unallocated;
4138             }
4139             is_mult = true;
4140         }
4141         break;
4142     case 3:
4143         if (op2 != 0) {
4144             /* STZ2G */
4145             is_pair = is_zero = true;
4146             index = op2 - 2;
4147         } else {
4148             /* LDGM */
4149             if (s->current_el == 0 || offset != 0) {
4150                 goto do_unallocated;
4151             }
4152             is_mult = is_load = true;
4153         }
4154         break;
4155 
4156     default:
4157     do_unallocated:
4158         unallocated_encoding(s);
4159         return;
4160     }
4161 
4162     if (is_mult
4163         ? !dc_isar_feature(aa64_mte, s)
4164         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4165         goto do_unallocated;
4166     }
4167 
4168     if (rn == 31) {
4169         gen_check_sp_alignment(s);
4170     }
4171 
4172     addr = read_cpu_reg_sp(s, rn, true);
4173     if (index >= 0) {
4174         /* pre-index or signed offset */
4175         tcg_gen_addi_i64(addr, addr, offset);
4176     }
4177 
4178     if (is_mult) {
4179         tcg_rt = cpu_reg(s, rt);
4180 
4181         if (is_zero) {
4182             int size = 4 << s->dcz_blocksize;
4183 
4184             if (s->ata) {
4185                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4186             }
4187             /*
4188              * The non-tags portion of STZGM is mostly like DC_ZVA,
4189              * except the alignment happens before the access.
4190              */
4191             clean_addr = clean_data_tbi(s, addr);
4192             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4193             gen_helper_dc_zva(cpu_env, clean_addr);
4194         } else if (s->ata) {
4195             if (is_load) {
4196                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4197             } else {
4198                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4199             }
4200         } else {
4201             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4202             int size = 4 << GMID_EL1_BS;
4203 
4204             clean_addr = clean_data_tbi(s, addr);
4205             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4206             gen_probe_access(s, clean_addr, acc, size);
4207 
4208             if (is_load) {
4209                 /* The result tags are zeros.  */
4210                 tcg_gen_movi_i64(tcg_rt, 0);
4211             }
4212         }
4213         return;
4214     }
4215 
4216     if (is_load) {
4217         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4218         tcg_rt = cpu_reg(s, rt);
4219         if (s->ata) {
4220             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4221         } else {
4222             /*
4223              * Tag access disabled: we must check for aborts on the load
4224              * load from [rn+offset], and then insert a 0 tag into rt.
4225              */
4226             clean_addr = clean_data_tbi(s, addr);
4227             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4228             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4229         }
4230     } else {
4231         tcg_rt = cpu_reg_sp(s, rt);
4232         if (!s->ata) {
4233             /*
4234              * For STG and ST2G, we need to check alignment and probe memory.
4235              * TODO: For STZG and STZ2G, we could rely on the stores below,
4236              * at least for system mode; user-only won't enforce alignment.
4237              */
4238             if (is_pair) {
4239                 gen_helper_st2g_stub(cpu_env, addr);
4240             } else {
4241                 gen_helper_stg_stub(cpu_env, addr);
4242             }
4243         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4244             if (is_pair) {
4245                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4246             } else {
4247                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4248             }
4249         } else {
4250             if (is_pair) {
4251                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4252             } else {
4253                 gen_helper_stg(cpu_env, addr, tcg_rt);
4254             }
4255         }
4256     }
4257 
4258     if (is_zero) {
4259         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4260         TCGv_i64 zero64 = tcg_constant_i64(0);
4261         TCGv_i128 zero128 = tcg_temp_new_i128();
4262         int mem_index = get_mem_index(s);
4263         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4264 
4265         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4266 
4267         /* This is 1 or 2 atomic 16-byte operations. */
4268         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4269         if (is_pair) {
4270             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4271             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4272         }
4273     }
4274 
4275     if (index != 0) {
4276         /* pre-index or post-index */
4277         if (index < 0) {
4278             /* post-index */
4279             tcg_gen_addi_i64(addr, addr, offset);
4280         }
4281         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4282     }
4283 }
4284 
4285 /* Loads and stores */
4286 static void disas_ldst(DisasContext *s, uint32_t insn)
4287 {
4288     switch (extract32(insn, 24, 6)) {
4289     case 0x08: /* Load/store exclusive */
4290         disas_ldst_excl(s, insn);
4291         break;
4292     case 0x18: case 0x1c: /* Load register (literal) */
4293         disas_ld_lit(s, insn);
4294         break;
4295     case 0x28: case 0x29:
4296     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4297         disas_ldst_pair(s, insn);
4298         break;
4299     case 0x38: case 0x39:
4300     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4301         disas_ldst_reg(s, insn);
4302         break;
4303     case 0x0c: /* AdvSIMD load/store multiple structures */
4304         disas_ldst_multiple_struct(s, insn);
4305         break;
4306     case 0x0d: /* AdvSIMD load/store single structure */
4307         disas_ldst_single_struct(s, insn);
4308         break;
4309     case 0x19:
4310         if (extract32(insn, 21, 1) != 0) {
4311             disas_ldst_tag(s, insn);
4312         } else if (extract32(insn, 10, 2) == 0) {
4313             disas_ldst_ldapr_stlr(s, insn);
4314         } else {
4315             unallocated_encoding(s);
4316         }
4317         break;
4318     default:
4319         unallocated_encoding(s);
4320         break;
4321     }
4322 }
4323 
4324 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4325 
4326 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4327                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4328 {
4329     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4330     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4331     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4332 
4333     fn(tcg_rd, tcg_rn, tcg_imm);
4334     if (!a->sf) {
4335         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4336     }
4337     return true;
4338 }
4339 
4340 /*
4341  * PC-rel. addressing
4342  */
4343 
4344 static bool trans_ADR(DisasContext *s, arg_ri *a)
4345 {
4346     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4347     return true;
4348 }
4349 
4350 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4351 {
4352     int64_t offset = (int64_t)a->imm << 12;
4353 
4354     /* The page offset is ok for CF_PCREL. */
4355     offset -= s->pc_curr & 0xfff;
4356     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4357     return true;
4358 }
4359 
4360 /*
4361  * Add/subtract (immediate)
4362  */
4363 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4364 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4365 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4366 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4367 
4368 /*
4369  * Add/subtract (immediate, with tags)
4370  */
4371 
4372 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4373                                       bool sub_op)
4374 {
4375     TCGv_i64 tcg_rn, tcg_rd;
4376     int imm;
4377 
4378     imm = a->uimm6 << LOG2_TAG_GRANULE;
4379     if (sub_op) {
4380         imm = -imm;
4381     }
4382 
4383     tcg_rn = cpu_reg_sp(s, a->rn);
4384     tcg_rd = cpu_reg_sp(s, a->rd);
4385 
4386     if (s->ata) {
4387         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4388                            tcg_constant_i32(imm),
4389                            tcg_constant_i32(a->uimm4));
4390     } else {
4391         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4392         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4393     }
4394     return true;
4395 }
4396 
4397 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4398 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4399 
4400 /* The input should be a value in the bottom e bits (with higher
4401  * bits zero); returns that value replicated into every element
4402  * of size e in a 64 bit integer.
4403  */
4404 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4405 {
4406     assert(e != 0);
4407     while (e < 64) {
4408         mask |= mask << e;
4409         e *= 2;
4410     }
4411     return mask;
4412 }
4413 
4414 /*
4415  * Logical (immediate)
4416  */
4417 
4418 /*
4419  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4420  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4421  * value (ie should cause a guest UNDEF exception), and true if they are
4422  * valid, in which case the decoded bit pattern is written to result.
4423  */
4424 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4425                             unsigned int imms, unsigned int immr)
4426 {
4427     uint64_t mask;
4428     unsigned e, levels, s, r;
4429     int len;
4430 
4431     assert(immn < 2 && imms < 64 && immr < 64);
4432 
4433     /* The bit patterns we create here are 64 bit patterns which
4434      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4435      * 64 bits each. Each element contains the same value: a run
4436      * of between 1 and e-1 non-zero bits, rotated within the
4437      * element by between 0 and e-1 bits.
4438      *
4439      * The element size and run length are encoded into immn (1 bit)
4440      * and imms (6 bits) as follows:
4441      * 64 bit elements: immn = 1, imms = <length of run - 1>
4442      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4443      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4444      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4445      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4446      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4447      * Notice that immn = 0, imms = 11111x is the only combination
4448      * not covered by one of the above options; this is reserved.
4449      * Further, <length of run - 1> all-ones is a reserved pattern.
4450      *
4451      * In all cases the rotation is by immr % e (and immr is 6 bits).
4452      */
4453 
4454     /* First determine the element size */
4455     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4456     if (len < 1) {
4457         /* This is the immn == 0, imms == 0x11111x case */
4458         return false;
4459     }
4460     e = 1 << len;
4461 
4462     levels = e - 1;
4463     s = imms & levels;
4464     r = immr & levels;
4465 
4466     if (s == levels) {
4467         /* <length of run - 1> mustn't be all-ones. */
4468         return false;
4469     }
4470 
4471     /* Create the value of one element: s+1 set bits rotated
4472      * by r within the element (which is e bits wide)...
4473      */
4474     mask = MAKE_64BIT_MASK(0, s + 1);
4475     if (r) {
4476         mask = (mask >> r) | (mask << (e - r));
4477         mask &= MAKE_64BIT_MASK(0, e);
4478     }
4479     /* ...then replicate the element over the whole 64 bit value */
4480     mask = bitfield_replicate(mask, e);
4481     *result = mask;
4482     return true;
4483 }
4484 
4485 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4486                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4487 {
4488     TCGv_i64 tcg_rd, tcg_rn;
4489     uint64_t imm;
4490 
4491     /* Some immediate field values are reserved. */
4492     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4493                                 extract32(a->dbm, 0, 6),
4494                                 extract32(a->dbm, 6, 6))) {
4495         return false;
4496     }
4497     if (!a->sf) {
4498         imm &= 0xffffffffull;
4499     }
4500 
4501     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4502     tcg_rn = cpu_reg(s, a->rn);
4503 
4504     fn(tcg_rd, tcg_rn, imm);
4505     if (set_cc) {
4506         gen_logic_CC(a->sf, tcg_rd);
4507     }
4508     if (!a->sf) {
4509         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4510     }
4511     return true;
4512 }
4513 
4514 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4515 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4516 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4517 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4518 
4519 /*
4520  * Move wide (immediate)
4521  */
4522 
4523 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4524 {
4525     int pos = a->hw << 4;
4526     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4527     return true;
4528 }
4529 
4530 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4531 {
4532     int pos = a->hw << 4;
4533     uint64_t imm = a->imm;
4534 
4535     imm = ~(imm << pos);
4536     if (!a->sf) {
4537         imm = (uint32_t)imm;
4538     }
4539     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4540     return true;
4541 }
4542 
4543 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4544 {
4545     int pos = a->hw << 4;
4546     TCGv_i64 tcg_rd, tcg_im;
4547 
4548     tcg_rd = cpu_reg(s, a->rd);
4549     tcg_im = tcg_constant_i64(a->imm);
4550     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4551     if (!a->sf) {
4552         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4553     }
4554     return true;
4555 }
4556 
4557 /*
4558  * Bitfield
4559  */
4560 
4561 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4562 {
4563     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4564     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4565     unsigned int bitsize = a->sf ? 64 : 32;
4566     unsigned int ri = a->immr;
4567     unsigned int si = a->imms;
4568     unsigned int pos, len;
4569 
4570     if (si >= ri) {
4571         /* Wd<s-r:0> = Wn<s:r> */
4572         len = (si - ri) + 1;
4573         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4574         if (!a->sf) {
4575             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4576         }
4577     } else {
4578         /* Wd<32+s-r,32-r> = Wn<s:0> */
4579         len = si + 1;
4580         pos = (bitsize - ri) & (bitsize - 1);
4581 
4582         if (len < ri) {
4583             /*
4584              * Sign extend the destination field from len to fill the
4585              * balance of the word.  Let the deposit below insert all
4586              * of those sign bits.
4587              */
4588             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4589             len = ri;
4590         }
4591 
4592         /*
4593          * We start with zero, and we haven't modified any bits outside
4594          * bitsize, therefore no final zero-extension is unneeded for !sf.
4595          */
4596         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4597     }
4598     return true;
4599 }
4600 
4601 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4602 {
4603     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4604     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4605     unsigned int bitsize = a->sf ? 64 : 32;
4606     unsigned int ri = a->immr;
4607     unsigned int si = a->imms;
4608     unsigned int pos, len;
4609 
4610     tcg_rd = cpu_reg(s, a->rd);
4611     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4612 
4613     if (si >= ri) {
4614         /* Wd<s-r:0> = Wn<s:r> */
4615         len = (si - ri) + 1;
4616         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4617     } else {
4618         /* Wd<32+s-r,32-r> = Wn<s:0> */
4619         len = si + 1;
4620         pos = (bitsize - ri) & (bitsize - 1);
4621         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4622     }
4623     return true;
4624 }
4625 
4626 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4627 {
4628     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4629     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4630     unsigned int bitsize = a->sf ? 64 : 32;
4631     unsigned int ri = a->immr;
4632     unsigned int si = a->imms;
4633     unsigned int pos, len;
4634 
4635     tcg_rd = cpu_reg(s, a->rd);
4636     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4637 
4638     if (si >= ri) {
4639         /* Wd<s-r:0> = Wn<s:r> */
4640         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4641         len = (si - ri) + 1;
4642         pos = 0;
4643     } else {
4644         /* Wd<32+s-r,32-r> = Wn<s:0> */
4645         len = si + 1;
4646         pos = (bitsize - ri) & (bitsize - 1);
4647     }
4648 
4649     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4650     if (!a->sf) {
4651         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4652     }
4653     return true;
4654 }
4655 
4656 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4657 {
4658     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4659 
4660     tcg_rd = cpu_reg(s, a->rd);
4661 
4662     if (unlikely(a->imm == 0)) {
4663         /*
4664          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4665          * so an extract from bit 0 is a special case.
4666          */
4667         if (a->sf) {
4668             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4669         } else {
4670             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4671         }
4672     } else {
4673         tcg_rm = cpu_reg(s, a->rm);
4674         tcg_rn = cpu_reg(s, a->rn);
4675 
4676         if (a->sf) {
4677             /* Specialization to ROR happens in EXTRACT2.  */
4678             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4679         } else {
4680             TCGv_i32 t0 = tcg_temp_new_i32();
4681 
4682             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4683             if (a->rm == a->rn) {
4684                 tcg_gen_rotri_i32(t0, t0, a->imm);
4685             } else {
4686                 TCGv_i32 t1 = tcg_temp_new_i32();
4687                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4688                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4689             }
4690             tcg_gen_extu_i32_i64(tcg_rd, t0);
4691         }
4692     }
4693     return true;
4694 }
4695 
4696 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4697  * Note that it is the caller's responsibility to ensure that the
4698  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4699  * mandated semantics for out of range shifts.
4700  */
4701 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4702                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4703 {
4704     switch (shift_type) {
4705     case A64_SHIFT_TYPE_LSL:
4706         tcg_gen_shl_i64(dst, src, shift_amount);
4707         break;
4708     case A64_SHIFT_TYPE_LSR:
4709         tcg_gen_shr_i64(dst, src, shift_amount);
4710         break;
4711     case A64_SHIFT_TYPE_ASR:
4712         if (!sf) {
4713             tcg_gen_ext32s_i64(dst, src);
4714         }
4715         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4716         break;
4717     case A64_SHIFT_TYPE_ROR:
4718         if (sf) {
4719             tcg_gen_rotr_i64(dst, src, shift_amount);
4720         } else {
4721             TCGv_i32 t0, t1;
4722             t0 = tcg_temp_new_i32();
4723             t1 = tcg_temp_new_i32();
4724             tcg_gen_extrl_i64_i32(t0, src);
4725             tcg_gen_extrl_i64_i32(t1, shift_amount);
4726             tcg_gen_rotr_i32(t0, t0, t1);
4727             tcg_gen_extu_i32_i64(dst, t0);
4728         }
4729         break;
4730     default:
4731         assert(FALSE); /* all shift types should be handled */
4732         break;
4733     }
4734 
4735     if (!sf) { /* zero extend final result */
4736         tcg_gen_ext32u_i64(dst, dst);
4737     }
4738 }
4739 
4740 /* Shift a TCGv src by immediate, put result in dst.
4741  * The shift amount must be in range (this should always be true as the
4742  * relevant instructions will UNDEF on bad shift immediates).
4743  */
4744 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4745                           enum a64_shift_type shift_type, unsigned int shift_i)
4746 {
4747     assert(shift_i < (sf ? 64 : 32));
4748 
4749     if (shift_i == 0) {
4750         tcg_gen_mov_i64(dst, src);
4751     } else {
4752         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4753     }
4754 }
4755 
4756 /* Logical (shifted register)
4757  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4758  * +----+-----+-----------+-------+---+------+--------+------+------+
4759  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4760  * +----+-----+-----------+-------+---+------+--------+------+------+
4761  */
4762 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4763 {
4764     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4765     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4766 
4767     sf = extract32(insn, 31, 1);
4768     opc = extract32(insn, 29, 2);
4769     shift_type = extract32(insn, 22, 2);
4770     invert = extract32(insn, 21, 1);
4771     rm = extract32(insn, 16, 5);
4772     shift_amount = extract32(insn, 10, 6);
4773     rn = extract32(insn, 5, 5);
4774     rd = extract32(insn, 0, 5);
4775 
4776     if (!sf && (shift_amount & (1 << 5))) {
4777         unallocated_encoding(s);
4778         return;
4779     }
4780 
4781     tcg_rd = cpu_reg(s, rd);
4782 
4783     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4784         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4785          * register-register MOV and MVN, so it is worth special casing.
4786          */
4787         tcg_rm = cpu_reg(s, rm);
4788         if (invert) {
4789             tcg_gen_not_i64(tcg_rd, tcg_rm);
4790             if (!sf) {
4791                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4792             }
4793         } else {
4794             if (sf) {
4795                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4796             } else {
4797                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4798             }
4799         }
4800         return;
4801     }
4802 
4803     tcg_rm = read_cpu_reg(s, rm, sf);
4804 
4805     if (shift_amount) {
4806         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4807     }
4808 
4809     tcg_rn = cpu_reg(s, rn);
4810 
4811     switch (opc | (invert << 2)) {
4812     case 0: /* AND */
4813     case 3: /* ANDS */
4814         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4815         break;
4816     case 1: /* ORR */
4817         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4818         break;
4819     case 2: /* EOR */
4820         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4821         break;
4822     case 4: /* BIC */
4823     case 7: /* BICS */
4824         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4825         break;
4826     case 5: /* ORN */
4827         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4828         break;
4829     case 6: /* EON */
4830         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4831         break;
4832     default:
4833         assert(FALSE);
4834         break;
4835     }
4836 
4837     if (!sf) {
4838         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4839     }
4840 
4841     if (opc == 3) {
4842         gen_logic_CC(sf, tcg_rd);
4843     }
4844 }
4845 
4846 /*
4847  * Add/subtract (extended register)
4848  *
4849  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4850  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4851  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4852  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4853  *
4854  *  sf: 0 -> 32bit, 1 -> 64bit
4855  *  op: 0 -> add  , 1 -> sub
4856  *   S: 1 -> set flags
4857  * opt: 00
4858  * option: extension type (see DecodeRegExtend)
4859  * imm3: optional shift to Rm
4860  *
4861  * Rd = Rn + LSL(extend(Rm), amount)
4862  */
4863 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4864 {
4865     int rd = extract32(insn, 0, 5);
4866     int rn = extract32(insn, 5, 5);
4867     int imm3 = extract32(insn, 10, 3);
4868     int option = extract32(insn, 13, 3);
4869     int rm = extract32(insn, 16, 5);
4870     int opt = extract32(insn, 22, 2);
4871     bool setflags = extract32(insn, 29, 1);
4872     bool sub_op = extract32(insn, 30, 1);
4873     bool sf = extract32(insn, 31, 1);
4874 
4875     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4876     TCGv_i64 tcg_rd;
4877     TCGv_i64 tcg_result;
4878 
4879     if (imm3 > 4 || opt != 0) {
4880         unallocated_encoding(s);
4881         return;
4882     }
4883 
4884     /* non-flag setting ops may use SP */
4885     if (!setflags) {
4886         tcg_rd = cpu_reg_sp(s, rd);
4887     } else {
4888         tcg_rd = cpu_reg(s, rd);
4889     }
4890     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4891 
4892     tcg_rm = read_cpu_reg(s, rm, sf);
4893     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4894 
4895     tcg_result = tcg_temp_new_i64();
4896 
4897     if (!setflags) {
4898         if (sub_op) {
4899             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4900         } else {
4901             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4902         }
4903     } else {
4904         if (sub_op) {
4905             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4906         } else {
4907             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4908         }
4909     }
4910 
4911     if (sf) {
4912         tcg_gen_mov_i64(tcg_rd, tcg_result);
4913     } else {
4914         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4915     }
4916 }
4917 
4918 /*
4919  * Add/subtract (shifted register)
4920  *
4921  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4922  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4923  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4924  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4925  *
4926  *    sf: 0 -> 32bit, 1 -> 64bit
4927  *    op: 0 -> add  , 1 -> sub
4928  *     S: 1 -> set flags
4929  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4930  *  imm6: Shift amount to apply to Rm before the add/sub
4931  */
4932 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4933 {
4934     int rd = extract32(insn, 0, 5);
4935     int rn = extract32(insn, 5, 5);
4936     int imm6 = extract32(insn, 10, 6);
4937     int rm = extract32(insn, 16, 5);
4938     int shift_type = extract32(insn, 22, 2);
4939     bool setflags = extract32(insn, 29, 1);
4940     bool sub_op = extract32(insn, 30, 1);
4941     bool sf = extract32(insn, 31, 1);
4942 
4943     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4944     TCGv_i64 tcg_rn, tcg_rm;
4945     TCGv_i64 tcg_result;
4946 
4947     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4948         unallocated_encoding(s);
4949         return;
4950     }
4951 
4952     tcg_rn = read_cpu_reg(s, rn, sf);
4953     tcg_rm = read_cpu_reg(s, rm, sf);
4954 
4955     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4956 
4957     tcg_result = tcg_temp_new_i64();
4958 
4959     if (!setflags) {
4960         if (sub_op) {
4961             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4962         } else {
4963             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4964         }
4965     } else {
4966         if (sub_op) {
4967             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4968         } else {
4969             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4970         }
4971     }
4972 
4973     if (sf) {
4974         tcg_gen_mov_i64(tcg_rd, tcg_result);
4975     } else {
4976         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4977     }
4978 }
4979 
4980 /* Data-processing (3 source)
4981  *
4982  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4983  *  +--+------+-----------+------+------+----+------+------+------+
4984  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4985  *  +--+------+-----------+------+------+----+------+------+------+
4986  */
4987 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4988 {
4989     int rd = extract32(insn, 0, 5);
4990     int rn = extract32(insn, 5, 5);
4991     int ra = extract32(insn, 10, 5);
4992     int rm = extract32(insn, 16, 5);
4993     int op_id = (extract32(insn, 29, 3) << 4) |
4994         (extract32(insn, 21, 3) << 1) |
4995         extract32(insn, 15, 1);
4996     bool sf = extract32(insn, 31, 1);
4997     bool is_sub = extract32(op_id, 0, 1);
4998     bool is_high = extract32(op_id, 2, 1);
4999     bool is_signed = false;
5000     TCGv_i64 tcg_op1;
5001     TCGv_i64 tcg_op2;
5002     TCGv_i64 tcg_tmp;
5003 
5004     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5005     switch (op_id) {
5006     case 0x42: /* SMADDL */
5007     case 0x43: /* SMSUBL */
5008     case 0x44: /* SMULH */
5009         is_signed = true;
5010         break;
5011     case 0x0: /* MADD (32bit) */
5012     case 0x1: /* MSUB (32bit) */
5013     case 0x40: /* MADD (64bit) */
5014     case 0x41: /* MSUB (64bit) */
5015     case 0x4a: /* UMADDL */
5016     case 0x4b: /* UMSUBL */
5017     case 0x4c: /* UMULH */
5018         break;
5019     default:
5020         unallocated_encoding(s);
5021         return;
5022     }
5023 
5024     if (is_high) {
5025         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5026         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5027         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5028         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5029 
5030         if (is_signed) {
5031             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5032         } else {
5033             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5034         }
5035         return;
5036     }
5037 
5038     tcg_op1 = tcg_temp_new_i64();
5039     tcg_op2 = tcg_temp_new_i64();
5040     tcg_tmp = tcg_temp_new_i64();
5041 
5042     if (op_id < 0x42) {
5043         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5044         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5045     } else {
5046         if (is_signed) {
5047             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5048             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5049         } else {
5050             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5051             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5052         }
5053     }
5054 
5055     if (ra == 31 && !is_sub) {
5056         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5057         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5058     } else {
5059         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5060         if (is_sub) {
5061             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5062         } else {
5063             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5064         }
5065     }
5066 
5067     if (!sf) {
5068         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5069     }
5070 }
5071 
5072 /* Add/subtract (with carry)
5073  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5074  * +--+--+--+------------------------+------+-------------+------+-----+
5075  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5076  * +--+--+--+------------------------+------+-------------+------+-----+
5077  */
5078 
5079 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5080 {
5081     unsigned int sf, op, setflags, rm, rn, rd;
5082     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5083 
5084     sf = extract32(insn, 31, 1);
5085     op = extract32(insn, 30, 1);
5086     setflags = extract32(insn, 29, 1);
5087     rm = extract32(insn, 16, 5);
5088     rn = extract32(insn, 5, 5);
5089     rd = extract32(insn, 0, 5);
5090 
5091     tcg_rd = cpu_reg(s, rd);
5092     tcg_rn = cpu_reg(s, rn);
5093 
5094     if (op) {
5095         tcg_y = tcg_temp_new_i64();
5096         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5097     } else {
5098         tcg_y = cpu_reg(s, rm);
5099     }
5100 
5101     if (setflags) {
5102         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5103     } else {
5104         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5105     }
5106 }
5107 
5108 /*
5109  * Rotate right into flags
5110  *  31 30 29                21       15          10      5  4      0
5111  * +--+--+--+-----------------+--------+-----------+------+--+------+
5112  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5113  * +--+--+--+-----------------+--------+-----------+------+--+------+
5114  */
5115 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5116 {
5117     int mask = extract32(insn, 0, 4);
5118     int o2 = extract32(insn, 4, 1);
5119     int rn = extract32(insn, 5, 5);
5120     int imm6 = extract32(insn, 15, 6);
5121     int sf_op_s = extract32(insn, 29, 3);
5122     TCGv_i64 tcg_rn;
5123     TCGv_i32 nzcv;
5124 
5125     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5126         unallocated_encoding(s);
5127         return;
5128     }
5129 
5130     tcg_rn = read_cpu_reg(s, rn, 1);
5131     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5132 
5133     nzcv = tcg_temp_new_i32();
5134     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5135 
5136     if (mask & 8) { /* N */
5137         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5138     }
5139     if (mask & 4) { /* Z */
5140         tcg_gen_not_i32(cpu_ZF, nzcv);
5141         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5142     }
5143     if (mask & 2) { /* C */
5144         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5145     }
5146     if (mask & 1) { /* V */
5147         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5148     }
5149 }
5150 
5151 /*
5152  * Evaluate into flags
5153  *  31 30 29                21        15   14        10      5  4      0
5154  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5155  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5156  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5157  */
5158 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5159 {
5160     int o3_mask = extract32(insn, 0, 5);
5161     int rn = extract32(insn, 5, 5);
5162     int o2 = extract32(insn, 15, 6);
5163     int sz = extract32(insn, 14, 1);
5164     int sf_op_s = extract32(insn, 29, 3);
5165     TCGv_i32 tmp;
5166     int shift;
5167 
5168     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5169         !dc_isar_feature(aa64_condm_4, s)) {
5170         unallocated_encoding(s);
5171         return;
5172     }
5173     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5174 
5175     tmp = tcg_temp_new_i32();
5176     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5177     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5178     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5179     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5180     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5181 }
5182 
5183 /* Conditional compare (immediate / register)
5184  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5185  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5186  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5187  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5188  *        [1]                             y                [0]       [0]
5189  */
5190 static void disas_cc(DisasContext *s, uint32_t insn)
5191 {
5192     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5193     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5194     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5195     DisasCompare c;
5196 
5197     if (!extract32(insn, 29, 1)) {
5198         unallocated_encoding(s);
5199         return;
5200     }
5201     if (insn & (1 << 10 | 1 << 4)) {
5202         unallocated_encoding(s);
5203         return;
5204     }
5205     sf = extract32(insn, 31, 1);
5206     op = extract32(insn, 30, 1);
5207     is_imm = extract32(insn, 11, 1);
5208     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5209     cond = extract32(insn, 12, 4);
5210     rn = extract32(insn, 5, 5);
5211     nzcv = extract32(insn, 0, 4);
5212 
5213     /* Set T0 = !COND.  */
5214     tcg_t0 = tcg_temp_new_i32();
5215     arm_test_cc(&c, cond);
5216     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5217 
5218     /* Load the arguments for the new comparison.  */
5219     if (is_imm) {
5220         tcg_y = tcg_temp_new_i64();
5221         tcg_gen_movi_i64(tcg_y, y);
5222     } else {
5223         tcg_y = cpu_reg(s, y);
5224     }
5225     tcg_rn = cpu_reg(s, rn);
5226 
5227     /* Set the flags for the new comparison.  */
5228     tcg_tmp = tcg_temp_new_i64();
5229     if (op) {
5230         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5231     } else {
5232         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5233     }
5234 
5235     /* If COND was false, force the flags to #nzcv.  Compute two masks
5236      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5237      * For tcg hosts that support ANDC, we can make do with just T1.
5238      * In either case, allow the tcg optimizer to delete any unused mask.
5239      */
5240     tcg_t1 = tcg_temp_new_i32();
5241     tcg_t2 = tcg_temp_new_i32();
5242     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5243     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5244 
5245     if (nzcv & 8) { /* N */
5246         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5247     } else {
5248         if (TCG_TARGET_HAS_andc_i32) {
5249             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5250         } else {
5251             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5252         }
5253     }
5254     if (nzcv & 4) { /* Z */
5255         if (TCG_TARGET_HAS_andc_i32) {
5256             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5257         } else {
5258             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5259         }
5260     } else {
5261         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5262     }
5263     if (nzcv & 2) { /* C */
5264         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5265     } else {
5266         if (TCG_TARGET_HAS_andc_i32) {
5267             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5268         } else {
5269             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5270         }
5271     }
5272     if (nzcv & 1) { /* V */
5273         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5274     } else {
5275         if (TCG_TARGET_HAS_andc_i32) {
5276             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5277         } else {
5278             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5279         }
5280     }
5281 }
5282 
5283 /* Conditional select
5284  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5285  * +----+----+---+-----------------+------+------+-----+------+------+
5286  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5287  * +----+----+---+-----------------+------+------+-----+------+------+
5288  */
5289 static void disas_cond_select(DisasContext *s, uint32_t insn)
5290 {
5291     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5292     TCGv_i64 tcg_rd, zero;
5293     DisasCompare64 c;
5294 
5295     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5296         /* S == 1 or op2<1> == 1 */
5297         unallocated_encoding(s);
5298         return;
5299     }
5300     sf = extract32(insn, 31, 1);
5301     else_inv = extract32(insn, 30, 1);
5302     rm = extract32(insn, 16, 5);
5303     cond = extract32(insn, 12, 4);
5304     else_inc = extract32(insn, 10, 1);
5305     rn = extract32(insn, 5, 5);
5306     rd = extract32(insn, 0, 5);
5307 
5308     tcg_rd = cpu_reg(s, rd);
5309 
5310     a64_test_cc(&c, cond);
5311     zero = tcg_constant_i64(0);
5312 
5313     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5314         /* CSET & CSETM.  */
5315         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5316         if (else_inv) {
5317             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5318         }
5319     } else {
5320         TCGv_i64 t_true = cpu_reg(s, rn);
5321         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5322         if (else_inv && else_inc) {
5323             tcg_gen_neg_i64(t_false, t_false);
5324         } else if (else_inv) {
5325             tcg_gen_not_i64(t_false, t_false);
5326         } else if (else_inc) {
5327             tcg_gen_addi_i64(t_false, t_false, 1);
5328         }
5329         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5330     }
5331 
5332     if (!sf) {
5333         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5334     }
5335 }
5336 
5337 static void handle_clz(DisasContext *s, unsigned int sf,
5338                        unsigned int rn, unsigned int rd)
5339 {
5340     TCGv_i64 tcg_rd, tcg_rn;
5341     tcg_rd = cpu_reg(s, rd);
5342     tcg_rn = cpu_reg(s, rn);
5343 
5344     if (sf) {
5345         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5346     } else {
5347         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5348         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5349         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5350         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5351     }
5352 }
5353 
5354 static void handle_cls(DisasContext *s, unsigned int sf,
5355                        unsigned int rn, unsigned int rd)
5356 {
5357     TCGv_i64 tcg_rd, tcg_rn;
5358     tcg_rd = cpu_reg(s, rd);
5359     tcg_rn = cpu_reg(s, rn);
5360 
5361     if (sf) {
5362         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5363     } else {
5364         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5365         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5366         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5367         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5368     }
5369 }
5370 
5371 static void handle_rbit(DisasContext *s, unsigned int sf,
5372                         unsigned int rn, unsigned int rd)
5373 {
5374     TCGv_i64 tcg_rd, tcg_rn;
5375     tcg_rd = cpu_reg(s, rd);
5376     tcg_rn = cpu_reg(s, rn);
5377 
5378     if (sf) {
5379         gen_helper_rbit64(tcg_rd, tcg_rn);
5380     } else {
5381         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5382         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5383         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5384         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5385     }
5386 }
5387 
5388 /* REV with sf==1, opcode==3 ("REV64") */
5389 static void handle_rev64(DisasContext *s, unsigned int sf,
5390                          unsigned int rn, unsigned int rd)
5391 {
5392     if (!sf) {
5393         unallocated_encoding(s);
5394         return;
5395     }
5396     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5397 }
5398 
5399 /* REV with sf==0, opcode==2
5400  * REV32 (sf==1, opcode==2)
5401  */
5402 static void handle_rev32(DisasContext *s, unsigned int sf,
5403                          unsigned int rn, unsigned int rd)
5404 {
5405     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5406     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5407 
5408     if (sf) {
5409         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5410         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5411     } else {
5412         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5413     }
5414 }
5415 
5416 /* REV16 (opcode==1) */
5417 static void handle_rev16(DisasContext *s, unsigned int sf,
5418                          unsigned int rn, unsigned int rd)
5419 {
5420     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5421     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5422     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5423     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5424 
5425     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5426     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5427     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5428     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5429     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5430 }
5431 
5432 /* Data-processing (1 source)
5433  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5434  * +----+---+---+-----------------+---------+--------+------+------+
5435  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5436  * +----+---+---+-----------------+---------+--------+------+------+
5437  */
5438 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5439 {
5440     unsigned int sf, opcode, opcode2, rn, rd;
5441     TCGv_i64 tcg_rd;
5442 
5443     if (extract32(insn, 29, 1)) {
5444         unallocated_encoding(s);
5445         return;
5446     }
5447 
5448     sf = extract32(insn, 31, 1);
5449     opcode = extract32(insn, 10, 6);
5450     opcode2 = extract32(insn, 16, 5);
5451     rn = extract32(insn, 5, 5);
5452     rd = extract32(insn, 0, 5);
5453 
5454 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5455 
5456     switch (MAP(sf, opcode2, opcode)) {
5457     case MAP(0, 0x00, 0x00): /* RBIT */
5458     case MAP(1, 0x00, 0x00):
5459         handle_rbit(s, sf, rn, rd);
5460         break;
5461     case MAP(0, 0x00, 0x01): /* REV16 */
5462     case MAP(1, 0x00, 0x01):
5463         handle_rev16(s, sf, rn, rd);
5464         break;
5465     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5466     case MAP(1, 0x00, 0x02):
5467         handle_rev32(s, sf, rn, rd);
5468         break;
5469     case MAP(1, 0x00, 0x03): /* REV64 */
5470         handle_rev64(s, sf, rn, rd);
5471         break;
5472     case MAP(0, 0x00, 0x04): /* CLZ */
5473     case MAP(1, 0x00, 0x04):
5474         handle_clz(s, sf, rn, rd);
5475         break;
5476     case MAP(0, 0x00, 0x05): /* CLS */
5477     case MAP(1, 0x00, 0x05):
5478         handle_cls(s, sf, rn, rd);
5479         break;
5480     case MAP(1, 0x01, 0x00): /* PACIA */
5481         if (s->pauth_active) {
5482             tcg_rd = cpu_reg(s, rd);
5483             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5484         } else if (!dc_isar_feature(aa64_pauth, s)) {
5485             goto do_unallocated;
5486         }
5487         break;
5488     case MAP(1, 0x01, 0x01): /* PACIB */
5489         if (s->pauth_active) {
5490             tcg_rd = cpu_reg(s, rd);
5491             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5492         } else if (!dc_isar_feature(aa64_pauth, s)) {
5493             goto do_unallocated;
5494         }
5495         break;
5496     case MAP(1, 0x01, 0x02): /* PACDA */
5497         if (s->pauth_active) {
5498             tcg_rd = cpu_reg(s, rd);
5499             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5500         } else if (!dc_isar_feature(aa64_pauth, s)) {
5501             goto do_unallocated;
5502         }
5503         break;
5504     case MAP(1, 0x01, 0x03): /* PACDB */
5505         if (s->pauth_active) {
5506             tcg_rd = cpu_reg(s, rd);
5507             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5508         } else if (!dc_isar_feature(aa64_pauth, s)) {
5509             goto do_unallocated;
5510         }
5511         break;
5512     case MAP(1, 0x01, 0x04): /* AUTIA */
5513         if (s->pauth_active) {
5514             tcg_rd = cpu_reg(s, rd);
5515             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5516         } else if (!dc_isar_feature(aa64_pauth, s)) {
5517             goto do_unallocated;
5518         }
5519         break;
5520     case MAP(1, 0x01, 0x05): /* AUTIB */
5521         if (s->pauth_active) {
5522             tcg_rd = cpu_reg(s, rd);
5523             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5524         } else if (!dc_isar_feature(aa64_pauth, s)) {
5525             goto do_unallocated;
5526         }
5527         break;
5528     case MAP(1, 0x01, 0x06): /* AUTDA */
5529         if (s->pauth_active) {
5530             tcg_rd = cpu_reg(s, rd);
5531             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5532         } else if (!dc_isar_feature(aa64_pauth, s)) {
5533             goto do_unallocated;
5534         }
5535         break;
5536     case MAP(1, 0x01, 0x07): /* AUTDB */
5537         if (s->pauth_active) {
5538             tcg_rd = cpu_reg(s, rd);
5539             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5540         } else if (!dc_isar_feature(aa64_pauth, s)) {
5541             goto do_unallocated;
5542         }
5543         break;
5544     case MAP(1, 0x01, 0x08): /* PACIZA */
5545         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5546             goto do_unallocated;
5547         } else if (s->pauth_active) {
5548             tcg_rd = cpu_reg(s, rd);
5549             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5550         }
5551         break;
5552     case MAP(1, 0x01, 0x09): /* PACIZB */
5553         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5554             goto do_unallocated;
5555         } else if (s->pauth_active) {
5556             tcg_rd = cpu_reg(s, rd);
5557             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5558         }
5559         break;
5560     case MAP(1, 0x01, 0x0a): /* PACDZA */
5561         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5562             goto do_unallocated;
5563         } else if (s->pauth_active) {
5564             tcg_rd = cpu_reg(s, rd);
5565             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5566         }
5567         break;
5568     case MAP(1, 0x01, 0x0b): /* PACDZB */
5569         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5570             goto do_unallocated;
5571         } else if (s->pauth_active) {
5572             tcg_rd = cpu_reg(s, rd);
5573             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5574         }
5575         break;
5576     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5577         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5578             goto do_unallocated;
5579         } else if (s->pauth_active) {
5580             tcg_rd = cpu_reg(s, rd);
5581             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5582         }
5583         break;
5584     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5585         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5586             goto do_unallocated;
5587         } else if (s->pauth_active) {
5588             tcg_rd = cpu_reg(s, rd);
5589             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5590         }
5591         break;
5592     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5593         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5594             goto do_unallocated;
5595         } else if (s->pauth_active) {
5596             tcg_rd = cpu_reg(s, rd);
5597             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5598         }
5599         break;
5600     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5601         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5602             goto do_unallocated;
5603         } else if (s->pauth_active) {
5604             tcg_rd = cpu_reg(s, rd);
5605             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5606         }
5607         break;
5608     case MAP(1, 0x01, 0x10): /* XPACI */
5609         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5610             goto do_unallocated;
5611         } else if (s->pauth_active) {
5612             tcg_rd = cpu_reg(s, rd);
5613             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5614         }
5615         break;
5616     case MAP(1, 0x01, 0x11): /* XPACD */
5617         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5618             goto do_unallocated;
5619         } else if (s->pauth_active) {
5620             tcg_rd = cpu_reg(s, rd);
5621             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5622         }
5623         break;
5624     default:
5625     do_unallocated:
5626         unallocated_encoding(s);
5627         break;
5628     }
5629 
5630 #undef MAP
5631 }
5632 
5633 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5634                        unsigned int rm, unsigned int rn, unsigned int rd)
5635 {
5636     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5637     tcg_rd = cpu_reg(s, rd);
5638 
5639     if (!sf && is_signed) {
5640         tcg_n = tcg_temp_new_i64();
5641         tcg_m = tcg_temp_new_i64();
5642         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5643         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5644     } else {
5645         tcg_n = read_cpu_reg(s, rn, sf);
5646         tcg_m = read_cpu_reg(s, rm, sf);
5647     }
5648 
5649     if (is_signed) {
5650         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5651     } else {
5652         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5653     }
5654 
5655     if (!sf) { /* zero extend final result */
5656         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5657     }
5658 }
5659 
5660 /* LSLV, LSRV, ASRV, RORV */
5661 static void handle_shift_reg(DisasContext *s,
5662                              enum a64_shift_type shift_type, unsigned int sf,
5663                              unsigned int rm, unsigned int rn, unsigned int rd)
5664 {
5665     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5666     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5667     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5668 
5669     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5670     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5671 }
5672 
5673 /* CRC32[BHWX], CRC32C[BHWX] */
5674 static void handle_crc32(DisasContext *s,
5675                          unsigned int sf, unsigned int sz, bool crc32c,
5676                          unsigned int rm, unsigned int rn, unsigned int rd)
5677 {
5678     TCGv_i64 tcg_acc, tcg_val;
5679     TCGv_i32 tcg_bytes;
5680 
5681     if (!dc_isar_feature(aa64_crc32, s)
5682         || (sf == 1 && sz != 3)
5683         || (sf == 0 && sz == 3)) {
5684         unallocated_encoding(s);
5685         return;
5686     }
5687 
5688     if (sz == 3) {
5689         tcg_val = cpu_reg(s, rm);
5690     } else {
5691         uint64_t mask;
5692         switch (sz) {
5693         case 0:
5694             mask = 0xFF;
5695             break;
5696         case 1:
5697             mask = 0xFFFF;
5698             break;
5699         case 2:
5700             mask = 0xFFFFFFFF;
5701             break;
5702         default:
5703             g_assert_not_reached();
5704         }
5705         tcg_val = tcg_temp_new_i64();
5706         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5707     }
5708 
5709     tcg_acc = cpu_reg(s, rn);
5710     tcg_bytes = tcg_constant_i32(1 << sz);
5711 
5712     if (crc32c) {
5713         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5714     } else {
5715         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5716     }
5717 }
5718 
5719 /* Data-processing (2 source)
5720  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5721  * +----+---+---+-----------------+------+--------+------+------+
5722  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5723  * +----+---+---+-----------------+------+--------+------+------+
5724  */
5725 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5726 {
5727     unsigned int sf, rm, opcode, rn, rd, setflag;
5728     sf = extract32(insn, 31, 1);
5729     setflag = extract32(insn, 29, 1);
5730     rm = extract32(insn, 16, 5);
5731     opcode = extract32(insn, 10, 6);
5732     rn = extract32(insn, 5, 5);
5733     rd = extract32(insn, 0, 5);
5734 
5735     if (setflag && opcode != 0) {
5736         unallocated_encoding(s);
5737         return;
5738     }
5739 
5740     switch (opcode) {
5741     case 0: /* SUBP(S) */
5742         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5743             goto do_unallocated;
5744         } else {
5745             TCGv_i64 tcg_n, tcg_m, tcg_d;
5746 
5747             tcg_n = read_cpu_reg_sp(s, rn, true);
5748             tcg_m = read_cpu_reg_sp(s, rm, true);
5749             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5750             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5751             tcg_d = cpu_reg(s, rd);
5752 
5753             if (setflag) {
5754                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5755             } else {
5756                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5757             }
5758         }
5759         break;
5760     case 2: /* UDIV */
5761         handle_div(s, false, sf, rm, rn, rd);
5762         break;
5763     case 3: /* SDIV */
5764         handle_div(s, true, sf, rm, rn, rd);
5765         break;
5766     case 4: /* IRG */
5767         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5768             goto do_unallocated;
5769         }
5770         if (s->ata) {
5771             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5772                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5773         } else {
5774             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5775                                              cpu_reg_sp(s, rn));
5776         }
5777         break;
5778     case 5: /* GMI */
5779         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5780             goto do_unallocated;
5781         } else {
5782             TCGv_i64 t = tcg_temp_new_i64();
5783 
5784             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5785             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5786             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5787         }
5788         break;
5789     case 8: /* LSLV */
5790         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5791         break;
5792     case 9: /* LSRV */
5793         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5794         break;
5795     case 10: /* ASRV */
5796         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5797         break;
5798     case 11: /* RORV */
5799         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5800         break;
5801     case 12: /* PACGA */
5802         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5803             goto do_unallocated;
5804         }
5805         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5806                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5807         break;
5808     case 16:
5809     case 17:
5810     case 18:
5811     case 19:
5812     case 20:
5813     case 21:
5814     case 22:
5815     case 23: /* CRC32 */
5816     {
5817         int sz = extract32(opcode, 0, 2);
5818         bool crc32c = extract32(opcode, 2, 1);
5819         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5820         break;
5821     }
5822     default:
5823     do_unallocated:
5824         unallocated_encoding(s);
5825         break;
5826     }
5827 }
5828 
5829 /*
5830  * Data processing - register
5831  *  31  30 29  28      25    21  20  16      10         0
5832  * +--+---+--+---+-------+-----+-------+-------+---------+
5833  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5834  * +--+---+--+---+-------+-----+-------+-------+---------+
5835  */
5836 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5837 {
5838     int op0 = extract32(insn, 30, 1);
5839     int op1 = extract32(insn, 28, 1);
5840     int op2 = extract32(insn, 21, 4);
5841     int op3 = extract32(insn, 10, 6);
5842 
5843     if (!op1) {
5844         if (op2 & 8) {
5845             if (op2 & 1) {
5846                 /* Add/sub (extended register) */
5847                 disas_add_sub_ext_reg(s, insn);
5848             } else {
5849                 /* Add/sub (shifted register) */
5850                 disas_add_sub_reg(s, insn);
5851             }
5852         } else {
5853             /* Logical (shifted register) */
5854             disas_logic_reg(s, insn);
5855         }
5856         return;
5857     }
5858 
5859     switch (op2) {
5860     case 0x0:
5861         switch (op3) {
5862         case 0x00: /* Add/subtract (with carry) */
5863             disas_adc_sbc(s, insn);
5864             break;
5865 
5866         case 0x01: /* Rotate right into flags */
5867         case 0x21:
5868             disas_rotate_right_into_flags(s, insn);
5869             break;
5870 
5871         case 0x02: /* Evaluate into flags */
5872         case 0x12:
5873         case 0x22:
5874         case 0x32:
5875             disas_evaluate_into_flags(s, insn);
5876             break;
5877 
5878         default:
5879             goto do_unallocated;
5880         }
5881         break;
5882 
5883     case 0x2: /* Conditional compare */
5884         disas_cc(s, insn); /* both imm and reg forms */
5885         break;
5886 
5887     case 0x4: /* Conditional select */
5888         disas_cond_select(s, insn);
5889         break;
5890 
5891     case 0x6: /* Data-processing */
5892         if (op0) {    /* (1 source) */
5893             disas_data_proc_1src(s, insn);
5894         } else {      /* (2 source) */
5895             disas_data_proc_2src(s, insn);
5896         }
5897         break;
5898     case 0x8 ... 0xf: /* (3 source) */
5899         disas_data_proc_3src(s, insn);
5900         break;
5901 
5902     default:
5903     do_unallocated:
5904         unallocated_encoding(s);
5905         break;
5906     }
5907 }
5908 
5909 static void handle_fp_compare(DisasContext *s, int size,
5910                               unsigned int rn, unsigned int rm,
5911                               bool cmp_with_zero, bool signal_all_nans)
5912 {
5913     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5914     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5915 
5916     if (size == MO_64) {
5917         TCGv_i64 tcg_vn, tcg_vm;
5918 
5919         tcg_vn = read_fp_dreg(s, rn);
5920         if (cmp_with_zero) {
5921             tcg_vm = tcg_constant_i64(0);
5922         } else {
5923             tcg_vm = read_fp_dreg(s, rm);
5924         }
5925         if (signal_all_nans) {
5926             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5927         } else {
5928             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5929         }
5930     } else {
5931         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5932         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5933 
5934         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5935         if (cmp_with_zero) {
5936             tcg_gen_movi_i32(tcg_vm, 0);
5937         } else {
5938             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5939         }
5940 
5941         switch (size) {
5942         case MO_32:
5943             if (signal_all_nans) {
5944                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5945             } else {
5946                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5947             }
5948             break;
5949         case MO_16:
5950             if (signal_all_nans) {
5951                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5952             } else {
5953                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5954             }
5955             break;
5956         default:
5957             g_assert_not_reached();
5958         }
5959     }
5960 
5961     gen_set_nzcv(tcg_flags);
5962 }
5963 
5964 /* Floating point compare
5965  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5966  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5967  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5968  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5969  */
5970 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5971 {
5972     unsigned int mos, type, rm, op, rn, opc, op2r;
5973     int size;
5974 
5975     mos = extract32(insn, 29, 3);
5976     type = extract32(insn, 22, 2);
5977     rm = extract32(insn, 16, 5);
5978     op = extract32(insn, 14, 2);
5979     rn = extract32(insn, 5, 5);
5980     opc = extract32(insn, 3, 2);
5981     op2r = extract32(insn, 0, 3);
5982 
5983     if (mos || op || op2r) {
5984         unallocated_encoding(s);
5985         return;
5986     }
5987 
5988     switch (type) {
5989     case 0:
5990         size = MO_32;
5991         break;
5992     case 1:
5993         size = MO_64;
5994         break;
5995     case 3:
5996         size = MO_16;
5997         if (dc_isar_feature(aa64_fp16, s)) {
5998             break;
5999         }
6000         /* fallthru */
6001     default:
6002         unallocated_encoding(s);
6003         return;
6004     }
6005 
6006     if (!fp_access_check(s)) {
6007         return;
6008     }
6009 
6010     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6011 }
6012 
6013 /* Floating point conditional compare
6014  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6015  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6016  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6017  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6018  */
6019 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6020 {
6021     unsigned int mos, type, rm, cond, rn, op, nzcv;
6022     TCGLabel *label_continue = NULL;
6023     int size;
6024 
6025     mos = extract32(insn, 29, 3);
6026     type = extract32(insn, 22, 2);
6027     rm = extract32(insn, 16, 5);
6028     cond = extract32(insn, 12, 4);
6029     rn = extract32(insn, 5, 5);
6030     op = extract32(insn, 4, 1);
6031     nzcv = extract32(insn, 0, 4);
6032 
6033     if (mos) {
6034         unallocated_encoding(s);
6035         return;
6036     }
6037 
6038     switch (type) {
6039     case 0:
6040         size = MO_32;
6041         break;
6042     case 1:
6043         size = MO_64;
6044         break;
6045     case 3:
6046         size = MO_16;
6047         if (dc_isar_feature(aa64_fp16, s)) {
6048             break;
6049         }
6050         /* fallthru */
6051     default:
6052         unallocated_encoding(s);
6053         return;
6054     }
6055 
6056     if (!fp_access_check(s)) {
6057         return;
6058     }
6059 
6060     if (cond < 0x0e) { /* not always */
6061         TCGLabel *label_match = gen_new_label();
6062         label_continue = gen_new_label();
6063         arm_gen_test_cc(cond, label_match);
6064         /* nomatch: */
6065         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6066         tcg_gen_br(label_continue);
6067         gen_set_label(label_match);
6068     }
6069 
6070     handle_fp_compare(s, size, rn, rm, false, op);
6071 
6072     if (cond < 0x0e) {
6073         gen_set_label(label_continue);
6074     }
6075 }
6076 
6077 /* Floating point conditional select
6078  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6079  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6080  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6081  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6082  */
6083 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6084 {
6085     unsigned int mos, type, rm, cond, rn, rd;
6086     TCGv_i64 t_true, t_false;
6087     DisasCompare64 c;
6088     MemOp sz;
6089 
6090     mos = extract32(insn, 29, 3);
6091     type = extract32(insn, 22, 2);
6092     rm = extract32(insn, 16, 5);
6093     cond = extract32(insn, 12, 4);
6094     rn = extract32(insn, 5, 5);
6095     rd = extract32(insn, 0, 5);
6096 
6097     if (mos) {
6098         unallocated_encoding(s);
6099         return;
6100     }
6101 
6102     switch (type) {
6103     case 0:
6104         sz = MO_32;
6105         break;
6106     case 1:
6107         sz = MO_64;
6108         break;
6109     case 3:
6110         sz = MO_16;
6111         if (dc_isar_feature(aa64_fp16, s)) {
6112             break;
6113         }
6114         /* fallthru */
6115     default:
6116         unallocated_encoding(s);
6117         return;
6118     }
6119 
6120     if (!fp_access_check(s)) {
6121         return;
6122     }
6123 
6124     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6125     t_true = tcg_temp_new_i64();
6126     t_false = tcg_temp_new_i64();
6127     read_vec_element(s, t_true, rn, 0, sz);
6128     read_vec_element(s, t_false, rm, 0, sz);
6129 
6130     a64_test_cc(&c, cond);
6131     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6132                         t_true, t_false);
6133 
6134     /* Note that sregs & hregs write back zeros to the high bits,
6135        and we've already done the zero-extension.  */
6136     write_fp_dreg(s, rd, t_true);
6137 }
6138 
6139 /* Floating-point data-processing (1 source) - half precision */
6140 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6141 {
6142     TCGv_ptr fpst = NULL;
6143     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6144     TCGv_i32 tcg_res = tcg_temp_new_i32();
6145 
6146     switch (opcode) {
6147     case 0x0: /* FMOV */
6148         tcg_gen_mov_i32(tcg_res, tcg_op);
6149         break;
6150     case 0x1: /* FABS */
6151         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6152         break;
6153     case 0x2: /* FNEG */
6154         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6155         break;
6156     case 0x3: /* FSQRT */
6157         fpst = fpstatus_ptr(FPST_FPCR_F16);
6158         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6159         break;
6160     case 0x8: /* FRINTN */
6161     case 0x9: /* FRINTP */
6162     case 0xa: /* FRINTM */
6163     case 0xb: /* FRINTZ */
6164     case 0xc: /* FRINTA */
6165     {
6166         TCGv_i32 tcg_rmode;
6167 
6168         fpst = fpstatus_ptr(FPST_FPCR_F16);
6169         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6170         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6171         gen_restore_rmode(tcg_rmode, fpst);
6172         break;
6173     }
6174     case 0xe: /* FRINTX */
6175         fpst = fpstatus_ptr(FPST_FPCR_F16);
6176         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6177         break;
6178     case 0xf: /* FRINTI */
6179         fpst = fpstatus_ptr(FPST_FPCR_F16);
6180         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6181         break;
6182     default:
6183         g_assert_not_reached();
6184     }
6185 
6186     write_fp_sreg(s, rd, tcg_res);
6187 }
6188 
6189 /* Floating-point data-processing (1 source) - single precision */
6190 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6191 {
6192     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6193     TCGv_i32 tcg_op, tcg_res;
6194     TCGv_ptr fpst;
6195     int rmode = -1;
6196 
6197     tcg_op = read_fp_sreg(s, rn);
6198     tcg_res = tcg_temp_new_i32();
6199 
6200     switch (opcode) {
6201     case 0x0: /* FMOV */
6202         tcg_gen_mov_i32(tcg_res, tcg_op);
6203         goto done;
6204     case 0x1: /* FABS */
6205         gen_helper_vfp_abss(tcg_res, tcg_op);
6206         goto done;
6207     case 0x2: /* FNEG */
6208         gen_helper_vfp_negs(tcg_res, tcg_op);
6209         goto done;
6210     case 0x3: /* FSQRT */
6211         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6212         goto done;
6213     case 0x6: /* BFCVT */
6214         gen_fpst = gen_helper_bfcvt;
6215         break;
6216     case 0x8: /* FRINTN */
6217     case 0x9: /* FRINTP */
6218     case 0xa: /* FRINTM */
6219     case 0xb: /* FRINTZ */
6220     case 0xc: /* FRINTA */
6221         rmode = opcode & 7;
6222         gen_fpst = gen_helper_rints;
6223         break;
6224     case 0xe: /* FRINTX */
6225         gen_fpst = gen_helper_rints_exact;
6226         break;
6227     case 0xf: /* FRINTI */
6228         gen_fpst = gen_helper_rints;
6229         break;
6230     case 0x10: /* FRINT32Z */
6231         rmode = FPROUNDING_ZERO;
6232         gen_fpst = gen_helper_frint32_s;
6233         break;
6234     case 0x11: /* FRINT32X */
6235         gen_fpst = gen_helper_frint32_s;
6236         break;
6237     case 0x12: /* FRINT64Z */
6238         rmode = FPROUNDING_ZERO;
6239         gen_fpst = gen_helper_frint64_s;
6240         break;
6241     case 0x13: /* FRINT64X */
6242         gen_fpst = gen_helper_frint64_s;
6243         break;
6244     default:
6245         g_assert_not_reached();
6246     }
6247 
6248     fpst = fpstatus_ptr(FPST_FPCR);
6249     if (rmode >= 0) {
6250         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6251         gen_fpst(tcg_res, tcg_op, fpst);
6252         gen_restore_rmode(tcg_rmode, fpst);
6253     } else {
6254         gen_fpst(tcg_res, tcg_op, fpst);
6255     }
6256 
6257  done:
6258     write_fp_sreg(s, rd, tcg_res);
6259 }
6260 
6261 /* Floating-point data-processing (1 source) - double precision */
6262 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6263 {
6264     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6265     TCGv_i64 tcg_op, tcg_res;
6266     TCGv_ptr fpst;
6267     int rmode = -1;
6268 
6269     switch (opcode) {
6270     case 0x0: /* FMOV */
6271         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6272         return;
6273     }
6274 
6275     tcg_op = read_fp_dreg(s, rn);
6276     tcg_res = tcg_temp_new_i64();
6277 
6278     switch (opcode) {
6279     case 0x1: /* FABS */
6280         gen_helper_vfp_absd(tcg_res, tcg_op);
6281         goto done;
6282     case 0x2: /* FNEG */
6283         gen_helper_vfp_negd(tcg_res, tcg_op);
6284         goto done;
6285     case 0x3: /* FSQRT */
6286         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6287         goto done;
6288     case 0x8: /* FRINTN */
6289     case 0x9: /* FRINTP */
6290     case 0xa: /* FRINTM */
6291     case 0xb: /* FRINTZ */
6292     case 0xc: /* FRINTA */
6293         rmode = opcode & 7;
6294         gen_fpst = gen_helper_rintd;
6295         break;
6296     case 0xe: /* FRINTX */
6297         gen_fpst = gen_helper_rintd_exact;
6298         break;
6299     case 0xf: /* FRINTI */
6300         gen_fpst = gen_helper_rintd;
6301         break;
6302     case 0x10: /* FRINT32Z */
6303         rmode = FPROUNDING_ZERO;
6304         gen_fpst = gen_helper_frint32_d;
6305         break;
6306     case 0x11: /* FRINT32X */
6307         gen_fpst = gen_helper_frint32_d;
6308         break;
6309     case 0x12: /* FRINT64Z */
6310         rmode = FPROUNDING_ZERO;
6311         gen_fpst = gen_helper_frint64_d;
6312         break;
6313     case 0x13: /* FRINT64X */
6314         gen_fpst = gen_helper_frint64_d;
6315         break;
6316     default:
6317         g_assert_not_reached();
6318     }
6319 
6320     fpst = fpstatus_ptr(FPST_FPCR);
6321     if (rmode >= 0) {
6322         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6323         gen_fpst(tcg_res, tcg_op, fpst);
6324         gen_restore_rmode(tcg_rmode, fpst);
6325     } else {
6326         gen_fpst(tcg_res, tcg_op, fpst);
6327     }
6328 
6329  done:
6330     write_fp_dreg(s, rd, tcg_res);
6331 }
6332 
6333 static void handle_fp_fcvt(DisasContext *s, int opcode,
6334                            int rd, int rn, int dtype, int ntype)
6335 {
6336     switch (ntype) {
6337     case 0x0:
6338     {
6339         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6340         if (dtype == 1) {
6341             /* Single to double */
6342             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6343             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6344             write_fp_dreg(s, rd, tcg_rd);
6345         } else {
6346             /* Single to half */
6347             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6348             TCGv_i32 ahp = get_ahp_flag();
6349             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6350 
6351             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6352             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6353             write_fp_sreg(s, rd, tcg_rd);
6354         }
6355         break;
6356     }
6357     case 0x1:
6358     {
6359         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6360         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6361         if (dtype == 0) {
6362             /* Double to single */
6363             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6364         } else {
6365             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6366             TCGv_i32 ahp = get_ahp_flag();
6367             /* Double to half */
6368             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6369             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6370         }
6371         write_fp_sreg(s, rd, tcg_rd);
6372         break;
6373     }
6374     case 0x3:
6375     {
6376         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6377         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6378         TCGv_i32 tcg_ahp = get_ahp_flag();
6379         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6380         if (dtype == 0) {
6381             /* Half to single */
6382             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6383             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6384             write_fp_sreg(s, rd, tcg_rd);
6385         } else {
6386             /* Half to double */
6387             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6388             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6389             write_fp_dreg(s, rd, tcg_rd);
6390         }
6391         break;
6392     }
6393     default:
6394         g_assert_not_reached();
6395     }
6396 }
6397 
6398 /* Floating point data-processing (1 source)
6399  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6400  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6401  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6402  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6403  */
6404 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6405 {
6406     int mos = extract32(insn, 29, 3);
6407     int type = extract32(insn, 22, 2);
6408     int opcode = extract32(insn, 15, 6);
6409     int rn = extract32(insn, 5, 5);
6410     int rd = extract32(insn, 0, 5);
6411 
6412     if (mos) {
6413         goto do_unallocated;
6414     }
6415 
6416     switch (opcode) {
6417     case 0x4: case 0x5: case 0x7:
6418     {
6419         /* FCVT between half, single and double precision */
6420         int dtype = extract32(opcode, 0, 2);
6421         if (type == 2 || dtype == type) {
6422             goto do_unallocated;
6423         }
6424         if (!fp_access_check(s)) {
6425             return;
6426         }
6427 
6428         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6429         break;
6430     }
6431 
6432     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6433         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6434             goto do_unallocated;
6435         }
6436         /* fall through */
6437     case 0x0 ... 0x3:
6438     case 0x8 ... 0xc:
6439     case 0xe ... 0xf:
6440         /* 32-to-32 and 64-to-64 ops */
6441         switch (type) {
6442         case 0:
6443             if (!fp_access_check(s)) {
6444                 return;
6445             }
6446             handle_fp_1src_single(s, opcode, rd, rn);
6447             break;
6448         case 1:
6449             if (!fp_access_check(s)) {
6450                 return;
6451             }
6452             handle_fp_1src_double(s, opcode, rd, rn);
6453             break;
6454         case 3:
6455             if (!dc_isar_feature(aa64_fp16, s)) {
6456                 goto do_unallocated;
6457             }
6458 
6459             if (!fp_access_check(s)) {
6460                 return;
6461             }
6462             handle_fp_1src_half(s, opcode, rd, rn);
6463             break;
6464         default:
6465             goto do_unallocated;
6466         }
6467         break;
6468 
6469     case 0x6:
6470         switch (type) {
6471         case 1: /* BFCVT */
6472             if (!dc_isar_feature(aa64_bf16, s)) {
6473                 goto do_unallocated;
6474             }
6475             if (!fp_access_check(s)) {
6476                 return;
6477             }
6478             handle_fp_1src_single(s, opcode, rd, rn);
6479             break;
6480         default:
6481             goto do_unallocated;
6482         }
6483         break;
6484 
6485     default:
6486     do_unallocated:
6487         unallocated_encoding(s);
6488         break;
6489     }
6490 }
6491 
6492 /* Floating-point data-processing (2 source) - single precision */
6493 static void handle_fp_2src_single(DisasContext *s, int opcode,
6494                                   int rd, int rn, int rm)
6495 {
6496     TCGv_i32 tcg_op1;
6497     TCGv_i32 tcg_op2;
6498     TCGv_i32 tcg_res;
6499     TCGv_ptr fpst;
6500 
6501     tcg_res = tcg_temp_new_i32();
6502     fpst = fpstatus_ptr(FPST_FPCR);
6503     tcg_op1 = read_fp_sreg(s, rn);
6504     tcg_op2 = read_fp_sreg(s, rm);
6505 
6506     switch (opcode) {
6507     case 0x0: /* FMUL */
6508         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6509         break;
6510     case 0x1: /* FDIV */
6511         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6512         break;
6513     case 0x2: /* FADD */
6514         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6515         break;
6516     case 0x3: /* FSUB */
6517         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6518         break;
6519     case 0x4: /* FMAX */
6520         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6521         break;
6522     case 0x5: /* FMIN */
6523         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6524         break;
6525     case 0x6: /* FMAXNM */
6526         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6527         break;
6528     case 0x7: /* FMINNM */
6529         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6530         break;
6531     case 0x8: /* FNMUL */
6532         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6533         gen_helper_vfp_negs(tcg_res, tcg_res);
6534         break;
6535     }
6536 
6537     write_fp_sreg(s, rd, tcg_res);
6538 }
6539 
6540 /* Floating-point data-processing (2 source) - double precision */
6541 static void handle_fp_2src_double(DisasContext *s, int opcode,
6542                                   int rd, int rn, int rm)
6543 {
6544     TCGv_i64 tcg_op1;
6545     TCGv_i64 tcg_op2;
6546     TCGv_i64 tcg_res;
6547     TCGv_ptr fpst;
6548 
6549     tcg_res = tcg_temp_new_i64();
6550     fpst = fpstatus_ptr(FPST_FPCR);
6551     tcg_op1 = read_fp_dreg(s, rn);
6552     tcg_op2 = read_fp_dreg(s, rm);
6553 
6554     switch (opcode) {
6555     case 0x0: /* FMUL */
6556         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6557         break;
6558     case 0x1: /* FDIV */
6559         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6560         break;
6561     case 0x2: /* FADD */
6562         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6563         break;
6564     case 0x3: /* FSUB */
6565         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6566         break;
6567     case 0x4: /* FMAX */
6568         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6569         break;
6570     case 0x5: /* FMIN */
6571         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6572         break;
6573     case 0x6: /* FMAXNM */
6574         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6575         break;
6576     case 0x7: /* FMINNM */
6577         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6578         break;
6579     case 0x8: /* FNMUL */
6580         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6581         gen_helper_vfp_negd(tcg_res, tcg_res);
6582         break;
6583     }
6584 
6585     write_fp_dreg(s, rd, tcg_res);
6586 }
6587 
6588 /* Floating-point data-processing (2 source) - half precision */
6589 static void handle_fp_2src_half(DisasContext *s, int opcode,
6590                                 int rd, int rn, int rm)
6591 {
6592     TCGv_i32 tcg_op1;
6593     TCGv_i32 tcg_op2;
6594     TCGv_i32 tcg_res;
6595     TCGv_ptr fpst;
6596 
6597     tcg_res = tcg_temp_new_i32();
6598     fpst = fpstatus_ptr(FPST_FPCR_F16);
6599     tcg_op1 = read_fp_hreg(s, rn);
6600     tcg_op2 = read_fp_hreg(s, rm);
6601 
6602     switch (opcode) {
6603     case 0x0: /* FMUL */
6604         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6605         break;
6606     case 0x1: /* FDIV */
6607         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6608         break;
6609     case 0x2: /* FADD */
6610         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6611         break;
6612     case 0x3: /* FSUB */
6613         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6614         break;
6615     case 0x4: /* FMAX */
6616         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6617         break;
6618     case 0x5: /* FMIN */
6619         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6620         break;
6621     case 0x6: /* FMAXNM */
6622         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6623         break;
6624     case 0x7: /* FMINNM */
6625         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6626         break;
6627     case 0x8: /* FNMUL */
6628         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6629         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6630         break;
6631     default:
6632         g_assert_not_reached();
6633     }
6634 
6635     write_fp_sreg(s, rd, tcg_res);
6636 }
6637 
6638 /* Floating point data-processing (2 source)
6639  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6640  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6641  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6642  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6643  */
6644 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6645 {
6646     int mos = extract32(insn, 29, 3);
6647     int type = extract32(insn, 22, 2);
6648     int rd = extract32(insn, 0, 5);
6649     int rn = extract32(insn, 5, 5);
6650     int rm = extract32(insn, 16, 5);
6651     int opcode = extract32(insn, 12, 4);
6652 
6653     if (opcode > 8 || mos) {
6654         unallocated_encoding(s);
6655         return;
6656     }
6657 
6658     switch (type) {
6659     case 0:
6660         if (!fp_access_check(s)) {
6661             return;
6662         }
6663         handle_fp_2src_single(s, opcode, rd, rn, rm);
6664         break;
6665     case 1:
6666         if (!fp_access_check(s)) {
6667             return;
6668         }
6669         handle_fp_2src_double(s, opcode, rd, rn, rm);
6670         break;
6671     case 3:
6672         if (!dc_isar_feature(aa64_fp16, s)) {
6673             unallocated_encoding(s);
6674             return;
6675         }
6676         if (!fp_access_check(s)) {
6677             return;
6678         }
6679         handle_fp_2src_half(s, opcode, rd, rn, rm);
6680         break;
6681     default:
6682         unallocated_encoding(s);
6683     }
6684 }
6685 
6686 /* Floating-point data-processing (3 source) - single precision */
6687 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6688                                   int rd, int rn, int rm, int ra)
6689 {
6690     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6691     TCGv_i32 tcg_res = tcg_temp_new_i32();
6692     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6693 
6694     tcg_op1 = read_fp_sreg(s, rn);
6695     tcg_op2 = read_fp_sreg(s, rm);
6696     tcg_op3 = read_fp_sreg(s, ra);
6697 
6698     /* These are fused multiply-add, and must be done as one
6699      * floating point operation with no rounding between the
6700      * multiplication and addition steps.
6701      * NB that doing the negations here as separate steps is
6702      * correct : an input NaN should come out with its sign bit
6703      * flipped if it is a negated-input.
6704      */
6705     if (o1 == true) {
6706         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6707     }
6708 
6709     if (o0 != o1) {
6710         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6711     }
6712 
6713     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6714 
6715     write_fp_sreg(s, rd, tcg_res);
6716 }
6717 
6718 /* Floating-point data-processing (3 source) - double precision */
6719 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6720                                   int rd, int rn, int rm, int ra)
6721 {
6722     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6723     TCGv_i64 tcg_res = tcg_temp_new_i64();
6724     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6725 
6726     tcg_op1 = read_fp_dreg(s, rn);
6727     tcg_op2 = read_fp_dreg(s, rm);
6728     tcg_op3 = read_fp_dreg(s, ra);
6729 
6730     /* These are fused multiply-add, and must be done as one
6731      * floating point operation with no rounding between the
6732      * multiplication and addition steps.
6733      * NB that doing the negations here as separate steps is
6734      * correct : an input NaN should come out with its sign bit
6735      * flipped if it is a negated-input.
6736      */
6737     if (o1 == true) {
6738         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6739     }
6740 
6741     if (o0 != o1) {
6742         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6743     }
6744 
6745     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6746 
6747     write_fp_dreg(s, rd, tcg_res);
6748 }
6749 
6750 /* Floating-point data-processing (3 source) - half precision */
6751 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6752                                 int rd, int rn, int rm, int ra)
6753 {
6754     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6755     TCGv_i32 tcg_res = tcg_temp_new_i32();
6756     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6757 
6758     tcg_op1 = read_fp_hreg(s, rn);
6759     tcg_op2 = read_fp_hreg(s, rm);
6760     tcg_op3 = read_fp_hreg(s, ra);
6761 
6762     /* These are fused multiply-add, and must be done as one
6763      * floating point operation with no rounding between the
6764      * multiplication and addition steps.
6765      * NB that doing the negations here as separate steps is
6766      * correct : an input NaN should come out with its sign bit
6767      * flipped if it is a negated-input.
6768      */
6769     if (o1 == true) {
6770         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6771     }
6772 
6773     if (o0 != o1) {
6774         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6775     }
6776 
6777     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6778 
6779     write_fp_sreg(s, rd, tcg_res);
6780 }
6781 
6782 /* Floating point data-processing (3 source)
6783  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6784  * +---+---+---+-----------+------+----+------+----+------+------+------+
6785  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6786  * +---+---+---+-----------+------+----+------+----+------+------+------+
6787  */
6788 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6789 {
6790     int mos = extract32(insn, 29, 3);
6791     int type = extract32(insn, 22, 2);
6792     int rd = extract32(insn, 0, 5);
6793     int rn = extract32(insn, 5, 5);
6794     int ra = extract32(insn, 10, 5);
6795     int rm = extract32(insn, 16, 5);
6796     bool o0 = extract32(insn, 15, 1);
6797     bool o1 = extract32(insn, 21, 1);
6798 
6799     if (mos) {
6800         unallocated_encoding(s);
6801         return;
6802     }
6803 
6804     switch (type) {
6805     case 0:
6806         if (!fp_access_check(s)) {
6807             return;
6808         }
6809         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6810         break;
6811     case 1:
6812         if (!fp_access_check(s)) {
6813             return;
6814         }
6815         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6816         break;
6817     case 3:
6818         if (!dc_isar_feature(aa64_fp16, s)) {
6819             unallocated_encoding(s);
6820             return;
6821         }
6822         if (!fp_access_check(s)) {
6823             return;
6824         }
6825         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6826         break;
6827     default:
6828         unallocated_encoding(s);
6829     }
6830 }
6831 
6832 /* Floating point immediate
6833  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6834  * +---+---+---+-----------+------+---+------------+-------+------+------+
6835  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6836  * +---+---+---+-----------+------+---+------------+-------+------+------+
6837  */
6838 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6839 {
6840     int rd = extract32(insn, 0, 5);
6841     int imm5 = extract32(insn, 5, 5);
6842     int imm8 = extract32(insn, 13, 8);
6843     int type = extract32(insn, 22, 2);
6844     int mos = extract32(insn, 29, 3);
6845     uint64_t imm;
6846     MemOp sz;
6847 
6848     if (mos || imm5) {
6849         unallocated_encoding(s);
6850         return;
6851     }
6852 
6853     switch (type) {
6854     case 0:
6855         sz = MO_32;
6856         break;
6857     case 1:
6858         sz = MO_64;
6859         break;
6860     case 3:
6861         sz = MO_16;
6862         if (dc_isar_feature(aa64_fp16, s)) {
6863             break;
6864         }
6865         /* fallthru */
6866     default:
6867         unallocated_encoding(s);
6868         return;
6869     }
6870 
6871     if (!fp_access_check(s)) {
6872         return;
6873     }
6874 
6875     imm = vfp_expand_imm(sz, imm8);
6876     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6877 }
6878 
6879 /* Handle floating point <=> fixed point conversions. Note that we can
6880  * also deal with fp <=> integer conversions as a special case (scale == 64)
6881  * OPTME: consider handling that special case specially or at least skipping
6882  * the call to scalbn in the helpers for zero shifts.
6883  */
6884 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6885                            bool itof, int rmode, int scale, int sf, int type)
6886 {
6887     bool is_signed = !(opcode & 1);
6888     TCGv_ptr tcg_fpstatus;
6889     TCGv_i32 tcg_shift, tcg_single;
6890     TCGv_i64 tcg_double;
6891 
6892     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6893 
6894     tcg_shift = tcg_constant_i32(64 - scale);
6895 
6896     if (itof) {
6897         TCGv_i64 tcg_int = cpu_reg(s, rn);
6898         if (!sf) {
6899             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6900 
6901             if (is_signed) {
6902                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6903             } else {
6904                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6905             }
6906 
6907             tcg_int = tcg_extend;
6908         }
6909 
6910         switch (type) {
6911         case 1: /* float64 */
6912             tcg_double = tcg_temp_new_i64();
6913             if (is_signed) {
6914                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6915                                      tcg_shift, tcg_fpstatus);
6916             } else {
6917                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6918                                      tcg_shift, tcg_fpstatus);
6919             }
6920             write_fp_dreg(s, rd, tcg_double);
6921             break;
6922 
6923         case 0: /* float32 */
6924             tcg_single = tcg_temp_new_i32();
6925             if (is_signed) {
6926                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6927                                      tcg_shift, tcg_fpstatus);
6928             } else {
6929                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6930                                      tcg_shift, tcg_fpstatus);
6931             }
6932             write_fp_sreg(s, rd, tcg_single);
6933             break;
6934 
6935         case 3: /* float16 */
6936             tcg_single = tcg_temp_new_i32();
6937             if (is_signed) {
6938                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6939                                      tcg_shift, tcg_fpstatus);
6940             } else {
6941                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6942                                      tcg_shift, tcg_fpstatus);
6943             }
6944             write_fp_sreg(s, rd, tcg_single);
6945             break;
6946 
6947         default:
6948             g_assert_not_reached();
6949         }
6950     } else {
6951         TCGv_i64 tcg_int = cpu_reg(s, rd);
6952         TCGv_i32 tcg_rmode;
6953 
6954         if (extract32(opcode, 2, 1)) {
6955             /* There are too many rounding modes to all fit into rmode,
6956              * so FCVTA[US] is a special case.
6957              */
6958             rmode = FPROUNDING_TIEAWAY;
6959         }
6960 
6961         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6962 
6963         switch (type) {
6964         case 1: /* float64 */
6965             tcg_double = read_fp_dreg(s, rn);
6966             if (is_signed) {
6967                 if (!sf) {
6968                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6969                                          tcg_shift, tcg_fpstatus);
6970                 } else {
6971                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6972                                          tcg_shift, tcg_fpstatus);
6973                 }
6974             } else {
6975                 if (!sf) {
6976                     gen_helper_vfp_tould(tcg_int, tcg_double,
6977                                          tcg_shift, tcg_fpstatus);
6978                 } else {
6979                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6980                                          tcg_shift, tcg_fpstatus);
6981                 }
6982             }
6983             if (!sf) {
6984                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6985             }
6986             break;
6987 
6988         case 0: /* float32 */
6989             tcg_single = read_fp_sreg(s, rn);
6990             if (sf) {
6991                 if (is_signed) {
6992                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6993                                          tcg_shift, tcg_fpstatus);
6994                 } else {
6995                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6996                                          tcg_shift, tcg_fpstatus);
6997                 }
6998             } else {
6999                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7000                 if (is_signed) {
7001                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7002                                          tcg_shift, tcg_fpstatus);
7003                 } else {
7004                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7005                                          tcg_shift, tcg_fpstatus);
7006                 }
7007                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7008             }
7009             break;
7010 
7011         case 3: /* float16 */
7012             tcg_single = read_fp_sreg(s, rn);
7013             if (sf) {
7014                 if (is_signed) {
7015                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7016                                          tcg_shift, tcg_fpstatus);
7017                 } else {
7018                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7019                                          tcg_shift, tcg_fpstatus);
7020                 }
7021             } else {
7022                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7023                 if (is_signed) {
7024                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7025                                          tcg_shift, tcg_fpstatus);
7026                 } else {
7027                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7028                                          tcg_shift, tcg_fpstatus);
7029                 }
7030                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7031             }
7032             break;
7033 
7034         default:
7035             g_assert_not_reached();
7036         }
7037 
7038         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7039     }
7040 }
7041 
7042 /* Floating point <-> fixed point conversions
7043  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7044  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7045  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7046  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7047  */
7048 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7049 {
7050     int rd = extract32(insn, 0, 5);
7051     int rn = extract32(insn, 5, 5);
7052     int scale = extract32(insn, 10, 6);
7053     int opcode = extract32(insn, 16, 3);
7054     int rmode = extract32(insn, 19, 2);
7055     int type = extract32(insn, 22, 2);
7056     bool sbit = extract32(insn, 29, 1);
7057     bool sf = extract32(insn, 31, 1);
7058     bool itof;
7059 
7060     if (sbit || (!sf && scale < 32)) {
7061         unallocated_encoding(s);
7062         return;
7063     }
7064 
7065     switch (type) {
7066     case 0: /* float32 */
7067     case 1: /* float64 */
7068         break;
7069     case 3: /* float16 */
7070         if (dc_isar_feature(aa64_fp16, s)) {
7071             break;
7072         }
7073         /* fallthru */
7074     default:
7075         unallocated_encoding(s);
7076         return;
7077     }
7078 
7079     switch ((rmode << 3) | opcode) {
7080     case 0x2: /* SCVTF */
7081     case 0x3: /* UCVTF */
7082         itof = true;
7083         break;
7084     case 0x18: /* FCVTZS */
7085     case 0x19: /* FCVTZU */
7086         itof = false;
7087         break;
7088     default:
7089         unallocated_encoding(s);
7090         return;
7091     }
7092 
7093     if (!fp_access_check(s)) {
7094         return;
7095     }
7096 
7097     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7098 }
7099 
7100 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7101 {
7102     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7103      * without conversion.
7104      */
7105 
7106     if (itof) {
7107         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7108         TCGv_i64 tmp;
7109 
7110         switch (type) {
7111         case 0:
7112             /* 32 bit */
7113             tmp = tcg_temp_new_i64();
7114             tcg_gen_ext32u_i64(tmp, tcg_rn);
7115             write_fp_dreg(s, rd, tmp);
7116             break;
7117         case 1:
7118             /* 64 bit */
7119             write_fp_dreg(s, rd, tcg_rn);
7120             break;
7121         case 2:
7122             /* 64 bit to top half. */
7123             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7124             clear_vec_high(s, true, rd);
7125             break;
7126         case 3:
7127             /* 16 bit */
7128             tmp = tcg_temp_new_i64();
7129             tcg_gen_ext16u_i64(tmp, tcg_rn);
7130             write_fp_dreg(s, rd, tmp);
7131             break;
7132         default:
7133             g_assert_not_reached();
7134         }
7135     } else {
7136         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7137 
7138         switch (type) {
7139         case 0:
7140             /* 32 bit */
7141             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7142             break;
7143         case 1:
7144             /* 64 bit */
7145             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7146             break;
7147         case 2:
7148             /* 64 bits from top half */
7149             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7150             break;
7151         case 3:
7152             /* 16 bit */
7153             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7154             break;
7155         default:
7156             g_assert_not_reached();
7157         }
7158     }
7159 }
7160 
7161 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7162 {
7163     TCGv_i64 t = read_fp_dreg(s, rn);
7164     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7165 
7166     gen_helper_fjcvtzs(t, t, fpstatus);
7167 
7168     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7169     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7170     tcg_gen_movi_i32(cpu_CF, 0);
7171     tcg_gen_movi_i32(cpu_NF, 0);
7172     tcg_gen_movi_i32(cpu_VF, 0);
7173 }
7174 
7175 /* Floating point <-> integer conversions
7176  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7177  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7178  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7179  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7180  */
7181 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7182 {
7183     int rd = extract32(insn, 0, 5);
7184     int rn = extract32(insn, 5, 5);
7185     int opcode = extract32(insn, 16, 3);
7186     int rmode = extract32(insn, 19, 2);
7187     int type = extract32(insn, 22, 2);
7188     bool sbit = extract32(insn, 29, 1);
7189     bool sf = extract32(insn, 31, 1);
7190     bool itof = false;
7191 
7192     if (sbit) {
7193         goto do_unallocated;
7194     }
7195 
7196     switch (opcode) {
7197     case 2: /* SCVTF */
7198     case 3: /* UCVTF */
7199         itof = true;
7200         /* fallthru */
7201     case 4: /* FCVTAS */
7202     case 5: /* FCVTAU */
7203         if (rmode != 0) {
7204             goto do_unallocated;
7205         }
7206         /* fallthru */
7207     case 0: /* FCVT[NPMZ]S */
7208     case 1: /* FCVT[NPMZ]U */
7209         switch (type) {
7210         case 0: /* float32 */
7211         case 1: /* float64 */
7212             break;
7213         case 3: /* float16 */
7214             if (!dc_isar_feature(aa64_fp16, s)) {
7215                 goto do_unallocated;
7216             }
7217             break;
7218         default:
7219             goto do_unallocated;
7220         }
7221         if (!fp_access_check(s)) {
7222             return;
7223         }
7224         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7225         break;
7226 
7227     default:
7228         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7229         case 0b01100110: /* FMOV half <-> 32-bit int */
7230         case 0b01100111:
7231         case 0b11100110: /* FMOV half <-> 64-bit int */
7232         case 0b11100111:
7233             if (!dc_isar_feature(aa64_fp16, s)) {
7234                 goto do_unallocated;
7235             }
7236             /* fallthru */
7237         case 0b00000110: /* FMOV 32-bit */
7238         case 0b00000111:
7239         case 0b10100110: /* FMOV 64-bit */
7240         case 0b10100111:
7241         case 0b11001110: /* FMOV top half of 128-bit */
7242         case 0b11001111:
7243             if (!fp_access_check(s)) {
7244                 return;
7245             }
7246             itof = opcode & 1;
7247             handle_fmov(s, rd, rn, type, itof);
7248             break;
7249 
7250         case 0b00111110: /* FJCVTZS */
7251             if (!dc_isar_feature(aa64_jscvt, s)) {
7252                 goto do_unallocated;
7253             } else if (fp_access_check(s)) {
7254                 handle_fjcvtzs(s, rd, rn);
7255             }
7256             break;
7257 
7258         default:
7259         do_unallocated:
7260             unallocated_encoding(s);
7261             return;
7262         }
7263         break;
7264     }
7265 }
7266 
7267 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7268  *   31  30  29 28     25 24                          0
7269  * +---+---+---+---------+-----------------------------+
7270  * |   | 0 |   | 1 1 1 1 |                             |
7271  * +---+---+---+---------+-----------------------------+
7272  */
7273 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7274 {
7275     if (extract32(insn, 24, 1)) {
7276         /* Floating point data-processing (3 source) */
7277         disas_fp_3src(s, insn);
7278     } else if (extract32(insn, 21, 1) == 0) {
7279         /* Floating point to fixed point conversions */
7280         disas_fp_fixed_conv(s, insn);
7281     } else {
7282         switch (extract32(insn, 10, 2)) {
7283         case 1:
7284             /* Floating point conditional compare */
7285             disas_fp_ccomp(s, insn);
7286             break;
7287         case 2:
7288             /* Floating point data-processing (2 source) */
7289             disas_fp_2src(s, insn);
7290             break;
7291         case 3:
7292             /* Floating point conditional select */
7293             disas_fp_csel(s, insn);
7294             break;
7295         case 0:
7296             switch (ctz32(extract32(insn, 12, 4))) {
7297             case 0: /* [15:12] == xxx1 */
7298                 /* Floating point immediate */
7299                 disas_fp_imm(s, insn);
7300                 break;
7301             case 1: /* [15:12] == xx10 */
7302                 /* Floating point compare */
7303                 disas_fp_compare(s, insn);
7304                 break;
7305             case 2: /* [15:12] == x100 */
7306                 /* Floating point data-processing (1 source) */
7307                 disas_fp_1src(s, insn);
7308                 break;
7309             case 3: /* [15:12] == 1000 */
7310                 unallocated_encoding(s);
7311                 break;
7312             default: /* [15:12] == 0000 */
7313                 /* Floating point <-> integer conversions */
7314                 disas_fp_int_conv(s, insn);
7315                 break;
7316             }
7317             break;
7318         }
7319     }
7320 }
7321 
7322 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7323                      int pos)
7324 {
7325     /* Extract 64 bits from the middle of two concatenated 64 bit
7326      * vector register slices left:right. The extracted bits start
7327      * at 'pos' bits into the right (least significant) side.
7328      * We return the result in tcg_right, and guarantee not to
7329      * trash tcg_left.
7330      */
7331     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7332     assert(pos > 0 && pos < 64);
7333 
7334     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7335     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7336     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7337 }
7338 
7339 /* EXT
7340  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7341  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7342  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7343  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7344  */
7345 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7346 {
7347     int is_q = extract32(insn, 30, 1);
7348     int op2 = extract32(insn, 22, 2);
7349     int imm4 = extract32(insn, 11, 4);
7350     int rm = extract32(insn, 16, 5);
7351     int rn = extract32(insn, 5, 5);
7352     int rd = extract32(insn, 0, 5);
7353     int pos = imm4 << 3;
7354     TCGv_i64 tcg_resl, tcg_resh;
7355 
7356     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7357         unallocated_encoding(s);
7358         return;
7359     }
7360 
7361     if (!fp_access_check(s)) {
7362         return;
7363     }
7364 
7365     tcg_resh = tcg_temp_new_i64();
7366     tcg_resl = tcg_temp_new_i64();
7367 
7368     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7369      * either extracting 128 bits from a 128:128 concatenation, or
7370      * extracting 64 bits from a 64:64 concatenation.
7371      */
7372     if (!is_q) {
7373         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7374         if (pos != 0) {
7375             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7376             do_ext64(s, tcg_resh, tcg_resl, pos);
7377         }
7378     } else {
7379         TCGv_i64 tcg_hh;
7380         typedef struct {
7381             int reg;
7382             int elt;
7383         } EltPosns;
7384         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7385         EltPosns *elt = eltposns;
7386 
7387         if (pos >= 64) {
7388             elt++;
7389             pos -= 64;
7390         }
7391 
7392         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7393         elt++;
7394         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7395         elt++;
7396         if (pos != 0) {
7397             do_ext64(s, tcg_resh, tcg_resl, pos);
7398             tcg_hh = tcg_temp_new_i64();
7399             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7400             do_ext64(s, tcg_hh, tcg_resh, pos);
7401         }
7402     }
7403 
7404     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7405     if (is_q) {
7406         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7407     }
7408     clear_vec_high(s, is_q, rd);
7409 }
7410 
7411 /* TBL/TBX
7412  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7413  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7414  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7415  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7416  */
7417 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7418 {
7419     int op2 = extract32(insn, 22, 2);
7420     int is_q = extract32(insn, 30, 1);
7421     int rm = extract32(insn, 16, 5);
7422     int rn = extract32(insn, 5, 5);
7423     int rd = extract32(insn, 0, 5);
7424     int is_tbx = extract32(insn, 12, 1);
7425     int len = (extract32(insn, 13, 2) + 1) * 16;
7426 
7427     if (op2 != 0) {
7428         unallocated_encoding(s);
7429         return;
7430     }
7431 
7432     if (!fp_access_check(s)) {
7433         return;
7434     }
7435 
7436     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7437                        vec_full_reg_offset(s, rm), cpu_env,
7438                        is_q ? 16 : 8, vec_full_reg_size(s),
7439                        (len << 6) | (is_tbx << 5) | rn,
7440                        gen_helper_simd_tblx);
7441 }
7442 
7443 /* ZIP/UZP/TRN
7444  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7445  * +---+---+-------------+------+---+------+---+------------------+------+
7446  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7447  * +---+---+-------------+------+---+------+---+------------------+------+
7448  */
7449 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7450 {
7451     int rd = extract32(insn, 0, 5);
7452     int rn = extract32(insn, 5, 5);
7453     int rm = extract32(insn, 16, 5);
7454     int size = extract32(insn, 22, 2);
7455     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7456      * bit 2 indicates 1 vs 2 variant of the insn.
7457      */
7458     int opcode = extract32(insn, 12, 2);
7459     bool part = extract32(insn, 14, 1);
7460     bool is_q = extract32(insn, 30, 1);
7461     int esize = 8 << size;
7462     int i;
7463     int datasize = is_q ? 128 : 64;
7464     int elements = datasize / esize;
7465     TCGv_i64 tcg_res[2], tcg_ele;
7466 
7467     if (opcode == 0 || (size == 3 && !is_q)) {
7468         unallocated_encoding(s);
7469         return;
7470     }
7471 
7472     if (!fp_access_check(s)) {
7473         return;
7474     }
7475 
7476     tcg_res[0] = tcg_temp_new_i64();
7477     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7478     tcg_ele = tcg_temp_new_i64();
7479 
7480     for (i = 0; i < elements; i++) {
7481         int o, w;
7482 
7483         switch (opcode) {
7484         case 1: /* UZP1/2 */
7485         {
7486             int midpoint = elements / 2;
7487             if (i < midpoint) {
7488                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7489             } else {
7490                 read_vec_element(s, tcg_ele, rm,
7491                                  2 * (i - midpoint) + part, size);
7492             }
7493             break;
7494         }
7495         case 2: /* TRN1/2 */
7496             if (i & 1) {
7497                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7498             } else {
7499                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7500             }
7501             break;
7502         case 3: /* ZIP1/2 */
7503         {
7504             int base = part * elements / 2;
7505             if (i & 1) {
7506                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7507             } else {
7508                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7509             }
7510             break;
7511         }
7512         default:
7513             g_assert_not_reached();
7514         }
7515 
7516         w = (i * esize) / 64;
7517         o = (i * esize) % 64;
7518         if (o == 0) {
7519             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7520         } else {
7521             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7522             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7523         }
7524     }
7525 
7526     for (i = 0; i <= is_q; ++i) {
7527         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7528     }
7529     clear_vec_high(s, is_q, rd);
7530 }
7531 
7532 /*
7533  * do_reduction_op helper
7534  *
7535  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7536  * important for correct NaN propagation that we do these
7537  * operations in exactly the order specified by the pseudocode.
7538  *
7539  * This is a recursive function, TCG temps should be freed by the
7540  * calling function once it is done with the values.
7541  */
7542 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7543                                 int esize, int size, int vmap, TCGv_ptr fpst)
7544 {
7545     if (esize == size) {
7546         int element;
7547         MemOp msize = esize == 16 ? MO_16 : MO_32;
7548         TCGv_i32 tcg_elem;
7549 
7550         /* We should have one register left here */
7551         assert(ctpop8(vmap) == 1);
7552         element = ctz32(vmap);
7553         assert(element < 8);
7554 
7555         tcg_elem = tcg_temp_new_i32();
7556         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7557         return tcg_elem;
7558     } else {
7559         int bits = size / 2;
7560         int shift = ctpop8(vmap) / 2;
7561         int vmap_lo = (vmap >> shift) & vmap;
7562         int vmap_hi = (vmap & ~vmap_lo);
7563         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7564 
7565         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7566         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7567         tcg_res = tcg_temp_new_i32();
7568 
7569         switch (fpopcode) {
7570         case 0x0c: /* fmaxnmv half-precision */
7571             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7572             break;
7573         case 0x0f: /* fmaxv half-precision */
7574             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7575             break;
7576         case 0x1c: /* fminnmv half-precision */
7577             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7578             break;
7579         case 0x1f: /* fminv half-precision */
7580             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7581             break;
7582         case 0x2c: /* fmaxnmv */
7583             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7584             break;
7585         case 0x2f: /* fmaxv */
7586             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7587             break;
7588         case 0x3c: /* fminnmv */
7589             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7590             break;
7591         case 0x3f: /* fminv */
7592             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7593             break;
7594         default:
7595             g_assert_not_reached();
7596         }
7597         return tcg_res;
7598     }
7599 }
7600 
7601 /* AdvSIMD across lanes
7602  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7603  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7604  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7605  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7606  */
7607 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7608 {
7609     int rd = extract32(insn, 0, 5);
7610     int rn = extract32(insn, 5, 5);
7611     int size = extract32(insn, 22, 2);
7612     int opcode = extract32(insn, 12, 5);
7613     bool is_q = extract32(insn, 30, 1);
7614     bool is_u = extract32(insn, 29, 1);
7615     bool is_fp = false;
7616     bool is_min = false;
7617     int esize;
7618     int elements;
7619     int i;
7620     TCGv_i64 tcg_res, tcg_elt;
7621 
7622     switch (opcode) {
7623     case 0x1b: /* ADDV */
7624         if (is_u) {
7625             unallocated_encoding(s);
7626             return;
7627         }
7628         /* fall through */
7629     case 0x3: /* SADDLV, UADDLV */
7630     case 0xa: /* SMAXV, UMAXV */
7631     case 0x1a: /* SMINV, UMINV */
7632         if (size == 3 || (size == 2 && !is_q)) {
7633             unallocated_encoding(s);
7634             return;
7635         }
7636         break;
7637     case 0xc: /* FMAXNMV, FMINNMV */
7638     case 0xf: /* FMAXV, FMINV */
7639         /* Bit 1 of size field encodes min vs max and the actual size
7640          * depends on the encoding of the U bit. If not set (and FP16
7641          * enabled) then we do half-precision float instead of single
7642          * precision.
7643          */
7644         is_min = extract32(size, 1, 1);
7645         is_fp = true;
7646         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7647             size = 1;
7648         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7649             unallocated_encoding(s);
7650             return;
7651         } else {
7652             size = 2;
7653         }
7654         break;
7655     default:
7656         unallocated_encoding(s);
7657         return;
7658     }
7659 
7660     if (!fp_access_check(s)) {
7661         return;
7662     }
7663 
7664     esize = 8 << size;
7665     elements = (is_q ? 128 : 64) / esize;
7666 
7667     tcg_res = tcg_temp_new_i64();
7668     tcg_elt = tcg_temp_new_i64();
7669 
7670     /* These instructions operate across all lanes of a vector
7671      * to produce a single result. We can guarantee that a 64
7672      * bit intermediate is sufficient:
7673      *  + for [US]ADDLV the maximum element size is 32 bits, and
7674      *    the result type is 64 bits
7675      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7676      *    same as the element size, which is 32 bits at most
7677      * For the integer operations we can choose to work at 64
7678      * or 32 bits and truncate at the end; for simplicity
7679      * we use 64 bits always. The floating point
7680      * ops do require 32 bit intermediates, though.
7681      */
7682     if (!is_fp) {
7683         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7684 
7685         for (i = 1; i < elements; i++) {
7686             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7687 
7688             switch (opcode) {
7689             case 0x03: /* SADDLV / UADDLV */
7690             case 0x1b: /* ADDV */
7691                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7692                 break;
7693             case 0x0a: /* SMAXV / UMAXV */
7694                 if (is_u) {
7695                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7696                 } else {
7697                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7698                 }
7699                 break;
7700             case 0x1a: /* SMINV / UMINV */
7701                 if (is_u) {
7702                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7703                 } else {
7704                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7705                 }
7706                 break;
7707             default:
7708                 g_assert_not_reached();
7709             }
7710 
7711         }
7712     } else {
7713         /* Floating point vector reduction ops which work across 32
7714          * bit (single) or 16 bit (half-precision) intermediates.
7715          * Note that correct NaN propagation requires that we do these
7716          * operations in exactly the order specified by the pseudocode.
7717          */
7718         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7719         int fpopcode = opcode | is_min << 4 | is_u << 5;
7720         int vmap = (1 << elements) - 1;
7721         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7722                                              (is_q ? 128 : 64), vmap, fpst);
7723         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7724     }
7725 
7726     /* Now truncate the result to the width required for the final output */
7727     if (opcode == 0x03) {
7728         /* SADDLV, UADDLV: result is 2*esize */
7729         size++;
7730     }
7731 
7732     switch (size) {
7733     case 0:
7734         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7735         break;
7736     case 1:
7737         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7738         break;
7739     case 2:
7740         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7741         break;
7742     case 3:
7743         break;
7744     default:
7745         g_assert_not_reached();
7746     }
7747 
7748     write_fp_dreg(s, rd, tcg_res);
7749 }
7750 
7751 /* DUP (Element, Vector)
7752  *
7753  *  31  30   29              21 20    16 15        10  9    5 4    0
7754  * +---+---+-------------------+--------+-------------+------+------+
7755  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7756  * +---+---+-------------------+--------+-------------+------+------+
7757  *
7758  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7759  */
7760 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7761                              int imm5)
7762 {
7763     int size = ctz32(imm5);
7764     int index;
7765 
7766     if (size > 3 || (size == 3 && !is_q)) {
7767         unallocated_encoding(s);
7768         return;
7769     }
7770 
7771     if (!fp_access_check(s)) {
7772         return;
7773     }
7774 
7775     index = imm5 >> (size + 1);
7776     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7777                          vec_reg_offset(s, rn, index, size),
7778                          is_q ? 16 : 8, vec_full_reg_size(s));
7779 }
7780 
7781 /* DUP (element, scalar)
7782  *  31                   21 20    16 15        10  9    5 4    0
7783  * +-----------------------+--------+-------------+------+------+
7784  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7785  * +-----------------------+--------+-------------+------+------+
7786  */
7787 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7788                               int imm5)
7789 {
7790     int size = ctz32(imm5);
7791     int index;
7792     TCGv_i64 tmp;
7793 
7794     if (size > 3) {
7795         unallocated_encoding(s);
7796         return;
7797     }
7798 
7799     if (!fp_access_check(s)) {
7800         return;
7801     }
7802 
7803     index = imm5 >> (size + 1);
7804 
7805     /* This instruction just extracts the specified element and
7806      * zero-extends it into the bottom of the destination register.
7807      */
7808     tmp = tcg_temp_new_i64();
7809     read_vec_element(s, tmp, rn, index, size);
7810     write_fp_dreg(s, rd, tmp);
7811 }
7812 
7813 /* DUP (General)
7814  *
7815  *  31  30   29              21 20    16 15        10  9    5 4    0
7816  * +---+---+-------------------+--------+-------------+------+------+
7817  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7818  * +---+---+-------------------+--------+-------------+------+------+
7819  *
7820  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7821  */
7822 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7823                              int imm5)
7824 {
7825     int size = ctz32(imm5);
7826     uint32_t dofs, oprsz, maxsz;
7827 
7828     if (size > 3 || ((size == 3) && !is_q)) {
7829         unallocated_encoding(s);
7830         return;
7831     }
7832 
7833     if (!fp_access_check(s)) {
7834         return;
7835     }
7836 
7837     dofs = vec_full_reg_offset(s, rd);
7838     oprsz = is_q ? 16 : 8;
7839     maxsz = vec_full_reg_size(s);
7840 
7841     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7842 }
7843 
7844 /* INS (Element)
7845  *
7846  *  31                   21 20    16 15  14    11  10 9    5 4    0
7847  * +-----------------------+--------+------------+---+------+------+
7848  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7849  * +-----------------------+--------+------------+---+------+------+
7850  *
7851  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7852  * index: encoded in imm5<4:size+1>
7853  */
7854 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7855                              int imm4, int imm5)
7856 {
7857     int size = ctz32(imm5);
7858     int src_index, dst_index;
7859     TCGv_i64 tmp;
7860 
7861     if (size > 3) {
7862         unallocated_encoding(s);
7863         return;
7864     }
7865 
7866     if (!fp_access_check(s)) {
7867         return;
7868     }
7869 
7870     dst_index = extract32(imm5, 1+size, 5);
7871     src_index = extract32(imm4, size, 4);
7872 
7873     tmp = tcg_temp_new_i64();
7874 
7875     read_vec_element(s, tmp, rn, src_index, size);
7876     write_vec_element(s, tmp, rd, dst_index, size);
7877 
7878     /* INS is considered a 128-bit write for SVE. */
7879     clear_vec_high(s, true, rd);
7880 }
7881 
7882 
7883 /* INS (General)
7884  *
7885  *  31                   21 20    16 15        10  9    5 4    0
7886  * +-----------------------+--------+-------------+------+------+
7887  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7888  * +-----------------------+--------+-------------+------+------+
7889  *
7890  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7891  * index: encoded in imm5<4:size+1>
7892  */
7893 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7894 {
7895     int size = ctz32(imm5);
7896     int idx;
7897 
7898     if (size > 3) {
7899         unallocated_encoding(s);
7900         return;
7901     }
7902 
7903     if (!fp_access_check(s)) {
7904         return;
7905     }
7906 
7907     idx = extract32(imm5, 1 + size, 4 - size);
7908     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7909 
7910     /* INS is considered a 128-bit write for SVE. */
7911     clear_vec_high(s, true, rd);
7912 }
7913 
7914 /*
7915  * UMOV (General)
7916  * SMOV (General)
7917  *
7918  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7919  * +---+---+-------------------+--------+-------------+------+------+
7920  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7921  * +---+---+-------------------+--------+-------------+------+------+
7922  *
7923  * U: unsigned when set
7924  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7925  */
7926 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7927                                   int rn, int rd, int imm5)
7928 {
7929     int size = ctz32(imm5);
7930     int element;
7931     TCGv_i64 tcg_rd;
7932 
7933     /* Check for UnallocatedEncodings */
7934     if (is_signed) {
7935         if (size > 2 || (size == 2 && !is_q)) {
7936             unallocated_encoding(s);
7937             return;
7938         }
7939     } else {
7940         if (size > 3
7941             || (size < 3 && is_q)
7942             || (size == 3 && !is_q)) {
7943             unallocated_encoding(s);
7944             return;
7945         }
7946     }
7947 
7948     if (!fp_access_check(s)) {
7949         return;
7950     }
7951 
7952     element = extract32(imm5, 1+size, 4);
7953 
7954     tcg_rd = cpu_reg(s, rd);
7955     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7956     if (is_signed && !is_q) {
7957         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7958     }
7959 }
7960 
7961 /* AdvSIMD copy
7962  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7963  * +---+---+----+-----------------+------+---+------+---+------+------+
7964  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7965  * +---+---+----+-----------------+------+---+------+---+------+------+
7966  */
7967 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7968 {
7969     int rd = extract32(insn, 0, 5);
7970     int rn = extract32(insn, 5, 5);
7971     int imm4 = extract32(insn, 11, 4);
7972     int op = extract32(insn, 29, 1);
7973     int is_q = extract32(insn, 30, 1);
7974     int imm5 = extract32(insn, 16, 5);
7975 
7976     if (op) {
7977         if (is_q) {
7978             /* INS (element) */
7979             handle_simd_inse(s, rd, rn, imm4, imm5);
7980         } else {
7981             unallocated_encoding(s);
7982         }
7983     } else {
7984         switch (imm4) {
7985         case 0:
7986             /* DUP (element - vector) */
7987             handle_simd_dupe(s, is_q, rd, rn, imm5);
7988             break;
7989         case 1:
7990             /* DUP (general) */
7991             handle_simd_dupg(s, is_q, rd, rn, imm5);
7992             break;
7993         case 3:
7994             if (is_q) {
7995                 /* INS (general) */
7996                 handle_simd_insg(s, rd, rn, imm5);
7997             } else {
7998                 unallocated_encoding(s);
7999             }
8000             break;
8001         case 5:
8002         case 7:
8003             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8004             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8005             break;
8006         default:
8007             unallocated_encoding(s);
8008             break;
8009         }
8010     }
8011 }
8012 
8013 /* AdvSIMD modified immediate
8014  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8015  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8016  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8017  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8018  *
8019  * There are a number of operations that can be carried out here:
8020  *   MOVI - move (shifted) imm into register
8021  *   MVNI - move inverted (shifted) imm into register
8022  *   ORR  - bitwise OR of (shifted) imm with register
8023  *   BIC  - bitwise clear of (shifted) imm with register
8024  * With ARMv8.2 we also have:
8025  *   FMOV half-precision
8026  */
8027 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8028 {
8029     int rd = extract32(insn, 0, 5);
8030     int cmode = extract32(insn, 12, 4);
8031     int o2 = extract32(insn, 11, 1);
8032     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8033     bool is_neg = extract32(insn, 29, 1);
8034     bool is_q = extract32(insn, 30, 1);
8035     uint64_t imm = 0;
8036 
8037     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8038         /* Check for FMOV (vector, immediate) - half-precision */
8039         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8040             unallocated_encoding(s);
8041             return;
8042         }
8043     }
8044 
8045     if (!fp_access_check(s)) {
8046         return;
8047     }
8048 
8049     if (cmode == 15 && o2 && !is_neg) {
8050         /* FMOV (vector, immediate) - half-precision */
8051         imm = vfp_expand_imm(MO_16, abcdefgh);
8052         /* now duplicate across the lanes */
8053         imm = dup_const(MO_16, imm);
8054     } else {
8055         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8056     }
8057 
8058     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8059         /* MOVI or MVNI, with MVNI negation handled above.  */
8060         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8061                              vec_full_reg_size(s), imm);
8062     } else {
8063         /* ORR or BIC, with BIC negation to AND handled above.  */
8064         if (is_neg) {
8065             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8066         } else {
8067             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8068         }
8069     }
8070 }
8071 
8072 /* AdvSIMD scalar copy
8073  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8074  * +-----+----+-----------------+------+---+------+---+------+------+
8075  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8076  * +-----+----+-----------------+------+---+------+---+------+------+
8077  */
8078 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8079 {
8080     int rd = extract32(insn, 0, 5);
8081     int rn = extract32(insn, 5, 5);
8082     int imm4 = extract32(insn, 11, 4);
8083     int imm5 = extract32(insn, 16, 5);
8084     int op = extract32(insn, 29, 1);
8085 
8086     if (op != 0 || imm4 != 0) {
8087         unallocated_encoding(s);
8088         return;
8089     }
8090 
8091     /* DUP (element, scalar) */
8092     handle_simd_dupes(s, rd, rn, imm5);
8093 }
8094 
8095 /* AdvSIMD scalar pairwise
8096  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8097  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8098  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8099  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8100  */
8101 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8102 {
8103     int u = extract32(insn, 29, 1);
8104     int size = extract32(insn, 22, 2);
8105     int opcode = extract32(insn, 12, 5);
8106     int rn = extract32(insn, 5, 5);
8107     int rd = extract32(insn, 0, 5);
8108     TCGv_ptr fpst;
8109 
8110     /* For some ops (the FP ones), size[1] is part of the encoding.
8111      * For ADDP strictly it is not but size[1] is always 1 for valid
8112      * encodings.
8113      */
8114     opcode |= (extract32(size, 1, 1) << 5);
8115 
8116     switch (opcode) {
8117     case 0x3b: /* ADDP */
8118         if (u || size != 3) {
8119             unallocated_encoding(s);
8120             return;
8121         }
8122         if (!fp_access_check(s)) {
8123             return;
8124         }
8125 
8126         fpst = NULL;
8127         break;
8128     case 0xc: /* FMAXNMP */
8129     case 0xd: /* FADDP */
8130     case 0xf: /* FMAXP */
8131     case 0x2c: /* FMINNMP */
8132     case 0x2f: /* FMINP */
8133         /* FP op, size[0] is 32 or 64 bit*/
8134         if (!u) {
8135             if (!dc_isar_feature(aa64_fp16, s)) {
8136                 unallocated_encoding(s);
8137                 return;
8138             } else {
8139                 size = MO_16;
8140             }
8141         } else {
8142             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8143         }
8144 
8145         if (!fp_access_check(s)) {
8146             return;
8147         }
8148 
8149         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8150         break;
8151     default:
8152         unallocated_encoding(s);
8153         return;
8154     }
8155 
8156     if (size == MO_64) {
8157         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8158         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8159         TCGv_i64 tcg_res = tcg_temp_new_i64();
8160 
8161         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8162         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8163 
8164         switch (opcode) {
8165         case 0x3b: /* ADDP */
8166             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8167             break;
8168         case 0xc: /* FMAXNMP */
8169             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8170             break;
8171         case 0xd: /* FADDP */
8172             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8173             break;
8174         case 0xf: /* FMAXP */
8175             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8176             break;
8177         case 0x2c: /* FMINNMP */
8178             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8179             break;
8180         case 0x2f: /* FMINP */
8181             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8182             break;
8183         default:
8184             g_assert_not_reached();
8185         }
8186 
8187         write_fp_dreg(s, rd, tcg_res);
8188     } else {
8189         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8190         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8191         TCGv_i32 tcg_res = tcg_temp_new_i32();
8192 
8193         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8194         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8195 
8196         if (size == MO_16) {
8197             switch (opcode) {
8198             case 0xc: /* FMAXNMP */
8199                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8200                 break;
8201             case 0xd: /* FADDP */
8202                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8203                 break;
8204             case 0xf: /* FMAXP */
8205                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8206                 break;
8207             case 0x2c: /* FMINNMP */
8208                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8209                 break;
8210             case 0x2f: /* FMINP */
8211                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8212                 break;
8213             default:
8214                 g_assert_not_reached();
8215             }
8216         } else {
8217             switch (opcode) {
8218             case 0xc: /* FMAXNMP */
8219                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8220                 break;
8221             case 0xd: /* FADDP */
8222                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8223                 break;
8224             case 0xf: /* FMAXP */
8225                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8226                 break;
8227             case 0x2c: /* FMINNMP */
8228                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8229                 break;
8230             case 0x2f: /* FMINP */
8231                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8232                 break;
8233             default:
8234                 g_assert_not_reached();
8235             }
8236         }
8237 
8238         write_fp_sreg(s, rd, tcg_res);
8239     }
8240 }
8241 
8242 /*
8243  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8244  *
8245  * This code is handles the common shifting code and is used by both
8246  * the vector and scalar code.
8247  */
8248 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8249                                     TCGv_i64 tcg_rnd, bool accumulate,
8250                                     bool is_u, int size, int shift)
8251 {
8252     bool extended_result = false;
8253     bool round = tcg_rnd != NULL;
8254     int ext_lshift = 0;
8255     TCGv_i64 tcg_src_hi;
8256 
8257     if (round && size == 3) {
8258         extended_result = true;
8259         ext_lshift = 64 - shift;
8260         tcg_src_hi = tcg_temp_new_i64();
8261     } else if (shift == 64) {
8262         if (!accumulate && is_u) {
8263             /* result is zero */
8264             tcg_gen_movi_i64(tcg_res, 0);
8265             return;
8266         }
8267     }
8268 
8269     /* Deal with the rounding step */
8270     if (round) {
8271         if (extended_result) {
8272             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8273             if (!is_u) {
8274                 /* take care of sign extending tcg_res */
8275                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8276                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8277                                  tcg_src, tcg_src_hi,
8278                                  tcg_rnd, tcg_zero);
8279             } else {
8280                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8281                                  tcg_src, tcg_zero,
8282                                  tcg_rnd, tcg_zero);
8283             }
8284         } else {
8285             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8286         }
8287     }
8288 
8289     /* Now do the shift right */
8290     if (round && extended_result) {
8291         /* extended case, >64 bit precision required */
8292         if (ext_lshift == 0) {
8293             /* special case, only high bits matter */
8294             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8295         } else {
8296             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8297             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8298             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8299         }
8300     } else {
8301         if (is_u) {
8302             if (shift == 64) {
8303                 /* essentially shifting in 64 zeros */
8304                 tcg_gen_movi_i64(tcg_src, 0);
8305             } else {
8306                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8307             }
8308         } else {
8309             if (shift == 64) {
8310                 /* effectively extending the sign-bit */
8311                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8312             } else {
8313                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8314             }
8315         }
8316     }
8317 
8318     if (accumulate) {
8319         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8320     } else {
8321         tcg_gen_mov_i64(tcg_res, tcg_src);
8322     }
8323 }
8324 
8325 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8326 static void handle_scalar_simd_shri(DisasContext *s,
8327                                     bool is_u, int immh, int immb,
8328                                     int opcode, int rn, int rd)
8329 {
8330     const int size = 3;
8331     int immhb = immh << 3 | immb;
8332     int shift = 2 * (8 << size) - immhb;
8333     bool accumulate = false;
8334     bool round = false;
8335     bool insert = false;
8336     TCGv_i64 tcg_rn;
8337     TCGv_i64 tcg_rd;
8338     TCGv_i64 tcg_round;
8339 
8340     if (!extract32(immh, 3, 1)) {
8341         unallocated_encoding(s);
8342         return;
8343     }
8344 
8345     if (!fp_access_check(s)) {
8346         return;
8347     }
8348 
8349     switch (opcode) {
8350     case 0x02: /* SSRA / USRA (accumulate) */
8351         accumulate = true;
8352         break;
8353     case 0x04: /* SRSHR / URSHR (rounding) */
8354         round = true;
8355         break;
8356     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8357         accumulate = round = true;
8358         break;
8359     case 0x08: /* SRI */
8360         insert = true;
8361         break;
8362     }
8363 
8364     if (round) {
8365         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8366     } else {
8367         tcg_round = NULL;
8368     }
8369 
8370     tcg_rn = read_fp_dreg(s, rn);
8371     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8372 
8373     if (insert) {
8374         /* shift count same as element size is valid but does nothing;
8375          * special case to avoid potential shift by 64.
8376          */
8377         int esize = 8 << size;
8378         if (shift != esize) {
8379             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8380             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8381         }
8382     } else {
8383         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8384                                 accumulate, is_u, size, shift);
8385     }
8386 
8387     write_fp_dreg(s, rd, tcg_rd);
8388 }
8389 
8390 /* SHL/SLI - Scalar shift left */
8391 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8392                                     int immh, int immb, int opcode,
8393                                     int rn, int rd)
8394 {
8395     int size = 32 - clz32(immh) - 1;
8396     int immhb = immh << 3 | immb;
8397     int shift = immhb - (8 << size);
8398     TCGv_i64 tcg_rn;
8399     TCGv_i64 tcg_rd;
8400 
8401     if (!extract32(immh, 3, 1)) {
8402         unallocated_encoding(s);
8403         return;
8404     }
8405 
8406     if (!fp_access_check(s)) {
8407         return;
8408     }
8409 
8410     tcg_rn = read_fp_dreg(s, rn);
8411     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8412 
8413     if (insert) {
8414         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8415     } else {
8416         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8417     }
8418 
8419     write_fp_dreg(s, rd, tcg_rd);
8420 }
8421 
8422 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8423  * (signed/unsigned) narrowing */
8424 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8425                                    bool is_u_shift, bool is_u_narrow,
8426                                    int immh, int immb, int opcode,
8427                                    int rn, int rd)
8428 {
8429     int immhb = immh << 3 | immb;
8430     int size = 32 - clz32(immh) - 1;
8431     int esize = 8 << size;
8432     int shift = (2 * esize) - immhb;
8433     int elements = is_scalar ? 1 : (64 / esize);
8434     bool round = extract32(opcode, 0, 1);
8435     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8436     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8437     TCGv_i32 tcg_rd_narrowed;
8438     TCGv_i64 tcg_final;
8439 
8440     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8441         { gen_helper_neon_narrow_sat_s8,
8442           gen_helper_neon_unarrow_sat8 },
8443         { gen_helper_neon_narrow_sat_s16,
8444           gen_helper_neon_unarrow_sat16 },
8445         { gen_helper_neon_narrow_sat_s32,
8446           gen_helper_neon_unarrow_sat32 },
8447         { NULL, NULL },
8448     };
8449     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8450         gen_helper_neon_narrow_sat_u8,
8451         gen_helper_neon_narrow_sat_u16,
8452         gen_helper_neon_narrow_sat_u32,
8453         NULL
8454     };
8455     NeonGenNarrowEnvFn *narrowfn;
8456 
8457     int i;
8458 
8459     assert(size < 4);
8460 
8461     if (extract32(immh, 3, 1)) {
8462         unallocated_encoding(s);
8463         return;
8464     }
8465 
8466     if (!fp_access_check(s)) {
8467         return;
8468     }
8469 
8470     if (is_u_shift) {
8471         narrowfn = unsigned_narrow_fns[size];
8472     } else {
8473         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8474     }
8475 
8476     tcg_rn = tcg_temp_new_i64();
8477     tcg_rd = tcg_temp_new_i64();
8478     tcg_rd_narrowed = tcg_temp_new_i32();
8479     tcg_final = tcg_temp_new_i64();
8480 
8481     if (round) {
8482         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8483     } else {
8484         tcg_round = NULL;
8485     }
8486 
8487     for (i = 0; i < elements; i++) {
8488         read_vec_element(s, tcg_rn, rn, i, ldop);
8489         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8490                                 false, is_u_shift, size+1, shift);
8491         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8492         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8493         if (i == 0) {
8494             tcg_gen_mov_i64(tcg_final, tcg_rd);
8495         } else {
8496             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8497         }
8498     }
8499 
8500     if (!is_q) {
8501         write_vec_element(s, tcg_final, rd, 0, MO_64);
8502     } else {
8503         write_vec_element(s, tcg_final, rd, 1, MO_64);
8504     }
8505     clear_vec_high(s, is_q, rd);
8506 }
8507 
8508 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8509 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8510                              bool src_unsigned, bool dst_unsigned,
8511                              int immh, int immb, int rn, int rd)
8512 {
8513     int immhb = immh << 3 | immb;
8514     int size = 32 - clz32(immh) - 1;
8515     int shift = immhb - (8 << size);
8516     int pass;
8517 
8518     assert(immh != 0);
8519     assert(!(scalar && is_q));
8520 
8521     if (!scalar) {
8522         if (!is_q && extract32(immh, 3, 1)) {
8523             unallocated_encoding(s);
8524             return;
8525         }
8526 
8527         /* Since we use the variable-shift helpers we must
8528          * replicate the shift count into each element of
8529          * the tcg_shift value.
8530          */
8531         switch (size) {
8532         case 0:
8533             shift |= shift << 8;
8534             /* fall through */
8535         case 1:
8536             shift |= shift << 16;
8537             break;
8538         case 2:
8539         case 3:
8540             break;
8541         default:
8542             g_assert_not_reached();
8543         }
8544     }
8545 
8546     if (!fp_access_check(s)) {
8547         return;
8548     }
8549 
8550     if (size == 3) {
8551         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8552         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8553             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8554             { NULL, gen_helper_neon_qshl_u64 },
8555         };
8556         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8557         int maxpass = is_q ? 2 : 1;
8558 
8559         for (pass = 0; pass < maxpass; pass++) {
8560             TCGv_i64 tcg_op = tcg_temp_new_i64();
8561 
8562             read_vec_element(s, tcg_op, rn, pass, MO_64);
8563             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8564             write_vec_element(s, tcg_op, rd, pass, MO_64);
8565         }
8566         clear_vec_high(s, is_q, rd);
8567     } else {
8568         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8569         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8570             {
8571                 { gen_helper_neon_qshl_s8,
8572                   gen_helper_neon_qshl_s16,
8573                   gen_helper_neon_qshl_s32 },
8574                 { gen_helper_neon_qshlu_s8,
8575                   gen_helper_neon_qshlu_s16,
8576                   gen_helper_neon_qshlu_s32 }
8577             }, {
8578                 { NULL, NULL, NULL },
8579                 { gen_helper_neon_qshl_u8,
8580                   gen_helper_neon_qshl_u16,
8581                   gen_helper_neon_qshl_u32 }
8582             }
8583         };
8584         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8585         MemOp memop = scalar ? size : MO_32;
8586         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8587 
8588         for (pass = 0; pass < maxpass; pass++) {
8589             TCGv_i32 tcg_op = tcg_temp_new_i32();
8590 
8591             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8592             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8593             if (scalar) {
8594                 switch (size) {
8595                 case 0:
8596                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8597                     break;
8598                 case 1:
8599                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8600                     break;
8601                 case 2:
8602                     break;
8603                 default:
8604                     g_assert_not_reached();
8605                 }
8606                 write_fp_sreg(s, rd, tcg_op);
8607             } else {
8608                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8609             }
8610         }
8611 
8612         if (!scalar) {
8613             clear_vec_high(s, is_q, rd);
8614         }
8615     }
8616 }
8617 
8618 /* Common vector code for handling integer to FP conversion */
8619 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8620                                    int elements, int is_signed,
8621                                    int fracbits, int size)
8622 {
8623     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8624     TCGv_i32 tcg_shift = NULL;
8625 
8626     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8627     int pass;
8628 
8629     if (fracbits || size == MO_64) {
8630         tcg_shift = tcg_constant_i32(fracbits);
8631     }
8632 
8633     if (size == MO_64) {
8634         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8635         TCGv_i64 tcg_double = tcg_temp_new_i64();
8636 
8637         for (pass = 0; pass < elements; pass++) {
8638             read_vec_element(s, tcg_int64, rn, pass, mop);
8639 
8640             if (is_signed) {
8641                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8642                                      tcg_shift, tcg_fpst);
8643             } else {
8644                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8645                                      tcg_shift, tcg_fpst);
8646             }
8647             if (elements == 1) {
8648                 write_fp_dreg(s, rd, tcg_double);
8649             } else {
8650                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8651             }
8652         }
8653     } else {
8654         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8655         TCGv_i32 tcg_float = tcg_temp_new_i32();
8656 
8657         for (pass = 0; pass < elements; pass++) {
8658             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8659 
8660             switch (size) {
8661             case MO_32:
8662                 if (fracbits) {
8663                     if (is_signed) {
8664                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8665                                              tcg_shift, tcg_fpst);
8666                     } else {
8667                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8668                                              tcg_shift, tcg_fpst);
8669                     }
8670                 } else {
8671                     if (is_signed) {
8672                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8673                     } else {
8674                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8675                     }
8676                 }
8677                 break;
8678             case MO_16:
8679                 if (fracbits) {
8680                     if (is_signed) {
8681                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8682                                              tcg_shift, tcg_fpst);
8683                     } else {
8684                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8685                                              tcg_shift, tcg_fpst);
8686                     }
8687                 } else {
8688                     if (is_signed) {
8689                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8690                     } else {
8691                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8692                     }
8693                 }
8694                 break;
8695             default:
8696                 g_assert_not_reached();
8697             }
8698 
8699             if (elements == 1) {
8700                 write_fp_sreg(s, rd, tcg_float);
8701             } else {
8702                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8703             }
8704         }
8705     }
8706 
8707     clear_vec_high(s, elements << size == 16, rd);
8708 }
8709 
8710 /* UCVTF/SCVTF - Integer to FP conversion */
8711 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8712                                          bool is_q, bool is_u,
8713                                          int immh, int immb, int opcode,
8714                                          int rn, int rd)
8715 {
8716     int size, elements, fracbits;
8717     int immhb = immh << 3 | immb;
8718 
8719     if (immh & 8) {
8720         size = MO_64;
8721         if (!is_scalar && !is_q) {
8722             unallocated_encoding(s);
8723             return;
8724         }
8725     } else if (immh & 4) {
8726         size = MO_32;
8727     } else if (immh & 2) {
8728         size = MO_16;
8729         if (!dc_isar_feature(aa64_fp16, s)) {
8730             unallocated_encoding(s);
8731             return;
8732         }
8733     } else {
8734         /* immh == 0 would be a failure of the decode logic */
8735         g_assert(immh == 1);
8736         unallocated_encoding(s);
8737         return;
8738     }
8739 
8740     if (is_scalar) {
8741         elements = 1;
8742     } else {
8743         elements = (8 << is_q) >> size;
8744     }
8745     fracbits = (16 << size) - immhb;
8746 
8747     if (!fp_access_check(s)) {
8748         return;
8749     }
8750 
8751     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8752 }
8753 
8754 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8755 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8756                                          bool is_q, bool is_u,
8757                                          int immh, int immb, int rn, int rd)
8758 {
8759     int immhb = immh << 3 | immb;
8760     int pass, size, fracbits;
8761     TCGv_ptr tcg_fpstatus;
8762     TCGv_i32 tcg_rmode, tcg_shift;
8763 
8764     if (immh & 0x8) {
8765         size = MO_64;
8766         if (!is_scalar && !is_q) {
8767             unallocated_encoding(s);
8768             return;
8769         }
8770     } else if (immh & 0x4) {
8771         size = MO_32;
8772     } else if (immh & 0x2) {
8773         size = MO_16;
8774         if (!dc_isar_feature(aa64_fp16, s)) {
8775             unallocated_encoding(s);
8776             return;
8777         }
8778     } else {
8779         /* Should have split out AdvSIMD modified immediate earlier.  */
8780         assert(immh == 1);
8781         unallocated_encoding(s);
8782         return;
8783     }
8784 
8785     if (!fp_access_check(s)) {
8786         return;
8787     }
8788 
8789     assert(!(is_scalar && is_q));
8790 
8791     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8792     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8793     fracbits = (16 << size) - immhb;
8794     tcg_shift = tcg_constant_i32(fracbits);
8795 
8796     if (size == MO_64) {
8797         int maxpass = is_scalar ? 1 : 2;
8798 
8799         for (pass = 0; pass < maxpass; pass++) {
8800             TCGv_i64 tcg_op = tcg_temp_new_i64();
8801 
8802             read_vec_element(s, tcg_op, rn, pass, MO_64);
8803             if (is_u) {
8804                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8805             } else {
8806                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8807             }
8808             write_vec_element(s, tcg_op, rd, pass, MO_64);
8809         }
8810         clear_vec_high(s, is_q, rd);
8811     } else {
8812         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8813         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8814 
8815         switch (size) {
8816         case MO_16:
8817             if (is_u) {
8818                 fn = gen_helper_vfp_touhh;
8819             } else {
8820                 fn = gen_helper_vfp_toshh;
8821             }
8822             break;
8823         case MO_32:
8824             if (is_u) {
8825                 fn = gen_helper_vfp_touls;
8826             } else {
8827                 fn = gen_helper_vfp_tosls;
8828             }
8829             break;
8830         default:
8831             g_assert_not_reached();
8832         }
8833 
8834         for (pass = 0; pass < maxpass; pass++) {
8835             TCGv_i32 tcg_op = tcg_temp_new_i32();
8836 
8837             read_vec_element_i32(s, tcg_op, rn, pass, size);
8838             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8839             if (is_scalar) {
8840                 write_fp_sreg(s, rd, tcg_op);
8841             } else {
8842                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8843             }
8844         }
8845         if (!is_scalar) {
8846             clear_vec_high(s, is_q, rd);
8847         }
8848     }
8849 
8850     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8851 }
8852 
8853 /* AdvSIMD scalar shift by immediate
8854  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8855  * +-----+---+-------------+------+------+--------+---+------+------+
8856  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8857  * +-----+---+-------------+------+------+--------+---+------+------+
8858  *
8859  * This is the scalar version so it works on a fixed sized registers
8860  */
8861 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8862 {
8863     int rd = extract32(insn, 0, 5);
8864     int rn = extract32(insn, 5, 5);
8865     int opcode = extract32(insn, 11, 5);
8866     int immb = extract32(insn, 16, 3);
8867     int immh = extract32(insn, 19, 4);
8868     bool is_u = extract32(insn, 29, 1);
8869 
8870     if (immh == 0) {
8871         unallocated_encoding(s);
8872         return;
8873     }
8874 
8875     switch (opcode) {
8876     case 0x08: /* SRI */
8877         if (!is_u) {
8878             unallocated_encoding(s);
8879             return;
8880         }
8881         /* fall through */
8882     case 0x00: /* SSHR / USHR */
8883     case 0x02: /* SSRA / USRA */
8884     case 0x04: /* SRSHR / URSHR */
8885     case 0x06: /* SRSRA / URSRA */
8886         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8887         break;
8888     case 0x0a: /* SHL / SLI */
8889         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8890         break;
8891     case 0x1c: /* SCVTF, UCVTF */
8892         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8893                                      opcode, rn, rd);
8894         break;
8895     case 0x10: /* SQSHRUN, SQSHRUN2 */
8896     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8897         if (!is_u) {
8898             unallocated_encoding(s);
8899             return;
8900         }
8901         handle_vec_simd_sqshrn(s, true, false, false, true,
8902                                immh, immb, opcode, rn, rd);
8903         break;
8904     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8905     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8906         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8907                                immh, immb, opcode, rn, rd);
8908         break;
8909     case 0xc: /* SQSHLU */
8910         if (!is_u) {
8911             unallocated_encoding(s);
8912             return;
8913         }
8914         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8915         break;
8916     case 0xe: /* SQSHL, UQSHL */
8917         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8918         break;
8919     case 0x1f: /* FCVTZS, FCVTZU */
8920         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8921         break;
8922     default:
8923         unallocated_encoding(s);
8924         break;
8925     }
8926 }
8927 
8928 /* AdvSIMD scalar three different
8929  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8930  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8931  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8932  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8933  */
8934 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8935 {
8936     bool is_u = extract32(insn, 29, 1);
8937     int size = extract32(insn, 22, 2);
8938     int opcode = extract32(insn, 12, 4);
8939     int rm = extract32(insn, 16, 5);
8940     int rn = extract32(insn, 5, 5);
8941     int rd = extract32(insn, 0, 5);
8942 
8943     if (is_u) {
8944         unallocated_encoding(s);
8945         return;
8946     }
8947 
8948     switch (opcode) {
8949     case 0x9: /* SQDMLAL, SQDMLAL2 */
8950     case 0xb: /* SQDMLSL, SQDMLSL2 */
8951     case 0xd: /* SQDMULL, SQDMULL2 */
8952         if (size == 0 || size == 3) {
8953             unallocated_encoding(s);
8954             return;
8955         }
8956         break;
8957     default:
8958         unallocated_encoding(s);
8959         return;
8960     }
8961 
8962     if (!fp_access_check(s)) {
8963         return;
8964     }
8965 
8966     if (size == 2) {
8967         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8968         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8969         TCGv_i64 tcg_res = tcg_temp_new_i64();
8970 
8971         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8972         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8973 
8974         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8975         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8976 
8977         switch (opcode) {
8978         case 0xd: /* SQDMULL, SQDMULL2 */
8979             break;
8980         case 0xb: /* SQDMLSL, SQDMLSL2 */
8981             tcg_gen_neg_i64(tcg_res, tcg_res);
8982             /* fall through */
8983         case 0x9: /* SQDMLAL, SQDMLAL2 */
8984             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8985             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8986                                               tcg_res, tcg_op1);
8987             break;
8988         default:
8989             g_assert_not_reached();
8990         }
8991 
8992         write_fp_dreg(s, rd, tcg_res);
8993     } else {
8994         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8995         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8996         TCGv_i64 tcg_res = tcg_temp_new_i64();
8997 
8998         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8999         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9000 
9001         switch (opcode) {
9002         case 0xd: /* SQDMULL, SQDMULL2 */
9003             break;
9004         case 0xb: /* SQDMLSL, SQDMLSL2 */
9005             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9006             /* fall through */
9007         case 0x9: /* SQDMLAL, SQDMLAL2 */
9008         {
9009             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9010             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9011             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9012                                               tcg_res, tcg_op3);
9013             break;
9014         }
9015         default:
9016             g_assert_not_reached();
9017         }
9018 
9019         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9020         write_fp_dreg(s, rd, tcg_res);
9021     }
9022 }
9023 
9024 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9025                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9026 {
9027     /* Handle 64x64->64 opcodes which are shared between the scalar
9028      * and vector 3-same groups. We cover every opcode where size == 3
9029      * is valid in either the three-reg-same (integer, not pairwise)
9030      * or scalar-three-reg-same groups.
9031      */
9032     TCGCond cond;
9033 
9034     switch (opcode) {
9035     case 0x1: /* SQADD */
9036         if (u) {
9037             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9038         } else {
9039             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9040         }
9041         break;
9042     case 0x5: /* SQSUB */
9043         if (u) {
9044             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9045         } else {
9046             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9047         }
9048         break;
9049     case 0x6: /* CMGT, CMHI */
9050         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9051          * We implement this using setcond (test) and then negating.
9052          */
9053         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9054     do_cmop:
9055         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9056         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9057         break;
9058     case 0x7: /* CMGE, CMHS */
9059         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9060         goto do_cmop;
9061     case 0x11: /* CMTST, CMEQ */
9062         if (u) {
9063             cond = TCG_COND_EQ;
9064             goto do_cmop;
9065         }
9066         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9067         break;
9068     case 0x8: /* SSHL, USHL */
9069         if (u) {
9070             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9071         } else {
9072             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9073         }
9074         break;
9075     case 0x9: /* SQSHL, UQSHL */
9076         if (u) {
9077             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9078         } else {
9079             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9080         }
9081         break;
9082     case 0xa: /* SRSHL, URSHL */
9083         if (u) {
9084             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9085         } else {
9086             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9087         }
9088         break;
9089     case 0xb: /* SQRSHL, UQRSHL */
9090         if (u) {
9091             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9092         } else {
9093             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9094         }
9095         break;
9096     case 0x10: /* ADD, SUB */
9097         if (u) {
9098             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9099         } else {
9100             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9101         }
9102         break;
9103     default:
9104         g_assert_not_reached();
9105     }
9106 }
9107 
9108 /* Handle the 3-same-operands float operations; shared by the scalar
9109  * and vector encodings. The caller must filter out any encodings
9110  * not allocated for the encoding it is dealing with.
9111  */
9112 static void handle_3same_float(DisasContext *s, int size, int elements,
9113                                int fpopcode, int rd, int rn, int rm)
9114 {
9115     int pass;
9116     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9117 
9118     for (pass = 0; pass < elements; pass++) {
9119         if (size) {
9120             /* Double */
9121             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9122             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9123             TCGv_i64 tcg_res = tcg_temp_new_i64();
9124 
9125             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9126             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9127 
9128             switch (fpopcode) {
9129             case 0x39: /* FMLS */
9130                 /* As usual for ARM, separate negation for fused multiply-add */
9131                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9132                 /* fall through */
9133             case 0x19: /* FMLA */
9134                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9135                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9136                                        tcg_res, fpst);
9137                 break;
9138             case 0x18: /* FMAXNM */
9139                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9140                 break;
9141             case 0x1a: /* FADD */
9142                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9143                 break;
9144             case 0x1b: /* FMULX */
9145                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9146                 break;
9147             case 0x1c: /* FCMEQ */
9148                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9149                 break;
9150             case 0x1e: /* FMAX */
9151                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9152                 break;
9153             case 0x1f: /* FRECPS */
9154                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9155                 break;
9156             case 0x38: /* FMINNM */
9157                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9158                 break;
9159             case 0x3a: /* FSUB */
9160                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9161                 break;
9162             case 0x3e: /* FMIN */
9163                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9164                 break;
9165             case 0x3f: /* FRSQRTS */
9166                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9167                 break;
9168             case 0x5b: /* FMUL */
9169                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9170                 break;
9171             case 0x5c: /* FCMGE */
9172                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9173                 break;
9174             case 0x5d: /* FACGE */
9175                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9176                 break;
9177             case 0x5f: /* FDIV */
9178                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9179                 break;
9180             case 0x7a: /* FABD */
9181                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9182                 gen_helper_vfp_absd(tcg_res, tcg_res);
9183                 break;
9184             case 0x7c: /* FCMGT */
9185                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9186                 break;
9187             case 0x7d: /* FACGT */
9188                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9189                 break;
9190             default:
9191                 g_assert_not_reached();
9192             }
9193 
9194             write_vec_element(s, tcg_res, rd, pass, MO_64);
9195         } else {
9196             /* Single */
9197             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9198             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9199             TCGv_i32 tcg_res = tcg_temp_new_i32();
9200 
9201             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9202             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9203 
9204             switch (fpopcode) {
9205             case 0x39: /* FMLS */
9206                 /* As usual for ARM, separate negation for fused multiply-add */
9207                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9208                 /* fall through */
9209             case 0x19: /* FMLA */
9210                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9211                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9212                                        tcg_res, fpst);
9213                 break;
9214             case 0x1a: /* FADD */
9215                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9216                 break;
9217             case 0x1b: /* FMULX */
9218                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9219                 break;
9220             case 0x1c: /* FCMEQ */
9221                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9222                 break;
9223             case 0x1e: /* FMAX */
9224                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9225                 break;
9226             case 0x1f: /* FRECPS */
9227                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9228                 break;
9229             case 0x18: /* FMAXNM */
9230                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9231                 break;
9232             case 0x38: /* FMINNM */
9233                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9234                 break;
9235             case 0x3a: /* FSUB */
9236                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9237                 break;
9238             case 0x3e: /* FMIN */
9239                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9240                 break;
9241             case 0x3f: /* FRSQRTS */
9242                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9243                 break;
9244             case 0x5b: /* FMUL */
9245                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9246                 break;
9247             case 0x5c: /* FCMGE */
9248                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9249                 break;
9250             case 0x5d: /* FACGE */
9251                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9252                 break;
9253             case 0x5f: /* FDIV */
9254                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9255                 break;
9256             case 0x7a: /* FABD */
9257                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9258                 gen_helper_vfp_abss(tcg_res, tcg_res);
9259                 break;
9260             case 0x7c: /* FCMGT */
9261                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9262                 break;
9263             case 0x7d: /* FACGT */
9264                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9265                 break;
9266             default:
9267                 g_assert_not_reached();
9268             }
9269 
9270             if (elements == 1) {
9271                 /* scalar single so clear high part */
9272                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9273 
9274                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9275                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9276             } else {
9277                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9278             }
9279         }
9280     }
9281 
9282     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9283 }
9284 
9285 /* AdvSIMD scalar three same
9286  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9287  * +-----+---+-----------+------+---+------+--------+---+------+------+
9288  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9289  * +-----+---+-----------+------+---+------+--------+---+------+------+
9290  */
9291 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9292 {
9293     int rd = extract32(insn, 0, 5);
9294     int rn = extract32(insn, 5, 5);
9295     int opcode = extract32(insn, 11, 5);
9296     int rm = extract32(insn, 16, 5);
9297     int size = extract32(insn, 22, 2);
9298     bool u = extract32(insn, 29, 1);
9299     TCGv_i64 tcg_rd;
9300 
9301     if (opcode >= 0x18) {
9302         /* Floating point: U, size[1] and opcode indicate operation */
9303         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9304         switch (fpopcode) {
9305         case 0x1b: /* FMULX */
9306         case 0x1f: /* FRECPS */
9307         case 0x3f: /* FRSQRTS */
9308         case 0x5d: /* FACGE */
9309         case 0x7d: /* FACGT */
9310         case 0x1c: /* FCMEQ */
9311         case 0x5c: /* FCMGE */
9312         case 0x7c: /* FCMGT */
9313         case 0x7a: /* FABD */
9314             break;
9315         default:
9316             unallocated_encoding(s);
9317             return;
9318         }
9319 
9320         if (!fp_access_check(s)) {
9321             return;
9322         }
9323 
9324         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9325         return;
9326     }
9327 
9328     switch (opcode) {
9329     case 0x1: /* SQADD, UQADD */
9330     case 0x5: /* SQSUB, UQSUB */
9331     case 0x9: /* SQSHL, UQSHL */
9332     case 0xb: /* SQRSHL, UQRSHL */
9333         break;
9334     case 0x8: /* SSHL, USHL */
9335     case 0xa: /* SRSHL, URSHL */
9336     case 0x6: /* CMGT, CMHI */
9337     case 0x7: /* CMGE, CMHS */
9338     case 0x11: /* CMTST, CMEQ */
9339     case 0x10: /* ADD, SUB (vector) */
9340         if (size != 3) {
9341             unallocated_encoding(s);
9342             return;
9343         }
9344         break;
9345     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9346         if (size != 1 && size != 2) {
9347             unallocated_encoding(s);
9348             return;
9349         }
9350         break;
9351     default:
9352         unallocated_encoding(s);
9353         return;
9354     }
9355 
9356     if (!fp_access_check(s)) {
9357         return;
9358     }
9359 
9360     tcg_rd = tcg_temp_new_i64();
9361 
9362     if (size == 3) {
9363         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9364         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9365 
9366         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9367     } else {
9368         /* Do a single operation on the lowest element in the vector.
9369          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9370          * no side effects for all these operations.
9371          * OPTME: special-purpose helpers would avoid doing some
9372          * unnecessary work in the helper for the 8 and 16 bit cases.
9373          */
9374         NeonGenTwoOpEnvFn *genenvfn;
9375         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9376         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9377         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9378 
9379         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9380         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9381 
9382         switch (opcode) {
9383         case 0x1: /* SQADD, UQADD */
9384         {
9385             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9386                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9387                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9388                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9389             };
9390             genenvfn = fns[size][u];
9391             break;
9392         }
9393         case 0x5: /* SQSUB, UQSUB */
9394         {
9395             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9396                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9397                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9398                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9399             };
9400             genenvfn = fns[size][u];
9401             break;
9402         }
9403         case 0x9: /* SQSHL, UQSHL */
9404         {
9405             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9406                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9407                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9408                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9409             };
9410             genenvfn = fns[size][u];
9411             break;
9412         }
9413         case 0xb: /* SQRSHL, UQRSHL */
9414         {
9415             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9416                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9417                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9418                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9419             };
9420             genenvfn = fns[size][u];
9421             break;
9422         }
9423         case 0x16: /* SQDMULH, SQRDMULH */
9424         {
9425             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9426                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9427                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9428             };
9429             assert(size == 1 || size == 2);
9430             genenvfn = fns[size - 1][u];
9431             break;
9432         }
9433         default:
9434             g_assert_not_reached();
9435         }
9436 
9437         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9438         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9439     }
9440 
9441     write_fp_dreg(s, rd, tcg_rd);
9442 }
9443 
9444 /* AdvSIMD scalar three same FP16
9445  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9446  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9447  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9448  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9449  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9450  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9451  */
9452 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9453                                                   uint32_t insn)
9454 {
9455     int rd = extract32(insn, 0, 5);
9456     int rn = extract32(insn, 5, 5);
9457     int opcode = extract32(insn, 11, 3);
9458     int rm = extract32(insn, 16, 5);
9459     bool u = extract32(insn, 29, 1);
9460     bool a = extract32(insn, 23, 1);
9461     int fpopcode = opcode | (a << 3) |  (u << 4);
9462     TCGv_ptr fpst;
9463     TCGv_i32 tcg_op1;
9464     TCGv_i32 tcg_op2;
9465     TCGv_i32 tcg_res;
9466 
9467     switch (fpopcode) {
9468     case 0x03: /* FMULX */
9469     case 0x04: /* FCMEQ (reg) */
9470     case 0x07: /* FRECPS */
9471     case 0x0f: /* FRSQRTS */
9472     case 0x14: /* FCMGE (reg) */
9473     case 0x15: /* FACGE */
9474     case 0x1a: /* FABD */
9475     case 0x1c: /* FCMGT (reg) */
9476     case 0x1d: /* FACGT */
9477         break;
9478     default:
9479         unallocated_encoding(s);
9480         return;
9481     }
9482 
9483     if (!dc_isar_feature(aa64_fp16, s)) {
9484         unallocated_encoding(s);
9485     }
9486 
9487     if (!fp_access_check(s)) {
9488         return;
9489     }
9490 
9491     fpst = fpstatus_ptr(FPST_FPCR_F16);
9492 
9493     tcg_op1 = read_fp_hreg(s, rn);
9494     tcg_op2 = read_fp_hreg(s, rm);
9495     tcg_res = tcg_temp_new_i32();
9496 
9497     switch (fpopcode) {
9498     case 0x03: /* FMULX */
9499         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9500         break;
9501     case 0x04: /* FCMEQ (reg) */
9502         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9503         break;
9504     case 0x07: /* FRECPS */
9505         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9506         break;
9507     case 0x0f: /* FRSQRTS */
9508         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9509         break;
9510     case 0x14: /* FCMGE (reg) */
9511         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9512         break;
9513     case 0x15: /* FACGE */
9514         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9515         break;
9516     case 0x1a: /* FABD */
9517         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9518         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9519         break;
9520     case 0x1c: /* FCMGT (reg) */
9521         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9522         break;
9523     case 0x1d: /* FACGT */
9524         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9525         break;
9526     default:
9527         g_assert_not_reached();
9528     }
9529 
9530     write_fp_sreg(s, rd, tcg_res);
9531 }
9532 
9533 /* AdvSIMD scalar three same extra
9534  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9535  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9536  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9537  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9538  */
9539 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9540                                                    uint32_t insn)
9541 {
9542     int rd = extract32(insn, 0, 5);
9543     int rn = extract32(insn, 5, 5);
9544     int opcode = extract32(insn, 11, 4);
9545     int rm = extract32(insn, 16, 5);
9546     int size = extract32(insn, 22, 2);
9547     bool u = extract32(insn, 29, 1);
9548     TCGv_i32 ele1, ele2, ele3;
9549     TCGv_i64 res;
9550     bool feature;
9551 
9552     switch (u * 16 + opcode) {
9553     case 0x10: /* SQRDMLAH (vector) */
9554     case 0x11: /* SQRDMLSH (vector) */
9555         if (size != 1 && size != 2) {
9556             unallocated_encoding(s);
9557             return;
9558         }
9559         feature = dc_isar_feature(aa64_rdm, s);
9560         break;
9561     default:
9562         unallocated_encoding(s);
9563         return;
9564     }
9565     if (!feature) {
9566         unallocated_encoding(s);
9567         return;
9568     }
9569     if (!fp_access_check(s)) {
9570         return;
9571     }
9572 
9573     /* Do a single operation on the lowest element in the vector.
9574      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9575      * with no side effects for all these operations.
9576      * OPTME: special-purpose helpers would avoid doing some
9577      * unnecessary work in the helper for the 16 bit cases.
9578      */
9579     ele1 = tcg_temp_new_i32();
9580     ele2 = tcg_temp_new_i32();
9581     ele3 = tcg_temp_new_i32();
9582 
9583     read_vec_element_i32(s, ele1, rn, 0, size);
9584     read_vec_element_i32(s, ele2, rm, 0, size);
9585     read_vec_element_i32(s, ele3, rd, 0, size);
9586 
9587     switch (opcode) {
9588     case 0x0: /* SQRDMLAH */
9589         if (size == 1) {
9590             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9591         } else {
9592             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9593         }
9594         break;
9595     case 0x1: /* SQRDMLSH */
9596         if (size == 1) {
9597             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9598         } else {
9599             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9600         }
9601         break;
9602     default:
9603         g_assert_not_reached();
9604     }
9605 
9606     res = tcg_temp_new_i64();
9607     tcg_gen_extu_i32_i64(res, ele3);
9608     write_fp_dreg(s, rd, res);
9609 }
9610 
9611 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9612                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9613                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9614 {
9615     /* Handle 64->64 opcodes which are shared between the scalar and
9616      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9617      * is valid in either group and also the double-precision fp ops.
9618      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9619      * requires them.
9620      */
9621     TCGCond cond;
9622 
9623     switch (opcode) {
9624     case 0x4: /* CLS, CLZ */
9625         if (u) {
9626             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9627         } else {
9628             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9629         }
9630         break;
9631     case 0x5: /* NOT */
9632         /* This opcode is shared with CNT and RBIT but we have earlier
9633          * enforced that size == 3 if and only if this is the NOT insn.
9634          */
9635         tcg_gen_not_i64(tcg_rd, tcg_rn);
9636         break;
9637     case 0x7: /* SQABS, SQNEG */
9638         if (u) {
9639             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9640         } else {
9641             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9642         }
9643         break;
9644     case 0xa: /* CMLT */
9645         /* 64 bit integer comparison against zero, result is
9646          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9647          * subtracting 1.
9648          */
9649         cond = TCG_COND_LT;
9650     do_cmop:
9651         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9652         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9653         break;
9654     case 0x8: /* CMGT, CMGE */
9655         cond = u ? TCG_COND_GE : TCG_COND_GT;
9656         goto do_cmop;
9657     case 0x9: /* CMEQ, CMLE */
9658         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9659         goto do_cmop;
9660     case 0xb: /* ABS, NEG */
9661         if (u) {
9662             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9663         } else {
9664             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9665         }
9666         break;
9667     case 0x2f: /* FABS */
9668         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9669         break;
9670     case 0x6f: /* FNEG */
9671         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9672         break;
9673     case 0x7f: /* FSQRT */
9674         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9675         break;
9676     case 0x1a: /* FCVTNS */
9677     case 0x1b: /* FCVTMS */
9678     case 0x1c: /* FCVTAS */
9679     case 0x3a: /* FCVTPS */
9680     case 0x3b: /* FCVTZS */
9681         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9682         break;
9683     case 0x5a: /* FCVTNU */
9684     case 0x5b: /* FCVTMU */
9685     case 0x5c: /* FCVTAU */
9686     case 0x7a: /* FCVTPU */
9687     case 0x7b: /* FCVTZU */
9688         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9689         break;
9690     case 0x18: /* FRINTN */
9691     case 0x19: /* FRINTM */
9692     case 0x38: /* FRINTP */
9693     case 0x39: /* FRINTZ */
9694     case 0x58: /* FRINTA */
9695     case 0x79: /* FRINTI */
9696         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9697         break;
9698     case 0x59: /* FRINTX */
9699         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9700         break;
9701     case 0x1e: /* FRINT32Z */
9702     case 0x5e: /* FRINT32X */
9703         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9704         break;
9705     case 0x1f: /* FRINT64Z */
9706     case 0x5f: /* FRINT64X */
9707         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9708         break;
9709     default:
9710         g_assert_not_reached();
9711     }
9712 }
9713 
9714 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9715                                    bool is_scalar, bool is_u, bool is_q,
9716                                    int size, int rn, int rd)
9717 {
9718     bool is_double = (size == MO_64);
9719     TCGv_ptr fpst;
9720 
9721     if (!fp_access_check(s)) {
9722         return;
9723     }
9724 
9725     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9726 
9727     if (is_double) {
9728         TCGv_i64 tcg_op = tcg_temp_new_i64();
9729         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9730         TCGv_i64 tcg_res = tcg_temp_new_i64();
9731         NeonGenTwoDoubleOpFn *genfn;
9732         bool swap = false;
9733         int pass;
9734 
9735         switch (opcode) {
9736         case 0x2e: /* FCMLT (zero) */
9737             swap = true;
9738             /* fallthrough */
9739         case 0x2c: /* FCMGT (zero) */
9740             genfn = gen_helper_neon_cgt_f64;
9741             break;
9742         case 0x2d: /* FCMEQ (zero) */
9743             genfn = gen_helper_neon_ceq_f64;
9744             break;
9745         case 0x6d: /* FCMLE (zero) */
9746             swap = true;
9747             /* fall through */
9748         case 0x6c: /* FCMGE (zero) */
9749             genfn = gen_helper_neon_cge_f64;
9750             break;
9751         default:
9752             g_assert_not_reached();
9753         }
9754 
9755         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9756             read_vec_element(s, tcg_op, rn, pass, MO_64);
9757             if (swap) {
9758                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9759             } else {
9760                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9761             }
9762             write_vec_element(s, tcg_res, rd, pass, MO_64);
9763         }
9764 
9765         clear_vec_high(s, !is_scalar, rd);
9766     } else {
9767         TCGv_i32 tcg_op = tcg_temp_new_i32();
9768         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9769         TCGv_i32 tcg_res = tcg_temp_new_i32();
9770         NeonGenTwoSingleOpFn *genfn;
9771         bool swap = false;
9772         int pass, maxpasses;
9773 
9774         if (size == MO_16) {
9775             switch (opcode) {
9776             case 0x2e: /* FCMLT (zero) */
9777                 swap = true;
9778                 /* fall through */
9779             case 0x2c: /* FCMGT (zero) */
9780                 genfn = gen_helper_advsimd_cgt_f16;
9781                 break;
9782             case 0x2d: /* FCMEQ (zero) */
9783                 genfn = gen_helper_advsimd_ceq_f16;
9784                 break;
9785             case 0x6d: /* FCMLE (zero) */
9786                 swap = true;
9787                 /* fall through */
9788             case 0x6c: /* FCMGE (zero) */
9789                 genfn = gen_helper_advsimd_cge_f16;
9790                 break;
9791             default:
9792                 g_assert_not_reached();
9793             }
9794         } else {
9795             switch (opcode) {
9796             case 0x2e: /* FCMLT (zero) */
9797                 swap = true;
9798                 /* fall through */
9799             case 0x2c: /* FCMGT (zero) */
9800                 genfn = gen_helper_neon_cgt_f32;
9801                 break;
9802             case 0x2d: /* FCMEQ (zero) */
9803                 genfn = gen_helper_neon_ceq_f32;
9804                 break;
9805             case 0x6d: /* FCMLE (zero) */
9806                 swap = true;
9807                 /* fall through */
9808             case 0x6c: /* FCMGE (zero) */
9809                 genfn = gen_helper_neon_cge_f32;
9810                 break;
9811             default:
9812                 g_assert_not_reached();
9813             }
9814         }
9815 
9816         if (is_scalar) {
9817             maxpasses = 1;
9818         } else {
9819             int vector_size = 8 << is_q;
9820             maxpasses = vector_size >> size;
9821         }
9822 
9823         for (pass = 0; pass < maxpasses; pass++) {
9824             read_vec_element_i32(s, tcg_op, rn, pass, size);
9825             if (swap) {
9826                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9827             } else {
9828                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9829             }
9830             if (is_scalar) {
9831                 write_fp_sreg(s, rd, tcg_res);
9832             } else {
9833                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9834             }
9835         }
9836 
9837         if (!is_scalar) {
9838             clear_vec_high(s, is_q, rd);
9839         }
9840     }
9841 }
9842 
9843 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9844                                     bool is_scalar, bool is_u, bool is_q,
9845                                     int size, int rn, int rd)
9846 {
9847     bool is_double = (size == 3);
9848     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9849 
9850     if (is_double) {
9851         TCGv_i64 tcg_op = tcg_temp_new_i64();
9852         TCGv_i64 tcg_res = tcg_temp_new_i64();
9853         int pass;
9854 
9855         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9856             read_vec_element(s, tcg_op, rn, pass, MO_64);
9857             switch (opcode) {
9858             case 0x3d: /* FRECPE */
9859                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9860                 break;
9861             case 0x3f: /* FRECPX */
9862                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9863                 break;
9864             case 0x7d: /* FRSQRTE */
9865                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9866                 break;
9867             default:
9868                 g_assert_not_reached();
9869             }
9870             write_vec_element(s, tcg_res, rd, pass, MO_64);
9871         }
9872         clear_vec_high(s, !is_scalar, rd);
9873     } else {
9874         TCGv_i32 tcg_op = tcg_temp_new_i32();
9875         TCGv_i32 tcg_res = tcg_temp_new_i32();
9876         int pass, maxpasses;
9877 
9878         if (is_scalar) {
9879             maxpasses = 1;
9880         } else {
9881             maxpasses = is_q ? 4 : 2;
9882         }
9883 
9884         for (pass = 0; pass < maxpasses; pass++) {
9885             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9886 
9887             switch (opcode) {
9888             case 0x3c: /* URECPE */
9889                 gen_helper_recpe_u32(tcg_res, tcg_op);
9890                 break;
9891             case 0x3d: /* FRECPE */
9892                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9893                 break;
9894             case 0x3f: /* FRECPX */
9895                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9896                 break;
9897             case 0x7d: /* FRSQRTE */
9898                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9899                 break;
9900             default:
9901                 g_assert_not_reached();
9902             }
9903 
9904             if (is_scalar) {
9905                 write_fp_sreg(s, rd, tcg_res);
9906             } else {
9907                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9908             }
9909         }
9910         if (!is_scalar) {
9911             clear_vec_high(s, is_q, rd);
9912         }
9913     }
9914 }
9915 
9916 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9917                                 int opcode, bool u, bool is_q,
9918                                 int size, int rn, int rd)
9919 {
9920     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9921      * in the source becomes a size element in the destination).
9922      */
9923     int pass;
9924     TCGv_i32 tcg_res[2];
9925     int destelt = is_q ? 2 : 0;
9926     int passes = scalar ? 1 : 2;
9927 
9928     if (scalar) {
9929         tcg_res[1] = tcg_constant_i32(0);
9930     }
9931 
9932     for (pass = 0; pass < passes; pass++) {
9933         TCGv_i64 tcg_op = tcg_temp_new_i64();
9934         NeonGenNarrowFn *genfn = NULL;
9935         NeonGenNarrowEnvFn *genenvfn = NULL;
9936 
9937         if (scalar) {
9938             read_vec_element(s, tcg_op, rn, pass, size + 1);
9939         } else {
9940             read_vec_element(s, tcg_op, rn, pass, MO_64);
9941         }
9942         tcg_res[pass] = tcg_temp_new_i32();
9943 
9944         switch (opcode) {
9945         case 0x12: /* XTN, SQXTUN */
9946         {
9947             static NeonGenNarrowFn * const xtnfns[3] = {
9948                 gen_helper_neon_narrow_u8,
9949                 gen_helper_neon_narrow_u16,
9950                 tcg_gen_extrl_i64_i32,
9951             };
9952             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9953                 gen_helper_neon_unarrow_sat8,
9954                 gen_helper_neon_unarrow_sat16,
9955                 gen_helper_neon_unarrow_sat32,
9956             };
9957             if (u) {
9958                 genenvfn = sqxtunfns[size];
9959             } else {
9960                 genfn = xtnfns[size];
9961             }
9962             break;
9963         }
9964         case 0x14: /* SQXTN, UQXTN */
9965         {
9966             static NeonGenNarrowEnvFn * const fns[3][2] = {
9967                 { gen_helper_neon_narrow_sat_s8,
9968                   gen_helper_neon_narrow_sat_u8 },
9969                 { gen_helper_neon_narrow_sat_s16,
9970                   gen_helper_neon_narrow_sat_u16 },
9971                 { gen_helper_neon_narrow_sat_s32,
9972                   gen_helper_neon_narrow_sat_u32 },
9973             };
9974             genenvfn = fns[size][u];
9975             break;
9976         }
9977         case 0x16: /* FCVTN, FCVTN2 */
9978             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9979             if (size == 2) {
9980                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9981             } else {
9982                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9983                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9984                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9985                 TCGv_i32 ahp = get_ahp_flag();
9986 
9987                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9988                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9989                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9990                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9991             }
9992             break;
9993         case 0x36: /* BFCVTN, BFCVTN2 */
9994             {
9995                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9996                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9997             }
9998             break;
9999         case 0x56:  /* FCVTXN, FCVTXN2 */
10000             /* 64 bit to 32 bit float conversion
10001              * with von Neumann rounding (round to odd)
10002              */
10003             assert(size == 2);
10004             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10005             break;
10006         default:
10007             g_assert_not_reached();
10008         }
10009 
10010         if (genfn) {
10011             genfn(tcg_res[pass], tcg_op);
10012         } else if (genenvfn) {
10013             genenvfn(tcg_res[pass], cpu_env, tcg_op);
10014         }
10015     }
10016 
10017     for (pass = 0; pass < 2; pass++) {
10018         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10019     }
10020     clear_vec_high(s, is_q, rd);
10021 }
10022 
10023 /* Remaining saturating accumulating ops */
10024 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10025                                 bool is_q, int size, int rn, int rd)
10026 {
10027     bool is_double = (size == 3);
10028 
10029     if (is_double) {
10030         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10031         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10032         int pass;
10033 
10034         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10035             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10036             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10037 
10038             if (is_u) { /* USQADD */
10039                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10040             } else { /* SUQADD */
10041                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10042             }
10043             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10044         }
10045         clear_vec_high(s, !is_scalar, rd);
10046     } else {
10047         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10048         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10049         int pass, maxpasses;
10050 
10051         if (is_scalar) {
10052             maxpasses = 1;
10053         } else {
10054             maxpasses = is_q ? 4 : 2;
10055         }
10056 
10057         for (pass = 0; pass < maxpasses; pass++) {
10058             if (is_scalar) {
10059                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10060                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10061             } else {
10062                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10063                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10064             }
10065 
10066             if (is_u) { /* USQADD */
10067                 switch (size) {
10068                 case 0:
10069                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10070                     break;
10071                 case 1:
10072                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10073                     break;
10074                 case 2:
10075                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10076                     break;
10077                 default:
10078                     g_assert_not_reached();
10079                 }
10080             } else { /* SUQADD */
10081                 switch (size) {
10082                 case 0:
10083                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10084                     break;
10085                 case 1:
10086                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10087                     break;
10088                 case 2:
10089                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10090                     break;
10091                 default:
10092                     g_assert_not_reached();
10093                 }
10094             }
10095 
10096             if (is_scalar) {
10097                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10098             }
10099             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10100         }
10101         clear_vec_high(s, is_q, rd);
10102     }
10103 }
10104 
10105 /* AdvSIMD scalar two reg misc
10106  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10107  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10108  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10109  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10110  */
10111 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10112 {
10113     int rd = extract32(insn, 0, 5);
10114     int rn = extract32(insn, 5, 5);
10115     int opcode = extract32(insn, 12, 5);
10116     int size = extract32(insn, 22, 2);
10117     bool u = extract32(insn, 29, 1);
10118     bool is_fcvt = false;
10119     int rmode;
10120     TCGv_i32 tcg_rmode;
10121     TCGv_ptr tcg_fpstatus;
10122 
10123     switch (opcode) {
10124     case 0x3: /* USQADD / SUQADD*/
10125         if (!fp_access_check(s)) {
10126             return;
10127         }
10128         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10129         return;
10130     case 0x7: /* SQABS / SQNEG */
10131         break;
10132     case 0xa: /* CMLT */
10133         if (u) {
10134             unallocated_encoding(s);
10135             return;
10136         }
10137         /* fall through */
10138     case 0x8: /* CMGT, CMGE */
10139     case 0x9: /* CMEQ, CMLE */
10140     case 0xb: /* ABS, NEG */
10141         if (size != 3) {
10142             unallocated_encoding(s);
10143             return;
10144         }
10145         break;
10146     case 0x12: /* SQXTUN */
10147         if (!u) {
10148             unallocated_encoding(s);
10149             return;
10150         }
10151         /* fall through */
10152     case 0x14: /* SQXTN, UQXTN */
10153         if (size == 3) {
10154             unallocated_encoding(s);
10155             return;
10156         }
10157         if (!fp_access_check(s)) {
10158             return;
10159         }
10160         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10161         return;
10162     case 0xc ... 0xf:
10163     case 0x16 ... 0x1d:
10164     case 0x1f:
10165         /* Floating point: U, size[1] and opcode indicate operation;
10166          * size[0] indicates single or double precision.
10167          */
10168         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10169         size = extract32(size, 0, 1) ? 3 : 2;
10170         switch (opcode) {
10171         case 0x2c: /* FCMGT (zero) */
10172         case 0x2d: /* FCMEQ (zero) */
10173         case 0x2e: /* FCMLT (zero) */
10174         case 0x6c: /* FCMGE (zero) */
10175         case 0x6d: /* FCMLE (zero) */
10176             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10177             return;
10178         case 0x1d: /* SCVTF */
10179         case 0x5d: /* UCVTF */
10180         {
10181             bool is_signed = (opcode == 0x1d);
10182             if (!fp_access_check(s)) {
10183                 return;
10184             }
10185             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10186             return;
10187         }
10188         case 0x3d: /* FRECPE */
10189         case 0x3f: /* FRECPX */
10190         case 0x7d: /* FRSQRTE */
10191             if (!fp_access_check(s)) {
10192                 return;
10193             }
10194             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10195             return;
10196         case 0x1a: /* FCVTNS */
10197         case 0x1b: /* FCVTMS */
10198         case 0x3a: /* FCVTPS */
10199         case 0x3b: /* FCVTZS */
10200         case 0x5a: /* FCVTNU */
10201         case 0x5b: /* FCVTMU */
10202         case 0x7a: /* FCVTPU */
10203         case 0x7b: /* FCVTZU */
10204             is_fcvt = true;
10205             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10206             break;
10207         case 0x1c: /* FCVTAS */
10208         case 0x5c: /* FCVTAU */
10209             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10210             is_fcvt = true;
10211             rmode = FPROUNDING_TIEAWAY;
10212             break;
10213         case 0x56: /* FCVTXN, FCVTXN2 */
10214             if (size == 2) {
10215                 unallocated_encoding(s);
10216                 return;
10217             }
10218             if (!fp_access_check(s)) {
10219                 return;
10220             }
10221             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10222             return;
10223         default:
10224             unallocated_encoding(s);
10225             return;
10226         }
10227         break;
10228     default:
10229         unallocated_encoding(s);
10230         return;
10231     }
10232 
10233     if (!fp_access_check(s)) {
10234         return;
10235     }
10236 
10237     if (is_fcvt) {
10238         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10239         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10240     } else {
10241         tcg_fpstatus = NULL;
10242         tcg_rmode = NULL;
10243     }
10244 
10245     if (size == 3) {
10246         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10247         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10248 
10249         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10250         write_fp_dreg(s, rd, tcg_rd);
10251     } else {
10252         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10253         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10254 
10255         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10256 
10257         switch (opcode) {
10258         case 0x7: /* SQABS, SQNEG */
10259         {
10260             NeonGenOneOpEnvFn *genfn;
10261             static NeonGenOneOpEnvFn * const fns[3][2] = {
10262                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10263                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10264                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10265             };
10266             genfn = fns[size][u];
10267             genfn(tcg_rd, cpu_env, tcg_rn);
10268             break;
10269         }
10270         case 0x1a: /* FCVTNS */
10271         case 0x1b: /* FCVTMS */
10272         case 0x1c: /* FCVTAS */
10273         case 0x3a: /* FCVTPS */
10274         case 0x3b: /* FCVTZS */
10275             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10276                                  tcg_fpstatus);
10277             break;
10278         case 0x5a: /* FCVTNU */
10279         case 0x5b: /* FCVTMU */
10280         case 0x5c: /* FCVTAU */
10281         case 0x7a: /* FCVTPU */
10282         case 0x7b: /* FCVTZU */
10283             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10284                                  tcg_fpstatus);
10285             break;
10286         default:
10287             g_assert_not_reached();
10288         }
10289 
10290         write_fp_sreg(s, rd, tcg_rd);
10291     }
10292 
10293     if (is_fcvt) {
10294         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10295     }
10296 }
10297 
10298 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10299 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10300                                  int immh, int immb, int opcode, int rn, int rd)
10301 {
10302     int size = 32 - clz32(immh) - 1;
10303     int immhb = immh << 3 | immb;
10304     int shift = 2 * (8 << size) - immhb;
10305     GVecGen2iFn *gvec_fn;
10306 
10307     if (extract32(immh, 3, 1) && !is_q) {
10308         unallocated_encoding(s);
10309         return;
10310     }
10311     tcg_debug_assert(size <= 3);
10312 
10313     if (!fp_access_check(s)) {
10314         return;
10315     }
10316 
10317     switch (opcode) {
10318     case 0x02: /* SSRA / USRA (accumulate) */
10319         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10320         break;
10321 
10322     case 0x08: /* SRI */
10323         gvec_fn = gen_gvec_sri;
10324         break;
10325 
10326     case 0x00: /* SSHR / USHR */
10327         if (is_u) {
10328             if (shift == 8 << size) {
10329                 /* Shift count the same size as element size produces zero.  */
10330                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10331                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10332                 return;
10333             }
10334             gvec_fn = tcg_gen_gvec_shri;
10335         } else {
10336             /* Shift count the same size as element size produces all sign.  */
10337             if (shift == 8 << size) {
10338                 shift -= 1;
10339             }
10340             gvec_fn = tcg_gen_gvec_sari;
10341         }
10342         break;
10343 
10344     case 0x04: /* SRSHR / URSHR (rounding) */
10345         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10346         break;
10347 
10348     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10349         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10350         break;
10351 
10352     default:
10353         g_assert_not_reached();
10354     }
10355 
10356     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10357 }
10358 
10359 /* SHL/SLI - Vector shift left */
10360 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
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 
10367     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10368     assert(size >= 0 && size <= 3);
10369 
10370     if (extract32(immh, 3, 1) && !is_q) {
10371         unallocated_encoding(s);
10372         return;
10373     }
10374 
10375     if (!fp_access_check(s)) {
10376         return;
10377     }
10378 
10379     if (insert) {
10380         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10381     } else {
10382         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10383     }
10384 }
10385 
10386 /* USHLL/SHLL - Vector shift left with widening */
10387 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10388                                  int immh, int immb, int opcode, int rn, int rd)
10389 {
10390     int size = 32 - clz32(immh) - 1;
10391     int immhb = immh << 3 | immb;
10392     int shift = immhb - (8 << size);
10393     int dsize = 64;
10394     int esize = 8 << size;
10395     int elements = dsize/esize;
10396     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10397     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10398     int i;
10399 
10400     if (size >= 3) {
10401         unallocated_encoding(s);
10402         return;
10403     }
10404 
10405     if (!fp_access_check(s)) {
10406         return;
10407     }
10408 
10409     /* For the LL variants the store is larger than the load,
10410      * so if rd == rn we would overwrite parts of our input.
10411      * So load everything right now and use shifts in the main loop.
10412      */
10413     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10414 
10415     for (i = 0; i < elements; i++) {
10416         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10417         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10418         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10419         write_vec_element(s, tcg_rd, rd, i, size + 1);
10420     }
10421 }
10422 
10423 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10424 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10425                                  int immh, int immb, int opcode, int rn, int rd)
10426 {
10427     int immhb = immh << 3 | immb;
10428     int size = 32 - clz32(immh) - 1;
10429     int dsize = 64;
10430     int esize = 8 << size;
10431     int elements = dsize/esize;
10432     int shift = (2 * esize) - immhb;
10433     bool round = extract32(opcode, 0, 1);
10434     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10435     TCGv_i64 tcg_round;
10436     int i;
10437 
10438     if (extract32(immh, 3, 1)) {
10439         unallocated_encoding(s);
10440         return;
10441     }
10442 
10443     if (!fp_access_check(s)) {
10444         return;
10445     }
10446 
10447     tcg_rn = tcg_temp_new_i64();
10448     tcg_rd = tcg_temp_new_i64();
10449     tcg_final = tcg_temp_new_i64();
10450     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10451 
10452     if (round) {
10453         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10454     } else {
10455         tcg_round = NULL;
10456     }
10457 
10458     for (i = 0; i < elements; i++) {
10459         read_vec_element(s, tcg_rn, rn, i, size+1);
10460         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10461                                 false, true, size+1, shift);
10462 
10463         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10464     }
10465 
10466     if (!is_q) {
10467         write_vec_element(s, tcg_final, rd, 0, MO_64);
10468     } else {
10469         write_vec_element(s, tcg_final, rd, 1, MO_64);
10470     }
10471 
10472     clear_vec_high(s, is_q, rd);
10473 }
10474 
10475 
10476 /* AdvSIMD shift by immediate
10477  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10478  * +---+---+---+-------------+------+------+--------+---+------+------+
10479  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10480  * +---+---+---+-------------+------+------+--------+---+------+------+
10481  */
10482 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10483 {
10484     int rd = extract32(insn, 0, 5);
10485     int rn = extract32(insn, 5, 5);
10486     int opcode = extract32(insn, 11, 5);
10487     int immb = extract32(insn, 16, 3);
10488     int immh = extract32(insn, 19, 4);
10489     bool is_u = extract32(insn, 29, 1);
10490     bool is_q = extract32(insn, 30, 1);
10491 
10492     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10493     assert(immh != 0);
10494 
10495     switch (opcode) {
10496     case 0x08: /* SRI */
10497         if (!is_u) {
10498             unallocated_encoding(s);
10499             return;
10500         }
10501         /* fall through */
10502     case 0x00: /* SSHR / USHR */
10503     case 0x02: /* SSRA / USRA (accumulate) */
10504     case 0x04: /* SRSHR / URSHR (rounding) */
10505     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10506         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10507         break;
10508     case 0x0a: /* SHL / SLI */
10509         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10510         break;
10511     case 0x10: /* SHRN */
10512     case 0x11: /* RSHRN / SQRSHRUN */
10513         if (is_u) {
10514             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10515                                    opcode, rn, rd);
10516         } else {
10517             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10518         }
10519         break;
10520     case 0x12: /* SQSHRN / UQSHRN */
10521     case 0x13: /* SQRSHRN / UQRSHRN */
10522         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10523                                opcode, rn, rd);
10524         break;
10525     case 0x14: /* SSHLL / USHLL */
10526         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10527         break;
10528     case 0x1c: /* SCVTF / UCVTF */
10529         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10530                                      opcode, rn, rd);
10531         break;
10532     case 0xc: /* SQSHLU */
10533         if (!is_u) {
10534             unallocated_encoding(s);
10535             return;
10536         }
10537         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10538         break;
10539     case 0xe: /* SQSHL, UQSHL */
10540         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10541         break;
10542     case 0x1f: /* FCVTZS/ FCVTZU */
10543         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10544         return;
10545     default:
10546         unallocated_encoding(s);
10547         return;
10548     }
10549 }
10550 
10551 /* Generate code to do a "long" addition or subtraction, ie one done in
10552  * TCGv_i64 on vector lanes twice the width specified by size.
10553  */
10554 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10555                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10556 {
10557     static NeonGenTwo64OpFn * const fns[3][2] = {
10558         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10559         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10560         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10561     };
10562     NeonGenTwo64OpFn *genfn;
10563     assert(size < 3);
10564 
10565     genfn = fns[size][is_sub];
10566     genfn(tcg_res, tcg_op1, tcg_op2);
10567 }
10568 
10569 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10570                                 int opcode, int rd, int rn, int rm)
10571 {
10572     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10573     TCGv_i64 tcg_res[2];
10574     int pass, accop;
10575 
10576     tcg_res[0] = tcg_temp_new_i64();
10577     tcg_res[1] = tcg_temp_new_i64();
10578 
10579     /* Does this op do an adding accumulate, a subtracting accumulate,
10580      * or no accumulate at all?
10581      */
10582     switch (opcode) {
10583     case 5:
10584     case 8:
10585     case 9:
10586         accop = 1;
10587         break;
10588     case 10:
10589     case 11:
10590         accop = -1;
10591         break;
10592     default:
10593         accop = 0;
10594         break;
10595     }
10596 
10597     if (accop != 0) {
10598         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10599         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10600     }
10601 
10602     /* size == 2 means two 32x32->64 operations; this is worth special
10603      * casing because we can generally handle it inline.
10604      */
10605     if (size == 2) {
10606         for (pass = 0; pass < 2; pass++) {
10607             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10608             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10609             TCGv_i64 tcg_passres;
10610             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10611 
10612             int elt = pass + is_q * 2;
10613 
10614             read_vec_element(s, tcg_op1, rn, elt, memop);
10615             read_vec_element(s, tcg_op2, rm, elt, memop);
10616 
10617             if (accop == 0) {
10618                 tcg_passres = tcg_res[pass];
10619             } else {
10620                 tcg_passres = tcg_temp_new_i64();
10621             }
10622 
10623             switch (opcode) {
10624             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10625                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10626                 break;
10627             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10628                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10629                 break;
10630             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10631             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10632             {
10633                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10634                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10635 
10636                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10637                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10638                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10639                                     tcg_passres,
10640                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10641                 break;
10642             }
10643             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10644             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10645             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10646                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10647                 break;
10648             case 9: /* SQDMLAL, SQDMLAL2 */
10649             case 11: /* SQDMLSL, SQDMLSL2 */
10650             case 13: /* SQDMULL, SQDMULL2 */
10651                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10652                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10653                                                   tcg_passres, tcg_passres);
10654                 break;
10655             default:
10656                 g_assert_not_reached();
10657             }
10658 
10659             if (opcode == 9 || opcode == 11) {
10660                 /* saturating accumulate ops */
10661                 if (accop < 0) {
10662                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10663                 }
10664                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10665                                                   tcg_res[pass], tcg_passres);
10666             } else if (accop > 0) {
10667                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10668             } else if (accop < 0) {
10669                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10670             }
10671         }
10672     } else {
10673         /* size 0 or 1, generally helper functions */
10674         for (pass = 0; pass < 2; pass++) {
10675             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10676             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10677             TCGv_i64 tcg_passres;
10678             int elt = pass + is_q * 2;
10679 
10680             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10681             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10682 
10683             if (accop == 0) {
10684                 tcg_passres = tcg_res[pass];
10685             } else {
10686                 tcg_passres = tcg_temp_new_i64();
10687             }
10688 
10689             switch (opcode) {
10690             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10691             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10692             {
10693                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10694                 static NeonGenWidenFn * const widenfns[2][2] = {
10695                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10696                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10697                 };
10698                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10699 
10700                 widenfn(tcg_op2_64, tcg_op2);
10701                 widenfn(tcg_passres, tcg_op1);
10702                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10703                               tcg_passres, tcg_op2_64);
10704                 break;
10705             }
10706             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10707             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10708                 if (size == 0) {
10709                     if (is_u) {
10710                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10711                     } else {
10712                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10713                     }
10714                 } else {
10715                     if (is_u) {
10716                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10717                     } else {
10718                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10719                     }
10720                 }
10721                 break;
10722             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10723             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10724             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10725                 if (size == 0) {
10726                     if (is_u) {
10727                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10728                     } else {
10729                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10730                     }
10731                 } else {
10732                     if (is_u) {
10733                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10734                     } else {
10735                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10736                     }
10737                 }
10738                 break;
10739             case 9: /* SQDMLAL, SQDMLAL2 */
10740             case 11: /* SQDMLSL, SQDMLSL2 */
10741             case 13: /* SQDMULL, SQDMULL2 */
10742                 assert(size == 1);
10743                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10744                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10745                                                   tcg_passres, tcg_passres);
10746                 break;
10747             default:
10748                 g_assert_not_reached();
10749             }
10750 
10751             if (accop != 0) {
10752                 if (opcode == 9 || opcode == 11) {
10753                     /* saturating accumulate ops */
10754                     if (accop < 0) {
10755                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10756                     }
10757                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10758                                                       tcg_res[pass],
10759                                                       tcg_passres);
10760                 } else {
10761                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10762                                   tcg_res[pass], tcg_passres);
10763                 }
10764             }
10765         }
10766     }
10767 
10768     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10769     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10770 }
10771 
10772 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10773                             int opcode, int rd, int rn, int rm)
10774 {
10775     TCGv_i64 tcg_res[2];
10776     int part = is_q ? 2 : 0;
10777     int pass;
10778 
10779     for (pass = 0; pass < 2; pass++) {
10780         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10781         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10782         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10783         static NeonGenWidenFn * const widenfns[3][2] = {
10784             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10785             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10786             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10787         };
10788         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10789 
10790         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10791         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10792         widenfn(tcg_op2_wide, tcg_op2);
10793         tcg_res[pass] = tcg_temp_new_i64();
10794         gen_neon_addl(size, (opcode == 3),
10795                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10796     }
10797 
10798     for (pass = 0; pass < 2; pass++) {
10799         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10800     }
10801 }
10802 
10803 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10804 {
10805     tcg_gen_addi_i64(in, in, 1U << 31);
10806     tcg_gen_extrh_i64_i32(res, in);
10807 }
10808 
10809 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10810                                  int opcode, int rd, int rn, int rm)
10811 {
10812     TCGv_i32 tcg_res[2];
10813     int part = is_q ? 2 : 0;
10814     int pass;
10815 
10816     for (pass = 0; pass < 2; pass++) {
10817         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10818         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10819         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10820         static NeonGenNarrowFn * const narrowfns[3][2] = {
10821             { gen_helper_neon_narrow_high_u8,
10822               gen_helper_neon_narrow_round_high_u8 },
10823             { gen_helper_neon_narrow_high_u16,
10824               gen_helper_neon_narrow_round_high_u16 },
10825             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10826         };
10827         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10828 
10829         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10830         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10831 
10832         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10833 
10834         tcg_res[pass] = tcg_temp_new_i32();
10835         gennarrow(tcg_res[pass], tcg_wideres);
10836     }
10837 
10838     for (pass = 0; pass < 2; pass++) {
10839         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10840     }
10841     clear_vec_high(s, is_q, rd);
10842 }
10843 
10844 /* AdvSIMD three different
10845  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10846  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10847  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10848  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10849  */
10850 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10851 {
10852     /* Instructions in this group fall into three basic classes
10853      * (in each case with the operation working on each element in
10854      * the input vectors):
10855      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10856      *     128 bit input)
10857      * (2) wide 64 x 128 -> 128
10858      * (3) narrowing 128 x 128 -> 64
10859      * Here we do initial decode, catch unallocated cases and
10860      * dispatch to separate functions for each class.
10861      */
10862     int is_q = extract32(insn, 30, 1);
10863     int is_u = extract32(insn, 29, 1);
10864     int size = extract32(insn, 22, 2);
10865     int opcode = extract32(insn, 12, 4);
10866     int rm = extract32(insn, 16, 5);
10867     int rn = extract32(insn, 5, 5);
10868     int rd = extract32(insn, 0, 5);
10869 
10870     switch (opcode) {
10871     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10872     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10873         /* 64 x 128 -> 128 */
10874         if (size == 3) {
10875             unallocated_encoding(s);
10876             return;
10877         }
10878         if (!fp_access_check(s)) {
10879             return;
10880         }
10881         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10882         break;
10883     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10884     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10885         /* 128 x 128 -> 64 */
10886         if (size == 3) {
10887             unallocated_encoding(s);
10888             return;
10889         }
10890         if (!fp_access_check(s)) {
10891             return;
10892         }
10893         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10894         break;
10895     case 14: /* PMULL, PMULL2 */
10896         if (is_u) {
10897             unallocated_encoding(s);
10898             return;
10899         }
10900         switch (size) {
10901         case 0: /* PMULL.P8 */
10902             if (!fp_access_check(s)) {
10903                 return;
10904             }
10905             /* The Q field specifies lo/hi half input for this insn.  */
10906             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10907                              gen_helper_neon_pmull_h);
10908             break;
10909 
10910         case 3: /* PMULL.P64 */
10911             if (!dc_isar_feature(aa64_pmull, s)) {
10912                 unallocated_encoding(s);
10913                 return;
10914             }
10915             if (!fp_access_check(s)) {
10916                 return;
10917             }
10918             /* The Q field specifies lo/hi half input for this insn.  */
10919             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10920                              gen_helper_gvec_pmull_q);
10921             break;
10922 
10923         default:
10924             unallocated_encoding(s);
10925             break;
10926         }
10927         return;
10928     case 9: /* SQDMLAL, SQDMLAL2 */
10929     case 11: /* SQDMLSL, SQDMLSL2 */
10930     case 13: /* SQDMULL, SQDMULL2 */
10931         if (is_u || size == 0) {
10932             unallocated_encoding(s);
10933             return;
10934         }
10935         /* fall through */
10936     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10937     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10938     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10939     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10940     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10941     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10942     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10943         /* 64 x 64 -> 128 */
10944         if (size == 3) {
10945             unallocated_encoding(s);
10946             return;
10947         }
10948         if (!fp_access_check(s)) {
10949             return;
10950         }
10951 
10952         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10953         break;
10954     default:
10955         /* opcode 15 not allocated */
10956         unallocated_encoding(s);
10957         break;
10958     }
10959 }
10960 
10961 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10962 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10963 {
10964     int rd = extract32(insn, 0, 5);
10965     int rn = extract32(insn, 5, 5);
10966     int rm = extract32(insn, 16, 5);
10967     int size = extract32(insn, 22, 2);
10968     bool is_u = extract32(insn, 29, 1);
10969     bool is_q = extract32(insn, 30, 1);
10970 
10971     if (!fp_access_check(s)) {
10972         return;
10973     }
10974 
10975     switch (size + 4 * is_u) {
10976     case 0: /* AND */
10977         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10978         return;
10979     case 1: /* BIC */
10980         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10981         return;
10982     case 2: /* ORR */
10983         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10984         return;
10985     case 3: /* ORN */
10986         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10987         return;
10988     case 4: /* EOR */
10989         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10990         return;
10991 
10992     case 5: /* BSL bitwise select */
10993         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10994         return;
10995     case 6: /* BIT, bitwise insert if true */
10996         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10997         return;
10998     case 7: /* BIF, bitwise insert if false */
10999         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11000         return;
11001 
11002     default:
11003         g_assert_not_reached();
11004     }
11005 }
11006 
11007 /* Pairwise op subgroup of C3.6.16.
11008  *
11009  * This is called directly or via the handle_3same_float for float pairwise
11010  * operations where the opcode and size are calculated differently.
11011  */
11012 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11013                                    int size, int rn, int rm, int rd)
11014 {
11015     TCGv_ptr fpst;
11016     int pass;
11017 
11018     /* Floating point operations need fpst */
11019     if (opcode >= 0x58) {
11020         fpst = fpstatus_ptr(FPST_FPCR);
11021     } else {
11022         fpst = NULL;
11023     }
11024 
11025     if (!fp_access_check(s)) {
11026         return;
11027     }
11028 
11029     /* These operations work on the concatenated rm:rn, with each pair of
11030      * adjacent elements being operated on to produce an element in the result.
11031      */
11032     if (size == 3) {
11033         TCGv_i64 tcg_res[2];
11034 
11035         for (pass = 0; pass < 2; pass++) {
11036             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11037             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11038             int passreg = (pass == 0) ? rn : rm;
11039 
11040             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11041             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11042             tcg_res[pass] = tcg_temp_new_i64();
11043 
11044             switch (opcode) {
11045             case 0x17: /* ADDP */
11046                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11047                 break;
11048             case 0x58: /* FMAXNMP */
11049                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11050                 break;
11051             case 0x5a: /* FADDP */
11052                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11053                 break;
11054             case 0x5e: /* FMAXP */
11055                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11056                 break;
11057             case 0x78: /* FMINNMP */
11058                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11059                 break;
11060             case 0x7e: /* FMINP */
11061                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11062                 break;
11063             default:
11064                 g_assert_not_reached();
11065             }
11066         }
11067 
11068         for (pass = 0; pass < 2; pass++) {
11069             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11070         }
11071     } else {
11072         int maxpass = is_q ? 4 : 2;
11073         TCGv_i32 tcg_res[4];
11074 
11075         for (pass = 0; pass < maxpass; pass++) {
11076             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11077             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11078             NeonGenTwoOpFn *genfn = NULL;
11079             int passreg = pass < (maxpass / 2) ? rn : rm;
11080             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11081 
11082             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11083             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11084             tcg_res[pass] = tcg_temp_new_i32();
11085 
11086             switch (opcode) {
11087             case 0x17: /* ADDP */
11088             {
11089                 static NeonGenTwoOpFn * const fns[3] = {
11090                     gen_helper_neon_padd_u8,
11091                     gen_helper_neon_padd_u16,
11092                     tcg_gen_add_i32,
11093                 };
11094                 genfn = fns[size];
11095                 break;
11096             }
11097             case 0x14: /* SMAXP, UMAXP */
11098             {
11099                 static NeonGenTwoOpFn * const fns[3][2] = {
11100                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11101                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11102                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11103                 };
11104                 genfn = fns[size][u];
11105                 break;
11106             }
11107             case 0x15: /* SMINP, UMINP */
11108             {
11109                 static NeonGenTwoOpFn * const fns[3][2] = {
11110                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11111                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11112                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11113                 };
11114                 genfn = fns[size][u];
11115                 break;
11116             }
11117             /* The FP operations are all on single floats (32 bit) */
11118             case 0x58: /* FMAXNMP */
11119                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11120                 break;
11121             case 0x5a: /* FADDP */
11122                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11123                 break;
11124             case 0x5e: /* FMAXP */
11125                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11126                 break;
11127             case 0x78: /* FMINNMP */
11128                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11129                 break;
11130             case 0x7e: /* FMINP */
11131                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11132                 break;
11133             default:
11134                 g_assert_not_reached();
11135             }
11136 
11137             /* FP ops called directly, otherwise call now */
11138             if (genfn) {
11139                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11140             }
11141         }
11142 
11143         for (pass = 0; pass < maxpass; pass++) {
11144             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11145         }
11146         clear_vec_high(s, is_q, rd);
11147     }
11148 }
11149 
11150 /* Floating point op subgroup of C3.6.16. */
11151 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11152 {
11153     /* For floating point ops, the U, size[1] and opcode bits
11154      * together indicate the operation. size[0] indicates single
11155      * or double.
11156      */
11157     int fpopcode = extract32(insn, 11, 5)
11158         | (extract32(insn, 23, 1) << 5)
11159         | (extract32(insn, 29, 1) << 6);
11160     int is_q = extract32(insn, 30, 1);
11161     int size = extract32(insn, 22, 1);
11162     int rm = extract32(insn, 16, 5);
11163     int rn = extract32(insn, 5, 5);
11164     int rd = extract32(insn, 0, 5);
11165 
11166     int datasize = is_q ? 128 : 64;
11167     int esize = 32 << size;
11168     int elements = datasize / esize;
11169 
11170     if (size == 1 && !is_q) {
11171         unallocated_encoding(s);
11172         return;
11173     }
11174 
11175     switch (fpopcode) {
11176     case 0x58: /* FMAXNMP */
11177     case 0x5a: /* FADDP */
11178     case 0x5e: /* FMAXP */
11179     case 0x78: /* FMINNMP */
11180     case 0x7e: /* FMINP */
11181         if (size && !is_q) {
11182             unallocated_encoding(s);
11183             return;
11184         }
11185         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11186                                rn, rm, rd);
11187         return;
11188     case 0x1b: /* FMULX */
11189     case 0x1f: /* FRECPS */
11190     case 0x3f: /* FRSQRTS */
11191     case 0x5d: /* FACGE */
11192     case 0x7d: /* FACGT */
11193     case 0x19: /* FMLA */
11194     case 0x39: /* FMLS */
11195     case 0x18: /* FMAXNM */
11196     case 0x1a: /* FADD */
11197     case 0x1c: /* FCMEQ */
11198     case 0x1e: /* FMAX */
11199     case 0x38: /* FMINNM */
11200     case 0x3a: /* FSUB */
11201     case 0x3e: /* FMIN */
11202     case 0x5b: /* FMUL */
11203     case 0x5c: /* FCMGE */
11204     case 0x5f: /* FDIV */
11205     case 0x7a: /* FABD */
11206     case 0x7c: /* FCMGT */
11207         if (!fp_access_check(s)) {
11208             return;
11209         }
11210         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11211         return;
11212 
11213     case 0x1d: /* FMLAL  */
11214     case 0x3d: /* FMLSL  */
11215     case 0x59: /* FMLAL2 */
11216     case 0x79: /* FMLSL2 */
11217         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11218             unallocated_encoding(s);
11219             return;
11220         }
11221         if (fp_access_check(s)) {
11222             int is_s = extract32(insn, 23, 1);
11223             int is_2 = extract32(insn, 29, 1);
11224             int data = (is_2 << 1) | is_s;
11225             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11226                                vec_full_reg_offset(s, rn),
11227                                vec_full_reg_offset(s, rm), cpu_env,
11228                                is_q ? 16 : 8, vec_full_reg_size(s),
11229                                data, gen_helper_gvec_fmlal_a64);
11230         }
11231         return;
11232 
11233     default:
11234         unallocated_encoding(s);
11235         return;
11236     }
11237 }
11238 
11239 /* Integer op subgroup of C3.6.16. */
11240 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11241 {
11242     int is_q = extract32(insn, 30, 1);
11243     int u = extract32(insn, 29, 1);
11244     int size = extract32(insn, 22, 2);
11245     int opcode = extract32(insn, 11, 5);
11246     int rm = extract32(insn, 16, 5);
11247     int rn = extract32(insn, 5, 5);
11248     int rd = extract32(insn, 0, 5);
11249     int pass;
11250     TCGCond cond;
11251 
11252     switch (opcode) {
11253     case 0x13: /* MUL, PMUL */
11254         if (u && size != 0) {
11255             unallocated_encoding(s);
11256             return;
11257         }
11258         /* fall through */
11259     case 0x0: /* SHADD, UHADD */
11260     case 0x2: /* SRHADD, URHADD */
11261     case 0x4: /* SHSUB, UHSUB */
11262     case 0xc: /* SMAX, UMAX */
11263     case 0xd: /* SMIN, UMIN */
11264     case 0xe: /* SABD, UABD */
11265     case 0xf: /* SABA, UABA */
11266     case 0x12: /* MLA, MLS */
11267         if (size == 3) {
11268             unallocated_encoding(s);
11269             return;
11270         }
11271         break;
11272     case 0x16: /* SQDMULH, SQRDMULH */
11273         if (size == 0 || size == 3) {
11274             unallocated_encoding(s);
11275             return;
11276         }
11277         break;
11278     default:
11279         if (size == 3 && !is_q) {
11280             unallocated_encoding(s);
11281             return;
11282         }
11283         break;
11284     }
11285 
11286     if (!fp_access_check(s)) {
11287         return;
11288     }
11289 
11290     switch (opcode) {
11291     case 0x01: /* SQADD, UQADD */
11292         if (u) {
11293             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11294         } else {
11295             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11296         }
11297         return;
11298     case 0x05: /* SQSUB, UQSUB */
11299         if (u) {
11300             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11301         } else {
11302             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11303         }
11304         return;
11305     case 0x08: /* SSHL, USHL */
11306         if (u) {
11307             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11308         } else {
11309             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11310         }
11311         return;
11312     case 0x0c: /* SMAX, UMAX */
11313         if (u) {
11314             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11315         } else {
11316             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11317         }
11318         return;
11319     case 0x0d: /* SMIN, UMIN */
11320         if (u) {
11321             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11322         } else {
11323             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11324         }
11325         return;
11326     case 0xe: /* SABD, UABD */
11327         if (u) {
11328             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11329         } else {
11330             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11331         }
11332         return;
11333     case 0xf: /* SABA, UABA */
11334         if (u) {
11335             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11336         } else {
11337             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11338         }
11339         return;
11340     case 0x10: /* ADD, SUB */
11341         if (u) {
11342             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11343         } else {
11344             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11345         }
11346         return;
11347     case 0x13: /* MUL, PMUL */
11348         if (!u) { /* MUL */
11349             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11350         } else {  /* PMUL */
11351             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11352         }
11353         return;
11354     case 0x12: /* MLA, MLS */
11355         if (u) {
11356             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11357         } else {
11358             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11359         }
11360         return;
11361     case 0x16: /* SQDMULH, SQRDMULH */
11362         {
11363             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11364                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11365                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11366             };
11367             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11368         }
11369         return;
11370     case 0x11:
11371         if (!u) { /* CMTST */
11372             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11373             return;
11374         }
11375         /* else CMEQ */
11376         cond = TCG_COND_EQ;
11377         goto do_gvec_cmp;
11378     case 0x06: /* CMGT, CMHI */
11379         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11380         goto do_gvec_cmp;
11381     case 0x07: /* CMGE, CMHS */
11382         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11383     do_gvec_cmp:
11384         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11385                          vec_full_reg_offset(s, rn),
11386                          vec_full_reg_offset(s, rm),
11387                          is_q ? 16 : 8, vec_full_reg_size(s));
11388         return;
11389     }
11390 
11391     if (size == 3) {
11392         assert(is_q);
11393         for (pass = 0; pass < 2; pass++) {
11394             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11395             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11396             TCGv_i64 tcg_res = tcg_temp_new_i64();
11397 
11398             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11399             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11400 
11401             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11402 
11403             write_vec_element(s, tcg_res, rd, pass, MO_64);
11404         }
11405     } else {
11406         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11407             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11408             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11409             TCGv_i32 tcg_res = tcg_temp_new_i32();
11410             NeonGenTwoOpFn *genfn = NULL;
11411             NeonGenTwoOpEnvFn *genenvfn = NULL;
11412 
11413             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11414             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11415 
11416             switch (opcode) {
11417             case 0x0: /* SHADD, UHADD */
11418             {
11419                 static NeonGenTwoOpFn * const fns[3][2] = {
11420                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11421                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11422                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11423                 };
11424                 genfn = fns[size][u];
11425                 break;
11426             }
11427             case 0x2: /* SRHADD, URHADD */
11428             {
11429                 static NeonGenTwoOpFn * const fns[3][2] = {
11430                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11431                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11432                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11433                 };
11434                 genfn = fns[size][u];
11435                 break;
11436             }
11437             case 0x4: /* SHSUB, UHSUB */
11438             {
11439                 static NeonGenTwoOpFn * const fns[3][2] = {
11440                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11441                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11442                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11443                 };
11444                 genfn = fns[size][u];
11445                 break;
11446             }
11447             case 0x9: /* SQSHL, UQSHL */
11448             {
11449                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11450                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11451                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11452                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11453                 };
11454                 genenvfn = fns[size][u];
11455                 break;
11456             }
11457             case 0xa: /* SRSHL, URSHL */
11458             {
11459                 static NeonGenTwoOpFn * const fns[3][2] = {
11460                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11461                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11462                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11463                 };
11464                 genfn = fns[size][u];
11465                 break;
11466             }
11467             case 0xb: /* SQRSHL, UQRSHL */
11468             {
11469                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11470                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11471                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11472                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11473                 };
11474                 genenvfn = fns[size][u];
11475                 break;
11476             }
11477             default:
11478                 g_assert_not_reached();
11479             }
11480 
11481             if (genenvfn) {
11482                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11483             } else {
11484                 genfn(tcg_res, tcg_op1, tcg_op2);
11485             }
11486 
11487             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11488         }
11489     }
11490     clear_vec_high(s, is_q, rd);
11491 }
11492 
11493 /* AdvSIMD three same
11494  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11495  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11496  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11497  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11498  */
11499 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11500 {
11501     int opcode = extract32(insn, 11, 5);
11502 
11503     switch (opcode) {
11504     case 0x3: /* logic ops */
11505         disas_simd_3same_logic(s, insn);
11506         break;
11507     case 0x17: /* ADDP */
11508     case 0x14: /* SMAXP, UMAXP */
11509     case 0x15: /* SMINP, UMINP */
11510     {
11511         /* Pairwise operations */
11512         int is_q = extract32(insn, 30, 1);
11513         int u = extract32(insn, 29, 1);
11514         int size = extract32(insn, 22, 2);
11515         int rm = extract32(insn, 16, 5);
11516         int rn = extract32(insn, 5, 5);
11517         int rd = extract32(insn, 0, 5);
11518         if (opcode == 0x17) {
11519             if (u || (size == 3 && !is_q)) {
11520                 unallocated_encoding(s);
11521                 return;
11522             }
11523         } else {
11524             if (size == 3) {
11525                 unallocated_encoding(s);
11526                 return;
11527             }
11528         }
11529         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11530         break;
11531     }
11532     case 0x18 ... 0x31:
11533         /* floating point ops, sz[1] and U are part of opcode */
11534         disas_simd_3same_float(s, insn);
11535         break;
11536     default:
11537         disas_simd_3same_int(s, insn);
11538         break;
11539     }
11540 }
11541 
11542 /*
11543  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11544  *
11545  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11546  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11547  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11548  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11549  *
11550  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11551  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11552  *
11553  */
11554 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11555 {
11556     int opcode = extract32(insn, 11, 3);
11557     int u = extract32(insn, 29, 1);
11558     int a = extract32(insn, 23, 1);
11559     int is_q = extract32(insn, 30, 1);
11560     int rm = extract32(insn, 16, 5);
11561     int rn = extract32(insn, 5, 5);
11562     int rd = extract32(insn, 0, 5);
11563     /*
11564      * For these floating point ops, the U, a and opcode bits
11565      * together indicate the operation.
11566      */
11567     int fpopcode = opcode | (a << 3) | (u << 4);
11568     int datasize = is_q ? 128 : 64;
11569     int elements = datasize / 16;
11570     bool pairwise;
11571     TCGv_ptr fpst;
11572     int pass;
11573 
11574     switch (fpopcode) {
11575     case 0x0: /* FMAXNM */
11576     case 0x1: /* FMLA */
11577     case 0x2: /* FADD */
11578     case 0x3: /* FMULX */
11579     case 0x4: /* FCMEQ */
11580     case 0x6: /* FMAX */
11581     case 0x7: /* FRECPS */
11582     case 0x8: /* FMINNM */
11583     case 0x9: /* FMLS */
11584     case 0xa: /* FSUB */
11585     case 0xe: /* FMIN */
11586     case 0xf: /* FRSQRTS */
11587     case 0x13: /* FMUL */
11588     case 0x14: /* FCMGE */
11589     case 0x15: /* FACGE */
11590     case 0x17: /* FDIV */
11591     case 0x1a: /* FABD */
11592     case 0x1c: /* FCMGT */
11593     case 0x1d: /* FACGT */
11594         pairwise = false;
11595         break;
11596     case 0x10: /* FMAXNMP */
11597     case 0x12: /* FADDP */
11598     case 0x16: /* FMAXP */
11599     case 0x18: /* FMINNMP */
11600     case 0x1e: /* FMINP */
11601         pairwise = true;
11602         break;
11603     default:
11604         unallocated_encoding(s);
11605         return;
11606     }
11607 
11608     if (!dc_isar_feature(aa64_fp16, s)) {
11609         unallocated_encoding(s);
11610         return;
11611     }
11612 
11613     if (!fp_access_check(s)) {
11614         return;
11615     }
11616 
11617     fpst = fpstatus_ptr(FPST_FPCR_F16);
11618 
11619     if (pairwise) {
11620         int maxpass = is_q ? 8 : 4;
11621         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11622         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11623         TCGv_i32 tcg_res[8];
11624 
11625         for (pass = 0; pass < maxpass; pass++) {
11626             int passreg = pass < (maxpass / 2) ? rn : rm;
11627             int passelt = (pass << 1) & (maxpass - 1);
11628 
11629             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11630             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11631             tcg_res[pass] = tcg_temp_new_i32();
11632 
11633             switch (fpopcode) {
11634             case 0x10: /* FMAXNMP */
11635                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11636                                            fpst);
11637                 break;
11638             case 0x12: /* FADDP */
11639                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11640                 break;
11641             case 0x16: /* FMAXP */
11642                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11643                 break;
11644             case 0x18: /* FMINNMP */
11645                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11646                                            fpst);
11647                 break;
11648             case 0x1e: /* FMINP */
11649                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11650                 break;
11651             default:
11652                 g_assert_not_reached();
11653             }
11654         }
11655 
11656         for (pass = 0; pass < maxpass; pass++) {
11657             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11658         }
11659     } else {
11660         for (pass = 0; pass < elements; pass++) {
11661             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11662             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11663             TCGv_i32 tcg_res = tcg_temp_new_i32();
11664 
11665             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11666             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11667 
11668             switch (fpopcode) {
11669             case 0x0: /* FMAXNM */
11670                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11671                 break;
11672             case 0x1: /* FMLA */
11673                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11674                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11675                                            fpst);
11676                 break;
11677             case 0x2: /* FADD */
11678                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11679                 break;
11680             case 0x3: /* FMULX */
11681                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11682                 break;
11683             case 0x4: /* FCMEQ */
11684                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11685                 break;
11686             case 0x6: /* FMAX */
11687                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11688                 break;
11689             case 0x7: /* FRECPS */
11690                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11691                 break;
11692             case 0x8: /* FMINNM */
11693                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11694                 break;
11695             case 0x9: /* FMLS */
11696                 /* As usual for ARM, separate negation for fused multiply-add */
11697                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11698                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11699                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11700                                            fpst);
11701                 break;
11702             case 0xa: /* FSUB */
11703                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11704                 break;
11705             case 0xe: /* FMIN */
11706                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11707                 break;
11708             case 0xf: /* FRSQRTS */
11709                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11710                 break;
11711             case 0x13: /* FMUL */
11712                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11713                 break;
11714             case 0x14: /* FCMGE */
11715                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11716                 break;
11717             case 0x15: /* FACGE */
11718                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11719                 break;
11720             case 0x17: /* FDIV */
11721                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11722                 break;
11723             case 0x1a: /* FABD */
11724                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11725                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11726                 break;
11727             case 0x1c: /* FCMGT */
11728                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11729                 break;
11730             case 0x1d: /* FACGT */
11731                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11732                 break;
11733             default:
11734                 g_assert_not_reached();
11735             }
11736 
11737             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11738         }
11739     }
11740 
11741     clear_vec_high(s, is_q, rd);
11742 }
11743 
11744 /* AdvSIMD three same extra
11745  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11746  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11747  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11748  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11749  */
11750 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11751 {
11752     int rd = extract32(insn, 0, 5);
11753     int rn = extract32(insn, 5, 5);
11754     int opcode = extract32(insn, 11, 4);
11755     int rm = extract32(insn, 16, 5);
11756     int size = extract32(insn, 22, 2);
11757     bool u = extract32(insn, 29, 1);
11758     bool is_q = extract32(insn, 30, 1);
11759     bool feature;
11760     int rot;
11761 
11762     switch (u * 16 + opcode) {
11763     case 0x10: /* SQRDMLAH (vector) */
11764     case 0x11: /* SQRDMLSH (vector) */
11765         if (size != 1 && size != 2) {
11766             unallocated_encoding(s);
11767             return;
11768         }
11769         feature = dc_isar_feature(aa64_rdm, s);
11770         break;
11771     case 0x02: /* SDOT (vector) */
11772     case 0x12: /* UDOT (vector) */
11773         if (size != MO_32) {
11774             unallocated_encoding(s);
11775             return;
11776         }
11777         feature = dc_isar_feature(aa64_dp, s);
11778         break;
11779     case 0x03: /* USDOT */
11780         if (size != MO_32) {
11781             unallocated_encoding(s);
11782             return;
11783         }
11784         feature = dc_isar_feature(aa64_i8mm, s);
11785         break;
11786     case 0x04: /* SMMLA */
11787     case 0x14: /* UMMLA */
11788     case 0x05: /* USMMLA */
11789         if (!is_q || size != MO_32) {
11790             unallocated_encoding(s);
11791             return;
11792         }
11793         feature = dc_isar_feature(aa64_i8mm, s);
11794         break;
11795     case 0x18: /* FCMLA, #0 */
11796     case 0x19: /* FCMLA, #90 */
11797     case 0x1a: /* FCMLA, #180 */
11798     case 0x1b: /* FCMLA, #270 */
11799     case 0x1c: /* FCADD, #90 */
11800     case 0x1e: /* FCADD, #270 */
11801         if (size == 0
11802             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11803             || (size == 3 && !is_q)) {
11804             unallocated_encoding(s);
11805             return;
11806         }
11807         feature = dc_isar_feature(aa64_fcma, s);
11808         break;
11809     case 0x1d: /* BFMMLA */
11810         if (size != MO_16 || !is_q) {
11811             unallocated_encoding(s);
11812             return;
11813         }
11814         feature = dc_isar_feature(aa64_bf16, s);
11815         break;
11816     case 0x1f:
11817         switch (size) {
11818         case 1: /* BFDOT */
11819         case 3: /* BFMLAL{B,T} */
11820             feature = dc_isar_feature(aa64_bf16, s);
11821             break;
11822         default:
11823             unallocated_encoding(s);
11824             return;
11825         }
11826         break;
11827     default:
11828         unallocated_encoding(s);
11829         return;
11830     }
11831     if (!feature) {
11832         unallocated_encoding(s);
11833         return;
11834     }
11835     if (!fp_access_check(s)) {
11836         return;
11837     }
11838 
11839     switch (opcode) {
11840     case 0x0: /* SQRDMLAH (vector) */
11841         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11842         return;
11843 
11844     case 0x1: /* SQRDMLSH (vector) */
11845         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11846         return;
11847 
11848     case 0x2: /* SDOT / UDOT */
11849         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11850                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11851         return;
11852 
11853     case 0x3: /* USDOT */
11854         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11855         return;
11856 
11857     case 0x04: /* SMMLA, UMMLA */
11858         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11859                          u ? gen_helper_gvec_ummla_b
11860                          : gen_helper_gvec_smmla_b);
11861         return;
11862     case 0x05: /* USMMLA */
11863         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11864         return;
11865 
11866     case 0x8: /* FCMLA, #0 */
11867     case 0x9: /* FCMLA, #90 */
11868     case 0xa: /* FCMLA, #180 */
11869     case 0xb: /* FCMLA, #270 */
11870         rot = extract32(opcode, 0, 2);
11871         switch (size) {
11872         case 1:
11873             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11874                               gen_helper_gvec_fcmlah);
11875             break;
11876         case 2:
11877             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11878                               gen_helper_gvec_fcmlas);
11879             break;
11880         case 3:
11881             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11882                               gen_helper_gvec_fcmlad);
11883             break;
11884         default:
11885             g_assert_not_reached();
11886         }
11887         return;
11888 
11889     case 0xc: /* FCADD, #90 */
11890     case 0xe: /* FCADD, #270 */
11891         rot = extract32(opcode, 1, 1);
11892         switch (size) {
11893         case 1:
11894             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11895                               gen_helper_gvec_fcaddh);
11896             break;
11897         case 2:
11898             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11899                               gen_helper_gvec_fcadds);
11900             break;
11901         case 3:
11902             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11903                               gen_helper_gvec_fcaddd);
11904             break;
11905         default:
11906             g_assert_not_reached();
11907         }
11908         return;
11909 
11910     case 0xd: /* BFMMLA */
11911         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11912         return;
11913     case 0xf:
11914         switch (size) {
11915         case 1: /* BFDOT */
11916             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11917             break;
11918         case 3: /* BFMLAL{B,T} */
11919             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11920                               gen_helper_gvec_bfmlal);
11921             break;
11922         default:
11923             g_assert_not_reached();
11924         }
11925         return;
11926 
11927     default:
11928         g_assert_not_reached();
11929     }
11930 }
11931 
11932 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11933                                   int size, int rn, int rd)
11934 {
11935     /* Handle 2-reg-misc ops which are widening (so each size element
11936      * in the source becomes a 2*size element in the destination.
11937      * The only instruction like this is FCVTL.
11938      */
11939     int pass;
11940 
11941     if (size == 3) {
11942         /* 32 -> 64 bit fp conversion */
11943         TCGv_i64 tcg_res[2];
11944         int srcelt = is_q ? 2 : 0;
11945 
11946         for (pass = 0; pass < 2; pass++) {
11947             TCGv_i32 tcg_op = tcg_temp_new_i32();
11948             tcg_res[pass] = tcg_temp_new_i64();
11949 
11950             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11951             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11952         }
11953         for (pass = 0; pass < 2; pass++) {
11954             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11955         }
11956     } else {
11957         /* 16 -> 32 bit fp conversion */
11958         int srcelt = is_q ? 4 : 0;
11959         TCGv_i32 tcg_res[4];
11960         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11961         TCGv_i32 ahp = get_ahp_flag();
11962 
11963         for (pass = 0; pass < 4; pass++) {
11964             tcg_res[pass] = tcg_temp_new_i32();
11965 
11966             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11967             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11968                                            fpst, ahp);
11969         }
11970         for (pass = 0; pass < 4; pass++) {
11971             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11972         }
11973     }
11974 }
11975 
11976 static void handle_rev(DisasContext *s, int opcode, bool u,
11977                        bool is_q, int size, int rn, int rd)
11978 {
11979     int op = (opcode << 1) | u;
11980     int opsz = op + size;
11981     int grp_size = 3 - opsz;
11982     int dsize = is_q ? 128 : 64;
11983     int i;
11984 
11985     if (opsz >= 3) {
11986         unallocated_encoding(s);
11987         return;
11988     }
11989 
11990     if (!fp_access_check(s)) {
11991         return;
11992     }
11993 
11994     if (size == 0) {
11995         /* Special case bytes, use bswap op on each group of elements */
11996         int groups = dsize / (8 << grp_size);
11997 
11998         for (i = 0; i < groups; i++) {
11999             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12000 
12001             read_vec_element(s, tcg_tmp, rn, i, grp_size);
12002             switch (grp_size) {
12003             case MO_16:
12004                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12005                 break;
12006             case MO_32:
12007                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12008                 break;
12009             case MO_64:
12010                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12011                 break;
12012             default:
12013                 g_assert_not_reached();
12014             }
12015             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12016         }
12017         clear_vec_high(s, is_q, rd);
12018     } else {
12019         int revmask = (1 << grp_size) - 1;
12020         int esize = 8 << size;
12021         int elements = dsize / esize;
12022         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12023         TCGv_i64 tcg_rd[2];
12024 
12025         for (i = 0; i < 2; i++) {
12026             tcg_rd[i] = tcg_temp_new_i64();
12027             tcg_gen_movi_i64(tcg_rd[i], 0);
12028         }
12029 
12030         for (i = 0; i < elements; i++) {
12031             int e_rev = (i & 0xf) ^ revmask;
12032             int w = (e_rev * esize) / 64;
12033             int o = (e_rev * esize) % 64;
12034 
12035             read_vec_element(s, tcg_rn, rn, i, size);
12036             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12037         }
12038 
12039         for (i = 0; i < 2; i++) {
12040             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12041         }
12042         clear_vec_high(s, true, rd);
12043     }
12044 }
12045 
12046 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12047                                   bool is_q, int size, int rn, int rd)
12048 {
12049     /* Implement the pairwise operations from 2-misc:
12050      * SADDLP, UADDLP, SADALP, UADALP.
12051      * These all add pairs of elements in the input to produce a
12052      * double-width result element in the output (possibly accumulating).
12053      */
12054     bool accum = (opcode == 0x6);
12055     int maxpass = is_q ? 2 : 1;
12056     int pass;
12057     TCGv_i64 tcg_res[2];
12058 
12059     if (size == 2) {
12060         /* 32 + 32 -> 64 op */
12061         MemOp memop = size + (u ? 0 : MO_SIGN);
12062 
12063         for (pass = 0; pass < maxpass; pass++) {
12064             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12065             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12066 
12067             tcg_res[pass] = tcg_temp_new_i64();
12068 
12069             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12070             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12071             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12072             if (accum) {
12073                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12074                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12075             }
12076         }
12077     } else {
12078         for (pass = 0; pass < maxpass; pass++) {
12079             TCGv_i64 tcg_op = tcg_temp_new_i64();
12080             NeonGenOne64OpFn *genfn;
12081             static NeonGenOne64OpFn * const fns[2][2] = {
12082                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12083                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12084             };
12085 
12086             genfn = fns[size][u];
12087 
12088             tcg_res[pass] = tcg_temp_new_i64();
12089 
12090             read_vec_element(s, tcg_op, rn, pass, MO_64);
12091             genfn(tcg_res[pass], tcg_op);
12092 
12093             if (accum) {
12094                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12095                 if (size == 0) {
12096                     gen_helper_neon_addl_u16(tcg_res[pass],
12097                                              tcg_res[pass], tcg_op);
12098                 } else {
12099                     gen_helper_neon_addl_u32(tcg_res[pass],
12100                                              tcg_res[pass], tcg_op);
12101                 }
12102             }
12103         }
12104     }
12105     if (!is_q) {
12106         tcg_res[1] = tcg_constant_i64(0);
12107     }
12108     for (pass = 0; pass < 2; pass++) {
12109         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12110     }
12111 }
12112 
12113 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12114 {
12115     /* Implement SHLL and SHLL2 */
12116     int pass;
12117     int part = is_q ? 2 : 0;
12118     TCGv_i64 tcg_res[2];
12119 
12120     for (pass = 0; pass < 2; pass++) {
12121         static NeonGenWidenFn * const widenfns[3] = {
12122             gen_helper_neon_widen_u8,
12123             gen_helper_neon_widen_u16,
12124             tcg_gen_extu_i32_i64,
12125         };
12126         NeonGenWidenFn *widenfn = widenfns[size];
12127         TCGv_i32 tcg_op = tcg_temp_new_i32();
12128 
12129         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12130         tcg_res[pass] = tcg_temp_new_i64();
12131         widenfn(tcg_res[pass], tcg_op);
12132         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12133     }
12134 
12135     for (pass = 0; pass < 2; pass++) {
12136         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12137     }
12138 }
12139 
12140 /* AdvSIMD two reg misc
12141  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12142  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12143  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12144  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12145  */
12146 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12147 {
12148     int size = extract32(insn, 22, 2);
12149     int opcode = extract32(insn, 12, 5);
12150     bool u = extract32(insn, 29, 1);
12151     bool is_q = extract32(insn, 30, 1);
12152     int rn = extract32(insn, 5, 5);
12153     int rd = extract32(insn, 0, 5);
12154     bool need_fpstatus = false;
12155     int rmode = -1;
12156     TCGv_i32 tcg_rmode;
12157     TCGv_ptr tcg_fpstatus;
12158 
12159     switch (opcode) {
12160     case 0x0: /* REV64, REV32 */
12161     case 0x1: /* REV16 */
12162         handle_rev(s, opcode, u, is_q, size, rn, rd);
12163         return;
12164     case 0x5: /* CNT, NOT, RBIT */
12165         if (u && size == 0) {
12166             /* NOT */
12167             break;
12168         } else if (u && size == 1) {
12169             /* RBIT */
12170             break;
12171         } else if (!u && size == 0) {
12172             /* CNT */
12173             break;
12174         }
12175         unallocated_encoding(s);
12176         return;
12177     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12178     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12179         if (size == 3) {
12180             unallocated_encoding(s);
12181             return;
12182         }
12183         if (!fp_access_check(s)) {
12184             return;
12185         }
12186 
12187         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12188         return;
12189     case 0x4: /* CLS, CLZ */
12190         if (size == 3) {
12191             unallocated_encoding(s);
12192             return;
12193         }
12194         break;
12195     case 0x2: /* SADDLP, UADDLP */
12196     case 0x6: /* SADALP, UADALP */
12197         if (size == 3) {
12198             unallocated_encoding(s);
12199             return;
12200         }
12201         if (!fp_access_check(s)) {
12202             return;
12203         }
12204         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12205         return;
12206     case 0x13: /* SHLL, SHLL2 */
12207         if (u == 0 || size == 3) {
12208             unallocated_encoding(s);
12209             return;
12210         }
12211         if (!fp_access_check(s)) {
12212             return;
12213         }
12214         handle_shll(s, is_q, size, rn, rd);
12215         return;
12216     case 0xa: /* CMLT */
12217         if (u == 1) {
12218             unallocated_encoding(s);
12219             return;
12220         }
12221         /* fall through */
12222     case 0x8: /* CMGT, CMGE */
12223     case 0x9: /* CMEQ, CMLE */
12224     case 0xb: /* ABS, NEG */
12225         if (size == 3 && !is_q) {
12226             unallocated_encoding(s);
12227             return;
12228         }
12229         break;
12230     case 0x3: /* SUQADD, USQADD */
12231         if (size == 3 && !is_q) {
12232             unallocated_encoding(s);
12233             return;
12234         }
12235         if (!fp_access_check(s)) {
12236             return;
12237         }
12238         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12239         return;
12240     case 0x7: /* SQABS, SQNEG */
12241         if (size == 3 && !is_q) {
12242             unallocated_encoding(s);
12243             return;
12244         }
12245         break;
12246     case 0xc ... 0xf:
12247     case 0x16 ... 0x1f:
12248     {
12249         /* Floating point: U, size[1] and opcode indicate operation;
12250          * size[0] indicates single or double precision.
12251          */
12252         int is_double = extract32(size, 0, 1);
12253         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12254         size = is_double ? 3 : 2;
12255         switch (opcode) {
12256         case 0x2f: /* FABS */
12257         case 0x6f: /* FNEG */
12258             if (size == 3 && !is_q) {
12259                 unallocated_encoding(s);
12260                 return;
12261             }
12262             break;
12263         case 0x1d: /* SCVTF */
12264         case 0x5d: /* UCVTF */
12265         {
12266             bool is_signed = (opcode == 0x1d) ? true : false;
12267             int elements = is_double ? 2 : is_q ? 4 : 2;
12268             if (is_double && !is_q) {
12269                 unallocated_encoding(s);
12270                 return;
12271             }
12272             if (!fp_access_check(s)) {
12273                 return;
12274             }
12275             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12276             return;
12277         }
12278         case 0x2c: /* FCMGT (zero) */
12279         case 0x2d: /* FCMEQ (zero) */
12280         case 0x2e: /* FCMLT (zero) */
12281         case 0x6c: /* FCMGE (zero) */
12282         case 0x6d: /* FCMLE (zero) */
12283             if (size == 3 && !is_q) {
12284                 unallocated_encoding(s);
12285                 return;
12286             }
12287             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12288             return;
12289         case 0x7f: /* FSQRT */
12290             if (size == 3 && !is_q) {
12291                 unallocated_encoding(s);
12292                 return;
12293             }
12294             break;
12295         case 0x1a: /* FCVTNS */
12296         case 0x1b: /* FCVTMS */
12297         case 0x3a: /* FCVTPS */
12298         case 0x3b: /* FCVTZS */
12299         case 0x5a: /* FCVTNU */
12300         case 0x5b: /* FCVTMU */
12301         case 0x7a: /* FCVTPU */
12302         case 0x7b: /* FCVTZU */
12303             need_fpstatus = true;
12304             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12305             if (size == 3 && !is_q) {
12306                 unallocated_encoding(s);
12307                 return;
12308             }
12309             break;
12310         case 0x5c: /* FCVTAU */
12311         case 0x1c: /* FCVTAS */
12312             need_fpstatus = true;
12313             rmode = FPROUNDING_TIEAWAY;
12314             if (size == 3 && !is_q) {
12315                 unallocated_encoding(s);
12316                 return;
12317             }
12318             break;
12319         case 0x3c: /* URECPE */
12320             if (size == 3) {
12321                 unallocated_encoding(s);
12322                 return;
12323             }
12324             /* fall through */
12325         case 0x3d: /* FRECPE */
12326         case 0x7d: /* FRSQRTE */
12327             if (size == 3 && !is_q) {
12328                 unallocated_encoding(s);
12329                 return;
12330             }
12331             if (!fp_access_check(s)) {
12332                 return;
12333             }
12334             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12335             return;
12336         case 0x56: /* FCVTXN, FCVTXN2 */
12337             if (size == 2) {
12338                 unallocated_encoding(s);
12339                 return;
12340             }
12341             /* fall through */
12342         case 0x16: /* FCVTN, FCVTN2 */
12343             /* handle_2misc_narrow does a 2*size -> size operation, but these
12344              * instructions encode the source size rather than dest size.
12345              */
12346             if (!fp_access_check(s)) {
12347                 return;
12348             }
12349             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12350             return;
12351         case 0x36: /* BFCVTN, BFCVTN2 */
12352             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12353                 unallocated_encoding(s);
12354                 return;
12355             }
12356             if (!fp_access_check(s)) {
12357                 return;
12358             }
12359             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12360             return;
12361         case 0x17: /* FCVTL, FCVTL2 */
12362             if (!fp_access_check(s)) {
12363                 return;
12364             }
12365             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12366             return;
12367         case 0x18: /* FRINTN */
12368         case 0x19: /* FRINTM */
12369         case 0x38: /* FRINTP */
12370         case 0x39: /* FRINTZ */
12371             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12372             /* fall through */
12373         case 0x59: /* FRINTX */
12374         case 0x79: /* FRINTI */
12375             need_fpstatus = true;
12376             if (size == 3 && !is_q) {
12377                 unallocated_encoding(s);
12378                 return;
12379             }
12380             break;
12381         case 0x58: /* FRINTA */
12382             rmode = FPROUNDING_TIEAWAY;
12383             need_fpstatus = true;
12384             if (size == 3 && !is_q) {
12385                 unallocated_encoding(s);
12386                 return;
12387             }
12388             break;
12389         case 0x7c: /* URSQRTE */
12390             if (size == 3) {
12391                 unallocated_encoding(s);
12392                 return;
12393             }
12394             break;
12395         case 0x1e: /* FRINT32Z */
12396         case 0x1f: /* FRINT64Z */
12397             rmode = FPROUNDING_ZERO;
12398             /* fall through */
12399         case 0x5e: /* FRINT32X */
12400         case 0x5f: /* FRINT64X */
12401             need_fpstatus = true;
12402             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12403                 unallocated_encoding(s);
12404                 return;
12405             }
12406             break;
12407         default:
12408             unallocated_encoding(s);
12409             return;
12410         }
12411         break;
12412     }
12413     default:
12414         unallocated_encoding(s);
12415         return;
12416     }
12417 
12418     if (!fp_access_check(s)) {
12419         return;
12420     }
12421 
12422     if (need_fpstatus || rmode >= 0) {
12423         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12424     } else {
12425         tcg_fpstatus = NULL;
12426     }
12427     if (rmode >= 0) {
12428         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12429     } else {
12430         tcg_rmode = NULL;
12431     }
12432 
12433     switch (opcode) {
12434     case 0x5:
12435         if (u && size == 0) { /* NOT */
12436             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12437             return;
12438         }
12439         break;
12440     case 0x8: /* CMGT, CMGE */
12441         if (u) {
12442             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12443         } else {
12444             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12445         }
12446         return;
12447     case 0x9: /* CMEQ, CMLE */
12448         if (u) {
12449             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12450         } else {
12451             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12452         }
12453         return;
12454     case 0xa: /* CMLT */
12455         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12456         return;
12457     case 0xb:
12458         if (u) { /* ABS, NEG */
12459             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12460         } else {
12461             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12462         }
12463         return;
12464     }
12465 
12466     if (size == 3) {
12467         /* All 64-bit element operations can be shared with scalar 2misc */
12468         int pass;
12469 
12470         /* Coverity claims (size == 3 && !is_q) has been eliminated
12471          * from all paths leading to here.
12472          */
12473         tcg_debug_assert(is_q);
12474         for (pass = 0; pass < 2; pass++) {
12475             TCGv_i64 tcg_op = tcg_temp_new_i64();
12476             TCGv_i64 tcg_res = tcg_temp_new_i64();
12477 
12478             read_vec_element(s, tcg_op, rn, pass, MO_64);
12479 
12480             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12481                             tcg_rmode, tcg_fpstatus);
12482 
12483             write_vec_element(s, tcg_res, rd, pass, MO_64);
12484         }
12485     } else {
12486         int pass;
12487 
12488         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12489             TCGv_i32 tcg_op = tcg_temp_new_i32();
12490             TCGv_i32 tcg_res = tcg_temp_new_i32();
12491 
12492             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12493 
12494             if (size == 2) {
12495                 /* Special cases for 32 bit elements */
12496                 switch (opcode) {
12497                 case 0x4: /* CLS */
12498                     if (u) {
12499                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12500                     } else {
12501                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12502                     }
12503                     break;
12504                 case 0x7: /* SQABS, SQNEG */
12505                     if (u) {
12506                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12507                     } else {
12508                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12509                     }
12510                     break;
12511                 case 0x2f: /* FABS */
12512                     gen_helper_vfp_abss(tcg_res, tcg_op);
12513                     break;
12514                 case 0x6f: /* FNEG */
12515                     gen_helper_vfp_negs(tcg_res, tcg_op);
12516                     break;
12517                 case 0x7f: /* FSQRT */
12518                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12519                     break;
12520                 case 0x1a: /* FCVTNS */
12521                 case 0x1b: /* FCVTMS */
12522                 case 0x1c: /* FCVTAS */
12523                 case 0x3a: /* FCVTPS */
12524                 case 0x3b: /* FCVTZS */
12525                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12526                                          tcg_constant_i32(0), tcg_fpstatus);
12527                     break;
12528                 case 0x5a: /* FCVTNU */
12529                 case 0x5b: /* FCVTMU */
12530                 case 0x5c: /* FCVTAU */
12531                 case 0x7a: /* FCVTPU */
12532                 case 0x7b: /* FCVTZU */
12533                     gen_helper_vfp_touls(tcg_res, tcg_op,
12534                                          tcg_constant_i32(0), tcg_fpstatus);
12535                     break;
12536                 case 0x18: /* FRINTN */
12537                 case 0x19: /* FRINTM */
12538                 case 0x38: /* FRINTP */
12539                 case 0x39: /* FRINTZ */
12540                 case 0x58: /* FRINTA */
12541                 case 0x79: /* FRINTI */
12542                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12543                     break;
12544                 case 0x59: /* FRINTX */
12545                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12546                     break;
12547                 case 0x7c: /* URSQRTE */
12548                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12549                     break;
12550                 case 0x1e: /* FRINT32Z */
12551                 case 0x5e: /* FRINT32X */
12552                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12553                     break;
12554                 case 0x1f: /* FRINT64Z */
12555                 case 0x5f: /* FRINT64X */
12556                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12557                     break;
12558                 default:
12559                     g_assert_not_reached();
12560                 }
12561             } else {
12562                 /* Use helpers for 8 and 16 bit elements */
12563                 switch (opcode) {
12564                 case 0x5: /* CNT, RBIT */
12565                     /* For these two insns size is part of the opcode specifier
12566                      * (handled earlier); they always operate on byte elements.
12567                      */
12568                     if (u) {
12569                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12570                     } else {
12571                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12572                     }
12573                     break;
12574                 case 0x7: /* SQABS, SQNEG */
12575                 {
12576                     NeonGenOneOpEnvFn *genfn;
12577                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12578                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12579                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12580                     };
12581                     genfn = fns[size][u];
12582                     genfn(tcg_res, cpu_env, tcg_op);
12583                     break;
12584                 }
12585                 case 0x4: /* CLS, CLZ */
12586                     if (u) {
12587                         if (size == 0) {
12588                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12589                         } else {
12590                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12591                         }
12592                     } else {
12593                         if (size == 0) {
12594                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12595                         } else {
12596                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12597                         }
12598                     }
12599                     break;
12600                 default:
12601                     g_assert_not_reached();
12602                 }
12603             }
12604 
12605             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12606         }
12607     }
12608     clear_vec_high(s, is_q, rd);
12609 
12610     if (tcg_rmode) {
12611         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12612     }
12613 }
12614 
12615 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12616  *
12617  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12618  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12619  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12620  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12621  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12622  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12623  *
12624  * This actually covers two groups where scalar access is governed by
12625  * bit 28. A bunch of the instructions (float to integral) only exist
12626  * in the vector form and are un-allocated for the scalar decode. Also
12627  * in the scalar decode Q is always 1.
12628  */
12629 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12630 {
12631     int fpop, opcode, a, u;
12632     int rn, rd;
12633     bool is_q;
12634     bool is_scalar;
12635     bool only_in_vector = false;
12636 
12637     int pass;
12638     TCGv_i32 tcg_rmode = NULL;
12639     TCGv_ptr tcg_fpstatus = NULL;
12640     bool need_fpst = true;
12641     int rmode = -1;
12642 
12643     if (!dc_isar_feature(aa64_fp16, s)) {
12644         unallocated_encoding(s);
12645         return;
12646     }
12647 
12648     rd = extract32(insn, 0, 5);
12649     rn = extract32(insn, 5, 5);
12650 
12651     a = extract32(insn, 23, 1);
12652     u = extract32(insn, 29, 1);
12653     is_scalar = extract32(insn, 28, 1);
12654     is_q = extract32(insn, 30, 1);
12655 
12656     opcode = extract32(insn, 12, 5);
12657     fpop = deposit32(opcode, 5, 1, a);
12658     fpop = deposit32(fpop, 6, 1, u);
12659 
12660     switch (fpop) {
12661     case 0x1d: /* SCVTF */
12662     case 0x5d: /* UCVTF */
12663     {
12664         int elements;
12665 
12666         if (is_scalar) {
12667             elements = 1;
12668         } else {
12669             elements = (is_q ? 8 : 4);
12670         }
12671 
12672         if (!fp_access_check(s)) {
12673             return;
12674         }
12675         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12676         return;
12677     }
12678     break;
12679     case 0x2c: /* FCMGT (zero) */
12680     case 0x2d: /* FCMEQ (zero) */
12681     case 0x2e: /* FCMLT (zero) */
12682     case 0x6c: /* FCMGE (zero) */
12683     case 0x6d: /* FCMLE (zero) */
12684         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12685         return;
12686     case 0x3d: /* FRECPE */
12687     case 0x3f: /* FRECPX */
12688         break;
12689     case 0x18: /* FRINTN */
12690         only_in_vector = true;
12691         rmode = FPROUNDING_TIEEVEN;
12692         break;
12693     case 0x19: /* FRINTM */
12694         only_in_vector = true;
12695         rmode = FPROUNDING_NEGINF;
12696         break;
12697     case 0x38: /* FRINTP */
12698         only_in_vector = true;
12699         rmode = FPROUNDING_POSINF;
12700         break;
12701     case 0x39: /* FRINTZ */
12702         only_in_vector = true;
12703         rmode = FPROUNDING_ZERO;
12704         break;
12705     case 0x58: /* FRINTA */
12706         only_in_vector = true;
12707         rmode = FPROUNDING_TIEAWAY;
12708         break;
12709     case 0x59: /* FRINTX */
12710     case 0x79: /* FRINTI */
12711         only_in_vector = true;
12712         /* current rounding mode */
12713         break;
12714     case 0x1a: /* FCVTNS */
12715         rmode = FPROUNDING_TIEEVEN;
12716         break;
12717     case 0x1b: /* FCVTMS */
12718         rmode = FPROUNDING_NEGINF;
12719         break;
12720     case 0x1c: /* FCVTAS */
12721         rmode = FPROUNDING_TIEAWAY;
12722         break;
12723     case 0x3a: /* FCVTPS */
12724         rmode = FPROUNDING_POSINF;
12725         break;
12726     case 0x3b: /* FCVTZS */
12727         rmode = FPROUNDING_ZERO;
12728         break;
12729     case 0x5a: /* FCVTNU */
12730         rmode = FPROUNDING_TIEEVEN;
12731         break;
12732     case 0x5b: /* FCVTMU */
12733         rmode = FPROUNDING_NEGINF;
12734         break;
12735     case 0x5c: /* FCVTAU */
12736         rmode = FPROUNDING_TIEAWAY;
12737         break;
12738     case 0x7a: /* FCVTPU */
12739         rmode = FPROUNDING_POSINF;
12740         break;
12741     case 0x7b: /* FCVTZU */
12742         rmode = FPROUNDING_ZERO;
12743         break;
12744     case 0x2f: /* FABS */
12745     case 0x6f: /* FNEG */
12746         need_fpst = false;
12747         break;
12748     case 0x7d: /* FRSQRTE */
12749     case 0x7f: /* FSQRT (vector) */
12750         break;
12751     default:
12752         unallocated_encoding(s);
12753         return;
12754     }
12755 
12756 
12757     /* Check additional constraints for the scalar encoding */
12758     if (is_scalar) {
12759         if (!is_q) {
12760             unallocated_encoding(s);
12761             return;
12762         }
12763         /* FRINTxx is only in the vector form */
12764         if (only_in_vector) {
12765             unallocated_encoding(s);
12766             return;
12767         }
12768     }
12769 
12770     if (!fp_access_check(s)) {
12771         return;
12772     }
12773 
12774     if (rmode >= 0 || need_fpst) {
12775         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12776     }
12777 
12778     if (rmode >= 0) {
12779         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12780     }
12781 
12782     if (is_scalar) {
12783         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12784         TCGv_i32 tcg_res = tcg_temp_new_i32();
12785 
12786         switch (fpop) {
12787         case 0x1a: /* FCVTNS */
12788         case 0x1b: /* FCVTMS */
12789         case 0x1c: /* FCVTAS */
12790         case 0x3a: /* FCVTPS */
12791         case 0x3b: /* FCVTZS */
12792             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12793             break;
12794         case 0x3d: /* FRECPE */
12795             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12796             break;
12797         case 0x3f: /* FRECPX */
12798             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12799             break;
12800         case 0x5a: /* FCVTNU */
12801         case 0x5b: /* FCVTMU */
12802         case 0x5c: /* FCVTAU */
12803         case 0x7a: /* FCVTPU */
12804         case 0x7b: /* FCVTZU */
12805             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12806             break;
12807         case 0x6f: /* FNEG */
12808             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12809             break;
12810         case 0x7d: /* FRSQRTE */
12811             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12812             break;
12813         default:
12814             g_assert_not_reached();
12815         }
12816 
12817         /* limit any sign extension going on */
12818         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12819         write_fp_sreg(s, rd, tcg_res);
12820     } else {
12821         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12822             TCGv_i32 tcg_op = tcg_temp_new_i32();
12823             TCGv_i32 tcg_res = tcg_temp_new_i32();
12824 
12825             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12826 
12827             switch (fpop) {
12828             case 0x1a: /* FCVTNS */
12829             case 0x1b: /* FCVTMS */
12830             case 0x1c: /* FCVTAS */
12831             case 0x3a: /* FCVTPS */
12832             case 0x3b: /* FCVTZS */
12833                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12834                 break;
12835             case 0x3d: /* FRECPE */
12836                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12837                 break;
12838             case 0x5a: /* FCVTNU */
12839             case 0x5b: /* FCVTMU */
12840             case 0x5c: /* FCVTAU */
12841             case 0x7a: /* FCVTPU */
12842             case 0x7b: /* FCVTZU */
12843                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12844                 break;
12845             case 0x18: /* FRINTN */
12846             case 0x19: /* FRINTM */
12847             case 0x38: /* FRINTP */
12848             case 0x39: /* FRINTZ */
12849             case 0x58: /* FRINTA */
12850             case 0x79: /* FRINTI */
12851                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12852                 break;
12853             case 0x59: /* FRINTX */
12854                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12855                 break;
12856             case 0x2f: /* FABS */
12857                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12858                 break;
12859             case 0x6f: /* FNEG */
12860                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12861                 break;
12862             case 0x7d: /* FRSQRTE */
12863                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12864                 break;
12865             case 0x7f: /* FSQRT */
12866                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12867                 break;
12868             default:
12869                 g_assert_not_reached();
12870             }
12871 
12872             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12873         }
12874 
12875         clear_vec_high(s, is_q, rd);
12876     }
12877 
12878     if (tcg_rmode) {
12879         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12880     }
12881 }
12882 
12883 /* AdvSIMD scalar x indexed element
12884  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12885  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12886  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12887  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12888  * AdvSIMD vector x indexed element
12889  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12890  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12891  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12892  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12893  */
12894 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12895 {
12896     /* This encoding has two kinds of instruction:
12897      *  normal, where we perform elt x idxelt => elt for each
12898      *     element in the vector
12899      *  long, where we perform elt x idxelt and generate a result of
12900      *     double the width of the input element
12901      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12902      */
12903     bool is_scalar = extract32(insn, 28, 1);
12904     bool is_q = extract32(insn, 30, 1);
12905     bool u = extract32(insn, 29, 1);
12906     int size = extract32(insn, 22, 2);
12907     int l = extract32(insn, 21, 1);
12908     int m = extract32(insn, 20, 1);
12909     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12910     int rm = extract32(insn, 16, 4);
12911     int opcode = extract32(insn, 12, 4);
12912     int h = extract32(insn, 11, 1);
12913     int rn = extract32(insn, 5, 5);
12914     int rd = extract32(insn, 0, 5);
12915     bool is_long = false;
12916     int is_fp = 0;
12917     bool is_fp16 = false;
12918     int index;
12919     TCGv_ptr fpst;
12920 
12921     switch (16 * u + opcode) {
12922     case 0x08: /* MUL */
12923     case 0x10: /* MLA */
12924     case 0x14: /* MLS */
12925         if (is_scalar) {
12926             unallocated_encoding(s);
12927             return;
12928         }
12929         break;
12930     case 0x02: /* SMLAL, SMLAL2 */
12931     case 0x12: /* UMLAL, UMLAL2 */
12932     case 0x06: /* SMLSL, SMLSL2 */
12933     case 0x16: /* UMLSL, UMLSL2 */
12934     case 0x0a: /* SMULL, SMULL2 */
12935     case 0x1a: /* UMULL, UMULL2 */
12936         if (is_scalar) {
12937             unallocated_encoding(s);
12938             return;
12939         }
12940         is_long = true;
12941         break;
12942     case 0x03: /* SQDMLAL, SQDMLAL2 */
12943     case 0x07: /* SQDMLSL, SQDMLSL2 */
12944     case 0x0b: /* SQDMULL, SQDMULL2 */
12945         is_long = true;
12946         break;
12947     case 0x0c: /* SQDMULH */
12948     case 0x0d: /* SQRDMULH */
12949         break;
12950     case 0x01: /* FMLA */
12951     case 0x05: /* FMLS */
12952     case 0x09: /* FMUL */
12953     case 0x19: /* FMULX */
12954         is_fp = 1;
12955         break;
12956     case 0x1d: /* SQRDMLAH */
12957     case 0x1f: /* SQRDMLSH */
12958         if (!dc_isar_feature(aa64_rdm, s)) {
12959             unallocated_encoding(s);
12960             return;
12961         }
12962         break;
12963     case 0x0e: /* SDOT */
12964     case 0x1e: /* UDOT */
12965         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12966             unallocated_encoding(s);
12967             return;
12968         }
12969         break;
12970     case 0x0f:
12971         switch (size) {
12972         case 0: /* SUDOT */
12973         case 2: /* USDOT */
12974             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12975                 unallocated_encoding(s);
12976                 return;
12977             }
12978             size = MO_32;
12979             break;
12980         case 1: /* BFDOT */
12981             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12982                 unallocated_encoding(s);
12983                 return;
12984             }
12985             size = MO_32;
12986             break;
12987         case 3: /* BFMLAL{B,T} */
12988             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12989                 unallocated_encoding(s);
12990                 return;
12991             }
12992             /* can't set is_fp without other incorrect size checks */
12993             size = MO_16;
12994             break;
12995         default:
12996             unallocated_encoding(s);
12997             return;
12998         }
12999         break;
13000     case 0x11: /* FCMLA #0 */
13001     case 0x13: /* FCMLA #90 */
13002     case 0x15: /* FCMLA #180 */
13003     case 0x17: /* FCMLA #270 */
13004         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13005             unallocated_encoding(s);
13006             return;
13007         }
13008         is_fp = 2;
13009         break;
13010     case 0x00: /* FMLAL */
13011     case 0x04: /* FMLSL */
13012     case 0x18: /* FMLAL2 */
13013     case 0x1c: /* FMLSL2 */
13014         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13015             unallocated_encoding(s);
13016             return;
13017         }
13018         size = MO_16;
13019         /* is_fp, but we pass cpu_env not fp_status.  */
13020         break;
13021     default:
13022         unallocated_encoding(s);
13023         return;
13024     }
13025 
13026     switch (is_fp) {
13027     case 1: /* normal fp */
13028         /* convert insn encoded size to MemOp size */
13029         switch (size) {
13030         case 0: /* half-precision */
13031             size = MO_16;
13032             is_fp16 = true;
13033             break;
13034         case MO_32: /* single precision */
13035         case MO_64: /* double precision */
13036             break;
13037         default:
13038             unallocated_encoding(s);
13039             return;
13040         }
13041         break;
13042 
13043     case 2: /* complex fp */
13044         /* Each indexable element is a complex pair.  */
13045         size += 1;
13046         switch (size) {
13047         case MO_32:
13048             if (h && !is_q) {
13049                 unallocated_encoding(s);
13050                 return;
13051             }
13052             is_fp16 = true;
13053             break;
13054         case MO_64:
13055             break;
13056         default:
13057             unallocated_encoding(s);
13058             return;
13059         }
13060         break;
13061 
13062     default: /* integer */
13063         switch (size) {
13064         case MO_8:
13065         case MO_64:
13066             unallocated_encoding(s);
13067             return;
13068         }
13069         break;
13070     }
13071     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13072         unallocated_encoding(s);
13073         return;
13074     }
13075 
13076     /* Given MemOp size, adjust register and indexing.  */
13077     switch (size) {
13078     case MO_16:
13079         index = h << 2 | l << 1 | m;
13080         break;
13081     case MO_32:
13082         index = h << 1 | l;
13083         rm |= m << 4;
13084         break;
13085     case MO_64:
13086         if (l || !is_q) {
13087             unallocated_encoding(s);
13088             return;
13089         }
13090         index = h;
13091         rm |= m << 4;
13092         break;
13093     default:
13094         g_assert_not_reached();
13095     }
13096 
13097     if (!fp_access_check(s)) {
13098         return;
13099     }
13100 
13101     if (is_fp) {
13102         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13103     } else {
13104         fpst = NULL;
13105     }
13106 
13107     switch (16 * u + opcode) {
13108     case 0x0e: /* SDOT */
13109     case 0x1e: /* UDOT */
13110         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13111                          u ? gen_helper_gvec_udot_idx_b
13112                          : gen_helper_gvec_sdot_idx_b);
13113         return;
13114     case 0x0f:
13115         switch (extract32(insn, 22, 2)) {
13116         case 0: /* SUDOT */
13117             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13118                              gen_helper_gvec_sudot_idx_b);
13119             return;
13120         case 1: /* BFDOT */
13121             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13122                              gen_helper_gvec_bfdot_idx);
13123             return;
13124         case 2: /* USDOT */
13125             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13126                              gen_helper_gvec_usdot_idx_b);
13127             return;
13128         case 3: /* BFMLAL{B,T} */
13129             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13130                               gen_helper_gvec_bfmlal_idx);
13131             return;
13132         }
13133         g_assert_not_reached();
13134     case 0x11: /* FCMLA #0 */
13135     case 0x13: /* FCMLA #90 */
13136     case 0x15: /* FCMLA #180 */
13137     case 0x17: /* FCMLA #270 */
13138         {
13139             int rot = extract32(insn, 13, 2);
13140             int data = (index << 2) | rot;
13141             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13142                                vec_full_reg_offset(s, rn),
13143                                vec_full_reg_offset(s, rm),
13144                                vec_full_reg_offset(s, rd), fpst,
13145                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13146                                size == MO_64
13147                                ? gen_helper_gvec_fcmlas_idx
13148                                : gen_helper_gvec_fcmlah_idx);
13149         }
13150         return;
13151 
13152     case 0x00: /* FMLAL */
13153     case 0x04: /* FMLSL */
13154     case 0x18: /* FMLAL2 */
13155     case 0x1c: /* FMLSL2 */
13156         {
13157             int is_s = extract32(opcode, 2, 1);
13158             int is_2 = u;
13159             int data = (index << 2) | (is_2 << 1) | is_s;
13160             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13161                                vec_full_reg_offset(s, rn),
13162                                vec_full_reg_offset(s, rm), cpu_env,
13163                                is_q ? 16 : 8, vec_full_reg_size(s),
13164                                data, gen_helper_gvec_fmlal_idx_a64);
13165         }
13166         return;
13167 
13168     case 0x08: /* MUL */
13169         if (!is_long && !is_scalar) {
13170             static gen_helper_gvec_3 * const fns[3] = {
13171                 gen_helper_gvec_mul_idx_h,
13172                 gen_helper_gvec_mul_idx_s,
13173                 gen_helper_gvec_mul_idx_d,
13174             };
13175             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13176                                vec_full_reg_offset(s, rn),
13177                                vec_full_reg_offset(s, rm),
13178                                is_q ? 16 : 8, vec_full_reg_size(s),
13179                                index, fns[size - 1]);
13180             return;
13181         }
13182         break;
13183 
13184     case 0x10: /* MLA */
13185         if (!is_long && !is_scalar) {
13186             static gen_helper_gvec_4 * const fns[3] = {
13187                 gen_helper_gvec_mla_idx_h,
13188                 gen_helper_gvec_mla_idx_s,
13189                 gen_helper_gvec_mla_idx_d,
13190             };
13191             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13192                                vec_full_reg_offset(s, rn),
13193                                vec_full_reg_offset(s, rm),
13194                                vec_full_reg_offset(s, rd),
13195                                is_q ? 16 : 8, vec_full_reg_size(s),
13196                                index, fns[size - 1]);
13197             return;
13198         }
13199         break;
13200 
13201     case 0x14: /* MLS */
13202         if (!is_long && !is_scalar) {
13203             static gen_helper_gvec_4 * const fns[3] = {
13204                 gen_helper_gvec_mls_idx_h,
13205                 gen_helper_gvec_mls_idx_s,
13206                 gen_helper_gvec_mls_idx_d,
13207             };
13208             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13209                                vec_full_reg_offset(s, rn),
13210                                vec_full_reg_offset(s, rm),
13211                                vec_full_reg_offset(s, rd),
13212                                is_q ? 16 : 8, vec_full_reg_size(s),
13213                                index, fns[size - 1]);
13214             return;
13215         }
13216         break;
13217     }
13218 
13219     if (size == 3) {
13220         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13221         int pass;
13222 
13223         assert(is_fp && is_q && !is_long);
13224 
13225         read_vec_element(s, tcg_idx, rm, index, MO_64);
13226 
13227         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13228             TCGv_i64 tcg_op = tcg_temp_new_i64();
13229             TCGv_i64 tcg_res = tcg_temp_new_i64();
13230 
13231             read_vec_element(s, tcg_op, rn, pass, MO_64);
13232 
13233             switch (16 * u + opcode) {
13234             case 0x05: /* FMLS */
13235                 /* As usual for ARM, separate negation for fused multiply-add */
13236                 gen_helper_vfp_negd(tcg_op, tcg_op);
13237                 /* fall through */
13238             case 0x01: /* FMLA */
13239                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13240                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13241                 break;
13242             case 0x09: /* FMUL */
13243                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13244                 break;
13245             case 0x19: /* FMULX */
13246                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13247                 break;
13248             default:
13249                 g_assert_not_reached();
13250             }
13251 
13252             write_vec_element(s, tcg_res, rd, pass, MO_64);
13253         }
13254 
13255         clear_vec_high(s, !is_scalar, rd);
13256     } else if (!is_long) {
13257         /* 32 bit floating point, or 16 or 32 bit integer.
13258          * For the 16 bit scalar case we use the usual Neon helpers and
13259          * rely on the fact that 0 op 0 == 0 with no side effects.
13260          */
13261         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13262         int pass, maxpasses;
13263 
13264         if (is_scalar) {
13265             maxpasses = 1;
13266         } else {
13267             maxpasses = is_q ? 4 : 2;
13268         }
13269 
13270         read_vec_element_i32(s, tcg_idx, rm, index, size);
13271 
13272         if (size == 1 && !is_scalar) {
13273             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13274              * the index into both halves of the 32 bit tcg_idx and then use
13275              * the usual Neon helpers.
13276              */
13277             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13278         }
13279 
13280         for (pass = 0; pass < maxpasses; pass++) {
13281             TCGv_i32 tcg_op = tcg_temp_new_i32();
13282             TCGv_i32 tcg_res = tcg_temp_new_i32();
13283 
13284             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13285 
13286             switch (16 * u + opcode) {
13287             case 0x08: /* MUL */
13288             case 0x10: /* MLA */
13289             case 0x14: /* MLS */
13290             {
13291                 static NeonGenTwoOpFn * const fns[2][2] = {
13292                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13293                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13294                 };
13295                 NeonGenTwoOpFn *genfn;
13296                 bool is_sub = opcode == 0x4;
13297 
13298                 if (size == 1) {
13299                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13300                 } else {
13301                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13302                 }
13303                 if (opcode == 0x8) {
13304                     break;
13305                 }
13306                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13307                 genfn = fns[size - 1][is_sub];
13308                 genfn(tcg_res, tcg_op, tcg_res);
13309                 break;
13310             }
13311             case 0x05: /* FMLS */
13312             case 0x01: /* FMLA */
13313                 read_vec_element_i32(s, tcg_res, rd, pass,
13314                                      is_scalar ? size : MO_32);
13315                 switch (size) {
13316                 case 1:
13317                     if (opcode == 0x5) {
13318                         /* As usual for ARM, separate negation for fused
13319                          * multiply-add */
13320                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13321                     }
13322                     if (is_scalar) {
13323                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13324                                                    tcg_res, fpst);
13325                     } else {
13326                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13327                                                     tcg_res, fpst);
13328                     }
13329                     break;
13330                 case 2:
13331                     if (opcode == 0x5) {
13332                         /* As usual for ARM, separate negation for
13333                          * fused multiply-add */
13334                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13335                     }
13336                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13337                                            tcg_res, fpst);
13338                     break;
13339                 default:
13340                     g_assert_not_reached();
13341                 }
13342                 break;
13343             case 0x09: /* FMUL */
13344                 switch (size) {
13345                 case 1:
13346                     if (is_scalar) {
13347                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13348                                                 tcg_idx, fpst);
13349                     } else {
13350                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13351                                                  tcg_idx, fpst);
13352                     }
13353                     break;
13354                 case 2:
13355                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13356                     break;
13357                 default:
13358                     g_assert_not_reached();
13359                 }
13360                 break;
13361             case 0x19: /* FMULX */
13362                 switch (size) {
13363                 case 1:
13364                     if (is_scalar) {
13365                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13366                                                  tcg_idx, fpst);
13367                     } else {
13368                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13369                                                   tcg_idx, fpst);
13370                     }
13371                     break;
13372                 case 2:
13373                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13374                     break;
13375                 default:
13376                     g_assert_not_reached();
13377                 }
13378                 break;
13379             case 0x0c: /* SQDMULH */
13380                 if (size == 1) {
13381                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13382                                                tcg_op, tcg_idx);
13383                 } else {
13384                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13385                                                tcg_op, tcg_idx);
13386                 }
13387                 break;
13388             case 0x0d: /* SQRDMULH */
13389                 if (size == 1) {
13390                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13391                                                 tcg_op, tcg_idx);
13392                 } else {
13393                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13394                                                 tcg_op, tcg_idx);
13395                 }
13396                 break;
13397             case 0x1d: /* SQRDMLAH */
13398                 read_vec_element_i32(s, tcg_res, rd, pass,
13399                                      is_scalar ? size : MO_32);
13400                 if (size == 1) {
13401                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13402                                                 tcg_op, tcg_idx, tcg_res);
13403                 } else {
13404                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13405                                                 tcg_op, tcg_idx, tcg_res);
13406                 }
13407                 break;
13408             case 0x1f: /* SQRDMLSH */
13409                 read_vec_element_i32(s, tcg_res, rd, pass,
13410                                      is_scalar ? size : MO_32);
13411                 if (size == 1) {
13412                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13413                                                 tcg_op, tcg_idx, tcg_res);
13414                 } else {
13415                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13416                                                 tcg_op, tcg_idx, tcg_res);
13417                 }
13418                 break;
13419             default:
13420                 g_assert_not_reached();
13421             }
13422 
13423             if (is_scalar) {
13424                 write_fp_sreg(s, rd, tcg_res);
13425             } else {
13426                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13427             }
13428         }
13429 
13430         clear_vec_high(s, is_q, rd);
13431     } else {
13432         /* long ops: 16x16->32 or 32x32->64 */
13433         TCGv_i64 tcg_res[2];
13434         int pass;
13435         bool satop = extract32(opcode, 0, 1);
13436         MemOp memop = MO_32;
13437 
13438         if (satop || !u) {
13439             memop |= MO_SIGN;
13440         }
13441 
13442         if (size == 2) {
13443             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13444 
13445             read_vec_element(s, tcg_idx, rm, index, memop);
13446 
13447             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13448                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13449                 TCGv_i64 tcg_passres;
13450                 int passelt;
13451 
13452                 if (is_scalar) {
13453                     passelt = 0;
13454                 } else {
13455                     passelt = pass + (is_q * 2);
13456                 }
13457 
13458                 read_vec_element(s, tcg_op, rn, passelt, memop);
13459 
13460                 tcg_res[pass] = tcg_temp_new_i64();
13461 
13462                 if (opcode == 0xa || opcode == 0xb) {
13463                     /* Non-accumulating ops */
13464                     tcg_passres = tcg_res[pass];
13465                 } else {
13466                     tcg_passres = tcg_temp_new_i64();
13467                 }
13468 
13469                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13470 
13471                 if (satop) {
13472                     /* saturating, doubling */
13473                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13474                                                       tcg_passres, tcg_passres);
13475                 }
13476 
13477                 if (opcode == 0xa || opcode == 0xb) {
13478                     continue;
13479                 }
13480 
13481                 /* Accumulating op: handle accumulate step */
13482                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13483 
13484                 switch (opcode) {
13485                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13486                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13487                     break;
13488                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13489                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13490                     break;
13491                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13492                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13493                     /* fall through */
13494                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13495                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13496                                                       tcg_res[pass],
13497                                                       tcg_passres);
13498                     break;
13499                 default:
13500                     g_assert_not_reached();
13501                 }
13502             }
13503 
13504             clear_vec_high(s, !is_scalar, rd);
13505         } else {
13506             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13507 
13508             assert(size == 1);
13509             read_vec_element_i32(s, tcg_idx, rm, index, size);
13510 
13511             if (!is_scalar) {
13512                 /* The simplest way to handle the 16x16 indexed ops is to
13513                  * duplicate the index into both halves of the 32 bit tcg_idx
13514                  * and then use the usual Neon helpers.
13515                  */
13516                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13517             }
13518 
13519             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13520                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13521                 TCGv_i64 tcg_passres;
13522 
13523                 if (is_scalar) {
13524                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13525                 } else {
13526                     read_vec_element_i32(s, tcg_op, rn,
13527                                          pass + (is_q * 2), MO_32);
13528                 }
13529 
13530                 tcg_res[pass] = tcg_temp_new_i64();
13531 
13532                 if (opcode == 0xa || opcode == 0xb) {
13533                     /* Non-accumulating ops */
13534                     tcg_passres = tcg_res[pass];
13535                 } else {
13536                     tcg_passres = tcg_temp_new_i64();
13537                 }
13538 
13539                 if (memop & MO_SIGN) {
13540                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13541                 } else {
13542                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13543                 }
13544                 if (satop) {
13545                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13546                                                       tcg_passres, tcg_passres);
13547                 }
13548 
13549                 if (opcode == 0xa || opcode == 0xb) {
13550                     continue;
13551                 }
13552 
13553                 /* Accumulating op: handle accumulate step */
13554                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13555 
13556                 switch (opcode) {
13557                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13558                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13559                                              tcg_passres);
13560                     break;
13561                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13562                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13563                                              tcg_passres);
13564                     break;
13565                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13566                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13567                     /* fall through */
13568                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13569                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13570                                                       tcg_res[pass],
13571                                                       tcg_passres);
13572                     break;
13573                 default:
13574                     g_assert_not_reached();
13575                 }
13576             }
13577 
13578             if (is_scalar) {
13579                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13580             }
13581         }
13582 
13583         if (is_scalar) {
13584             tcg_res[1] = tcg_constant_i64(0);
13585         }
13586 
13587         for (pass = 0; pass < 2; pass++) {
13588             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13589         }
13590     }
13591 }
13592 
13593 /* Crypto AES
13594  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13595  * +-----------------+------+-----------+--------+-----+------+------+
13596  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13597  * +-----------------+------+-----------+--------+-----+------+------+
13598  */
13599 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13600 {
13601     int size = extract32(insn, 22, 2);
13602     int opcode = extract32(insn, 12, 5);
13603     int rn = extract32(insn, 5, 5);
13604     int rd = extract32(insn, 0, 5);
13605     int decrypt;
13606     gen_helper_gvec_2 *genfn2 = NULL;
13607     gen_helper_gvec_3 *genfn3 = NULL;
13608 
13609     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13610         unallocated_encoding(s);
13611         return;
13612     }
13613 
13614     switch (opcode) {
13615     case 0x4: /* AESE */
13616         decrypt = 0;
13617         genfn3 = gen_helper_crypto_aese;
13618         break;
13619     case 0x6: /* AESMC */
13620         decrypt = 0;
13621         genfn2 = gen_helper_crypto_aesmc;
13622         break;
13623     case 0x5: /* AESD */
13624         decrypt = 1;
13625         genfn3 = gen_helper_crypto_aese;
13626         break;
13627     case 0x7: /* AESIMC */
13628         decrypt = 1;
13629         genfn2 = gen_helper_crypto_aesmc;
13630         break;
13631     default:
13632         unallocated_encoding(s);
13633         return;
13634     }
13635 
13636     if (!fp_access_check(s)) {
13637         return;
13638     }
13639     if (genfn2) {
13640         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13641     } else {
13642         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13643     }
13644 }
13645 
13646 /* Crypto three-reg SHA
13647  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13648  * +-----------------+------+---+------+---+--------+-----+------+------+
13649  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13650  * +-----------------+------+---+------+---+--------+-----+------+------+
13651  */
13652 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13653 {
13654     int size = extract32(insn, 22, 2);
13655     int opcode = extract32(insn, 12, 3);
13656     int rm = extract32(insn, 16, 5);
13657     int rn = extract32(insn, 5, 5);
13658     int rd = extract32(insn, 0, 5);
13659     gen_helper_gvec_3 *genfn;
13660     bool feature;
13661 
13662     if (size != 0) {
13663         unallocated_encoding(s);
13664         return;
13665     }
13666 
13667     switch (opcode) {
13668     case 0: /* SHA1C */
13669         genfn = gen_helper_crypto_sha1c;
13670         feature = dc_isar_feature(aa64_sha1, s);
13671         break;
13672     case 1: /* SHA1P */
13673         genfn = gen_helper_crypto_sha1p;
13674         feature = dc_isar_feature(aa64_sha1, s);
13675         break;
13676     case 2: /* SHA1M */
13677         genfn = gen_helper_crypto_sha1m;
13678         feature = dc_isar_feature(aa64_sha1, s);
13679         break;
13680     case 3: /* SHA1SU0 */
13681         genfn = gen_helper_crypto_sha1su0;
13682         feature = dc_isar_feature(aa64_sha1, s);
13683         break;
13684     case 4: /* SHA256H */
13685         genfn = gen_helper_crypto_sha256h;
13686         feature = dc_isar_feature(aa64_sha256, s);
13687         break;
13688     case 5: /* SHA256H2 */
13689         genfn = gen_helper_crypto_sha256h2;
13690         feature = dc_isar_feature(aa64_sha256, s);
13691         break;
13692     case 6: /* SHA256SU1 */
13693         genfn = gen_helper_crypto_sha256su1;
13694         feature = dc_isar_feature(aa64_sha256, s);
13695         break;
13696     default:
13697         unallocated_encoding(s);
13698         return;
13699     }
13700 
13701     if (!feature) {
13702         unallocated_encoding(s);
13703         return;
13704     }
13705 
13706     if (!fp_access_check(s)) {
13707         return;
13708     }
13709     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13710 }
13711 
13712 /* Crypto two-reg SHA
13713  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13714  * +-----------------+------+-----------+--------+-----+------+------+
13715  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13716  * +-----------------+------+-----------+--------+-----+------+------+
13717  */
13718 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13719 {
13720     int size = extract32(insn, 22, 2);
13721     int opcode = extract32(insn, 12, 5);
13722     int rn = extract32(insn, 5, 5);
13723     int rd = extract32(insn, 0, 5);
13724     gen_helper_gvec_2 *genfn;
13725     bool feature;
13726 
13727     if (size != 0) {
13728         unallocated_encoding(s);
13729         return;
13730     }
13731 
13732     switch (opcode) {
13733     case 0: /* SHA1H */
13734         feature = dc_isar_feature(aa64_sha1, s);
13735         genfn = gen_helper_crypto_sha1h;
13736         break;
13737     case 1: /* SHA1SU1 */
13738         feature = dc_isar_feature(aa64_sha1, s);
13739         genfn = gen_helper_crypto_sha1su1;
13740         break;
13741     case 2: /* SHA256SU0 */
13742         feature = dc_isar_feature(aa64_sha256, s);
13743         genfn = gen_helper_crypto_sha256su0;
13744         break;
13745     default:
13746         unallocated_encoding(s);
13747         return;
13748     }
13749 
13750     if (!feature) {
13751         unallocated_encoding(s);
13752         return;
13753     }
13754 
13755     if (!fp_access_check(s)) {
13756         return;
13757     }
13758     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13759 }
13760 
13761 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13762 {
13763     tcg_gen_rotli_i64(d, m, 1);
13764     tcg_gen_xor_i64(d, d, n);
13765 }
13766 
13767 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13768 {
13769     tcg_gen_rotli_vec(vece, d, m, 1);
13770     tcg_gen_xor_vec(vece, d, d, n);
13771 }
13772 
13773 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13774                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13775 {
13776     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13777     static const GVecGen3 op = {
13778         .fni8 = gen_rax1_i64,
13779         .fniv = gen_rax1_vec,
13780         .opt_opc = vecop_list,
13781         .fno = gen_helper_crypto_rax1,
13782         .vece = MO_64,
13783     };
13784     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13785 }
13786 
13787 /* Crypto three-reg SHA512
13788  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13789  * +-----------------------+------+---+---+-----+--------+------+------+
13790  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13791  * +-----------------------+------+---+---+-----+--------+------+------+
13792  */
13793 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13794 {
13795     int opcode = extract32(insn, 10, 2);
13796     int o =  extract32(insn, 14, 1);
13797     int rm = extract32(insn, 16, 5);
13798     int rn = extract32(insn, 5, 5);
13799     int rd = extract32(insn, 0, 5);
13800     bool feature;
13801     gen_helper_gvec_3 *oolfn = NULL;
13802     GVecGen3Fn *gvecfn = NULL;
13803 
13804     if (o == 0) {
13805         switch (opcode) {
13806         case 0: /* SHA512H */
13807             feature = dc_isar_feature(aa64_sha512, s);
13808             oolfn = gen_helper_crypto_sha512h;
13809             break;
13810         case 1: /* SHA512H2 */
13811             feature = dc_isar_feature(aa64_sha512, s);
13812             oolfn = gen_helper_crypto_sha512h2;
13813             break;
13814         case 2: /* SHA512SU1 */
13815             feature = dc_isar_feature(aa64_sha512, s);
13816             oolfn = gen_helper_crypto_sha512su1;
13817             break;
13818         case 3: /* RAX1 */
13819             feature = dc_isar_feature(aa64_sha3, s);
13820             gvecfn = gen_gvec_rax1;
13821             break;
13822         default:
13823             g_assert_not_reached();
13824         }
13825     } else {
13826         switch (opcode) {
13827         case 0: /* SM3PARTW1 */
13828             feature = dc_isar_feature(aa64_sm3, s);
13829             oolfn = gen_helper_crypto_sm3partw1;
13830             break;
13831         case 1: /* SM3PARTW2 */
13832             feature = dc_isar_feature(aa64_sm3, s);
13833             oolfn = gen_helper_crypto_sm3partw2;
13834             break;
13835         case 2: /* SM4EKEY */
13836             feature = dc_isar_feature(aa64_sm4, s);
13837             oolfn = gen_helper_crypto_sm4ekey;
13838             break;
13839         default:
13840             unallocated_encoding(s);
13841             return;
13842         }
13843     }
13844 
13845     if (!feature) {
13846         unallocated_encoding(s);
13847         return;
13848     }
13849 
13850     if (!fp_access_check(s)) {
13851         return;
13852     }
13853 
13854     if (oolfn) {
13855         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13856     } else {
13857         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13858     }
13859 }
13860 
13861 /* Crypto two-reg SHA512
13862  *  31                                     12  11  10  9    5 4    0
13863  * +-----------------------------------------+--------+------+------+
13864  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13865  * +-----------------------------------------+--------+------+------+
13866  */
13867 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13868 {
13869     int opcode = extract32(insn, 10, 2);
13870     int rn = extract32(insn, 5, 5);
13871     int rd = extract32(insn, 0, 5);
13872     bool feature;
13873 
13874     switch (opcode) {
13875     case 0: /* SHA512SU0 */
13876         feature = dc_isar_feature(aa64_sha512, s);
13877         break;
13878     case 1: /* SM4E */
13879         feature = dc_isar_feature(aa64_sm4, s);
13880         break;
13881     default:
13882         unallocated_encoding(s);
13883         return;
13884     }
13885 
13886     if (!feature) {
13887         unallocated_encoding(s);
13888         return;
13889     }
13890 
13891     if (!fp_access_check(s)) {
13892         return;
13893     }
13894 
13895     switch (opcode) {
13896     case 0: /* SHA512SU0 */
13897         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13898         break;
13899     case 1: /* SM4E */
13900         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13901         break;
13902     default:
13903         g_assert_not_reached();
13904     }
13905 }
13906 
13907 /* Crypto four-register
13908  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13909  * +-------------------+-----+------+---+------+------+------+
13910  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13911  * +-------------------+-----+------+---+------+------+------+
13912  */
13913 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13914 {
13915     int op0 = extract32(insn, 21, 2);
13916     int rm = extract32(insn, 16, 5);
13917     int ra = extract32(insn, 10, 5);
13918     int rn = extract32(insn, 5, 5);
13919     int rd = extract32(insn, 0, 5);
13920     bool feature;
13921 
13922     switch (op0) {
13923     case 0: /* EOR3 */
13924     case 1: /* BCAX */
13925         feature = dc_isar_feature(aa64_sha3, s);
13926         break;
13927     case 2: /* SM3SS1 */
13928         feature = dc_isar_feature(aa64_sm3, s);
13929         break;
13930     default:
13931         unallocated_encoding(s);
13932         return;
13933     }
13934 
13935     if (!feature) {
13936         unallocated_encoding(s);
13937         return;
13938     }
13939 
13940     if (!fp_access_check(s)) {
13941         return;
13942     }
13943 
13944     if (op0 < 2) {
13945         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13946         int pass;
13947 
13948         tcg_op1 = tcg_temp_new_i64();
13949         tcg_op2 = tcg_temp_new_i64();
13950         tcg_op3 = tcg_temp_new_i64();
13951         tcg_res[0] = tcg_temp_new_i64();
13952         tcg_res[1] = tcg_temp_new_i64();
13953 
13954         for (pass = 0; pass < 2; pass++) {
13955             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13956             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13957             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13958 
13959             if (op0 == 0) {
13960                 /* EOR3 */
13961                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13962             } else {
13963                 /* BCAX */
13964                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13965             }
13966             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13967         }
13968         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13969         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13970     } else {
13971         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13972 
13973         tcg_op1 = tcg_temp_new_i32();
13974         tcg_op2 = tcg_temp_new_i32();
13975         tcg_op3 = tcg_temp_new_i32();
13976         tcg_res = tcg_temp_new_i32();
13977         tcg_zero = tcg_constant_i32(0);
13978 
13979         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13980         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13981         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13982 
13983         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13984         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13985         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13986         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13987 
13988         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13989         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13990         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13991         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13992     }
13993 }
13994 
13995 /* Crypto XAR
13996  *  31                   21 20  16 15    10 9    5 4    0
13997  * +-----------------------+------+--------+------+------+
13998  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13999  * +-----------------------+------+--------+------+------+
14000  */
14001 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14002 {
14003     int rm = extract32(insn, 16, 5);
14004     int imm6 = extract32(insn, 10, 6);
14005     int rn = extract32(insn, 5, 5);
14006     int rd = extract32(insn, 0, 5);
14007 
14008     if (!dc_isar_feature(aa64_sha3, s)) {
14009         unallocated_encoding(s);
14010         return;
14011     }
14012 
14013     if (!fp_access_check(s)) {
14014         return;
14015     }
14016 
14017     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14018                  vec_full_reg_offset(s, rn),
14019                  vec_full_reg_offset(s, rm), imm6, 16,
14020                  vec_full_reg_size(s));
14021 }
14022 
14023 /* Crypto three-reg imm2
14024  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14025  * +-----------------------+------+-----+------+--------+------+------+
14026  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14027  * +-----------------------+------+-----+------+--------+------+------+
14028  */
14029 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14030 {
14031     static gen_helper_gvec_3 * const fns[4] = {
14032         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14033         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14034     };
14035     int opcode = extract32(insn, 10, 2);
14036     int imm2 = extract32(insn, 12, 2);
14037     int rm = extract32(insn, 16, 5);
14038     int rn = extract32(insn, 5, 5);
14039     int rd = extract32(insn, 0, 5);
14040 
14041     if (!dc_isar_feature(aa64_sm3, s)) {
14042         unallocated_encoding(s);
14043         return;
14044     }
14045 
14046     if (!fp_access_check(s)) {
14047         return;
14048     }
14049 
14050     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14051 }
14052 
14053 /* C3.6 Data processing - SIMD, inc Crypto
14054  *
14055  * As the decode gets a little complex we are using a table based
14056  * approach for this part of the decode.
14057  */
14058 static const AArch64DecodeTable data_proc_simd[] = {
14059     /* pattern  ,  mask     ,  fn                        */
14060     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14061     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14062     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14063     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14064     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14065     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14066     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14067     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14068     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14069     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14070     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14071     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14072     { 0x2e000000, 0xbf208400, disas_simd_ext },
14073     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14074     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14075     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14076     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14077     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14078     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14079     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14080     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14081     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14082     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14083     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14084     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14085     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14086     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14087     { 0xce800000, 0xffe00000, disas_crypto_xar },
14088     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14089     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14090     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14091     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14092     { 0x00000000, 0x00000000, NULL }
14093 };
14094 
14095 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14096 {
14097     /* Note that this is called with all non-FP cases from
14098      * table C3-6 so it must UNDEF for entries not specifically
14099      * allocated to instructions in that table.
14100      */
14101     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14102     if (fn) {
14103         fn(s, insn);
14104     } else {
14105         unallocated_encoding(s);
14106     }
14107 }
14108 
14109 /* C3.6 Data processing - SIMD and floating point */
14110 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14111 {
14112     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14113         disas_data_proc_fp(s, insn);
14114     } else {
14115         /* SIMD, including crypto */
14116         disas_data_proc_simd(s, insn);
14117     }
14118 }
14119 
14120 static bool trans_OK(DisasContext *s, arg_OK *a)
14121 {
14122     return true;
14123 }
14124 
14125 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14126 {
14127     s->is_nonstreaming = true;
14128     return true;
14129 }
14130 
14131 /**
14132  * is_guarded_page:
14133  * @env: The cpu environment
14134  * @s: The DisasContext
14135  *
14136  * Return true if the page is guarded.
14137  */
14138 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14139 {
14140     uint64_t addr = s->base.pc_first;
14141 #ifdef CONFIG_USER_ONLY
14142     return page_get_flags(addr) & PAGE_BTI;
14143 #else
14144     CPUTLBEntryFull *full;
14145     void *host;
14146     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14147     int flags;
14148 
14149     /*
14150      * We test this immediately after reading an insn, which means
14151      * that the TLB entry must be present and valid, and thus this
14152      * access will never raise an exception.
14153      */
14154     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14155                               false, &host, &full, 0);
14156     assert(!(flags & TLB_INVALID_MASK));
14157 
14158     return full->guarded;
14159 #endif
14160 }
14161 
14162 /**
14163  * btype_destination_ok:
14164  * @insn: The instruction at the branch destination
14165  * @bt: SCTLR_ELx.BT
14166  * @btype: PSTATE.BTYPE, and is non-zero
14167  *
14168  * On a guarded page, there are a limited number of insns
14169  * that may be present at the branch target:
14170  *   - branch target identifiers,
14171  *   - paciasp, pacibsp,
14172  *   - BRK insn
14173  *   - HLT insn
14174  * Anything else causes a Branch Target Exception.
14175  *
14176  * Return true if the branch is compatible, false to raise BTITRAP.
14177  */
14178 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14179 {
14180     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14181         /* HINT space */
14182         switch (extract32(insn, 5, 7)) {
14183         case 0b011001: /* PACIASP */
14184         case 0b011011: /* PACIBSP */
14185             /*
14186              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14187              * with btype == 3.  Otherwise all btype are ok.
14188              */
14189             return !bt || btype != 3;
14190         case 0b100000: /* BTI */
14191             /* Not compatible with any btype.  */
14192             return false;
14193         case 0b100010: /* BTI c */
14194             /* Not compatible with btype == 3 */
14195             return btype != 3;
14196         case 0b100100: /* BTI j */
14197             /* Not compatible with btype == 2 */
14198             return btype != 2;
14199         case 0b100110: /* BTI jc */
14200             /* Compatible with any btype.  */
14201             return true;
14202         }
14203     } else {
14204         switch (insn & 0xffe0001fu) {
14205         case 0xd4200000u: /* BRK */
14206         case 0xd4400000u: /* HLT */
14207             /* Give priority to the breakpoint exception.  */
14208             return true;
14209         }
14210     }
14211     return false;
14212 }
14213 
14214 /* C3.1 A64 instruction index by encoding */
14215 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14216 {
14217     switch (extract32(insn, 25, 4)) {
14218     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14219         disas_b_exc_sys(s, insn);
14220         break;
14221     case 0x4:
14222     case 0x6:
14223     case 0xc:
14224     case 0xe:      /* Loads and stores */
14225         disas_ldst(s, insn);
14226         break;
14227     case 0x5:
14228     case 0xd:      /* Data processing - register */
14229         disas_data_proc_reg(s, insn);
14230         break;
14231     case 0x7:
14232     case 0xf:      /* Data processing - SIMD and floating point */
14233         disas_data_proc_simd_fp(s, insn);
14234         break;
14235     default:
14236         unallocated_encoding(s);
14237         break;
14238     }
14239 }
14240 
14241 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14242                                           CPUState *cpu)
14243 {
14244     DisasContext *dc = container_of(dcbase, DisasContext, base);
14245     CPUARMState *env = cpu->env_ptr;
14246     ARMCPU *arm_cpu = env_archcpu(env);
14247     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14248     int bound, core_mmu_idx;
14249 
14250     dc->isar = &arm_cpu->isar;
14251     dc->condjmp = 0;
14252     dc->pc_save = dc->base.pc_first;
14253     dc->aarch64 = true;
14254     dc->thumb = false;
14255     dc->sctlr_b = 0;
14256     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14257     dc->condexec_mask = 0;
14258     dc->condexec_cond = 0;
14259     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14260     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14261     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14262     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14263     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14264     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14265 #if !defined(CONFIG_USER_ONLY)
14266     dc->user = (dc->current_el == 0);
14267 #endif
14268     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14269     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14270     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14271     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14272     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14273     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14274     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14275     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14276     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14277     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14278     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14279     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14280     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14281     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14282     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14283     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14284     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14285     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14286     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14287     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14288     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14289     dc->vec_len = 0;
14290     dc->vec_stride = 0;
14291     dc->cp_regs = arm_cpu->cp_regs;
14292     dc->features = env->features;
14293     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14294 
14295 #ifdef CONFIG_USER_ONLY
14296     /* In sve_probe_page, we assume TBI is enabled. */
14297     tcg_debug_assert(dc->tbid & 1);
14298 #endif
14299 
14300     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14301 
14302     /* Single step state. The code-generation logic here is:
14303      *  SS_ACTIVE == 0:
14304      *   generate code with no special handling for single-stepping (except
14305      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14306      *   this happens anyway because those changes are all system register or
14307      *   PSTATE writes).
14308      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14309      *   emit code for one insn
14310      *   emit code to clear PSTATE.SS
14311      *   emit code to generate software step exception for completed step
14312      *   end TB (as usual for having generated an exception)
14313      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14314      *   emit code to generate a software step exception
14315      *   end the TB
14316      */
14317     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14318     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14319     dc->is_ldex = false;
14320 
14321     /* Bound the number of insns to execute to those left on the page.  */
14322     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14323 
14324     /* If architectural single step active, limit to 1.  */
14325     if (dc->ss_active) {
14326         bound = 1;
14327     }
14328     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14329 }
14330 
14331 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14332 {
14333 }
14334 
14335 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14336 {
14337     DisasContext *dc = container_of(dcbase, DisasContext, base);
14338     target_ulong pc_arg = dc->base.pc_next;
14339 
14340     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14341         pc_arg &= ~TARGET_PAGE_MASK;
14342     }
14343     tcg_gen_insn_start(pc_arg, 0, 0);
14344     dc->insn_start = tcg_last_op();
14345 }
14346 
14347 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14348 {
14349     DisasContext *s = container_of(dcbase, DisasContext, base);
14350     CPUARMState *env = cpu->env_ptr;
14351     uint64_t pc = s->base.pc_next;
14352     uint32_t insn;
14353 
14354     /* Singlestep exceptions have the highest priority. */
14355     if (s->ss_active && !s->pstate_ss) {
14356         /* Singlestep state is Active-pending.
14357          * If we're in this state at the start of a TB then either
14358          *  a) we just took an exception to an EL which is being debugged
14359          *     and this is the first insn in the exception handler
14360          *  b) debug exceptions were masked and we just unmasked them
14361          *     without changing EL (eg by clearing PSTATE.D)
14362          * In either case we're going to take a swstep exception in the
14363          * "did not step an insn" case, and so the syndrome ISV and EX
14364          * bits should be zero.
14365          */
14366         assert(s->base.num_insns == 1);
14367         gen_swstep_exception(s, 0, 0);
14368         s->base.is_jmp = DISAS_NORETURN;
14369         s->base.pc_next = pc + 4;
14370         return;
14371     }
14372 
14373     if (pc & 3) {
14374         /*
14375          * PC alignment fault.  This has priority over the instruction abort
14376          * that we would receive from a translation fault via arm_ldl_code.
14377          * This should only be possible after an indirect branch, at the
14378          * start of the TB.
14379          */
14380         assert(s->base.num_insns == 1);
14381         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14382         s->base.is_jmp = DISAS_NORETURN;
14383         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14384         return;
14385     }
14386 
14387     s->pc_curr = pc;
14388     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14389     s->insn = insn;
14390     s->base.pc_next = pc + 4;
14391 
14392     s->fp_access_checked = false;
14393     s->sve_access_checked = false;
14394 
14395     if (s->pstate_il) {
14396         /*
14397          * Illegal execution state. This has priority over BTI
14398          * exceptions, but comes after instruction abort exceptions.
14399          */
14400         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14401         return;
14402     }
14403 
14404     if (dc_isar_feature(aa64_bti, s)) {
14405         if (s->base.num_insns == 1) {
14406             /*
14407              * At the first insn of the TB, compute s->guarded_page.
14408              * We delayed computing this until successfully reading
14409              * the first insn of the TB, above.  This (mostly) ensures
14410              * that the softmmu tlb entry has been populated, and the
14411              * page table GP bit is available.
14412              *
14413              * Note that we need to compute this even if btype == 0,
14414              * because this value is used for BR instructions later
14415              * where ENV is not available.
14416              */
14417             s->guarded_page = is_guarded_page(env, s);
14418 
14419             /* First insn can have btype set to non-zero.  */
14420             tcg_debug_assert(s->btype >= 0);
14421 
14422             /*
14423              * Note that the Branch Target Exception has fairly high
14424              * priority -- below debugging exceptions but above most
14425              * everything else.  This allows us to handle this now
14426              * instead of waiting until the insn is otherwise decoded.
14427              */
14428             if (s->btype != 0
14429                 && s->guarded_page
14430                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14431                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14432                 return;
14433             }
14434         } else {
14435             /* Not the first insn: btype must be 0.  */
14436             tcg_debug_assert(s->btype == 0);
14437         }
14438     }
14439 
14440     s->is_nonstreaming = false;
14441     if (s->sme_trap_nonstreaming) {
14442         disas_sme_fa64(s, insn);
14443     }
14444 
14445     if (!disas_a64(s, insn) &&
14446         !disas_sme(s, insn) &&
14447         !disas_sve(s, insn)) {
14448         disas_a64_legacy(s, insn);
14449     }
14450 
14451     /*
14452      * After execution of most insns, btype is reset to 0.
14453      * Note that we set btype == -1 when the insn sets btype.
14454      */
14455     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14456         reset_btype(s);
14457     }
14458 }
14459 
14460 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14461 {
14462     DisasContext *dc = container_of(dcbase, DisasContext, base);
14463 
14464     if (unlikely(dc->ss_active)) {
14465         /* Note that this means single stepping WFI doesn't halt the CPU.
14466          * For conditional branch insns this is harmless unreachable code as
14467          * gen_goto_tb() has already handled emitting the debug exception
14468          * (and thus a tb-jump is not possible when singlestepping).
14469          */
14470         switch (dc->base.is_jmp) {
14471         default:
14472             gen_a64_update_pc(dc, 4);
14473             /* fall through */
14474         case DISAS_EXIT:
14475         case DISAS_JUMP:
14476             gen_step_complete_exception(dc);
14477             break;
14478         case DISAS_NORETURN:
14479             break;
14480         }
14481     } else {
14482         switch (dc->base.is_jmp) {
14483         case DISAS_NEXT:
14484         case DISAS_TOO_MANY:
14485             gen_goto_tb(dc, 1, 4);
14486             break;
14487         default:
14488         case DISAS_UPDATE_EXIT:
14489             gen_a64_update_pc(dc, 4);
14490             /* fall through */
14491         case DISAS_EXIT:
14492             tcg_gen_exit_tb(NULL, 0);
14493             break;
14494         case DISAS_UPDATE_NOCHAIN:
14495             gen_a64_update_pc(dc, 4);
14496             /* fall through */
14497         case DISAS_JUMP:
14498             tcg_gen_lookup_and_goto_ptr();
14499             break;
14500         case DISAS_NORETURN:
14501         case DISAS_SWI:
14502             break;
14503         case DISAS_WFE:
14504             gen_a64_update_pc(dc, 4);
14505             gen_helper_wfe(cpu_env);
14506             break;
14507         case DISAS_YIELD:
14508             gen_a64_update_pc(dc, 4);
14509             gen_helper_yield(cpu_env);
14510             break;
14511         case DISAS_WFI:
14512             /*
14513              * This is a special case because we don't want to just halt
14514              * the CPU if trying to debug across a WFI.
14515              */
14516             gen_a64_update_pc(dc, 4);
14517             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14518             /*
14519              * The helper doesn't necessarily throw an exception, but we
14520              * must go back to the main loop to check for interrupts anyway.
14521              */
14522             tcg_gen_exit_tb(NULL, 0);
14523             break;
14524         }
14525     }
14526 }
14527 
14528 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14529                                  CPUState *cpu, FILE *logfile)
14530 {
14531     DisasContext *dc = container_of(dcbase, DisasContext, base);
14532 
14533     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14534     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14535 }
14536 
14537 const TranslatorOps aarch64_translator_ops = {
14538     .init_disas_context = aarch64_tr_init_disas_context,
14539     .tb_start           = aarch64_tr_tb_start,
14540     .insn_start         = aarch64_tr_insn_start,
14541     .translate_insn     = aarch64_tr_translate_insn,
14542     .tb_stop            = aarch64_tr_tb_stop,
14543     .disas_log          = aarch64_tr_disas_log,
14544 };
14545