xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 45d063d1)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "translate.h"
22 #include "translate-a64.h"
23 #include "qemu/log.h"
24 #include "disas/disas.h"
25 #include "arm_ldst.h"
26 #include "semihosting/semihost.h"
27 #include "cpregs.h"
28 
29 static TCGv_i64 cpu_X[32];
30 static TCGv_i64 cpu_pc;
31 
32 /* Load/store exclusive handling */
33 static TCGv_i64 cpu_exclusive_high;
34 
35 static const char *regnames[] = {
36     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
37     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
38     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
39     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
40 };
41 
42 enum a64_shift_type {
43     A64_SHIFT_TYPE_LSL = 0,
44     A64_SHIFT_TYPE_LSR = 1,
45     A64_SHIFT_TYPE_ASR = 2,
46     A64_SHIFT_TYPE_ROR = 3
47 };
48 
49 /*
50  * Include the generated decoders.
51  */
52 
53 #include "decode-sme-fa64.c.inc"
54 #include "decode-a64.c.inc"
55 
56 /* Table based decoder typedefs - used when the relevant bits for decode
57  * are too awkwardly scattered across the instruction (eg SIMD).
58  */
59 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
60 
61 typedef struct AArch64DecodeTable {
62     uint32_t pattern;
63     uint32_t mask;
64     AArch64DecodeFn *disas_fn;
65 } AArch64DecodeTable;
66 
67 /* initialize TCG globals.  */
68 void a64_translate_init(void)
69 {
70     int i;
71 
72     cpu_pc = tcg_global_mem_new_i64(cpu_env,
73                                     offsetof(CPUARMState, pc),
74                                     "pc");
75     for (i = 0; i < 32; i++) {
76         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
77                                           offsetof(CPUARMState, xregs[i]),
78                                           regnames[i]);
79     }
80 
81     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
82         offsetof(CPUARMState, exclusive_high), "exclusive_high");
83 }
84 
85 /*
86  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
87  */
88 static int get_a64_user_mem_index(DisasContext *s)
89 {
90     /*
91      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
92      * which is the usual mmu_idx for this cpu state.
93      */
94     ARMMMUIdx useridx = s->mmu_idx;
95 
96     if (s->unpriv) {
97         /*
98          * We have pre-computed the condition for AccType_UNPRIV.
99          * Therefore we should never get here with a mmu_idx for
100          * which we do not know the corresponding user mmu_idx.
101          */
102         switch (useridx) {
103         case ARMMMUIdx_E10_1:
104         case ARMMMUIdx_E10_1_PAN:
105             useridx = ARMMMUIdx_E10_0;
106             break;
107         case ARMMMUIdx_E20_2:
108         case ARMMMUIdx_E20_2_PAN:
109             useridx = ARMMMUIdx_E20_0;
110             break;
111         default:
112             g_assert_not_reached();
113         }
114     }
115     return arm_to_core_mmu_idx(useridx);
116 }
117 
118 static void set_btype_raw(int val)
119 {
120     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
121                    offsetof(CPUARMState, btype));
122 }
123 
124 static void set_btype(DisasContext *s, int val)
125 {
126     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
127     tcg_debug_assert(val >= 1 && val <= 3);
128     set_btype_raw(val);
129     s->btype = -1;
130 }
131 
132 static void reset_btype(DisasContext *s)
133 {
134     if (s->btype != 0) {
135         set_btype_raw(0);
136         s->btype = 0;
137     }
138 }
139 
140 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
141 {
142     assert(s->pc_save != -1);
143     if (tb_cflags(s->base.tb) & CF_PCREL) {
144         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
145     } else {
146         tcg_gen_movi_i64(dest, s->pc_curr + diff);
147     }
148 }
149 
150 void gen_a64_update_pc(DisasContext *s, target_long diff)
151 {
152     gen_pc_plus_diff(s, cpu_pc, diff);
153     s->pc_save = s->pc_curr + diff;
154 }
155 
156 /*
157  * Handle Top Byte Ignore (TBI) bits.
158  *
159  * If address tagging is enabled via the TCR TBI bits:
160  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
161  *    then the address is zero-extended, clearing bits [63:56]
162  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
163  *    and TBI1 controls addressses with bit 55 == 1.
164  *    If the appropriate TBI bit is set for the address then
165  *    the address is sign-extended from bit 55 into bits [63:56]
166  *
167  * Here We have concatenated TBI{1,0} into tbi.
168  */
169 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
170                                 TCGv_i64 src, int tbi)
171 {
172     if (tbi == 0) {
173         /* Load unmodified address */
174         tcg_gen_mov_i64(dst, src);
175     } else if (!regime_has_2_ranges(s->mmu_idx)) {
176         /* Force tag byte to all zero */
177         tcg_gen_extract_i64(dst, src, 0, 56);
178     } else {
179         /* Sign-extend from bit 55.  */
180         tcg_gen_sextract_i64(dst, src, 0, 56);
181 
182         switch (tbi) {
183         case 1:
184             /* tbi0 but !tbi1: only use the extension if positive */
185             tcg_gen_and_i64(dst, dst, src);
186             break;
187         case 2:
188             /* !tbi0 but tbi1: only use the extension if negative */
189             tcg_gen_or_i64(dst, dst, src);
190             break;
191         case 3:
192             /* tbi0 and tbi1: always use the extension */
193             break;
194         default:
195             g_assert_not_reached();
196         }
197     }
198 }
199 
200 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
201 {
202     /*
203      * If address tagging is enabled for instructions via the TCR TBI bits,
204      * then loading an address into the PC will clear out any tag.
205      */
206     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
207     s->pc_save = -1;
208 }
209 
210 /*
211  * Handle MTE and/or TBI.
212  *
213  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
214  * for the tag to be present in the FAR_ELx register.  But for user-only
215  * mode we do not have a TLB with which to implement this, so we must
216  * remove the top byte now.
217  *
218  * Always return a fresh temporary that we can increment independently
219  * of the write-back address.
220  */
221 
222 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
223 {
224     TCGv_i64 clean = tcg_temp_new_i64();
225 #ifdef CONFIG_USER_ONLY
226     gen_top_byte_ignore(s, clean, addr, s->tbid);
227 #else
228     tcg_gen_mov_i64(clean, addr);
229 #endif
230     return clean;
231 }
232 
233 /* Insert a zero tag into src, with the result at dst. */
234 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
235 {
236     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
237 }
238 
239 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
240                              MMUAccessType acc, int log2_size)
241 {
242     gen_helper_probe_access(cpu_env, ptr,
243                             tcg_constant_i32(acc),
244                             tcg_constant_i32(get_mem_index(s)),
245                             tcg_constant_i32(1 << log2_size));
246 }
247 
248 /*
249  * For MTE, check a single logical or atomic access.  This probes a single
250  * address, the exact one specified.  The size and alignment of the access
251  * is not relevant to MTE, per se, but watchpoints do require the size,
252  * and we want to recognize those before making any other changes to state.
253  */
254 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
255                                       bool is_write, bool tag_checked,
256                                       MemOp memop, bool is_unpriv,
257                                       int core_idx)
258 {
259     if (tag_checked && s->mte_active[is_unpriv]) {
260         TCGv_i64 ret;
261         int desc = 0;
262 
263         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
264         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
265         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
266         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
267         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
268         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
269 
270         ret = tcg_temp_new_i64();
271         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
272 
273         return ret;
274     }
275     return clean_data_tbi(s, addr);
276 }
277 
278 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
279                         bool tag_checked, MemOp memop)
280 {
281     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
282                                  false, get_mem_index(s));
283 }
284 
285 /*
286  * For MTE, check multiple logical sequential accesses.
287  */
288 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
289                         bool tag_checked, int total_size, MemOp single_mop)
290 {
291     if (tag_checked && s->mte_active[0]) {
292         TCGv_i64 ret;
293         int desc = 0;
294 
295         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
296         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
297         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
298         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
299         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
300         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
301 
302         ret = tcg_temp_new_i64();
303         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
304 
305         return ret;
306     }
307     return clean_data_tbi(s, addr);
308 }
309 
310 /*
311  * Generate the special alignment check that applies to AccType_ATOMIC
312  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
313  * naturally aligned, but it must not cross a 16-byte boundary.
314  * See AArch64.CheckAlignment().
315  */
316 static void check_lse2_align(DisasContext *s, int rn, int imm,
317                              bool is_write, MemOp mop)
318 {
319     TCGv_i32 tmp;
320     TCGv_i64 addr;
321     TCGLabel *over_label;
322     MMUAccessType type;
323     int mmu_idx;
324 
325     tmp = tcg_temp_new_i32();
326     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
327     tcg_gen_addi_i32(tmp, tmp, imm & 15);
328     tcg_gen_andi_i32(tmp, tmp, 15);
329     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
330 
331     over_label = gen_new_label();
332     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
333 
334     addr = tcg_temp_new_i64();
335     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
336 
337     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
338     mmu_idx = get_mem_index(s);
339     gen_helper_unaligned_access(cpu_env, addr, tcg_constant_i32(type),
340                                 tcg_constant_i32(mmu_idx));
341 
342     gen_set_label(over_label);
343 
344 }
345 
346 /* Handle the alignment check for AccType_ATOMIC instructions. */
347 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
348 {
349     MemOp size = mop & MO_SIZE;
350 
351     if (size == MO_8) {
352         return mop;
353     }
354 
355     /*
356      * If size == MO_128, this is a LDXP, and the operation is single-copy
357      * atomic for each doubleword, not the entire quadword; it still must
358      * be quadword aligned.
359      */
360     if (size == MO_128) {
361         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
362                                    MO_ATOM_IFALIGN_PAIR);
363     }
364     if (dc_isar_feature(aa64_lse2, s)) {
365         check_lse2_align(s, rn, 0, true, mop);
366     } else {
367         mop |= MO_ALIGN;
368     }
369     return finalize_memop(s, mop);
370 }
371 
372 /* Handle the alignment check for AccType_ORDERED instructions. */
373 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
374                                  bool is_write, MemOp mop)
375 {
376     MemOp size = mop & MO_SIZE;
377 
378     if (size == MO_8) {
379         return mop;
380     }
381     if (size == MO_128) {
382         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
383                                    MO_ATOM_IFALIGN_PAIR);
384     }
385     if (!dc_isar_feature(aa64_lse2, s)) {
386         mop |= MO_ALIGN;
387     } else if (!s->naa) {
388         check_lse2_align(s, rn, imm, is_write, mop);
389     }
390     return finalize_memop(s, mop);
391 }
392 
393 typedef struct DisasCompare64 {
394     TCGCond cond;
395     TCGv_i64 value;
396 } DisasCompare64;
397 
398 static void a64_test_cc(DisasCompare64 *c64, int cc)
399 {
400     DisasCompare c32;
401 
402     arm_test_cc(&c32, cc);
403 
404     /*
405      * Sign-extend the 32-bit value so that the GE/LT comparisons work
406      * properly.  The NE/EQ comparisons are also fine with this choice.
407       */
408     c64->cond = c32.cond;
409     c64->value = tcg_temp_new_i64();
410     tcg_gen_ext_i32_i64(c64->value, c32.value);
411 }
412 
413 static void gen_rebuild_hflags(DisasContext *s)
414 {
415     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
416 }
417 
418 static void gen_exception_internal(int excp)
419 {
420     assert(excp_is_internal(excp));
421     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
422 }
423 
424 static void gen_exception_internal_insn(DisasContext *s, int excp)
425 {
426     gen_a64_update_pc(s, 0);
427     gen_exception_internal(excp);
428     s->base.is_jmp = DISAS_NORETURN;
429 }
430 
431 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
432 {
433     gen_a64_update_pc(s, 0);
434     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
435     s->base.is_jmp = DISAS_NORETURN;
436 }
437 
438 static void gen_step_complete_exception(DisasContext *s)
439 {
440     /* We just completed step of an insn. Move from Active-not-pending
441      * to Active-pending, and then also take the swstep exception.
442      * This corresponds to making the (IMPDEF) choice to prioritize
443      * swstep exceptions over asynchronous exceptions taken to an exception
444      * level where debug is disabled. This choice has the advantage that
445      * we do not need to maintain internal state corresponding to the
446      * ISV/EX syndrome bits between completion of the step and generation
447      * of the exception, and our syndrome information is always correct.
448      */
449     gen_ss_advance(s);
450     gen_swstep_exception(s, 1, s->is_ldex);
451     s->base.is_jmp = DISAS_NORETURN;
452 }
453 
454 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
455 {
456     if (s->ss_active) {
457         return false;
458     }
459     return translator_use_goto_tb(&s->base, dest);
460 }
461 
462 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
463 {
464     if (use_goto_tb(s, s->pc_curr + diff)) {
465         /*
466          * For pcrel, the pc must always be up-to-date on entry to
467          * the linked TB, so that it can use simple additions for all
468          * further adjustments.  For !pcrel, the linked TB is compiled
469          * to know its full virtual address, so we can delay the
470          * update to pc to the unlinked path.  A long chain of links
471          * can thus avoid many updates to the PC.
472          */
473         if (tb_cflags(s->base.tb) & CF_PCREL) {
474             gen_a64_update_pc(s, diff);
475             tcg_gen_goto_tb(n);
476         } else {
477             tcg_gen_goto_tb(n);
478             gen_a64_update_pc(s, diff);
479         }
480         tcg_gen_exit_tb(s->base.tb, n);
481         s->base.is_jmp = DISAS_NORETURN;
482     } else {
483         gen_a64_update_pc(s, diff);
484         if (s->ss_active) {
485             gen_step_complete_exception(s);
486         } else {
487             tcg_gen_lookup_and_goto_ptr();
488             s->base.is_jmp = DISAS_NORETURN;
489         }
490     }
491 }
492 
493 /*
494  * Register access functions
495  *
496  * These functions are used for directly accessing a register in where
497  * changes to the final register value are likely to be made. If you
498  * need to use a register for temporary calculation (e.g. index type
499  * operations) use the read_* form.
500  *
501  * B1.2.1 Register mappings
502  *
503  * In instruction register encoding 31 can refer to ZR (zero register) or
504  * the SP (stack pointer) depending on context. In QEMU's case we map SP
505  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
506  * This is the point of the _sp forms.
507  */
508 TCGv_i64 cpu_reg(DisasContext *s, int reg)
509 {
510     if (reg == 31) {
511         TCGv_i64 t = tcg_temp_new_i64();
512         tcg_gen_movi_i64(t, 0);
513         return t;
514     } else {
515         return cpu_X[reg];
516     }
517 }
518 
519 /* register access for when 31 == SP */
520 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
521 {
522     return cpu_X[reg];
523 }
524 
525 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
526  * representing the register contents. This TCGv is an auto-freed
527  * temporary so it need not be explicitly freed, and may be modified.
528  */
529 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
530 {
531     TCGv_i64 v = tcg_temp_new_i64();
532     if (reg != 31) {
533         if (sf) {
534             tcg_gen_mov_i64(v, cpu_X[reg]);
535         } else {
536             tcg_gen_ext32u_i64(v, cpu_X[reg]);
537         }
538     } else {
539         tcg_gen_movi_i64(v, 0);
540     }
541     return v;
542 }
543 
544 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
545 {
546     TCGv_i64 v = tcg_temp_new_i64();
547     if (sf) {
548         tcg_gen_mov_i64(v, cpu_X[reg]);
549     } else {
550         tcg_gen_ext32u_i64(v, cpu_X[reg]);
551     }
552     return v;
553 }
554 
555 /* Return the offset into CPUARMState of a slice (from
556  * the least significant end) of FP register Qn (ie
557  * Dn, Sn, Hn or Bn).
558  * (Note that this is not the same mapping as for A32; see cpu.h)
559  */
560 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
561 {
562     return vec_reg_offset(s, regno, 0, size);
563 }
564 
565 /* Offset of the high half of the 128 bit vector Qn */
566 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
567 {
568     return vec_reg_offset(s, regno, 1, MO_64);
569 }
570 
571 /* Convenience accessors for reading and writing single and double
572  * FP registers. Writing clears the upper parts of the associated
573  * 128 bit vector register, as required by the architecture.
574  * Note that unlike the GP register accessors, the values returned
575  * by the read functions must be manually freed.
576  */
577 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
578 {
579     TCGv_i64 v = tcg_temp_new_i64();
580 
581     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
582     return v;
583 }
584 
585 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
586 {
587     TCGv_i32 v = tcg_temp_new_i32();
588 
589     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
590     return v;
591 }
592 
593 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
594 {
595     TCGv_i32 v = tcg_temp_new_i32();
596 
597     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
598     return v;
599 }
600 
601 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
602  * If SVE is not enabled, then there are only 128 bits in the vector.
603  */
604 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
605 {
606     unsigned ofs = fp_reg_offset(s, rd, MO_64);
607     unsigned vsz = vec_full_reg_size(s);
608 
609     /* Nop move, with side effect of clearing the tail. */
610     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
611 }
612 
613 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
614 {
615     unsigned ofs = fp_reg_offset(s, reg, MO_64);
616 
617     tcg_gen_st_i64(v, cpu_env, ofs);
618     clear_vec_high(s, false, reg);
619 }
620 
621 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
622 {
623     TCGv_i64 tmp = tcg_temp_new_i64();
624 
625     tcg_gen_extu_i32_i64(tmp, v);
626     write_fp_dreg(s, reg, tmp);
627 }
628 
629 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
630 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
631                          GVecGen2Fn *gvec_fn, int vece)
632 {
633     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
634             is_q ? 16 : 8, vec_full_reg_size(s));
635 }
636 
637 /* Expand a 2-operand + immediate AdvSIMD vector operation using
638  * an expander function.
639  */
640 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
641                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
642 {
643     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
644             imm, is_q ? 16 : 8, vec_full_reg_size(s));
645 }
646 
647 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
648 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
649                          GVecGen3Fn *gvec_fn, int vece)
650 {
651     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
652             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
653 }
654 
655 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
656 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
657                          int rx, GVecGen4Fn *gvec_fn, int vece)
658 {
659     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
660             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
661             is_q ? 16 : 8, vec_full_reg_size(s));
662 }
663 
664 /* Expand a 2-operand operation using an out-of-line helper.  */
665 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
666                              int rn, int data, gen_helper_gvec_2 *fn)
667 {
668     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
669                        vec_full_reg_offset(s, rn),
670                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
671 }
672 
673 /* Expand a 3-operand operation using an out-of-line helper.  */
674 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
675                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
676 {
677     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
678                        vec_full_reg_offset(s, rn),
679                        vec_full_reg_offset(s, rm),
680                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
681 }
682 
683 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
684  * an out-of-line helper.
685  */
686 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
687                               int rm, bool is_fp16, int data,
688                               gen_helper_gvec_3_ptr *fn)
689 {
690     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
691     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
692                        vec_full_reg_offset(s, rn),
693                        vec_full_reg_offset(s, rm), fpst,
694                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
695 }
696 
697 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
698 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
699                             int rm, gen_helper_gvec_3_ptr *fn)
700 {
701     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
702 
703     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
704     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
705                        vec_full_reg_offset(s, rn),
706                        vec_full_reg_offset(s, rm), qc_ptr,
707                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
708 }
709 
710 /* Expand a 4-operand operation using an out-of-line helper.  */
711 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
712                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
713 {
714     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
715                        vec_full_reg_offset(s, rn),
716                        vec_full_reg_offset(s, rm),
717                        vec_full_reg_offset(s, ra),
718                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
719 }
720 
721 /*
722  * Expand a 4-operand + fpstatus pointer + simd data value operation using
723  * an out-of-line helper.
724  */
725 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
726                               int rm, int ra, bool is_fp16, int data,
727                               gen_helper_gvec_4_ptr *fn)
728 {
729     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
730     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
731                        vec_full_reg_offset(s, rn),
732                        vec_full_reg_offset(s, rm),
733                        vec_full_reg_offset(s, ra), fpst,
734                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
735 }
736 
737 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
738  * than the 32 bit equivalent.
739  */
740 static inline void gen_set_NZ64(TCGv_i64 result)
741 {
742     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
743     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
744 }
745 
746 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
747 static inline void gen_logic_CC(int sf, TCGv_i64 result)
748 {
749     if (sf) {
750         gen_set_NZ64(result);
751     } else {
752         tcg_gen_extrl_i64_i32(cpu_ZF, result);
753         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
754     }
755     tcg_gen_movi_i32(cpu_CF, 0);
756     tcg_gen_movi_i32(cpu_VF, 0);
757 }
758 
759 /* dest = T0 + T1; compute C, N, V and Z flags */
760 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
761 {
762     TCGv_i64 result, flag, tmp;
763     result = tcg_temp_new_i64();
764     flag = tcg_temp_new_i64();
765     tmp = tcg_temp_new_i64();
766 
767     tcg_gen_movi_i64(tmp, 0);
768     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
769 
770     tcg_gen_extrl_i64_i32(cpu_CF, flag);
771 
772     gen_set_NZ64(result);
773 
774     tcg_gen_xor_i64(flag, result, t0);
775     tcg_gen_xor_i64(tmp, t0, t1);
776     tcg_gen_andc_i64(flag, flag, tmp);
777     tcg_gen_extrh_i64_i32(cpu_VF, flag);
778 
779     tcg_gen_mov_i64(dest, result);
780 }
781 
782 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
783 {
784     TCGv_i32 t0_32 = tcg_temp_new_i32();
785     TCGv_i32 t1_32 = tcg_temp_new_i32();
786     TCGv_i32 tmp = tcg_temp_new_i32();
787 
788     tcg_gen_movi_i32(tmp, 0);
789     tcg_gen_extrl_i64_i32(t0_32, t0);
790     tcg_gen_extrl_i64_i32(t1_32, t1);
791     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
792     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
793     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
794     tcg_gen_xor_i32(tmp, t0_32, t1_32);
795     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
796     tcg_gen_extu_i32_i64(dest, cpu_NF);
797 }
798 
799 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
800 {
801     if (sf) {
802         gen_add64_CC(dest, t0, t1);
803     } else {
804         gen_add32_CC(dest, t0, t1);
805     }
806 }
807 
808 /* dest = T0 - T1; compute C, N, V and Z flags */
809 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
810 {
811     /* 64 bit arithmetic */
812     TCGv_i64 result, flag, tmp;
813 
814     result = tcg_temp_new_i64();
815     flag = tcg_temp_new_i64();
816     tcg_gen_sub_i64(result, t0, t1);
817 
818     gen_set_NZ64(result);
819 
820     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
821     tcg_gen_extrl_i64_i32(cpu_CF, flag);
822 
823     tcg_gen_xor_i64(flag, result, t0);
824     tmp = tcg_temp_new_i64();
825     tcg_gen_xor_i64(tmp, t0, t1);
826     tcg_gen_and_i64(flag, flag, tmp);
827     tcg_gen_extrh_i64_i32(cpu_VF, flag);
828     tcg_gen_mov_i64(dest, result);
829 }
830 
831 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
832 {
833     /* 32 bit arithmetic */
834     TCGv_i32 t0_32 = tcg_temp_new_i32();
835     TCGv_i32 t1_32 = tcg_temp_new_i32();
836     TCGv_i32 tmp;
837 
838     tcg_gen_extrl_i64_i32(t0_32, t0);
839     tcg_gen_extrl_i64_i32(t1_32, t1);
840     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
841     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
842     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
843     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
844     tmp = tcg_temp_new_i32();
845     tcg_gen_xor_i32(tmp, t0_32, t1_32);
846     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
847     tcg_gen_extu_i32_i64(dest, cpu_NF);
848 }
849 
850 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
851 {
852     if (sf) {
853         gen_sub64_CC(dest, t0, t1);
854     } else {
855         gen_sub32_CC(dest, t0, t1);
856     }
857 }
858 
859 /* dest = T0 + T1 + CF; do not compute flags. */
860 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
861 {
862     TCGv_i64 flag = tcg_temp_new_i64();
863     tcg_gen_extu_i32_i64(flag, cpu_CF);
864     tcg_gen_add_i64(dest, t0, t1);
865     tcg_gen_add_i64(dest, dest, flag);
866 
867     if (!sf) {
868         tcg_gen_ext32u_i64(dest, dest);
869     }
870 }
871 
872 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
873 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
874 {
875     if (sf) {
876         TCGv_i64 result = tcg_temp_new_i64();
877         TCGv_i64 cf_64 = tcg_temp_new_i64();
878         TCGv_i64 vf_64 = tcg_temp_new_i64();
879         TCGv_i64 tmp = tcg_temp_new_i64();
880         TCGv_i64 zero = tcg_constant_i64(0);
881 
882         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
883         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
884         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
885         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
886         gen_set_NZ64(result);
887 
888         tcg_gen_xor_i64(vf_64, result, t0);
889         tcg_gen_xor_i64(tmp, t0, t1);
890         tcg_gen_andc_i64(vf_64, vf_64, tmp);
891         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
892 
893         tcg_gen_mov_i64(dest, result);
894     } else {
895         TCGv_i32 t0_32 = tcg_temp_new_i32();
896         TCGv_i32 t1_32 = tcg_temp_new_i32();
897         TCGv_i32 tmp = tcg_temp_new_i32();
898         TCGv_i32 zero = tcg_constant_i32(0);
899 
900         tcg_gen_extrl_i64_i32(t0_32, t0);
901         tcg_gen_extrl_i64_i32(t1_32, t1);
902         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
903         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
904 
905         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
906         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
907         tcg_gen_xor_i32(tmp, t0_32, t1_32);
908         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
909         tcg_gen_extu_i32_i64(dest, cpu_NF);
910     }
911 }
912 
913 /*
914  * Load/Store generators
915  */
916 
917 /*
918  * Store from GPR register to memory.
919  */
920 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
921                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
922                              bool iss_valid,
923                              unsigned int iss_srt,
924                              bool iss_sf, bool iss_ar)
925 {
926     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
927 
928     if (iss_valid) {
929         uint32_t syn;
930 
931         syn = syn_data_abort_with_iss(0,
932                                       (memop & MO_SIZE),
933                                       false,
934                                       iss_srt,
935                                       iss_sf,
936                                       iss_ar,
937                                       0, 0, 0, 0, 0, false);
938         disas_set_insn_syndrome(s, syn);
939     }
940 }
941 
942 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
943                       TCGv_i64 tcg_addr, MemOp memop,
944                       bool iss_valid,
945                       unsigned int iss_srt,
946                       bool iss_sf, bool iss_ar)
947 {
948     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
949                      iss_valid, iss_srt, iss_sf, iss_ar);
950 }
951 
952 /*
953  * Load from memory to GPR register
954  */
955 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
956                              MemOp memop, bool extend, int memidx,
957                              bool iss_valid, unsigned int iss_srt,
958                              bool iss_sf, bool iss_ar)
959 {
960     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
961 
962     if (extend && (memop & MO_SIGN)) {
963         g_assert((memop & MO_SIZE) <= MO_32);
964         tcg_gen_ext32u_i64(dest, dest);
965     }
966 
967     if (iss_valid) {
968         uint32_t syn;
969 
970         syn = syn_data_abort_with_iss(0,
971                                       (memop & MO_SIZE),
972                                       (memop & MO_SIGN) != 0,
973                                       iss_srt,
974                                       iss_sf,
975                                       iss_ar,
976                                       0, 0, 0, 0, 0, false);
977         disas_set_insn_syndrome(s, syn);
978     }
979 }
980 
981 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
982                       MemOp memop, bool extend,
983                       bool iss_valid, unsigned int iss_srt,
984                       bool iss_sf, bool iss_ar)
985 {
986     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
987                      iss_valid, iss_srt, iss_sf, iss_ar);
988 }
989 
990 /*
991  * Store from FP register to memory
992  */
993 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
994 {
995     /* This writes the bottom N bits of a 128 bit wide vector to memory */
996     TCGv_i64 tmplo = tcg_temp_new_i64();
997 
998     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
999 
1000     if ((mop & MO_SIZE) < MO_128) {
1001         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1002     } else {
1003         TCGv_i64 tmphi = tcg_temp_new_i64();
1004         TCGv_i128 t16 = tcg_temp_new_i128();
1005 
1006         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
1007         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1008 
1009         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1010     }
1011 }
1012 
1013 /*
1014  * Load from memory to FP register
1015  */
1016 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1017 {
1018     /* This always zero-extends and writes to a full 128 bit wide vector */
1019     TCGv_i64 tmplo = tcg_temp_new_i64();
1020     TCGv_i64 tmphi = NULL;
1021 
1022     if ((mop & MO_SIZE) < MO_128) {
1023         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1024     } else {
1025         TCGv_i128 t16 = tcg_temp_new_i128();
1026 
1027         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1028 
1029         tmphi = tcg_temp_new_i64();
1030         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1031     }
1032 
1033     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1034 
1035     if (tmphi) {
1036         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1037     }
1038     clear_vec_high(s, tmphi != NULL, destidx);
1039 }
1040 
1041 /*
1042  * Vector load/store helpers.
1043  *
1044  * The principal difference between this and a FP load is that we don't
1045  * zero extend as we are filling a partial chunk of the vector register.
1046  * These functions don't support 128 bit loads/stores, which would be
1047  * normal load/store operations.
1048  *
1049  * The _i32 versions are useful when operating on 32 bit quantities
1050  * (eg for floating point single or using Neon helper functions).
1051  */
1052 
1053 /* Get value of an element within a vector register */
1054 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1055                              int element, MemOp memop)
1056 {
1057     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1058     switch ((unsigned)memop) {
1059     case MO_8:
1060         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1061         break;
1062     case MO_16:
1063         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1064         break;
1065     case MO_32:
1066         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1067         break;
1068     case MO_8|MO_SIGN:
1069         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1070         break;
1071     case MO_16|MO_SIGN:
1072         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1073         break;
1074     case MO_32|MO_SIGN:
1075         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1076         break;
1077     case MO_64:
1078     case MO_64|MO_SIGN:
1079         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1080         break;
1081     default:
1082         g_assert_not_reached();
1083     }
1084 }
1085 
1086 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1087                                  int element, MemOp memop)
1088 {
1089     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1090     switch (memop) {
1091     case MO_8:
1092         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1093         break;
1094     case MO_16:
1095         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1096         break;
1097     case MO_8|MO_SIGN:
1098         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1099         break;
1100     case MO_16|MO_SIGN:
1101         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1102         break;
1103     case MO_32:
1104     case MO_32|MO_SIGN:
1105         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1106         break;
1107     default:
1108         g_assert_not_reached();
1109     }
1110 }
1111 
1112 /* Set value of an element within a vector register */
1113 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1114                               int element, MemOp memop)
1115 {
1116     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1117     switch (memop) {
1118     case MO_8:
1119         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1120         break;
1121     case MO_16:
1122         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1123         break;
1124     case MO_32:
1125         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1126         break;
1127     case MO_64:
1128         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1129         break;
1130     default:
1131         g_assert_not_reached();
1132     }
1133 }
1134 
1135 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1136                                   int destidx, int element, MemOp memop)
1137 {
1138     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1139     switch (memop) {
1140     case MO_8:
1141         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1142         break;
1143     case MO_16:
1144         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1145         break;
1146     case MO_32:
1147         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1148         break;
1149     default:
1150         g_assert_not_reached();
1151     }
1152 }
1153 
1154 /* Store from vector register to memory */
1155 static void do_vec_st(DisasContext *s, int srcidx, int element,
1156                       TCGv_i64 tcg_addr, MemOp mop)
1157 {
1158     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1159 
1160     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1161     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1162 }
1163 
1164 /* Load from memory to vector register */
1165 static void do_vec_ld(DisasContext *s, int destidx, int element,
1166                       TCGv_i64 tcg_addr, MemOp mop)
1167 {
1168     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1169 
1170     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1171     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1172 }
1173 
1174 /* Check that FP/Neon access is enabled. If it is, return
1175  * true. If not, emit code to generate an appropriate exception,
1176  * and return false; the caller should not emit any code for
1177  * the instruction. Note that this check must happen after all
1178  * unallocated-encoding checks (otherwise the syndrome information
1179  * for the resulting exception will be incorrect).
1180  */
1181 static bool fp_access_check_only(DisasContext *s)
1182 {
1183     if (s->fp_excp_el) {
1184         assert(!s->fp_access_checked);
1185         s->fp_access_checked = true;
1186 
1187         gen_exception_insn_el(s, 0, EXCP_UDEF,
1188                               syn_fp_access_trap(1, 0xe, false, 0),
1189                               s->fp_excp_el);
1190         return false;
1191     }
1192     s->fp_access_checked = true;
1193     return true;
1194 }
1195 
1196 static bool fp_access_check(DisasContext *s)
1197 {
1198     if (!fp_access_check_only(s)) {
1199         return false;
1200     }
1201     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1202         gen_exception_insn(s, 0, EXCP_UDEF,
1203                            syn_smetrap(SME_ET_Streaming, false));
1204         return false;
1205     }
1206     return true;
1207 }
1208 
1209 /*
1210  * Check that SVE access is enabled.  If it is, return true.
1211  * If not, emit code to generate an appropriate exception and return false.
1212  * This function corresponds to CheckSVEEnabled().
1213  */
1214 bool sve_access_check(DisasContext *s)
1215 {
1216     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1217         assert(dc_isar_feature(aa64_sme, s));
1218         if (!sme_sm_enabled_check(s)) {
1219             goto fail_exit;
1220         }
1221     } else if (s->sve_excp_el) {
1222         gen_exception_insn_el(s, 0, EXCP_UDEF,
1223                               syn_sve_access_trap(), s->sve_excp_el);
1224         goto fail_exit;
1225     }
1226     s->sve_access_checked = true;
1227     return fp_access_check(s);
1228 
1229  fail_exit:
1230     /* Assert that we only raise one exception per instruction. */
1231     assert(!s->sve_access_checked);
1232     s->sve_access_checked = true;
1233     return false;
1234 }
1235 
1236 /*
1237  * Check that SME access is enabled, raise an exception if not.
1238  * Note that this function corresponds to CheckSMEAccess and is
1239  * only used directly for cpregs.
1240  */
1241 static bool sme_access_check(DisasContext *s)
1242 {
1243     if (s->sme_excp_el) {
1244         gen_exception_insn_el(s, 0, EXCP_UDEF,
1245                               syn_smetrap(SME_ET_AccessTrap, false),
1246                               s->sme_excp_el);
1247         return false;
1248     }
1249     return true;
1250 }
1251 
1252 /* This function corresponds to CheckSMEEnabled. */
1253 bool sme_enabled_check(DisasContext *s)
1254 {
1255     /*
1256      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1257      * to be zero when fp_excp_el has priority.  This is because we need
1258      * sme_excp_el by itself for cpregs access checks.
1259      */
1260     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1261         s->fp_access_checked = true;
1262         return sme_access_check(s);
1263     }
1264     return fp_access_check_only(s);
1265 }
1266 
1267 /* Common subroutine for CheckSMEAnd*Enabled. */
1268 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1269 {
1270     if (!sme_enabled_check(s)) {
1271         return false;
1272     }
1273     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1274         gen_exception_insn(s, 0, EXCP_UDEF,
1275                            syn_smetrap(SME_ET_NotStreaming, false));
1276         return false;
1277     }
1278     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1279         gen_exception_insn(s, 0, EXCP_UDEF,
1280                            syn_smetrap(SME_ET_InactiveZA, false));
1281         return false;
1282     }
1283     return true;
1284 }
1285 
1286 /*
1287  * This utility function is for doing register extension with an
1288  * optional shift. You will likely want to pass a temporary for the
1289  * destination register. See DecodeRegExtend() in the ARM ARM.
1290  */
1291 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1292                               int option, unsigned int shift)
1293 {
1294     int extsize = extract32(option, 0, 2);
1295     bool is_signed = extract32(option, 2, 1);
1296 
1297     if (is_signed) {
1298         switch (extsize) {
1299         case 0:
1300             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1301             break;
1302         case 1:
1303             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1304             break;
1305         case 2:
1306             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1307             break;
1308         case 3:
1309             tcg_gen_mov_i64(tcg_out, tcg_in);
1310             break;
1311         }
1312     } else {
1313         switch (extsize) {
1314         case 0:
1315             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1316             break;
1317         case 1:
1318             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1319             break;
1320         case 2:
1321             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1322             break;
1323         case 3:
1324             tcg_gen_mov_i64(tcg_out, tcg_in);
1325             break;
1326         }
1327     }
1328 
1329     if (shift) {
1330         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1331     }
1332 }
1333 
1334 static inline void gen_check_sp_alignment(DisasContext *s)
1335 {
1336     /* The AArch64 architecture mandates that (if enabled via PSTATE
1337      * or SCTLR bits) there is a check that SP is 16-aligned on every
1338      * SP-relative load or store (with an exception generated if it is not).
1339      * In line with general QEMU practice regarding misaligned accesses,
1340      * we omit these checks for the sake of guest program performance.
1341      * This function is provided as a hook so we can more easily add these
1342      * checks in future (possibly as a "favour catching guest program bugs
1343      * over speed" user selectable option).
1344      */
1345 }
1346 
1347 /*
1348  * This provides a simple table based table lookup decoder. It is
1349  * intended to be used when the relevant bits for decode are too
1350  * awkwardly placed and switch/if based logic would be confusing and
1351  * deeply nested. Since it's a linear search through the table, tables
1352  * should be kept small.
1353  *
1354  * It returns the first handler where insn & mask == pattern, or
1355  * NULL if there is no match.
1356  * The table is terminated by an empty mask (i.e. 0)
1357  */
1358 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1359                                                uint32_t insn)
1360 {
1361     const AArch64DecodeTable *tptr = table;
1362 
1363     while (tptr->mask) {
1364         if ((insn & tptr->mask) == tptr->pattern) {
1365             return tptr->disas_fn;
1366         }
1367         tptr++;
1368     }
1369     return NULL;
1370 }
1371 
1372 /*
1373  * The instruction disassembly implemented here matches
1374  * the instruction encoding classifications in chapter C4
1375  * of the ARM Architecture Reference Manual (DDI0487B_a);
1376  * classification names and decode diagrams here should generally
1377  * match up with those in the manual.
1378  */
1379 
1380 static bool trans_B(DisasContext *s, arg_i *a)
1381 {
1382     reset_btype(s);
1383     gen_goto_tb(s, 0, a->imm);
1384     return true;
1385 }
1386 
1387 static bool trans_BL(DisasContext *s, arg_i *a)
1388 {
1389     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1390     reset_btype(s);
1391     gen_goto_tb(s, 0, a->imm);
1392     return true;
1393 }
1394 
1395 
1396 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1397 {
1398     DisasLabel match;
1399     TCGv_i64 tcg_cmp;
1400 
1401     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1402     reset_btype(s);
1403 
1404     match = gen_disas_label(s);
1405     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1406                         tcg_cmp, 0, match.label);
1407     gen_goto_tb(s, 0, 4);
1408     set_disas_label(s, match);
1409     gen_goto_tb(s, 1, a->imm);
1410     return true;
1411 }
1412 
1413 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1414 {
1415     DisasLabel match;
1416     TCGv_i64 tcg_cmp;
1417 
1418     tcg_cmp = tcg_temp_new_i64();
1419     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1420 
1421     reset_btype(s);
1422 
1423     match = gen_disas_label(s);
1424     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1425                         tcg_cmp, 0, match.label);
1426     gen_goto_tb(s, 0, 4);
1427     set_disas_label(s, match);
1428     gen_goto_tb(s, 1, a->imm);
1429     return true;
1430 }
1431 
1432 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1433 {
1434     reset_btype(s);
1435     if (a->cond < 0x0e) {
1436         /* genuinely conditional branches */
1437         DisasLabel match = gen_disas_label(s);
1438         arm_gen_test_cc(a->cond, match.label);
1439         gen_goto_tb(s, 0, 4);
1440         set_disas_label(s, match);
1441         gen_goto_tb(s, 1, a->imm);
1442     } else {
1443         /* 0xe and 0xf are both "always" conditions */
1444         gen_goto_tb(s, 0, a->imm);
1445     }
1446     return true;
1447 }
1448 
1449 static void set_btype_for_br(DisasContext *s, int rn)
1450 {
1451     if (dc_isar_feature(aa64_bti, s)) {
1452         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1453         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1454     }
1455 }
1456 
1457 static void set_btype_for_blr(DisasContext *s)
1458 {
1459     if (dc_isar_feature(aa64_bti, s)) {
1460         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1461         set_btype(s, 2);
1462     }
1463 }
1464 
1465 static bool trans_BR(DisasContext *s, arg_r *a)
1466 {
1467     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1468     set_btype_for_br(s, a->rn);
1469     s->base.is_jmp = DISAS_JUMP;
1470     return true;
1471 }
1472 
1473 static bool trans_BLR(DisasContext *s, arg_r *a)
1474 {
1475     TCGv_i64 dst = cpu_reg(s, a->rn);
1476     TCGv_i64 lr = cpu_reg(s, 30);
1477     if (dst == lr) {
1478         TCGv_i64 tmp = tcg_temp_new_i64();
1479         tcg_gen_mov_i64(tmp, dst);
1480         dst = tmp;
1481     }
1482     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1483     gen_a64_set_pc(s, dst);
1484     set_btype_for_blr(s);
1485     s->base.is_jmp = DISAS_JUMP;
1486     return true;
1487 }
1488 
1489 static bool trans_RET(DisasContext *s, arg_r *a)
1490 {
1491     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1492     s->base.is_jmp = DISAS_JUMP;
1493     return true;
1494 }
1495 
1496 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1497                                    TCGv_i64 modifier, bool use_key_a)
1498 {
1499     TCGv_i64 truedst;
1500     /*
1501      * Return the branch target for a BRAA/RETA/etc, which is either
1502      * just the destination dst, or that value with the pauth check
1503      * done and the code removed from the high bits.
1504      */
1505     if (!s->pauth_active) {
1506         return dst;
1507     }
1508 
1509     truedst = tcg_temp_new_i64();
1510     if (use_key_a) {
1511         gen_helper_autia(truedst, cpu_env, dst, modifier);
1512     } else {
1513         gen_helper_autib(truedst, cpu_env, dst, modifier);
1514     }
1515     return truedst;
1516 }
1517 
1518 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1519 {
1520     TCGv_i64 dst;
1521 
1522     if (!dc_isar_feature(aa64_pauth, s)) {
1523         return false;
1524     }
1525 
1526     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1527     gen_a64_set_pc(s, dst);
1528     set_btype_for_br(s, a->rn);
1529     s->base.is_jmp = DISAS_JUMP;
1530     return true;
1531 }
1532 
1533 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1534 {
1535     TCGv_i64 dst, lr;
1536 
1537     if (!dc_isar_feature(aa64_pauth, s)) {
1538         return false;
1539     }
1540 
1541     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1542     lr = cpu_reg(s, 30);
1543     if (dst == lr) {
1544         TCGv_i64 tmp = tcg_temp_new_i64();
1545         tcg_gen_mov_i64(tmp, dst);
1546         dst = tmp;
1547     }
1548     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1549     gen_a64_set_pc(s, dst);
1550     set_btype_for_blr(s);
1551     s->base.is_jmp = DISAS_JUMP;
1552     return true;
1553 }
1554 
1555 static bool trans_RETA(DisasContext *s, arg_reta *a)
1556 {
1557     TCGv_i64 dst;
1558 
1559     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1560     gen_a64_set_pc(s, dst);
1561     s->base.is_jmp = DISAS_JUMP;
1562     return true;
1563 }
1564 
1565 static bool trans_BRA(DisasContext *s, arg_bra *a)
1566 {
1567     TCGv_i64 dst;
1568 
1569     if (!dc_isar_feature(aa64_pauth, s)) {
1570         return false;
1571     }
1572     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1573     gen_a64_set_pc(s, dst);
1574     set_btype_for_br(s, a->rn);
1575     s->base.is_jmp = DISAS_JUMP;
1576     return true;
1577 }
1578 
1579 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1580 {
1581     TCGv_i64 dst, lr;
1582 
1583     if (!dc_isar_feature(aa64_pauth, s)) {
1584         return false;
1585     }
1586     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1587     lr = cpu_reg(s, 30);
1588     if (dst == lr) {
1589         TCGv_i64 tmp = tcg_temp_new_i64();
1590         tcg_gen_mov_i64(tmp, dst);
1591         dst = tmp;
1592     }
1593     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1594     gen_a64_set_pc(s, dst);
1595     set_btype_for_blr(s);
1596     s->base.is_jmp = DISAS_JUMP;
1597     return true;
1598 }
1599 
1600 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1601 {
1602     TCGv_i64 dst;
1603 
1604     if (s->current_el == 0) {
1605         return false;
1606     }
1607     if (s->fgt_eret) {
1608         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1609         return true;
1610     }
1611     dst = tcg_temp_new_i64();
1612     tcg_gen_ld_i64(dst, cpu_env,
1613                    offsetof(CPUARMState, elr_el[s->current_el]));
1614 
1615     translator_io_start(&s->base);
1616 
1617     gen_helper_exception_return(cpu_env, dst);
1618     /* Must exit loop to check un-masked IRQs */
1619     s->base.is_jmp = DISAS_EXIT;
1620     return true;
1621 }
1622 
1623 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1624 {
1625     TCGv_i64 dst;
1626 
1627     if (!dc_isar_feature(aa64_pauth, s)) {
1628         return false;
1629     }
1630     if (s->current_el == 0) {
1631         return false;
1632     }
1633     /* The FGT trap takes precedence over an auth trap. */
1634     if (s->fgt_eret) {
1635         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1636         return true;
1637     }
1638     dst = tcg_temp_new_i64();
1639     tcg_gen_ld_i64(dst, cpu_env,
1640                    offsetof(CPUARMState, elr_el[s->current_el]));
1641 
1642     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1643 
1644     translator_io_start(&s->base);
1645 
1646     gen_helper_exception_return(cpu_env, dst);
1647     /* Must exit loop to check un-masked IRQs */
1648     s->base.is_jmp = DISAS_EXIT;
1649     return true;
1650 }
1651 
1652 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1653 {
1654     return true;
1655 }
1656 
1657 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1658 {
1659     /*
1660      * When running in MTTCG we don't generate jumps to the yield and
1661      * WFE helpers as it won't affect the scheduling of other vCPUs.
1662      * If we wanted to more completely model WFE/SEV so we don't busy
1663      * spin unnecessarily we would need to do something more involved.
1664      */
1665     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1666         s->base.is_jmp = DISAS_YIELD;
1667     }
1668     return true;
1669 }
1670 
1671 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1672 {
1673     s->base.is_jmp = DISAS_WFI;
1674     return true;
1675 }
1676 
1677 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1678 {
1679     /*
1680      * When running in MTTCG we don't generate jumps to the yield and
1681      * WFE helpers as it won't affect the scheduling of other vCPUs.
1682      * If we wanted to more completely model WFE/SEV so we don't busy
1683      * spin unnecessarily we would need to do something more involved.
1684      */
1685     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1686         s->base.is_jmp = DISAS_WFE;
1687     }
1688     return true;
1689 }
1690 
1691 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1692 {
1693     if (s->pauth_active) {
1694         gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1695     }
1696     return true;
1697 }
1698 
1699 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1700 {
1701     if (s->pauth_active) {
1702         gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1703     }
1704     return true;
1705 }
1706 
1707 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1708 {
1709     if (s->pauth_active) {
1710         gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1711     }
1712     return true;
1713 }
1714 
1715 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1716 {
1717     if (s->pauth_active) {
1718         gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1719     }
1720     return true;
1721 }
1722 
1723 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1724 {
1725     if (s->pauth_active) {
1726         gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1727     }
1728     return true;
1729 }
1730 
1731 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1732 {
1733     /* Without RAS, we must implement this as NOP. */
1734     if (dc_isar_feature(aa64_ras, s)) {
1735         /*
1736          * QEMU does not have a source of physical SErrors,
1737          * so we are only concerned with virtual SErrors.
1738          * The pseudocode in the ARM for this case is
1739          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1740          *      AArch64.vESBOperation();
1741          * Most of the condition can be evaluated at translation time.
1742          * Test for EL2 present, and defer test for SEL2 to runtime.
1743          */
1744         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1745             gen_helper_vesb(cpu_env);
1746         }
1747     }
1748     return true;
1749 }
1750 
1751 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1752 {
1753     if (s->pauth_active) {
1754         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1755     }
1756     return true;
1757 }
1758 
1759 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1760 {
1761     if (s->pauth_active) {
1762         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1763     }
1764     return true;
1765 }
1766 
1767 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1768 {
1769     if (s->pauth_active) {
1770         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1771     }
1772     return true;
1773 }
1774 
1775 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1776 {
1777     if (s->pauth_active) {
1778         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1779     }
1780     return true;
1781 }
1782 
1783 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1784 {
1785     if (s->pauth_active) {
1786         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1787     }
1788     return true;
1789 }
1790 
1791 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1792 {
1793     if (s->pauth_active) {
1794         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1795     }
1796     return true;
1797 }
1798 
1799 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1800 {
1801     if (s->pauth_active) {
1802         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1803     }
1804     return true;
1805 }
1806 
1807 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1808 {
1809     if (s->pauth_active) {
1810         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1811     }
1812     return true;
1813 }
1814 
1815 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1816 {
1817     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1818     return true;
1819 }
1820 
1821 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1822 {
1823     /* We handle DSB and DMB the same way */
1824     TCGBar bar;
1825 
1826     switch (a->types) {
1827     case 1: /* MBReqTypes_Reads */
1828         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1829         break;
1830     case 2: /* MBReqTypes_Writes */
1831         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1832         break;
1833     default: /* MBReqTypes_All */
1834         bar = TCG_BAR_SC | TCG_MO_ALL;
1835         break;
1836     }
1837     tcg_gen_mb(bar);
1838     return true;
1839 }
1840 
1841 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1842 {
1843     /*
1844      * We need to break the TB after this insn to execute
1845      * self-modifying code correctly and also to take
1846      * any pending interrupts immediately.
1847      */
1848     reset_btype(s);
1849     gen_goto_tb(s, 0, 4);
1850     return true;
1851 }
1852 
1853 static bool trans_SB(DisasContext *s, arg_SB *a)
1854 {
1855     if (!dc_isar_feature(aa64_sb, s)) {
1856         return false;
1857     }
1858     /*
1859      * TODO: There is no speculation barrier opcode for TCG;
1860      * MB and end the TB instead.
1861      */
1862     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1863     gen_goto_tb(s, 0, 4);
1864     return true;
1865 }
1866 
1867 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1868 {
1869     if (!dc_isar_feature(aa64_condm_4, s)) {
1870         return false;
1871     }
1872     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1873     return true;
1874 }
1875 
1876 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1877 {
1878     TCGv_i32 z;
1879 
1880     if (!dc_isar_feature(aa64_condm_5, s)) {
1881         return false;
1882     }
1883 
1884     z = tcg_temp_new_i32();
1885 
1886     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1887 
1888     /*
1889      * (!C & !Z) << 31
1890      * (!(C | Z)) << 31
1891      * ~((C | Z) << 31)
1892      * ~-(C | Z)
1893      * (C | Z) - 1
1894      */
1895     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1896     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1897 
1898     /* !(Z & C) */
1899     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1900     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1901 
1902     /* (!C & Z) << 31 -> -(Z & ~C) */
1903     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1904     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1905 
1906     /* C | Z */
1907     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1908 
1909     return true;
1910 }
1911 
1912 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1913 {
1914     if (!dc_isar_feature(aa64_condm_5, s)) {
1915         return false;
1916     }
1917 
1918     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1919     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1920 
1921     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1922     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1923 
1924     tcg_gen_movi_i32(cpu_NF, 0);
1925     tcg_gen_movi_i32(cpu_VF, 0);
1926 
1927     return true;
1928 }
1929 
1930 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1931 {
1932     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1933         return false;
1934     }
1935     if (a->imm & 1) {
1936         set_pstate_bits(PSTATE_UAO);
1937     } else {
1938         clear_pstate_bits(PSTATE_UAO);
1939     }
1940     gen_rebuild_hflags(s);
1941     s->base.is_jmp = DISAS_TOO_MANY;
1942     return true;
1943 }
1944 
1945 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1946 {
1947     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1948         return false;
1949     }
1950     if (a->imm & 1) {
1951         set_pstate_bits(PSTATE_PAN);
1952     } else {
1953         clear_pstate_bits(PSTATE_PAN);
1954     }
1955     gen_rebuild_hflags(s);
1956     s->base.is_jmp = DISAS_TOO_MANY;
1957     return true;
1958 }
1959 
1960 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1961 {
1962     if (s->current_el == 0) {
1963         return false;
1964     }
1965     gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(a->imm & PSTATE_SP));
1966     s->base.is_jmp = DISAS_TOO_MANY;
1967     return true;
1968 }
1969 
1970 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
1971 {
1972     if (!dc_isar_feature(aa64_ssbs, s)) {
1973         return false;
1974     }
1975     if (a->imm & 1) {
1976         set_pstate_bits(PSTATE_SSBS);
1977     } else {
1978         clear_pstate_bits(PSTATE_SSBS);
1979     }
1980     /* Don't need to rebuild hflags since SSBS is a nop */
1981     s->base.is_jmp = DISAS_TOO_MANY;
1982     return true;
1983 }
1984 
1985 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
1986 {
1987     if (!dc_isar_feature(aa64_dit, s)) {
1988         return false;
1989     }
1990     if (a->imm & 1) {
1991         set_pstate_bits(PSTATE_DIT);
1992     } else {
1993         clear_pstate_bits(PSTATE_DIT);
1994     }
1995     /* There's no need to rebuild hflags because DIT is a nop */
1996     s->base.is_jmp = DISAS_TOO_MANY;
1997     return true;
1998 }
1999 
2000 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2001 {
2002     if (dc_isar_feature(aa64_mte, s)) {
2003         /* Full MTE is enabled -- set the TCO bit as directed. */
2004         if (a->imm & 1) {
2005             set_pstate_bits(PSTATE_TCO);
2006         } else {
2007             clear_pstate_bits(PSTATE_TCO);
2008         }
2009         gen_rebuild_hflags(s);
2010         /* Many factors, including TCO, go into MTE_ACTIVE. */
2011         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2012         return true;
2013     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2014         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2015         return true;
2016     } else {
2017         /* Insn not present */
2018         return false;
2019     }
2020 }
2021 
2022 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2023 {
2024     gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(a->imm));
2025     s->base.is_jmp = DISAS_TOO_MANY;
2026     return true;
2027 }
2028 
2029 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2030 {
2031     gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(a->imm));
2032     /* Exit the cpu loop to re-evaluate pending IRQs. */
2033     s->base.is_jmp = DISAS_UPDATE_EXIT;
2034     return true;
2035 }
2036 
2037 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2038 {
2039     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2040         return false;
2041     }
2042     if (sme_access_check(s)) {
2043         int old = s->pstate_sm | (s->pstate_za << 1);
2044         int new = a->imm * 3;
2045 
2046         if ((old ^ new) & a->mask) {
2047             /* At least one bit changes. */
2048             gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2049                                 tcg_constant_i32(a->mask));
2050             s->base.is_jmp = DISAS_TOO_MANY;
2051         }
2052     }
2053     return true;
2054 }
2055 
2056 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2057 {
2058     TCGv_i32 tmp = tcg_temp_new_i32();
2059     TCGv_i32 nzcv = tcg_temp_new_i32();
2060 
2061     /* build bit 31, N */
2062     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2063     /* build bit 30, Z */
2064     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2065     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2066     /* build bit 29, C */
2067     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2068     /* build bit 28, V */
2069     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2070     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2071     /* generate result */
2072     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2073 }
2074 
2075 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2076 {
2077     TCGv_i32 nzcv = tcg_temp_new_i32();
2078 
2079     /* take NZCV from R[t] */
2080     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2081 
2082     /* bit 31, N */
2083     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2084     /* bit 30, Z */
2085     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2086     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2087     /* bit 29, C */
2088     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2089     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2090     /* bit 28, V */
2091     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2092     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2093 }
2094 
2095 static void gen_sysreg_undef(DisasContext *s, bool isread,
2096                              uint8_t op0, uint8_t op1, uint8_t op2,
2097                              uint8_t crn, uint8_t crm, uint8_t rt)
2098 {
2099     /*
2100      * Generate code to emit an UNDEF with correct syndrome
2101      * information for a failed system register access.
2102      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2103      * but if FEAT_IDST is implemented then read accesses to registers
2104      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2105      * syndrome.
2106      */
2107     uint32_t syndrome;
2108 
2109     if (isread && dc_isar_feature(aa64_ids, s) &&
2110         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2111         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2112     } else {
2113         syndrome = syn_uncategorized();
2114     }
2115     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2116 }
2117 
2118 /* MRS - move from system register
2119  * MSR (register) - move to system register
2120  * SYS
2121  * SYSL
2122  * These are all essentially the same insn in 'read' and 'write'
2123  * versions, with varying op0 fields.
2124  */
2125 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2126                        unsigned int op0, unsigned int op1, unsigned int op2,
2127                        unsigned int crn, unsigned int crm, unsigned int rt)
2128 {
2129     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2130                                       crn, crm, op0, op1, op2);
2131     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2132     bool need_exit_tb = false;
2133     TCGv_ptr tcg_ri = NULL;
2134     TCGv_i64 tcg_rt;
2135 
2136     if (!ri) {
2137         /* Unknown register; this might be a guest error or a QEMU
2138          * unimplemented feature.
2139          */
2140         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2141                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2142                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2143         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2144         return;
2145     }
2146 
2147     /* Check access permissions */
2148     if (!cp_access_ok(s->current_el, ri, isread)) {
2149         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2150         return;
2151     }
2152 
2153     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2154         /* Emit code to perform further access permissions checks at
2155          * runtime; this may result in an exception.
2156          */
2157         uint32_t syndrome;
2158 
2159         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2160         gen_a64_update_pc(s, 0);
2161         tcg_ri = tcg_temp_new_ptr();
2162         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2163                                        tcg_constant_i32(key),
2164                                        tcg_constant_i32(syndrome),
2165                                        tcg_constant_i32(isread));
2166     } else if (ri->type & ARM_CP_RAISES_EXC) {
2167         /*
2168          * The readfn or writefn might raise an exception;
2169          * synchronize the CPU state in case it does.
2170          */
2171         gen_a64_update_pc(s, 0);
2172     }
2173 
2174     /* Handle special cases first */
2175     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2176     case 0:
2177         break;
2178     case ARM_CP_NOP:
2179         return;
2180     case ARM_CP_NZCV:
2181         tcg_rt = cpu_reg(s, rt);
2182         if (isread) {
2183             gen_get_nzcv(tcg_rt);
2184         } else {
2185             gen_set_nzcv(tcg_rt);
2186         }
2187         return;
2188     case ARM_CP_CURRENTEL:
2189         /* Reads as current EL value from pstate, which is
2190          * guaranteed to be constant by the tb flags.
2191          */
2192         tcg_rt = cpu_reg(s, rt);
2193         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2194         return;
2195     case ARM_CP_DC_ZVA:
2196         /* Writes clear the aligned block of memory which rt points into. */
2197         if (s->mte_active[0]) {
2198             int desc = 0;
2199 
2200             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2201             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2202             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2203 
2204             tcg_rt = tcg_temp_new_i64();
2205             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2206                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2207         } else {
2208             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2209         }
2210         gen_helper_dc_zva(cpu_env, tcg_rt);
2211         return;
2212     case ARM_CP_DC_GVA:
2213         {
2214             TCGv_i64 clean_addr, tag;
2215 
2216             /*
2217              * DC_GVA, like DC_ZVA, requires that we supply the original
2218              * pointer for an invalid page.  Probe that address first.
2219              */
2220             tcg_rt = cpu_reg(s, rt);
2221             clean_addr = clean_data_tbi(s, tcg_rt);
2222             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2223 
2224             if (s->ata) {
2225                 /* Extract the tag from the register to match STZGM.  */
2226                 tag = tcg_temp_new_i64();
2227                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2228                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2229             }
2230         }
2231         return;
2232     case ARM_CP_DC_GZVA:
2233         {
2234             TCGv_i64 clean_addr, tag;
2235 
2236             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2237             tcg_rt = cpu_reg(s, rt);
2238             clean_addr = clean_data_tbi(s, tcg_rt);
2239             gen_helper_dc_zva(cpu_env, clean_addr);
2240 
2241             if (s->ata) {
2242                 /* Extract the tag from the register to match STZGM.  */
2243                 tag = tcg_temp_new_i64();
2244                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2245                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2246             }
2247         }
2248         return;
2249     default:
2250         g_assert_not_reached();
2251     }
2252     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2253         return;
2254     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2255         return;
2256     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2257         return;
2258     }
2259 
2260     if (ri->type & ARM_CP_IO) {
2261         /* I/O operations must end the TB here (whether read or write) */
2262         need_exit_tb = translator_io_start(&s->base);
2263     }
2264 
2265     tcg_rt = cpu_reg(s, rt);
2266 
2267     if (isread) {
2268         if (ri->type & ARM_CP_CONST) {
2269             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2270         } else if (ri->readfn) {
2271             if (!tcg_ri) {
2272                 tcg_ri = gen_lookup_cp_reg(key);
2273             }
2274             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2275         } else {
2276             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2277         }
2278     } else {
2279         if (ri->type & ARM_CP_CONST) {
2280             /* If not forbidden by access permissions, treat as WI */
2281             return;
2282         } else if (ri->writefn) {
2283             if (!tcg_ri) {
2284                 tcg_ri = gen_lookup_cp_reg(key);
2285             }
2286             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2287         } else {
2288             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2289         }
2290     }
2291 
2292     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2293         /*
2294          * A write to any coprocessor regiser that ends a TB
2295          * must rebuild the hflags for the next TB.
2296          */
2297         gen_rebuild_hflags(s);
2298         /*
2299          * We default to ending the TB on a coprocessor register write,
2300          * but allow this to be suppressed by the register definition
2301          * (usually only necessary to work around guest bugs).
2302          */
2303         need_exit_tb = true;
2304     }
2305     if (need_exit_tb) {
2306         s->base.is_jmp = DISAS_UPDATE_EXIT;
2307     }
2308 }
2309 
2310 /* System
2311  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2312  * +---------------------+---+-----+-----+-------+-------+-----+------+
2313  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2314  * +---------------------+---+-----+-----+-------+-------+-----+------+
2315  */
2316 static void disas_system(DisasContext *s, uint32_t insn)
2317 {
2318     unsigned int l, op0, op1, crn, crm, op2, rt;
2319     l = extract32(insn, 21, 1);
2320     op0 = extract32(insn, 19, 2);
2321     op1 = extract32(insn, 16, 3);
2322     crn = extract32(insn, 12, 4);
2323     crm = extract32(insn, 8, 4);
2324     op2 = extract32(insn, 5, 3);
2325     rt = extract32(insn, 0, 5);
2326 
2327     if (op0 == 0) {
2328         unallocated_encoding(s);
2329         return;
2330     }
2331     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2332 }
2333 
2334 /* Exception generation
2335  *
2336  *  31             24 23 21 20                     5 4   2 1  0
2337  * +-----------------+-----+------------------------+-----+----+
2338  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2339  * +-----------------------+------------------------+----------+
2340  */
2341 static void disas_exc(DisasContext *s, uint32_t insn)
2342 {
2343     int opc = extract32(insn, 21, 3);
2344     int op2_ll = extract32(insn, 0, 5);
2345     int imm16 = extract32(insn, 5, 16);
2346     uint32_t syndrome;
2347 
2348     switch (opc) {
2349     case 0:
2350         /* For SVC, HVC and SMC we advance the single-step state
2351          * machine before taking the exception. This is architecturally
2352          * mandated, to ensure that single-stepping a system call
2353          * instruction works properly.
2354          */
2355         switch (op2_ll) {
2356         case 1:                                                     /* SVC */
2357             syndrome = syn_aa64_svc(imm16);
2358             if (s->fgt_svc) {
2359                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2360                 break;
2361             }
2362             gen_ss_advance(s);
2363             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2364             break;
2365         case 2:                                                     /* HVC */
2366             if (s->current_el == 0) {
2367                 unallocated_encoding(s);
2368                 break;
2369             }
2370             /* The pre HVC helper handles cases when HVC gets trapped
2371              * as an undefined insn by runtime configuration.
2372              */
2373             gen_a64_update_pc(s, 0);
2374             gen_helper_pre_hvc(cpu_env);
2375             gen_ss_advance(s);
2376             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2377             break;
2378         case 3:                                                     /* SMC */
2379             if (s->current_el == 0) {
2380                 unallocated_encoding(s);
2381                 break;
2382             }
2383             gen_a64_update_pc(s, 0);
2384             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2385             gen_ss_advance(s);
2386             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2387             break;
2388         default:
2389             unallocated_encoding(s);
2390             break;
2391         }
2392         break;
2393     case 1:
2394         if (op2_ll != 0) {
2395             unallocated_encoding(s);
2396             break;
2397         }
2398         /* BRK */
2399         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2400         break;
2401     case 2:
2402         if (op2_ll != 0) {
2403             unallocated_encoding(s);
2404             break;
2405         }
2406         /* HLT. This has two purposes.
2407          * Architecturally, it is an external halting debug instruction.
2408          * Since QEMU doesn't implement external debug, we treat this as
2409          * it is required for halting debug disabled: it will UNDEF.
2410          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2411          */
2412         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2413             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2414         } else {
2415             unallocated_encoding(s);
2416         }
2417         break;
2418     case 5:
2419         if (op2_ll < 1 || op2_ll > 3) {
2420             unallocated_encoding(s);
2421             break;
2422         }
2423         /* DCPS1, DCPS2, DCPS3 */
2424         unallocated_encoding(s);
2425         break;
2426     default:
2427         unallocated_encoding(s);
2428         break;
2429     }
2430 }
2431 
2432 /* Branches, exception generating and system instructions */
2433 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2434 {
2435     switch (extract32(insn, 25, 7)) {
2436     case 0x6a: /* Exception generation / System */
2437         if (insn & (1 << 24)) {
2438             if (extract32(insn, 22, 2) == 0) {
2439                 disas_system(s, insn);
2440             } else {
2441                 unallocated_encoding(s);
2442             }
2443         } else {
2444             disas_exc(s, insn);
2445         }
2446         break;
2447     default:
2448         unallocated_encoding(s);
2449         break;
2450     }
2451 }
2452 
2453 /*
2454  * Load/Store exclusive instructions are implemented by remembering
2455  * the value/address loaded, and seeing if these are the same
2456  * when the store is performed. This is not actually the architecturally
2457  * mandated semantics, but it works for typical guest code sequences
2458  * and avoids having to monitor regular stores.
2459  *
2460  * The store exclusive uses the atomic cmpxchg primitives to avoid
2461  * races in multi-threaded linux-user and when MTTCG softmmu is
2462  * enabled.
2463  */
2464 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2465                                int size, bool is_pair)
2466 {
2467     int idx = get_mem_index(s);
2468     TCGv_i64 dirty_addr, clean_addr;
2469     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2470 
2471     s->is_ldex = true;
2472     dirty_addr = cpu_reg_sp(s, rn);
2473     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2474 
2475     g_assert(size <= 3);
2476     if (is_pair) {
2477         g_assert(size >= 2);
2478         if (size == 2) {
2479             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2480             if (s->be_data == MO_LE) {
2481                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2482                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2483             } else {
2484                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2485                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2486             }
2487         } else {
2488             TCGv_i128 t16 = tcg_temp_new_i128();
2489 
2490             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2491 
2492             if (s->be_data == MO_LE) {
2493                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2494                                       cpu_exclusive_high, t16);
2495             } else {
2496                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2497                                       cpu_exclusive_val, t16);
2498             }
2499             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2500             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2501         }
2502     } else {
2503         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2504         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2505     }
2506     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2507 }
2508 
2509 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2510                                 int rn, int size, int is_pair)
2511 {
2512     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2513      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2514      *     [addr] = {Rt};
2515      *     if (is_pair) {
2516      *         [addr + datasize] = {Rt2};
2517      *     }
2518      *     {Rd} = 0;
2519      * } else {
2520      *     {Rd} = 1;
2521      * }
2522      * env->exclusive_addr = -1;
2523      */
2524     TCGLabel *fail_label = gen_new_label();
2525     TCGLabel *done_label = gen_new_label();
2526     TCGv_i64 tmp, clean_addr;
2527     MemOp memop;
2528 
2529     /*
2530      * FIXME: We are out of spec here.  We have recorded only the address
2531      * from load_exclusive, not the entire range, and we assume that the
2532      * size of the access on both sides match.  The architecture allows the
2533      * store to be smaller than the load, so long as the stored bytes are
2534      * within the range recorded by the load.
2535      */
2536 
2537     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2538     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2539     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2540 
2541     /*
2542      * The write, and any associated faults, only happen if the virtual
2543      * and physical addresses pass the exclusive monitor check.  These
2544      * faults are exceedingly unlikely, because normally the guest uses
2545      * the exact same address register for the load_exclusive, and we
2546      * would have recognized these faults there.
2547      *
2548      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2549      * unaligned 4-byte write within the range of an aligned 8-byte load.
2550      * With LSE2, the store would need to cross a 16-byte boundary when the
2551      * load did not, which would mean the store is outside the range
2552      * recorded for the monitor, which would have failed a corrected monitor
2553      * check above.  For now, we assume no size change and retain the
2554      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2555      *
2556      * It is possible to trigger an MTE fault, by performing the load with
2557      * a virtual address with a valid tag and performing the store with the
2558      * same virtual address and a different invalid tag.
2559      */
2560     memop = size + is_pair;
2561     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2562         memop |= MO_ALIGN;
2563     }
2564     memop = finalize_memop(s, memop);
2565     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2566 
2567     tmp = tcg_temp_new_i64();
2568     if (is_pair) {
2569         if (size == 2) {
2570             if (s->be_data == MO_LE) {
2571                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2572             } else {
2573                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2574             }
2575             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2576                                        cpu_exclusive_val, tmp,
2577                                        get_mem_index(s), memop);
2578             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2579         } else {
2580             TCGv_i128 t16 = tcg_temp_new_i128();
2581             TCGv_i128 c16 = tcg_temp_new_i128();
2582             TCGv_i64 a, b;
2583 
2584             if (s->be_data == MO_LE) {
2585                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2586                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2587                                         cpu_exclusive_high);
2588             } else {
2589                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2590                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2591                                         cpu_exclusive_val);
2592             }
2593 
2594             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2595                                         get_mem_index(s), memop);
2596 
2597             a = tcg_temp_new_i64();
2598             b = tcg_temp_new_i64();
2599             if (s->be_data == MO_LE) {
2600                 tcg_gen_extr_i128_i64(a, b, t16);
2601             } else {
2602                 tcg_gen_extr_i128_i64(b, a, t16);
2603             }
2604 
2605             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2606             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2607             tcg_gen_or_i64(tmp, a, b);
2608 
2609             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2610         }
2611     } else {
2612         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2613                                    cpu_reg(s, rt), get_mem_index(s), memop);
2614         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2615     }
2616     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2617     tcg_gen_br(done_label);
2618 
2619     gen_set_label(fail_label);
2620     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2621     gen_set_label(done_label);
2622     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2623 }
2624 
2625 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2626                                  int rn, int size)
2627 {
2628     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2629     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2630     int memidx = get_mem_index(s);
2631     TCGv_i64 clean_addr;
2632     MemOp memop;
2633 
2634     if (rn == 31) {
2635         gen_check_sp_alignment(s);
2636     }
2637     memop = check_atomic_align(s, rn, size);
2638     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2639     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2640                                memidx, memop);
2641 }
2642 
2643 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2644                                       int rn, int size)
2645 {
2646     TCGv_i64 s1 = cpu_reg(s, rs);
2647     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2648     TCGv_i64 t1 = cpu_reg(s, rt);
2649     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2650     TCGv_i64 clean_addr;
2651     int memidx = get_mem_index(s);
2652     MemOp memop;
2653 
2654     if (rn == 31) {
2655         gen_check_sp_alignment(s);
2656     }
2657 
2658     /* This is a single atomic access, despite the "pair". */
2659     memop = check_atomic_align(s, rn, size + 1);
2660     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2661 
2662     if (size == 2) {
2663         TCGv_i64 cmp = tcg_temp_new_i64();
2664         TCGv_i64 val = tcg_temp_new_i64();
2665 
2666         if (s->be_data == MO_LE) {
2667             tcg_gen_concat32_i64(val, t1, t2);
2668             tcg_gen_concat32_i64(cmp, s1, s2);
2669         } else {
2670             tcg_gen_concat32_i64(val, t2, t1);
2671             tcg_gen_concat32_i64(cmp, s2, s1);
2672         }
2673 
2674         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2675 
2676         if (s->be_data == MO_LE) {
2677             tcg_gen_extr32_i64(s1, s2, cmp);
2678         } else {
2679             tcg_gen_extr32_i64(s2, s1, cmp);
2680         }
2681     } else {
2682         TCGv_i128 cmp = tcg_temp_new_i128();
2683         TCGv_i128 val = tcg_temp_new_i128();
2684 
2685         if (s->be_data == MO_LE) {
2686             tcg_gen_concat_i64_i128(val, t1, t2);
2687             tcg_gen_concat_i64_i128(cmp, s1, s2);
2688         } else {
2689             tcg_gen_concat_i64_i128(val, t2, t1);
2690             tcg_gen_concat_i64_i128(cmp, s2, s1);
2691         }
2692 
2693         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2694 
2695         if (s->be_data == MO_LE) {
2696             tcg_gen_extr_i128_i64(s1, s2, cmp);
2697         } else {
2698             tcg_gen_extr_i128_i64(s2, s1, cmp);
2699         }
2700     }
2701 }
2702 
2703 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2704  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2705  */
2706 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2707 {
2708     int opc0 = extract32(opc, 0, 1);
2709     int regsize;
2710 
2711     if (is_signed) {
2712         regsize = opc0 ? 32 : 64;
2713     } else {
2714         regsize = size == 3 ? 64 : 32;
2715     }
2716     return regsize == 64;
2717 }
2718 
2719 /* Load/store exclusive
2720  *
2721  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2722  * +-----+-------------+----+---+----+------+----+-------+------+------+
2723  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2724  * +-----+-------------+----+---+----+------+----+-------+------+------+
2725  *
2726  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2727  *   L: 0 -> store, 1 -> load
2728  *  o2: 0 -> exclusive, 1 -> not
2729  *  o1: 0 -> single register, 1 -> register pair
2730  *  o0: 1 -> load-acquire/store-release, 0 -> not
2731  */
2732 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2733 {
2734     int rt = extract32(insn, 0, 5);
2735     int rn = extract32(insn, 5, 5);
2736     int rt2 = extract32(insn, 10, 5);
2737     int rs = extract32(insn, 16, 5);
2738     int is_lasr = extract32(insn, 15, 1);
2739     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2740     int size = extract32(insn, 30, 2);
2741     TCGv_i64 clean_addr;
2742     MemOp memop;
2743 
2744     switch (o2_L_o1_o0) {
2745     case 0x0: /* STXR */
2746     case 0x1: /* STLXR */
2747         if (rn == 31) {
2748             gen_check_sp_alignment(s);
2749         }
2750         if (is_lasr) {
2751             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2752         }
2753         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2754         return;
2755 
2756     case 0x4: /* LDXR */
2757     case 0x5: /* LDAXR */
2758         if (rn == 31) {
2759             gen_check_sp_alignment(s);
2760         }
2761         gen_load_exclusive(s, rt, rt2, rn, size, false);
2762         if (is_lasr) {
2763             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2764         }
2765         return;
2766 
2767     case 0x8: /* STLLR */
2768         if (!dc_isar_feature(aa64_lor, s)) {
2769             break;
2770         }
2771         /* StoreLORelease is the same as Store-Release for QEMU.  */
2772         /* fall through */
2773     case 0x9: /* STLR */
2774         /* Generate ISS for non-exclusive accesses including LASR.  */
2775         if (rn == 31) {
2776             gen_check_sp_alignment(s);
2777         }
2778         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2779         memop = check_ordered_align(s, rn, 0, true, size);
2780         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2781                                     true, rn != 31, memop);
2782         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2783                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2784         return;
2785 
2786     case 0xc: /* LDLAR */
2787         if (!dc_isar_feature(aa64_lor, s)) {
2788             break;
2789         }
2790         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2791         /* fall through */
2792     case 0xd: /* LDAR */
2793         /* Generate ISS for non-exclusive accesses including LASR.  */
2794         if (rn == 31) {
2795             gen_check_sp_alignment(s);
2796         }
2797         memop = check_ordered_align(s, rn, 0, false, size);
2798         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2799                                     false, rn != 31, memop);
2800         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2801                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2802         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2803         return;
2804 
2805     case 0x2: case 0x3: /* CASP / STXP */
2806         if (size & 2) { /* STXP / STLXP */
2807             if (rn == 31) {
2808                 gen_check_sp_alignment(s);
2809             }
2810             if (is_lasr) {
2811                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2812             }
2813             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2814             return;
2815         }
2816         if (rt2 == 31
2817             && ((rt | rs) & 1) == 0
2818             && dc_isar_feature(aa64_atomics, s)) {
2819             /* CASP / CASPL */
2820             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2821             return;
2822         }
2823         break;
2824 
2825     case 0x6: case 0x7: /* CASPA / LDXP */
2826         if (size & 2) { /* LDXP / LDAXP */
2827             if (rn == 31) {
2828                 gen_check_sp_alignment(s);
2829             }
2830             gen_load_exclusive(s, rt, rt2, rn, size, true);
2831             if (is_lasr) {
2832                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2833             }
2834             return;
2835         }
2836         if (rt2 == 31
2837             && ((rt | rs) & 1) == 0
2838             && dc_isar_feature(aa64_atomics, s)) {
2839             /* CASPA / CASPAL */
2840             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2841             return;
2842         }
2843         break;
2844 
2845     case 0xa: /* CAS */
2846     case 0xb: /* CASL */
2847     case 0xe: /* CASA */
2848     case 0xf: /* CASAL */
2849         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2850             gen_compare_and_swap(s, rs, rt, rn, size);
2851             return;
2852         }
2853         break;
2854     }
2855     unallocated_encoding(s);
2856 }
2857 
2858 /*
2859  * Load register (literal)
2860  *
2861  *  31 30 29   27  26 25 24 23                5 4     0
2862  * +-----+-------+---+-----+-------------------+-------+
2863  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2864  * +-----+-------+---+-----+-------------------+-------+
2865  *
2866  * V: 1 -> vector (simd/fp)
2867  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2868  *                   10-> 32 bit signed, 11 -> prefetch
2869  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2870  */
2871 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2872 {
2873     int rt = extract32(insn, 0, 5);
2874     int64_t imm = sextract32(insn, 5, 19) << 2;
2875     bool is_vector = extract32(insn, 26, 1);
2876     int opc = extract32(insn, 30, 2);
2877     bool is_signed = false;
2878     int size = 2;
2879     TCGv_i64 tcg_rt, clean_addr;
2880     MemOp memop;
2881 
2882     if (is_vector) {
2883         if (opc == 3) {
2884             unallocated_encoding(s);
2885             return;
2886         }
2887         size = 2 + opc;
2888         if (!fp_access_check(s)) {
2889             return;
2890         }
2891         memop = finalize_memop_asimd(s, size);
2892     } else {
2893         if (opc == 3) {
2894             /* PRFM (literal) : prefetch */
2895             return;
2896         }
2897         size = 2 + extract32(opc, 0, 1);
2898         is_signed = extract32(opc, 1, 1);
2899         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2900     }
2901 
2902     tcg_rt = cpu_reg(s, rt);
2903 
2904     clean_addr = tcg_temp_new_i64();
2905     gen_pc_plus_diff(s, clean_addr, imm);
2906 
2907     if (is_vector) {
2908         do_fp_ld(s, rt, clean_addr, memop);
2909     } else {
2910         /* Only unsigned 32bit loads target 32bit registers.  */
2911         bool iss_sf = opc != 0;
2912         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2913     }
2914 }
2915 
2916 /*
2917  * LDNP (Load Pair - non-temporal hint)
2918  * LDP (Load Pair - non vector)
2919  * LDPSW (Load Pair Signed Word - non vector)
2920  * STNP (Store Pair - non-temporal hint)
2921  * STP (Store Pair - non vector)
2922  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2923  * LDP (Load Pair of SIMD&FP)
2924  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2925  * STP (Store Pair of SIMD&FP)
2926  *
2927  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2928  * +-----+-------+---+---+-------+---+-----------------------------+
2929  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2930  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2931  *
2932  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2933  *      LDPSW/STGP               01
2934  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2935  *   V: 0 -> GPR, 1 -> Vector
2936  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2937  *      10 -> signed offset, 11 -> pre-index
2938  *   L: 0 -> Store 1 -> Load
2939  *
2940  * Rt, Rt2 = GPR or SIMD registers to be stored
2941  * Rn = general purpose register containing address
2942  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2943  */
2944 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2945 {
2946     int rt = extract32(insn, 0, 5);
2947     int rn = extract32(insn, 5, 5);
2948     int rt2 = extract32(insn, 10, 5);
2949     uint64_t offset = sextract64(insn, 15, 7);
2950     int index = extract32(insn, 23, 2);
2951     bool is_vector = extract32(insn, 26, 1);
2952     bool is_load = extract32(insn, 22, 1);
2953     int opc = extract32(insn, 30, 2);
2954     bool is_signed = false;
2955     bool postindex = false;
2956     bool wback = false;
2957     bool set_tag = false;
2958     TCGv_i64 clean_addr, dirty_addr;
2959     MemOp mop;
2960     int size;
2961 
2962     if (opc == 3) {
2963         unallocated_encoding(s);
2964         return;
2965     }
2966 
2967     if (is_vector) {
2968         size = 2 + opc;
2969     } else if (opc == 1 && !is_load) {
2970         /* STGP */
2971         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2972             unallocated_encoding(s);
2973             return;
2974         }
2975         size = 3;
2976         set_tag = true;
2977     } else {
2978         size = 2 + extract32(opc, 1, 1);
2979         is_signed = extract32(opc, 0, 1);
2980         if (!is_load && is_signed) {
2981             unallocated_encoding(s);
2982             return;
2983         }
2984     }
2985 
2986     switch (index) {
2987     case 1: /* post-index */
2988         postindex = true;
2989         wback = true;
2990         break;
2991     case 0:
2992         /* signed offset with "non-temporal" hint. Since we don't emulate
2993          * caches we don't care about hints to the cache system about
2994          * data access patterns, and handle this identically to plain
2995          * signed offset.
2996          */
2997         if (is_signed) {
2998             /* There is no non-temporal-hint version of LDPSW */
2999             unallocated_encoding(s);
3000             return;
3001         }
3002         postindex = false;
3003         break;
3004     case 2: /* signed offset, rn not updated */
3005         postindex = false;
3006         break;
3007     case 3: /* pre-index */
3008         postindex = false;
3009         wback = true;
3010         break;
3011     }
3012 
3013     if (is_vector && !fp_access_check(s)) {
3014         return;
3015     }
3016 
3017     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
3018 
3019     if (rn == 31) {
3020         gen_check_sp_alignment(s);
3021     }
3022 
3023     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3024     if (!postindex) {
3025         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3026     }
3027 
3028     if (set_tag) {
3029         if (!s->ata) {
3030             /*
3031              * TODO: We could rely on the stores below, at least for
3032              * system mode, if we arrange to add MO_ALIGN_16.
3033              */
3034             gen_helper_stg_stub(cpu_env, dirty_addr);
3035         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3036             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3037         } else {
3038             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3039         }
3040     }
3041 
3042     if (is_vector) {
3043         mop = finalize_memop_asimd(s, size);
3044     } else {
3045         mop = finalize_memop(s, size);
3046     }
3047     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3048                                 (wback || rn != 31) && !set_tag,
3049                                 2 << size, mop);
3050 
3051     if (is_vector) {
3052         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3053         if (is_load) {
3054             do_fp_ld(s, rt, clean_addr, mop);
3055         } else {
3056             do_fp_st(s, rt, clean_addr, mop);
3057         }
3058         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3059         if (is_load) {
3060             do_fp_ld(s, rt2, clean_addr, mop);
3061         } else {
3062             do_fp_st(s, rt2, clean_addr, mop);
3063         }
3064     } else {
3065         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3066         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3067 
3068         /*
3069          * We built mop above for the single logical access -- rebuild it
3070          * now for the paired operation.
3071          *
3072          * With LSE2, non-sign-extending pairs are treated atomically if
3073          * aligned, and if unaligned one of the pair will be completely
3074          * within a 16-byte block and that element will be atomic.
3075          * Otherwise each element is separately atomic.
3076          * In all cases, issue one operation with the correct atomicity.
3077          *
3078          * This treats sign-extending loads like zero-extending loads,
3079          * since that reuses the most code below.
3080          */
3081         mop = size + 1;
3082         if (s->align_mem) {
3083             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3084         }
3085         mop = finalize_memop_pair(s, mop);
3086 
3087         if (is_load) {
3088             if (size == 2) {
3089                 int o2 = s->be_data == MO_LE ? 32 : 0;
3090                 int o1 = o2 ^ 32;
3091 
3092                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3093                 if (is_signed) {
3094                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3095                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3096                 } else {
3097                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3098                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3099                 }
3100             } else {
3101                 TCGv_i128 tmp = tcg_temp_new_i128();
3102 
3103                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3104                 if (s->be_data == MO_LE) {
3105                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3106                 } else {
3107                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3108                 }
3109             }
3110         } else {
3111             if (size == 2) {
3112                 TCGv_i64 tmp = tcg_temp_new_i64();
3113 
3114                 if (s->be_data == MO_LE) {
3115                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3116                 } else {
3117                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3118                 }
3119                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3120             } else {
3121                 TCGv_i128 tmp = tcg_temp_new_i128();
3122 
3123                 if (s->be_data == MO_LE) {
3124                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3125                 } else {
3126                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3127                 }
3128                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3129             }
3130         }
3131     }
3132 
3133     if (wback) {
3134         if (postindex) {
3135             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3136         }
3137         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3138     }
3139 }
3140 
3141 /*
3142  * Load/store (immediate post-indexed)
3143  * Load/store (immediate pre-indexed)
3144  * Load/store (unscaled immediate)
3145  *
3146  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3147  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3148  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3149  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3150  *
3151  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3152          10 -> unprivileged
3153  * V = 0 -> non-vector
3154  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3155  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3156  */
3157 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3158                                 int opc,
3159                                 int size,
3160                                 int rt,
3161                                 bool is_vector)
3162 {
3163     int rn = extract32(insn, 5, 5);
3164     int imm9 = sextract32(insn, 12, 9);
3165     int idx = extract32(insn, 10, 2);
3166     bool is_signed = false;
3167     bool is_store = false;
3168     bool is_extended = false;
3169     bool is_unpriv = (idx == 2);
3170     bool iss_valid;
3171     bool post_index;
3172     bool writeback;
3173     int memidx;
3174     MemOp memop;
3175     TCGv_i64 clean_addr, dirty_addr;
3176 
3177     if (is_vector) {
3178         size |= (opc & 2) << 1;
3179         if (size > 4 || is_unpriv) {
3180             unallocated_encoding(s);
3181             return;
3182         }
3183         is_store = ((opc & 1) == 0);
3184         if (!fp_access_check(s)) {
3185             return;
3186         }
3187         memop = finalize_memop_asimd(s, size);
3188     } else {
3189         if (size == 3 && opc == 2) {
3190             /* PRFM - prefetch */
3191             if (idx != 0) {
3192                 unallocated_encoding(s);
3193                 return;
3194             }
3195             return;
3196         }
3197         if (opc == 3 && size > 1) {
3198             unallocated_encoding(s);
3199             return;
3200         }
3201         is_store = (opc == 0);
3202         is_signed = !is_store && extract32(opc, 1, 1);
3203         is_extended = (size < 3) && extract32(opc, 0, 1);
3204         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3205     }
3206 
3207     switch (idx) {
3208     case 0:
3209     case 2:
3210         post_index = false;
3211         writeback = false;
3212         break;
3213     case 1:
3214         post_index = true;
3215         writeback = true;
3216         break;
3217     case 3:
3218         post_index = false;
3219         writeback = true;
3220         break;
3221     default:
3222         g_assert_not_reached();
3223     }
3224 
3225     iss_valid = !is_vector && !writeback;
3226 
3227     if (rn == 31) {
3228         gen_check_sp_alignment(s);
3229     }
3230 
3231     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3232     if (!post_index) {
3233         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3234     }
3235 
3236     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3237 
3238     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3239                                        writeback || rn != 31,
3240                                        memop, is_unpriv, memidx);
3241 
3242     if (is_vector) {
3243         if (is_store) {
3244             do_fp_st(s, rt, clean_addr, memop);
3245         } else {
3246             do_fp_ld(s, rt, clean_addr, memop);
3247         }
3248     } else {
3249         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3250         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3251 
3252         if (is_store) {
3253             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3254                              iss_valid, rt, iss_sf, false);
3255         } else {
3256             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3257                              is_extended, memidx,
3258                              iss_valid, rt, iss_sf, false);
3259         }
3260     }
3261 
3262     if (writeback) {
3263         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3264         if (post_index) {
3265             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3266         }
3267         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3268     }
3269 }
3270 
3271 /*
3272  * Load/store (register offset)
3273  *
3274  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3275  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3276  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3277  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3278  *
3279  * For non-vector:
3280  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3281  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3282  * For vector:
3283  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3284  *   opc<0>: 0 -> store, 1 -> load
3285  * V: 1 -> vector/simd
3286  * opt: extend encoding (see DecodeRegExtend)
3287  * S: if S=1 then scale (essentially index by sizeof(size))
3288  * Rt: register to transfer into/out of
3289  * Rn: address register or SP for base
3290  * Rm: offset register or ZR for offset
3291  */
3292 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3293                                    int opc,
3294                                    int size,
3295                                    int rt,
3296                                    bool is_vector)
3297 {
3298     int rn = extract32(insn, 5, 5);
3299     int shift = extract32(insn, 12, 1);
3300     int rm = extract32(insn, 16, 5);
3301     int opt = extract32(insn, 13, 3);
3302     bool is_signed = false;
3303     bool is_store = false;
3304     bool is_extended = false;
3305     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3306     MemOp memop;
3307 
3308     if (extract32(opt, 1, 1) == 0) {
3309         unallocated_encoding(s);
3310         return;
3311     }
3312 
3313     if (is_vector) {
3314         size |= (opc & 2) << 1;
3315         if (size > 4) {
3316             unallocated_encoding(s);
3317             return;
3318         }
3319         is_store = !extract32(opc, 0, 1);
3320         if (!fp_access_check(s)) {
3321             return;
3322         }
3323         memop = finalize_memop_asimd(s, size);
3324     } else {
3325         if (size == 3 && opc == 2) {
3326             /* PRFM - prefetch */
3327             return;
3328         }
3329         if (opc == 3 && size > 1) {
3330             unallocated_encoding(s);
3331             return;
3332         }
3333         is_store = (opc == 0);
3334         is_signed = !is_store && extract32(opc, 1, 1);
3335         is_extended = (size < 3) && extract32(opc, 0, 1);
3336         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3337     }
3338 
3339     if (rn == 31) {
3340         gen_check_sp_alignment(s);
3341     }
3342     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3343 
3344     tcg_rm = read_cpu_reg(s, rm, 1);
3345     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3346 
3347     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3348 
3349     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3350 
3351     if (is_vector) {
3352         if (is_store) {
3353             do_fp_st(s, rt, clean_addr, memop);
3354         } else {
3355             do_fp_ld(s, rt, clean_addr, memop);
3356         }
3357     } else {
3358         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3359         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3360 
3361         if (is_store) {
3362             do_gpr_st(s, tcg_rt, clean_addr, memop,
3363                       true, rt, iss_sf, false);
3364         } else {
3365             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3366                       is_extended, true, rt, iss_sf, false);
3367         }
3368     }
3369 }
3370 
3371 /*
3372  * Load/store (unsigned immediate)
3373  *
3374  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3375  * +----+-------+---+-----+-----+------------+-------+------+
3376  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3377  * +----+-------+---+-----+-----+------------+-------+------+
3378  *
3379  * For non-vector:
3380  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3381  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3382  * For vector:
3383  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3384  *   opc<0>: 0 -> store, 1 -> load
3385  * Rn: base address register (inc SP)
3386  * Rt: target register
3387  */
3388 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3389                                         int opc,
3390                                         int size,
3391                                         int rt,
3392                                         bool is_vector)
3393 {
3394     int rn = extract32(insn, 5, 5);
3395     unsigned int imm12 = extract32(insn, 10, 12);
3396     unsigned int offset;
3397     TCGv_i64 clean_addr, dirty_addr;
3398     bool is_store;
3399     bool is_signed = false;
3400     bool is_extended = false;
3401     MemOp memop;
3402 
3403     if (is_vector) {
3404         size |= (opc & 2) << 1;
3405         if (size > 4) {
3406             unallocated_encoding(s);
3407             return;
3408         }
3409         is_store = !extract32(opc, 0, 1);
3410         if (!fp_access_check(s)) {
3411             return;
3412         }
3413         memop = finalize_memop_asimd(s, size);
3414     } else {
3415         if (size == 3 && opc == 2) {
3416             /* PRFM - prefetch */
3417             return;
3418         }
3419         if (opc == 3 && size > 1) {
3420             unallocated_encoding(s);
3421             return;
3422         }
3423         is_store = (opc == 0);
3424         is_signed = !is_store && extract32(opc, 1, 1);
3425         is_extended = (size < 3) && extract32(opc, 0, 1);
3426         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3427     }
3428 
3429     if (rn == 31) {
3430         gen_check_sp_alignment(s);
3431     }
3432     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3433     offset = imm12 << size;
3434     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3435 
3436     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3437 
3438     if (is_vector) {
3439         if (is_store) {
3440             do_fp_st(s, rt, clean_addr, memop);
3441         } else {
3442             do_fp_ld(s, rt, clean_addr, memop);
3443         }
3444     } else {
3445         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3446         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3447         if (is_store) {
3448             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3449         } else {
3450             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3451                       is_extended, true, rt, iss_sf, false);
3452         }
3453     }
3454 }
3455 
3456 /* Atomic memory operations
3457  *
3458  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3459  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3460  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3461  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3462  *
3463  * Rt: the result register
3464  * Rn: base address or SP
3465  * Rs: the source register for the operation
3466  * V: vector flag (always 0 as of v8.3)
3467  * A: acquire flag
3468  * R: release flag
3469  */
3470 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3471                               int size, int rt, bool is_vector)
3472 {
3473     int rs = extract32(insn, 16, 5);
3474     int rn = extract32(insn, 5, 5);
3475     int o3_opc = extract32(insn, 12, 4);
3476     bool r = extract32(insn, 22, 1);
3477     bool a = extract32(insn, 23, 1);
3478     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3479     AtomicThreeOpFn *fn = NULL;
3480     MemOp mop = size;
3481 
3482     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3483         unallocated_encoding(s);
3484         return;
3485     }
3486     switch (o3_opc) {
3487     case 000: /* LDADD */
3488         fn = tcg_gen_atomic_fetch_add_i64;
3489         break;
3490     case 001: /* LDCLR */
3491         fn = tcg_gen_atomic_fetch_and_i64;
3492         break;
3493     case 002: /* LDEOR */
3494         fn = tcg_gen_atomic_fetch_xor_i64;
3495         break;
3496     case 003: /* LDSET */
3497         fn = tcg_gen_atomic_fetch_or_i64;
3498         break;
3499     case 004: /* LDSMAX */
3500         fn = tcg_gen_atomic_fetch_smax_i64;
3501         mop |= MO_SIGN;
3502         break;
3503     case 005: /* LDSMIN */
3504         fn = tcg_gen_atomic_fetch_smin_i64;
3505         mop |= MO_SIGN;
3506         break;
3507     case 006: /* LDUMAX */
3508         fn = tcg_gen_atomic_fetch_umax_i64;
3509         break;
3510     case 007: /* LDUMIN */
3511         fn = tcg_gen_atomic_fetch_umin_i64;
3512         break;
3513     case 010: /* SWP */
3514         fn = tcg_gen_atomic_xchg_i64;
3515         break;
3516     case 014: /* LDAPR, LDAPRH, LDAPRB */
3517         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3518             rs != 31 || a != 1 || r != 0) {
3519             unallocated_encoding(s);
3520             return;
3521         }
3522         break;
3523     default:
3524         unallocated_encoding(s);
3525         return;
3526     }
3527 
3528     if (rn == 31) {
3529         gen_check_sp_alignment(s);
3530     }
3531 
3532     mop = check_atomic_align(s, rn, mop);
3533     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3534 
3535     if (o3_opc == 014) {
3536         /*
3537          * LDAPR* are a special case because they are a simple load, not a
3538          * fetch-and-do-something op.
3539          * The architectural consistency requirements here are weaker than
3540          * full load-acquire (we only need "load-acquire processor consistent"),
3541          * but we choose to implement them as full LDAQ.
3542          */
3543         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3544                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3545         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3546         return;
3547     }
3548 
3549     tcg_rs = read_cpu_reg(s, rs, true);
3550     tcg_rt = cpu_reg(s, rt);
3551 
3552     if (o3_opc == 1) { /* LDCLR */
3553         tcg_gen_not_i64(tcg_rs, tcg_rs);
3554     }
3555 
3556     /* The tcg atomic primitives are all full barriers.  Therefore we
3557      * can ignore the Acquire and Release bits of this instruction.
3558      */
3559     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3560 
3561     if (mop & MO_SIGN) {
3562         switch (size) {
3563         case MO_8:
3564             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3565             break;
3566         case MO_16:
3567             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3568             break;
3569         case MO_32:
3570             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3571             break;
3572         case MO_64:
3573             break;
3574         default:
3575             g_assert_not_reached();
3576         }
3577     }
3578 }
3579 
3580 /*
3581  * PAC memory operations
3582  *
3583  *  31  30      27  26    24    22  21       12  11  10    5     0
3584  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3585  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3586  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3587  *
3588  * Rt: the result register
3589  * Rn: base address or SP
3590  * V: vector flag (always 0 as of v8.3)
3591  * M: clear for key DA, set for key DB
3592  * W: pre-indexing flag
3593  * S: sign for imm9.
3594  */
3595 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3596                            int size, int rt, bool is_vector)
3597 {
3598     int rn = extract32(insn, 5, 5);
3599     bool is_wback = extract32(insn, 11, 1);
3600     bool use_key_a = !extract32(insn, 23, 1);
3601     int offset;
3602     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3603     MemOp memop;
3604 
3605     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3606         unallocated_encoding(s);
3607         return;
3608     }
3609 
3610     if (rn == 31) {
3611         gen_check_sp_alignment(s);
3612     }
3613     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3614 
3615     if (s->pauth_active) {
3616         if (use_key_a) {
3617             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3618                              tcg_constant_i64(0));
3619         } else {
3620             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3621                              tcg_constant_i64(0));
3622         }
3623     }
3624 
3625     /* Form the 10-bit signed, scaled offset.  */
3626     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3627     offset = sextract32(offset << size, 0, 10 + size);
3628     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3629 
3630     memop = finalize_memop(s, size);
3631 
3632     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3633     clean_addr = gen_mte_check1(s, dirty_addr, false,
3634                                 is_wback || rn != 31, memop);
3635 
3636     tcg_rt = cpu_reg(s, rt);
3637     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3638               /* extend */ false, /* iss_valid */ !is_wback,
3639               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3640 
3641     if (is_wback) {
3642         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3643     }
3644 }
3645 
3646 /*
3647  * LDAPR/STLR (unscaled immediate)
3648  *
3649  *  31  30            24    22  21       12    10    5     0
3650  * +------+-------------+-----+---+--------+-----+----+-----+
3651  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3652  * +------+-------------+-----+---+--------+-----+----+-----+
3653  *
3654  * Rt: source or destination register
3655  * Rn: base register
3656  * imm9: unscaled immediate offset
3657  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3658  * size: size of load/store
3659  */
3660 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3661 {
3662     int rt = extract32(insn, 0, 5);
3663     int rn = extract32(insn, 5, 5);
3664     int offset = sextract32(insn, 12, 9);
3665     int opc = extract32(insn, 22, 2);
3666     int size = extract32(insn, 30, 2);
3667     TCGv_i64 clean_addr, dirty_addr;
3668     bool is_store = false;
3669     bool extend = false;
3670     bool iss_sf;
3671     MemOp mop = size;
3672 
3673     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3674         unallocated_encoding(s);
3675         return;
3676     }
3677 
3678     switch (opc) {
3679     case 0: /* STLURB */
3680         is_store = true;
3681         break;
3682     case 1: /* LDAPUR* */
3683         break;
3684     case 2: /* LDAPURS* 64-bit variant */
3685         if (size == 3) {
3686             unallocated_encoding(s);
3687             return;
3688         }
3689         mop |= MO_SIGN;
3690         break;
3691     case 3: /* LDAPURS* 32-bit variant */
3692         if (size > 1) {
3693             unallocated_encoding(s);
3694             return;
3695         }
3696         mop |= MO_SIGN;
3697         extend = true; /* zero-extend 32->64 after signed load */
3698         break;
3699     default:
3700         g_assert_not_reached();
3701     }
3702 
3703     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3704 
3705     if (rn == 31) {
3706         gen_check_sp_alignment(s);
3707     }
3708 
3709     mop = check_ordered_align(s, rn, offset, is_store, mop);
3710 
3711     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3712     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3713     clean_addr = clean_data_tbi(s, dirty_addr);
3714 
3715     if (is_store) {
3716         /* Store-Release semantics */
3717         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3718         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3719     } else {
3720         /*
3721          * Load-AcquirePC semantics; we implement as the slightly more
3722          * restrictive Load-Acquire.
3723          */
3724         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3725                   extend, true, rt, iss_sf, true);
3726         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3727     }
3728 }
3729 
3730 /* Load/store register (all forms) */
3731 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3732 {
3733     int rt = extract32(insn, 0, 5);
3734     int opc = extract32(insn, 22, 2);
3735     bool is_vector = extract32(insn, 26, 1);
3736     int size = extract32(insn, 30, 2);
3737 
3738     switch (extract32(insn, 24, 2)) {
3739     case 0:
3740         if (extract32(insn, 21, 1) == 0) {
3741             /* Load/store register (unscaled immediate)
3742              * Load/store immediate pre/post-indexed
3743              * Load/store register unprivileged
3744              */
3745             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3746             return;
3747         }
3748         switch (extract32(insn, 10, 2)) {
3749         case 0:
3750             disas_ldst_atomic(s, insn, size, rt, is_vector);
3751             return;
3752         case 2:
3753             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3754             return;
3755         default:
3756             disas_ldst_pac(s, insn, size, rt, is_vector);
3757             return;
3758         }
3759         break;
3760     case 1:
3761         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3762         return;
3763     }
3764     unallocated_encoding(s);
3765 }
3766 
3767 /* AdvSIMD load/store multiple structures
3768  *
3769  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3770  * +---+---+---------------+---+-------------+--------+------+------+------+
3771  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3772  * +---+---+---------------+---+-------------+--------+------+------+------+
3773  *
3774  * AdvSIMD load/store multiple structures (post-indexed)
3775  *
3776  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3777  * +---+---+---------------+---+---+---------+--------+------+------+------+
3778  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3779  * +---+---+---------------+---+---+---------+--------+------+------+------+
3780  *
3781  * Rt: first (or only) SIMD&FP register to be transferred
3782  * Rn: base address or SP
3783  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3784  */
3785 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3786 {
3787     int rt = extract32(insn, 0, 5);
3788     int rn = extract32(insn, 5, 5);
3789     int rm = extract32(insn, 16, 5);
3790     int size = extract32(insn, 10, 2);
3791     int opcode = extract32(insn, 12, 4);
3792     bool is_store = !extract32(insn, 22, 1);
3793     bool is_postidx = extract32(insn, 23, 1);
3794     bool is_q = extract32(insn, 30, 1);
3795     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3796     MemOp endian, align, mop;
3797 
3798     int total;    /* total bytes */
3799     int elements; /* elements per vector */
3800     int rpt;    /* num iterations */
3801     int selem;  /* structure elements */
3802     int r;
3803 
3804     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3805         unallocated_encoding(s);
3806         return;
3807     }
3808 
3809     if (!is_postidx && rm != 0) {
3810         unallocated_encoding(s);
3811         return;
3812     }
3813 
3814     /* From the shared decode logic */
3815     switch (opcode) {
3816     case 0x0:
3817         rpt = 1;
3818         selem = 4;
3819         break;
3820     case 0x2:
3821         rpt = 4;
3822         selem = 1;
3823         break;
3824     case 0x4:
3825         rpt = 1;
3826         selem = 3;
3827         break;
3828     case 0x6:
3829         rpt = 3;
3830         selem = 1;
3831         break;
3832     case 0x7:
3833         rpt = 1;
3834         selem = 1;
3835         break;
3836     case 0x8:
3837         rpt = 1;
3838         selem = 2;
3839         break;
3840     case 0xa:
3841         rpt = 2;
3842         selem = 1;
3843         break;
3844     default:
3845         unallocated_encoding(s);
3846         return;
3847     }
3848 
3849     if (size == 3 && !is_q && selem != 1) {
3850         /* reserved */
3851         unallocated_encoding(s);
3852         return;
3853     }
3854 
3855     if (!fp_access_check(s)) {
3856         return;
3857     }
3858 
3859     if (rn == 31) {
3860         gen_check_sp_alignment(s);
3861     }
3862 
3863     /* For our purposes, bytes are always little-endian.  */
3864     endian = s->be_data;
3865     if (size == 0) {
3866         endian = MO_LE;
3867     }
3868 
3869     total = rpt * selem * (is_q ? 16 : 8);
3870     tcg_rn = cpu_reg_sp(s, rn);
3871 
3872     /*
3873      * Issue the MTE check vs the logical repeat count, before we
3874      * promote consecutive little-endian elements below.
3875      */
3876     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3877                                 total, finalize_memop_asimd(s, size));
3878 
3879     /*
3880      * Consecutive little-endian elements from a single register
3881      * can be promoted to a larger little-endian operation.
3882      */
3883     align = MO_ALIGN;
3884     if (selem == 1 && endian == MO_LE) {
3885         align = pow2_align(size);
3886         size = 3;
3887     }
3888     if (!s->align_mem) {
3889         align = 0;
3890     }
3891     mop = endian | size | align;
3892 
3893     elements = (is_q ? 16 : 8) >> size;
3894     tcg_ebytes = tcg_constant_i64(1 << size);
3895     for (r = 0; r < rpt; r++) {
3896         int e;
3897         for (e = 0; e < elements; e++) {
3898             int xs;
3899             for (xs = 0; xs < selem; xs++) {
3900                 int tt = (rt + r + xs) % 32;
3901                 if (is_store) {
3902                     do_vec_st(s, tt, e, clean_addr, mop);
3903                 } else {
3904                     do_vec_ld(s, tt, e, clean_addr, mop);
3905                 }
3906                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3907             }
3908         }
3909     }
3910 
3911     if (!is_store) {
3912         /* For non-quad operations, setting a slice of the low
3913          * 64 bits of the register clears the high 64 bits (in
3914          * the ARM ARM pseudocode this is implicit in the fact
3915          * that 'rval' is a 64 bit wide variable).
3916          * For quad operations, we might still need to zero the
3917          * high bits of SVE.
3918          */
3919         for (r = 0; r < rpt * selem; r++) {
3920             int tt = (rt + r) % 32;
3921             clear_vec_high(s, is_q, tt);
3922         }
3923     }
3924 
3925     if (is_postidx) {
3926         if (rm == 31) {
3927             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3928         } else {
3929             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3930         }
3931     }
3932 }
3933 
3934 /* AdvSIMD load/store single structure
3935  *
3936  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3937  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3938  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3939  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3940  *
3941  * AdvSIMD load/store single structure (post-indexed)
3942  *
3943  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3944  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3945  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3946  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3947  *
3948  * Rt: first (or only) SIMD&FP register to be transferred
3949  * Rn: base address or SP
3950  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3951  * index = encoded in Q:S:size dependent on size
3952  *
3953  * lane_size = encoded in R, opc
3954  * transfer width = encoded in opc, S, size
3955  */
3956 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3957 {
3958     int rt = extract32(insn, 0, 5);
3959     int rn = extract32(insn, 5, 5);
3960     int rm = extract32(insn, 16, 5);
3961     int size = extract32(insn, 10, 2);
3962     int S = extract32(insn, 12, 1);
3963     int opc = extract32(insn, 13, 3);
3964     int R = extract32(insn, 21, 1);
3965     int is_load = extract32(insn, 22, 1);
3966     int is_postidx = extract32(insn, 23, 1);
3967     int is_q = extract32(insn, 30, 1);
3968 
3969     int scale = extract32(opc, 1, 2);
3970     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3971     bool replicate = false;
3972     int index = is_q << 3 | S << 2 | size;
3973     int xs, total;
3974     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3975     MemOp mop;
3976 
3977     if (extract32(insn, 31, 1)) {
3978         unallocated_encoding(s);
3979         return;
3980     }
3981     if (!is_postidx && rm != 0) {
3982         unallocated_encoding(s);
3983         return;
3984     }
3985 
3986     switch (scale) {
3987     case 3:
3988         if (!is_load || S) {
3989             unallocated_encoding(s);
3990             return;
3991         }
3992         scale = size;
3993         replicate = true;
3994         break;
3995     case 0:
3996         break;
3997     case 1:
3998         if (extract32(size, 0, 1)) {
3999             unallocated_encoding(s);
4000             return;
4001         }
4002         index >>= 1;
4003         break;
4004     case 2:
4005         if (extract32(size, 1, 1)) {
4006             unallocated_encoding(s);
4007             return;
4008         }
4009         if (!extract32(size, 0, 1)) {
4010             index >>= 2;
4011         } else {
4012             if (S) {
4013                 unallocated_encoding(s);
4014                 return;
4015             }
4016             index >>= 3;
4017             scale = 3;
4018         }
4019         break;
4020     default:
4021         g_assert_not_reached();
4022     }
4023 
4024     if (!fp_access_check(s)) {
4025         return;
4026     }
4027 
4028     if (rn == 31) {
4029         gen_check_sp_alignment(s);
4030     }
4031 
4032     total = selem << scale;
4033     tcg_rn = cpu_reg_sp(s, rn);
4034 
4035     mop = finalize_memop_asimd(s, scale);
4036 
4037     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4038                                 total, mop);
4039 
4040     tcg_ebytes = tcg_constant_i64(1 << scale);
4041     for (xs = 0; xs < selem; xs++) {
4042         if (replicate) {
4043             /* Load and replicate to all elements */
4044             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4045 
4046             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4047             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4048                                  (is_q + 1) * 8, vec_full_reg_size(s),
4049                                  tcg_tmp);
4050         } else {
4051             /* Load/store one element per register */
4052             if (is_load) {
4053                 do_vec_ld(s, rt, index, clean_addr, mop);
4054             } else {
4055                 do_vec_st(s, rt, index, clean_addr, mop);
4056             }
4057         }
4058         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4059         rt = (rt + 1) % 32;
4060     }
4061 
4062     if (is_postidx) {
4063         if (rm == 31) {
4064             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4065         } else {
4066             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4067         }
4068     }
4069 }
4070 
4071 /*
4072  * Load/Store memory tags
4073  *
4074  *  31 30 29         24     22  21     12    10      5      0
4075  * +-----+-------------+-----+---+------+-----+------+------+
4076  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4077  * +-----+-------------+-----+---+------+-----+------+------+
4078  */
4079 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4080 {
4081     int rt = extract32(insn, 0, 5);
4082     int rn = extract32(insn, 5, 5);
4083     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4084     int op2 = extract32(insn, 10, 2);
4085     int op1 = extract32(insn, 22, 2);
4086     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4087     int index = 0;
4088     TCGv_i64 addr, clean_addr, tcg_rt;
4089 
4090     /* We checked insn bits [29:24,21] in the caller.  */
4091     if (extract32(insn, 30, 2) != 3) {
4092         goto do_unallocated;
4093     }
4094 
4095     /*
4096      * @index is a tri-state variable which has 3 states:
4097      * < 0 : post-index, writeback
4098      * = 0 : signed offset
4099      * > 0 : pre-index, writeback
4100      */
4101     switch (op1) {
4102     case 0:
4103         if (op2 != 0) {
4104             /* STG */
4105             index = op2 - 2;
4106         } else {
4107             /* STZGM */
4108             if (s->current_el == 0 || offset != 0) {
4109                 goto do_unallocated;
4110             }
4111             is_mult = is_zero = true;
4112         }
4113         break;
4114     case 1:
4115         if (op2 != 0) {
4116             /* STZG */
4117             is_zero = true;
4118             index = op2 - 2;
4119         } else {
4120             /* LDG */
4121             is_load = true;
4122         }
4123         break;
4124     case 2:
4125         if (op2 != 0) {
4126             /* ST2G */
4127             is_pair = true;
4128             index = op2 - 2;
4129         } else {
4130             /* STGM */
4131             if (s->current_el == 0 || offset != 0) {
4132                 goto do_unallocated;
4133             }
4134             is_mult = true;
4135         }
4136         break;
4137     case 3:
4138         if (op2 != 0) {
4139             /* STZ2G */
4140             is_pair = is_zero = true;
4141             index = op2 - 2;
4142         } else {
4143             /* LDGM */
4144             if (s->current_el == 0 || offset != 0) {
4145                 goto do_unallocated;
4146             }
4147             is_mult = is_load = true;
4148         }
4149         break;
4150 
4151     default:
4152     do_unallocated:
4153         unallocated_encoding(s);
4154         return;
4155     }
4156 
4157     if (is_mult
4158         ? !dc_isar_feature(aa64_mte, s)
4159         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4160         goto do_unallocated;
4161     }
4162 
4163     if (rn == 31) {
4164         gen_check_sp_alignment(s);
4165     }
4166 
4167     addr = read_cpu_reg_sp(s, rn, true);
4168     if (index >= 0) {
4169         /* pre-index or signed offset */
4170         tcg_gen_addi_i64(addr, addr, offset);
4171     }
4172 
4173     if (is_mult) {
4174         tcg_rt = cpu_reg(s, rt);
4175 
4176         if (is_zero) {
4177             int size = 4 << s->dcz_blocksize;
4178 
4179             if (s->ata) {
4180                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4181             }
4182             /*
4183              * The non-tags portion of STZGM is mostly like DC_ZVA,
4184              * except the alignment happens before the access.
4185              */
4186             clean_addr = clean_data_tbi(s, addr);
4187             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4188             gen_helper_dc_zva(cpu_env, clean_addr);
4189         } else if (s->ata) {
4190             if (is_load) {
4191                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4192             } else {
4193                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4194             }
4195         } else {
4196             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4197             int size = 4 << GMID_EL1_BS;
4198 
4199             clean_addr = clean_data_tbi(s, addr);
4200             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4201             gen_probe_access(s, clean_addr, acc, size);
4202 
4203             if (is_load) {
4204                 /* The result tags are zeros.  */
4205                 tcg_gen_movi_i64(tcg_rt, 0);
4206             }
4207         }
4208         return;
4209     }
4210 
4211     if (is_load) {
4212         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4213         tcg_rt = cpu_reg(s, rt);
4214         if (s->ata) {
4215             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4216         } else {
4217             /*
4218              * Tag access disabled: we must check for aborts on the load
4219              * load from [rn+offset], and then insert a 0 tag into rt.
4220              */
4221             clean_addr = clean_data_tbi(s, addr);
4222             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4223             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4224         }
4225     } else {
4226         tcg_rt = cpu_reg_sp(s, rt);
4227         if (!s->ata) {
4228             /*
4229              * For STG and ST2G, we need to check alignment and probe memory.
4230              * TODO: For STZG and STZ2G, we could rely on the stores below,
4231              * at least for system mode; user-only won't enforce alignment.
4232              */
4233             if (is_pair) {
4234                 gen_helper_st2g_stub(cpu_env, addr);
4235             } else {
4236                 gen_helper_stg_stub(cpu_env, addr);
4237             }
4238         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4239             if (is_pair) {
4240                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4241             } else {
4242                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4243             }
4244         } else {
4245             if (is_pair) {
4246                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4247             } else {
4248                 gen_helper_stg(cpu_env, addr, tcg_rt);
4249             }
4250         }
4251     }
4252 
4253     if (is_zero) {
4254         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4255         TCGv_i64 zero64 = tcg_constant_i64(0);
4256         TCGv_i128 zero128 = tcg_temp_new_i128();
4257         int mem_index = get_mem_index(s);
4258         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4259 
4260         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4261 
4262         /* This is 1 or 2 atomic 16-byte operations. */
4263         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4264         if (is_pair) {
4265             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4266             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4267         }
4268     }
4269 
4270     if (index != 0) {
4271         /* pre-index or post-index */
4272         if (index < 0) {
4273             /* post-index */
4274             tcg_gen_addi_i64(addr, addr, offset);
4275         }
4276         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4277     }
4278 }
4279 
4280 /* Loads and stores */
4281 static void disas_ldst(DisasContext *s, uint32_t insn)
4282 {
4283     switch (extract32(insn, 24, 6)) {
4284     case 0x08: /* Load/store exclusive */
4285         disas_ldst_excl(s, insn);
4286         break;
4287     case 0x18: case 0x1c: /* Load register (literal) */
4288         disas_ld_lit(s, insn);
4289         break;
4290     case 0x28: case 0x29:
4291     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4292         disas_ldst_pair(s, insn);
4293         break;
4294     case 0x38: case 0x39:
4295     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4296         disas_ldst_reg(s, insn);
4297         break;
4298     case 0x0c: /* AdvSIMD load/store multiple structures */
4299         disas_ldst_multiple_struct(s, insn);
4300         break;
4301     case 0x0d: /* AdvSIMD load/store single structure */
4302         disas_ldst_single_struct(s, insn);
4303         break;
4304     case 0x19:
4305         if (extract32(insn, 21, 1) != 0) {
4306             disas_ldst_tag(s, insn);
4307         } else if (extract32(insn, 10, 2) == 0) {
4308             disas_ldst_ldapr_stlr(s, insn);
4309         } else {
4310             unallocated_encoding(s);
4311         }
4312         break;
4313     default:
4314         unallocated_encoding(s);
4315         break;
4316     }
4317 }
4318 
4319 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4320 
4321 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4322                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4323 {
4324     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4325     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4326     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4327 
4328     fn(tcg_rd, tcg_rn, tcg_imm);
4329     if (!a->sf) {
4330         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4331     }
4332     return true;
4333 }
4334 
4335 /*
4336  * PC-rel. addressing
4337  */
4338 
4339 static bool trans_ADR(DisasContext *s, arg_ri *a)
4340 {
4341     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4342     return true;
4343 }
4344 
4345 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4346 {
4347     int64_t offset = (int64_t)a->imm << 12;
4348 
4349     /* The page offset is ok for CF_PCREL. */
4350     offset -= s->pc_curr & 0xfff;
4351     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4352     return true;
4353 }
4354 
4355 /*
4356  * Add/subtract (immediate)
4357  */
4358 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4359 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4360 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4361 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4362 
4363 /*
4364  * Add/subtract (immediate, with tags)
4365  */
4366 
4367 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4368                                       bool sub_op)
4369 {
4370     TCGv_i64 tcg_rn, tcg_rd;
4371     int imm;
4372 
4373     imm = a->uimm6 << LOG2_TAG_GRANULE;
4374     if (sub_op) {
4375         imm = -imm;
4376     }
4377 
4378     tcg_rn = cpu_reg_sp(s, a->rn);
4379     tcg_rd = cpu_reg_sp(s, a->rd);
4380 
4381     if (s->ata) {
4382         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4383                            tcg_constant_i32(imm),
4384                            tcg_constant_i32(a->uimm4));
4385     } else {
4386         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4387         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4388     }
4389     return true;
4390 }
4391 
4392 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4393 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4394 
4395 /* The input should be a value in the bottom e bits (with higher
4396  * bits zero); returns that value replicated into every element
4397  * of size e in a 64 bit integer.
4398  */
4399 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4400 {
4401     assert(e != 0);
4402     while (e < 64) {
4403         mask |= mask << e;
4404         e *= 2;
4405     }
4406     return mask;
4407 }
4408 
4409 /*
4410  * Logical (immediate)
4411  */
4412 
4413 /*
4414  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4415  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4416  * value (ie should cause a guest UNDEF exception), and true if they are
4417  * valid, in which case the decoded bit pattern is written to result.
4418  */
4419 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4420                             unsigned int imms, unsigned int immr)
4421 {
4422     uint64_t mask;
4423     unsigned e, levels, s, r;
4424     int len;
4425 
4426     assert(immn < 2 && imms < 64 && immr < 64);
4427 
4428     /* The bit patterns we create here are 64 bit patterns which
4429      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4430      * 64 bits each. Each element contains the same value: a run
4431      * of between 1 and e-1 non-zero bits, rotated within the
4432      * element by between 0 and e-1 bits.
4433      *
4434      * The element size and run length are encoded into immn (1 bit)
4435      * and imms (6 bits) as follows:
4436      * 64 bit elements: immn = 1, imms = <length of run - 1>
4437      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4438      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4439      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4440      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4441      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4442      * Notice that immn = 0, imms = 11111x is the only combination
4443      * not covered by one of the above options; this is reserved.
4444      * Further, <length of run - 1> all-ones is a reserved pattern.
4445      *
4446      * In all cases the rotation is by immr % e (and immr is 6 bits).
4447      */
4448 
4449     /* First determine the element size */
4450     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4451     if (len < 1) {
4452         /* This is the immn == 0, imms == 0x11111x case */
4453         return false;
4454     }
4455     e = 1 << len;
4456 
4457     levels = e - 1;
4458     s = imms & levels;
4459     r = immr & levels;
4460 
4461     if (s == levels) {
4462         /* <length of run - 1> mustn't be all-ones. */
4463         return false;
4464     }
4465 
4466     /* Create the value of one element: s+1 set bits rotated
4467      * by r within the element (which is e bits wide)...
4468      */
4469     mask = MAKE_64BIT_MASK(0, s + 1);
4470     if (r) {
4471         mask = (mask >> r) | (mask << (e - r));
4472         mask &= MAKE_64BIT_MASK(0, e);
4473     }
4474     /* ...then replicate the element over the whole 64 bit value */
4475     mask = bitfield_replicate(mask, e);
4476     *result = mask;
4477     return true;
4478 }
4479 
4480 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4481                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4482 {
4483     TCGv_i64 tcg_rd, tcg_rn;
4484     uint64_t imm;
4485 
4486     /* Some immediate field values are reserved. */
4487     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4488                                 extract32(a->dbm, 0, 6),
4489                                 extract32(a->dbm, 6, 6))) {
4490         return false;
4491     }
4492     if (!a->sf) {
4493         imm &= 0xffffffffull;
4494     }
4495 
4496     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4497     tcg_rn = cpu_reg(s, a->rn);
4498 
4499     fn(tcg_rd, tcg_rn, imm);
4500     if (set_cc) {
4501         gen_logic_CC(a->sf, tcg_rd);
4502     }
4503     if (!a->sf) {
4504         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4505     }
4506     return true;
4507 }
4508 
4509 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4510 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4511 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4512 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4513 
4514 /*
4515  * Move wide (immediate)
4516  */
4517 
4518 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4519 {
4520     int pos = a->hw << 4;
4521     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4522     return true;
4523 }
4524 
4525 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4526 {
4527     int pos = a->hw << 4;
4528     uint64_t imm = a->imm;
4529 
4530     imm = ~(imm << pos);
4531     if (!a->sf) {
4532         imm = (uint32_t)imm;
4533     }
4534     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4535     return true;
4536 }
4537 
4538 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4539 {
4540     int pos = a->hw << 4;
4541     TCGv_i64 tcg_rd, tcg_im;
4542 
4543     tcg_rd = cpu_reg(s, a->rd);
4544     tcg_im = tcg_constant_i64(a->imm);
4545     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4546     if (!a->sf) {
4547         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4548     }
4549     return true;
4550 }
4551 
4552 /*
4553  * Bitfield
4554  */
4555 
4556 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4557 {
4558     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4559     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4560     unsigned int bitsize = a->sf ? 64 : 32;
4561     unsigned int ri = a->immr;
4562     unsigned int si = a->imms;
4563     unsigned int pos, len;
4564 
4565     if (si >= ri) {
4566         /* Wd<s-r:0> = Wn<s:r> */
4567         len = (si - ri) + 1;
4568         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4569         if (!a->sf) {
4570             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4571         }
4572     } else {
4573         /* Wd<32+s-r,32-r> = Wn<s:0> */
4574         len = si + 1;
4575         pos = (bitsize - ri) & (bitsize - 1);
4576 
4577         if (len < ri) {
4578             /*
4579              * Sign extend the destination field from len to fill the
4580              * balance of the word.  Let the deposit below insert all
4581              * of those sign bits.
4582              */
4583             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4584             len = ri;
4585         }
4586 
4587         /*
4588          * We start with zero, and we haven't modified any bits outside
4589          * bitsize, therefore no final zero-extension is unneeded for !sf.
4590          */
4591         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4592     }
4593     return true;
4594 }
4595 
4596 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4597 {
4598     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4599     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4600     unsigned int bitsize = a->sf ? 64 : 32;
4601     unsigned int ri = a->immr;
4602     unsigned int si = a->imms;
4603     unsigned int pos, len;
4604 
4605     tcg_rd = cpu_reg(s, a->rd);
4606     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4607 
4608     if (si >= ri) {
4609         /* Wd<s-r:0> = Wn<s:r> */
4610         len = (si - ri) + 1;
4611         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4612     } else {
4613         /* Wd<32+s-r,32-r> = Wn<s:0> */
4614         len = si + 1;
4615         pos = (bitsize - ri) & (bitsize - 1);
4616         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4617     }
4618     return true;
4619 }
4620 
4621 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4622 {
4623     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4624     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4625     unsigned int bitsize = a->sf ? 64 : 32;
4626     unsigned int ri = a->immr;
4627     unsigned int si = a->imms;
4628     unsigned int pos, len;
4629 
4630     tcg_rd = cpu_reg(s, a->rd);
4631     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4632 
4633     if (si >= ri) {
4634         /* Wd<s-r:0> = Wn<s:r> */
4635         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4636         len = (si - ri) + 1;
4637         pos = 0;
4638     } else {
4639         /* Wd<32+s-r,32-r> = Wn<s:0> */
4640         len = si + 1;
4641         pos = (bitsize - ri) & (bitsize - 1);
4642     }
4643 
4644     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4645     if (!a->sf) {
4646         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4647     }
4648     return true;
4649 }
4650 
4651 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4652 {
4653     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4654 
4655     tcg_rd = cpu_reg(s, a->rd);
4656 
4657     if (unlikely(a->imm == 0)) {
4658         /*
4659          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4660          * so an extract from bit 0 is a special case.
4661          */
4662         if (a->sf) {
4663             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4664         } else {
4665             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4666         }
4667     } else {
4668         tcg_rm = cpu_reg(s, a->rm);
4669         tcg_rn = cpu_reg(s, a->rn);
4670 
4671         if (a->sf) {
4672             /* Specialization to ROR happens in EXTRACT2.  */
4673             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4674         } else {
4675             TCGv_i32 t0 = tcg_temp_new_i32();
4676 
4677             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4678             if (a->rm == a->rn) {
4679                 tcg_gen_rotri_i32(t0, t0, a->imm);
4680             } else {
4681                 TCGv_i32 t1 = tcg_temp_new_i32();
4682                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4683                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4684             }
4685             tcg_gen_extu_i32_i64(tcg_rd, t0);
4686         }
4687     }
4688     return true;
4689 }
4690 
4691 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4692  * Note that it is the caller's responsibility to ensure that the
4693  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4694  * mandated semantics for out of range shifts.
4695  */
4696 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4697                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4698 {
4699     switch (shift_type) {
4700     case A64_SHIFT_TYPE_LSL:
4701         tcg_gen_shl_i64(dst, src, shift_amount);
4702         break;
4703     case A64_SHIFT_TYPE_LSR:
4704         tcg_gen_shr_i64(dst, src, shift_amount);
4705         break;
4706     case A64_SHIFT_TYPE_ASR:
4707         if (!sf) {
4708             tcg_gen_ext32s_i64(dst, src);
4709         }
4710         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4711         break;
4712     case A64_SHIFT_TYPE_ROR:
4713         if (sf) {
4714             tcg_gen_rotr_i64(dst, src, shift_amount);
4715         } else {
4716             TCGv_i32 t0, t1;
4717             t0 = tcg_temp_new_i32();
4718             t1 = tcg_temp_new_i32();
4719             tcg_gen_extrl_i64_i32(t0, src);
4720             tcg_gen_extrl_i64_i32(t1, shift_amount);
4721             tcg_gen_rotr_i32(t0, t0, t1);
4722             tcg_gen_extu_i32_i64(dst, t0);
4723         }
4724         break;
4725     default:
4726         assert(FALSE); /* all shift types should be handled */
4727         break;
4728     }
4729 
4730     if (!sf) { /* zero extend final result */
4731         tcg_gen_ext32u_i64(dst, dst);
4732     }
4733 }
4734 
4735 /* Shift a TCGv src by immediate, put result in dst.
4736  * The shift amount must be in range (this should always be true as the
4737  * relevant instructions will UNDEF on bad shift immediates).
4738  */
4739 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4740                           enum a64_shift_type shift_type, unsigned int shift_i)
4741 {
4742     assert(shift_i < (sf ? 64 : 32));
4743 
4744     if (shift_i == 0) {
4745         tcg_gen_mov_i64(dst, src);
4746     } else {
4747         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4748     }
4749 }
4750 
4751 /* Logical (shifted register)
4752  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4753  * +----+-----+-----------+-------+---+------+--------+------+------+
4754  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4755  * +----+-----+-----------+-------+---+------+--------+------+------+
4756  */
4757 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4758 {
4759     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4760     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4761 
4762     sf = extract32(insn, 31, 1);
4763     opc = extract32(insn, 29, 2);
4764     shift_type = extract32(insn, 22, 2);
4765     invert = extract32(insn, 21, 1);
4766     rm = extract32(insn, 16, 5);
4767     shift_amount = extract32(insn, 10, 6);
4768     rn = extract32(insn, 5, 5);
4769     rd = extract32(insn, 0, 5);
4770 
4771     if (!sf && (shift_amount & (1 << 5))) {
4772         unallocated_encoding(s);
4773         return;
4774     }
4775 
4776     tcg_rd = cpu_reg(s, rd);
4777 
4778     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4779         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4780          * register-register MOV and MVN, so it is worth special casing.
4781          */
4782         tcg_rm = cpu_reg(s, rm);
4783         if (invert) {
4784             tcg_gen_not_i64(tcg_rd, tcg_rm);
4785             if (!sf) {
4786                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4787             }
4788         } else {
4789             if (sf) {
4790                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4791             } else {
4792                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4793             }
4794         }
4795         return;
4796     }
4797 
4798     tcg_rm = read_cpu_reg(s, rm, sf);
4799 
4800     if (shift_amount) {
4801         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4802     }
4803 
4804     tcg_rn = cpu_reg(s, rn);
4805 
4806     switch (opc | (invert << 2)) {
4807     case 0: /* AND */
4808     case 3: /* ANDS */
4809         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4810         break;
4811     case 1: /* ORR */
4812         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4813         break;
4814     case 2: /* EOR */
4815         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4816         break;
4817     case 4: /* BIC */
4818     case 7: /* BICS */
4819         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4820         break;
4821     case 5: /* ORN */
4822         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4823         break;
4824     case 6: /* EON */
4825         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4826         break;
4827     default:
4828         assert(FALSE);
4829         break;
4830     }
4831 
4832     if (!sf) {
4833         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4834     }
4835 
4836     if (opc == 3) {
4837         gen_logic_CC(sf, tcg_rd);
4838     }
4839 }
4840 
4841 /*
4842  * Add/subtract (extended register)
4843  *
4844  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4845  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4846  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4847  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4848  *
4849  *  sf: 0 -> 32bit, 1 -> 64bit
4850  *  op: 0 -> add  , 1 -> sub
4851  *   S: 1 -> set flags
4852  * opt: 00
4853  * option: extension type (see DecodeRegExtend)
4854  * imm3: optional shift to Rm
4855  *
4856  * Rd = Rn + LSL(extend(Rm), amount)
4857  */
4858 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4859 {
4860     int rd = extract32(insn, 0, 5);
4861     int rn = extract32(insn, 5, 5);
4862     int imm3 = extract32(insn, 10, 3);
4863     int option = extract32(insn, 13, 3);
4864     int rm = extract32(insn, 16, 5);
4865     int opt = extract32(insn, 22, 2);
4866     bool setflags = extract32(insn, 29, 1);
4867     bool sub_op = extract32(insn, 30, 1);
4868     bool sf = extract32(insn, 31, 1);
4869 
4870     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4871     TCGv_i64 tcg_rd;
4872     TCGv_i64 tcg_result;
4873 
4874     if (imm3 > 4 || opt != 0) {
4875         unallocated_encoding(s);
4876         return;
4877     }
4878 
4879     /* non-flag setting ops may use SP */
4880     if (!setflags) {
4881         tcg_rd = cpu_reg_sp(s, rd);
4882     } else {
4883         tcg_rd = cpu_reg(s, rd);
4884     }
4885     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4886 
4887     tcg_rm = read_cpu_reg(s, rm, sf);
4888     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4889 
4890     tcg_result = tcg_temp_new_i64();
4891 
4892     if (!setflags) {
4893         if (sub_op) {
4894             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4895         } else {
4896             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4897         }
4898     } else {
4899         if (sub_op) {
4900             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4901         } else {
4902             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4903         }
4904     }
4905 
4906     if (sf) {
4907         tcg_gen_mov_i64(tcg_rd, tcg_result);
4908     } else {
4909         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4910     }
4911 }
4912 
4913 /*
4914  * Add/subtract (shifted register)
4915  *
4916  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4917  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4918  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4919  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4920  *
4921  *    sf: 0 -> 32bit, 1 -> 64bit
4922  *    op: 0 -> add  , 1 -> sub
4923  *     S: 1 -> set flags
4924  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4925  *  imm6: Shift amount to apply to Rm before the add/sub
4926  */
4927 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4928 {
4929     int rd = extract32(insn, 0, 5);
4930     int rn = extract32(insn, 5, 5);
4931     int imm6 = extract32(insn, 10, 6);
4932     int rm = extract32(insn, 16, 5);
4933     int shift_type = extract32(insn, 22, 2);
4934     bool setflags = extract32(insn, 29, 1);
4935     bool sub_op = extract32(insn, 30, 1);
4936     bool sf = extract32(insn, 31, 1);
4937 
4938     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4939     TCGv_i64 tcg_rn, tcg_rm;
4940     TCGv_i64 tcg_result;
4941 
4942     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4943         unallocated_encoding(s);
4944         return;
4945     }
4946 
4947     tcg_rn = read_cpu_reg(s, rn, sf);
4948     tcg_rm = read_cpu_reg(s, rm, sf);
4949 
4950     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4951 
4952     tcg_result = tcg_temp_new_i64();
4953 
4954     if (!setflags) {
4955         if (sub_op) {
4956             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4957         } else {
4958             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4959         }
4960     } else {
4961         if (sub_op) {
4962             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4963         } else {
4964             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4965         }
4966     }
4967 
4968     if (sf) {
4969         tcg_gen_mov_i64(tcg_rd, tcg_result);
4970     } else {
4971         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4972     }
4973 }
4974 
4975 /* Data-processing (3 source)
4976  *
4977  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4978  *  +--+------+-----------+------+------+----+------+------+------+
4979  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4980  *  +--+------+-----------+------+------+----+------+------+------+
4981  */
4982 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4983 {
4984     int rd = extract32(insn, 0, 5);
4985     int rn = extract32(insn, 5, 5);
4986     int ra = extract32(insn, 10, 5);
4987     int rm = extract32(insn, 16, 5);
4988     int op_id = (extract32(insn, 29, 3) << 4) |
4989         (extract32(insn, 21, 3) << 1) |
4990         extract32(insn, 15, 1);
4991     bool sf = extract32(insn, 31, 1);
4992     bool is_sub = extract32(op_id, 0, 1);
4993     bool is_high = extract32(op_id, 2, 1);
4994     bool is_signed = false;
4995     TCGv_i64 tcg_op1;
4996     TCGv_i64 tcg_op2;
4997     TCGv_i64 tcg_tmp;
4998 
4999     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5000     switch (op_id) {
5001     case 0x42: /* SMADDL */
5002     case 0x43: /* SMSUBL */
5003     case 0x44: /* SMULH */
5004         is_signed = true;
5005         break;
5006     case 0x0: /* MADD (32bit) */
5007     case 0x1: /* MSUB (32bit) */
5008     case 0x40: /* MADD (64bit) */
5009     case 0x41: /* MSUB (64bit) */
5010     case 0x4a: /* UMADDL */
5011     case 0x4b: /* UMSUBL */
5012     case 0x4c: /* UMULH */
5013         break;
5014     default:
5015         unallocated_encoding(s);
5016         return;
5017     }
5018 
5019     if (is_high) {
5020         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5021         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5022         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5023         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5024 
5025         if (is_signed) {
5026             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5027         } else {
5028             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5029         }
5030         return;
5031     }
5032 
5033     tcg_op1 = tcg_temp_new_i64();
5034     tcg_op2 = tcg_temp_new_i64();
5035     tcg_tmp = tcg_temp_new_i64();
5036 
5037     if (op_id < 0x42) {
5038         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5039         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5040     } else {
5041         if (is_signed) {
5042             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5043             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5044         } else {
5045             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5046             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5047         }
5048     }
5049 
5050     if (ra == 31 && !is_sub) {
5051         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5052         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5053     } else {
5054         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5055         if (is_sub) {
5056             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5057         } else {
5058             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5059         }
5060     }
5061 
5062     if (!sf) {
5063         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5064     }
5065 }
5066 
5067 /* Add/subtract (with carry)
5068  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5069  * +--+--+--+------------------------+------+-------------+------+-----+
5070  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5071  * +--+--+--+------------------------+------+-------------+------+-----+
5072  */
5073 
5074 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5075 {
5076     unsigned int sf, op, setflags, rm, rn, rd;
5077     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5078 
5079     sf = extract32(insn, 31, 1);
5080     op = extract32(insn, 30, 1);
5081     setflags = extract32(insn, 29, 1);
5082     rm = extract32(insn, 16, 5);
5083     rn = extract32(insn, 5, 5);
5084     rd = extract32(insn, 0, 5);
5085 
5086     tcg_rd = cpu_reg(s, rd);
5087     tcg_rn = cpu_reg(s, rn);
5088 
5089     if (op) {
5090         tcg_y = tcg_temp_new_i64();
5091         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5092     } else {
5093         tcg_y = cpu_reg(s, rm);
5094     }
5095 
5096     if (setflags) {
5097         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5098     } else {
5099         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5100     }
5101 }
5102 
5103 /*
5104  * Rotate right into flags
5105  *  31 30 29                21       15          10      5  4      0
5106  * +--+--+--+-----------------+--------+-----------+------+--+------+
5107  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5108  * +--+--+--+-----------------+--------+-----------+------+--+------+
5109  */
5110 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5111 {
5112     int mask = extract32(insn, 0, 4);
5113     int o2 = extract32(insn, 4, 1);
5114     int rn = extract32(insn, 5, 5);
5115     int imm6 = extract32(insn, 15, 6);
5116     int sf_op_s = extract32(insn, 29, 3);
5117     TCGv_i64 tcg_rn;
5118     TCGv_i32 nzcv;
5119 
5120     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5121         unallocated_encoding(s);
5122         return;
5123     }
5124 
5125     tcg_rn = read_cpu_reg(s, rn, 1);
5126     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5127 
5128     nzcv = tcg_temp_new_i32();
5129     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5130 
5131     if (mask & 8) { /* N */
5132         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5133     }
5134     if (mask & 4) { /* Z */
5135         tcg_gen_not_i32(cpu_ZF, nzcv);
5136         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5137     }
5138     if (mask & 2) { /* C */
5139         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5140     }
5141     if (mask & 1) { /* V */
5142         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5143     }
5144 }
5145 
5146 /*
5147  * Evaluate into flags
5148  *  31 30 29                21        15   14        10      5  4      0
5149  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5150  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5151  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5152  */
5153 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5154 {
5155     int o3_mask = extract32(insn, 0, 5);
5156     int rn = extract32(insn, 5, 5);
5157     int o2 = extract32(insn, 15, 6);
5158     int sz = extract32(insn, 14, 1);
5159     int sf_op_s = extract32(insn, 29, 3);
5160     TCGv_i32 tmp;
5161     int shift;
5162 
5163     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5164         !dc_isar_feature(aa64_condm_4, s)) {
5165         unallocated_encoding(s);
5166         return;
5167     }
5168     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5169 
5170     tmp = tcg_temp_new_i32();
5171     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5172     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5173     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5174     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5175     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5176 }
5177 
5178 /* Conditional compare (immediate / register)
5179  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5180  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5181  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5182  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5183  *        [1]                             y                [0]       [0]
5184  */
5185 static void disas_cc(DisasContext *s, uint32_t insn)
5186 {
5187     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5188     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5189     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5190     DisasCompare c;
5191 
5192     if (!extract32(insn, 29, 1)) {
5193         unallocated_encoding(s);
5194         return;
5195     }
5196     if (insn & (1 << 10 | 1 << 4)) {
5197         unallocated_encoding(s);
5198         return;
5199     }
5200     sf = extract32(insn, 31, 1);
5201     op = extract32(insn, 30, 1);
5202     is_imm = extract32(insn, 11, 1);
5203     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5204     cond = extract32(insn, 12, 4);
5205     rn = extract32(insn, 5, 5);
5206     nzcv = extract32(insn, 0, 4);
5207 
5208     /* Set T0 = !COND.  */
5209     tcg_t0 = tcg_temp_new_i32();
5210     arm_test_cc(&c, cond);
5211     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5212 
5213     /* Load the arguments for the new comparison.  */
5214     if (is_imm) {
5215         tcg_y = tcg_temp_new_i64();
5216         tcg_gen_movi_i64(tcg_y, y);
5217     } else {
5218         tcg_y = cpu_reg(s, y);
5219     }
5220     tcg_rn = cpu_reg(s, rn);
5221 
5222     /* Set the flags for the new comparison.  */
5223     tcg_tmp = tcg_temp_new_i64();
5224     if (op) {
5225         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5226     } else {
5227         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5228     }
5229 
5230     /* If COND was false, force the flags to #nzcv.  Compute two masks
5231      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5232      * For tcg hosts that support ANDC, we can make do with just T1.
5233      * In either case, allow the tcg optimizer to delete any unused mask.
5234      */
5235     tcg_t1 = tcg_temp_new_i32();
5236     tcg_t2 = tcg_temp_new_i32();
5237     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5238     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5239 
5240     if (nzcv & 8) { /* N */
5241         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5242     } else {
5243         if (TCG_TARGET_HAS_andc_i32) {
5244             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5245         } else {
5246             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5247         }
5248     }
5249     if (nzcv & 4) { /* Z */
5250         if (TCG_TARGET_HAS_andc_i32) {
5251             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5252         } else {
5253             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5254         }
5255     } else {
5256         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5257     }
5258     if (nzcv & 2) { /* C */
5259         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5260     } else {
5261         if (TCG_TARGET_HAS_andc_i32) {
5262             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5263         } else {
5264             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5265         }
5266     }
5267     if (nzcv & 1) { /* V */
5268         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5269     } else {
5270         if (TCG_TARGET_HAS_andc_i32) {
5271             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5272         } else {
5273             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5274         }
5275     }
5276 }
5277 
5278 /* Conditional select
5279  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5280  * +----+----+---+-----------------+------+------+-----+------+------+
5281  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5282  * +----+----+---+-----------------+------+------+-----+------+------+
5283  */
5284 static void disas_cond_select(DisasContext *s, uint32_t insn)
5285 {
5286     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5287     TCGv_i64 tcg_rd, zero;
5288     DisasCompare64 c;
5289 
5290     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5291         /* S == 1 or op2<1> == 1 */
5292         unallocated_encoding(s);
5293         return;
5294     }
5295     sf = extract32(insn, 31, 1);
5296     else_inv = extract32(insn, 30, 1);
5297     rm = extract32(insn, 16, 5);
5298     cond = extract32(insn, 12, 4);
5299     else_inc = extract32(insn, 10, 1);
5300     rn = extract32(insn, 5, 5);
5301     rd = extract32(insn, 0, 5);
5302 
5303     tcg_rd = cpu_reg(s, rd);
5304 
5305     a64_test_cc(&c, cond);
5306     zero = tcg_constant_i64(0);
5307 
5308     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5309         /* CSET & CSETM.  */
5310         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5311         if (else_inv) {
5312             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5313         }
5314     } else {
5315         TCGv_i64 t_true = cpu_reg(s, rn);
5316         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5317         if (else_inv && else_inc) {
5318             tcg_gen_neg_i64(t_false, t_false);
5319         } else if (else_inv) {
5320             tcg_gen_not_i64(t_false, t_false);
5321         } else if (else_inc) {
5322             tcg_gen_addi_i64(t_false, t_false, 1);
5323         }
5324         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5325     }
5326 
5327     if (!sf) {
5328         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5329     }
5330 }
5331 
5332 static void handle_clz(DisasContext *s, unsigned int sf,
5333                        unsigned int rn, unsigned int rd)
5334 {
5335     TCGv_i64 tcg_rd, tcg_rn;
5336     tcg_rd = cpu_reg(s, rd);
5337     tcg_rn = cpu_reg(s, rn);
5338 
5339     if (sf) {
5340         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5341     } else {
5342         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5343         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5344         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5345         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5346     }
5347 }
5348 
5349 static void handle_cls(DisasContext *s, unsigned int sf,
5350                        unsigned int rn, unsigned int rd)
5351 {
5352     TCGv_i64 tcg_rd, tcg_rn;
5353     tcg_rd = cpu_reg(s, rd);
5354     tcg_rn = cpu_reg(s, rn);
5355 
5356     if (sf) {
5357         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5358     } else {
5359         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5360         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5361         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5362         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5363     }
5364 }
5365 
5366 static void handle_rbit(DisasContext *s, unsigned int sf,
5367                         unsigned int rn, unsigned int rd)
5368 {
5369     TCGv_i64 tcg_rd, tcg_rn;
5370     tcg_rd = cpu_reg(s, rd);
5371     tcg_rn = cpu_reg(s, rn);
5372 
5373     if (sf) {
5374         gen_helper_rbit64(tcg_rd, tcg_rn);
5375     } else {
5376         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5377         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5378         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5379         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5380     }
5381 }
5382 
5383 /* REV with sf==1, opcode==3 ("REV64") */
5384 static void handle_rev64(DisasContext *s, unsigned int sf,
5385                          unsigned int rn, unsigned int rd)
5386 {
5387     if (!sf) {
5388         unallocated_encoding(s);
5389         return;
5390     }
5391     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5392 }
5393 
5394 /* REV with sf==0, opcode==2
5395  * REV32 (sf==1, opcode==2)
5396  */
5397 static void handle_rev32(DisasContext *s, unsigned int sf,
5398                          unsigned int rn, unsigned int rd)
5399 {
5400     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5401     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5402 
5403     if (sf) {
5404         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5405         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5406     } else {
5407         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5408     }
5409 }
5410 
5411 /* REV16 (opcode==1) */
5412 static void handle_rev16(DisasContext *s, unsigned int sf,
5413                          unsigned int rn, unsigned int rd)
5414 {
5415     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5416     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5417     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5418     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5419 
5420     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5421     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5422     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5423     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5424     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5425 }
5426 
5427 /* Data-processing (1 source)
5428  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5429  * +----+---+---+-----------------+---------+--------+------+------+
5430  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5431  * +----+---+---+-----------------+---------+--------+------+------+
5432  */
5433 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5434 {
5435     unsigned int sf, opcode, opcode2, rn, rd;
5436     TCGv_i64 tcg_rd;
5437 
5438     if (extract32(insn, 29, 1)) {
5439         unallocated_encoding(s);
5440         return;
5441     }
5442 
5443     sf = extract32(insn, 31, 1);
5444     opcode = extract32(insn, 10, 6);
5445     opcode2 = extract32(insn, 16, 5);
5446     rn = extract32(insn, 5, 5);
5447     rd = extract32(insn, 0, 5);
5448 
5449 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5450 
5451     switch (MAP(sf, opcode2, opcode)) {
5452     case MAP(0, 0x00, 0x00): /* RBIT */
5453     case MAP(1, 0x00, 0x00):
5454         handle_rbit(s, sf, rn, rd);
5455         break;
5456     case MAP(0, 0x00, 0x01): /* REV16 */
5457     case MAP(1, 0x00, 0x01):
5458         handle_rev16(s, sf, rn, rd);
5459         break;
5460     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5461     case MAP(1, 0x00, 0x02):
5462         handle_rev32(s, sf, rn, rd);
5463         break;
5464     case MAP(1, 0x00, 0x03): /* REV64 */
5465         handle_rev64(s, sf, rn, rd);
5466         break;
5467     case MAP(0, 0x00, 0x04): /* CLZ */
5468     case MAP(1, 0x00, 0x04):
5469         handle_clz(s, sf, rn, rd);
5470         break;
5471     case MAP(0, 0x00, 0x05): /* CLS */
5472     case MAP(1, 0x00, 0x05):
5473         handle_cls(s, sf, rn, rd);
5474         break;
5475     case MAP(1, 0x01, 0x00): /* PACIA */
5476         if (s->pauth_active) {
5477             tcg_rd = cpu_reg(s, rd);
5478             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5479         } else if (!dc_isar_feature(aa64_pauth, s)) {
5480             goto do_unallocated;
5481         }
5482         break;
5483     case MAP(1, 0x01, 0x01): /* PACIB */
5484         if (s->pauth_active) {
5485             tcg_rd = cpu_reg(s, rd);
5486             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5487         } else if (!dc_isar_feature(aa64_pauth, s)) {
5488             goto do_unallocated;
5489         }
5490         break;
5491     case MAP(1, 0x01, 0x02): /* PACDA */
5492         if (s->pauth_active) {
5493             tcg_rd = cpu_reg(s, rd);
5494             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5495         } else if (!dc_isar_feature(aa64_pauth, s)) {
5496             goto do_unallocated;
5497         }
5498         break;
5499     case MAP(1, 0x01, 0x03): /* PACDB */
5500         if (s->pauth_active) {
5501             tcg_rd = cpu_reg(s, rd);
5502             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5503         } else if (!dc_isar_feature(aa64_pauth, s)) {
5504             goto do_unallocated;
5505         }
5506         break;
5507     case MAP(1, 0x01, 0x04): /* AUTIA */
5508         if (s->pauth_active) {
5509             tcg_rd = cpu_reg(s, rd);
5510             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5511         } else if (!dc_isar_feature(aa64_pauth, s)) {
5512             goto do_unallocated;
5513         }
5514         break;
5515     case MAP(1, 0x01, 0x05): /* AUTIB */
5516         if (s->pauth_active) {
5517             tcg_rd = cpu_reg(s, rd);
5518             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5519         } else if (!dc_isar_feature(aa64_pauth, s)) {
5520             goto do_unallocated;
5521         }
5522         break;
5523     case MAP(1, 0x01, 0x06): /* AUTDA */
5524         if (s->pauth_active) {
5525             tcg_rd = cpu_reg(s, rd);
5526             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5527         } else if (!dc_isar_feature(aa64_pauth, s)) {
5528             goto do_unallocated;
5529         }
5530         break;
5531     case MAP(1, 0x01, 0x07): /* AUTDB */
5532         if (s->pauth_active) {
5533             tcg_rd = cpu_reg(s, rd);
5534             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5535         } else if (!dc_isar_feature(aa64_pauth, s)) {
5536             goto do_unallocated;
5537         }
5538         break;
5539     case MAP(1, 0x01, 0x08): /* PACIZA */
5540         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5541             goto do_unallocated;
5542         } else if (s->pauth_active) {
5543             tcg_rd = cpu_reg(s, rd);
5544             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5545         }
5546         break;
5547     case MAP(1, 0x01, 0x09): /* PACIZB */
5548         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5549             goto do_unallocated;
5550         } else if (s->pauth_active) {
5551             tcg_rd = cpu_reg(s, rd);
5552             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5553         }
5554         break;
5555     case MAP(1, 0x01, 0x0a): /* PACDZA */
5556         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5557             goto do_unallocated;
5558         } else if (s->pauth_active) {
5559             tcg_rd = cpu_reg(s, rd);
5560             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5561         }
5562         break;
5563     case MAP(1, 0x01, 0x0b): /* PACDZB */
5564         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5565             goto do_unallocated;
5566         } else if (s->pauth_active) {
5567             tcg_rd = cpu_reg(s, rd);
5568             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5569         }
5570         break;
5571     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5572         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5573             goto do_unallocated;
5574         } else if (s->pauth_active) {
5575             tcg_rd = cpu_reg(s, rd);
5576             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5577         }
5578         break;
5579     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5580         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5581             goto do_unallocated;
5582         } else if (s->pauth_active) {
5583             tcg_rd = cpu_reg(s, rd);
5584             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5585         }
5586         break;
5587     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5588         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5589             goto do_unallocated;
5590         } else if (s->pauth_active) {
5591             tcg_rd = cpu_reg(s, rd);
5592             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5593         }
5594         break;
5595     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5596         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5597             goto do_unallocated;
5598         } else if (s->pauth_active) {
5599             tcg_rd = cpu_reg(s, rd);
5600             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5601         }
5602         break;
5603     case MAP(1, 0x01, 0x10): /* XPACI */
5604         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5605             goto do_unallocated;
5606         } else if (s->pauth_active) {
5607             tcg_rd = cpu_reg(s, rd);
5608             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5609         }
5610         break;
5611     case MAP(1, 0x01, 0x11): /* XPACD */
5612         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5613             goto do_unallocated;
5614         } else if (s->pauth_active) {
5615             tcg_rd = cpu_reg(s, rd);
5616             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5617         }
5618         break;
5619     default:
5620     do_unallocated:
5621         unallocated_encoding(s);
5622         break;
5623     }
5624 
5625 #undef MAP
5626 }
5627 
5628 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5629                        unsigned int rm, unsigned int rn, unsigned int rd)
5630 {
5631     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5632     tcg_rd = cpu_reg(s, rd);
5633 
5634     if (!sf && is_signed) {
5635         tcg_n = tcg_temp_new_i64();
5636         tcg_m = tcg_temp_new_i64();
5637         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5638         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5639     } else {
5640         tcg_n = read_cpu_reg(s, rn, sf);
5641         tcg_m = read_cpu_reg(s, rm, sf);
5642     }
5643 
5644     if (is_signed) {
5645         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5646     } else {
5647         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5648     }
5649 
5650     if (!sf) { /* zero extend final result */
5651         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5652     }
5653 }
5654 
5655 /* LSLV, LSRV, ASRV, RORV */
5656 static void handle_shift_reg(DisasContext *s,
5657                              enum a64_shift_type shift_type, unsigned int sf,
5658                              unsigned int rm, unsigned int rn, unsigned int rd)
5659 {
5660     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5661     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5662     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5663 
5664     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5665     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5666 }
5667 
5668 /* CRC32[BHWX], CRC32C[BHWX] */
5669 static void handle_crc32(DisasContext *s,
5670                          unsigned int sf, unsigned int sz, bool crc32c,
5671                          unsigned int rm, unsigned int rn, unsigned int rd)
5672 {
5673     TCGv_i64 tcg_acc, tcg_val;
5674     TCGv_i32 tcg_bytes;
5675 
5676     if (!dc_isar_feature(aa64_crc32, s)
5677         || (sf == 1 && sz != 3)
5678         || (sf == 0 && sz == 3)) {
5679         unallocated_encoding(s);
5680         return;
5681     }
5682 
5683     if (sz == 3) {
5684         tcg_val = cpu_reg(s, rm);
5685     } else {
5686         uint64_t mask;
5687         switch (sz) {
5688         case 0:
5689             mask = 0xFF;
5690             break;
5691         case 1:
5692             mask = 0xFFFF;
5693             break;
5694         case 2:
5695             mask = 0xFFFFFFFF;
5696             break;
5697         default:
5698             g_assert_not_reached();
5699         }
5700         tcg_val = tcg_temp_new_i64();
5701         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5702     }
5703 
5704     tcg_acc = cpu_reg(s, rn);
5705     tcg_bytes = tcg_constant_i32(1 << sz);
5706 
5707     if (crc32c) {
5708         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5709     } else {
5710         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5711     }
5712 }
5713 
5714 /* Data-processing (2 source)
5715  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5716  * +----+---+---+-----------------+------+--------+------+------+
5717  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5718  * +----+---+---+-----------------+------+--------+------+------+
5719  */
5720 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5721 {
5722     unsigned int sf, rm, opcode, rn, rd, setflag;
5723     sf = extract32(insn, 31, 1);
5724     setflag = extract32(insn, 29, 1);
5725     rm = extract32(insn, 16, 5);
5726     opcode = extract32(insn, 10, 6);
5727     rn = extract32(insn, 5, 5);
5728     rd = extract32(insn, 0, 5);
5729 
5730     if (setflag && opcode != 0) {
5731         unallocated_encoding(s);
5732         return;
5733     }
5734 
5735     switch (opcode) {
5736     case 0: /* SUBP(S) */
5737         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5738             goto do_unallocated;
5739         } else {
5740             TCGv_i64 tcg_n, tcg_m, tcg_d;
5741 
5742             tcg_n = read_cpu_reg_sp(s, rn, true);
5743             tcg_m = read_cpu_reg_sp(s, rm, true);
5744             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5745             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5746             tcg_d = cpu_reg(s, rd);
5747 
5748             if (setflag) {
5749                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5750             } else {
5751                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5752             }
5753         }
5754         break;
5755     case 2: /* UDIV */
5756         handle_div(s, false, sf, rm, rn, rd);
5757         break;
5758     case 3: /* SDIV */
5759         handle_div(s, true, sf, rm, rn, rd);
5760         break;
5761     case 4: /* IRG */
5762         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5763             goto do_unallocated;
5764         }
5765         if (s->ata) {
5766             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5767                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5768         } else {
5769             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5770                                              cpu_reg_sp(s, rn));
5771         }
5772         break;
5773     case 5: /* GMI */
5774         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5775             goto do_unallocated;
5776         } else {
5777             TCGv_i64 t = tcg_temp_new_i64();
5778 
5779             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5780             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5781             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5782         }
5783         break;
5784     case 8: /* LSLV */
5785         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5786         break;
5787     case 9: /* LSRV */
5788         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5789         break;
5790     case 10: /* ASRV */
5791         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5792         break;
5793     case 11: /* RORV */
5794         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5795         break;
5796     case 12: /* PACGA */
5797         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5798             goto do_unallocated;
5799         }
5800         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5801                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5802         break;
5803     case 16:
5804     case 17:
5805     case 18:
5806     case 19:
5807     case 20:
5808     case 21:
5809     case 22:
5810     case 23: /* CRC32 */
5811     {
5812         int sz = extract32(opcode, 0, 2);
5813         bool crc32c = extract32(opcode, 2, 1);
5814         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5815         break;
5816     }
5817     default:
5818     do_unallocated:
5819         unallocated_encoding(s);
5820         break;
5821     }
5822 }
5823 
5824 /*
5825  * Data processing - register
5826  *  31  30 29  28      25    21  20  16      10         0
5827  * +--+---+--+---+-------+-----+-------+-------+---------+
5828  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5829  * +--+---+--+---+-------+-----+-------+-------+---------+
5830  */
5831 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5832 {
5833     int op0 = extract32(insn, 30, 1);
5834     int op1 = extract32(insn, 28, 1);
5835     int op2 = extract32(insn, 21, 4);
5836     int op3 = extract32(insn, 10, 6);
5837 
5838     if (!op1) {
5839         if (op2 & 8) {
5840             if (op2 & 1) {
5841                 /* Add/sub (extended register) */
5842                 disas_add_sub_ext_reg(s, insn);
5843             } else {
5844                 /* Add/sub (shifted register) */
5845                 disas_add_sub_reg(s, insn);
5846             }
5847         } else {
5848             /* Logical (shifted register) */
5849             disas_logic_reg(s, insn);
5850         }
5851         return;
5852     }
5853 
5854     switch (op2) {
5855     case 0x0:
5856         switch (op3) {
5857         case 0x00: /* Add/subtract (with carry) */
5858             disas_adc_sbc(s, insn);
5859             break;
5860 
5861         case 0x01: /* Rotate right into flags */
5862         case 0x21:
5863             disas_rotate_right_into_flags(s, insn);
5864             break;
5865 
5866         case 0x02: /* Evaluate into flags */
5867         case 0x12:
5868         case 0x22:
5869         case 0x32:
5870             disas_evaluate_into_flags(s, insn);
5871             break;
5872 
5873         default:
5874             goto do_unallocated;
5875         }
5876         break;
5877 
5878     case 0x2: /* Conditional compare */
5879         disas_cc(s, insn); /* both imm and reg forms */
5880         break;
5881 
5882     case 0x4: /* Conditional select */
5883         disas_cond_select(s, insn);
5884         break;
5885 
5886     case 0x6: /* Data-processing */
5887         if (op0) {    /* (1 source) */
5888             disas_data_proc_1src(s, insn);
5889         } else {      /* (2 source) */
5890             disas_data_proc_2src(s, insn);
5891         }
5892         break;
5893     case 0x8 ... 0xf: /* (3 source) */
5894         disas_data_proc_3src(s, insn);
5895         break;
5896 
5897     default:
5898     do_unallocated:
5899         unallocated_encoding(s);
5900         break;
5901     }
5902 }
5903 
5904 static void handle_fp_compare(DisasContext *s, int size,
5905                               unsigned int rn, unsigned int rm,
5906                               bool cmp_with_zero, bool signal_all_nans)
5907 {
5908     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5909     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5910 
5911     if (size == MO_64) {
5912         TCGv_i64 tcg_vn, tcg_vm;
5913 
5914         tcg_vn = read_fp_dreg(s, rn);
5915         if (cmp_with_zero) {
5916             tcg_vm = tcg_constant_i64(0);
5917         } else {
5918             tcg_vm = read_fp_dreg(s, rm);
5919         }
5920         if (signal_all_nans) {
5921             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5922         } else {
5923             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5924         }
5925     } else {
5926         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5927         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5928 
5929         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5930         if (cmp_with_zero) {
5931             tcg_gen_movi_i32(tcg_vm, 0);
5932         } else {
5933             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5934         }
5935 
5936         switch (size) {
5937         case MO_32:
5938             if (signal_all_nans) {
5939                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5940             } else {
5941                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5942             }
5943             break;
5944         case MO_16:
5945             if (signal_all_nans) {
5946                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5947             } else {
5948                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5949             }
5950             break;
5951         default:
5952             g_assert_not_reached();
5953         }
5954     }
5955 
5956     gen_set_nzcv(tcg_flags);
5957 }
5958 
5959 /* Floating point compare
5960  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5961  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5962  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5963  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5964  */
5965 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5966 {
5967     unsigned int mos, type, rm, op, rn, opc, op2r;
5968     int size;
5969 
5970     mos = extract32(insn, 29, 3);
5971     type = extract32(insn, 22, 2);
5972     rm = extract32(insn, 16, 5);
5973     op = extract32(insn, 14, 2);
5974     rn = extract32(insn, 5, 5);
5975     opc = extract32(insn, 3, 2);
5976     op2r = extract32(insn, 0, 3);
5977 
5978     if (mos || op || op2r) {
5979         unallocated_encoding(s);
5980         return;
5981     }
5982 
5983     switch (type) {
5984     case 0:
5985         size = MO_32;
5986         break;
5987     case 1:
5988         size = MO_64;
5989         break;
5990     case 3:
5991         size = MO_16;
5992         if (dc_isar_feature(aa64_fp16, s)) {
5993             break;
5994         }
5995         /* fallthru */
5996     default:
5997         unallocated_encoding(s);
5998         return;
5999     }
6000 
6001     if (!fp_access_check(s)) {
6002         return;
6003     }
6004 
6005     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6006 }
6007 
6008 /* Floating point conditional compare
6009  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6010  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6011  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6012  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6013  */
6014 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6015 {
6016     unsigned int mos, type, rm, cond, rn, op, nzcv;
6017     TCGLabel *label_continue = NULL;
6018     int size;
6019 
6020     mos = extract32(insn, 29, 3);
6021     type = extract32(insn, 22, 2);
6022     rm = extract32(insn, 16, 5);
6023     cond = extract32(insn, 12, 4);
6024     rn = extract32(insn, 5, 5);
6025     op = extract32(insn, 4, 1);
6026     nzcv = extract32(insn, 0, 4);
6027 
6028     if (mos) {
6029         unallocated_encoding(s);
6030         return;
6031     }
6032 
6033     switch (type) {
6034     case 0:
6035         size = MO_32;
6036         break;
6037     case 1:
6038         size = MO_64;
6039         break;
6040     case 3:
6041         size = MO_16;
6042         if (dc_isar_feature(aa64_fp16, s)) {
6043             break;
6044         }
6045         /* fallthru */
6046     default:
6047         unallocated_encoding(s);
6048         return;
6049     }
6050 
6051     if (!fp_access_check(s)) {
6052         return;
6053     }
6054 
6055     if (cond < 0x0e) { /* not always */
6056         TCGLabel *label_match = gen_new_label();
6057         label_continue = gen_new_label();
6058         arm_gen_test_cc(cond, label_match);
6059         /* nomatch: */
6060         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6061         tcg_gen_br(label_continue);
6062         gen_set_label(label_match);
6063     }
6064 
6065     handle_fp_compare(s, size, rn, rm, false, op);
6066 
6067     if (cond < 0x0e) {
6068         gen_set_label(label_continue);
6069     }
6070 }
6071 
6072 /* Floating point conditional select
6073  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6074  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6075  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6076  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6077  */
6078 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6079 {
6080     unsigned int mos, type, rm, cond, rn, rd;
6081     TCGv_i64 t_true, t_false;
6082     DisasCompare64 c;
6083     MemOp sz;
6084 
6085     mos = extract32(insn, 29, 3);
6086     type = extract32(insn, 22, 2);
6087     rm = extract32(insn, 16, 5);
6088     cond = extract32(insn, 12, 4);
6089     rn = extract32(insn, 5, 5);
6090     rd = extract32(insn, 0, 5);
6091 
6092     if (mos) {
6093         unallocated_encoding(s);
6094         return;
6095     }
6096 
6097     switch (type) {
6098     case 0:
6099         sz = MO_32;
6100         break;
6101     case 1:
6102         sz = MO_64;
6103         break;
6104     case 3:
6105         sz = MO_16;
6106         if (dc_isar_feature(aa64_fp16, s)) {
6107             break;
6108         }
6109         /* fallthru */
6110     default:
6111         unallocated_encoding(s);
6112         return;
6113     }
6114 
6115     if (!fp_access_check(s)) {
6116         return;
6117     }
6118 
6119     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6120     t_true = tcg_temp_new_i64();
6121     t_false = tcg_temp_new_i64();
6122     read_vec_element(s, t_true, rn, 0, sz);
6123     read_vec_element(s, t_false, rm, 0, sz);
6124 
6125     a64_test_cc(&c, cond);
6126     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6127                         t_true, t_false);
6128 
6129     /* Note that sregs & hregs write back zeros to the high bits,
6130        and we've already done the zero-extension.  */
6131     write_fp_dreg(s, rd, t_true);
6132 }
6133 
6134 /* Floating-point data-processing (1 source) - half precision */
6135 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6136 {
6137     TCGv_ptr fpst = NULL;
6138     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6139     TCGv_i32 tcg_res = tcg_temp_new_i32();
6140 
6141     switch (opcode) {
6142     case 0x0: /* FMOV */
6143         tcg_gen_mov_i32(tcg_res, tcg_op);
6144         break;
6145     case 0x1: /* FABS */
6146         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6147         break;
6148     case 0x2: /* FNEG */
6149         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6150         break;
6151     case 0x3: /* FSQRT */
6152         fpst = fpstatus_ptr(FPST_FPCR_F16);
6153         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6154         break;
6155     case 0x8: /* FRINTN */
6156     case 0x9: /* FRINTP */
6157     case 0xa: /* FRINTM */
6158     case 0xb: /* FRINTZ */
6159     case 0xc: /* FRINTA */
6160     {
6161         TCGv_i32 tcg_rmode;
6162 
6163         fpst = fpstatus_ptr(FPST_FPCR_F16);
6164         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6165         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6166         gen_restore_rmode(tcg_rmode, fpst);
6167         break;
6168     }
6169     case 0xe: /* FRINTX */
6170         fpst = fpstatus_ptr(FPST_FPCR_F16);
6171         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6172         break;
6173     case 0xf: /* FRINTI */
6174         fpst = fpstatus_ptr(FPST_FPCR_F16);
6175         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6176         break;
6177     default:
6178         g_assert_not_reached();
6179     }
6180 
6181     write_fp_sreg(s, rd, tcg_res);
6182 }
6183 
6184 /* Floating-point data-processing (1 source) - single precision */
6185 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6186 {
6187     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6188     TCGv_i32 tcg_op, tcg_res;
6189     TCGv_ptr fpst;
6190     int rmode = -1;
6191 
6192     tcg_op = read_fp_sreg(s, rn);
6193     tcg_res = tcg_temp_new_i32();
6194 
6195     switch (opcode) {
6196     case 0x0: /* FMOV */
6197         tcg_gen_mov_i32(tcg_res, tcg_op);
6198         goto done;
6199     case 0x1: /* FABS */
6200         gen_helper_vfp_abss(tcg_res, tcg_op);
6201         goto done;
6202     case 0x2: /* FNEG */
6203         gen_helper_vfp_negs(tcg_res, tcg_op);
6204         goto done;
6205     case 0x3: /* FSQRT */
6206         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6207         goto done;
6208     case 0x6: /* BFCVT */
6209         gen_fpst = gen_helper_bfcvt;
6210         break;
6211     case 0x8: /* FRINTN */
6212     case 0x9: /* FRINTP */
6213     case 0xa: /* FRINTM */
6214     case 0xb: /* FRINTZ */
6215     case 0xc: /* FRINTA */
6216         rmode = opcode & 7;
6217         gen_fpst = gen_helper_rints;
6218         break;
6219     case 0xe: /* FRINTX */
6220         gen_fpst = gen_helper_rints_exact;
6221         break;
6222     case 0xf: /* FRINTI */
6223         gen_fpst = gen_helper_rints;
6224         break;
6225     case 0x10: /* FRINT32Z */
6226         rmode = FPROUNDING_ZERO;
6227         gen_fpst = gen_helper_frint32_s;
6228         break;
6229     case 0x11: /* FRINT32X */
6230         gen_fpst = gen_helper_frint32_s;
6231         break;
6232     case 0x12: /* FRINT64Z */
6233         rmode = FPROUNDING_ZERO;
6234         gen_fpst = gen_helper_frint64_s;
6235         break;
6236     case 0x13: /* FRINT64X */
6237         gen_fpst = gen_helper_frint64_s;
6238         break;
6239     default:
6240         g_assert_not_reached();
6241     }
6242 
6243     fpst = fpstatus_ptr(FPST_FPCR);
6244     if (rmode >= 0) {
6245         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6246         gen_fpst(tcg_res, tcg_op, fpst);
6247         gen_restore_rmode(tcg_rmode, fpst);
6248     } else {
6249         gen_fpst(tcg_res, tcg_op, fpst);
6250     }
6251 
6252  done:
6253     write_fp_sreg(s, rd, tcg_res);
6254 }
6255 
6256 /* Floating-point data-processing (1 source) - double precision */
6257 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6258 {
6259     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6260     TCGv_i64 tcg_op, tcg_res;
6261     TCGv_ptr fpst;
6262     int rmode = -1;
6263 
6264     switch (opcode) {
6265     case 0x0: /* FMOV */
6266         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6267         return;
6268     }
6269 
6270     tcg_op = read_fp_dreg(s, rn);
6271     tcg_res = tcg_temp_new_i64();
6272 
6273     switch (opcode) {
6274     case 0x1: /* FABS */
6275         gen_helper_vfp_absd(tcg_res, tcg_op);
6276         goto done;
6277     case 0x2: /* FNEG */
6278         gen_helper_vfp_negd(tcg_res, tcg_op);
6279         goto done;
6280     case 0x3: /* FSQRT */
6281         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6282         goto done;
6283     case 0x8: /* FRINTN */
6284     case 0x9: /* FRINTP */
6285     case 0xa: /* FRINTM */
6286     case 0xb: /* FRINTZ */
6287     case 0xc: /* FRINTA */
6288         rmode = opcode & 7;
6289         gen_fpst = gen_helper_rintd;
6290         break;
6291     case 0xe: /* FRINTX */
6292         gen_fpst = gen_helper_rintd_exact;
6293         break;
6294     case 0xf: /* FRINTI */
6295         gen_fpst = gen_helper_rintd;
6296         break;
6297     case 0x10: /* FRINT32Z */
6298         rmode = FPROUNDING_ZERO;
6299         gen_fpst = gen_helper_frint32_d;
6300         break;
6301     case 0x11: /* FRINT32X */
6302         gen_fpst = gen_helper_frint32_d;
6303         break;
6304     case 0x12: /* FRINT64Z */
6305         rmode = FPROUNDING_ZERO;
6306         gen_fpst = gen_helper_frint64_d;
6307         break;
6308     case 0x13: /* FRINT64X */
6309         gen_fpst = gen_helper_frint64_d;
6310         break;
6311     default:
6312         g_assert_not_reached();
6313     }
6314 
6315     fpst = fpstatus_ptr(FPST_FPCR);
6316     if (rmode >= 0) {
6317         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6318         gen_fpst(tcg_res, tcg_op, fpst);
6319         gen_restore_rmode(tcg_rmode, fpst);
6320     } else {
6321         gen_fpst(tcg_res, tcg_op, fpst);
6322     }
6323 
6324  done:
6325     write_fp_dreg(s, rd, tcg_res);
6326 }
6327 
6328 static void handle_fp_fcvt(DisasContext *s, int opcode,
6329                            int rd, int rn, int dtype, int ntype)
6330 {
6331     switch (ntype) {
6332     case 0x0:
6333     {
6334         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6335         if (dtype == 1) {
6336             /* Single to double */
6337             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6338             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6339             write_fp_dreg(s, rd, tcg_rd);
6340         } else {
6341             /* Single to half */
6342             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6343             TCGv_i32 ahp = get_ahp_flag();
6344             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6345 
6346             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6347             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6348             write_fp_sreg(s, rd, tcg_rd);
6349         }
6350         break;
6351     }
6352     case 0x1:
6353     {
6354         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6355         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6356         if (dtype == 0) {
6357             /* Double to single */
6358             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6359         } else {
6360             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6361             TCGv_i32 ahp = get_ahp_flag();
6362             /* Double to half */
6363             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6364             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6365         }
6366         write_fp_sreg(s, rd, tcg_rd);
6367         break;
6368     }
6369     case 0x3:
6370     {
6371         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6372         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6373         TCGv_i32 tcg_ahp = get_ahp_flag();
6374         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6375         if (dtype == 0) {
6376             /* Half to single */
6377             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6378             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6379             write_fp_sreg(s, rd, tcg_rd);
6380         } else {
6381             /* Half to double */
6382             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6383             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6384             write_fp_dreg(s, rd, tcg_rd);
6385         }
6386         break;
6387     }
6388     default:
6389         g_assert_not_reached();
6390     }
6391 }
6392 
6393 /* Floating point data-processing (1 source)
6394  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6395  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6396  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6397  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6398  */
6399 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6400 {
6401     int mos = extract32(insn, 29, 3);
6402     int type = extract32(insn, 22, 2);
6403     int opcode = extract32(insn, 15, 6);
6404     int rn = extract32(insn, 5, 5);
6405     int rd = extract32(insn, 0, 5);
6406 
6407     if (mos) {
6408         goto do_unallocated;
6409     }
6410 
6411     switch (opcode) {
6412     case 0x4: case 0x5: case 0x7:
6413     {
6414         /* FCVT between half, single and double precision */
6415         int dtype = extract32(opcode, 0, 2);
6416         if (type == 2 || dtype == type) {
6417             goto do_unallocated;
6418         }
6419         if (!fp_access_check(s)) {
6420             return;
6421         }
6422 
6423         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6424         break;
6425     }
6426 
6427     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6428         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6429             goto do_unallocated;
6430         }
6431         /* fall through */
6432     case 0x0 ... 0x3:
6433     case 0x8 ... 0xc:
6434     case 0xe ... 0xf:
6435         /* 32-to-32 and 64-to-64 ops */
6436         switch (type) {
6437         case 0:
6438             if (!fp_access_check(s)) {
6439                 return;
6440             }
6441             handle_fp_1src_single(s, opcode, rd, rn);
6442             break;
6443         case 1:
6444             if (!fp_access_check(s)) {
6445                 return;
6446             }
6447             handle_fp_1src_double(s, opcode, rd, rn);
6448             break;
6449         case 3:
6450             if (!dc_isar_feature(aa64_fp16, s)) {
6451                 goto do_unallocated;
6452             }
6453 
6454             if (!fp_access_check(s)) {
6455                 return;
6456             }
6457             handle_fp_1src_half(s, opcode, rd, rn);
6458             break;
6459         default:
6460             goto do_unallocated;
6461         }
6462         break;
6463 
6464     case 0x6:
6465         switch (type) {
6466         case 1: /* BFCVT */
6467             if (!dc_isar_feature(aa64_bf16, s)) {
6468                 goto do_unallocated;
6469             }
6470             if (!fp_access_check(s)) {
6471                 return;
6472             }
6473             handle_fp_1src_single(s, opcode, rd, rn);
6474             break;
6475         default:
6476             goto do_unallocated;
6477         }
6478         break;
6479 
6480     default:
6481     do_unallocated:
6482         unallocated_encoding(s);
6483         break;
6484     }
6485 }
6486 
6487 /* Floating-point data-processing (2 source) - single precision */
6488 static void handle_fp_2src_single(DisasContext *s, int opcode,
6489                                   int rd, int rn, int rm)
6490 {
6491     TCGv_i32 tcg_op1;
6492     TCGv_i32 tcg_op2;
6493     TCGv_i32 tcg_res;
6494     TCGv_ptr fpst;
6495 
6496     tcg_res = tcg_temp_new_i32();
6497     fpst = fpstatus_ptr(FPST_FPCR);
6498     tcg_op1 = read_fp_sreg(s, rn);
6499     tcg_op2 = read_fp_sreg(s, rm);
6500 
6501     switch (opcode) {
6502     case 0x0: /* FMUL */
6503         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6504         break;
6505     case 0x1: /* FDIV */
6506         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6507         break;
6508     case 0x2: /* FADD */
6509         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6510         break;
6511     case 0x3: /* FSUB */
6512         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6513         break;
6514     case 0x4: /* FMAX */
6515         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6516         break;
6517     case 0x5: /* FMIN */
6518         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6519         break;
6520     case 0x6: /* FMAXNM */
6521         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6522         break;
6523     case 0x7: /* FMINNM */
6524         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6525         break;
6526     case 0x8: /* FNMUL */
6527         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6528         gen_helper_vfp_negs(tcg_res, tcg_res);
6529         break;
6530     }
6531 
6532     write_fp_sreg(s, rd, tcg_res);
6533 }
6534 
6535 /* Floating-point data-processing (2 source) - double precision */
6536 static void handle_fp_2src_double(DisasContext *s, int opcode,
6537                                   int rd, int rn, int rm)
6538 {
6539     TCGv_i64 tcg_op1;
6540     TCGv_i64 tcg_op2;
6541     TCGv_i64 tcg_res;
6542     TCGv_ptr fpst;
6543 
6544     tcg_res = tcg_temp_new_i64();
6545     fpst = fpstatus_ptr(FPST_FPCR);
6546     tcg_op1 = read_fp_dreg(s, rn);
6547     tcg_op2 = read_fp_dreg(s, rm);
6548 
6549     switch (opcode) {
6550     case 0x0: /* FMUL */
6551         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6552         break;
6553     case 0x1: /* FDIV */
6554         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6555         break;
6556     case 0x2: /* FADD */
6557         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6558         break;
6559     case 0x3: /* FSUB */
6560         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6561         break;
6562     case 0x4: /* FMAX */
6563         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6564         break;
6565     case 0x5: /* FMIN */
6566         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6567         break;
6568     case 0x6: /* FMAXNM */
6569         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6570         break;
6571     case 0x7: /* FMINNM */
6572         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6573         break;
6574     case 0x8: /* FNMUL */
6575         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6576         gen_helper_vfp_negd(tcg_res, tcg_res);
6577         break;
6578     }
6579 
6580     write_fp_dreg(s, rd, tcg_res);
6581 }
6582 
6583 /* Floating-point data-processing (2 source) - half precision */
6584 static void handle_fp_2src_half(DisasContext *s, int opcode,
6585                                 int rd, int rn, int rm)
6586 {
6587     TCGv_i32 tcg_op1;
6588     TCGv_i32 tcg_op2;
6589     TCGv_i32 tcg_res;
6590     TCGv_ptr fpst;
6591 
6592     tcg_res = tcg_temp_new_i32();
6593     fpst = fpstatus_ptr(FPST_FPCR_F16);
6594     tcg_op1 = read_fp_hreg(s, rn);
6595     tcg_op2 = read_fp_hreg(s, rm);
6596 
6597     switch (opcode) {
6598     case 0x0: /* FMUL */
6599         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6600         break;
6601     case 0x1: /* FDIV */
6602         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6603         break;
6604     case 0x2: /* FADD */
6605         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6606         break;
6607     case 0x3: /* FSUB */
6608         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6609         break;
6610     case 0x4: /* FMAX */
6611         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6612         break;
6613     case 0x5: /* FMIN */
6614         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6615         break;
6616     case 0x6: /* FMAXNM */
6617         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6618         break;
6619     case 0x7: /* FMINNM */
6620         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6621         break;
6622     case 0x8: /* FNMUL */
6623         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6624         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6625         break;
6626     default:
6627         g_assert_not_reached();
6628     }
6629 
6630     write_fp_sreg(s, rd, tcg_res);
6631 }
6632 
6633 /* Floating point data-processing (2 source)
6634  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6635  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6636  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6637  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6638  */
6639 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6640 {
6641     int mos = extract32(insn, 29, 3);
6642     int type = extract32(insn, 22, 2);
6643     int rd = extract32(insn, 0, 5);
6644     int rn = extract32(insn, 5, 5);
6645     int rm = extract32(insn, 16, 5);
6646     int opcode = extract32(insn, 12, 4);
6647 
6648     if (opcode > 8 || mos) {
6649         unallocated_encoding(s);
6650         return;
6651     }
6652 
6653     switch (type) {
6654     case 0:
6655         if (!fp_access_check(s)) {
6656             return;
6657         }
6658         handle_fp_2src_single(s, opcode, rd, rn, rm);
6659         break;
6660     case 1:
6661         if (!fp_access_check(s)) {
6662             return;
6663         }
6664         handle_fp_2src_double(s, opcode, rd, rn, rm);
6665         break;
6666     case 3:
6667         if (!dc_isar_feature(aa64_fp16, s)) {
6668             unallocated_encoding(s);
6669             return;
6670         }
6671         if (!fp_access_check(s)) {
6672             return;
6673         }
6674         handle_fp_2src_half(s, opcode, rd, rn, rm);
6675         break;
6676     default:
6677         unallocated_encoding(s);
6678     }
6679 }
6680 
6681 /* Floating-point data-processing (3 source) - single precision */
6682 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6683                                   int rd, int rn, int rm, int ra)
6684 {
6685     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6686     TCGv_i32 tcg_res = tcg_temp_new_i32();
6687     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6688 
6689     tcg_op1 = read_fp_sreg(s, rn);
6690     tcg_op2 = read_fp_sreg(s, rm);
6691     tcg_op3 = read_fp_sreg(s, ra);
6692 
6693     /* These are fused multiply-add, and must be done as one
6694      * floating point operation with no rounding between the
6695      * multiplication and addition steps.
6696      * NB that doing the negations here as separate steps is
6697      * correct : an input NaN should come out with its sign bit
6698      * flipped if it is a negated-input.
6699      */
6700     if (o1 == true) {
6701         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6702     }
6703 
6704     if (o0 != o1) {
6705         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6706     }
6707 
6708     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6709 
6710     write_fp_sreg(s, rd, tcg_res);
6711 }
6712 
6713 /* Floating-point data-processing (3 source) - double precision */
6714 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6715                                   int rd, int rn, int rm, int ra)
6716 {
6717     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6718     TCGv_i64 tcg_res = tcg_temp_new_i64();
6719     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6720 
6721     tcg_op1 = read_fp_dreg(s, rn);
6722     tcg_op2 = read_fp_dreg(s, rm);
6723     tcg_op3 = read_fp_dreg(s, ra);
6724 
6725     /* These are fused multiply-add, and must be done as one
6726      * floating point operation with no rounding between the
6727      * multiplication and addition steps.
6728      * NB that doing the negations here as separate steps is
6729      * correct : an input NaN should come out with its sign bit
6730      * flipped if it is a negated-input.
6731      */
6732     if (o1 == true) {
6733         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6734     }
6735 
6736     if (o0 != o1) {
6737         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6738     }
6739 
6740     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6741 
6742     write_fp_dreg(s, rd, tcg_res);
6743 }
6744 
6745 /* Floating-point data-processing (3 source) - half precision */
6746 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6747                                 int rd, int rn, int rm, int ra)
6748 {
6749     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6750     TCGv_i32 tcg_res = tcg_temp_new_i32();
6751     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6752 
6753     tcg_op1 = read_fp_hreg(s, rn);
6754     tcg_op2 = read_fp_hreg(s, rm);
6755     tcg_op3 = read_fp_hreg(s, ra);
6756 
6757     /* These are fused multiply-add, and must be done as one
6758      * floating point operation with no rounding between the
6759      * multiplication and addition steps.
6760      * NB that doing the negations here as separate steps is
6761      * correct : an input NaN should come out with its sign bit
6762      * flipped if it is a negated-input.
6763      */
6764     if (o1 == true) {
6765         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6766     }
6767 
6768     if (o0 != o1) {
6769         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6770     }
6771 
6772     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6773 
6774     write_fp_sreg(s, rd, tcg_res);
6775 }
6776 
6777 /* Floating point data-processing (3 source)
6778  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6779  * +---+---+---+-----------+------+----+------+----+------+------+------+
6780  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6781  * +---+---+---+-----------+------+----+------+----+------+------+------+
6782  */
6783 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6784 {
6785     int mos = extract32(insn, 29, 3);
6786     int type = extract32(insn, 22, 2);
6787     int rd = extract32(insn, 0, 5);
6788     int rn = extract32(insn, 5, 5);
6789     int ra = extract32(insn, 10, 5);
6790     int rm = extract32(insn, 16, 5);
6791     bool o0 = extract32(insn, 15, 1);
6792     bool o1 = extract32(insn, 21, 1);
6793 
6794     if (mos) {
6795         unallocated_encoding(s);
6796         return;
6797     }
6798 
6799     switch (type) {
6800     case 0:
6801         if (!fp_access_check(s)) {
6802             return;
6803         }
6804         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6805         break;
6806     case 1:
6807         if (!fp_access_check(s)) {
6808             return;
6809         }
6810         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6811         break;
6812     case 3:
6813         if (!dc_isar_feature(aa64_fp16, s)) {
6814             unallocated_encoding(s);
6815             return;
6816         }
6817         if (!fp_access_check(s)) {
6818             return;
6819         }
6820         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6821         break;
6822     default:
6823         unallocated_encoding(s);
6824     }
6825 }
6826 
6827 /* Floating point immediate
6828  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6829  * +---+---+---+-----------+------+---+------------+-------+------+------+
6830  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6831  * +---+---+---+-----------+------+---+------------+-------+------+------+
6832  */
6833 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6834 {
6835     int rd = extract32(insn, 0, 5);
6836     int imm5 = extract32(insn, 5, 5);
6837     int imm8 = extract32(insn, 13, 8);
6838     int type = extract32(insn, 22, 2);
6839     int mos = extract32(insn, 29, 3);
6840     uint64_t imm;
6841     MemOp sz;
6842 
6843     if (mos || imm5) {
6844         unallocated_encoding(s);
6845         return;
6846     }
6847 
6848     switch (type) {
6849     case 0:
6850         sz = MO_32;
6851         break;
6852     case 1:
6853         sz = MO_64;
6854         break;
6855     case 3:
6856         sz = MO_16;
6857         if (dc_isar_feature(aa64_fp16, s)) {
6858             break;
6859         }
6860         /* fallthru */
6861     default:
6862         unallocated_encoding(s);
6863         return;
6864     }
6865 
6866     if (!fp_access_check(s)) {
6867         return;
6868     }
6869 
6870     imm = vfp_expand_imm(sz, imm8);
6871     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6872 }
6873 
6874 /* Handle floating point <=> fixed point conversions. Note that we can
6875  * also deal with fp <=> integer conversions as a special case (scale == 64)
6876  * OPTME: consider handling that special case specially or at least skipping
6877  * the call to scalbn in the helpers for zero shifts.
6878  */
6879 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6880                            bool itof, int rmode, int scale, int sf, int type)
6881 {
6882     bool is_signed = !(opcode & 1);
6883     TCGv_ptr tcg_fpstatus;
6884     TCGv_i32 tcg_shift, tcg_single;
6885     TCGv_i64 tcg_double;
6886 
6887     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6888 
6889     tcg_shift = tcg_constant_i32(64 - scale);
6890 
6891     if (itof) {
6892         TCGv_i64 tcg_int = cpu_reg(s, rn);
6893         if (!sf) {
6894             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6895 
6896             if (is_signed) {
6897                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6898             } else {
6899                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6900             }
6901 
6902             tcg_int = tcg_extend;
6903         }
6904 
6905         switch (type) {
6906         case 1: /* float64 */
6907             tcg_double = tcg_temp_new_i64();
6908             if (is_signed) {
6909                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6910                                      tcg_shift, tcg_fpstatus);
6911             } else {
6912                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6913                                      tcg_shift, tcg_fpstatus);
6914             }
6915             write_fp_dreg(s, rd, tcg_double);
6916             break;
6917 
6918         case 0: /* float32 */
6919             tcg_single = tcg_temp_new_i32();
6920             if (is_signed) {
6921                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6922                                      tcg_shift, tcg_fpstatus);
6923             } else {
6924                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6925                                      tcg_shift, tcg_fpstatus);
6926             }
6927             write_fp_sreg(s, rd, tcg_single);
6928             break;
6929 
6930         case 3: /* float16 */
6931             tcg_single = tcg_temp_new_i32();
6932             if (is_signed) {
6933                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6934                                      tcg_shift, tcg_fpstatus);
6935             } else {
6936                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6937                                      tcg_shift, tcg_fpstatus);
6938             }
6939             write_fp_sreg(s, rd, tcg_single);
6940             break;
6941 
6942         default:
6943             g_assert_not_reached();
6944         }
6945     } else {
6946         TCGv_i64 tcg_int = cpu_reg(s, rd);
6947         TCGv_i32 tcg_rmode;
6948 
6949         if (extract32(opcode, 2, 1)) {
6950             /* There are too many rounding modes to all fit into rmode,
6951              * so FCVTA[US] is a special case.
6952              */
6953             rmode = FPROUNDING_TIEAWAY;
6954         }
6955 
6956         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6957 
6958         switch (type) {
6959         case 1: /* float64 */
6960             tcg_double = read_fp_dreg(s, rn);
6961             if (is_signed) {
6962                 if (!sf) {
6963                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6964                                          tcg_shift, tcg_fpstatus);
6965                 } else {
6966                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6967                                          tcg_shift, tcg_fpstatus);
6968                 }
6969             } else {
6970                 if (!sf) {
6971                     gen_helper_vfp_tould(tcg_int, tcg_double,
6972                                          tcg_shift, tcg_fpstatus);
6973                 } else {
6974                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6975                                          tcg_shift, tcg_fpstatus);
6976                 }
6977             }
6978             if (!sf) {
6979                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6980             }
6981             break;
6982 
6983         case 0: /* float32 */
6984             tcg_single = read_fp_sreg(s, rn);
6985             if (sf) {
6986                 if (is_signed) {
6987                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6988                                          tcg_shift, tcg_fpstatus);
6989                 } else {
6990                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6991                                          tcg_shift, tcg_fpstatus);
6992                 }
6993             } else {
6994                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6995                 if (is_signed) {
6996                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6997                                          tcg_shift, tcg_fpstatus);
6998                 } else {
6999                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7000                                          tcg_shift, tcg_fpstatus);
7001                 }
7002                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7003             }
7004             break;
7005 
7006         case 3: /* float16 */
7007             tcg_single = read_fp_sreg(s, rn);
7008             if (sf) {
7009                 if (is_signed) {
7010                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7011                                          tcg_shift, tcg_fpstatus);
7012                 } else {
7013                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7014                                          tcg_shift, tcg_fpstatus);
7015                 }
7016             } else {
7017                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7018                 if (is_signed) {
7019                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7020                                          tcg_shift, tcg_fpstatus);
7021                 } else {
7022                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7023                                          tcg_shift, tcg_fpstatus);
7024                 }
7025                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7026             }
7027             break;
7028 
7029         default:
7030             g_assert_not_reached();
7031         }
7032 
7033         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7034     }
7035 }
7036 
7037 /* Floating point <-> fixed point conversions
7038  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7039  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7040  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7041  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7042  */
7043 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7044 {
7045     int rd = extract32(insn, 0, 5);
7046     int rn = extract32(insn, 5, 5);
7047     int scale = extract32(insn, 10, 6);
7048     int opcode = extract32(insn, 16, 3);
7049     int rmode = extract32(insn, 19, 2);
7050     int type = extract32(insn, 22, 2);
7051     bool sbit = extract32(insn, 29, 1);
7052     bool sf = extract32(insn, 31, 1);
7053     bool itof;
7054 
7055     if (sbit || (!sf && scale < 32)) {
7056         unallocated_encoding(s);
7057         return;
7058     }
7059 
7060     switch (type) {
7061     case 0: /* float32 */
7062     case 1: /* float64 */
7063         break;
7064     case 3: /* float16 */
7065         if (dc_isar_feature(aa64_fp16, s)) {
7066             break;
7067         }
7068         /* fallthru */
7069     default:
7070         unallocated_encoding(s);
7071         return;
7072     }
7073 
7074     switch ((rmode << 3) | opcode) {
7075     case 0x2: /* SCVTF */
7076     case 0x3: /* UCVTF */
7077         itof = true;
7078         break;
7079     case 0x18: /* FCVTZS */
7080     case 0x19: /* FCVTZU */
7081         itof = false;
7082         break;
7083     default:
7084         unallocated_encoding(s);
7085         return;
7086     }
7087 
7088     if (!fp_access_check(s)) {
7089         return;
7090     }
7091 
7092     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7093 }
7094 
7095 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7096 {
7097     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7098      * without conversion.
7099      */
7100 
7101     if (itof) {
7102         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7103         TCGv_i64 tmp;
7104 
7105         switch (type) {
7106         case 0:
7107             /* 32 bit */
7108             tmp = tcg_temp_new_i64();
7109             tcg_gen_ext32u_i64(tmp, tcg_rn);
7110             write_fp_dreg(s, rd, tmp);
7111             break;
7112         case 1:
7113             /* 64 bit */
7114             write_fp_dreg(s, rd, tcg_rn);
7115             break;
7116         case 2:
7117             /* 64 bit to top half. */
7118             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7119             clear_vec_high(s, true, rd);
7120             break;
7121         case 3:
7122             /* 16 bit */
7123             tmp = tcg_temp_new_i64();
7124             tcg_gen_ext16u_i64(tmp, tcg_rn);
7125             write_fp_dreg(s, rd, tmp);
7126             break;
7127         default:
7128             g_assert_not_reached();
7129         }
7130     } else {
7131         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7132 
7133         switch (type) {
7134         case 0:
7135             /* 32 bit */
7136             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7137             break;
7138         case 1:
7139             /* 64 bit */
7140             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7141             break;
7142         case 2:
7143             /* 64 bits from top half */
7144             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7145             break;
7146         case 3:
7147             /* 16 bit */
7148             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7149             break;
7150         default:
7151             g_assert_not_reached();
7152         }
7153     }
7154 }
7155 
7156 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7157 {
7158     TCGv_i64 t = read_fp_dreg(s, rn);
7159     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7160 
7161     gen_helper_fjcvtzs(t, t, fpstatus);
7162 
7163     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7164     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7165     tcg_gen_movi_i32(cpu_CF, 0);
7166     tcg_gen_movi_i32(cpu_NF, 0);
7167     tcg_gen_movi_i32(cpu_VF, 0);
7168 }
7169 
7170 /* Floating point <-> integer conversions
7171  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7172  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7173  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7174  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7175  */
7176 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7177 {
7178     int rd = extract32(insn, 0, 5);
7179     int rn = extract32(insn, 5, 5);
7180     int opcode = extract32(insn, 16, 3);
7181     int rmode = extract32(insn, 19, 2);
7182     int type = extract32(insn, 22, 2);
7183     bool sbit = extract32(insn, 29, 1);
7184     bool sf = extract32(insn, 31, 1);
7185     bool itof = false;
7186 
7187     if (sbit) {
7188         goto do_unallocated;
7189     }
7190 
7191     switch (opcode) {
7192     case 2: /* SCVTF */
7193     case 3: /* UCVTF */
7194         itof = true;
7195         /* fallthru */
7196     case 4: /* FCVTAS */
7197     case 5: /* FCVTAU */
7198         if (rmode != 0) {
7199             goto do_unallocated;
7200         }
7201         /* fallthru */
7202     case 0: /* FCVT[NPMZ]S */
7203     case 1: /* FCVT[NPMZ]U */
7204         switch (type) {
7205         case 0: /* float32 */
7206         case 1: /* float64 */
7207             break;
7208         case 3: /* float16 */
7209             if (!dc_isar_feature(aa64_fp16, s)) {
7210                 goto do_unallocated;
7211             }
7212             break;
7213         default:
7214             goto do_unallocated;
7215         }
7216         if (!fp_access_check(s)) {
7217             return;
7218         }
7219         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7220         break;
7221 
7222     default:
7223         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7224         case 0b01100110: /* FMOV half <-> 32-bit int */
7225         case 0b01100111:
7226         case 0b11100110: /* FMOV half <-> 64-bit int */
7227         case 0b11100111:
7228             if (!dc_isar_feature(aa64_fp16, s)) {
7229                 goto do_unallocated;
7230             }
7231             /* fallthru */
7232         case 0b00000110: /* FMOV 32-bit */
7233         case 0b00000111:
7234         case 0b10100110: /* FMOV 64-bit */
7235         case 0b10100111:
7236         case 0b11001110: /* FMOV top half of 128-bit */
7237         case 0b11001111:
7238             if (!fp_access_check(s)) {
7239                 return;
7240             }
7241             itof = opcode & 1;
7242             handle_fmov(s, rd, rn, type, itof);
7243             break;
7244 
7245         case 0b00111110: /* FJCVTZS */
7246             if (!dc_isar_feature(aa64_jscvt, s)) {
7247                 goto do_unallocated;
7248             } else if (fp_access_check(s)) {
7249                 handle_fjcvtzs(s, rd, rn);
7250             }
7251             break;
7252 
7253         default:
7254         do_unallocated:
7255             unallocated_encoding(s);
7256             return;
7257         }
7258         break;
7259     }
7260 }
7261 
7262 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7263  *   31  30  29 28     25 24                          0
7264  * +---+---+---+---------+-----------------------------+
7265  * |   | 0 |   | 1 1 1 1 |                             |
7266  * +---+---+---+---------+-----------------------------+
7267  */
7268 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7269 {
7270     if (extract32(insn, 24, 1)) {
7271         /* Floating point data-processing (3 source) */
7272         disas_fp_3src(s, insn);
7273     } else if (extract32(insn, 21, 1) == 0) {
7274         /* Floating point to fixed point conversions */
7275         disas_fp_fixed_conv(s, insn);
7276     } else {
7277         switch (extract32(insn, 10, 2)) {
7278         case 1:
7279             /* Floating point conditional compare */
7280             disas_fp_ccomp(s, insn);
7281             break;
7282         case 2:
7283             /* Floating point data-processing (2 source) */
7284             disas_fp_2src(s, insn);
7285             break;
7286         case 3:
7287             /* Floating point conditional select */
7288             disas_fp_csel(s, insn);
7289             break;
7290         case 0:
7291             switch (ctz32(extract32(insn, 12, 4))) {
7292             case 0: /* [15:12] == xxx1 */
7293                 /* Floating point immediate */
7294                 disas_fp_imm(s, insn);
7295                 break;
7296             case 1: /* [15:12] == xx10 */
7297                 /* Floating point compare */
7298                 disas_fp_compare(s, insn);
7299                 break;
7300             case 2: /* [15:12] == x100 */
7301                 /* Floating point data-processing (1 source) */
7302                 disas_fp_1src(s, insn);
7303                 break;
7304             case 3: /* [15:12] == 1000 */
7305                 unallocated_encoding(s);
7306                 break;
7307             default: /* [15:12] == 0000 */
7308                 /* Floating point <-> integer conversions */
7309                 disas_fp_int_conv(s, insn);
7310                 break;
7311             }
7312             break;
7313         }
7314     }
7315 }
7316 
7317 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7318                      int pos)
7319 {
7320     /* Extract 64 bits from the middle of two concatenated 64 bit
7321      * vector register slices left:right. The extracted bits start
7322      * at 'pos' bits into the right (least significant) side.
7323      * We return the result in tcg_right, and guarantee not to
7324      * trash tcg_left.
7325      */
7326     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7327     assert(pos > 0 && pos < 64);
7328 
7329     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7330     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7331     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7332 }
7333 
7334 /* EXT
7335  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7336  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7337  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7338  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7339  */
7340 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7341 {
7342     int is_q = extract32(insn, 30, 1);
7343     int op2 = extract32(insn, 22, 2);
7344     int imm4 = extract32(insn, 11, 4);
7345     int rm = extract32(insn, 16, 5);
7346     int rn = extract32(insn, 5, 5);
7347     int rd = extract32(insn, 0, 5);
7348     int pos = imm4 << 3;
7349     TCGv_i64 tcg_resl, tcg_resh;
7350 
7351     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7352         unallocated_encoding(s);
7353         return;
7354     }
7355 
7356     if (!fp_access_check(s)) {
7357         return;
7358     }
7359 
7360     tcg_resh = tcg_temp_new_i64();
7361     tcg_resl = tcg_temp_new_i64();
7362 
7363     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7364      * either extracting 128 bits from a 128:128 concatenation, or
7365      * extracting 64 bits from a 64:64 concatenation.
7366      */
7367     if (!is_q) {
7368         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7369         if (pos != 0) {
7370             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7371             do_ext64(s, tcg_resh, tcg_resl, pos);
7372         }
7373     } else {
7374         TCGv_i64 tcg_hh;
7375         typedef struct {
7376             int reg;
7377             int elt;
7378         } EltPosns;
7379         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7380         EltPosns *elt = eltposns;
7381 
7382         if (pos >= 64) {
7383             elt++;
7384             pos -= 64;
7385         }
7386 
7387         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7388         elt++;
7389         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7390         elt++;
7391         if (pos != 0) {
7392             do_ext64(s, tcg_resh, tcg_resl, pos);
7393             tcg_hh = tcg_temp_new_i64();
7394             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7395             do_ext64(s, tcg_hh, tcg_resh, pos);
7396         }
7397     }
7398 
7399     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7400     if (is_q) {
7401         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7402     }
7403     clear_vec_high(s, is_q, rd);
7404 }
7405 
7406 /* TBL/TBX
7407  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7408  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7409  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7410  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7411  */
7412 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7413 {
7414     int op2 = extract32(insn, 22, 2);
7415     int is_q = extract32(insn, 30, 1);
7416     int rm = extract32(insn, 16, 5);
7417     int rn = extract32(insn, 5, 5);
7418     int rd = extract32(insn, 0, 5);
7419     int is_tbx = extract32(insn, 12, 1);
7420     int len = (extract32(insn, 13, 2) + 1) * 16;
7421 
7422     if (op2 != 0) {
7423         unallocated_encoding(s);
7424         return;
7425     }
7426 
7427     if (!fp_access_check(s)) {
7428         return;
7429     }
7430 
7431     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7432                        vec_full_reg_offset(s, rm), cpu_env,
7433                        is_q ? 16 : 8, vec_full_reg_size(s),
7434                        (len << 6) | (is_tbx << 5) | rn,
7435                        gen_helper_simd_tblx);
7436 }
7437 
7438 /* ZIP/UZP/TRN
7439  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7440  * +---+---+-------------+------+---+------+---+------------------+------+
7441  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7442  * +---+---+-------------+------+---+------+---+------------------+------+
7443  */
7444 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7445 {
7446     int rd = extract32(insn, 0, 5);
7447     int rn = extract32(insn, 5, 5);
7448     int rm = extract32(insn, 16, 5);
7449     int size = extract32(insn, 22, 2);
7450     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7451      * bit 2 indicates 1 vs 2 variant of the insn.
7452      */
7453     int opcode = extract32(insn, 12, 2);
7454     bool part = extract32(insn, 14, 1);
7455     bool is_q = extract32(insn, 30, 1);
7456     int esize = 8 << size;
7457     int i;
7458     int datasize = is_q ? 128 : 64;
7459     int elements = datasize / esize;
7460     TCGv_i64 tcg_res[2], tcg_ele;
7461 
7462     if (opcode == 0 || (size == 3 && !is_q)) {
7463         unallocated_encoding(s);
7464         return;
7465     }
7466 
7467     if (!fp_access_check(s)) {
7468         return;
7469     }
7470 
7471     tcg_res[0] = tcg_temp_new_i64();
7472     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7473     tcg_ele = tcg_temp_new_i64();
7474 
7475     for (i = 0; i < elements; i++) {
7476         int o, w;
7477 
7478         switch (opcode) {
7479         case 1: /* UZP1/2 */
7480         {
7481             int midpoint = elements / 2;
7482             if (i < midpoint) {
7483                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7484             } else {
7485                 read_vec_element(s, tcg_ele, rm,
7486                                  2 * (i - midpoint) + part, size);
7487             }
7488             break;
7489         }
7490         case 2: /* TRN1/2 */
7491             if (i & 1) {
7492                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7493             } else {
7494                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7495             }
7496             break;
7497         case 3: /* ZIP1/2 */
7498         {
7499             int base = part * elements / 2;
7500             if (i & 1) {
7501                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7502             } else {
7503                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7504             }
7505             break;
7506         }
7507         default:
7508             g_assert_not_reached();
7509         }
7510 
7511         w = (i * esize) / 64;
7512         o = (i * esize) % 64;
7513         if (o == 0) {
7514             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7515         } else {
7516             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7517             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7518         }
7519     }
7520 
7521     for (i = 0; i <= is_q; ++i) {
7522         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7523     }
7524     clear_vec_high(s, is_q, rd);
7525 }
7526 
7527 /*
7528  * do_reduction_op helper
7529  *
7530  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7531  * important for correct NaN propagation that we do these
7532  * operations in exactly the order specified by the pseudocode.
7533  *
7534  * This is a recursive function, TCG temps should be freed by the
7535  * calling function once it is done with the values.
7536  */
7537 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7538                                 int esize, int size, int vmap, TCGv_ptr fpst)
7539 {
7540     if (esize == size) {
7541         int element;
7542         MemOp msize = esize == 16 ? MO_16 : MO_32;
7543         TCGv_i32 tcg_elem;
7544 
7545         /* We should have one register left here */
7546         assert(ctpop8(vmap) == 1);
7547         element = ctz32(vmap);
7548         assert(element < 8);
7549 
7550         tcg_elem = tcg_temp_new_i32();
7551         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7552         return tcg_elem;
7553     } else {
7554         int bits = size / 2;
7555         int shift = ctpop8(vmap) / 2;
7556         int vmap_lo = (vmap >> shift) & vmap;
7557         int vmap_hi = (vmap & ~vmap_lo);
7558         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7559 
7560         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7561         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7562         tcg_res = tcg_temp_new_i32();
7563 
7564         switch (fpopcode) {
7565         case 0x0c: /* fmaxnmv half-precision */
7566             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7567             break;
7568         case 0x0f: /* fmaxv half-precision */
7569             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7570             break;
7571         case 0x1c: /* fminnmv half-precision */
7572             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7573             break;
7574         case 0x1f: /* fminv half-precision */
7575             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7576             break;
7577         case 0x2c: /* fmaxnmv */
7578             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7579             break;
7580         case 0x2f: /* fmaxv */
7581             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7582             break;
7583         case 0x3c: /* fminnmv */
7584             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7585             break;
7586         case 0x3f: /* fminv */
7587             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7588             break;
7589         default:
7590             g_assert_not_reached();
7591         }
7592         return tcg_res;
7593     }
7594 }
7595 
7596 /* AdvSIMD across lanes
7597  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7598  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7599  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7600  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7601  */
7602 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7603 {
7604     int rd = extract32(insn, 0, 5);
7605     int rn = extract32(insn, 5, 5);
7606     int size = extract32(insn, 22, 2);
7607     int opcode = extract32(insn, 12, 5);
7608     bool is_q = extract32(insn, 30, 1);
7609     bool is_u = extract32(insn, 29, 1);
7610     bool is_fp = false;
7611     bool is_min = false;
7612     int esize;
7613     int elements;
7614     int i;
7615     TCGv_i64 tcg_res, tcg_elt;
7616 
7617     switch (opcode) {
7618     case 0x1b: /* ADDV */
7619         if (is_u) {
7620             unallocated_encoding(s);
7621             return;
7622         }
7623         /* fall through */
7624     case 0x3: /* SADDLV, UADDLV */
7625     case 0xa: /* SMAXV, UMAXV */
7626     case 0x1a: /* SMINV, UMINV */
7627         if (size == 3 || (size == 2 && !is_q)) {
7628             unallocated_encoding(s);
7629             return;
7630         }
7631         break;
7632     case 0xc: /* FMAXNMV, FMINNMV */
7633     case 0xf: /* FMAXV, FMINV */
7634         /* Bit 1 of size field encodes min vs max and the actual size
7635          * depends on the encoding of the U bit. If not set (and FP16
7636          * enabled) then we do half-precision float instead of single
7637          * precision.
7638          */
7639         is_min = extract32(size, 1, 1);
7640         is_fp = true;
7641         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7642             size = 1;
7643         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7644             unallocated_encoding(s);
7645             return;
7646         } else {
7647             size = 2;
7648         }
7649         break;
7650     default:
7651         unallocated_encoding(s);
7652         return;
7653     }
7654 
7655     if (!fp_access_check(s)) {
7656         return;
7657     }
7658 
7659     esize = 8 << size;
7660     elements = (is_q ? 128 : 64) / esize;
7661 
7662     tcg_res = tcg_temp_new_i64();
7663     tcg_elt = tcg_temp_new_i64();
7664 
7665     /* These instructions operate across all lanes of a vector
7666      * to produce a single result. We can guarantee that a 64
7667      * bit intermediate is sufficient:
7668      *  + for [US]ADDLV the maximum element size is 32 bits, and
7669      *    the result type is 64 bits
7670      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7671      *    same as the element size, which is 32 bits at most
7672      * For the integer operations we can choose to work at 64
7673      * or 32 bits and truncate at the end; for simplicity
7674      * we use 64 bits always. The floating point
7675      * ops do require 32 bit intermediates, though.
7676      */
7677     if (!is_fp) {
7678         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7679 
7680         for (i = 1; i < elements; i++) {
7681             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7682 
7683             switch (opcode) {
7684             case 0x03: /* SADDLV / UADDLV */
7685             case 0x1b: /* ADDV */
7686                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7687                 break;
7688             case 0x0a: /* SMAXV / UMAXV */
7689                 if (is_u) {
7690                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7691                 } else {
7692                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7693                 }
7694                 break;
7695             case 0x1a: /* SMINV / UMINV */
7696                 if (is_u) {
7697                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7698                 } else {
7699                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7700                 }
7701                 break;
7702             default:
7703                 g_assert_not_reached();
7704             }
7705 
7706         }
7707     } else {
7708         /* Floating point vector reduction ops which work across 32
7709          * bit (single) or 16 bit (half-precision) intermediates.
7710          * Note that correct NaN propagation requires that we do these
7711          * operations in exactly the order specified by the pseudocode.
7712          */
7713         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7714         int fpopcode = opcode | is_min << 4 | is_u << 5;
7715         int vmap = (1 << elements) - 1;
7716         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7717                                              (is_q ? 128 : 64), vmap, fpst);
7718         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7719     }
7720 
7721     /* Now truncate the result to the width required for the final output */
7722     if (opcode == 0x03) {
7723         /* SADDLV, UADDLV: result is 2*esize */
7724         size++;
7725     }
7726 
7727     switch (size) {
7728     case 0:
7729         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7730         break;
7731     case 1:
7732         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7733         break;
7734     case 2:
7735         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7736         break;
7737     case 3:
7738         break;
7739     default:
7740         g_assert_not_reached();
7741     }
7742 
7743     write_fp_dreg(s, rd, tcg_res);
7744 }
7745 
7746 /* DUP (Element, Vector)
7747  *
7748  *  31  30   29              21 20    16 15        10  9    5 4    0
7749  * +---+---+-------------------+--------+-------------+------+------+
7750  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7751  * +---+---+-------------------+--------+-------------+------+------+
7752  *
7753  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7754  */
7755 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7756                              int imm5)
7757 {
7758     int size = ctz32(imm5);
7759     int index;
7760 
7761     if (size > 3 || (size == 3 && !is_q)) {
7762         unallocated_encoding(s);
7763         return;
7764     }
7765 
7766     if (!fp_access_check(s)) {
7767         return;
7768     }
7769 
7770     index = imm5 >> (size + 1);
7771     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7772                          vec_reg_offset(s, rn, index, size),
7773                          is_q ? 16 : 8, vec_full_reg_size(s));
7774 }
7775 
7776 /* DUP (element, scalar)
7777  *  31                   21 20    16 15        10  9    5 4    0
7778  * +-----------------------+--------+-------------+------+------+
7779  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7780  * +-----------------------+--------+-------------+------+------+
7781  */
7782 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7783                               int imm5)
7784 {
7785     int size = ctz32(imm5);
7786     int index;
7787     TCGv_i64 tmp;
7788 
7789     if (size > 3) {
7790         unallocated_encoding(s);
7791         return;
7792     }
7793 
7794     if (!fp_access_check(s)) {
7795         return;
7796     }
7797 
7798     index = imm5 >> (size + 1);
7799 
7800     /* This instruction just extracts the specified element and
7801      * zero-extends it into the bottom of the destination register.
7802      */
7803     tmp = tcg_temp_new_i64();
7804     read_vec_element(s, tmp, rn, index, size);
7805     write_fp_dreg(s, rd, tmp);
7806 }
7807 
7808 /* DUP (General)
7809  *
7810  *  31  30   29              21 20    16 15        10  9    5 4    0
7811  * +---+---+-------------------+--------+-------------+------+------+
7812  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7813  * +---+---+-------------------+--------+-------------+------+------+
7814  *
7815  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7816  */
7817 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7818                              int imm5)
7819 {
7820     int size = ctz32(imm5);
7821     uint32_t dofs, oprsz, maxsz;
7822 
7823     if (size > 3 || ((size == 3) && !is_q)) {
7824         unallocated_encoding(s);
7825         return;
7826     }
7827 
7828     if (!fp_access_check(s)) {
7829         return;
7830     }
7831 
7832     dofs = vec_full_reg_offset(s, rd);
7833     oprsz = is_q ? 16 : 8;
7834     maxsz = vec_full_reg_size(s);
7835 
7836     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7837 }
7838 
7839 /* INS (Element)
7840  *
7841  *  31                   21 20    16 15  14    11  10 9    5 4    0
7842  * +-----------------------+--------+------------+---+------+------+
7843  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7844  * +-----------------------+--------+------------+---+------+------+
7845  *
7846  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7847  * index: encoded in imm5<4:size+1>
7848  */
7849 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7850                              int imm4, int imm5)
7851 {
7852     int size = ctz32(imm5);
7853     int src_index, dst_index;
7854     TCGv_i64 tmp;
7855 
7856     if (size > 3) {
7857         unallocated_encoding(s);
7858         return;
7859     }
7860 
7861     if (!fp_access_check(s)) {
7862         return;
7863     }
7864 
7865     dst_index = extract32(imm5, 1+size, 5);
7866     src_index = extract32(imm4, size, 4);
7867 
7868     tmp = tcg_temp_new_i64();
7869 
7870     read_vec_element(s, tmp, rn, src_index, size);
7871     write_vec_element(s, tmp, rd, dst_index, size);
7872 
7873     /* INS is considered a 128-bit write for SVE. */
7874     clear_vec_high(s, true, rd);
7875 }
7876 
7877 
7878 /* INS (General)
7879  *
7880  *  31                   21 20    16 15        10  9    5 4    0
7881  * +-----------------------+--------+-------------+------+------+
7882  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7883  * +-----------------------+--------+-------------+------+------+
7884  *
7885  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7886  * index: encoded in imm5<4:size+1>
7887  */
7888 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7889 {
7890     int size = ctz32(imm5);
7891     int idx;
7892 
7893     if (size > 3) {
7894         unallocated_encoding(s);
7895         return;
7896     }
7897 
7898     if (!fp_access_check(s)) {
7899         return;
7900     }
7901 
7902     idx = extract32(imm5, 1 + size, 4 - size);
7903     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7904 
7905     /* INS is considered a 128-bit write for SVE. */
7906     clear_vec_high(s, true, rd);
7907 }
7908 
7909 /*
7910  * UMOV (General)
7911  * SMOV (General)
7912  *
7913  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7914  * +---+---+-------------------+--------+-------------+------+------+
7915  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7916  * +---+---+-------------------+--------+-------------+------+------+
7917  *
7918  * U: unsigned when set
7919  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7920  */
7921 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7922                                   int rn, int rd, int imm5)
7923 {
7924     int size = ctz32(imm5);
7925     int element;
7926     TCGv_i64 tcg_rd;
7927 
7928     /* Check for UnallocatedEncodings */
7929     if (is_signed) {
7930         if (size > 2 || (size == 2 && !is_q)) {
7931             unallocated_encoding(s);
7932             return;
7933         }
7934     } else {
7935         if (size > 3
7936             || (size < 3 && is_q)
7937             || (size == 3 && !is_q)) {
7938             unallocated_encoding(s);
7939             return;
7940         }
7941     }
7942 
7943     if (!fp_access_check(s)) {
7944         return;
7945     }
7946 
7947     element = extract32(imm5, 1+size, 4);
7948 
7949     tcg_rd = cpu_reg(s, rd);
7950     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7951     if (is_signed && !is_q) {
7952         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7953     }
7954 }
7955 
7956 /* AdvSIMD copy
7957  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7958  * +---+---+----+-----------------+------+---+------+---+------+------+
7959  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7960  * +---+---+----+-----------------+------+---+------+---+------+------+
7961  */
7962 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7963 {
7964     int rd = extract32(insn, 0, 5);
7965     int rn = extract32(insn, 5, 5);
7966     int imm4 = extract32(insn, 11, 4);
7967     int op = extract32(insn, 29, 1);
7968     int is_q = extract32(insn, 30, 1);
7969     int imm5 = extract32(insn, 16, 5);
7970 
7971     if (op) {
7972         if (is_q) {
7973             /* INS (element) */
7974             handle_simd_inse(s, rd, rn, imm4, imm5);
7975         } else {
7976             unallocated_encoding(s);
7977         }
7978     } else {
7979         switch (imm4) {
7980         case 0:
7981             /* DUP (element - vector) */
7982             handle_simd_dupe(s, is_q, rd, rn, imm5);
7983             break;
7984         case 1:
7985             /* DUP (general) */
7986             handle_simd_dupg(s, is_q, rd, rn, imm5);
7987             break;
7988         case 3:
7989             if (is_q) {
7990                 /* INS (general) */
7991                 handle_simd_insg(s, rd, rn, imm5);
7992             } else {
7993                 unallocated_encoding(s);
7994             }
7995             break;
7996         case 5:
7997         case 7:
7998             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7999             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8000             break;
8001         default:
8002             unallocated_encoding(s);
8003             break;
8004         }
8005     }
8006 }
8007 
8008 /* AdvSIMD modified immediate
8009  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8010  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8011  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8012  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8013  *
8014  * There are a number of operations that can be carried out here:
8015  *   MOVI - move (shifted) imm into register
8016  *   MVNI - move inverted (shifted) imm into register
8017  *   ORR  - bitwise OR of (shifted) imm with register
8018  *   BIC  - bitwise clear of (shifted) imm with register
8019  * With ARMv8.2 we also have:
8020  *   FMOV half-precision
8021  */
8022 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8023 {
8024     int rd = extract32(insn, 0, 5);
8025     int cmode = extract32(insn, 12, 4);
8026     int o2 = extract32(insn, 11, 1);
8027     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8028     bool is_neg = extract32(insn, 29, 1);
8029     bool is_q = extract32(insn, 30, 1);
8030     uint64_t imm = 0;
8031 
8032     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8033         /* Check for FMOV (vector, immediate) - half-precision */
8034         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8035             unallocated_encoding(s);
8036             return;
8037         }
8038     }
8039 
8040     if (!fp_access_check(s)) {
8041         return;
8042     }
8043 
8044     if (cmode == 15 && o2 && !is_neg) {
8045         /* FMOV (vector, immediate) - half-precision */
8046         imm = vfp_expand_imm(MO_16, abcdefgh);
8047         /* now duplicate across the lanes */
8048         imm = dup_const(MO_16, imm);
8049     } else {
8050         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8051     }
8052 
8053     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8054         /* MOVI or MVNI, with MVNI negation handled above.  */
8055         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8056                              vec_full_reg_size(s), imm);
8057     } else {
8058         /* ORR or BIC, with BIC negation to AND handled above.  */
8059         if (is_neg) {
8060             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8061         } else {
8062             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8063         }
8064     }
8065 }
8066 
8067 /* AdvSIMD scalar copy
8068  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8069  * +-----+----+-----------------+------+---+------+---+------+------+
8070  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8071  * +-----+----+-----------------+------+---+------+---+------+------+
8072  */
8073 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8074 {
8075     int rd = extract32(insn, 0, 5);
8076     int rn = extract32(insn, 5, 5);
8077     int imm4 = extract32(insn, 11, 4);
8078     int imm5 = extract32(insn, 16, 5);
8079     int op = extract32(insn, 29, 1);
8080 
8081     if (op != 0 || imm4 != 0) {
8082         unallocated_encoding(s);
8083         return;
8084     }
8085 
8086     /* DUP (element, scalar) */
8087     handle_simd_dupes(s, rd, rn, imm5);
8088 }
8089 
8090 /* AdvSIMD scalar pairwise
8091  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8092  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8093  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8094  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8095  */
8096 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8097 {
8098     int u = extract32(insn, 29, 1);
8099     int size = extract32(insn, 22, 2);
8100     int opcode = extract32(insn, 12, 5);
8101     int rn = extract32(insn, 5, 5);
8102     int rd = extract32(insn, 0, 5);
8103     TCGv_ptr fpst;
8104 
8105     /* For some ops (the FP ones), size[1] is part of the encoding.
8106      * For ADDP strictly it is not but size[1] is always 1 for valid
8107      * encodings.
8108      */
8109     opcode |= (extract32(size, 1, 1) << 5);
8110 
8111     switch (opcode) {
8112     case 0x3b: /* ADDP */
8113         if (u || size != 3) {
8114             unallocated_encoding(s);
8115             return;
8116         }
8117         if (!fp_access_check(s)) {
8118             return;
8119         }
8120 
8121         fpst = NULL;
8122         break;
8123     case 0xc: /* FMAXNMP */
8124     case 0xd: /* FADDP */
8125     case 0xf: /* FMAXP */
8126     case 0x2c: /* FMINNMP */
8127     case 0x2f: /* FMINP */
8128         /* FP op, size[0] is 32 or 64 bit*/
8129         if (!u) {
8130             if (!dc_isar_feature(aa64_fp16, s)) {
8131                 unallocated_encoding(s);
8132                 return;
8133             } else {
8134                 size = MO_16;
8135             }
8136         } else {
8137             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8138         }
8139 
8140         if (!fp_access_check(s)) {
8141             return;
8142         }
8143 
8144         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8145         break;
8146     default:
8147         unallocated_encoding(s);
8148         return;
8149     }
8150 
8151     if (size == MO_64) {
8152         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8153         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8154         TCGv_i64 tcg_res = tcg_temp_new_i64();
8155 
8156         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8157         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8158 
8159         switch (opcode) {
8160         case 0x3b: /* ADDP */
8161             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8162             break;
8163         case 0xc: /* FMAXNMP */
8164             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8165             break;
8166         case 0xd: /* FADDP */
8167             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8168             break;
8169         case 0xf: /* FMAXP */
8170             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8171             break;
8172         case 0x2c: /* FMINNMP */
8173             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8174             break;
8175         case 0x2f: /* FMINP */
8176             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8177             break;
8178         default:
8179             g_assert_not_reached();
8180         }
8181 
8182         write_fp_dreg(s, rd, tcg_res);
8183     } else {
8184         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8185         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8186         TCGv_i32 tcg_res = tcg_temp_new_i32();
8187 
8188         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8189         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8190 
8191         if (size == MO_16) {
8192             switch (opcode) {
8193             case 0xc: /* FMAXNMP */
8194                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8195                 break;
8196             case 0xd: /* FADDP */
8197                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8198                 break;
8199             case 0xf: /* FMAXP */
8200                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8201                 break;
8202             case 0x2c: /* FMINNMP */
8203                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8204                 break;
8205             case 0x2f: /* FMINP */
8206                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8207                 break;
8208             default:
8209                 g_assert_not_reached();
8210             }
8211         } else {
8212             switch (opcode) {
8213             case 0xc: /* FMAXNMP */
8214                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8215                 break;
8216             case 0xd: /* FADDP */
8217                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8218                 break;
8219             case 0xf: /* FMAXP */
8220                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8221                 break;
8222             case 0x2c: /* FMINNMP */
8223                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8224                 break;
8225             case 0x2f: /* FMINP */
8226                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8227                 break;
8228             default:
8229                 g_assert_not_reached();
8230             }
8231         }
8232 
8233         write_fp_sreg(s, rd, tcg_res);
8234     }
8235 }
8236 
8237 /*
8238  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8239  *
8240  * This code is handles the common shifting code and is used by both
8241  * the vector and scalar code.
8242  */
8243 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8244                                     TCGv_i64 tcg_rnd, bool accumulate,
8245                                     bool is_u, int size, int shift)
8246 {
8247     bool extended_result = false;
8248     bool round = tcg_rnd != NULL;
8249     int ext_lshift = 0;
8250     TCGv_i64 tcg_src_hi;
8251 
8252     if (round && size == 3) {
8253         extended_result = true;
8254         ext_lshift = 64 - shift;
8255         tcg_src_hi = tcg_temp_new_i64();
8256     } else if (shift == 64) {
8257         if (!accumulate && is_u) {
8258             /* result is zero */
8259             tcg_gen_movi_i64(tcg_res, 0);
8260             return;
8261         }
8262     }
8263 
8264     /* Deal with the rounding step */
8265     if (round) {
8266         if (extended_result) {
8267             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8268             if (!is_u) {
8269                 /* take care of sign extending tcg_res */
8270                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8271                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8272                                  tcg_src, tcg_src_hi,
8273                                  tcg_rnd, tcg_zero);
8274             } else {
8275                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8276                                  tcg_src, tcg_zero,
8277                                  tcg_rnd, tcg_zero);
8278             }
8279         } else {
8280             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8281         }
8282     }
8283 
8284     /* Now do the shift right */
8285     if (round && extended_result) {
8286         /* extended case, >64 bit precision required */
8287         if (ext_lshift == 0) {
8288             /* special case, only high bits matter */
8289             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8290         } else {
8291             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8292             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8293             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8294         }
8295     } else {
8296         if (is_u) {
8297             if (shift == 64) {
8298                 /* essentially shifting in 64 zeros */
8299                 tcg_gen_movi_i64(tcg_src, 0);
8300             } else {
8301                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8302             }
8303         } else {
8304             if (shift == 64) {
8305                 /* effectively extending the sign-bit */
8306                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8307             } else {
8308                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8309             }
8310         }
8311     }
8312 
8313     if (accumulate) {
8314         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8315     } else {
8316         tcg_gen_mov_i64(tcg_res, tcg_src);
8317     }
8318 }
8319 
8320 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8321 static void handle_scalar_simd_shri(DisasContext *s,
8322                                     bool is_u, int immh, int immb,
8323                                     int opcode, int rn, int rd)
8324 {
8325     const int size = 3;
8326     int immhb = immh << 3 | immb;
8327     int shift = 2 * (8 << size) - immhb;
8328     bool accumulate = false;
8329     bool round = false;
8330     bool insert = false;
8331     TCGv_i64 tcg_rn;
8332     TCGv_i64 tcg_rd;
8333     TCGv_i64 tcg_round;
8334 
8335     if (!extract32(immh, 3, 1)) {
8336         unallocated_encoding(s);
8337         return;
8338     }
8339 
8340     if (!fp_access_check(s)) {
8341         return;
8342     }
8343 
8344     switch (opcode) {
8345     case 0x02: /* SSRA / USRA (accumulate) */
8346         accumulate = true;
8347         break;
8348     case 0x04: /* SRSHR / URSHR (rounding) */
8349         round = true;
8350         break;
8351     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8352         accumulate = round = true;
8353         break;
8354     case 0x08: /* SRI */
8355         insert = true;
8356         break;
8357     }
8358 
8359     if (round) {
8360         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8361     } else {
8362         tcg_round = NULL;
8363     }
8364 
8365     tcg_rn = read_fp_dreg(s, rn);
8366     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8367 
8368     if (insert) {
8369         /* shift count same as element size is valid but does nothing;
8370          * special case to avoid potential shift by 64.
8371          */
8372         int esize = 8 << size;
8373         if (shift != esize) {
8374             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8375             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8376         }
8377     } else {
8378         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8379                                 accumulate, is_u, size, shift);
8380     }
8381 
8382     write_fp_dreg(s, rd, tcg_rd);
8383 }
8384 
8385 /* SHL/SLI - Scalar shift left */
8386 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8387                                     int immh, int immb, int opcode,
8388                                     int rn, int rd)
8389 {
8390     int size = 32 - clz32(immh) - 1;
8391     int immhb = immh << 3 | immb;
8392     int shift = immhb - (8 << size);
8393     TCGv_i64 tcg_rn;
8394     TCGv_i64 tcg_rd;
8395 
8396     if (!extract32(immh, 3, 1)) {
8397         unallocated_encoding(s);
8398         return;
8399     }
8400 
8401     if (!fp_access_check(s)) {
8402         return;
8403     }
8404 
8405     tcg_rn = read_fp_dreg(s, rn);
8406     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8407 
8408     if (insert) {
8409         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8410     } else {
8411         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8412     }
8413 
8414     write_fp_dreg(s, rd, tcg_rd);
8415 }
8416 
8417 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8418  * (signed/unsigned) narrowing */
8419 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8420                                    bool is_u_shift, bool is_u_narrow,
8421                                    int immh, int immb, int opcode,
8422                                    int rn, int rd)
8423 {
8424     int immhb = immh << 3 | immb;
8425     int size = 32 - clz32(immh) - 1;
8426     int esize = 8 << size;
8427     int shift = (2 * esize) - immhb;
8428     int elements = is_scalar ? 1 : (64 / esize);
8429     bool round = extract32(opcode, 0, 1);
8430     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8431     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8432     TCGv_i32 tcg_rd_narrowed;
8433     TCGv_i64 tcg_final;
8434 
8435     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8436         { gen_helper_neon_narrow_sat_s8,
8437           gen_helper_neon_unarrow_sat8 },
8438         { gen_helper_neon_narrow_sat_s16,
8439           gen_helper_neon_unarrow_sat16 },
8440         { gen_helper_neon_narrow_sat_s32,
8441           gen_helper_neon_unarrow_sat32 },
8442         { NULL, NULL },
8443     };
8444     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8445         gen_helper_neon_narrow_sat_u8,
8446         gen_helper_neon_narrow_sat_u16,
8447         gen_helper_neon_narrow_sat_u32,
8448         NULL
8449     };
8450     NeonGenNarrowEnvFn *narrowfn;
8451 
8452     int i;
8453 
8454     assert(size < 4);
8455 
8456     if (extract32(immh, 3, 1)) {
8457         unallocated_encoding(s);
8458         return;
8459     }
8460 
8461     if (!fp_access_check(s)) {
8462         return;
8463     }
8464 
8465     if (is_u_shift) {
8466         narrowfn = unsigned_narrow_fns[size];
8467     } else {
8468         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8469     }
8470 
8471     tcg_rn = tcg_temp_new_i64();
8472     tcg_rd = tcg_temp_new_i64();
8473     tcg_rd_narrowed = tcg_temp_new_i32();
8474     tcg_final = tcg_temp_new_i64();
8475 
8476     if (round) {
8477         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8478     } else {
8479         tcg_round = NULL;
8480     }
8481 
8482     for (i = 0; i < elements; i++) {
8483         read_vec_element(s, tcg_rn, rn, i, ldop);
8484         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8485                                 false, is_u_shift, size+1, shift);
8486         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8487         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8488         if (i == 0) {
8489             tcg_gen_mov_i64(tcg_final, tcg_rd);
8490         } else {
8491             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8492         }
8493     }
8494 
8495     if (!is_q) {
8496         write_vec_element(s, tcg_final, rd, 0, MO_64);
8497     } else {
8498         write_vec_element(s, tcg_final, rd, 1, MO_64);
8499     }
8500     clear_vec_high(s, is_q, rd);
8501 }
8502 
8503 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8504 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8505                              bool src_unsigned, bool dst_unsigned,
8506                              int immh, int immb, int rn, int rd)
8507 {
8508     int immhb = immh << 3 | immb;
8509     int size = 32 - clz32(immh) - 1;
8510     int shift = immhb - (8 << size);
8511     int pass;
8512 
8513     assert(immh != 0);
8514     assert(!(scalar && is_q));
8515 
8516     if (!scalar) {
8517         if (!is_q && extract32(immh, 3, 1)) {
8518             unallocated_encoding(s);
8519             return;
8520         }
8521 
8522         /* Since we use the variable-shift helpers we must
8523          * replicate the shift count into each element of
8524          * the tcg_shift value.
8525          */
8526         switch (size) {
8527         case 0:
8528             shift |= shift << 8;
8529             /* fall through */
8530         case 1:
8531             shift |= shift << 16;
8532             break;
8533         case 2:
8534         case 3:
8535             break;
8536         default:
8537             g_assert_not_reached();
8538         }
8539     }
8540 
8541     if (!fp_access_check(s)) {
8542         return;
8543     }
8544 
8545     if (size == 3) {
8546         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8547         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8548             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8549             { NULL, gen_helper_neon_qshl_u64 },
8550         };
8551         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8552         int maxpass = is_q ? 2 : 1;
8553 
8554         for (pass = 0; pass < maxpass; pass++) {
8555             TCGv_i64 tcg_op = tcg_temp_new_i64();
8556 
8557             read_vec_element(s, tcg_op, rn, pass, MO_64);
8558             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8559             write_vec_element(s, tcg_op, rd, pass, MO_64);
8560         }
8561         clear_vec_high(s, is_q, rd);
8562     } else {
8563         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8564         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8565             {
8566                 { gen_helper_neon_qshl_s8,
8567                   gen_helper_neon_qshl_s16,
8568                   gen_helper_neon_qshl_s32 },
8569                 { gen_helper_neon_qshlu_s8,
8570                   gen_helper_neon_qshlu_s16,
8571                   gen_helper_neon_qshlu_s32 }
8572             }, {
8573                 { NULL, NULL, NULL },
8574                 { gen_helper_neon_qshl_u8,
8575                   gen_helper_neon_qshl_u16,
8576                   gen_helper_neon_qshl_u32 }
8577             }
8578         };
8579         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8580         MemOp memop = scalar ? size : MO_32;
8581         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8582 
8583         for (pass = 0; pass < maxpass; pass++) {
8584             TCGv_i32 tcg_op = tcg_temp_new_i32();
8585 
8586             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8587             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8588             if (scalar) {
8589                 switch (size) {
8590                 case 0:
8591                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8592                     break;
8593                 case 1:
8594                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8595                     break;
8596                 case 2:
8597                     break;
8598                 default:
8599                     g_assert_not_reached();
8600                 }
8601                 write_fp_sreg(s, rd, tcg_op);
8602             } else {
8603                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8604             }
8605         }
8606 
8607         if (!scalar) {
8608             clear_vec_high(s, is_q, rd);
8609         }
8610     }
8611 }
8612 
8613 /* Common vector code for handling integer to FP conversion */
8614 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8615                                    int elements, int is_signed,
8616                                    int fracbits, int size)
8617 {
8618     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8619     TCGv_i32 tcg_shift = NULL;
8620 
8621     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8622     int pass;
8623 
8624     if (fracbits || size == MO_64) {
8625         tcg_shift = tcg_constant_i32(fracbits);
8626     }
8627 
8628     if (size == MO_64) {
8629         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8630         TCGv_i64 tcg_double = tcg_temp_new_i64();
8631 
8632         for (pass = 0; pass < elements; pass++) {
8633             read_vec_element(s, tcg_int64, rn, pass, mop);
8634 
8635             if (is_signed) {
8636                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8637                                      tcg_shift, tcg_fpst);
8638             } else {
8639                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8640                                      tcg_shift, tcg_fpst);
8641             }
8642             if (elements == 1) {
8643                 write_fp_dreg(s, rd, tcg_double);
8644             } else {
8645                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8646             }
8647         }
8648     } else {
8649         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8650         TCGv_i32 tcg_float = tcg_temp_new_i32();
8651 
8652         for (pass = 0; pass < elements; pass++) {
8653             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8654 
8655             switch (size) {
8656             case MO_32:
8657                 if (fracbits) {
8658                     if (is_signed) {
8659                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8660                                              tcg_shift, tcg_fpst);
8661                     } else {
8662                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8663                                              tcg_shift, tcg_fpst);
8664                     }
8665                 } else {
8666                     if (is_signed) {
8667                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8668                     } else {
8669                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8670                     }
8671                 }
8672                 break;
8673             case MO_16:
8674                 if (fracbits) {
8675                     if (is_signed) {
8676                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8677                                              tcg_shift, tcg_fpst);
8678                     } else {
8679                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8680                                              tcg_shift, tcg_fpst);
8681                     }
8682                 } else {
8683                     if (is_signed) {
8684                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8685                     } else {
8686                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8687                     }
8688                 }
8689                 break;
8690             default:
8691                 g_assert_not_reached();
8692             }
8693 
8694             if (elements == 1) {
8695                 write_fp_sreg(s, rd, tcg_float);
8696             } else {
8697                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8698             }
8699         }
8700     }
8701 
8702     clear_vec_high(s, elements << size == 16, rd);
8703 }
8704 
8705 /* UCVTF/SCVTF - Integer to FP conversion */
8706 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8707                                          bool is_q, bool is_u,
8708                                          int immh, int immb, int opcode,
8709                                          int rn, int rd)
8710 {
8711     int size, elements, fracbits;
8712     int immhb = immh << 3 | immb;
8713 
8714     if (immh & 8) {
8715         size = MO_64;
8716         if (!is_scalar && !is_q) {
8717             unallocated_encoding(s);
8718             return;
8719         }
8720     } else if (immh & 4) {
8721         size = MO_32;
8722     } else if (immh & 2) {
8723         size = MO_16;
8724         if (!dc_isar_feature(aa64_fp16, s)) {
8725             unallocated_encoding(s);
8726             return;
8727         }
8728     } else {
8729         /* immh == 0 would be a failure of the decode logic */
8730         g_assert(immh == 1);
8731         unallocated_encoding(s);
8732         return;
8733     }
8734 
8735     if (is_scalar) {
8736         elements = 1;
8737     } else {
8738         elements = (8 << is_q) >> size;
8739     }
8740     fracbits = (16 << size) - immhb;
8741 
8742     if (!fp_access_check(s)) {
8743         return;
8744     }
8745 
8746     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8747 }
8748 
8749 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8750 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8751                                          bool is_q, bool is_u,
8752                                          int immh, int immb, int rn, int rd)
8753 {
8754     int immhb = immh << 3 | immb;
8755     int pass, size, fracbits;
8756     TCGv_ptr tcg_fpstatus;
8757     TCGv_i32 tcg_rmode, tcg_shift;
8758 
8759     if (immh & 0x8) {
8760         size = MO_64;
8761         if (!is_scalar && !is_q) {
8762             unallocated_encoding(s);
8763             return;
8764         }
8765     } else if (immh & 0x4) {
8766         size = MO_32;
8767     } else if (immh & 0x2) {
8768         size = MO_16;
8769         if (!dc_isar_feature(aa64_fp16, s)) {
8770             unallocated_encoding(s);
8771             return;
8772         }
8773     } else {
8774         /* Should have split out AdvSIMD modified immediate earlier.  */
8775         assert(immh == 1);
8776         unallocated_encoding(s);
8777         return;
8778     }
8779 
8780     if (!fp_access_check(s)) {
8781         return;
8782     }
8783 
8784     assert(!(is_scalar && is_q));
8785 
8786     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8787     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8788     fracbits = (16 << size) - immhb;
8789     tcg_shift = tcg_constant_i32(fracbits);
8790 
8791     if (size == MO_64) {
8792         int maxpass = is_scalar ? 1 : 2;
8793 
8794         for (pass = 0; pass < maxpass; pass++) {
8795             TCGv_i64 tcg_op = tcg_temp_new_i64();
8796 
8797             read_vec_element(s, tcg_op, rn, pass, MO_64);
8798             if (is_u) {
8799                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8800             } else {
8801                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8802             }
8803             write_vec_element(s, tcg_op, rd, pass, MO_64);
8804         }
8805         clear_vec_high(s, is_q, rd);
8806     } else {
8807         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8808         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8809 
8810         switch (size) {
8811         case MO_16:
8812             if (is_u) {
8813                 fn = gen_helper_vfp_touhh;
8814             } else {
8815                 fn = gen_helper_vfp_toshh;
8816             }
8817             break;
8818         case MO_32:
8819             if (is_u) {
8820                 fn = gen_helper_vfp_touls;
8821             } else {
8822                 fn = gen_helper_vfp_tosls;
8823             }
8824             break;
8825         default:
8826             g_assert_not_reached();
8827         }
8828 
8829         for (pass = 0; pass < maxpass; pass++) {
8830             TCGv_i32 tcg_op = tcg_temp_new_i32();
8831 
8832             read_vec_element_i32(s, tcg_op, rn, pass, size);
8833             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8834             if (is_scalar) {
8835                 write_fp_sreg(s, rd, tcg_op);
8836             } else {
8837                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8838             }
8839         }
8840         if (!is_scalar) {
8841             clear_vec_high(s, is_q, rd);
8842         }
8843     }
8844 
8845     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8846 }
8847 
8848 /* AdvSIMD scalar shift by immediate
8849  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8850  * +-----+---+-------------+------+------+--------+---+------+------+
8851  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8852  * +-----+---+-------------+------+------+--------+---+------+------+
8853  *
8854  * This is the scalar version so it works on a fixed sized registers
8855  */
8856 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8857 {
8858     int rd = extract32(insn, 0, 5);
8859     int rn = extract32(insn, 5, 5);
8860     int opcode = extract32(insn, 11, 5);
8861     int immb = extract32(insn, 16, 3);
8862     int immh = extract32(insn, 19, 4);
8863     bool is_u = extract32(insn, 29, 1);
8864 
8865     if (immh == 0) {
8866         unallocated_encoding(s);
8867         return;
8868     }
8869 
8870     switch (opcode) {
8871     case 0x08: /* SRI */
8872         if (!is_u) {
8873             unallocated_encoding(s);
8874             return;
8875         }
8876         /* fall through */
8877     case 0x00: /* SSHR / USHR */
8878     case 0x02: /* SSRA / USRA */
8879     case 0x04: /* SRSHR / URSHR */
8880     case 0x06: /* SRSRA / URSRA */
8881         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8882         break;
8883     case 0x0a: /* SHL / SLI */
8884         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8885         break;
8886     case 0x1c: /* SCVTF, UCVTF */
8887         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8888                                      opcode, rn, rd);
8889         break;
8890     case 0x10: /* SQSHRUN, SQSHRUN2 */
8891     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8892         if (!is_u) {
8893             unallocated_encoding(s);
8894             return;
8895         }
8896         handle_vec_simd_sqshrn(s, true, false, false, true,
8897                                immh, immb, opcode, rn, rd);
8898         break;
8899     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8900     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8901         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8902                                immh, immb, opcode, rn, rd);
8903         break;
8904     case 0xc: /* SQSHLU */
8905         if (!is_u) {
8906             unallocated_encoding(s);
8907             return;
8908         }
8909         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8910         break;
8911     case 0xe: /* SQSHL, UQSHL */
8912         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8913         break;
8914     case 0x1f: /* FCVTZS, FCVTZU */
8915         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8916         break;
8917     default:
8918         unallocated_encoding(s);
8919         break;
8920     }
8921 }
8922 
8923 /* AdvSIMD scalar three different
8924  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8925  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8926  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8927  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8928  */
8929 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8930 {
8931     bool is_u = extract32(insn, 29, 1);
8932     int size = extract32(insn, 22, 2);
8933     int opcode = extract32(insn, 12, 4);
8934     int rm = extract32(insn, 16, 5);
8935     int rn = extract32(insn, 5, 5);
8936     int rd = extract32(insn, 0, 5);
8937 
8938     if (is_u) {
8939         unallocated_encoding(s);
8940         return;
8941     }
8942 
8943     switch (opcode) {
8944     case 0x9: /* SQDMLAL, SQDMLAL2 */
8945     case 0xb: /* SQDMLSL, SQDMLSL2 */
8946     case 0xd: /* SQDMULL, SQDMULL2 */
8947         if (size == 0 || size == 3) {
8948             unallocated_encoding(s);
8949             return;
8950         }
8951         break;
8952     default:
8953         unallocated_encoding(s);
8954         return;
8955     }
8956 
8957     if (!fp_access_check(s)) {
8958         return;
8959     }
8960 
8961     if (size == 2) {
8962         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8963         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8964         TCGv_i64 tcg_res = tcg_temp_new_i64();
8965 
8966         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8967         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8968 
8969         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8970         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8971 
8972         switch (opcode) {
8973         case 0xd: /* SQDMULL, SQDMULL2 */
8974             break;
8975         case 0xb: /* SQDMLSL, SQDMLSL2 */
8976             tcg_gen_neg_i64(tcg_res, tcg_res);
8977             /* fall through */
8978         case 0x9: /* SQDMLAL, SQDMLAL2 */
8979             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8980             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8981                                               tcg_res, tcg_op1);
8982             break;
8983         default:
8984             g_assert_not_reached();
8985         }
8986 
8987         write_fp_dreg(s, rd, tcg_res);
8988     } else {
8989         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8990         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8991         TCGv_i64 tcg_res = tcg_temp_new_i64();
8992 
8993         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8994         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8995 
8996         switch (opcode) {
8997         case 0xd: /* SQDMULL, SQDMULL2 */
8998             break;
8999         case 0xb: /* SQDMLSL, SQDMLSL2 */
9000             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9001             /* fall through */
9002         case 0x9: /* SQDMLAL, SQDMLAL2 */
9003         {
9004             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9005             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9006             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9007                                               tcg_res, tcg_op3);
9008             break;
9009         }
9010         default:
9011             g_assert_not_reached();
9012         }
9013 
9014         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9015         write_fp_dreg(s, rd, tcg_res);
9016     }
9017 }
9018 
9019 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9020                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9021 {
9022     /* Handle 64x64->64 opcodes which are shared between the scalar
9023      * and vector 3-same groups. We cover every opcode where size == 3
9024      * is valid in either the three-reg-same (integer, not pairwise)
9025      * or scalar-three-reg-same groups.
9026      */
9027     TCGCond cond;
9028 
9029     switch (opcode) {
9030     case 0x1: /* SQADD */
9031         if (u) {
9032             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9033         } else {
9034             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9035         }
9036         break;
9037     case 0x5: /* SQSUB */
9038         if (u) {
9039             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9040         } else {
9041             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9042         }
9043         break;
9044     case 0x6: /* CMGT, CMHI */
9045         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9046          * We implement this using setcond (test) and then negating.
9047          */
9048         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9049     do_cmop:
9050         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9051         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9052         break;
9053     case 0x7: /* CMGE, CMHS */
9054         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9055         goto do_cmop;
9056     case 0x11: /* CMTST, CMEQ */
9057         if (u) {
9058             cond = TCG_COND_EQ;
9059             goto do_cmop;
9060         }
9061         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9062         break;
9063     case 0x8: /* SSHL, USHL */
9064         if (u) {
9065             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9066         } else {
9067             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9068         }
9069         break;
9070     case 0x9: /* SQSHL, UQSHL */
9071         if (u) {
9072             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9073         } else {
9074             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9075         }
9076         break;
9077     case 0xa: /* SRSHL, URSHL */
9078         if (u) {
9079             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9080         } else {
9081             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9082         }
9083         break;
9084     case 0xb: /* SQRSHL, UQRSHL */
9085         if (u) {
9086             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9087         } else {
9088             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9089         }
9090         break;
9091     case 0x10: /* ADD, SUB */
9092         if (u) {
9093             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9094         } else {
9095             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9096         }
9097         break;
9098     default:
9099         g_assert_not_reached();
9100     }
9101 }
9102 
9103 /* Handle the 3-same-operands float operations; shared by the scalar
9104  * and vector encodings. The caller must filter out any encodings
9105  * not allocated for the encoding it is dealing with.
9106  */
9107 static void handle_3same_float(DisasContext *s, int size, int elements,
9108                                int fpopcode, int rd, int rn, int rm)
9109 {
9110     int pass;
9111     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9112 
9113     for (pass = 0; pass < elements; pass++) {
9114         if (size) {
9115             /* Double */
9116             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9117             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9118             TCGv_i64 tcg_res = tcg_temp_new_i64();
9119 
9120             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9121             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9122 
9123             switch (fpopcode) {
9124             case 0x39: /* FMLS */
9125                 /* As usual for ARM, separate negation for fused multiply-add */
9126                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9127                 /* fall through */
9128             case 0x19: /* FMLA */
9129                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9130                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9131                                        tcg_res, fpst);
9132                 break;
9133             case 0x18: /* FMAXNM */
9134                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9135                 break;
9136             case 0x1a: /* FADD */
9137                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9138                 break;
9139             case 0x1b: /* FMULX */
9140                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9141                 break;
9142             case 0x1c: /* FCMEQ */
9143                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9144                 break;
9145             case 0x1e: /* FMAX */
9146                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9147                 break;
9148             case 0x1f: /* FRECPS */
9149                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9150                 break;
9151             case 0x38: /* FMINNM */
9152                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9153                 break;
9154             case 0x3a: /* FSUB */
9155                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9156                 break;
9157             case 0x3e: /* FMIN */
9158                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9159                 break;
9160             case 0x3f: /* FRSQRTS */
9161                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9162                 break;
9163             case 0x5b: /* FMUL */
9164                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9165                 break;
9166             case 0x5c: /* FCMGE */
9167                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9168                 break;
9169             case 0x5d: /* FACGE */
9170                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9171                 break;
9172             case 0x5f: /* FDIV */
9173                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9174                 break;
9175             case 0x7a: /* FABD */
9176                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9177                 gen_helper_vfp_absd(tcg_res, tcg_res);
9178                 break;
9179             case 0x7c: /* FCMGT */
9180                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9181                 break;
9182             case 0x7d: /* FACGT */
9183                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9184                 break;
9185             default:
9186                 g_assert_not_reached();
9187             }
9188 
9189             write_vec_element(s, tcg_res, rd, pass, MO_64);
9190         } else {
9191             /* Single */
9192             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9193             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9194             TCGv_i32 tcg_res = tcg_temp_new_i32();
9195 
9196             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9197             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9198 
9199             switch (fpopcode) {
9200             case 0x39: /* FMLS */
9201                 /* As usual for ARM, separate negation for fused multiply-add */
9202                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9203                 /* fall through */
9204             case 0x19: /* FMLA */
9205                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9206                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9207                                        tcg_res, fpst);
9208                 break;
9209             case 0x1a: /* FADD */
9210                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9211                 break;
9212             case 0x1b: /* FMULX */
9213                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9214                 break;
9215             case 0x1c: /* FCMEQ */
9216                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9217                 break;
9218             case 0x1e: /* FMAX */
9219                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9220                 break;
9221             case 0x1f: /* FRECPS */
9222                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9223                 break;
9224             case 0x18: /* FMAXNM */
9225                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9226                 break;
9227             case 0x38: /* FMINNM */
9228                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9229                 break;
9230             case 0x3a: /* FSUB */
9231                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9232                 break;
9233             case 0x3e: /* FMIN */
9234                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9235                 break;
9236             case 0x3f: /* FRSQRTS */
9237                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9238                 break;
9239             case 0x5b: /* FMUL */
9240                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9241                 break;
9242             case 0x5c: /* FCMGE */
9243                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9244                 break;
9245             case 0x5d: /* FACGE */
9246                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9247                 break;
9248             case 0x5f: /* FDIV */
9249                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9250                 break;
9251             case 0x7a: /* FABD */
9252                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9253                 gen_helper_vfp_abss(tcg_res, tcg_res);
9254                 break;
9255             case 0x7c: /* FCMGT */
9256                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9257                 break;
9258             case 0x7d: /* FACGT */
9259                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9260                 break;
9261             default:
9262                 g_assert_not_reached();
9263             }
9264 
9265             if (elements == 1) {
9266                 /* scalar single so clear high part */
9267                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9268 
9269                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9270                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9271             } else {
9272                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9273             }
9274         }
9275     }
9276 
9277     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9278 }
9279 
9280 /* AdvSIMD scalar three same
9281  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9282  * +-----+---+-----------+------+---+------+--------+---+------+------+
9283  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9284  * +-----+---+-----------+------+---+------+--------+---+------+------+
9285  */
9286 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9287 {
9288     int rd = extract32(insn, 0, 5);
9289     int rn = extract32(insn, 5, 5);
9290     int opcode = extract32(insn, 11, 5);
9291     int rm = extract32(insn, 16, 5);
9292     int size = extract32(insn, 22, 2);
9293     bool u = extract32(insn, 29, 1);
9294     TCGv_i64 tcg_rd;
9295 
9296     if (opcode >= 0x18) {
9297         /* Floating point: U, size[1] and opcode indicate operation */
9298         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9299         switch (fpopcode) {
9300         case 0x1b: /* FMULX */
9301         case 0x1f: /* FRECPS */
9302         case 0x3f: /* FRSQRTS */
9303         case 0x5d: /* FACGE */
9304         case 0x7d: /* FACGT */
9305         case 0x1c: /* FCMEQ */
9306         case 0x5c: /* FCMGE */
9307         case 0x7c: /* FCMGT */
9308         case 0x7a: /* FABD */
9309             break;
9310         default:
9311             unallocated_encoding(s);
9312             return;
9313         }
9314 
9315         if (!fp_access_check(s)) {
9316             return;
9317         }
9318 
9319         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9320         return;
9321     }
9322 
9323     switch (opcode) {
9324     case 0x1: /* SQADD, UQADD */
9325     case 0x5: /* SQSUB, UQSUB */
9326     case 0x9: /* SQSHL, UQSHL */
9327     case 0xb: /* SQRSHL, UQRSHL */
9328         break;
9329     case 0x8: /* SSHL, USHL */
9330     case 0xa: /* SRSHL, URSHL */
9331     case 0x6: /* CMGT, CMHI */
9332     case 0x7: /* CMGE, CMHS */
9333     case 0x11: /* CMTST, CMEQ */
9334     case 0x10: /* ADD, SUB (vector) */
9335         if (size != 3) {
9336             unallocated_encoding(s);
9337             return;
9338         }
9339         break;
9340     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9341         if (size != 1 && size != 2) {
9342             unallocated_encoding(s);
9343             return;
9344         }
9345         break;
9346     default:
9347         unallocated_encoding(s);
9348         return;
9349     }
9350 
9351     if (!fp_access_check(s)) {
9352         return;
9353     }
9354 
9355     tcg_rd = tcg_temp_new_i64();
9356 
9357     if (size == 3) {
9358         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9359         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9360 
9361         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9362     } else {
9363         /* Do a single operation on the lowest element in the vector.
9364          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9365          * no side effects for all these operations.
9366          * OPTME: special-purpose helpers would avoid doing some
9367          * unnecessary work in the helper for the 8 and 16 bit cases.
9368          */
9369         NeonGenTwoOpEnvFn *genenvfn;
9370         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9371         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9372         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9373 
9374         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9375         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9376 
9377         switch (opcode) {
9378         case 0x1: /* SQADD, UQADD */
9379         {
9380             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9381                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9382                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9383                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9384             };
9385             genenvfn = fns[size][u];
9386             break;
9387         }
9388         case 0x5: /* SQSUB, UQSUB */
9389         {
9390             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9391                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9392                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9393                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9394             };
9395             genenvfn = fns[size][u];
9396             break;
9397         }
9398         case 0x9: /* SQSHL, UQSHL */
9399         {
9400             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9401                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9402                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9403                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9404             };
9405             genenvfn = fns[size][u];
9406             break;
9407         }
9408         case 0xb: /* SQRSHL, UQRSHL */
9409         {
9410             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9411                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9412                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9413                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9414             };
9415             genenvfn = fns[size][u];
9416             break;
9417         }
9418         case 0x16: /* SQDMULH, SQRDMULH */
9419         {
9420             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9421                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9422                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9423             };
9424             assert(size == 1 || size == 2);
9425             genenvfn = fns[size - 1][u];
9426             break;
9427         }
9428         default:
9429             g_assert_not_reached();
9430         }
9431 
9432         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9433         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9434     }
9435 
9436     write_fp_dreg(s, rd, tcg_rd);
9437 }
9438 
9439 /* AdvSIMD scalar three same FP16
9440  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9441  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9442  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9443  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9444  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9445  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9446  */
9447 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9448                                                   uint32_t insn)
9449 {
9450     int rd = extract32(insn, 0, 5);
9451     int rn = extract32(insn, 5, 5);
9452     int opcode = extract32(insn, 11, 3);
9453     int rm = extract32(insn, 16, 5);
9454     bool u = extract32(insn, 29, 1);
9455     bool a = extract32(insn, 23, 1);
9456     int fpopcode = opcode | (a << 3) |  (u << 4);
9457     TCGv_ptr fpst;
9458     TCGv_i32 tcg_op1;
9459     TCGv_i32 tcg_op2;
9460     TCGv_i32 tcg_res;
9461 
9462     switch (fpopcode) {
9463     case 0x03: /* FMULX */
9464     case 0x04: /* FCMEQ (reg) */
9465     case 0x07: /* FRECPS */
9466     case 0x0f: /* FRSQRTS */
9467     case 0x14: /* FCMGE (reg) */
9468     case 0x15: /* FACGE */
9469     case 0x1a: /* FABD */
9470     case 0x1c: /* FCMGT (reg) */
9471     case 0x1d: /* FACGT */
9472         break;
9473     default:
9474         unallocated_encoding(s);
9475         return;
9476     }
9477 
9478     if (!dc_isar_feature(aa64_fp16, s)) {
9479         unallocated_encoding(s);
9480     }
9481 
9482     if (!fp_access_check(s)) {
9483         return;
9484     }
9485 
9486     fpst = fpstatus_ptr(FPST_FPCR_F16);
9487 
9488     tcg_op1 = read_fp_hreg(s, rn);
9489     tcg_op2 = read_fp_hreg(s, rm);
9490     tcg_res = tcg_temp_new_i32();
9491 
9492     switch (fpopcode) {
9493     case 0x03: /* FMULX */
9494         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9495         break;
9496     case 0x04: /* FCMEQ (reg) */
9497         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9498         break;
9499     case 0x07: /* FRECPS */
9500         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9501         break;
9502     case 0x0f: /* FRSQRTS */
9503         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9504         break;
9505     case 0x14: /* FCMGE (reg) */
9506         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9507         break;
9508     case 0x15: /* FACGE */
9509         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9510         break;
9511     case 0x1a: /* FABD */
9512         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9513         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9514         break;
9515     case 0x1c: /* FCMGT (reg) */
9516         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9517         break;
9518     case 0x1d: /* FACGT */
9519         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9520         break;
9521     default:
9522         g_assert_not_reached();
9523     }
9524 
9525     write_fp_sreg(s, rd, tcg_res);
9526 }
9527 
9528 /* AdvSIMD scalar three same extra
9529  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9530  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9531  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9532  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9533  */
9534 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9535                                                    uint32_t insn)
9536 {
9537     int rd = extract32(insn, 0, 5);
9538     int rn = extract32(insn, 5, 5);
9539     int opcode = extract32(insn, 11, 4);
9540     int rm = extract32(insn, 16, 5);
9541     int size = extract32(insn, 22, 2);
9542     bool u = extract32(insn, 29, 1);
9543     TCGv_i32 ele1, ele2, ele3;
9544     TCGv_i64 res;
9545     bool feature;
9546 
9547     switch (u * 16 + opcode) {
9548     case 0x10: /* SQRDMLAH (vector) */
9549     case 0x11: /* SQRDMLSH (vector) */
9550         if (size != 1 && size != 2) {
9551             unallocated_encoding(s);
9552             return;
9553         }
9554         feature = dc_isar_feature(aa64_rdm, s);
9555         break;
9556     default:
9557         unallocated_encoding(s);
9558         return;
9559     }
9560     if (!feature) {
9561         unallocated_encoding(s);
9562         return;
9563     }
9564     if (!fp_access_check(s)) {
9565         return;
9566     }
9567 
9568     /* Do a single operation on the lowest element in the vector.
9569      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9570      * with no side effects for all these operations.
9571      * OPTME: special-purpose helpers would avoid doing some
9572      * unnecessary work in the helper for the 16 bit cases.
9573      */
9574     ele1 = tcg_temp_new_i32();
9575     ele2 = tcg_temp_new_i32();
9576     ele3 = tcg_temp_new_i32();
9577 
9578     read_vec_element_i32(s, ele1, rn, 0, size);
9579     read_vec_element_i32(s, ele2, rm, 0, size);
9580     read_vec_element_i32(s, ele3, rd, 0, size);
9581 
9582     switch (opcode) {
9583     case 0x0: /* SQRDMLAH */
9584         if (size == 1) {
9585             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9586         } else {
9587             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9588         }
9589         break;
9590     case 0x1: /* SQRDMLSH */
9591         if (size == 1) {
9592             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9593         } else {
9594             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9595         }
9596         break;
9597     default:
9598         g_assert_not_reached();
9599     }
9600 
9601     res = tcg_temp_new_i64();
9602     tcg_gen_extu_i32_i64(res, ele3);
9603     write_fp_dreg(s, rd, res);
9604 }
9605 
9606 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9607                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9608                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9609 {
9610     /* Handle 64->64 opcodes which are shared between the scalar and
9611      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9612      * is valid in either group and also the double-precision fp ops.
9613      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9614      * requires them.
9615      */
9616     TCGCond cond;
9617 
9618     switch (opcode) {
9619     case 0x4: /* CLS, CLZ */
9620         if (u) {
9621             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9622         } else {
9623             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9624         }
9625         break;
9626     case 0x5: /* NOT */
9627         /* This opcode is shared with CNT and RBIT but we have earlier
9628          * enforced that size == 3 if and only if this is the NOT insn.
9629          */
9630         tcg_gen_not_i64(tcg_rd, tcg_rn);
9631         break;
9632     case 0x7: /* SQABS, SQNEG */
9633         if (u) {
9634             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9635         } else {
9636             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9637         }
9638         break;
9639     case 0xa: /* CMLT */
9640         /* 64 bit integer comparison against zero, result is
9641          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9642          * subtracting 1.
9643          */
9644         cond = TCG_COND_LT;
9645     do_cmop:
9646         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9647         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9648         break;
9649     case 0x8: /* CMGT, CMGE */
9650         cond = u ? TCG_COND_GE : TCG_COND_GT;
9651         goto do_cmop;
9652     case 0x9: /* CMEQ, CMLE */
9653         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9654         goto do_cmop;
9655     case 0xb: /* ABS, NEG */
9656         if (u) {
9657             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9658         } else {
9659             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9660         }
9661         break;
9662     case 0x2f: /* FABS */
9663         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9664         break;
9665     case 0x6f: /* FNEG */
9666         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9667         break;
9668     case 0x7f: /* FSQRT */
9669         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9670         break;
9671     case 0x1a: /* FCVTNS */
9672     case 0x1b: /* FCVTMS */
9673     case 0x1c: /* FCVTAS */
9674     case 0x3a: /* FCVTPS */
9675     case 0x3b: /* FCVTZS */
9676         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9677         break;
9678     case 0x5a: /* FCVTNU */
9679     case 0x5b: /* FCVTMU */
9680     case 0x5c: /* FCVTAU */
9681     case 0x7a: /* FCVTPU */
9682     case 0x7b: /* FCVTZU */
9683         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9684         break;
9685     case 0x18: /* FRINTN */
9686     case 0x19: /* FRINTM */
9687     case 0x38: /* FRINTP */
9688     case 0x39: /* FRINTZ */
9689     case 0x58: /* FRINTA */
9690     case 0x79: /* FRINTI */
9691         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9692         break;
9693     case 0x59: /* FRINTX */
9694         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9695         break;
9696     case 0x1e: /* FRINT32Z */
9697     case 0x5e: /* FRINT32X */
9698         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9699         break;
9700     case 0x1f: /* FRINT64Z */
9701     case 0x5f: /* FRINT64X */
9702         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9703         break;
9704     default:
9705         g_assert_not_reached();
9706     }
9707 }
9708 
9709 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9710                                    bool is_scalar, bool is_u, bool is_q,
9711                                    int size, int rn, int rd)
9712 {
9713     bool is_double = (size == MO_64);
9714     TCGv_ptr fpst;
9715 
9716     if (!fp_access_check(s)) {
9717         return;
9718     }
9719 
9720     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9721 
9722     if (is_double) {
9723         TCGv_i64 tcg_op = tcg_temp_new_i64();
9724         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9725         TCGv_i64 tcg_res = tcg_temp_new_i64();
9726         NeonGenTwoDoubleOpFn *genfn;
9727         bool swap = false;
9728         int pass;
9729 
9730         switch (opcode) {
9731         case 0x2e: /* FCMLT (zero) */
9732             swap = true;
9733             /* fallthrough */
9734         case 0x2c: /* FCMGT (zero) */
9735             genfn = gen_helper_neon_cgt_f64;
9736             break;
9737         case 0x2d: /* FCMEQ (zero) */
9738             genfn = gen_helper_neon_ceq_f64;
9739             break;
9740         case 0x6d: /* FCMLE (zero) */
9741             swap = true;
9742             /* fall through */
9743         case 0x6c: /* FCMGE (zero) */
9744             genfn = gen_helper_neon_cge_f64;
9745             break;
9746         default:
9747             g_assert_not_reached();
9748         }
9749 
9750         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9751             read_vec_element(s, tcg_op, rn, pass, MO_64);
9752             if (swap) {
9753                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9754             } else {
9755                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9756             }
9757             write_vec_element(s, tcg_res, rd, pass, MO_64);
9758         }
9759 
9760         clear_vec_high(s, !is_scalar, rd);
9761     } else {
9762         TCGv_i32 tcg_op = tcg_temp_new_i32();
9763         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9764         TCGv_i32 tcg_res = tcg_temp_new_i32();
9765         NeonGenTwoSingleOpFn *genfn;
9766         bool swap = false;
9767         int pass, maxpasses;
9768 
9769         if (size == MO_16) {
9770             switch (opcode) {
9771             case 0x2e: /* FCMLT (zero) */
9772                 swap = true;
9773                 /* fall through */
9774             case 0x2c: /* FCMGT (zero) */
9775                 genfn = gen_helper_advsimd_cgt_f16;
9776                 break;
9777             case 0x2d: /* FCMEQ (zero) */
9778                 genfn = gen_helper_advsimd_ceq_f16;
9779                 break;
9780             case 0x6d: /* FCMLE (zero) */
9781                 swap = true;
9782                 /* fall through */
9783             case 0x6c: /* FCMGE (zero) */
9784                 genfn = gen_helper_advsimd_cge_f16;
9785                 break;
9786             default:
9787                 g_assert_not_reached();
9788             }
9789         } else {
9790             switch (opcode) {
9791             case 0x2e: /* FCMLT (zero) */
9792                 swap = true;
9793                 /* fall through */
9794             case 0x2c: /* FCMGT (zero) */
9795                 genfn = gen_helper_neon_cgt_f32;
9796                 break;
9797             case 0x2d: /* FCMEQ (zero) */
9798                 genfn = gen_helper_neon_ceq_f32;
9799                 break;
9800             case 0x6d: /* FCMLE (zero) */
9801                 swap = true;
9802                 /* fall through */
9803             case 0x6c: /* FCMGE (zero) */
9804                 genfn = gen_helper_neon_cge_f32;
9805                 break;
9806             default:
9807                 g_assert_not_reached();
9808             }
9809         }
9810 
9811         if (is_scalar) {
9812             maxpasses = 1;
9813         } else {
9814             int vector_size = 8 << is_q;
9815             maxpasses = vector_size >> size;
9816         }
9817 
9818         for (pass = 0; pass < maxpasses; pass++) {
9819             read_vec_element_i32(s, tcg_op, rn, pass, size);
9820             if (swap) {
9821                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9822             } else {
9823                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9824             }
9825             if (is_scalar) {
9826                 write_fp_sreg(s, rd, tcg_res);
9827             } else {
9828                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9829             }
9830         }
9831 
9832         if (!is_scalar) {
9833             clear_vec_high(s, is_q, rd);
9834         }
9835     }
9836 }
9837 
9838 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9839                                     bool is_scalar, bool is_u, bool is_q,
9840                                     int size, int rn, int rd)
9841 {
9842     bool is_double = (size == 3);
9843     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9844 
9845     if (is_double) {
9846         TCGv_i64 tcg_op = tcg_temp_new_i64();
9847         TCGv_i64 tcg_res = tcg_temp_new_i64();
9848         int pass;
9849 
9850         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9851             read_vec_element(s, tcg_op, rn, pass, MO_64);
9852             switch (opcode) {
9853             case 0x3d: /* FRECPE */
9854                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9855                 break;
9856             case 0x3f: /* FRECPX */
9857                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9858                 break;
9859             case 0x7d: /* FRSQRTE */
9860                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9861                 break;
9862             default:
9863                 g_assert_not_reached();
9864             }
9865             write_vec_element(s, tcg_res, rd, pass, MO_64);
9866         }
9867         clear_vec_high(s, !is_scalar, rd);
9868     } else {
9869         TCGv_i32 tcg_op = tcg_temp_new_i32();
9870         TCGv_i32 tcg_res = tcg_temp_new_i32();
9871         int pass, maxpasses;
9872 
9873         if (is_scalar) {
9874             maxpasses = 1;
9875         } else {
9876             maxpasses = is_q ? 4 : 2;
9877         }
9878 
9879         for (pass = 0; pass < maxpasses; pass++) {
9880             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9881 
9882             switch (opcode) {
9883             case 0x3c: /* URECPE */
9884                 gen_helper_recpe_u32(tcg_res, tcg_op);
9885                 break;
9886             case 0x3d: /* FRECPE */
9887                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9888                 break;
9889             case 0x3f: /* FRECPX */
9890                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9891                 break;
9892             case 0x7d: /* FRSQRTE */
9893                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9894                 break;
9895             default:
9896                 g_assert_not_reached();
9897             }
9898 
9899             if (is_scalar) {
9900                 write_fp_sreg(s, rd, tcg_res);
9901             } else {
9902                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9903             }
9904         }
9905         if (!is_scalar) {
9906             clear_vec_high(s, is_q, rd);
9907         }
9908     }
9909 }
9910 
9911 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9912                                 int opcode, bool u, bool is_q,
9913                                 int size, int rn, int rd)
9914 {
9915     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9916      * in the source becomes a size element in the destination).
9917      */
9918     int pass;
9919     TCGv_i32 tcg_res[2];
9920     int destelt = is_q ? 2 : 0;
9921     int passes = scalar ? 1 : 2;
9922 
9923     if (scalar) {
9924         tcg_res[1] = tcg_constant_i32(0);
9925     }
9926 
9927     for (pass = 0; pass < passes; pass++) {
9928         TCGv_i64 tcg_op = tcg_temp_new_i64();
9929         NeonGenNarrowFn *genfn = NULL;
9930         NeonGenNarrowEnvFn *genenvfn = NULL;
9931 
9932         if (scalar) {
9933             read_vec_element(s, tcg_op, rn, pass, size + 1);
9934         } else {
9935             read_vec_element(s, tcg_op, rn, pass, MO_64);
9936         }
9937         tcg_res[pass] = tcg_temp_new_i32();
9938 
9939         switch (opcode) {
9940         case 0x12: /* XTN, SQXTUN */
9941         {
9942             static NeonGenNarrowFn * const xtnfns[3] = {
9943                 gen_helper_neon_narrow_u8,
9944                 gen_helper_neon_narrow_u16,
9945                 tcg_gen_extrl_i64_i32,
9946             };
9947             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9948                 gen_helper_neon_unarrow_sat8,
9949                 gen_helper_neon_unarrow_sat16,
9950                 gen_helper_neon_unarrow_sat32,
9951             };
9952             if (u) {
9953                 genenvfn = sqxtunfns[size];
9954             } else {
9955                 genfn = xtnfns[size];
9956             }
9957             break;
9958         }
9959         case 0x14: /* SQXTN, UQXTN */
9960         {
9961             static NeonGenNarrowEnvFn * const fns[3][2] = {
9962                 { gen_helper_neon_narrow_sat_s8,
9963                   gen_helper_neon_narrow_sat_u8 },
9964                 { gen_helper_neon_narrow_sat_s16,
9965                   gen_helper_neon_narrow_sat_u16 },
9966                 { gen_helper_neon_narrow_sat_s32,
9967                   gen_helper_neon_narrow_sat_u32 },
9968             };
9969             genenvfn = fns[size][u];
9970             break;
9971         }
9972         case 0x16: /* FCVTN, FCVTN2 */
9973             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9974             if (size == 2) {
9975                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9976             } else {
9977                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9978                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9979                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9980                 TCGv_i32 ahp = get_ahp_flag();
9981 
9982                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9983                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9984                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9985                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9986             }
9987             break;
9988         case 0x36: /* BFCVTN, BFCVTN2 */
9989             {
9990                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9991                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9992             }
9993             break;
9994         case 0x56:  /* FCVTXN, FCVTXN2 */
9995             /* 64 bit to 32 bit float conversion
9996              * with von Neumann rounding (round to odd)
9997              */
9998             assert(size == 2);
9999             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10000             break;
10001         default:
10002             g_assert_not_reached();
10003         }
10004 
10005         if (genfn) {
10006             genfn(tcg_res[pass], tcg_op);
10007         } else if (genenvfn) {
10008             genenvfn(tcg_res[pass], cpu_env, tcg_op);
10009         }
10010     }
10011 
10012     for (pass = 0; pass < 2; pass++) {
10013         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10014     }
10015     clear_vec_high(s, is_q, rd);
10016 }
10017 
10018 /* Remaining saturating accumulating ops */
10019 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10020                                 bool is_q, int size, int rn, int rd)
10021 {
10022     bool is_double = (size == 3);
10023 
10024     if (is_double) {
10025         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10026         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10027         int pass;
10028 
10029         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10030             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10031             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10032 
10033             if (is_u) { /* USQADD */
10034                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10035             } else { /* SUQADD */
10036                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10037             }
10038             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10039         }
10040         clear_vec_high(s, !is_scalar, rd);
10041     } else {
10042         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10043         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10044         int pass, maxpasses;
10045 
10046         if (is_scalar) {
10047             maxpasses = 1;
10048         } else {
10049             maxpasses = is_q ? 4 : 2;
10050         }
10051 
10052         for (pass = 0; pass < maxpasses; pass++) {
10053             if (is_scalar) {
10054                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10055                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10056             } else {
10057                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10058                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10059             }
10060 
10061             if (is_u) { /* USQADD */
10062                 switch (size) {
10063                 case 0:
10064                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10065                     break;
10066                 case 1:
10067                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10068                     break;
10069                 case 2:
10070                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10071                     break;
10072                 default:
10073                     g_assert_not_reached();
10074                 }
10075             } else { /* SUQADD */
10076                 switch (size) {
10077                 case 0:
10078                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10079                     break;
10080                 case 1:
10081                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10082                     break;
10083                 case 2:
10084                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10085                     break;
10086                 default:
10087                     g_assert_not_reached();
10088                 }
10089             }
10090 
10091             if (is_scalar) {
10092                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10093             }
10094             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10095         }
10096         clear_vec_high(s, is_q, rd);
10097     }
10098 }
10099 
10100 /* AdvSIMD scalar two reg misc
10101  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10102  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10103  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10104  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10105  */
10106 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10107 {
10108     int rd = extract32(insn, 0, 5);
10109     int rn = extract32(insn, 5, 5);
10110     int opcode = extract32(insn, 12, 5);
10111     int size = extract32(insn, 22, 2);
10112     bool u = extract32(insn, 29, 1);
10113     bool is_fcvt = false;
10114     int rmode;
10115     TCGv_i32 tcg_rmode;
10116     TCGv_ptr tcg_fpstatus;
10117 
10118     switch (opcode) {
10119     case 0x3: /* USQADD / SUQADD*/
10120         if (!fp_access_check(s)) {
10121             return;
10122         }
10123         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10124         return;
10125     case 0x7: /* SQABS / SQNEG */
10126         break;
10127     case 0xa: /* CMLT */
10128         if (u) {
10129             unallocated_encoding(s);
10130             return;
10131         }
10132         /* fall through */
10133     case 0x8: /* CMGT, CMGE */
10134     case 0x9: /* CMEQ, CMLE */
10135     case 0xb: /* ABS, NEG */
10136         if (size != 3) {
10137             unallocated_encoding(s);
10138             return;
10139         }
10140         break;
10141     case 0x12: /* SQXTUN */
10142         if (!u) {
10143             unallocated_encoding(s);
10144             return;
10145         }
10146         /* fall through */
10147     case 0x14: /* SQXTN, UQXTN */
10148         if (size == 3) {
10149             unallocated_encoding(s);
10150             return;
10151         }
10152         if (!fp_access_check(s)) {
10153             return;
10154         }
10155         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10156         return;
10157     case 0xc ... 0xf:
10158     case 0x16 ... 0x1d:
10159     case 0x1f:
10160         /* Floating point: U, size[1] and opcode indicate operation;
10161          * size[0] indicates single or double precision.
10162          */
10163         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10164         size = extract32(size, 0, 1) ? 3 : 2;
10165         switch (opcode) {
10166         case 0x2c: /* FCMGT (zero) */
10167         case 0x2d: /* FCMEQ (zero) */
10168         case 0x2e: /* FCMLT (zero) */
10169         case 0x6c: /* FCMGE (zero) */
10170         case 0x6d: /* FCMLE (zero) */
10171             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10172             return;
10173         case 0x1d: /* SCVTF */
10174         case 0x5d: /* UCVTF */
10175         {
10176             bool is_signed = (opcode == 0x1d);
10177             if (!fp_access_check(s)) {
10178                 return;
10179             }
10180             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10181             return;
10182         }
10183         case 0x3d: /* FRECPE */
10184         case 0x3f: /* FRECPX */
10185         case 0x7d: /* FRSQRTE */
10186             if (!fp_access_check(s)) {
10187                 return;
10188             }
10189             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10190             return;
10191         case 0x1a: /* FCVTNS */
10192         case 0x1b: /* FCVTMS */
10193         case 0x3a: /* FCVTPS */
10194         case 0x3b: /* FCVTZS */
10195         case 0x5a: /* FCVTNU */
10196         case 0x5b: /* FCVTMU */
10197         case 0x7a: /* FCVTPU */
10198         case 0x7b: /* FCVTZU */
10199             is_fcvt = true;
10200             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10201             break;
10202         case 0x1c: /* FCVTAS */
10203         case 0x5c: /* FCVTAU */
10204             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10205             is_fcvt = true;
10206             rmode = FPROUNDING_TIEAWAY;
10207             break;
10208         case 0x56: /* FCVTXN, FCVTXN2 */
10209             if (size == 2) {
10210                 unallocated_encoding(s);
10211                 return;
10212             }
10213             if (!fp_access_check(s)) {
10214                 return;
10215             }
10216             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10217             return;
10218         default:
10219             unallocated_encoding(s);
10220             return;
10221         }
10222         break;
10223     default:
10224         unallocated_encoding(s);
10225         return;
10226     }
10227 
10228     if (!fp_access_check(s)) {
10229         return;
10230     }
10231 
10232     if (is_fcvt) {
10233         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10234         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10235     } else {
10236         tcg_fpstatus = NULL;
10237         tcg_rmode = NULL;
10238     }
10239 
10240     if (size == 3) {
10241         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10242         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10243 
10244         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10245         write_fp_dreg(s, rd, tcg_rd);
10246     } else {
10247         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10248         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10249 
10250         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10251 
10252         switch (opcode) {
10253         case 0x7: /* SQABS, SQNEG */
10254         {
10255             NeonGenOneOpEnvFn *genfn;
10256             static NeonGenOneOpEnvFn * const fns[3][2] = {
10257                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10258                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10259                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10260             };
10261             genfn = fns[size][u];
10262             genfn(tcg_rd, cpu_env, tcg_rn);
10263             break;
10264         }
10265         case 0x1a: /* FCVTNS */
10266         case 0x1b: /* FCVTMS */
10267         case 0x1c: /* FCVTAS */
10268         case 0x3a: /* FCVTPS */
10269         case 0x3b: /* FCVTZS */
10270             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10271                                  tcg_fpstatus);
10272             break;
10273         case 0x5a: /* FCVTNU */
10274         case 0x5b: /* FCVTMU */
10275         case 0x5c: /* FCVTAU */
10276         case 0x7a: /* FCVTPU */
10277         case 0x7b: /* FCVTZU */
10278             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10279                                  tcg_fpstatus);
10280             break;
10281         default:
10282             g_assert_not_reached();
10283         }
10284 
10285         write_fp_sreg(s, rd, tcg_rd);
10286     }
10287 
10288     if (is_fcvt) {
10289         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10290     }
10291 }
10292 
10293 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10294 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10295                                  int immh, int immb, int opcode, int rn, int rd)
10296 {
10297     int size = 32 - clz32(immh) - 1;
10298     int immhb = immh << 3 | immb;
10299     int shift = 2 * (8 << size) - immhb;
10300     GVecGen2iFn *gvec_fn;
10301 
10302     if (extract32(immh, 3, 1) && !is_q) {
10303         unallocated_encoding(s);
10304         return;
10305     }
10306     tcg_debug_assert(size <= 3);
10307 
10308     if (!fp_access_check(s)) {
10309         return;
10310     }
10311 
10312     switch (opcode) {
10313     case 0x02: /* SSRA / USRA (accumulate) */
10314         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10315         break;
10316 
10317     case 0x08: /* SRI */
10318         gvec_fn = gen_gvec_sri;
10319         break;
10320 
10321     case 0x00: /* SSHR / USHR */
10322         if (is_u) {
10323             if (shift == 8 << size) {
10324                 /* Shift count the same size as element size produces zero.  */
10325                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10326                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10327                 return;
10328             }
10329             gvec_fn = tcg_gen_gvec_shri;
10330         } else {
10331             /* Shift count the same size as element size produces all sign.  */
10332             if (shift == 8 << size) {
10333                 shift -= 1;
10334             }
10335             gvec_fn = tcg_gen_gvec_sari;
10336         }
10337         break;
10338 
10339     case 0x04: /* SRSHR / URSHR (rounding) */
10340         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10341         break;
10342 
10343     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10344         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10345         break;
10346 
10347     default:
10348         g_assert_not_reached();
10349     }
10350 
10351     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10352 }
10353 
10354 /* SHL/SLI - Vector shift left */
10355 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10356                                  int immh, int immb, int opcode, int rn, int rd)
10357 {
10358     int size = 32 - clz32(immh) - 1;
10359     int immhb = immh << 3 | immb;
10360     int shift = immhb - (8 << size);
10361 
10362     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10363     assert(size >= 0 && size <= 3);
10364 
10365     if (extract32(immh, 3, 1) && !is_q) {
10366         unallocated_encoding(s);
10367         return;
10368     }
10369 
10370     if (!fp_access_check(s)) {
10371         return;
10372     }
10373 
10374     if (insert) {
10375         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10376     } else {
10377         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10378     }
10379 }
10380 
10381 /* USHLL/SHLL - Vector shift left with widening */
10382 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10383                                  int immh, int immb, int opcode, int rn, int rd)
10384 {
10385     int size = 32 - clz32(immh) - 1;
10386     int immhb = immh << 3 | immb;
10387     int shift = immhb - (8 << size);
10388     int dsize = 64;
10389     int esize = 8 << size;
10390     int elements = dsize/esize;
10391     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10392     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10393     int i;
10394 
10395     if (size >= 3) {
10396         unallocated_encoding(s);
10397         return;
10398     }
10399 
10400     if (!fp_access_check(s)) {
10401         return;
10402     }
10403 
10404     /* For the LL variants the store is larger than the load,
10405      * so if rd == rn we would overwrite parts of our input.
10406      * So load everything right now and use shifts in the main loop.
10407      */
10408     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10409 
10410     for (i = 0; i < elements; i++) {
10411         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10412         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10413         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10414         write_vec_element(s, tcg_rd, rd, i, size + 1);
10415     }
10416 }
10417 
10418 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10419 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10420                                  int immh, int immb, int opcode, int rn, int rd)
10421 {
10422     int immhb = immh << 3 | immb;
10423     int size = 32 - clz32(immh) - 1;
10424     int dsize = 64;
10425     int esize = 8 << size;
10426     int elements = dsize/esize;
10427     int shift = (2 * esize) - immhb;
10428     bool round = extract32(opcode, 0, 1);
10429     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10430     TCGv_i64 tcg_round;
10431     int i;
10432 
10433     if (extract32(immh, 3, 1)) {
10434         unallocated_encoding(s);
10435         return;
10436     }
10437 
10438     if (!fp_access_check(s)) {
10439         return;
10440     }
10441 
10442     tcg_rn = tcg_temp_new_i64();
10443     tcg_rd = tcg_temp_new_i64();
10444     tcg_final = tcg_temp_new_i64();
10445     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10446 
10447     if (round) {
10448         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10449     } else {
10450         tcg_round = NULL;
10451     }
10452 
10453     for (i = 0; i < elements; i++) {
10454         read_vec_element(s, tcg_rn, rn, i, size+1);
10455         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10456                                 false, true, size+1, shift);
10457 
10458         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10459     }
10460 
10461     if (!is_q) {
10462         write_vec_element(s, tcg_final, rd, 0, MO_64);
10463     } else {
10464         write_vec_element(s, tcg_final, rd, 1, MO_64);
10465     }
10466 
10467     clear_vec_high(s, is_q, rd);
10468 }
10469 
10470 
10471 /* AdvSIMD shift by immediate
10472  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10473  * +---+---+---+-------------+------+------+--------+---+------+------+
10474  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10475  * +---+---+---+-------------+------+------+--------+---+------+------+
10476  */
10477 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10478 {
10479     int rd = extract32(insn, 0, 5);
10480     int rn = extract32(insn, 5, 5);
10481     int opcode = extract32(insn, 11, 5);
10482     int immb = extract32(insn, 16, 3);
10483     int immh = extract32(insn, 19, 4);
10484     bool is_u = extract32(insn, 29, 1);
10485     bool is_q = extract32(insn, 30, 1);
10486 
10487     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10488     assert(immh != 0);
10489 
10490     switch (opcode) {
10491     case 0x08: /* SRI */
10492         if (!is_u) {
10493             unallocated_encoding(s);
10494             return;
10495         }
10496         /* fall through */
10497     case 0x00: /* SSHR / USHR */
10498     case 0x02: /* SSRA / USRA (accumulate) */
10499     case 0x04: /* SRSHR / URSHR (rounding) */
10500     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10501         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10502         break;
10503     case 0x0a: /* SHL / SLI */
10504         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10505         break;
10506     case 0x10: /* SHRN */
10507     case 0x11: /* RSHRN / SQRSHRUN */
10508         if (is_u) {
10509             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10510                                    opcode, rn, rd);
10511         } else {
10512             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10513         }
10514         break;
10515     case 0x12: /* SQSHRN / UQSHRN */
10516     case 0x13: /* SQRSHRN / UQRSHRN */
10517         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10518                                opcode, rn, rd);
10519         break;
10520     case 0x14: /* SSHLL / USHLL */
10521         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10522         break;
10523     case 0x1c: /* SCVTF / UCVTF */
10524         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10525                                      opcode, rn, rd);
10526         break;
10527     case 0xc: /* SQSHLU */
10528         if (!is_u) {
10529             unallocated_encoding(s);
10530             return;
10531         }
10532         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10533         break;
10534     case 0xe: /* SQSHL, UQSHL */
10535         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10536         break;
10537     case 0x1f: /* FCVTZS/ FCVTZU */
10538         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10539         return;
10540     default:
10541         unallocated_encoding(s);
10542         return;
10543     }
10544 }
10545 
10546 /* Generate code to do a "long" addition or subtraction, ie one done in
10547  * TCGv_i64 on vector lanes twice the width specified by size.
10548  */
10549 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10550                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10551 {
10552     static NeonGenTwo64OpFn * const fns[3][2] = {
10553         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10554         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10555         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10556     };
10557     NeonGenTwo64OpFn *genfn;
10558     assert(size < 3);
10559 
10560     genfn = fns[size][is_sub];
10561     genfn(tcg_res, tcg_op1, tcg_op2);
10562 }
10563 
10564 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10565                                 int opcode, int rd, int rn, int rm)
10566 {
10567     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10568     TCGv_i64 tcg_res[2];
10569     int pass, accop;
10570 
10571     tcg_res[0] = tcg_temp_new_i64();
10572     tcg_res[1] = tcg_temp_new_i64();
10573 
10574     /* Does this op do an adding accumulate, a subtracting accumulate,
10575      * or no accumulate at all?
10576      */
10577     switch (opcode) {
10578     case 5:
10579     case 8:
10580     case 9:
10581         accop = 1;
10582         break;
10583     case 10:
10584     case 11:
10585         accop = -1;
10586         break;
10587     default:
10588         accop = 0;
10589         break;
10590     }
10591 
10592     if (accop != 0) {
10593         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10594         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10595     }
10596 
10597     /* size == 2 means two 32x32->64 operations; this is worth special
10598      * casing because we can generally handle it inline.
10599      */
10600     if (size == 2) {
10601         for (pass = 0; pass < 2; pass++) {
10602             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10603             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10604             TCGv_i64 tcg_passres;
10605             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10606 
10607             int elt = pass + is_q * 2;
10608 
10609             read_vec_element(s, tcg_op1, rn, elt, memop);
10610             read_vec_element(s, tcg_op2, rm, elt, memop);
10611 
10612             if (accop == 0) {
10613                 tcg_passres = tcg_res[pass];
10614             } else {
10615                 tcg_passres = tcg_temp_new_i64();
10616             }
10617 
10618             switch (opcode) {
10619             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10620                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10621                 break;
10622             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10623                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10624                 break;
10625             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10626             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10627             {
10628                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10629                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10630 
10631                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10632                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10633                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10634                                     tcg_passres,
10635                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10636                 break;
10637             }
10638             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10639             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10640             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10641                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10642                 break;
10643             case 9: /* SQDMLAL, SQDMLAL2 */
10644             case 11: /* SQDMLSL, SQDMLSL2 */
10645             case 13: /* SQDMULL, SQDMULL2 */
10646                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10647                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10648                                                   tcg_passres, tcg_passres);
10649                 break;
10650             default:
10651                 g_assert_not_reached();
10652             }
10653 
10654             if (opcode == 9 || opcode == 11) {
10655                 /* saturating accumulate ops */
10656                 if (accop < 0) {
10657                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10658                 }
10659                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10660                                                   tcg_res[pass], tcg_passres);
10661             } else if (accop > 0) {
10662                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10663             } else if (accop < 0) {
10664                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10665             }
10666         }
10667     } else {
10668         /* size 0 or 1, generally helper functions */
10669         for (pass = 0; pass < 2; pass++) {
10670             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10671             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10672             TCGv_i64 tcg_passres;
10673             int elt = pass + is_q * 2;
10674 
10675             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10676             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10677 
10678             if (accop == 0) {
10679                 tcg_passres = tcg_res[pass];
10680             } else {
10681                 tcg_passres = tcg_temp_new_i64();
10682             }
10683 
10684             switch (opcode) {
10685             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10686             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10687             {
10688                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10689                 static NeonGenWidenFn * const widenfns[2][2] = {
10690                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10691                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10692                 };
10693                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10694 
10695                 widenfn(tcg_op2_64, tcg_op2);
10696                 widenfn(tcg_passres, tcg_op1);
10697                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10698                               tcg_passres, tcg_op2_64);
10699                 break;
10700             }
10701             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10702             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10703                 if (size == 0) {
10704                     if (is_u) {
10705                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10706                     } else {
10707                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10708                     }
10709                 } else {
10710                     if (is_u) {
10711                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10712                     } else {
10713                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10714                     }
10715                 }
10716                 break;
10717             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10718             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10719             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10720                 if (size == 0) {
10721                     if (is_u) {
10722                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10723                     } else {
10724                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10725                     }
10726                 } else {
10727                     if (is_u) {
10728                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10729                     } else {
10730                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10731                     }
10732                 }
10733                 break;
10734             case 9: /* SQDMLAL, SQDMLAL2 */
10735             case 11: /* SQDMLSL, SQDMLSL2 */
10736             case 13: /* SQDMULL, SQDMULL2 */
10737                 assert(size == 1);
10738                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10739                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10740                                                   tcg_passres, tcg_passres);
10741                 break;
10742             default:
10743                 g_assert_not_reached();
10744             }
10745 
10746             if (accop != 0) {
10747                 if (opcode == 9 || opcode == 11) {
10748                     /* saturating accumulate ops */
10749                     if (accop < 0) {
10750                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10751                     }
10752                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10753                                                       tcg_res[pass],
10754                                                       tcg_passres);
10755                 } else {
10756                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10757                                   tcg_res[pass], tcg_passres);
10758                 }
10759             }
10760         }
10761     }
10762 
10763     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10764     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10765 }
10766 
10767 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10768                             int opcode, int rd, int rn, int rm)
10769 {
10770     TCGv_i64 tcg_res[2];
10771     int part = is_q ? 2 : 0;
10772     int pass;
10773 
10774     for (pass = 0; pass < 2; pass++) {
10775         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10776         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10777         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10778         static NeonGenWidenFn * const widenfns[3][2] = {
10779             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10780             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10781             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10782         };
10783         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10784 
10785         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10786         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10787         widenfn(tcg_op2_wide, tcg_op2);
10788         tcg_res[pass] = tcg_temp_new_i64();
10789         gen_neon_addl(size, (opcode == 3),
10790                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10791     }
10792 
10793     for (pass = 0; pass < 2; pass++) {
10794         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10795     }
10796 }
10797 
10798 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10799 {
10800     tcg_gen_addi_i64(in, in, 1U << 31);
10801     tcg_gen_extrh_i64_i32(res, in);
10802 }
10803 
10804 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10805                                  int opcode, int rd, int rn, int rm)
10806 {
10807     TCGv_i32 tcg_res[2];
10808     int part = is_q ? 2 : 0;
10809     int pass;
10810 
10811     for (pass = 0; pass < 2; pass++) {
10812         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10813         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10814         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10815         static NeonGenNarrowFn * const narrowfns[3][2] = {
10816             { gen_helper_neon_narrow_high_u8,
10817               gen_helper_neon_narrow_round_high_u8 },
10818             { gen_helper_neon_narrow_high_u16,
10819               gen_helper_neon_narrow_round_high_u16 },
10820             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10821         };
10822         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10823 
10824         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10825         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10826 
10827         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10828 
10829         tcg_res[pass] = tcg_temp_new_i32();
10830         gennarrow(tcg_res[pass], tcg_wideres);
10831     }
10832 
10833     for (pass = 0; pass < 2; pass++) {
10834         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10835     }
10836     clear_vec_high(s, is_q, rd);
10837 }
10838 
10839 /* AdvSIMD three different
10840  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10841  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10842  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10843  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10844  */
10845 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10846 {
10847     /* Instructions in this group fall into three basic classes
10848      * (in each case with the operation working on each element in
10849      * the input vectors):
10850      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10851      *     128 bit input)
10852      * (2) wide 64 x 128 -> 128
10853      * (3) narrowing 128 x 128 -> 64
10854      * Here we do initial decode, catch unallocated cases and
10855      * dispatch to separate functions for each class.
10856      */
10857     int is_q = extract32(insn, 30, 1);
10858     int is_u = extract32(insn, 29, 1);
10859     int size = extract32(insn, 22, 2);
10860     int opcode = extract32(insn, 12, 4);
10861     int rm = extract32(insn, 16, 5);
10862     int rn = extract32(insn, 5, 5);
10863     int rd = extract32(insn, 0, 5);
10864 
10865     switch (opcode) {
10866     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10867     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10868         /* 64 x 128 -> 128 */
10869         if (size == 3) {
10870             unallocated_encoding(s);
10871             return;
10872         }
10873         if (!fp_access_check(s)) {
10874             return;
10875         }
10876         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10877         break;
10878     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10879     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10880         /* 128 x 128 -> 64 */
10881         if (size == 3) {
10882             unallocated_encoding(s);
10883             return;
10884         }
10885         if (!fp_access_check(s)) {
10886             return;
10887         }
10888         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10889         break;
10890     case 14: /* PMULL, PMULL2 */
10891         if (is_u) {
10892             unallocated_encoding(s);
10893             return;
10894         }
10895         switch (size) {
10896         case 0: /* PMULL.P8 */
10897             if (!fp_access_check(s)) {
10898                 return;
10899             }
10900             /* The Q field specifies lo/hi half input for this insn.  */
10901             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10902                              gen_helper_neon_pmull_h);
10903             break;
10904 
10905         case 3: /* PMULL.P64 */
10906             if (!dc_isar_feature(aa64_pmull, s)) {
10907                 unallocated_encoding(s);
10908                 return;
10909             }
10910             if (!fp_access_check(s)) {
10911                 return;
10912             }
10913             /* The Q field specifies lo/hi half input for this insn.  */
10914             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10915                              gen_helper_gvec_pmull_q);
10916             break;
10917 
10918         default:
10919             unallocated_encoding(s);
10920             break;
10921         }
10922         return;
10923     case 9: /* SQDMLAL, SQDMLAL2 */
10924     case 11: /* SQDMLSL, SQDMLSL2 */
10925     case 13: /* SQDMULL, SQDMULL2 */
10926         if (is_u || size == 0) {
10927             unallocated_encoding(s);
10928             return;
10929         }
10930         /* fall through */
10931     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10932     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10933     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10934     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10935     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10936     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10937     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10938         /* 64 x 64 -> 128 */
10939         if (size == 3) {
10940             unallocated_encoding(s);
10941             return;
10942         }
10943         if (!fp_access_check(s)) {
10944             return;
10945         }
10946 
10947         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10948         break;
10949     default:
10950         /* opcode 15 not allocated */
10951         unallocated_encoding(s);
10952         break;
10953     }
10954 }
10955 
10956 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10957 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10958 {
10959     int rd = extract32(insn, 0, 5);
10960     int rn = extract32(insn, 5, 5);
10961     int rm = extract32(insn, 16, 5);
10962     int size = extract32(insn, 22, 2);
10963     bool is_u = extract32(insn, 29, 1);
10964     bool is_q = extract32(insn, 30, 1);
10965 
10966     if (!fp_access_check(s)) {
10967         return;
10968     }
10969 
10970     switch (size + 4 * is_u) {
10971     case 0: /* AND */
10972         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10973         return;
10974     case 1: /* BIC */
10975         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10976         return;
10977     case 2: /* ORR */
10978         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10979         return;
10980     case 3: /* ORN */
10981         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10982         return;
10983     case 4: /* EOR */
10984         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10985         return;
10986 
10987     case 5: /* BSL bitwise select */
10988         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10989         return;
10990     case 6: /* BIT, bitwise insert if true */
10991         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10992         return;
10993     case 7: /* BIF, bitwise insert if false */
10994         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10995         return;
10996 
10997     default:
10998         g_assert_not_reached();
10999     }
11000 }
11001 
11002 /* Pairwise op subgroup of C3.6.16.
11003  *
11004  * This is called directly or via the handle_3same_float for float pairwise
11005  * operations where the opcode and size are calculated differently.
11006  */
11007 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11008                                    int size, int rn, int rm, int rd)
11009 {
11010     TCGv_ptr fpst;
11011     int pass;
11012 
11013     /* Floating point operations need fpst */
11014     if (opcode >= 0x58) {
11015         fpst = fpstatus_ptr(FPST_FPCR);
11016     } else {
11017         fpst = NULL;
11018     }
11019 
11020     if (!fp_access_check(s)) {
11021         return;
11022     }
11023 
11024     /* These operations work on the concatenated rm:rn, with each pair of
11025      * adjacent elements being operated on to produce an element in the result.
11026      */
11027     if (size == 3) {
11028         TCGv_i64 tcg_res[2];
11029 
11030         for (pass = 0; pass < 2; pass++) {
11031             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11032             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11033             int passreg = (pass == 0) ? rn : rm;
11034 
11035             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11036             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11037             tcg_res[pass] = tcg_temp_new_i64();
11038 
11039             switch (opcode) {
11040             case 0x17: /* ADDP */
11041                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11042                 break;
11043             case 0x58: /* FMAXNMP */
11044                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11045                 break;
11046             case 0x5a: /* FADDP */
11047                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11048                 break;
11049             case 0x5e: /* FMAXP */
11050                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11051                 break;
11052             case 0x78: /* FMINNMP */
11053                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11054                 break;
11055             case 0x7e: /* FMINP */
11056                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11057                 break;
11058             default:
11059                 g_assert_not_reached();
11060             }
11061         }
11062 
11063         for (pass = 0; pass < 2; pass++) {
11064             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11065         }
11066     } else {
11067         int maxpass = is_q ? 4 : 2;
11068         TCGv_i32 tcg_res[4];
11069 
11070         for (pass = 0; pass < maxpass; pass++) {
11071             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11072             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11073             NeonGenTwoOpFn *genfn = NULL;
11074             int passreg = pass < (maxpass / 2) ? rn : rm;
11075             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11076 
11077             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11078             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11079             tcg_res[pass] = tcg_temp_new_i32();
11080 
11081             switch (opcode) {
11082             case 0x17: /* ADDP */
11083             {
11084                 static NeonGenTwoOpFn * const fns[3] = {
11085                     gen_helper_neon_padd_u8,
11086                     gen_helper_neon_padd_u16,
11087                     tcg_gen_add_i32,
11088                 };
11089                 genfn = fns[size];
11090                 break;
11091             }
11092             case 0x14: /* SMAXP, UMAXP */
11093             {
11094                 static NeonGenTwoOpFn * const fns[3][2] = {
11095                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11096                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11097                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11098                 };
11099                 genfn = fns[size][u];
11100                 break;
11101             }
11102             case 0x15: /* SMINP, UMINP */
11103             {
11104                 static NeonGenTwoOpFn * const fns[3][2] = {
11105                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11106                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11107                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11108                 };
11109                 genfn = fns[size][u];
11110                 break;
11111             }
11112             /* The FP operations are all on single floats (32 bit) */
11113             case 0x58: /* FMAXNMP */
11114                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11115                 break;
11116             case 0x5a: /* FADDP */
11117                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11118                 break;
11119             case 0x5e: /* FMAXP */
11120                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11121                 break;
11122             case 0x78: /* FMINNMP */
11123                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11124                 break;
11125             case 0x7e: /* FMINP */
11126                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11127                 break;
11128             default:
11129                 g_assert_not_reached();
11130             }
11131 
11132             /* FP ops called directly, otherwise call now */
11133             if (genfn) {
11134                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11135             }
11136         }
11137 
11138         for (pass = 0; pass < maxpass; pass++) {
11139             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11140         }
11141         clear_vec_high(s, is_q, rd);
11142     }
11143 }
11144 
11145 /* Floating point op subgroup of C3.6.16. */
11146 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11147 {
11148     /* For floating point ops, the U, size[1] and opcode bits
11149      * together indicate the operation. size[0] indicates single
11150      * or double.
11151      */
11152     int fpopcode = extract32(insn, 11, 5)
11153         | (extract32(insn, 23, 1) << 5)
11154         | (extract32(insn, 29, 1) << 6);
11155     int is_q = extract32(insn, 30, 1);
11156     int size = extract32(insn, 22, 1);
11157     int rm = extract32(insn, 16, 5);
11158     int rn = extract32(insn, 5, 5);
11159     int rd = extract32(insn, 0, 5);
11160 
11161     int datasize = is_q ? 128 : 64;
11162     int esize = 32 << size;
11163     int elements = datasize / esize;
11164 
11165     if (size == 1 && !is_q) {
11166         unallocated_encoding(s);
11167         return;
11168     }
11169 
11170     switch (fpopcode) {
11171     case 0x58: /* FMAXNMP */
11172     case 0x5a: /* FADDP */
11173     case 0x5e: /* FMAXP */
11174     case 0x78: /* FMINNMP */
11175     case 0x7e: /* FMINP */
11176         if (size && !is_q) {
11177             unallocated_encoding(s);
11178             return;
11179         }
11180         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11181                                rn, rm, rd);
11182         return;
11183     case 0x1b: /* FMULX */
11184     case 0x1f: /* FRECPS */
11185     case 0x3f: /* FRSQRTS */
11186     case 0x5d: /* FACGE */
11187     case 0x7d: /* FACGT */
11188     case 0x19: /* FMLA */
11189     case 0x39: /* FMLS */
11190     case 0x18: /* FMAXNM */
11191     case 0x1a: /* FADD */
11192     case 0x1c: /* FCMEQ */
11193     case 0x1e: /* FMAX */
11194     case 0x38: /* FMINNM */
11195     case 0x3a: /* FSUB */
11196     case 0x3e: /* FMIN */
11197     case 0x5b: /* FMUL */
11198     case 0x5c: /* FCMGE */
11199     case 0x5f: /* FDIV */
11200     case 0x7a: /* FABD */
11201     case 0x7c: /* FCMGT */
11202         if (!fp_access_check(s)) {
11203             return;
11204         }
11205         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11206         return;
11207 
11208     case 0x1d: /* FMLAL  */
11209     case 0x3d: /* FMLSL  */
11210     case 0x59: /* FMLAL2 */
11211     case 0x79: /* FMLSL2 */
11212         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11213             unallocated_encoding(s);
11214             return;
11215         }
11216         if (fp_access_check(s)) {
11217             int is_s = extract32(insn, 23, 1);
11218             int is_2 = extract32(insn, 29, 1);
11219             int data = (is_2 << 1) | is_s;
11220             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11221                                vec_full_reg_offset(s, rn),
11222                                vec_full_reg_offset(s, rm), cpu_env,
11223                                is_q ? 16 : 8, vec_full_reg_size(s),
11224                                data, gen_helper_gvec_fmlal_a64);
11225         }
11226         return;
11227 
11228     default:
11229         unallocated_encoding(s);
11230         return;
11231     }
11232 }
11233 
11234 /* Integer op subgroup of C3.6.16. */
11235 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11236 {
11237     int is_q = extract32(insn, 30, 1);
11238     int u = extract32(insn, 29, 1);
11239     int size = extract32(insn, 22, 2);
11240     int opcode = extract32(insn, 11, 5);
11241     int rm = extract32(insn, 16, 5);
11242     int rn = extract32(insn, 5, 5);
11243     int rd = extract32(insn, 0, 5);
11244     int pass;
11245     TCGCond cond;
11246 
11247     switch (opcode) {
11248     case 0x13: /* MUL, PMUL */
11249         if (u && size != 0) {
11250             unallocated_encoding(s);
11251             return;
11252         }
11253         /* fall through */
11254     case 0x0: /* SHADD, UHADD */
11255     case 0x2: /* SRHADD, URHADD */
11256     case 0x4: /* SHSUB, UHSUB */
11257     case 0xc: /* SMAX, UMAX */
11258     case 0xd: /* SMIN, UMIN */
11259     case 0xe: /* SABD, UABD */
11260     case 0xf: /* SABA, UABA */
11261     case 0x12: /* MLA, MLS */
11262         if (size == 3) {
11263             unallocated_encoding(s);
11264             return;
11265         }
11266         break;
11267     case 0x16: /* SQDMULH, SQRDMULH */
11268         if (size == 0 || size == 3) {
11269             unallocated_encoding(s);
11270             return;
11271         }
11272         break;
11273     default:
11274         if (size == 3 && !is_q) {
11275             unallocated_encoding(s);
11276             return;
11277         }
11278         break;
11279     }
11280 
11281     if (!fp_access_check(s)) {
11282         return;
11283     }
11284 
11285     switch (opcode) {
11286     case 0x01: /* SQADD, UQADD */
11287         if (u) {
11288             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11289         } else {
11290             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11291         }
11292         return;
11293     case 0x05: /* SQSUB, UQSUB */
11294         if (u) {
11295             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11296         } else {
11297             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11298         }
11299         return;
11300     case 0x08: /* SSHL, USHL */
11301         if (u) {
11302             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11303         } else {
11304             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11305         }
11306         return;
11307     case 0x0c: /* SMAX, UMAX */
11308         if (u) {
11309             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11310         } else {
11311             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11312         }
11313         return;
11314     case 0x0d: /* SMIN, UMIN */
11315         if (u) {
11316             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11317         } else {
11318             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11319         }
11320         return;
11321     case 0xe: /* SABD, UABD */
11322         if (u) {
11323             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11324         } else {
11325             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11326         }
11327         return;
11328     case 0xf: /* SABA, UABA */
11329         if (u) {
11330             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11331         } else {
11332             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11333         }
11334         return;
11335     case 0x10: /* ADD, SUB */
11336         if (u) {
11337             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11338         } else {
11339             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11340         }
11341         return;
11342     case 0x13: /* MUL, PMUL */
11343         if (!u) { /* MUL */
11344             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11345         } else {  /* PMUL */
11346             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11347         }
11348         return;
11349     case 0x12: /* MLA, MLS */
11350         if (u) {
11351             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11352         } else {
11353             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11354         }
11355         return;
11356     case 0x16: /* SQDMULH, SQRDMULH */
11357         {
11358             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11359                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11360                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11361             };
11362             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11363         }
11364         return;
11365     case 0x11:
11366         if (!u) { /* CMTST */
11367             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11368             return;
11369         }
11370         /* else CMEQ */
11371         cond = TCG_COND_EQ;
11372         goto do_gvec_cmp;
11373     case 0x06: /* CMGT, CMHI */
11374         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11375         goto do_gvec_cmp;
11376     case 0x07: /* CMGE, CMHS */
11377         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11378     do_gvec_cmp:
11379         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11380                          vec_full_reg_offset(s, rn),
11381                          vec_full_reg_offset(s, rm),
11382                          is_q ? 16 : 8, vec_full_reg_size(s));
11383         return;
11384     }
11385 
11386     if (size == 3) {
11387         assert(is_q);
11388         for (pass = 0; pass < 2; pass++) {
11389             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11390             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11391             TCGv_i64 tcg_res = tcg_temp_new_i64();
11392 
11393             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11394             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11395 
11396             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11397 
11398             write_vec_element(s, tcg_res, rd, pass, MO_64);
11399         }
11400     } else {
11401         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11402             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11403             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11404             TCGv_i32 tcg_res = tcg_temp_new_i32();
11405             NeonGenTwoOpFn *genfn = NULL;
11406             NeonGenTwoOpEnvFn *genenvfn = NULL;
11407 
11408             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11409             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11410 
11411             switch (opcode) {
11412             case 0x0: /* SHADD, UHADD */
11413             {
11414                 static NeonGenTwoOpFn * const fns[3][2] = {
11415                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11416                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11417                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11418                 };
11419                 genfn = fns[size][u];
11420                 break;
11421             }
11422             case 0x2: /* SRHADD, URHADD */
11423             {
11424                 static NeonGenTwoOpFn * const fns[3][2] = {
11425                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11426                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11427                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11428                 };
11429                 genfn = fns[size][u];
11430                 break;
11431             }
11432             case 0x4: /* SHSUB, UHSUB */
11433             {
11434                 static NeonGenTwoOpFn * const fns[3][2] = {
11435                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11436                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11437                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11438                 };
11439                 genfn = fns[size][u];
11440                 break;
11441             }
11442             case 0x9: /* SQSHL, UQSHL */
11443             {
11444                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11445                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11446                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11447                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11448                 };
11449                 genenvfn = fns[size][u];
11450                 break;
11451             }
11452             case 0xa: /* SRSHL, URSHL */
11453             {
11454                 static NeonGenTwoOpFn * const fns[3][2] = {
11455                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11456                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11457                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11458                 };
11459                 genfn = fns[size][u];
11460                 break;
11461             }
11462             case 0xb: /* SQRSHL, UQRSHL */
11463             {
11464                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11465                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11466                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11467                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11468                 };
11469                 genenvfn = fns[size][u];
11470                 break;
11471             }
11472             default:
11473                 g_assert_not_reached();
11474             }
11475 
11476             if (genenvfn) {
11477                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11478             } else {
11479                 genfn(tcg_res, tcg_op1, tcg_op2);
11480             }
11481 
11482             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11483         }
11484     }
11485     clear_vec_high(s, is_q, rd);
11486 }
11487 
11488 /* AdvSIMD three same
11489  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11490  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11491  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11492  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11493  */
11494 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11495 {
11496     int opcode = extract32(insn, 11, 5);
11497 
11498     switch (opcode) {
11499     case 0x3: /* logic ops */
11500         disas_simd_3same_logic(s, insn);
11501         break;
11502     case 0x17: /* ADDP */
11503     case 0x14: /* SMAXP, UMAXP */
11504     case 0x15: /* SMINP, UMINP */
11505     {
11506         /* Pairwise operations */
11507         int is_q = extract32(insn, 30, 1);
11508         int u = extract32(insn, 29, 1);
11509         int size = extract32(insn, 22, 2);
11510         int rm = extract32(insn, 16, 5);
11511         int rn = extract32(insn, 5, 5);
11512         int rd = extract32(insn, 0, 5);
11513         if (opcode == 0x17) {
11514             if (u || (size == 3 && !is_q)) {
11515                 unallocated_encoding(s);
11516                 return;
11517             }
11518         } else {
11519             if (size == 3) {
11520                 unallocated_encoding(s);
11521                 return;
11522             }
11523         }
11524         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11525         break;
11526     }
11527     case 0x18 ... 0x31:
11528         /* floating point ops, sz[1] and U are part of opcode */
11529         disas_simd_3same_float(s, insn);
11530         break;
11531     default:
11532         disas_simd_3same_int(s, insn);
11533         break;
11534     }
11535 }
11536 
11537 /*
11538  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11539  *
11540  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11541  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11542  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11543  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11544  *
11545  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11546  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11547  *
11548  */
11549 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11550 {
11551     int opcode = extract32(insn, 11, 3);
11552     int u = extract32(insn, 29, 1);
11553     int a = extract32(insn, 23, 1);
11554     int is_q = extract32(insn, 30, 1);
11555     int rm = extract32(insn, 16, 5);
11556     int rn = extract32(insn, 5, 5);
11557     int rd = extract32(insn, 0, 5);
11558     /*
11559      * For these floating point ops, the U, a and opcode bits
11560      * together indicate the operation.
11561      */
11562     int fpopcode = opcode | (a << 3) | (u << 4);
11563     int datasize = is_q ? 128 : 64;
11564     int elements = datasize / 16;
11565     bool pairwise;
11566     TCGv_ptr fpst;
11567     int pass;
11568 
11569     switch (fpopcode) {
11570     case 0x0: /* FMAXNM */
11571     case 0x1: /* FMLA */
11572     case 0x2: /* FADD */
11573     case 0x3: /* FMULX */
11574     case 0x4: /* FCMEQ */
11575     case 0x6: /* FMAX */
11576     case 0x7: /* FRECPS */
11577     case 0x8: /* FMINNM */
11578     case 0x9: /* FMLS */
11579     case 0xa: /* FSUB */
11580     case 0xe: /* FMIN */
11581     case 0xf: /* FRSQRTS */
11582     case 0x13: /* FMUL */
11583     case 0x14: /* FCMGE */
11584     case 0x15: /* FACGE */
11585     case 0x17: /* FDIV */
11586     case 0x1a: /* FABD */
11587     case 0x1c: /* FCMGT */
11588     case 0x1d: /* FACGT */
11589         pairwise = false;
11590         break;
11591     case 0x10: /* FMAXNMP */
11592     case 0x12: /* FADDP */
11593     case 0x16: /* FMAXP */
11594     case 0x18: /* FMINNMP */
11595     case 0x1e: /* FMINP */
11596         pairwise = true;
11597         break;
11598     default:
11599         unallocated_encoding(s);
11600         return;
11601     }
11602 
11603     if (!dc_isar_feature(aa64_fp16, s)) {
11604         unallocated_encoding(s);
11605         return;
11606     }
11607 
11608     if (!fp_access_check(s)) {
11609         return;
11610     }
11611 
11612     fpst = fpstatus_ptr(FPST_FPCR_F16);
11613 
11614     if (pairwise) {
11615         int maxpass = is_q ? 8 : 4;
11616         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11617         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11618         TCGv_i32 tcg_res[8];
11619 
11620         for (pass = 0; pass < maxpass; pass++) {
11621             int passreg = pass < (maxpass / 2) ? rn : rm;
11622             int passelt = (pass << 1) & (maxpass - 1);
11623 
11624             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11625             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11626             tcg_res[pass] = tcg_temp_new_i32();
11627 
11628             switch (fpopcode) {
11629             case 0x10: /* FMAXNMP */
11630                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11631                                            fpst);
11632                 break;
11633             case 0x12: /* FADDP */
11634                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11635                 break;
11636             case 0x16: /* FMAXP */
11637                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11638                 break;
11639             case 0x18: /* FMINNMP */
11640                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11641                                            fpst);
11642                 break;
11643             case 0x1e: /* FMINP */
11644                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11645                 break;
11646             default:
11647                 g_assert_not_reached();
11648             }
11649         }
11650 
11651         for (pass = 0; pass < maxpass; pass++) {
11652             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11653         }
11654     } else {
11655         for (pass = 0; pass < elements; pass++) {
11656             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11657             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11658             TCGv_i32 tcg_res = tcg_temp_new_i32();
11659 
11660             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11661             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11662 
11663             switch (fpopcode) {
11664             case 0x0: /* FMAXNM */
11665                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11666                 break;
11667             case 0x1: /* FMLA */
11668                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11669                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11670                                            fpst);
11671                 break;
11672             case 0x2: /* FADD */
11673                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11674                 break;
11675             case 0x3: /* FMULX */
11676                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11677                 break;
11678             case 0x4: /* FCMEQ */
11679                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11680                 break;
11681             case 0x6: /* FMAX */
11682                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11683                 break;
11684             case 0x7: /* FRECPS */
11685                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11686                 break;
11687             case 0x8: /* FMINNM */
11688                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11689                 break;
11690             case 0x9: /* FMLS */
11691                 /* As usual for ARM, separate negation for fused multiply-add */
11692                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11693                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11694                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11695                                            fpst);
11696                 break;
11697             case 0xa: /* FSUB */
11698                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11699                 break;
11700             case 0xe: /* FMIN */
11701                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11702                 break;
11703             case 0xf: /* FRSQRTS */
11704                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11705                 break;
11706             case 0x13: /* FMUL */
11707                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11708                 break;
11709             case 0x14: /* FCMGE */
11710                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11711                 break;
11712             case 0x15: /* FACGE */
11713                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11714                 break;
11715             case 0x17: /* FDIV */
11716                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11717                 break;
11718             case 0x1a: /* FABD */
11719                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11720                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11721                 break;
11722             case 0x1c: /* FCMGT */
11723                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11724                 break;
11725             case 0x1d: /* FACGT */
11726                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11727                 break;
11728             default:
11729                 g_assert_not_reached();
11730             }
11731 
11732             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11733         }
11734     }
11735 
11736     clear_vec_high(s, is_q, rd);
11737 }
11738 
11739 /* AdvSIMD three same extra
11740  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11741  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11742  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11743  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11744  */
11745 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11746 {
11747     int rd = extract32(insn, 0, 5);
11748     int rn = extract32(insn, 5, 5);
11749     int opcode = extract32(insn, 11, 4);
11750     int rm = extract32(insn, 16, 5);
11751     int size = extract32(insn, 22, 2);
11752     bool u = extract32(insn, 29, 1);
11753     bool is_q = extract32(insn, 30, 1);
11754     bool feature;
11755     int rot;
11756 
11757     switch (u * 16 + opcode) {
11758     case 0x10: /* SQRDMLAH (vector) */
11759     case 0x11: /* SQRDMLSH (vector) */
11760         if (size != 1 && size != 2) {
11761             unallocated_encoding(s);
11762             return;
11763         }
11764         feature = dc_isar_feature(aa64_rdm, s);
11765         break;
11766     case 0x02: /* SDOT (vector) */
11767     case 0x12: /* UDOT (vector) */
11768         if (size != MO_32) {
11769             unallocated_encoding(s);
11770             return;
11771         }
11772         feature = dc_isar_feature(aa64_dp, s);
11773         break;
11774     case 0x03: /* USDOT */
11775         if (size != MO_32) {
11776             unallocated_encoding(s);
11777             return;
11778         }
11779         feature = dc_isar_feature(aa64_i8mm, s);
11780         break;
11781     case 0x04: /* SMMLA */
11782     case 0x14: /* UMMLA */
11783     case 0x05: /* USMMLA */
11784         if (!is_q || size != MO_32) {
11785             unallocated_encoding(s);
11786             return;
11787         }
11788         feature = dc_isar_feature(aa64_i8mm, s);
11789         break;
11790     case 0x18: /* FCMLA, #0 */
11791     case 0x19: /* FCMLA, #90 */
11792     case 0x1a: /* FCMLA, #180 */
11793     case 0x1b: /* FCMLA, #270 */
11794     case 0x1c: /* FCADD, #90 */
11795     case 0x1e: /* FCADD, #270 */
11796         if (size == 0
11797             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11798             || (size == 3 && !is_q)) {
11799             unallocated_encoding(s);
11800             return;
11801         }
11802         feature = dc_isar_feature(aa64_fcma, s);
11803         break;
11804     case 0x1d: /* BFMMLA */
11805         if (size != MO_16 || !is_q) {
11806             unallocated_encoding(s);
11807             return;
11808         }
11809         feature = dc_isar_feature(aa64_bf16, s);
11810         break;
11811     case 0x1f:
11812         switch (size) {
11813         case 1: /* BFDOT */
11814         case 3: /* BFMLAL{B,T} */
11815             feature = dc_isar_feature(aa64_bf16, s);
11816             break;
11817         default:
11818             unallocated_encoding(s);
11819             return;
11820         }
11821         break;
11822     default:
11823         unallocated_encoding(s);
11824         return;
11825     }
11826     if (!feature) {
11827         unallocated_encoding(s);
11828         return;
11829     }
11830     if (!fp_access_check(s)) {
11831         return;
11832     }
11833 
11834     switch (opcode) {
11835     case 0x0: /* SQRDMLAH (vector) */
11836         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11837         return;
11838 
11839     case 0x1: /* SQRDMLSH (vector) */
11840         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11841         return;
11842 
11843     case 0x2: /* SDOT / UDOT */
11844         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11845                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11846         return;
11847 
11848     case 0x3: /* USDOT */
11849         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11850         return;
11851 
11852     case 0x04: /* SMMLA, UMMLA */
11853         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11854                          u ? gen_helper_gvec_ummla_b
11855                          : gen_helper_gvec_smmla_b);
11856         return;
11857     case 0x05: /* USMMLA */
11858         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11859         return;
11860 
11861     case 0x8: /* FCMLA, #0 */
11862     case 0x9: /* FCMLA, #90 */
11863     case 0xa: /* FCMLA, #180 */
11864     case 0xb: /* FCMLA, #270 */
11865         rot = extract32(opcode, 0, 2);
11866         switch (size) {
11867         case 1:
11868             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11869                               gen_helper_gvec_fcmlah);
11870             break;
11871         case 2:
11872             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11873                               gen_helper_gvec_fcmlas);
11874             break;
11875         case 3:
11876             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11877                               gen_helper_gvec_fcmlad);
11878             break;
11879         default:
11880             g_assert_not_reached();
11881         }
11882         return;
11883 
11884     case 0xc: /* FCADD, #90 */
11885     case 0xe: /* FCADD, #270 */
11886         rot = extract32(opcode, 1, 1);
11887         switch (size) {
11888         case 1:
11889             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11890                               gen_helper_gvec_fcaddh);
11891             break;
11892         case 2:
11893             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11894                               gen_helper_gvec_fcadds);
11895             break;
11896         case 3:
11897             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11898                               gen_helper_gvec_fcaddd);
11899             break;
11900         default:
11901             g_assert_not_reached();
11902         }
11903         return;
11904 
11905     case 0xd: /* BFMMLA */
11906         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11907         return;
11908     case 0xf:
11909         switch (size) {
11910         case 1: /* BFDOT */
11911             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11912             break;
11913         case 3: /* BFMLAL{B,T} */
11914             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11915                               gen_helper_gvec_bfmlal);
11916             break;
11917         default:
11918             g_assert_not_reached();
11919         }
11920         return;
11921 
11922     default:
11923         g_assert_not_reached();
11924     }
11925 }
11926 
11927 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11928                                   int size, int rn, int rd)
11929 {
11930     /* Handle 2-reg-misc ops which are widening (so each size element
11931      * in the source becomes a 2*size element in the destination.
11932      * The only instruction like this is FCVTL.
11933      */
11934     int pass;
11935 
11936     if (size == 3) {
11937         /* 32 -> 64 bit fp conversion */
11938         TCGv_i64 tcg_res[2];
11939         int srcelt = is_q ? 2 : 0;
11940 
11941         for (pass = 0; pass < 2; pass++) {
11942             TCGv_i32 tcg_op = tcg_temp_new_i32();
11943             tcg_res[pass] = tcg_temp_new_i64();
11944 
11945             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11946             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11947         }
11948         for (pass = 0; pass < 2; pass++) {
11949             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11950         }
11951     } else {
11952         /* 16 -> 32 bit fp conversion */
11953         int srcelt = is_q ? 4 : 0;
11954         TCGv_i32 tcg_res[4];
11955         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11956         TCGv_i32 ahp = get_ahp_flag();
11957 
11958         for (pass = 0; pass < 4; pass++) {
11959             tcg_res[pass] = tcg_temp_new_i32();
11960 
11961             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11962             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11963                                            fpst, ahp);
11964         }
11965         for (pass = 0; pass < 4; pass++) {
11966             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11967         }
11968     }
11969 }
11970 
11971 static void handle_rev(DisasContext *s, int opcode, bool u,
11972                        bool is_q, int size, int rn, int rd)
11973 {
11974     int op = (opcode << 1) | u;
11975     int opsz = op + size;
11976     int grp_size = 3 - opsz;
11977     int dsize = is_q ? 128 : 64;
11978     int i;
11979 
11980     if (opsz >= 3) {
11981         unallocated_encoding(s);
11982         return;
11983     }
11984 
11985     if (!fp_access_check(s)) {
11986         return;
11987     }
11988 
11989     if (size == 0) {
11990         /* Special case bytes, use bswap op on each group of elements */
11991         int groups = dsize / (8 << grp_size);
11992 
11993         for (i = 0; i < groups; i++) {
11994             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11995 
11996             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11997             switch (grp_size) {
11998             case MO_16:
11999                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12000                 break;
12001             case MO_32:
12002                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12003                 break;
12004             case MO_64:
12005                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12006                 break;
12007             default:
12008                 g_assert_not_reached();
12009             }
12010             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12011         }
12012         clear_vec_high(s, is_q, rd);
12013     } else {
12014         int revmask = (1 << grp_size) - 1;
12015         int esize = 8 << size;
12016         int elements = dsize / esize;
12017         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12018         TCGv_i64 tcg_rd[2];
12019 
12020         for (i = 0; i < 2; i++) {
12021             tcg_rd[i] = tcg_temp_new_i64();
12022             tcg_gen_movi_i64(tcg_rd[i], 0);
12023         }
12024 
12025         for (i = 0; i < elements; i++) {
12026             int e_rev = (i & 0xf) ^ revmask;
12027             int w = (e_rev * esize) / 64;
12028             int o = (e_rev * esize) % 64;
12029 
12030             read_vec_element(s, tcg_rn, rn, i, size);
12031             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12032         }
12033 
12034         for (i = 0; i < 2; i++) {
12035             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12036         }
12037         clear_vec_high(s, true, rd);
12038     }
12039 }
12040 
12041 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12042                                   bool is_q, int size, int rn, int rd)
12043 {
12044     /* Implement the pairwise operations from 2-misc:
12045      * SADDLP, UADDLP, SADALP, UADALP.
12046      * These all add pairs of elements in the input to produce a
12047      * double-width result element in the output (possibly accumulating).
12048      */
12049     bool accum = (opcode == 0x6);
12050     int maxpass = is_q ? 2 : 1;
12051     int pass;
12052     TCGv_i64 tcg_res[2];
12053 
12054     if (size == 2) {
12055         /* 32 + 32 -> 64 op */
12056         MemOp memop = size + (u ? 0 : MO_SIGN);
12057 
12058         for (pass = 0; pass < maxpass; pass++) {
12059             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12060             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12061 
12062             tcg_res[pass] = tcg_temp_new_i64();
12063 
12064             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12065             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12066             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12067             if (accum) {
12068                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12069                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12070             }
12071         }
12072     } else {
12073         for (pass = 0; pass < maxpass; pass++) {
12074             TCGv_i64 tcg_op = tcg_temp_new_i64();
12075             NeonGenOne64OpFn *genfn;
12076             static NeonGenOne64OpFn * const fns[2][2] = {
12077                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12078                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12079             };
12080 
12081             genfn = fns[size][u];
12082 
12083             tcg_res[pass] = tcg_temp_new_i64();
12084 
12085             read_vec_element(s, tcg_op, rn, pass, MO_64);
12086             genfn(tcg_res[pass], tcg_op);
12087 
12088             if (accum) {
12089                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12090                 if (size == 0) {
12091                     gen_helper_neon_addl_u16(tcg_res[pass],
12092                                              tcg_res[pass], tcg_op);
12093                 } else {
12094                     gen_helper_neon_addl_u32(tcg_res[pass],
12095                                              tcg_res[pass], tcg_op);
12096                 }
12097             }
12098         }
12099     }
12100     if (!is_q) {
12101         tcg_res[1] = tcg_constant_i64(0);
12102     }
12103     for (pass = 0; pass < 2; pass++) {
12104         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12105     }
12106 }
12107 
12108 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12109 {
12110     /* Implement SHLL and SHLL2 */
12111     int pass;
12112     int part = is_q ? 2 : 0;
12113     TCGv_i64 tcg_res[2];
12114 
12115     for (pass = 0; pass < 2; pass++) {
12116         static NeonGenWidenFn * const widenfns[3] = {
12117             gen_helper_neon_widen_u8,
12118             gen_helper_neon_widen_u16,
12119             tcg_gen_extu_i32_i64,
12120         };
12121         NeonGenWidenFn *widenfn = widenfns[size];
12122         TCGv_i32 tcg_op = tcg_temp_new_i32();
12123 
12124         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12125         tcg_res[pass] = tcg_temp_new_i64();
12126         widenfn(tcg_res[pass], tcg_op);
12127         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12128     }
12129 
12130     for (pass = 0; pass < 2; pass++) {
12131         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12132     }
12133 }
12134 
12135 /* AdvSIMD two reg misc
12136  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12137  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12138  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12139  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12140  */
12141 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12142 {
12143     int size = extract32(insn, 22, 2);
12144     int opcode = extract32(insn, 12, 5);
12145     bool u = extract32(insn, 29, 1);
12146     bool is_q = extract32(insn, 30, 1);
12147     int rn = extract32(insn, 5, 5);
12148     int rd = extract32(insn, 0, 5);
12149     bool need_fpstatus = false;
12150     int rmode = -1;
12151     TCGv_i32 tcg_rmode;
12152     TCGv_ptr tcg_fpstatus;
12153 
12154     switch (opcode) {
12155     case 0x0: /* REV64, REV32 */
12156     case 0x1: /* REV16 */
12157         handle_rev(s, opcode, u, is_q, size, rn, rd);
12158         return;
12159     case 0x5: /* CNT, NOT, RBIT */
12160         if (u && size == 0) {
12161             /* NOT */
12162             break;
12163         } else if (u && size == 1) {
12164             /* RBIT */
12165             break;
12166         } else if (!u && size == 0) {
12167             /* CNT */
12168             break;
12169         }
12170         unallocated_encoding(s);
12171         return;
12172     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12173     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12174         if (size == 3) {
12175             unallocated_encoding(s);
12176             return;
12177         }
12178         if (!fp_access_check(s)) {
12179             return;
12180         }
12181 
12182         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12183         return;
12184     case 0x4: /* CLS, CLZ */
12185         if (size == 3) {
12186             unallocated_encoding(s);
12187             return;
12188         }
12189         break;
12190     case 0x2: /* SADDLP, UADDLP */
12191     case 0x6: /* SADALP, UADALP */
12192         if (size == 3) {
12193             unallocated_encoding(s);
12194             return;
12195         }
12196         if (!fp_access_check(s)) {
12197             return;
12198         }
12199         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12200         return;
12201     case 0x13: /* SHLL, SHLL2 */
12202         if (u == 0 || size == 3) {
12203             unallocated_encoding(s);
12204             return;
12205         }
12206         if (!fp_access_check(s)) {
12207             return;
12208         }
12209         handle_shll(s, is_q, size, rn, rd);
12210         return;
12211     case 0xa: /* CMLT */
12212         if (u == 1) {
12213             unallocated_encoding(s);
12214             return;
12215         }
12216         /* fall through */
12217     case 0x8: /* CMGT, CMGE */
12218     case 0x9: /* CMEQ, CMLE */
12219     case 0xb: /* ABS, NEG */
12220         if (size == 3 && !is_q) {
12221             unallocated_encoding(s);
12222             return;
12223         }
12224         break;
12225     case 0x3: /* SUQADD, USQADD */
12226         if (size == 3 && !is_q) {
12227             unallocated_encoding(s);
12228             return;
12229         }
12230         if (!fp_access_check(s)) {
12231             return;
12232         }
12233         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12234         return;
12235     case 0x7: /* SQABS, SQNEG */
12236         if (size == 3 && !is_q) {
12237             unallocated_encoding(s);
12238             return;
12239         }
12240         break;
12241     case 0xc ... 0xf:
12242     case 0x16 ... 0x1f:
12243     {
12244         /* Floating point: U, size[1] and opcode indicate operation;
12245          * size[0] indicates single or double precision.
12246          */
12247         int is_double = extract32(size, 0, 1);
12248         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12249         size = is_double ? 3 : 2;
12250         switch (opcode) {
12251         case 0x2f: /* FABS */
12252         case 0x6f: /* FNEG */
12253             if (size == 3 && !is_q) {
12254                 unallocated_encoding(s);
12255                 return;
12256             }
12257             break;
12258         case 0x1d: /* SCVTF */
12259         case 0x5d: /* UCVTF */
12260         {
12261             bool is_signed = (opcode == 0x1d) ? true : false;
12262             int elements = is_double ? 2 : is_q ? 4 : 2;
12263             if (is_double && !is_q) {
12264                 unallocated_encoding(s);
12265                 return;
12266             }
12267             if (!fp_access_check(s)) {
12268                 return;
12269             }
12270             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12271             return;
12272         }
12273         case 0x2c: /* FCMGT (zero) */
12274         case 0x2d: /* FCMEQ (zero) */
12275         case 0x2e: /* FCMLT (zero) */
12276         case 0x6c: /* FCMGE (zero) */
12277         case 0x6d: /* FCMLE (zero) */
12278             if (size == 3 && !is_q) {
12279                 unallocated_encoding(s);
12280                 return;
12281             }
12282             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12283             return;
12284         case 0x7f: /* FSQRT */
12285             if (size == 3 && !is_q) {
12286                 unallocated_encoding(s);
12287                 return;
12288             }
12289             break;
12290         case 0x1a: /* FCVTNS */
12291         case 0x1b: /* FCVTMS */
12292         case 0x3a: /* FCVTPS */
12293         case 0x3b: /* FCVTZS */
12294         case 0x5a: /* FCVTNU */
12295         case 0x5b: /* FCVTMU */
12296         case 0x7a: /* FCVTPU */
12297         case 0x7b: /* FCVTZU */
12298             need_fpstatus = true;
12299             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12300             if (size == 3 && !is_q) {
12301                 unallocated_encoding(s);
12302                 return;
12303             }
12304             break;
12305         case 0x5c: /* FCVTAU */
12306         case 0x1c: /* FCVTAS */
12307             need_fpstatus = true;
12308             rmode = FPROUNDING_TIEAWAY;
12309             if (size == 3 && !is_q) {
12310                 unallocated_encoding(s);
12311                 return;
12312             }
12313             break;
12314         case 0x3c: /* URECPE */
12315             if (size == 3) {
12316                 unallocated_encoding(s);
12317                 return;
12318             }
12319             /* fall through */
12320         case 0x3d: /* FRECPE */
12321         case 0x7d: /* FRSQRTE */
12322             if (size == 3 && !is_q) {
12323                 unallocated_encoding(s);
12324                 return;
12325             }
12326             if (!fp_access_check(s)) {
12327                 return;
12328             }
12329             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12330             return;
12331         case 0x56: /* FCVTXN, FCVTXN2 */
12332             if (size == 2) {
12333                 unallocated_encoding(s);
12334                 return;
12335             }
12336             /* fall through */
12337         case 0x16: /* FCVTN, FCVTN2 */
12338             /* handle_2misc_narrow does a 2*size -> size operation, but these
12339              * instructions encode the source size rather than dest size.
12340              */
12341             if (!fp_access_check(s)) {
12342                 return;
12343             }
12344             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12345             return;
12346         case 0x36: /* BFCVTN, BFCVTN2 */
12347             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12348                 unallocated_encoding(s);
12349                 return;
12350             }
12351             if (!fp_access_check(s)) {
12352                 return;
12353             }
12354             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12355             return;
12356         case 0x17: /* FCVTL, FCVTL2 */
12357             if (!fp_access_check(s)) {
12358                 return;
12359             }
12360             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12361             return;
12362         case 0x18: /* FRINTN */
12363         case 0x19: /* FRINTM */
12364         case 0x38: /* FRINTP */
12365         case 0x39: /* FRINTZ */
12366             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12367             /* fall through */
12368         case 0x59: /* FRINTX */
12369         case 0x79: /* FRINTI */
12370             need_fpstatus = true;
12371             if (size == 3 && !is_q) {
12372                 unallocated_encoding(s);
12373                 return;
12374             }
12375             break;
12376         case 0x58: /* FRINTA */
12377             rmode = FPROUNDING_TIEAWAY;
12378             need_fpstatus = true;
12379             if (size == 3 && !is_q) {
12380                 unallocated_encoding(s);
12381                 return;
12382             }
12383             break;
12384         case 0x7c: /* URSQRTE */
12385             if (size == 3) {
12386                 unallocated_encoding(s);
12387                 return;
12388             }
12389             break;
12390         case 0x1e: /* FRINT32Z */
12391         case 0x1f: /* FRINT64Z */
12392             rmode = FPROUNDING_ZERO;
12393             /* fall through */
12394         case 0x5e: /* FRINT32X */
12395         case 0x5f: /* FRINT64X */
12396             need_fpstatus = true;
12397             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12398                 unallocated_encoding(s);
12399                 return;
12400             }
12401             break;
12402         default:
12403             unallocated_encoding(s);
12404             return;
12405         }
12406         break;
12407     }
12408     default:
12409         unallocated_encoding(s);
12410         return;
12411     }
12412 
12413     if (!fp_access_check(s)) {
12414         return;
12415     }
12416 
12417     if (need_fpstatus || rmode >= 0) {
12418         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12419     } else {
12420         tcg_fpstatus = NULL;
12421     }
12422     if (rmode >= 0) {
12423         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12424     } else {
12425         tcg_rmode = NULL;
12426     }
12427 
12428     switch (opcode) {
12429     case 0x5:
12430         if (u && size == 0) { /* NOT */
12431             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12432             return;
12433         }
12434         break;
12435     case 0x8: /* CMGT, CMGE */
12436         if (u) {
12437             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12438         } else {
12439             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12440         }
12441         return;
12442     case 0x9: /* CMEQ, CMLE */
12443         if (u) {
12444             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12445         } else {
12446             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12447         }
12448         return;
12449     case 0xa: /* CMLT */
12450         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12451         return;
12452     case 0xb:
12453         if (u) { /* ABS, NEG */
12454             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12455         } else {
12456             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12457         }
12458         return;
12459     }
12460 
12461     if (size == 3) {
12462         /* All 64-bit element operations can be shared with scalar 2misc */
12463         int pass;
12464 
12465         /* Coverity claims (size == 3 && !is_q) has been eliminated
12466          * from all paths leading to here.
12467          */
12468         tcg_debug_assert(is_q);
12469         for (pass = 0; pass < 2; pass++) {
12470             TCGv_i64 tcg_op = tcg_temp_new_i64();
12471             TCGv_i64 tcg_res = tcg_temp_new_i64();
12472 
12473             read_vec_element(s, tcg_op, rn, pass, MO_64);
12474 
12475             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12476                             tcg_rmode, tcg_fpstatus);
12477 
12478             write_vec_element(s, tcg_res, rd, pass, MO_64);
12479         }
12480     } else {
12481         int pass;
12482 
12483         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12484             TCGv_i32 tcg_op = tcg_temp_new_i32();
12485             TCGv_i32 tcg_res = tcg_temp_new_i32();
12486 
12487             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12488 
12489             if (size == 2) {
12490                 /* Special cases for 32 bit elements */
12491                 switch (opcode) {
12492                 case 0x4: /* CLS */
12493                     if (u) {
12494                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12495                     } else {
12496                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12497                     }
12498                     break;
12499                 case 0x7: /* SQABS, SQNEG */
12500                     if (u) {
12501                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12502                     } else {
12503                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12504                     }
12505                     break;
12506                 case 0x2f: /* FABS */
12507                     gen_helper_vfp_abss(tcg_res, tcg_op);
12508                     break;
12509                 case 0x6f: /* FNEG */
12510                     gen_helper_vfp_negs(tcg_res, tcg_op);
12511                     break;
12512                 case 0x7f: /* FSQRT */
12513                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12514                     break;
12515                 case 0x1a: /* FCVTNS */
12516                 case 0x1b: /* FCVTMS */
12517                 case 0x1c: /* FCVTAS */
12518                 case 0x3a: /* FCVTPS */
12519                 case 0x3b: /* FCVTZS */
12520                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12521                                          tcg_constant_i32(0), tcg_fpstatus);
12522                     break;
12523                 case 0x5a: /* FCVTNU */
12524                 case 0x5b: /* FCVTMU */
12525                 case 0x5c: /* FCVTAU */
12526                 case 0x7a: /* FCVTPU */
12527                 case 0x7b: /* FCVTZU */
12528                     gen_helper_vfp_touls(tcg_res, tcg_op,
12529                                          tcg_constant_i32(0), tcg_fpstatus);
12530                     break;
12531                 case 0x18: /* FRINTN */
12532                 case 0x19: /* FRINTM */
12533                 case 0x38: /* FRINTP */
12534                 case 0x39: /* FRINTZ */
12535                 case 0x58: /* FRINTA */
12536                 case 0x79: /* FRINTI */
12537                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12538                     break;
12539                 case 0x59: /* FRINTX */
12540                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12541                     break;
12542                 case 0x7c: /* URSQRTE */
12543                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12544                     break;
12545                 case 0x1e: /* FRINT32Z */
12546                 case 0x5e: /* FRINT32X */
12547                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12548                     break;
12549                 case 0x1f: /* FRINT64Z */
12550                 case 0x5f: /* FRINT64X */
12551                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12552                     break;
12553                 default:
12554                     g_assert_not_reached();
12555                 }
12556             } else {
12557                 /* Use helpers for 8 and 16 bit elements */
12558                 switch (opcode) {
12559                 case 0x5: /* CNT, RBIT */
12560                     /* For these two insns size is part of the opcode specifier
12561                      * (handled earlier); they always operate on byte elements.
12562                      */
12563                     if (u) {
12564                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12565                     } else {
12566                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12567                     }
12568                     break;
12569                 case 0x7: /* SQABS, SQNEG */
12570                 {
12571                     NeonGenOneOpEnvFn *genfn;
12572                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12573                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12574                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12575                     };
12576                     genfn = fns[size][u];
12577                     genfn(tcg_res, cpu_env, tcg_op);
12578                     break;
12579                 }
12580                 case 0x4: /* CLS, CLZ */
12581                     if (u) {
12582                         if (size == 0) {
12583                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12584                         } else {
12585                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12586                         }
12587                     } else {
12588                         if (size == 0) {
12589                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12590                         } else {
12591                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12592                         }
12593                     }
12594                     break;
12595                 default:
12596                     g_assert_not_reached();
12597                 }
12598             }
12599 
12600             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12601         }
12602     }
12603     clear_vec_high(s, is_q, rd);
12604 
12605     if (tcg_rmode) {
12606         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12607     }
12608 }
12609 
12610 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12611  *
12612  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12613  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12614  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12615  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12616  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12617  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12618  *
12619  * This actually covers two groups where scalar access is governed by
12620  * bit 28. A bunch of the instructions (float to integral) only exist
12621  * in the vector form and are un-allocated for the scalar decode. Also
12622  * in the scalar decode Q is always 1.
12623  */
12624 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12625 {
12626     int fpop, opcode, a, u;
12627     int rn, rd;
12628     bool is_q;
12629     bool is_scalar;
12630     bool only_in_vector = false;
12631 
12632     int pass;
12633     TCGv_i32 tcg_rmode = NULL;
12634     TCGv_ptr tcg_fpstatus = NULL;
12635     bool need_fpst = true;
12636     int rmode = -1;
12637 
12638     if (!dc_isar_feature(aa64_fp16, s)) {
12639         unallocated_encoding(s);
12640         return;
12641     }
12642 
12643     rd = extract32(insn, 0, 5);
12644     rn = extract32(insn, 5, 5);
12645 
12646     a = extract32(insn, 23, 1);
12647     u = extract32(insn, 29, 1);
12648     is_scalar = extract32(insn, 28, 1);
12649     is_q = extract32(insn, 30, 1);
12650 
12651     opcode = extract32(insn, 12, 5);
12652     fpop = deposit32(opcode, 5, 1, a);
12653     fpop = deposit32(fpop, 6, 1, u);
12654 
12655     switch (fpop) {
12656     case 0x1d: /* SCVTF */
12657     case 0x5d: /* UCVTF */
12658     {
12659         int elements;
12660 
12661         if (is_scalar) {
12662             elements = 1;
12663         } else {
12664             elements = (is_q ? 8 : 4);
12665         }
12666 
12667         if (!fp_access_check(s)) {
12668             return;
12669         }
12670         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12671         return;
12672     }
12673     break;
12674     case 0x2c: /* FCMGT (zero) */
12675     case 0x2d: /* FCMEQ (zero) */
12676     case 0x2e: /* FCMLT (zero) */
12677     case 0x6c: /* FCMGE (zero) */
12678     case 0x6d: /* FCMLE (zero) */
12679         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12680         return;
12681     case 0x3d: /* FRECPE */
12682     case 0x3f: /* FRECPX */
12683         break;
12684     case 0x18: /* FRINTN */
12685         only_in_vector = true;
12686         rmode = FPROUNDING_TIEEVEN;
12687         break;
12688     case 0x19: /* FRINTM */
12689         only_in_vector = true;
12690         rmode = FPROUNDING_NEGINF;
12691         break;
12692     case 0x38: /* FRINTP */
12693         only_in_vector = true;
12694         rmode = FPROUNDING_POSINF;
12695         break;
12696     case 0x39: /* FRINTZ */
12697         only_in_vector = true;
12698         rmode = FPROUNDING_ZERO;
12699         break;
12700     case 0x58: /* FRINTA */
12701         only_in_vector = true;
12702         rmode = FPROUNDING_TIEAWAY;
12703         break;
12704     case 0x59: /* FRINTX */
12705     case 0x79: /* FRINTI */
12706         only_in_vector = true;
12707         /* current rounding mode */
12708         break;
12709     case 0x1a: /* FCVTNS */
12710         rmode = FPROUNDING_TIEEVEN;
12711         break;
12712     case 0x1b: /* FCVTMS */
12713         rmode = FPROUNDING_NEGINF;
12714         break;
12715     case 0x1c: /* FCVTAS */
12716         rmode = FPROUNDING_TIEAWAY;
12717         break;
12718     case 0x3a: /* FCVTPS */
12719         rmode = FPROUNDING_POSINF;
12720         break;
12721     case 0x3b: /* FCVTZS */
12722         rmode = FPROUNDING_ZERO;
12723         break;
12724     case 0x5a: /* FCVTNU */
12725         rmode = FPROUNDING_TIEEVEN;
12726         break;
12727     case 0x5b: /* FCVTMU */
12728         rmode = FPROUNDING_NEGINF;
12729         break;
12730     case 0x5c: /* FCVTAU */
12731         rmode = FPROUNDING_TIEAWAY;
12732         break;
12733     case 0x7a: /* FCVTPU */
12734         rmode = FPROUNDING_POSINF;
12735         break;
12736     case 0x7b: /* FCVTZU */
12737         rmode = FPROUNDING_ZERO;
12738         break;
12739     case 0x2f: /* FABS */
12740     case 0x6f: /* FNEG */
12741         need_fpst = false;
12742         break;
12743     case 0x7d: /* FRSQRTE */
12744     case 0x7f: /* FSQRT (vector) */
12745         break;
12746     default:
12747         unallocated_encoding(s);
12748         return;
12749     }
12750 
12751 
12752     /* Check additional constraints for the scalar encoding */
12753     if (is_scalar) {
12754         if (!is_q) {
12755             unallocated_encoding(s);
12756             return;
12757         }
12758         /* FRINTxx is only in the vector form */
12759         if (only_in_vector) {
12760             unallocated_encoding(s);
12761             return;
12762         }
12763     }
12764 
12765     if (!fp_access_check(s)) {
12766         return;
12767     }
12768 
12769     if (rmode >= 0 || need_fpst) {
12770         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12771     }
12772 
12773     if (rmode >= 0) {
12774         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12775     }
12776 
12777     if (is_scalar) {
12778         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12779         TCGv_i32 tcg_res = tcg_temp_new_i32();
12780 
12781         switch (fpop) {
12782         case 0x1a: /* FCVTNS */
12783         case 0x1b: /* FCVTMS */
12784         case 0x1c: /* FCVTAS */
12785         case 0x3a: /* FCVTPS */
12786         case 0x3b: /* FCVTZS */
12787             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12788             break;
12789         case 0x3d: /* FRECPE */
12790             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12791             break;
12792         case 0x3f: /* FRECPX */
12793             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12794             break;
12795         case 0x5a: /* FCVTNU */
12796         case 0x5b: /* FCVTMU */
12797         case 0x5c: /* FCVTAU */
12798         case 0x7a: /* FCVTPU */
12799         case 0x7b: /* FCVTZU */
12800             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12801             break;
12802         case 0x6f: /* FNEG */
12803             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12804             break;
12805         case 0x7d: /* FRSQRTE */
12806             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12807             break;
12808         default:
12809             g_assert_not_reached();
12810         }
12811 
12812         /* limit any sign extension going on */
12813         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12814         write_fp_sreg(s, rd, tcg_res);
12815     } else {
12816         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12817             TCGv_i32 tcg_op = tcg_temp_new_i32();
12818             TCGv_i32 tcg_res = tcg_temp_new_i32();
12819 
12820             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12821 
12822             switch (fpop) {
12823             case 0x1a: /* FCVTNS */
12824             case 0x1b: /* FCVTMS */
12825             case 0x1c: /* FCVTAS */
12826             case 0x3a: /* FCVTPS */
12827             case 0x3b: /* FCVTZS */
12828                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12829                 break;
12830             case 0x3d: /* FRECPE */
12831                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12832                 break;
12833             case 0x5a: /* FCVTNU */
12834             case 0x5b: /* FCVTMU */
12835             case 0x5c: /* FCVTAU */
12836             case 0x7a: /* FCVTPU */
12837             case 0x7b: /* FCVTZU */
12838                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12839                 break;
12840             case 0x18: /* FRINTN */
12841             case 0x19: /* FRINTM */
12842             case 0x38: /* FRINTP */
12843             case 0x39: /* FRINTZ */
12844             case 0x58: /* FRINTA */
12845             case 0x79: /* FRINTI */
12846                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12847                 break;
12848             case 0x59: /* FRINTX */
12849                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12850                 break;
12851             case 0x2f: /* FABS */
12852                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12853                 break;
12854             case 0x6f: /* FNEG */
12855                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12856                 break;
12857             case 0x7d: /* FRSQRTE */
12858                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12859                 break;
12860             case 0x7f: /* FSQRT */
12861                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12862                 break;
12863             default:
12864                 g_assert_not_reached();
12865             }
12866 
12867             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12868         }
12869 
12870         clear_vec_high(s, is_q, rd);
12871     }
12872 
12873     if (tcg_rmode) {
12874         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12875     }
12876 }
12877 
12878 /* AdvSIMD scalar x indexed element
12879  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12880  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12881  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12882  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12883  * AdvSIMD vector 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 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12887  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12888  */
12889 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12890 {
12891     /* This encoding has two kinds of instruction:
12892      *  normal, where we perform elt x idxelt => elt for each
12893      *     element in the vector
12894      *  long, where we perform elt x idxelt and generate a result of
12895      *     double the width of the input element
12896      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12897      */
12898     bool is_scalar = extract32(insn, 28, 1);
12899     bool is_q = extract32(insn, 30, 1);
12900     bool u = extract32(insn, 29, 1);
12901     int size = extract32(insn, 22, 2);
12902     int l = extract32(insn, 21, 1);
12903     int m = extract32(insn, 20, 1);
12904     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12905     int rm = extract32(insn, 16, 4);
12906     int opcode = extract32(insn, 12, 4);
12907     int h = extract32(insn, 11, 1);
12908     int rn = extract32(insn, 5, 5);
12909     int rd = extract32(insn, 0, 5);
12910     bool is_long = false;
12911     int is_fp = 0;
12912     bool is_fp16 = false;
12913     int index;
12914     TCGv_ptr fpst;
12915 
12916     switch (16 * u + opcode) {
12917     case 0x08: /* MUL */
12918     case 0x10: /* MLA */
12919     case 0x14: /* MLS */
12920         if (is_scalar) {
12921             unallocated_encoding(s);
12922             return;
12923         }
12924         break;
12925     case 0x02: /* SMLAL, SMLAL2 */
12926     case 0x12: /* UMLAL, UMLAL2 */
12927     case 0x06: /* SMLSL, SMLSL2 */
12928     case 0x16: /* UMLSL, UMLSL2 */
12929     case 0x0a: /* SMULL, SMULL2 */
12930     case 0x1a: /* UMULL, UMULL2 */
12931         if (is_scalar) {
12932             unallocated_encoding(s);
12933             return;
12934         }
12935         is_long = true;
12936         break;
12937     case 0x03: /* SQDMLAL, SQDMLAL2 */
12938     case 0x07: /* SQDMLSL, SQDMLSL2 */
12939     case 0x0b: /* SQDMULL, SQDMULL2 */
12940         is_long = true;
12941         break;
12942     case 0x0c: /* SQDMULH */
12943     case 0x0d: /* SQRDMULH */
12944         break;
12945     case 0x01: /* FMLA */
12946     case 0x05: /* FMLS */
12947     case 0x09: /* FMUL */
12948     case 0x19: /* FMULX */
12949         is_fp = 1;
12950         break;
12951     case 0x1d: /* SQRDMLAH */
12952     case 0x1f: /* SQRDMLSH */
12953         if (!dc_isar_feature(aa64_rdm, s)) {
12954             unallocated_encoding(s);
12955             return;
12956         }
12957         break;
12958     case 0x0e: /* SDOT */
12959     case 0x1e: /* UDOT */
12960         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12961             unallocated_encoding(s);
12962             return;
12963         }
12964         break;
12965     case 0x0f:
12966         switch (size) {
12967         case 0: /* SUDOT */
12968         case 2: /* USDOT */
12969             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12970                 unallocated_encoding(s);
12971                 return;
12972             }
12973             size = MO_32;
12974             break;
12975         case 1: /* BFDOT */
12976             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12977                 unallocated_encoding(s);
12978                 return;
12979             }
12980             size = MO_32;
12981             break;
12982         case 3: /* BFMLAL{B,T} */
12983             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12984                 unallocated_encoding(s);
12985                 return;
12986             }
12987             /* can't set is_fp without other incorrect size checks */
12988             size = MO_16;
12989             break;
12990         default:
12991             unallocated_encoding(s);
12992             return;
12993         }
12994         break;
12995     case 0x11: /* FCMLA #0 */
12996     case 0x13: /* FCMLA #90 */
12997     case 0x15: /* FCMLA #180 */
12998     case 0x17: /* FCMLA #270 */
12999         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13000             unallocated_encoding(s);
13001             return;
13002         }
13003         is_fp = 2;
13004         break;
13005     case 0x00: /* FMLAL */
13006     case 0x04: /* FMLSL */
13007     case 0x18: /* FMLAL2 */
13008     case 0x1c: /* FMLSL2 */
13009         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13010             unallocated_encoding(s);
13011             return;
13012         }
13013         size = MO_16;
13014         /* is_fp, but we pass cpu_env not fp_status.  */
13015         break;
13016     default:
13017         unallocated_encoding(s);
13018         return;
13019     }
13020 
13021     switch (is_fp) {
13022     case 1: /* normal fp */
13023         /* convert insn encoded size to MemOp size */
13024         switch (size) {
13025         case 0: /* half-precision */
13026             size = MO_16;
13027             is_fp16 = true;
13028             break;
13029         case MO_32: /* single precision */
13030         case MO_64: /* double precision */
13031             break;
13032         default:
13033             unallocated_encoding(s);
13034             return;
13035         }
13036         break;
13037 
13038     case 2: /* complex fp */
13039         /* Each indexable element is a complex pair.  */
13040         size += 1;
13041         switch (size) {
13042         case MO_32:
13043             if (h && !is_q) {
13044                 unallocated_encoding(s);
13045                 return;
13046             }
13047             is_fp16 = true;
13048             break;
13049         case MO_64:
13050             break;
13051         default:
13052             unallocated_encoding(s);
13053             return;
13054         }
13055         break;
13056 
13057     default: /* integer */
13058         switch (size) {
13059         case MO_8:
13060         case MO_64:
13061             unallocated_encoding(s);
13062             return;
13063         }
13064         break;
13065     }
13066     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13067         unallocated_encoding(s);
13068         return;
13069     }
13070 
13071     /* Given MemOp size, adjust register and indexing.  */
13072     switch (size) {
13073     case MO_16:
13074         index = h << 2 | l << 1 | m;
13075         break;
13076     case MO_32:
13077         index = h << 1 | l;
13078         rm |= m << 4;
13079         break;
13080     case MO_64:
13081         if (l || !is_q) {
13082             unallocated_encoding(s);
13083             return;
13084         }
13085         index = h;
13086         rm |= m << 4;
13087         break;
13088     default:
13089         g_assert_not_reached();
13090     }
13091 
13092     if (!fp_access_check(s)) {
13093         return;
13094     }
13095 
13096     if (is_fp) {
13097         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13098     } else {
13099         fpst = NULL;
13100     }
13101 
13102     switch (16 * u + opcode) {
13103     case 0x0e: /* SDOT */
13104     case 0x1e: /* UDOT */
13105         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13106                          u ? gen_helper_gvec_udot_idx_b
13107                          : gen_helper_gvec_sdot_idx_b);
13108         return;
13109     case 0x0f:
13110         switch (extract32(insn, 22, 2)) {
13111         case 0: /* SUDOT */
13112             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13113                              gen_helper_gvec_sudot_idx_b);
13114             return;
13115         case 1: /* BFDOT */
13116             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13117                              gen_helper_gvec_bfdot_idx);
13118             return;
13119         case 2: /* USDOT */
13120             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13121                              gen_helper_gvec_usdot_idx_b);
13122             return;
13123         case 3: /* BFMLAL{B,T} */
13124             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13125                               gen_helper_gvec_bfmlal_idx);
13126             return;
13127         }
13128         g_assert_not_reached();
13129     case 0x11: /* FCMLA #0 */
13130     case 0x13: /* FCMLA #90 */
13131     case 0x15: /* FCMLA #180 */
13132     case 0x17: /* FCMLA #270 */
13133         {
13134             int rot = extract32(insn, 13, 2);
13135             int data = (index << 2) | rot;
13136             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13137                                vec_full_reg_offset(s, rn),
13138                                vec_full_reg_offset(s, rm),
13139                                vec_full_reg_offset(s, rd), fpst,
13140                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13141                                size == MO_64
13142                                ? gen_helper_gvec_fcmlas_idx
13143                                : gen_helper_gvec_fcmlah_idx);
13144         }
13145         return;
13146 
13147     case 0x00: /* FMLAL */
13148     case 0x04: /* FMLSL */
13149     case 0x18: /* FMLAL2 */
13150     case 0x1c: /* FMLSL2 */
13151         {
13152             int is_s = extract32(opcode, 2, 1);
13153             int is_2 = u;
13154             int data = (index << 2) | (is_2 << 1) | is_s;
13155             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13156                                vec_full_reg_offset(s, rn),
13157                                vec_full_reg_offset(s, rm), cpu_env,
13158                                is_q ? 16 : 8, vec_full_reg_size(s),
13159                                data, gen_helper_gvec_fmlal_idx_a64);
13160         }
13161         return;
13162 
13163     case 0x08: /* MUL */
13164         if (!is_long && !is_scalar) {
13165             static gen_helper_gvec_3 * const fns[3] = {
13166                 gen_helper_gvec_mul_idx_h,
13167                 gen_helper_gvec_mul_idx_s,
13168                 gen_helper_gvec_mul_idx_d,
13169             };
13170             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13171                                vec_full_reg_offset(s, rn),
13172                                vec_full_reg_offset(s, rm),
13173                                is_q ? 16 : 8, vec_full_reg_size(s),
13174                                index, fns[size - 1]);
13175             return;
13176         }
13177         break;
13178 
13179     case 0x10: /* MLA */
13180         if (!is_long && !is_scalar) {
13181             static gen_helper_gvec_4 * const fns[3] = {
13182                 gen_helper_gvec_mla_idx_h,
13183                 gen_helper_gvec_mla_idx_s,
13184                 gen_helper_gvec_mla_idx_d,
13185             };
13186             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13187                                vec_full_reg_offset(s, rn),
13188                                vec_full_reg_offset(s, rm),
13189                                vec_full_reg_offset(s, rd),
13190                                is_q ? 16 : 8, vec_full_reg_size(s),
13191                                index, fns[size - 1]);
13192             return;
13193         }
13194         break;
13195 
13196     case 0x14: /* MLS */
13197         if (!is_long && !is_scalar) {
13198             static gen_helper_gvec_4 * const fns[3] = {
13199                 gen_helper_gvec_mls_idx_h,
13200                 gen_helper_gvec_mls_idx_s,
13201                 gen_helper_gvec_mls_idx_d,
13202             };
13203             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13204                                vec_full_reg_offset(s, rn),
13205                                vec_full_reg_offset(s, rm),
13206                                vec_full_reg_offset(s, rd),
13207                                is_q ? 16 : 8, vec_full_reg_size(s),
13208                                index, fns[size - 1]);
13209             return;
13210         }
13211         break;
13212     }
13213 
13214     if (size == 3) {
13215         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13216         int pass;
13217 
13218         assert(is_fp && is_q && !is_long);
13219 
13220         read_vec_element(s, tcg_idx, rm, index, MO_64);
13221 
13222         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13223             TCGv_i64 tcg_op = tcg_temp_new_i64();
13224             TCGv_i64 tcg_res = tcg_temp_new_i64();
13225 
13226             read_vec_element(s, tcg_op, rn, pass, MO_64);
13227 
13228             switch (16 * u + opcode) {
13229             case 0x05: /* FMLS */
13230                 /* As usual for ARM, separate negation for fused multiply-add */
13231                 gen_helper_vfp_negd(tcg_op, tcg_op);
13232                 /* fall through */
13233             case 0x01: /* FMLA */
13234                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13235                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13236                 break;
13237             case 0x09: /* FMUL */
13238                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13239                 break;
13240             case 0x19: /* FMULX */
13241                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13242                 break;
13243             default:
13244                 g_assert_not_reached();
13245             }
13246 
13247             write_vec_element(s, tcg_res, rd, pass, MO_64);
13248         }
13249 
13250         clear_vec_high(s, !is_scalar, rd);
13251     } else if (!is_long) {
13252         /* 32 bit floating point, or 16 or 32 bit integer.
13253          * For the 16 bit scalar case we use the usual Neon helpers and
13254          * rely on the fact that 0 op 0 == 0 with no side effects.
13255          */
13256         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13257         int pass, maxpasses;
13258 
13259         if (is_scalar) {
13260             maxpasses = 1;
13261         } else {
13262             maxpasses = is_q ? 4 : 2;
13263         }
13264 
13265         read_vec_element_i32(s, tcg_idx, rm, index, size);
13266 
13267         if (size == 1 && !is_scalar) {
13268             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13269              * the index into both halves of the 32 bit tcg_idx and then use
13270              * the usual Neon helpers.
13271              */
13272             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13273         }
13274 
13275         for (pass = 0; pass < maxpasses; pass++) {
13276             TCGv_i32 tcg_op = tcg_temp_new_i32();
13277             TCGv_i32 tcg_res = tcg_temp_new_i32();
13278 
13279             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13280 
13281             switch (16 * u + opcode) {
13282             case 0x08: /* MUL */
13283             case 0x10: /* MLA */
13284             case 0x14: /* MLS */
13285             {
13286                 static NeonGenTwoOpFn * const fns[2][2] = {
13287                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13288                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13289                 };
13290                 NeonGenTwoOpFn *genfn;
13291                 bool is_sub = opcode == 0x4;
13292 
13293                 if (size == 1) {
13294                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13295                 } else {
13296                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13297                 }
13298                 if (opcode == 0x8) {
13299                     break;
13300                 }
13301                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13302                 genfn = fns[size - 1][is_sub];
13303                 genfn(tcg_res, tcg_op, tcg_res);
13304                 break;
13305             }
13306             case 0x05: /* FMLS */
13307             case 0x01: /* FMLA */
13308                 read_vec_element_i32(s, tcg_res, rd, pass,
13309                                      is_scalar ? size : MO_32);
13310                 switch (size) {
13311                 case 1:
13312                     if (opcode == 0x5) {
13313                         /* As usual for ARM, separate negation for fused
13314                          * multiply-add */
13315                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13316                     }
13317                     if (is_scalar) {
13318                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13319                                                    tcg_res, fpst);
13320                     } else {
13321                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13322                                                     tcg_res, fpst);
13323                     }
13324                     break;
13325                 case 2:
13326                     if (opcode == 0x5) {
13327                         /* As usual for ARM, separate negation for
13328                          * fused multiply-add */
13329                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13330                     }
13331                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13332                                            tcg_res, fpst);
13333                     break;
13334                 default:
13335                     g_assert_not_reached();
13336                 }
13337                 break;
13338             case 0x09: /* FMUL */
13339                 switch (size) {
13340                 case 1:
13341                     if (is_scalar) {
13342                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13343                                                 tcg_idx, fpst);
13344                     } else {
13345                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13346                                                  tcg_idx, fpst);
13347                     }
13348                     break;
13349                 case 2:
13350                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13351                     break;
13352                 default:
13353                     g_assert_not_reached();
13354                 }
13355                 break;
13356             case 0x19: /* FMULX */
13357                 switch (size) {
13358                 case 1:
13359                     if (is_scalar) {
13360                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13361                                                  tcg_idx, fpst);
13362                     } else {
13363                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13364                                                   tcg_idx, fpst);
13365                     }
13366                     break;
13367                 case 2:
13368                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13369                     break;
13370                 default:
13371                     g_assert_not_reached();
13372                 }
13373                 break;
13374             case 0x0c: /* SQDMULH */
13375                 if (size == 1) {
13376                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13377                                                tcg_op, tcg_idx);
13378                 } else {
13379                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13380                                                tcg_op, tcg_idx);
13381                 }
13382                 break;
13383             case 0x0d: /* SQRDMULH */
13384                 if (size == 1) {
13385                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13386                                                 tcg_op, tcg_idx);
13387                 } else {
13388                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13389                                                 tcg_op, tcg_idx);
13390                 }
13391                 break;
13392             case 0x1d: /* SQRDMLAH */
13393                 read_vec_element_i32(s, tcg_res, rd, pass,
13394                                      is_scalar ? size : MO_32);
13395                 if (size == 1) {
13396                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13397                                                 tcg_op, tcg_idx, tcg_res);
13398                 } else {
13399                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13400                                                 tcg_op, tcg_idx, tcg_res);
13401                 }
13402                 break;
13403             case 0x1f: /* SQRDMLSH */
13404                 read_vec_element_i32(s, tcg_res, rd, pass,
13405                                      is_scalar ? size : MO_32);
13406                 if (size == 1) {
13407                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13408                                                 tcg_op, tcg_idx, tcg_res);
13409                 } else {
13410                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13411                                                 tcg_op, tcg_idx, tcg_res);
13412                 }
13413                 break;
13414             default:
13415                 g_assert_not_reached();
13416             }
13417 
13418             if (is_scalar) {
13419                 write_fp_sreg(s, rd, tcg_res);
13420             } else {
13421                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13422             }
13423         }
13424 
13425         clear_vec_high(s, is_q, rd);
13426     } else {
13427         /* long ops: 16x16->32 or 32x32->64 */
13428         TCGv_i64 tcg_res[2];
13429         int pass;
13430         bool satop = extract32(opcode, 0, 1);
13431         MemOp memop = MO_32;
13432 
13433         if (satop || !u) {
13434             memop |= MO_SIGN;
13435         }
13436 
13437         if (size == 2) {
13438             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13439 
13440             read_vec_element(s, tcg_idx, rm, index, memop);
13441 
13442             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13443                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13444                 TCGv_i64 tcg_passres;
13445                 int passelt;
13446 
13447                 if (is_scalar) {
13448                     passelt = 0;
13449                 } else {
13450                     passelt = pass + (is_q * 2);
13451                 }
13452 
13453                 read_vec_element(s, tcg_op, rn, passelt, memop);
13454 
13455                 tcg_res[pass] = tcg_temp_new_i64();
13456 
13457                 if (opcode == 0xa || opcode == 0xb) {
13458                     /* Non-accumulating ops */
13459                     tcg_passres = tcg_res[pass];
13460                 } else {
13461                     tcg_passres = tcg_temp_new_i64();
13462                 }
13463 
13464                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13465 
13466                 if (satop) {
13467                     /* saturating, doubling */
13468                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13469                                                       tcg_passres, tcg_passres);
13470                 }
13471 
13472                 if (opcode == 0xa || opcode == 0xb) {
13473                     continue;
13474                 }
13475 
13476                 /* Accumulating op: handle accumulate step */
13477                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13478 
13479                 switch (opcode) {
13480                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13481                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13482                     break;
13483                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13484                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13485                     break;
13486                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13487                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13488                     /* fall through */
13489                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13490                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13491                                                       tcg_res[pass],
13492                                                       tcg_passres);
13493                     break;
13494                 default:
13495                     g_assert_not_reached();
13496                 }
13497             }
13498 
13499             clear_vec_high(s, !is_scalar, rd);
13500         } else {
13501             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13502 
13503             assert(size == 1);
13504             read_vec_element_i32(s, tcg_idx, rm, index, size);
13505 
13506             if (!is_scalar) {
13507                 /* The simplest way to handle the 16x16 indexed ops is to
13508                  * duplicate the index into both halves of the 32 bit tcg_idx
13509                  * and then use the usual Neon helpers.
13510                  */
13511                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13512             }
13513 
13514             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13515                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13516                 TCGv_i64 tcg_passres;
13517 
13518                 if (is_scalar) {
13519                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13520                 } else {
13521                     read_vec_element_i32(s, tcg_op, rn,
13522                                          pass + (is_q * 2), MO_32);
13523                 }
13524 
13525                 tcg_res[pass] = tcg_temp_new_i64();
13526 
13527                 if (opcode == 0xa || opcode == 0xb) {
13528                     /* Non-accumulating ops */
13529                     tcg_passres = tcg_res[pass];
13530                 } else {
13531                     tcg_passres = tcg_temp_new_i64();
13532                 }
13533 
13534                 if (memop & MO_SIGN) {
13535                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13536                 } else {
13537                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13538                 }
13539                 if (satop) {
13540                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13541                                                       tcg_passres, tcg_passres);
13542                 }
13543 
13544                 if (opcode == 0xa || opcode == 0xb) {
13545                     continue;
13546                 }
13547 
13548                 /* Accumulating op: handle accumulate step */
13549                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13550 
13551                 switch (opcode) {
13552                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13553                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13554                                              tcg_passres);
13555                     break;
13556                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13557                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13558                                              tcg_passres);
13559                     break;
13560                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13561                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13562                     /* fall through */
13563                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13564                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13565                                                       tcg_res[pass],
13566                                                       tcg_passres);
13567                     break;
13568                 default:
13569                     g_assert_not_reached();
13570                 }
13571             }
13572 
13573             if (is_scalar) {
13574                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13575             }
13576         }
13577 
13578         if (is_scalar) {
13579             tcg_res[1] = tcg_constant_i64(0);
13580         }
13581 
13582         for (pass = 0; pass < 2; pass++) {
13583             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13584         }
13585     }
13586 }
13587 
13588 /* Crypto AES
13589  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13590  * +-----------------+------+-----------+--------+-----+------+------+
13591  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13592  * +-----------------+------+-----------+--------+-----+------+------+
13593  */
13594 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13595 {
13596     int size = extract32(insn, 22, 2);
13597     int opcode = extract32(insn, 12, 5);
13598     int rn = extract32(insn, 5, 5);
13599     int rd = extract32(insn, 0, 5);
13600     int decrypt;
13601     gen_helper_gvec_2 *genfn2 = NULL;
13602     gen_helper_gvec_3 *genfn3 = NULL;
13603 
13604     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13605         unallocated_encoding(s);
13606         return;
13607     }
13608 
13609     switch (opcode) {
13610     case 0x4: /* AESE */
13611         decrypt = 0;
13612         genfn3 = gen_helper_crypto_aese;
13613         break;
13614     case 0x6: /* AESMC */
13615         decrypt = 0;
13616         genfn2 = gen_helper_crypto_aesmc;
13617         break;
13618     case 0x5: /* AESD */
13619         decrypt = 1;
13620         genfn3 = gen_helper_crypto_aese;
13621         break;
13622     case 0x7: /* AESIMC */
13623         decrypt = 1;
13624         genfn2 = gen_helper_crypto_aesmc;
13625         break;
13626     default:
13627         unallocated_encoding(s);
13628         return;
13629     }
13630 
13631     if (!fp_access_check(s)) {
13632         return;
13633     }
13634     if (genfn2) {
13635         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13636     } else {
13637         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13638     }
13639 }
13640 
13641 /* Crypto three-reg SHA
13642  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13643  * +-----------------+------+---+------+---+--------+-----+------+------+
13644  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13645  * +-----------------+------+---+------+---+--------+-----+------+------+
13646  */
13647 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13648 {
13649     int size = extract32(insn, 22, 2);
13650     int opcode = extract32(insn, 12, 3);
13651     int rm = extract32(insn, 16, 5);
13652     int rn = extract32(insn, 5, 5);
13653     int rd = extract32(insn, 0, 5);
13654     gen_helper_gvec_3 *genfn;
13655     bool feature;
13656 
13657     if (size != 0) {
13658         unallocated_encoding(s);
13659         return;
13660     }
13661 
13662     switch (opcode) {
13663     case 0: /* SHA1C */
13664         genfn = gen_helper_crypto_sha1c;
13665         feature = dc_isar_feature(aa64_sha1, s);
13666         break;
13667     case 1: /* SHA1P */
13668         genfn = gen_helper_crypto_sha1p;
13669         feature = dc_isar_feature(aa64_sha1, s);
13670         break;
13671     case 2: /* SHA1M */
13672         genfn = gen_helper_crypto_sha1m;
13673         feature = dc_isar_feature(aa64_sha1, s);
13674         break;
13675     case 3: /* SHA1SU0 */
13676         genfn = gen_helper_crypto_sha1su0;
13677         feature = dc_isar_feature(aa64_sha1, s);
13678         break;
13679     case 4: /* SHA256H */
13680         genfn = gen_helper_crypto_sha256h;
13681         feature = dc_isar_feature(aa64_sha256, s);
13682         break;
13683     case 5: /* SHA256H2 */
13684         genfn = gen_helper_crypto_sha256h2;
13685         feature = dc_isar_feature(aa64_sha256, s);
13686         break;
13687     case 6: /* SHA256SU1 */
13688         genfn = gen_helper_crypto_sha256su1;
13689         feature = dc_isar_feature(aa64_sha256, s);
13690         break;
13691     default:
13692         unallocated_encoding(s);
13693         return;
13694     }
13695 
13696     if (!feature) {
13697         unallocated_encoding(s);
13698         return;
13699     }
13700 
13701     if (!fp_access_check(s)) {
13702         return;
13703     }
13704     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13705 }
13706 
13707 /* Crypto two-reg SHA
13708  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13709  * +-----------------+------+-----------+--------+-----+------+------+
13710  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13711  * +-----------------+------+-----------+--------+-----+------+------+
13712  */
13713 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13714 {
13715     int size = extract32(insn, 22, 2);
13716     int opcode = extract32(insn, 12, 5);
13717     int rn = extract32(insn, 5, 5);
13718     int rd = extract32(insn, 0, 5);
13719     gen_helper_gvec_2 *genfn;
13720     bool feature;
13721 
13722     if (size != 0) {
13723         unallocated_encoding(s);
13724         return;
13725     }
13726 
13727     switch (opcode) {
13728     case 0: /* SHA1H */
13729         feature = dc_isar_feature(aa64_sha1, s);
13730         genfn = gen_helper_crypto_sha1h;
13731         break;
13732     case 1: /* SHA1SU1 */
13733         feature = dc_isar_feature(aa64_sha1, s);
13734         genfn = gen_helper_crypto_sha1su1;
13735         break;
13736     case 2: /* SHA256SU0 */
13737         feature = dc_isar_feature(aa64_sha256, s);
13738         genfn = gen_helper_crypto_sha256su0;
13739         break;
13740     default:
13741         unallocated_encoding(s);
13742         return;
13743     }
13744 
13745     if (!feature) {
13746         unallocated_encoding(s);
13747         return;
13748     }
13749 
13750     if (!fp_access_check(s)) {
13751         return;
13752     }
13753     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13754 }
13755 
13756 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13757 {
13758     tcg_gen_rotli_i64(d, m, 1);
13759     tcg_gen_xor_i64(d, d, n);
13760 }
13761 
13762 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13763 {
13764     tcg_gen_rotli_vec(vece, d, m, 1);
13765     tcg_gen_xor_vec(vece, d, d, n);
13766 }
13767 
13768 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13769                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13770 {
13771     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13772     static const GVecGen3 op = {
13773         .fni8 = gen_rax1_i64,
13774         .fniv = gen_rax1_vec,
13775         .opt_opc = vecop_list,
13776         .fno = gen_helper_crypto_rax1,
13777         .vece = MO_64,
13778     };
13779     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13780 }
13781 
13782 /* Crypto three-reg SHA512
13783  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13784  * +-----------------------+------+---+---+-----+--------+------+------+
13785  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13786  * +-----------------------+------+---+---+-----+--------+------+------+
13787  */
13788 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13789 {
13790     int opcode = extract32(insn, 10, 2);
13791     int o =  extract32(insn, 14, 1);
13792     int rm = extract32(insn, 16, 5);
13793     int rn = extract32(insn, 5, 5);
13794     int rd = extract32(insn, 0, 5);
13795     bool feature;
13796     gen_helper_gvec_3 *oolfn = NULL;
13797     GVecGen3Fn *gvecfn = NULL;
13798 
13799     if (o == 0) {
13800         switch (opcode) {
13801         case 0: /* SHA512H */
13802             feature = dc_isar_feature(aa64_sha512, s);
13803             oolfn = gen_helper_crypto_sha512h;
13804             break;
13805         case 1: /* SHA512H2 */
13806             feature = dc_isar_feature(aa64_sha512, s);
13807             oolfn = gen_helper_crypto_sha512h2;
13808             break;
13809         case 2: /* SHA512SU1 */
13810             feature = dc_isar_feature(aa64_sha512, s);
13811             oolfn = gen_helper_crypto_sha512su1;
13812             break;
13813         case 3: /* RAX1 */
13814             feature = dc_isar_feature(aa64_sha3, s);
13815             gvecfn = gen_gvec_rax1;
13816             break;
13817         default:
13818             g_assert_not_reached();
13819         }
13820     } else {
13821         switch (opcode) {
13822         case 0: /* SM3PARTW1 */
13823             feature = dc_isar_feature(aa64_sm3, s);
13824             oolfn = gen_helper_crypto_sm3partw1;
13825             break;
13826         case 1: /* SM3PARTW2 */
13827             feature = dc_isar_feature(aa64_sm3, s);
13828             oolfn = gen_helper_crypto_sm3partw2;
13829             break;
13830         case 2: /* SM4EKEY */
13831             feature = dc_isar_feature(aa64_sm4, s);
13832             oolfn = gen_helper_crypto_sm4ekey;
13833             break;
13834         default:
13835             unallocated_encoding(s);
13836             return;
13837         }
13838     }
13839 
13840     if (!feature) {
13841         unallocated_encoding(s);
13842         return;
13843     }
13844 
13845     if (!fp_access_check(s)) {
13846         return;
13847     }
13848 
13849     if (oolfn) {
13850         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13851     } else {
13852         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13853     }
13854 }
13855 
13856 /* Crypto two-reg SHA512
13857  *  31                                     12  11  10  9    5 4    0
13858  * +-----------------------------------------+--------+------+------+
13859  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13860  * +-----------------------------------------+--------+------+------+
13861  */
13862 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13863 {
13864     int opcode = extract32(insn, 10, 2);
13865     int rn = extract32(insn, 5, 5);
13866     int rd = extract32(insn, 0, 5);
13867     bool feature;
13868 
13869     switch (opcode) {
13870     case 0: /* SHA512SU0 */
13871         feature = dc_isar_feature(aa64_sha512, s);
13872         break;
13873     case 1: /* SM4E */
13874         feature = dc_isar_feature(aa64_sm4, s);
13875         break;
13876     default:
13877         unallocated_encoding(s);
13878         return;
13879     }
13880 
13881     if (!feature) {
13882         unallocated_encoding(s);
13883         return;
13884     }
13885 
13886     if (!fp_access_check(s)) {
13887         return;
13888     }
13889 
13890     switch (opcode) {
13891     case 0: /* SHA512SU0 */
13892         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13893         break;
13894     case 1: /* SM4E */
13895         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13896         break;
13897     default:
13898         g_assert_not_reached();
13899     }
13900 }
13901 
13902 /* Crypto four-register
13903  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13904  * +-------------------+-----+------+---+------+------+------+
13905  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13906  * +-------------------+-----+------+---+------+------+------+
13907  */
13908 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13909 {
13910     int op0 = extract32(insn, 21, 2);
13911     int rm = extract32(insn, 16, 5);
13912     int ra = extract32(insn, 10, 5);
13913     int rn = extract32(insn, 5, 5);
13914     int rd = extract32(insn, 0, 5);
13915     bool feature;
13916 
13917     switch (op0) {
13918     case 0: /* EOR3 */
13919     case 1: /* BCAX */
13920         feature = dc_isar_feature(aa64_sha3, s);
13921         break;
13922     case 2: /* SM3SS1 */
13923         feature = dc_isar_feature(aa64_sm3, s);
13924         break;
13925     default:
13926         unallocated_encoding(s);
13927         return;
13928     }
13929 
13930     if (!feature) {
13931         unallocated_encoding(s);
13932         return;
13933     }
13934 
13935     if (!fp_access_check(s)) {
13936         return;
13937     }
13938 
13939     if (op0 < 2) {
13940         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13941         int pass;
13942 
13943         tcg_op1 = tcg_temp_new_i64();
13944         tcg_op2 = tcg_temp_new_i64();
13945         tcg_op3 = tcg_temp_new_i64();
13946         tcg_res[0] = tcg_temp_new_i64();
13947         tcg_res[1] = tcg_temp_new_i64();
13948 
13949         for (pass = 0; pass < 2; pass++) {
13950             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13951             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13952             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13953 
13954             if (op0 == 0) {
13955                 /* EOR3 */
13956                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13957             } else {
13958                 /* BCAX */
13959                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13960             }
13961             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13962         }
13963         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13964         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13965     } else {
13966         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13967 
13968         tcg_op1 = tcg_temp_new_i32();
13969         tcg_op2 = tcg_temp_new_i32();
13970         tcg_op3 = tcg_temp_new_i32();
13971         tcg_res = tcg_temp_new_i32();
13972         tcg_zero = tcg_constant_i32(0);
13973 
13974         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13975         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13976         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13977 
13978         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13979         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13980         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13981         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13982 
13983         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13984         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13985         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13986         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13987     }
13988 }
13989 
13990 /* Crypto XAR
13991  *  31                   21 20  16 15    10 9    5 4    0
13992  * +-----------------------+------+--------+------+------+
13993  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13994  * +-----------------------+------+--------+------+------+
13995  */
13996 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13997 {
13998     int rm = extract32(insn, 16, 5);
13999     int imm6 = extract32(insn, 10, 6);
14000     int rn = extract32(insn, 5, 5);
14001     int rd = extract32(insn, 0, 5);
14002 
14003     if (!dc_isar_feature(aa64_sha3, s)) {
14004         unallocated_encoding(s);
14005         return;
14006     }
14007 
14008     if (!fp_access_check(s)) {
14009         return;
14010     }
14011 
14012     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14013                  vec_full_reg_offset(s, rn),
14014                  vec_full_reg_offset(s, rm), imm6, 16,
14015                  vec_full_reg_size(s));
14016 }
14017 
14018 /* Crypto three-reg imm2
14019  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14020  * +-----------------------+------+-----+------+--------+------+------+
14021  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14022  * +-----------------------+------+-----+------+--------+------+------+
14023  */
14024 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14025 {
14026     static gen_helper_gvec_3 * const fns[4] = {
14027         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14028         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14029     };
14030     int opcode = extract32(insn, 10, 2);
14031     int imm2 = extract32(insn, 12, 2);
14032     int rm = extract32(insn, 16, 5);
14033     int rn = extract32(insn, 5, 5);
14034     int rd = extract32(insn, 0, 5);
14035 
14036     if (!dc_isar_feature(aa64_sm3, s)) {
14037         unallocated_encoding(s);
14038         return;
14039     }
14040 
14041     if (!fp_access_check(s)) {
14042         return;
14043     }
14044 
14045     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14046 }
14047 
14048 /* C3.6 Data processing - SIMD, inc Crypto
14049  *
14050  * As the decode gets a little complex we are using a table based
14051  * approach for this part of the decode.
14052  */
14053 static const AArch64DecodeTable data_proc_simd[] = {
14054     /* pattern  ,  mask     ,  fn                        */
14055     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14056     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14057     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14058     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14059     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14060     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14061     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14062     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14063     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14064     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14065     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14066     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14067     { 0x2e000000, 0xbf208400, disas_simd_ext },
14068     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14069     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14070     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14071     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14072     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14073     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14074     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14075     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14076     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14077     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14078     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14079     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14080     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14081     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14082     { 0xce800000, 0xffe00000, disas_crypto_xar },
14083     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14084     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14085     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14086     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14087     { 0x00000000, 0x00000000, NULL }
14088 };
14089 
14090 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14091 {
14092     /* Note that this is called with all non-FP cases from
14093      * table C3-6 so it must UNDEF for entries not specifically
14094      * allocated to instructions in that table.
14095      */
14096     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14097     if (fn) {
14098         fn(s, insn);
14099     } else {
14100         unallocated_encoding(s);
14101     }
14102 }
14103 
14104 /* C3.6 Data processing - SIMD and floating point */
14105 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14106 {
14107     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14108         disas_data_proc_fp(s, insn);
14109     } else {
14110         /* SIMD, including crypto */
14111         disas_data_proc_simd(s, insn);
14112     }
14113 }
14114 
14115 static bool trans_OK(DisasContext *s, arg_OK *a)
14116 {
14117     return true;
14118 }
14119 
14120 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14121 {
14122     s->is_nonstreaming = true;
14123     return true;
14124 }
14125 
14126 /**
14127  * is_guarded_page:
14128  * @env: The cpu environment
14129  * @s: The DisasContext
14130  *
14131  * Return true if the page is guarded.
14132  */
14133 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14134 {
14135     uint64_t addr = s->base.pc_first;
14136 #ifdef CONFIG_USER_ONLY
14137     return page_get_flags(addr) & PAGE_BTI;
14138 #else
14139     CPUTLBEntryFull *full;
14140     void *host;
14141     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14142     int flags;
14143 
14144     /*
14145      * We test this immediately after reading an insn, which means
14146      * that the TLB entry must be present and valid, and thus this
14147      * access will never raise an exception.
14148      */
14149     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14150                               false, &host, &full, 0);
14151     assert(!(flags & TLB_INVALID_MASK));
14152 
14153     return full->guarded;
14154 #endif
14155 }
14156 
14157 /**
14158  * btype_destination_ok:
14159  * @insn: The instruction at the branch destination
14160  * @bt: SCTLR_ELx.BT
14161  * @btype: PSTATE.BTYPE, and is non-zero
14162  *
14163  * On a guarded page, there are a limited number of insns
14164  * that may be present at the branch target:
14165  *   - branch target identifiers,
14166  *   - paciasp, pacibsp,
14167  *   - BRK insn
14168  *   - HLT insn
14169  * Anything else causes a Branch Target Exception.
14170  *
14171  * Return true if the branch is compatible, false to raise BTITRAP.
14172  */
14173 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14174 {
14175     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14176         /* HINT space */
14177         switch (extract32(insn, 5, 7)) {
14178         case 0b011001: /* PACIASP */
14179         case 0b011011: /* PACIBSP */
14180             /*
14181              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14182              * with btype == 3.  Otherwise all btype are ok.
14183              */
14184             return !bt || btype != 3;
14185         case 0b100000: /* BTI */
14186             /* Not compatible with any btype.  */
14187             return false;
14188         case 0b100010: /* BTI c */
14189             /* Not compatible with btype == 3 */
14190             return btype != 3;
14191         case 0b100100: /* BTI j */
14192             /* Not compatible with btype == 2 */
14193             return btype != 2;
14194         case 0b100110: /* BTI jc */
14195             /* Compatible with any btype.  */
14196             return true;
14197         }
14198     } else {
14199         switch (insn & 0xffe0001fu) {
14200         case 0xd4200000u: /* BRK */
14201         case 0xd4400000u: /* HLT */
14202             /* Give priority to the breakpoint exception.  */
14203             return true;
14204         }
14205     }
14206     return false;
14207 }
14208 
14209 /* C3.1 A64 instruction index by encoding */
14210 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14211 {
14212     switch (extract32(insn, 25, 4)) {
14213     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14214         disas_b_exc_sys(s, insn);
14215         break;
14216     case 0x4:
14217     case 0x6:
14218     case 0xc:
14219     case 0xe:      /* Loads and stores */
14220         disas_ldst(s, insn);
14221         break;
14222     case 0x5:
14223     case 0xd:      /* Data processing - register */
14224         disas_data_proc_reg(s, insn);
14225         break;
14226     case 0x7:
14227     case 0xf:      /* Data processing - SIMD and floating point */
14228         disas_data_proc_simd_fp(s, insn);
14229         break;
14230     default:
14231         unallocated_encoding(s);
14232         break;
14233     }
14234 }
14235 
14236 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14237                                           CPUState *cpu)
14238 {
14239     DisasContext *dc = container_of(dcbase, DisasContext, base);
14240     CPUARMState *env = cpu->env_ptr;
14241     ARMCPU *arm_cpu = env_archcpu(env);
14242     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14243     int bound, core_mmu_idx;
14244 
14245     dc->isar = &arm_cpu->isar;
14246     dc->condjmp = 0;
14247     dc->pc_save = dc->base.pc_first;
14248     dc->aarch64 = true;
14249     dc->thumb = false;
14250     dc->sctlr_b = 0;
14251     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14252     dc->condexec_mask = 0;
14253     dc->condexec_cond = 0;
14254     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14255     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14256     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14257     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14258     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14259     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14260 #if !defined(CONFIG_USER_ONLY)
14261     dc->user = (dc->current_el == 0);
14262 #endif
14263     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14264     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14265     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14266     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14267     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14268     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14269     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14270     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14271     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14272     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14273     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14274     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14275     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14276     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14277     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14278     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14279     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14280     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14281     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14282     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14283     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14284     dc->vec_len = 0;
14285     dc->vec_stride = 0;
14286     dc->cp_regs = arm_cpu->cp_regs;
14287     dc->features = env->features;
14288     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14289 
14290 #ifdef CONFIG_USER_ONLY
14291     /* In sve_probe_page, we assume TBI is enabled. */
14292     tcg_debug_assert(dc->tbid & 1);
14293 #endif
14294 
14295     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14296 
14297     /* Single step state. The code-generation logic here is:
14298      *  SS_ACTIVE == 0:
14299      *   generate code with no special handling for single-stepping (except
14300      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14301      *   this happens anyway because those changes are all system register or
14302      *   PSTATE writes).
14303      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14304      *   emit code for one insn
14305      *   emit code to clear PSTATE.SS
14306      *   emit code to generate software step exception for completed step
14307      *   end TB (as usual for having generated an exception)
14308      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14309      *   emit code to generate a software step exception
14310      *   end the TB
14311      */
14312     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14313     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14314     dc->is_ldex = false;
14315 
14316     /* Bound the number of insns to execute to those left on the page.  */
14317     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14318 
14319     /* If architectural single step active, limit to 1.  */
14320     if (dc->ss_active) {
14321         bound = 1;
14322     }
14323     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14324 }
14325 
14326 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14327 {
14328 }
14329 
14330 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14331 {
14332     DisasContext *dc = container_of(dcbase, DisasContext, base);
14333     target_ulong pc_arg = dc->base.pc_next;
14334 
14335     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14336         pc_arg &= ~TARGET_PAGE_MASK;
14337     }
14338     tcg_gen_insn_start(pc_arg, 0, 0);
14339     dc->insn_start = tcg_last_op();
14340 }
14341 
14342 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14343 {
14344     DisasContext *s = container_of(dcbase, DisasContext, base);
14345     CPUARMState *env = cpu->env_ptr;
14346     uint64_t pc = s->base.pc_next;
14347     uint32_t insn;
14348 
14349     /* Singlestep exceptions have the highest priority. */
14350     if (s->ss_active && !s->pstate_ss) {
14351         /* Singlestep state is Active-pending.
14352          * If we're in this state at the start of a TB then either
14353          *  a) we just took an exception to an EL which is being debugged
14354          *     and this is the first insn in the exception handler
14355          *  b) debug exceptions were masked and we just unmasked them
14356          *     without changing EL (eg by clearing PSTATE.D)
14357          * In either case we're going to take a swstep exception in the
14358          * "did not step an insn" case, and so the syndrome ISV and EX
14359          * bits should be zero.
14360          */
14361         assert(s->base.num_insns == 1);
14362         gen_swstep_exception(s, 0, 0);
14363         s->base.is_jmp = DISAS_NORETURN;
14364         s->base.pc_next = pc + 4;
14365         return;
14366     }
14367 
14368     if (pc & 3) {
14369         /*
14370          * PC alignment fault.  This has priority over the instruction abort
14371          * that we would receive from a translation fault via arm_ldl_code.
14372          * This should only be possible after an indirect branch, at the
14373          * start of the TB.
14374          */
14375         assert(s->base.num_insns == 1);
14376         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14377         s->base.is_jmp = DISAS_NORETURN;
14378         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14379         return;
14380     }
14381 
14382     s->pc_curr = pc;
14383     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14384     s->insn = insn;
14385     s->base.pc_next = pc + 4;
14386 
14387     s->fp_access_checked = false;
14388     s->sve_access_checked = false;
14389 
14390     if (s->pstate_il) {
14391         /*
14392          * Illegal execution state. This has priority over BTI
14393          * exceptions, but comes after instruction abort exceptions.
14394          */
14395         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14396         return;
14397     }
14398 
14399     if (dc_isar_feature(aa64_bti, s)) {
14400         if (s->base.num_insns == 1) {
14401             /*
14402              * At the first insn of the TB, compute s->guarded_page.
14403              * We delayed computing this until successfully reading
14404              * the first insn of the TB, above.  This (mostly) ensures
14405              * that the softmmu tlb entry has been populated, and the
14406              * page table GP bit is available.
14407              *
14408              * Note that we need to compute this even if btype == 0,
14409              * because this value is used for BR instructions later
14410              * where ENV is not available.
14411              */
14412             s->guarded_page = is_guarded_page(env, s);
14413 
14414             /* First insn can have btype set to non-zero.  */
14415             tcg_debug_assert(s->btype >= 0);
14416 
14417             /*
14418              * Note that the Branch Target Exception has fairly high
14419              * priority -- below debugging exceptions but above most
14420              * everything else.  This allows us to handle this now
14421              * instead of waiting until the insn is otherwise decoded.
14422              */
14423             if (s->btype != 0
14424                 && s->guarded_page
14425                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14426                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14427                 return;
14428             }
14429         } else {
14430             /* Not the first insn: btype must be 0.  */
14431             tcg_debug_assert(s->btype == 0);
14432         }
14433     }
14434 
14435     s->is_nonstreaming = false;
14436     if (s->sme_trap_nonstreaming) {
14437         disas_sme_fa64(s, insn);
14438     }
14439 
14440     if (!disas_a64(s, insn) &&
14441         !disas_sme(s, insn) &&
14442         !disas_sve(s, insn)) {
14443         disas_a64_legacy(s, insn);
14444     }
14445 
14446     /*
14447      * After execution of most insns, btype is reset to 0.
14448      * Note that we set btype == -1 when the insn sets btype.
14449      */
14450     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14451         reset_btype(s);
14452     }
14453 }
14454 
14455 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14456 {
14457     DisasContext *dc = container_of(dcbase, DisasContext, base);
14458 
14459     if (unlikely(dc->ss_active)) {
14460         /* Note that this means single stepping WFI doesn't halt the CPU.
14461          * For conditional branch insns this is harmless unreachable code as
14462          * gen_goto_tb() has already handled emitting the debug exception
14463          * (and thus a tb-jump is not possible when singlestepping).
14464          */
14465         switch (dc->base.is_jmp) {
14466         default:
14467             gen_a64_update_pc(dc, 4);
14468             /* fall through */
14469         case DISAS_EXIT:
14470         case DISAS_JUMP:
14471             gen_step_complete_exception(dc);
14472             break;
14473         case DISAS_NORETURN:
14474             break;
14475         }
14476     } else {
14477         switch (dc->base.is_jmp) {
14478         case DISAS_NEXT:
14479         case DISAS_TOO_MANY:
14480             gen_goto_tb(dc, 1, 4);
14481             break;
14482         default:
14483         case DISAS_UPDATE_EXIT:
14484             gen_a64_update_pc(dc, 4);
14485             /* fall through */
14486         case DISAS_EXIT:
14487             tcg_gen_exit_tb(NULL, 0);
14488             break;
14489         case DISAS_UPDATE_NOCHAIN:
14490             gen_a64_update_pc(dc, 4);
14491             /* fall through */
14492         case DISAS_JUMP:
14493             tcg_gen_lookup_and_goto_ptr();
14494             break;
14495         case DISAS_NORETURN:
14496         case DISAS_SWI:
14497             break;
14498         case DISAS_WFE:
14499             gen_a64_update_pc(dc, 4);
14500             gen_helper_wfe(cpu_env);
14501             break;
14502         case DISAS_YIELD:
14503             gen_a64_update_pc(dc, 4);
14504             gen_helper_yield(cpu_env);
14505             break;
14506         case DISAS_WFI:
14507             /*
14508              * This is a special case because we don't want to just halt
14509              * the CPU if trying to debug across a WFI.
14510              */
14511             gen_a64_update_pc(dc, 4);
14512             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14513             /*
14514              * The helper doesn't necessarily throw an exception, but we
14515              * must go back to the main loop to check for interrupts anyway.
14516              */
14517             tcg_gen_exit_tb(NULL, 0);
14518             break;
14519         }
14520     }
14521 }
14522 
14523 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14524                                  CPUState *cpu, FILE *logfile)
14525 {
14526     DisasContext *dc = container_of(dcbase, DisasContext, base);
14527 
14528     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14529     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14530 }
14531 
14532 const TranslatorOps aarch64_translator_ops = {
14533     .init_disas_context = aarch64_tr_init_disas_context,
14534     .tb_start           = aarch64_tr_tb_start,
14535     .insn_start         = aarch64_tr_insn_start,
14536     .translate_insn     = aarch64_tr_translate_insn,
14537     .tb_stop            = aarch64_tr_tb_stop,
14538     .disas_log          = aarch64_tr_disas_log,
14539 };
14540