xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision b7ecc3da)
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 "exec/exec-all.h"
22 #include "translate.h"
23 #include "translate-a64.h"
24 #include "qemu/log.h"
25 #include "disas/disas.h"
26 #include "arm_ldst.h"
27 #include "semihosting/semihost.h"
28 #include "cpregs.h"
29 
30 static TCGv_i64 cpu_X[32];
31 static TCGv_i64 cpu_pc;
32 
33 /* Load/store exclusive handling */
34 static TCGv_i64 cpu_exclusive_high;
35 
36 static const char *regnames[] = {
37     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
38     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
39     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
40     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
41 };
42 
43 enum a64_shift_type {
44     A64_SHIFT_TYPE_LSL = 0,
45     A64_SHIFT_TYPE_LSR = 1,
46     A64_SHIFT_TYPE_ASR = 2,
47     A64_SHIFT_TYPE_ROR = 3
48 };
49 
50 /*
51  * Helpers for extracting complex instruction fields
52  */
53 
54 /*
55  * For load/store with an unsigned 12 bit immediate scaled by the element
56  * size. The input has the immediate field in bits [14:3] and the element
57  * size in [2:0].
58  */
59 static int uimm_scaled(DisasContext *s, int x)
60 {
61     unsigned imm = x >> 3;
62     unsigned scale = extract32(x, 0, 3);
63     return imm << scale;
64 }
65 
66 /* For load/store memory tags: scale offset by LOG2_TAG_GRANULE */
67 static int scale_by_log2_tag_granule(DisasContext *s, int x)
68 {
69     return x << LOG2_TAG_GRANULE;
70 }
71 
72 /*
73  * Include the generated decoders.
74  */
75 
76 #include "decode-sme-fa64.c.inc"
77 #include "decode-a64.c.inc"
78 
79 /* Table based decoder typedefs - used when the relevant bits for decode
80  * are too awkwardly scattered across the instruction (eg SIMD).
81  */
82 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
83 
84 typedef struct AArch64DecodeTable {
85     uint32_t pattern;
86     uint32_t mask;
87     AArch64DecodeFn *disas_fn;
88 } AArch64DecodeTable;
89 
90 /* initialize TCG globals.  */
91 void a64_translate_init(void)
92 {
93     int i;
94 
95     cpu_pc = tcg_global_mem_new_i64(tcg_env,
96                                     offsetof(CPUARMState, pc),
97                                     "pc");
98     for (i = 0; i < 32; i++) {
99         cpu_X[i] = tcg_global_mem_new_i64(tcg_env,
100                                           offsetof(CPUARMState, xregs[i]),
101                                           regnames[i]);
102     }
103 
104     cpu_exclusive_high = tcg_global_mem_new_i64(tcg_env,
105         offsetof(CPUARMState, exclusive_high), "exclusive_high");
106 }
107 
108 /*
109  * Return the core mmu_idx to use for A64 load/store insns which
110  * have a "unprivileged load/store" variant. Those insns access
111  * EL0 if executed from an EL which has control over EL0 (usually
112  * EL1) but behave like normal loads and stores if executed from
113  * elsewhere (eg EL3).
114  *
115  * @unpriv : true for the unprivileged encoding; false for the
116  *           normal encoding (in which case we will return the same
117  *           thing as get_mem_index().
118  */
119 static int get_a64_user_mem_index(DisasContext *s, bool unpriv)
120 {
121     /*
122      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
123      * which is the usual mmu_idx for this cpu state.
124      */
125     ARMMMUIdx useridx = s->mmu_idx;
126 
127     if (unpriv && s->unpriv) {
128         /*
129          * We have pre-computed the condition for AccType_UNPRIV.
130          * Therefore we should never get here with a mmu_idx for
131          * which we do not know the corresponding user mmu_idx.
132          */
133         switch (useridx) {
134         case ARMMMUIdx_E10_1:
135         case ARMMMUIdx_E10_1_PAN:
136             useridx = ARMMMUIdx_E10_0;
137             break;
138         case ARMMMUIdx_E20_2:
139         case ARMMMUIdx_E20_2_PAN:
140             useridx = ARMMMUIdx_E20_0;
141             break;
142         default:
143             g_assert_not_reached();
144         }
145     }
146     return arm_to_core_mmu_idx(useridx);
147 }
148 
149 static void set_btype_raw(int val)
150 {
151     tcg_gen_st_i32(tcg_constant_i32(val), tcg_env,
152                    offsetof(CPUARMState, btype));
153 }
154 
155 static void set_btype(DisasContext *s, int val)
156 {
157     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
158     tcg_debug_assert(val >= 1 && val <= 3);
159     set_btype_raw(val);
160     s->btype = -1;
161 }
162 
163 static void reset_btype(DisasContext *s)
164 {
165     if (s->btype != 0) {
166         set_btype_raw(0);
167         s->btype = 0;
168     }
169 }
170 
171 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
172 {
173     assert(s->pc_save != -1);
174     if (tb_cflags(s->base.tb) & CF_PCREL) {
175         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
176     } else {
177         tcg_gen_movi_i64(dest, s->pc_curr + diff);
178     }
179 }
180 
181 void gen_a64_update_pc(DisasContext *s, target_long diff)
182 {
183     gen_pc_plus_diff(s, cpu_pc, diff);
184     s->pc_save = s->pc_curr + diff;
185 }
186 
187 /*
188  * Handle Top Byte Ignore (TBI) bits.
189  *
190  * If address tagging is enabled via the TCR TBI bits:
191  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
192  *    then the address is zero-extended, clearing bits [63:56]
193  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
194  *    and TBI1 controls addresses with bit 55 == 1.
195  *    If the appropriate TBI bit is set for the address then
196  *    the address is sign-extended from bit 55 into bits [63:56]
197  *
198  * Here We have concatenated TBI{1,0} into tbi.
199  */
200 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
201                                 TCGv_i64 src, int tbi)
202 {
203     if (tbi == 0) {
204         /* Load unmodified address */
205         tcg_gen_mov_i64(dst, src);
206     } else if (!regime_has_2_ranges(s->mmu_idx)) {
207         /* Force tag byte to all zero */
208         tcg_gen_extract_i64(dst, src, 0, 56);
209     } else {
210         /* Sign-extend from bit 55.  */
211         tcg_gen_sextract_i64(dst, src, 0, 56);
212 
213         switch (tbi) {
214         case 1:
215             /* tbi0 but !tbi1: only use the extension if positive */
216             tcg_gen_and_i64(dst, dst, src);
217             break;
218         case 2:
219             /* !tbi0 but tbi1: only use the extension if negative */
220             tcg_gen_or_i64(dst, dst, src);
221             break;
222         case 3:
223             /* tbi0 and tbi1: always use the extension */
224             break;
225         default:
226             g_assert_not_reached();
227         }
228     }
229 }
230 
231 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
232 {
233     /*
234      * If address tagging is enabled for instructions via the TCR TBI bits,
235      * then loading an address into the PC will clear out any tag.
236      */
237     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
238     s->pc_save = -1;
239 }
240 
241 /*
242  * Handle MTE and/or TBI.
243  *
244  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
245  * for the tag to be present in the FAR_ELx register.  But for user-only
246  * mode we do not have a TLB with which to implement this, so we must
247  * remove the top byte now.
248  *
249  * Always return a fresh temporary that we can increment independently
250  * of the write-back address.
251  */
252 
253 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
254 {
255     TCGv_i64 clean = tcg_temp_new_i64();
256 #ifdef CONFIG_USER_ONLY
257     gen_top_byte_ignore(s, clean, addr, s->tbid);
258 #else
259     tcg_gen_mov_i64(clean, addr);
260 #endif
261     return clean;
262 }
263 
264 /* Insert a zero tag into src, with the result at dst. */
265 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
266 {
267     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
268 }
269 
270 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
271                              MMUAccessType acc, int log2_size)
272 {
273     gen_helper_probe_access(tcg_env, ptr,
274                             tcg_constant_i32(acc),
275                             tcg_constant_i32(get_mem_index(s)),
276                             tcg_constant_i32(1 << log2_size));
277 }
278 
279 /*
280  * For MTE, check a single logical or atomic access.  This probes a single
281  * address, the exact one specified.  The size and alignment of the access
282  * is not relevant to MTE, per se, but watchpoints do require the size,
283  * and we want to recognize those before making any other changes to state.
284  */
285 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
286                                       bool is_write, bool tag_checked,
287                                       MemOp memop, bool is_unpriv,
288                                       int core_idx)
289 {
290     if (tag_checked && s->mte_active[is_unpriv]) {
291         TCGv_i64 ret;
292         int desc = 0;
293 
294         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
295         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
296         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
297         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
298         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
299         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
300 
301         ret = tcg_temp_new_i64();
302         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
303 
304         return ret;
305     }
306     return clean_data_tbi(s, addr);
307 }
308 
309 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
310                         bool tag_checked, MemOp memop)
311 {
312     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
313                                  false, get_mem_index(s));
314 }
315 
316 /*
317  * For MTE, check multiple logical sequential accesses.
318  */
319 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
320                         bool tag_checked, int total_size, MemOp single_mop)
321 {
322     if (tag_checked && s->mte_active[0]) {
323         TCGv_i64 ret;
324         int desc = 0;
325 
326         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
327         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
328         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
329         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
330         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
331         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
332 
333         ret = tcg_temp_new_i64();
334         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
335 
336         return ret;
337     }
338     return clean_data_tbi(s, addr);
339 }
340 
341 /*
342  * Generate the special alignment check that applies to AccType_ATOMIC
343  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
344  * naturally aligned, but it must not cross a 16-byte boundary.
345  * See AArch64.CheckAlignment().
346  */
347 static void check_lse2_align(DisasContext *s, int rn, int imm,
348                              bool is_write, MemOp mop)
349 {
350     TCGv_i32 tmp;
351     TCGv_i64 addr;
352     TCGLabel *over_label;
353     MMUAccessType type;
354     int mmu_idx;
355 
356     tmp = tcg_temp_new_i32();
357     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
358     tcg_gen_addi_i32(tmp, tmp, imm & 15);
359     tcg_gen_andi_i32(tmp, tmp, 15);
360     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
361 
362     over_label = gen_new_label();
363     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
364 
365     addr = tcg_temp_new_i64();
366     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
367 
368     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
369     mmu_idx = get_mem_index(s);
370     gen_helper_unaligned_access(tcg_env, addr, tcg_constant_i32(type),
371                                 tcg_constant_i32(mmu_idx));
372 
373     gen_set_label(over_label);
374 
375 }
376 
377 /* Handle the alignment check for AccType_ATOMIC instructions. */
378 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
379 {
380     MemOp size = mop & MO_SIZE;
381 
382     if (size == MO_8) {
383         return mop;
384     }
385 
386     /*
387      * If size == MO_128, this is a LDXP, and the operation is single-copy
388      * atomic for each doubleword, not the entire quadword; it still must
389      * be quadword aligned.
390      */
391     if (size == MO_128) {
392         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
393                                    MO_ATOM_IFALIGN_PAIR);
394     }
395     if (dc_isar_feature(aa64_lse2, s)) {
396         check_lse2_align(s, rn, 0, true, mop);
397     } else {
398         mop |= MO_ALIGN;
399     }
400     return finalize_memop(s, mop);
401 }
402 
403 /* Handle the alignment check for AccType_ORDERED instructions. */
404 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
405                                  bool is_write, MemOp mop)
406 {
407     MemOp size = mop & MO_SIZE;
408 
409     if (size == MO_8) {
410         return mop;
411     }
412     if (size == MO_128) {
413         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
414                                    MO_ATOM_IFALIGN_PAIR);
415     }
416     if (!dc_isar_feature(aa64_lse2, s)) {
417         mop |= MO_ALIGN;
418     } else if (!s->naa) {
419         check_lse2_align(s, rn, imm, is_write, mop);
420     }
421     return finalize_memop(s, mop);
422 }
423 
424 typedef struct DisasCompare64 {
425     TCGCond cond;
426     TCGv_i64 value;
427 } DisasCompare64;
428 
429 static void a64_test_cc(DisasCompare64 *c64, int cc)
430 {
431     DisasCompare c32;
432 
433     arm_test_cc(&c32, cc);
434 
435     /*
436      * Sign-extend the 32-bit value so that the GE/LT comparisons work
437      * properly.  The NE/EQ comparisons are also fine with this choice.
438       */
439     c64->cond = c32.cond;
440     c64->value = tcg_temp_new_i64();
441     tcg_gen_ext_i32_i64(c64->value, c32.value);
442 }
443 
444 static void gen_rebuild_hflags(DisasContext *s)
445 {
446     gen_helper_rebuild_hflags_a64(tcg_env, tcg_constant_i32(s->current_el));
447 }
448 
449 static void gen_exception_internal(int excp)
450 {
451     assert(excp_is_internal(excp));
452     gen_helper_exception_internal(tcg_env, tcg_constant_i32(excp));
453 }
454 
455 static void gen_exception_internal_insn(DisasContext *s, int excp)
456 {
457     gen_a64_update_pc(s, 0);
458     gen_exception_internal(excp);
459     s->base.is_jmp = DISAS_NORETURN;
460 }
461 
462 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
463 {
464     gen_a64_update_pc(s, 0);
465     gen_helper_exception_bkpt_insn(tcg_env, tcg_constant_i32(syndrome));
466     s->base.is_jmp = DISAS_NORETURN;
467 }
468 
469 static void gen_step_complete_exception(DisasContext *s)
470 {
471     /* We just completed step of an insn. Move from Active-not-pending
472      * to Active-pending, and then also take the swstep exception.
473      * This corresponds to making the (IMPDEF) choice to prioritize
474      * swstep exceptions over asynchronous exceptions taken to an exception
475      * level where debug is disabled. This choice has the advantage that
476      * we do not need to maintain internal state corresponding to the
477      * ISV/EX syndrome bits between completion of the step and generation
478      * of the exception, and our syndrome information is always correct.
479      */
480     gen_ss_advance(s);
481     gen_swstep_exception(s, 1, s->is_ldex);
482     s->base.is_jmp = DISAS_NORETURN;
483 }
484 
485 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
486 {
487     if (s->ss_active) {
488         return false;
489     }
490     return translator_use_goto_tb(&s->base, dest);
491 }
492 
493 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
494 {
495     if (use_goto_tb(s, s->pc_curr + diff)) {
496         /*
497          * For pcrel, the pc must always be up-to-date on entry to
498          * the linked TB, so that it can use simple additions for all
499          * further adjustments.  For !pcrel, the linked TB is compiled
500          * to know its full virtual address, so we can delay the
501          * update to pc to the unlinked path.  A long chain of links
502          * can thus avoid many updates to the PC.
503          */
504         if (tb_cflags(s->base.tb) & CF_PCREL) {
505             gen_a64_update_pc(s, diff);
506             tcg_gen_goto_tb(n);
507         } else {
508             tcg_gen_goto_tb(n);
509             gen_a64_update_pc(s, diff);
510         }
511         tcg_gen_exit_tb(s->base.tb, n);
512         s->base.is_jmp = DISAS_NORETURN;
513     } else {
514         gen_a64_update_pc(s, diff);
515         if (s->ss_active) {
516             gen_step_complete_exception(s);
517         } else {
518             tcg_gen_lookup_and_goto_ptr();
519             s->base.is_jmp = DISAS_NORETURN;
520         }
521     }
522 }
523 
524 /*
525  * Register access functions
526  *
527  * These functions are used for directly accessing a register in where
528  * changes to the final register value are likely to be made. If you
529  * need to use a register for temporary calculation (e.g. index type
530  * operations) use the read_* form.
531  *
532  * B1.2.1 Register mappings
533  *
534  * In instruction register encoding 31 can refer to ZR (zero register) or
535  * the SP (stack pointer) depending on context. In QEMU's case we map SP
536  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
537  * This is the point of the _sp forms.
538  */
539 TCGv_i64 cpu_reg(DisasContext *s, int reg)
540 {
541     if (reg == 31) {
542         TCGv_i64 t = tcg_temp_new_i64();
543         tcg_gen_movi_i64(t, 0);
544         return t;
545     } else {
546         return cpu_X[reg];
547     }
548 }
549 
550 /* register access for when 31 == SP */
551 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
552 {
553     return cpu_X[reg];
554 }
555 
556 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
557  * representing the register contents. This TCGv is an auto-freed
558  * temporary so it need not be explicitly freed, and may be modified.
559  */
560 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
561 {
562     TCGv_i64 v = tcg_temp_new_i64();
563     if (reg != 31) {
564         if (sf) {
565             tcg_gen_mov_i64(v, cpu_X[reg]);
566         } else {
567             tcg_gen_ext32u_i64(v, cpu_X[reg]);
568         }
569     } else {
570         tcg_gen_movi_i64(v, 0);
571     }
572     return v;
573 }
574 
575 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
576 {
577     TCGv_i64 v = tcg_temp_new_i64();
578     if (sf) {
579         tcg_gen_mov_i64(v, cpu_X[reg]);
580     } else {
581         tcg_gen_ext32u_i64(v, cpu_X[reg]);
582     }
583     return v;
584 }
585 
586 /* Return the offset into CPUARMState of a slice (from
587  * the least significant end) of FP register Qn (ie
588  * Dn, Sn, Hn or Bn).
589  * (Note that this is not the same mapping as for A32; see cpu.h)
590  */
591 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
592 {
593     return vec_reg_offset(s, regno, 0, size);
594 }
595 
596 /* Offset of the high half of the 128 bit vector Qn */
597 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
598 {
599     return vec_reg_offset(s, regno, 1, MO_64);
600 }
601 
602 /* Convenience accessors for reading and writing single and double
603  * FP registers. Writing clears the upper parts of the associated
604  * 128 bit vector register, as required by the architecture.
605  * Note that unlike the GP register accessors, the values returned
606  * by the read functions must be manually freed.
607  */
608 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
609 {
610     TCGv_i64 v = tcg_temp_new_i64();
611 
612     tcg_gen_ld_i64(v, tcg_env, fp_reg_offset(s, reg, MO_64));
613     return v;
614 }
615 
616 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
617 {
618     TCGv_i32 v = tcg_temp_new_i32();
619 
620     tcg_gen_ld_i32(v, tcg_env, fp_reg_offset(s, reg, MO_32));
621     return v;
622 }
623 
624 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
625 {
626     TCGv_i32 v = tcg_temp_new_i32();
627 
628     tcg_gen_ld16u_i32(v, tcg_env, fp_reg_offset(s, reg, MO_16));
629     return v;
630 }
631 
632 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
633  * If SVE is not enabled, then there are only 128 bits in the vector.
634  */
635 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
636 {
637     unsigned ofs = fp_reg_offset(s, rd, MO_64);
638     unsigned vsz = vec_full_reg_size(s);
639 
640     /* Nop move, with side effect of clearing the tail. */
641     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
642 }
643 
644 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
645 {
646     unsigned ofs = fp_reg_offset(s, reg, MO_64);
647 
648     tcg_gen_st_i64(v, tcg_env, ofs);
649     clear_vec_high(s, false, reg);
650 }
651 
652 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
653 {
654     TCGv_i64 tmp = tcg_temp_new_i64();
655 
656     tcg_gen_extu_i32_i64(tmp, v);
657     write_fp_dreg(s, reg, tmp);
658 }
659 
660 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
661 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
662                          GVecGen2Fn *gvec_fn, int vece)
663 {
664     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
665             is_q ? 16 : 8, vec_full_reg_size(s));
666 }
667 
668 /* Expand a 2-operand + immediate AdvSIMD vector operation using
669  * an expander function.
670  */
671 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
672                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
673 {
674     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
675             imm, is_q ? 16 : 8, vec_full_reg_size(s));
676 }
677 
678 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
679 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
680                          GVecGen3Fn *gvec_fn, int vece)
681 {
682     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
683             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
684 }
685 
686 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
687 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
688                          int rx, GVecGen4Fn *gvec_fn, int vece)
689 {
690     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
691             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
692             is_q ? 16 : 8, vec_full_reg_size(s));
693 }
694 
695 /* Expand a 2-operand operation using an out-of-line helper.  */
696 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
697                              int rn, int data, gen_helper_gvec_2 *fn)
698 {
699     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
700                        vec_full_reg_offset(s, rn),
701                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
702 }
703 
704 /* Expand a 3-operand operation using an out-of-line helper.  */
705 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
706                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
707 {
708     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
709                        vec_full_reg_offset(s, rn),
710                        vec_full_reg_offset(s, rm),
711                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
712 }
713 
714 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
715  * an out-of-line helper.
716  */
717 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
718                               int rm, bool is_fp16, int data,
719                               gen_helper_gvec_3_ptr *fn)
720 {
721     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
722     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
723                        vec_full_reg_offset(s, rn),
724                        vec_full_reg_offset(s, rm), fpst,
725                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
726 }
727 
728 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
729 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
730                             int rm, gen_helper_gvec_3_ptr *fn)
731 {
732     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
733 
734     tcg_gen_addi_ptr(qc_ptr, tcg_env, offsetof(CPUARMState, vfp.qc));
735     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
736                        vec_full_reg_offset(s, rn),
737                        vec_full_reg_offset(s, rm), qc_ptr,
738                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
739 }
740 
741 /* Expand a 4-operand operation using an out-of-line helper.  */
742 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
743                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
744 {
745     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
746                        vec_full_reg_offset(s, rn),
747                        vec_full_reg_offset(s, rm),
748                        vec_full_reg_offset(s, ra),
749                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
750 }
751 
752 /*
753  * Expand a 4-operand + fpstatus pointer + simd data value operation using
754  * an out-of-line helper.
755  */
756 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
757                               int rm, int ra, bool is_fp16, int data,
758                               gen_helper_gvec_4_ptr *fn)
759 {
760     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
761     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
762                        vec_full_reg_offset(s, rn),
763                        vec_full_reg_offset(s, rm),
764                        vec_full_reg_offset(s, ra), fpst,
765                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
766 }
767 
768 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
769  * than the 32 bit equivalent.
770  */
771 static inline void gen_set_NZ64(TCGv_i64 result)
772 {
773     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
774     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
775 }
776 
777 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
778 static inline void gen_logic_CC(int sf, TCGv_i64 result)
779 {
780     if (sf) {
781         gen_set_NZ64(result);
782     } else {
783         tcg_gen_extrl_i64_i32(cpu_ZF, result);
784         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
785     }
786     tcg_gen_movi_i32(cpu_CF, 0);
787     tcg_gen_movi_i32(cpu_VF, 0);
788 }
789 
790 /* dest = T0 + T1; compute C, N, V and Z flags */
791 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
792 {
793     TCGv_i64 result, flag, tmp;
794     result = tcg_temp_new_i64();
795     flag = tcg_temp_new_i64();
796     tmp = tcg_temp_new_i64();
797 
798     tcg_gen_movi_i64(tmp, 0);
799     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
800 
801     tcg_gen_extrl_i64_i32(cpu_CF, flag);
802 
803     gen_set_NZ64(result);
804 
805     tcg_gen_xor_i64(flag, result, t0);
806     tcg_gen_xor_i64(tmp, t0, t1);
807     tcg_gen_andc_i64(flag, flag, tmp);
808     tcg_gen_extrh_i64_i32(cpu_VF, flag);
809 
810     tcg_gen_mov_i64(dest, result);
811 }
812 
813 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
814 {
815     TCGv_i32 t0_32 = tcg_temp_new_i32();
816     TCGv_i32 t1_32 = tcg_temp_new_i32();
817     TCGv_i32 tmp = tcg_temp_new_i32();
818 
819     tcg_gen_movi_i32(tmp, 0);
820     tcg_gen_extrl_i64_i32(t0_32, t0);
821     tcg_gen_extrl_i64_i32(t1_32, t1);
822     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
823     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
824     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
825     tcg_gen_xor_i32(tmp, t0_32, t1_32);
826     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
827     tcg_gen_extu_i32_i64(dest, cpu_NF);
828 }
829 
830 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
831 {
832     if (sf) {
833         gen_add64_CC(dest, t0, t1);
834     } else {
835         gen_add32_CC(dest, t0, t1);
836     }
837 }
838 
839 /* dest = T0 - T1; compute C, N, V and Z flags */
840 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
841 {
842     /* 64 bit arithmetic */
843     TCGv_i64 result, flag, tmp;
844 
845     result = tcg_temp_new_i64();
846     flag = tcg_temp_new_i64();
847     tcg_gen_sub_i64(result, t0, t1);
848 
849     gen_set_NZ64(result);
850 
851     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
852     tcg_gen_extrl_i64_i32(cpu_CF, flag);
853 
854     tcg_gen_xor_i64(flag, result, t0);
855     tmp = tcg_temp_new_i64();
856     tcg_gen_xor_i64(tmp, t0, t1);
857     tcg_gen_and_i64(flag, flag, tmp);
858     tcg_gen_extrh_i64_i32(cpu_VF, flag);
859     tcg_gen_mov_i64(dest, result);
860 }
861 
862 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
863 {
864     /* 32 bit arithmetic */
865     TCGv_i32 t0_32 = tcg_temp_new_i32();
866     TCGv_i32 t1_32 = tcg_temp_new_i32();
867     TCGv_i32 tmp;
868 
869     tcg_gen_extrl_i64_i32(t0_32, t0);
870     tcg_gen_extrl_i64_i32(t1_32, t1);
871     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
872     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
873     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
874     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
875     tmp = tcg_temp_new_i32();
876     tcg_gen_xor_i32(tmp, t0_32, t1_32);
877     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
878     tcg_gen_extu_i32_i64(dest, cpu_NF);
879 }
880 
881 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
882 {
883     if (sf) {
884         gen_sub64_CC(dest, t0, t1);
885     } else {
886         gen_sub32_CC(dest, t0, t1);
887     }
888 }
889 
890 /* dest = T0 + T1 + CF; do not compute flags. */
891 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
892 {
893     TCGv_i64 flag = tcg_temp_new_i64();
894     tcg_gen_extu_i32_i64(flag, cpu_CF);
895     tcg_gen_add_i64(dest, t0, t1);
896     tcg_gen_add_i64(dest, dest, flag);
897 
898     if (!sf) {
899         tcg_gen_ext32u_i64(dest, dest);
900     }
901 }
902 
903 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
904 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
905 {
906     if (sf) {
907         TCGv_i64 result = tcg_temp_new_i64();
908         TCGv_i64 cf_64 = tcg_temp_new_i64();
909         TCGv_i64 vf_64 = tcg_temp_new_i64();
910         TCGv_i64 tmp = tcg_temp_new_i64();
911         TCGv_i64 zero = tcg_constant_i64(0);
912 
913         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
914         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
915         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
916         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
917         gen_set_NZ64(result);
918 
919         tcg_gen_xor_i64(vf_64, result, t0);
920         tcg_gen_xor_i64(tmp, t0, t1);
921         tcg_gen_andc_i64(vf_64, vf_64, tmp);
922         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
923 
924         tcg_gen_mov_i64(dest, result);
925     } else {
926         TCGv_i32 t0_32 = tcg_temp_new_i32();
927         TCGv_i32 t1_32 = tcg_temp_new_i32();
928         TCGv_i32 tmp = tcg_temp_new_i32();
929         TCGv_i32 zero = tcg_constant_i32(0);
930 
931         tcg_gen_extrl_i64_i32(t0_32, t0);
932         tcg_gen_extrl_i64_i32(t1_32, t1);
933         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
934         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
935 
936         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
937         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
938         tcg_gen_xor_i32(tmp, t0_32, t1_32);
939         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
940         tcg_gen_extu_i32_i64(dest, cpu_NF);
941     }
942 }
943 
944 /*
945  * Load/Store generators
946  */
947 
948 /*
949  * Store from GPR register to memory.
950  */
951 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
952                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
953                              bool iss_valid,
954                              unsigned int iss_srt,
955                              bool iss_sf, bool iss_ar)
956 {
957     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
958 
959     if (iss_valid) {
960         uint32_t syn;
961 
962         syn = syn_data_abort_with_iss(0,
963                                       (memop & MO_SIZE),
964                                       false,
965                                       iss_srt,
966                                       iss_sf,
967                                       iss_ar,
968                                       0, 0, 0, 0, 0, false);
969         disas_set_insn_syndrome(s, syn);
970     }
971 }
972 
973 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
974                       TCGv_i64 tcg_addr, MemOp memop,
975                       bool iss_valid,
976                       unsigned int iss_srt,
977                       bool iss_sf, bool iss_ar)
978 {
979     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
980                      iss_valid, iss_srt, iss_sf, iss_ar);
981 }
982 
983 /*
984  * Load from memory to GPR register
985  */
986 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
987                              MemOp memop, bool extend, int memidx,
988                              bool iss_valid, unsigned int iss_srt,
989                              bool iss_sf, bool iss_ar)
990 {
991     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
992 
993     if (extend && (memop & MO_SIGN)) {
994         g_assert((memop & MO_SIZE) <= MO_32);
995         tcg_gen_ext32u_i64(dest, dest);
996     }
997 
998     if (iss_valid) {
999         uint32_t syn;
1000 
1001         syn = syn_data_abort_with_iss(0,
1002                                       (memop & MO_SIZE),
1003                                       (memop & MO_SIGN) != 0,
1004                                       iss_srt,
1005                                       iss_sf,
1006                                       iss_ar,
1007                                       0, 0, 0, 0, 0, false);
1008         disas_set_insn_syndrome(s, syn);
1009     }
1010 }
1011 
1012 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1013                       MemOp memop, bool extend,
1014                       bool iss_valid, unsigned int iss_srt,
1015                       bool iss_sf, bool iss_ar)
1016 {
1017     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1018                      iss_valid, iss_srt, iss_sf, iss_ar);
1019 }
1020 
1021 /*
1022  * Store from FP register to memory
1023  */
1024 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1025 {
1026     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1027     TCGv_i64 tmplo = tcg_temp_new_i64();
1028 
1029     tcg_gen_ld_i64(tmplo, tcg_env, fp_reg_offset(s, srcidx, MO_64));
1030 
1031     if ((mop & MO_SIZE) < MO_128) {
1032         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1033     } else {
1034         TCGv_i64 tmphi = tcg_temp_new_i64();
1035         TCGv_i128 t16 = tcg_temp_new_i128();
1036 
1037         tcg_gen_ld_i64(tmphi, tcg_env, fp_reg_hi_offset(s, srcidx));
1038         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1039 
1040         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1041     }
1042 }
1043 
1044 /*
1045  * Load from memory to FP register
1046  */
1047 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1048 {
1049     /* This always zero-extends and writes to a full 128 bit wide vector */
1050     TCGv_i64 tmplo = tcg_temp_new_i64();
1051     TCGv_i64 tmphi = NULL;
1052 
1053     if ((mop & MO_SIZE) < MO_128) {
1054         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1055     } else {
1056         TCGv_i128 t16 = tcg_temp_new_i128();
1057 
1058         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1059 
1060         tmphi = tcg_temp_new_i64();
1061         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1062     }
1063 
1064     tcg_gen_st_i64(tmplo, tcg_env, fp_reg_offset(s, destidx, MO_64));
1065 
1066     if (tmphi) {
1067         tcg_gen_st_i64(tmphi, tcg_env, fp_reg_hi_offset(s, destidx));
1068     }
1069     clear_vec_high(s, tmphi != NULL, destidx);
1070 }
1071 
1072 /*
1073  * Vector load/store helpers.
1074  *
1075  * The principal difference between this and a FP load is that we don't
1076  * zero extend as we are filling a partial chunk of the vector register.
1077  * These functions don't support 128 bit loads/stores, which would be
1078  * normal load/store operations.
1079  *
1080  * The _i32 versions are useful when operating on 32 bit quantities
1081  * (eg for floating point single or using Neon helper functions).
1082  */
1083 
1084 /* Get value of an element within a vector register */
1085 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1086                              int element, MemOp memop)
1087 {
1088     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1089     switch ((unsigned)memop) {
1090     case MO_8:
1091         tcg_gen_ld8u_i64(tcg_dest, tcg_env, vect_off);
1092         break;
1093     case MO_16:
1094         tcg_gen_ld16u_i64(tcg_dest, tcg_env, vect_off);
1095         break;
1096     case MO_32:
1097         tcg_gen_ld32u_i64(tcg_dest, tcg_env, vect_off);
1098         break;
1099     case MO_8|MO_SIGN:
1100         tcg_gen_ld8s_i64(tcg_dest, tcg_env, vect_off);
1101         break;
1102     case MO_16|MO_SIGN:
1103         tcg_gen_ld16s_i64(tcg_dest, tcg_env, vect_off);
1104         break;
1105     case MO_32|MO_SIGN:
1106         tcg_gen_ld32s_i64(tcg_dest, tcg_env, vect_off);
1107         break;
1108     case MO_64:
1109     case MO_64|MO_SIGN:
1110         tcg_gen_ld_i64(tcg_dest, tcg_env, vect_off);
1111         break;
1112     default:
1113         g_assert_not_reached();
1114     }
1115 }
1116 
1117 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1118                                  int element, MemOp memop)
1119 {
1120     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1121     switch (memop) {
1122     case MO_8:
1123         tcg_gen_ld8u_i32(tcg_dest, tcg_env, vect_off);
1124         break;
1125     case MO_16:
1126         tcg_gen_ld16u_i32(tcg_dest, tcg_env, vect_off);
1127         break;
1128     case MO_8|MO_SIGN:
1129         tcg_gen_ld8s_i32(tcg_dest, tcg_env, vect_off);
1130         break;
1131     case MO_16|MO_SIGN:
1132         tcg_gen_ld16s_i32(tcg_dest, tcg_env, vect_off);
1133         break;
1134     case MO_32:
1135     case MO_32|MO_SIGN:
1136         tcg_gen_ld_i32(tcg_dest, tcg_env, vect_off);
1137         break;
1138     default:
1139         g_assert_not_reached();
1140     }
1141 }
1142 
1143 /* Set value of an element within a vector register */
1144 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1145                               int element, MemOp memop)
1146 {
1147     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1148     switch (memop) {
1149     case MO_8:
1150         tcg_gen_st8_i64(tcg_src, tcg_env, vect_off);
1151         break;
1152     case MO_16:
1153         tcg_gen_st16_i64(tcg_src, tcg_env, vect_off);
1154         break;
1155     case MO_32:
1156         tcg_gen_st32_i64(tcg_src, tcg_env, vect_off);
1157         break;
1158     case MO_64:
1159         tcg_gen_st_i64(tcg_src, tcg_env, vect_off);
1160         break;
1161     default:
1162         g_assert_not_reached();
1163     }
1164 }
1165 
1166 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1167                                   int destidx, int element, MemOp memop)
1168 {
1169     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1170     switch (memop) {
1171     case MO_8:
1172         tcg_gen_st8_i32(tcg_src, tcg_env, vect_off);
1173         break;
1174     case MO_16:
1175         tcg_gen_st16_i32(tcg_src, tcg_env, vect_off);
1176         break;
1177     case MO_32:
1178         tcg_gen_st_i32(tcg_src, tcg_env, vect_off);
1179         break;
1180     default:
1181         g_assert_not_reached();
1182     }
1183 }
1184 
1185 /* Store from vector register to memory */
1186 static void do_vec_st(DisasContext *s, int srcidx, int element,
1187                       TCGv_i64 tcg_addr, MemOp mop)
1188 {
1189     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1190 
1191     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1192     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1193 }
1194 
1195 /* Load from memory to vector register */
1196 static void do_vec_ld(DisasContext *s, int destidx, int element,
1197                       TCGv_i64 tcg_addr, MemOp mop)
1198 {
1199     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1200 
1201     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1202     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1203 }
1204 
1205 /* Check that FP/Neon access is enabled. If it is, return
1206  * true. If not, emit code to generate an appropriate exception,
1207  * and return false; the caller should not emit any code for
1208  * the instruction. Note that this check must happen after all
1209  * unallocated-encoding checks (otherwise the syndrome information
1210  * for the resulting exception will be incorrect).
1211  */
1212 static bool fp_access_check_only(DisasContext *s)
1213 {
1214     if (s->fp_excp_el) {
1215         assert(!s->fp_access_checked);
1216         s->fp_access_checked = true;
1217 
1218         gen_exception_insn_el(s, 0, EXCP_UDEF,
1219                               syn_fp_access_trap(1, 0xe, false, 0),
1220                               s->fp_excp_el);
1221         return false;
1222     }
1223     s->fp_access_checked = true;
1224     return true;
1225 }
1226 
1227 static bool fp_access_check(DisasContext *s)
1228 {
1229     if (!fp_access_check_only(s)) {
1230         return false;
1231     }
1232     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1233         gen_exception_insn(s, 0, EXCP_UDEF,
1234                            syn_smetrap(SME_ET_Streaming, false));
1235         return false;
1236     }
1237     return true;
1238 }
1239 
1240 /*
1241  * Check that SVE access is enabled.  If it is, return true.
1242  * If not, emit code to generate an appropriate exception and return false.
1243  * This function corresponds to CheckSVEEnabled().
1244  */
1245 bool sve_access_check(DisasContext *s)
1246 {
1247     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1248         assert(dc_isar_feature(aa64_sme, s));
1249         if (!sme_sm_enabled_check(s)) {
1250             goto fail_exit;
1251         }
1252     } else if (s->sve_excp_el) {
1253         gen_exception_insn_el(s, 0, EXCP_UDEF,
1254                               syn_sve_access_trap(), s->sve_excp_el);
1255         goto fail_exit;
1256     }
1257     s->sve_access_checked = true;
1258     return fp_access_check(s);
1259 
1260  fail_exit:
1261     /* Assert that we only raise one exception per instruction. */
1262     assert(!s->sve_access_checked);
1263     s->sve_access_checked = true;
1264     return false;
1265 }
1266 
1267 /*
1268  * Check that SME access is enabled, raise an exception if not.
1269  * Note that this function corresponds to CheckSMEAccess and is
1270  * only used directly for cpregs.
1271  */
1272 static bool sme_access_check(DisasContext *s)
1273 {
1274     if (s->sme_excp_el) {
1275         gen_exception_insn_el(s, 0, EXCP_UDEF,
1276                               syn_smetrap(SME_ET_AccessTrap, false),
1277                               s->sme_excp_el);
1278         return false;
1279     }
1280     return true;
1281 }
1282 
1283 /* This function corresponds to CheckSMEEnabled. */
1284 bool sme_enabled_check(DisasContext *s)
1285 {
1286     /*
1287      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1288      * to be zero when fp_excp_el has priority.  This is because we need
1289      * sme_excp_el by itself for cpregs access checks.
1290      */
1291     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1292         s->fp_access_checked = true;
1293         return sme_access_check(s);
1294     }
1295     return fp_access_check_only(s);
1296 }
1297 
1298 /* Common subroutine for CheckSMEAnd*Enabled. */
1299 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1300 {
1301     if (!sme_enabled_check(s)) {
1302         return false;
1303     }
1304     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1305         gen_exception_insn(s, 0, EXCP_UDEF,
1306                            syn_smetrap(SME_ET_NotStreaming, false));
1307         return false;
1308     }
1309     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1310         gen_exception_insn(s, 0, EXCP_UDEF,
1311                            syn_smetrap(SME_ET_InactiveZA, false));
1312         return false;
1313     }
1314     return true;
1315 }
1316 
1317 /*
1318  * This utility function is for doing register extension with an
1319  * optional shift. You will likely want to pass a temporary for the
1320  * destination register. See DecodeRegExtend() in the ARM ARM.
1321  */
1322 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1323                               int option, unsigned int shift)
1324 {
1325     int extsize = extract32(option, 0, 2);
1326     bool is_signed = extract32(option, 2, 1);
1327 
1328     tcg_gen_ext_i64(tcg_out, tcg_in, extsize | (is_signed ? MO_SIGN : 0));
1329     tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1330 }
1331 
1332 static inline void gen_check_sp_alignment(DisasContext *s)
1333 {
1334     /* The AArch64 architecture mandates that (if enabled via PSTATE
1335      * or SCTLR bits) there is a check that SP is 16-aligned on every
1336      * SP-relative load or store (with an exception generated if it is not).
1337      * In line with general QEMU practice regarding misaligned accesses,
1338      * we omit these checks for the sake of guest program performance.
1339      * This function is provided as a hook so we can more easily add these
1340      * checks in future (possibly as a "favour catching guest program bugs
1341      * over speed" user selectable option).
1342      */
1343 }
1344 
1345 /*
1346  * This provides a simple table based table lookup decoder. It is
1347  * intended to be used when the relevant bits for decode are too
1348  * awkwardly placed and switch/if based logic would be confusing and
1349  * deeply nested. Since it's a linear search through the table, tables
1350  * should be kept small.
1351  *
1352  * It returns the first handler where insn & mask == pattern, or
1353  * NULL if there is no match.
1354  * The table is terminated by an empty mask (i.e. 0)
1355  */
1356 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1357                                                uint32_t insn)
1358 {
1359     const AArch64DecodeTable *tptr = table;
1360 
1361     while (tptr->mask) {
1362         if ((insn & tptr->mask) == tptr->pattern) {
1363             return tptr->disas_fn;
1364         }
1365         tptr++;
1366     }
1367     return NULL;
1368 }
1369 
1370 /*
1371  * The instruction disassembly implemented here matches
1372  * the instruction encoding classifications in chapter C4
1373  * of the ARM Architecture Reference Manual (DDI0487B_a);
1374  * classification names and decode diagrams here should generally
1375  * match up with those in the manual.
1376  */
1377 
1378 static bool trans_B(DisasContext *s, arg_i *a)
1379 {
1380     reset_btype(s);
1381     gen_goto_tb(s, 0, a->imm);
1382     return true;
1383 }
1384 
1385 static bool trans_BL(DisasContext *s, arg_i *a)
1386 {
1387     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1388     reset_btype(s);
1389     gen_goto_tb(s, 0, a->imm);
1390     return true;
1391 }
1392 
1393 
1394 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1395 {
1396     DisasLabel match;
1397     TCGv_i64 tcg_cmp;
1398 
1399     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1400     reset_btype(s);
1401 
1402     match = gen_disas_label(s);
1403     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1404                         tcg_cmp, 0, match.label);
1405     gen_goto_tb(s, 0, 4);
1406     set_disas_label(s, match);
1407     gen_goto_tb(s, 1, a->imm);
1408     return true;
1409 }
1410 
1411 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1412 {
1413     DisasLabel match;
1414     TCGv_i64 tcg_cmp;
1415 
1416     tcg_cmp = tcg_temp_new_i64();
1417     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1418 
1419     reset_btype(s);
1420 
1421     match = gen_disas_label(s);
1422     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1423                         tcg_cmp, 0, match.label);
1424     gen_goto_tb(s, 0, 4);
1425     set_disas_label(s, match);
1426     gen_goto_tb(s, 1, a->imm);
1427     return true;
1428 }
1429 
1430 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1431 {
1432     /* BC.cond is only present with FEAT_HBC */
1433     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
1434         return false;
1435     }
1436     reset_btype(s);
1437     if (a->cond < 0x0e) {
1438         /* genuinely conditional branches */
1439         DisasLabel match = gen_disas_label(s);
1440         arm_gen_test_cc(a->cond, match.label);
1441         gen_goto_tb(s, 0, 4);
1442         set_disas_label(s, match);
1443         gen_goto_tb(s, 1, a->imm);
1444     } else {
1445         /* 0xe and 0xf are both "always" conditions */
1446         gen_goto_tb(s, 0, a->imm);
1447     }
1448     return true;
1449 }
1450 
1451 static void set_btype_for_br(DisasContext *s, int rn)
1452 {
1453     if (dc_isar_feature(aa64_bti, s)) {
1454         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1455         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1456     }
1457 }
1458 
1459 static void set_btype_for_blr(DisasContext *s)
1460 {
1461     if (dc_isar_feature(aa64_bti, s)) {
1462         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1463         set_btype(s, 2);
1464     }
1465 }
1466 
1467 static bool trans_BR(DisasContext *s, arg_r *a)
1468 {
1469     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1470     set_btype_for_br(s, a->rn);
1471     s->base.is_jmp = DISAS_JUMP;
1472     return true;
1473 }
1474 
1475 static bool trans_BLR(DisasContext *s, arg_r *a)
1476 {
1477     TCGv_i64 dst = cpu_reg(s, a->rn);
1478     TCGv_i64 lr = cpu_reg(s, 30);
1479     if (dst == lr) {
1480         TCGv_i64 tmp = tcg_temp_new_i64();
1481         tcg_gen_mov_i64(tmp, dst);
1482         dst = tmp;
1483     }
1484     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1485     gen_a64_set_pc(s, dst);
1486     set_btype_for_blr(s);
1487     s->base.is_jmp = DISAS_JUMP;
1488     return true;
1489 }
1490 
1491 static bool trans_RET(DisasContext *s, arg_r *a)
1492 {
1493     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1494     s->base.is_jmp = DISAS_JUMP;
1495     return true;
1496 }
1497 
1498 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1499                                    TCGv_i64 modifier, bool use_key_a)
1500 {
1501     TCGv_i64 truedst;
1502     /*
1503      * Return the branch target for a BRAA/RETA/etc, which is either
1504      * just the destination dst, or that value with the pauth check
1505      * done and the code removed from the high bits.
1506      */
1507     if (!s->pauth_active) {
1508         return dst;
1509     }
1510 
1511     truedst = tcg_temp_new_i64();
1512     if (use_key_a) {
1513         gen_helper_autia_combined(truedst, tcg_env, dst, modifier);
1514     } else {
1515         gen_helper_autib_combined(truedst, tcg_env, dst, modifier);
1516     }
1517     return truedst;
1518 }
1519 
1520 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1521 {
1522     TCGv_i64 dst;
1523 
1524     if (!dc_isar_feature(aa64_pauth, s)) {
1525         return false;
1526     }
1527 
1528     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1529     gen_a64_set_pc(s, dst);
1530     set_btype_for_br(s, a->rn);
1531     s->base.is_jmp = DISAS_JUMP;
1532     return true;
1533 }
1534 
1535 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1536 {
1537     TCGv_i64 dst, lr;
1538 
1539     if (!dc_isar_feature(aa64_pauth, s)) {
1540         return false;
1541     }
1542 
1543     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1544     lr = cpu_reg(s, 30);
1545     if (dst == lr) {
1546         TCGv_i64 tmp = tcg_temp_new_i64();
1547         tcg_gen_mov_i64(tmp, dst);
1548         dst = tmp;
1549     }
1550     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1551     gen_a64_set_pc(s, dst);
1552     set_btype_for_blr(s);
1553     s->base.is_jmp = DISAS_JUMP;
1554     return true;
1555 }
1556 
1557 static bool trans_RETA(DisasContext *s, arg_reta *a)
1558 {
1559     TCGv_i64 dst;
1560 
1561     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1562     gen_a64_set_pc(s, dst);
1563     s->base.is_jmp = DISAS_JUMP;
1564     return true;
1565 }
1566 
1567 static bool trans_BRA(DisasContext *s, arg_bra *a)
1568 {
1569     TCGv_i64 dst;
1570 
1571     if (!dc_isar_feature(aa64_pauth, s)) {
1572         return false;
1573     }
1574     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1575     gen_a64_set_pc(s, dst);
1576     set_btype_for_br(s, a->rn);
1577     s->base.is_jmp = DISAS_JUMP;
1578     return true;
1579 }
1580 
1581 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1582 {
1583     TCGv_i64 dst, lr;
1584 
1585     if (!dc_isar_feature(aa64_pauth, s)) {
1586         return false;
1587     }
1588     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1589     lr = cpu_reg(s, 30);
1590     if (dst == lr) {
1591         TCGv_i64 tmp = tcg_temp_new_i64();
1592         tcg_gen_mov_i64(tmp, dst);
1593         dst = tmp;
1594     }
1595     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1596     gen_a64_set_pc(s, dst);
1597     set_btype_for_blr(s);
1598     s->base.is_jmp = DISAS_JUMP;
1599     return true;
1600 }
1601 
1602 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1603 {
1604     TCGv_i64 dst;
1605 
1606     if (s->current_el == 0) {
1607         return false;
1608     }
1609     if (s->trap_eret) {
1610         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(0), 2);
1611         return true;
1612     }
1613     dst = tcg_temp_new_i64();
1614     tcg_gen_ld_i64(dst, tcg_env,
1615                    offsetof(CPUARMState, elr_el[s->current_el]));
1616 
1617     translator_io_start(&s->base);
1618 
1619     gen_helper_exception_return(tcg_env, dst);
1620     /* Must exit loop to check un-masked IRQs */
1621     s->base.is_jmp = DISAS_EXIT;
1622     return true;
1623 }
1624 
1625 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1626 {
1627     TCGv_i64 dst;
1628 
1629     if (!dc_isar_feature(aa64_pauth, s)) {
1630         return false;
1631     }
1632     if (s->current_el == 0) {
1633         return false;
1634     }
1635     /* The FGT trap takes precedence over an auth trap. */
1636     if (s->trap_eret) {
1637         gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(a->m ? 3 : 2), 2);
1638         return true;
1639     }
1640     dst = tcg_temp_new_i64();
1641     tcg_gen_ld_i64(dst, tcg_env,
1642                    offsetof(CPUARMState, elr_el[s->current_el]));
1643 
1644     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1645 
1646     translator_io_start(&s->base);
1647 
1648     gen_helper_exception_return(tcg_env, dst);
1649     /* Must exit loop to check un-masked IRQs */
1650     s->base.is_jmp = DISAS_EXIT;
1651     return true;
1652 }
1653 
1654 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1655 {
1656     return true;
1657 }
1658 
1659 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1660 {
1661     /*
1662      * When running in MTTCG we don't generate jumps to the yield and
1663      * WFE helpers as it won't affect the scheduling of other vCPUs.
1664      * If we wanted to more completely model WFE/SEV so we don't busy
1665      * spin unnecessarily we would need to do something more involved.
1666      */
1667     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1668         s->base.is_jmp = DISAS_YIELD;
1669     }
1670     return true;
1671 }
1672 
1673 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1674 {
1675     s->base.is_jmp = DISAS_WFI;
1676     return true;
1677 }
1678 
1679 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1680 {
1681     /*
1682      * When running in MTTCG we don't generate jumps to the yield and
1683      * WFE helpers as it won't affect the scheduling of other vCPUs.
1684      * If we wanted to more completely model WFE/SEV so we don't busy
1685      * spin unnecessarily we would need to do something more involved.
1686      */
1687     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1688         s->base.is_jmp = DISAS_WFE;
1689     }
1690     return true;
1691 }
1692 
1693 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1694 {
1695     if (s->pauth_active) {
1696         gen_helper_xpaci(cpu_X[30], tcg_env, cpu_X[30]);
1697     }
1698     return true;
1699 }
1700 
1701 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1702 {
1703     if (s->pauth_active) {
1704         gen_helper_pacia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1705     }
1706     return true;
1707 }
1708 
1709 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1710 {
1711     if (s->pauth_active) {
1712         gen_helper_pacib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1713     }
1714     return true;
1715 }
1716 
1717 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1718 {
1719     if (s->pauth_active) {
1720         gen_helper_autia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1721     }
1722     return true;
1723 }
1724 
1725 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1726 {
1727     if (s->pauth_active) {
1728         gen_helper_autib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1729     }
1730     return true;
1731 }
1732 
1733 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1734 {
1735     /* Without RAS, we must implement this as NOP. */
1736     if (dc_isar_feature(aa64_ras, s)) {
1737         /*
1738          * QEMU does not have a source of physical SErrors,
1739          * so we are only concerned with virtual SErrors.
1740          * The pseudocode in the ARM for this case is
1741          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1742          *      AArch64.vESBOperation();
1743          * Most of the condition can be evaluated at translation time.
1744          * Test for EL2 present, and defer test for SEL2 to runtime.
1745          */
1746         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1747             gen_helper_vesb(tcg_env);
1748         }
1749     }
1750     return true;
1751 }
1752 
1753 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1754 {
1755     if (s->pauth_active) {
1756         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1757     }
1758     return true;
1759 }
1760 
1761 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1762 {
1763     if (s->pauth_active) {
1764         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1765     }
1766     return true;
1767 }
1768 
1769 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1770 {
1771     if (s->pauth_active) {
1772         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1773     }
1774     return true;
1775 }
1776 
1777 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1778 {
1779     if (s->pauth_active) {
1780         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1781     }
1782     return true;
1783 }
1784 
1785 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1786 {
1787     if (s->pauth_active) {
1788         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1789     }
1790     return true;
1791 }
1792 
1793 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1794 {
1795     if (s->pauth_active) {
1796         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1797     }
1798     return true;
1799 }
1800 
1801 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1802 {
1803     if (s->pauth_active) {
1804         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1805     }
1806     return true;
1807 }
1808 
1809 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1810 {
1811     if (s->pauth_active) {
1812         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1813     }
1814     return true;
1815 }
1816 
1817 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1818 {
1819     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1820     return true;
1821 }
1822 
1823 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1824 {
1825     /* We handle DSB and DMB the same way */
1826     TCGBar bar;
1827 
1828     switch (a->types) {
1829     case 1: /* MBReqTypes_Reads */
1830         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1831         break;
1832     case 2: /* MBReqTypes_Writes */
1833         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1834         break;
1835     default: /* MBReqTypes_All */
1836         bar = TCG_BAR_SC | TCG_MO_ALL;
1837         break;
1838     }
1839     tcg_gen_mb(bar);
1840     return true;
1841 }
1842 
1843 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1844 {
1845     /*
1846      * We need to break the TB after this insn to execute
1847      * self-modifying code correctly and also to take
1848      * any pending interrupts immediately.
1849      */
1850     reset_btype(s);
1851     gen_goto_tb(s, 0, 4);
1852     return true;
1853 }
1854 
1855 static bool trans_SB(DisasContext *s, arg_SB *a)
1856 {
1857     if (!dc_isar_feature(aa64_sb, s)) {
1858         return false;
1859     }
1860     /*
1861      * TODO: There is no speculation barrier opcode for TCG;
1862      * MB and end the TB instead.
1863      */
1864     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1865     gen_goto_tb(s, 0, 4);
1866     return true;
1867 }
1868 
1869 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1870 {
1871     if (!dc_isar_feature(aa64_condm_4, s)) {
1872         return false;
1873     }
1874     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1875     return true;
1876 }
1877 
1878 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1879 {
1880     TCGv_i32 z;
1881 
1882     if (!dc_isar_feature(aa64_condm_5, s)) {
1883         return false;
1884     }
1885 
1886     z = tcg_temp_new_i32();
1887 
1888     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1889 
1890     /*
1891      * (!C & !Z) << 31
1892      * (!(C | Z)) << 31
1893      * ~((C | Z) << 31)
1894      * ~-(C | Z)
1895      * (C | Z) - 1
1896      */
1897     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1898     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1899 
1900     /* !(Z & C) */
1901     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1902     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1903 
1904     /* (!C & Z) << 31 -> -(Z & ~C) */
1905     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1906     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1907 
1908     /* C | Z */
1909     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1910 
1911     return true;
1912 }
1913 
1914 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1915 {
1916     if (!dc_isar_feature(aa64_condm_5, s)) {
1917         return false;
1918     }
1919 
1920     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1921     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1922 
1923     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1924     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1925 
1926     tcg_gen_movi_i32(cpu_NF, 0);
1927     tcg_gen_movi_i32(cpu_VF, 0);
1928 
1929     return true;
1930 }
1931 
1932 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1933 {
1934     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1935         return false;
1936     }
1937     if (a->imm & 1) {
1938         set_pstate_bits(PSTATE_UAO);
1939     } else {
1940         clear_pstate_bits(PSTATE_UAO);
1941     }
1942     gen_rebuild_hflags(s);
1943     s->base.is_jmp = DISAS_TOO_MANY;
1944     return true;
1945 }
1946 
1947 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1948 {
1949     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1950         return false;
1951     }
1952     if (a->imm & 1) {
1953         set_pstate_bits(PSTATE_PAN);
1954     } else {
1955         clear_pstate_bits(PSTATE_PAN);
1956     }
1957     gen_rebuild_hflags(s);
1958     s->base.is_jmp = DISAS_TOO_MANY;
1959     return true;
1960 }
1961 
1962 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1963 {
1964     if (s->current_el == 0) {
1965         return false;
1966     }
1967     gen_helper_msr_i_spsel(tcg_env, tcg_constant_i32(a->imm & PSTATE_SP));
1968     s->base.is_jmp = DISAS_TOO_MANY;
1969     return true;
1970 }
1971 
1972 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
1973 {
1974     if (!dc_isar_feature(aa64_ssbs, s)) {
1975         return false;
1976     }
1977     if (a->imm & 1) {
1978         set_pstate_bits(PSTATE_SSBS);
1979     } else {
1980         clear_pstate_bits(PSTATE_SSBS);
1981     }
1982     /* Don't need to rebuild hflags since SSBS is a nop */
1983     s->base.is_jmp = DISAS_TOO_MANY;
1984     return true;
1985 }
1986 
1987 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
1988 {
1989     if (!dc_isar_feature(aa64_dit, s)) {
1990         return false;
1991     }
1992     if (a->imm & 1) {
1993         set_pstate_bits(PSTATE_DIT);
1994     } else {
1995         clear_pstate_bits(PSTATE_DIT);
1996     }
1997     /* There's no need to rebuild hflags because DIT is a nop */
1998     s->base.is_jmp = DISAS_TOO_MANY;
1999     return true;
2000 }
2001 
2002 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2003 {
2004     if (dc_isar_feature(aa64_mte, s)) {
2005         /* Full MTE is enabled -- set the TCO bit as directed. */
2006         if (a->imm & 1) {
2007             set_pstate_bits(PSTATE_TCO);
2008         } else {
2009             clear_pstate_bits(PSTATE_TCO);
2010         }
2011         gen_rebuild_hflags(s);
2012         /* Many factors, including TCO, go into MTE_ACTIVE. */
2013         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2014         return true;
2015     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2016         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2017         return true;
2018     } else {
2019         /* Insn not present */
2020         return false;
2021     }
2022 }
2023 
2024 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2025 {
2026     gen_helper_msr_i_daifset(tcg_env, tcg_constant_i32(a->imm));
2027     s->base.is_jmp = DISAS_TOO_MANY;
2028     return true;
2029 }
2030 
2031 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2032 {
2033     gen_helper_msr_i_daifclear(tcg_env, tcg_constant_i32(a->imm));
2034     /* Exit the cpu loop to re-evaluate pending IRQs. */
2035     s->base.is_jmp = DISAS_UPDATE_EXIT;
2036     return true;
2037 }
2038 
2039 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2040 {
2041     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2042         return false;
2043     }
2044     if (sme_access_check(s)) {
2045         int old = s->pstate_sm | (s->pstate_za << 1);
2046         int new = a->imm * 3;
2047 
2048         if ((old ^ new) & a->mask) {
2049             /* At least one bit changes. */
2050             gen_helper_set_svcr(tcg_env, tcg_constant_i32(new),
2051                                 tcg_constant_i32(a->mask));
2052             s->base.is_jmp = DISAS_TOO_MANY;
2053         }
2054     }
2055     return true;
2056 }
2057 
2058 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2059 {
2060     TCGv_i32 tmp = tcg_temp_new_i32();
2061     TCGv_i32 nzcv = tcg_temp_new_i32();
2062 
2063     /* build bit 31, N */
2064     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2065     /* build bit 30, Z */
2066     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2067     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2068     /* build bit 29, C */
2069     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2070     /* build bit 28, V */
2071     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2072     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2073     /* generate result */
2074     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2075 }
2076 
2077 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2078 {
2079     TCGv_i32 nzcv = tcg_temp_new_i32();
2080 
2081     /* take NZCV from R[t] */
2082     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2083 
2084     /* bit 31, N */
2085     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2086     /* bit 30, Z */
2087     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2088     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2089     /* bit 29, C */
2090     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2091     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2092     /* bit 28, V */
2093     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2094     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2095 }
2096 
2097 static void gen_sysreg_undef(DisasContext *s, bool isread,
2098                              uint8_t op0, uint8_t op1, uint8_t op2,
2099                              uint8_t crn, uint8_t crm, uint8_t rt)
2100 {
2101     /*
2102      * Generate code to emit an UNDEF with correct syndrome
2103      * information for a failed system register access.
2104      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2105      * but if FEAT_IDST is implemented then read accesses to registers
2106      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2107      * syndrome.
2108      */
2109     uint32_t syndrome;
2110 
2111     if (isread && dc_isar_feature(aa64_ids, s) &&
2112         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2113         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2114     } else {
2115         syndrome = syn_uncategorized();
2116     }
2117     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2118 }
2119 
2120 /* MRS - move from system register
2121  * MSR (register) - move to system register
2122  * SYS
2123  * SYSL
2124  * These are all essentially the same insn in 'read' and 'write'
2125  * versions, with varying op0 fields.
2126  */
2127 static void handle_sys(DisasContext *s, bool isread,
2128                        unsigned int op0, unsigned int op1, unsigned int op2,
2129                        unsigned int crn, unsigned int crm, unsigned int rt)
2130 {
2131     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2132                                       crn, crm, op0, op1, op2);
2133     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2134     bool need_exit_tb = false;
2135     bool nv_trap_to_el2 = false;
2136     bool skip_fp_access_checks = false;
2137     TCGv_ptr tcg_ri = NULL;
2138     TCGv_i64 tcg_rt;
2139     uint32_t syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2140 
2141     if (crn == 11 || crn == 15) {
2142         /*
2143          * Check for TIDCP trap, which must take precedence over
2144          * the UNDEF for "no such register" etc.
2145          */
2146         switch (s->current_el) {
2147         case 0:
2148             if (dc_isar_feature(aa64_tidcp1, s)) {
2149                 gen_helper_tidcp_el0(tcg_env, tcg_constant_i32(syndrome));
2150             }
2151             break;
2152         case 1:
2153             gen_helper_tidcp_el1(tcg_env, tcg_constant_i32(syndrome));
2154             break;
2155         }
2156     }
2157 
2158     if (!ri) {
2159         /* Unknown register; this might be a guest error or a QEMU
2160          * unimplemented feature.
2161          */
2162         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2163                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2164                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2165         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2166         return;
2167     }
2168 
2169     /* Check access permissions */
2170     if (!cp_access_ok(s->current_el, ri, isread)) {
2171         /*
2172          * FEAT_NV/NV2 handling does not do the usual FP access checks
2173          * for registers only accessible at EL2 (though it *does* do them
2174          * for registers accessible at EL1).
2175          */
2176         skip_fp_access_checks = true;
2177         if (s->nv && arm_cpreg_traps_in_nv(ri)) {
2178             /*
2179              * This register / instruction exists and is an EL2 register, so
2180              * we must trap to EL2 if accessed in nested virtualization EL1
2181              * instead of UNDEFing. We'll do that after the usual access checks.
2182              * (This makes a difference only for a couple of registers like
2183              * VSTTBR_EL2 where the "UNDEF if NonSecure" should take priority
2184              * over the trap-to-EL2. Most trapped-by-FEAT_NV registers have
2185              * an accessfn which does nothing when called from EL1, because
2186              * the trap-to-EL3 controls which would apply to that register
2187              * at EL2 don't take priority over the FEAT_NV trap-to-EL2.)
2188              */
2189             nv_trap_to_el2 = true;
2190         } else {
2191             gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2192             return;
2193         }
2194     }
2195 
2196     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2197         /* Emit code to perform further access permissions checks at
2198          * runtime; this may result in an exception.
2199          */
2200         gen_a64_update_pc(s, 0);
2201         tcg_ri = tcg_temp_new_ptr();
2202         gen_helper_access_check_cp_reg(tcg_ri, tcg_env,
2203                                        tcg_constant_i32(key),
2204                                        tcg_constant_i32(syndrome),
2205                                        tcg_constant_i32(isread));
2206     } else if (ri->type & ARM_CP_RAISES_EXC) {
2207         /*
2208          * The readfn or writefn might raise an exception;
2209          * synchronize the CPU state in case it does.
2210          */
2211         gen_a64_update_pc(s, 0);
2212     }
2213 
2214     if (!skip_fp_access_checks) {
2215         if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2216             return;
2217         } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2218             return;
2219         } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2220             return;
2221         }
2222     }
2223 
2224     if (nv_trap_to_el2) {
2225         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2226         return;
2227     }
2228 
2229     /* Handle special cases first */
2230     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2231     case 0:
2232         break;
2233     case ARM_CP_NOP:
2234         return;
2235     case ARM_CP_NZCV:
2236         tcg_rt = cpu_reg(s, rt);
2237         if (isread) {
2238             gen_get_nzcv(tcg_rt);
2239         } else {
2240             gen_set_nzcv(tcg_rt);
2241         }
2242         return;
2243     case ARM_CP_CURRENTEL:
2244     {
2245         /*
2246          * Reads as current EL value from pstate, which is
2247          * guaranteed to be constant by the tb flags.
2248          * For nested virt we should report EL2.
2249          */
2250         int el = s->nv ? 2 : s->current_el;
2251         tcg_rt = cpu_reg(s, rt);
2252         tcg_gen_movi_i64(tcg_rt, el << 2);
2253         return;
2254     }
2255     case ARM_CP_DC_ZVA:
2256         /* Writes clear the aligned block of memory which rt points into. */
2257         if (s->mte_active[0]) {
2258             int desc = 0;
2259 
2260             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2261             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2262             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2263 
2264             tcg_rt = tcg_temp_new_i64();
2265             gen_helper_mte_check_zva(tcg_rt, tcg_env,
2266                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2267         } else {
2268             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2269         }
2270         gen_helper_dc_zva(tcg_env, tcg_rt);
2271         return;
2272     case ARM_CP_DC_GVA:
2273         {
2274             TCGv_i64 clean_addr, tag;
2275 
2276             /*
2277              * DC_GVA, like DC_ZVA, requires that we supply the original
2278              * pointer for an invalid page.  Probe that address first.
2279              */
2280             tcg_rt = cpu_reg(s, rt);
2281             clean_addr = clean_data_tbi(s, tcg_rt);
2282             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2283 
2284             if (s->ata[0]) {
2285                 /* Extract the tag from the register to match STZGM.  */
2286                 tag = tcg_temp_new_i64();
2287                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2288                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2289             }
2290         }
2291         return;
2292     case ARM_CP_DC_GZVA:
2293         {
2294             TCGv_i64 clean_addr, tag;
2295 
2296             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2297             tcg_rt = cpu_reg(s, rt);
2298             clean_addr = clean_data_tbi(s, tcg_rt);
2299             gen_helper_dc_zva(tcg_env, clean_addr);
2300 
2301             if (s->ata[0]) {
2302                 /* Extract the tag from the register to match STZGM.  */
2303                 tag = tcg_temp_new_i64();
2304                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2305                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2306             }
2307         }
2308         return;
2309     default:
2310         g_assert_not_reached();
2311     }
2312 
2313     if (ri->type & ARM_CP_IO) {
2314         /* I/O operations must end the TB here (whether read or write) */
2315         need_exit_tb = translator_io_start(&s->base);
2316     }
2317 
2318     tcg_rt = cpu_reg(s, rt);
2319 
2320     if (isread) {
2321         if (ri->type & ARM_CP_CONST) {
2322             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2323         } else if (ri->readfn) {
2324             if (!tcg_ri) {
2325                 tcg_ri = gen_lookup_cp_reg(key);
2326             }
2327             gen_helper_get_cp_reg64(tcg_rt, tcg_env, tcg_ri);
2328         } else {
2329             tcg_gen_ld_i64(tcg_rt, tcg_env, ri->fieldoffset);
2330         }
2331     } else {
2332         if (ri->type & ARM_CP_CONST) {
2333             /* If not forbidden by access permissions, treat as WI */
2334             return;
2335         } else if (ri->writefn) {
2336             if (!tcg_ri) {
2337                 tcg_ri = gen_lookup_cp_reg(key);
2338             }
2339             gen_helper_set_cp_reg64(tcg_env, tcg_ri, tcg_rt);
2340         } else {
2341             tcg_gen_st_i64(tcg_rt, tcg_env, ri->fieldoffset);
2342         }
2343     }
2344 
2345     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2346         /*
2347          * A write to any coprocessor register that ends a TB
2348          * must rebuild the hflags for the next TB.
2349          */
2350         gen_rebuild_hflags(s);
2351         /*
2352          * We default to ending the TB on a coprocessor register write,
2353          * but allow this to be suppressed by the register definition
2354          * (usually only necessary to work around guest bugs).
2355          */
2356         need_exit_tb = true;
2357     }
2358     if (need_exit_tb) {
2359         s->base.is_jmp = DISAS_UPDATE_EXIT;
2360     }
2361 }
2362 
2363 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2364 {
2365     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2366     return true;
2367 }
2368 
2369 static bool trans_SVC(DisasContext *s, arg_i *a)
2370 {
2371     /*
2372      * For SVC, HVC and SMC we advance the single-step state
2373      * machine before taking the exception. This is architecturally
2374      * mandated, to ensure that single-stepping a system call
2375      * instruction works properly.
2376      */
2377     uint32_t syndrome = syn_aa64_svc(a->imm);
2378     if (s->fgt_svc) {
2379         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2380         return true;
2381     }
2382     gen_ss_advance(s);
2383     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2384     return true;
2385 }
2386 
2387 static bool trans_HVC(DisasContext *s, arg_i *a)
2388 {
2389     int target_el = s->current_el == 3 ? 3 : 2;
2390 
2391     if (s->current_el == 0) {
2392         unallocated_encoding(s);
2393         return true;
2394     }
2395     /*
2396      * The pre HVC helper handles cases when HVC gets trapped
2397      * as an undefined insn by runtime configuration.
2398      */
2399     gen_a64_update_pc(s, 0);
2400     gen_helper_pre_hvc(tcg_env);
2401     /* Architecture requires ss advance before we do the actual work */
2402     gen_ss_advance(s);
2403     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), target_el);
2404     return true;
2405 }
2406 
2407 static bool trans_SMC(DisasContext *s, arg_i *a)
2408 {
2409     if (s->current_el == 0) {
2410         unallocated_encoding(s);
2411         return true;
2412     }
2413     gen_a64_update_pc(s, 0);
2414     gen_helper_pre_smc(tcg_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2415     /* Architecture requires ss advance before we do the actual work */
2416     gen_ss_advance(s);
2417     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2418     return true;
2419 }
2420 
2421 static bool trans_BRK(DisasContext *s, arg_i *a)
2422 {
2423     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2424     return true;
2425 }
2426 
2427 static bool trans_HLT(DisasContext *s, arg_i *a)
2428 {
2429     /*
2430      * HLT. This has two purposes.
2431      * Architecturally, it is an external halting debug instruction.
2432      * Since QEMU doesn't implement external debug, we treat this as
2433      * it is required for halting debug disabled: it will UNDEF.
2434      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2435      */
2436     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2437         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2438     } else {
2439         unallocated_encoding(s);
2440     }
2441     return true;
2442 }
2443 
2444 /*
2445  * Load/Store exclusive instructions are implemented by remembering
2446  * the value/address loaded, and seeing if these are the same
2447  * when the store is performed. This is not actually the architecturally
2448  * mandated semantics, but it works for typical guest code sequences
2449  * and avoids having to monitor regular stores.
2450  *
2451  * The store exclusive uses the atomic cmpxchg primitives to avoid
2452  * races in multi-threaded linux-user and when MTTCG softmmu is
2453  * enabled.
2454  */
2455 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2456                                int size, bool is_pair)
2457 {
2458     int idx = get_mem_index(s);
2459     TCGv_i64 dirty_addr, clean_addr;
2460     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2461 
2462     s->is_ldex = true;
2463     dirty_addr = cpu_reg_sp(s, rn);
2464     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2465 
2466     g_assert(size <= 3);
2467     if (is_pair) {
2468         g_assert(size >= 2);
2469         if (size == 2) {
2470             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2471             if (s->be_data == MO_LE) {
2472                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2473                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2474             } else {
2475                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2476                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2477             }
2478         } else {
2479             TCGv_i128 t16 = tcg_temp_new_i128();
2480 
2481             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2482 
2483             if (s->be_data == MO_LE) {
2484                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2485                                       cpu_exclusive_high, t16);
2486             } else {
2487                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2488                                       cpu_exclusive_val, t16);
2489             }
2490             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2491             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2492         }
2493     } else {
2494         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2495         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2496     }
2497     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2498 }
2499 
2500 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2501                                 int rn, int size, int is_pair)
2502 {
2503     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2504      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2505      *     [addr] = {Rt};
2506      *     if (is_pair) {
2507      *         [addr + datasize] = {Rt2};
2508      *     }
2509      *     {Rd} = 0;
2510      * } else {
2511      *     {Rd} = 1;
2512      * }
2513      * env->exclusive_addr = -1;
2514      */
2515     TCGLabel *fail_label = gen_new_label();
2516     TCGLabel *done_label = gen_new_label();
2517     TCGv_i64 tmp, clean_addr;
2518     MemOp memop;
2519 
2520     /*
2521      * FIXME: We are out of spec here.  We have recorded only the address
2522      * from load_exclusive, not the entire range, and we assume that the
2523      * size of the access on both sides match.  The architecture allows the
2524      * store to be smaller than the load, so long as the stored bytes are
2525      * within the range recorded by the load.
2526      */
2527 
2528     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2529     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2530     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2531 
2532     /*
2533      * The write, and any associated faults, only happen if the virtual
2534      * and physical addresses pass the exclusive monitor check.  These
2535      * faults are exceedingly unlikely, because normally the guest uses
2536      * the exact same address register for the load_exclusive, and we
2537      * would have recognized these faults there.
2538      *
2539      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2540      * unaligned 4-byte write within the range of an aligned 8-byte load.
2541      * With LSE2, the store would need to cross a 16-byte boundary when the
2542      * load did not, which would mean the store is outside the range
2543      * recorded for the monitor, which would have failed a corrected monitor
2544      * check above.  For now, we assume no size change and retain the
2545      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2546      *
2547      * It is possible to trigger an MTE fault, by performing the load with
2548      * a virtual address with a valid tag and performing the store with the
2549      * same virtual address and a different invalid tag.
2550      */
2551     memop = size + is_pair;
2552     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2553         memop |= MO_ALIGN;
2554     }
2555     memop = finalize_memop(s, memop);
2556     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2557 
2558     tmp = tcg_temp_new_i64();
2559     if (is_pair) {
2560         if (size == 2) {
2561             if (s->be_data == MO_LE) {
2562                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2563             } else {
2564                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2565             }
2566             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2567                                        cpu_exclusive_val, tmp,
2568                                        get_mem_index(s), memop);
2569             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2570         } else {
2571             TCGv_i128 t16 = tcg_temp_new_i128();
2572             TCGv_i128 c16 = tcg_temp_new_i128();
2573             TCGv_i64 a, b;
2574 
2575             if (s->be_data == MO_LE) {
2576                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2577                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2578                                         cpu_exclusive_high);
2579             } else {
2580                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2581                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2582                                         cpu_exclusive_val);
2583             }
2584 
2585             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2586                                         get_mem_index(s), memop);
2587 
2588             a = tcg_temp_new_i64();
2589             b = tcg_temp_new_i64();
2590             if (s->be_data == MO_LE) {
2591                 tcg_gen_extr_i128_i64(a, b, t16);
2592             } else {
2593                 tcg_gen_extr_i128_i64(b, a, t16);
2594             }
2595 
2596             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2597             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2598             tcg_gen_or_i64(tmp, a, b);
2599 
2600             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2601         }
2602     } else {
2603         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2604                                    cpu_reg(s, rt), get_mem_index(s), memop);
2605         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2606     }
2607     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2608     tcg_gen_br(done_label);
2609 
2610     gen_set_label(fail_label);
2611     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2612     gen_set_label(done_label);
2613     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2614 }
2615 
2616 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2617                                  int rn, int size)
2618 {
2619     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2620     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2621     int memidx = get_mem_index(s);
2622     TCGv_i64 clean_addr;
2623     MemOp memop;
2624 
2625     if (rn == 31) {
2626         gen_check_sp_alignment(s);
2627     }
2628     memop = check_atomic_align(s, rn, size);
2629     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2630     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2631                                memidx, memop);
2632 }
2633 
2634 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2635                                       int rn, int size)
2636 {
2637     TCGv_i64 s1 = cpu_reg(s, rs);
2638     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2639     TCGv_i64 t1 = cpu_reg(s, rt);
2640     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2641     TCGv_i64 clean_addr;
2642     int memidx = get_mem_index(s);
2643     MemOp memop;
2644 
2645     if (rn == 31) {
2646         gen_check_sp_alignment(s);
2647     }
2648 
2649     /* This is a single atomic access, despite the "pair". */
2650     memop = check_atomic_align(s, rn, size + 1);
2651     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2652 
2653     if (size == 2) {
2654         TCGv_i64 cmp = tcg_temp_new_i64();
2655         TCGv_i64 val = tcg_temp_new_i64();
2656 
2657         if (s->be_data == MO_LE) {
2658             tcg_gen_concat32_i64(val, t1, t2);
2659             tcg_gen_concat32_i64(cmp, s1, s2);
2660         } else {
2661             tcg_gen_concat32_i64(val, t2, t1);
2662             tcg_gen_concat32_i64(cmp, s2, s1);
2663         }
2664 
2665         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2666 
2667         if (s->be_data == MO_LE) {
2668             tcg_gen_extr32_i64(s1, s2, cmp);
2669         } else {
2670             tcg_gen_extr32_i64(s2, s1, cmp);
2671         }
2672     } else {
2673         TCGv_i128 cmp = tcg_temp_new_i128();
2674         TCGv_i128 val = tcg_temp_new_i128();
2675 
2676         if (s->be_data == MO_LE) {
2677             tcg_gen_concat_i64_i128(val, t1, t2);
2678             tcg_gen_concat_i64_i128(cmp, s1, s2);
2679         } else {
2680             tcg_gen_concat_i64_i128(val, t2, t1);
2681             tcg_gen_concat_i64_i128(cmp, s2, s1);
2682         }
2683 
2684         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2685 
2686         if (s->be_data == MO_LE) {
2687             tcg_gen_extr_i128_i64(s1, s2, cmp);
2688         } else {
2689             tcg_gen_extr_i128_i64(s2, s1, cmp);
2690         }
2691     }
2692 }
2693 
2694 /*
2695  * Compute the ISS.SF bit for syndrome information if an exception
2696  * is taken on a load or store. This indicates whether the instruction
2697  * is accessing a 32-bit or 64-bit register. This logic is derived
2698  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2699  */
2700 static bool ldst_iss_sf(int size, bool sign, bool ext)
2701 {
2702 
2703     if (sign) {
2704         /*
2705          * Signed loads are 64 bit results if we are not going to
2706          * do a zero-extend from 32 to 64 after the load.
2707          * (For a store, sign and ext are always false.)
2708          */
2709         return !ext;
2710     } else {
2711         /* Unsigned loads/stores work at the specified size */
2712         return size == MO_64;
2713     }
2714 }
2715 
2716 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2717 {
2718     if (a->rn == 31) {
2719         gen_check_sp_alignment(s);
2720     }
2721     if (a->lasr) {
2722         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2723     }
2724     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2725     return true;
2726 }
2727 
2728 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2729 {
2730     if (a->rn == 31) {
2731         gen_check_sp_alignment(s);
2732     }
2733     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2734     if (a->lasr) {
2735         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2736     }
2737     return true;
2738 }
2739 
2740 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2741 {
2742     TCGv_i64 clean_addr;
2743     MemOp memop;
2744     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2745 
2746     /*
2747      * StoreLORelease is the same as Store-Release for QEMU, but
2748      * needs the feature-test.
2749      */
2750     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2751         return false;
2752     }
2753     /* Generate ISS for non-exclusive accesses including LASR.  */
2754     if (a->rn == 31) {
2755         gen_check_sp_alignment(s);
2756     }
2757     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2758     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2759     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2760                                 true, a->rn != 31, memop);
2761     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2762               iss_sf, a->lasr);
2763     return true;
2764 }
2765 
2766 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2767 {
2768     TCGv_i64 clean_addr;
2769     MemOp memop;
2770     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2771 
2772     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2773     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2774         return false;
2775     }
2776     /* Generate ISS for non-exclusive accesses including LASR.  */
2777     if (a->rn == 31) {
2778         gen_check_sp_alignment(s);
2779     }
2780     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2781     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2782                                 false, a->rn != 31, memop);
2783     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2784               a->rt, iss_sf, a->lasr);
2785     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2786     return true;
2787 }
2788 
2789 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2790 {
2791     if (a->rn == 31) {
2792         gen_check_sp_alignment(s);
2793     }
2794     if (a->lasr) {
2795         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2796     }
2797     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2798     return true;
2799 }
2800 
2801 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2802 {
2803     if (a->rn == 31) {
2804         gen_check_sp_alignment(s);
2805     }
2806     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2807     if (a->lasr) {
2808         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2809     }
2810     return true;
2811 }
2812 
2813 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2814 {
2815     if (!dc_isar_feature(aa64_atomics, s)) {
2816         return false;
2817     }
2818     if (((a->rt | a->rs) & 1) != 0) {
2819         return false;
2820     }
2821 
2822     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2823     return true;
2824 }
2825 
2826 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2827 {
2828     if (!dc_isar_feature(aa64_atomics, s)) {
2829         return false;
2830     }
2831     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2832     return true;
2833 }
2834 
2835 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2836 {
2837     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2838     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
2839     TCGv_i64 clean_addr = tcg_temp_new_i64();
2840     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
2841 
2842     gen_pc_plus_diff(s, clean_addr, a->imm);
2843     do_gpr_ld(s, tcg_rt, clean_addr, memop,
2844               false, true, a->rt, iss_sf, false);
2845     return true;
2846 }
2847 
2848 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
2849 {
2850     /* Load register (literal), vector version */
2851     TCGv_i64 clean_addr;
2852     MemOp memop;
2853 
2854     if (!fp_access_check(s)) {
2855         return true;
2856     }
2857     memop = finalize_memop_asimd(s, a->sz);
2858     clean_addr = tcg_temp_new_i64();
2859     gen_pc_plus_diff(s, clean_addr, a->imm);
2860     do_fp_ld(s, a->rt, clean_addr, memop);
2861     return true;
2862 }
2863 
2864 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
2865                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
2866                                  uint64_t offset, bool is_store, MemOp mop)
2867 {
2868     if (a->rn == 31) {
2869         gen_check_sp_alignment(s);
2870     }
2871 
2872     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
2873     if (!a->p) {
2874         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
2875     }
2876 
2877     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
2878                                  (a->w || a->rn != 31), 2 << a->sz, mop);
2879 }
2880 
2881 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
2882                                   TCGv_i64 dirty_addr, uint64_t offset)
2883 {
2884     if (a->w) {
2885         if (a->p) {
2886             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2887         }
2888         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
2889     }
2890 }
2891 
2892 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
2893 {
2894     uint64_t offset = a->imm << a->sz;
2895     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2896     MemOp mop = finalize_memop(s, a->sz);
2897 
2898     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2899     tcg_rt = cpu_reg(s, a->rt);
2900     tcg_rt2 = cpu_reg(s, a->rt2);
2901     /*
2902      * We built mop above for the single logical access -- rebuild it
2903      * now for the paired operation.
2904      *
2905      * With LSE2, non-sign-extending pairs are treated atomically if
2906      * aligned, and if unaligned one of the pair will be completely
2907      * within a 16-byte block and that element will be atomic.
2908      * Otherwise each element is separately atomic.
2909      * In all cases, issue one operation with the correct atomicity.
2910      */
2911     mop = a->sz + 1;
2912     if (s->align_mem) {
2913         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2914     }
2915     mop = finalize_memop_pair(s, mop);
2916     if (a->sz == 2) {
2917         TCGv_i64 tmp = tcg_temp_new_i64();
2918 
2919         if (s->be_data == MO_LE) {
2920             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2921         } else {
2922             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2923         }
2924         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2925     } else {
2926         TCGv_i128 tmp = tcg_temp_new_i128();
2927 
2928         if (s->be_data == MO_LE) {
2929             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
2930         } else {
2931             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
2932         }
2933         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
2934     }
2935     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2936     return true;
2937 }
2938 
2939 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
2940 {
2941     uint64_t offset = a->imm << a->sz;
2942     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2943     MemOp mop = finalize_memop(s, a->sz);
2944 
2945     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2946     tcg_rt = cpu_reg(s, a->rt);
2947     tcg_rt2 = cpu_reg(s, a->rt2);
2948 
2949     /*
2950      * We built mop above for the single logical access -- rebuild it
2951      * now for the paired operation.
2952      *
2953      * With LSE2, non-sign-extending pairs are treated atomically if
2954      * aligned, and if unaligned one of the pair will be completely
2955      * within a 16-byte block and that element will be atomic.
2956      * Otherwise each element is separately atomic.
2957      * In all cases, issue one operation with the correct atomicity.
2958      *
2959      * This treats sign-extending loads like zero-extending loads,
2960      * since that reuses the most code below.
2961      */
2962     mop = a->sz + 1;
2963     if (s->align_mem) {
2964         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2965     }
2966     mop = finalize_memop_pair(s, mop);
2967     if (a->sz == 2) {
2968         int o2 = s->be_data == MO_LE ? 32 : 0;
2969         int o1 = o2 ^ 32;
2970 
2971         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2972         if (a->sign) {
2973             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2974             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2975         } else {
2976             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2977             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2978         }
2979     } else {
2980         TCGv_i128 tmp = tcg_temp_new_i128();
2981 
2982         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2983         if (s->be_data == MO_LE) {
2984             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2985         } else {
2986             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2987         }
2988     }
2989     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2990     return true;
2991 }
2992 
2993 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
2994 {
2995     uint64_t offset = a->imm << a->sz;
2996     TCGv_i64 clean_addr, dirty_addr;
2997     MemOp mop;
2998 
2999     if (!fp_access_check(s)) {
3000         return true;
3001     }
3002 
3003     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3004     mop = finalize_memop_asimd(s, a->sz);
3005     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3006     do_fp_st(s, a->rt, clean_addr, mop);
3007     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3008     do_fp_st(s, a->rt2, clean_addr, mop);
3009     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3010     return true;
3011 }
3012 
3013 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
3014 {
3015     uint64_t offset = a->imm << a->sz;
3016     TCGv_i64 clean_addr, dirty_addr;
3017     MemOp mop;
3018 
3019     if (!fp_access_check(s)) {
3020         return true;
3021     }
3022 
3023     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3024     mop = finalize_memop_asimd(s, a->sz);
3025     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3026     do_fp_ld(s, a->rt, clean_addr, mop);
3027     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3028     do_fp_ld(s, a->rt2, clean_addr, mop);
3029     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3030     return true;
3031 }
3032 
3033 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3034 {
3035     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3036     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3037     MemOp mop;
3038     TCGv_i128 tmp;
3039 
3040     /* STGP only comes in one size. */
3041     tcg_debug_assert(a->sz == MO_64);
3042 
3043     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3044         return false;
3045     }
3046 
3047     if (a->rn == 31) {
3048         gen_check_sp_alignment(s);
3049     }
3050 
3051     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3052     if (!a->p) {
3053         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3054     }
3055 
3056     clean_addr = clean_data_tbi(s, dirty_addr);
3057     tcg_rt = cpu_reg(s, a->rt);
3058     tcg_rt2 = cpu_reg(s, a->rt2);
3059 
3060     /*
3061      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3062      * and one tag operation.  We implement it as one single aligned 16-byte
3063      * memory operation for convenience.  Note that the alignment ensures
3064      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3065      */
3066     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3067 
3068     tmp = tcg_temp_new_i128();
3069     if (s->be_data == MO_LE) {
3070         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3071     } else {
3072         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3073     }
3074     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3075 
3076     /* Perform the tag store, if tag access enabled. */
3077     if (s->ata[0]) {
3078         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3079             gen_helper_stg_parallel(tcg_env, dirty_addr, dirty_addr);
3080         } else {
3081             gen_helper_stg(tcg_env, dirty_addr, dirty_addr);
3082         }
3083     }
3084 
3085     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3086     return true;
3087 }
3088 
3089 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3090                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3091                                  uint64_t offset, bool is_store, MemOp mop)
3092 {
3093     int memidx;
3094 
3095     if (a->rn == 31) {
3096         gen_check_sp_alignment(s);
3097     }
3098 
3099     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3100     if (!a->p) {
3101         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3102     }
3103     memidx = get_a64_user_mem_index(s, a->unpriv);
3104     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3105                                         a->w || a->rn != 31,
3106                                         mop, a->unpriv, memidx);
3107 }
3108 
3109 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3110                                   TCGv_i64 dirty_addr, uint64_t offset)
3111 {
3112     if (a->w) {
3113         if (a->p) {
3114             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3115         }
3116         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3117     }
3118 }
3119 
3120 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3121 {
3122     bool iss_sf, iss_valid = !a->w;
3123     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3124     int memidx = get_a64_user_mem_index(s, a->unpriv);
3125     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3126 
3127     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3128 
3129     tcg_rt = cpu_reg(s, a->rt);
3130     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3131 
3132     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3133                      iss_valid, a->rt, iss_sf, false);
3134     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3135     return true;
3136 }
3137 
3138 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3139 {
3140     bool iss_sf, iss_valid = !a->w;
3141     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3142     int memidx = get_a64_user_mem_index(s, a->unpriv);
3143     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3144 
3145     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3146 
3147     tcg_rt = cpu_reg(s, a->rt);
3148     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3149 
3150     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3151                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3152     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3153     return true;
3154 }
3155 
3156 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3157 {
3158     TCGv_i64 clean_addr, dirty_addr;
3159     MemOp mop;
3160 
3161     if (!fp_access_check(s)) {
3162         return true;
3163     }
3164     mop = finalize_memop_asimd(s, a->sz);
3165     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3166     do_fp_st(s, a->rt, clean_addr, mop);
3167     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3168     return true;
3169 }
3170 
3171 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3172 {
3173     TCGv_i64 clean_addr, dirty_addr;
3174     MemOp mop;
3175 
3176     if (!fp_access_check(s)) {
3177         return true;
3178     }
3179     mop = finalize_memop_asimd(s, a->sz);
3180     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3181     do_fp_ld(s, a->rt, clean_addr, mop);
3182     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3183     return true;
3184 }
3185 
3186 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3187                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3188                              bool is_store, MemOp memop)
3189 {
3190     TCGv_i64 tcg_rm;
3191 
3192     if (a->rn == 31) {
3193         gen_check_sp_alignment(s);
3194     }
3195     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3196 
3197     tcg_rm = read_cpu_reg(s, a->rm, 1);
3198     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3199 
3200     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3201     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3202 }
3203 
3204 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3205 {
3206     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3207     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3208     MemOp memop;
3209 
3210     if (extract32(a->opt, 1, 1) == 0) {
3211         return false;
3212     }
3213 
3214     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3215     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3216     tcg_rt = cpu_reg(s, a->rt);
3217     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3218               a->ext, true, a->rt, iss_sf, false);
3219     return true;
3220 }
3221 
3222 static bool trans_STR(DisasContext *s, arg_ldst *a)
3223 {
3224     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3225     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3226     MemOp memop;
3227 
3228     if (extract32(a->opt, 1, 1) == 0) {
3229         return false;
3230     }
3231 
3232     memop = finalize_memop(s, a->sz);
3233     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3234     tcg_rt = cpu_reg(s, a->rt);
3235     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3236     return true;
3237 }
3238 
3239 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3240 {
3241     TCGv_i64 clean_addr, dirty_addr;
3242     MemOp memop;
3243 
3244     if (extract32(a->opt, 1, 1) == 0) {
3245         return false;
3246     }
3247 
3248     if (!fp_access_check(s)) {
3249         return true;
3250     }
3251 
3252     memop = finalize_memop_asimd(s, a->sz);
3253     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3254     do_fp_ld(s, a->rt, clean_addr, memop);
3255     return true;
3256 }
3257 
3258 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3259 {
3260     TCGv_i64 clean_addr, dirty_addr;
3261     MemOp memop;
3262 
3263     if (extract32(a->opt, 1, 1) == 0) {
3264         return false;
3265     }
3266 
3267     if (!fp_access_check(s)) {
3268         return true;
3269     }
3270 
3271     memop = finalize_memop_asimd(s, a->sz);
3272     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3273     do_fp_st(s, a->rt, clean_addr, memop);
3274     return true;
3275 }
3276 
3277 
3278 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3279                          int sign, bool invert)
3280 {
3281     MemOp mop = a->sz | sign;
3282     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3283 
3284     if (a->rn == 31) {
3285         gen_check_sp_alignment(s);
3286     }
3287     mop = check_atomic_align(s, a->rn, mop);
3288     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3289                                 a->rn != 31, mop);
3290     tcg_rs = read_cpu_reg(s, a->rs, true);
3291     tcg_rt = cpu_reg(s, a->rt);
3292     if (invert) {
3293         tcg_gen_not_i64(tcg_rs, tcg_rs);
3294     }
3295     /*
3296      * The tcg atomic primitives are all full barriers.  Therefore we
3297      * can ignore the Acquire and Release bits of this instruction.
3298      */
3299     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3300 
3301     if (mop & MO_SIGN) {
3302         switch (a->sz) {
3303         case MO_8:
3304             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3305             break;
3306         case MO_16:
3307             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3308             break;
3309         case MO_32:
3310             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3311             break;
3312         case MO_64:
3313             break;
3314         default:
3315             g_assert_not_reached();
3316         }
3317     }
3318     return true;
3319 }
3320 
3321 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3322 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3323 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3324 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3325 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3326 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3327 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3328 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3329 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3330 
3331 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3332 {
3333     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3334     TCGv_i64 clean_addr;
3335     MemOp mop;
3336 
3337     if (!dc_isar_feature(aa64_atomics, s) ||
3338         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3339         return false;
3340     }
3341     if (a->rn == 31) {
3342         gen_check_sp_alignment(s);
3343     }
3344     mop = check_atomic_align(s, a->rn, a->sz);
3345     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3346                                 a->rn != 31, mop);
3347     /*
3348      * LDAPR* are a special case because they are a simple load, not a
3349      * fetch-and-do-something op.
3350      * The architectural consistency requirements here are weaker than
3351      * full load-acquire (we only need "load-acquire processor consistent"),
3352      * but we choose to implement them as full LDAQ.
3353      */
3354     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3355               true, a->rt, iss_sf, true);
3356     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3357     return true;
3358 }
3359 
3360 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3361 {
3362     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3363     MemOp memop;
3364 
3365     /* Load with pointer authentication */
3366     if (!dc_isar_feature(aa64_pauth, s)) {
3367         return false;
3368     }
3369 
3370     if (a->rn == 31) {
3371         gen_check_sp_alignment(s);
3372     }
3373     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3374 
3375     if (s->pauth_active) {
3376         if (!a->m) {
3377             gen_helper_autda_combined(dirty_addr, tcg_env, dirty_addr,
3378                                       tcg_constant_i64(0));
3379         } else {
3380             gen_helper_autdb_combined(dirty_addr, tcg_env, dirty_addr,
3381                                       tcg_constant_i64(0));
3382         }
3383     }
3384 
3385     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3386 
3387     memop = finalize_memop(s, MO_64);
3388 
3389     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3390     clean_addr = gen_mte_check1(s, dirty_addr, false,
3391                                 a->w || a->rn != 31, memop);
3392 
3393     tcg_rt = cpu_reg(s, a->rt);
3394     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3395               /* extend */ false, /* iss_valid */ !a->w,
3396               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3397 
3398     if (a->w) {
3399         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3400     }
3401     return true;
3402 }
3403 
3404 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3405 {
3406     TCGv_i64 clean_addr, dirty_addr;
3407     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3408     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3409 
3410     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3411         return false;
3412     }
3413 
3414     if (a->rn == 31) {
3415         gen_check_sp_alignment(s);
3416     }
3417 
3418     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3419     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3420     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3421     clean_addr = clean_data_tbi(s, dirty_addr);
3422 
3423     /*
3424      * Load-AcquirePC semantics; we implement as the slightly more
3425      * restrictive Load-Acquire.
3426      */
3427     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3428               a->rt, iss_sf, true);
3429     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3430     return true;
3431 }
3432 
3433 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3434 {
3435     TCGv_i64 clean_addr, dirty_addr;
3436     MemOp mop = a->sz;
3437     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3438 
3439     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3440         return false;
3441     }
3442 
3443     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3444 
3445     if (a->rn == 31) {
3446         gen_check_sp_alignment(s);
3447     }
3448 
3449     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3450     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3451     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3452     clean_addr = clean_data_tbi(s, dirty_addr);
3453 
3454     /* Store-Release semantics */
3455     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3456     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3457     return true;
3458 }
3459 
3460 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3461 {
3462     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3463     MemOp endian, align, mop;
3464 
3465     int total;    /* total bytes */
3466     int elements; /* elements per vector */
3467     int r;
3468     int size = a->sz;
3469 
3470     if (!a->p && a->rm != 0) {
3471         /* For non-postindexed accesses the Rm field must be 0 */
3472         return false;
3473     }
3474     if (size == 3 && !a->q && a->selem != 1) {
3475         return false;
3476     }
3477     if (!fp_access_check(s)) {
3478         return true;
3479     }
3480 
3481     if (a->rn == 31) {
3482         gen_check_sp_alignment(s);
3483     }
3484 
3485     /* For our purposes, bytes are always little-endian.  */
3486     endian = s->be_data;
3487     if (size == 0) {
3488         endian = MO_LE;
3489     }
3490 
3491     total = a->rpt * a->selem * (a->q ? 16 : 8);
3492     tcg_rn = cpu_reg_sp(s, a->rn);
3493 
3494     /*
3495      * Issue the MTE check vs the logical repeat count, before we
3496      * promote consecutive little-endian elements below.
3497      */
3498     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3499                                 finalize_memop_asimd(s, size));
3500 
3501     /*
3502      * Consecutive little-endian elements from a single register
3503      * can be promoted to a larger little-endian operation.
3504      */
3505     align = MO_ALIGN;
3506     if (a->selem == 1 && endian == MO_LE) {
3507         align = pow2_align(size);
3508         size = 3;
3509     }
3510     if (!s->align_mem) {
3511         align = 0;
3512     }
3513     mop = endian | size | align;
3514 
3515     elements = (a->q ? 16 : 8) >> size;
3516     tcg_ebytes = tcg_constant_i64(1 << size);
3517     for (r = 0; r < a->rpt; r++) {
3518         int e;
3519         for (e = 0; e < elements; e++) {
3520             int xs;
3521             for (xs = 0; xs < a->selem; xs++) {
3522                 int tt = (a->rt + r + xs) % 32;
3523                 do_vec_ld(s, tt, e, clean_addr, mop);
3524                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3525             }
3526         }
3527     }
3528 
3529     /*
3530      * For non-quad operations, setting a slice of the low 64 bits of
3531      * the register clears the high 64 bits (in the ARM ARM pseudocode
3532      * this is implicit in the fact that 'rval' is a 64 bit wide
3533      * variable).  For quad operations, we might still need to zero
3534      * the high bits of SVE.
3535      */
3536     for (r = 0; r < a->rpt * a->selem; r++) {
3537         int tt = (a->rt + r) % 32;
3538         clear_vec_high(s, a->q, tt);
3539     }
3540 
3541     if (a->p) {
3542         if (a->rm == 31) {
3543             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3544         } else {
3545             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3546         }
3547     }
3548     return true;
3549 }
3550 
3551 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3552 {
3553     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3554     MemOp endian, align, mop;
3555 
3556     int total;    /* total bytes */
3557     int elements; /* elements per vector */
3558     int r;
3559     int size = a->sz;
3560 
3561     if (!a->p && a->rm != 0) {
3562         /* For non-postindexed accesses the Rm field must be 0 */
3563         return false;
3564     }
3565     if (size == 3 && !a->q && a->selem != 1) {
3566         return false;
3567     }
3568     if (!fp_access_check(s)) {
3569         return true;
3570     }
3571 
3572     if (a->rn == 31) {
3573         gen_check_sp_alignment(s);
3574     }
3575 
3576     /* For our purposes, bytes are always little-endian.  */
3577     endian = s->be_data;
3578     if (size == 0) {
3579         endian = MO_LE;
3580     }
3581 
3582     total = a->rpt * a->selem * (a->q ? 16 : 8);
3583     tcg_rn = cpu_reg_sp(s, a->rn);
3584 
3585     /*
3586      * Issue the MTE check vs the logical repeat count, before we
3587      * promote consecutive little-endian elements below.
3588      */
3589     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3590                                 finalize_memop_asimd(s, size));
3591 
3592     /*
3593      * Consecutive little-endian elements from a single register
3594      * can be promoted to a larger little-endian operation.
3595      */
3596     align = MO_ALIGN;
3597     if (a->selem == 1 && endian == MO_LE) {
3598         align = pow2_align(size);
3599         size = 3;
3600     }
3601     if (!s->align_mem) {
3602         align = 0;
3603     }
3604     mop = endian | size | align;
3605 
3606     elements = (a->q ? 16 : 8) >> size;
3607     tcg_ebytes = tcg_constant_i64(1 << size);
3608     for (r = 0; r < a->rpt; r++) {
3609         int e;
3610         for (e = 0; e < elements; e++) {
3611             int xs;
3612             for (xs = 0; xs < a->selem; xs++) {
3613                 int tt = (a->rt + r + xs) % 32;
3614                 do_vec_st(s, tt, e, clean_addr, mop);
3615                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3616             }
3617         }
3618     }
3619 
3620     if (a->p) {
3621         if (a->rm == 31) {
3622             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3623         } else {
3624             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3625         }
3626     }
3627     return true;
3628 }
3629 
3630 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3631 {
3632     int xs, total, rt;
3633     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3634     MemOp mop;
3635 
3636     if (!a->p && a->rm != 0) {
3637         return false;
3638     }
3639     if (!fp_access_check(s)) {
3640         return true;
3641     }
3642 
3643     if (a->rn == 31) {
3644         gen_check_sp_alignment(s);
3645     }
3646 
3647     total = a->selem << a->scale;
3648     tcg_rn = cpu_reg_sp(s, a->rn);
3649 
3650     mop = finalize_memop_asimd(s, a->scale);
3651     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3652                                 total, mop);
3653 
3654     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3655     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3656         do_vec_st(s, rt, a->index, clean_addr, mop);
3657         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3658     }
3659 
3660     if (a->p) {
3661         if (a->rm == 31) {
3662             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3663         } else {
3664             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3665         }
3666     }
3667     return true;
3668 }
3669 
3670 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3671 {
3672     int xs, total, rt;
3673     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3674     MemOp mop;
3675 
3676     if (!a->p && a->rm != 0) {
3677         return false;
3678     }
3679     if (!fp_access_check(s)) {
3680         return true;
3681     }
3682 
3683     if (a->rn == 31) {
3684         gen_check_sp_alignment(s);
3685     }
3686 
3687     total = a->selem << a->scale;
3688     tcg_rn = cpu_reg_sp(s, a->rn);
3689 
3690     mop = finalize_memop_asimd(s, a->scale);
3691     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3692                                 total, mop);
3693 
3694     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3695     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3696         do_vec_ld(s, rt, a->index, clean_addr, mop);
3697         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3698     }
3699 
3700     if (a->p) {
3701         if (a->rm == 31) {
3702             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3703         } else {
3704             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3705         }
3706     }
3707     return true;
3708 }
3709 
3710 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3711 {
3712     int xs, total, rt;
3713     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3714     MemOp mop;
3715 
3716     if (!a->p && a->rm != 0) {
3717         return false;
3718     }
3719     if (!fp_access_check(s)) {
3720         return true;
3721     }
3722 
3723     if (a->rn == 31) {
3724         gen_check_sp_alignment(s);
3725     }
3726 
3727     total = a->selem << a->scale;
3728     tcg_rn = cpu_reg_sp(s, a->rn);
3729 
3730     mop = finalize_memop_asimd(s, a->scale);
3731     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3732                                 total, mop);
3733 
3734     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3735     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3736         /* Load and replicate to all elements */
3737         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3738 
3739         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3740         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3741                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3742         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3743     }
3744 
3745     if (a->p) {
3746         if (a->rm == 31) {
3747             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3748         } else {
3749             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3750         }
3751     }
3752     return true;
3753 }
3754 
3755 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3756 {
3757     TCGv_i64 addr, clean_addr, tcg_rt;
3758     int size = 4 << s->dcz_blocksize;
3759 
3760     if (!dc_isar_feature(aa64_mte, s)) {
3761         return false;
3762     }
3763     if (s->current_el == 0) {
3764         return false;
3765     }
3766 
3767     if (a->rn == 31) {
3768         gen_check_sp_alignment(s);
3769     }
3770 
3771     addr = read_cpu_reg_sp(s, a->rn, true);
3772     tcg_gen_addi_i64(addr, addr, a->imm);
3773     tcg_rt = cpu_reg(s, a->rt);
3774 
3775     if (s->ata[0]) {
3776         gen_helper_stzgm_tags(tcg_env, addr, tcg_rt);
3777     }
3778     /*
3779      * The non-tags portion of STZGM is mostly like DC_ZVA,
3780      * except the alignment happens before the access.
3781      */
3782     clean_addr = clean_data_tbi(s, addr);
3783     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3784     gen_helper_dc_zva(tcg_env, clean_addr);
3785     return true;
3786 }
3787 
3788 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3789 {
3790     TCGv_i64 addr, clean_addr, tcg_rt;
3791 
3792     if (!dc_isar_feature(aa64_mte, s)) {
3793         return false;
3794     }
3795     if (s->current_el == 0) {
3796         return false;
3797     }
3798 
3799     if (a->rn == 31) {
3800         gen_check_sp_alignment(s);
3801     }
3802 
3803     addr = read_cpu_reg_sp(s, a->rn, true);
3804     tcg_gen_addi_i64(addr, addr, a->imm);
3805     tcg_rt = cpu_reg(s, a->rt);
3806 
3807     if (s->ata[0]) {
3808         gen_helper_stgm(tcg_env, addr, tcg_rt);
3809     } else {
3810         MMUAccessType acc = MMU_DATA_STORE;
3811         int size = 4 << s->gm_blocksize;
3812 
3813         clean_addr = clean_data_tbi(s, addr);
3814         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3815         gen_probe_access(s, clean_addr, acc, size);
3816     }
3817     return true;
3818 }
3819 
3820 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3821 {
3822     TCGv_i64 addr, clean_addr, tcg_rt;
3823 
3824     if (!dc_isar_feature(aa64_mte, s)) {
3825         return false;
3826     }
3827     if (s->current_el == 0) {
3828         return false;
3829     }
3830 
3831     if (a->rn == 31) {
3832         gen_check_sp_alignment(s);
3833     }
3834 
3835     addr = read_cpu_reg_sp(s, a->rn, true);
3836     tcg_gen_addi_i64(addr, addr, a->imm);
3837     tcg_rt = cpu_reg(s, a->rt);
3838 
3839     if (s->ata[0]) {
3840         gen_helper_ldgm(tcg_rt, tcg_env, addr);
3841     } else {
3842         MMUAccessType acc = MMU_DATA_LOAD;
3843         int size = 4 << s->gm_blocksize;
3844 
3845         clean_addr = clean_data_tbi(s, addr);
3846         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3847         gen_probe_access(s, clean_addr, acc, size);
3848         /* The result tags are zeros.  */
3849         tcg_gen_movi_i64(tcg_rt, 0);
3850     }
3851     return true;
3852 }
3853 
3854 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
3855 {
3856     TCGv_i64 addr, clean_addr, tcg_rt;
3857 
3858     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3859         return false;
3860     }
3861 
3862     if (a->rn == 31) {
3863         gen_check_sp_alignment(s);
3864     }
3865 
3866     addr = read_cpu_reg_sp(s, a->rn, true);
3867     if (!a->p) {
3868         /* pre-index or signed offset */
3869         tcg_gen_addi_i64(addr, addr, a->imm);
3870     }
3871 
3872     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
3873     tcg_rt = cpu_reg(s, a->rt);
3874     if (s->ata[0]) {
3875         gen_helper_ldg(tcg_rt, tcg_env, addr, tcg_rt);
3876     } else {
3877         /*
3878          * Tag access disabled: we must check for aborts on the load
3879          * load from [rn+offset], and then insert a 0 tag into rt.
3880          */
3881         clean_addr = clean_data_tbi(s, addr);
3882         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
3883         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
3884     }
3885 
3886     if (a->w) {
3887         /* pre-index or post-index */
3888         if (a->p) {
3889             /* post-index */
3890             tcg_gen_addi_i64(addr, addr, a->imm);
3891         }
3892         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3893     }
3894     return true;
3895 }
3896 
3897 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
3898 {
3899     TCGv_i64 addr, tcg_rt;
3900 
3901     if (a->rn == 31) {
3902         gen_check_sp_alignment(s);
3903     }
3904 
3905     addr = read_cpu_reg_sp(s, a->rn, true);
3906     if (!a->p) {
3907         /* pre-index or signed offset */
3908         tcg_gen_addi_i64(addr, addr, a->imm);
3909     }
3910     tcg_rt = cpu_reg_sp(s, a->rt);
3911     if (!s->ata[0]) {
3912         /*
3913          * For STG and ST2G, we need to check alignment and probe memory.
3914          * TODO: For STZG and STZ2G, we could rely on the stores below,
3915          * at least for system mode; user-only won't enforce alignment.
3916          */
3917         if (is_pair) {
3918             gen_helper_st2g_stub(tcg_env, addr);
3919         } else {
3920             gen_helper_stg_stub(tcg_env, addr);
3921         }
3922     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3923         if (is_pair) {
3924             gen_helper_st2g_parallel(tcg_env, addr, tcg_rt);
3925         } else {
3926             gen_helper_stg_parallel(tcg_env, addr, tcg_rt);
3927         }
3928     } else {
3929         if (is_pair) {
3930             gen_helper_st2g(tcg_env, addr, tcg_rt);
3931         } else {
3932             gen_helper_stg(tcg_env, addr, tcg_rt);
3933         }
3934     }
3935 
3936     if (is_zero) {
3937         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
3938         TCGv_i64 zero64 = tcg_constant_i64(0);
3939         TCGv_i128 zero128 = tcg_temp_new_i128();
3940         int mem_index = get_mem_index(s);
3941         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
3942 
3943         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
3944 
3945         /* This is 1 or 2 atomic 16-byte operations. */
3946         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3947         if (is_pair) {
3948             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
3949             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3950         }
3951     }
3952 
3953     if (a->w) {
3954         /* pre-index or post-index */
3955         if (a->p) {
3956             /* post-index */
3957             tcg_gen_addi_i64(addr, addr, a->imm);
3958         }
3959         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3960     }
3961     return true;
3962 }
3963 
3964 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
3965 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
3966 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
3967 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
3968 
3969 typedef void SetFn(TCGv_env, TCGv_i32, TCGv_i32);
3970 
3971 static bool do_SET(DisasContext *s, arg_set *a, bool is_epilogue,
3972                    bool is_setg, SetFn fn)
3973 {
3974     int memidx;
3975     uint32_t syndrome, desc = 0;
3976 
3977     if (is_setg && !dc_isar_feature(aa64_mte, s)) {
3978         return false;
3979     }
3980 
3981     /*
3982      * UNPREDICTABLE cases: we choose to UNDEF, which allows
3983      * us to pull this check before the CheckMOPSEnabled() test
3984      * (which we do in the helper function)
3985      */
3986     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
3987         a->rd == 31 || a->rn == 31) {
3988         return false;
3989     }
3990 
3991     memidx = get_a64_user_mem_index(s, a->unpriv);
3992 
3993     /*
3994      * We pass option_a == true, matching our implementation;
3995      * we pass wrong_option == false: helper function may set that bit.
3996      */
3997     syndrome = syn_mop(true, is_setg, (a->nontemp << 1) | a->unpriv,
3998                        is_epilogue, false, true, a->rd, a->rs, a->rn);
3999 
4000     if (is_setg ? s->ata[a->unpriv] : s->mte_active[a->unpriv]) {
4001         /* We may need to do MTE tag checking, so assemble the descriptor */
4002         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
4003         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
4004         desc = FIELD_DP32(desc, MTEDESC, WRITE, true);
4005         /* SIZEM1 and ALIGN we leave 0 (byte write) */
4006     }
4007     /* The helper function always needs the memidx even with MTE disabled */
4008     desc = FIELD_DP32(desc, MTEDESC, MIDX, memidx);
4009 
4010     /*
4011      * The helper needs the register numbers, but since they're in
4012      * the syndrome anyway, we let it extract them from there rather
4013      * than passing in an extra three integer arguments.
4014      */
4015     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(desc));
4016     return true;
4017 }
4018 
4019 TRANS_FEAT(SETP, aa64_mops, do_SET, a, false, false, gen_helper_setp)
4020 TRANS_FEAT(SETM, aa64_mops, do_SET, a, false, false, gen_helper_setm)
4021 TRANS_FEAT(SETE, aa64_mops, do_SET, a, true, false, gen_helper_sete)
4022 TRANS_FEAT(SETGP, aa64_mops, do_SET, a, false, true, gen_helper_setgp)
4023 TRANS_FEAT(SETGM, aa64_mops, do_SET, a, false, true, gen_helper_setgm)
4024 TRANS_FEAT(SETGE, aa64_mops, do_SET, a, true, true, gen_helper_setge)
4025 
4026 typedef void CpyFn(TCGv_env, TCGv_i32, TCGv_i32, TCGv_i32);
4027 
4028 static bool do_CPY(DisasContext *s, arg_cpy *a, bool is_epilogue, CpyFn fn)
4029 {
4030     int rmemidx, wmemidx;
4031     uint32_t syndrome, rdesc = 0, wdesc = 0;
4032     bool wunpriv = extract32(a->options, 0, 1);
4033     bool runpriv = extract32(a->options, 1, 1);
4034 
4035     /*
4036      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4037      * us to pull this check before the CheckMOPSEnabled() test
4038      * (which we do in the helper function)
4039      */
4040     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4041         a->rd == 31 || a->rs == 31 || a->rn == 31) {
4042         return false;
4043     }
4044 
4045     rmemidx = get_a64_user_mem_index(s, runpriv);
4046     wmemidx = get_a64_user_mem_index(s, wunpriv);
4047 
4048     /*
4049      * We pass option_a == true, matching our implementation;
4050      * we pass wrong_option == false: helper function may set that bit.
4051      */
4052     syndrome = syn_mop(false, false, a->options, is_epilogue,
4053                        false, true, a->rd, a->rs, a->rn);
4054 
4055     /* If we need to do MTE tag checking, assemble the descriptors */
4056     if (s->mte_active[runpriv]) {
4057         rdesc = FIELD_DP32(rdesc, MTEDESC, TBI, s->tbid);
4058         rdesc = FIELD_DP32(rdesc, MTEDESC, TCMA, s->tcma);
4059     }
4060     if (s->mte_active[wunpriv]) {
4061         wdesc = FIELD_DP32(wdesc, MTEDESC, TBI, s->tbid);
4062         wdesc = FIELD_DP32(wdesc, MTEDESC, TCMA, s->tcma);
4063         wdesc = FIELD_DP32(wdesc, MTEDESC, WRITE, true);
4064     }
4065     /* The helper function needs these parts of the descriptor regardless */
4066     rdesc = FIELD_DP32(rdesc, MTEDESC, MIDX, rmemidx);
4067     wdesc = FIELD_DP32(wdesc, MTEDESC, MIDX, wmemidx);
4068 
4069     /*
4070      * The helper needs the register numbers, but since they're in
4071      * the syndrome anyway, we let it extract them from there rather
4072      * than passing in an extra three integer arguments.
4073      */
4074     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(wdesc),
4075        tcg_constant_i32(rdesc));
4076     return true;
4077 }
4078 
4079 TRANS_FEAT(CPYP, aa64_mops, do_CPY, a, false, gen_helper_cpyp)
4080 TRANS_FEAT(CPYM, aa64_mops, do_CPY, a, false, gen_helper_cpym)
4081 TRANS_FEAT(CPYE, aa64_mops, do_CPY, a, true, gen_helper_cpye)
4082 TRANS_FEAT(CPYFP, aa64_mops, do_CPY, a, false, gen_helper_cpyfp)
4083 TRANS_FEAT(CPYFM, aa64_mops, do_CPY, a, false, gen_helper_cpyfm)
4084 TRANS_FEAT(CPYFE, aa64_mops, do_CPY, a, true, gen_helper_cpyfe)
4085 
4086 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4087 
4088 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4089                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4090 {
4091     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4092     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4093     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4094 
4095     fn(tcg_rd, tcg_rn, tcg_imm);
4096     if (!a->sf) {
4097         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4098     }
4099     return true;
4100 }
4101 
4102 /*
4103  * PC-rel. addressing
4104  */
4105 
4106 static bool trans_ADR(DisasContext *s, arg_ri *a)
4107 {
4108     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4109     return true;
4110 }
4111 
4112 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4113 {
4114     int64_t offset = (int64_t)a->imm << 12;
4115 
4116     /* The page offset is ok for CF_PCREL. */
4117     offset -= s->pc_curr & 0xfff;
4118     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4119     return true;
4120 }
4121 
4122 /*
4123  * Add/subtract (immediate)
4124  */
4125 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4126 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4127 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4128 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4129 
4130 /*
4131  * Add/subtract (immediate, with tags)
4132  */
4133 
4134 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4135                                       bool sub_op)
4136 {
4137     TCGv_i64 tcg_rn, tcg_rd;
4138     int imm;
4139 
4140     imm = a->uimm6 << LOG2_TAG_GRANULE;
4141     if (sub_op) {
4142         imm = -imm;
4143     }
4144 
4145     tcg_rn = cpu_reg_sp(s, a->rn);
4146     tcg_rd = cpu_reg_sp(s, a->rd);
4147 
4148     if (s->ata[0]) {
4149         gen_helper_addsubg(tcg_rd, tcg_env, tcg_rn,
4150                            tcg_constant_i32(imm),
4151                            tcg_constant_i32(a->uimm4));
4152     } else {
4153         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4154         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4155     }
4156     return true;
4157 }
4158 
4159 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4160 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4161 
4162 /* The input should be a value in the bottom e bits (with higher
4163  * bits zero); returns that value replicated into every element
4164  * of size e in a 64 bit integer.
4165  */
4166 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4167 {
4168     assert(e != 0);
4169     while (e < 64) {
4170         mask |= mask << e;
4171         e *= 2;
4172     }
4173     return mask;
4174 }
4175 
4176 /*
4177  * Logical (immediate)
4178  */
4179 
4180 /*
4181  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4182  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4183  * value (ie should cause a guest UNDEF exception), and true if they are
4184  * valid, in which case the decoded bit pattern is written to result.
4185  */
4186 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4187                             unsigned int imms, unsigned int immr)
4188 {
4189     uint64_t mask;
4190     unsigned e, levels, s, r;
4191     int len;
4192 
4193     assert(immn < 2 && imms < 64 && immr < 64);
4194 
4195     /* The bit patterns we create here are 64 bit patterns which
4196      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4197      * 64 bits each. Each element contains the same value: a run
4198      * of between 1 and e-1 non-zero bits, rotated within the
4199      * element by between 0 and e-1 bits.
4200      *
4201      * The element size and run length are encoded into immn (1 bit)
4202      * and imms (6 bits) as follows:
4203      * 64 bit elements: immn = 1, imms = <length of run - 1>
4204      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4205      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4206      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4207      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4208      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4209      * Notice that immn = 0, imms = 11111x is the only combination
4210      * not covered by one of the above options; this is reserved.
4211      * Further, <length of run - 1> all-ones is a reserved pattern.
4212      *
4213      * In all cases the rotation is by immr % e (and immr is 6 bits).
4214      */
4215 
4216     /* First determine the element size */
4217     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4218     if (len < 1) {
4219         /* This is the immn == 0, imms == 0x11111x case */
4220         return false;
4221     }
4222     e = 1 << len;
4223 
4224     levels = e - 1;
4225     s = imms & levels;
4226     r = immr & levels;
4227 
4228     if (s == levels) {
4229         /* <length of run - 1> mustn't be all-ones. */
4230         return false;
4231     }
4232 
4233     /* Create the value of one element: s+1 set bits rotated
4234      * by r within the element (which is e bits wide)...
4235      */
4236     mask = MAKE_64BIT_MASK(0, s + 1);
4237     if (r) {
4238         mask = (mask >> r) | (mask << (e - r));
4239         mask &= MAKE_64BIT_MASK(0, e);
4240     }
4241     /* ...then replicate the element over the whole 64 bit value */
4242     mask = bitfield_replicate(mask, e);
4243     *result = mask;
4244     return true;
4245 }
4246 
4247 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4248                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4249 {
4250     TCGv_i64 tcg_rd, tcg_rn;
4251     uint64_t imm;
4252 
4253     /* Some immediate field values are reserved. */
4254     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4255                                 extract32(a->dbm, 0, 6),
4256                                 extract32(a->dbm, 6, 6))) {
4257         return false;
4258     }
4259     if (!a->sf) {
4260         imm &= 0xffffffffull;
4261     }
4262 
4263     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4264     tcg_rn = cpu_reg(s, a->rn);
4265 
4266     fn(tcg_rd, tcg_rn, imm);
4267     if (set_cc) {
4268         gen_logic_CC(a->sf, tcg_rd);
4269     }
4270     if (!a->sf) {
4271         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4272     }
4273     return true;
4274 }
4275 
4276 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4277 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4278 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4279 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4280 
4281 /*
4282  * Move wide (immediate)
4283  */
4284 
4285 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4286 {
4287     int pos = a->hw << 4;
4288     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4289     return true;
4290 }
4291 
4292 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4293 {
4294     int pos = a->hw << 4;
4295     uint64_t imm = a->imm;
4296 
4297     imm = ~(imm << pos);
4298     if (!a->sf) {
4299         imm = (uint32_t)imm;
4300     }
4301     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4302     return true;
4303 }
4304 
4305 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4306 {
4307     int pos = a->hw << 4;
4308     TCGv_i64 tcg_rd, tcg_im;
4309 
4310     tcg_rd = cpu_reg(s, a->rd);
4311     tcg_im = tcg_constant_i64(a->imm);
4312     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4313     if (!a->sf) {
4314         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4315     }
4316     return true;
4317 }
4318 
4319 /*
4320  * Bitfield
4321  */
4322 
4323 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4324 {
4325     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4326     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4327     unsigned int bitsize = a->sf ? 64 : 32;
4328     unsigned int ri = a->immr;
4329     unsigned int si = a->imms;
4330     unsigned int pos, len;
4331 
4332     if (si >= ri) {
4333         /* Wd<s-r:0> = Wn<s:r> */
4334         len = (si - ri) + 1;
4335         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4336         if (!a->sf) {
4337             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4338         }
4339     } else {
4340         /* Wd<32+s-r,32-r> = Wn<s:0> */
4341         len = si + 1;
4342         pos = (bitsize - ri) & (bitsize - 1);
4343 
4344         if (len < ri) {
4345             /*
4346              * Sign extend the destination field from len to fill the
4347              * balance of the word.  Let the deposit below insert all
4348              * of those sign bits.
4349              */
4350             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4351             len = ri;
4352         }
4353 
4354         /*
4355          * We start with zero, and we haven't modified any bits outside
4356          * bitsize, therefore no final zero-extension is unneeded for !sf.
4357          */
4358         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4359     }
4360     return true;
4361 }
4362 
4363 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4364 {
4365     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4366     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4367     unsigned int bitsize = a->sf ? 64 : 32;
4368     unsigned int ri = a->immr;
4369     unsigned int si = a->imms;
4370     unsigned int pos, len;
4371 
4372     tcg_rd = cpu_reg(s, a->rd);
4373     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4374 
4375     if (si >= ri) {
4376         /* Wd<s-r:0> = Wn<s:r> */
4377         len = (si - ri) + 1;
4378         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4379     } else {
4380         /* Wd<32+s-r,32-r> = Wn<s:0> */
4381         len = si + 1;
4382         pos = (bitsize - ri) & (bitsize - 1);
4383         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4384     }
4385     return true;
4386 }
4387 
4388 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4389 {
4390     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4391     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4392     unsigned int bitsize = a->sf ? 64 : 32;
4393     unsigned int ri = a->immr;
4394     unsigned int si = a->imms;
4395     unsigned int pos, len;
4396 
4397     tcg_rd = cpu_reg(s, a->rd);
4398     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4399 
4400     if (si >= ri) {
4401         /* Wd<s-r:0> = Wn<s:r> */
4402         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4403         len = (si - ri) + 1;
4404         pos = 0;
4405     } else {
4406         /* Wd<32+s-r,32-r> = Wn<s:0> */
4407         len = si + 1;
4408         pos = (bitsize - ri) & (bitsize - 1);
4409     }
4410 
4411     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4412     if (!a->sf) {
4413         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4414     }
4415     return true;
4416 }
4417 
4418 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4419 {
4420     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4421 
4422     tcg_rd = cpu_reg(s, a->rd);
4423 
4424     if (unlikely(a->imm == 0)) {
4425         /*
4426          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4427          * so an extract from bit 0 is a special case.
4428          */
4429         if (a->sf) {
4430             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4431         } else {
4432             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4433         }
4434     } else {
4435         tcg_rm = cpu_reg(s, a->rm);
4436         tcg_rn = cpu_reg(s, a->rn);
4437 
4438         if (a->sf) {
4439             /* Specialization to ROR happens in EXTRACT2.  */
4440             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4441         } else {
4442             TCGv_i32 t0 = tcg_temp_new_i32();
4443 
4444             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4445             if (a->rm == a->rn) {
4446                 tcg_gen_rotri_i32(t0, t0, a->imm);
4447             } else {
4448                 TCGv_i32 t1 = tcg_temp_new_i32();
4449                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4450                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4451             }
4452             tcg_gen_extu_i32_i64(tcg_rd, t0);
4453         }
4454     }
4455     return true;
4456 }
4457 
4458 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4459  * Note that it is the caller's responsibility to ensure that the
4460  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4461  * mandated semantics for out of range shifts.
4462  */
4463 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4464                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4465 {
4466     switch (shift_type) {
4467     case A64_SHIFT_TYPE_LSL:
4468         tcg_gen_shl_i64(dst, src, shift_amount);
4469         break;
4470     case A64_SHIFT_TYPE_LSR:
4471         tcg_gen_shr_i64(dst, src, shift_amount);
4472         break;
4473     case A64_SHIFT_TYPE_ASR:
4474         if (!sf) {
4475             tcg_gen_ext32s_i64(dst, src);
4476         }
4477         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4478         break;
4479     case A64_SHIFT_TYPE_ROR:
4480         if (sf) {
4481             tcg_gen_rotr_i64(dst, src, shift_amount);
4482         } else {
4483             TCGv_i32 t0, t1;
4484             t0 = tcg_temp_new_i32();
4485             t1 = tcg_temp_new_i32();
4486             tcg_gen_extrl_i64_i32(t0, src);
4487             tcg_gen_extrl_i64_i32(t1, shift_amount);
4488             tcg_gen_rotr_i32(t0, t0, t1);
4489             tcg_gen_extu_i32_i64(dst, t0);
4490         }
4491         break;
4492     default:
4493         assert(FALSE); /* all shift types should be handled */
4494         break;
4495     }
4496 
4497     if (!sf) { /* zero extend final result */
4498         tcg_gen_ext32u_i64(dst, dst);
4499     }
4500 }
4501 
4502 /* Shift a TCGv src by immediate, put result in dst.
4503  * The shift amount must be in range (this should always be true as the
4504  * relevant instructions will UNDEF on bad shift immediates).
4505  */
4506 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4507                           enum a64_shift_type shift_type, unsigned int shift_i)
4508 {
4509     assert(shift_i < (sf ? 64 : 32));
4510 
4511     if (shift_i == 0) {
4512         tcg_gen_mov_i64(dst, src);
4513     } else {
4514         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4515     }
4516 }
4517 
4518 /* Logical (shifted register)
4519  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4520  * +----+-----+-----------+-------+---+------+--------+------+------+
4521  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4522  * +----+-----+-----------+-------+---+------+--------+------+------+
4523  */
4524 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4525 {
4526     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4527     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4528 
4529     sf = extract32(insn, 31, 1);
4530     opc = extract32(insn, 29, 2);
4531     shift_type = extract32(insn, 22, 2);
4532     invert = extract32(insn, 21, 1);
4533     rm = extract32(insn, 16, 5);
4534     shift_amount = extract32(insn, 10, 6);
4535     rn = extract32(insn, 5, 5);
4536     rd = extract32(insn, 0, 5);
4537 
4538     if (!sf && (shift_amount & (1 << 5))) {
4539         unallocated_encoding(s);
4540         return;
4541     }
4542 
4543     tcg_rd = cpu_reg(s, rd);
4544 
4545     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4546         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4547          * register-register MOV and MVN, so it is worth special casing.
4548          */
4549         tcg_rm = cpu_reg(s, rm);
4550         if (invert) {
4551             tcg_gen_not_i64(tcg_rd, tcg_rm);
4552             if (!sf) {
4553                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4554             }
4555         } else {
4556             if (sf) {
4557                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4558             } else {
4559                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4560             }
4561         }
4562         return;
4563     }
4564 
4565     tcg_rm = read_cpu_reg(s, rm, sf);
4566 
4567     if (shift_amount) {
4568         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4569     }
4570 
4571     tcg_rn = cpu_reg(s, rn);
4572 
4573     switch (opc | (invert << 2)) {
4574     case 0: /* AND */
4575     case 3: /* ANDS */
4576         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4577         break;
4578     case 1: /* ORR */
4579         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4580         break;
4581     case 2: /* EOR */
4582         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4583         break;
4584     case 4: /* BIC */
4585     case 7: /* BICS */
4586         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4587         break;
4588     case 5: /* ORN */
4589         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4590         break;
4591     case 6: /* EON */
4592         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4593         break;
4594     default:
4595         assert(FALSE);
4596         break;
4597     }
4598 
4599     if (!sf) {
4600         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4601     }
4602 
4603     if (opc == 3) {
4604         gen_logic_CC(sf, tcg_rd);
4605     }
4606 }
4607 
4608 /*
4609  * Add/subtract (extended register)
4610  *
4611  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4612  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4613  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4614  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4615  *
4616  *  sf: 0 -> 32bit, 1 -> 64bit
4617  *  op: 0 -> add  , 1 -> sub
4618  *   S: 1 -> set flags
4619  * opt: 00
4620  * option: extension type (see DecodeRegExtend)
4621  * imm3: optional shift to Rm
4622  *
4623  * Rd = Rn + LSL(extend(Rm), amount)
4624  */
4625 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4626 {
4627     int rd = extract32(insn, 0, 5);
4628     int rn = extract32(insn, 5, 5);
4629     int imm3 = extract32(insn, 10, 3);
4630     int option = extract32(insn, 13, 3);
4631     int rm = extract32(insn, 16, 5);
4632     int opt = extract32(insn, 22, 2);
4633     bool setflags = extract32(insn, 29, 1);
4634     bool sub_op = extract32(insn, 30, 1);
4635     bool sf = extract32(insn, 31, 1);
4636 
4637     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4638     TCGv_i64 tcg_rd;
4639     TCGv_i64 tcg_result;
4640 
4641     if (imm3 > 4 || opt != 0) {
4642         unallocated_encoding(s);
4643         return;
4644     }
4645 
4646     /* non-flag setting ops may use SP */
4647     if (!setflags) {
4648         tcg_rd = cpu_reg_sp(s, rd);
4649     } else {
4650         tcg_rd = cpu_reg(s, rd);
4651     }
4652     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4653 
4654     tcg_rm = read_cpu_reg(s, rm, sf);
4655     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4656 
4657     tcg_result = tcg_temp_new_i64();
4658 
4659     if (!setflags) {
4660         if (sub_op) {
4661             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4662         } else {
4663             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4664         }
4665     } else {
4666         if (sub_op) {
4667             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4668         } else {
4669             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4670         }
4671     }
4672 
4673     if (sf) {
4674         tcg_gen_mov_i64(tcg_rd, tcg_result);
4675     } else {
4676         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4677     }
4678 }
4679 
4680 /*
4681  * Add/subtract (shifted register)
4682  *
4683  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4684  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4685  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4686  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4687  *
4688  *    sf: 0 -> 32bit, 1 -> 64bit
4689  *    op: 0 -> add  , 1 -> sub
4690  *     S: 1 -> set flags
4691  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4692  *  imm6: Shift amount to apply to Rm before the add/sub
4693  */
4694 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4695 {
4696     int rd = extract32(insn, 0, 5);
4697     int rn = extract32(insn, 5, 5);
4698     int imm6 = extract32(insn, 10, 6);
4699     int rm = extract32(insn, 16, 5);
4700     int shift_type = extract32(insn, 22, 2);
4701     bool setflags = extract32(insn, 29, 1);
4702     bool sub_op = extract32(insn, 30, 1);
4703     bool sf = extract32(insn, 31, 1);
4704 
4705     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4706     TCGv_i64 tcg_rn, tcg_rm;
4707     TCGv_i64 tcg_result;
4708 
4709     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4710         unallocated_encoding(s);
4711         return;
4712     }
4713 
4714     tcg_rn = read_cpu_reg(s, rn, sf);
4715     tcg_rm = read_cpu_reg(s, rm, sf);
4716 
4717     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4718 
4719     tcg_result = tcg_temp_new_i64();
4720 
4721     if (!setflags) {
4722         if (sub_op) {
4723             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4724         } else {
4725             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4726         }
4727     } else {
4728         if (sub_op) {
4729             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4730         } else {
4731             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4732         }
4733     }
4734 
4735     if (sf) {
4736         tcg_gen_mov_i64(tcg_rd, tcg_result);
4737     } else {
4738         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4739     }
4740 }
4741 
4742 /* Data-processing (3 source)
4743  *
4744  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4745  *  +--+------+-----------+------+------+----+------+------+------+
4746  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4747  *  +--+------+-----------+------+------+----+------+------+------+
4748  */
4749 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4750 {
4751     int rd = extract32(insn, 0, 5);
4752     int rn = extract32(insn, 5, 5);
4753     int ra = extract32(insn, 10, 5);
4754     int rm = extract32(insn, 16, 5);
4755     int op_id = (extract32(insn, 29, 3) << 4) |
4756         (extract32(insn, 21, 3) << 1) |
4757         extract32(insn, 15, 1);
4758     bool sf = extract32(insn, 31, 1);
4759     bool is_sub = extract32(op_id, 0, 1);
4760     bool is_high = extract32(op_id, 2, 1);
4761     bool is_signed = false;
4762     TCGv_i64 tcg_op1;
4763     TCGv_i64 tcg_op2;
4764     TCGv_i64 tcg_tmp;
4765 
4766     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4767     switch (op_id) {
4768     case 0x42: /* SMADDL */
4769     case 0x43: /* SMSUBL */
4770     case 0x44: /* SMULH */
4771         is_signed = true;
4772         break;
4773     case 0x0: /* MADD (32bit) */
4774     case 0x1: /* MSUB (32bit) */
4775     case 0x40: /* MADD (64bit) */
4776     case 0x41: /* MSUB (64bit) */
4777     case 0x4a: /* UMADDL */
4778     case 0x4b: /* UMSUBL */
4779     case 0x4c: /* UMULH */
4780         break;
4781     default:
4782         unallocated_encoding(s);
4783         return;
4784     }
4785 
4786     if (is_high) {
4787         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4788         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4789         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4790         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4791 
4792         if (is_signed) {
4793             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4794         } else {
4795             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4796         }
4797         return;
4798     }
4799 
4800     tcg_op1 = tcg_temp_new_i64();
4801     tcg_op2 = tcg_temp_new_i64();
4802     tcg_tmp = tcg_temp_new_i64();
4803 
4804     if (op_id < 0x42) {
4805         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4806         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4807     } else {
4808         if (is_signed) {
4809             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4810             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4811         } else {
4812             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4813             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4814         }
4815     }
4816 
4817     if (ra == 31 && !is_sub) {
4818         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4819         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4820     } else {
4821         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4822         if (is_sub) {
4823             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4824         } else {
4825             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4826         }
4827     }
4828 
4829     if (!sf) {
4830         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4831     }
4832 }
4833 
4834 /* Add/subtract (with carry)
4835  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4836  * +--+--+--+------------------------+------+-------------+------+-----+
4837  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4838  * +--+--+--+------------------------+------+-------------+------+-----+
4839  */
4840 
4841 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4842 {
4843     unsigned int sf, op, setflags, rm, rn, rd;
4844     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4845 
4846     sf = extract32(insn, 31, 1);
4847     op = extract32(insn, 30, 1);
4848     setflags = extract32(insn, 29, 1);
4849     rm = extract32(insn, 16, 5);
4850     rn = extract32(insn, 5, 5);
4851     rd = extract32(insn, 0, 5);
4852 
4853     tcg_rd = cpu_reg(s, rd);
4854     tcg_rn = cpu_reg(s, rn);
4855 
4856     if (op) {
4857         tcg_y = tcg_temp_new_i64();
4858         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4859     } else {
4860         tcg_y = cpu_reg(s, rm);
4861     }
4862 
4863     if (setflags) {
4864         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4865     } else {
4866         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4867     }
4868 }
4869 
4870 /*
4871  * Rotate right into flags
4872  *  31 30 29                21       15          10      5  4      0
4873  * +--+--+--+-----------------+--------+-----------+------+--+------+
4874  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4875  * +--+--+--+-----------------+--------+-----------+------+--+------+
4876  */
4877 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4878 {
4879     int mask = extract32(insn, 0, 4);
4880     int o2 = extract32(insn, 4, 1);
4881     int rn = extract32(insn, 5, 5);
4882     int imm6 = extract32(insn, 15, 6);
4883     int sf_op_s = extract32(insn, 29, 3);
4884     TCGv_i64 tcg_rn;
4885     TCGv_i32 nzcv;
4886 
4887     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4888         unallocated_encoding(s);
4889         return;
4890     }
4891 
4892     tcg_rn = read_cpu_reg(s, rn, 1);
4893     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4894 
4895     nzcv = tcg_temp_new_i32();
4896     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4897 
4898     if (mask & 8) { /* N */
4899         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4900     }
4901     if (mask & 4) { /* Z */
4902         tcg_gen_not_i32(cpu_ZF, nzcv);
4903         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4904     }
4905     if (mask & 2) { /* C */
4906         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4907     }
4908     if (mask & 1) { /* V */
4909         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4910     }
4911 }
4912 
4913 /*
4914  * Evaluate into flags
4915  *  31 30 29                21        15   14        10      5  4      0
4916  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4917  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
4918  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4919  */
4920 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4921 {
4922     int o3_mask = extract32(insn, 0, 5);
4923     int rn = extract32(insn, 5, 5);
4924     int o2 = extract32(insn, 15, 6);
4925     int sz = extract32(insn, 14, 1);
4926     int sf_op_s = extract32(insn, 29, 3);
4927     TCGv_i32 tmp;
4928     int shift;
4929 
4930     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4931         !dc_isar_feature(aa64_condm_4, s)) {
4932         unallocated_encoding(s);
4933         return;
4934     }
4935     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
4936 
4937     tmp = tcg_temp_new_i32();
4938     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4939     tcg_gen_shli_i32(cpu_NF, tmp, shift);
4940     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4941     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4942     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4943 }
4944 
4945 /* Conditional compare (immediate / register)
4946  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
4947  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4948  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
4949  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4950  *        [1]                             y                [0]       [0]
4951  */
4952 static void disas_cc(DisasContext *s, uint32_t insn)
4953 {
4954     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4955     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4956     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4957     DisasCompare c;
4958 
4959     if (!extract32(insn, 29, 1)) {
4960         unallocated_encoding(s);
4961         return;
4962     }
4963     if (insn & (1 << 10 | 1 << 4)) {
4964         unallocated_encoding(s);
4965         return;
4966     }
4967     sf = extract32(insn, 31, 1);
4968     op = extract32(insn, 30, 1);
4969     is_imm = extract32(insn, 11, 1);
4970     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4971     cond = extract32(insn, 12, 4);
4972     rn = extract32(insn, 5, 5);
4973     nzcv = extract32(insn, 0, 4);
4974 
4975     /* Set T0 = !COND.  */
4976     tcg_t0 = tcg_temp_new_i32();
4977     arm_test_cc(&c, cond);
4978     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4979 
4980     /* Load the arguments for the new comparison.  */
4981     if (is_imm) {
4982         tcg_y = tcg_temp_new_i64();
4983         tcg_gen_movi_i64(tcg_y, y);
4984     } else {
4985         tcg_y = cpu_reg(s, y);
4986     }
4987     tcg_rn = cpu_reg(s, rn);
4988 
4989     /* Set the flags for the new comparison.  */
4990     tcg_tmp = tcg_temp_new_i64();
4991     if (op) {
4992         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4993     } else {
4994         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4995     }
4996 
4997     /* If COND was false, force the flags to #nzcv.  Compute two masks
4998      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4999      * For tcg hosts that support ANDC, we can make do with just T1.
5000      * In either case, allow the tcg optimizer to delete any unused mask.
5001      */
5002     tcg_t1 = tcg_temp_new_i32();
5003     tcg_t2 = tcg_temp_new_i32();
5004     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5005     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5006 
5007     if (nzcv & 8) { /* N */
5008         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5009     } else {
5010         if (TCG_TARGET_HAS_andc_i32) {
5011             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5012         } else {
5013             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5014         }
5015     }
5016     if (nzcv & 4) { /* Z */
5017         if (TCG_TARGET_HAS_andc_i32) {
5018             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5019         } else {
5020             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5021         }
5022     } else {
5023         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5024     }
5025     if (nzcv & 2) { /* C */
5026         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5027     } else {
5028         if (TCG_TARGET_HAS_andc_i32) {
5029             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5030         } else {
5031             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5032         }
5033     }
5034     if (nzcv & 1) { /* V */
5035         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5036     } else {
5037         if (TCG_TARGET_HAS_andc_i32) {
5038             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5039         } else {
5040             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5041         }
5042     }
5043 }
5044 
5045 /* Conditional select
5046  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5047  * +----+----+---+-----------------+------+------+-----+------+------+
5048  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5049  * +----+----+---+-----------------+------+------+-----+------+------+
5050  */
5051 static void disas_cond_select(DisasContext *s, uint32_t insn)
5052 {
5053     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5054     TCGv_i64 tcg_rd, zero;
5055     DisasCompare64 c;
5056 
5057     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5058         /* S == 1 or op2<1> == 1 */
5059         unallocated_encoding(s);
5060         return;
5061     }
5062     sf = extract32(insn, 31, 1);
5063     else_inv = extract32(insn, 30, 1);
5064     rm = extract32(insn, 16, 5);
5065     cond = extract32(insn, 12, 4);
5066     else_inc = extract32(insn, 10, 1);
5067     rn = extract32(insn, 5, 5);
5068     rd = extract32(insn, 0, 5);
5069 
5070     tcg_rd = cpu_reg(s, rd);
5071 
5072     a64_test_cc(&c, cond);
5073     zero = tcg_constant_i64(0);
5074 
5075     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5076         /* CSET & CSETM.  */
5077         if (else_inv) {
5078             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
5079                                    tcg_rd, c.value, zero);
5080         } else {
5081             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
5082                                 tcg_rd, c.value, zero);
5083         }
5084     } else {
5085         TCGv_i64 t_true = cpu_reg(s, rn);
5086         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5087         if (else_inv && else_inc) {
5088             tcg_gen_neg_i64(t_false, t_false);
5089         } else if (else_inv) {
5090             tcg_gen_not_i64(t_false, t_false);
5091         } else if (else_inc) {
5092             tcg_gen_addi_i64(t_false, t_false, 1);
5093         }
5094         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5095     }
5096 
5097     if (!sf) {
5098         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5099     }
5100 }
5101 
5102 static void handle_clz(DisasContext *s, unsigned int sf,
5103                        unsigned int rn, unsigned int rd)
5104 {
5105     TCGv_i64 tcg_rd, tcg_rn;
5106     tcg_rd = cpu_reg(s, rd);
5107     tcg_rn = cpu_reg(s, rn);
5108 
5109     if (sf) {
5110         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5111     } else {
5112         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5113         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5114         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5115         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5116     }
5117 }
5118 
5119 static void handle_cls(DisasContext *s, unsigned int sf,
5120                        unsigned int rn, unsigned int rd)
5121 {
5122     TCGv_i64 tcg_rd, tcg_rn;
5123     tcg_rd = cpu_reg(s, rd);
5124     tcg_rn = cpu_reg(s, rn);
5125 
5126     if (sf) {
5127         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5128     } else {
5129         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5130         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5131         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5132         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5133     }
5134 }
5135 
5136 static void handle_rbit(DisasContext *s, unsigned int sf,
5137                         unsigned int rn, unsigned int rd)
5138 {
5139     TCGv_i64 tcg_rd, tcg_rn;
5140     tcg_rd = cpu_reg(s, rd);
5141     tcg_rn = cpu_reg(s, rn);
5142 
5143     if (sf) {
5144         gen_helper_rbit64(tcg_rd, tcg_rn);
5145     } else {
5146         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5147         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5148         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5149         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5150     }
5151 }
5152 
5153 /* REV with sf==1, opcode==3 ("REV64") */
5154 static void handle_rev64(DisasContext *s, unsigned int sf,
5155                          unsigned int rn, unsigned int rd)
5156 {
5157     if (!sf) {
5158         unallocated_encoding(s);
5159         return;
5160     }
5161     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5162 }
5163 
5164 /* REV with sf==0, opcode==2
5165  * REV32 (sf==1, opcode==2)
5166  */
5167 static void handle_rev32(DisasContext *s, unsigned int sf,
5168                          unsigned int rn, unsigned int rd)
5169 {
5170     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5171     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5172 
5173     if (sf) {
5174         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5175         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5176     } else {
5177         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5178     }
5179 }
5180 
5181 /* REV16 (opcode==1) */
5182 static void handle_rev16(DisasContext *s, unsigned int sf,
5183                          unsigned int rn, unsigned int rd)
5184 {
5185     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5186     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5187     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5188     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5189 
5190     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5191     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5192     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5193     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5194     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5195 }
5196 
5197 /* Data-processing (1 source)
5198  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5199  * +----+---+---+-----------------+---------+--------+------+------+
5200  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5201  * +----+---+---+-----------------+---------+--------+------+------+
5202  */
5203 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5204 {
5205     unsigned int sf, opcode, opcode2, rn, rd;
5206     TCGv_i64 tcg_rd;
5207 
5208     if (extract32(insn, 29, 1)) {
5209         unallocated_encoding(s);
5210         return;
5211     }
5212 
5213     sf = extract32(insn, 31, 1);
5214     opcode = extract32(insn, 10, 6);
5215     opcode2 = extract32(insn, 16, 5);
5216     rn = extract32(insn, 5, 5);
5217     rd = extract32(insn, 0, 5);
5218 
5219 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5220 
5221     switch (MAP(sf, opcode2, opcode)) {
5222     case MAP(0, 0x00, 0x00): /* RBIT */
5223     case MAP(1, 0x00, 0x00):
5224         handle_rbit(s, sf, rn, rd);
5225         break;
5226     case MAP(0, 0x00, 0x01): /* REV16 */
5227     case MAP(1, 0x00, 0x01):
5228         handle_rev16(s, sf, rn, rd);
5229         break;
5230     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5231     case MAP(1, 0x00, 0x02):
5232         handle_rev32(s, sf, rn, rd);
5233         break;
5234     case MAP(1, 0x00, 0x03): /* REV64 */
5235         handle_rev64(s, sf, rn, rd);
5236         break;
5237     case MAP(0, 0x00, 0x04): /* CLZ */
5238     case MAP(1, 0x00, 0x04):
5239         handle_clz(s, sf, rn, rd);
5240         break;
5241     case MAP(0, 0x00, 0x05): /* CLS */
5242     case MAP(1, 0x00, 0x05):
5243         handle_cls(s, sf, rn, rd);
5244         break;
5245     case MAP(1, 0x01, 0x00): /* PACIA */
5246         if (s->pauth_active) {
5247             tcg_rd = cpu_reg(s, rd);
5248             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5249         } else if (!dc_isar_feature(aa64_pauth, s)) {
5250             goto do_unallocated;
5251         }
5252         break;
5253     case MAP(1, 0x01, 0x01): /* PACIB */
5254         if (s->pauth_active) {
5255             tcg_rd = cpu_reg(s, rd);
5256             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5257         } else if (!dc_isar_feature(aa64_pauth, s)) {
5258             goto do_unallocated;
5259         }
5260         break;
5261     case MAP(1, 0x01, 0x02): /* PACDA */
5262         if (s->pauth_active) {
5263             tcg_rd = cpu_reg(s, rd);
5264             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5265         } else if (!dc_isar_feature(aa64_pauth, s)) {
5266             goto do_unallocated;
5267         }
5268         break;
5269     case MAP(1, 0x01, 0x03): /* PACDB */
5270         if (s->pauth_active) {
5271             tcg_rd = cpu_reg(s, rd);
5272             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5273         } else if (!dc_isar_feature(aa64_pauth, s)) {
5274             goto do_unallocated;
5275         }
5276         break;
5277     case MAP(1, 0x01, 0x04): /* AUTIA */
5278         if (s->pauth_active) {
5279             tcg_rd = cpu_reg(s, rd);
5280             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5281         } else if (!dc_isar_feature(aa64_pauth, s)) {
5282             goto do_unallocated;
5283         }
5284         break;
5285     case MAP(1, 0x01, 0x05): /* AUTIB */
5286         if (s->pauth_active) {
5287             tcg_rd = cpu_reg(s, rd);
5288             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5289         } else if (!dc_isar_feature(aa64_pauth, s)) {
5290             goto do_unallocated;
5291         }
5292         break;
5293     case MAP(1, 0x01, 0x06): /* AUTDA */
5294         if (s->pauth_active) {
5295             tcg_rd = cpu_reg(s, rd);
5296             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5297         } else if (!dc_isar_feature(aa64_pauth, s)) {
5298             goto do_unallocated;
5299         }
5300         break;
5301     case MAP(1, 0x01, 0x07): /* AUTDB */
5302         if (s->pauth_active) {
5303             tcg_rd = cpu_reg(s, rd);
5304             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5305         } else if (!dc_isar_feature(aa64_pauth, s)) {
5306             goto do_unallocated;
5307         }
5308         break;
5309     case MAP(1, 0x01, 0x08): /* PACIZA */
5310         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5311             goto do_unallocated;
5312         } else if (s->pauth_active) {
5313             tcg_rd = cpu_reg(s, rd);
5314             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5315         }
5316         break;
5317     case MAP(1, 0x01, 0x09): /* PACIZB */
5318         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5319             goto do_unallocated;
5320         } else if (s->pauth_active) {
5321             tcg_rd = cpu_reg(s, rd);
5322             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5323         }
5324         break;
5325     case MAP(1, 0x01, 0x0a): /* PACDZA */
5326         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5327             goto do_unallocated;
5328         } else if (s->pauth_active) {
5329             tcg_rd = cpu_reg(s, rd);
5330             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5331         }
5332         break;
5333     case MAP(1, 0x01, 0x0b): /* PACDZB */
5334         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5335             goto do_unallocated;
5336         } else if (s->pauth_active) {
5337             tcg_rd = cpu_reg(s, rd);
5338             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5339         }
5340         break;
5341     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5342         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5343             goto do_unallocated;
5344         } else if (s->pauth_active) {
5345             tcg_rd = cpu_reg(s, rd);
5346             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5347         }
5348         break;
5349     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5350         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5351             goto do_unallocated;
5352         } else if (s->pauth_active) {
5353             tcg_rd = cpu_reg(s, rd);
5354             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5355         }
5356         break;
5357     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5358         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5359             goto do_unallocated;
5360         } else if (s->pauth_active) {
5361             tcg_rd = cpu_reg(s, rd);
5362             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5363         }
5364         break;
5365     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5366         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5367             goto do_unallocated;
5368         } else if (s->pauth_active) {
5369             tcg_rd = cpu_reg(s, rd);
5370             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5371         }
5372         break;
5373     case MAP(1, 0x01, 0x10): /* XPACI */
5374         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5375             goto do_unallocated;
5376         } else if (s->pauth_active) {
5377             tcg_rd = cpu_reg(s, rd);
5378             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
5379         }
5380         break;
5381     case MAP(1, 0x01, 0x11): /* XPACD */
5382         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5383             goto do_unallocated;
5384         } else if (s->pauth_active) {
5385             tcg_rd = cpu_reg(s, rd);
5386             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
5387         }
5388         break;
5389     default:
5390     do_unallocated:
5391         unallocated_encoding(s);
5392         break;
5393     }
5394 
5395 #undef MAP
5396 }
5397 
5398 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5399                        unsigned int rm, unsigned int rn, unsigned int rd)
5400 {
5401     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5402     tcg_rd = cpu_reg(s, rd);
5403 
5404     if (!sf && is_signed) {
5405         tcg_n = tcg_temp_new_i64();
5406         tcg_m = tcg_temp_new_i64();
5407         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5408         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5409     } else {
5410         tcg_n = read_cpu_reg(s, rn, sf);
5411         tcg_m = read_cpu_reg(s, rm, sf);
5412     }
5413 
5414     if (is_signed) {
5415         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5416     } else {
5417         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5418     }
5419 
5420     if (!sf) { /* zero extend final result */
5421         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5422     }
5423 }
5424 
5425 /* LSLV, LSRV, ASRV, RORV */
5426 static void handle_shift_reg(DisasContext *s,
5427                              enum a64_shift_type shift_type, unsigned int sf,
5428                              unsigned int rm, unsigned int rn, unsigned int rd)
5429 {
5430     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5431     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5432     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5433 
5434     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5435     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5436 }
5437 
5438 /* CRC32[BHWX], CRC32C[BHWX] */
5439 static void handle_crc32(DisasContext *s,
5440                          unsigned int sf, unsigned int sz, bool crc32c,
5441                          unsigned int rm, unsigned int rn, unsigned int rd)
5442 {
5443     TCGv_i64 tcg_acc, tcg_val;
5444     TCGv_i32 tcg_bytes;
5445 
5446     if (!dc_isar_feature(aa64_crc32, s)
5447         || (sf == 1 && sz != 3)
5448         || (sf == 0 && sz == 3)) {
5449         unallocated_encoding(s);
5450         return;
5451     }
5452 
5453     if (sz == 3) {
5454         tcg_val = cpu_reg(s, rm);
5455     } else {
5456         uint64_t mask;
5457         switch (sz) {
5458         case 0:
5459             mask = 0xFF;
5460             break;
5461         case 1:
5462             mask = 0xFFFF;
5463             break;
5464         case 2:
5465             mask = 0xFFFFFFFF;
5466             break;
5467         default:
5468             g_assert_not_reached();
5469         }
5470         tcg_val = tcg_temp_new_i64();
5471         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5472     }
5473 
5474     tcg_acc = cpu_reg(s, rn);
5475     tcg_bytes = tcg_constant_i32(1 << sz);
5476 
5477     if (crc32c) {
5478         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5479     } else {
5480         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5481     }
5482 }
5483 
5484 /* Data-processing (2 source)
5485  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5486  * +----+---+---+-----------------+------+--------+------+------+
5487  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5488  * +----+---+---+-----------------+------+--------+------+------+
5489  */
5490 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5491 {
5492     unsigned int sf, rm, opcode, rn, rd, setflag;
5493     sf = extract32(insn, 31, 1);
5494     setflag = extract32(insn, 29, 1);
5495     rm = extract32(insn, 16, 5);
5496     opcode = extract32(insn, 10, 6);
5497     rn = extract32(insn, 5, 5);
5498     rd = extract32(insn, 0, 5);
5499 
5500     if (setflag && opcode != 0) {
5501         unallocated_encoding(s);
5502         return;
5503     }
5504 
5505     switch (opcode) {
5506     case 0: /* SUBP(S) */
5507         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5508             goto do_unallocated;
5509         } else {
5510             TCGv_i64 tcg_n, tcg_m, tcg_d;
5511 
5512             tcg_n = read_cpu_reg_sp(s, rn, true);
5513             tcg_m = read_cpu_reg_sp(s, rm, true);
5514             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5515             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5516             tcg_d = cpu_reg(s, rd);
5517 
5518             if (setflag) {
5519                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5520             } else {
5521                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5522             }
5523         }
5524         break;
5525     case 2: /* UDIV */
5526         handle_div(s, false, sf, rm, rn, rd);
5527         break;
5528     case 3: /* SDIV */
5529         handle_div(s, true, sf, rm, rn, rd);
5530         break;
5531     case 4: /* IRG */
5532         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5533             goto do_unallocated;
5534         }
5535         if (s->ata[0]) {
5536             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
5537                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5538         } else {
5539             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5540                                              cpu_reg_sp(s, rn));
5541         }
5542         break;
5543     case 5: /* GMI */
5544         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5545             goto do_unallocated;
5546         } else {
5547             TCGv_i64 t = tcg_temp_new_i64();
5548 
5549             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5550             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5551             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5552         }
5553         break;
5554     case 8: /* LSLV */
5555         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5556         break;
5557     case 9: /* LSRV */
5558         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5559         break;
5560     case 10: /* ASRV */
5561         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5562         break;
5563     case 11: /* RORV */
5564         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5565         break;
5566     case 12: /* PACGA */
5567         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5568             goto do_unallocated;
5569         }
5570         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
5571                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5572         break;
5573     case 16:
5574     case 17:
5575     case 18:
5576     case 19:
5577     case 20:
5578     case 21:
5579     case 22:
5580     case 23: /* CRC32 */
5581     {
5582         int sz = extract32(opcode, 0, 2);
5583         bool crc32c = extract32(opcode, 2, 1);
5584         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5585         break;
5586     }
5587     default:
5588     do_unallocated:
5589         unallocated_encoding(s);
5590         break;
5591     }
5592 }
5593 
5594 /*
5595  * Data processing - register
5596  *  31  30 29  28      25    21  20  16      10         0
5597  * +--+---+--+---+-------+-----+-------+-------+---------+
5598  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5599  * +--+---+--+---+-------+-----+-------+-------+---------+
5600  */
5601 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5602 {
5603     int op0 = extract32(insn, 30, 1);
5604     int op1 = extract32(insn, 28, 1);
5605     int op2 = extract32(insn, 21, 4);
5606     int op3 = extract32(insn, 10, 6);
5607 
5608     if (!op1) {
5609         if (op2 & 8) {
5610             if (op2 & 1) {
5611                 /* Add/sub (extended register) */
5612                 disas_add_sub_ext_reg(s, insn);
5613             } else {
5614                 /* Add/sub (shifted register) */
5615                 disas_add_sub_reg(s, insn);
5616             }
5617         } else {
5618             /* Logical (shifted register) */
5619             disas_logic_reg(s, insn);
5620         }
5621         return;
5622     }
5623 
5624     switch (op2) {
5625     case 0x0:
5626         switch (op3) {
5627         case 0x00: /* Add/subtract (with carry) */
5628             disas_adc_sbc(s, insn);
5629             break;
5630 
5631         case 0x01: /* Rotate right into flags */
5632         case 0x21:
5633             disas_rotate_right_into_flags(s, insn);
5634             break;
5635 
5636         case 0x02: /* Evaluate into flags */
5637         case 0x12:
5638         case 0x22:
5639         case 0x32:
5640             disas_evaluate_into_flags(s, insn);
5641             break;
5642 
5643         default:
5644             goto do_unallocated;
5645         }
5646         break;
5647 
5648     case 0x2: /* Conditional compare */
5649         disas_cc(s, insn); /* both imm and reg forms */
5650         break;
5651 
5652     case 0x4: /* Conditional select */
5653         disas_cond_select(s, insn);
5654         break;
5655 
5656     case 0x6: /* Data-processing */
5657         if (op0) {    /* (1 source) */
5658             disas_data_proc_1src(s, insn);
5659         } else {      /* (2 source) */
5660             disas_data_proc_2src(s, insn);
5661         }
5662         break;
5663     case 0x8 ... 0xf: /* (3 source) */
5664         disas_data_proc_3src(s, insn);
5665         break;
5666 
5667     default:
5668     do_unallocated:
5669         unallocated_encoding(s);
5670         break;
5671     }
5672 }
5673 
5674 static void handle_fp_compare(DisasContext *s, int size,
5675                               unsigned int rn, unsigned int rm,
5676                               bool cmp_with_zero, bool signal_all_nans)
5677 {
5678     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5679     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5680 
5681     if (size == MO_64) {
5682         TCGv_i64 tcg_vn, tcg_vm;
5683 
5684         tcg_vn = read_fp_dreg(s, rn);
5685         if (cmp_with_zero) {
5686             tcg_vm = tcg_constant_i64(0);
5687         } else {
5688             tcg_vm = read_fp_dreg(s, rm);
5689         }
5690         if (signal_all_nans) {
5691             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5692         } else {
5693             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5694         }
5695     } else {
5696         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5697         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5698 
5699         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5700         if (cmp_with_zero) {
5701             tcg_gen_movi_i32(tcg_vm, 0);
5702         } else {
5703             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5704         }
5705 
5706         switch (size) {
5707         case MO_32:
5708             if (signal_all_nans) {
5709                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5710             } else {
5711                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5712             }
5713             break;
5714         case MO_16:
5715             if (signal_all_nans) {
5716                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5717             } else {
5718                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5719             }
5720             break;
5721         default:
5722             g_assert_not_reached();
5723         }
5724     }
5725 
5726     gen_set_nzcv(tcg_flags);
5727 }
5728 
5729 /* Floating point compare
5730  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5731  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5732  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5733  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5734  */
5735 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5736 {
5737     unsigned int mos, type, rm, op, rn, opc, op2r;
5738     int size;
5739 
5740     mos = extract32(insn, 29, 3);
5741     type = extract32(insn, 22, 2);
5742     rm = extract32(insn, 16, 5);
5743     op = extract32(insn, 14, 2);
5744     rn = extract32(insn, 5, 5);
5745     opc = extract32(insn, 3, 2);
5746     op2r = extract32(insn, 0, 3);
5747 
5748     if (mos || op || op2r) {
5749         unallocated_encoding(s);
5750         return;
5751     }
5752 
5753     switch (type) {
5754     case 0:
5755         size = MO_32;
5756         break;
5757     case 1:
5758         size = MO_64;
5759         break;
5760     case 3:
5761         size = MO_16;
5762         if (dc_isar_feature(aa64_fp16, s)) {
5763             break;
5764         }
5765         /* fallthru */
5766     default:
5767         unallocated_encoding(s);
5768         return;
5769     }
5770 
5771     if (!fp_access_check(s)) {
5772         return;
5773     }
5774 
5775     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5776 }
5777 
5778 /* Floating point conditional compare
5779  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5780  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5781  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5782  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5783  */
5784 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5785 {
5786     unsigned int mos, type, rm, cond, rn, op, nzcv;
5787     TCGLabel *label_continue = NULL;
5788     int size;
5789 
5790     mos = extract32(insn, 29, 3);
5791     type = extract32(insn, 22, 2);
5792     rm = extract32(insn, 16, 5);
5793     cond = extract32(insn, 12, 4);
5794     rn = extract32(insn, 5, 5);
5795     op = extract32(insn, 4, 1);
5796     nzcv = extract32(insn, 0, 4);
5797 
5798     if (mos) {
5799         unallocated_encoding(s);
5800         return;
5801     }
5802 
5803     switch (type) {
5804     case 0:
5805         size = MO_32;
5806         break;
5807     case 1:
5808         size = MO_64;
5809         break;
5810     case 3:
5811         size = MO_16;
5812         if (dc_isar_feature(aa64_fp16, s)) {
5813             break;
5814         }
5815         /* fallthru */
5816     default:
5817         unallocated_encoding(s);
5818         return;
5819     }
5820 
5821     if (!fp_access_check(s)) {
5822         return;
5823     }
5824 
5825     if (cond < 0x0e) { /* not always */
5826         TCGLabel *label_match = gen_new_label();
5827         label_continue = gen_new_label();
5828         arm_gen_test_cc(cond, label_match);
5829         /* nomatch: */
5830         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5831         tcg_gen_br(label_continue);
5832         gen_set_label(label_match);
5833     }
5834 
5835     handle_fp_compare(s, size, rn, rm, false, op);
5836 
5837     if (cond < 0x0e) {
5838         gen_set_label(label_continue);
5839     }
5840 }
5841 
5842 /* Floating point conditional select
5843  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5844  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5845  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5846  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5847  */
5848 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5849 {
5850     unsigned int mos, type, rm, cond, rn, rd;
5851     TCGv_i64 t_true, t_false;
5852     DisasCompare64 c;
5853     MemOp sz;
5854 
5855     mos = extract32(insn, 29, 3);
5856     type = extract32(insn, 22, 2);
5857     rm = extract32(insn, 16, 5);
5858     cond = extract32(insn, 12, 4);
5859     rn = extract32(insn, 5, 5);
5860     rd = extract32(insn, 0, 5);
5861 
5862     if (mos) {
5863         unallocated_encoding(s);
5864         return;
5865     }
5866 
5867     switch (type) {
5868     case 0:
5869         sz = MO_32;
5870         break;
5871     case 1:
5872         sz = MO_64;
5873         break;
5874     case 3:
5875         sz = MO_16;
5876         if (dc_isar_feature(aa64_fp16, s)) {
5877             break;
5878         }
5879         /* fallthru */
5880     default:
5881         unallocated_encoding(s);
5882         return;
5883     }
5884 
5885     if (!fp_access_check(s)) {
5886         return;
5887     }
5888 
5889     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5890     t_true = tcg_temp_new_i64();
5891     t_false = tcg_temp_new_i64();
5892     read_vec_element(s, t_true, rn, 0, sz);
5893     read_vec_element(s, t_false, rm, 0, sz);
5894 
5895     a64_test_cc(&c, cond);
5896     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5897                         t_true, t_false);
5898 
5899     /* Note that sregs & hregs write back zeros to the high bits,
5900        and we've already done the zero-extension.  */
5901     write_fp_dreg(s, rd, t_true);
5902 }
5903 
5904 /* Floating-point data-processing (1 source) - half precision */
5905 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5906 {
5907     TCGv_ptr fpst = NULL;
5908     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5909     TCGv_i32 tcg_res = tcg_temp_new_i32();
5910 
5911     switch (opcode) {
5912     case 0x0: /* FMOV */
5913         tcg_gen_mov_i32(tcg_res, tcg_op);
5914         break;
5915     case 0x1: /* FABS */
5916         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5917         break;
5918     case 0x2: /* FNEG */
5919         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5920         break;
5921     case 0x3: /* FSQRT */
5922         fpst = fpstatus_ptr(FPST_FPCR_F16);
5923         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5924         break;
5925     case 0x8: /* FRINTN */
5926     case 0x9: /* FRINTP */
5927     case 0xa: /* FRINTM */
5928     case 0xb: /* FRINTZ */
5929     case 0xc: /* FRINTA */
5930     {
5931         TCGv_i32 tcg_rmode;
5932 
5933         fpst = fpstatus_ptr(FPST_FPCR_F16);
5934         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
5935         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5936         gen_restore_rmode(tcg_rmode, fpst);
5937         break;
5938     }
5939     case 0xe: /* FRINTX */
5940         fpst = fpstatus_ptr(FPST_FPCR_F16);
5941         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5942         break;
5943     case 0xf: /* FRINTI */
5944         fpst = fpstatus_ptr(FPST_FPCR_F16);
5945         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5946         break;
5947     default:
5948         g_assert_not_reached();
5949     }
5950 
5951     write_fp_sreg(s, rd, tcg_res);
5952 }
5953 
5954 /* Floating-point data-processing (1 source) - single precision */
5955 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5956 {
5957     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5958     TCGv_i32 tcg_op, tcg_res;
5959     TCGv_ptr fpst;
5960     int rmode = -1;
5961 
5962     tcg_op = read_fp_sreg(s, rn);
5963     tcg_res = tcg_temp_new_i32();
5964 
5965     switch (opcode) {
5966     case 0x0: /* FMOV */
5967         tcg_gen_mov_i32(tcg_res, tcg_op);
5968         goto done;
5969     case 0x1: /* FABS */
5970         gen_helper_vfp_abss(tcg_res, tcg_op);
5971         goto done;
5972     case 0x2: /* FNEG */
5973         gen_helper_vfp_negs(tcg_res, tcg_op);
5974         goto done;
5975     case 0x3: /* FSQRT */
5976         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
5977         goto done;
5978     case 0x6: /* BFCVT */
5979         gen_fpst = gen_helper_bfcvt;
5980         break;
5981     case 0x8: /* FRINTN */
5982     case 0x9: /* FRINTP */
5983     case 0xa: /* FRINTM */
5984     case 0xb: /* FRINTZ */
5985     case 0xc: /* FRINTA */
5986         rmode = opcode & 7;
5987         gen_fpst = gen_helper_rints;
5988         break;
5989     case 0xe: /* FRINTX */
5990         gen_fpst = gen_helper_rints_exact;
5991         break;
5992     case 0xf: /* FRINTI */
5993         gen_fpst = gen_helper_rints;
5994         break;
5995     case 0x10: /* FRINT32Z */
5996         rmode = FPROUNDING_ZERO;
5997         gen_fpst = gen_helper_frint32_s;
5998         break;
5999     case 0x11: /* FRINT32X */
6000         gen_fpst = gen_helper_frint32_s;
6001         break;
6002     case 0x12: /* FRINT64Z */
6003         rmode = FPROUNDING_ZERO;
6004         gen_fpst = gen_helper_frint64_s;
6005         break;
6006     case 0x13: /* FRINT64X */
6007         gen_fpst = gen_helper_frint64_s;
6008         break;
6009     default:
6010         g_assert_not_reached();
6011     }
6012 
6013     fpst = fpstatus_ptr(FPST_FPCR);
6014     if (rmode >= 0) {
6015         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6016         gen_fpst(tcg_res, tcg_op, fpst);
6017         gen_restore_rmode(tcg_rmode, fpst);
6018     } else {
6019         gen_fpst(tcg_res, tcg_op, fpst);
6020     }
6021 
6022  done:
6023     write_fp_sreg(s, rd, tcg_res);
6024 }
6025 
6026 /* Floating-point data-processing (1 source) - double precision */
6027 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6028 {
6029     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6030     TCGv_i64 tcg_op, tcg_res;
6031     TCGv_ptr fpst;
6032     int rmode = -1;
6033 
6034     switch (opcode) {
6035     case 0x0: /* FMOV */
6036         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6037         return;
6038     }
6039 
6040     tcg_op = read_fp_dreg(s, rn);
6041     tcg_res = tcg_temp_new_i64();
6042 
6043     switch (opcode) {
6044     case 0x1: /* FABS */
6045         gen_helper_vfp_absd(tcg_res, tcg_op);
6046         goto done;
6047     case 0x2: /* FNEG */
6048         gen_helper_vfp_negd(tcg_res, tcg_op);
6049         goto done;
6050     case 0x3: /* FSQRT */
6051         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
6052         goto done;
6053     case 0x8: /* FRINTN */
6054     case 0x9: /* FRINTP */
6055     case 0xa: /* FRINTM */
6056     case 0xb: /* FRINTZ */
6057     case 0xc: /* FRINTA */
6058         rmode = opcode & 7;
6059         gen_fpst = gen_helper_rintd;
6060         break;
6061     case 0xe: /* FRINTX */
6062         gen_fpst = gen_helper_rintd_exact;
6063         break;
6064     case 0xf: /* FRINTI */
6065         gen_fpst = gen_helper_rintd;
6066         break;
6067     case 0x10: /* FRINT32Z */
6068         rmode = FPROUNDING_ZERO;
6069         gen_fpst = gen_helper_frint32_d;
6070         break;
6071     case 0x11: /* FRINT32X */
6072         gen_fpst = gen_helper_frint32_d;
6073         break;
6074     case 0x12: /* FRINT64Z */
6075         rmode = FPROUNDING_ZERO;
6076         gen_fpst = gen_helper_frint64_d;
6077         break;
6078     case 0x13: /* FRINT64X */
6079         gen_fpst = gen_helper_frint64_d;
6080         break;
6081     default:
6082         g_assert_not_reached();
6083     }
6084 
6085     fpst = fpstatus_ptr(FPST_FPCR);
6086     if (rmode >= 0) {
6087         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6088         gen_fpst(tcg_res, tcg_op, fpst);
6089         gen_restore_rmode(tcg_rmode, fpst);
6090     } else {
6091         gen_fpst(tcg_res, tcg_op, fpst);
6092     }
6093 
6094  done:
6095     write_fp_dreg(s, rd, tcg_res);
6096 }
6097 
6098 static void handle_fp_fcvt(DisasContext *s, int opcode,
6099                            int rd, int rn, int dtype, int ntype)
6100 {
6101     switch (ntype) {
6102     case 0x0:
6103     {
6104         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6105         if (dtype == 1) {
6106             /* Single to double */
6107             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6108             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
6109             write_fp_dreg(s, rd, tcg_rd);
6110         } else {
6111             /* Single to half */
6112             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6113             TCGv_i32 ahp = get_ahp_flag();
6114             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6115 
6116             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6117             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6118             write_fp_sreg(s, rd, tcg_rd);
6119         }
6120         break;
6121     }
6122     case 0x1:
6123     {
6124         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6125         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6126         if (dtype == 0) {
6127             /* Double to single */
6128             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
6129         } else {
6130             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6131             TCGv_i32 ahp = get_ahp_flag();
6132             /* Double to half */
6133             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6134             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6135         }
6136         write_fp_sreg(s, rd, tcg_rd);
6137         break;
6138     }
6139     case 0x3:
6140     {
6141         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6142         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6143         TCGv_i32 tcg_ahp = get_ahp_flag();
6144         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6145         if (dtype == 0) {
6146             /* Half to single */
6147             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6148             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6149             write_fp_sreg(s, rd, tcg_rd);
6150         } else {
6151             /* Half to double */
6152             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6153             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6154             write_fp_dreg(s, rd, tcg_rd);
6155         }
6156         break;
6157     }
6158     default:
6159         g_assert_not_reached();
6160     }
6161 }
6162 
6163 /* Floating point data-processing (1 source)
6164  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6165  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6166  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6167  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6168  */
6169 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6170 {
6171     int mos = extract32(insn, 29, 3);
6172     int type = extract32(insn, 22, 2);
6173     int opcode = extract32(insn, 15, 6);
6174     int rn = extract32(insn, 5, 5);
6175     int rd = extract32(insn, 0, 5);
6176 
6177     if (mos) {
6178         goto do_unallocated;
6179     }
6180 
6181     switch (opcode) {
6182     case 0x4: case 0x5: case 0x7:
6183     {
6184         /* FCVT between half, single and double precision */
6185         int dtype = extract32(opcode, 0, 2);
6186         if (type == 2 || dtype == type) {
6187             goto do_unallocated;
6188         }
6189         if (!fp_access_check(s)) {
6190             return;
6191         }
6192 
6193         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6194         break;
6195     }
6196 
6197     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6198         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6199             goto do_unallocated;
6200         }
6201         /* fall through */
6202     case 0x0 ... 0x3:
6203     case 0x8 ... 0xc:
6204     case 0xe ... 0xf:
6205         /* 32-to-32 and 64-to-64 ops */
6206         switch (type) {
6207         case 0:
6208             if (!fp_access_check(s)) {
6209                 return;
6210             }
6211             handle_fp_1src_single(s, opcode, rd, rn);
6212             break;
6213         case 1:
6214             if (!fp_access_check(s)) {
6215                 return;
6216             }
6217             handle_fp_1src_double(s, opcode, rd, rn);
6218             break;
6219         case 3:
6220             if (!dc_isar_feature(aa64_fp16, s)) {
6221                 goto do_unallocated;
6222             }
6223 
6224             if (!fp_access_check(s)) {
6225                 return;
6226             }
6227             handle_fp_1src_half(s, opcode, rd, rn);
6228             break;
6229         default:
6230             goto do_unallocated;
6231         }
6232         break;
6233 
6234     case 0x6:
6235         switch (type) {
6236         case 1: /* BFCVT */
6237             if (!dc_isar_feature(aa64_bf16, s)) {
6238                 goto do_unallocated;
6239             }
6240             if (!fp_access_check(s)) {
6241                 return;
6242             }
6243             handle_fp_1src_single(s, opcode, rd, rn);
6244             break;
6245         default:
6246             goto do_unallocated;
6247         }
6248         break;
6249 
6250     default:
6251     do_unallocated:
6252         unallocated_encoding(s);
6253         break;
6254     }
6255 }
6256 
6257 /* Floating-point data-processing (2 source) - single precision */
6258 static void handle_fp_2src_single(DisasContext *s, int opcode,
6259                                   int rd, int rn, int rm)
6260 {
6261     TCGv_i32 tcg_op1;
6262     TCGv_i32 tcg_op2;
6263     TCGv_i32 tcg_res;
6264     TCGv_ptr fpst;
6265 
6266     tcg_res = tcg_temp_new_i32();
6267     fpst = fpstatus_ptr(FPST_FPCR);
6268     tcg_op1 = read_fp_sreg(s, rn);
6269     tcg_op2 = read_fp_sreg(s, rm);
6270 
6271     switch (opcode) {
6272     case 0x0: /* FMUL */
6273         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6274         break;
6275     case 0x1: /* FDIV */
6276         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6277         break;
6278     case 0x2: /* FADD */
6279         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6280         break;
6281     case 0x3: /* FSUB */
6282         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6283         break;
6284     case 0x4: /* FMAX */
6285         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6286         break;
6287     case 0x5: /* FMIN */
6288         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6289         break;
6290     case 0x6: /* FMAXNM */
6291         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6292         break;
6293     case 0x7: /* FMINNM */
6294         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6295         break;
6296     case 0x8: /* FNMUL */
6297         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6298         gen_helper_vfp_negs(tcg_res, tcg_res);
6299         break;
6300     }
6301 
6302     write_fp_sreg(s, rd, tcg_res);
6303 }
6304 
6305 /* Floating-point data-processing (2 source) - double precision */
6306 static void handle_fp_2src_double(DisasContext *s, int opcode,
6307                                   int rd, int rn, int rm)
6308 {
6309     TCGv_i64 tcg_op1;
6310     TCGv_i64 tcg_op2;
6311     TCGv_i64 tcg_res;
6312     TCGv_ptr fpst;
6313 
6314     tcg_res = tcg_temp_new_i64();
6315     fpst = fpstatus_ptr(FPST_FPCR);
6316     tcg_op1 = read_fp_dreg(s, rn);
6317     tcg_op2 = read_fp_dreg(s, rm);
6318 
6319     switch (opcode) {
6320     case 0x0: /* FMUL */
6321         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6322         break;
6323     case 0x1: /* FDIV */
6324         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6325         break;
6326     case 0x2: /* FADD */
6327         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6328         break;
6329     case 0x3: /* FSUB */
6330         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6331         break;
6332     case 0x4: /* FMAX */
6333         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6334         break;
6335     case 0x5: /* FMIN */
6336         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6337         break;
6338     case 0x6: /* FMAXNM */
6339         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6340         break;
6341     case 0x7: /* FMINNM */
6342         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6343         break;
6344     case 0x8: /* FNMUL */
6345         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6346         gen_helper_vfp_negd(tcg_res, tcg_res);
6347         break;
6348     }
6349 
6350     write_fp_dreg(s, rd, tcg_res);
6351 }
6352 
6353 /* Floating-point data-processing (2 source) - half precision */
6354 static void handle_fp_2src_half(DisasContext *s, int opcode,
6355                                 int rd, int rn, int rm)
6356 {
6357     TCGv_i32 tcg_op1;
6358     TCGv_i32 tcg_op2;
6359     TCGv_i32 tcg_res;
6360     TCGv_ptr fpst;
6361 
6362     tcg_res = tcg_temp_new_i32();
6363     fpst = fpstatus_ptr(FPST_FPCR_F16);
6364     tcg_op1 = read_fp_hreg(s, rn);
6365     tcg_op2 = read_fp_hreg(s, rm);
6366 
6367     switch (opcode) {
6368     case 0x0: /* FMUL */
6369         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6370         break;
6371     case 0x1: /* FDIV */
6372         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6373         break;
6374     case 0x2: /* FADD */
6375         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6376         break;
6377     case 0x3: /* FSUB */
6378         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6379         break;
6380     case 0x4: /* FMAX */
6381         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6382         break;
6383     case 0x5: /* FMIN */
6384         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6385         break;
6386     case 0x6: /* FMAXNM */
6387         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6388         break;
6389     case 0x7: /* FMINNM */
6390         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6391         break;
6392     case 0x8: /* FNMUL */
6393         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6394         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6395         break;
6396     default:
6397         g_assert_not_reached();
6398     }
6399 
6400     write_fp_sreg(s, rd, tcg_res);
6401 }
6402 
6403 /* Floating point data-processing (2 source)
6404  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6405  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6406  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6407  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6408  */
6409 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6410 {
6411     int mos = extract32(insn, 29, 3);
6412     int type = extract32(insn, 22, 2);
6413     int rd = extract32(insn, 0, 5);
6414     int rn = extract32(insn, 5, 5);
6415     int rm = extract32(insn, 16, 5);
6416     int opcode = extract32(insn, 12, 4);
6417 
6418     if (opcode > 8 || mos) {
6419         unallocated_encoding(s);
6420         return;
6421     }
6422 
6423     switch (type) {
6424     case 0:
6425         if (!fp_access_check(s)) {
6426             return;
6427         }
6428         handle_fp_2src_single(s, opcode, rd, rn, rm);
6429         break;
6430     case 1:
6431         if (!fp_access_check(s)) {
6432             return;
6433         }
6434         handle_fp_2src_double(s, opcode, rd, rn, rm);
6435         break;
6436     case 3:
6437         if (!dc_isar_feature(aa64_fp16, s)) {
6438             unallocated_encoding(s);
6439             return;
6440         }
6441         if (!fp_access_check(s)) {
6442             return;
6443         }
6444         handle_fp_2src_half(s, opcode, rd, rn, rm);
6445         break;
6446     default:
6447         unallocated_encoding(s);
6448     }
6449 }
6450 
6451 /* Floating-point data-processing (3 source) - single precision */
6452 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6453                                   int rd, int rn, int rm, int ra)
6454 {
6455     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6456     TCGv_i32 tcg_res = tcg_temp_new_i32();
6457     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6458 
6459     tcg_op1 = read_fp_sreg(s, rn);
6460     tcg_op2 = read_fp_sreg(s, rm);
6461     tcg_op3 = read_fp_sreg(s, ra);
6462 
6463     /* These are fused multiply-add, and must be done as one
6464      * floating point operation with no rounding between the
6465      * multiplication and addition steps.
6466      * NB that doing the negations here as separate steps is
6467      * correct : an input NaN should come out with its sign bit
6468      * flipped if it is a negated-input.
6469      */
6470     if (o1 == true) {
6471         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6472     }
6473 
6474     if (o0 != o1) {
6475         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6476     }
6477 
6478     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6479 
6480     write_fp_sreg(s, rd, tcg_res);
6481 }
6482 
6483 /* Floating-point data-processing (3 source) - double precision */
6484 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6485                                   int rd, int rn, int rm, int ra)
6486 {
6487     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6488     TCGv_i64 tcg_res = tcg_temp_new_i64();
6489     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6490 
6491     tcg_op1 = read_fp_dreg(s, rn);
6492     tcg_op2 = read_fp_dreg(s, rm);
6493     tcg_op3 = read_fp_dreg(s, ra);
6494 
6495     /* These are fused multiply-add, and must be done as one
6496      * floating point operation with no rounding between the
6497      * multiplication and addition steps.
6498      * NB that doing the negations here as separate steps is
6499      * correct : an input NaN should come out with its sign bit
6500      * flipped if it is a negated-input.
6501      */
6502     if (o1 == true) {
6503         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6504     }
6505 
6506     if (o0 != o1) {
6507         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6508     }
6509 
6510     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6511 
6512     write_fp_dreg(s, rd, tcg_res);
6513 }
6514 
6515 /* Floating-point data-processing (3 source) - half precision */
6516 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6517                                 int rd, int rn, int rm, int ra)
6518 {
6519     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6520     TCGv_i32 tcg_res = tcg_temp_new_i32();
6521     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6522 
6523     tcg_op1 = read_fp_hreg(s, rn);
6524     tcg_op2 = read_fp_hreg(s, rm);
6525     tcg_op3 = read_fp_hreg(s, ra);
6526 
6527     /* These are fused multiply-add, and must be done as one
6528      * floating point operation with no rounding between the
6529      * multiplication and addition steps.
6530      * NB that doing the negations here as separate steps is
6531      * correct : an input NaN should come out with its sign bit
6532      * flipped if it is a negated-input.
6533      */
6534     if (o1 == true) {
6535         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6536     }
6537 
6538     if (o0 != o1) {
6539         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6540     }
6541 
6542     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6543 
6544     write_fp_sreg(s, rd, tcg_res);
6545 }
6546 
6547 /* Floating point data-processing (3 source)
6548  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6549  * +---+---+---+-----------+------+----+------+----+------+------+------+
6550  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6551  * +---+---+---+-----------+------+----+------+----+------+------+------+
6552  */
6553 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6554 {
6555     int mos = extract32(insn, 29, 3);
6556     int type = extract32(insn, 22, 2);
6557     int rd = extract32(insn, 0, 5);
6558     int rn = extract32(insn, 5, 5);
6559     int ra = extract32(insn, 10, 5);
6560     int rm = extract32(insn, 16, 5);
6561     bool o0 = extract32(insn, 15, 1);
6562     bool o1 = extract32(insn, 21, 1);
6563 
6564     if (mos) {
6565         unallocated_encoding(s);
6566         return;
6567     }
6568 
6569     switch (type) {
6570     case 0:
6571         if (!fp_access_check(s)) {
6572             return;
6573         }
6574         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6575         break;
6576     case 1:
6577         if (!fp_access_check(s)) {
6578             return;
6579         }
6580         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6581         break;
6582     case 3:
6583         if (!dc_isar_feature(aa64_fp16, s)) {
6584             unallocated_encoding(s);
6585             return;
6586         }
6587         if (!fp_access_check(s)) {
6588             return;
6589         }
6590         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6591         break;
6592     default:
6593         unallocated_encoding(s);
6594     }
6595 }
6596 
6597 /* Floating point immediate
6598  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6599  * +---+---+---+-----------+------+---+------------+-------+------+------+
6600  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6601  * +---+---+---+-----------+------+---+------------+-------+------+------+
6602  */
6603 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6604 {
6605     int rd = extract32(insn, 0, 5);
6606     int imm5 = extract32(insn, 5, 5);
6607     int imm8 = extract32(insn, 13, 8);
6608     int type = extract32(insn, 22, 2);
6609     int mos = extract32(insn, 29, 3);
6610     uint64_t imm;
6611     MemOp sz;
6612 
6613     if (mos || imm5) {
6614         unallocated_encoding(s);
6615         return;
6616     }
6617 
6618     switch (type) {
6619     case 0:
6620         sz = MO_32;
6621         break;
6622     case 1:
6623         sz = MO_64;
6624         break;
6625     case 3:
6626         sz = MO_16;
6627         if (dc_isar_feature(aa64_fp16, s)) {
6628             break;
6629         }
6630         /* fallthru */
6631     default:
6632         unallocated_encoding(s);
6633         return;
6634     }
6635 
6636     if (!fp_access_check(s)) {
6637         return;
6638     }
6639 
6640     imm = vfp_expand_imm(sz, imm8);
6641     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6642 }
6643 
6644 /* Handle floating point <=> fixed point conversions. Note that we can
6645  * also deal with fp <=> integer conversions as a special case (scale == 64)
6646  * OPTME: consider handling that special case specially or at least skipping
6647  * the call to scalbn in the helpers for zero shifts.
6648  */
6649 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6650                            bool itof, int rmode, int scale, int sf, int type)
6651 {
6652     bool is_signed = !(opcode & 1);
6653     TCGv_ptr tcg_fpstatus;
6654     TCGv_i32 tcg_shift, tcg_single;
6655     TCGv_i64 tcg_double;
6656 
6657     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6658 
6659     tcg_shift = tcg_constant_i32(64 - scale);
6660 
6661     if (itof) {
6662         TCGv_i64 tcg_int = cpu_reg(s, rn);
6663         if (!sf) {
6664             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6665 
6666             if (is_signed) {
6667                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6668             } else {
6669                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6670             }
6671 
6672             tcg_int = tcg_extend;
6673         }
6674 
6675         switch (type) {
6676         case 1: /* float64 */
6677             tcg_double = tcg_temp_new_i64();
6678             if (is_signed) {
6679                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6680                                      tcg_shift, tcg_fpstatus);
6681             } else {
6682                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6683                                      tcg_shift, tcg_fpstatus);
6684             }
6685             write_fp_dreg(s, rd, tcg_double);
6686             break;
6687 
6688         case 0: /* float32 */
6689             tcg_single = tcg_temp_new_i32();
6690             if (is_signed) {
6691                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6692                                      tcg_shift, tcg_fpstatus);
6693             } else {
6694                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6695                                      tcg_shift, tcg_fpstatus);
6696             }
6697             write_fp_sreg(s, rd, tcg_single);
6698             break;
6699 
6700         case 3: /* float16 */
6701             tcg_single = tcg_temp_new_i32();
6702             if (is_signed) {
6703                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6704                                      tcg_shift, tcg_fpstatus);
6705             } else {
6706                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6707                                      tcg_shift, tcg_fpstatus);
6708             }
6709             write_fp_sreg(s, rd, tcg_single);
6710             break;
6711 
6712         default:
6713             g_assert_not_reached();
6714         }
6715     } else {
6716         TCGv_i64 tcg_int = cpu_reg(s, rd);
6717         TCGv_i32 tcg_rmode;
6718 
6719         if (extract32(opcode, 2, 1)) {
6720             /* There are too many rounding modes to all fit into rmode,
6721              * so FCVTA[US] is a special case.
6722              */
6723             rmode = FPROUNDING_TIEAWAY;
6724         }
6725 
6726         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6727 
6728         switch (type) {
6729         case 1: /* float64 */
6730             tcg_double = read_fp_dreg(s, rn);
6731             if (is_signed) {
6732                 if (!sf) {
6733                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6734                                          tcg_shift, tcg_fpstatus);
6735                 } else {
6736                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6737                                          tcg_shift, tcg_fpstatus);
6738                 }
6739             } else {
6740                 if (!sf) {
6741                     gen_helper_vfp_tould(tcg_int, tcg_double,
6742                                          tcg_shift, tcg_fpstatus);
6743                 } else {
6744                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6745                                          tcg_shift, tcg_fpstatus);
6746                 }
6747             }
6748             if (!sf) {
6749                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6750             }
6751             break;
6752 
6753         case 0: /* float32 */
6754             tcg_single = read_fp_sreg(s, rn);
6755             if (sf) {
6756                 if (is_signed) {
6757                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6758                                          tcg_shift, tcg_fpstatus);
6759                 } else {
6760                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6761                                          tcg_shift, tcg_fpstatus);
6762                 }
6763             } else {
6764                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6765                 if (is_signed) {
6766                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6767                                          tcg_shift, tcg_fpstatus);
6768                 } else {
6769                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6770                                          tcg_shift, tcg_fpstatus);
6771                 }
6772                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6773             }
6774             break;
6775 
6776         case 3: /* float16 */
6777             tcg_single = read_fp_sreg(s, rn);
6778             if (sf) {
6779                 if (is_signed) {
6780                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6781                                          tcg_shift, tcg_fpstatus);
6782                 } else {
6783                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6784                                          tcg_shift, tcg_fpstatus);
6785                 }
6786             } else {
6787                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6788                 if (is_signed) {
6789                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6790                                          tcg_shift, tcg_fpstatus);
6791                 } else {
6792                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6793                                          tcg_shift, tcg_fpstatus);
6794                 }
6795                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6796             }
6797             break;
6798 
6799         default:
6800             g_assert_not_reached();
6801         }
6802 
6803         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6804     }
6805 }
6806 
6807 /* Floating point <-> fixed point conversions
6808  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6809  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6810  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6811  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6812  */
6813 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6814 {
6815     int rd = extract32(insn, 0, 5);
6816     int rn = extract32(insn, 5, 5);
6817     int scale = extract32(insn, 10, 6);
6818     int opcode = extract32(insn, 16, 3);
6819     int rmode = extract32(insn, 19, 2);
6820     int type = extract32(insn, 22, 2);
6821     bool sbit = extract32(insn, 29, 1);
6822     bool sf = extract32(insn, 31, 1);
6823     bool itof;
6824 
6825     if (sbit || (!sf && scale < 32)) {
6826         unallocated_encoding(s);
6827         return;
6828     }
6829 
6830     switch (type) {
6831     case 0: /* float32 */
6832     case 1: /* float64 */
6833         break;
6834     case 3: /* float16 */
6835         if (dc_isar_feature(aa64_fp16, s)) {
6836             break;
6837         }
6838         /* fallthru */
6839     default:
6840         unallocated_encoding(s);
6841         return;
6842     }
6843 
6844     switch ((rmode << 3) | opcode) {
6845     case 0x2: /* SCVTF */
6846     case 0x3: /* UCVTF */
6847         itof = true;
6848         break;
6849     case 0x18: /* FCVTZS */
6850     case 0x19: /* FCVTZU */
6851         itof = false;
6852         break;
6853     default:
6854         unallocated_encoding(s);
6855         return;
6856     }
6857 
6858     if (!fp_access_check(s)) {
6859         return;
6860     }
6861 
6862     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6863 }
6864 
6865 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6866 {
6867     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6868      * without conversion.
6869      */
6870 
6871     if (itof) {
6872         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6873         TCGv_i64 tmp;
6874 
6875         switch (type) {
6876         case 0:
6877             /* 32 bit */
6878             tmp = tcg_temp_new_i64();
6879             tcg_gen_ext32u_i64(tmp, tcg_rn);
6880             write_fp_dreg(s, rd, tmp);
6881             break;
6882         case 1:
6883             /* 64 bit */
6884             write_fp_dreg(s, rd, tcg_rn);
6885             break;
6886         case 2:
6887             /* 64 bit to top half. */
6888             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
6889             clear_vec_high(s, true, rd);
6890             break;
6891         case 3:
6892             /* 16 bit */
6893             tmp = tcg_temp_new_i64();
6894             tcg_gen_ext16u_i64(tmp, tcg_rn);
6895             write_fp_dreg(s, rd, tmp);
6896             break;
6897         default:
6898             g_assert_not_reached();
6899         }
6900     } else {
6901         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6902 
6903         switch (type) {
6904         case 0:
6905             /* 32 bit */
6906             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
6907             break;
6908         case 1:
6909             /* 64 bit */
6910             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
6911             break;
6912         case 2:
6913             /* 64 bits from top half */
6914             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
6915             break;
6916         case 3:
6917             /* 16 bit */
6918             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
6919             break;
6920         default:
6921             g_assert_not_reached();
6922         }
6923     }
6924 }
6925 
6926 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6927 {
6928     TCGv_i64 t = read_fp_dreg(s, rn);
6929     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
6930 
6931     gen_helper_fjcvtzs(t, t, fpstatus);
6932 
6933     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6934     tcg_gen_extrh_i64_i32(cpu_ZF, t);
6935     tcg_gen_movi_i32(cpu_CF, 0);
6936     tcg_gen_movi_i32(cpu_NF, 0);
6937     tcg_gen_movi_i32(cpu_VF, 0);
6938 }
6939 
6940 /* Floating point <-> integer conversions
6941  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
6942  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6943  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6944  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6945  */
6946 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6947 {
6948     int rd = extract32(insn, 0, 5);
6949     int rn = extract32(insn, 5, 5);
6950     int opcode = extract32(insn, 16, 3);
6951     int rmode = extract32(insn, 19, 2);
6952     int type = extract32(insn, 22, 2);
6953     bool sbit = extract32(insn, 29, 1);
6954     bool sf = extract32(insn, 31, 1);
6955     bool itof = false;
6956 
6957     if (sbit) {
6958         goto do_unallocated;
6959     }
6960 
6961     switch (opcode) {
6962     case 2: /* SCVTF */
6963     case 3: /* UCVTF */
6964         itof = true;
6965         /* fallthru */
6966     case 4: /* FCVTAS */
6967     case 5: /* FCVTAU */
6968         if (rmode != 0) {
6969             goto do_unallocated;
6970         }
6971         /* fallthru */
6972     case 0: /* FCVT[NPMZ]S */
6973     case 1: /* FCVT[NPMZ]U */
6974         switch (type) {
6975         case 0: /* float32 */
6976         case 1: /* float64 */
6977             break;
6978         case 3: /* float16 */
6979             if (!dc_isar_feature(aa64_fp16, s)) {
6980                 goto do_unallocated;
6981             }
6982             break;
6983         default:
6984             goto do_unallocated;
6985         }
6986         if (!fp_access_check(s)) {
6987             return;
6988         }
6989         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6990         break;
6991 
6992     default:
6993         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6994         case 0b01100110: /* FMOV half <-> 32-bit int */
6995         case 0b01100111:
6996         case 0b11100110: /* FMOV half <-> 64-bit int */
6997         case 0b11100111:
6998             if (!dc_isar_feature(aa64_fp16, s)) {
6999                 goto do_unallocated;
7000             }
7001             /* fallthru */
7002         case 0b00000110: /* FMOV 32-bit */
7003         case 0b00000111:
7004         case 0b10100110: /* FMOV 64-bit */
7005         case 0b10100111:
7006         case 0b11001110: /* FMOV top half of 128-bit */
7007         case 0b11001111:
7008             if (!fp_access_check(s)) {
7009                 return;
7010             }
7011             itof = opcode & 1;
7012             handle_fmov(s, rd, rn, type, itof);
7013             break;
7014 
7015         case 0b00111110: /* FJCVTZS */
7016             if (!dc_isar_feature(aa64_jscvt, s)) {
7017                 goto do_unallocated;
7018             } else if (fp_access_check(s)) {
7019                 handle_fjcvtzs(s, rd, rn);
7020             }
7021             break;
7022 
7023         default:
7024         do_unallocated:
7025             unallocated_encoding(s);
7026             return;
7027         }
7028         break;
7029     }
7030 }
7031 
7032 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7033  *   31  30  29 28     25 24                          0
7034  * +---+---+---+---------+-----------------------------+
7035  * |   | 0 |   | 1 1 1 1 |                             |
7036  * +---+---+---+---------+-----------------------------+
7037  */
7038 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7039 {
7040     if (extract32(insn, 24, 1)) {
7041         /* Floating point data-processing (3 source) */
7042         disas_fp_3src(s, insn);
7043     } else if (extract32(insn, 21, 1) == 0) {
7044         /* Floating point to fixed point conversions */
7045         disas_fp_fixed_conv(s, insn);
7046     } else {
7047         switch (extract32(insn, 10, 2)) {
7048         case 1:
7049             /* Floating point conditional compare */
7050             disas_fp_ccomp(s, insn);
7051             break;
7052         case 2:
7053             /* Floating point data-processing (2 source) */
7054             disas_fp_2src(s, insn);
7055             break;
7056         case 3:
7057             /* Floating point conditional select */
7058             disas_fp_csel(s, insn);
7059             break;
7060         case 0:
7061             switch (ctz32(extract32(insn, 12, 4))) {
7062             case 0: /* [15:12] == xxx1 */
7063                 /* Floating point immediate */
7064                 disas_fp_imm(s, insn);
7065                 break;
7066             case 1: /* [15:12] == xx10 */
7067                 /* Floating point compare */
7068                 disas_fp_compare(s, insn);
7069                 break;
7070             case 2: /* [15:12] == x100 */
7071                 /* Floating point data-processing (1 source) */
7072                 disas_fp_1src(s, insn);
7073                 break;
7074             case 3: /* [15:12] == 1000 */
7075                 unallocated_encoding(s);
7076                 break;
7077             default: /* [15:12] == 0000 */
7078                 /* Floating point <-> integer conversions */
7079                 disas_fp_int_conv(s, insn);
7080                 break;
7081             }
7082             break;
7083         }
7084     }
7085 }
7086 
7087 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7088                      int pos)
7089 {
7090     /* Extract 64 bits from the middle of two concatenated 64 bit
7091      * vector register slices left:right. The extracted bits start
7092      * at 'pos' bits into the right (least significant) side.
7093      * We return the result in tcg_right, and guarantee not to
7094      * trash tcg_left.
7095      */
7096     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7097     assert(pos > 0 && pos < 64);
7098 
7099     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7100     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7101     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7102 }
7103 
7104 /* EXT
7105  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7106  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7107  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7108  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7109  */
7110 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7111 {
7112     int is_q = extract32(insn, 30, 1);
7113     int op2 = extract32(insn, 22, 2);
7114     int imm4 = extract32(insn, 11, 4);
7115     int rm = extract32(insn, 16, 5);
7116     int rn = extract32(insn, 5, 5);
7117     int rd = extract32(insn, 0, 5);
7118     int pos = imm4 << 3;
7119     TCGv_i64 tcg_resl, tcg_resh;
7120 
7121     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7122         unallocated_encoding(s);
7123         return;
7124     }
7125 
7126     if (!fp_access_check(s)) {
7127         return;
7128     }
7129 
7130     tcg_resh = tcg_temp_new_i64();
7131     tcg_resl = tcg_temp_new_i64();
7132 
7133     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7134      * either extracting 128 bits from a 128:128 concatenation, or
7135      * extracting 64 bits from a 64:64 concatenation.
7136      */
7137     if (!is_q) {
7138         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7139         if (pos != 0) {
7140             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7141             do_ext64(s, tcg_resh, tcg_resl, pos);
7142         }
7143     } else {
7144         TCGv_i64 tcg_hh;
7145         typedef struct {
7146             int reg;
7147             int elt;
7148         } EltPosns;
7149         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7150         EltPosns *elt = eltposns;
7151 
7152         if (pos >= 64) {
7153             elt++;
7154             pos -= 64;
7155         }
7156 
7157         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7158         elt++;
7159         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7160         elt++;
7161         if (pos != 0) {
7162             do_ext64(s, tcg_resh, tcg_resl, pos);
7163             tcg_hh = tcg_temp_new_i64();
7164             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7165             do_ext64(s, tcg_hh, tcg_resh, pos);
7166         }
7167     }
7168 
7169     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7170     if (is_q) {
7171         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7172     }
7173     clear_vec_high(s, is_q, rd);
7174 }
7175 
7176 /* TBL/TBX
7177  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7178  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7179  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7180  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7181  */
7182 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7183 {
7184     int op2 = extract32(insn, 22, 2);
7185     int is_q = extract32(insn, 30, 1);
7186     int rm = extract32(insn, 16, 5);
7187     int rn = extract32(insn, 5, 5);
7188     int rd = extract32(insn, 0, 5);
7189     int is_tbx = extract32(insn, 12, 1);
7190     int len = (extract32(insn, 13, 2) + 1) * 16;
7191 
7192     if (op2 != 0) {
7193         unallocated_encoding(s);
7194         return;
7195     }
7196 
7197     if (!fp_access_check(s)) {
7198         return;
7199     }
7200 
7201     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7202                        vec_full_reg_offset(s, rm), tcg_env,
7203                        is_q ? 16 : 8, vec_full_reg_size(s),
7204                        (len << 6) | (is_tbx << 5) | rn,
7205                        gen_helper_simd_tblx);
7206 }
7207 
7208 /* ZIP/UZP/TRN
7209  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7210  * +---+---+-------------+------+---+------+---+------------------+------+
7211  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7212  * +---+---+-------------+------+---+------+---+------------------+------+
7213  */
7214 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7215 {
7216     int rd = extract32(insn, 0, 5);
7217     int rn = extract32(insn, 5, 5);
7218     int rm = extract32(insn, 16, 5);
7219     int size = extract32(insn, 22, 2);
7220     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7221      * bit 2 indicates 1 vs 2 variant of the insn.
7222      */
7223     int opcode = extract32(insn, 12, 2);
7224     bool part = extract32(insn, 14, 1);
7225     bool is_q = extract32(insn, 30, 1);
7226     int esize = 8 << size;
7227     int i;
7228     int datasize = is_q ? 128 : 64;
7229     int elements = datasize / esize;
7230     TCGv_i64 tcg_res[2], tcg_ele;
7231 
7232     if (opcode == 0 || (size == 3 && !is_q)) {
7233         unallocated_encoding(s);
7234         return;
7235     }
7236 
7237     if (!fp_access_check(s)) {
7238         return;
7239     }
7240 
7241     tcg_res[0] = tcg_temp_new_i64();
7242     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7243     tcg_ele = tcg_temp_new_i64();
7244 
7245     for (i = 0; i < elements; i++) {
7246         int o, w;
7247 
7248         switch (opcode) {
7249         case 1: /* UZP1/2 */
7250         {
7251             int midpoint = elements / 2;
7252             if (i < midpoint) {
7253                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7254             } else {
7255                 read_vec_element(s, tcg_ele, rm,
7256                                  2 * (i - midpoint) + part, size);
7257             }
7258             break;
7259         }
7260         case 2: /* TRN1/2 */
7261             if (i & 1) {
7262                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7263             } else {
7264                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7265             }
7266             break;
7267         case 3: /* ZIP1/2 */
7268         {
7269             int base = part * elements / 2;
7270             if (i & 1) {
7271                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7272             } else {
7273                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7274             }
7275             break;
7276         }
7277         default:
7278             g_assert_not_reached();
7279         }
7280 
7281         w = (i * esize) / 64;
7282         o = (i * esize) % 64;
7283         if (o == 0) {
7284             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7285         } else {
7286             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7287             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7288         }
7289     }
7290 
7291     for (i = 0; i <= is_q; ++i) {
7292         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7293     }
7294     clear_vec_high(s, is_q, rd);
7295 }
7296 
7297 /*
7298  * do_reduction_op helper
7299  *
7300  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7301  * important for correct NaN propagation that we do these
7302  * operations in exactly the order specified by the pseudocode.
7303  *
7304  * This is a recursive function, TCG temps should be freed by the
7305  * calling function once it is done with the values.
7306  */
7307 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7308                                 int esize, int size, int vmap, TCGv_ptr fpst)
7309 {
7310     if (esize == size) {
7311         int element;
7312         MemOp msize = esize == 16 ? MO_16 : MO_32;
7313         TCGv_i32 tcg_elem;
7314 
7315         /* We should have one register left here */
7316         assert(ctpop8(vmap) == 1);
7317         element = ctz32(vmap);
7318         assert(element < 8);
7319 
7320         tcg_elem = tcg_temp_new_i32();
7321         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7322         return tcg_elem;
7323     } else {
7324         int bits = size / 2;
7325         int shift = ctpop8(vmap) / 2;
7326         int vmap_lo = (vmap >> shift) & vmap;
7327         int vmap_hi = (vmap & ~vmap_lo);
7328         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7329 
7330         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7331         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7332         tcg_res = tcg_temp_new_i32();
7333 
7334         switch (fpopcode) {
7335         case 0x0c: /* fmaxnmv half-precision */
7336             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7337             break;
7338         case 0x0f: /* fmaxv half-precision */
7339             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7340             break;
7341         case 0x1c: /* fminnmv half-precision */
7342             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7343             break;
7344         case 0x1f: /* fminv half-precision */
7345             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7346             break;
7347         case 0x2c: /* fmaxnmv */
7348             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7349             break;
7350         case 0x2f: /* fmaxv */
7351             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7352             break;
7353         case 0x3c: /* fminnmv */
7354             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7355             break;
7356         case 0x3f: /* fminv */
7357             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7358             break;
7359         default:
7360             g_assert_not_reached();
7361         }
7362         return tcg_res;
7363     }
7364 }
7365 
7366 /* AdvSIMD across lanes
7367  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7368  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7369  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7370  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7371  */
7372 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7373 {
7374     int rd = extract32(insn, 0, 5);
7375     int rn = extract32(insn, 5, 5);
7376     int size = extract32(insn, 22, 2);
7377     int opcode = extract32(insn, 12, 5);
7378     bool is_q = extract32(insn, 30, 1);
7379     bool is_u = extract32(insn, 29, 1);
7380     bool is_fp = false;
7381     bool is_min = false;
7382     int esize;
7383     int elements;
7384     int i;
7385     TCGv_i64 tcg_res, tcg_elt;
7386 
7387     switch (opcode) {
7388     case 0x1b: /* ADDV */
7389         if (is_u) {
7390             unallocated_encoding(s);
7391             return;
7392         }
7393         /* fall through */
7394     case 0x3: /* SADDLV, UADDLV */
7395     case 0xa: /* SMAXV, UMAXV */
7396     case 0x1a: /* SMINV, UMINV */
7397         if (size == 3 || (size == 2 && !is_q)) {
7398             unallocated_encoding(s);
7399             return;
7400         }
7401         break;
7402     case 0xc: /* FMAXNMV, FMINNMV */
7403     case 0xf: /* FMAXV, FMINV */
7404         /* Bit 1 of size field encodes min vs max and the actual size
7405          * depends on the encoding of the U bit. If not set (and FP16
7406          * enabled) then we do half-precision float instead of single
7407          * precision.
7408          */
7409         is_min = extract32(size, 1, 1);
7410         is_fp = true;
7411         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7412             size = 1;
7413         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7414             unallocated_encoding(s);
7415             return;
7416         } else {
7417             size = 2;
7418         }
7419         break;
7420     default:
7421         unallocated_encoding(s);
7422         return;
7423     }
7424 
7425     if (!fp_access_check(s)) {
7426         return;
7427     }
7428 
7429     esize = 8 << size;
7430     elements = (is_q ? 128 : 64) / esize;
7431 
7432     tcg_res = tcg_temp_new_i64();
7433     tcg_elt = tcg_temp_new_i64();
7434 
7435     /* These instructions operate across all lanes of a vector
7436      * to produce a single result. We can guarantee that a 64
7437      * bit intermediate is sufficient:
7438      *  + for [US]ADDLV the maximum element size is 32 bits, and
7439      *    the result type is 64 bits
7440      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7441      *    same as the element size, which is 32 bits at most
7442      * For the integer operations we can choose to work at 64
7443      * or 32 bits and truncate at the end; for simplicity
7444      * we use 64 bits always. The floating point
7445      * ops do require 32 bit intermediates, though.
7446      */
7447     if (!is_fp) {
7448         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7449 
7450         for (i = 1; i < elements; i++) {
7451             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7452 
7453             switch (opcode) {
7454             case 0x03: /* SADDLV / UADDLV */
7455             case 0x1b: /* ADDV */
7456                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7457                 break;
7458             case 0x0a: /* SMAXV / UMAXV */
7459                 if (is_u) {
7460                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7461                 } else {
7462                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7463                 }
7464                 break;
7465             case 0x1a: /* SMINV / UMINV */
7466                 if (is_u) {
7467                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7468                 } else {
7469                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7470                 }
7471                 break;
7472             default:
7473                 g_assert_not_reached();
7474             }
7475 
7476         }
7477     } else {
7478         /* Floating point vector reduction ops which work across 32
7479          * bit (single) or 16 bit (half-precision) intermediates.
7480          * Note that correct NaN propagation requires that we do these
7481          * operations in exactly the order specified by the pseudocode.
7482          */
7483         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7484         int fpopcode = opcode | is_min << 4 | is_u << 5;
7485         int vmap = (1 << elements) - 1;
7486         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7487                                              (is_q ? 128 : 64), vmap, fpst);
7488         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7489     }
7490 
7491     /* Now truncate the result to the width required for the final output */
7492     if (opcode == 0x03) {
7493         /* SADDLV, UADDLV: result is 2*esize */
7494         size++;
7495     }
7496 
7497     switch (size) {
7498     case 0:
7499         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7500         break;
7501     case 1:
7502         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7503         break;
7504     case 2:
7505         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7506         break;
7507     case 3:
7508         break;
7509     default:
7510         g_assert_not_reached();
7511     }
7512 
7513     write_fp_dreg(s, rd, tcg_res);
7514 }
7515 
7516 /* DUP (Element, Vector)
7517  *
7518  *  31  30   29              21 20    16 15        10  9    5 4    0
7519  * +---+---+-------------------+--------+-------------+------+------+
7520  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7521  * +---+---+-------------------+--------+-------------+------+------+
7522  *
7523  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7524  */
7525 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7526                              int imm5)
7527 {
7528     int size = ctz32(imm5);
7529     int index;
7530 
7531     if (size > 3 || (size == 3 && !is_q)) {
7532         unallocated_encoding(s);
7533         return;
7534     }
7535 
7536     if (!fp_access_check(s)) {
7537         return;
7538     }
7539 
7540     index = imm5 >> (size + 1);
7541     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7542                          vec_reg_offset(s, rn, index, size),
7543                          is_q ? 16 : 8, vec_full_reg_size(s));
7544 }
7545 
7546 /* DUP (element, scalar)
7547  *  31                   21 20    16 15        10  9    5 4    0
7548  * +-----------------------+--------+-------------+------+------+
7549  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7550  * +-----------------------+--------+-------------+------+------+
7551  */
7552 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7553                               int imm5)
7554 {
7555     int size = ctz32(imm5);
7556     int index;
7557     TCGv_i64 tmp;
7558 
7559     if (size > 3) {
7560         unallocated_encoding(s);
7561         return;
7562     }
7563 
7564     if (!fp_access_check(s)) {
7565         return;
7566     }
7567 
7568     index = imm5 >> (size + 1);
7569 
7570     /* This instruction just extracts the specified element and
7571      * zero-extends it into the bottom of the destination register.
7572      */
7573     tmp = tcg_temp_new_i64();
7574     read_vec_element(s, tmp, rn, index, size);
7575     write_fp_dreg(s, rd, tmp);
7576 }
7577 
7578 /* DUP (General)
7579  *
7580  *  31  30   29              21 20    16 15        10  9    5 4    0
7581  * +---+---+-------------------+--------+-------------+------+------+
7582  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7583  * +---+---+-------------------+--------+-------------+------+------+
7584  *
7585  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7586  */
7587 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7588                              int imm5)
7589 {
7590     int size = ctz32(imm5);
7591     uint32_t dofs, oprsz, maxsz;
7592 
7593     if (size > 3 || ((size == 3) && !is_q)) {
7594         unallocated_encoding(s);
7595         return;
7596     }
7597 
7598     if (!fp_access_check(s)) {
7599         return;
7600     }
7601 
7602     dofs = vec_full_reg_offset(s, rd);
7603     oprsz = is_q ? 16 : 8;
7604     maxsz = vec_full_reg_size(s);
7605 
7606     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7607 }
7608 
7609 /* INS (Element)
7610  *
7611  *  31                   21 20    16 15  14    11  10 9    5 4    0
7612  * +-----------------------+--------+------------+---+------+------+
7613  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7614  * +-----------------------+--------+------------+---+------+------+
7615  *
7616  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7617  * index: encoded in imm5<4:size+1>
7618  */
7619 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7620                              int imm4, int imm5)
7621 {
7622     int size = ctz32(imm5);
7623     int src_index, dst_index;
7624     TCGv_i64 tmp;
7625 
7626     if (size > 3) {
7627         unallocated_encoding(s);
7628         return;
7629     }
7630 
7631     if (!fp_access_check(s)) {
7632         return;
7633     }
7634 
7635     dst_index = extract32(imm5, 1+size, 5);
7636     src_index = extract32(imm4, size, 4);
7637 
7638     tmp = tcg_temp_new_i64();
7639 
7640     read_vec_element(s, tmp, rn, src_index, size);
7641     write_vec_element(s, tmp, rd, dst_index, size);
7642 
7643     /* INS is considered a 128-bit write for SVE. */
7644     clear_vec_high(s, true, rd);
7645 }
7646 
7647 
7648 /* INS (General)
7649  *
7650  *  31                   21 20    16 15        10  9    5 4    0
7651  * +-----------------------+--------+-------------+------+------+
7652  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7653  * +-----------------------+--------+-------------+------+------+
7654  *
7655  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7656  * index: encoded in imm5<4:size+1>
7657  */
7658 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7659 {
7660     int size = ctz32(imm5);
7661     int idx;
7662 
7663     if (size > 3) {
7664         unallocated_encoding(s);
7665         return;
7666     }
7667 
7668     if (!fp_access_check(s)) {
7669         return;
7670     }
7671 
7672     idx = extract32(imm5, 1 + size, 4 - size);
7673     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7674 
7675     /* INS is considered a 128-bit write for SVE. */
7676     clear_vec_high(s, true, rd);
7677 }
7678 
7679 /*
7680  * UMOV (General)
7681  * SMOV (General)
7682  *
7683  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7684  * +---+---+-------------------+--------+-------------+------+------+
7685  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7686  * +---+---+-------------------+--------+-------------+------+------+
7687  *
7688  * U: unsigned when set
7689  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7690  */
7691 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7692                                   int rn, int rd, int imm5)
7693 {
7694     int size = ctz32(imm5);
7695     int element;
7696     TCGv_i64 tcg_rd;
7697 
7698     /* Check for UnallocatedEncodings */
7699     if (is_signed) {
7700         if (size > 2 || (size == 2 && !is_q)) {
7701             unallocated_encoding(s);
7702             return;
7703         }
7704     } else {
7705         if (size > 3
7706             || (size < 3 && is_q)
7707             || (size == 3 && !is_q)) {
7708             unallocated_encoding(s);
7709             return;
7710         }
7711     }
7712 
7713     if (!fp_access_check(s)) {
7714         return;
7715     }
7716 
7717     element = extract32(imm5, 1+size, 4);
7718 
7719     tcg_rd = cpu_reg(s, rd);
7720     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7721     if (is_signed && !is_q) {
7722         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7723     }
7724 }
7725 
7726 /* AdvSIMD copy
7727  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7728  * +---+---+----+-----------------+------+---+------+---+------+------+
7729  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7730  * +---+---+----+-----------------+------+---+------+---+------+------+
7731  */
7732 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7733 {
7734     int rd = extract32(insn, 0, 5);
7735     int rn = extract32(insn, 5, 5);
7736     int imm4 = extract32(insn, 11, 4);
7737     int op = extract32(insn, 29, 1);
7738     int is_q = extract32(insn, 30, 1);
7739     int imm5 = extract32(insn, 16, 5);
7740 
7741     if (op) {
7742         if (is_q) {
7743             /* INS (element) */
7744             handle_simd_inse(s, rd, rn, imm4, imm5);
7745         } else {
7746             unallocated_encoding(s);
7747         }
7748     } else {
7749         switch (imm4) {
7750         case 0:
7751             /* DUP (element - vector) */
7752             handle_simd_dupe(s, is_q, rd, rn, imm5);
7753             break;
7754         case 1:
7755             /* DUP (general) */
7756             handle_simd_dupg(s, is_q, rd, rn, imm5);
7757             break;
7758         case 3:
7759             if (is_q) {
7760                 /* INS (general) */
7761                 handle_simd_insg(s, rd, rn, imm5);
7762             } else {
7763                 unallocated_encoding(s);
7764             }
7765             break;
7766         case 5:
7767         case 7:
7768             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7769             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7770             break;
7771         default:
7772             unallocated_encoding(s);
7773             break;
7774         }
7775     }
7776 }
7777 
7778 /* AdvSIMD modified immediate
7779  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7780  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7781  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7782  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7783  *
7784  * There are a number of operations that can be carried out here:
7785  *   MOVI - move (shifted) imm into register
7786  *   MVNI - move inverted (shifted) imm into register
7787  *   ORR  - bitwise OR of (shifted) imm with register
7788  *   BIC  - bitwise clear of (shifted) imm with register
7789  * With ARMv8.2 we also have:
7790  *   FMOV half-precision
7791  */
7792 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7793 {
7794     int rd = extract32(insn, 0, 5);
7795     int cmode = extract32(insn, 12, 4);
7796     int o2 = extract32(insn, 11, 1);
7797     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7798     bool is_neg = extract32(insn, 29, 1);
7799     bool is_q = extract32(insn, 30, 1);
7800     uint64_t imm = 0;
7801 
7802     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7803         /* Check for FMOV (vector, immediate) - half-precision */
7804         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7805             unallocated_encoding(s);
7806             return;
7807         }
7808     }
7809 
7810     if (!fp_access_check(s)) {
7811         return;
7812     }
7813 
7814     if (cmode == 15 && o2 && !is_neg) {
7815         /* FMOV (vector, immediate) - half-precision */
7816         imm = vfp_expand_imm(MO_16, abcdefgh);
7817         /* now duplicate across the lanes */
7818         imm = dup_const(MO_16, imm);
7819     } else {
7820         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7821     }
7822 
7823     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7824         /* MOVI or MVNI, with MVNI negation handled above.  */
7825         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7826                              vec_full_reg_size(s), imm);
7827     } else {
7828         /* ORR or BIC, with BIC negation to AND handled above.  */
7829         if (is_neg) {
7830             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7831         } else {
7832             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7833         }
7834     }
7835 }
7836 
7837 /* AdvSIMD scalar copy
7838  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7839  * +-----+----+-----------------+------+---+------+---+------+------+
7840  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7841  * +-----+----+-----------------+------+---+------+---+------+------+
7842  */
7843 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7844 {
7845     int rd = extract32(insn, 0, 5);
7846     int rn = extract32(insn, 5, 5);
7847     int imm4 = extract32(insn, 11, 4);
7848     int imm5 = extract32(insn, 16, 5);
7849     int op = extract32(insn, 29, 1);
7850 
7851     if (op != 0 || imm4 != 0) {
7852         unallocated_encoding(s);
7853         return;
7854     }
7855 
7856     /* DUP (element, scalar) */
7857     handle_simd_dupes(s, rd, rn, imm5);
7858 }
7859 
7860 /* AdvSIMD scalar pairwise
7861  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7862  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7863  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7864  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7865  */
7866 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7867 {
7868     int u = extract32(insn, 29, 1);
7869     int size = extract32(insn, 22, 2);
7870     int opcode = extract32(insn, 12, 5);
7871     int rn = extract32(insn, 5, 5);
7872     int rd = extract32(insn, 0, 5);
7873     TCGv_ptr fpst;
7874 
7875     /* For some ops (the FP ones), size[1] is part of the encoding.
7876      * For ADDP strictly it is not but size[1] is always 1 for valid
7877      * encodings.
7878      */
7879     opcode |= (extract32(size, 1, 1) << 5);
7880 
7881     switch (opcode) {
7882     case 0x3b: /* ADDP */
7883         if (u || size != 3) {
7884             unallocated_encoding(s);
7885             return;
7886         }
7887         if (!fp_access_check(s)) {
7888             return;
7889         }
7890 
7891         fpst = NULL;
7892         break;
7893     case 0xc: /* FMAXNMP */
7894     case 0xd: /* FADDP */
7895     case 0xf: /* FMAXP */
7896     case 0x2c: /* FMINNMP */
7897     case 0x2f: /* FMINP */
7898         /* FP op, size[0] is 32 or 64 bit*/
7899         if (!u) {
7900             if (!dc_isar_feature(aa64_fp16, s)) {
7901                 unallocated_encoding(s);
7902                 return;
7903             } else {
7904                 size = MO_16;
7905             }
7906         } else {
7907             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7908         }
7909 
7910         if (!fp_access_check(s)) {
7911             return;
7912         }
7913 
7914         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7915         break;
7916     default:
7917         unallocated_encoding(s);
7918         return;
7919     }
7920 
7921     if (size == MO_64) {
7922         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7923         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7924         TCGv_i64 tcg_res = tcg_temp_new_i64();
7925 
7926         read_vec_element(s, tcg_op1, rn, 0, MO_64);
7927         read_vec_element(s, tcg_op2, rn, 1, MO_64);
7928 
7929         switch (opcode) {
7930         case 0x3b: /* ADDP */
7931             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7932             break;
7933         case 0xc: /* FMAXNMP */
7934             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7935             break;
7936         case 0xd: /* FADDP */
7937             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7938             break;
7939         case 0xf: /* FMAXP */
7940             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7941             break;
7942         case 0x2c: /* FMINNMP */
7943             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7944             break;
7945         case 0x2f: /* FMINP */
7946             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7947             break;
7948         default:
7949             g_assert_not_reached();
7950         }
7951 
7952         write_fp_dreg(s, rd, tcg_res);
7953     } else {
7954         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7955         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7956         TCGv_i32 tcg_res = tcg_temp_new_i32();
7957 
7958         read_vec_element_i32(s, tcg_op1, rn, 0, size);
7959         read_vec_element_i32(s, tcg_op2, rn, 1, size);
7960 
7961         if (size == MO_16) {
7962             switch (opcode) {
7963             case 0xc: /* FMAXNMP */
7964                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7965                 break;
7966             case 0xd: /* FADDP */
7967                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7968                 break;
7969             case 0xf: /* FMAXP */
7970                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7971                 break;
7972             case 0x2c: /* FMINNMP */
7973                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7974                 break;
7975             case 0x2f: /* FMINP */
7976                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7977                 break;
7978             default:
7979                 g_assert_not_reached();
7980             }
7981         } else {
7982             switch (opcode) {
7983             case 0xc: /* FMAXNMP */
7984                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7985                 break;
7986             case 0xd: /* FADDP */
7987                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7988                 break;
7989             case 0xf: /* FMAXP */
7990                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7991                 break;
7992             case 0x2c: /* FMINNMP */
7993                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7994                 break;
7995             case 0x2f: /* FMINP */
7996                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7997                 break;
7998             default:
7999                 g_assert_not_reached();
8000             }
8001         }
8002 
8003         write_fp_sreg(s, rd, tcg_res);
8004     }
8005 }
8006 
8007 /*
8008  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8009  *
8010  * This code is handles the common shifting code and is used by both
8011  * the vector and scalar code.
8012  */
8013 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8014                                     TCGv_i64 tcg_rnd, bool accumulate,
8015                                     bool is_u, int size, int shift)
8016 {
8017     bool extended_result = false;
8018     bool round = tcg_rnd != NULL;
8019     int ext_lshift = 0;
8020     TCGv_i64 tcg_src_hi;
8021 
8022     if (round && size == 3) {
8023         extended_result = true;
8024         ext_lshift = 64 - shift;
8025         tcg_src_hi = tcg_temp_new_i64();
8026     } else if (shift == 64) {
8027         if (!accumulate && is_u) {
8028             /* result is zero */
8029             tcg_gen_movi_i64(tcg_res, 0);
8030             return;
8031         }
8032     }
8033 
8034     /* Deal with the rounding step */
8035     if (round) {
8036         if (extended_result) {
8037             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8038             if (!is_u) {
8039                 /* take care of sign extending tcg_res */
8040                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8041                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8042                                  tcg_src, tcg_src_hi,
8043                                  tcg_rnd, tcg_zero);
8044             } else {
8045                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8046                                  tcg_src, tcg_zero,
8047                                  tcg_rnd, tcg_zero);
8048             }
8049         } else {
8050             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8051         }
8052     }
8053 
8054     /* Now do the shift right */
8055     if (round && extended_result) {
8056         /* extended case, >64 bit precision required */
8057         if (ext_lshift == 0) {
8058             /* special case, only high bits matter */
8059             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8060         } else {
8061             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8062             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8063             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8064         }
8065     } else {
8066         if (is_u) {
8067             if (shift == 64) {
8068                 /* essentially shifting in 64 zeros */
8069                 tcg_gen_movi_i64(tcg_src, 0);
8070             } else {
8071                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8072             }
8073         } else {
8074             if (shift == 64) {
8075                 /* effectively extending the sign-bit */
8076                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8077             } else {
8078                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8079             }
8080         }
8081     }
8082 
8083     if (accumulate) {
8084         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8085     } else {
8086         tcg_gen_mov_i64(tcg_res, tcg_src);
8087     }
8088 }
8089 
8090 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8091 static void handle_scalar_simd_shri(DisasContext *s,
8092                                     bool is_u, int immh, int immb,
8093                                     int opcode, int rn, int rd)
8094 {
8095     const int size = 3;
8096     int immhb = immh << 3 | immb;
8097     int shift = 2 * (8 << size) - immhb;
8098     bool accumulate = false;
8099     bool round = false;
8100     bool insert = false;
8101     TCGv_i64 tcg_rn;
8102     TCGv_i64 tcg_rd;
8103     TCGv_i64 tcg_round;
8104 
8105     if (!extract32(immh, 3, 1)) {
8106         unallocated_encoding(s);
8107         return;
8108     }
8109 
8110     if (!fp_access_check(s)) {
8111         return;
8112     }
8113 
8114     switch (opcode) {
8115     case 0x02: /* SSRA / USRA (accumulate) */
8116         accumulate = true;
8117         break;
8118     case 0x04: /* SRSHR / URSHR (rounding) */
8119         round = true;
8120         break;
8121     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8122         accumulate = round = true;
8123         break;
8124     case 0x08: /* SRI */
8125         insert = true;
8126         break;
8127     }
8128 
8129     if (round) {
8130         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8131     } else {
8132         tcg_round = NULL;
8133     }
8134 
8135     tcg_rn = read_fp_dreg(s, rn);
8136     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8137 
8138     if (insert) {
8139         /* shift count same as element size is valid but does nothing;
8140          * special case to avoid potential shift by 64.
8141          */
8142         int esize = 8 << size;
8143         if (shift != esize) {
8144             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8145             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8146         }
8147     } else {
8148         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8149                                 accumulate, is_u, size, shift);
8150     }
8151 
8152     write_fp_dreg(s, rd, tcg_rd);
8153 }
8154 
8155 /* SHL/SLI - Scalar shift left */
8156 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8157                                     int immh, int immb, int opcode,
8158                                     int rn, int rd)
8159 {
8160     int size = 32 - clz32(immh) - 1;
8161     int immhb = immh << 3 | immb;
8162     int shift = immhb - (8 << size);
8163     TCGv_i64 tcg_rn;
8164     TCGv_i64 tcg_rd;
8165 
8166     if (!extract32(immh, 3, 1)) {
8167         unallocated_encoding(s);
8168         return;
8169     }
8170 
8171     if (!fp_access_check(s)) {
8172         return;
8173     }
8174 
8175     tcg_rn = read_fp_dreg(s, rn);
8176     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8177 
8178     if (insert) {
8179         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8180     } else {
8181         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8182     }
8183 
8184     write_fp_dreg(s, rd, tcg_rd);
8185 }
8186 
8187 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8188  * (signed/unsigned) narrowing */
8189 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8190                                    bool is_u_shift, bool is_u_narrow,
8191                                    int immh, int immb, int opcode,
8192                                    int rn, int rd)
8193 {
8194     int immhb = immh << 3 | immb;
8195     int size = 32 - clz32(immh) - 1;
8196     int esize = 8 << size;
8197     int shift = (2 * esize) - immhb;
8198     int elements = is_scalar ? 1 : (64 / esize);
8199     bool round = extract32(opcode, 0, 1);
8200     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8201     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8202     TCGv_i32 tcg_rd_narrowed;
8203     TCGv_i64 tcg_final;
8204 
8205     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8206         { gen_helper_neon_narrow_sat_s8,
8207           gen_helper_neon_unarrow_sat8 },
8208         { gen_helper_neon_narrow_sat_s16,
8209           gen_helper_neon_unarrow_sat16 },
8210         { gen_helper_neon_narrow_sat_s32,
8211           gen_helper_neon_unarrow_sat32 },
8212         { NULL, NULL },
8213     };
8214     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8215         gen_helper_neon_narrow_sat_u8,
8216         gen_helper_neon_narrow_sat_u16,
8217         gen_helper_neon_narrow_sat_u32,
8218         NULL
8219     };
8220     NeonGenNarrowEnvFn *narrowfn;
8221 
8222     int i;
8223 
8224     assert(size < 4);
8225 
8226     if (extract32(immh, 3, 1)) {
8227         unallocated_encoding(s);
8228         return;
8229     }
8230 
8231     if (!fp_access_check(s)) {
8232         return;
8233     }
8234 
8235     if (is_u_shift) {
8236         narrowfn = unsigned_narrow_fns[size];
8237     } else {
8238         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8239     }
8240 
8241     tcg_rn = tcg_temp_new_i64();
8242     tcg_rd = tcg_temp_new_i64();
8243     tcg_rd_narrowed = tcg_temp_new_i32();
8244     tcg_final = tcg_temp_new_i64();
8245 
8246     if (round) {
8247         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8248     } else {
8249         tcg_round = NULL;
8250     }
8251 
8252     for (i = 0; i < elements; i++) {
8253         read_vec_element(s, tcg_rn, rn, i, ldop);
8254         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8255                                 false, is_u_shift, size+1, shift);
8256         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8257         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8258         if (i == 0) {
8259             tcg_gen_mov_i64(tcg_final, tcg_rd);
8260         } else {
8261             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8262         }
8263     }
8264 
8265     if (!is_q) {
8266         write_vec_element(s, tcg_final, rd, 0, MO_64);
8267     } else {
8268         write_vec_element(s, tcg_final, rd, 1, MO_64);
8269     }
8270     clear_vec_high(s, is_q, rd);
8271 }
8272 
8273 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8274 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8275                              bool src_unsigned, bool dst_unsigned,
8276                              int immh, int immb, int rn, int rd)
8277 {
8278     int immhb = immh << 3 | immb;
8279     int size = 32 - clz32(immh) - 1;
8280     int shift = immhb - (8 << size);
8281     int pass;
8282 
8283     assert(immh != 0);
8284     assert(!(scalar && is_q));
8285 
8286     if (!scalar) {
8287         if (!is_q && extract32(immh, 3, 1)) {
8288             unallocated_encoding(s);
8289             return;
8290         }
8291 
8292         /* Since we use the variable-shift helpers we must
8293          * replicate the shift count into each element of
8294          * the tcg_shift value.
8295          */
8296         switch (size) {
8297         case 0:
8298             shift |= shift << 8;
8299             /* fall through */
8300         case 1:
8301             shift |= shift << 16;
8302             break;
8303         case 2:
8304         case 3:
8305             break;
8306         default:
8307             g_assert_not_reached();
8308         }
8309     }
8310 
8311     if (!fp_access_check(s)) {
8312         return;
8313     }
8314 
8315     if (size == 3) {
8316         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8317         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8318             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8319             { NULL, gen_helper_neon_qshl_u64 },
8320         };
8321         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8322         int maxpass = is_q ? 2 : 1;
8323 
8324         for (pass = 0; pass < maxpass; pass++) {
8325             TCGv_i64 tcg_op = tcg_temp_new_i64();
8326 
8327             read_vec_element(s, tcg_op, rn, pass, MO_64);
8328             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8329             write_vec_element(s, tcg_op, rd, pass, MO_64);
8330         }
8331         clear_vec_high(s, is_q, rd);
8332     } else {
8333         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8334         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8335             {
8336                 { gen_helper_neon_qshl_s8,
8337                   gen_helper_neon_qshl_s16,
8338                   gen_helper_neon_qshl_s32 },
8339                 { gen_helper_neon_qshlu_s8,
8340                   gen_helper_neon_qshlu_s16,
8341                   gen_helper_neon_qshlu_s32 }
8342             }, {
8343                 { NULL, NULL, NULL },
8344                 { gen_helper_neon_qshl_u8,
8345                   gen_helper_neon_qshl_u16,
8346                   gen_helper_neon_qshl_u32 }
8347             }
8348         };
8349         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8350         MemOp memop = scalar ? size : MO_32;
8351         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8352 
8353         for (pass = 0; pass < maxpass; pass++) {
8354             TCGv_i32 tcg_op = tcg_temp_new_i32();
8355 
8356             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8357             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8358             if (scalar) {
8359                 switch (size) {
8360                 case 0:
8361                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8362                     break;
8363                 case 1:
8364                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8365                     break;
8366                 case 2:
8367                     break;
8368                 default:
8369                     g_assert_not_reached();
8370                 }
8371                 write_fp_sreg(s, rd, tcg_op);
8372             } else {
8373                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8374             }
8375         }
8376 
8377         if (!scalar) {
8378             clear_vec_high(s, is_q, rd);
8379         }
8380     }
8381 }
8382 
8383 /* Common vector code for handling integer to FP conversion */
8384 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8385                                    int elements, int is_signed,
8386                                    int fracbits, int size)
8387 {
8388     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8389     TCGv_i32 tcg_shift = NULL;
8390 
8391     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8392     int pass;
8393 
8394     if (fracbits || size == MO_64) {
8395         tcg_shift = tcg_constant_i32(fracbits);
8396     }
8397 
8398     if (size == MO_64) {
8399         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8400         TCGv_i64 tcg_double = tcg_temp_new_i64();
8401 
8402         for (pass = 0; pass < elements; pass++) {
8403             read_vec_element(s, tcg_int64, rn, pass, mop);
8404 
8405             if (is_signed) {
8406                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8407                                      tcg_shift, tcg_fpst);
8408             } else {
8409                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8410                                      tcg_shift, tcg_fpst);
8411             }
8412             if (elements == 1) {
8413                 write_fp_dreg(s, rd, tcg_double);
8414             } else {
8415                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8416             }
8417         }
8418     } else {
8419         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8420         TCGv_i32 tcg_float = tcg_temp_new_i32();
8421 
8422         for (pass = 0; pass < elements; pass++) {
8423             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8424 
8425             switch (size) {
8426             case MO_32:
8427                 if (fracbits) {
8428                     if (is_signed) {
8429                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8430                                              tcg_shift, tcg_fpst);
8431                     } else {
8432                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8433                                              tcg_shift, tcg_fpst);
8434                     }
8435                 } else {
8436                     if (is_signed) {
8437                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8438                     } else {
8439                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8440                     }
8441                 }
8442                 break;
8443             case MO_16:
8444                 if (fracbits) {
8445                     if (is_signed) {
8446                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8447                                              tcg_shift, tcg_fpst);
8448                     } else {
8449                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8450                                              tcg_shift, tcg_fpst);
8451                     }
8452                 } else {
8453                     if (is_signed) {
8454                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8455                     } else {
8456                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8457                     }
8458                 }
8459                 break;
8460             default:
8461                 g_assert_not_reached();
8462             }
8463 
8464             if (elements == 1) {
8465                 write_fp_sreg(s, rd, tcg_float);
8466             } else {
8467                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8468             }
8469         }
8470     }
8471 
8472     clear_vec_high(s, elements << size == 16, rd);
8473 }
8474 
8475 /* UCVTF/SCVTF - Integer to FP conversion */
8476 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8477                                          bool is_q, bool is_u,
8478                                          int immh, int immb, int opcode,
8479                                          int rn, int rd)
8480 {
8481     int size, elements, fracbits;
8482     int immhb = immh << 3 | immb;
8483 
8484     if (immh & 8) {
8485         size = MO_64;
8486         if (!is_scalar && !is_q) {
8487             unallocated_encoding(s);
8488             return;
8489         }
8490     } else if (immh & 4) {
8491         size = MO_32;
8492     } else if (immh & 2) {
8493         size = MO_16;
8494         if (!dc_isar_feature(aa64_fp16, s)) {
8495             unallocated_encoding(s);
8496             return;
8497         }
8498     } else {
8499         /* immh == 0 would be a failure of the decode logic */
8500         g_assert(immh == 1);
8501         unallocated_encoding(s);
8502         return;
8503     }
8504 
8505     if (is_scalar) {
8506         elements = 1;
8507     } else {
8508         elements = (8 << is_q) >> size;
8509     }
8510     fracbits = (16 << size) - immhb;
8511 
8512     if (!fp_access_check(s)) {
8513         return;
8514     }
8515 
8516     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8517 }
8518 
8519 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8520 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8521                                          bool is_q, bool is_u,
8522                                          int immh, int immb, int rn, int rd)
8523 {
8524     int immhb = immh << 3 | immb;
8525     int pass, size, fracbits;
8526     TCGv_ptr tcg_fpstatus;
8527     TCGv_i32 tcg_rmode, tcg_shift;
8528 
8529     if (immh & 0x8) {
8530         size = MO_64;
8531         if (!is_scalar && !is_q) {
8532             unallocated_encoding(s);
8533             return;
8534         }
8535     } else if (immh & 0x4) {
8536         size = MO_32;
8537     } else if (immh & 0x2) {
8538         size = MO_16;
8539         if (!dc_isar_feature(aa64_fp16, s)) {
8540             unallocated_encoding(s);
8541             return;
8542         }
8543     } else {
8544         /* Should have split out AdvSIMD modified immediate earlier.  */
8545         assert(immh == 1);
8546         unallocated_encoding(s);
8547         return;
8548     }
8549 
8550     if (!fp_access_check(s)) {
8551         return;
8552     }
8553 
8554     assert(!(is_scalar && is_q));
8555 
8556     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8557     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8558     fracbits = (16 << size) - immhb;
8559     tcg_shift = tcg_constant_i32(fracbits);
8560 
8561     if (size == MO_64) {
8562         int maxpass = is_scalar ? 1 : 2;
8563 
8564         for (pass = 0; pass < maxpass; pass++) {
8565             TCGv_i64 tcg_op = tcg_temp_new_i64();
8566 
8567             read_vec_element(s, tcg_op, rn, pass, MO_64);
8568             if (is_u) {
8569                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8570             } else {
8571                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8572             }
8573             write_vec_element(s, tcg_op, rd, pass, MO_64);
8574         }
8575         clear_vec_high(s, is_q, rd);
8576     } else {
8577         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8578         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8579 
8580         switch (size) {
8581         case MO_16:
8582             if (is_u) {
8583                 fn = gen_helper_vfp_touhh;
8584             } else {
8585                 fn = gen_helper_vfp_toshh;
8586             }
8587             break;
8588         case MO_32:
8589             if (is_u) {
8590                 fn = gen_helper_vfp_touls;
8591             } else {
8592                 fn = gen_helper_vfp_tosls;
8593             }
8594             break;
8595         default:
8596             g_assert_not_reached();
8597         }
8598 
8599         for (pass = 0; pass < maxpass; pass++) {
8600             TCGv_i32 tcg_op = tcg_temp_new_i32();
8601 
8602             read_vec_element_i32(s, tcg_op, rn, pass, size);
8603             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8604             if (is_scalar) {
8605                 write_fp_sreg(s, rd, tcg_op);
8606             } else {
8607                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8608             }
8609         }
8610         if (!is_scalar) {
8611             clear_vec_high(s, is_q, rd);
8612         }
8613     }
8614 
8615     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8616 }
8617 
8618 /* AdvSIMD scalar shift by immediate
8619  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8620  * +-----+---+-------------+------+------+--------+---+------+------+
8621  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8622  * +-----+---+-------------+------+------+--------+---+------+------+
8623  *
8624  * This is the scalar version so it works on a fixed sized registers
8625  */
8626 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8627 {
8628     int rd = extract32(insn, 0, 5);
8629     int rn = extract32(insn, 5, 5);
8630     int opcode = extract32(insn, 11, 5);
8631     int immb = extract32(insn, 16, 3);
8632     int immh = extract32(insn, 19, 4);
8633     bool is_u = extract32(insn, 29, 1);
8634 
8635     if (immh == 0) {
8636         unallocated_encoding(s);
8637         return;
8638     }
8639 
8640     switch (opcode) {
8641     case 0x08: /* SRI */
8642         if (!is_u) {
8643             unallocated_encoding(s);
8644             return;
8645         }
8646         /* fall through */
8647     case 0x00: /* SSHR / USHR */
8648     case 0x02: /* SSRA / USRA */
8649     case 0x04: /* SRSHR / URSHR */
8650     case 0x06: /* SRSRA / URSRA */
8651         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8652         break;
8653     case 0x0a: /* SHL / SLI */
8654         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8655         break;
8656     case 0x1c: /* SCVTF, UCVTF */
8657         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8658                                      opcode, rn, rd);
8659         break;
8660     case 0x10: /* SQSHRUN, SQSHRUN2 */
8661     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8662         if (!is_u) {
8663             unallocated_encoding(s);
8664             return;
8665         }
8666         handle_vec_simd_sqshrn(s, true, false, false, true,
8667                                immh, immb, opcode, rn, rd);
8668         break;
8669     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8670     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8671         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8672                                immh, immb, opcode, rn, rd);
8673         break;
8674     case 0xc: /* SQSHLU */
8675         if (!is_u) {
8676             unallocated_encoding(s);
8677             return;
8678         }
8679         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8680         break;
8681     case 0xe: /* SQSHL, UQSHL */
8682         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8683         break;
8684     case 0x1f: /* FCVTZS, FCVTZU */
8685         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8686         break;
8687     default:
8688         unallocated_encoding(s);
8689         break;
8690     }
8691 }
8692 
8693 /* AdvSIMD scalar three different
8694  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8695  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8696  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8697  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8698  */
8699 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8700 {
8701     bool is_u = extract32(insn, 29, 1);
8702     int size = extract32(insn, 22, 2);
8703     int opcode = extract32(insn, 12, 4);
8704     int rm = extract32(insn, 16, 5);
8705     int rn = extract32(insn, 5, 5);
8706     int rd = extract32(insn, 0, 5);
8707 
8708     if (is_u) {
8709         unallocated_encoding(s);
8710         return;
8711     }
8712 
8713     switch (opcode) {
8714     case 0x9: /* SQDMLAL, SQDMLAL2 */
8715     case 0xb: /* SQDMLSL, SQDMLSL2 */
8716     case 0xd: /* SQDMULL, SQDMULL2 */
8717         if (size == 0 || size == 3) {
8718             unallocated_encoding(s);
8719             return;
8720         }
8721         break;
8722     default:
8723         unallocated_encoding(s);
8724         return;
8725     }
8726 
8727     if (!fp_access_check(s)) {
8728         return;
8729     }
8730 
8731     if (size == 2) {
8732         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8733         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8734         TCGv_i64 tcg_res = tcg_temp_new_i64();
8735 
8736         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8737         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8738 
8739         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8740         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
8741 
8742         switch (opcode) {
8743         case 0xd: /* SQDMULL, SQDMULL2 */
8744             break;
8745         case 0xb: /* SQDMLSL, SQDMLSL2 */
8746             tcg_gen_neg_i64(tcg_res, tcg_res);
8747             /* fall through */
8748         case 0x9: /* SQDMLAL, SQDMLAL2 */
8749             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8750             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
8751                                               tcg_res, tcg_op1);
8752             break;
8753         default:
8754             g_assert_not_reached();
8755         }
8756 
8757         write_fp_dreg(s, rd, tcg_res);
8758     } else {
8759         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8760         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8761         TCGv_i64 tcg_res = tcg_temp_new_i64();
8762 
8763         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8764         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
8765 
8766         switch (opcode) {
8767         case 0xd: /* SQDMULL, SQDMULL2 */
8768             break;
8769         case 0xb: /* SQDMLSL, SQDMLSL2 */
8770             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8771             /* fall through */
8772         case 0x9: /* SQDMLAL, SQDMLAL2 */
8773         {
8774             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8775             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8776             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
8777                                               tcg_res, tcg_op3);
8778             break;
8779         }
8780         default:
8781             g_assert_not_reached();
8782         }
8783 
8784         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8785         write_fp_dreg(s, rd, tcg_res);
8786     }
8787 }
8788 
8789 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8790                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8791 {
8792     /* Handle 64x64->64 opcodes which are shared between the scalar
8793      * and vector 3-same groups. We cover every opcode where size == 3
8794      * is valid in either the three-reg-same (integer, not pairwise)
8795      * or scalar-three-reg-same groups.
8796      */
8797     TCGCond cond;
8798 
8799     switch (opcode) {
8800     case 0x1: /* SQADD */
8801         if (u) {
8802             gen_helper_neon_qadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8803         } else {
8804             gen_helper_neon_qadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8805         }
8806         break;
8807     case 0x5: /* SQSUB */
8808         if (u) {
8809             gen_helper_neon_qsub_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8810         } else {
8811             gen_helper_neon_qsub_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8812         }
8813         break;
8814     case 0x6: /* CMGT, CMHI */
8815         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8816     do_cmop:
8817         /* 64 bit integer comparison, result = test ? -1 : 0. */
8818         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8819         break;
8820     case 0x7: /* CMGE, CMHS */
8821         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8822         goto do_cmop;
8823     case 0x11: /* CMTST, CMEQ */
8824         if (u) {
8825             cond = TCG_COND_EQ;
8826             goto do_cmop;
8827         }
8828         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8829         break;
8830     case 0x8: /* SSHL, USHL */
8831         if (u) {
8832             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8833         } else {
8834             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8835         }
8836         break;
8837     case 0x9: /* SQSHL, UQSHL */
8838         if (u) {
8839             gen_helper_neon_qshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8840         } else {
8841             gen_helper_neon_qshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8842         }
8843         break;
8844     case 0xa: /* SRSHL, URSHL */
8845         if (u) {
8846             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8847         } else {
8848             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8849         }
8850         break;
8851     case 0xb: /* SQRSHL, UQRSHL */
8852         if (u) {
8853             gen_helper_neon_qrshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8854         } else {
8855             gen_helper_neon_qrshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8856         }
8857         break;
8858     case 0x10: /* ADD, SUB */
8859         if (u) {
8860             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8861         } else {
8862             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8863         }
8864         break;
8865     default:
8866         g_assert_not_reached();
8867     }
8868 }
8869 
8870 /* Handle the 3-same-operands float operations; shared by the scalar
8871  * and vector encodings. The caller must filter out any encodings
8872  * not allocated for the encoding it is dealing with.
8873  */
8874 static void handle_3same_float(DisasContext *s, int size, int elements,
8875                                int fpopcode, int rd, int rn, int rm)
8876 {
8877     int pass;
8878     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8879 
8880     for (pass = 0; pass < elements; pass++) {
8881         if (size) {
8882             /* Double */
8883             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8884             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8885             TCGv_i64 tcg_res = tcg_temp_new_i64();
8886 
8887             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8888             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8889 
8890             switch (fpopcode) {
8891             case 0x39: /* FMLS */
8892                 /* As usual for ARM, separate negation for fused multiply-add */
8893                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8894                 /* fall through */
8895             case 0x19: /* FMLA */
8896                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8897                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8898                                        tcg_res, fpst);
8899                 break;
8900             case 0x18: /* FMAXNM */
8901                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8902                 break;
8903             case 0x1a: /* FADD */
8904                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8905                 break;
8906             case 0x1b: /* FMULX */
8907                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8908                 break;
8909             case 0x1c: /* FCMEQ */
8910                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8911                 break;
8912             case 0x1e: /* FMAX */
8913                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8914                 break;
8915             case 0x1f: /* FRECPS */
8916                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8917                 break;
8918             case 0x38: /* FMINNM */
8919                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8920                 break;
8921             case 0x3a: /* FSUB */
8922                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8923                 break;
8924             case 0x3e: /* FMIN */
8925                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8926                 break;
8927             case 0x3f: /* FRSQRTS */
8928                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8929                 break;
8930             case 0x5b: /* FMUL */
8931                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8932                 break;
8933             case 0x5c: /* FCMGE */
8934                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8935                 break;
8936             case 0x5d: /* FACGE */
8937                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8938                 break;
8939             case 0x5f: /* FDIV */
8940                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8941                 break;
8942             case 0x7a: /* FABD */
8943                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8944                 gen_helper_vfp_absd(tcg_res, tcg_res);
8945                 break;
8946             case 0x7c: /* FCMGT */
8947                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8948                 break;
8949             case 0x7d: /* FACGT */
8950                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8951                 break;
8952             default:
8953                 g_assert_not_reached();
8954             }
8955 
8956             write_vec_element(s, tcg_res, rd, pass, MO_64);
8957         } else {
8958             /* Single */
8959             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8960             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8961             TCGv_i32 tcg_res = tcg_temp_new_i32();
8962 
8963             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8964             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8965 
8966             switch (fpopcode) {
8967             case 0x39: /* FMLS */
8968                 /* As usual for ARM, separate negation for fused multiply-add */
8969                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8970                 /* fall through */
8971             case 0x19: /* FMLA */
8972                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8973                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8974                                        tcg_res, fpst);
8975                 break;
8976             case 0x1a: /* FADD */
8977                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8978                 break;
8979             case 0x1b: /* FMULX */
8980                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8981                 break;
8982             case 0x1c: /* FCMEQ */
8983                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8984                 break;
8985             case 0x1e: /* FMAX */
8986                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8987                 break;
8988             case 0x1f: /* FRECPS */
8989                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8990                 break;
8991             case 0x18: /* FMAXNM */
8992                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8993                 break;
8994             case 0x38: /* FMINNM */
8995                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8996                 break;
8997             case 0x3a: /* FSUB */
8998                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8999                 break;
9000             case 0x3e: /* FMIN */
9001                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9002                 break;
9003             case 0x3f: /* FRSQRTS */
9004                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9005                 break;
9006             case 0x5b: /* FMUL */
9007                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9008                 break;
9009             case 0x5c: /* FCMGE */
9010                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9011                 break;
9012             case 0x5d: /* FACGE */
9013                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9014                 break;
9015             case 0x5f: /* FDIV */
9016                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9017                 break;
9018             case 0x7a: /* FABD */
9019                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9020                 gen_helper_vfp_abss(tcg_res, tcg_res);
9021                 break;
9022             case 0x7c: /* FCMGT */
9023                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9024                 break;
9025             case 0x7d: /* FACGT */
9026                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9027                 break;
9028             default:
9029                 g_assert_not_reached();
9030             }
9031 
9032             if (elements == 1) {
9033                 /* scalar single so clear high part */
9034                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9035 
9036                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9037                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9038             } else {
9039                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9040             }
9041         }
9042     }
9043 
9044     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9045 }
9046 
9047 /* AdvSIMD scalar three same
9048  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9049  * +-----+---+-----------+------+---+------+--------+---+------+------+
9050  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9051  * +-----+---+-----------+------+---+------+--------+---+------+------+
9052  */
9053 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9054 {
9055     int rd = extract32(insn, 0, 5);
9056     int rn = extract32(insn, 5, 5);
9057     int opcode = extract32(insn, 11, 5);
9058     int rm = extract32(insn, 16, 5);
9059     int size = extract32(insn, 22, 2);
9060     bool u = extract32(insn, 29, 1);
9061     TCGv_i64 tcg_rd;
9062 
9063     if (opcode >= 0x18) {
9064         /* Floating point: U, size[1] and opcode indicate operation */
9065         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9066         switch (fpopcode) {
9067         case 0x1b: /* FMULX */
9068         case 0x1f: /* FRECPS */
9069         case 0x3f: /* FRSQRTS */
9070         case 0x5d: /* FACGE */
9071         case 0x7d: /* FACGT */
9072         case 0x1c: /* FCMEQ */
9073         case 0x5c: /* FCMGE */
9074         case 0x7c: /* FCMGT */
9075         case 0x7a: /* FABD */
9076             break;
9077         default:
9078             unallocated_encoding(s);
9079             return;
9080         }
9081 
9082         if (!fp_access_check(s)) {
9083             return;
9084         }
9085 
9086         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9087         return;
9088     }
9089 
9090     switch (opcode) {
9091     case 0x1: /* SQADD, UQADD */
9092     case 0x5: /* SQSUB, UQSUB */
9093     case 0x9: /* SQSHL, UQSHL */
9094     case 0xb: /* SQRSHL, UQRSHL */
9095         break;
9096     case 0x8: /* SSHL, USHL */
9097     case 0xa: /* SRSHL, URSHL */
9098     case 0x6: /* CMGT, CMHI */
9099     case 0x7: /* CMGE, CMHS */
9100     case 0x11: /* CMTST, CMEQ */
9101     case 0x10: /* ADD, SUB (vector) */
9102         if (size != 3) {
9103             unallocated_encoding(s);
9104             return;
9105         }
9106         break;
9107     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9108         if (size != 1 && size != 2) {
9109             unallocated_encoding(s);
9110             return;
9111         }
9112         break;
9113     default:
9114         unallocated_encoding(s);
9115         return;
9116     }
9117 
9118     if (!fp_access_check(s)) {
9119         return;
9120     }
9121 
9122     tcg_rd = tcg_temp_new_i64();
9123 
9124     if (size == 3) {
9125         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9126         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9127 
9128         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9129     } else {
9130         /* Do a single operation on the lowest element in the vector.
9131          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9132          * no side effects for all these operations.
9133          * OPTME: special-purpose helpers would avoid doing some
9134          * unnecessary work in the helper for the 8 and 16 bit cases.
9135          */
9136         NeonGenTwoOpEnvFn *genenvfn;
9137         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9138         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9139         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9140 
9141         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9142         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9143 
9144         switch (opcode) {
9145         case 0x1: /* SQADD, UQADD */
9146         {
9147             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9148                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9149                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9150                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9151             };
9152             genenvfn = fns[size][u];
9153             break;
9154         }
9155         case 0x5: /* SQSUB, UQSUB */
9156         {
9157             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9158                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9159                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9160                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9161             };
9162             genenvfn = fns[size][u];
9163             break;
9164         }
9165         case 0x9: /* SQSHL, UQSHL */
9166         {
9167             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9168                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9169                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9170                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9171             };
9172             genenvfn = fns[size][u];
9173             break;
9174         }
9175         case 0xb: /* SQRSHL, UQRSHL */
9176         {
9177             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9178                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9179                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9180                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9181             };
9182             genenvfn = fns[size][u];
9183             break;
9184         }
9185         case 0x16: /* SQDMULH, SQRDMULH */
9186         {
9187             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9188                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9189                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9190             };
9191             assert(size == 1 || size == 2);
9192             genenvfn = fns[size - 1][u];
9193             break;
9194         }
9195         default:
9196             g_assert_not_reached();
9197         }
9198 
9199         genenvfn(tcg_rd32, tcg_env, tcg_rn, tcg_rm);
9200         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9201     }
9202 
9203     write_fp_dreg(s, rd, tcg_rd);
9204 }
9205 
9206 /* AdvSIMD scalar three same FP16
9207  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9208  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9209  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9210  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9211  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9212  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9213  */
9214 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9215                                                   uint32_t insn)
9216 {
9217     int rd = extract32(insn, 0, 5);
9218     int rn = extract32(insn, 5, 5);
9219     int opcode = extract32(insn, 11, 3);
9220     int rm = extract32(insn, 16, 5);
9221     bool u = extract32(insn, 29, 1);
9222     bool a = extract32(insn, 23, 1);
9223     int fpopcode = opcode | (a << 3) |  (u << 4);
9224     TCGv_ptr fpst;
9225     TCGv_i32 tcg_op1;
9226     TCGv_i32 tcg_op2;
9227     TCGv_i32 tcg_res;
9228 
9229     switch (fpopcode) {
9230     case 0x03: /* FMULX */
9231     case 0x04: /* FCMEQ (reg) */
9232     case 0x07: /* FRECPS */
9233     case 0x0f: /* FRSQRTS */
9234     case 0x14: /* FCMGE (reg) */
9235     case 0x15: /* FACGE */
9236     case 0x1a: /* FABD */
9237     case 0x1c: /* FCMGT (reg) */
9238     case 0x1d: /* FACGT */
9239         break;
9240     default:
9241         unallocated_encoding(s);
9242         return;
9243     }
9244 
9245     if (!dc_isar_feature(aa64_fp16, s)) {
9246         unallocated_encoding(s);
9247     }
9248 
9249     if (!fp_access_check(s)) {
9250         return;
9251     }
9252 
9253     fpst = fpstatus_ptr(FPST_FPCR_F16);
9254 
9255     tcg_op1 = read_fp_hreg(s, rn);
9256     tcg_op2 = read_fp_hreg(s, rm);
9257     tcg_res = tcg_temp_new_i32();
9258 
9259     switch (fpopcode) {
9260     case 0x03: /* FMULX */
9261         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9262         break;
9263     case 0x04: /* FCMEQ (reg) */
9264         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9265         break;
9266     case 0x07: /* FRECPS */
9267         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9268         break;
9269     case 0x0f: /* FRSQRTS */
9270         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9271         break;
9272     case 0x14: /* FCMGE (reg) */
9273         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9274         break;
9275     case 0x15: /* FACGE */
9276         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9277         break;
9278     case 0x1a: /* FABD */
9279         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9280         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9281         break;
9282     case 0x1c: /* FCMGT (reg) */
9283         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9284         break;
9285     case 0x1d: /* FACGT */
9286         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9287         break;
9288     default:
9289         g_assert_not_reached();
9290     }
9291 
9292     write_fp_sreg(s, rd, tcg_res);
9293 }
9294 
9295 /* AdvSIMD scalar three same extra
9296  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9297  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9298  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9299  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9300  */
9301 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9302                                                    uint32_t insn)
9303 {
9304     int rd = extract32(insn, 0, 5);
9305     int rn = extract32(insn, 5, 5);
9306     int opcode = extract32(insn, 11, 4);
9307     int rm = extract32(insn, 16, 5);
9308     int size = extract32(insn, 22, 2);
9309     bool u = extract32(insn, 29, 1);
9310     TCGv_i32 ele1, ele2, ele3;
9311     TCGv_i64 res;
9312     bool feature;
9313 
9314     switch (u * 16 + opcode) {
9315     case 0x10: /* SQRDMLAH (vector) */
9316     case 0x11: /* SQRDMLSH (vector) */
9317         if (size != 1 && size != 2) {
9318             unallocated_encoding(s);
9319             return;
9320         }
9321         feature = dc_isar_feature(aa64_rdm, s);
9322         break;
9323     default:
9324         unallocated_encoding(s);
9325         return;
9326     }
9327     if (!feature) {
9328         unallocated_encoding(s);
9329         return;
9330     }
9331     if (!fp_access_check(s)) {
9332         return;
9333     }
9334 
9335     /* Do a single operation on the lowest element in the vector.
9336      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9337      * with no side effects for all these operations.
9338      * OPTME: special-purpose helpers would avoid doing some
9339      * unnecessary work in the helper for the 16 bit cases.
9340      */
9341     ele1 = tcg_temp_new_i32();
9342     ele2 = tcg_temp_new_i32();
9343     ele3 = tcg_temp_new_i32();
9344 
9345     read_vec_element_i32(s, ele1, rn, 0, size);
9346     read_vec_element_i32(s, ele2, rm, 0, size);
9347     read_vec_element_i32(s, ele3, rd, 0, size);
9348 
9349     switch (opcode) {
9350     case 0x0: /* SQRDMLAH */
9351         if (size == 1) {
9352             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9353         } else {
9354             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9355         }
9356         break;
9357     case 0x1: /* SQRDMLSH */
9358         if (size == 1) {
9359             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9360         } else {
9361             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9362         }
9363         break;
9364     default:
9365         g_assert_not_reached();
9366     }
9367 
9368     res = tcg_temp_new_i64();
9369     tcg_gen_extu_i32_i64(res, ele3);
9370     write_fp_dreg(s, rd, res);
9371 }
9372 
9373 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9374                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9375                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9376 {
9377     /* Handle 64->64 opcodes which are shared between the scalar and
9378      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9379      * is valid in either group and also the double-precision fp ops.
9380      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9381      * requires them.
9382      */
9383     TCGCond cond;
9384 
9385     switch (opcode) {
9386     case 0x4: /* CLS, CLZ */
9387         if (u) {
9388             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9389         } else {
9390             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9391         }
9392         break;
9393     case 0x5: /* NOT */
9394         /* This opcode is shared with CNT and RBIT but we have earlier
9395          * enforced that size == 3 if and only if this is the NOT insn.
9396          */
9397         tcg_gen_not_i64(tcg_rd, tcg_rn);
9398         break;
9399     case 0x7: /* SQABS, SQNEG */
9400         if (u) {
9401             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9402         } else {
9403             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9404         }
9405         break;
9406     case 0xa: /* CMLT */
9407         cond = TCG_COND_LT;
9408     do_cmop:
9409         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9410         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9411         break;
9412     case 0x8: /* CMGT, CMGE */
9413         cond = u ? TCG_COND_GE : TCG_COND_GT;
9414         goto do_cmop;
9415     case 0x9: /* CMEQ, CMLE */
9416         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9417         goto do_cmop;
9418     case 0xb: /* ABS, NEG */
9419         if (u) {
9420             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9421         } else {
9422             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9423         }
9424         break;
9425     case 0x2f: /* FABS */
9426         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9427         break;
9428     case 0x6f: /* FNEG */
9429         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9430         break;
9431     case 0x7f: /* FSQRT */
9432         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9433         break;
9434     case 0x1a: /* FCVTNS */
9435     case 0x1b: /* FCVTMS */
9436     case 0x1c: /* FCVTAS */
9437     case 0x3a: /* FCVTPS */
9438     case 0x3b: /* FCVTZS */
9439         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9440         break;
9441     case 0x5a: /* FCVTNU */
9442     case 0x5b: /* FCVTMU */
9443     case 0x5c: /* FCVTAU */
9444     case 0x7a: /* FCVTPU */
9445     case 0x7b: /* FCVTZU */
9446         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9447         break;
9448     case 0x18: /* FRINTN */
9449     case 0x19: /* FRINTM */
9450     case 0x38: /* FRINTP */
9451     case 0x39: /* FRINTZ */
9452     case 0x58: /* FRINTA */
9453     case 0x79: /* FRINTI */
9454         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9455         break;
9456     case 0x59: /* FRINTX */
9457         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9458         break;
9459     case 0x1e: /* FRINT32Z */
9460     case 0x5e: /* FRINT32X */
9461         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9462         break;
9463     case 0x1f: /* FRINT64Z */
9464     case 0x5f: /* FRINT64X */
9465         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9466         break;
9467     default:
9468         g_assert_not_reached();
9469     }
9470 }
9471 
9472 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9473                                    bool is_scalar, bool is_u, bool is_q,
9474                                    int size, int rn, int rd)
9475 {
9476     bool is_double = (size == MO_64);
9477     TCGv_ptr fpst;
9478 
9479     if (!fp_access_check(s)) {
9480         return;
9481     }
9482 
9483     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9484 
9485     if (is_double) {
9486         TCGv_i64 tcg_op = tcg_temp_new_i64();
9487         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9488         TCGv_i64 tcg_res = tcg_temp_new_i64();
9489         NeonGenTwoDoubleOpFn *genfn;
9490         bool swap = false;
9491         int pass;
9492 
9493         switch (opcode) {
9494         case 0x2e: /* FCMLT (zero) */
9495             swap = true;
9496             /* fallthrough */
9497         case 0x2c: /* FCMGT (zero) */
9498             genfn = gen_helper_neon_cgt_f64;
9499             break;
9500         case 0x2d: /* FCMEQ (zero) */
9501             genfn = gen_helper_neon_ceq_f64;
9502             break;
9503         case 0x6d: /* FCMLE (zero) */
9504             swap = true;
9505             /* fall through */
9506         case 0x6c: /* FCMGE (zero) */
9507             genfn = gen_helper_neon_cge_f64;
9508             break;
9509         default:
9510             g_assert_not_reached();
9511         }
9512 
9513         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9514             read_vec_element(s, tcg_op, rn, pass, MO_64);
9515             if (swap) {
9516                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9517             } else {
9518                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9519             }
9520             write_vec_element(s, tcg_res, rd, pass, MO_64);
9521         }
9522 
9523         clear_vec_high(s, !is_scalar, rd);
9524     } else {
9525         TCGv_i32 tcg_op = tcg_temp_new_i32();
9526         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9527         TCGv_i32 tcg_res = tcg_temp_new_i32();
9528         NeonGenTwoSingleOpFn *genfn;
9529         bool swap = false;
9530         int pass, maxpasses;
9531 
9532         if (size == MO_16) {
9533             switch (opcode) {
9534             case 0x2e: /* FCMLT (zero) */
9535                 swap = true;
9536                 /* fall through */
9537             case 0x2c: /* FCMGT (zero) */
9538                 genfn = gen_helper_advsimd_cgt_f16;
9539                 break;
9540             case 0x2d: /* FCMEQ (zero) */
9541                 genfn = gen_helper_advsimd_ceq_f16;
9542                 break;
9543             case 0x6d: /* FCMLE (zero) */
9544                 swap = true;
9545                 /* fall through */
9546             case 0x6c: /* FCMGE (zero) */
9547                 genfn = gen_helper_advsimd_cge_f16;
9548                 break;
9549             default:
9550                 g_assert_not_reached();
9551             }
9552         } else {
9553             switch (opcode) {
9554             case 0x2e: /* FCMLT (zero) */
9555                 swap = true;
9556                 /* fall through */
9557             case 0x2c: /* FCMGT (zero) */
9558                 genfn = gen_helper_neon_cgt_f32;
9559                 break;
9560             case 0x2d: /* FCMEQ (zero) */
9561                 genfn = gen_helper_neon_ceq_f32;
9562                 break;
9563             case 0x6d: /* FCMLE (zero) */
9564                 swap = true;
9565                 /* fall through */
9566             case 0x6c: /* FCMGE (zero) */
9567                 genfn = gen_helper_neon_cge_f32;
9568                 break;
9569             default:
9570                 g_assert_not_reached();
9571             }
9572         }
9573 
9574         if (is_scalar) {
9575             maxpasses = 1;
9576         } else {
9577             int vector_size = 8 << is_q;
9578             maxpasses = vector_size >> size;
9579         }
9580 
9581         for (pass = 0; pass < maxpasses; pass++) {
9582             read_vec_element_i32(s, tcg_op, rn, pass, size);
9583             if (swap) {
9584                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9585             } else {
9586                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9587             }
9588             if (is_scalar) {
9589                 write_fp_sreg(s, rd, tcg_res);
9590             } else {
9591                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9592             }
9593         }
9594 
9595         if (!is_scalar) {
9596             clear_vec_high(s, is_q, rd);
9597         }
9598     }
9599 }
9600 
9601 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9602                                     bool is_scalar, bool is_u, bool is_q,
9603                                     int size, int rn, int rd)
9604 {
9605     bool is_double = (size == 3);
9606     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9607 
9608     if (is_double) {
9609         TCGv_i64 tcg_op = tcg_temp_new_i64();
9610         TCGv_i64 tcg_res = tcg_temp_new_i64();
9611         int pass;
9612 
9613         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9614             read_vec_element(s, tcg_op, rn, pass, MO_64);
9615             switch (opcode) {
9616             case 0x3d: /* FRECPE */
9617                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9618                 break;
9619             case 0x3f: /* FRECPX */
9620                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9621                 break;
9622             case 0x7d: /* FRSQRTE */
9623                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9624                 break;
9625             default:
9626                 g_assert_not_reached();
9627             }
9628             write_vec_element(s, tcg_res, rd, pass, MO_64);
9629         }
9630         clear_vec_high(s, !is_scalar, rd);
9631     } else {
9632         TCGv_i32 tcg_op = tcg_temp_new_i32();
9633         TCGv_i32 tcg_res = tcg_temp_new_i32();
9634         int pass, maxpasses;
9635 
9636         if (is_scalar) {
9637             maxpasses = 1;
9638         } else {
9639             maxpasses = is_q ? 4 : 2;
9640         }
9641 
9642         for (pass = 0; pass < maxpasses; pass++) {
9643             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9644 
9645             switch (opcode) {
9646             case 0x3c: /* URECPE */
9647                 gen_helper_recpe_u32(tcg_res, tcg_op);
9648                 break;
9649             case 0x3d: /* FRECPE */
9650                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9651                 break;
9652             case 0x3f: /* FRECPX */
9653                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9654                 break;
9655             case 0x7d: /* FRSQRTE */
9656                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9657                 break;
9658             default:
9659                 g_assert_not_reached();
9660             }
9661 
9662             if (is_scalar) {
9663                 write_fp_sreg(s, rd, tcg_res);
9664             } else {
9665                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9666             }
9667         }
9668         if (!is_scalar) {
9669             clear_vec_high(s, is_q, rd);
9670         }
9671     }
9672 }
9673 
9674 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9675                                 int opcode, bool u, bool is_q,
9676                                 int size, int rn, int rd)
9677 {
9678     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9679      * in the source becomes a size element in the destination).
9680      */
9681     int pass;
9682     TCGv_i32 tcg_res[2];
9683     int destelt = is_q ? 2 : 0;
9684     int passes = scalar ? 1 : 2;
9685 
9686     if (scalar) {
9687         tcg_res[1] = tcg_constant_i32(0);
9688     }
9689 
9690     for (pass = 0; pass < passes; pass++) {
9691         TCGv_i64 tcg_op = tcg_temp_new_i64();
9692         NeonGenNarrowFn *genfn = NULL;
9693         NeonGenNarrowEnvFn *genenvfn = NULL;
9694 
9695         if (scalar) {
9696             read_vec_element(s, tcg_op, rn, pass, size + 1);
9697         } else {
9698             read_vec_element(s, tcg_op, rn, pass, MO_64);
9699         }
9700         tcg_res[pass] = tcg_temp_new_i32();
9701 
9702         switch (opcode) {
9703         case 0x12: /* XTN, SQXTUN */
9704         {
9705             static NeonGenNarrowFn * const xtnfns[3] = {
9706                 gen_helper_neon_narrow_u8,
9707                 gen_helper_neon_narrow_u16,
9708                 tcg_gen_extrl_i64_i32,
9709             };
9710             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9711                 gen_helper_neon_unarrow_sat8,
9712                 gen_helper_neon_unarrow_sat16,
9713                 gen_helper_neon_unarrow_sat32,
9714             };
9715             if (u) {
9716                 genenvfn = sqxtunfns[size];
9717             } else {
9718                 genfn = xtnfns[size];
9719             }
9720             break;
9721         }
9722         case 0x14: /* SQXTN, UQXTN */
9723         {
9724             static NeonGenNarrowEnvFn * const fns[3][2] = {
9725                 { gen_helper_neon_narrow_sat_s8,
9726                   gen_helper_neon_narrow_sat_u8 },
9727                 { gen_helper_neon_narrow_sat_s16,
9728                   gen_helper_neon_narrow_sat_u16 },
9729                 { gen_helper_neon_narrow_sat_s32,
9730                   gen_helper_neon_narrow_sat_u32 },
9731             };
9732             genenvfn = fns[size][u];
9733             break;
9734         }
9735         case 0x16: /* FCVTN, FCVTN2 */
9736             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9737             if (size == 2) {
9738                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9739             } else {
9740                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9741                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9742                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9743                 TCGv_i32 ahp = get_ahp_flag();
9744 
9745                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9746                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9747                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9748                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9749             }
9750             break;
9751         case 0x36: /* BFCVTN, BFCVTN2 */
9752             {
9753                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9754                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9755             }
9756             break;
9757         case 0x56:  /* FCVTXN, FCVTXN2 */
9758             /* 64 bit to 32 bit float conversion
9759              * with von Neumann rounding (round to odd)
9760              */
9761             assert(size == 2);
9762             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
9763             break;
9764         default:
9765             g_assert_not_reached();
9766         }
9767 
9768         if (genfn) {
9769             genfn(tcg_res[pass], tcg_op);
9770         } else if (genenvfn) {
9771             genenvfn(tcg_res[pass], tcg_env, tcg_op);
9772         }
9773     }
9774 
9775     for (pass = 0; pass < 2; pass++) {
9776         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9777     }
9778     clear_vec_high(s, is_q, rd);
9779 }
9780 
9781 /* Remaining saturating accumulating ops */
9782 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9783                                 bool is_q, int size, int rn, int rd)
9784 {
9785     bool is_double = (size == 3);
9786 
9787     if (is_double) {
9788         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9789         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9790         int pass;
9791 
9792         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9793             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9794             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9795 
9796             if (is_u) { /* USQADD */
9797                 gen_helper_neon_uqadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9798             } else { /* SUQADD */
9799                 gen_helper_neon_sqadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9800             }
9801             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9802         }
9803         clear_vec_high(s, !is_scalar, rd);
9804     } else {
9805         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9806         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9807         int pass, maxpasses;
9808 
9809         if (is_scalar) {
9810             maxpasses = 1;
9811         } else {
9812             maxpasses = is_q ? 4 : 2;
9813         }
9814 
9815         for (pass = 0; pass < maxpasses; pass++) {
9816             if (is_scalar) {
9817                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9818                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9819             } else {
9820                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9821                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9822             }
9823 
9824             if (is_u) { /* USQADD */
9825                 switch (size) {
9826                 case 0:
9827                     gen_helper_neon_uqadd_s8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9828                     break;
9829                 case 1:
9830                     gen_helper_neon_uqadd_s16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9831                     break;
9832                 case 2:
9833                     gen_helper_neon_uqadd_s32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9834                     break;
9835                 default:
9836                     g_assert_not_reached();
9837                 }
9838             } else { /* SUQADD */
9839                 switch (size) {
9840                 case 0:
9841                     gen_helper_neon_sqadd_u8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9842                     break;
9843                 case 1:
9844                     gen_helper_neon_sqadd_u16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9845                     break;
9846                 case 2:
9847                     gen_helper_neon_sqadd_u32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9848                     break;
9849                 default:
9850                     g_assert_not_reached();
9851                 }
9852             }
9853 
9854             if (is_scalar) {
9855                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9856             }
9857             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9858         }
9859         clear_vec_high(s, is_q, rd);
9860     }
9861 }
9862 
9863 /* AdvSIMD scalar two reg misc
9864  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9865  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9866  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9867  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9868  */
9869 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9870 {
9871     int rd = extract32(insn, 0, 5);
9872     int rn = extract32(insn, 5, 5);
9873     int opcode = extract32(insn, 12, 5);
9874     int size = extract32(insn, 22, 2);
9875     bool u = extract32(insn, 29, 1);
9876     bool is_fcvt = false;
9877     int rmode;
9878     TCGv_i32 tcg_rmode;
9879     TCGv_ptr tcg_fpstatus;
9880 
9881     switch (opcode) {
9882     case 0x3: /* USQADD / SUQADD*/
9883         if (!fp_access_check(s)) {
9884             return;
9885         }
9886         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9887         return;
9888     case 0x7: /* SQABS / SQNEG */
9889         break;
9890     case 0xa: /* CMLT */
9891         if (u) {
9892             unallocated_encoding(s);
9893             return;
9894         }
9895         /* fall through */
9896     case 0x8: /* CMGT, CMGE */
9897     case 0x9: /* CMEQ, CMLE */
9898     case 0xb: /* ABS, NEG */
9899         if (size != 3) {
9900             unallocated_encoding(s);
9901             return;
9902         }
9903         break;
9904     case 0x12: /* SQXTUN */
9905         if (!u) {
9906             unallocated_encoding(s);
9907             return;
9908         }
9909         /* fall through */
9910     case 0x14: /* SQXTN, UQXTN */
9911         if (size == 3) {
9912             unallocated_encoding(s);
9913             return;
9914         }
9915         if (!fp_access_check(s)) {
9916             return;
9917         }
9918         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9919         return;
9920     case 0xc ... 0xf:
9921     case 0x16 ... 0x1d:
9922     case 0x1f:
9923         /* Floating point: U, size[1] and opcode indicate operation;
9924          * size[0] indicates single or double precision.
9925          */
9926         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9927         size = extract32(size, 0, 1) ? 3 : 2;
9928         switch (opcode) {
9929         case 0x2c: /* FCMGT (zero) */
9930         case 0x2d: /* FCMEQ (zero) */
9931         case 0x2e: /* FCMLT (zero) */
9932         case 0x6c: /* FCMGE (zero) */
9933         case 0x6d: /* FCMLE (zero) */
9934             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9935             return;
9936         case 0x1d: /* SCVTF */
9937         case 0x5d: /* UCVTF */
9938         {
9939             bool is_signed = (opcode == 0x1d);
9940             if (!fp_access_check(s)) {
9941                 return;
9942             }
9943             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9944             return;
9945         }
9946         case 0x3d: /* FRECPE */
9947         case 0x3f: /* FRECPX */
9948         case 0x7d: /* FRSQRTE */
9949             if (!fp_access_check(s)) {
9950                 return;
9951             }
9952             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9953             return;
9954         case 0x1a: /* FCVTNS */
9955         case 0x1b: /* FCVTMS */
9956         case 0x3a: /* FCVTPS */
9957         case 0x3b: /* FCVTZS */
9958         case 0x5a: /* FCVTNU */
9959         case 0x5b: /* FCVTMU */
9960         case 0x7a: /* FCVTPU */
9961         case 0x7b: /* FCVTZU */
9962             is_fcvt = true;
9963             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9964             break;
9965         case 0x1c: /* FCVTAS */
9966         case 0x5c: /* FCVTAU */
9967             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9968             is_fcvt = true;
9969             rmode = FPROUNDING_TIEAWAY;
9970             break;
9971         case 0x56: /* FCVTXN, FCVTXN2 */
9972             if (size == 2) {
9973                 unallocated_encoding(s);
9974                 return;
9975             }
9976             if (!fp_access_check(s)) {
9977                 return;
9978             }
9979             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9980             return;
9981         default:
9982             unallocated_encoding(s);
9983             return;
9984         }
9985         break;
9986     default:
9987         unallocated_encoding(s);
9988         return;
9989     }
9990 
9991     if (!fp_access_check(s)) {
9992         return;
9993     }
9994 
9995     if (is_fcvt) {
9996         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
9997         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
9998     } else {
9999         tcg_fpstatus = NULL;
10000         tcg_rmode = NULL;
10001     }
10002 
10003     if (size == 3) {
10004         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10005         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10006 
10007         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10008         write_fp_dreg(s, rd, tcg_rd);
10009     } else {
10010         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10011         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10012 
10013         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10014 
10015         switch (opcode) {
10016         case 0x7: /* SQABS, SQNEG */
10017         {
10018             NeonGenOneOpEnvFn *genfn;
10019             static NeonGenOneOpEnvFn * const fns[3][2] = {
10020                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10021                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10022                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10023             };
10024             genfn = fns[size][u];
10025             genfn(tcg_rd, tcg_env, tcg_rn);
10026             break;
10027         }
10028         case 0x1a: /* FCVTNS */
10029         case 0x1b: /* FCVTMS */
10030         case 0x1c: /* FCVTAS */
10031         case 0x3a: /* FCVTPS */
10032         case 0x3b: /* FCVTZS */
10033             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10034                                  tcg_fpstatus);
10035             break;
10036         case 0x5a: /* FCVTNU */
10037         case 0x5b: /* FCVTMU */
10038         case 0x5c: /* FCVTAU */
10039         case 0x7a: /* FCVTPU */
10040         case 0x7b: /* FCVTZU */
10041             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10042                                  tcg_fpstatus);
10043             break;
10044         default:
10045             g_assert_not_reached();
10046         }
10047 
10048         write_fp_sreg(s, rd, tcg_rd);
10049     }
10050 
10051     if (is_fcvt) {
10052         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10053     }
10054 }
10055 
10056 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10057 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10058                                  int immh, int immb, int opcode, int rn, int rd)
10059 {
10060     int size = 32 - clz32(immh) - 1;
10061     int immhb = immh << 3 | immb;
10062     int shift = 2 * (8 << size) - immhb;
10063     GVecGen2iFn *gvec_fn;
10064 
10065     if (extract32(immh, 3, 1) && !is_q) {
10066         unallocated_encoding(s);
10067         return;
10068     }
10069     tcg_debug_assert(size <= 3);
10070 
10071     if (!fp_access_check(s)) {
10072         return;
10073     }
10074 
10075     switch (opcode) {
10076     case 0x02: /* SSRA / USRA (accumulate) */
10077         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10078         break;
10079 
10080     case 0x08: /* SRI */
10081         gvec_fn = gen_gvec_sri;
10082         break;
10083 
10084     case 0x00: /* SSHR / USHR */
10085         if (is_u) {
10086             if (shift == 8 << size) {
10087                 /* Shift count the same size as element size produces zero.  */
10088                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10089                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10090                 return;
10091             }
10092             gvec_fn = tcg_gen_gvec_shri;
10093         } else {
10094             /* Shift count the same size as element size produces all sign.  */
10095             if (shift == 8 << size) {
10096                 shift -= 1;
10097             }
10098             gvec_fn = tcg_gen_gvec_sari;
10099         }
10100         break;
10101 
10102     case 0x04: /* SRSHR / URSHR (rounding) */
10103         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10104         break;
10105 
10106     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10107         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10108         break;
10109 
10110     default:
10111         g_assert_not_reached();
10112     }
10113 
10114     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10115 }
10116 
10117 /* SHL/SLI - Vector shift left */
10118 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10119                                  int immh, int immb, int opcode, int rn, int rd)
10120 {
10121     int size = 32 - clz32(immh) - 1;
10122     int immhb = immh << 3 | immb;
10123     int shift = immhb - (8 << size);
10124 
10125     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10126     assert(size >= 0 && size <= 3);
10127 
10128     if (extract32(immh, 3, 1) && !is_q) {
10129         unallocated_encoding(s);
10130         return;
10131     }
10132 
10133     if (!fp_access_check(s)) {
10134         return;
10135     }
10136 
10137     if (insert) {
10138         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10139     } else {
10140         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10141     }
10142 }
10143 
10144 /* USHLL/SHLL - Vector shift left with widening */
10145 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10146                                  int immh, int immb, int opcode, int rn, int rd)
10147 {
10148     int size = 32 - clz32(immh) - 1;
10149     int immhb = immh << 3 | immb;
10150     int shift = immhb - (8 << size);
10151     int dsize = 64;
10152     int esize = 8 << size;
10153     int elements = dsize/esize;
10154     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10155     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10156     int i;
10157 
10158     if (size >= 3) {
10159         unallocated_encoding(s);
10160         return;
10161     }
10162 
10163     if (!fp_access_check(s)) {
10164         return;
10165     }
10166 
10167     /* For the LL variants the store is larger than the load,
10168      * so if rd == rn we would overwrite parts of our input.
10169      * So load everything right now and use shifts in the main loop.
10170      */
10171     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10172 
10173     for (i = 0; i < elements; i++) {
10174         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10175         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10176         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10177         write_vec_element(s, tcg_rd, rd, i, size + 1);
10178     }
10179 }
10180 
10181 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10182 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10183                                  int immh, int immb, int opcode, int rn, int rd)
10184 {
10185     int immhb = immh << 3 | immb;
10186     int size = 32 - clz32(immh) - 1;
10187     int dsize = 64;
10188     int esize = 8 << size;
10189     int elements = dsize/esize;
10190     int shift = (2 * esize) - immhb;
10191     bool round = extract32(opcode, 0, 1);
10192     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10193     TCGv_i64 tcg_round;
10194     int i;
10195 
10196     if (extract32(immh, 3, 1)) {
10197         unallocated_encoding(s);
10198         return;
10199     }
10200 
10201     if (!fp_access_check(s)) {
10202         return;
10203     }
10204 
10205     tcg_rn = tcg_temp_new_i64();
10206     tcg_rd = tcg_temp_new_i64();
10207     tcg_final = tcg_temp_new_i64();
10208     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10209 
10210     if (round) {
10211         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10212     } else {
10213         tcg_round = NULL;
10214     }
10215 
10216     for (i = 0; i < elements; i++) {
10217         read_vec_element(s, tcg_rn, rn, i, size+1);
10218         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10219                                 false, true, size+1, shift);
10220 
10221         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10222     }
10223 
10224     if (!is_q) {
10225         write_vec_element(s, tcg_final, rd, 0, MO_64);
10226     } else {
10227         write_vec_element(s, tcg_final, rd, 1, MO_64);
10228     }
10229 
10230     clear_vec_high(s, is_q, rd);
10231 }
10232 
10233 
10234 /* AdvSIMD shift by immediate
10235  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10236  * +---+---+---+-------------+------+------+--------+---+------+------+
10237  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10238  * +---+---+---+-------------+------+------+--------+---+------+------+
10239  */
10240 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10241 {
10242     int rd = extract32(insn, 0, 5);
10243     int rn = extract32(insn, 5, 5);
10244     int opcode = extract32(insn, 11, 5);
10245     int immb = extract32(insn, 16, 3);
10246     int immh = extract32(insn, 19, 4);
10247     bool is_u = extract32(insn, 29, 1);
10248     bool is_q = extract32(insn, 30, 1);
10249 
10250     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10251     assert(immh != 0);
10252 
10253     switch (opcode) {
10254     case 0x08: /* SRI */
10255         if (!is_u) {
10256             unallocated_encoding(s);
10257             return;
10258         }
10259         /* fall through */
10260     case 0x00: /* SSHR / USHR */
10261     case 0x02: /* SSRA / USRA (accumulate) */
10262     case 0x04: /* SRSHR / URSHR (rounding) */
10263     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10264         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10265         break;
10266     case 0x0a: /* SHL / SLI */
10267         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10268         break;
10269     case 0x10: /* SHRN */
10270     case 0x11: /* RSHRN / SQRSHRUN */
10271         if (is_u) {
10272             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10273                                    opcode, rn, rd);
10274         } else {
10275             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10276         }
10277         break;
10278     case 0x12: /* SQSHRN / UQSHRN */
10279     case 0x13: /* SQRSHRN / UQRSHRN */
10280         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10281                                opcode, rn, rd);
10282         break;
10283     case 0x14: /* SSHLL / USHLL */
10284         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10285         break;
10286     case 0x1c: /* SCVTF / UCVTF */
10287         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10288                                      opcode, rn, rd);
10289         break;
10290     case 0xc: /* SQSHLU */
10291         if (!is_u) {
10292             unallocated_encoding(s);
10293             return;
10294         }
10295         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10296         break;
10297     case 0xe: /* SQSHL, UQSHL */
10298         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10299         break;
10300     case 0x1f: /* FCVTZS/ FCVTZU */
10301         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10302         return;
10303     default:
10304         unallocated_encoding(s);
10305         return;
10306     }
10307 }
10308 
10309 /* Generate code to do a "long" addition or subtraction, ie one done in
10310  * TCGv_i64 on vector lanes twice the width specified by size.
10311  */
10312 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10313                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10314 {
10315     static NeonGenTwo64OpFn * const fns[3][2] = {
10316         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10317         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10318         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10319     };
10320     NeonGenTwo64OpFn *genfn;
10321     assert(size < 3);
10322 
10323     genfn = fns[size][is_sub];
10324     genfn(tcg_res, tcg_op1, tcg_op2);
10325 }
10326 
10327 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10328                                 int opcode, int rd, int rn, int rm)
10329 {
10330     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10331     TCGv_i64 tcg_res[2];
10332     int pass, accop;
10333 
10334     tcg_res[0] = tcg_temp_new_i64();
10335     tcg_res[1] = tcg_temp_new_i64();
10336 
10337     /* Does this op do an adding accumulate, a subtracting accumulate,
10338      * or no accumulate at all?
10339      */
10340     switch (opcode) {
10341     case 5:
10342     case 8:
10343     case 9:
10344         accop = 1;
10345         break;
10346     case 10:
10347     case 11:
10348         accop = -1;
10349         break;
10350     default:
10351         accop = 0;
10352         break;
10353     }
10354 
10355     if (accop != 0) {
10356         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10357         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10358     }
10359 
10360     /* size == 2 means two 32x32->64 operations; this is worth special
10361      * casing because we can generally handle it inline.
10362      */
10363     if (size == 2) {
10364         for (pass = 0; pass < 2; pass++) {
10365             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10366             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10367             TCGv_i64 tcg_passres;
10368             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10369 
10370             int elt = pass + is_q * 2;
10371 
10372             read_vec_element(s, tcg_op1, rn, elt, memop);
10373             read_vec_element(s, tcg_op2, rm, elt, memop);
10374 
10375             if (accop == 0) {
10376                 tcg_passres = tcg_res[pass];
10377             } else {
10378                 tcg_passres = tcg_temp_new_i64();
10379             }
10380 
10381             switch (opcode) {
10382             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10383                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10384                 break;
10385             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10386                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10387                 break;
10388             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10389             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10390             {
10391                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10392                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10393 
10394                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10395                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10396                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10397                                     tcg_passres,
10398                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10399                 break;
10400             }
10401             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10402             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10403             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10404                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10405                 break;
10406             case 9: /* SQDMLAL, SQDMLAL2 */
10407             case 11: /* SQDMLSL, SQDMLSL2 */
10408             case 13: /* SQDMULL, SQDMULL2 */
10409                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10410                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10411                                                   tcg_passres, tcg_passres);
10412                 break;
10413             default:
10414                 g_assert_not_reached();
10415             }
10416 
10417             if (opcode == 9 || opcode == 11) {
10418                 /* saturating accumulate ops */
10419                 if (accop < 0) {
10420                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10421                 }
10422                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10423                                                   tcg_res[pass], tcg_passres);
10424             } else if (accop > 0) {
10425                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10426             } else if (accop < 0) {
10427                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10428             }
10429         }
10430     } else {
10431         /* size 0 or 1, generally helper functions */
10432         for (pass = 0; pass < 2; pass++) {
10433             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10434             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10435             TCGv_i64 tcg_passres;
10436             int elt = pass + is_q * 2;
10437 
10438             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10439             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10440 
10441             if (accop == 0) {
10442                 tcg_passres = tcg_res[pass];
10443             } else {
10444                 tcg_passres = tcg_temp_new_i64();
10445             }
10446 
10447             switch (opcode) {
10448             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10449             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10450             {
10451                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10452                 static NeonGenWidenFn * const widenfns[2][2] = {
10453                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10454                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10455                 };
10456                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10457 
10458                 widenfn(tcg_op2_64, tcg_op2);
10459                 widenfn(tcg_passres, tcg_op1);
10460                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10461                               tcg_passres, tcg_op2_64);
10462                 break;
10463             }
10464             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10465             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10466                 if (size == 0) {
10467                     if (is_u) {
10468                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10469                     } else {
10470                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10471                     }
10472                 } else {
10473                     if (is_u) {
10474                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10475                     } else {
10476                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10477                     }
10478                 }
10479                 break;
10480             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10481             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10482             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10483                 if (size == 0) {
10484                     if (is_u) {
10485                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10486                     } else {
10487                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10488                     }
10489                 } else {
10490                     if (is_u) {
10491                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10492                     } else {
10493                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10494                     }
10495                 }
10496                 break;
10497             case 9: /* SQDMLAL, SQDMLAL2 */
10498             case 11: /* SQDMLSL, SQDMLSL2 */
10499             case 13: /* SQDMULL, SQDMULL2 */
10500                 assert(size == 1);
10501                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10502                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10503                                                   tcg_passres, tcg_passres);
10504                 break;
10505             default:
10506                 g_assert_not_reached();
10507             }
10508 
10509             if (accop != 0) {
10510                 if (opcode == 9 || opcode == 11) {
10511                     /* saturating accumulate ops */
10512                     if (accop < 0) {
10513                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10514                     }
10515                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10516                                                       tcg_res[pass],
10517                                                       tcg_passres);
10518                 } else {
10519                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10520                                   tcg_res[pass], tcg_passres);
10521                 }
10522             }
10523         }
10524     }
10525 
10526     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10527     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10528 }
10529 
10530 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10531                             int opcode, int rd, int rn, int rm)
10532 {
10533     TCGv_i64 tcg_res[2];
10534     int part = is_q ? 2 : 0;
10535     int pass;
10536 
10537     for (pass = 0; pass < 2; pass++) {
10538         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10539         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10540         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10541         static NeonGenWidenFn * const widenfns[3][2] = {
10542             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10543             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10544             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10545         };
10546         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10547 
10548         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10549         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10550         widenfn(tcg_op2_wide, tcg_op2);
10551         tcg_res[pass] = tcg_temp_new_i64();
10552         gen_neon_addl(size, (opcode == 3),
10553                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10554     }
10555 
10556     for (pass = 0; pass < 2; pass++) {
10557         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10558     }
10559 }
10560 
10561 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10562 {
10563     tcg_gen_addi_i64(in, in, 1U << 31);
10564     tcg_gen_extrh_i64_i32(res, in);
10565 }
10566 
10567 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10568                                  int opcode, int rd, int rn, int rm)
10569 {
10570     TCGv_i32 tcg_res[2];
10571     int part = is_q ? 2 : 0;
10572     int pass;
10573 
10574     for (pass = 0; pass < 2; pass++) {
10575         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10576         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10577         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10578         static NeonGenNarrowFn * const narrowfns[3][2] = {
10579             { gen_helper_neon_narrow_high_u8,
10580               gen_helper_neon_narrow_round_high_u8 },
10581             { gen_helper_neon_narrow_high_u16,
10582               gen_helper_neon_narrow_round_high_u16 },
10583             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10584         };
10585         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10586 
10587         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10588         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10589 
10590         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10591 
10592         tcg_res[pass] = tcg_temp_new_i32();
10593         gennarrow(tcg_res[pass], tcg_wideres);
10594     }
10595 
10596     for (pass = 0; pass < 2; pass++) {
10597         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10598     }
10599     clear_vec_high(s, is_q, rd);
10600 }
10601 
10602 /* AdvSIMD three different
10603  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10604  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10605  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10606  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10607  */
10608 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10609 {
10610     /* Instructions in this group fall into three basic classes
10611      * (in each case with the operation working on each element in
10612      * the input vectors):
10613      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10614      *     128 bit input)
10615      * (2) wide 64 x 128 -> 128
10616      * (3) narrowing 128 x 128 -> 64
10617      * Here we do initial decode, catch unallocated cases and
10618      * dispatch to separate functions for each class.
10619      */
10620     int is_q = extract32(insn, 30, 1);
10621     int is_u = extract32(insn, 29, 1);
10622     int size = extract32(insn, 22, 2);
10623     int opcode = extract32(insn, 12, 4);
10624     int rm = extract32(insn, 16, 5);
10625     int rn = extract32(insn, 5, 5);
10626     int rd = extract32(insn, 0, 5);
10627 
10628     switch (opcode) {
10629     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10630     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10631         /* 64 x 128 -> 128 */
10632         if (size == 3) {
10633             unallocated_encoding(s);
10634             return;
10635         }
10636         if (!fp_access_check(s)) {
10637             return;
10638         }
10639         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10640         break;
10641     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10642     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10643         /* 128 x 128 -> 64 */
10644         if (size == 3) {
10645             unallocated_encoding(s);
10646             return;
10647         }
10648         if (!fp_access_check(s)) {
10649             return;
10650         }
10651         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10652         break;
10653     case 14: /* PMULL, PMULL2 */
10654         if (is_u) {
10655             unallocated_encoding(s);
10656             return;
10657         }
10658         switch (size) {
10659         case 0: /* PMULL.P8 */
10660             if (!fp_access_check(s)) {
10661                 return;
10662             }
10663             /* The Q field specifies lo/hi half input for this insn.  */
10664             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10665                              gen_helper_neon_pmull_h);
10666             break;
10667 
10668         case 3: /* PMULL.P64 */
10669             if (!dc_isar_feature(aa64_pmull, s)) {
10670                 unallocated_encoding(s);
10671                 return;
10672             }
10673             if (!fp_access_check(s)) {
10674                 return;
10675             }
10676             /* The Q field specifies lo/hi half input for this insn.  */
10677             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10678                              gen_helper_gvec_pmull_q);
10679             break;
10680 
10681         default:
10682             unallocated_encoding(s);
10683             break;
10684         }
10685         return;
10686     case 9: /* SQDMLAL, SQDMLAL2 */
10687     case 11: /* SQDMLSL, SQDMLSL2 */
10688     case 13: /* SQDMULL, SQDMULL2 */
10689         if (is_u || size == 0) {
10690             unallocated_encoding(s);
10691             return;
10692         }
10693         /* fall through */
10694     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10695     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10696     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10697     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10698     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10699     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10700     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10701         /* 64 x 64 -> 128 */
10702         if (size == 3) {
10703             unallocated_encoding(s);
10704             return;
10705         }
10706         if (!fp_access_check(s)) {
10707             return;
10708         }
10709 
10710         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10711         break;
10712     default:
10713         /* opcode 15 not allocated */
10714         unallocated_encoding(s);
10715         break;
10716     }
10717 }
10718 
10719 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10720 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10721 {
10722     int rd = extract32(insn, 0, 5);
10723     int rn = extract32(insn, 5, 5);
10724     int rm = extract32(insn, 16, 5);
10725     int size = extract32(insn, 22, 2);
10726     bool is_u = extract32(insn, 29, 1);
10727     bool is_q = extract32(insn, 30, 1);
10728 
10729     if (!fp_access_check(s)) {
10730         return;
10731     }
10732 
10733     switch (size + 4 * is_u) {
10734     case 0: /* AND */
10735         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10736         return;
10737     case 1: /* BIC */
10738         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10739         return;
10740     case 2: /* ORR */
10741         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10742         return;
10743     case 3: /* ORN */
10744         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10745         return;
10746     case 4: /* EOR */
10747         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10748         return;
10749 
10750     case 5: /* BSL bitwise select */
10751         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10752         return;
10753     case 6: /* BIT, bitwise insert if true */
10754         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10755         return;
10756     case 7: /* BIF, bitwise insert if false */
10757         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10758         return;
10759 
10760     default:
10761         g_assert_not_reached();
10762     }
10763 }
10764 
10765 /* Pairwise op subgroup of C3.6.16.
10766  *
10767  * This is called directly or via the handle_3same_float for float pairwise
10768  * operations where the opcode and size are calculated differently.
10769  */
10770 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10771                                    int size, int rn, int rm, int rd)
10772 {
10773     TCGv_ptr fpst;
10774     int pass;
10775 
10776     /* Floating point operations need fpst */
10777     if (opcode >= 0x58) {
10778         fpst = fpstatus_ptr(FPST_FPCR);
10779     } else {
10780         fpst = NULL;
10781     }
10782 
10783     if (!fp_access_check(s)) {
10784         return;
10785     }
10786 
10787     /* These operations work on the concatenated rm:rn, with each pair of
10788      * adjacent elements being operated on to produce an element in the result.
10789      */
10790     if (size == 3) {
10791         TCGv_i64 tcg_res[2];
10792 
10793         for (pass = 0; pass < 2; pass++) {
10794             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10795             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10796             int passreg = (pass == 0) ? rn : rm;
10797 
10798             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10799             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10800             tcg_res[pass] = tcg_temp_new_i64();
10801 
10802             switch (opcode) {
10803             case 0x17: /* ADDP */
10804                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10805                 break;
10806             case 0x58: /* FMAXNMP */
10807                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10808                 break;
10809             case 0x5a: /* FADDP */
10810                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10811                 break;
10812             case 0x5e: /* FMAXP */
10813                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10814                 break;
10815             case 0x78: /* FMINNMP */
10816                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10817                 break;
10818             case 0x7e: /* FMINP */
10819                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10820                 break;
10821             default:
10822                 g_assert_not_reached();
10823             }
10824         }
10825 
10826         for (pass = 0; pass < 2; pass++) {
10827             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10828         }
10829     } else {
10830         int maxpass = is_q ? 4 : 2;
10831         TCGv_i32 tcg_res[4];
10832 
10833         for (pass = 0; pass < maxpass; pass++) {
10834             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10835             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10836             NeonGenTwoOpFn *genfn = NULL;
10837             int passreg = pass < (maxpass / 2) ? rn : rm;
10838             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10839 
10840             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10841             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10842             tcg_res[pass] = tcg_temp_new_i32();
10843 
10844             switch (opcode) {
10845             case 0x17: /* ADDP */
10846             {
10847                 static NeonGenTwoOpFn * const fns[3] = {
10848                     gen_helper_neon_padd_u8,
10849                     gen_helper_neon_padd_u16,
10850                     tcg_gen_add_i32,
10851                 };
10852                 genfn = fns[size];
10853                 break;
10854             }
10855             case 0x14: /* SMAXP, UMAXP */
10856             {
10857                 static NeonGenTwoOpFn * const fns[3][2] = {
10858                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10859                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10860                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10861                 };
10862                 genfn = fns[size][u];
10863                 break;
10864             }
10865             case 0x15: /* SMINP, UMINP */
10866             {
10867                 static NeonGenTwoOpFn * const fns[3][2] = {
10868                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10869                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10870                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10871                 };
10872                 genfn = fns[size][u];
10873                 break;
10874             }
10875             /* The FP operations are all on single floats (32 bit) */
10876             case 0x58: /* FMAXNMP */
10877                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10878                 break;
10879             case 0x5a: /* FADDP */
10880                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10881                 break;
10882             case 0x5e: /* FMAXP */
10883                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10884                 break;
10885             case 0x78: /* FMINNMP */
10886                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10887                 break;
10888             case 0x7e: /* FMINP */
10889                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10890                 break;
10891             default:
10892                 g_assert_not_reached();
10893             }
10894 
10895             /* FP ops called directly, otherwise call now */
10896             if (genfn) {
10897                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10898             }
10899         }
10900 
10901         for (pass = 0; pass < maxpass; pass++) {
10902             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10903         }
10904         clear_vec_high(s, is_q, rd);
10905     }
10906 }
10907 
10908 /* Floating point op subgroup of C3.6.16. */
10909 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10910 {
10911     /* For floating point ops, the U, size[1] and opcode bits
10912      * together indicate the operation. size[0] indicates single
10913      * or double.
10914      */
10915     int fpopcode = extract32(insn, 11, 5)
10916         | (extract32(insn, 23, 1) << 5)
10917         | (extract32(insn, 29, 1) << 6);
10918     int is_q = extract32(insn, 30, 1);
10919     int size = extract32(insn, 22, 1);
10920     int rm = extract32(insn, 16, 5);
10921     int rn = extract32(insn, 5, 5);
10922     int rd = extract32(insn, 0, 5);
10923 
10924     int datasize = is_q ? 128 : 64;
10925     int esize = 32 << size;
10926     int elements = datasize / esize;
10927 
10928     if (size == 1 && !is_q) {
10929         unallocated_encoding(s);
10930         return;
10931     }
10932 
10933     switch (fpopcode) {
10934     case 0x58: /* FMAXNMP */
10935     case 0x5a: /* FADDP */
10936     case 0x5e: /* FMAXP */
10937     case 0x78: /* FMINNMP */
10938     case 0x7e: /* FMINP */
10939         if (size && !is_q) {
10940             unallocated_encoding(s);
10941             return;
10942         }
10943         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10944                                rn, rm, rd);
10945         return;
10946     case 0x1b: /* FMULX */
10947     case 0x1f: /* FRECPS */
10948     case 0x3f: /* FRSQRTS */
10949     case 0x5d: /* FACGE */
10950     case 0x7d: /* FACGT */
10951     case 0x19: /* FMLA */
10952     case 0x39: /* FMLS */
10953     case 0x18: /* FMAXNM */
10954     case 0x1a: /* FADD */
10955     case 0x1c: /* FCMEQ */
10956     case 0x1e: /* FMAX */
10957     case 0x38: /* FMINNM */
10958     case 0x3a: /* FSUB */
10959     case 0x3e: /* FMIN */
10960     case 0x5b: /* FMUL */
10961     case 0x5c: /* FCMGE */
10962     case 0x5f: /* FDIV */
10963     case 0x7a: /* FABD */
10964     case 0x7c: /* FCMGT */
10965         if (!fp_access_check(s)) {
10966             return;
10967         }
10968         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10969         return;
10970 
10971     case 0x1d: /* FMLAL  */
10972     case 0x3d: /* FMLSL  */
10973     case 0x59: /* FMLAL2 */
10974     case 0x79: /* FMLSL2 */
10975         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10976             unallocated_encoding(s);
10977             return;
10978         }
10979         if (fp_access_check(s)) {
10980             int is_s = extract32(insn, 23, 1);
10981             int is_2 = extract32(insn, 29, 1);
10982             int data = (is_2 << 1) | is_s;
10983             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10984                                vec_full_reg_offset(s, rn),
10985                                vec_full_reg_offset(s, rm), tcg_env,
10986                                is_q ? 16 : 8, vec_full_reg_size(s),
10987                                data, gen_helper_gvec_fmlal_a64);
10988         }
10989         return;
10990 
10991     default:
10992         unallocated_encoding(s);
10993         return;
10994     }
10995 }
10996 
10997 /* Integer op subgroup of C3.6.16. */
10998 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10999 {
11000     int is_q = extract32(insn, 30, 1);
11001     int u = extract32(insn, 29, 1);
11002     int size = extract32(insn, 22, 2);
11003     int opcode = extract32(insn, 11, 5);
11004     int rm = extract32(insn, 16, 5);
11005     int rn = extract32(insn, 5, 5);
11006     int rd = extract32(insn, 0, 5);
11007     int pass;
11008     TCGCond cond;
11009 
11010     switch (opcode) {
11011     case 0x13: /* MUL, PMUL */
11012         if (u && size != 0) {
11013             unallocated_encoding(s);
11014             return;
11015         }
11016         /* fall through */
11017     case 0x0: /* SHADD, UHADD */
11018     case 0x2: /* SRHADD, URHADD */
11019     case 0x4: /* SHSUB, UHSUB */
11020     case 0xc: /* SMAX, UMAX */
11021     case 0xd: /* SMIN, UMIN */
11022     case 0xe: /* SABD, UABD */
11023     case 0xf: /* SABA, UABA */
11024     case 0x12: /* MLA, MLS */
11025         if (size == 3) {
11026             unallocated_encoding(s);
11027             return;
11028         }
11029         break;
11030     case 0x16: /* SQDMULH, SQRDMULH */
11031         if (size == 0 || size == 3) {
11032             unallocated_encoding(s);
11033             return;
11034         }
11035         break;
11036     default:
11037         if (size == 3 && !is_q) {
11038             unallocated_encoding(s);
11039             return;
11040         }
11041         break;
11042     }
11043 
11044     if (!fp_access_check(s)) {
11045         return;
11046     }
11047 
11048     switch (opcode) {
11049     case 0x01: /* SQADD, UQADD */
11050         if (u) {
11051             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11052         } else {
11053             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11054         }
11055         return;
11056     case 0x05: /* SQSUB, UQSUB */
11057         if (u) {
11058             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11059         } else {
11060             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11061         }
11062         return;
11063     case 0x08: /* SSHL, USHL */
11064         if (u) {
11065             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11066         } else {
11067             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11068         }
11069         return;
11070     case 0x0c: /* SMAX, UMAX */
11071         if (u) {
11072             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11073         } else {
11074             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11075         }
11076         return;
11077     case 0x0d: /* SMIN, UMIN */
11078         if (u) {
11079             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11080         } else {
11081             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11082         }
11083         return;
11084     case 0xe: /* SABD, UABD */
11085         if (u) {
11086             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11087         } else {
11088             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11089         }
11090         return;
11091     case 0xf: /* SABA, UABA */
11092         if (u) {
11093             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11094         } else {
11095             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11096         }
11097         return;
11098     case 0x10: /* ADD, SUB */
11099         if (u) {
11100             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11101         } else {
11102             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11103         }
11104         return;
11105     case 0x13: /* MUL, PMUL */
11106         if (!u) { /* MUL */
11107             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11108         } else {  /* PMUL */
11109             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11110         }
11111         return;
11112     case 0x12: /* MLA, MLS */
11113         if (u) {
11114             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11115         } else {
11116             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11117         }
11118         return;
11119     case 0x16: /* SQDMULH, SQRDMULH */
11120         {
11121             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11122                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11123                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11124             };
11125             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11126         }
11127         return;
11128     case 0x11:
11129         if (!u) { /* CMTST */
11130             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11131             return;
11132         }
11133         /* else CMEQ */
11134         cond = TCG_COND_EQ;
11135         goto do_gvec_cmp;
11136     case 0x06: /* CMGT, CMHI */
11137         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11138         goto do_gvec_cmp;
11139     case 0x07: /* CMGE, CMHS */
11140         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11141     do_gvec_cmp:
11142         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11143                          vec_full_reg_offset(s, rn),
11144                          vec_full_reg_offset(s, rm),
11145                          is_q ? 16 : 8, vec_full_reg_size(s));
11146         return;
11147     }
11148 
11149     if (size == 3) {
11150         assert(is_q);
11151         for (pass = 0; pass < 2; pass++) {
11152             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11153             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11154             TCGv_i64 tcg_res = tcg_temp_new_i64();
11155 
11156             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11157             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11158 
11159             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11160 
11161             write_vec_element(s, tcg_res, rd, pass, MO_64);
11162         }
11163     } else {
11164         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11165             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11166             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11167             TCGv_i32 tcg_res = tcg_temp_new_i32();
11168             NeonGenTwoOpFn *genfn = NULL;
11169             NeonGenTwoOpEnvFn *genenvfn = NULL;
11170 
11171             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11172             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11173 
11174             switch (opcode) {
11175             case 0x0: /* SHADD, UHADD */
11176             {
11177                 static NeonGenTwoOpFn * const fns[3][2] = {
11178                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11179                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11180                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11181                 };
11182                 genfn = fns[size][u];
11183                 break;
11184             }
11185             case 0x2: /* SRHADD, URHADD */
11186             {
11187                 static NeonGenTwoOpFn * const fns[3][2] = {
11188                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11189                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11190                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11191                 };
11192                 genfn = fns[size][u];
11193                 break;
11194             }
11195             case 0x4: /* SHSUB, UHSUB */
11196             {
11197                 static NeonGenTwoOpFn * const fns[3][2] = {
11198                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11199                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11200                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11201                 };
11202                 genfn = fns[size][u];
11203                 break;
11204             }
11205             case 0x9: /* SQSHL, UQSHL */
11206             {
11207                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11208                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11209                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11210                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11211                 };
11212                 genenvfn = fns[size][u];
11213                 break;
11214             }
11215             case 0xa: /* SRSHL, URSHL */
11216             {
11217                 static NeonGenTwoOpFn * const fns[3][2] = {
11218                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11219                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11220                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11221                 };
11222                 genfn = fns[size][u];
11223                 break;
11224             }
11225             case 0xb: /* SQRSHL, UQRSHL */
11226             {
11227                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11228                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11229                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11230                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11231                 };
11232                 genenvfn = fns[size][u];
11233                 break;
11234             }
11235             default:
11236                 g_assert_not_reached();
11237             }
11238 
11239             if (genenvfn) {
11240                 genenvfn(tcg_res, tcg_env, tcg_op1, tcg_op2);
11241             } else {
11242                 genfn(tcg_res, tcg_op1, tcg_op2);
11243             }
11244 
11245             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11246         }
11247     }
11248     clear_vec_high(s, is_q, rd);
11249 }
11250 
11251 /* AdvSIMD three same
11252  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11253  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11254  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11255  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11256  */
11257 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11258 {
11259     int opcode = extract32(insn, 11, 5);
11260 
11261     switch (opcode) {
11262     case 0x3: /* logic ops */
11263         disas_simd_3same_logic(s, insn);
11264         break;
11265     case 0x17: /* ADDP */
11266     case 0x14: /* SMAXP, UMAXP */
11267     case 0x15: /* SMINP, UMINP */
11268     {
11269         /* Pairwise operations */
11270         int is_q = extract32(insn, 30, 1);
11271         int u = extract32(insn, 29, 1);
11272         int size = extract32(insn, 22, 2);
11273         int rm = extract32(insn, 16, 5);
11274         int rn = extract32(insn, 5, 5);
11275         int rd = extract32(insn, 0, 5);
11276         if (opcode == 0x17) {
11277             if (u || (size == 3 && !is_q)) {
11278                 unallocated_encoding(s);
11279                 return;
11280             }
11281         } else {
11282             if (size == 3) {
11283                 unallocated_encoding(s);
11284                 return;
11285             }
11286         }
11287         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11288         break;
11289     }
11290     case 0x18 ... 0x31:
11291         /* floating point ops, sz[1] and U are part of opcode */
11292         disas_simd_3same_float(s, insn);
11293         break;
11294     default:
11295         disas_simd_3same_int(s, insn);
11296         break;
11297     }
11298 }
11299 
11300 /*
11301  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11302  *
11303  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11304  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11305  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11306  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11307  *
11308  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11309  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11310  *
11311  */
11312 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11313 {
11314     int opcode = extract32(insn, 11, 3);
11315     int u = extract32(insn, 29, 1);
11316     int a = extract32(insn, 23, 1);
11317     int is_q = extract32(insn, 30, 1);
11318     int rm = extract32(insn, 16, 5);
11319     int rn = extract32(insn, 5, 5);
11320     int rd = extract32(insn, 0, 5);
11321     /*
11322      * For these floating point ops, the U, a and opcode bits
11323      * together indicate the operation.
11324      */
11325     int fpopcode = opcode | (a << 3) | (u << 4);
11326     int datasize = is_q ? 128 : 64;
11327     int elements = datasize / 16;
11328     bool pairwise;
11329     TCGv_ptr fpst;
11330     int pass;
11331 
11332     switch (fpopcode) {
11333     case 0x0: /* FMAXNM */
11334     case 0x1: /* FMLA */
11335     case 0x2: /* FADD */
11336     case 0x3: /* FMULX */
11337     case 0x4: /* FCMEQ */
11338     case 0x6: /* FMAX */
11339     case 0x7: /* FRECPS */
11340     case 0x8: /* FMINNM */
11341     case 0x9: /* FMLS */
11342     case 0xa: /* FSUB */
11343     case 0xe: /* FMIN */
11344     case 0xf: /* FRSQRTS */
11345     case 0x13: /* FMUL */
11346     case 0x14: /* FCMGE */
11347     case 0x15: /* FACGE */
11348     case 0x17: /* FDIV */
11349     case 0x1a: /* FABD */
11350     case 0x1c: /* FCMGT */
11351     case 0x1d: /* FACGT */
11352         pairwise = false;
11353         break;
11354     case 0x10: /* FMAXNMP */
11355     case 0x12: /* FADDP */
11356     case 0x16: /* FMAXP */
11357     case 0x18: /* FMINNMP */
11358     case 0x1e: /* FMINP */
11359         pairwise = true;
11360         break;
11361     default:
11362         unallocated_encoding(s);
11363         return;
11364     }
11365 
11366     if (!dc_isar_feature(aa64_fp16, s)) {
11367         unallocated_encoding(s);
11368         return;
11369     }
11370 
11371     if (!fp_access_check(s)) {
11372         return;
11373     }
11374 
11375     fpst = fpstatus_ptr(FPST_FPCR_F16);
11376 
11377     if (pairwise) {
11378         int maxpass = is_q ? 8 : 4;
11379         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11380         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11381         TCGv_i32 tcg_res[8];
11382 
11383         for (pass = 0; pass < maxpass; pass++) {
11384             int passreg = pass < (maxpass / 2) ? rn : rm;
11385             int passelt = (pass << 1) & (maxpass - 1);
11386 
11387             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11388             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11389             tcg_res[pass] = tcg_temp_new_i32();
11390 
11391             switch (fpopcode) {
11392             case 0x10: /* FMAXNMP */
11393                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11394                                            fpst);
11395                 break;
11396             case 0x12: /* FADDP */
11397                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11398                 break;
11399             case 0x16: /* FMAXP */
11400                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11401                 break;
11402             case 0x18: /* FMINNMP */
11403                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11404                                            fpst);
11405                 break;
11406             case 0x1e: /* FMINP */
11407                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11408                 break;
11409             default:
11410                 g_assert_not_reached();
11411             }
11412         }
11413 
11414         for (pass = 0; pass < maxpass; pass++) {
11415             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11416         }
11417     } else {
11418         for (pass = 0; pass < elements; pass++) {
11419             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11420             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11421             TCGv_i32 tcg_res = tcg_temp_new_i32();
11422 
11423             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11424             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11425 
11426             switch (fpopcode) {
11427             case 0x0: /* FMAXNM */
11428                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11429                 break;
11430             case 0x1: /* FMLA */
11431                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11432                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11433                                            fpst);
11434                 break;
11435             case 0x2: /* FADD */
11436                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11437                 break;
11438             case 0x3: /* FMULX */
11439                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11440                 break;
11441             case 0x4: /* FCMEQ */
11442                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11443                 break;
11444             case 0x6: /* FMAX */
11445                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11446                 break;
11447             case 0x7: /* FRECPS */
11448                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11449                 break;
11450             case 0x8: /* FMINNM */
11451                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11452                 break;
11453             case 0x9: /* FMLS */
11454                 /* As usual for ARM, separate negation for fused multiply-add */
11455                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11456                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11457                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11458                                            fpst);
11459                 break;
11460             case 0xa: /* FSUB */
11461                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11462                 break;
11463             case 0xe: /* FMIN */
11464                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11465                 break;
11466             case 0xf: /* FRSQRTS */
11467                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11468                 break;
11469             case 0x13: /* FMUL */
11470                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11471                 break;
11472             case 0x14: /* FCMGE */
11473                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11474                 break;
11475             case 0x15: /* FACGE */
11476                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11477                 break;
11478             case 0x17: /* FDIV */
11479                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11480                 break;
11481             case 0x1a: /* FABD */
11482                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11483                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11484                 break;
11485             case 0x1c: /* FCMGT */
11486                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11487                 break;
11488             case 0x1d: /* FACGT */
11489                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11490                 break;
11491             default:
11492                 g_assert_not_reached();
11493             }
11494 
11495             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11496         }
11497     }
11498 
11499     clear_vec_high(s, is_q, rd);
11500 }
11501 
11502 /* AdvSIMD three same extra
11503  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11504  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11505  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11506  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11507  */
11508 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11509 {
11510     int rd = extract32(insn, 0, 5);
11511     int rn = extract32(insn, 5, 5);
11512     int opcode = extract32(insn, 11, 4);
11513     int rm = extract32(insn, 16, 5);
11514     int size = extract32(insn, 22, 2);
11515     bool u = extract32(insn, 29, 1);
11516     bool is_q = extract32(insn, 30, 1);
11517     bool feature;
11518     int rot;
11519 
11520     switch (u * 16 + opcode) {
11521     case 0x10: /* SQRDMLAH (vector) */
11522     case 0x11: /* SQRDMLSH (vector) */
11523         if (size != 1 && size != 2) {
11524             unallocated_encoding(s);
11525             return;
11526         }
11527         feature = dc_isar_feature(aa64_rdm, s);
11528         break;
11529     case 0x02: /* SDOT (vector) */
11530     case 0x12: /* UDOT (vector) */
11531         if (size != MO_32) {
11532             unallocated_encoding(s);
11533             return;
11534         }
11535         feature = dc_isar_feature(aa64_dp, s);
11536         break;
11537     case 0x03: /* USDOT */
11538         if (size != MO_32) {
11539             unallocated_encoding(s);
11540             return;
11541         }
11542         feature = dc_isar_feature(aa64_i8mm, s);
11543         break;
11544     case 0x04: /* SMMLA */
11545     case 0x14: /* UMMLA */
11546     case 0x05: /* USMMLA */
11547         if (!is_q || size != MO_32) {
11548             unallocated_encoding(s);
11549             return;
11550         }
11551         feature = dc_isar_feature(aa64_i8mm, s);
11552         break;
11553     case 0x18: /* FCMLA, #0 */
11554     case 0x19: /* FCMLA, #90 */
11555     case 0x1a: /* FCMLA, #180 */
11556     case 0x1b: /* FCMLA, #270 */
11557     case 0x1c: /* FCADD, #90 */
11558     case 0x1e: /* FCADD, #270 */
11559         if (size == 0
11560             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11561             || (size == 3 && !is_q)) {
11562             unallocated_encoding(s);
11563             return;
11564         }
11565         feature = dc_isar_feature(aa64_fcma, s);
11566         break;
11567     case 0x1d: /* BFMMLA */
11568         if (size != MO_16 || !is_q) {
11569             unallocated_encoding(s);
11570             return;
11571         }
11572         feature = dc_isar_feature(aa64_bf16, s);
11573         break;
11574     case 0x1f:
11575         switch (size) {
11576         case 1: /* BFDOT */
11577         case 3: /* BFMLAL{B,T} */
11578             feature = dc_isar_feature(aa64_bf16, s);
11579             break;
11580         default:
11581             unallocated_encoding(s);
11582             return;
11583         }
11584         break;
11585     default:
11586         unallocated_encoding(s);
11587         return;
11588     }
11589     if (!feature) {
11590         unallocated_encoding(s);
11591         return;
11592     }
11593     if (!fp_access_check(s)) {
11594         return;
11595     }
11596 
11597     switch (opcode) {
11598     case 0x0: /* SQRDMLAH (vector) */
11599         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11600         return;
11601 
11602     case 0x1: /* SQRDMLSH (vector) */
11603         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11604         return;
11605 
11606     case 0x2: /* SDOT / UDOT */
11607         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11608                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11609         return;
11610 
11611     case 0x3: /* USDOT */
11612         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11613         return;
11614 
11615     case 0x04: /* SMMLA, UMMLA */
11616         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11617                          u ? gen_helper_gvec_ummla_b
11618                          : gen_helper_gvec_smmla_b);
11619         return;
11620     case 0x05: /* USMMLA */
11621         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11622         return;
11623 
11624     case 0x8: /* FCMLA, #0 */
11625     case 0x9: /* FCMLA, #90 */
11626     case 0xa: /* FCMLA, #180 */
11627     case 0xb: /* FCMLA, #270 */
11628         rot = extract32(opcode, 0, 2);
11629         switch (size) {
11630         case 1:
11631             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11632                               gen_helper_gvec_fcmlah);
11633             break;
11634         case 2:
11635             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11636                               gen_helper_gvec_fcmlas);
11637             break;
11638         case 3:
11639             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11640                               gen_helper_gvec_fcmlad);
11641             break;
11642         default:
11643             g_assert_not_reached();
11644         }
11645         return;
11646 
11647     case 0xc: /* FCADD, #90 */
11648     case 0xe: /* FCADD, #270 */
11649         rot = extract32(opcode, 1, 1);
11650         switch (size) {
11651         case 1:
11652             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11653                               gen_helper_gvec_fcaddh);
11654             break;
11655         case 2:
11656             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11657                               gen_helper_gvec_fcadds);
11658             break;
11659         case 3:
11660             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11661                               gen_helper_gvec_fcaddd);
11662             break;
11663         default:
11664             g_assert_not_reached();
11665         }
11666         return;
11667 
11668     case 0xd: /* BFMMLA */
11669         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11670         return;
11671     case 0xf:
11672         switch (size) {
11673         case 1: /* BFDOT */
11674             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11675             break;
11676         case 3: /* BFMLAL{B,T} */
11677             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11678                               gen_helper_gvec_bfmlal);
11679             break;
11680         default:
11681             g_assert_not_reached();
11682         }
11683         return;
11684 
11685     default:
11686         g_assert_not_reached();
11687     }
11688 }
11689 
11690 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11691                                   int size, int rn, int rd)
11692 {
11693     /* Handle 2-reg-misc ops which are widening (so each size element
11694      * in the source becomes a 2*size element in the destination.
11695      * The only instruction like this is FCVTL.
11696      */
11697     int pass;
11698 
11699     if (size == 3) {
11700         /* 32 -> 64 bit fp conversion */
11701         TCGv_i64 tcg_res[2];
11702         int srcelt = is_q ? 2 : 0;
11703 
11704         for (pass = 0; pass < 2; pass++) {
11705             TCGv_i32 tcg_op = tcg_temp_new_i32();
11706             tcg_res[pass] = tcg_temp_new_i64();
11707 
11708             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11709             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11710         }
11711         for (pass = 0; pass < 2; pass++) {
11712             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11713         }
11714     } else {
11715         /* 16 -> 32 bit fp conversion */
11716         int srcelt = is_q ? 4 : 0;
11717         TCGv_i32 tcg_res[4];
11718         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11719         TCGv_i32 ahp = get_ahp_flag();
11720 
11721         for (pass = 0; pass < 4; pass++) {
11722             tcg_res[pass] = tcg_temp_new_i32();
11723 
11724             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11725             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11726                                            fpst, ahp);
11727         }
11728         for (pass = 0; pass < 4; pass++) {
11729             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11730         }
11731     }
11732 }
11733 
11734 static void handle_rev(DisasContext *s, int opcode, bool u,
11735                        bool is_q, int size, int rn, int rd)
11736 {
11737     int op = (opcode << 1) | u;
11738     int opsz = op + size;
11739     int grp_size = 3 - opsz;
11740     int dsize = is_q ? 128 : 64;
11741     int i;
11742 
11743     if (opsz >= 3) {
11744         unallocated_encoding(s);
11745         return;
11746     }
11747 
11748     if (!fp_access_check(s)) {
11749         return;
11750     }
11751 
11752     if (size == 0) {
11753         /* Special case bytes, use bswap op on each group of elements */
11754         int groups = dsize / (8 << grp_size);
11755 
11756         for (i = 0; i < groups; i++) {
11757             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11758 
11759             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11760             switch (grp_size) {
11761             case MO_16:
11762                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11763                 break;
11764             case MO_32:
11765                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11766                 break;
11767             case MO_64:
11768                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11769                 break;
11770             default:
11771                 g_assert_not_reached();
11772             }
11773             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11774         }
11775         clear_vec_high(s, is_q, rd);
11776     } else {
11777         int revmask = (1 << grp_size) - 1;
11778         int esize = 8 << size;
11779         int elements = dsize / esize;
11780         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11781         TCGv_i64 tcg_rd[2];
11782 
11783         for (i = 0; i < 2; i++) {
11784             tcg_rd[i] = tcg_temp_new_i64();
11785             tcg_gen_movi_i64(tcg_rd[i], 0);
11786         }
11787 
11788         for (i = 0; i < elements; i++) {
11789             int e_rev = (i & 0xf) ^ revmask;
11790             int w = (e_rev * esize) / 64;
11791             int o = (e_rev * esize) % 64;
11792 
11793             read_vec_element(s, tcg_rn, rn, i, size);
11794             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11795         }
11796 
11797         for (i = 0; i < 2; i++) {
11798             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11799         }
11800         clear_vec_high(s, true, rd);
11801     }
11802 }
11803 
11804 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11805                                   bool is_q, int size, int rn, int rd)
11806 {
11807     /* Implement the pairwise operations from 2-misc:
11808      * SADDLP, UADDLP, SADALP, UADALP.
11809      * These all add pairs of elements in the input to produce a
11810      * double-width result element in the output (possibly accumulating).
11811      */
11812     bool accum = (opcode == 0x6);
11813     int maxpass = is_q ? 2 : 1;
11814     int pass;
11815     TCGv_i64 tcg_res[2];
11816 
11817     if (size == 2) {
11818         /* 32 + 32 -> 64 op */
11819         MemOp memop = size + (u ? 0 : MO_SIGN);
11820 
11821         for (pass = 0; pass < maxpass; pass++) {
11822             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11823             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11824 
11825             tcg_res[pass] = tcg_temp_new_i64();
11826 
11827             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11828             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11829             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11830             if (accum) {
11831                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11832                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11833             }
11834         }
11835     } else {
11836         for (pass = 0; pass < maxpass; pass++) {
11837             TCGv_i64 tcg_op = tcg_temp_new_i64();
11838             NeonGenOne64OpFn *genfn;
11839             static NeonGenOne64OpFn * const fns[2][2] = {
11840                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11841                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11842             };
11843 
11844             genfn = fns[size][u];
11845 
11846             tcg_res[pass] = tcg_temp_new_i64();
11847 
11848             read_vec_element(s, tcg_op, rn, pass, MO_64);
11849             genfn(tcg_res[pass], tcg_op);
11850 
11851             if (accum) {
11852                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11853                 if (size == 0) {
11854                     gen_helper_neon_addl_u16(tcg_res[pass],
11855                                              tcg_res[pass], tcg_op);
11856                 } else {
11857                     gen_helper_neon_addl_u32(tcg_res[pass],
11858                                              tcg_res[pass], tcg_op);
11859                 }
11860             }
11861         }
11862     }
11863     if (!is_q) {
11864         tcg_res[1] = tcg_constant_i64(0);
11865     }
11866     for (pass = 0; pass < 2; pass++) {
11867         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11868     }
11869 }
11870 
11871 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11872 {
11873     /* Implement SHLL and SHLL2 */
11874     int pass;
11875     int part = is_q ? 2 : 0;
11876     TCGv_i64 tcg_res[2];
11877 
11878     for (pass = 0; pass < 2; pass++) {
11879         static NeonGenWidenFn * const widenfns[3] = {
11880             gen_helper_neon_widen_u8,
11881             gen_helper_neon_widen_u16,
11882             tcg_gen_extu_i32_i64,
11883         };
11884         NeonGenWidenFn *widenfn = widenfns[size];
11885         TCGv_i32 tcg_op = tcg_temp_new_i32();
11886 
11887         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11888         tcg_res[pass] = tcg_temp_new_i64();
11889         widenfn(tcg_res[pass], tcg_op);
11890         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11891     }
11892 
11893     for (pass = 0; pass < 2; pass++) {
11894         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11895     }
11896 }
11897 
11898 /* AdvSIMD two reg misc
11899  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11900  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11901  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11902  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11903  */
11904 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11905 {
11906     int size = extract32(insn, 22, 2);
11907     int opcode = extract32(insn, 12, 5);
11908     bool u = extract32(insn, 29, 1);
11909     bool is_q = extract32(insn, 30, 1);
11910     int rn = extract32(insn, 5, 5);
11911     int rd = extract32(insn, 0, 5);
11912     bool need_fpstatus = false;
11913     int rmode = -1;
11914     TCGv_i32 tcg_rmode;
11915     TCGv_ptr tcg_fpstatus;
11916 
11917     switch (opcode) {
11918     case 0x0: /* REV64, REV32 */
11919     case 0x1: /* REV16 */
11920         handle_rev(s, opcode, u, is_q, size, rn, rd);
11921         return;
11922     case 0x5: /* CNT, NOT, RBIT */
11923         if (u && size == 0) {
11924             /* NOT */
11925             break;
11926         } else if (u && size == 1) {
11927             /* RBIT */
11928             break;
11929         } else if (!u && size == 0) {
11930             /* CNT */
11931             break;
11932         }
11933         unallocated_encoding(s);
11934         return;
11935     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11936     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11937         if (size == 3) {
11938             unallocated_encoding(s);
11939             return;
11940         }
11941         if (!fp_access_check(s)) {
11942             return;
11943         }
11944 
11945         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11946         return;
11947     case 0x4: /* CLS, CLZ */
11948         if (size == 3) {
11949             unallocated_encoding(s);
11950             return;
11951         }
11952         break;
11953     case 0x2: /* SADDLP, UADDLP */
11954     case 0x6: /* SADALP, UADALP */
11955         if (size == 3) {
11956             unallocated_encoding(s);
11957             return;
11958         }
11959         if (!fp_access_check(s)) {
11960             return;
11961         }
11962         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11963         return;
11964     case 0x13: /* SHLL, SHLL2 */
11965         if (u == 0 || size == 3) {
11966             unallocated_encoding(s);
11967             return;
11968         }
11969         if (!fp_access_check(s)) {
11970             return;
11971         }
11972         handle_shll(s, is_q, size, rn, rd);
11973         return;
11974     case 0xa: /* CMLT */
11975         if (u == 1) {
11976             unallocated_encoding(s);
11977             return;
11978         }
11979         /* fall through */
11980     case 0x8: /* CMGT, CMGE */
11981     case 0x9: /* CMEQ, CMLE */
11982     case 0xb: /* ABS, NEG */
11983         if (size == 3 && !is_q) {
11984             unallocated_encoding(s);
11985             return;
11986         }
11987         break;
11988     case 0x3: /* SUQADD, USQADD */
11989         if (size == 3 && !is_q) {
11990             unallocated_encoding(s);
11991             return;
11992         }
11993         if (!fp_access_check(s)) {
11994             return;
11995         }
11996         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11997         return;
11998     case 0x7: /* SQABS, SQNEG */
11999         if (size == 3 && !is_q) {
12000             unallocated_encoding(s);
12001             return;
12002         }
12003         break;
12004     case 0xc ... 0xf:
12005     case 0x16 ... 0x1f:
12006     {
12007         /* Floating point: U, size[1] and opcode indicate operation;
12008          * size[0] indicates single or double precision.
12009          */
12010         int is_double = extract32(size, 0, 1);
12011         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12012         size = is_double ? 3 : 2;
12013         switch (opcode) {
12014         case 0x2f: /* FABS */
12015         case 0x6f: /* FNEG */
12016             if (size == 3 && !is_q) {
12017                 unallocated_encoding(s);
12018                 return;
12019             }
12020             break;
12021         case 0x1d: /* SCVTF */
12022         case 0x5d: /* UCVTF */
12023         {
12024             bool is_signed = (opcode == 0x1d) ? true : false;
12025             int elements = is_double ? 2 : is_q ? 4 : 2;
12026             if (is_double && !is_q) {
12027                 unallocated_encoding(s);
12028                 return;
12029             }
12030             if (!fp_access_check(s)) {
12031                 return;
12032             }
12033             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12034             return;
12035         }
12036         case 0x2c: /* FCMGT (zero) */
12037         case 0x2d: /* FCMEQ (zero) */
12038         case 0x2e: /* FCMLT (zero) */
12039         case 0x6c: /* FCMGE (zero) */
12040         case 0x6d: /* FCMLE (zero) */
12041             if (size == 3 && !is_q) {
12042                 unallocated_encoding(s);
12043                 return;
12044             }
12045             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12046             return;
12047         case 0x7f: /* FSQRT */
12048             if (size == 3 && !is_q) {
12049                 unallocated_encoding(s);
12050                 return;
12051             }
12052             break;
12053         case 0x1a: /* FCVTNS */
12054         case 0x1b: /* FCVTMS */
12055         case 0x3a: /* FCVTPS */
12056         case 0x3b: /* FCVTZS */
12057         case 0x5a: /* FCVTNU */
12058         case 0x5b: /* FCVTMU */
12059         case 0x7a: /* FCVTPU */
12060         case 0x7b: /* FCVTZU */
12061             need_fpstatus = true;
12062             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12063             if (size == 3 && !is_q) {
12064                 unallocated_encoding(s);
12065                 return;
12066             }
12067             break;
12068         case 0x5c: /* FCVTAU */
12069         case 0x1c: /* FCVTAS */
12070             need_fpstatus = true;
12071             rmode = FPROUNDING_TIEAWAY;
12072             if (size == 3 && !is_q) {
12073                 unallocated_encoding(s);
12074                 return;
12075             }
12076             break;
12077         case 0x3c: /* URECPE */
12078             if (size == 3) {
12079                 unallocated_encoding(s);
12080                 return;
12081             }
12082             /* fall through */
12083         case 0x3d: /* FRECPE */
12084         case 0x7d: /* FRSQRTE */
12085             if (size == 3 && !is_q) {
12086                 unallocated_encoding(s);
12087                 return;
12088             }
12089             if (!fp_access_check(s)) {
12090                 return;
12091             }
12092             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12093             return;
12094         case 0x56: /* FCVTXN, FCVTXN2 */
12095             if (size == 2) {
12096                 unallocated_encoding(s);
12097                 return;
12098             }
12099             /* fall through */
12100         case 0x16: /* FCVTN, FCVTN2 */
12101             /* handle_2misc_narrow does a 2*size -> size operation, but these
12102              * instructions encode the source size rather than dest size.
12103              */
12104             if (!fp_access_check(s)) {
12105                 return;
12106             }
12107             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12108             return;
12109         case 0x36: /* BFCVTN, BFCVTN2 */
12110             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12111                 unallocated_encoding(s);
12112                 return;
12113             }
12114             if (!fp_access_check(s)) {
12115                 return;
12116             }
12117             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12118             return;
12119         case 0x17: /* FCVTL, FCVTL2 */
12120             if (!fp_access_check(s)) {
12121                 return;
12122             }
12123             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12124             return;
12125         case 0x18: /* FRINTN */
12126         case 0x19: /* FRINTM */
12127         case 0x38: /* FRINTP */
12128         case 0x39: /* FRINTZ */
12129             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12130             /* fall through */
12131         case 0x59: /* FRINTX */
12132         case 0x79: /* FRINTI */
12133             need_fpstatus = true;
12134             if (size == 3 && !is_q) {
12135                 unallocated_encoding(s);
12136                 return;
12137             }
12138             break;
12139         case 0x58: /* FRINTA */
12140             rmode = FPROUNDING_TIEAWAY;
12141             need_fpstatus = true;
12142             if (size == 3 && !is_q) {
12143                 unallocated_encoding(s);
12144                 return;
12145             }
12146             break;
12147         case 0x7c: /* URSQRTE */
12148             if (size == 3) {
12149                 unallocated_encoding(s);
12150                 return;
12151             }
12152             break;
12153         case 0x1e: /* FRINT32Z */
12154         case 0x1f: /* FRINT64Z */
12155             rmode = FPROUNDING_ZERO;
12156             /* fall through */
12157         case 0x5e: /* FRINT32X */
12158         case 0x5f: /* FRINT64X */
12159             need_fpstatus = true;
12160             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12161                 unallocated_encoding(s);
12162                 return;
12163             }
12164             break;
12165         default:
12166             unallocated_encoding(s);
12167             return;
12168         }
12169         break;
12170     }
12171     default:
12172         unallocated_encoding(s);
12173         return;
12174     }
12175 
12176     if (!fp_access_check(s)) {
12177         return;
12178     }
12179 
12180     if (need_fpstatus || rmode >= 0) {
12181         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12182     } else {
12183         tcg_fpstatus = NULL;
12184     }
12185     if (rmode >= 0) {
12186         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12187     } else {
12188         tcg_rmode = NULL;
12189     }
12190 
12191     switch (opcode) {
12192     case 0x5:
12193         if (u && size == 0) { /* NOT */
12194             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12195             return;
12196         }
12197         break;
12198     case 0x8: /* CMGT, CMGE */
12199         if (u) {
12200             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12201         } else {
12202             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12203         }
12204         return;
12205     case 0x9: /* CMEQ, CMLE */
12206         if (u) {
12207             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12208         } else {
12209             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12210         }
12211         return;
12212     case 0xa: /* CMLT */
12213         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12214         return;
12215     case 0xb:
12216         if (u) { /* ABS, NEG */
12217             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12218         } else {
12219             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12220         }
12221         return;
12222     }
12223 
12224     if (size == 3) {
12225         /* All 64-bit element operations can be shared with scalar 2misc */
12226         int pass;
12227 
12228         /* Coverity claims (size == 3 && !is_q) has been eliminated
12229          * from all paths leading to here.
12230          */
12231         tcg_debug_assert(is_q);
12232         for (pass = 0; pass < 2; pass++) {
12233             TCGv_i64 tcg_op = tcg_temp_new_i64();
12234             TCGv_i64 tcg_res = tcg_temp_new_i64();
12235 
12236             read_vec_element(s, tcg_op, rn, pass, MO_64);
12237 
12238             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12239                             tcg_rmode, tcg_fpstatus);
12240 
12241             write_vec_element(s, tcg_res, rd, pass, MO_64);
12242         }
12243     } else {
12244         int pass;
12245 
12246         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12247             TCGv_i32 tcg_op = tcg_temp_new_i32();
12248             TCGv_i32 tcg_res = tcg_temp_new_i32();
12249 
12250             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12251 
12252             if (size == 2) {
12253                 /* Special cases for 32 bit elements */
12254                 switch (opcode) {
12255                 case 0x4: /* CLS */
12256                     if (u) {
12257                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12258                     } else {
12259                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12260                     }
12261                     break;
12262                 case 0x7: /* SQABS, SQNEG */
12263                     if (u) {
12264                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
12265                     } else {
12266                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
12267                     }
12268                     break;
12269                 case 0x2f: /* FABS */
12270                     gen_helper_vfp_abss(tcg_res, tcg_op);
12271                     break;
12272                 case 0x6f: /* FNEG */
12273                     gen_helper_vfp_negs(tcg_res, tcg_op);
12274                     break;
12275                 case 0x7f: /* FSQRT */
12276                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
12277                     break;
12278                 case 0x1a: /* FCVTNS */
12279                 case 0x1b: /* FCVTMS */
12280                 case 0x1c: /* FCVTAS */
12281                 case 0x3a: /* FCVTPS */
12282                 case 0x3b: /* FCVTZS */
12283                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12284                                          tcg_constant_i32(0), tcg_fpstatus);
12285                     break;
12286                 case 0x5a: /* FCVTNU */
12287                 case 0x5b: /* FCVTMU */
12288                 case 0x5c: /* FCVTAU */
12289                 case 0x7a: /* FCVTPU */
12290                 case 0x7b: /* FCVTZU */
12291                     gen_helper_vfp_touls(tcg_res, tcg_op,
12292                                          tcg_constant_i32(0), tcg_fpstatus);
12293                     break;
12294                 case 0x18: /* FRINTN */
12295                 case 0x19: /* FRINTM */
12296                 case 0x38: /* FRINTP */
12297                 case 0x39: /* FRINTZ */
12298                 case 0x58: /* FRINTA */
12299                 case 0x79: /* FRINTI */
12300                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12301                     break;
12302                 case 0x59: /* FRINTX */
12303                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12304                     break;
12305                 case 0x7c: /* URSQRTE */
12306                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12307                     break;
12308                 case 0x1e: /* FRINT32Z */
12309                 case 0x5e: /* FRINT32X */
12310                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12311                     break;
12312                 case 0x1f: /* FRINT64Z */
12313                 case 0x5f: /* FRINT64X */
12314                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12315                     break;
12316                 default:
12317                     g_assert_not_reached();
12318                 }
12319             } else {
12320                 /* Use helpers for 8 and 16 bit elements */
12321                 switch (opcode) {
12322                 case 0x5: /* CNT, RBIT */
12323                     /* For these two insns size is part of the opcode specifier
12324                      * (handled earlier); they always operate on byte elements.
12325                      */
12326                     if (u) {
12327                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12328                     } else {
12329                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12330                     }
12331                     break;
12332                 case 0x7: /* SQABS, SQNEG */
12333                 {
12334                     NeonGenOneOpEnvFn *genfn;
12335                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12336                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12337                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12338                     };
12339                     genfn = fns[size][u];
12340                     genfn(tcg_res, tcg_env, tcg_op);
12341                     break;
12342                 }
12343                 case 0x4: /* CLS, CLZ */
12344                     if (u) {
12345                         if (size == 0) {
12346                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12347                         } else {
12348                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12349                         }
12350                     } else {
12351                         if (size == 0) {
12352                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12353                         } else {
12354                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12355                         }
12356                     }
12357                     break;
12358                 default:
12359                     g_assert_not_reached();
12360                 }
12361             }
12362 
12363             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12364         }
12365     }
12366     clear_vec_high(s, is_q, rd);
12367 
12368     if (tcg_rmode) {
12369         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12370     }
12371 }
12372 
12373 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12374  *
12375  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12376  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12377  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12378  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12379  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12380  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12381  *
12382  * This actually covers two groups where scalar access is governed by
12383  * bit 28. A bunch of the instructions (float to integral) only exist
12384  * in the vector form and are un-allocated for the scalar decode. Also
12385  * in the scalar decode Q is always 1.
12386  */
12387 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12388 {
12389     int fpop, opcode, a, u;
12390     int rn, rd;
12391     bool is_q;
12392     bool is_scalar;
12393     bool only_in_vector = false;
12394 
12395     int pass;
12396     TCGv_i32 tcg_rmode = NULL;
12397     TCGv_ptr tcg_fpstatus = NULL;
12398     bool need_fpst = true;
12399     int rmode = -1;
12400 
12401     if (!dc_isar_feature(aa64_fp16, s)) {
12402         unallocated_encoding(s);
12403         return;
12404     }
12405 
12406     rd = extract32(insn, 0, 5);
12407     rn = extract32(insn, 5, 5);
12408 
12409     a = extract32(insn, 23, 1);
12410     u = extract32(insn, 29, 1);
12411     is_scalar = extract32(insn, 28, 1);
12412     is_q = extract32(insn, 30, 1);
12413 
12414     opcode = extract32(insn, 12, 5);
12415     fpop = deposit32(opcode, 5, 1, a);
12416     fpop = deposit32(fpop, 6, 1, u);
12417 
12418     switch (fpop) {
12419     case 0x1d: /* SCVTF */
12420     case 0x5d: /* UCVTF */
12421     {
12422         int elements;
12423 
12424         if (is_scalar) {
12425             elements = 1;
12426         } else {
12427             elements = (is_q ? 8 : 4);
12428         }
12429 
12430         if (!fp_access_check(s)) {
12431             return;
12432         }
12433         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12434         return;
12435     }
12436     break;
12437     case 0x2c: /* FCMGT (zero) */
12438     case 0x2d: /* FCMEQ (zero) */
12439     case 0x2e: /* FCMLT (zero) */
12440     case 0x6c: /* FCMGE (zero) */
12441     case 0x6d: /* FCMLE (zero) */
12442         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12443         return;
12444     case 0x3d: /* FRECPE */
12445     case 0x3f: /* FRECPX */
12446         break;
12447     case 0x18: /* FRINTN */
12448         only_in_vector = true;
12449         rmode = FPROUNDING_TIEEVEN;
12450         break;
12451     case 0x19: /* FRINTM */
12452         only_in_vector = true;
12453         rmode = FPROUNDING_NEGINF;
12454         break;
12455     case 0x38: /* FRINTP */
12456         only_in_vector = true;
12457         rmode = FPROUNDING_POSINF;
12458         break;
12459     case 0x39: /* FRINTZ */
12460         only_in_vector = true;
12461         rmode = FPROUNDING_ZERO;
12462         break;
12463     case 0x58: /* FRINTA */
12464         only_in_vector = true;
12465         rmode = FPROUNDING_TIEAWAY;
12466         break;
12467     case 0x59: /* FRINTX */
12468     case 0x79: /* FRINTI */
12469         only_in_vector = true;
12470         /* current rounding mode */
12471         break;
12472     case 0x1a: /* FCVTNS */
12473         rmode = FPROUNDING_TIEEVEN;
12474         break;
12475     case 0x1b: /* FCVTMS */
12476         rmode = FPROUNDING_NEGINF;
12477         break;
12478     case 0x1c: /* FCVTAS */
12479         rmode = FPROUNDING_TIEAWAY;
12480         break;
12481     case 0x3a: /* FCVTPS */
12482         rmode = FPROUNDING_POSINF;
12483         break;
12484     case 0x3b: /* FCVTZS */
12485         rmode = FPROUNDING_ZERO;
12486         break;
12487     case 0x5a: /* FCVTNU */
12488         rmode = FPROUNDING_TIEEVEN;
12489         break;
12490     case 0x5b: /* FCVTMU */
12491         rmode = FPROUNDING_NEGINF;
12492         break;
12493     case 0x5c: /* FCVTAU */
12494         rmode = FPROUNDING_TIEAWAY;
12495         break;
12496     case 0x7a: /* FCVTPU */
12497         rmode = FPROUNDING_POSINF;
12498         break;
12499     case 0x7b: /* FCVTZU */
12500         rmode = FPROUNDING_ZERO;
12501         break;
12502     case 0x2f: /* FABS */
12503     case 0x6f: /* FNEG */
12504         need_fpst = false;
12505         break;
12506     case 0x7d: /* FRSQRTE */
12507     case 0x7f: /* FSQRT (vector) */
12508         break;
12509     default:
12510         unallocated_encoding(s);
12511         return;
12512     }
12513 
12514 
12515     /* Check additional constraints for the scalar encoding */
12516     if (is_scalar) {
12517         if (!is_q) {
12518             unallocated_encoding(s);
12519             return;
12520         }
12521         /* FRINTxx is only in the vector form */
12522         if (only_in_vector) {
12523             unallocated_encoding(s);
12524             return;
12525         }
12526     }
12527 
12528     if (!fp_access_check(s)) {
12529         return;
12530     }
12531 
12532     if (rmode >= 0 || need_fpst) {
12533         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12534     }
12535 
12536     if (rmode >= 0) {
12537         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12538     }
12539 
12540     if (is_scalar) {
12541         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12542         TCGv_i32 tcg_res = tcg_temp_new_i32();
12543 
12544         switch (fpop) {
12545         case 0x1a: /* FCVTNS */
12546         case 0x1b: /* FCVTMS */
12547         case 0x1c: /* FCVTAS */
12548         case 0x3a: /* FCVTPS */
12549         case 0x3b: /* FCVTZS */
12550             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12551             break;
12552         case 0x3d: /* FRECPE */
12553             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12554             break;
12555         case 0x3f: /* FRECPX */
12556             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12557             break;
12558         case 0x5a: /* FCVTNU */
12559         case 0x5b: /* FCVTMU */
12560         case 0x5c: /* FCVTAU */
12561         case 0x7a: /* FCVTPU */
12562         case 0x7b: /* FCVTZU */
12563             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12564             break;
12565         case 0x6f: /* FNEG */
12566             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12567             break;
12568         case 0x7d: /* FRSQRTE */
12569             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12570             break;
12571         default:
12572             g_assert_not_reached();
12573         }
12574 
12575         /* limit any sign extension going on */
12576         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12577         write_fp_sreg(s, rd, tcg_res);
12578     } else {
12579         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12580             TCGv_i32 tcg_op = tcg_temp_new_i32();
12581             TCGv_i32 tcg_res = tcg_temp_new_i32();
12582 
12583             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12584 
12585             switch (fpop) {
12586             case 0x1a: /* FCVTNS */
12587             case 0x1b: /* FCVTMS */
12588             case 0x1c: /* FCVTAS */
12589             case 0x3a: /* FCVTPS */
12590             case 0x3b: /* FCVTZS */
12591                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12592                 break;
12593             case 0x3d: /* FRECPE */
12594                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12595                 break;
12596             case 0x5a: /* FCVTNU */
12597             case 0x5b: /* FCVTMU */
12598             case 0x5c: /* FCVTAU */
12599             case 0x7a: /* FCVTPU */
12600             case 0x7b: /* FCVTZU */
12601                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12602                 break;
12603             case 0x18: /* FRINTN */
12604             case 0x19: /* FRINTM */
12605             case 0x38: /* FRINTP */
12606             case 0x39: /* FRINTZ */
12607             case 0x58: /* FRINTA */
12608             case 0x79: /* FRINTI */
12609                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12610                 break;
12611             case 0x59: /* FRINTX */
12612                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12613                 break;
12614             case 0x2f: /* FABS */
12615                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12616                 break;
12617             case 0x6f: /* FNEG */
12618                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12619                 break;
12620             case 0x7d: /* FRSQRTE */
12621                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12622                 break;
12623             case 0x7f: /* FSQRT */
12624                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12625                 break;
12626             default:
12627                 g_assert_not_reached();
12628             }
12629 
12630             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12631         }
12632 
12633         clear_vec_high(s, is_q, rd);
12634     }
12635 
12636     if (tcg_rmode) {
12637         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12638     }
12639 }
12640 
12641 /* AdvSIMD scalar x indexed element
12642  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12643  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12644  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12645  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12646  * AdvSIMD vector x indexed element
12647  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12648  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12649  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12650  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12651  */
12652 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12653 {
12654     /* This encoding has two kinds of instruction:
12655      *  normal, where we perform elt x idxelt => elt for each
12656      *     element in the vector
12657      *  long, where we perform elt x idxelt and generate a result of
12658      *     double the width of the input element
12659      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12660      */
12661     bool is_scalar = extract32(insn, 28, 1);
12662     bool is_q = extract32(insn, 30, 1);
12663     bool u = extract32(insn, 29, 1);
12664     int size = extract32(insn, 22, 2);
12665     int l = extract32(insn, 21, 1);
12666     int m = extract32(insn, 20, 1);
12667     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12668     int rm = extract32(insn, 16, 4);
12669     int opcode = extract32(insn, 12, 4);
12670     int h = extract32(insn, 11, 1);
12671     int rn = extract32(insn, 5, 5);
12672     int rd = extract32(insn, 0, 5);
12673     bool is_long = false;
12674     int is_fp = 0;
12675     bool is_fp16 = false;
12676     int index;
12677     TCGv_ptr fpst;
12678 
12679     switch (16 * u + opcode) {
12680     case 0x08: /* MUL */
12681     case 0x10: /* MLA */
12682     case 0x14: /* MLS */
12683         if (is_scalar) {
12684             unallocated_encoding(s);
12685             return;
12686         }
12687         break;
12688     case 0x02: /* SMLAL, SMLAL2 */
12689     case 0x12: /* UMLAL, UMLAL2 */
12690     case 0x06: /* SMLSL, SMLSL2 */
12691     case 0x16: /* UMLSL, UMLSL2 */
12692     case 0x0a: /* SMULL, SMULL2 */
12693     case 0x1a: /* UMULL, UMULL2 */
12694         if (is_scalar) {
12695             unallocated_encoding(s);
12696             return;
12697         }
12698         is_long = true;
12699         break;
12700     case 0x03: /* SQDMLAL, SQDMLAL2 */
12701     case 0x07: /* SQDMLSL, SQDMLSL2 */
12702     case 0x0b: /* SQDMULL, SQDMULL2 */
12703         is_long = true;
12704         break;
12705     case 0x0c: /* SQDMULH */
12706     case 0x0d: /* SQRDMULH */
12707         break;
12708     case 0x01: /* FMLA */
12709     case 0x05: /* FMLS */
12710     case 0x09: /* FMUL */
12711     case 0x19: /* FMULX */
12712         is_fp = 1;
12713         break;
12714     case 0x1d: /* SQRDMLAH */
12715     case 0x1f: /* SQRDMLSH */
12716         if (!dc_isar_feature(aa64_rdm, s)) {
12717             unallocated_encoding(s);
12718             return;
12719         }
12720         break;
12721     case 0x0e: /* SDOT */
12722     case 0x1e: /* UDOT */
12723         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12724             unallocated_encoding(s);
12725             return;
12726         }
12727         break;
12728     case 0x0f:
12729         switch (size) {
12730         case 0: /* SUDOT */
12731         case 2: /* USDOT */
12732             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12733                 unallocated_encoding(s);
12734                 return;
12735             }
12736             size = MO_32;
12737             break;
12738         case 1: /* BFDOT */
12739             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12740                 unallocated_encoding(s);
12741                 return;
12742             }
12743             size = MO_32;
12744             break;
12745         case 3: /* BFMLAL{B,T} */
12746             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12747                 unallocated_encoding(s);
12748                 return;
12749             }
12750             /* can't set is_fp without other incorrect size checks */
12751             size = MO_16;
12752             break;
12753         default:
12754             unallocated_encoding(s);
12755             return;
12756         }
12757         break;
12758     case 0x11: /* FCMLA #0 */
12759     case 0x13: /* FCMLA #90 */
12760     case 0x15: /* FCMLA #180 */
12761     case 0x17: /* FCMLA #270 */
12762         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12763             unallocated_encoding(s);
12764             return;
12765         }
12766         is_fp = 2;
12767         break;
12768     case 0x00: /* FMLAL */
12769     case 0x04: /* FMLSL */
12770     case 0x18: /* FMLAL2 */
12771     case 0x1c: /* FMLSL2 */
12772         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12773             unallocated_encoding(s);
12774             return;
12775         }
12776         size = MO_16;
12777         /* is_fp, but we pass tcg_env not fp_status.  */
12778         break;
12779     default:
12780         unallocated_encoding(s);
12781         return;
12782     }
12783 
12784     switch (is_fp) {
12785     case 1: /* normal fp */
12786         /* convert insn encoded size to MemOp size */
12787         switch (size) {
12788         case 0: /* half-precision */
12789             size = MO_16;
12790             is_fp16 = true;
12791             break;
12792         case MO_32: /* single precision */
12793         case MO_64: /* double precision */
12794             break;
12795         default:
12796             unallocated_encoding(s);
12797             return;
12798         }
12799         break;
12800 
12801     case 2: /* complex fp */
12802         /* Each indexable element is a complex pair.  */
12803         size += 1;
12804         switch (size) {
12805         case MO_32:
12806             if (h && !is_q) {
12807                 unallocated_encoding(s);
12808                 return;
12809             }
12810             is_fp16 = true;
12811             break;
12812         case MO_64:
12813             break;
12814         default:
12815             unallocated_encoding(s);
12816             return;
12817         }
12818         break;
12819 
12820     default: /* integer */
12821         switch (size) {
12822         case MO_8:
12823         case MO_64:
12824             unallocated_encoding(s);
12825             return;
12826         }
12827         break;
12828     }
12829     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12830         unallocated_encoding(s);
12831         return;
12832     }
12833 
12834     /* Given MemOp size, adjust register and indexing.  */
12835     switch (size) {
12836     case MO_16:
12837         index = h << 2 | l << 1 | m;
12838         break;
12839     case MO_32:
12840         index = h << 1 | l;
12841         rm |= m << 4;
12842         break;
12843     case MO_64:
12844         if (l || !is_q) {
12845             unallocated_encoding(s);
12846             return;
12847         }
12848         index = h;
12849         rm |= m << 4;
12850         break;
12851     default:
12852         g_assert_not_reached();
12853     }
12854 
12855     if (!fp_access_check(s)) {
12856         return;
12857     }
12858 
12859     if (is_fp) {
12860         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12861     } else {
12862         fpst = NULL;
12863     }
12864 
12865     switch (16 * u + opcode) {
12866     case 0x0e: /* SDOT */
12867     case 0x1e: /* UDOT */
12868         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12869                          u ? gen_helper_gvec_udot_idx_b
12870                          : gen_helper_gvec_sdot_idx_b);
12871         return;
12872     case 0x0f:
12873         switch (extract32(insn, 22, 2)) {
12874         case 0: /* SUDOT */
12875             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12876                              gen_helper_gvec_sudot_idx_b);
12877             return;
12878         case 1: /* BFDOT */
12879             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12880                              gen_helper_gvec_bfdot_idx);
12881             return;
12882         case 2: /* USDOT */
12883             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12884                              gen_helper_gvec_usdot_idx_b);
12885             return;
12886         case 3: /* BFMLAL{B,T} */
12887             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12888                               gen_helper_gvec_bfmlal_idx);
12889             return;
12890         }
12891         g_assert_not_reached();
12892     case 0x11: /* FCMLA #0 */
12893     case 0x13: /* FCMLA #90 */
12894     case 0x15: /* FCMLA #180 */
12895     case 0x17: /* FCMLA #270 */
12896         {
12897             int rot = extract32(insn, 13, 2);
12898             int data = (index << 2) | rot;
12899             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12900                                vec_full_reg_offset(s, rn),
12901                                vec_full_reg_offset(s, rm),
12902                                vec_full_reg_offset(s, rd), fpst,
12903                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12904                                size == MO_64
12905                                ? gen_helper_gvec_fcmlas_idx
12906                                : gen_helper_gvec_fcmlah_idx);
12907         }
12908         return;
12909 
12910     case 0x00: /* FMLAL */
12911     case 0x04: /* FMLSL */
12912     case 0x18: /* FMLAL2 */
12913     case 0x1c: /* FMLSL2 */
12914         {
12915             int is_s = extract32(opcode, 2, 1);
12916             int is_2 = u;
12917             int data = (index << 2) | (is_2 << 1) | is_s;
12918             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12919                                vec_full_reg_offset(s, rn),
12920                                vec_full_reg_offset(s, rm), tcg_env,
12921                                is_q ? 16 : 8, vec_full_reg_size(s),
12922                                data, gen_helper_gvec_fmlal_idx_a64);
12923         }
12924         return;
12925 
12926     case 0x08: /* MUL */
12927         if (!is_long && !is_scalar) {
12928             static gen_helper_gvec_3 * const fns[3] = {
12929                 gen_helper_gvec_mul_idx_h,
12930                 gen_helper_gvec_mul_idx_s,
12931                 gen_helper_gvec_mul_idx_d,
12932             };
12933             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
12934                                vec_full_reg_offset(s, rn),
12935                                vec_full_reg_offset(s, rm),
12936                                is_q ? 16 : 8, vec_full_reg_size(s),
12937                                index, fns[size - 1]);
12938             return;
12939         }
12940         break;
12941 
12942     case 0x10: /* MLA */
12943         if (!is_long && !is_scalar) {
12944             static gen_helper_gvec_4 * const fns[3] = {
12945                 gen_helper_gvec_mla_idx_h,
12946                 gen_helper_gvec_mla_idx_s,
12947                 gen_helper_gvec_mla_idx_d,
12948             };
12949             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12950                                vec_full_reg_offset(s, rn),
12951                                vec_full_reg_offset(s, rm),
12952                                vec_full_reg_offset(s, rd),
12953                                is_q ? 16 : 8, vec_full_reg_size(s),
12954                                index, fns[size - 1]);
12955             return;
12956         }
12957         break;
12958 
12959     case 0x14: /* MLS */
12960         if (!is_long && !is_scalar) {
12961             static gen_helper_gvec_4 * const fns[3] = {
12962                 gen_helper_gvec_mls_idx_h,
12963                 gen_helper_gvec_mls_idx_s,
12964                 gen_helper_gvec_mls_idx_d,
12965             };
12966             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12967                                vec_full_reg_offset(s, rn),
12968                                vec_full_reg_offset(s, rm),
12969                                vec_full_reg_offset(s, rd),
12970                                is_q ? 16 : 8, vec_full_reg_size(s),
12971                                index, fns[size - 1]);
12972             return;
12973         }
12974         break;
12975     }
12976 
12977     if (size == 3) {
12978         TCGv_i64 tcg_idx = tcg_temp_new_i64();
12979         int pass;
12980 
12981         assert(is_fp && is_q && !is_long);
12982 
12983         read_vec_element(s, tcg_idx, rm, index, MO_64);
12984 
12985         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12986             TCGv_i64 tcg_op = tcg_temp_new_i64();
12987             TCGv_i64 tcg_res = tcg_temp_new_i64();
12988 
12989             read_vec_element(s, tcg_op, rn, pass, MO_64);
12990 
12991             switch (16 * u + opcode) {
12992             case 0x05: /* FMLS */
12993                 /* As usual for ARM, separate negation for fused multiply-add */
12994                 gen_helper_vfp_negd(tcg_op, tcg_op);
12995                 /* fall through */
12996             case 0x01: /* FMLA */
12997                 read_vec_element(s, tcg_res, rd, pass, MO_64);
12998                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12999                 break;
13000             case 0x09: /* FMUL */
13001                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13002                 break;
13003             case 0x19: /* FMULX */
13004                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13005                 break;
13006             default:
13007                 g_assert_not_reached();
13008             }
13009 
13010             write_vec_element(s, tcg_res, rd, pass, MO_64);
13011         }
13012 
13013         clear_vec_high(s, !is_scalar, rd);
13014     } else if (!is_long) {
13015         /* 32 bit floating point, or 16 or 32 bit integer.
13016          * For the 16 bit scalar case we use the usual Neon helpers and
13017          * rely on the fact that 0 op 0 == 0 with no side effects.
13018          */
13019         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13020         int pass, maxpasses;
13021 
13022         if (is_scalar) {
13023             maxpasses = 1;
13024         } else {
13025             maxpasses = is_q ? 4 : 2;
13026         }
13027 
13028         read_vec_element_i32(s, tcg_idx, rm, index, size);
13029 
13030         if (size == 1 && !is_scalar) {
13031             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13032              * the index into both halves of the 32 bit tcg_idx and then use
13033              * the usual Neon helpers.
13034              */
13035             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13036         }
13037 
13038         for (pass = 0; pass < maxpasses; pass++) {
13039             TCGv_i32 tcg_op = tcg_temp_new_i32();
13040             TCGv_i32 tcg_res = tcg_temp_new_i32();
13041 
13042             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13043 
13044             switch (16 * u + opcode) {
13045             case 0x08: /* MUL */
13046             case 0x10: /* MLA */
13047             case 0x14: /* MLS */
13048             {
13049                 static NeonGenTwoOpFn * const fns[2][2] = {
13050                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13051                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13052                 };
13053                 NeonGenTwoOpFn *genfn;
13054                 bool is_sub = opcode == 0x4;
13055 
13056                 if (size == 1) {
13057                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13058                 } else {
13059                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13060                 }
13061                 if (opcode == 0x8) {
13062                     break;
13063                 }
13064                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13065                 genfn = fns[size - 1][is_sub];
13066                 genfn(tcg_res, tcg_op, tcg_res);
13067                 break;
13068             }
13069             case 0x05: /* FMLS */
13070             case 0x01: /* FMLA */
13071                 read_vec_element_i32(s, tcg_res, rd, pass,
13072                                      is_scalar ? size : MO_32);
13073                 switch (size) {
13074                 case 1:
13075                     if (opcode == 0x5) {
13076                         /* As usual for ARM, separate negation for fused
13077                          * multiply-add */
13078                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13079                     }
13080                     if (is_scalar) {
13081                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13082                                                    tcg_res, fpst);
13083                     } else {
13084                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13085                                                     tcg_res, fpst);
13086                     }
13087                     break;
13088                 case 2:
13089                     if (opcode == 0x5) {
13090                         /* As usual for ARM, separate negation for
13091                          * fused multiply-add */
13092                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13093                     }
13094                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13095                                            tcg_res, fpst);
13096                     break;
13097                 default:
13098                     g_assert_not_reached();
13099                 }
13100                 break;
13101             case 0x09: /* FMUL */
13102                 switch (size) {
13103                 case 1:
13104                     if (is_scalar) {
13105                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13106                                                 tcg_idx, fpst);
13107                     } else {
13108                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13109                                                  tcg_idx, fpst);
13110                     }
13111                     break;
13112                 case 2:
13113                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13114                     break;
13115                 default:
13116                     g_assert_not_reached();
13117                 }
13118                 break;
13119             case 0x19: /* FMULX */
13120                 switch (size) {
13121                 case 1:
13122                     if (is_scalar) {
13123                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13124                                                  tcg_idx, fpst);
13125                     } else {
13126                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13127                                                   tcg_idx, fpst);
13128                     }
13129                     break;
13130                 case 2:
13131                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13132                     break;
13133                 default:
13134                     g_assert_not_reached();
13135                 }
13136                 break;
13137             case 0x0c: /* SQDMULH */
13138                 if (size == 1) {
13139                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
13140                                                tcg_op, tcg_idx);
13141                 } else {
13142                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
13143                                                tcg_op, tcg_idx);
13144                 }
13145                 break;
13146             case 0x0d: /* SQRDMULH */
13147                 if (size == 1) {
13148                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
13149                                                 tcg_op, tcg_idx);
13150                 } else {
13151                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
13152                                                 tcg_op, tcg_idx);
13153                 }
13154                 break;
13155             case 0x1d: /* SQRDMLAH */
13156                 read_vec_element_i32(s, tcg_res, rd, pass,
13157                                      is_scalar ? size : MO_32);
13158                 if (size == 1) {
13159                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
13160                                                 tcg_op, tcg_idx, tcg_res);
13161                 } else {
13162                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
13163                                                 tcg_op, tcg_idx, tcg_res);
13164                 }
13165                 break;
13166             case 0x1f: /* SQRDMLSH */
13167                 read_vec_element_i32(s, tcg_res, rd, pass,
13168                                      is_scalar ? size : MO_32);
13169                 if (size == 1) {
13170                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
13171                                                 tcg_op, tcg_idx, tcg_res);
13172                 } else {
13173                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
13174                                                 tcg_op, tcg_idx, tcg_res);
13175                 }
13176                 break;
13177             default:
13178                 g_assert_not_reached();
13179             }
13180 
13181             if (is_scalar) {
13182                 write_fp_sreg(s, rd, tcg_res);
13183             } else {
13184                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13185             }
13186         }
13187 
13188         clear_vec_high(s, is_q, rd);
13189     } else {
13190         /* long ops: 16x16->32 or 32x32->64 */
13191         TCGv_i64 tcg_res[2];
13192         int pass;
13193         bool satop = extract32(opcode, 0, 1);
13194         MemOp memop = MO_32;
13195 
13196         if (satop || !u) {
13197             memop |= MO_SIGN;
13198         }
13199 
13200         if (size == 2) {
13201             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13202 
13203             read_vec_element(s, tcg_idx, rm, index, memop);
13204 
13205             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13206                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13207                 TCGv_i64 tcg_passres;
13208                 int passelt;
13209 
13210                 if (is_scalar) {
13211                     passelt = 0;
13212                 } else {
13213                     passelt = pass + (is_q * 2);
13214                 }
13215 
13216                 read_vec_element(s, tcg_op, rn, passelt, memop);
13217 
13218                 tcg_res[pass] = tcg_temp_new_i64();
13219 
13220                 if (opcode == 0xa || opcode == 0xb) {
13221                     /* Non-accumulating ops */
13222                     tcg_passres = tcg_res[pass];
13223                 } else {
13224                     tcg_passres = tcg_temp_new_i64();
13225                 }
13226 
13227                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13228 
13229                 if (satop) {
13230                     /* saturating, doubling */
13231                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
13232                                                       tcg_passres, tcg_passres);
13233                 }
13234 
13235                 if (opcode == 0xa || opcode == 0xb) {
13236                     continue;
13237                 }
13238 
13239                 /* Accumulating op: handle accumulate step */
13240                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13241 
13242                 switch (opcode) {
13243                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13244                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13245                     break;
13246                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13247                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13248                     break;
13249                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13250                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13251                     /* fall through */
13252                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13253                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
13254                                                       tcg_res[pass],
13255                                                       tcg_passres);
13256                     break;
13257                 default:
13258                     g_assert_not_reached();
13259                 }
13260             }
13261 
13262             clear_vec_high(s, !is_scalar, rd);
13263         } else {
13264             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13265 
13266             assert(size == 1);
13267             read_vec_element_i32(s, tcg_idx, rm, index, size);
13268 
13269             if (!is_scalar) {
13270                 /* The simplest way to handle the 16x16 indexed ops is to
13271                  * duplicate the index into both halves of the 32 bit tcg_idx
13272                  * and then use the usual Neon helpers.
13273                  */
13274                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13275             }
13276 
13277             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13278                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13279                 TCGv_i64 tcg_passres;
13280 
13281                 if (is_scalar) {
13282                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13283                 } else {
13284                     read_vec_element_i32(s, tcg_op, rn,
13285                                          pass + (is_q * 2), MO_32);
13286                 }
13287 
13288                 tcg_res[pass] = tcg_temp_new_i64();
13289 
13290                 if (opcode == 0xa || opcode == 0xb) {
13291                     /* Non-accumulating ops */
13292                     tcg_passres = tcg_res[pass];
13293                 } else {
13294                     tcg_passres = tcg_temp_new_i64();
13295                 }
13296 
13297                 if (memop & MO_SIGN) {
13298                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13299                 } else {
13300                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13301                 }
13302                 if (satop) {
13303                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
13304                                                       tcg_passres, tcg_passres);
13305                 }
13306 
13307                 if (opcode == 0xa || opcode == 0xb) {
13308                     continue;
13309                 }
13310 
13311                 /* Accumulating op: handle accumulate step */
13312                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13313 
13314                 switch (opcode) {
13315                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13316                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13317                                              tcg_passres);
13318                     break;
13319                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13320                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13321                                              tcg_passres);
13322                     break;
13323                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13324                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13325                     /* fall through */
13326                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13327                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
13328                                                       tcg_res[pass],
13329                                                       tcg_passres);
13330                     break;
13331                 default:
13332                     g_assert_not_reached();
13333                 }
13334             }
13335 
13336             if (is_scalar) {
13337                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13338             }
13339         }
13340 
13341         if (is_scalar) {
13342             tcg_res[1] = tcg_constant_i64(0);
13343         }
13344 
13345         for (pass = 0; pass < 2; pass++) {
13346             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13347         }
13348     }
13349 }
13350 
13351 /* Crypto AES
13352  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13353  * +-----------------+------+-----------+--------+-----+------+------+
13354  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13355  * +-----------------+------+-----------+--------+-----+------+------+
13356  */
13357 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13358 {
13359     int size = extract32(insn, 22, 2);
13360     int opcode = extract32(insn, 12, 5);
13361     int rn = extract32(insn, 5, 5);
13362     int rd = extract32(insn, 0, 5);
13363     gen_helper_gvec_2 *genfn2 = NULL;
13364     gen_helper_gvec_3 *genfn3 = NULL;
13365 
13366     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13367         unallocated_encoding(s);
13368         return;
13369     }
13370 
13371     switch (opcode) {
13372     case 0x4: /* AESE */
13373         genfn3 = gen_helper_crypto_aese;
13374         break;
13375     case 0x6: /* AESMC */
13376         genfn2 = gen_helper_crypto_aesmc;
13377         break;
13378     case 0x5: /* AESD */
13379         genfn3 = gen_helper_crypto_aesd;
13380         break;
13381     case 0x7: /* AESIMC */
13382         genfn2 = gen_helper_crypto_aesimc;
13383         break;
13384     default:
13385         unallocated_encoding(s);
13386         return;
13387     }
13388 
13389     if (!fp_access_check(s)) {
13390         return;
13391     }
13392     if (genfn2) {
13393         gen_gvec_op2_ool(s, true, rd, rn, 0, genfn2);
13394     } else {
13395         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, genfn3);
13396     }
13397 }
13398 
13399 /* Crypto three-reg SHA
13400  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13401  * +-----------------+------+---+------+---+--------+-----+------+------+
13402  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13403  * +-----------------+------+---+------+---+--------+-----+------+------+
13404  */
13405 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13406 {
13407     int size = extract32(insn, 22, 2);
13408     int opcode = extract32(insn, 12, 3);
13409     int rm = extract32(insn, 16, 5);
13410     int rn = extract32(insn, 5, 5);
13411     int rd = extract32(insn, 0, 5);
13412     gen_helper_gvec_3 *genfn;
13413     bool feature;
13414 
13415     if (size != 0) {
13416         unallocated_encoding(s);
13417         return;
13418     }
13419 
13420     switch (opcode) {
13421     case 0: /* SHA1C */
13422         genfn = gen_helper_crypto_sha1c;
13423         feature = dc_isar_feature(aa64_sha1, s);
13424         break;
13425     case 1: /* SHA1P */
13426         genfn = gen_helper_crypto_sha1p;
13427         feature = dc_isar_feature(aa64_sha1, s);
13428         break;
13429     case 2: /* SHA1M */
13430         genfn = gen_helper_crypto_sha1m;
13431         feature = dc_isar_feature(aa64_sha1, s);
13432         break;
13433     case 3: /* SHA1SU0 */
13434         genfn = gen_helper_crypto_sha1su0;
13435         feature = dc_isar_feature(aa64_sha1, s);
13436         break;
13437     case 4: /* SHA256H */
13438         genfn = gen_helper_crypto_sha256h;
13439         feature = dc_isar_feature(aa64_sha256, s);
13440         break;
13441     case 5: /* SHA256H2 */
13442         genfn = gen_helper_crypto_sha256h2;
13443         feature = dc_isar_feature(aa64_sha256, s);
13444         break;
13445     case 6: /* SHA256SU1 */
13446         genfn = gen_helper_crypto_sha256su1;
13447         feature = dc_isar_feature(aa64_sha256, s);
13448         break;
13449     default:
13450         unallocated_encoding(s);
13451         return;
13452     }
13453 
13454     if (!feature) {
13455         unallocated_encoding(s);
13456         return;
13457     }
13458 
13459     if (!fp_access_check(s)) {
13460         return;
13461     }
13462     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13463 }
13464 
13465 /* Crypto two-reg SHA
13466  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13467  * +-----------------+------+-----------+--------+-----+------+------+
13468  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13469  * +-----------------+------+-----------+--------+-----+------+------+
13470  */
13471 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13472 {
13473     int size = extract32(insn, 22, 2);
13474     int opcode = extract32(insn, 12, 5);
13475     int rn = extract32(insn, 5, 5);
13476     int rd = extract32(insn, 0, 5);
13477     gen_helper_gvec_2 *genfn;
13478     bool feature;
13479 
13480     if (size != 0) {
13481         unallocated_encoding(s);
13482         return;
13483     }
13484 
13485     switch (opcode) {
13486     case 0: /* SHA1H */
13487         feature = dc_isar_feature(aa64_sha1, s);
13488         genfn = gen_helper_crypto_sha1h;
13489         break;
13490     case 1: /* SHA1SU1 */
13491         feature = dc_isar_feature(aa64_sha1, s);
13492         genfn = gen_helper_crypto_sha1su1;
13493         break;
13494     case 2: /* SHA256SU0 */
13495         feature = dc_isar_feature(aa64_sha256, s);
13496         genfn = gen_helper_crypto_sha256su0;
13497         break;
13498     default:
13499         unallocated_encoding(s);
13500         return;
13501     }
13502 
13503     if (!feature) {
13504         unallocated_encoding(s);
13505         return;
13506     }
13507 
13508     if (!fp_access_check(s)) {
13509         return;
13510     }
13511     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13512 }
13513 
13514 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13515 {
13516     tcg_gen_rotli_i64(d, m, 1);
13517     tcg_gen_xor_i64(d, d, n);
13518 }
13519 
13520 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13521 {
13522     tcg_gen_rotli_vec(vece, d, m, 1);
13523     tcg_gen_xor_vec(vece, d, d, n);
13524 }
13525 
13526 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13527                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13528 {
13529     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13530     static const GVecGen3 op = {
13531         .fni8 = gen_rax1_i64,
13532         .fniv = gen_rax1_vec,
13533         .opt_opc = vecop_list,
13534         .fno = gen_helper_crypto_rax1,
13535         .vece = MO_64,
13536     };
13537     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13538 }
13539 
13540 /* Crypto three-reg SHA512
13541  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13542  * +-----------------------+------+---+---+-----+--------+------+------+
13543  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13544  * +-----------------------+------+---+---+-----+--------+------+------+
13545  */
13546 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13547 {
13548     int opcode = extract32(insn, 10, 2);
13549     int o =  extract32(insn, 14, 1);
13550     int rm = extract32(insn, 16, 5);
13551     int rn = extract32(insn, 5, 5);
13552     int rd = extract32(insn, 0, 5);
13553     bool feature;
13554     gen_helper_gvec_3 *oolfn = NULL;
13555     GVecGen3Fn *gvecfn = NULL;
13556 
13557     if (o == 0) {
13558         switch (opcode) {
13559         case 0: /* SHA512H */
13560             feature = dc_isar_feature(aa64_sha512, s);
13561             oolfn = gen_helper_crypto_sha512h;
13562             break;
13563         case 1: /* SHA512H2 */
13564             feature = dc_isar_feature(aa64_sha512, s);
13565             oolfn = gen_helper_crypto_sha512h2;
13566             break;
13567         case 2: /* SHA512SU1 */
13568             feature = dc_isar_feature(aa64_sha512, s);
13569             oolfn = gen_helper_crypto_sha512su1;
13570             break;
13571         case 3: /* RAX1 */
13572             feature = dc_isar_feature(aa64_sha3, s);
13573             gvecfn = gen_gvec_rax1;
13574             break;
13575         default:
13576             g_assert_not_reached();
13577         }
13578     } else {
13579         switch (opcode) {
13580         case 0: /* SM3PARTW1 */
13581             feature = dc_isar_feature(aa64_sm3, s);
13582             oolfn = gen_helper_crypto_sm3partw1;
13583             break;
13584         case 1: /* SM3PARTW2 */
13585             feature = dc_isar_feature(aa64_sm3, s);
13586             oolfn = gen_helper_crypto_sm3partw2;
13587             break;
13588         case 2: /* SM4EKEY */
13589             feature = dc_isar_feature(aa64_sm4, s);
13590             oolfn = gen_helper_crypto_sm4ekey;
13591             break;
13592         default:
13593             unallocated_encoding(s);
13594             return;
13595         }
13596     }
13597 
13598     if (!feature) {
13599         unallocated_encoding(s);
13600         return;
13601     }
13602 
13603     if (!fp_access_check(s)) {
13604         return;
13605     }
13606 
13607     if (oolfn) {
13608         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13609     } else {
13610         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13611     }
13612 }
13613 
13614 /* Crypto two-reg SHA512
13615  *  31                                     12  11  10  9    5 4    0
13616  * +-----------------------------------------+--------+------+------+
13617  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13618  * +-----------------------------------------+--------+------+------+
13619  */
13620 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13621 {
13622     int opcode = extract32(insn, 10, 2);
13623     int rn = extract32(insn, 5, 5);
13624     int rd = extract32(insn, 0, 5);
13625     bool feature;
13626 
13627     switch (opcode) {
13628     case 0: /* SHA512SU0 */
13629         feature = dc_isar_feature(aa64_sha512, s);
13630         break;
13631     case 1: /* SM4E */
13632         feature = dc_isar_feature(aa64_sm4, s);
13633         break;
13634     default:
13635         unallocated_encoding(s);
13636         return;
13637     }
13638 
13639     if (!feature) {
13640         unallocated_encoding(s);
13641         return;
13642     }
13643 
13644     if (!fp_access_check(s)) {
13645         return;
13646     }
13647 
13648     switch (opcode) {
13649     case 0: /* SHA512SU0 */
13650         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13651         break;
13652     case 1: /* SM4E */
13653         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13654         break;
13655     default:
13656         g_assert_not_reached();
13657     }
13658 }
13659 
13660 /* Crypto four-register
13661  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13662  * +-------------------+-----+------+---+------+------+------+
13663  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13664  * +-------------------+-----+------+---+------+------+------+
13665  */
13666 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13667 {
13668     int op0 = extract32(insn, 21, 2);
13669     int rm = extract32(insn, 16, 5);
13670     int ra = extract32(insn, 10, 5);
13671     int rn = extract32(insn, 5, 5);
13672     int rd = extract32(insn, 0, 5);
13673     bool feature;
13674 
13675     switch (op0) {
13676     case 0: /* EOR3 */
13677     case 1: /* BCAX */
13678         feature = dc_isar_feature(aa64_sha3, s);
13679         break;
13680     case 2: /* SM3SS1 */
13681         feature = dc_isar_feature(aa64_sm3, s);
13682         break;
13683     default:
13684         unallocated_encoding(s);
13685         return;
13686     }
13687 
13688     if (!feature) {
13689         unallocated_encoding(s);
13690         return;
13691     }
13692 
13693     if (!fp_access_check(s)) {
13694         return;
13695     }
13696 
13697     if (op0 < 2) {
13698         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13699         int pass;
13700 
13701         tcg_op1 = tcg_temp_new_i64();
13702         tcg_op2 = tcg_temp_new_i64();
13703         tcg_op3 = tcg_temp_new_i64();
13704         tcg_res[0] = tcg_temp_new_i64();
13705         tcg_res[1] = tcg_temp_new_i64();
13706 
13707         for (pass = 0; pass < 2; pass++) {
13708             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13709             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13710             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13711 
13712             if (op0 == 0) {
13713                 /* EOR3 */
13714                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13715             } else {
13716                 /* BCAX */
13717                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13718             }
13719             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13720         }
13721         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13722         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13723     } else {
13724         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13725 
13726         tcg_op1 = tcg_temp_new_i32();
13727         tcg_op2 = tcg_temp_new_i32();
13728         tcg_op3 = tcg_temp_new_i32();
13729         tcg_res = tcg_temp_new_i32();
13730         tcg_zero = tcg_constant_i32(0);
13731 
13732         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13733         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13734         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13735 
13736         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13737         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13738         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13739         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13740 
13741         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13742         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13743         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13744         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13745     }
13746 }
13747 
13748 /* Crypto XAR
13749  *  31                   21 20  16 15    10 9    5 4    0
13750  * +-----------------------+------+--------+------+------+
13751  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13752  * +-----------------------+------+--------+------+------+
13753  */
13754 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13755 {
13756     int rm = extract32(insn, 16, 5);
13757     int imm6 = extract32(insn, 10, 6);
13758     int rn = extract32(insn, 5, 5);
13759     int rd = extract32(insn, 0, 5);
13760 
13761     if (!dc_isar_feature(aa64_sha3, s)) {
13762         unallocated_encoding(s);
13763         return;
13764     }
13765 
13766     if (!fp_access_check(s)) {
13767         return;
13768     }
13769 
13770     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13771                  vec_full_reg_offset(s, rn),
13772                  vec_full_reg_offset(s, rm), imm6, 16,
13773                  vec_full_reg_size(s));
13774 }
13775 
13776 /* Crypto three-reg imm2
13777  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13778  * +-----------------------+------+-----+------+--------+------+------+
13779  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13780  * +-----------------------+------+-----+------+--------+------+------+
13781  */
13782 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13783 {
13784     static gen_helper_gvec_3 * const fns[4] = {
13785         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13786         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13787     };
13788     int opcode = extract32(insn, 10, 2);
13789     int imm2 = extract32(insn, 12, 2);
13790     int rm = extract32(insn, 16, 5);
13791     int rn = extract32(insn, 5, 5);
13792     int rd = extract32(insn, 0, 5);
13793 
13794     if (!dc_isar_feature(aa64_sm3, s)) {
13795         unallocated_encoding(s);
13796         return;
13797     }
13798 
13799     if (!fp_access_check(s)) {
13800         return;
13801     }
13802 
13803     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13804 }
13805 
13806 /* C3.6 Data processing - SIMD, inc Crypto
13807  *
13808  * As the decode gets a little complex we are using a table based
13809  * approach for this part of the decode.
13810  */
13811 static const AArch64DecodeTable data_proc_simd[] = {
13812     /* pattern  ,  mask     ,  fn                        */
13813     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13814     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13815     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13816     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13817     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13818     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13819     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13820     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13821     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13822     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13823     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13824     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13825     { 0x2e000000, 0xbf208400, disas_simd_ext },
13826     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13827     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13828     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13829     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13830     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13831     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13832     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13833     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13834     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13835     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13836     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13837     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13838     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13839     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13840     { 0xce800000, 0xffe00000, disas_crypto_xar },
13841     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13842     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13843     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13844     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13845     { 0x00000000, 0x00000000, NULL }
13846 };
13847 
13848 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13849 {
13850     /* Note that this is called with all non-FP cases from
13851      * table C3-6 so it must UNDEF for entries not specifically
13852      * allocated to instructions in that table.
13853      */
13854     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13855     if (fn) {
13856         fn(s, insn);
13857     } else {
13858         unallocated_encoding(s);
13859     }
13860 }
13861 
13862 /* C3.6 Data processing - SIMD and floating point */
13863 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13864 {
13865     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13866         disas_data_proc_fp(s, insn);
13867     } else {
13868         /* SIMD, including crypto */
13869         disas_data_proc_simd(s, insn);
13870     }
13871 }
13872 
13873 static bool trans_OK(DisasContext *s, arg_OK *a)
13874 {
13875     return true;
13876 }
13877 
13878 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13879 {
13880     s->is_nonstreaming = true;
13881     return true;
13882 }
13883 
13884 /**
13885  * is_guarded_page:
13886  * @env: The cpu environment
13887  * @s: The DisasContext
13888  *
13889  * Return true if the page is guarded.
13890  */
13891 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13892 {
13893     uint64_t addr = s->base.pc_first;
13894 #ifdef CONFIG_USER_ONLY
13895     return page_get_flags(addr) & PAGE_BTI;
13896 #else
13897     CPUTLBEntryFull *full;
13898     void *host;
13899     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13900     int flags;
13901 
13902     /*
13903      * We test this immediately after reading an insn, which means
13904      * that the TLB entry must be present and valid, and thus this
13905      * access will never raise an exception.
13906      */
13907     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13908                               false, &host, &full, 0);
13909     assert(!(flags & TLB_INVALID_MASK));
13910 
13911     return full->extra.arm.guarded;
13912 #endif
13913 }
13914 
13915 /**
13916  * btype_destination_ok:
13917  * @insn: The instruction at the branch destination
13918  * @bt: SCTLR_ELx.BT
13919  * @btype: PSTATE.BTYPE, and is non-zero
13920  *
13921  * On a guarded page, there are a limited number of insns
13922  * that may be present at the branch target:
13923  *   - branch target identifiers,
13924  *   - paciasp, pacibsp,
13925  *   - BRK insn
13926  *   - HLT insn
13927  * Anything else causes a Branch Target Exception.
13928  *
13929  * Return true if the branch is compatible, false to raise BTITRAP.
13930  */
13931 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13932 {
13933     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13934         /* HINT space */
13935         switch (extract32(insn, 5, 7)) {
13936         case 0b011001: /* PACIASP */
13937         case 0b011011: /* PACIBSP */
13938             /*
13939              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13940              * with btype == 3.  Otherwise all btype are ok.
13941              */
13942             return !bt || btype != 3;
13943         case 0b100000: /* BTI */
13944             /* Not compatible with any btype.  */
13945             return false;
13946         case 0b100010: /* BTI c */
13947             /* Not compatible with btype == 3 */
13948             return btype != 3;
13949         case 0b100100: /* BTI j */
13950             /* Not compatible with btype == 2 */
13951             return btype != 2;
13952         case 0b100110: /* BTI jc */
13953             /* Compatible with any btype.  */
13954             return true;
13955         }
13956     } else {
13957         switch (insn & 0xffe0001fu) {
13958         case 0xd4200000u: /* BRK */
13959         case 0xd4400000u: /* HLT */
13960             /* Give priority to the breakpoint exception.  */
13961             return true;
13962         }
13963     }
13964     return false;
13965 }
13966 
13967 /* C3.1 A64 instruction index by encoding */
13968 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13969 {
13970     switch (extract32(insn, 25, 4)) {
13971     case 0x5:
13972     case 0xd:      /* Data processing - register */
13973         disas_data_proc_reg(s, insn);
13974         break;
13975     case 0x7:
13976     case 0xf:      /* Data processing - SIMD and floating point */
13977         disas_data_proc_simd_fp(s, insn);
13978         break;
13979     default:
13980         unallocated_encoding(s);
13981         break;
13982     }
13983 }
13984 
13985 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13986                                           CPUState *cpu)
13987 {
13988     DisasContext *dc = container_of(dcbase, DisasContext, base);
13989     CPUARMState *env = cpu_env(cpu);
13990     ARMCPU *arm_cpu = env_archcpu(env);
13991     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13992     int bound, core_mmu_idx;
13993 
13994     dc->isar = &arm_cpu->isar;
13995     dc->condjmp = 0;
13996     dc->pc_save = dc->base.pc_first;
13997     dc->aarch64 = true;
13998     dc->thumb = false;
13999     dc->sctlr_b = 0;
14000     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14001     dc->condexec_mask = 0;
14002     dc->condexec_cond = 0;
14003     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14004     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14005     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14006     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14007     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14008     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14009 #if !defined(CONFIG_USER_ONLY)
14010     dc->user = (dc->current_el == 0);
14011 #endif
14012     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14013     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14014     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14015     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14016     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14017     dc->trap_eret = EX_TBFLAG_A64(tb_flags, TRAP_ERET);
14018     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14019     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14020     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14021     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14022     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14023     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14024     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14025     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14026     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
14027     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
14028     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14029     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14030     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14031     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14032     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14033     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14034     dc->nv = EX_TBFLAG_A64(tb_flags, NV);
14035     dc->vec_len = 0;
14036     dc->vec_stride = 0;
14037     dc->cp_regs = arm_cpu->cp_regs;
14038     dc->features = env->features;
14039     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14040     dc->gm_blocksize = arm_cpu->gm_blocksize;
14041 
14042 #ifdef CONFIG_USER_ONLY
14043     /* In sve_probe_page, we assume TBI is enabled. */
14044     tcg_debug_assert(dc->tbid & 1);
14045 #endif
14046 
14047     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14048 
14049     /* Single step state. The code-generation logic here is:
14050      *  SS_ACTIVE == 0:
14051      *   generate code with no special handling for single-stepping (except
14052      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14053      *   this happens anyway because those changes are all system register or
14054      *   PSTATE writes).
14055      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14056      *   emit code for one insn
14057      *   emit code to clear PSTATE.SS
14058      *   emit code to generate software step exception for completed step
14059      *   end TB (as usual for having generated an exception)
14060      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14061      *   emit code to generate a software step exception
14062      *   end the TB
14063      */
14064     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14065     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14066     dc->is_ldex = false;
14067 
14068     /* Bound the number of insns to execute to those left on the page.  */
14069     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14070 
14071     /* If architectural single step active, limit to 1.  */
14072     if (dc->ss_active) {
14073         bound = 1;
14074     }
14075     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14076 }
14077 
14078 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14079 {
14080 }
14081 
14082 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14083 {
14084     DisasContext *dc = container_of(dcbase, DisasContext, base);
14085     target_ulong pc_arg = dc->base.pc_next;
14086 
14087     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14088         pc_arg &= ~TARGET_PAGE_MASK;
14089     }
14090     tcg_gen_insn_start(pc_arg, 0, 0);
14091     dc->insn_start = tcg_last_op();
14092 }
14093 
14094 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14095 {
14096     DisasContext *s = container_of(dcbase, DisasContext, base);
14097     CPUARMState *env = cpu_env(cpu);
14098     uint64_t pc = s->base.pc_next;
14099     uint32_t insn;
14100 
14101     /* Singlestep exceptions have the highest priority. */
14102     if (s->ss_active && !s->pstate_ss) {
14103         /* Singlestep state is Active-pending.
14104          * If we're in this state at the start of a TB then either
14105          *  a) we just took an exception to an EL which is being debugged
14106          *     and this is the first insn in the exception handler
14107          *  b) debug exceptions were masked and we just unmasked them
14108          *     without changing EL (eg by clearing PSTATE.D)
14109          * In either case we're going to take a swstep exception in the
14110          * "did not step an insn" case, and so the syndrome ISV and EX
14111          * bits should be zero.
14112          */
14113         assert(s->base.num_insns == 1);
14114         gen_swstep_exception(s, 0, 0);
14115         s->base.is_jmp = DISAS_NORETURN;
14116         s->base.pc_next = pc + 4;
14117         return;
14118     }
14119 
14120     if (pc & 3) {
14121         /*
14122          * PC alignment fault.  This has priority over the instruction abort
14123          * that we would receive from a translation fault via arm_ldl_code.
14124          * This should only be possible after an indirect branch, at the
14125          * start of the TB.
14126          */
14127         assert(s->base.num_insns == 1);
14128         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
14129         s->base.is_jmp = DISAS_NORETURN;
14130         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14131         return;
14132     }
14133 
14134     s->pc_curr = pc;
14135     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14136     s->insn = insn;
14137     s->base.pc_next = pc + 4;
14138 
14139     s->fp_access_checked = false;
14140     s->sve_access_checked = false;
14141 
14142     if (s->pstate_il) {
14143         /*
14144          * Illegal execution state. This has priority over BTI
14145          * exceptions, but comes after instruction abort exceptions.
14146          */
14147         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14148         return;
14149     }
14150 
14151     if (dc_isar_feature(aa64_bti, s)) {
14152         if (s->base.num_insns == 1) {
14153             /*
14154              * At the first insn of the TB, compute s->guarded_page.
14155              * We delayed computing this until successfully reading
14156              * the first insn of the TB, above.  This (mostly) ensures
14157              * that the softmmu tlb entry has been populated, and the
14158              * page table GP bit is available.
14159              *
14160              * Note that we need to compute this even if btype == 0,
14161              * because this value is used for BR instructions later
14162              * where ENV is not available.
14163              */
14164             s->guarded_page = is_guarded_page(env, s);
14165 
14166             /* First insn can have btype set to non-zero.  */
14167             tcg_debug_assert(s->btype >= 0);
14168 
14169             /*
14170              * Note that the Branch Target Exception has fairly high
14171              * priority -- below debugging exceptions but above most
14172              * everything else.  This allows us to handle this now
14173              * instead of waiting until the insn is otherwise decoded.
14174              */
14175             if (s->btype != 0
14176                 && s->guarded_page
14177                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14178                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14179                 return;
14180             }
14181         } else {
14182             /* Not the first insn: btype must be 0.  */
14183             tcg_debug_assert(s->btype == 0);
14184         }
14185     }
14186 
14187     s->is_nonstreaming = false;
14188     if (s->sme_trap_nonstreaming) {
14189         disas_sme_fa64(s, insn);
14190     }
14191 
14192     if (!disas_a64(s, insn) &&
14193         !disas_sme(s, insn) &&
14194         !disas_sve(s, insn)) {
14195         disas_a64_legacy(s, insn);
14196     }
14197 
14198     /*
14199      * After execution of most insns, btype is reset to 0.
14200      * Note that we set btype == -1 when the insn sets btype.
14201      */
14202     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14203         reset_btype(s);
14204     }
14205 }
14206 
14207 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14208 {
14209     DisasContext *dc = container_of(dcbase, DisasContext, base);
14210 
14211     if (unlikely(dc->ss_active)) {
14212         /* Note that this means single stepping WFI doesn't halt the CPU.
14213          * For conditional branch insns this is harmless unreachable code as
14214          * gen_goto_tb() has already handled emitting the debug exception
14215          * (and thus a tb-jump is not possible when singlestepping).
14216          */
14217         switch (dc->base.is_jmp) {
14218         default:
14219             gen_a64_update_pc(dc, 4);
14220             /* fall through */
14221         case DISAS_EXIT:
14222         case DISAS_JUMP:
14223             gen_step_complete_exception(dc);
14224             break;
14225         case DISAS_NORETURN:
14226             break;
14227         }
14228     } else {
14229         switch (dc->base.is_jmp) {
14230         case DISAS_NEXT:
14231         case DISAS_TOO_MANY:
14232             gen_goto_tb(dc, 1, 4);
14233             break;
14234         default:
14235         case DISAS_UPDATE_EXIT:
14236             gen_a64_update_pc(dc, 4);
14237             /* fall through */
14238         case DISAS_EXIT:
14239             tcg_gen_exit_tb(NULL, 0);
14240             break;
14241         case DISAS_UPDATE_NOCHAIN:
14242             gen_a64_update_pc(dc, 4);
14243             /* fall through */
14244         case DISAS_JUMP:
14245             tcg_gen_lookup_and_goto_ptr();
14246             break;
14247         case DISAS_NORETURN:
14248         case DISAS_SWI:
14249             break;
14250         case DISAS_WFE:
14251             gen_a64_update_pc(dc, 4);
14252             gen_helper_wfe(tcg_env);
14253             break;
14254         case DISAS_YIELD:
14255             gen_a64_update_pc(dc, 4);
14256             gen_helper_yield(tcg_env);
14257             break;
14258         case DISAS_WFI:
14259             /*
14260              * This is a special case because we don't want to just halt
14261              * the CPU if trying to debug across a WFI.
14262              */
14263             gen_a64_update_pc(dc, 4);
14264             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
14265             /*
14266              * The helper doesn't necessarily throw an exception, but we
14267              * must go back to the main loop to check for interrupts anyway.
14268              */
14269             tcg_gen_exit_tb(NULL, 0);
14270             break;
14271         }
14272     }
14273 }
14274 
14275 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14276                                  CPUState *cpu, FILE *logfile)
14277 {
14278     DisasContext *dc = container_of(dcbase, DisasContext, base);
14279 
14280     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14281     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14282 }
14283 
14284 const TranslatorOps aarch64_translator_ops = {
14285     .init_disas_context = aarch64_tr_init_disas_context,
14286     .tb_start           = aarch64_tr_tb_start,
14287     .insn_start         = aarch64_tr_insn_start,
14288     .translate_insn     = aarch64_tr_translate_insn,
14289     .tb_stop            = aarch64_tr_tb_stop,
14290     .disas_log          = aarch64_tr_disas_log,
14291 };
14292